2 * Copyright 2012, The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "MediaCodec-JNI"
19 #include <utils/Log.h>
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>
27 #include "android_runtime/AndroidRuntime.h"
29 #include <nativehelper/JNIHelp.h>
30 #include "android_media_Utils.h"
32 using namespace android;
34 static sp<IMediaCodecList> getCodecList(JNIEnv *env) {
35 sp<IMediaCodecList> mcl = MediaCodecList::getInstance();
37 // This should never happen unless something is really wrong
39 env, "java/lang/RuntimeException", "cannot get MediaCodecList");
44 static jint android_media_MediaCodecList_getCodecCount(
45 JNIEnv *env, jobject /* thiz */) {
46 sp<IMediaCodecList> mcl = getCodecList(env);
48 // Runtime exception already pending.
51 return mcl->countCodecs();
54 static jstring android_media_MediaCodecList_getCodecName(
55 JNIEnv *env, jobject /* thiz */, jint index) {
56 sp<IMediaCodecList> mcl = getCodecList(env);
58 // Runtime exception already pending.
62 const sp<MediaCodecInfo> &info = mcl->getCodecInfo(index);
64 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
68 const char *name = info->getCodecName();
69 return env->NewStringUTF(name);
72 static jint android_media_MediaCodecList_findCodecByName(
73 JNIEnv *env, jobject /* thiz */, jstring name) {
75 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
79 const char *nameStr = env->GetStringUTFChars(name, NULL);
80 if (nameStr == NULL) {
81 // Out of memory exception already pending.
85 sp<IMediaCodecList> mcl = getCodecList(env);
87 // Runtime exception already pending.
88 env->ReleaseStringUTFChars(name, nameStr);
92 jint ret = mcl->findCodecByName(nameStr);
93 env->ReleaseStringUTFChars(name, nameStr);
97 static jboolean android_media_MediaCodecList_isEncoder(
98 JNIEnv *env, jobject /* thiz */, jint index) {
99 sp<IMediaCodecList> mcl = getCodecList(env);
101 // Runtime exception already pending.
105 const sp<MediaCodecInfo> &info = mcl->getCodecInfo(index);
107 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
111 return info->isEncoder();
114 static jarray android_media_MediaCodecList_getSupportedTypes(
115 JNIEnv *env, jobject /* thiz */, jint index) {
116 sp<IMediaCodecList> mcl = getCodecList(env);
118 // Runtime exception already pending.
122 const sp<MediaCodecInfo> &info = mcl->getCodecInfo(index);
124 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
128 Vector<AString> types;
129 info->getSupportedMimes(&types);
131 jclass clazz = env->FindClass("java/lang/String");
132 CHECK(clazz != NULL);
134 jobjectArray array = env->NewObjectArray(types.size(), clazz, NULL);
136 for (size_t i = 0; i < types.size(); ++i) {
137 jstring obj = env->NewStringUTF(types.itemAt(i).c_str());
138 env->SetObjectArrayElement(array, i, obj);
139 env->DeleteLocalRef(obj);
146 static jobject android_media_MediaCodecList_getCodecCapabilities(
147 JNIEnv *env, jobject /* thiz */, jint index, jstring type) {
149 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
153 sp<IMediaCodecList> mcl = getCodecList(env);
155 // Runtime exception already pending.
159 const sp<MediaCodecInfo> &info = mcl->getCodecInfo(index);
161 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
165 const char *typeStr = env->GetStringUTFChars(type, NULL);
166 if (typeStr == NULL) {
167 // Out of memory exception already pending.
171 Vector<MediaCodecInfo::ProfileLevel> profileLevels;
172 Vector<uint32_t> colorFormats;
174 sp<AMessage> defaultFormat = new AMessage();
175 defaultFormat->setString("mime", typeStr);
177 // TODO query default-format also from codec/codec list
178 const sp<MediaCodecInfo::Capabilities> &capabilities =
179 info->getCapabilitiesFor(typeStr);
180 env->ReleaseStringUTFChars(type, typeStr);
182 if (capabilities == NULL) {
183 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
187 capabilities->getSupportedColorFormats(&colorFormats);
188 capabilities->getSupportedProfileLevels(&profileLevels);
189 uint32_t flags = capabilities->getFlags();
190 sp<AMessage> details = capabilities->getDetails();
191 bool isEncoder = info->isEncoder();
193 jobject defaultFormatObj = NULL;
194 if (ConvertMessageToMap(env, defaultFormat, &defaultFormatObj)) {
198 jobject infoObj = NULL;
199 if (ConvertMessageToMap(env, details, &infoObj)) {
200 env->DeleteLocalRef(defaultFormatObj);
205 env->FindClass("android/media/MediaCodecInfo$CodecCapabilities");
206 CHECK(capsClazz != NULL);
208 jclass profileLevelClazz =
209 env->FindClass("android/media/MediaCodecInfo$CodecProfileLevel");
210 CHECK(profileLevelClazz != NULL);
212 jobjectArray profileLevelArray =
213 env->NewObjectArray(profileLevels.size(), profileLevelClazz, NULL);
215 jfieldID profileField =
216 env->GetFieldID(profileLevelClazz, "profile", "I");
218 jfieldID levelField =
219 env->GetFieldID(profileLevelClazz, "level", "I");
221 for (size_t i = 0; i < profileLevels.size(); ++i) {
222 const MediaCodecInfo::ProfileLevel &src = profileLevels.itemAt(i);
224 jobject profileLevelObj = env->AllocObject(profileLevelClazz);
226 env->SetIntField(profileLevelObj, profileField, src.mProfile);
227 env->SetIntField(profileLevelObj, levelField, src.mLevel);
229 env->SetObjectArrayElement(profileLevelArray, i, profileLevelObj);
231 env->DeleteLocalRef(profileLevelObj);
232 profileLevelObj = NULL;
235 jintArray colorFormatsArray = env->NewIntArray(colorFormats.size());
237 for (size_t i = 0; i < colorFormats.size(); ++i) {
238 jint val = colorFormats.itemAt(i);
239 env->SetIntArrayRegion(colorFormatsArray, i, 1, &val);
242 jmethodID capsConstructID = env->GetMethodID(capsClazz, "<init>",
243 "([Landroid/media/MediaCodecInfo$CodecProfileLevel;[IZI"
244 "Ljava/util/Map;Ljava/util/Map;)V");
246 jobject caps = env->NewObject(capsClazz, capsConstructID,
247 profileLevelArray, colorFormatsArray, isEncoder, flags,
248 defaultFormatObj, infoObj);
250 env->DeleteLocalRef(profileLevelArray);
251 profileLevelArray = NULL;
253 env->DeleteLocalRef(colorFormatsArray);
254 colorFormatsArray = NULL;
256 env->DeleteLocalRef(defaultFormatObj);
257 defaultFormatObj = NULL;
259 env->DeleteLocalRef(infoObj);
265 static jobject android_media_MediaCodecList_getGlobalSettings(JNIEnv *env, jobject /* thiz */) {
266 sp<IMediaCodecList> mcl = getCodecList(env);
268 // Runtime exception already pending.
272 const sp<AMessage> settings = mcl->getGlobalSettings();
273 if (settings == NULL) {
274 jniThrowException(env, "java/lang/RuntimeException", "cannot get global settings");
278 jobject settingsObj = NULL;
279 if (ConvertMessageToMap(env, settings, &settingsObj)) {
286 static void android_media_MediaCodecList_native_init(JNIEnv* /* env */) {
289 static const JNINativeMethod gMethods[] = {
290 { "native_getCodecCount", "()I", (void *)android_media_MediaCodecList_getCodecCount },
291 { "getCodecName", "(I)Ljava/lang/String;",
292 (void *)android_media_MediaCodecList_getCodecName },
293 { "isEncoder", "(I)Z", (void *)android_media_MediaCodecList_isEncoder },
294 { "getSupportedTypes", "(I)[Ljava/lang/String;",
295 (void *)android_media_MediaCodecList_getSupportedTypes },
297 { "getCodecCapabilities",
298 "(ILjava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;",
299 (void *)android_media_MediaCodecList_getCodecCapabilities },
301 { "native_getGlobalSettings",
303 (void *)android_media_MediaCodecList_getGlobalSettings },
305 { "findCodecByName", "(Ljava/lang/String;)I",
306 (void *)android_media_MediaCodecList_findCodecByName },
308 { "native_init", "()V", (void *)android_media_MediaCodecList_native_init },
311 int register_android_media_MediaCodecList(JNIEnv *env) {
312 return AndroidRuntime::registerNativeMethods(env,
313 "android/media/MediaCodecList", gMethods, NELEM(gMethods));