android_media_MediaPlayer.cpp revision 81b37d8bde34ef3bb8eb8dfd7492761a39a8fd09
1/*
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 "android_runtime/android_view_Surface.h"
34#include "utils/Errors.h"  // for status_t
35#include "utils/KeyedVector.h"
36#include "utils/String8.h"
37#include "android_media_Utils.h"
38
39#include "android_util_Binder.h"
40#include <binder/Parcel.h>
41#include <gui/ISurfaceTexture.h>
42#include <surfaceflinger/Surface.h>
43#include <binder/IPCThreadState.h>
44#include <binder/IServiceManager.h>
45
46// ----------------------------------------------------------------------------
47
48using namespace android;
49
50// ----------------------------------------------------------------------------
51
52struct fields_t {
53    jfieldID    context;
54    jfieldID    surface_texture;
55
56    jmethodID   post_event;
57};
58static fields_t fields;
59
60static Mutex sLock;
61
62// ----------------------------------------------------------------------------
63// ref-counted object for callbacks
64class JNIMediaPlayerListener: public MediaPlayerListener
65{
66public:
67    JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
68    ~JNIMediaPlayerListener();
69    virtual void notify(int msg, int ext1, int ext2, const Parcel *obj = NULL);
70private:
71    JNIMediaPlayerListener();
72    jclass      mClass;     // Reference to MediaPlayer class
73    jobject     mObject;    // Weak ref to MediaPlayer Java object to call on
74};
75
76JNIMediaPlayerListener::JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz)
77{
78
79    // Hold onto the MediaPlayer class for use in calling the static method
80    // that posts events to the application thread.
81    jclass clazz = env->GetObjectClass(thiz);
82    if (clazz == NULL) {
83        LOGE("Can't find android/media/MediaPlayer");
84        jniThrowException(env, "java/lang/Exception", NULL);
85        return;
86    }
87    mClass = (jclass)env->NewGlobalRef(clazz);
88
89    // We use a weak reference so the MediaPlayer object can be garbage collected.
90    // The reference is only used as a proxy for callbacks.
91    mObject  = env->NewGlobalRef(weak_thiz);
92}
93
94JNIMediaPlayerListener::~JNIMediaPlayerListener()
95{
96    // remove global references
97    JNIEnv *env = AndroidRuntime::getJNIEnv();
98    env->DeleteGlobalRef(mObject);
99    env->DeleteGlobalRef(mClass);
100}
101
102void JNIMediaPlayerListener::notify(int msg, int ext1, int ext2, const Parcel *obj)
103{
104    JNIEnv *env = AndroidRuntime::getJNIEnv();
105    if (obj && obj->dataSize() > 0) {
106        jbyteArray jArray = env->NewByteArray(obj->dataSize());
107        if (jArray != NULL) {
108            jbyte *nArray = env->GetByteArrayElements(jArray, NULL);
109            memcpy(nArray, obj->data(), obj->dataSize());
110            env->ReleaseByteArrayElements(jArray, nArray, 0);
111            env->CallStaticVoidMethod(mClass, fields.post_event, mObject,
112                    msg, ext1, ext2, jArray);
113            env->DeleteLocalRef(jArray);
114        }
115    } else {
116        env->CallStaticVoidMethod(mClass, fields.post_event, mObject,
117                msg, ext1, ext2, NULL);
118    }
119}
120
121// ----------------------------------------------------------------------------
122
123static sp<MediaPlayer> getMediaPlayer(JNIEnv* env, jobject thiz)
124{
125    Mutex::Autolock l(sLock);
126    MediaPlayer* const p = (MediaPlayer*)env->GetIntField(thiz, fields.context);
127    return sp<MediaPlayer>(p);
128}
129
130static sp<MediaPlayer> setMediaPlayer(JNIEnv* env, jobject thiz, const sp<MediaPlayer>& player)
131{
132    Mutex::Autolock l(sLock);
133    sp<MediaPlayer> old = (MediaPlayer*)env->GetIntField(thiz, fields.context);
134    if (player.get()) {
135        player->incStrong(thiz);
136    }
137    if (old != 0) {
138        old->decStrong(thiz);
139    }
140    env->SetIntField(thiz, fields.context, (int)player.get());
141    return old;
142}
143
144// If exception is NULL and opStatus is not OK, this method sends an error
145// event to the client application; otherwise, if exception is not NULL and
146// opStatus is not OK, this method throws the given exception to the client
147// application.
148static void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message)
149{
150    if (exception == NULL) {  // Don't throw exception. Instead, send an event.
151        if (opStatus != (status_t) OK) {
152            sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
153            if (mp != 0) mp->notify(MEDIA_ERROR, opStatus, 0);
154        }
155    } else {  // Throw exception!
156        if ( opStatus == (status_t) INVALID_OPERATION ) {
157            jniThrowException(env, "java/lang/IllegalStateException", NULL);
158        } else if ( opStatus == (status_t) PERMISSION_DENIED ) {
159            jniThrowException(env, "java/lang/SecurityException", NULL);
160        } else if ( opStatus != (status_t) OK ) {
161            if (strlen(message) > 230) {
162               // if the message is too long, don't bother displaying the status code
163               jniThrowException( env, exception, message);
164            } else {
165               char msg[256];
166                // append the status code to the message
167               sprintf(msg, "%s: status=0x%X", message, opStatus);
168               jniThrowException( env, exception, msg);
169            }
170        }
171    }
172}
173
174static void
175android_media_MediaPlayer_setDataSourceAndHeaders(
176        JNIEnv *env, jobject thiz, jstring path,
177        jobjectArray keys, jobjectArray values) {
178
179    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
180    if (mp == NULL ) {
181        jniThrowException(env, "java/lang/IllegalStateException", NULL);
182        return;
183    }
184
185    if (path == NULL) {
186        jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
187        return;
188    }
189
190    const char *tmp = env->GetStringUTFChars(path, NULL);
191    if (tmp == NULL) {  // Out of memory
192        return;
193    }
194    LOGV("setDataSource: path %s", tmp);
195
196    String8 pathStr(tmp);
197    env->ReleaseStringUTFChars(path, tmp);
198    tmp = NULL;
199
200    // We build a KeyedVector out of the key and val arrays
201    KeyedVector<String8, String8> headersVector;
202    if (!ConvertKeyValueArraysToKeyedVector(
203            env, keys, values, &headersVector)) {
204        return;
205    }
206
207    status_t opStatus =
208        mp->setDataSource(
209                pathStr,
210                headersVector.size() > 0? &headersVector : NULL);
211
212    process_media_player_call(
213            env, thiz, opStatus, "java/io/IOException",
214            "setDataSource failed." );
215}
216
217static void
218android_media_MediaPlayer_setDataSource(JNIEnv *env, jobject thiz, jstring path)
219{
220    android_media_MediaPlayer_setDataSourceAndHeaders(env, thiz, path, NULL, NULL);
221}
222
223static void
224android_media_MediaPlayer_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length)
225{
226    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
227    if (mp == NULL ) {
228        jniThrowException(env, "java/lang/IllegalStateException", NULL);
229        return;
230    }
231
232    if (fileDescriptor == NULL) {
233        jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
234        return;
235    }
236    int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
237    LOGV("setDataSourceFD: fd %d", fd);
238    process_media_player_call( env, thiz, mp->setDataSource(fd, offset, length), "java/io/IOException", "setDataSourceFD failed." );
239}
240
241static sp<ISurfaceTexture>
242getVideoSurfaceTexture(JNIEnv* env, jobject thiz) {
243    ISurfaceTexture * const p = (ISurfaceTexture*)env->GetIntField(thiz, fields.surface_texture);
244    return sp<ISurfaceTexture>(p);
245}
246
247static void
248decVideoSurfaceRef(JNIEnv *env, jobject thiz)
249{
250    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
251    if (mp == NULL) {
252        return;
253    }
254
255    sp<ISurfaceTexture> old_st = getVideoSurfaceTexture(env, thiz);
256    if (old_st != NULL) {
257        old_st->decStrong(thiz);
258    }
259}
260
261static void
262setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface, jboolean mediaPlayerMustBeAlive)
263{
264    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
265    if (mp == NULL) {
266        if (mediaPlayerMustBeAlive) {
267            jniThrowException(env, "java/lang/IllegalStateException", NULL);
268        }
269        return;
270    }
271
272    decVideoSurfaceRef(env, thiz);
273
274    sp<ISurfaceTexture> new_st;
275    if (jsurface) {
276        sp<Surface> surface(Surface_getSurface(env, jsurface));
277        new_st = surface->getSurfaceTexture();
278        new_st->incStrong(thiz);
279    }
280
281    env->SetIntField(thiz, fields.surface_texture, (int)new_st.get());
282
283    // This will fail if the media player has not been initialized yet. This
284    // can be the case if setDisplay() on MediaPlayer.java has been called
285    // before setDataSource(). The redundant call to setVideoSurfaceTexture()
286    // in prepare/prepareAsync covers for this case.
287    mp->setVideoSurfaceTexture(new_st);
288}
289
290static void
291android_media_MediaPlayer_setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface)
292{
293    setVideoSurface(env, thiz, jsurface, true /* mediaPlayerMustBeAlive */);
294}
295
296static void
297android_media_MediaPlayer_prepare(JNIEnv *env, jobject thiz)
298{
299    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
300    if (mp == NULL ) {
301        jniThrowException(env, "java/lang/IllegalStateException", NULL);
302        return;
303    }
304
305    // Handle the case where the display surface was set before the mp was
306    // initialized. We try again to make it stick.
307    sp<ISurfaceTexture> st = getVideoSurfaceTexture(env, thiz);
308    mp->setVideoSurfaceTexture(st);
309
310    process_media_player_call( env, thiz, mp->prepare(), "java/io/IOException", "Prepare failed." );
311}
312
313static void
314android_media_MediaPlayer_prepareAsync(JNIEnv *env, jobject thiz)
315{
316    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
317    if (mp == NULL ) {
318        jniThrowException(env, "java/lang/IllegalStateException", NULL);
319        return;
320    }
321
322    // Handle the case where the display surface was set before the mp was
323    // initialized. We try again to make it stick.
324    sp<ISurfaceTexture> st = getVideoSurfaceTexture(env, thiz);
325    mp->setVideoSurfaceTexture(st);
326
327    process_media_player_call( env, thiz, mp->prepareAsync(), "java/io/IOException", "Prepare Async failed." );
328}
329
330static void
331android_media_MediaPlayer_start(JNIEnv *env, jobject thiz)
332{
333    LOGV("start");
334    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
335    if (mp == NULL ) {
336        jniThrowException(env, "java/lang/IllegalStateException", NULL);
337        return;
338    }
339    process_media_player_call( env, thiz, mp->start(), NULL, NULL );
340}
341
342static void
343android_media_MediaPlayer_stop(JNIEnv *env, jobject thiz)
344{
345    LOGV("stop");
346    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
347    if (mp == NULL ) {
348        jniThrowException(env, "java/lang/IllegalStateException", NULL);
349        return;
350    }
351    process_media_player_call( env, thiz, mp->stop(), NULL, NULL );
352}
353
354static void
355android_media_MediaPlayer_pause(JNIEnv *env, jobject thiz)
356{
357    LOGV("pause");
358    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
359    if (mp == NULL ) {
360        jniThrowException(env, "java/lang/IllegalStateException", NULL);
361        return;
362    }
363    process_media_player_call( env, thiz, mp->pause(), NULL, NULL );
364}
365
366static jboolean
367android_media_MediaPlayer_isPlaying(JNIEnv *env, jobject thiz)
368{
369    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
370    if (mp == NULL ) {
371        jniThrowException(env, "java/lang/IllegalStateException", NULL);
372        return false;
373    }
374    const jboolean is_playing = mp->isPlaying();
375
376    LOGV("isPlaying: %d", is_playing);
377    return is_playing;
378}
379
380static void
381android_media_MediaPlayer_seekTo(JNIEnv *env, jobject thiz, int msec)
382{
383    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
384    if (mp == NULL ) {
385        jniThrowException(env, "java/lang/IllegalStateException", NULL);
386        return;
387    }
388    LOGV("seekTo: %d(msec)", msec);
389    process_media_player_call( env, thiz, mp->seekTo(msec), NULL, NULL );
390}
391
392static int
393android_media_MediaPlayer_getVideoWidth(JNIEnv *env, jobject thiz)
394{
395    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
396    if (mp == NULL ) {
397        jniThrowException(env, "java/lang/IllegalStateException", NULL);
398        return 0;
399    }
400    int w;
401    if (0 != mp->getVideoWidth(&w)) {
402        LOGE("getVideoWidth failed");
403        w = 0;
404    }
405    LOGV("getVideoWidth: %d", w);
406    return w;
407}
408
409static int
410android_media_MediaPlayer_getVideoHeight(JNIEnv *env, jobject thiz)
411{
412    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
413    if (mp == NULL ) {
414        jniThrowException(env, "java/lang/IllegalStateException", NULL);
415        return 0;
416    }
417    int h;
418    if (0 != mp->getVideoHeight(&h)) {
419        LOGE("getVideoHeight failed");
420        h = 0;
421    }
422    LOGV("getVideoHeight: %d", h);
423    return h;
424}
425
426
427static int
428android_media_MediaPlayer_getCurrentPosition(JNIEnv *env, jobject thiz)
429{
430    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
431    if (mp == NULL ) {
432        jniThrowException(env, "java/lang/IllegalStateException", NULL);
433        return 0;
434    }
435    int msec;
436    process_media_player_call( env, thiz, mp->getCurrentPosition(&msec), NULL, NULL );
437    LOGV("getCurrentPosition: %d (msec)", msec);
438    return msec;
439}
440
441static int
442android_media_MediaPlayer_getDuration(JNIEnv *env, jobject thiz)
443{
444    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
445    if (mp == NULL ) {
446        jniThrowException(env, "java/lang/IllegalStateException", NULL);
447        return 0;
448    }
449    int msec;
450    process_media_player_call( env, thiz, mp->getDuration(&msec), NULL, NULL );
451    LOGV("getDuration: %d (msec)", msec);
452    return msec;
453}
454
455static void
456android_media_MediaPlayer_reset(JNIEnv *env, jobject thiz)
457{
458    LOGV("reset");
459    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
460    if (mp == NULL ) {
461        jniThrowException(env, "java/lang/IllegalStateException", NULL);
462        return;
463    }
464    process_media_player_call( env, thiz, mp->reset(), NULL, NULL );
465}
466
467static void
468android_media_MediaPlayer_setAudioStreamType(JNIEnv *env, jobject thiz, int streamtype)
469{
470    LOGV("setAudioStreamType: %d", streamtype);
471    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
472    if (mp == NULL ) {
473        jniThrowException(env, "java/lang/IllegalStateException", NULL);
474        return;
475    }
476    process_media_player_call( env, thiz, mp->setAudioStreamType(streamtype) , NULL, NULL );
477}
478
479static void
480android_media_MediaPlayer_setLooping(JNIEnv *env, jobject thiz, jboolean looping)
481{
482    LOGV("setLooping: %d", looping);
483    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
484    if (mp == NULL ) {
485        jniThrowException(env, "java/lang/IllegalStateException", NULL);
486        return;
487    }
488    process_media_player_call( env, thiz, mp->setLooping(looping), NULL, NULL );
489}
490
491static jboolean
492android_media_MediaPlayer_isLooping(JNIEnv *env, jobject thiz)
493{
494    LOGV("isLooping");
495    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
496    if (mp == NULL ) {
497        jniThrowException(env, "java/lang/IllegalStateException", NULL);
498        return false;
499    }
500    return mp->isLooping();
501}
502
503static void
504android_media_MediaPlayer_setVolume(JNIEnv *env, jobject thiz, float leftVolume, float rightVolume)
505{
506    LOGV("setVolume: left %f  right %f", leftVolume, rightVolume);
507    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
508    if (mp == NULL ) {
509        jniThrowException(env, "java/lang/IllegalStateException", NULL);
510        return;
511    }
512    process_media_player_call( env, thiz, mp->setVolume(leftVolume, rightVolume), NULL, NULL );
513}
514
515// FIXME: deprecated
516static jobject
517android_media_MediaPlayer_getFrameAt(JNIEnv *env, jobject thiz, jint msec)
518{
519    return NULL;
520}
521
522
523// Sends the request and reply parcels to the media player via the
524// binder interface.
525static jint
526android_media_MediaPlayer_invoke(JNIEnv *env, jobject thiz,
527                                 jobject java_request, jobject java_reply)
528{
529    sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
530    if (media_player == NULL ) {
531        jniThrowException(env, "java/lang/IllegalStateException", NULL);
532        return UNKNOWN_ERROR;
533    }
534
535
536    Parcel *request = parcelForJavaObject(env, java_request);
537    Parcel *reply = parcelForJavaObject(env, java_reply);
538
539    // Don't use process_media_player_call which use the async loop to
540    // report errors, instead returns the status.
541    return media_player->invoke(*request, reply);
542}
543
544// Sends the new filter to the client.
545static jint
546android_media_MediaPlayer_setMetadataFilter(JNIEnv *env, jobject thiz, jobject request)
547{
548    sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
549    if (media_player == NULL ) {
550        jniThrowException(env, "java/lang/IllegalStateException", NULL);
551        return UNKNOWN_ERROR;
552    }
553
554    Parcel *filter = parcelForJavaObject(env, request);
555
556    if (filter == NULL ) {
557        jniThrowException(env, "java/lang/RuntimeException", "Filter is null");
558        return UNKNOWN_ERROR;
559    }
560
561    return media_player->setMetadataFilter(*filter);
562}
563
564static jboolean
565android_media_MediaPlayer_getMetadata(JNIEnv *env, jobject thiz, jboolean update_only,
566                                      jboolean apply_filter, jobject reply)
567{
568    sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
569    if (media_player == NULL ) {
570        jniThrowException(env, "java/lang/IllegalStateException", NULL);
571        return false;
572    }
573
574    Parcel *metadata = parcelForJavaObject(env, reply);
575
576    if (metadata == NULL ) {
577        jniThrowException(env, "java/lang/RuntimeException", "Reply parcel is null");
578        return false;
579    }
580
581    metadata->freeData();
582    // On return metadata is positioned at the beginning of the
583    // metadata. Note however that the parcel actually starts with the
584    // return code so you should not rewind the parcel using
585    // setDataPosition(0).
586    return media_player->getMetadata(update_only, apply_filter, metadata) == OK;
587}
588
589// This function gets some field IDs, which in turn causes class initialization.
590// It is called from a static block in MediaPlayer, which won't run until the
591// first time an instance of this class is used.
592static void
593android_media_MediaPlayer_native_init(JNIEnv *env)
594{
595    jclass clazz;
596
597    clazz = env->FindClass("android/media/MediaPlayer");
598    if (clazz == NULL) {
599        return;
600    }
601
602    fields.context = env->GetFieldID(clazz, "mNativeContext", "I");
603    if (fields.context == NULL) {
604        return;
605    }
606
607    fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
608                                               "(Ljava/lang/Object;IIILjava/lang/Object;)V");
609    if (fields.post_event == NULL) {
610        return;
611    }
612
613    fields.surface_texture = env->GetFieldID(clazz, "mNativeSurfaceTexture", "I");
614    if (fields.surface_texture == NULL) {
615        return;
616    }
617}
618
619static void
620android_media_MediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
621{
622    LOGV("native_setup");
623    sp<MediaPlayer> mp = new MediaPlayer();
624    if (mp == NULL) {
625        jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
626        return;
627    }
628
629    // create new listener and give it to MediaPlayer
630    sp<JNIMediaPlayerListener> listener = new JNIMediaPlayerListener(env, thiz, weak_this);
631    mp->setListener(listener);
632
633    // Stow our new C++ MediaPlayer in an opaque field in the Java object.
634    setMediaPlayer(env, thiz, mp);
635}
636
637static void
638android_media_MediaPlayer_release(JNIEnv *env, jobject thiz)
639{
640    LOGV("release");
641    decVideoSurfaceRef(env, thiz);
642    sp<MediaPlayer> mp = setMediaPlayer(env, thiz, 0);
643    if (mp != NULL) {
644        // this prevents native callbacks after the object is released
645        mp->setListener(0);
646        mp->disconnect();
647    }
648}
649
650static void
651android_media_MediaPlayer_native_finalize(JNIEnv *env, jobject thiz)
652{
653    LOGV("native_finalize");
654    android_media_MediaPlayer_release(env, thiz);
655}
656
657static void android_media_MediaPlayer_set_audio_session_id(JNIEnv *env,  jobject thiz, jint sessionId) {
658    LOGV("set_session_id(): %d", sessionId);
659    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
660    if (mp == NULL ) {
661        jniThrowException(env, "java/lang/IllegalStateException", NULL);
662        return;
663    }
664    process_media_player_call( env, thiz, mp->setAudioSessionId(sessionId), NULL, NULL );
665}
666
667static jint android_media_MediaPlayer_get_audio_session_id(JNIEnv *env,  jobject thiz) {
668    LOGV("get_session_id()");
669    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
670    if (mp == NULL ) {
671        jniThrowException(env, "java/lang/IllegalStateException", NULL);
672        return 0;
673    }
674
675    return mp->getAudioSessionId();
676}
677
678static void
679android_media_MediaPlayer_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level)
680{
681    LOGV("setAuxEffectSendLevel: level %f", level);
682    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
683    if (mp == NULL ) {
684        jniThrowException(env, "java/lang/IllegalStateException", NULL);
685        return;
686    }
687    process_media_player_call( env, thiz, mp->setAuxEffectSendLevel(level), NULL, NULL );
688}
689
690static void android_media_MediaPlayer_attachAuxEffect(JNIEnv *env,  jobject thiz, jint effectId) {
691    LOGV("attachAuxEffect(): %d", effectId);
692    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
693    if (mp == NULL ) {
694        jniThrowException(env, "java/lang/IllegalStateException", NULL);
695        return;
696    }
697    process_media_player_call( env, thiz, mp->attachAuxEffect(effectId), NULL, NULL );
698}
699
700static jint
701android_media_MediaPlayer_pullBatteryData(JNIEnv *env, jobject thiz, jobject java_reply)
702{
703    sp<IBinder> binder = defaultServiceManager()->getService(String16("media.player"));
704    sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
705    if (service.get() == NULL) {
706        jniThrowException(env, "java/lang/RuntimeException", "cannot get MediaPlayerService");
707        return UNKNOWN_ERROR;
708    }
709
710    Parcel *reply = parcelForJavaObject(env, java_reply);
711
712    return service->pullBatteryData(reply);
713}
714
715static jboolean
716android_media_MediaPlayer_setParameter(JNIEnv *env, jobject thiz, jint key, jobject java_request)
717{
718    LOGV("setParameter: key %d", key);
719    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
720    if (mp == NULL ) {
721        jniThrowException(env, "java/lang/IllegalStateException", NULL);
722        return false;
723    }
724
725    Parcel *request = parcelForJavaObject(env, java_request);
726    status_t err = mp->setParameter(key, *request);
727    if (err == OK) {
728        return true;
729    } else {
730        return false;
731    }
732}
733
734static void
735android_media_MediaPlayer_getParameter(JNIEnv *env, jobject thiz, jint key, jobject java_reply)
736{
737    LOGV("getParameter: key %d", key);
738    sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
739    if (mp == NULL ) {
740        jniThrowException(env, "java/lang/IllegalStateException", NULL);
741        return;
742    }
743
744    Parcel *reply = parcelForJavaObject(env, java_reply);
745    process_media_player_call(env, thiz, mp->getParameter(key, reply), NULL, NULL );
746}
747
748// ----------------------------------------------------------------------------
749
750static JNINativeMethod gMethods[] = {
751    {"setDataSource",       "(Ljava/lang/String;)V",            (void *)android_media_MediaPlayer_setDataSource},
752
753    {
754        "_setDataSource",
755        "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;)V",
756        (void *)android_media_MediaPlayer_setDataSourceAndHeaders
757    },
758
759    {"setDataSource",       "(Ljava/io/FileDescriptor;JJ)V",    (void *)android_media_MediaPlayer_setDataSourceFD},
760    {"_setVideoSurface",    "(Landroid/view/Surface;)V",        (void *)android_media_MediaPlayer_setVideoSurface},
761    {"prepare",             "()V",                              (void *)android_media_MediaPlayer_prepare},
762    {"prepareAsync",        "()V",                              (void *)android_media_MediaPlayer_prepareAsync},
763    {"_start",              "()V",                              (void *)android_media_MediaPlayer_start},
764    {"_stop",               "()V",                              (void *)android_media_MediaPlayer_stop},
765    {"getVideoWidth",       "()I",                              (void *)android_media_MediaPlayer_getVideoWidth},
766    {"getVideoHeight",      "()I",                              (void *)android_media_MediaPlayer_getVideoHeight},
767    {"seekTo",              "(I)V",                             (void *)android_media_MediaPlayer_seekTo},
768    {"_pause",              "()V",                              (void *)android_media_MediaPlayer_pause},
769    {"isPlaying",           "()Z",                              (void *)android_media_MediaPlayer_isPlaying},
770    {"getCurrentPosition",  "()I",                              (void *)android_media_MediaPlayer_getCurrentPosition},
771    {"getDuration",         "()I",                              (void *)android_media_MediaPlayer_getDuration},
772    {"_release",            "()V",                              (void *)android_media_MediaPlayer_release},
773    {"_reset",              "()V",                              (void *)android_media_MediaPlayer_reset},
774    {"setAudioStreamType",  "(I)V",                             (void *)android_media_MediaPlayer_setAudioStreamType},
775    {"setLooping",          "(Z)V",                             (void *)android_media_MediaPlayer_setLooping},
776    {"isLooping",           "()Z",                              (void *)android_media_MediaPlayer_isLooping},
777    {"setVolume",           "(FF)V",                            (void *)android_media_MediaPlayer_setVolume},
778    {"getFrameAt",          "(I)Landroid/graphics/Bitmap;",     (void *)android_media_MediaPlayer_getFrameAt},
779    {"native_invoke",       "(Landroid/os/Parcel;Landroid/os/Parcel;)I",(void *)android_media_MediaPlayer_invoke},
780    {"native_setMetadataFilter", "(Landroid/os/Parcel;)I",      (void *)android_media_MediaPlayer_setMetadataFilter},
781    {"native_getMetadata", "(ZZLandroid/os/Parcel;)Z",          (void *)android_media_MediaPlayer_getMetadata},
782    {"native_init",         "()V",                              (void *)android_media_MediaPlayer_native_init},
783    {"native_setup",        "(Ljava/lang/Object;)V",            (void *)android_media_MediaPlayer_native_setup},
784    {"native_finalize",     "()V",                              (void *)android_media_MediaPlayer_native_finalize},
785    {"getAudioSessionId",   "()I",                              (void *)android_media_MediaPlayer_get_audio_session_id},
786    {"setAudioSessionId",   "(I)V",                             (void *)android_media_MediaPlayer_set_audio_session_id},
787    {"setAuxEffectSendLevel", "(F)V",                           (void *)android_media_MediaPlayer_setAuxEffectSendLevel},
788    {"attachAuxEffect",     "(I)V",                             (void *)android_media_MediaPlayer_attachAuxEffect},
789    {"native_pullBatteryData", "(Landroid/os/Parcel;)I",        (void *)android_media_MediaPlayer_pullBatteryData},
790    {"setParameter",        "(ILandroid/os/Parcel;)Z",          (void *)android_media_MediaPlayer_setParameter},
791    {"getParameter",        "(ILandroid/os/Parcel;)V",          (void *)android_media_MediaPlayer_getParameter},
792};
793
794static const char* const kClassPathName = "android/media/MediaPlayer";
795
796// This function only registers the native methods
797static int register_android_media_MediaPlayer(JNIEnv *env)
798{
799    return AndroidRuntime::registerNativeMethods(env,
800                "android/media/MediaPlayer", gMethods, NELEM(gMethods));
801}
802
803extern int register_android_media_MediaMetadataRetriever(JNIEnv *env);
804extern int register_android_media_MediaRecorder(JNIEnv *env);
805extern int register_android_media_MediaScanner(JNIEnv *env);
806extern int register_android_media_ResampleInputStream(JNIEnv *env);
807extern int register_android_media_MediaProfiles(JNIEnv *env);
808extern int register_android_media_AmrInputStream(JNIEnv *env);
809extern int register_android_mtp_MtpDatabase(JNIEnv *env);
810extern int register_android_mtp_MtpDevice(JNIEnv *env);
811extern int register_android_mtp_MtpServer(JNIEnv *env);
812
813jint JNI_OnLoad(JavaVM* vm, void* reserved)
814{
815    JNIEnv* env = NULL;
816    jint result = -1;
817
818    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
819        LOGE("ERROR: GetEnv failed\n");
820        goto bail;
821    }
822    assert(env != NULL);
823
824    if (register_android_media_MediaPlayer(env) < 0) {
825        LOGE("ERROR: MediaPlayer native registration failed\n");
826        goto bail;
827    }
828
829    if (register_android_media_MediaRecorder(env) < 0) {
830        LOGE("ERROR: MediaRecorder native registration failed\n");
831        goto bail;
832    }
833
834    if (register_android_media_MediaScanner(env) < 0) {
835        LOGE("ERROR: MediaScanner native registration failed\n");
836        goto bail;
837    }
838
839    if (register_android_media_MediaMetadataRetriever(env) < 0) {
840        LOGE("ERROR: MediaMetadataRetriever native registration failed\n");
841        goto bail;
842    }
843
844    if (register_android_media_AmrInputStream(env) < 0) {
845        LOGE("ERROR: AmrInputStream native registration failed\n");
846        goto bail;
847    }
848
849    if (register_android_media_ResampleInputStream(env) < 0) {
850        LOGE("ERROR: ResampleInputStream native registration failed\n");
851        goto bail;
852    }
853
854    if (register_android_media_MediaProfiles(env) < 0) {
855        LOGE("ERROR: MediaProfiles native registration failed");
856        goto bail;
857    }
858
859    if (register_android_mtp_MtpDatabase(env) < 0) {
860        LOGE("ERROR: MtpDatabase native registration failed");
861        goto bail;
862    }
863
864    if (register_android_mtp_MtpDevice(env) < 0) {
865        LOGE("ERROR: MtpDevice native registration failed");
866        goto bail;
867    }
868
869    if (register_android_mtp_MtpServer(env) < 0) {
870        LOGE("ERROR: MtpServer native registration failed");
871        goto bail;
872    }
873
874    /* success -- return valid version number */
875    result = JNI_VERSION_1_4;
876
877bail:
878    return result;
879}
880
881// KTHXBYE
882