Home | History | Annotate | Download | only in ndk
      1 /*
      2  * Copyright (C) 2014 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 "NdkMediaFormat"
     19 
     20 #include <inttypes.h>
     21 
     22 #include "NdkMediaFormat.h"
     23 
     24 #include <utils/Log.h>
     25 #include <utils/StrongPointer.h>
     26 #include <media/stagefright/foundation/ABuffer.h>
     27 #include <media/stagefright/foundation/AMessage.h>
     28 #include <media/stagefright/MetaData.h>
     29 #include <android_runtime/AndroidRuntime.h>
     30 #include <android_util_Binder.h>
     31 
     32 #include <jni.h>
     33 
     34 using namespace android;
     35 
     36 struct AMediaFormat {
     37     sp<AMessage> mFormat;
     38     String8 mDebug;
     39     KeyedVector<String8, String8> mStringCache;
     40 };
     41 
     42 extern "C" {
     43 
     44 // private functions for conversion to/from AMessage
     45 AMediaFormat* AMediaFormat_fromMsg(const void* data) {
     46     ALOGV("private ctor");
     47     AMediaFormat* mData = new AMediaFormat();
     48     mData->mFormat = *((sp<AMessage>*)data);
     49     return mData;
     50 }
     51 
     52 void AMediaFormat_getFormat(const AMediaFormat* mData, void* dest) {
     53     *((sp<AMessage>*)dest) = mData->mFormat;
     54 }
     55 
     56 
     57 /*
     58  * public function follow
     59  */
     60 EXPORT
     61 AMediaFormat *AMediaFormat_new() {
     62     ALOGV("ctor");
     63     sp<AMessage> msg = new AMessage();
     64     return AMediaFormat_fromMsg(&msg);
     65 }
     66 
     67 EXPORT
     68 media_status_t AMediaFormat_delete(AMediaFormat *mData) {
     69     ALOGV("dtor");
     70     delete mData;
     71     return AMEDIA_OK;
     72 }
     73 
     74 
     75 EXPORT
     76 const char* AMediaFormat_toString(AMediaFormat *mData) {
     77     sp<AMessage> f = mData->mFormat;
     78     String8 ret;
     79     int num = f->countEntries();
     80     for (int i = 0; i < num; i++) {
     81         if (i != 0) {
     82             ret.append(", ");
     83         }
     84         AMessage::Type t;
     85         const char *name = f->getEntryNameAt(i, &t);
     86         ret.append(name);
     87         ret.append(": ");
     88         switch (t) {
     89             case AMessage::kTypeInt32:
     90             {
     91                 int32_t val;
     92                 f->findInt32(name, &val);
     93                 ret.appendFormat("int32(%" PRId32 ")", val);
     94                 break;
     95             }
     96             case AMessage::kTypeInt64:
     97             {
     98                 int64_t val;
     99                 f->findInt64(name, &val);
    100                 ret.appendFormat("int64(%" PRId64 ")", val);
    101                 break;
    102             }
    103             case AMessage::kTypeSize:
    104             {
    105                 size_t val;
    106                 f->findSize(name, &val);
    107                 ret.appendFormat("size_t(%zu)", val);
    108                 break;
    109             }
    110             case AMessage::kTypeFloat:
    111             {
    112                 float val;
    113                 f->findFloat(name, &val);
    114                 ret.appendFormat("float(%f)", val);
    115                 break;
    116             }
    117             case AMessage::kTypeDouble:
    118             {
    119                 double val;
    120                 f->findDouble(name, &val);
    121                 ret.appendFormat("double(%f)", val);
    122                 break;
    123             }
    124             case AMessage::kTypeString:
    125             {
    126                 AString val;
    127                 f->findString(name, &val);
    128                 ret.appendFormat("string(%s)", val.c_str());
    129                 break;
    130             }
    131             case AMessage::kTypeBuffer:
    132             {
    133                 ret.appendFormat("data");
    134                 break;
    135             }
    136             default:
    137             {
    138                 ret.appendFormat("unknown(%d)", t);
    139                 break;
    140             }
    141         }
    142     }
    143     ret.append("}");
    144     mData->mDebug = ret;
    145     return mData->mDebug.string();
    146 }
    147 
    148 EXPORT
    149 bool AMediaFormat_getInt32(AMediaFormat* format, const char *name, int32_t *out) {
    150     return format->mFormat->findInt32(name, out);
    151 }
    152 
    153 EXPORT
    154 bool AMediaFormat_getInt64(AMediaFormat* format, const char *name, int64_t *out) {
    155     return format->mFormat->findInt64(name, out);
    156 }
    157 
    158 EXPORT
    159 bool AMediaFormat_getFloat(AMediaFormat* format, const char *name, float *out) {
    160     return format->mFormat->findFloat(name, out);
    161 }
    162 
    163 EXPORT
    164 bool AMediaFormat_getSize(AMediaFormat* format, const char *name, size_t *out) {
    165     return format->mFormat->findSize(name, out);
    166 }
    167 
    168 EXPORT
    169 bool AMediaFormat_getBuffer(AMediaFormat* format, const char *name, void** data, size_t *outsize) {
    170     sp<ABuffer> buf;
    171     if (format->mFormat->findBuffer(name, &buf)) {
    172         *data = buf->data() + buf->offset();
    173         *outsize = buf->size();
    174         return true;
    175     }
    176     return false;
    177 }
    178 
    179 EXPORT
    180 bool AMediaFormat_getString(AMediaFormat* mData, const char *name, const char **out) {
    181 
    182     for (size_t i = 0; i < mData->mStringCache.size(); i++) {
    183         if (strcmp(mData->mStringCache.keyAt(i).string(), name) == 0) {
    184             mData->mStringCache.removeItemsAt(i, 1);
    185             break;
    186         }
    187     }
    188 
    189     AString tmp;
    190     if (mData->mFormat->findString(name, &tmp)) {
    191         String8 ret(tmp.c_str());
    192         mData->mStringCache.add(String8(name), ret);
    193         *out = ret.string();
    194         return true;
    195     }
    196     return false;
    197 }
    198 
    199 EXPORT
    200 void AMediaFormat_setInt32(AMediaFormat* format, const char *name, int32_t value) {
    201     format->mFormat->setInt32(name, value);
    202 }
    203 
    204 EXPORT
    205 void AMediaFormat_setInt64(AMediaFormat* format, const char *name, int64_t value) {
    206     format->mFormat->setInt64(name, value);
    207 }
    208 
    209 EXPORT
    210 void AMediaFormat_setFloat(AMediaFormat* format, const char* name, float value) {
    211     format->mFormat->setFloat(name, value);
    212 }
    213 
    214 EXPORT
    215 void AMediaFormat_setString(AMediaFormat* format, const char* name, const char* value) {
    216     // AMessage::setString() makes a copy of the string
    217     format->mFormat->setString(name, value, strlen(value));
    218 }
    219 
    220 EXPORT
    221 void AMediaFormat_setBuffer(AMediaFormat* format, const char* name, void* data, size_t size) {
    222     // the ABuffer(void*, size_t) constructor doesn't take ownership of the data, so create
    223     // a new buffer and copy the data into it
    224     sp<ABuffer> buf = new ABuffer(size);
    225     memcpy(buf->data(), data, size);
    226     buf->setRange(0, size);
    227     // AMessage::setBuffer() increases the refcount of the buffer
    228     format->mFormat->setBuffer(name, buf);
    229 }
    230 
    231 
    232 EXPORT const char* AMEDIAFORMAT_KEY_AAC_PROFILE = "aac-profile";
    233 EXPORT const char* AMEDIAFORMAT_KEY_BIT_RATE = "bitrate";
    234 EXPORT const char* AMEDIAFORMAT_KEY_CHANNEL_COUNT = "channel-count";
    235 EXPORT const char* AMEDIAFORMAT_KEY_CHANNEL_MASK = "channel-mask";
    236 EXPORT const char* AMEDIAFORMAT_KEY_COLOR_FORMAT = "color-format";
    237 EXPORT const char* AMEDIAFORMAT_KEY_DURATION = "durationUs";
    238 EXPORT const char* AMEDIAFORMAT_KEY_FLAC_COMPRESSION_LEVEL = "flac-compression-level";
    239 EXPORT const char* AMEDIAFORMAT_KEY_FRAME_RATE = "frame-rate";
    240 EXPORT const char* AMEDIAFORMAT_KEY_HEIGHT = "height";
    241 EXPORT const char* AMEDIAFORMAT_KEY_IS_ADTS = "is-adts";
    242 EXPORT const char* AMEDIAFORMAT_KEY_IS_AUTOSELECT = "is-autoselect";
    243 EXPORT const char* AMEDIAFORMAT_KEY_IS_DEFAULT = "is-default";
    244 EXPORT const char* AMEDIAFORMAT_KEY_IS_FORCED_SUBTITLE = "is-forced-subtitle";
    245 EXPORT const char* AMEDIAFORMAT_KEY_I_FRAME_INTERVAL = "i-frame-interval";
    246 EXPORT const char* AMEDIAFORMAT_KEY_LANGUAGE = "language";
    247 EXPORT const char* AMEDIAFORMAT_KEY_MAX_HEIGHT = "max-height";
    248 EXPORT const char* AMEDIAFORMAT_KEY_MAX_INPUT_SIZE = "max-input-size";
    249 EXPORT const char* AMEDIAFORMAT_KEY_MAX_WIDTH = "max-width";
    250 EXPORT const char* AMEDIAFORMAT_KEY_MIME = "mime";
    251 EXPORT const char* AMEDIAFORMAT_KEY_PUSH_BLANK_BUFFERS_ON_STOP = "push-blank-buffers-on-shutdown";
    252 EXPORT const char* AMEDIAFORMAT_KEY_REPEAT_PREVIOUS_FRAME_AFTER = "repeat-previous-frame-after";
    253 EXPORT const char* AMEDIAFORMAT_KEY_SAMPLE_RATE = "sample-rate";
    254 EXPORT const char* AMEDIAFORMAT_KEY_WIDTH = "width";
    255 EXPORT const char* AMEDIAFORMAT_KEY_STRIDE = "stride";
    256 
    257 
    258 } // extern "C"
    259 
    260 
    261