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