1 /*
2 * Copyright (C) 2010 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 "MediaProfilesJNI"
19 #include <utils/Log.h>
20
21 #include <stdio.h>
22 #include <utils/threads.h>
23
24 #include "jni.h"
25 #include <nativehelper/JNIHelp.h>
26 #include "android_runtime/AndroidRuntime.h"
27 #include <media/MediaProfiles.h>
28
29 using namespace android;
30
31 static Mutex sLock;
32 MediaProfiles *sProfiles = NULL;
33
34 // This function is called from a static block in MediaProfiles.java class,
35 // which won't run until the first time an instance of this class is used.
36 static void
android_media_MediaProfiles_native_init(JNIEnv *)37 android_media_MediaProfiles_native_init(JNIEnv* /* env */)
38 {
39 ALOGV("native_init");
40 Mutex::Autolock lock(sLock);
41
42 if (sProfiles == NULL) {
43 sProfiles = MediaProfiles::getInstance();
44 }
45 }
46
47 static jint
android_media_MediaProfiles_native_get_num_file_formats(JNIEnv *,jobject)48 android_media_MediaProfiles_native_get_num_file_formats(JNIEnv* /* env */, jobject /* thiz */)
49 {
50 ALOGV("native_get_num_file_formats");
51 return (jint) sProfiles->getOutputFileFormats().size();
52 }
53
54 static jint
android_media_MediaProfiles_native_get_file_format(JNIEnv * env,jobject,jint index)55 android_media_MediaProfiles_native_get_file_format(JNIEnv *env, jobject /* thiz */, jint index)
56 {
57 ALOGV("native_get_file_format: %d", index);
58 Vector<output_format> formats = sProfiles->getOutputFileFormats();
59 int nSize = formats.size();
60 if (index < 0 || index >= nSize) {
61 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
62 return -1;
63 }
64 return static_cast<jint>(formats[index]);
65 }
66
67 static jint
android_media_MediaProfiles_native_get_num_video_encoders(JNIEnv *,jobject)68 android_media_MediaProfiles_native_get_num_video_encoders(JNIEnv* /* env */, jobject /* thiz */)
69 {
70 ALOGV("native_get_num_video_encoders");
71 return sProfiles->getVideoEncoders().size();
72 }
73
74 static jobject
android_media_MediaProfiles_native_get_video_encoder_cap(JNIEnv * env,jobject,jint index)75 android_media_MediaProfiles_native_get_video_encoder_cap(JNIEnv *env, jobject /* thiz */,
76 jint index)
77 {
78 ALOGV("native_get_video_encoder_cap: %d", index);
79 Vector<video_encoder> encoders = sProfiles->getVideoEncoders();
80 int nSize = encoders.size();
81 if (index < 0 || index >= nSize) {
82 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
83 return NULL;
84 }
85
86 video_encoder encoder = encoders[index];
87 int minBitRate = sProfiles->getVideoEncoderParamByName("enc.vid.bps.min", encoder);
88 int maxBitRate = sProfiles->getVideoEncoderParamByName("enc.vid.bps.max", encoder);
89 int minFrameRate = sProfiles->getVideoEncoderParamByName("enc.vid.fps.min", encoder);
90 int maxFrameRate = sProfiles->getVideoEncoderParamByName("enc.vid.fps.max", encoder);
91 int minFrameWidth = sProfiles->getVideoEncoderParamByName("enc.vid.width.min", encoder);
92 int maxFrameWidth = sProfiles->getVideoEncoderParamByName("enc.vid.width.max", encoder);
93 int minFrameHeight = sProfiles->getVideoEncoderParamByName("enc.vid.height.min", encoder);
94 int maxFrameHeight = sProfiles->getVideoEncoderParamByName("enc.vid.height.max", encoder);
95
96 // Check on the values retrieved
97 if ((minBitRate == -1 || maxBitRate == -1) ||
98 (minFrameRate == -1 || maxFrameRate == -1) ||
99 (minFrameWidth == -1 || maxFrameWidth == -1) ||
100 (minFrameHeight == -1 || maxFrameHeight == -1)) {
101
102 jniThrowException(env, "java/lang/RuntimeException", "Error retrieving video encoder capability params");
103 return NULL;
104 }
105
106 // Construct an instance of the VideoEncoderCap and set its member variables
107 jclass videoEncoderCapClazz = env->FindClass("android/media/EncoderCapabilities$VideoEncoderCap");
108 jmethodID videoEncoderCapConstructorMethodID = env->GetMethodID(videoEncoderCapClazz, "<init>", "(IIIIIIIII)V");
109 jobject cap = env->NewObject(videoEncoderCapClazz,
110 videoEncoderCapConstructorMethodID,
111 static_cast<int>(encoder),
112 minBitRate, maxBitRate,
113 minFrameRate, maxFrameRate,
114 minFrameWidth, maxFrameWidth,
115 minFrameHeight, maxFrameHeight);
116 return cap;
117 }
118
119 static jint
android_media_MediaProfiles_native_get_num_audio_encoders(JNIEnv *,jobject)120 android_media_MediaProfiles_native_get_num_audio_encoders(JNIEnv* /* env */, jobject /* thiz */)
121 {
122 ALOGV("native_get_num_audio_encoders");
123 return (jint) sProfiles->getAudioEncoders().size();
124 }
125
126 static jobject
android_media_MediaProfiles_native_get_audio_encoder_cap(JNIEnv * env,jobject,jint index)127 android_media_MediaProfiles_native_get_audio_encoder_cap(JNIEnv *env, jobject /* thiz */,
128 jint index)
129 {
130 ALOGV("native_get_audio_encoder_cap: %d", index);
131 Vector<audio_encoder> encoders = sProfiles->getAudioEncoders();
132 int nSize = encoders.size();
133 if (index < 0 || index >= nSize) {
134 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
135 return NULL;
136 }
137
138 audio_encoder encoder = encoders[index];
139 int minBitRate = sProfiles->getAudioEncoderParamByName("enc.aud.bps.min", encoder);
140 int maxBitRate = sProfiles->getAudioEncoderParamByName("enc.aud.bps.max", encoder);
141 int minSampleRate = sProfiles->getAudioEncoderParamByName("enc.aud.hz.min", encoder);
142 int maxSampleRate = sProfiles->getAudioEncoderParamByName("enc.aud.hz.max", encoder);
143 int minChannels = sProfiles->getAudioEncoderParamByName("enc.aud.ch.min", encoder);
144 int maxChannels = sProfiles->getAudioEncoderParamByName("enc.aud.ch.max", encoder);
145
146 // Check on the values retrieved
147 if ((minBitRate == -1 || maxBitRate == -1) ||
148 (minSampleRate == -1 || maxSampleRate == -1) ||
149 (minChannels == -1 || maxChannels == -1)) {
150
151 jniThrowException(env, "java/lang/RuntimeException", "Error retrieving video encoder capability params");
152 return NULL;
153 }
154
155 jclass audioEncoderCapClazz = env->FindClass("android/media/EncoderCapabilities$AudioEncoderCap");
156 jmethodID audioEncoderCapConstructorMethodID = env->GetMethodID(audioEncoderCapClazz, "<init>", "(IIIIIII)V");
157 jobject cap = env->NewObject(audioEncoderCapClazz,
158 audioEncoderCapConstructorMethodID,
159 static_cast<int>(encoder),
160 minBitRate, maxBitRate,
161 minSampleRate, maxSampleRate,
162 minChannels, maxChannels);
163 return cap;
164 }
165
isCamcorderQualityKnown(int quality)166 static bool isCamcorderQualityKnown(int quality)
167 {
168 return ((quality >= CAMCORDER_QUALITY_LIST_START &&
169 quality <= CAMCORDER_QUALITY_LIST_END) ||
170 (quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
171 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END) ||
172 (quality >= CAMCORDER_QUALITY_HIGH_SPEED_LIST_START &&
173 quality <= CAMCORDER_QUALITY_HIGH_SPEED_LIST_END));
174 }
175
176 static jobject
android_media_MediaProfiles_native_get_camcorder_profile(JNIEnv * env,jobject,jint id,jint quality)177 android_media_MediaProfiles_native_get_camcorder_profile(JNIEnv *env, jobject /* thiz */, jint id,
178 jint quality)
179 {
180 ALOGV("native_get_camcorder_profile: %d %d", id, quality);
181 if (!isCamcorderQualityKnown(quality)) {
182 jniThrowException(env, "java/lang/RuntimeException", "Unknown camcorder profile quality");
183 return NULL;
184 }
185
186 camcorder_quality q = static_cast<camcorder_quality>(quality);
187 int duration = sProfiles->getCamcorderProfileParamByName("duration", id, q);
188 int fileFormat = sProfiles->getCamcorderProfileParamByName("file.format", id, q);
189 int videoCodec = sProfiles->getCamcorderProfileParamByName("vid.codec", id, q);
190 int videoBitRate = sProfiles->getCamcorderProfileParamByName("vid.bps", id, q);
191 int videoFrameRate = sProfiles->getCamcorderProfileParamByName("vid.fps", id, q);
192 int videoFrameWidth = sProfiles->getCamcorderProfileParamByName("vid.width", id, q);
193 int videoFrameHeight = sProfiles->getCamcorderProfileParamByName("vid.height", id, q);
194 int audioCodec = sProfiles->getCamcorderProfileParamByName("aud.codec", id, q);
195 int audioBitRate = sProfiles->getCamcorderProfileParamByName("aud.bps", id, q);
196 int audioSampleRate = sProfiles->getCamcorderProfileParamByName("aud.hz", id, q);
197 int audioChannels = sProfiles->getCamcorderProfileParamByName("aud.ch", id, q);
198
199 // Check on the values retrieved
200 if (duration == -1 || fileFormat == -1 || videoCodec == -1 || audioCodec == -1 ||
201 videoBitRate == -1 || videoFrameRate == -1 || videoFrameWidth == -1 || videoFrameHeight == -1 ||
202 audioBitRate == -1 || audioSampleRate == -1 || audioChannels == -1) {
203
204 jniThrowException(env, "java/lang/RuntimeException", "Error retrieving camcorder profile params");
205 return NULL;
206 }
207
208 jclass camcorderProfileClazz = env->FindClass("android/media/CamcorderProfile");
209 jmethodID camcorderProfileConstructorMethodID = env->GetMethodID(camcorderProfileClazz, "<init>", "(IIIIIIIIIIII)V");
210 return env->NewObject(camcorderProfileClazz,
211 camcorderProfileConstructorMethodID,
212 duration,
213 quality,
214 fileFormat,
215 videoCodec,
216 videoBitRate,
217 videoFrameRate,
218 videoFrameWidth,
219 videoFrameHeight,
220 audioCodec,
221 audioBitRate,
222 audioSampleRate,
223 audioChannels);
224 }
225
226 static jobject
android_media_MediaProfiles_native_get_camcorder_profiles(JNIEnv * env,jobject,jint id,jint quality)227 android_media_MediaProfiles_native_get_camcorder_profiles(JNIEnv *env, jobject /* thiz */, jint id,
228 jint quality)
229 {
230 ALOGV("native_get_camcorder_profiles: %d %d", id, quality);
231 if (!isCamcorderQualityKnown(quality)) {
232 jniThrowException(env, "java/lang/RuntimeException", "Unknown camcorder profile quality");
233 return NULL;
234 }
235
236 camcorder_quality q = static_cast<camcorder_quality>(quality);
237 const MediaProfiles::CamcorderProfile *cp = sProfiles->getCamcorderProfile(id, q);
238 if (!cp) {
239 jniThrowException(env, "java/lang/RuntimeException",
240 "Error retrieving camcorder profile params");
241 return NULL;
242 }
243
244 int duration = cp->getDuration();
245 int fileFormat = cp->getFileFormat();
246
247 jclass encoderProfilesClazz = env->FindClass("android/media/EncoderProfiles");
248 jmethodID encoderProfilesConstructorMethodID =
249 env->GetMethodID(encoderProfilesClazz, "<init>",
250 "(II[Landroid/media/EncoderProfiles$VideoProfile;[Landroid/media/EncoderProfiles$AudioProfile;)V");
251
252 jclass videoProfileClazz = env->FindClass("android/media/EncoderProfiles$VideoProfile");
253 jmethodID videoProfileConstructorMethodID =
254 env->GetMethodID(videoProfileClazz, "<init>", "(IIIIII)V");
255
256 jclass audioProfileClazz = env->FindClass("android/media/EncoderProfiles$AudioProfile");
257 jmethodID audioProfileConstructorMethodID =
258 env->GetMethodID(audioProfileClazz, "<init>", "(IIIII)V");
259
260 jobjectArray videoCodecs = (jobjectArray)env->NewObjectArray(
261 cp->getVideoCodecs().size(), videoProfileClazz, nullptr);
262 {
263 int i = 0;
264 for (const MediaProfiles::VideoCodec *vc : cp->getVideoCodecs()) {
265 jobject videoCodec = env->NewObject(videoProfileClazz,
266 videoProfileConstructorMethodID,
267 vc->getCodec(),
268 vc->getFrameWidth(),
269 vc->getFrameHeight(),
270 vc->getFrameRate(),
271 vc->getBitrate(),
272 vc->getProfile());
273 env->SetObjectArrayElement(videoCodecs, i++, videoCodec);
274 }
275 }
276
277 jobjectArray audioCodecs;
278 if (quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START
279 && quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END) {
280 // timelapse profiles do not have audio codecs
281 audioCodecs = (jobjectArray)env->NewObjectArray(0, audioProfileClazz, nullptr);
282 } else {
283 audioCodecs = (jobjectArray)env->NewObjectArray(
284 cp->getAudioCodecs().size(), audioProfileClazz, nullptr);
285 int i = 0;
286 for (const MediaProfiles::AudioCodec *ac : cp->getAudioCodecs()) {
287 jobject audioCodec = env->NewObject(audioProfileClazz,
288 audioProfileConstructorMethodID,
289 ac->getCodec(),
290 ac->getChannels(),
291 ac->getSampleRate(),
292 ac->getBitrate(),
293 ac->getProfile());
294
295 env->SetObjectArrayElement(audioCodecs, i++, audioCodec);
296 }
297 }
298
299 return env->NewObject(encoderProfilesClazz,
300 encoderProfilesConstructorMethodID,
301 duration,
302 fileFormat,
303 videoCodecs,
304 audioCodecs);
305 }
306
307 static jboolean
android_media_MediaProfiles_native_has_camcorder_profile(JNIEnv *,jobject,jint id,jint quality)308 android_media_MediaProfiles_native_has_camcorder_profile(JNIEnv* /* env */, jobject /* thiz */,
309 jint id, jint quality)
310 {
311 ALOGV("native_has_camcorder_profile: %d %d", id, quality);
312 if (!isCamcorderQualityKnown(quality)) {
313 return JNI_FALSE;
314 }
315
316 camcorder_quality q = static_cast<camcorder_quality>(quality);
317 return sProfiles->hasCamcorderProfile(id, q) ? JNI_TRUE : JNI_FALSE;
318 }
319
320 static jint
android_media_MediaProfiles_native_get_num_video_decoders(JNIEnv *,jobject)321 android_media_MediaProfiles_native_get_num_video_decoders(JNIEnv* /* env */, jobject /* thiz */)
322 {
323 ALOGV("native_get_num_video_decoders");
324 return (jint) sProfiles->getVideoDecoders().size();
325 }
326
327 static jint
android_media_MediaProfiles_native_get_video_decoder_type(JNIEnv * env,jobject,jint index)328 android_media_MediaProfiles_native_get_video_decoder_type(JNIEnv *env, jobject /* thiz */,
329 jint index)
330 {
331 ALOGV("native_get_video_decoder_type: %d", index);
332 Vector<video_decoder> decoders = sProfiles->getVideoDecoders();
333 int nSize = decoders.size();
334 if (index < 0 || index >= nSize) {
335 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
336 return -1;
337 }
338
339 return static_cast<jint>(decoders[index]);
340 }
341
342 static jint
android_media_MediaProfiles_native_get_num_audio_decoders(JNIEnv *,jobject)343 android_media_MediaProfiles_native_get_num_audio_decoders(JNIEnv* /* env */, jobject /* thiz */)
344 {
345 ALOGV("native_get_num_audio_decoders");
346 return (jint) sProfiles->getAudioDecoders().size();
347 }
348
349 static jint
android_media_MediaProfiles_native_get_audio_decoder_type(JNIEnv * env,jobject,jint index)350 android_media_MediaProfiles_native_get_audio_decoder_type(JNIEnv *env, jobject /* thiz */,
351 jint index)
352 {
353 ALOGV("native_get_audio_decoder_type: %d", index);
354 Vector<audio_decoder> decoders = sProfiles->getAudioDecoders();
355 int nSize = decoders.size();
356 if (index < 0 || index >= nSize) {
357 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
358 return -1;
359 }
360
361 return static_cast<jint>(decoders[index]);
362 }
363
364 static jint
android_media_MediaProfiles_native_get_num_image_encoding_quality_levels(JNIEnv *,jobject,jint cameraId)365 android_media_MediaProfiles_native_get_num_image_encoding_quality_levels(JNIEnv* /* env */,
366 jobject /* thiz */,
367 jint cameraId)
368 {
369 ALOGV("native_get_num_image_encoding_quality_levels");
370 return (jint) sProfiles->getImageEncodingQualityLevels(cameraId).size();
371 }
372
373 static jint
android_media_MediaProfiles_native_get_image_encoding_quality_level(JNIEnv * env,jobject,jint cameraId,jint index)374 android_media_MediaProfiles_native_get_image_encoding_quality_level(JNIEnv *env, jobject /* thiz */,
375 jint cameraId, jint index)
376 {
377 ALOGV("native_get_image_encoding_quality_level");
378 Vector<int> levels = sProfiles->getImageEncodingQualityLevels(cameraId);
379 if (index < 0 || index >= (jint) levels.size()) {
380 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
381 return -1;
382 }
383 return static_cast<jint>(levels[index]);
384 }
385 static const JNINativeMethod gMethodsForEncoderCapabilitiesClass[] = {
386 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
387 {"native_get_num_file_formats", "()I", (void *)android_media_MediaProfiles_native_get_num_file_formats},
388 {"native_get_file_format", "(I)I", (void *)android_media_MediaProfiles_native_get_file_format},
389 {"native_get_num_video_encoders", "()I", (void *)android_media_MediaProfiles_native_get_num_video_encoders},
390 {"native_get_num_audio_encoders", "()I", (void *)android_media_MediaProfiles_native_get_num_audio_encoders},
391
392 {"native_get_video_encoder_cap", "(I)Landroid/media/EncoderCapabilities$VideoEncoderCap;",
393 (void *)android_media_MediaProfiles_native_get_video_encoder_cap},
394
395 {"native_get_audio_encoder_cap", "(I)Landroid/media/EncoderCapabilities$AudioEncoderCap;",
396 (void *)android_media_MediaProfiles_native_get_audio_encoder_cap},
397 };
398
399 static const JNINativeMethod gMethodsForCamcorderProfileClass[] = {
400 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
401 {"native_get_camcorder_profile", "(II)Landroid/media/CamcorderProfile;",
402 (void *)android_media_MediaProfiles_native_get_camcorder_profile},
403 {"native_get_camcorder_profiles", "(II)Landroid/media/EncoderProfiles;",
404 (void *)android_media_MediaProfiles_native_get_camcorder_profiles},
405 {"native_has_camcorder_profile", "(II)Z",
406 (void *)android_media_MediaProfiles_native_has_camcorder_profile},
407 };
408
409 static const JNINativeMethod gMethodsForDecoderCapabilitiesClass[] = {
410 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
411 {"native_get_num_video_decoders", "()I", (void *)android_media_MediaProfiles_native_get_num_video_decoders},
412 {"native_get_num_audio_decoders", "()I", (void *)android_media_MediaProfiles_native_get_num_audio_decoders},
413 {"native_get_video_decoder_type", "(I)I", (void *)android_media_MediaProfiles_native_get_video_decoder_type},
414 {"native_get_audio_decoder_type", "(I)I", (void *)android_media_MediaProfiles_native_get_audio_decoder_type},
415 };
416
417 static const JNINativeMethod gMethodsForCameraProfileClass[] = {
418 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
419 {"native_get_num_image_encoding_quality_levels",
420 "(I)I", (void *)android_media_MediaProfiles_native_get_num_image_encoding_quality_levels},
421 {"native_get_image_encoding_quality_level","(II)I", (void *)android_media_MediaProfiles_native_get_image_encoding_quality_level},
422 };
423
424 static const char* const kEncoderCapabilitiesClassPathName = "android/media/EncoderCapabilities";
425 static const char* const kDecoderCapabilitiesClassPathName = "android/media/DecoderCapabilities";
426 static const char* const kCamcorderProfileClassPathName = "android/media/CamcorderProfile";
427 static const char* const kCameraProfileClassPathName = "android/media/CameraProfile";
428
429 // This function only registers the native methods, and is called from
430 // JNI_OnLoad in android_media_MediaPlayer.cpp
register_android_media_MediaProfiles(JNIEnv * env)431 int register_android_media_MediaProfiles(JNIEnv *env)
432 {
433 int ret1 = AndroidRuntime::registerNativeMethods(env,
434 kEncoderCapabilitiesClassPathName,
435 gMethodsForEncoderCapabilitiesClass,
436 NELEM(gMethodsForEncoderCapabilitiesClass));
437
438 int ret2 = AndroidRuntime::registerNativeMethods(env,
439 kCamcorderProfileClassPathName,
440 gMethodsForCamcorderProfileClass,
441 NELEM(gMethodsForCamcorderProfileClass));
442
443 int ret3 = AndroidRuntime::registerNativeMethods(env,
444 kDecoderCapabilitiesClassPathName,
445 gMethodsForDecoderCapabilitiesClass,
446 NELEM(gMethodsForDecoderCapabilitiesClass));
447
448 int ret4 = AndroidRuntime::registerNativeMethods(env,
449 kCameraProfileClassPathName,
450 gMethodsForCameraProfileClass,
451 NELEM(gMethodsForCameraProfileClass));
452
453 // Success if all return values from above are 0
454 return (ret1 || ret2 || ret3 || ret4);
455 }
456