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,jboolean advanced)227 android_media_MediaProfiles_native_get_camcorder_profiles(JNIEnv *env, jobject /* thiz */, jint id,
228 jint quality, jboolean advanced)
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 return NULL;
240 }
241
242 int duration = cp->getDuration();
243 int fileFormat = cp->getFileFormat();
244
245 jclass encoderProfilesClazz = env->FindClass("android/media/EncoderProfiles");
246 jmethodID encoderProfilesConstructorMethodID =
247 env->GetMethodID(encoderProfilesClazz, "<init>",
248 "(II[Landroid/media/EncoderProfiles$VideoProfile;[Landroid/media/EncoderProfiles$AudioProfile;)V");
249
250 jclass videoProfileClazz = env->FindClass("android/media/EncoderProfiles$VideoProfile");
251 jmethodID videoProfileConstructorMethodID =
252 env->GetMethodID(videoProfileClazz, "<init>", "(IIIIIIIII)V");
253
254 jclass audioProfileClazz = env->FindClass("android/media/EncoderProfiles$AudioProfile");
255 jmethodID audioProfileConstructorMethodID =
256 env->GetMethodID(audioProfileClazz, "<init>", "(IIIII)V");
257
258 jobjectArray videoCodecs = nullptr;
259 {
260 auto isAdvancedCodec = [](const MediaProfiles::VideoCodec *vc) -> bool {
261 return ((vc->getBitDepth() != 8
262 || vc->getChromaSubsampling() != CHROMA_SUBSAMPLING_YUV_420
263 || vc->getHdrFormat() != HDR_FORMAT_NONE));
264 };
265 std::vector<jobject> codecVector;
266 for (const MediaProfiles::VideoCodec *vc : cp->getVideoCodecs()) {
267 if (isAdvancedCodec(vc) && !static_cast<bool>(advanced)) {
268 continue;
269 }
270 chroma_subsampling cs = vc->getChromaSubsampling();
271 int bitDepth = vc->getBitDepth();
272 hdr_format hdr = vc->getHdrFormat();
273 jobject videoCodec = env->NewObject(videoProfileClazz,
274 videoProfileConstructorMethodID,
275 vc->getCodec(),
276 vc->getFrameWidth(),
277 vc->getFrameHeight(),
278 vc->getFrameRate(),
279 vc->getBitrate(),
280 vc->getProfile(),
281 static_cast<int>(cs),
282 bitDepth,
283 static_cast<int>(hdr));
284
285 codecVector.push_back(videoCodec);
286 }
287 videoCodecs = (jobjectArray)env->NewObjectArray(codecVector.size(),
288 videoProfileClazz, nullptr);
289
290 int i = 0;
291 for (jobject codecObj : codecVector) {
292 env->SetObjectArrayElement(videoCodecs, i++, codecObj);
293 }
294 }
295 jobjectArray audioCodecs;
296 if (quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START
297 && quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END) {
298 // timelapse profiles do not have audio codecs
299 audioCodecs = (jobjectArray)env->NewObjectArray(0, audioProfileClazz, nullptr);
300 } else {
301 audioCodecs = (jobjectArray)env->NewObjectArray(
302 cp->getAudioCodecs().size(), audioProfileClazz, nullptr);
303 int i = 0;
304 for (const MediaProfiles::AudioCodec *ac : cp->getAudioCodecs()) {
305 jobject audioCodec = env->NewObject(audioProfileClazz,
306 audioProfileConstructorMethodID,
307 ac->getCodec(),
308 ac->getChannels(),
309 ac->getSampleRate(),
310 ac->getBitrate(),
311 ac->getProfile());
312
313 env->SetObjectArrayElement(audioCodecs, i++, audioCodec);
314 }
315 }
316
317 return env->NewObject(encoderProfilesClazz,
318 encoderProfilesConstructorMethodID,
319 duration,
320 fileFormat,
321 videoCodecs,
322 audioCodecs);
323 }
324
325 static jboolean
android_media_MediaProfiles_native_has_camcorder_profile(JNIEnv *,jobject,jint id,jint quality)326 android_media_MediaProfiles_native_has_camcorder_profile(JNIEnv* /* env */, jobject /* thiz */,
327 jint id, jint quality)
328 {
329 ALOGV("native_has_camcorder_profile: %d %d", id, quality);
330 if (!isCamcorderQualityKnown(quality)) {
331 return JNI_FALSE;
332 }
333
334 camcorder_quality q = static_cast<camcorder_quality>(quality);
335 return sProfiles->hasCamcorderProfile(id, q) ? JNI_TRUE : JNI_FALSE;
336 }
337
338 static jint
android_media_MediaProfiles_native_get_num_video_decoders(JNIEnv *,jobject)339 android_media_MediaProfiles_native_get_num_video_decoders(JNIEnv* /* env */, jobject /* thiz */)
340 {
341 ALOGV("native_get_num_video_decoders");
342 return (jint) sProfiles->getVideoDecoders().size();
343 }
344
345 static jint
android_media_MediaProfiles_native_get_video_decoder_type(JNIEnv * env,jobject,jint index)346 android_media_MediaProfiles_native_get_video_decoder_type(JNIEnv *env, jobject /* thiz */,
347 jint index)
348 {
349 ALOGV("native_get_video_decoder_type: %d", index);
350 Vector<video_decoder> decoders = sProfiles->getVideoDecoders();
351 int nSize = decoders.size();
352 if (index < 0 || index >= nSize) {
353 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
354 return -1;
355 }
356
357 return static_cast<jint>(decoders[index]);
358 }
359
360 static jint
android_media_MediaProfiles_native_get_num_audio_decoders(JNIEnv *,jobject)361 android_media_MediaProfiles_native_get_num_audio_decoders(JNIEnv* /* env */, jobject /* thiz */)
362 {
363 ALOGV("native_get_num_audio_decoders");
364 return (jint) sProfiles->getAudioDecoders().size();
365 }
366
367 static jint
android_media_MediaProfiles_native_get_audio_decoder_type(JNIEnv * env,jobject,jint index)368 android_media_MediaProfiles_native_get_audio_decoder_type(JNIEnv *env, jobject /* thiz */,
369 jint index)
370 {
371 ALOGV("native_get_audio_decoder_type: %d", index);
372 Vector<audio_decoder> decoders = sProfiles->getAudioDecoders();
373 int nSize = decoders.size();
374 if (index < 0 || index >= nSize) {
375 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
376 return -1;
377 }
378
379 return static_cast<jint>(decoders[index]);
380 }
381
382 static jint
android_media_MediaProfiles_native_get_num_image_encoding_quality_levels(JNIEnv *,jobject,jint cameraId)383 android_media_MediaProfiles_native_get_num_image_encoding_quality_levels(JNIEnv* /* env */,
384 jobject /* thiz */,
385 jint cameraId)
386 {
387 ALOGV("native_get_num_image_encoding_quality_levels");
388 return (jint) sProfiles->getImageEncodingQualityLevels(cameraId).size();
389 }
390
391 static jint
android_media_MediaProfiles_native_get_image_encoding_quality_level(JNIEnv * env,jobject,jint cameraId,jint index)392 android_media_MediaProfiles_native_get_image_encoding_quality_level(JNIEnv *env, jobject /* thiz */,
393 jint cameraId, jint index)
394 {
395 ALOGV("native_get_image_encoding_quality_level");
396 Vector<int> levels = sProfiles->getImageEncodingQualityLevels(cameraId);
397 if (index < 0 || index >= (jint) levels.size()) {
398 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
399 return -1;
400 }
401 return static_cast<jint>(levels[index]);
402 }
403 static const JNINativeMethod gMethodsForEncoderCapabilitiesClass[] = {
404 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
405 {"native_get_num_file_formats", "()I", (void *)android_media_MediaProfiles_native_get_num_file_formats},
406 {"native_get_file_format", "(I)I", (void *)android_media_MediaProfiles_native_get_file_format},
407 {"native_get_num_video_encoders", "()I", (void *)android_media_MediaProfiles_native_get_num_video_encoders},
408 {"native_get_num_audio_encoders", "()I", (void *)android_media_MediaProfiles_native_get_num_audio_encoders},
409
410 {"native_get_video_encoder_cap", "(I)Landroid/media/EncoderCapabilities$VideoEncoderCap;",
411 (void *)android_media_MediaProfiles_native_get_video_encoder_cap},
412
413 {"native_get_audio_encoder_cap", "(I)Landroid/media/EncoderCapabilities$AudioEncoderCap;",
414 (void *)android_media_MediaProfiles_native_get_audio_encoder_cap},
415 };
416
417 static const JNINativeMethod gMethodsForCamcorderProfileClass[] = {
418 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
419 {"native_get_camcorder_profile", "(II)Landroid/media/CamcorderProfile;",
420 (void *)android_media_MediaProfiles_native_get_camcorder_profile},
421 {"native_get_camcorder_profiles", "(IIZ)Landroid/media/EncoderProfiles;",
422 (void *)android_media_MediaProfiles_native_get_camcorder_profiles},
423 {"native_has_camcorder_profile", "(II)Z",
424 (void *)android_media_MediaProfiles_native_has_camcorder_profile},
425 };
426
427 static const JNINativeMethod gMethodsForDecoderCapabilitiesClass[] = {
428 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
429 {"native_get_num_video_decoders", "()I", (void *)android_media_MediaProfiles_native_get_num_video_decoders},
430 {"native_get_num_audio_decoders", "()I", (void *)android_media_MediaProfiles_native_get_num_audio_decoders},
431 {"native_get_video_decoder_type", "(I)I", (void *)android_media_MediaProfiles_native_get_video_decoder_type},
432 {"native_get_audio_decoder_type", "(I)I", (void *)android_media_MediaProfiles_native_get_audio_decoder_type},
433 };
434
435 static const JNINativeMethod gMethodsForCameraProfileClass[] = {
436 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
437 {"native_get_num_image_encoding_quality_levels",
438 "(I)I", (void *)android_media_MediaProfiles_native_get_num_image_encoding_quality_levels},
439 {"native_get_image_encoding_quality_level","(II)I", (void *)android_media_MediaProfiles_native_get_image_encoding_quality_level},
440 };
441
442 static const char* const kEncoderCapabilitiesClassPathName = "android/media/EncoderCapabilities";
443 static const char* const kDecoderCapabilitiesClassPathName = "android/media/DecoderCapabilities";
444 static const char* const kCamcorderProfileClassPathName = "android/media/CamcorderProfile";
445 static const char* const kCameraProfileClassPathName = "android/media/CameraProfile";
446
447 // This function only registers the native methods, and is called from
448 // JNI_OnLoad in android_media_MediaPlayer.cpp
register_android_media_MediaProfiles(JNIEnv * env)449 int register_android_media_MediaProfiles(JNIEnv *env)
450 {
451 int ret1 = AndroidRuntime::registerNativeMethods(env,
452 kEncoderCapabilitiesClassPathName,
453 gMethodsForEncoderCapabilitiesClass,
454 NELEM(gMethodsForEncoderCapabilitiesClass));
455
456 int ret2 = AndroidRuntime::registerNativeMethods(env,
457 kCamcorderProfileClassPathName,
458 gMethodsForCamcorderProfileClass,
459 NELEM(gMethodsForCamcorderProfileClass));
460
461 int ret3 = AndroidRuntime::registerNativeMethods(env,
462 kDecoderCapabilitiesClassPathName,
463 gMethodsForDecoderCapabilitiesClass,
464 NELEM(gMethodsForDecoderCapabilitiesClass));
465
466 int ret4 = AndroidRuntime::registerNativeMethods(env,
467 kCameraProfileClassPathName,
468 gMethodsForCameraProfileClass,
469 NELEM(gMethodsForCameraProfileClass));
470
471 // Success if all return values from above are 0
472 return (ret1 || ret2 || ret3 || ret4);
473 }
474