blob: f8c349b54c5325bd8c6fecde63f8285df9134c5e [file] [log] [blame]
Andreas Huber5a04bf32012-03-29 16:41:38 -07001/*
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>
Lajos Molnarb58dc312014-07-18 12:10:33 -070022#include <media/stagefright/foundation/AMessage.h>
Andreas Huber5a04bf32012-03-29 16:41:38 -070023#include <media/stagefright/MediaCodecList.h>
Lajos Molnarcd5e3f92014-08-06 12:18:34 -070024#include <media/IMediaCodecList.h>
25#include <media/MediaCodecInfo.h>
Andreas Huber5a04bf32012-03-29 16:41:38 -070026
27#include "android_runtime/AndroidRuntime.h"
28#include "jni.h"
29#include "JNIHelp.h"
Lajos Molnarb58dc312014-07-18 12:10:33 -070030#include "android_media_Utils.h"
Andreas Huber5a04bf32012-03-29 16:41:38 -070031
32using namespace android;
33
Lajos Molnarcd5e3f92014-08-06 12:18:34 -070034static sp<IMediaCodecList> getCodecList(JNIEnv *env) {
35 sp<IMediaCodecList> mcl = MediaCodecList::getInstance();
36 if (mcl == NULL) {
37 // This should never happen unless something is really wrong
38 jniThrowException(
39 env, "java/lang/RuntimeException", "cannot get MediaCodecList");
40 }
41 return mcl;
42}
43
Andreas Huber60d610b2012-05-02 16:06:09 -070044static jint android_media_MediaCodecList_getCodecCount(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -080045 JNIEnv *env, jobject /* thiz */) {
Lajos Molnarcd5e3f92014-08-06 12:18:34 -070046 sp<IMediaCodecList> mcl = getCodecList(env);
47 if (mcl == NULL) {
48 // Runtime exception already pending.
49 return 0;
50 }
51 return mcl->countCodecs();
Andreas Huber5a04bf32012-03-29 16:41:38 -070052}
53
54static jstring android_media_MediaCodecList_getCodecName(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -080055 JNIEnv *env, jobject /* thiz */, jint index) {
Lajos Molnarcd5e3f92014-08-06 12:18:34 -070056 sp<IMediaCodecList> mcl = getCodecList(env);
57 if (mcl == NULL) {
58 // Runtime exception already pending.
59 return NULL;
60 }
Andreas Huber5a04bf32012-03-29 16:41:38 -070061
Lajos Molnarcd5e3f92014-08-06 12:18:34 -070062 const sp<MediaCodecInfo> &info = mcl->getCodecInfo(index);
63 if (info == NULL) {
Andreas Huber5a04bf32012-03-29 16:41:38 -070064 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
65 return NULL;
66 }
67
Lajos Molnarcd5e3f92014-08-06 12:18:34 -070068 const char *name = info->getCodecName();
Andreas Huber5a04bf32012-03-29 16:41:38 -070069 return env->NewStringUTF(name);
70}
71
Martin Storsjo93077a22012-09-25 11:55:25 +030072static jint android_media_MediaCodecList_findCodecByName(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -080073 JNIEnv *env, jobject /* thiz */, jstring name) {
Martin Storsjo93077a22012-09-25 11:55:25 +030074 if (name == NULL) {
75 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
76 return -ENOENT;
77 }
78
79 const char *nameStr = env->GetStringUTFChars(name, NULL);
Martin Storsjo93077a22012-09-25 11:55:25 +030080 if (nameStr == NULL) {
81 // Out of memory exception already pending.
82 return -ENOENT;
83 }
84
Lajos Molnarcd5e3f92014-08-06 12:18:34 -070085 sp<IMediaCodecList> mcl = getCodecList(env);
86 if (mcl == NULL) {
87 // Runtime exception already pending.
Lajos Molnar018b70f2014-08-12 11:37:24 -070088 env->ReleaseStringUTFChars(name, nameStr);
Lajos Molnarcd5e3f92014-08-06 12:18:34 -070089 return -ENOENT;
90 }
91
92 jint ret = mcl->findCodecByName(nameStr);
Martin Storsjo93077a22012-09-25 11:55:25 +030093 env->ReleaseStringUTFChars(name, nameStr);
94 return ret;
95}
96
Andreas Huber5a04bf32012-03-29 16:41:38 -070097static jboolean android_media_MediaCodecList_isEncoder(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -080098 JNIEnv *env, jobject /* thiz */, jint index) {
Lajos Molnarcd5e3f92014-08-06 12:18:34 -070099 sp<IMediaCodecList> mcl = getCodecList(env);
100 if (mcl == NULL) {
101 // Runtime exception already pending.
102 return false;
103 }
104
105 const sp<MediaCodecInfo> &info = mcl->getCodecInfo(index);
106 if (info == NULL) {
107 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
108 return false;
109 }
110
111 return info->isEncoder();
Andreas Huber5a04bf32012-03-29 16:41:38 -0700112}
113
114static jarray android_media_MediaCodecList_getSupportedTypes(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -0800115 JNIEnv *env, jobject /* thiz */, jint index) {
Lajos Molnarcd5e3f92014-08-06 12:18:34 -0700116 sp<IMediaCodecList> mcl = getCodecList(env);
117 if (mcl == NULL) {
118 // Runtime exception already pending.
119 return NULL;
120 }
Andreas Huber5a04bf32012-03-29 16:41:38 -0700121
Lajos Molnarcd5e3f92014-08-06 12:18:34 -0700122 const sp<MediaCodecInfo> &info = mcl->getCodecInfo(index);
123 if (info == NULL) {
Andreas Huber5a04bf32012-03-29 16:41:38 -0700124 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
125 return NULL;
126 }
127
Lajos Molnarcd5e3f92014-08-06 12:18:34 -0700128 Vector<AString> types;
129 info->getSupportedMimes(&types);
130
Andreas Huber5a04bf32012-03-29 16:41:38 -0700131 jclass clazz = env->FindClass("java/lang/String");
132 CHECK(clazz != NULL);
133
134 jobjectArray array = env->NewObjectArray(types.size(), clazz, NULL);
135
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);
140 obj = NULL;
141 }
142
143 return array;
144}
145
146static jobject android_media_MediaCodecList_getCodecCapabilities(
Andreas Gampe5a15d0d2014-11-10 18:19:40 -0800147 JNIEnv *env, jobject /* thiz */, jint index, jstring type) {
Andreas Huber5a04bf32012-03-29 16:41:38 -0700148 if (type == NULL) {
149 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
150 return NULL;
151 }
152
Lajos Molnarcd5e3f92014-08-06 12:18:34 -0700153 sp<IMediaCodecList> mcl = getCodecList(env);
154 if (mcl == NULL) {
155 // Runtime exception already pending.
156 return NULL;
157 }
158
159 const sp<MediaCodecInfo> &info = mcl->getCodecInfo(index);
160 if (info == NULL) {
161 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
162 return NULL;
163 }
164
Andreas Huber5a04bf32012-03-29 16:41:38 -0700165 const char *typeStr = env->GetStringUTFChars(type, NULL);
Andreas Huber5a04bf32012-03-29 16:41:38 -0700166 if (typeStr == NULL) {
167 // Out of memory exception already pending.
168 return NULL;
169 }
170
Lajos Molnarcd5e3f92014-08-06 12:18:34 -0700171 Vector<MediaCodecInfo::ProfileLevel> profileLevels;
Andreas Huber5a04bf32012-03-29 16:41:38 -0700172 Vector<uint32_t> colorFormats;
Lajos Molnarb58dc312014-07-18 12:10:33 -0700173
174 sp<AMessage> defaultFormat = new AMessage();
175 defaultFormat->setString("mime", typeStr);
176
177 // TODO query default-format also from codec/codec list
Lajos Molnarcd5e3f92014-08-06 12:18:34 -0700178 const sp<MediaCodecInfo::Capabilities> &capabilities =
179 info->getCapabilitiesFor(typeStr);
Lajos Molnar018b70f2014-08-12 11:37:24 -0700180 env->ReleaseStringUTFChars(type, typeStr);
181 typeStr = NULL;
Lajos Molnarcd5e3f92014-08-06 12:18:34 -0700182 if (capabilities == NULL) {
Andreas Huber5a04bf32012-03-29 16:41:38 -0700183 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
184 return NULL;
185 }
Lajos Molnarcd5e3f92014-08-06 12:18:34 -0700186
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();
Andreas Huber5a04bf32012-03-29 16:41:38 -0700192
Lajos Molnarb58dc312014-07-18 12:10:33 -0700193 jobject defaultFormatObj = NULL;
194 if (ConvertMessageToMap(env, defaultFormat, &defaultFormatObj)) {
195 return NULL;
196 }
197
198 jobject infoObj = NULL;
Lajos Molnarcd5e3f92014-08-06 12:18:34 -0700199 if (ConvertMessageToMap(env, details, &infoObj)) {
Lajos Molnarb58dc312014-07-18 12:10:33 -0700200 env->DeleteLocalRef(defaultFormatObj);
201 return NULL;
202 }
203
Andreas Huber5a04bf32012-03-29 16:41:38 -0700204 jclass capsClazz =
Andreas Huber60d610b2012-05-02 16:06:09 -0700205 env->FindClass("android/media/MediaCodecInfo$CodecCapabilities");
Andreas Huber5a04bf32012-03-29 16:41:38 -0700206 CHECK(capsClazz != NULL);
207
Andreas Huber5a04bf32012-03-29 16:41:38 -0700208 jclass profileLevelClazz =
Andreas Huber60d610b2012-05-02 16:06:09 -0700209 env->FindClass("android/media/MediaCodecInfo$CodecProfileLevel");
Andreas Huber5a04bf32012-03-29 16:41:38 -0700210 CHECK(profileLevelClazz != NULL);
211
212 jobjectArray profileLevelArray =
213 env->NewObjectArray(profileLevels.size(), profileLevelClazz, NULL);
214
215 jfieldID profileField =
Andreas Huber07ea4262012-04-11 12:21:20 -0700216 env->GetFieldID(profileLevelClazz, "profile", "I");
Andreas Huber5a04bf32012-03-29 16:41:38 -0700217
218 jfieldID levelField =
Andreas Huber07ea4262012-04-11 12:21:20 -0700219 env->GetFieldID(profileLevelClazz, "level", "I");
Andreas Huber5a04bf32012-03-29 16:41:38 -0700220
221 for (size_t i = 0; i < profileLevels.size(); ++i) {
Lajos Molnarcd5e3f92014-08-06 12:18:34 -0700222 const MediaCodecInfo::ProfileLevel &src = profileLevels.itemAt(i);
Andreas Huber5a04bf32012-03-29 16:41:38 -0700223
224 jobject profileLevelObj = env->AllocObject(profileLevelClazz);
225
226 env->SetIntField(profileLevelObj, profileField, src.mProfile);
227 env->SetIntField(profileLevelObj, levelField, src.mLevel);
228
229 env->SetObjectArrayElement(profileLevelArray, i, profileLevelObj);
230
231 env->DeleteLocalRef(profileLevelObj);
232 profileLevelObj = NULL;
233 }
234
Andreas Huber5a04bf32012-03-29 16:41:38 -0700235 jintArray colorFormatsArray = env->NewIntArray(colorFormats.size());
236
237 for (size_t i = 0; i < colorFormats.size(); ++i) {
238 jint val = colorFormats.itemAt(i);
239 env->SetIntArrayRegion(colorFormatsArray, i, 1, &val);
240 }
241
Lajos Molnarb58dc312014-07-18 12:10:33 -0700242 jmethodID capsConstructID = env->GetMethodID(capsClazz, "<init>",
243 "([Landroid/media/MediaCodecInfo$CodecProfileLevel;[IZI"
244 "Ljava/util/Map;Ljava/util/Map;)V");
245
246 jobject caps = env->NewObject(capsClazz, capsConstructID,
247 profileLevelArray, colorFormatsArray, isEncoder, flags,
248 defaultFormatObj, infoObj);
249
Lajos Molnarb58dc312014-07-18 12:10:33 -0700250 env->DeleteLocalRef(profileLevelArray);
251 profileLevelArray = NULL;
252
Andreas Huber5a04bf32012-03-29 16:41:38 -0700253 env->DeleteLocalRef(colorFormatsArray);
254 colorFormatsArray = NULL;
255
Lajos Molnarb58dc312014-07-18 12:10:33 -0700256 env->DeleteLocalRef(defaultFormatObj);
257 defaultFormatObj = NULL;
258
259 env->DeleteLocalRef(infoObj);
260 infoObj = NULL;
261
Andreas Huber5a04bf32012-03-29 16:41:38 -0700262 return caps;
263}
264
Andreas Gampe5a15d0d2014-11-10 18:19:40 -0800265static void android_media_MediaCodecList_native_init(JNIEnv* /* env */) {
Andreas Huber5a04bf32012-03-29 16:41:38 -0700266}
267
268static JNINativeMethod gMethods[] = {
Lajos Molnarb58dc312014-07-18 12:10:33 -0700269 { "native_getCodecCount", "()I", (void *)android_media_MediaCodecList_getCodecCount },
Andreas Huber5a04bf32012-03-29 16:41:38 -0700270 { "getCodecName", "(I)Ljava/lang/String;",
271 (void *)android_media_MediaCodecList_getCodecName },
272 { "isEncoder", "(I)Z", (void *)android_media_MediaCodecList_isEncoder },
273 { "getSupportedTypes", "(I)[Ljava/lang/String;",
274 (void *)android_media_MediaCodecList_getSupportedTypes },
275
276 { "getCodecCapabilities",
Andreas Huber60d610b2012-05-02 16:06:09 -0700277 "(ILjava/lang/String;)Landroid/media/MediaCodecInfo$CodecCapabilities;",
Andreas Huber5a04bf32012-03-29 16:41:38 -0700278 (void *)android_media_MediaCodecList_getCodecCapabilities },
279
Martin Storsjo93077a22012-09-25 11:55:25 +0300280 { "findCodecByName", "(Ljava/lang/String;)I",
281 (void *)android_media_MediaCodecList_findCodecByName },
282
Andreas Huber5a04bf32012-03-29 16:41:38 -0700283 { "native_init", "()V", (void *)android_media_MediaCodecList_native_init },
284};
285
286int register_android_media_MediaCodecList(JNIEnv *env) {
287 return AndroidRuntime::registerNativeMethods(env,
288 "android/media/MediaCodecList", gMethods, NELEM(gMethods));
289}
290