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