MetaData.h revision bd4bc59262df90dc85752684a1d4a92833a535a3
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    kKeyComposer          = 'comp',  // cstring
55    kKeyGenre             = 'genr',  // cstring
56    kKeyTitle             = 'titl',  // cstring
57    kKeyYear              = 'year',  // cstring
58    kKeyAlbumArt          = 'albA',  // compressed image data
59    kKeyAlbumArtMIME      = 'alAM',  // cstring
60    kKeyAuthor            = 'auth',  // cstring
61    kKeyCDTrackNumber     = 'cdtr',  // cstring
62    kKeyDate              = 'date',  // cstring
63};
64
65enum {
66    kTypeESDS        = 'esds',
67    kTypeAVCC        = 'avcc',
68};
69
70class MetaData : public RefBase {
71public:
72    MetaData();
73    MetaData(const MetaData &from);
74
75    enum Type {
76        TYPE_NONE     = 'none',
77        TYPE_C_STRING = 'cstr',
78        TYPE_INT32    = 'in32',
79        TYPE_INT64    = 'in64',
80        TYPE_FLOAT    = 'floa',
81        TYPE_POINTER  = 'ptr ',
82    };
83
84    void clear();
85    bool remove(uint32_t key);
86
87    bool setCString(uint32_t key, const char *value);
88    bool setInt32(uint32_t key, int32_t value);
89    bool setInt64(uint32_t key, int64_t value);
90    bool setFloat(uint32_t key, float value);
91    bool setPointer(uint32_t key, void *value);
92
93    bool findCString(uint32_t key, const char **value);
94    bool findInt32(uint32_t key, int32_t *value);
95    bool findInt64(uint32_t key, int64_t *value);
96    bool findFloat(uint32_t key, float *value);
97    bool findPointer(uint32_t key, void **value);
98
99    bool setData(uint32_t key, uint32_t type, const void *data, size_t size);
100
101    bool findData(uint32_t key, uint32_t *type,
102                  const void **data, size_t *size) const;
103
104protected:
105    virtual ~MetaData();
106
107private:
108    struct typed_data {
109        typed_data();
110        ~typed_data();
111
112        typed_data(const MetaData::typed_data &);
113        typed_data &operator=(const MetaData::typed_data &);
114
115        void clear();
116        void setData(uint32_t type, const void *data, size_t size);
117        void getData(uint32_t *type, const void **data, size_t *size) const;
118
119    private:
120        uint32_t mType;
121        size_t mSize;
122
123        union {
124            void *ext_data;
125            float reservoir;
126        } u;
127
128        bool usesReservoir() const {
129            return mSize <= sizeof(u.reservoir);
130        }
131
132        void allocateStorage(size_t size);
133        void freeStorage();
134
135        void *storage() {
136            return usesReservoir() ? &u.reservoir : u.ext_data;
137        }
138
139        const void *storage() const {
140            return usesReservoir() ? &u.reservoir : u.ext_data;
141        }
142    };
143
144    KeyedVector<uint32_t, typed_data> mItems;
145
146    // MetaData &operator=(const MetaData &);
147};
148
149}  // namespace android
150
151#endif  // META_DATA_H_
152