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