blob: 1e5c700e2c32e0d36cf98963e8b3c635cbdba9da [file] [log] [blame]
James Dongc3711942010-01-19 17:45:38 -08001/*
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 "JNIHelp.h"
26#include "android_runtime/AndroidRuntime.h"
27#include <media/MediaProfiles.h>
28
29using namespace android;
30
31static Mutex sLock;
32MediaProfiles *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.
36static void
37android_media_MediaProfiles_native_init(JNIEnv *env)
38{
Steve Block71f2cf12011-10-20 11:56:00 +010039 ALOGV("native_init");
James Dongc3711942010-01-19 17:45:38 -080040 Mutex::Autolock lock(sLock);
41
42 if (sProfiles == NULL) {
43 sProfiles = MediaProfiles::getInstance();
44 }
45}
46
James Dongd32fba42010-02-11 14:32:13 -080047static jint
James Dongc3711942010-01-19 17:45:38 -080048android_media_MediaProfiles_native_get_num_file_formats(JNIEnv *env, jobject thiz)
49{
Steve Block71f2cf12011-10-20 11:56:00 +010050 ALOGV("native_get_num_file_formats");
Ashok Bhat075e9a12014-01-06 13:45:09 +000051 return (jint) sProfiles->getOutputFileFormats().size();
James Dongc3711942010-01-19 17:45:38 -080052}
53
James Dongd32fba42010-02-11 14:32:13 -080054static jint
James Dongc3711942010-01-19 17:45:38 -080055android_media_MediaProfiles_native_get_file_format(JNIEnv *env, jobject thiz, jint index)
56{
Steve Block71f2cf12011-10-20 11:56:00 +010057 ALOGV("native_get_file_format: %d", index);
James Dongc3711942010-01-19 17:45:38 -080058 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 }
James Dongd32fba42010-02-11 14:32:13 -080064 return static_cast<jint>(formats[index]);
James Dongc3711942010-01-19 17:45:38 -080065}
66
James Dongd32fba42010-02-11 14:32:13 -080067static jint
James Dongc3711942010-01-19 17:45:38 -080068android_media_MediaProfiles_native_get_num_video_encoders(JNIEnv *env, jobject thiz)
69{
Steve Block71f2cf12011-10-20 11:56:00 +010070 ALOGV("native_get_num_video_encoders");
James Dongc3711942010-01-19 17:45:38 -080071 return sProfiles->getVideoEncoders().size();
72}
73
74static jobject
75android_media_MediaProfiles_native_get_video_encoder_cap(JNIEnv *env, jobject thiz, jint index)
76{
Steve Block71f2cf12011-10-20 11:56:00 +010077 ALOGV("native_get_video_encoder_cap: %d", index);
James Dongc3711942010-01-19 17:45:38 -080078 Vector<video_encoder> encoders = sProfiles->getVideoEncoders();
79 int nSize = encoders.size();
80 if (index < 0 || index >= nSize) {
81 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
82 return NULL;
83 }
84
85 video_encoder encoder = encoders[index];
86 int minBitRate = sProfiles->getVideoEncoderParamByName("enc.vid.bps.min", encoder);
87 int maxBitRate = sProfiles->getVideoEncoderParamByName("enc.vid.bps.max", encoder);
88 int minFrameRate = sProfiles->getVideoEncoderParamByName("enc.vid.fps.min", encoder);
89 int maxFrameRate = sProfiles->getVideoEncoderParamByName("enc.vid.fps.max", encoder);
90 int minFrameWidth = sProfiles->getVideoEncoderParamByName("enc.vid.width.min", encoder);
91 int maxFrameWidth = sProfiles->getVideoEncoderParamByName("enc.vid.width.max", encoder);
92 int minFrameHeight = sProfiles->getVideoEncoderParamByName("enc.vid.height.min", encoder);
93 int maxFrameHeight = sProfiles->getVideoEncoderParamByName("enc.vid.height.max", encoder);
94
95 // Check on the values retrieved
96 if ((minBitRate == -1 || maxBitRate == -1) ||
97 (minFrameRate == -1 || maxFrameRate == -1) ||
98 (minFrameWidth == -1 || maxFrameWidth == -1) ||
99 (minFrameHeight == -1 || maxFrameHeight == -1)) {
100
101 jniThrowException(env, "java/lang/RuntimeException", "Error retrieving video encoder capability params");
102 return NULL;
103 }
104
105 // Construct an instance of the VideoEncoderCap and set its member variables
106 jclass videoEncoderCapClazz = env->FindClass("android/media/EncoderCapabilities$VideoEncoderCap");
107 jmethodID videoEncoderCapConstructorMethodID = env->GetMethodID(videoEncoderCapClazz, "<init>", "(IIIIIIIII)V");
108 jobject cap = env->NewObject(videoEncoderCapClazz,
109 videoEncoderCapConstructorMethodID,
110 static_cast<int>(encoder),
111 minBitRate, maxBitRate,
112 minFrameRate, maxFrameRate,
113 minFrameWidth, maxFrameWidth,
114 minFrameHeight, maxFrameHeight);
115 return cap;
116}
117
James Dongd32fba42010-02-11 14:32:13 -0800118static jint
James Dongc3711942010-01-19 17:45:38 -0800119android_media_MediaProfiles_native_get_num_audio_encoders(JNIEnv *env, jobject thiz)
120{
Steve Block71f2cf12011-10-20 11:56:00 +0100121 ALOGV("native_get_num_audio_encoders");
Ashok Bhat075e9a12014-01-06 13:45:09 +0000122 return (jint) sProfiles->getAudioEncoders().size();
James Dongc3711942010-01-19 17:45:38 -0800123}
124
125static jobject
126android_media_MediaProfiles_native_get_audio_encoder_cap(JNIEnv *env, jobject thiz, jint index)
127{
Steve Block71f2cf12011-10-20 11:56:00 +0100128 ALOGV("native_get_audio_encoder_cap: %d", index);
James Dongc3711942010-01-19 17:45:38 -0800129 Vector<audio_encoder> encoders = sProfiles->getAudioEncoders();
130 int nSize = encoders.size();
131 if (index < 0 || index >= nSize) {
132 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
133 return NULL;
134 }
135
136 audio_encoder encoder = encoders[index];
137 int minBitRate = sProfiles->getAudioEncoderParamByName("enc.aud.bps.min", encoder);
138 int maxBitRate = sProfiles->getAudioEncoderParamByName("enc.aud.bps.max", encoder);
139 int minSampleRate = sProfiles->getAudioEncoderParamByName("enc.aud.hz.min", encoder);
140 int maxSampleRate = sProfiles->getAudioEncoderParamByName("enc.aud.hz.max", encoder);
141 int minChannels = sProfiles->getAudioEncoderParamByName("enc.aud.ch.min", encoder);
142 int maxChannels = sProfiles->getAudioEncoderParamByName("enc.aud.ch.max", encoder);
143
144 // Check on the values retrieved
145 if ((minBitRate == -1 || maxBitRate == -1) ||
146 (minSampleRate == -1 || maxSampleRate == -1) ||
147 (minChannels == -1 || maxChannels == -1)) {
148
149 jniThrowException(env, "java/lang/RuntimeException", "Error retrieving video encoder capability params");
150 return NULL;
151 }
152
153 jclass audioEncoderCapClazz = env->FindClass("android/media/EncoderCapabilities$AudioEncoderCap");
154 jmethodID audioEncoderCapConstructorMethodID = env->GetMethodID(audioEncoderCapClazz, "<init>", "(IIIIIII)V");
155 jobject cap = env->NewObject(audioEncoderCapClazz,
156 audioEncoderCapConstructorMethodID,
157 static_cast<int>(encoder),
158 minBitRate, maxBitRate,
159 minSampleRate, maxSampleRate,
160 minChannels, maxChannels);
161 return cap;
162}
163
James Dong07b9ae32011-09-19 16:27:31 -0700164static bool isCamcorderQualityKnown(int quality)
165{
166 return ((quality >= CAMCORDER_QUALITY_LIST_START &&
167 quality <= CAMCORDER_QUALITY_LIST_END) ||
168 (quality >= CAMCORDER_QUALITY_TIME_LAPSE_LIST_START &&
169 quality <= CAMCORDER_QUALITY_TIME_LAPSE_LIST_END));
170}
171
James Donge7038ac2010-02-03 16:50:18 -0800172static jobject
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800173android_media_MediaProfiles_native_get_camcorder_profile(JNIEnv *env, jobject thiz, jint id, jint quality)
James Donge7038ac2010-02-03 16:50:18 -0800174{
Steve Block71f2cf12011-10-20 11:56:00 +0100175 ALOGV("native_get_camcorder_profile: %d %d", id, quality);
James Dong07b9ae32011-09-19 16:27:31 -0700176 if (!isCamcorderQualityKnown(quality)) {
James Donge7038ac2010-02-03 16:50:18 -0800177 jniThrowException(env, "java/lang/RuntimeException", "Unknown camcorder profile quality");
178 return NULL;
179 }
180
181 camcorder_quality q = static_cast<camcorder_quality>(quality);
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800182 int duration = sProfiles->getCamcorderProfileParamByName("duration", id, q);
183 int fileFormat = sProfiles->getCamcorderProfileParamByName("file.format", id, q);
184 int videoCodec = sProfiles->getCamcorderProfileParamByName("vid.codec", id, q);
185 int videoBitRate = sProfiles->getCamcorderProfileParamByName("vid.bps", id, q);
186 int videoFrameRate = sProfiles->getCamcorderProfileParamByName("vid.fps", id, q);
187 int videoFrameWidth = sProfiles->getCamcorderProfileParamByName("vid.width", id, q);
188 int videoFrameHeight = sProfiles->getCamcorderProfileParamByName("vid.height", id, q);
189 int audioCodec = sProfiles->getCamcorderProfileParamByName("aud.codec", id, q);
190 int audioBitRate = sProfiles->getCamcorderProfileParamByName("aud.bps", id, q);
191 int audioSampleRate = sProfiles->getCamcorderProfileParamByName("aud.hz", id, q);
192 int audioChannels = sProfiles->getCamcorderProfileParamByName("aud.ch", id, q);
James Donge7038ac2010-02-03 16:50:18 -0800193
194 // Check on the values retrieved
James Dong9b433f02010-02-23 17:21:44 -0800195 if (duration == -1 || fileFormat == -1 || videoCodec == -1 || audioCodec == -1 ||
James Donge7038ac2010-02-03 16:50:18 -0800196 videoBitRate == -1 || videoFrameRate == -1 || videoFrameWidth == -1 || videoFrameHeight == -1 ||
197 audioBitRate == -1 || audioSampleRate == -1 || audioChannels == -1) {
198
199 jniThrowException(env, "java/lang/RuntimeException", "Error retrieving camcorder profile params");
200 return NULL;
201 }
202
203 jclass camcorderProfileClazz = env->FindClass("android/media/CamcorderProfile");
James Dong9b433f02010-02-23 17:21:44 -0800204 jmethodID camcorderProfileConstructorMethodID = env->GetMethodID(camcorderProfileClazz, "<init>", "(IIIIIIIIIIII)V");
James Donge7038ac2010-02-03 16:50:18 -0800205 return env->NewObject(camcorderProfileClazz,
206 camcorderProfileConstructorMethodID,
James Dong9b433f02010-02-23 17:21:44 -0800207 duration,
James Donge7038ac2010-02-03 16:50:18 -0800208 quality,
209 fileFormat,
210 videoCodec,
211 videoBitRate,
212 videoFrameRate,
213 videoFrameWidth,
214 videoFrameHeight,
215 audioCodec,
216 audioBitRate,
217 audioSampleRate,
218 audioChannels);
219}
220
Nipun Kwatra9d619542010-09-09 16:25:08 -0700221static jboolean
222android_media_MediaProfiles_native_has_camcorder_profile(JNIEnv *env, jobject thiz, jint id, jint quality)
223{
Steve Block71f2cf12011-10-20 11:56:00 +0100224 ALOGV("native_has_camcorder_profile: %d %d", id, quality);
James Dong07b9ae32011-09-19 16:27:31 -0700225 if (!isCamcorderQualityKnown(quality)) {
Ashok Bhat075e9a12014-01-06 13:45:09 +0000226 return JNI_FALSE;
Nipun Kwatra9d619542010-09-09 16:25:08 -0700227 }
228
229 camcorder_quality q = static_cast<camcorder_quality>(quality);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000230 return sProfiles->hasCamcorderProfile(id, q) ? JNI_TRUE : JNI_FALSE;
Nipun Kwatra9d619542010-09-09 16:25:08 -0700231}
232
James Dongd32fba42010-02-11 14:32:13 -0800233static jint
234android_media_MediaProfiles_native_get_num_video_decoders(JNIEnv *env, jobject thiz)
235{
Steve Block71f2cf12011-10-20 11:56:00 +0100236 ALOGV("native_get_num_video_decoders");
Ashok Bhat075e9a12014-01-06 13:45:09 +0000237 return (jint) sProfiles->getVideoDecoders().size();
James Dongd32fba42010-02-11 14:32:13 -0800238}
239
240static jint
241android_media_MediaProfiles_native_get_video_decoder_type(JNIEnv *env, jobject thiz, jint index)
242{
Steve Block71f2cf12011-10-20 11:56:00 +0100243 ALOGV("native_get_video_decoder_type: %d", index);
James Dongd32fba42010-02-11 14:32:13 -0800244 Vector<video_decoder> decoders = sProfiles->getVideoDecoders();
245 int nSize = decoders.size();
246 if (index < 0 || index >= nSize) {
247 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
248 return -1;
249 }
250
251 return static_cast<jint>(decoders[index]);
252}
253
254static jint
255android_media_MediaProfiles_native_get_num_audio_decoders(JNIEnv *env, jobject thiz)
256{
Steve Block71f2cf12011-10-20 11:56:00 +0100257 ALOGV("native_get_num_audio_decoders");
Ashok Bhat075e9a12014-01-06 13:45:09 +0000258 return (jint) sProfiles->getAudioDecoders().size();
James Dongd32fba42010-02-11 14:32:13 -0800259}
260
261static jint
262android_media_MediaProfiles_native_get_audio_decoder_type(JNIEnv *env, jobject thiz, jint index)
263{
Steve Block71f2cf12011-10-20 11:56:00 +0100264 ALOGV("native_get_audio_decoder_type: %d", index);
James Dongd32fba42010-02-11 14:32:13 -0800265 Vector<audio_decoder> decoders = sProfiles->getAudioDecoders();
266 int nSize = decoders.size();
267 if (index < 0 || index >= nSize) {
268 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
269 return -1;
270 }
271
272 return static_cast<jint>(decoders[index]);
273}
274
James Dong9b433f02010-02-23 17:21:44 -0800275static jint
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800276android_media_MediaProfiles_native_get_num_image_encoding_quality_levels(JNIEnv *env, jobject thiz, jint cameraId)
James Dong9b433f02010-02-23 17:21:44 -0800277{
Steve Block71f2cf12011-10-20 11:56:00 +0100278 ALOGV("native_get_num_image_encoding_quality_levels");
Ashok Bhat075e9a12014-01-06 13:45:09 +0000279 return (jint) sProfiles->getImageEncodingQualityLevels(cameraId).size();
James Dong9b433f02010-02-23 17:21:44 -0800280}
281
282static jint
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800283android_media_MediaProfiles_native_get_image_encoding_quality_level(JNIEnv *env, jobject thiz, jint cameraId, jint index)
James Dong9b433f02010-02-23 17:21:44 -0800284{
Steve Block71f2cf12011-10-20 11:56:00 +0100285 ALOGV("native_get_image_encoding_quality_level");
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800286 Vector<int> levels = sProfiles->getImageEncodingQualityLevels(cameraId);
Ashok Bhat075e9a12014-01-06 13:45:09 +0000287 if (index < 0 || index >= (jint) levels.size()) {
James Dong9b433f02010-02-23 17:21:44 -0800288 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
289 return -1;
290 }
291 return static_cast<jint>(levels[index]);
292}
James Donge7038ac2010-02-03 16:50:18 -0800293static JNINativeMethod gMethodsForEncoderCapabilitiesClass[] = {
James Dongc3711942010-01-19 17:45:38 -0800294 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
295 {"native_get_num_file_formats", "()I", (void *)android_media_MediaProfiles_native_get_num_file_formats},
296 {"native_get_file_format", "(I)I", (void *)android_media_MediaProfiles_native_get_file_format},
297 {"native_get_num_video_encoders", "()I", (void *)android_media_MediaProfiles_native_get_num_video_encoders},
298 {"native_get_num_audio_encoders", "()I", (void *)android_media_MediaProfiles_native_get_num_audio_encoders},
299
300 {"native_get_video_encoder_cap", "(I)Landroid/media/EncoderCapabilities$VideoEncoderCap;",
301 (void *)android_media_MediaProfiles_native_get_video_encoder_cap},
302
303 {"native_get_audio_encoder_cap", "(I)Landroid/media/EncoderCapabilities$AudioEncoderCap;",
304 (void *)android_media_MediaProfiles_native_get_audio_encoder_cap},
305};
306
James Donge7038ac2010-02-03 16:50:18 -0800307static JNINativeMethod gMethodsForCamcorderProfileClass[] = {
308 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800309 {"native_get_camcorder_profile", "(II)Landroid/media/CamcorderProfile;",
James Donge7038ac2010-02-03 16:50:18 -0800310 (void *)android_media_MediaProfiles_native_get_camcorder_profile},
Nipun Kwatra9d619542010-09-09 16:25:08 -0700311 {"native_has_camcorder_profile", "(II)Z",
312 (void *)android_media_MediaProfiles_native_has_camcorder_profile},
James Donge7038ac2010-02-03 16:50:18 -0800313};
314
James Dongd32fba42010-02-11 14:32:13 -0800315static JNINativeMethod gMethodsForDecoderCapabilitiesClass[] = {
316 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
317 {"native_get_num_video_decoders", "()I", (void *)android_media_MediaProfiles_native_get_num_video_decoders},
318 {"native_get_num_audio_decoders", "()I", (void *)android_media_MediaProfiles_native_get_num_audio_decoders},
319 {"native_get_video_decoder_type", "(I)I", (void *)android_media_MediaProfiles_native_get_video_decoder_type},
320 {"native_get_audio_decoder_type", "(I)I", (void *)android_media_MediaProfiles_native_get_audio_decoder_type},
321};
322
James Dong9b433f02010-02-23 17:21:44 -0800323static JNINativeMethod gMethodsForCameraProfileClass[] = {
324 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init},
325 {"native_get_num_image_encoding_quality_levels",
Chih-Chung Chang09b90052010-06-22 20:50:55 +0800326 "(I)I", (void *)android_media_MediaProfiles_native_get_num_image_encoding_quality_levels},
327 {"native_get_image_encoding_quality_level","(II)I", (void *)android_media_MediaProfiles_native_get_image_encoding_quality_level},
James Dong9b433f02010-02-23 17:21:44 -0800328};
329
James Donge7038ac2010-02-03 16:50:18 -0800330static const char* const kEncoderCapabilitiesClassPathName = "android/media/EncoderCapabilities";
James Dongd32fba42010-02-11 14:32:13 -0800331static const char* const kDecoderCapabilitiesClassPathName = "android/media/DecoderCapabilities";
James Donge7038ac2010-02-03 16:50:18 -0800332static const char* const kCamcorderProfileClassPathName = "android/media/CamcorderProfile";
James Dong9b433f02010-02-23 17:21:44 -0800333static const char* const kCameraProfileClassPathName = "android/media/CameraProfile";
James Dongc3711942010-01-19 17:45:38 -0800334
335// This function only registers the native methods, and is called from
336// JNI_OnLoad in android_media_MediaPlayer.cpp
337int register_android_media_MediaProfiles(JNIEnv *env)
338{
James Donge7038ac2010-02-03 16:50:18 -0800339 int ret1 = AndroidRuntime::registerNativeMethods(env,
340 kEncoderCapabilitiesClassPathName,
341 gMethodsForEncoderCapabilitiesClass,
342 NELEM(gMethodsForEncoderCapabilitiesClass));
343
344 int ret2 = AndroidRuntime::registerNativeMethods(env,
345 kCamcorderProfileClassPathName,
346 gMethodsForCamcorderProfileClass,
347 NELEM(gMethodsForCamcorderProfileClass));
348
James Dongd32fba42010-02-11 14:32:13 -0800349 int ret3 = AndroidRuntime::registerNativeMethods(env,
350 kDecoderCapabilitiesClassPathName,
351 gMethodsForDecoderCapabilitiesClass,
352 NELEM(gMethodsForDecoderCapabilitiesClass));
353
James Dong9b433f02010-02-23 17:21:44 -0800354 int ret4 = AndroidRuntime::registerNativeMethods(env,
355 kCameraProfileClassPathName,
356 gMethodsForCameraProfileClass,
357 NELEM(gMethodsForCameraProfileClass));
358
359 // Success if all return values from above are 0
Lajos Molnare0e77cb2014-04-21 15:34:02 -0700360 return (ret1 || ret2 || ret3 || ret4);
James Dongc3711942010-01-19 17:45:38 -0800361}