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/JNIPlatformHelp.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
process_media_retriever_call(JNIEnv * env,status_t opStatus,const char * exception,const char * message)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
getRetriever(JNIEnv * env,jobject thiz)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
setRetriever(JNIEnv * env,jobject thiz,const sp<MediaMetadataRetriever> & retriever)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
android_media_MediaMetadataRetriever_setDataSourceAndHeaders(JNIEnv * env,jobject thiz,jobject httpServiceBinderObj,jstring path,jobjectArray keys,jobjectArray values)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
android_media_MediaMetadataRetriever_setDataSourceFD(JNIEnv * env,jobject thiz,jobject fileDescriptor,jlong offset,jlong length)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
android_media_MediaMetadataRetriever_setDataSourceCallback(JNIEnv * env,jobject thiz,jobject dataSource)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>
rotate0(T * dst,const T * src,size_t width,size_t height)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>
rotate90(T * dst,const T * src,size_t width,size_t height)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>
rotate180(T * dst,const T * src,size_t width,size_t height)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>
rotate270(T * dst,const T * src,size_t width,size_t height)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>
rotate(T * dst,const T * src,size_t width,size_t height,int angle)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
getBitmapFromVideoFrame(JNIEnv * env,VideoFrame * videoFrame,jint dst_width,jint dst_height,AndroidBitmapFormat outColorType)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
getColorFormat(JNIEnv * env,jobject options,AndroidBitmapFormat defaultPreferred=ANDROID_BITMAP_FORMAT_RGBA_8888)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
setOutConfig(JNIEnv * env,jobject options,AndroidBitmapFormat colorFormat)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
android_media_MediaMetadataRetriever_getFrameAtTime(JNIEnv * env,jobject thiz,jlong timeUs,jint option,jint dst_width,jint dst_height,jobject params)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
android_media_MediaMetadataRetriever_getImageAtIndex(JNIEnv * env,jobject thiz,jint index,jobject params)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
android_media_MediaMetadataRetriever_getThumbnailImageAtIndex(JNIEnv * env,jobject thiz,jint index,jobject params,jint targetSize,jint maxPixels)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 if (frameMemory != 0) {
468 // TODO: Using unsecurePointer() has some associated security pitfalls
469 // (see declaration for details).
470 // Either document why it is safe in this case or address the
471 // issue (e.g. by copying).
472 videoFrame = static_cast<VideoFrame *>(frameMemory->unsecurePointer());
473 }
474
475 if (thumbPixels > maxPixels) {
476 int downscale = ceil(sqrt(thumbPixels / (float)maxPixels));
477 dst_width = thumbWidth / downscale;
478 dst_height = thumbHeight /downscale;
479 }
480 }
481 }
482 if (videoFrame == NULL) {
483 ALOGV("getThumbnailImageAtIndex: no suitable thumbnails available");
484 return NULL;
485 }
486
487 // Ignore rotation for thumbnail extraction to be consistent with
488 // thumbnails extracted by BitmapFactory APIs.
489 videoFrame->mRotationAngle = 0;
490
491 setOutConfig(env, params, colorFormat);
492 return getBitmapFromVideoFrame(env, videoFrame, dst_width, dst_height, colorFormat);
493 }
494
android_media_MediaMetadataRetriever_getFrameAtIndex(JNIEnv * env,jobject thiz,jint frameIndex,jint numFrames,jobject params)495 static jobject android_media_MediaMetadataRetriever_getFrameAtIndex(
496 JNIEnv *env, jobject thiz, jint frameIndex, jint numFrames, jobject params)
497 {
498 ALOGV("getFrameAtIndex: frameIndex %d, numFrames %d", frameIndex, numFrames);
499 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz);
500 if (retriever == 0) {
501 jniThrowException(env,
502 "java/lang/IllegalStateException", "No retriever available");
503 return NULL;
504 }
505
506
507 jobject arrayList = env->NewObject(fields.arrayListClazz, fields.arrayListInit);
508 if (arrayList == NULL) {
509 jniThrowException(env,
510 "java/lang/IllegalStateException", "Can't create bitmap array");
511 return NULL;
512 }
513
514 AndroidBitmapFormat colorFormat = getColorFormat(env, params);
515 setOutConfig(env, params, colorFormat);
516 size_t i = 0;
517 for (; i < numFrames; i++) {
518 sp<IMemory> frame = retriever->getFrameAtIndex(frameIndex + i, colorFormat);
519 if (frame == NULL || frame->unsecurePointer() == NULL) {
520 ALOGE("video frame at index %zu is a NULL pointer", frameIndex + i);
521 break;
522 }
523 // TODO: Using unsecurePointer() has some associated security pitfalls
524 // (see declaration for details).
525 // Either document why it is safe in this case or address the
526 // issue (e.g. by copying).
527 VideoFrame *videoFrame = static_cast<VideoFrame *>(frame->unsecurePointer());
528 jobject bitmapObj = getBitmapFromVideoFrame(env, videoFrame, -1, -1, colorFormat);
529 env->CallBooleanMethod(arrayList, fields.arrayListAdd, bitmapObj);
530 env->DeleteLocalRef(bitmapObj);
531 }
532
533 if (i == 0) {
534 env->DeleteLocalRef(arrayList);
535
536 jniThrowException(env,
537 "java/lang/IllegalStateException", "No frames from retriever");
538 return NULL;
539 }
540
541 return arrayList;
542 }
543
android_media_MediaMetadataRetriever_getEmbeddedPicture(JNIEnv * env,jobject thiz,jint pictureType)544 static jbyteArray android_media_MediaMetadataRetriever_getEmbeddedPicture(
545 JNIEnv *env, jobject thiz, jint pictureType)
546 {
547 ALOGV("getEmbeddedPicture: %d", pictureType);
548 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz);
549 if (retriever == 0) {
550 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available");
551 return NULL;
552 }
553 MediaAlbumArt* mediaAlbumArt = NULL;
554
555 // FIXME:
556 // Use pictureType to retrieve the intended embedded picture and also change
557 // the method name to getEmbeddedPicture().
558 sp<IMemory> albumArtMemory = retriever->extractAlbumArt();
559 if (albumArtMemory != 0) { // cast the shared structure to a MediaAlbumArt object
560 // TODO: Using unsecurePointer() has some associated security pitfalls
561 // (see declaration for details).
562 // Either document why it is safe in this case or address the
563 // issue (e.g. by copying).
564 mediaAlbumArt = static_cast<MediaAlbumArt *>(albumArtMemory->unsecurePointer());
565 }
566 if (mediaAlbumArt == NULL) {
567 ALOGE("getEmbeddedPicture: Call to getEmbeddedPicture failed.");
568 return NULL;
569 }
570
571 jbyteArray array = env->NewByteArray(mediaAlbumArt->size());
572 if (!array) { // OutOfMemoryError exception has already been thrown.
573 ALOGE("getEmbeddedPicture: OutOfMemoryError is thrown.");
574 } else {
575 const jbyte* data =
576 reinterpret_cast<const jbyte*>(mediaAlbumArt->data());
577 env->SetByteArrayRegion(array, 0, mediaAlbumArt->size(), data);
578 }
579
580 // No need to delete mediaAlbumArt here
581 return array;
582 }
583
android_media_MediaMetadataRetriever_extractMetadata(JNIEnv * env,jobject thiz,jint keyCode)584 static jobject android_media_MediaMetadataRetriever_extractMetadata(JNIEnv *env, jobject thiz, jint keyCode)
585 {
586 ALOGV("extractMetadata");
587 sp<MediaMetadataRetriever> retriever = getRetriever(env, thiz);
588 if (retriever == 0) {
589 jniThrowException(env, "java/lang/IllegalStateException", "No retriever available");
590 return NULL;
591 }
592 const char* value = retriever->extractMetadata(keyCode);
593 if (!value) {
594 ALOGV("extractMetadata: Metadata is not found");
595 return NULL;
596 }
597 ALOGV("extractMetadata: value (%s) for keyCode(%d)", value, keyCode);
598 return env->NewStringUTF(value);
599 }
600
android_media_MediaMetadataRetriever_release(JNIEnv * env,jobject thiz)601 static void android_media_MediaMetadataRetriever_release(JNIEnv *env, jobject thiz)
602 {
603 ALOGV("release");
604 Mutex::Autolock lock(sLock);
605 setRetriever(env, thiz, NULL);
606 }
607
android_media_MediaMetadataRetriever_native_finalize(JNIEnv * env,jobject thiz)608 static void android_media_MediaMetadataRetriever_native_finalize(JNIEnv *env, jobject thiz)
609 {
610 ALOGV("native_finalize");
611 // No lock is needed, since android_media_MediaMetadataRetriever_release() is protected
612 android_media_MediaMetadataRetriever_release(env, thiz);
613 }
614
615 // This function gets a field ID, which in turn causes class initialization.
616 // It is called from a static block in MediaMetadataRetriever, which won't run until the
617 // first time an instance of this class is used.
android_media_MediaMetadataRetriever_native_init(JNIEnv * env)618 static void android_media_MediaMetadataRetriever_native_init(JNIEnv *env)
619 {
620 ScopedLocalRef<jclass> clazz(env, env->FindClass(kClassPathName));
621 if (clazz.get() == NULL) {
622 return;
623 }
624
625 fields.context = env->GetFieldID(clazz.get(), "mNativeContext", "J");
626 if (fields.context == NULL) {
627 return;
628 }
629
630 clazz.reset(env->FindClass("android/graphics/Bitmap"));
631 if (clazz.get() == NULL) {
632 return;
633 }
634 fields.bitmapClazz = (jclass) env->NewGlobalRef(clazz.get());
635 if (fields.bitmapClazz == NULL) {
636 return;
637 }
638 fields.createBitmapMethod =
639 env->GetStaticMethodID(fields.bitmapClazz, "createBitmap",
640 "(IILandroid/graphics/Bitmap$Config;)"
641 "Landroid/graphics/Bitmap;");
642 if (fields.createBitmapMethod == NULL) {
643 return;
644 }
645 fields.createScaledBitmapMethod =
646 env->GetStaticMethodID(fields.bitmapClazz, "createScaledBitmap",
647 "(Landroid/graphics/Bitmap;IIZ)"
648 "Landroid/graphics/Bitmap;");
649 if (fields.createScaledBitmapMethod == NULL) {
650 return;
651 }
652
653 clazz.reset(env->FindClass("android/media/MediaMetadataRetriever$BitmapParams"));
654 if (clazz.get() == NULL) {
655 return;
656 }
657 fields.bitmapParamsClazz = (jclass) env->NewGlobalRef(clazz.get());
658 if (fields.bitmapParamsClazz == NULL) {
659 return;
660 }
661 fields.inPreferredConfig = env->GetFieldID(fields.bitmapParamsClazz,
662 "inPreferredConfig", "Landroid/graphics/Bitmap$Config;");
663 if (fields.inPreferredConfig == NULL) {
664 return;
665 }
666 fields.outActualConfig = env->GetFieldID(fields.bitmapParamsClazz,
667 "outActualConfig", "Landroid/graphics/Bitmap$Config;");
668 if (fields.outActualConfig == NULL) {
669 return;
670 }
671
672 clazz.reset(env->FindClass("java/util/ArrayList"));
673 if (clazz.get() == NULL) {
674 return;
675 }
676 fields.arrayListClazz = (jclass) env->NewGlobalRef(clazz.get());
677 if (fields.arrayListClazz == NULL) {
678 return;
679 }
680 fields.arrayListInit = env->GetMethodID(clazz.get(), "<init>", "()V");
681 if (fields.arrayListInit == NULL) {
682 return;
683 }
684 fields.arrayListAdd = env->GetMethodID(clazz.get(), "add", "(Ljava/lang/Object;)Z");
685 if (fields.arrayListAdd == NULL) {
686 return;
687 }
688 }
689
android_media_MediaMetadataRetriever_native_setup(JNIEnv * env,jobject thiz)690 static void android_media_MediaMetadataRetriever_native_setup(JNIEnv *env, jobject thiz)
691 {
692 ALOGV("native_setup");
693 sp<MediaMetadataRetriever> retriever = new MediaMetadataRetriever();
694 if (retriever == 0) {
695 jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
696 return;
697 }
698 setRetriever(env, thiz, retriever);
699 }
700
701 // JNI mapping between Java methods and native methods
702 static const JNINativeMethod nativeMethods[] = {
703 {
704 "_setDataSource",
705 "(Landroid/os/IBinder;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V",
706 (void *)android_media_MediaMetadataRetriever_setDataSourceAndHeaders
707 },
708
709 {"_setDataSource", "(Ljava/io/FileDescriptor;JJ)V",
710 (void *)android_media_MediaMetadataRetriever_setDataSourceFD},
711 {"_setDataSource", "(Landroid/media/MediaDataSource;)V",
712 (void *)android_media_MediaMetadataRetriever_setDataSourceCallback},
713 {"_getFrameAtTime", "(JIIILandroid/media/MediaMetadataRetriever$BitmapParams;)Landroid/graphics/Bitmap;",
714 (void *)android_media_MediaMetadataRetriever_getFrameAtTime},
715 {
716 "_getImageAtIndex",
717 "(ILandroid/media/MediaMetadataRetriever$BitmapParams;)Landroid/graphics/Bitmap;",
718 (void *)android_media_MediaMetadataRetriever_getImageAtIndex
719 },
720
721 {
722 "getThumbnailImageAtIndex",
723 "(ILandroid/media/MediaMetadataRetriever$BitmapParams;II)Landroid/graphics/Bitmap;",
724 (void *)android_media_MediaMetadataRetriever_getThumbnailImageAtIndex
725 },
726
727 {
728 "_getFrameAtIndex",
729 "(IILandroid/media/MediaMetadataRetriever$BitmapParams;)Ljava/util/List;",
730 (void *)android_media_MediaMetadataRetriever_getFrameAtIndex
731 },
732
733 {"nativeExtractMetadata", "(I)Ljava/lang/String;",
734 (void *)android_media_MediaMetadataRetriever_extractMetadata},
735 {"getEmbeddedPicture", "(I)[B",
736 (void *)android_media_MediaMetadataRetriever_getEmbeddedPicture},
737 {"release", "()V",
738 (void *)android_media_MediaMetadataRetriever_release},
739 {"native_finalize", "()V",
740 (void *)android_media_MediaMetadataRetriever_native_finalize},
741 {"native_setup", "()V",
742 (void *)android_media_MediaMetadataRetriever_native_setup},
743 {"native_init", "()V",
744 (void *)android_media_MediaMetadataRetriever_native_init},
745 };
746
747 // This function only registers the native methods, and is called from
748 // JNI_OnLoad in android_media_MediaPlayer.cpp
register_android_media_MediaMetadataRetriever(JNIEnv * env)749 int register_android_media_MediaMetadataRetriever(JNIEnv *env)
750 {
751 return AndroidRuntime::registerNativeMethods
752 (env, kClassPathName, nativeMethods, NELEM(nativeMethods));
753 }
754