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