Metadata.cpp revision 2959a5a5b4d151e14da3abe0289c15693409eb84
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//#define LOG_NDEBUG 0
18#define LOG_TAG "Metadata"
19#include <utils/Log.h>
20
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>
26
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.
30
31namespace {
32// All these constants below must be kept in sync with Metadata.java.
33enum MetadataId {
34    FIRST_SYSTEM_ID = 1,
35    LAST_SYSTEM_ID = 32,
36    FIRST_CUSTOM_ID = 8192
37};
38
39// Types
40enum Types {
41    STRING_VAL = 1,
42    INTEGER_VAL,
43    BOOLEAN_VAL,
44    LONG_VAL,
45    DOUBLE_VAL,
46    TIMED_TEXT_VAL,
47    DATE_VAL,
48    BYTE_ARRAY_VAL,
49};
50
51const size_t kRecordHeaderSize = 3 * sizeof(int32_t);
52const int32_t kMetaMarker = 0x4d455441;  // 'M' 'E' 'T' 'A'
53
54}  // anonymous namespace
55
56namespace android {
57namespace media {
58
59Metadata::Metadata(Parcel *p)
60    :mData(p),
61     mBegin(p->dataPosition()) { }
62
63Metadata::~Metadata() { }
64
65void Metadata::resetParcel()
66{
67    mData->setDataPosition(mBegin);
68}
69
70// Update the 4 bytes int at the beginning of the parcel which holds
71// the number of bytes written so far.
72void Metadata::updateLength()
73{
74    const size_t end = mData->dataPosition();
75
76    mData->setDataPosition(mBegin);
77    mData->writeInt32(end - mBegin);
78    mData->setDataPosition(end);
79}
80
81// Write the header. The java layer will look for the marker.
82bool Metadata::appendHeader()
83{
84    bool ok = true;
85
86    // Placeholder for the length of the metadata
87    ok = ok && mData->writeInt32(-1) == OK;
88    ok = ok && mData->writeInt32(kMetaMarker) == OK;
89    return ok;
90}
91
92bool Metadata::appendBool(int key, bool val)
93{
94    if (!checkKey(key)) {
95        return false;
96    }
97
98    const size_t begin = mData->dataPosition();
99    bool ok = true;
100
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;
106    if (!ok) {
107        mData->setDataPosition(begin);
108    }
109    return ok;
110}
111
112bool Metadata::appendInt32(int key, int32_t val)
113{
114    if (!checkKey(key)) {
115        return false;
116    }
117
118    const size_t begin = mData->dataPosition();
119    bool ok = true;
120
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;
126    if (!ok) {
127        mData->setDataPosition(begin);
128    }
129    return ok;
130}
131
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
134// (not allowed).
135bool Metadata::checkKey(int key)
136{
137    if (key < FIRST_SYSTEM_ID ||
138        (LAST_SYSTEM_ID < key && key < FIRST_CUSTOM_ID)) {
139        LOGE("Bad key %d", key);
140        return false;
141    }
142    size_t curr = mData->dataPosition();
143    // Loop over the keys to check if it has been used already.
144    mData->setDataPosition(mBegin);
145
146    bool error = false;
147    size_t left = curr - mBegin;
148    while (left > 0) {
149        size_t pos = mData->dataPosition();
150        size_t size = mData->readInt32();
151        if (size < kRecordHeaderSize || size > left) {
152            error = true;
153            break;
154        }
155        if (mData->readInt32() == key) {
156            LOGE("Key exists already %d", key);
157            error = true;
158            break;
159        }
160        mData->setDataPosition(pos + size);
161        left -= size;
162    }
163    mData->setDataPosition(curr);
164    return !error;
165}
166
167}  // namespace android::media
168}  // namespace android
169