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 LOGV("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 LOGV("native_get_num_file_formats"); 51 return sProfiles->getOutputFileFormats().size(); 52 } 53 54 static jint 55 android_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 67 static jint 68 android_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 74 static jobject 75 android_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 118 static jint 119 android_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 125 static jobject 126 android_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 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 } 171 172 static jobject 173 android_media_MediaProfiles_native_get_camcorder_profile(JNIEnv *env, jobject thiz, jint id, jint quality) 174 { 175 LOGV("native_get_camcorder_profile: %d %d", id, quality); 176 if (!isCamcorderQualityKnown(quality)) { 177 jniThrowException(env, "java/lang/RuntimeException", "Unknown camcorder profile quality"); 178 return NULL; 179 } 180 181 camcorder_quality q = static_cast<camcorder_quality>(quality); 182 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); 193 194 // Check on the values retrieved 195 if (duration == -1 || fileFormat == -1 || videoCodec == -1 || audioCodec == -1 || 196 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"); 204 jmethodID camcorderProfileConstructorMethodID = env->GetMethodID(camcorderProfileClazz, "<init>", "(IIIIIIIIIIII)V"); 205 return env->NewObject(camcorderProfileClazz, 206 camcorderProfileConstructorMethodID, 207 duration, 208 quality, 209 fileFormat, 210 videoCodec, 211 videoBitRate, 212 videoFrameRate, 213 videoFrameWidth, 214 videoFrameHeight, 215 audioCodec, 216 audioBitRate, 217 audioSampleRate, 218 audioChannels); 219 } 220 221 static jboolean 222 android_media_MediaProfiles_native_has_camcorder_profile(JNIEnv *env, jobject thiz, jint id, jint quality) 223 { 224 LOGV("native_has_camcorder_profile: %d %d", id, quality); 225 if (!isCamcorderQualityKnown(quality)) { 226 return false; 227 } 228 229 camcorder_quality q = static_cast<camcorder_quality>(quality); 230 return sProfiles->hasCamcorderProfile(id, q); 231 } 232 233 static jint 234 android_media_MediaProfiles_native_get_num_video_decoders(JNIEnv *env, jobject thiz) 235 { 236 LOGV("native_get_num_video_decoders"); 237 return sProfiles->getVideoDecoders().size(); 238 } 239 240 static jint 241 android_media_MediaProfiles_native_get_video_decoder_type(JNIEnv *env, jobject thiz, jint index) 242 { 243 LOGV("native_get_video_decoder_type: %d", index); 244 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 254 static jint 255 android_media_MediaProfiles_native_get_num_audio_decoders(JNIEnv *env, jobject thiz) 256 { 257 LOGV("native_get_num_audio_decoders"); 258 return sProfiles->getAudioDecoders().size(); 259 } 260 261 static jint 262 android_media_MediaProfiles_native_get_audio_decoder_type(JNIEnv *env, jobject thiz, jint index) 263 { 264 LOGV("native_get_audio_decoder_type: %d", index); 265 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 275 static jint 276 android_media_MediaProfiles_native_get_num_image_encoding_quality_levels(JNIEnv *env, jobject thiz, jint cameraId) 277 { 278 LOGV("native_get_num_image_encoding_quality_levels"); 279 return sProfiles->getImageEncodingQualityLevels(cameraId).size(); 280 } 281 282 static jint 283 android_media_MediaProfiles_native_get_image_encoding_quality_level(JNIEnv *env, jobject thiz, jint cameraId, jint index) 284 { 285 LOGV("native_get_image_encoding_quality_level"); 286 Vector<int> levels = sProfiles->getImageEncodingQualityLevels(cameraId); 287 if (index < 0 || index >= levels.size()) { 288 jniThrowException(env, "java/lang/IllegalArgumentException", "out of array boundary"); 289 return -1; 290 } 291 return static_cast<jint>(levels[index]); 292 } 293 static jobject 294 android_media_MediaProfiles_native_get_videoeditor_profile(JNIEnv *env, jobject thiz) 295 { 296 LOGV("native_get_videoeditor_profile"); 297 298 int maxInputFrameWidth = 299 sProfiles->getVideoEditorCapParamByName("videoeditor.input.width.max"); 300 int maxInputFrameHeight = 301 sProfiles->getVideoEditorCapParamByName("videoeditor.input.height.max"); 302 int maxOutputFrameWidth = 303 sProfiles->getVideoEditorCapParamByName("videoeditor.output.width.max"); 304 int maxOutputFrameHeight = 305 sProfiles->getVideoEditorCapParamByName("videoeditor.output.height.max"); 306 307 // Check on the values retrieved 308 if (maxInputFrameWidth == -1 || maxInputFrameHeight == -1 || 309 maxOutputFrameWidth == -1 || maxOutputFrameHeight == -1) { 310 311 jniThrowException(env, "java/lang/RuntimeException",\ 312 "Error retrieving videoeditor profile params"); 313 return NULL; 314 } 315 LOGV("native_get_videoeditor_profile \ 316 inWidth:%d inHeight:%d,outWidth:%d, outHeight:%d",\ 317 maxInputFrameWidth,maxInputFrameHeight,\ 318 maxOutputFrameWidth,maxOutputFrameHeight); 319 320 jclass VideoEditorProfileClazz = 321 env->FindClass("android/media/videoeditor/VideoEditorProfile"); 322 jmethodID VideoEditorProfileConstructorMethodID = 323 env->GetMethodID(VideoEditorProfileClazz, "<init>", "(IIII)V"); 324 return env->NewObject(VideoEditorProfileClazz, 325 VideoEditorProfileConstructorMethodID, 326 maxInputFrameWidth, 327 maxInputFrameHeight, 328 maxOutputFrameWidth, 329 maxOutputFrameHeight); 330 } 331 static jint 332 android_media_MediaProfiles_native_get_videoeditor_export_profile( 333 JNIEnv *env, jobject thiz, jint codec) 334 { 335 LOGV("android_media_MediaProfiles_native_get_export_profile index "); 336 int profile =0; 337 profile = sProfiles->getVideoEditorExportParamByName("videoeditor.export.profile", codec); 338 // Check the values retrieved 339 if (profile == -1) { 340 jniThrowException(env, "java/lang/RuntimeException",\ 341 "Error retrieving videoeditor export profile params"); 342 return -1; 343 } 344 return static_cast<jint>(profile); 345 } 346 347 static jint 348 android_media_MediaProfiles_native_get_videoeditor_export_level( 349 JNIEnv *env, jobject thiz, jint codec) 350 { 351 LOGV("android_media_MediaProfiles_native_get_export_level"); 352 int level =0; 353 level = sProfiles->getVideoEditorExportParamByName("videoeditor.export.level", codec); 354 // Check the values retrieved 355 if (level == -1) { 356 jniThrowException(env, "java/lang/RuntimeException",\ 357 "Error retrieving videoeditor export level params"); 358 return -1; 359 } 360 return static_cast<jint>(level); 361 } 362 static JNINativeMethod gMethodsForEncoderCapabilitiesClass[] = { 363 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init}, 364 {"native_get_num_file_formats", "()I", (void *)android_media_MediaProfiles_native_get_num_file_formats}, 365 {"native_get_file_format", "(I)I", (void *)android_media_MediaProfiles_native_get_file_format}, 366 {"native_get_num_video_encoders", "()I", (void *)android_media_MediaProfiles_native_get_num_video_encoders}, 367 {"native_get_num_audio_encoders", "()I", (void *)android_media_MediaProfiles_native_get_num_audio_encoders}, 368 369 {"native_get_video_encoder_cap", "(I)Landroid/media/EncoderCapabilities$VideoEncoderCap;", 370 (void *)android_media_MediaProfiles_native_get_video_encoder_cap}, 371 372 {"native_get_audio_encoder_cap", "(I)Landroid/media/EncoderCapabilities$AudioEncoderCap;", 373 (void *)android_media_MediaProfiles_native_get_audio_encoder_cap}, 374 }; 375 376 static JNINativeMethod gMethodsForCamcorderProfileClass[] = { 377 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init}, 378 {"native_get_camcorder_profile", "(II)Landroid/media/CamcorderProfile;", 379 (void *)android_media_MediaProfiles_native_get_camcorder_profile}, 380 {"native_has_camcorder_profile", "(II)Z", 381 (void *)android_media_MediaProfiles_native_has_camcorder_profile}, 382 }; 383 384 static JNINativeMethod gMethodsForDecoderCapabilitiesClass[] = { 385 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init}, 386 {"native_get_num_video_decoders", "()I", (void *)android_media_MediaProfiles_native_get_num_video_decoders}, 387 {"native_get_num_audio_decoders", "()I", (void *)android_media_MediaProfiles_native_get_num_audio_decoders}, 388 {"native_get_video_decoder_type", "(I)I", (void *)android_media_MediaProfiles_native_get_video_decoder_type}, 389 {"native_get_audio_decoder_type", "(I)I", (void *)android_media_MediaProfiles_native_get_audio_decoder_type}, 390 }; 391 392 static JNINativeMethod gMethodsForCameraProfileClass[] = { 393 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init}, 394 {"native_get_num_image_encoding_quality_levels", 395 "(I)I", (void *)android_media_MediaProfiles_native_get_num_image_encoding_quality_levels}, 396 {"native_get_image_encoding_quality_level","(II)I", (void *)android_media_MediaProfiles_native_get_image_encoding_quality_level}, 397 }; 398 399 static JNINativeMethod gMethodsForVideoEditorProfileClass[] = { 400 {"native_init", "()V", (void *)android_media_MediaProfiles_native_init}, 401 {"native_get_videoeditor_profile", "()Landroid/media/videoeditor/VideoEditorProfile;", (void *)android_media_MediaProfiles_native_get_videoeditor_profile}, 402 {"native_get_videoeditor_export_profile", "(I)I", (void *)android_media_MediaProfiles_native_get_videoeditor_export_profile}, 403 {"native_get_videoeditor_export_level", "(I)I", (void *)android_media_MediaProfiles_native_get_videoeditor_export_level}, 404 }; 405 406 static const char* const kEncoderCapabilitiesClassPathName = "android/media/EncoderCapabilities"; 407 static const char* const kDecoderCapabilitiesClassPathName = "android/media/DecoderCapabilities"; 408 static const char* const kCamcorderProfileClassPathName = "android/media/CamcorderProfile"; 409 static const char* const kCameraProfileClassPathName = "android/media/CameraProfile"; 410 static const char* const kVideoEditorProfileClassPathName = 411 "android/media/videoeditor/VideoEditorProfile"; 412 413 // This function only registers the native methods, and is called from 414 // JNI_OnLoad in android_media_MediaPlayer.cpp 415 int register_android_media_MediaProfiles(JNIEnv *env) 416 { 417 int ret1 = AndroidRuntime::registerNativeMethods(env, 418 kEncoderCapabilitiesClassPathName, 419 gMethodsForEncoderCapabilitiesClass, 420 NELEM(gMethodsForEncoderCapabilitiesClass)); 421 422 int ret2 = AndroidRuntime::registerNativeMethods(env, 423 kCamcorderProfileClassPathName, 424 gMethodsForCamcorderProfileClass, 425 NELEM(gMethodsForCamcorderProfileClass)); 426 427 int ret3 = AndroidRuntime::registerNativeMethods(env, 428 kDecoderCapabilitiesClassPathName, 429 gMethodsForDecoderCapabilitiesClass, 430 NELEM(gMethodsForDecoderCapabilitiesClass)); 431 432 int ret4 = AndroidRuntime::registerNativeMethods(env, 433 kCameraProfileClassPathName, 434 gMethodsForCameraProfileClass, 435 NELEM(gMethodsForCameraProfileClass)); 436 437 int ret5 = AndroidRuntime::registerNativeMethods(env, 438 kVideoEditorProfileClassPathName, 439 gMethodsForVideoEditorProfileClass, 440 NELEM(gMethodsForVideoEditorProfileClass)); 441 442 // Success if all return values from above are 0 443 return (ret1 || ret2 || ret3 || ret4 || ret5); 444 } 445