android_media_MediaMetadataRetriever.cpp revision db6cc0ba71a873ad0258e850cb939639b7857c26
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}; 40 41static fields_t fields; 42static Mutex sLock; 43static const char* const kClassPathName = "android/media/MediaMetadataRetriever"; 44 45static void process_media_retriever_call(JNIEnv *env, status_t opStatus, const char* exception, const char *message) 46{ 47 if (opStatus == (status_t) INVALID_OPERATION) { 48 jniThrowException(env, "java/lang/IllegalStateException", NULL); 49 } else if (opStatus != (status_t) OK) { 50 if (strlen(message) > 230) { 51 // If the message is too long, don't bother displaying the status code. 52 jniThrowException( env, exception, message); 53 } else { 54 char msg[256]; 55 // Append the status code to the message. 56 sprintf(msg, "%s: status = 0x%X", message, opStatus); 57 jniThrowException( env, exception, msg); 58 } 59 } 60} 61 62static MediaMetadataRetriever* getRetriever(JNIEnv* env, jobject thiz) 63{ 64 // No lock is needed, since it is called internally by other methods that are protected 65 MediaMetadataRetriever* retriever = (MediaMetadataRetriever*) env->GetIntField(thiz, fields.context); 66 return retriever; 67} 68 69static void setRetriever(JNIEnv* env, jobject thiz, int retriever) 70{ 71 // No lock is needed, since it is called internally by other methods that are protected 72 MediaMetadataRetriever *old = (MediaMetadataRetriever*) env->GetIntField(thiz, fields.context); 73 env->SetIntField(thiz, fields.context, retriever); 74} 75 76static void android_media_MediaMetadataRetriever_setDataSource(JNIEnv *env, jobject thiz, jstring path) 77{ 78 LOGV("setDataSource"); 79 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 80 if (retriever == 0) { 81 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 82 return; 83 } 84 if (!path) { 85 jniThrowException(env, "java/lang/IllegalArgumentException", "Null pointer"); 86 return; 87 } 88 89 const char *pathStr = env->GetStringUTFChars(path, NULL); 90 if (!pathStr) { // OutOfMemoryError exception already thrown 91 return; 92 } 93 94 // Don't let somebody trick us in to reading some random block of memory 95 if (strncmp("mem://", pathStr, 6) == 0) { 96 jniThrowException(env, "java/lang/IllegalArgumentException", "Invalid pathname"); 97 return; 98 } 99 100 process_media_retriever_call(env, retriever->setDataSource(pathStr), "java/lang/RuntimeException", "setDataSource failed"); 101 env->ReleaseStringUTFChars(path, pathStr); 102} 103 104static void android_media_MediaMetadataRetriever_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length) 105{ 106 LOGV("setDataSource"); 107 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 108 if (retriever == 0) { 109 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 110 return; 111 } 112 if (!fileDescriptor) { 113 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 114 return; 115 } 116 int fd = getParcelFileDescriptorFD(env, fileDescriptor); 117 if (offset < 0 || length < 0 || fd < 0) { 118 if (offset < 0) { 119 LOGE("negative offset (%lld)", offset); 120 } 121 if (length < 0) { 122 LOGE("negative length (%lld)", length); 123 } 124 if (fd < 0) { 125 LOGE("invalid file descriptor"); 126 } 127 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 128 return; 129 } 130 process_media_retriever_call(env, retriever->setDataSource(fd, offset, length), "java/lang/RuntimeException", "setDataSource failed"); 131} 132 133static void android_media_MediaMetadataRetriever_setMode(JNIEnv *env, jobject thiz, jint mode) 134{ 135 LOGV("setMode"); 136 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 137 if (retriever == 0) { 138 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 139 return; 140 } 141 process_media_retriever_call(env, retriever->setMode(mode), "java/lang/RuntimeException", "setMode failed"); 142} 143 144static int android_media_MediaMetadataRetriever_getMode(JNIEnv *env, jobject thiz) 145{ 146 LOGV("getMode"); 147 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 148 if (retriever == 0) { 149 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 150 return -1; // Error 151 } 152 int mode = -1; 153 retriever->getMode(&mode); 154 return mode; 155} 156 157static jobject android_media_MediaMetadataRetriever_captureFrame(JNIEnv *env, jobject thiz) 158{ 159 LOGV("captureFrame"); 160 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 161 if (retriever == 0) { 162 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 163 return NULL; 164 } 165 166 // Call native method to retrieve a video frame 167 VideoFrame *videoFrame = NULL; 168 sp<IMemory> frameMemory = retriever->captureFrame(); 169 if (frameMemory != 0) { // cast the shared structure to a VideoFrame object 170 videoFrame = static_cast<VideoFrame *>(frameMemory->pointer()); 171 } 172 if (videoFrame == NULL) { 173 LOGE("captureFrame: videoFrame is a NULL pointer"); 174 return NULL; 175 } 176 177 // Create a SkBitmap to hold the pixels 178 SkBitmap *bitmap = new SkBitmap(); 179 if (bitmap == NULL) { 180 LOGE("captureFrame: cannot instantiate a SkBitmap object."); 181 return NULL; 182 } 183 bitmap->setConfig(SkBitmap::kRGB_565_Config, videoFrame->mDisplayWidth, videoFrame->mDisplayHeight); 184 if (!bitmap->allocPixels()) { 185 delete bitmap; 186 LOGE("failed to allocate pixel buffer"); 187 return NULL; 188 } 189 memcpy((uint8_t*)bitmap->getPixels(), (uint8_t*)videoFrame + sizeof(VideoFrame), videoFrame->mSize); 190 191 // Since internally SkBitmap uses reference count to manage the reference to 192 // its pixels, it is important that the pixels (along with SkBitmap) be 193 // available after creating the Bitmap is returned to Java app. 194 return env->NewObject(fields.bitmapClazz, fields.bitmapConstructor, (int) bitmap, true, NULL, -1); 195} 196 197static jbyteArray android_media_MediaMetadataRetriever_extractAlbumArt(JNIEnv *env, jobject thiz) 198{ 199 LOGV("extractAlbumArt"); 200 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 201 if (retriever == 0) { 202 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 203 return NULL; 204 } 205 MediaAlbumArt* mediaAlbumArt = NULL; 206 sp<IMemory> albumArtMemory = retriever->extractAlbumArt(); 207 if (albumArtMemory != 0) { // cast the shared structure to a MediaAlbumArt object 208 mediaAlbumArt = static_cast<MediaAlbumArt *>(albumArtMemory->pointer()); 209 } 210 if (mediaAlbumArt == NULL) { 211 LOGE("extractAlbumArt: Call to extractAlbumArt failed."); 212 return NULL; 213 } 214 215 unsigned int len = mediaAlbumArt->mSize; 216 char* data = (char*) mediaAlbumArt + sizeof(MediaAlbumArt); 217 jbyteArray array = env->NewByteArray(len); 218 if (!array) { // OutOfMemoryError exception has already been thrown. 219 LOGE("extractAlbumArt: OutOfMemoryError is thrown."); 220 } else { 221 jbyte* bytes = env->GetByteArrayElements(array, NULL); 222 if (bytes != NULL) { 223 memcpy(bytes, data, len); 224 env->ReleaseByteArrayElements(array, bytes, 0); 225 } 226 } 227 228 // No need to delete mediaAlbumArt here 229 return array; 230} 231 232static jobject android_media_MediaMetadataRetriever_extractMetadata(JNIEnv *env, jobject thiz, jint keyCode) 233{ 234 LOGV("extractMetadata"); 235 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 236 if (retriever == 0) { 237 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 238 return NULL; 239 } 240 const char* value = retriever->extractMetadata(keyCode); 241 if (!value) { 242 LOGV("extractMetadata: Metadata is not found"); 243 return NULL; 244 } 245 LOGV("extractMetadata: value (%s) for keyCode(%d)", value, keyCode); 246 return env->NewStringUTF(value); 247} 248 249static void android_media_MediaMetadataRetriever_release(JNIEnv *env, jobject thiz) 250{ 251 LOGV("release"); 252 Mutex::Autolock lock(sLock); 253 MediaMetadataRetriever* retriever = getRetriever(env, thiz); 254 delete retriever; 255 setRetriever(env, thiz, 0); 256} 257 258static void android_media_MediaMetadataRetriever_native_finalize(JNIEnv *env, jobject thiz) 259{ 260 LOGV("native_finalize"); 261 262 // No lock is needed, since android_media_MediaMetadataRetriever_release() is protected 263 android_media_MediaMetadataRetriever_release(env, thiz); 264} 265 266// This function gets a field ID, which in turn causes class initialization. 267// It is called from a static block in MediaMetadataRetriever, which won't run until the 268// first time an instance of this class is used. 269static void android_media_MediaMetadataRetriever_native_init(JNIEnv *env) 270{ 271 jclass clazz = env->FindClass(kClassPathName); 272 if (clazz == NULL) { 273 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/media/MediaMetadataRetriever"); 274 return; 275 } 276 277 fields.context = env->GetFieldID(clazz, "mNativeContext", "I"); 278 if (fields.context == NULL) { 279 jniThrowException(env, "java/lang/RuntimeException", "Can't find MediaMetadataRetriever.mNativeContext"); 280 return; 281 } 282 283 fields.bitmapClazz = env->FindClass("android/graphics/Bitmap"); 284 if (fields.bitmapClazz == NULL) { 285 jniThrowException(env, "java/lang/RuntimeException", "Can't find android/graphics/Bitmap"); 286 return; 287 } 288 289 fields.bitmapConstructor = env->GetMethodID(fields.bitmapClazz, "<init>", "(IZ[BI)V"); 290 if (fields.bitmapConstructor == NULL) { 291 jniThrowException(env, "java/lang/RuntimeException", "Can't find Bitmap constructor"); 292 return; 293 } 294} 295 296static void android_media_MediaMetadataRetriever_native_setup(JNIEnv *env, jobject thiz) 297{ 298 LOGV("native_setup"); 299 MediaMetadataRetriever* retriever = new MediaMetadataRetriever(); 300 if (retriever == 0) { 301 jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); 302 return; 303 } 304 setRetriever(env, thiz, (int)retriever); 305} 306 307// JNI mapping between Java methods and native methods 308static JNINativeMethod nativeMethods[] = { 309 {"setDataSource", "(Ljava/lang/String;)V", (void *)android_media_MediaMetadataRetriever_setDataSource}, 310 {"setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaMetadataRetriever_setDataSourceFD}, 311 {"setMode", "(I)V", (void *)android_media_MediaMetadataRetriever_setMode}, 312 {"getMode", "()I", (void *)android_media_MediaMetadataRetriever_getMode}, 313 {"captureFrame", "()Landroid/graphics/Bitmap;", (void *)android_media_MediaMetadataRetriever_captureFrame}, 314 {"extractMetadata", "(I)Ljava/lang/String;", (void *)android_media_MediaMetadataRetriever_extractMetadata}, 315 {"extractAlbumArt", "()[B", (void *)android_media_MediaMetadataRetriever_extractAlbumArt}, 316 {"release", "()V", (void *)android_media_MediaMetadataRetriever_release}, 317 {"native_finalize", "()V", (void *)android_media_MediaMetadataRetriever_native_finalize}, 318 {"native_setup", "()V", (void *)android_media_MediaMetadataRetriever_native_setup}, 319 {"native_init", "()V", (void *)android_media_MediaMetadataRetriever_native_init}, 320}; 321 322// This function only registers the native methods, and is called from 323// JNI_OnLoad in android_media_MediaPlayer.cpp 324int register_android_media_MediaMetadataRetriever(JNIEnv *env) 325{ 326 return AndroidRuntime::registerNativeMethods 327 (env, kClassPathName, nativeMethods, NELEM(nativeMethods)); 328} 329