android_media_MediaMetadataRetriever.cpp revision faf09ba9405ff019b5ca7e2317debe4ff269d4f8
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 jobject android_media_MediaMetadataRetriever_getFrameAtTime(JNIEnv *env, jobject thiz, jlong timeUs, jint option) 146{ 147 LOGV("getFrameAtTime: %lld us option: %d", timeUs, option); 148 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 149 if (retriever == 0) { 150 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 151 return NULL; 152 } 153 154 // Call native method to retrieve a video frame 155 VideoFrame *videoFrame = NULL; 156 sp<IMemory> frameMemory = retriever->getFrameAtTime(timeUs, option); 157 if (frameMemory != 0) { // cast the shared structure to a VideoFrame object 158 videoFrame = static_cast<VideoFrame *>(frameMemory->pointer()); 159 } 160 if (videoFrame == NULL) { 161 LOGE("getFrameAtTime: videoFrame is a NULL pointer"); 162 return NULL; 163 } 164 165 jobject matrix = NULL; 166 if (videoFrame->mRotationAngle != 0) { 167 LOGD("Create a rotation matrix: %d degrees", videoFrame->mRotationAngle); 168 jclass matrixClazz = env->FindClass("android/graphics/Matrix"); 169 if (matrixClazz == NULL) { 170 jniThrowException(env, "java/lang/RuntimeException", 171 "Can't find android/graphics/Matrix"); 172 return NULL; 173 } 174 jmethodID matrixConstructor = 175 env->GetMethodID(matrixClazz, "<init>", "()V"); 176 if (matrixConstructor == NULL) { 177 jniThrowException(env, "java/lang/RuntimeException", 178 "Can't find Matrix constructor"); 179 return NULL; 180 } 181 matrix = 182 env->NewObject(matrixClazz, matrixConstructor); 183 if (matrix == NULL) { 184 LOGE("Could not create a Matrix object"); 185 return NULL; 186 } 187 188 LOGV("Rotate the matrix: %d degrees", videoFrame->mRotationAngle); 189 jmethodID setRotateMethod = 190 env->GetMethodID(matrixClazz, "setRotate", "(F)V"); 191 if (setRotateMethod == NULL) { 192 jniThrowException(env, "java/lang/RuntimeException", 193 "Can't find Matrix setRotate method"); 194 return NULL; 195 } 196 env->CallVoidMethod(matrix, setRotateMethod, 1.0 * videoFrame->mRotationAngle); 197 env->DeleteLocalRef(matrixClazz); 198 } 199 200 // Create a SkBitmap to hold the pixels 201 SkBitmap *bitmap = new SkBitmap(); 202 if (bitmap == NULL) { 203 LOGE("getFrameAtTime: cannot instantiate a SkBitmap object."); 204 return NULL; 205 } 206 bitmap->setConfig(SkBitmap::kRGB_565_Config, videoFrame->mDisplayWidth, videoFrame->mDisplayHeight); 207 if (!bitmap->allocPixels()) { 208 delete bitmap; 209 LOGE("failed to allocate pixel buffer"); 210 return NULL; 211 } 212 memcpy((uint8_t*)bitmap->getPixels(), (uint8_t*)videoFrame + sizeof(VideoFrame), videoFrame->mSize); 213 214 // Since internally SkBitmap uses reference count to manage the reference to 215 // its pixels, it is important that the pixels (along with SkBitmap) be 216 // available after creating the Bitmap is returned to Java app. 217 jobject jSrcBitmap = env->NewObject(fields.bitmapClazz, 218 fields.bitmapConstructor, (int) bitmap, true, NULL, -1); 219 220 LOGV("Return a new bitmap constructed with the rotation matrix"); 221 return env->CallStaticObjectMethod( 222 fields.bitmapClazz, fields.createBitmapMethod, 223 jSrcBitmap, // source Bitmap 224 0, // x 225 0, // y 226 videoFrame->mDisplayWidth, // width 227 videoFrame->mDisplayHeight, // height 228 matrix, // transform matrix 229 false); // filter 230} 231 232static jbyteArray android_media_MediaMetadataRetriever_extractAlbumArt(JNIEnv *env, jobject thiz) 233{ 234 LOGV("extractAlbumArt"); 235 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 236 if (retriever == 0) { 237 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 238 return NULL; 239 } 240 MediaAlbumArt* mediaAlbumArt = NULL; 241 sp<IMemory> albumArtMemory = retriever->extractAlbumArt(); 242 if (albumArtMemory != 0) { // cast the shared structure to a MediaAlbumArt object 243 mediaAlbumArt = static_cast<MediaAlbumArt *>(albumArtMemory->pointer()); 244 } 245 if (mediaAlbumArt == NULL) { 246 LOGE("extractAlbumArt: Call to extractAlbumArt failed."); 247 return NULL; 248 } 249 250 unsigned int len = mediaAlbumArt->mSize; 251 char* data = (char*) mediaAlbumArt + sizeof(MediaAlbumArt); 252 jbyteArray array = env->NewByteArray(len); 253 if (!array) { // OutOfMemoryError exception has already been thrown. 254 LOGE("extractAlbumArt: OutOfMemoryError is thrown."); 255 } else { 256 jbyte* bytes = env->GetByteArrayElements(array, NULL); 257 if (bytes != NULL) { 258 memcpy(bytes, data, len); 259 env->ReleaseByteArrayElements(array, bytes, 0); 260 } 261 } 262 263 // No need to delete mediaAlbumArt here 264 return array; 265} 266 267static jobject android_media_MediaMetadataRetriever_extractMetadata(JNIEnv *env, jobject thiz, jint keyCode) 268{ 269 LOGV("extractMetadata"); 270 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 271 if (retriever == 0) { 272 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 273 return NULL; 274 } 275 const char* value = retriever->extractMetadata(keyCode); 276 if (!value) { 277 LOGV("extractMetadata: Metadata is not found"); 278 return NULL; 279 } 280 LOGV("extractMetadata: value (%s) for keyCode(%d)", value, keyCode); 281 return env->NewStringUTF(value); 282} 283 284static void android_media_MediaMetadataRetriever_release(JNIEnv *env, jobject thiz) 285{ 286 LOGV("release"); 287 Mutex::Autolock lock(sLock); 288 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 289 delete retriever; 290 setRetriever(env, thiz, 0); 291} 292 293static void android_media_MediaMetadataRetriever_native_finalize(JNIEnv *env, jobject thiz) 294{ 295 LOGV("native_finalize"); 296 297 // No lock is needed, since android_media_MediaMetadataRetriever_release() is protected 298 android_media_MediaMetadataRetriever_release(env, thiz); 299} 300 301// This function gets a field ID, which in turn causes class initialization. 302// It is called from a static block in MediaMetadataRetriever, which won't run until the 303// first time an instance of this class is used. 304static void android_media_MediaMetadataRetriever_native_init(JNIEnv *env) 305{ 306 jclass clazz = env->FindClass(kClassPathName); 307 if (clazz == NULL) { 308 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/media/MediaMetadataRetriever"); 309 return; 310 } 311 312 fields.context = env->GetFieldID(clazz, "mNativeContext", "I"); 313 if (fields.context == NULL) { 314 jniThrowException(env, "java/lang/RuntimeException", "Can't find MediaMetadataRetriever.mNativeContext"); 315 return; 316 } 317 318 fields.bitmapClazz = env->FindClass("android/graphics/Bitmap"); 319 if (fields.bitmapClazz == NULL) { 320 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/graphics/Bitmap"); 321 return; 322 } 323 324 fields.bitmapConstructor = env->GetMethodID(fields.bitmapClazz, "<init>", "(IZ[BI)V"); 325 if (fields.bitmapConstructor == NULL) { 326 jniThrowException(env, "java/lang/RuntimeException", "Can't find Bitmap constructor"); 327 return; 328 } 329 fields.createBitmapMethod = 330 env->GetStaticMethodID(fields.bitmapClazz, "createBitmap", 331 "(Landroid/graphics/Bitmap;IIIILandroid/graphics/Matrix;Z)" 332 "Landroid/graphics/Bitmap;"); 333 if (fields.createBitmapMethod == NULL) { 334 jniThrowException(env, "java/lang/RuntimeException", 335 "Can't find Bitmap.createBitmap method"); 336 return; 337 } 338} 339 340static void android_media_MediaMetadataRetriever_native_setup(JNIEnv *env, jobject thiz) 341{ 342 LOGV("native_setup"); 343 MediaMetadataRetriever* retriever = new MediaMetadataRetriever(); 344 if (retriever == 0) { 345 jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); 346 return; 347 } 348 setRetriever(env, thiz, (int)retriever); 349} 350 351// JNI mapping between Java methods and native methods 352static JNINativeMethod nativeMethods[] = { 353 {"setDataSource", "(Ljava/lang/String;)V", (void *)android_media_MediaMetadataRetriever_setDataSource}, 354 {"setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaMetadataRetriever_setDataSourceFD}, 355 {"setMode", "(I)V", (void *)android_media_MediaMetadataRetriever_setMode}, 356 {"_getFrameAtTime", "(JI)Landroid/graphics/Bitmap;", (void *)android_media_MediaMetadataRetriever_getFrameAtTime}, 357 {"extractMetadata", "(I)Ljava/lang/String;", (void *)android_media_MediaMetadataRetriever_extractMetadata}, 358 {"extractAlbumArt", "()[B", (void *)android_media_MediaMetadataRetriever_extractAlbumArt}, 359 {"release", "()V", (void *)android_media_MediaMetadataRetriever_release}, 360 {"native_finalize", "()V", (void *)android_media_MediaMetadataRetriever_native_finalize}, 361 {"native_setup", "()V", (void *)android_media_MediaMetadataRetriever_native_setup}, 362 {"native_init", "()V", (void *)android_media_MediaMetadataRetriever_native_init}, 363}; 364 365// This function only registers the native methods, and is called from 366// JNI_OnLoad in android_media_MediaPlayer.cpp 367int register_android_media_MediaMetadataRetriever(JNIEnv *env) 368{ 369 return AndroidRuntime::registerNativeMethods 370 (env, kClassPathName, nativeMethods, NELEM(nativeMethods)); 371} 372