android_media_MediaPlayer.cpp revision 20cb94eeb5b9672573fc86bf51e09bd66a774581
1/* //device/libs/android_runtime/android_media_MediaPlayer.cpp
2**
3** Copyright 2007, 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 "MediaPlayer-JNI"
20#include "utils/Log.h"
21
22#include <media/mediaplayer.h>
23#include <media/MediaPlayerInterface.h>
24#include <stdio.h>
25#include <assert.h>
26#include <limits.h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <utils/threads.h>
30#include "jni.h"
31#include "JNIHelp.h"
32#include "android_runtime/AndroidRuntime.h"
33#include "utils/Errors.h"  // for status_t
34#include "android_util_Binder.h"
35#include <binder/Parcel.h>
36
37
38// ----------------------------------------------------------------------------
39
40using namespace android;
41
42// ----------------------------------------------------------------------------
43
44struct fields_t {
45    jfieldID    context;
46    jfieldID    surface;
47    /* actually in android.view.Surface XXX */
48    jfieldID    surface_native;
49
50    jmethodID   post_event;
51};
52static fields_t fields;
53
54static Mutex sLock;
55
56// ----------------------------------------------------------------------------
57// ref-counted object for callbacks
58class JNIMediaPlayerListener: public MediaPlayerListener
59{
60public:
61    JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
62    ~JNIMediaPlayerListener();
63    void notify(int msg, int ext1, int ext2);
64private:
65    JNIMediaPlayerListener();
66    jclass      mClass;     // Reference to MediaPlayer class
67    jobject     mObject;    // Weak ref to MediaPlayer Java object to call on
68};
69
70JNIMediaPlayerListener::JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz)
71{
72
73    // Hold onto the MediaPlayer class for use in calling the static method
74    // that posts events to the application thread.
75    jclass clazz = env->GetObjectClass(thiz);
76    if (clazz == NULL) {
77        LOGE("Can't find android/media/MediaPlayer");
78        jniThrowException(env, "java/lang/Exception", NULL);
79        return;
80    }
81    mClass = (jclass)env->NewGlobalRef(clazz);
82
83    // We use a weak reference so the MediaPlayer object can be garbage collected.
84    // The reference is only used as a proxy for callbacks.
85    mObject  = env->NewGlobalRef(weak_thiz);
86}
87
88JNIMediaPlayerListener::~JNIMediaPlayerListener()
89{
90    // remove global references
91    JNIEnv *env = AndroidRuntime::getJNIEnv();
92    env->DeleteGlobalRef(mObject);
93    env->DeleteGlobalRef(mClass);
94}
95
96void JNIMediaPlayerListener::notify(int msg, int ext1, int ext2)
97{
98    JNIEnv *env = AndroidRuntime::getJNIEnv();
99    env->CallStaticVoidMethod(mClass, fields.post_event, mObject, msg, ext1, ext2, 0);
100}
101
102// ----------------------------------------------------------------------------
103
104static sp<Surface> get_surface(JNIEnv* env, jobject clazz)
105{
106    Surface* const p = (Surface*)env->GetIntField(clazz, fields.surface_native);
107    return sp<Surface>(p);
108}
109
110static sp<MediaPlayer> getMediaPlayer(JNIEnv* env, jobject thiz)
111{
112    Mutex::Autolock l(sLock);
113    MediaPlayer* const p = (MediaPlayer*)env->GetIntField(thiz, fields.context);
114    return sp<MediaPlayer>(p);
115}
116
117static sp<MediaPlayer> setMediaPlayer(JNIEnv* env, jobject thiz, const sp<MediaPlayer>& player)
118{
119    Mutex::Autolock l(sLock);
120    sp<MediaPlayer> old = (MediaPlayer*)env->GetIntField(thiz, fields.context);
121    if (player.get()) {
122        player->incStrong(thiz);
123    }
124    if (old != 0) {
125        old->decStrong(thiz);
126    }
127    env->SetIntField(thiz, fields.context, (int)player.get());
128    return old;
129}
130
131// If exception is NULL and opStatus is not OK, this method sends an error
132// event to the client application; otherwise, if exception is not NULL and
133// opStatus is not OK, this method throws the given exception to the client
134// application.
135static void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message)
136{
137    if (exception == NULL) {  // Don't throw exception. Instead, send an event.
138        if (opStatus != (status_t) OK) {
139            sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
140            if (mp != 0) mp->notify(MEDIA_ERROR, opStatus, 0);
141        }
142    } else {  // Throw exception!
143        if ( opStatus == (status_t) INVALID_OPERATION ) {
144            jniThrowException(env, "java/lang/IllegalStateException", NULL);
145        } else if ( opStatus != (status_t) OK ) {
146            if (strlen(message) > 230) {
147               // if the message is too long, don't bother displaying the status code
148               jniThrowException( env, exception, message);
149            } else {
150               char msg[256];
151                // append the status code to the message
152               sprintf(msg, "%s: status=0x%X", message, opStatus);
153               jniThrowException( env, exception, msg);
154            }
155        }
156    }
157}
158
159static void
160android_media_MediaPlayer_setDataSource(JNIEnv *env, jobject thiz, jstring path)
161{
162    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
163    if (mp == NULL ) {
164        jniThrowException(env, "java/lang/IllegalStateException", NULL);
165        return;
166    }
167
168    if (path == NULL) {
169        jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
170        return;
171    }
172
173    const char *pathStr = env->GetStringUTFChars(path, NULL);
174    if (pathStr == NULL) {  // Out of memory
175        jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
176        return;
177    }
178    LOGV("setDataSource: path %s", pathStr);
179    status_t opStatus = mp->setDataSource(pathStr);
180
181    // Make sure that local ref is released before a potential exception
182    env->ReleaseStringUTFChars(path, pathStr);
183    process_media_player_call( env, thiz, opStatus, "java/io/IOException", "setDataSource failed." );
184}
185
186static void
187android_media_MediaPlayer_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length)
188{
189    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
190    if (mp == NULL ) {
191        jniThrowException(env, "java/lang/IllegalStateException", NULL);
192        return;
193    }
194
195    if (fileDescriptor == NULL) {
196        jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
197        return;
198    }
199    int fd = getParcelFileDescriptorFD(env, fileDescriptor);
200    LOGV("setDataSourceFD: fd %d", fd);
201    process_media_player_call( env, thiz, mp->setDataSource(fd, offset, length), "java/io/IOException", "setDataSourceFD failed." );
202}
203
204static void setVideoSurface(const sp<MediaPlayer>& mp, JNIEnv *env, jobject thiz)
205{
206    jobject surface = env->GetObjectField(thiz, fields.surface);
207    if (surface != NULL) {
208        const sp<Surface>& native_surface = get_surface(env, surface);
209        LOGV("prepare: surface=%p (id=%d)",
210             native_surface.get(), native_surface->ID());
211        mp->setVideoSurface(native_surface);
212    }
213}
214
215static void
216android_media_MediaPlayer_setVideoSurface(JNIEnv *env, jobject thiz)
217{
218    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
219    if (mp == NULL ) {
220        jniThrowException(env, "java/lang/IllegalStateException", NULL);
221        return;
222    }
223    setVideoSurface(mp, env, thiz);
224}
225
226static void
227android_media_MediaPlayer_prepare(JNIEnv *env, jobject thiz)
228{
229    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
230    if (mp == NULL ) {
231        jniThrowException(env, "java/lang/IllegalStateException", NULL);
232        return;
233    }
234    setVideoSurface(mp, env, thiz);
235    process_media_player_call( env, thiz, mp->prepare(), "java/io/IOException", "Prepare failed." );
236}
237
238static void
239android_media_MediaPlayer_prepareAsync(JNIEnv *env, jobject thiz)
240{
241    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
242    if (mp == NULL ) {
243        jniThrowException(env, "java/lang/IllegalStateException", NULL);
244        return;
245    }
246    jobject surface = env->GetObjectField(thiz, fields.surface);
247    if (surface != NULL) {
248        const sp<Surface>& native_surface = get_surface(env, surface);
249        LOGV("prepareAsync: surface=%p (id=%d)",
250             native_surface.get(), native_surface->ID());
251        mp->setVideoSurface(native_surface);
252    }
253    process_media_player_call( env, thiz, mp->prepareAsync(), "java/io/IOException", "Prepare Async failed." );
254}
255
256static void
257android_media_MediaPlayer_start(JNIEnv *env, jobject thiz)
258{
259    LOGV("start");
260    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
261    if (mp == NULL ) {
262        jniThrowException(env, "java/lang/IllegalStateException", NULL);
263        return;
264    }
265    process_media_player_call( env, thiz, mp->start(), NULL, NULL );
266}
267
268static void
269android_media_MediaPlayer_stop(JNIEnv *env, jobject thiz)
270{
271    LOGV("stop");
272    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
273    if (mp == NULL ) {
274        jniThrowException(env, "java/lang/IllegalStateException", NULL);
275        return;
276    }
277    process_media_player_call( env, thiz, mp->stop(), NULL, NULL );
278}
279
280static void
281android_media_MediaPlayer_pause(JNIEnv *env, jobject thiz)
282{
283    LOGV("pause");
284    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
285    if (mp == NULL ) {
286        jniThrowException(env, "java/lang/IllegalStateException", NULL);
287        return;
288    }
289    process_media_player_call( env, thiz, mp->pause(), NULL, NULL );
290}
291
292static jboolean
293android_media_MediaPlayer_isPlaying(JNIEnv *env, jobject thiz)
294{
295    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
296    if (mp == NULL ) {
297        jniThrowException(env, "java/lang/IllegalStateException", NULL);
298        return false;
299    }
300    const jboolean is_playing = mp->isPlaying();
301
302    LOGV("isPlaying: %d", is_playing);
303    return is_playing;
304}
305
306static void
307android_media_MediaPlayer_seekTo(JNIEnv *env, jobject thiz, int msec)
308{
309    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
310    if (mp == NULL ) {
311        jniThrowException(env, "java/lang/IllegalStateException", NULL);
312        return;
313    }
314    LOGV("seekTo: %d(msec)", msec);
315    process_media_player_call( env, thiz, mp->seekTo(msec), NULL, NULL );
316}
317
318static int
319android_media_MediaPlayer_getVideoWidth(JNIEnv *env, jobject thiz)
320{
321    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
322    if (mp == NULL ) {
323        jniThrowException(env, "java/lang/IllegalStateException", NULL);
324        return 0;
325    }
326    int w;
327    if (0 != mp->getVideoWidth(&w)) {
328        LOGE("getVideoWidth failed");
329        w = 0;
330    }
331    LOGV("getVideoWidth: %d", w);
332    return w;
333}
334
335static int
336android_media_MediaPlayer_getVideoHeight(JNIEnv *env, jobject thiz)
337{
338    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
339    if (mp == NULL ) {
340        jniThrowException(env, "java/lang/IllegalStateException", NULL);
341        return 0;
342    }
343    int h;
344    if (0 != mp->getVideoHeight(&h)) {
345        LOGE("getVideoHeight failed");
346        h = 0;
347    }
348    LOGV("getVideoHeight: %d", h);
349    return h;
350}
351
352
353static int
354android_media_MediaPlayer_getCurrentPosition(JNIEnv *env, jobject thiz)
355{
356    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
357    if (mp == NULL ) {
358        jniThrowException(env, "java/lang/IllegalStateException", NULL);
359        return 0;
360    }
361    int msec;
362    process_media_player_call( env, thiz, mp->getCurrentPosition(&msec), NULL, NULL );
363    LOGV("getCurrentPosition: %d (msec)", msec);
364    return msec;
365}
366
367static int
368android_media_MediaPlayer_getDuration(JNIEnv *env, jobject thiz)
369{
370    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
371    if (mp == NULL ) {
372        jniThrowException(env, "java/lang/IllegalStateException", NULL);
373        return 0;
374    }
375    int msec;
376    process_media_player_call( env, thiz, mp->getDuration(&msec), NULL, NULL );
377    LOGV("getDuration: %d (msec)", msec);
378    return msec;
379}
380
381static void
382android_media_MediaPlayer_reset(JNIEnv *env, jobject thiz)
383{
384    LOGV("reset");
385    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
386    if (mp == NULL ) {
387        jniThrowException(env, "java/lang/IllegalStateException", NULL);
388        return;
389    }
390    process_media_player_call( env, thiz, mp->reset(), NULL, NULL );
391}
392
393static void
394android_media_MediaPlayer_setAudioStreamType(JNIEnv *env, jobject thiz, int streamtype)
395{
396    LOGV("setAudioStreamType: %d", streamtype);
397    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
398    if (mp == NULL ) {
399        jniThrowException(env, "java/lang/IllegalStateException", NULL);
400        return;
401    }
402    process_media_player_call( env, thiz, mp->setAudioStreamType(streamtype) , NULL, NULL );
403}
404
405static void
406android_media_MediaPlayer_setLooping(JNIEnv *env, jobject thiz, jboolean looping)
407{
408    LOGV("setLooping: %d", looping);
409    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
410    if (mp == NULL ) {
411        jniThrowException(env, "java/lang/IllegalStateException", NULL);
412        return;
413    }
414    process_media_player_call( env, thiz, mp->setLooping(looping), NULL, NULL );
415}
416
417static jboolean
418android_media_MediaPlayer_isLooping(JNIEnv *env, jobject thiz)
419{
420    LOGV("isLooping");
421    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
422    if (mp == NULL ) {
423        jniThrowException(env, "java/lang/IllegalStateException", NULL);
424        return false;
425    }
426    return mp->isLooping();
427}
428
429static void
430android_media_MediaPlayer_setVolume(JNIEnv *env, jobject thiz, float leftVolume, float rightVolume)
431{
432    LOGV("setVolume: left %f  right %f", leftVolume, rightVolume);
433    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
434    if (mp == NULL ) {
435        jniThrowException(env, "java/lang/IllegalStateException", NULL);
436        return;
437    }
438    process_media_player_call( env, thiz, mp->setVolume(leftVolume, rightVolume), NULL, NULL );
439}
440
441// FIXME: deprecated
442static jobject
443android_media_MediaPlayer_getFrameAt(JNIEnv *env, jobject thiz, jint msec)
444{
445    return NULL;
446}
447
448
449// Sends the request and reply parcels to the media player via the
450// binder interface.
451static jint
452android_media_MediaPlayer_invoke(JNIEnv *env, jobject thiz,
453                                 jobject java_request, jobject java_reply)
454{
455    sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
456    if (media_player == NULL ) {
457        jniThrowException(env, "java/lang/IllegalStateException", NULL);
458    }
459
460
461    Parcel *request = parcelForJavaObject(env, java_request);
462    Parcel *reply = parcelForJavaObject(env, java_reply);
463
464    const status_t status = media_player->invoke(*request, reply);
465    // Don't use process_media_player_call which use the async loop to
466    // report errors, instead returns the status.
467    return status;
468}
469
470static void
471android_media_MediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
472{
473    LOGV("native_setup");
474    sp<MediaPlayer> mp = new MediaPlayer();
475    if (mp == NULL) {
476        jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
477        return;
478    }
479
480    // create new listener and give it to MediaPlayer
481    sp<JNIMediaPlayerListener> listener = new JNIMediaPlayerListener(env, thiz, weak_this);
482    mp->setListener(listener);
483
484    // Stow our new C++ MediaPlayer in an opaque field in the Java object.
485    setMediaPlayer(env, thiz, mp);
486}
487
488static void
489android_media_MediaPlayer_release(JNIEnv *env, jobject thiz)
490{
491    LOGV("release");
492    sp<MediaPlayer> mp = setMediaPlayer(env, thiz, 0);
493    if (mp != NULL) {
494        // this prevents native callbacks after the object is released
495        mp->setListener(0);
496        mp->disconnect();
497    }
498}
499
500static void
501android_media_MediaPlayer_native_finalize(JNIEnv *env, jobject thiz)
502{
503    LOGV("native_finalize");
504    android_media_MediaPlayer_release(env, thiz);
505}
506
507// ----------------------------------------------------------------------------
508
509static JNINativeMethod gMethods[] = {
510    {"setDataSource",       "(Ljava/lang/String;)V",            (void *)android_media_MediaPlayer_setDataSource},
511    {"setDataSource",       "(Ljava/io/FileDescriptor;JJ)V",    (void *)android_media_MediaPlayer_setDataSourceFD},
512    {"_setVideoSurface",    "()V",                              (void *)android_media_MediaPlayer_setVideoSurface},
513    {"prepare",             "()V",                              (void *)android_media_MediaPlayer_prepare},
514    {"prepareAsync",        "()V",                              (void *)android_media_MediaPlayer_prepareAsync},
515    {"_start",              "()V",                              (void *)android_media_MediaPlayer_start},
516    {"_stop",               "()V",                              (void *)android_media_MediaPlayer_stop},
517    {"getVideoWidth",       "()I",                              (void *)android_media_MediaPlayer_getVideoWidth},
518    {"getVideoHeight",      "()I",                              (void *)android_media_MediaPlayer_getVideoHeight},
519    {"seekTo",              "(I)V",                             (void *)android_media_MediaPlayer_seekTo},
520    {"_pause",              "()V",                              (void *)android_media_MediaPlayer_pause},
521    {"isPlaying",           "()Z",                              (void *)android_media_MediaPlayer_isPlaying},
522    {"getCurrentPosition",  "()I",                              (void *)android_media_MediaPlayer_getCurrentPosition},
523    {"getDuration",         "()I",                              (void *)android_media_MediaPlayer_getDuration},
524    {"_release",            "()V",                              (void *)android_media_MediaPlayer_release},
525    {"_reset",              "()V",                              (void *)android_media_MediaPlayer_reset},
526    {"setAudioStreamType",  "(I)V",                             (void *)android_media_MediaPlayer_setAudioStreamType},
527    {"setLooping",          "(Z)V",                             (void *)android_media_MediaPlayer_setLooping},
528    {"isLooping",           "()Z",                              (void *)android_media_MediaPlayer_isLooping},
529    {"setVolume",           "(FF)V",                            (void *)android_media_MediaPlayer_setVolume},
530    {"getFrameAt",          "(I)Landroid/graphics/Bitmap;",     (void *)android_media_MediaPlayer_getFrameAt},
531    {"native_invoke",       "(Landroid/os/Parcel;Landroid/os/Parcel;)I",(void *)android_media_MediaPlayer_invoke},
532    {"native_setup",        "(Ljava/lang/Object;)V",            (void *)android_media_MediaPlayer_native_setup},
533    {"native_finalize",     "()V",                              (void *)android_media_MediaPlayer_native_finalize},
534};
535
536static const char* const kClassPathName = "android/media/MediaPlayer";
537
538static int register_android_media_MediaPlayer(JNIEnv *env)
539{
540    jclass clazz;
541
542    clazz = env->FindClass("android/media/MediaPlayer");
543    if (clazz == NULL) {
544        LOGE("Can't find android/media/MediaPlayer");
545        return -1;
546    }
547
548    fields.context = env->GetFieldID(clazz, "mNativeContext", "I");
549    if (fields.context == NULL) {
550        LOGE("Can't find MediaPlayer.mNativeContext");
551        return -1;
552    }
553
554    fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
555                                               "(Ljava/lang/Object;IIILjava/lang/Object;)V");
556    if (fields.post_event == NULL) {
557        LOGE("Can't find MediaPlayer.postEventFromNative");
558        return -1;
559    }
560
561    fields.surface = env->GetFieldID(clazz, "mSurface", "Landroid/view/Surface;");
562    if (fields.surface == NULL) {
563        LOGE("Can't find MediaPlayer.mSurface");
564        return -1;
565    }
566
567    jclass surface = env->FindClass("android/view/Surface");
568    if (surface == NULL) {
569        LOGE("Can't find android/view/Surface");
570        return -1;
571    }
572
573    fields.surface_native = env->GetFieldID(surface, "mSurface", "I");
574    if (fields.surface_native == NULL) {
575        LOGE("Can't find Surface fields");
576        return -1;
577    }
578
579    return AndroidRuntime::registerNativeMethods(env,
580                "android/media/MediaPlayer", gMethods, NELEM(gMethods));
581}
582
583extern int register_android_media_MediaRecorder(JNIEnv *env);
584extern int register_android_media_MediaScanner(JNIEnv *env);
585extern int register_android_media_MediaMetadataRetriever(JNIEnv *env);
586extern int register_android_media_AmrInputStream(JNIEnv *env);
587extern int register_android_media_ResampleInputStream(JNIEnv *env);
588
589jint JNI_OnLoad(JavaVM* vm, void* reserved)
590{
591    JNIEnv* env = NULL;
592    jint result = -1;
593
594    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
595        LOGE("ERROR: GetEnv failed\n");
596        goto bail;
597    }
598    assert(env != NULL);
599
600    if (register_android_media_MediaPlayer(env) < 0) {
601        LOGE("ERROR: MediaPlayer native registration failed\n");
602        goto bail;
603    }
604
605    if (register_android_media_MediaRecorder(env) < 0) {
606        LOGE("ERROR: MediaRecorder native registration failed\n");
607        goto bail;
608    }
609
610    if (register_android_media_MediaScanner(env) < 0) {
611        LOGE("ERROR: MediaScanner native registration failed\n");
612        goto bail;
613    }
614
615    if (register_android_media_MediaMetadataRetriever(env) < 0) {
616        LOGE("ERROR: MediaMetadataRetriever native registration failed\n");
617        goto bail;
618    }
619
620    if (register_android_media_AmrInputStream(env) < 0) {
621        LOGE("ERROR: AmrInputStream native registration failed\n");
622        goto bail;
623    }
624
625    if (register_android_media_ResampleInputStream(env) < 0) {
626        LOGE("ERROR: ResampleInputStream native registration failed\n");
627        goto bail;
628    }
629
630    /* success -- return valid version number */
631    result = JNI_VERSION_1_4;
632
633bail:
634    return result;
635}
636
637// KTHXBYE
638