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 <cmath> 22 #include <assert.h> 23 #include <utils/Log.h> 24 #include <utils/threads.h> 25 #include <android/graphics/bitmap.h> 26 #include <media/IMediaHTTPService.h> 27 #include <media/mediametadataretriever.h> 28 #include <media/mediascanner.h> 29 #include <nativehelper/ScopedLocalRef.h> 30 #include <private/media/VideoFrame.h> 31 32 #include "jni.h" 33 #include <nativehelper/JNIHelp.h> 34 #include "android_runtime/AndroidRuntime.h" 35 #include "android_media_MediaDataSource.h" 36 #include "android_media_Streams.h" 37 #include "android_util_Binder.h" 38 39 using namespace android; 40 41 struct fields_t { 42 jfieldID context; 43 jclass bitmapClazz; // Must be a global ref 44 jmethodID createBitmapMethod; 45 jmethodID createScaledBitmapMethod; 46 jclass bitmapParamsClazz; // Must be a global ref 47 jfieldID inPreferredConfig; 48 jfieldID outActualConfig; 49 jclass arrayListClazz; // Must be a global ref 50 jmethodID arrayListInit; 51 jmethodID arrayListAdd; 52 }; 53 54 static fields_t fields; 55 static Mutex sLock; 56 static const char* const kClassPathName = "android/media/MediaMetadataRetriever"; 57 58 static void process_media_retriever_call(JNIEnv *env, status_t opStatus, const char* exception, const char *message) 59 { 60 if (opStatus == (status_t) INVALID_OPERATION) { 61 jniThrowException(env, "java/lang/IllegalStateException", NULL); 62 } else if (opStatus != (status_t) OK) { 63 if (strlen(message) > 230) { 64 // If the message is too long, don't bother displaying the status code. 65 jniThrowException( env, exception, message); 66 } else { 67 char msg[256]; 68 // Append the status code to the message. 69 sprintf(msg, "%s: status = 0x%X", message, opStatus); 70 jniThrowException( env, exception, msg); 71 } 72 } 73 } 74 75 static sp<MediaMetadataRetriever> getRetriever(JNIEnv* env, jobject thiz) 76 { 77 // No lock is needed, since it is called internally by other methods that are protected 78 MediaMetadataRetriever* retriever = (MediaMetadataRetriever*) env->GetLongField(thiz, fields.context); 79 return retriever; 80 } 81 82 static void setRetriever(JNIEnv* env, jobject thiz, const sp<MediaMetadataRetriever> &retriever) 83 { 84 // No lock is needed, since it is called internally by other methods that are protected 85 86 if (retriever != NULL) { 87 retriever->incStrong(thiz); 88 } 89 sp<MediaMetadataRetriever> old = getRetriever(env, thiz); 90 if (old != NULL) { 91 old->decStrong(thiz); 92 } 93 94 env->SetLongField(thiz, fields.context, (jlong) retriever.get()); 95 } 96 97 static void 98 android_media_MediaMetadataRetriever_setDataSourceAndHeaders( 99 JNIEnv *env, jobject thiz, jobject httpServiceBinderObj, jstring path, 100 jobjectArray keys, jobjectArray values) { 101 102 ALOGV("setDataSource"); 103 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz); 104 if (retriever == 0) { 105 jniThrowException( 106 env, 107 "java/lang/IllegalStateException", "No retriever available"); 108 109 return; 110 } 111 112 if (!path) { 113 jniThrowException( 114 env, "java/lang/IllegalArgumentException", "Null pointer"); 115 116 return; 117 } 118 119 const char *tmp = env->GetStringUTFChars(path, NULL); 120 if (!tmp) { // OutOfMemoryError exception already thrown 121 return; 122 } 123 124 String8 pathStr(tmp); 125 env->ReleaseStringUTFChars(path, tmp); 126 tmp = NULL; 127 128 // Don't let somebody trick us in to reading some random block of memory 129 if (strncmp("mem://", pathStr.string(), 6) == 0) { 130 jniThrowException( 131 env, "java/lang/IllegalArgumentException", "Invalid pathname"); 132 return; 133 } 134 135 // We build a similar KeyedVector out of it. 136 KeyedVector<String8, String8> headersVector; 137 if (!ConvertKeyValueArraysToKeyedVector( 138 env, keys, values, &headersVector)) { 139 return; 140 } 141 142 sp<IMediaHTTPService> httpService; 143 if (httpServiceBinderObj != NULL) { 144 sp<IBinder> binder = ibinderForJavaObject(env, httpServiceBinderObj); 145 httpService = interface_cast<IMediaHTTPService>(binder); 146 } 147 148 process_media_retriever_call( 149 env, 150 retriever->setDataSource( 151 httpService, 152 pathStr.string(), 153 headersVector.size() > 0 ? &headersVector : NULL), 154 155 "java/lang/RuntimeException", 156 "setDataSource failed"); 157 } 158 159 static void android_media_MediaMetadataRetriever_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length) 160 { 161 ALOGV("setDataSource"); 162 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz); 163 if (retriever == 0) { 164 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 165 return; 166 } 167 if (!fileDescriptor) { 168 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 169 return; 170 } 171 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); 172 if (offset < 0 || length < 0 || fd < 0) { 173 if (offset < 0) { 174 ALOGE("negative offset (%lld)", (long long)offset); 175 } 176 if (length < 0) { 177 ALOGE("negative length (%lld)", (long long)length); 178 } 179 if (fd < 0) { 180 ALOGE("invalid file descriptor"); 181 } 182 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 183 return; 184 } 185 process_media_retriever_call(env, retriever->setDataSource(fd, offset, length), "java/lang/RuntimeException", "setDataSource failed"); 186 } 187 188 static void android_media_MediaMetadataRetriever_setDataSourceCallback(JNIEnv *env, jobject thiz, jobject dataSource) 189 { 190 ALOGV("setDataSourceCallback"); 191 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz); 192 if (retriever == 0) { 193 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 194 return; 195 } 196 if (dataSource == NULL) { 197 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 198 return; 199 } 200 201 sp<IDataSource> callbackDataSource = new JMediaDataSource(env, dataSource); 202 process_media_retriever_call(env, retriever->setDataSource(callbackDataSource), "java/lang/RuntimeException", "setDataSourceCallback failed"); 203 } 204 205 template<typename T> 206 static void rotate0(T* dst, const T* src, size_t width, size_t height) 207 { 208 memcpy(dst, src, width * height * sizeof(T)); 209 } 210 211 template<typename T> 212 static void rotate90(T* dst, const T* src, size_t width, size_t height) 213 { 214 for (size_t i = 0; i < height; ++i) { 215 for (size_t j = 0; j < width; ++j) { 216 dst[j * height + height - 1 - i] = src[i * width + j]; 217 } 218 } 219 } 220 221 template<typename T> 222 static void rotate180(T* dst, const T* src, size_t width, size_t height) 223 { 224 for (size_t i = 0; i < height; ++i) { 225 for (size_t j = 0; j < width; ++j) { 226 dst[(height - 1 - i) * width + width - 1 - j] = src[i * width + j]; 227 } 228 } 229 } 230 231 template<typename T> 232 static void rotate270(T* dst, const T* src, size_t width, size_t height) 233 { 234 for (size_t i = 0; i < height; ++i) { 235 for (size_t j = 0; j < width; ++j) { 236 dst[(width - 1 - j) * height + i] = src[i * width + j]; 237 } 238 } 239 } 240 241 template<typename T> 242 static void rotate(T *dst, const T *src, size_t width, size_t height, int angle) 243 { 244 switch (angle) { 245 case 0: 246 rotate0(dst, src, width, height); 247 break; 248 case 90: 249 rotate90(dst, src, width, height); 250 break; 251 case 180: 252 rotate180(dst, src, width, height); 253 break; 254 case 270: 255 rotate270(dst, src, width, height); 256 break; 257 } 258 } 259 260 static jobject getBitmapFromVideoFrame( 261 JNIEnv *env, VideoFrame *videoFrame, jint dst_width, jint dst_height, 262 AndroidBitmapFormat outColorType) { 263 ALOGV("getBitmapFromVideoFrame: dimension = %dx%d, displaySize = %dx%d, bytes = %d", 264 videoFrame->mWidth, 265 videoFrame->mHeight, 266 videoFrame->mDisplayWidth, 267 videoFrame->mDisplayHeight, 268 videoFrame->mSize); 269 270 ScopedLocalRef<jobject> config(env, ABitmapConfig_getConfigFromFormat(env, outColorType)); 271 272 uint32_t width, height, displayWidth, displayHeight; 273 bool swapWidthAndHeight = false; 274 if (videoFrame->mRotationAngle == 90 || videoFrame->mRotationAngle == 270) { 275 width = videoFrame->mHeight; 276 height = videoFrame->mWidth; 277 swapWidthAndHeight = true; 278 displayWidth = videoFrame->mDisplayHeight; 279 displayHeight = videoFrame->mDisplayWidth; 280 } else { 281 width = videoFrame->mWidth; 282 height = videoFrame->mHeight; 283 displayWidth = videoFrame->mDisplayWidth; 284 displayHeight = videoFrame->mDisplayHeight; 285 } 286 287 jobject jBitmap = env->CallStaticObjectMethod( 288 fields.bitmapClazz, 289 fields.createBitmapMethod, 290 width, 291 height, 292 config.get()); 293 if (jBitmap == NULL) { 294 if (env->ExceptionCheck()) { 295 env->ExceptionClear(); 296 } 297 ALOGE("getBitmapFromVideoFrame: create Bitmap failed!"); 298 return NULL; 299 } 300 301 graphics::Bitmap bitmap(env, jBitmap); 302 303 if (outColorType == ANDROID_BITMAP_FORMAT_RGB_565) { 304 rotate((uint16_t*)bitmap.getPixels(), 305 (uint16_t*)((char*)videoFrame + sizeof(VideoFrame)), 306 videoFrame->mWidth, 307 videoFrame->mHeight, 308 videoFrame->mRotationAngle); 309 } else { 310 rotate((uint32_t*)bitmap.getPixels(), 311 (uint32_t*)((char*)videoFrame + sizeof(VideoFrame)), 312 videoFrame->mWidth, 313 videoFrame->mHeight, 314 videoFrame->mRotationAngle); 315 } 316 317 if (dst_width <= 0 || dst_height <= 0) { 318 dst_width = displayWidth; 319 dst_height = displayHeight; 320 } else { 321 float factor = std::min((float)dst_width / (float)displayWidth, 322 (float)dst_height / (float)displayHeight); 323 dst_width = std::round(displayWidth * factor); 324 dst_height = std::round(displayHeight * factor); 325 } 326 327 if ((uint32_t)dst_width != width || (uint32_t)dst_height != height) { 328 ALOGV("Bitmap dimension is scaled from %dx%d to %dx%d", 329 width, height, dst_width, dst_height); 330 jobject scaledBitmap = env->CallStaticObjectMethod(fields.bitmapClazz, 331 fields.createScaledBitmapMethod, 332 jBitmap, 333 dst_width, 334 dst_height, 335 true); 336 337 env->DeleteLocalRef(jBitmap); 338 return scaledBitmap; 339 } 340 341 return jBitmap; 342 } 343 344 static AndroidBitmapFormat getColorFormat(JNIEnv *env, jobject options, 345 AndroidBitmapFormat defaultPreferred = ANDROID_BITMAP_FORMAT_RGBA_8888) { 346 if (options == NULL) { 347 return defaultPreferred; 348 } 349 350 ScopedLocalRef<jobject> inConfig(env, env->GetObjectField(options, fields.inPreferredConfig)); 351 AndroidBitmapFormat format = ABitmapConfig_getFormatFromConfig(env, inConfig.get()); 352 353 if (format == ANDROID_BITMAP_FORMAT_RGB_565) { 354 return ANDROID_BITMAP_FORMAT_RGB_565; 355 } 356 return ANDROID_BITMAP_FORMAT_RGBA_8888; 357 } 358 359 static void setOutConfig(JNIEnv *env, jobject options, AndroidBitmapFormat colorFormat) { 360 if (options != NULL) { 361 ScopedLocalRef<jobject> config(env, ABitmapConfig_getConfigFromFormat(env, colorFormat)); 362 env->SetObjectField(options, fields.outActualConfig, config.get()); 363 } 364 } 365 366 static jobject android_media_MediaMetadataRetriever_getFrameAtTime( 367 JNIEnv *env, jobject thiz, jlong timeUs, jint option, 368 jint dst_width, jint dst_height, jobject params) 369 { 370 ALOGV("getFrameAtTime: %lld us option: %d dst width: %d heigh: %d", 371 (long long)timeUs, option, dst_width, dst_height); 372 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz); 373 if (retriever == 0) { 374 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 375 return NULL; 376 } 377 // For getFrameAtTime family of calls, default to ANDROID_BITMAP_FORMAT_RGB_565 378 // to keep the behavior consistent with older releases 379 AndroidBitmapFormat colorFormat = getColorFormat(env, params, ANDROID_BITMAP_FORMAT_RGB_565); 380 381 // Call native method to retrieve a video frame 382 VideoFrame *videoFrame = NULL; 383 sp<IMemory> frameMemory = retriever->getFrameAtTime(timeUs, option, colorFormat); 384 // TODO: Using unsecurePointer() has some associated security pitfalls 385 // (see declaration for details). 386 // Either document why it is safe in this case or address the 387 // issue (e.g. by copying). 388 if (frameMemory != 0) { // cast the shared structure to a VideoFrame object 389 videoFrame = static_cast<VideoFrame *>(frameMemory->unsecurePointer()); 390 } 391 if (videoFrame == NULL) { 392 ALOGE("getFrameAtTime: videoFrame is a NULL pointer"); 393 return NULL; 394 } 395 396 setOutConfig(env, params, colorFormat); 397 return getBitmapFromVideoFrame(env, videoFrame, dst_width, dst_height, colorFormat); 398 } 399 400 static jobject android_media_MediaMetadataRetriever_getImageAtIndex( 401 JNIEnv *env, jobject thiz, jint index, jobject params) 402 { 403 ALOGV("getImageAtIndex: index %d", index); 404 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz); 405 if (retriever == 0) { 406 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 407 return NULL; 408 } 409 410 AndroidBitmapFormat colorFormat = getColorFormat(env, params); 411 412 // Call native method to retrieve an image 413 VideoFrame *videoFrame = NULL; 414 sp<IMemory> frameMemory = retriever->getImageAtIndex(index, colorFormat); 415 if (frameMemory != 0) { // cast the shared structure to a VideoFrame object 416 // TODO: Using unsecurePointer() has some associated security pitfalls 417 // (see declaration for details). 418 // Either document why it is safe in this case or address the 419 // issue (e.g. by copying). 420 videoFrame = static_cast<VideoFrame *>(frameMemory->unsecurePointer()); 421 } 422 if (videoFrame == NULL) { 423 ALOGE("getImageAtIndex: videoFrame is a NULL pointer"); 424 return NULL; 425 } 426 427 setOutConfig(env, params, colorFormat); 428 return getBitmapFromVideoFrame(env, videoFrame, -1, -1, colorFormat); 429 } 430 431 static jobject android_media_MediaMetadataRetriever_getThumbnailImageAtIndex( 432 JNIEnv *env, jobject thiz, jint index, jobject params, jint targetSize, jint maxPixels) 433 { 434 ALOGV("getThumbnailImageAtIndex: index %d", index); 435 436 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz); 437 if (retriever == 0) { 438 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 439 return NULL; 440 } 441 442 AndroidBitmapFormat colorFormat = getColorFormat(env, params); 443 jint dst_width = -1, dst_height = -1; 444 445 // Call native method to retrieve an image 446 VideoFrame *videoFrame = NULL; 447 sp<IMemory> frameMemory = retriever->getImageAtIndex( 448 index, colorFormat, true /*metaOnly*/, true /*thumbnail*/); 449 if (frameMemory != 0) { 450 // TODO: Using unsecurePointer() has some associated security pitfalls 451 // (see declaration for details). 452 // Either document why it is safe in this case or address the 453 // issue (e.g. by copying). 454 videoFrame = static_cast<VideoFrame *>(frameMemory->unsecurePointer()); 455 int32_t thumbWidth = videoFrame->mWidth; 456 int32_t thumbHeight = videoFrame->mHeight; 457 videoFrame = NULL; 458 int64_t thumbPixels = thumbWidth * thumbHeight; 459 460 // Here we try to use the included thumbnail if it's not too shabby. 461 // If this fails ThumbnailUtils would have to decode the full image and 462 // downscale which could take long. 463 if (thumbWidth >= targetSize || thumbHeight >= targetSize 464 || thumbPixels * 6 >= maxPixels) { 465 frameMemory = retriever->getImageAtIndex( 466 index, colorFormat, false /*metaOnly*/, true /*thumbnail*/); 467 // TODO: Using unsecurePointer() has some associated security pitfalls 468 // (see declaration for details). 469 // Either document why it is safe in this case or address the 470 // issue (e.g. by copying). 471 videoFrame = static_cast<VideoFrame *>(frameMemory->unsecurePointer()); 472 473 if (thumbPixels > maxPixels) { 474 int downscale = ceil(sqrt(thumbPixels / (float)maxPixels)); 475 dst_width = thumbWidth / downscale; 476 dst_height = thumbHeight /downscale; 477 } 478 } 479 } 480 if (videoFrame == NULL) { 481 ALOGV("getThumbnailImageAtIndex: no suitable thumbnails available"); 482 return NULL; 483 } 484 485 // Ignore rotation for thumbnail extraction to be consistent with 486 // thumbnails extracted by BitmapFactory APIs. 487 videoFrame->mRotationAngle = 0; 488 489 setOutConfig(env, params, colorFormat); 490 return getBitmapFromVideoFrame(env, videoFrame, dst_width, dst_height, colorFormat); 491 } 492 493 static jobject android_media_MediaMetadataRetriever_getFrameAtIndex( 494 JNIEnv *env, jobject thiz, jint frameIndex, jint numFrames, jobject params) 495 { 496 ALOGV("getFrameAtIndex: frameIndex %d, numFrames %d", frameIndex, numFrames); 497 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz); 498 if (retriever == 0) { 499 jniThrowException(env, 500 "java/lang/IllegalStateException", "No retriever available"); 501 return NULL; 502 } 503 504 505 jobject arrayList = env->NewObject(fields.arrayListClazz, fields.arrayListInit); 506 if (arrayList == NULL) { 507 jniThrowException(env, 508 "java/lang/IllegalStateException", "Can't create bitmap array"); 509 return NULL; 510 } 511 512 AndroidBitmapFormat colorFormat = getColorFormat(env, params); 513 setOutConfig(env, params, colorFormat); 514 size_t i = 0; 515 for (; i < numFrames; i++) { 516 sp<IMemory> frame = retriever->getFrameAtIndex(frameIndex + i, colorFormat); 517 if (frame == NULL || frame->unsecurePointer() == NULL) { 518 ALOGE("video frame at index %zu is a NULL pointer", frameIndex + i); 519 break; 520 } 521 // TODO: Using unsecurePointer() has some associated security pitfalls 522 // (see declaration for details). 523 // Either document why it is safe in this case or address the 524 // issue (e.g. by copying). 525 VideoFrame *videoFrame = static_cast<VideoFrame *>(frame->unsecurePointer()); 526 jobject bitmapObj = getBitmapFromVideoFrame(env, videoFrame, -1, -1, colorFormat); 527 env->CallBooleanMethod(arrayList, fields.arrayListAdd, bitmapObj); 528 env->DeleteLocalRef(bitmapObj); 529 } 530 531 if (i == 0) { 532 env->DeleteLocalRef(arrayList); 533 534 jniThrowException(env, 535 "java/lang/IllegalStateException", "No frames from retriever"); 536 return NULL; 537 } 538 539 return arrayList; 540 } 541 542 static jbyteArray android_media_MediaMetadataRetriever_getEmbeddedPicture( 543 JNIEnv *env, jobject thiz, jint pictureType) 544 { 545 ALOGV("getEmbeddedPicture: %d", pictureType); 546 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz); 547 if (retriever == 0) { 548 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 549 return NULL; 550 } 551 MediaAlbumArt* mediaAlbumArt = NULL; 552 553 // FIXME: 554 // Use pictureType to retrieve the intended embedded picture and also change 555 // the method name to getEmbeddedPicture(). 556 sp<IMemory> albumArtMemory = retriever->extractAlbumArt(); 557 if (albumArtMemory != 0) { // cast the shared structure to a MediaAlbumArt object 558 // TODO: Using unsecurePointer() has some associated security pitfalls 559 // (see declaration for details). 560 // Either document why it is safe in this case or address the 561 // issue (e.g. by copying). 562 mediaAlbumArt = static_cast<MediaAlbumArt *>(albumArtMemory->unsecurePointer()); 563 } 564 if (mediaAlbumArt == NULL) { 565 ALOGE("getEmbeddedPicture: Call to getEmbeddedPicture failed."); 566 return NULL; 567 } 568 569 jbyteArray array = env->NewByteArray(mediaAlbumArt->size()); 570 if (!array) { // OutOfMemoryError exception has already been thrown. 571 ALOGE("getEmbeddedPicture: OutOfMemoryError is thrown."); 572 } else { 573 const jbyte* data = 574 reinterpret_cast<const jbyte*>(mediaAlbumArt->data()); 575 env->SetByteArrayRegion(array, 0, mediaAlbumArt->size(), data); 576 } 577 578 // No need to delete mediaAlbumArt here 579 return array; 580 } 581 582 static jobject android_media_MediaMetadataRetriever_extractMetadata(JNIEnv *env, jobject thiz, jint keyCode) 583 { 584 ALOGV("extractMetadata"); 585 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz); 586 if (retriever == 0) { 587 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available"); 588 return NULL; 589 } 590 const char* value = retriever->extractMetadata(keyCode); 591 if (!value) { 592 ALOGV("extractMetadata: Metadata is not found"); 593 return NULL; 594 } 595 ALOGV("extractMetadata: value (%s) for keyCode(%d)", value, keyCode); 596 return env->NewStringUTF(value); 597 } 598 599 static void android_media_MediaMetadataRetriever_release(JNIEnv *env, jobject thiz) 600 { 601 ALOGV("release"); 602 Mutex::Autolock lock(sLock); 603 setRetriever(env, thiz, NULL); 604 } 605 606 static void android_media_MediaMetadataRetriever_native_finalize(JNIEnv *env, jobject thiz) 607 { 608 ALOGV("native_finalize"); 609 // No lock is needed, since android_media_MediaMetadataRetriever_release() is protected 610 android_media_MediaMetadataRetriever_release(env, thiz); 611 } 612 613 // This function gets a field ID, which in turn causes class initialization. 614 // It is called from a static block in MediaMetadataRetriever, which won't run until the 615 // first time an instance of this class is used. 616 static void android_media_MediaMetadataRetriever_native_init(JNIEnv *env) 617 { 618 ScopedLocalRef<jclass> clazz(env, env->FindClass(kClassPathName)); 619 if (clazz.get() == NULL) { 620 return; 621 } 622 623 fields.context = env->GetFieldID(clazz.get(), "mNativeContext", "J"); 624 if (fields.context == NULL) { 625 return; 626 } 627 628 clazz.reset(env->FindClass("android/graphics/Bitmap")); 629 if (clazz.get() == NULL) { 630 return; 631 } 632 fields.bitmapClazz = (jclass) env->NewGlobalRef(clazz.get()); 633 if (fields.bitmapClazz == NULL) { 634 return; 635 } 636 fields.createBitmapMethod = 637 env->GetStaticMethodID(fields.bitmapClazz, "createBitmap", 638 "(IILandroid/graphics/Bitmap$Config;)" 639 "Landroid/graphics/Bitmap;"); 640 if (fields.createBitmapMethod == NULL) { 641 return; 642 } 643 fields.createScaledBitmapMethod = 644 env->GetStaticMethodID(fields.bitmapClazz, "createScaledBitmap", 645 "(Landroid/graphics/Bitmap;IIZ)" 646 "Landroid/graphics/Bitmap;"); 647 if (fields.createScaledBitmapMethod == NULL) { 648 return; 649 } 650 651 clazz.reset(env->FindClass("android/media/MediaMetadataRetriever$BitmapParams")); 652 if (clazz.get() == NULL) { 653 return; 654 } 655 fields.bitmapParamsClazz = (jclass) env->NewGlobalRef(clazz.get()); 656 if (fields.bitmapParamsClazz == NULL) { 657 return; 658 } 659 fields.inPreferredConfig = env->GetFieldID(fields.bitmapParamsClazz, 660 "inPreferredConfig", "Landroid/graphics/Bitmap$Config;"); 661 if (fields.inPreferredConfig == NULL) { 662 return; 663 } 664 fields.outActualConfig = env->GetFieldID(fields.bitmapParamsClazz, 665 "outActualConfig", "Landroid/graphics/Bitmap$Config;"); 666 if (fields.outActualConfig == NULL) { 667 return; 668 } 669 670 clazz.reset(env->FindClass("java/util/ArrayList")); 671 if (clazz.get() == NULL) { 672 return; 673 } 674 fields.arrayListClazz = (jclass) env->NewGlobalRef(clazz.get()); 675 if (fields.arrayListClazz == NULL) { 676 return; 677 } 678 fields.arrayListInit = env->GetMethodID(clazz.get(), "<init>", "()V"); 679 if (fields.arrayListInit == NULL) { 680 return; 681 } 682 fields.arrayListAdd = env->GetMethodID(clazz.get(), "add", "(Ljava/lang/Object;)Z"); 683 if (fields.arrayListAdd == NULL) { 684 return; 685 } 686 } 687 688 static void android_media_MediaMetadataRetriever_native_setup(JNIEnv *env, jobject thiz) 689 { 690 ALOGV("native_setup"); 691 sp<MediaMetadataRetriever> retriever = new MediaMetadataRetriever(); 692 if (retriever == 0) { 693 jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); 694 return; 695 } 696 setRetriever(env, thiz, retriever); 697 } 698 699 // JNI mapping between Java methods and native methods 700 static const JNINativeMethod nativeMethods[] = { 701 { 702 "_setDataSource", 703 "(Landroid/os/IBinder;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V", 704 (void *)android_media_MediaMetadataRetriever_setDataSourceAndHeaders 705 }, 706 707 {"setDataSource", "(Ljava/io/FileDescriptor;JJ)V", 708 (void *)android_media_MediaMetadataRetriever_setDataSourceFD}, 709 {"_setDataSource", "(Landroid/media/MediaDataSource;)V", 710 (void *)android_media_MediaMetadataRetriever_setDataSourceCallback}, 711 {"_getFrameAtTime", "(JIIILandroid/media/MediaMetadataRetriever$BitmapParams;)Landroid/graphics/Bitmap;", 712 (void *)android_media_MediaMetadataRetriever_getFrameAtTime}, 713 { 714 "_getImageAtIndex", 715 "(ILandroid/media/MediaMetadataRetriever$BitmapParams;)Landroid/graphics/Bitmap;", 716 (void *)android_media_MediaMetadataRetriever_getImageAtIndex 717 }, 718 719 { 720 "getThumbnailImageAtIndex", 721 "(ILandroid/media/MediaMetadataRetriever$BitmapParams;II)Landroid/graphics/Bitmap;", 722 (void *)android_media_MediaMetadataRetriever_getThumbnailImageAtIndex 723 }, 724 725 { 726 "_getFrameAtIndex", 727 "(IILandroid/media/MediaMetadataRetriever$BitmapParams;)Ljava/util/List;", 728 (void *)android_media_MediaMetadataRetriever_getFrameAtIndex 729 }, 730 731 {"nativeExtractMetadata", "(I)Ljava/lang/String;", 732 (void *)android_media_MediaMetadataRetriever_extractMetadata}, 733 {"getEmbeddedPicture", "(I)[B", 734 (void *)android_media_MediaMetadataRetriever_getEmbeddedPicture}, 735 {"release", "()V", 736 (void *)android_media_MediaMetadataRetriever_release}, 737 {"native_finalize", "()V", 738 (void *)android_media_MediaMetadataRetriever_native_finalize}, 739 {"native_setup", "()V", 740 (void *)android_media_MediaMetadataRetriever_native_setup}, 741 {"native_init", "()V", 742 (void *)android_media_MediaMetadataRetriever_native_init}, 743 }; 744 745 // This function only registers the native methods, and is called from 746 // JNI_OnLoad in android_media_MediaPlayer.cpp 747 int register_android_media_MediaMetadataRetriever(JNIEnv *env) 748 { 749 return AndroidRuntime::registerNativeMethods 750 (env, kClassPathName, nativeMethods, NELEM(nativeMethods)); 751 } 752