1 /*
2 * Copyright 2012, 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 "MediaCodec-JNI"
19 #include <utils/Log.h>
20
21 #include <media/stagefright/foundation/ADebug.h>
22 #include <media/stagefright/foundation/AMessage.h>
23 #include <media/stagefright/MediaCodecList.h>
24 #include <media/IMediaCodecList.h>
25 #include <media/MediaCodecInfo.h>
26
27 #include <utils/Vector.h>
28
29 #include <mutex>
30 #include <vector>
31
32 #include "android_runtime/AndroidRuntime.h"
33 #include "jni.h"
34 #include <nativehelper/JNIHelp.h>
35 #include "android_media_Streams.h"
36
37 using namespace android;
38
39 /**
40 * This object unwraps codec aliases into individual codec infos as the Java interface handles
41 * aliases in this way.
42 */
43 class JavaMediaCodecListWrapper {
44 public:
45 struct Info {
46 sp<MediaCodecInfo> info;
47 AString alias;
48 };
49
getCodecInfo(size_t index) const50 const Info getCodecInfo(size_t index) const {
51 if (index < mInfoList.size()) {
52 return mInfoList[index];
53 }
54 // return
55 return Info { nullptr /* info */, "(none)" /* alias */ };
56 }
57
countCodecs() const58 size_t countCodecs() const {
59 return mInfoList.size();
60 }
61
getCodecList() const62 sp<IMediaCodecList> getCodecList() const {
63 return mCodecList;
64 }
65
findCodecByName(AString name) const66 size_t findCodecByName(AString name) const {
67 auto it = mInfoIndex.find(name);
68 return it == mInfoIndex.end() ? -ENOENT : it->second;
69 }
70
JavaMediaCodecListWrapper(sp<IMediaCodecList> mcl)71 JavaMediaCodecListWrapper(sp<IMediaCodecList> mcl)
72 : mCodecList(mcl) {
73 size_t numCodecs = mcl->countCodecs();
74 for (size_t ix = 0; ix < numCodecs; ++ix) {
75 sp<MediaCodecInfo> info = mcl->getCodecInfo(ix);
76 Vector<AString> namesAndAliases;
77 info->getAliases(&namesAndAliases);
78 namesAndAliases.insertAt(0);
79 namesAndAliases.editItemAt(0) = info->getCodecName();
80 for (const AString &nameOrAlias : namesAndAliases) {
81 if (mInfoIndex.count(nameOrAlias) > 0) {
82 // skip duplicate names or aliases
83 continue;
84 }
85 mInfoIndex.emplace(nameOrAlias, mInfoList.size());
86 mInfoList.emplace_back(Info { info, nameOrAlias });
87 }
88 }
89 }
90
91 private:
92 sp<IMediaCodecList> mCodecList;
93 std::vector<Info> mInfoList;
94 std::map<AString, size_t> mInfoIndex;
95 };
96
97 static std::mutex sMutex;
98 static std::unique_ptr<JavaMediaCodecListWrapper> sListWrapper;
99
getCodecList(JNIEnv * env)100 static const JavaMediaCodecListWrapper *getCodecList(JNIEnv *env) {
101 std::lock_guard<std::mutex> lock(sMutex);
102 if (sListWrapper == nullptr) {
103 sp<IMediaCodecList> mcl = MediaCodecList::getInstance();
104 if (mcl == NULL) {
105 // This should never happen unless something is really wrong
106 jniThrowException(
107 env, "java/lang/RuntimeException", "cannot get MediaCodecList");
108 }
109
110 sListWrapper.reset(new JavaMediaCodecListWrapper(mcl));
111 }
112 return sListWrapper.get();
113 }
114
getCodecInfo(JNIEnv * env,jint index)115 static JavaMediaCodecListWrapper::Info getCodecInfo(JNIEnv *env, jint index) {
116 const JavaMediaCodecListWrapper *mcl = getCodecList(env);
117 if (mcl == nullptr) {
118 // Runtime exception already pending.
119 return JavaMediaCodecListWrapper::Info { nullptr /* info */, "(none)" /* alias */ };
120 }
121
122 JavaMediaCodecListWrapper::Info info = mcl->getCodecInfo(index);
123 if (info.info == NULL) {
124 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
125 }
126
127 return info;
128 }
129
android_media_MediaCodecList_getCodecCount(JNIEnv * env,jobject)130 static jint android_media_MediaCodecList_getCodecCount(
131 JNIEnv *env, jobject /* thiz */) {
132 const JavaMediaCodecListWrapper *mcl = getCodecList(env);
133 if (mcl == NULL) {
134 // Runtime exception already pending.
135 return 0;
136 }
137
138 return mcl->countCodecs();
139 }
140
android_media_MediaCodecList_getCodecName(JNIEnv * env,jobject,jint index)141 static jstring android_media_MediaCodecList_getCodecName(
142 JNIEnv *env, jobject /* thiz */, jint index) {
143 JavaMediaCodecListWrapper::Info info = getCodecInfo(env, index);
144 if (info.info == NULL) {
145 // Runtime exception already pending.
146 return NULL;
147 }
148
149 const char *name = info.alias.c_str();
150 return env->NewStringUTF(name);
151 }
152
android_media_MediaCodecList_getCanonicalName(JNIEnv * env,jobject,jint index)153 static jstring android_media_MediaCodecList_getCanonicalName(
154 JNIEnv *env, jobject /* thiz */, jint index) {
155 JavaMediaCodecListWrapper::Info info = getCodecInfo(env, index);
156 if (info.info == NULL) {
157 // Runtime exception already pending.
158 return NULL;
159 }
160
161 const char *name = info.info->getCodecName();
162 return env->NewStringUTF(name);
163 }
164
android_media_MediaCodecList_findCodecByName(JNIEnv * env,jobject,jstring name)165 static jint android_media_MediaCodecList_findCodecByName(
166 JNIEnv *env, jobject /* thiz */, jstring name) {
167 if (name == NULL) {
168 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
169 return -ENOENT;
170 }
171
172 const char *nameStr = env->GetStringUTFChars(name, NULL);
173 if (nameStr == NULL) {
174 // Out of memory exception already pending.
175 return -ENOENT;
176 }
177
178 const JavaMediaCodecListWrapper *mcl = getCodecList(env);
179 if (mcl == NULL) {
180 // Runtime exception already pending.
181 env->ReleaseStringUTFChars(name, nameStr);
182 return -ENOENT;
183 }
184
185 jint ret = mcl->findCodecByName(nameStr);
186 env->ReleaseStringUTFChars(name, nameStr);
187 return ret;
188 }
189
android_media_MediaCodecList_getAttributes(JNIEnv * env,jobject,jint index)190 static jboolean android_media_MediaCodecList_getAttributes(
191 JNIEnv *env, jobject /* thiz */, jint index) {
192 JavaMediaCodecListWrapper::Info info = getCodecInfo(env, index);
193 if (info.info == NULL) {
194 // Runtime exception already pending.
195 return 0;
196 }
197
198 return info.info->getAttributes();
199 }
200
android_media_MediaCodecList_getSupportedTypes(JNIEnv * env,jobject,jint index)201 static jarray android_media_MediaCodecList_getSupportedTypes(
202 JNIEnv *env, jobject /* thiz */, jint index) {
203 JavaMediaCodecListWrapper::Info info = getCodecInfo(env, index);
204 if (info.info == NULL) {
205 // Runtime exception already pending.
206 return NULL;
207 }
208
209 Vector<AString> types;
210 info.info->getSupportedMediaTypes(&types);
211
212 jclass clazz = env->FindClass("java/lang/String");
213 CHECK(clazz != NULL);
214
215 jobjectArray array = env->NewObjectArray(types.size(), clazz, NULL);
216
217 for (size_t i = 0; i < types.size(); ++i) {
218 jstring obj = env->NewStringUTF(types.itemAt(i).c_str());
219 env->SetObjectArrayElement(array, i, obj);
220 env->DeleteLocalRef(obj);
221 obj = NULL;
222 }
223
224 return array;
225 }
226
android_media_MediaCodecList_getCodecCapabilities(JNIEnv * env,jobject,jint index,jstring type)227 static jobject android_media_MediaCodecList_getCodecCapabilities(
228 JNIEnv *env, jobject /* thiz */, jint index, jstring type) {
229 if (type == NULL) {
230 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
231 return NULL;
232 }
233
234 JavaMediaCodecListWrapper::Info info = getCodecInfo(env, index);
235 if (info.info == NULL) {
236 // Runtime exception already pending.
237 return NULL;
238 }
239
240
241 const char *typeStr = env->GetStringUTFChars(type, NULL);
242 if (typeStr == NULL) {
243 // Out of memory exception already pending.
244 return NULL;
245 }
246
247 Vector<MediaCodecInfo::ProfileLevel> profileLevels;
248 Vector<uint32_t> colorFormats;
249
250 sp<AMessage> defaultFormat = new AMessage();
251 defaultFormat->setString("mime", typeStr);
252
253 // TODO query default-format also from codec/codec list
254 const sp<MediaCodecInfo::Capabilities> &capabilities =
255 info.info->getCapabilitiesFor(typeStr);
256 env->ReleaseStringUTFChars(type, typeStr);
257 typeStr = NULL;
258 if (capabilities == NULL) {
259 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
260 return NULL;
261 }
262
263 capabilities->getSupportedColorFormats(&colorFormats);
264 capabilities->getSupportedProfileLevels(&profileLevels);
265 sp<AMessage> details = capabilities->getDetails();
266 bool isEncoder = info.info->isEncoder();
267
268 jobject defaultFormatObj = NULL;
269 if (ConvertMessageToMap(env, defaultFormat, &defaultFormatObj)) {
270 return NULL;
271 }
272
273 jobject infoObj = NULL;
274 if (ConvertMessageToMap(env, details, &infoObj)) {
275 env->DeleteLocalRef(defaultFormatObj);
276 return NULL;
277 }
278
279 jclass capsClazz =
280 env->FindClass("android/media/MediaCodecInfo$CodecCapabilities");
281 CHECK(capsClazz != NULL);
282
283 jclass profileLevelClazz =
284 env->FindClass("android/media/MediaCodecInfo$CodecProfileLevel");
285 CHECK(profileLevelClazz != NULL);
286
287 jobjectArray profileLevelArray =
288 env->NewObjectArray(profileLevels.size(), profileLevelClazz, NULL);
289
290 jfieldID profileField =
291 env->GetFieldID(profileLevelClazz, "profile", "I");
292
293 jfieldID levelField =
294 env->GetFieldID(profileLevelClazz, "level", "I");
295
296 for (size_t i = 0; i < profileLevels.size(); ++i) {
297 const MediaCodecInfo::ProfileLevel &src = profileLevels.itemAt(i);
298
299 jobject profileLevelObj = env->AllocObject(profileLevelClazz);
300
301 env->SetIntField(profileLevelObj, profileField, src.mProfile);
302 env->SetIntField(profileLevelObj, levelField, src.mLevel);
303
304 env->SetObjectArrayElement(profileLevelArray, i, profileLevelObj);
305
306 env->DeleteLocalRef(profileLevelObj);
307 profileLevelObj = NULL;
308 }
309
310 jintArray colorFormatsArray = env->NewIntArray(colorFormats.size());
311
312 for (size_t i = 0; i < colorFormats.size(); ++i) {
313 jint val = colorFormats.itemAt(i);
314 env->SetIntArrayRegion(colorFormatsArray, i, 1, &val);
315 }
316
317 jmethodID capsConstructID = env->GetMethodID(capsClazz, "<init>",
318 "([Landroid/media/MediaCodecInfo$CodecProfileLevel;[IZ"
319 "Ljava/util/Map;Ljava/util/Map;)V");
320
321 jobject caps = env->NewObject(capsClazz, capsConstructID,
322 profileLevelArray, colorFormatsArray, isEncoder,
323 defaultFormatObj, infoObj);
324
325 env->DeleteLocalRef(profileLevelArray);
326 profileLevelArray = NULL;
327
328 env->DeleteLocalRef(colorFormatsArray);
329 colorFormatsArray = NULL;
330
331 env->DeleteLocalRef(defaultFormatObj);
332 defaultFormatObj = NULL;
333
334 env->DeleteLocalRef(infoObj);
335 infoObj = NULL;
336
337 return caps;
338 }
339
android_media_MediaCodecList_getGlobalSettings(JNIEnv * env,jobject)340 static jobject android_media_MediaCodecList_getGlobalSettings(JNIEnv *env, jobject /* thiz */) {
341 const JavaMediaCodecListWrapper *mcl = getCodecList(env);
342 if (mcl == NULL) {
343 // Runtime exception already pending.
344 return NULL;
345 }
346
347 const sp<AMessage> settings = mcl->getCodecList()->getGlobalSettings();
348 if (settings == NULL) {
349 jniThrowException(env, "java/lang/RuntimeException", "cannot get global settings");
350 return NULL;
351 }
352
353 jobject settingsObj = NULL;
354 if (ConvertMessageToMap(env, settings, &settingsObj)) {
355 return NULL;
356 }
357
358 return settingsObj;
359 }
360
android_media_MediaCodecList_native_init(JNIEnv *)361 static void android_media_MediaCodecList_native_init(JNIEnv* /* env */) {
362 }
363
364 static const JNINativeMethod gMethods[] = {
365 { "native_getCodecCount", "()I", (void *)android_media_MediaCodecList_getCodecCount },
366
367 { "getCanonicalName", "(I)Ljava/lang/String;",
368 (void *)android_media_MediaCodecList_getCanonicalName },
369
370 { "getCodecName", "(I)Ljava/lang/String;",
371 (void *)android_media_MediaCodecList_getCodecName },
372
373 { "getAttributes", "(I)I", (void *)android_media_MediaCodecList_getAttributes },
374
375 { "getSupportedTypes", "(I)[Ljava/lang/String;",
376 (void *)android_media_MediaCodecList_getSupportedTypes },
377
378 { "getCodecCapabilities",
379 "(ILjava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;",
380 (void *)android_media_MediaCodecList_getCodecCapabilities },
381
382 { "native_getGlobalSettings",
383 "()Ljava/util/Map;",
384 (void *)android_media_MediaCodecList_getGlobalSettings },
385
386 { "findCodecByName", "(Ljava/lang/String;)I",
387 (void *)android_media_MediaCodecList_findCodecByName },
388
389 { "native_init", "()V", (void *)android_media_MediaCodecList_native_init },
390 };
391
register_android_media_MediaCodecList(JNIEnv * env)392 int register_android_media_MediaCodecList(JNIEnv *env) {
393 return AndroidRuntime::registerNativeMethods(env,
394 "android/media/MediaCodecList", gMethods, NELEM(gMethods));
395 }
396
397