MetaData.h revision 2dec2b5be2056c6d9428897dc672185872d30d17
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',  // int32_t
34    kKeyHeight            = 'heig',  // int32_t
35    kKeyIFramesInterval   = 'ifiv',  // int32_t
36    kKeyStride            = 'strd',  // int32_t
37    kKeySliceHeight       = 'slht',  // int32_t
38    kKeyChannelCount      = '#chn',  // int32_t
39    kKeySampleRate        = 'srte',  // int32_t (also video frame rate)
40    kKeyBitRate           = 'brte',  // int32_t (bps)
41    kKeyESDS              = 'esds',  // raw data
42    kKeyAVCC              = 'avcc',  // raw data
43    kKeyVorbisInfo        = 'vinf',  // raw data
44    kKeyVorbisBooks       = 'vboo',  // raw data
45    kKeyWantsNALFragments = 'NALf',
46    kKey64BitFileOffset   = 'fobt',  // int32_t (bool)
47    kKeyIsSyncFrame       = 'sync',  // int32_t (bool)
48    kKeyIsCodecConfig     = 'conf',  // int32_t (bool)
49    kKeyTime              = 'time',  // int64_t (usecs)
50    kKeyDuration          = 'dura',  // int64_t (usecs)
51    kKeyColorFormat       = 'colf',
52    kKeyPlatformPrivate   = 'priv',  // pointer
53    kKeyDecoderComponent  = 'decC',  // cstring
54    kKeyBufferID          = 'bfID',
55    kKeyMaxInputSize      = 'inpS',
56    kKeyThumbnailTime     = 'thbT',  // int64_t (usecs)
57
58    kKeyAlbum             = 'albu',  // cstring
59    kKeyArtist            = 'arti',  // cstring
60    kKeyAlbumArtist       = 'aart',  // cstring
61    kKeyComposer          = 'comp',  // cstring
62    kKeyGenre             = 'genr',  // cstring
63    kKeyTitle             = 'titl',  // cstring
64    kKeyYear              = 'year',  // cstring
65    kKeyAlbumArt          = 'albA',  // compressed image data
66    kKeyAlbumArtMIME      = 'alAM',  // cstring
67    kKeyAuthor            = 'auth',  // cstring
68    kKeyCDTrackNumber     = 'cdtr',  // cstring
69    kKeyDiscNumber        = 'dnum',  // cstring
70    kKeyDate              = 'date',  // cstring
71    kKeyWriter            = 'writ',  // cstring
72};
73
74enum {
75    kTypeESDS        = 'esds',
76    kTypeAVCC        = 'avcc',
77};
78
79class MetaData : public RefBase {
80public:
81    MetaData();
82    MetaData(const MetaData &from);
83
84    enum Type {
85        TYPE_NONE     = 'none',
86        TYPE_C_STRING = 'cstr',
87        TYPE_INT32    = 'in32',
88        TYPE_INT64    = 'in64',
89        TYPE_FLOAT    = 'floa',
90        TYPE_POINTER  = 'ptr ',
91    };
92
93    void clear();
94    bool remove(uint32_t key);
95
96    bool setCString(uint32_t key, const char *value);
97    bool setInt32(uint32_t key, int32_t value);
98    bool setInt64(uint32_t key, int64_t value);
99    bool setFloat(uint32_t key, float value);
100    bool setPointer(uint32_t key, void *value);
101
102    bool findCString(uint32_t key, const char **value);
103    bool findInt32(uint32_t key, int32_t *value);
104    bool findInt64(uint32_t key, int64_t *value);
105    bool findFloat(uint32_t key, float *value);
106    bool findPointer(uint32_t key, void **value);
107
108    bool setData(uint32_t key, uint32_t type, const void *data, size_t size);
109
110    bool findData(uint32_t key, uint32_t *type,
111                  const void **data, size_t *size) const;
112
113protected:
114    virtual ~MetaData();
115
116private:
117    struct typed_data {
118        typed_data();
119        ~typed_data();
120
121        typed_data(const MetaData::typed_data &);
122        typed_data &operator=(const MetaData::typed_data &);
123
124        void clear();
125        void setData(uint32_t type, const void *data, size_t size);
126        void getData(uint32_t *type, const void **data, size_t *size) const;
127
128    private:
129        uint32_t mType;
130        size_t mSize;
131
132        union {
133            void *ext_data;
134            float reservoir;
135        } u;
136
137        bool usesReservoir() const {
138            return mSize <= sizeof(u.reservoir);
139        }
140
141        void allocateStorage(size_t size);
142        void freeStorage();
143
144        void *storage() {
145            return usesReservoir() ? &u.reservoir : u.ext_data;
146        }
147
148        const void *storage() const {
149            return usesReservoir() ? &u.reservoir : u.ext_data;
150        }
151    };
152
153    KeyedVector<uint32_t, typed_data> mItems;
154
155    // MetaData &operator=(const MetaData &);
156};
157
158}  // namespace android
159
160#endif  // META_DATA_H_
161