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
34using namespace android;
35
36struct AMediaFormat {
37    sp<AMessage> mFormat;
38    String8 mDebug;
39    KeyedVector<String8, String8> mStringCache;
40};
41
42extern "C" {
43
44// private functions for conversion to/from AMessage
45AMediaFormat* 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
52void AMediaFormat_getFormat(const AMediaFormat* mData, void* dest) {
53    *((sp<AMessage>*)dest) = mData->mFormat;
54}
55
56
57/*
58 * public function follow
59 */
60EXPORT
61AMediaFormat *AMediaFormat_new() {
62    ALOGV("ctor");
63    sp<AMessage> msg = new AMessage();
64    return AMediaFormat_fromMsg(&msg);
65}
66
67EXPORT
68media_status_t AMediaFormat_delete(AMediaFormat *mData) {
69    ALOGV("dtor");
70    delete mData;
71    return AMEDIA_OK;
72}
73
74
75EXPORT
76const 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
148EXPORT
149bool AMediaFormat_getInt32(AMediaFormat* format, const char *name, int32_t *out) {
150    return format->mFormat->findInt32(name, out);
151}
152
153EXPORT
154bool AMediaFormat_getInt64(AMediaFormat* format, const char *name, int64_t *out) {
155    return format->mFormat->findInt64(name, out);
156}
157
158EXPORT
159bool AMediaFormat_getFloat(AMediaFormat* format, const char *name, float *out) {
160    return format->mFormat->findFloat(name, out);
161}
162
163EXPORT
164bool AMediaFormat_getSize(AMediaFormat* format, const char *name, size_t *out) {
165    return format->mFormat->findSize(name, out);
166}
167
168EXPORT
169bool 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
179EXPORT
180bool 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
199EXPORT
200void AMediaFormat_setInt32(AMediaFormat* format, const char *name, int32_t value) {
201    format->mFormat->setInt32(name, value);
202}
203
204EXPORT
205void AMediaFormat_setInt64(AMediaFormat* format, const char *name, int64_t value) {
206    format->mFormat->setInt64(name, value);
207}
208
209EXPORT
210void AMediaFormat_setFloat(AMediaFormat* format, const char* name, float value) {
211    format->mFormat->setFloat(name, value);
212}
213
214EXPORT
215void 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
220EXPORT
221void 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
232EXPORT const char* AMEDIAFORMAT_KEY_AAC_PROFILE = "aac-profile";
233EXPORT const char* AMEDIAFORMAT_KEY_BIT_RATE = "bitrate";
234EXPORT const char* AMEDIAFORMAT_KEY_CHANNEL_COUNT = "channel-count";
235EXPORT const char* AMEDIAFORMAT_KEY_CHANNEL_MASK = "channel-mask";
236EXPORT const char* AMEDIAFORMAT_KEY_COLOR_FORMAT = "color-format";
237EXPORT const char* AMEDIAFORMAT_KEY_DURATION = "durationUs";
238EXPORT const char* AMEDIAFORMAT_KEY_FLAC_COMPRESSION_LEVEL = "flac-compression-level";
239EXPORT const char* AMEDIAFORMAT_KEY_FRAME_RATE = "frame-rate";
240EXPORT const char* AMEDIAFORMAT_KEY_HEIGHT = "height";
241EXPORT const char* AMEDIAFORMAT_KEY_IS_ADTS = "is-adts";
242EXPORT const char* AMEDIAFORMAT_KEY_IS_AUTOSELECT = "is-autoselect";
243EXPORT const char* AMEDIAFORMAT_KEY_IS_DEFAULT = "is-default";
244EXPORT const char* AMEDIAFORMAT_KEY_IS_FORCED_SUBTITLE = "is-forced-subtitle";
245EXPORT const char* AMEDIAFORMAT_KEY_I_FRAME_INTERVAL = "i-frame-interval";
246EXPORT const char* AMEDIAFORMAT_KEY_LANGUAGE = "language";
247EXPORT const char* AMEDIAFORMAT_KEY_MAX_HEIGHT = "max-height";
248EXPORT const char* AMEDIAFORMAT_KEY_MAX_INPUT_SIZE = "max-input-size";
249EXPORT const char* AMEDIAFORMAT_KEY_MAX_WIDTH = "max-width";
250EXPORT const char* AMEDIAFORMAT_KEY_MIME = "mime";
251EXPORT const char* AMEDIAFORMAT_KEY_PUSH_BLANK_BUFFERS_ON_STOP = "push-blank-buffers-on-shutdown";
252EXPORT const char* AMEDIAFORMAT_KEY_REPEAT_PREVIOUS_FRAME_AFTER = "repeat-previous-frame-after";
253EXPORT const char* AMEDIAFORMAT_KEY_SAMPLE_RATE = "sample-rate";
254EXPORT const char* AMEDIAFORMAT_KEY_WIDTH = "width";
255EXPORT const char* AMEDIAFORMAT_KEY_STRIDE = "stride";
256
257
258} // extern "C"
259
260
261