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