android_media_MediaMetadataRetriever.cpp revision 53ebc72fd83f83bb5536d5917390aae03b7f5cad
1/* 2** 3** Copyright 2008, The Android Open Source Project 4** 5** Licensed under the Apache License, Version 2.0 (the "License"); 6** you may not use this file except in compliance with the License. 7** You may obtain a copy of the License at 8** 9** http://www.apache.org/licenses/LICENSE-2.0 10** 11** Unless required by applicable law or agreed to in writing, software 12** distributed under the License is distributed on an "AS IS" BASIS, 13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14** See the License for the specific language governing permissions and 15** limitations under the License. 16*/ 17 18//#define LOG_NDEBUG 0 19#define LOG_TAG "MediaMetadataRetrieverJNI" 20 21#include <assert.h> 22#include <utils/Log.h> 23#include <utils/threads.h> 24#include <core/SkBitmap.h> 25#include <media/mediametadataretriever.h> 26#include <private/media/VideoFrame.h> 27 28#include "jni.h" 29#include "JNIHelp.h" 30#include "android_runtime/AndroidRuntime.h" 31 32 33using namespace android; 34 35struct fields_t { 36 jfieldID context; 37 jclass bitmapClazz; 38 jmethodID bitmapConstructor; 39 jmethodID createBitmapMethod; 40}; 41 42static fields_t fields; 43static Mutex sLock; 44static const char* const kClassPathName = "android/media/MediaMetadataRetriever"; 45 46static void process_media_retriever_call(JNIEnv *env, status_t opStatus, const char* exception, const char *message) 47{ 48 if (opStatus == (status_t) INVALID_OPERATION) { 49 jniThrowException(env, "java/lang/IllegalStateException", NULL); 50 } else if (opStatus != (status_t) OK) { 51 if (strlen(message) > 230) { 52 // If the message is too long, don't bother displaying the status code. 53 jniThrowException( env, exception, message); 54 } else { 55 char msg[256]; 56 // Append the status code to the message. 57 sprintf(msg, "%s: status = 0x%X", message, opStatus); 58 jniThrowException( env, exception, msg); 59 } 60 } 61} 62 63static MediaMetadataRetriever* getRetriever(JNIEnv* env, jobject thiz) 64{ 65 // No lock is needed, since it is called internally by other methods that are protected 66 MediaMetadataRetriever* retriever = (MediaMetadataRetriever*) env->GetIntField(thiz, fields.context); 67 return retriever; 68} 69 70static void setRetriever(JNIEnv* env, jobject thiz, int retriever) 71{ 72 // No lock is needed, since it is called internally by other methods that are protected 73 MediaMetadataRetriever *old = (MediaMetadataRetriever*) env->GetIntField(thiz, fields.context); 74 env->SetIntField(thiz, fields.context, retriever); 75} 76 77static void android_media_MediaMetadataRetriever_setDataSource(JNIEnv *env, jobject thiz, jstring path) 78{ 79 LOGV("setDataSource"); 80 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 81 if (retriever == 0) { 82 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 83 return; 84 } 85 if (!path) { 86 jniThrowException(env, "java/lang/IllegalArgumentException", "Null pointer"); 87 return; 88 } 89 90 const char *pathStr = env->GetStringUTFChars(path, NULL); 91 if (!pathStr) { // OutOfMemoryError exception already thrown 92 return; 93 } 94 95 // Don't let somebody trick us in to reading some random block of memory 96 if (strncmp("mem://", pathStr, 6) == 0) { 97 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid pathname"); 98 return; 99 } 100 101 process_media_retriever_call(env, retriever->setDataSource(pathStr), "java/lang/RuntimeException", "setDataSource failed"); 102 env->ReleaseStringUTFChars(path, pathStr); 103} 104 105static void android_media_MediaMetadataRetriever_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length) 106{ 107 LOGV("setDataSource"); 108 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 109 if (retriever == 0) { 110 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 111 return; 112 } 113 if (!fileDescriptor) { 114 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 115 return; 116 } 117 int fd = getParcelFileDescriptorFD(env, fileDescriptor); 118 if (offset < 0 || length < 0 || fd < 0) { 119 if (offset < 0) { 120 LOGE("negative offset (%lld)", offset); 121 } 122 if (length < 0) { 123 LOGE("negative length (%lld)", length); 124 } 125 if (fd < 0) { 126 LOGE("invalid file descriptor"); 127 } 128 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 129 return; 130 } 131 process_media_retriever_call(env, retriever->setDataSource(fd, offset, length), "java/lang/RuntimeException", "setDataSource failed"); 132} 133 134static void android_media_MediaMetadataRetriever_setMode(JNIEnv *env, jobject thiz, jint mode) 135{ 136 LOGV("setMode"); 137 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 138 if (retriever == 0) { 139 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 140 return; 141 } 142 process_media_retriever_call(env, retriever->setMode(mode), "java/lang/RuntimeException", "setMode failed"); 143} 144 145static int android_media_MediaMetadataRetriever_getMode(JNIEnv *env, jobject thiz) 146{ 147 LOGV("getMode"); 148 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 149 if (retriever == 0) { 150 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 151 return -1; // Error 152 } 153 int mode = -1; 154 retriever->getMode(&mode); 155 return mode; 156} 157 158static jobject android_media_MediaMetadataRetriever_captureFrame(JNIEnv *env, jobject thiz) 159{ 160 LOGV("captureFrame"); 161 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 162 if (retriever == 0) { 163 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 164 return NULL; 165 } 166 167 // Call native method to retrieve a video frame 168 VideoFrame *videoFrame = NULL; 169 sp<IMemory> frameMemory = retriever->captureFrame(); 170 if (frameMemory != 0) { // cast the shared structure to a VideoFrame object 171 videoFrame = static_cast<VideoFrame *>(frameMemory->pointer()); 172 } 173 if (videoFrame == NULL) { 174 LOGE("captureFrame: videoFrame is a NULL pointer"); 175 return NULL; 176 } 177 178 jobject matrix = NULL; 179 if (videoFrame->mRotationAngle != 0) { 180 LOGD("Create a rotation matrix: %d degrees", videoFrame->mRotationAngle); 181 jclass matrixClazz = env->FindClass("android/graphics/Matrix"); 182 if (matrixClazz == NULL) { 183 jniThrowException(env, "java/lang/RuntimeException", 184 "Can't find android/graphics/Matrix"); 185 return NULL; 186 } 187 jmethodID matrixConstructor = 188 env->GetMethodID(matrixClazz, "<init>", "()V"); 189 if (matrixConstructor == NULL) { 190 jniThrowException(env, "java/lang/RuntimeException", 191 "Can't find Matrix constructor"); 192 return NULL; 193 } 194 matrix = 195 env->NewObject(matrixClazz, matrixConstructor); 196 if (matrix == NULL) { 197 LOGE("Could not create a Matrix object"); 198 return NULL; 199 } 200 201 LOGV("Rotate the matrix: %d degrees", videoFrame->mRotationAngle); 202 jmethodID setRotateMethod = 203 env->GetMethodID(matrixClazz, "setRotate", "(F)V"); 204 if (setRotateMethod == NULL) { 205 jniThrowException(env, "java/lang/RuntimeException", 206 "Can't find Matrix setRotate method"); 207 return NULL; 208 } 209 env->CallVoidMethod(matrix, setRotateMethod, 1.0 * videoFrame->mRotationAngle); 210 env->DeleteLocalRef(matrixClazz); 211 } 212 213 // Create a SkBitmap to hold the pixels 214 SkBitmap *bitmap = new SkBitmap(); 215 if (bitmap == NULL) { 216 LOGE("captureFrame: cannot instantiate a SkBitmap object."); 217 return NULL; 218 } 219 bitmap->setConfig(SkBitmap::kRGB_565_Config, videoFrame->mDisplayWidth, videoFrame->mDisplayHeight); 220 if (!bitmap->allocPixels()) { 221 delete bitmap; 222 LOGE("failed to allocate pixel buffer"); 223 return NULL; 224 } 225 memcpy((uint8_t*)bitmap->getPixels(), (uint8_t*)videoFrame + sizeof(VideoFrame), videoFrame->mSize); 226 227 // Since internally SkBitmap uses reference count to manage the reference to 228 // its pixels, it is important that the pixels (along with SkBitmap) be 229 // available after creating the Bitmap is returned to Java app. 230 jobject jSrcBitmap = env->NewObject(fields.bitmapClazz, 231 fields.bitmapConstructor, (int) bitmap, true, NULL, -1); 232 233 LOGV("Return a new bitmap constructed with the rotation matrix"); 234 return env->CallStaticObjectMethod( 235 fields.bitmapClazz, fields.createBitmapMethod, 236 jSrcBitmap, // source Bitmap 237 0, // x 238 0, // y 239 videoFrame->mDisplayWidth, // width 240 videoFrame->mDisplayHeight, // height 241 matrix, // transform matrix 242 false); // filter 243} 244 245static jbyteArray android_media_MediaMetadataRetriever_extractAlbumArt(JNIEnv *env, jobject thiz) 246{ 247 LOGV("extractAlbumArt"); 248 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 249 if (retriever == 0) { 250 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 251 return NULL; 252 } 253 MediaAlbumArt* mediaAlbumArt = NULL; 254 sp<IMemory> albumArtMemory = retriever->extractAlbumArt(); 255 if (albumArtMemory != 0) { // cast the shared structure to a MediaAlbumArt object 256 mediaAlbumArt = static_cast<MediaAlbumArt *>(albumArtMemory->pointer()); 257 } 258 if (mediaAlbumArt == NULL) { 259 LOGE("extractAlbumArt: Call to extractAlbumArt failed."); 260 return NULL; 261 } 262 263 unsigned int len = mediaAlbumArt->mSize; 264 char* data = (char*) mediaAlbumArt + sizeof(MediaAlbumArt); 265 jbyteArray array = env->NewByteArray(len); 266 if (!array) { // OutOfMemoryError exception has already been thrown. 267 LOGE("extractAlbumArt: OutOfMemoryError is thrown."); 268 } else { 269 jbyte* bytes = env->GetByteArrayElements(array, NULL); 270 if (bytes != NULL) { 271 memcpy(bytes, data, len); 272 env->ReleaseByteArrayElements(array, bytes, 0); 273 } 274 } 275 276 // No need to delete mediaAlbumArt here 277 return array; 278} 279 280static jobject android_media_MediaMetadataRetriever_extractMetadata(JNIEnv *env, jobject thiz, jint keyCode) 281{ 282 LOGV("extractMetadata"); 283 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 284 if (retriever == 0) { 285 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 286 return NULL; 287 } 288 const char* value = retriever->extractMetadata(keyCode); 289 if (!value) { 290 LOGV("extractMetadata: Metadata is not found"); 291 return NULL; 292 } 293 LOGV("extractMetadata: value (%s) for keyCode(%d)", value, keyCode); 294 return env->NewStringUTF(value); 295} 296 297static void android_media_MediaMetadataRetriever_release(JNIEnv *env, jobject thiz) 298{ 299 LOGV("release"); 300 Mutex::Autolock lock(sLock); 301 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 302 delete retriever; 303 setRetriever(env, thiz, 0); 304} 305 306static void android_media_MediaMetadataRetriever_native_finalize(JNIEnv *env, jobject thiz) 307{ 308 LOGV("native_finalize"); 309 310 // No lock is needed, since android_media_MediaMetadataRetriever_release() is protected 311 android_media_MediaMetadataRetriever_release(env, thiz); 312} 313 314// This function gets a field ID, which in turn causes class initialization. 315// It is called from a static block in MediaMetadataRetriever, which won't run until the 316// first time an instance of this class is used. 317static void android_media_MediaMetadataRetriever_native_init(JNIEnv *env) 318{ 319 jclass clazz = env->FindClass(kClassPathName); 320 if (clazz == NULL) { 321 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/media/MediaMetadataRetriever"); 322 return; 323 } 324 325 fields.context = env->GetFieldID(clazz, "mNativeContext", "I"); 326 if (fields.context == NULL) { 327 jniThrowException(env, "java/lang/RuntimeException", "Can't find MediaMetadataRetriever.mNativeContext"); 328 return; 329 } 330 331 fields.bitmapClazz = env->FindClass("android/graphics/Bitmap"); 332 if (fields.bitmapClazz == NULL) { 333 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/graphics/Bitmap"); 334 return; 335 } 336 337 fields.bitmapConstructor = env->GetMethodID(fields.bitmapClazz, "<init>", "(IZ[BI)V"); 338 if (fields.bitmapConstructor == NULL) { 339 jniThrowException(env, "java/lang/RuntimeException", "Can't find Bitmap constructor"); 340 return; 341 } 342 fields.createBitmapMethod = 343 env->GetStaticMethodID(fields.bitmapClazz, "createBitmap", 344 "(Landroid/graphics/Bitmap;IIIILandroid/graphics/Matrix;Z)" 345 "Landroid/graphics/Bitmap;"); 346 if (fields.createBitmapMethod == NULL) { 347 jniThrowException(env, "java/lang/RuntimeException", 348 "Can't find Bitmap.createBitmap method"); 349 return; 350 } 351} 352 353static void android_media_MediaMetadataRetriever_native_setup(JNIEnv *env, jobject thiz) 354{ 355 LOGV("native_setup"); 356 MediaMetadataRetriever* retriever = new MediaMetadataRetriever(); 357 if (retriever == 0) { 358 jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); 359 return; 360 } 361 setRetriever(env, thiz, (int)retriever); 362} 363 364// JNI mapping between Java methods and native methods 365static JNINativeMethod nativeMethods[] = { 366 {"setDataSource", "(Ljava/lang/String;)V", (void *)android_media_MediaMetadataRetriever_setDataSource}, 367 {"setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaMetadataRetriever_setDataSourceFD}, 368 {"setMode", "(I)V", (void *)android_media_MediaMetadataRetriever_setMode}, 369 {"getMode", "()I", (void *)android_media_MediaMetadataRetriever_getMode}, 370 {"captureFrame", "()Landroid/graphics/Bitmap;", (void *)android_media_MediaMetadataRetriever_captureFrame}, 371 {"extractMetadata", "(I)Ljava/lang/String;", (void *)android_media_MediaMetadataRetriever_extractMetadata}, 372 {"extractAlbumArt", "()[B", (void *)android_media_MediaMetadataRetriever_extractAlbumArt}, 373 {"release", "()V", (void *)android_media_MediaMetadataRetriever_release}, 374 {"native_finalize", "()V", (void *)android_media_MediaMetadataRetriever_native_finalize}, 375 {"native_setup", "()V", (void *)android_media_MediaMetadataRetriever_native_setup}, 376 {"native_init", "()V", (void *)android_media_MediaMetadataRetriever_native_init}, 377}; 378 379// This function only registers the native methods, and is called from 380// JNI_OnLoad in android_media_MediaPlayer.cpp 381int register_android_media_MediaMetadataRetriever(JNIEnv *env) 382{ 383 return AndroidRuntime::registerNativeMethods 384 (env, kClassPathName, nativeMethods, NELEM(nativeMethods)); 385} 386