MetaData.h revision 66ac4df65516ebfd0e500bfca75dc4b5ef8d674e
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef META_DATA_H_
18
19#define META_DATA_H_
20
21#include <sys/types.h>
22
23#include <stdint.h>
24
25#include <utils/RefBase.h>
26#include <utils/KeyedVector.h>
27
28namespace android {
29
30// The following keys map to int32_t data unless indicated otherwise.
31enum {
32    kKeyMIMEType          = 'mime',  // cstring
33    kKeyWidth             = 'widt',
34    kKeyHeight            = 'heig',
35    kKeyChannelCount      = '#chn',
36    kKeySampleRate        = 'srte',
37    kKeyBitRate           = 'brte',  // int32_t (bps)
38    kKeyESDS              = 'esds',  // raw data
39    kKeyAVCC              = 'avcc',  // raw data
40    kKeyWantsNALFragments = 'NALf',
41    kKeyIsSyncFrame       = 'sync',  // int32_t (bool)
42    kKeyIsCodecConfig     = 'conf',  // int32_t (bool)
43    kKeyTime              = 'time',  // int64_t (usecs)
44    kKeyDuration          = 'dura',  // int64_t (usecs)
45    kKeyColorFormat       = 'colf',
46    kKeyPlatformPrivate   = 'priv',  // pointer
47    kKeyDecoderComponent  = 'decC',  // cstring
48    kKeyBufferID          = 'bfID',
49    kKeyMaxInputSize      = 'inpS',
50    kKeyThumbnailTime     = 'thbT',  // int64_t (usecs)
51
52    kKeyAlbum             = 'albu',  // cstring
53    kKeyArtist            = 'arti',  // cstring
54    kKeyAlbumArtist       = 'aart',  // cstring
55    kKeyComposer          = 'comp',  // cstring
56    kKeyGenre             = 'genr',  // cstring
57    kKeyTitle             = 'titl',  // cstring
58    kKeyYear              = 'year',  // cstring
59    kKeyAlbumArt          = 'albA',  // compressed image data
60    kKeyAlbumArtMIME      = 'alAM',  // cstring
61    kKeyAuthor            = 'auth',  // cstring
62    kKeyCDTrackNumber     = 'cdtr',  // cstring
63    kKeyDiscNumber        = 'dnum',  // cstring
64    kKeyDate              = 'date',  // cstring
65    kKeyWriter            = 'writ',  // cstring
66};
67
68enum {
69    kTypeESDS        = 'esds',
70    kTypeAVCC        = 'avcc',
71};
72
73class MetaData : public RefBase {
74public:
75    MetaData();
76    MetaData(const MetaData &from);
77
78    enum Type {
79        TYPE_NONE     = 'none',
80        TYPE_C_STRING = 'cstr',
81        TYPE_INT32    = 'in32',
82        TYPE_INT64    = 'in64',
83        TYPE_FLOAT    = 'floa',
84        TYPE_POINTER  = 'ptr ',
85    };
86
87    void clear();
88    bool remove(uint32_t key);
89
90    bool setCString(uint32_t key, const char *value);
91    bool setInt32(uint32_t key, int32_t value);
92    bool setInt64(uint32_t key, int64_t value);
93    bool setFloat(uint32_t key, float value);
94    bool setPointer(uint32_t key, void *value);
95
96    bool findCString(uint32_t key, const char **value);
97    bool findInt32(uint32_t key, int32_t *value);
98    bool findInt64(uint32_t key, int64_t *value);
99    bool findFloat(uint32_t key, float *value);
100    bool findPointer(uint32_t key, void **value);
101
102    bool setData(uint32_t key, uint32_t type, const void *data, size_t size);
103
104    bool findData(uint32_t key, uint32_t *type,
105                  const void **data, size_t *size) const;
106
107protected:
108    virtual ~MetaData();
109
110private:
111    struct typed_data {
112        typed_data();
113        ~typed_data();
114
115        typed_data(const MetaData::typed_data &);
116        typed_data &operator=(const MetaData::typed_data &);
117
118        void clear();
119        void setData(uint32_t type, const void *data, size_t size);
120        void getData(uint32_t *type, const void **data, size_t *size) const;
121
122    private:
123        uint32_t mType;
124        size_t mSize;
125
126        union {
127            void *ext_data;
128            float reservoir;
129        } u;
130
131        bool usesReservoir() const {
132            return mSize <= sizeof(u.reservoir);
133        }
134
135        void allocateStorage(size_t size);
136        void freeStorage();
137
138        void *storage() {
139            return usesReservoir() ? &u.reservoir : u.ext_data;
140        }
141
142        const void *storage() const {
143            return usesReservoir() ? &u.reservoir : u.ext_data;
144        }
145    };
146
147    KeyedVector<uint32_t, typed_data> mItems;
148
149    // MetaData &operator=(const MetaData &);
150};
151
152}  // namespace android
153
154#endif  // META_DATA_H_
155