android_media_MediaProfiles.cpp revision 9b433f0b654d32530b0b48a7a653216ae0bb94d8
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 "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{
39    LOGV("native_init");
40    Mutex::Autolock lock(sLock);
41
42    if (sProfiles == NULL) {
43        sProfiles = MediaProfiles::getInstance();
44    }
45}
46
47static jint
48android_media_MediaProfiles_native_get_num_file_formats(JNIEnv *env, jobject thiz)
49{
50    LOGV("native_get_num_file_formats");
51    return sProfiles->getOutputFileFormats().size();
52}
53
54static jint
55android_media_MediaProfiles_native_get_file_format(JNIEnv *env, jobject thiz, jint index)
56{
57    LOGV("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
67static jint
68android_media_MediaProfiles_native_get_num_video_encoders(JNIEnv *env, jobject thiz)
69{
70    LOGV("native_get_num_video_encoders");
71    return sProfiles->getVideoEncoders().size();
72}
73
74static jobject
75android_media_MediaProfiles_native_get_video_encoder_cap(JNIEnv *env, jobject thiz, jint index)
76{
77    LOGV("native_get_video_encoder_cap: %d", index);
78    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
118static jint
119android_media_MediaProfiles_native_get_num_audio_encoders(JNIEnv *env, jobject thiz)
120{
121    LOGV("native_get_num_audio_encoders");
122    return sProfiles->getAudioEncoders().size();
123}
124
125static jobject
126android_media_MediaProfiles_native_get_audio_encoder_cap(JNIEnv *env, jobject thiz, jint index)
127{
128    LOGV("native_get_audio_encoder_cap: %d", index);
129    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
164static jobject
165android_media_MediaProfiles_native_get_camcorder_profile(JNIEnv *env, jobject thiz, jint quality)
166{
167    LOGV("native_get_camcorder_profile: %d", quality);
168    if (quality != CAMCORDER_QUALITY_HIGH && quality != CAMCORDER_QUALITY_LOW) {
169        jniThrowException(env, "java/lang/RuntimeException", "Unknown camcorder profile quality");
170        return NULL;
171    }
172
173    camcorder_quality q = static_cast<camcorder_quality>(quality);
174    int duration         = sProfiles->getCamcorderProfileParamByName("duration", q);
175    int fileFormat       = sProfiles->getCamcorderProfileParamByName("file.format", q);
176    int videoCodec       = sProfiles->getCamcorderProfileParamByName("vid.codec",   q);
177    int videoBitRate     = sProfiles->getCamcorderProfileParamByName("vid.bps",     q);
178    int videoFrameRate   = sProfiles->getCamcorderProfileParamByName("vid.fps",     q);
179    int videoFrameWidth  = sProfiles->getCamcorderProfileParamByName("vid.width",   q);
180    int videoFrameHeight = sProfiles->getCamcorderProfileParamByName("vid.height",  q);
181    int audioCodec       = sProfiles->getCamcorderProfileParamByName("aud.codec",   q);
182    int audioBitRate     = sProfiles->getCamcorderProfileParamByName("aud.bps",     q);
183    int audioSampleRate  = sProfiles->getCamcorderProfileParamByName("aud.hz",      q);
184    int audioChannels    = sProfiles->getCamcorderProfileParamByName("aud.ch",      q);
185
186    // Check on the values retrieved
187    if (duration == -1 || fileFormat == -1 || videoCodec == -1 || audioCodec == -1 ||
188        videoBitRate == -1 || videoFrameRate == -1 || videoFrameWidth == -1 || videoFrameHeight == -1 ||
189        audioBitRate == -1 || audioSampleRate == -1 || audioChannels == -1) {
190
191        jniThrowException(env, "java/lang/RuntimeException", "Error retrieving camcorder profile params");
192        return NULL;
193    }
194
195    jclass camcorderProfileClazz = env->FindClass("android/media/CamcorderProfile");
196    jmethodID camcorderProfileConstructorMethodID = env->GetMethodID(camcorderProfileClazz, "<init>", "(IIIIIIIIIIII)V");
197    return env->NewObject(camcorderProfileClazz,
198                          camcorderProfileConstructorMethodID,
199                          duration,
200                          quality,
201                          fileFormat,
202                          videoCodec,
203                          videoBitRate,
204                          videoFrameRate,
205                          videoFrameWidth,
206                          videoFrameHeight,
207                          audioCodec,
208                          audioBitRate,
209                          audioSampleRate,
210                          audioChannels);
211}
212
213static jint
214android_media_MediaProfiles_native_get_num_video_decoders(JNIEnv *env, jobject thiz)
215{
216    LOGV("native_get_num_video_decoders");
217    return sProfiles->getVideoDecoders().size();
218}
219
220static jint
221android_media_MediaProfiles_native_get_video_decoder_type(JNIEnv *env, jobject thiz, jint index)
222{
223    LOGV("native_get_video_decoder_type: %d", index);
224    Vector<video_decoder> decoders = sProfiles->getVideoDecoders();
225    int nSize = decoders.size();
226    if (index < 0 || index >= nSize) {
227        jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
228        return -1;
229    }
230
231    return static_cast<jint>(decoders[index]);
232}
233
234static jint
235android_media_MediaProfiles_native_get_num_audio_decoders(JNIEnv *env, jobject thiz)
236{
237    LOGV("native_get_num_audio_decoders");
238    return sProfiles->getAudioDecoders().size();
239}
240
241static jint
242android_media_MediaProfiles_native_get_audio_decoder_type(JNIEnv *env, jobject thiz, jint index)
243{
244    LOGV("native_get_audio_decoder_type: %d", index);
245    Vector<audio_decoder> decoders = sProfiles->getAudioDecoders();
246    int nSize = decoders.size();
247    if (index < 0 || index >= nSize) {
248        jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
249        return -1;
250    }
251
252    return static_cast<jint>(decoders[index]);
253}
254
255static jint
256android_media_MediaProfiles_native_get_num_image_encoding_quality_levels(JNIEnv *env, jobject thiz)
257{
258    LOGV("native_get_num_image_encoding_quality_levels");
259    return sProfiles->getImageEncodingQualityLevels().size();
260}
261
262static jint
263android_media_MediaProfiles_native_get_image_encoding_quality_level(JNIEnv *env, jobject thiz, jint index)
264{
265    LOGV("native_get_image_encoding_quality_level");
266    Vector<int> levels = sProfiles->getImageEncodingQualityLevels();
267    if (index < 0 || index >= levels.size()) {
268        jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
269        return -1;
270    }
271    return static_cast<jint>(levels[index]);
272}
273
274static JNINativeMethod gMethodsForEncoderCapabilitiesClass[] = {
275    {"native_init",                            "()V",                    (void *)android_media_MediaProfiles_native_init},
276    {"native_get_num_file_formats",            "()I",                    (void *)android_media_MediaProfiles_native_get_num_file_formats},
277    {"native_get_file_format",                 "(I)I",                   (void *)android_media_MediaProfiles_native_get_file_format},
278    {"native_get_num_video_encoders",          "()I",                    (void *)android_media_MediaProfiles_native_get_num_video_encoders},
279    {"native_get_num_audio_encoders",          "()I",                    (void *)android_media_MediaProfiles_native_get_num_audio_encoders},
280
281    {"native_get_video_encoder_cap",           "(I)Landroid/media/EncoderCapabilities$VideoEncoderCap;",
282                                                                         (void *)android_media_MediaProfiles_native_get_video_encoder_cap},
283
284    {"native_get_audio_encoder_cap",           "(I)Landroid/media/EncoderCapabilities$AudioEncoderCap;",
285                                                                         (void *)android_media_MediaProfiles_native_get_audio_encoder_cap},
286};
287
288static JNINativeMethod gMethodsForCamcorderProfileClass[] = {
289    {"native_init",                            "()V",                    (void *)android_media_MediaProfiles_native_init},
290    {"native_get_camcorder_profile",           "(I)Landroid/media/CamcorderProfile;",
291                                                                         (void *)android_media_MediaProfiles_native_get_camcorder_profile},
292};
293
294static JNINativeMethod gMethodsForDecoderCapabilitiesClass[] = {
295    {"native_init",                            "()V",                    (void *)android_media_MediaProfiles_native_init},
296    {"native_get_num_video_decoders",          "()I",                    (void *)android_media_MediaProfiles_native_get_num_video_decoders},
297    {"native_get_num_audio_decoders",          "()I",                    (void *)android_media_MediaProfiles_native_get_num_audio_decoders},
298    {"native_get_video_decoder_type",          "(I)I",                   (void *)android_media_MediaProfiles_native_get_video_decoder_type},
299    {"native_get_audio_decoder_type",          "(I)I",                   (void *)android_media_MediaProfiles_native_get_audio_decoder_type},
300};
301
302static JNINativeMethod gMethodsForCameraProfileClass[] = {
303    {"native_init",                            "()V",                    (void *)android_media_MediaProfiles_native_init},
304    {"native_get_num_image_encoding_quality_levels",
305                                               "()I",                    (void *)android_media_MediaProfiles_native_get_num_image_encoding_quality_levels},
306    {"native_get_image_encoding_quality_level","(I)I",                   (void *)android_media_MediaProfiles_native_get_image_encoding_quality_level},
307};
308
309static const char* const kEncoderCapabilitiesClassPathName = "android/media/EncoderCapabilities";
310static const char* const kDecoderCapabilitiesClassPathName = "android/media/DecoderCapabilities";
311static const char* const kCamcorderProfileClassPathName = "android/media/CamcorderProfile";
312static const char* const kCameraProfileClassPathName = "android/media/CameraProfile";
313
314// This function only registers the native methods, and is called from
315// JNI_OnLoad in android_media_MediaPlayer.cpp
316int register_android_media_MediaProfiles(JNIEnv *env)
317{
318    int ret1 = AndroidRuntime::registerNativeMethods(env,
319               kEncoderCapabilitiesClassPathName,
320               gMethodsForEncoderCapabilitiesClass,
321               NELEM(gMethodsForEncoderCapabilitiesClass));
322
323    int ret2 = AndroidRuntime::registerNativeMethods(env,
324               kCamcorderProfileClassPathName,
325               gMethodsForCamcorderProfileClass,
326               NELEM(gMethodsForCamcorderProfileClass));
327
328    int ret3 = AndroidRuntime::registerNativeMethods(env,
329               kDecoderCapabilitiesClassPathName,
330               gMethodsForDecoderCapabilitiesClass,
331               NELEM(gMethodsForDecoderCapabilitiesClass));
332
333    int ret4 = AndroidRuntime::registerNativeMethods(env,
334               kCameraProfileClassPathName,
335               gMethodsForCameraProfileClass,
336               NELEM(gMethodsForCameraProfileClass));
337
338    // Success if all return values from above are 0
339    return (ret1 || ret2 || ret3 || ret4);
340}
341