MetaData.h revision 8b938cdab5bd3d074d9b41bc2915fcfc11e47f27
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
30enum {
31    kKeyMIMEType         = 'mime',
32    kKeyWidth            = 'widt',
33    kKeyHeight           = 'heig',
34    kKeyChannelCount     = '#chn',
35    kKeySampleRate       = 'srte',
36    kKeyBitRate          = 'brte',
37    kKeyESDS             = 'esds',
38    kKeyAVCC             = 'avcc',
39    kKeyTimeUnits        = '#tim',
40    kKeyTimeScale        = 'scal',
41    kKeyNeedsNALFraming  = 'NALf',
42    kKeyIsSyncFrame      = 'sync',
43    kKeyDuration         = 'dura',
44    kKeyColorFormat      = 'colf',
45    kKeyPlatformPrivate  = 'priv',
46    kKeyDecoderComponent = 'decC',
47    kKeyBufferID         = 'bfID',
48};
49
50enum {
51    kTypeESDS        = 'esds',
52    kTypeAVCC        = 'avcc',
53};
54
55class MetaData : public RefBase {
56public:
57    MetaData();
58    MetaData(const MetaData &from);
59
60    enum Type {
61        TYPE_NONE     = 'none',
62        TYPE_C_STRING = 'cstr',
63        TYPE_INT32    = 'in32',
64        TYPE_FLOAT    = 'floa',
65        TYPE_POINTER  = 'ptr ',
66    };
67
68    void clear();
69    bool remove(uint32_t key);
70
71    bool setCString(uint32_t key, const char *value);
72    bool setInt32(uint32_t key, int32_t value);
73    bool setFloat(uint32_t key, float value);
74    bool setPointer(uint32_t key, void *value);
75
76    bool findCString(uint32_t key, const char **value);
77    bool findInt32(uint32_t key, int32_t *value);
78    bool findFloat(uint32_t key, float *value);
79    bool findPointer(uint32_t key, void **value);
80
81    bool setData(uint32_t key, uint32_t type, const void *data, size_t size);
82
83    bool findData(uint32_t key, uint32_t *type,
84                  const void **data, size_t *size) const;
85
86protected:
87    virtual ~MetaData();
88
89private:
90    struct typed_data {
91        typed_data();
92        ~typed_data();
93
94        typed_data(const MetaData::typed_data &);
95        typed_data &operator=(const MetaData::typed_data &);
96
97        void clear();
98        void setData(uint32_t type, const void *data, size_t size);
99        void getData(uint32_t *type, const void **data, size_t *size) const;
100
101    private:
102        uint32_t mType;
103        size_t mSize;
104
105        union {
106            void *ext_data;
107            float reservoir;
108        } u;
109
110        bool usesReservoir() const {
111            return mSize <= sizeof(u.reservoir);
112        }
113
114        void allocateStorage(size_t size);
115        void freeStorage();
116
117        void *storage() {
118            return usesReservoir() ? &u.reservoir : u.ext_data;
119        }
120
121        const void *storage() const {
122            return usesReservoir() ? &u.reservoir : u.ext_data;
123        }
124    };
125
126    KeyedVector<uint32_t, typed_data> mItems;
127
128    // MetaData &operator=(const MetaData &);
129};
130
131}  // namespace android
132
133#endif  // META_DATA_H_
134