2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Metadata"
19 #include <utils/Log.h>
21 #include <sys/types.h>
22 #include <media/Metadata.h>
23 #include <binder/Parcel.h>
24 #include <utils/Errors.h>
25 #include <utils/RefBase.h>
27 // This file contains code to serialize Metadata triples (key, type,
28 // value) into a parcel. The Parcel is destinated to be decoded by the
29 // Metadata.java class.
32 // All these constants below must be kept in sync with Metadata.java.
36 FIRST_CUSTOM_ID = 8192
51 const size_t kRecordHeaderSize = 3 * sizeof(int32_t);
52 const int32_t kMetaMarker = 0x4d455441; // 'M' 'E' 'T' 'A'
54 } // anonymous namespace
59 Metadata::Metadata(Parcel *p)
61 mBegin(p->dataPosition()) { }
63 Metadata::~Metadata() { }
65 void Metadata::resetParcel()
67 mData->setDataPosition(mBegin);
70 // Update the 4 bytes int at the beginning of the parcel which holds
71 // the number of bytes written so far.
72 void Metadata::updateLength()
74 const size_t end = mData->dataPosition();
76 mData->setDataPosition(mBegin);
77 mData->writeInt32(end - mBegin);
78 mData->setDataPosition(end);
81 // Write the header. The java layer will look for the marker.
82 bool Metadata::appendHeader()
86 // Placeholder for the length of the metadata
87 ok = ok && mData->writeInt32(-1) == OK;
88 ok = ok && mData->writeInt32(kMetaMarker) == OK;
92 bool Metadata::appendBool(int key, bool val)
98 const size_t begin = mData->dataPosition();
101 // 4 int32s: size, key, type, value.
102 ok = ok && mData->writeInt32(4 * sizeof(int32_t)) == OK;
103 ok = ok && mData->writeInt32(key) == OK;
104 ok = ok && mData->writeInt32(BOOLEAN_VAL) == OK;
105 ok = ok && mData->writeInt32(val ? 1 : 0) == OK;
107 mData->setDataPosition(begin);
112 bool Metadata::appendInt32(int key, int32_t val)
114 if (!checkKey(key)) {
118 const size_t begin = mData->dataPosition();
121 // 4 int32s: size, key, type, value.
122 ok = ok && mData->writeInt32(4 * sizeof(int32_t)) == OK;
123 ok = ok && mData->writeInt32(key) == OK;
124 ok = ok && mData->writeInt32(INTEGER_VAL) == OK;
125 ok = ok && mData->writeInt32(val) == OK;
127 mData->setDataPosition(begin);
132 // Check the key (i.e metadata id) is valid if it is a system one.
133 // Loop over all the exiting ones in the Parcel to check for duplicate
135 bool Metadata::checkKey(int key)
137 if (key < FIRST_SYSTEM_ID ||
138 (LAST_SYSTEM_ID < key && key < FIRST_CUSTOM_ID)) {
139 LOGE("Bad key %d", key);
142 size_t curr = mData->dataPosition();
143 // Loop over the keys to check if it has been used already.
144 mData->setDataPosition(mBegin);
147 size_t left = curr - mBegin;
149 size_t pos = mData->dataPosition();
150 size_t size = mData->readInt32();
151 if (size < kRecordHeaderSize || size > left) {
155 if (mData->readInt32() == key) {
156 LOGE("Key exists already %d", key);
160 mData->setDataPosition(pos + size);
163 mData->setDataPosition(curr);
167 } // namespace android::media
168 } // namespace android