android_media_MediaProfiles.cpp revision e7038ace44ed6e6cd27be35b003e6dd0412e936f
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 int
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 int
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    int format = static_cast<int>(formats[index]);
65    return format;
66}
67
68static int
69android_media_MediaProfiles_native_get_num_video_encoders(JNIEnv *env, jobject thiz)
70{
71    LOGV("native_get_num_video_encoders");
72    return sProfiles->getVideoEncoders().size();
73}
74
75static jobject
76android_media_MediaProfiles_native_get_video_encoder_cap(JNIEnv *env, jobject thiz, jint index)
77{
78    LOGV("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
119static int
120android_media_MediaProfiles_native_get_num_audio_encoders(JNIEnv *env, jobject thiz)
121{
122    LOGV("native_get_num_audio_encoders");
123    return sProfiles->getAudioEncoders().size();
124}
125
126static jobject
127android_media_MediaProfiles_native_get_audio_encoder_cap(JNIEnv *env, jobject thiz, jint index)
128{
129    LOGV("native_get_audio_encoder_cap: %d", index);
130    Vector<audio_encoder> encoders = sProfiles->getAudioEncoders();
131    int nSize = encoders.size();
132    if (index < 0 || index >= nSize) {
133        jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary");
134        return NULL;
135    }
136
137    audio_encoder encoder = encoders[index];
138    int minBitRate = sProfiles->getAudioEncoderParamByName("enc.aud.bps.min", encoder);
139    int maxBitRate = sProfiles->getAudioEncoderParamByName("enc.aud.bps.max", encoder);
140    int minSampleRate = sProfiles->getAudioEncoderParamByName("enc.aud.hz.min", encoder);
141    int maxSampleRate = sProfiles->getAudioEncoderParamByName("enc.aud.hz.max", encoder);
142    int minChannels = sProfiles->getAudioEncoderParamByName("enc.aud.ch.min", encoder);
143    int maxChannels = sProfiles->getAudioEncoderParamByName("enc.aud.ch.max", encoder);
144
145    // Check on the values retrieved
146    if ((minBitRate == -1 || maxBitRate == -1) ||
147        (minSampleRate == -1 || maxSampleRate == -1) ||
148        (minChannels == -1 || maxChannels == -1)) {
149
150        jniThrowException(env, "java/lang/RuntimeException", "Error retrieving video encoder capability params");
151        return NULL;
152    }
153
154    jclass audioEncoderCapClazz = env->FindClass("android/media/EncoderCapabilities$AudioEncoderCap");
155    jmethodID audioEncoderCapConstructorMethodID = env->GetMethodID(audioEncoderCapClazz, "<init>", "(IIIIIII)V");
156    jobject cap = env->NewObject(audioEncoderCapClazz,
157                                 audioEncoderCapConstructorMethodID,
158                                 static_cast<int>(encoder),
159                                 minBitRate, maxBitRate,
160                                 minSampleRate, maxSampleRate,
161                                 minChannels, maxChannels);
162    return cap;
163}
164
165static jobject
166android_media_MediaProfiles_native_get_camcorder_profile(JNIEnv *env, jobject thiz, jint quality)
167{
168    LOGV("native_get_camcorder_profile: %d", quality);
169    if (quality != CAMCORDER_QUALITY_HIGH && quality != CAMCORDER_QUALITY_LOW) {
170        jniThrowException(env, "java/lang/RuntimeException", "Unknown camcorder profile quality");
171        return NULL;
172    }
173
174    camcorder_quality q = static_cast<camcorder_quality>(quality);
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 (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>", "(IIIIIIIIIII)V");
197    return env->NewObject(camcorderProfileClazz,
198                          camcorderProfileConstructorMethodID,
199                          quality,
200                          fileFormat,
201                          videoCodec,
202                          videoBitRate,
203                          videoFrameRate,
204                          videoFrameWidth,
205                          videoFrameHeight,
206                          audioCodec,
207                          audioBitRate,
208                          audioSampleRate,
209                          audioChannels);
210}
211
212static JNINativeMethod gMethodsForEncoderCapabilitiesClass[] = {
213    {"native_init",                            "()V",                    (void *)android_media_MediaProfiles_native_init},
214    {"native_get_num_file_formats",            "()I",                    (void *)android_media_MediaProfiles_native_get_num_file_formats},
215    {"native_get_file_format",                 "(I)I",                   (void *)android_media_MediaProfiles_native_get_file_format},
216    {"native_get_num_video_encoders",          "()I",                    (void *)android_media_MediaProfiles_native_get_num_video_encoders},
217    {"native_get_num_audio_encoders",          "()I",                    (void *)android_media_MediaProfiles_native_get_num_audio_encoders},
218
219    {"native_get_video_encoder_cap",           "(I)Landroid/media/EncoderCapabilities$VideoEncoderCap;",
220                                                                         (void *)android_media_MediaProfiles_native_get_video_encoder_cap},
221
222    {"native_get_audio_encoder_cap",           "(I)Landroid/media/EncoderCapabilities$AudioEncoderCap;",
223                                                                         (void *)android_media_MediaProfiles_native_get_audio_encoder_cap},
224};
225
226static JNINativeMethod gMethodsForCamcorderProfileClass[] = {
227    {"native_init",                            "()V",                    (void *)android_media_MediaProfiles_native_init},
228    {"native_get_camcorder_profile",           "(I)Landroid/media/CamcorderProfile;",
229                                                                         (void *)android_media_MediaProfiles_native_get_camcorder_profile},
230};
231
232static const char* const kEncoderCapabilitiesClassPathName = "android/media/EncoderCapabilities";
233static const char* const kCamcorderProfileClassPathName = "android/media/CamcorderProfile";
234
235// This function only registers the native methods, and is called from
236// JNI_OnLoad in android_media_MediaPlayer.cpp
237int register_android_media_MediaProfiles(JNIEnv *env)
238{
239    int ret1 = AndroidRuntime::registerNativeMethods(env,
240               kEncoderCapabilitiesClassPathName,
241               gMethodsForEncoderCapabilitiesClass,
242               NELEM(gMethodsForEncoderCapabilitiesClass));
243
244    int ret2 = AndroidRuntime::registerNativeMethods(env,
245               kCamcorderProfileClassPathName,
246               gMethodsForCamcorderProfileClass,
247               NELEM(gMethodsForCamcorderProfileClass));
248
249    // Success if ret1 == 0 && ret2 == 0
250    return (ret1 || ret2);
251}
252