3 ** Copyright 2007, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 //#define LOG_NDEBUG 0
19 #define LOG_TAG "MediaPlayer-JNI"
20 #include "utils/Log.h"
22 #include <media/mediaplayer.h>
23 #include <media/IMediaHTTPService.h>
24 #include <media/MediaPlayerInterface.h>
30 #include <utils/threads.h>
33 #include "android_runtime/AndroidRuntime.h"
34 #include "android_runtime/android_view_Surface.h"
35 #include "android_runtime/Log.h"
36 #include "utils/Errors.h" // for status_t
37 #include "utils/KeyedVector.h"
38 #include "utils/String8.h"
39 #include "android_media_Utils.h"
41 #include "android_os_Parcel.h"
42 #include "android_util_Binder.h"
43 #include <binder/Parcel.h>
44 #include <gui/IGraphicBufferProducer.h>
45 #include <gui/Surface.h>
46 #include <binder/IPCThreadState.h>
47 #include <binder/IServiceManager.h>
49 #include "android_util_Binder.h"
50 // ----------------------------------------------------------------------------
52 using namespace android;
54 // ----------------------------------------------------------------------------
58 jfieldID surface_texture;
62 jmethodID proxyConfigGetHost;
63 jmethodID proxyConfigGetPort;
64 jmethodID proxyConfigGetExclusionList;
66 static fields_t fields;
70 // ----------------------------------------------------------------------------
71 // ref-counted object for callbacks
72 class JNIMediaPlayerListener: public MediaPlayerListener
75 JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz);
76 ~JNIMediaPlayerListener();
77 virtual void notify(int msg, int ext1, int ext2, const Parcel *obj = NULL);
79 JNIMediaPlayerListener();
80 jclass mClass; // Reference to MediaPlayer class
81 jobject mObject; // Weak ref to MediaPlayer Java object to call on
84 JNIMediaPlayerListener::JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz)
87 // Hold onto the MediaPlayer class for use in calling the static method
88 // that posts events to the application thread.
89 jclass clazz = env->GetObjectClass(thiz);
91 ALOGE("Can't find android/media/MediaPlayer");
92 jniThrowException(env, "java/lang/Exception", NULL);
95 mClass = (jclass)env->NewGlobalRef(clazz);
97 // We use a weak reference so the MediaPlayer object can be garbage collected.
98 // The reference is only used as a proxy for callbacks.
99 mObject = env->NewGlobalRef(weak_thiz);
102 JNIMediaPlayerListener::~JNIMediaPlayerListener()
104 // remove global references
105 JNIEnv *env = AndroidRuntime::getJNIEnv();
106 env->DeleteGlobalRef(mObject);
107 env->DeleteGlobalRef(mClass);
110 void JNIMediaPlayerListener::notify(int msg, int ext1, int ext2, const Parcel *obj)
112 JNIEnv *env = AndroidRuntime::getJNIEnv();
113 if (obj && obj->dataSize() > 0) {
114 jobject jParcel = createJavaParcelObject(env);
115 if (jParcel != NULL) {
116 Parcel* nativeParcel = parcelForJavaObject(env, jParcel);
117 nativeParcel->setData(obj->data(), obj->dataSize());
118 env->CallStaticVoidMethod(mClass, fields.post_event, mObject,
119 msg, ext1, ext2, jParcel);
120 env->DeleteLocalRef(jParcel);
123 env->CallStaticVoidMethod(mClass, fields.post_event, mObject,
124 msg, ext1, ext2, NULL);
126 if (env->ExceptionCheck()) {
127 ALOGW("An exception occurred while notifying an event.");
129 env->ExceptionClear();
133 // ----------------------------------------------------------------------------
135 static sp<MediaPlayer> getMediaPlayer(JNIEnv* env, jobject thiz)
137 Mutex::Autolock l(sLock);
138 MediaPlayer* const p = (MediaPlayer*)env->GetLongField(thiz, fields.context);
139 return sp<MediaPlayer>(p);
142 static sp<MediaPlayer> setMediaPlayer(JNIEnv* env, jobject thiz, const sp<MediaPlayer>& player)
144 Mutex::Autolock l(sLock);
145 sp<MediaPlayer> old = (MediaPlayer*)env->GetLongField(thiz, fields.context);
147 player->incStrong((void*)setMediaPlayer);
150 old->decStrong((void*)setMediaPlayer);
152 env->SetLongField(thiz, fields.context, (jlong)player.get());
156 // If exception is NULL and opStatus is not OK, this method sends an error
157 // event to the client application; otherwise, if exception is not NULL and
158 // opStatus is not OK, this method throws the given exception to the client
160 static void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message)
162 if (exception == NULL) { // Don't throw exception. Instead, send an event.
163 if (opStatus != (status_t) OK) {
164 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
165 if (mp != 0) mp->notify(MEDIA_ERROR, opStatus, 0);
167 } else { // Throw exception!
168 if ( opStatus == (status_t) INVALID_OPERATION ) {
169 jniThrowException(env, "java/lang/IllegalStateException", NULL);
170 } else if ( opStatus == (status_t) PERMISSION_DENIED ) {
171 jniThrowException(env, "java/lang/SecurityException", NULL);
172 } else if ( opStatus != (status_t) OK ) {
173 if (strlen(message) > 230) {
174 // if the message is too long, don't bother displaying the status code
175 jniThrowException( env, exception, message);
178 // append the status code to the message
179 sprintf(msg, "%s: status=0x%X", message, opStatus);
180 jniThrowException( env, exception, msg);
187 android_media_MediaPlayer_setDataSourceAndHeaders(
188 JNIEnv *env, jobject thiz, jobject httpServiceBinderObj, jstring path,
189 jobjectArray keys, jobjectArray values) {
191 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
193 jniThrowException(env, "java/lang/IllegalStateException", NULL);
198 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
202 const char *tmp = env->GetStringUTFChars(path, NULL);
203 if (tmp == NULL) { // Out of memory
206 ALOGV("setDataSource: path %s", tmp);
208 String8 pathStr(tmp);
209 env->ReleaseStringUTFChars(path, tmp);
212 // We build a KeyedVector out of the key and val arrays
213 KeyedVector<String8, String8> headersVector;
214 if (!ConvertKeyValueArraysToKeyedVector(
215 env, keys, values, &headersVector)) {
219 sp<IMediaHTTPService> httpService;
220 if (httpServiceBinderObj != NULL) {
221 sp<IBinder> binder = ibinderForJavaObject(env, httpServiceBinderObj);
222 httpService = interface_cast<IMediaHTTPService>(binder);
229 headersVector.size() > 0? &headersVector : NULL);
231 process_media_player_call(
232 env, thiz, opStatus, "java/io/IOException",
233 "setDataSource failed." );
237 android_media_MediaPlayer_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length)
239 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
241 jniThrowException(env, "java/lang/IllegalStateException", NULL);
245 if (fileDescriptor == NULL) {
246 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
249 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
250 ALOGV("setDataSourceFD: fd %d", fd);
251 process_media_player_call( env, thiz, mp->setDataSource(fd, offset, length), "java/io/IOException", "setDataSourceFD failed." );
254 static sp<IGraphicBufferProducer>
255 getVideoSurfaceTexture(JNIEnv* env, jobject thiz) {
256 IGraphicBufferProducer * const p = (IGraphicBufferProducer*)env->GetLongField(thiz, fields.surface_texture);
257 return sp<IGraphicBufferProducer>(p);
261 decVideoSurfaceRef(JNIEnv *env, jobject thiz)
263 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
268 sp<IGraphicBufferProducer> old_st = getVideoSurfaceTexture(env, thiz);
269 if (old_st != NULL) {
270 old_st->decStrong((void*)decVideoSurfaceRef);
275 setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface, jboolean mediaPlayerMustBeAlive)
277 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
279 if (mediaPlayerMustBeAlive) {
280 jniThrowException(env, "java/lang/IllegalStateException", NULL);
285 decVideoSurfaceRef(env, thiz);
287 sp<IGraphicBufferProducer> new_st;
289 sp<Surface> surface(android_view_Surface_getSurface(env, jsurface));
290 if (surface != NULL) {
291 new_st = surface->getIGraphicBufferProducer();
292 if (new_st == NULL) {
293 jniThrowException(env, "java/lang/IllegalArgumentException",
294 "The surface does not have a binding SurfaceTexture!");
297 new_st->incStrong((void*)decVideoSurfaceRef);
299 jniThrowException(env, "java/lang/IllegalArgumentException",
300 "The surface has been released");
305 env->SetLongField(thiz, fields.surface_texture, (jlong)new_st.get());
307 // This will fail if the media player has not been initialized yet. This
308 // can be the case if setDisplay() on MediaPlayer.java has been called
309 // before setDataSource(). The redundant call to setVideoSurfaceTexture()
310 // in prepare/prepareAsync covers for this case.
311 mp->setVideoSurfaceTexture(new_st);
315 android_media_MediaPlayer_setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface)
317 setVideoSurface(env, thiz, jsurface, true /* mediaPlayerMustBeAlive */);
321 android_media_MediaPlayer_prepare(JNIEnv *env, jobject thiz)
323 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
325 jniThrowException(env, "java/lang/IllegalStateException", NULL);
329 // Handle the case where the display surface was set before the mp was
330 // initialized. We try again to make it stick.
331 sp<IGraphicBufferProducer> st = getVideoSurfaceTexture(env, thiz);
332 mp->setVideoSurfaceTexture(st);
334 process_media_player_call( env, thiz, mp->prepare(), "java/io/IOException", "Prepare failed." );
338 android_media_MediaPlayer_prepareAsync(JNIEnv *env, jobject thiz)
340 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
342 jniThrowException(env, "java/lang/IllegalStateException", NULL);
346 // Handle the case where the display surface was set before the mp was
347 // initialized. We try again to make it stick.
348 sp<IGraphicBufferProducer> st = getVideoSurfaceTexture(env, thiz);
349 mp->setVideoSurfaceTexture(st);
351 process_media_player_call( env, thiz, mp->prepareAsync(), "java/io/IOException", "Prepare Async failed." );
355 android_media_MediaPlayer_start(JNIEnv *env, jobject thiz)
358 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
360 jniThrowException(env, "java/lang/IllegalStateException", NULL);
363 process_media_player_call( env, thiz, mp->start(), NULL, NULL );
367 android_media_MediaPlayer_stop(JNIEnv *env, jobject thiz)
370 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
372 jniThrowException(env, "java/lang/IllegalStateException", NULL);
375 process_media_player_call( env, thiz, mp->stop(), NULL, NULL );
379 android_media_MediaPlayer_pause(JNIEnv *env, jobject thiz)
382 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
384 jniThrowException(env, "java/lang/IllegalStateException", NULL);
387 process_media_player_call( env, thiz, mp->pause(), NULL, NULL );
391 android_media_MediaPlayer_isPlaying(JNIEnv *env, jobject thiz)
393 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
395 jniThrowException(env, "java/lang/IllegalStateException", NULL);
398 const jboolean is_playing = mp->isPlaying();
400 ALOGV("isPlaying: %d", is_playing);
405 android_media_MediaPlayer_seekTo(JNIEnv *env, jobject thiz, jint msec)
407 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
409 jniThrowException(env, "java/lang/IllegalStateException", NULL);
412 ALOGV("seekTo: %d(msec)", msec);
413 process_media_player_call( env, thiz, mp->seekTo(msec), NULL, NULL );
417 android_media_MediaPlayer_getVideoWidth(JNIEnv *env, jobject thiz)
419 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
421 jniThrowException(env, "java/lang/IllegalStateException", NULL);
425 if (0 != mp->getVideoWidth(&w)) {
426 ALOGE("getVideoWidth failed");
429 ALOGV("getVideoWidth: %d", w);
434 android_media_MediaPlayer_getVideoHeight(JNIEnv *env, jobject thiz)
436 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
438 jniThrowException(env, "java/lang/IllegalStateException", NULL);
442 if (0 != mp->getVideoHeight(&h)) {
443 ALOGE("getVideoHeight failed");
446 ALOGV("getVideoHeight: %d", h);
452 android_media_MediaPlayer_getCurrentPosition(JNIEnv *env, jobject thiz)
454 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
456 jniThrowException(env, "java/lang/IllegalStateException", NULL);
460 process_media_player_call( env, thiz, mp->getCurrentPosition(&msec), NULL, NULL );
461 ALOGV("getCurrentPosition: %d (msec)", msec);
466 android_media_MediaPlayer_getDuration(JNIEnv *env, jobject thiz)
468 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
470 jniThrowException(env, "java/lang/IllegalStateException", NULL);
474 process_media_player_call( env, thiz, mp->getDuration(&msec), NULL, NULL );
475 ALOGV("getDuration: %d (msec)", msec);
480 android_media_MediaPlayer_reset(JNIEnv *env, jobject thiz)
483 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
485 jniThrowException(env, "java/lang/IllegalStateException", NULL);
488 process_media_player_call( env, thiz, mp->reset(), NULL, NULL );
492 android_media_MediaPlayer_setAudioStreamType(JNIEnv *env, jobject thiz, jint streamtype)
494 ALOGV("setAudioStreamType: %d", streamtype);
495 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
497 jniThrowException(env, "java/lang/IllegalStateException", NULL);
500 process_media_player_call( env, thiz, mp->setAudioStreamType((audio_stream_type_t) streamtype) , NULL, NULL );
504 android_media_MediaPlayer_getAudioStreamType(JNIEnv *env, jobject thiz)
506 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
508 jniThrowException(env, "java/lang/IllegalStateException", NULL);
511 audio_stream_type_t streamtype;
512 process_media_player_call( env, thiz, mp->getAudioStreamType(&streamtype), NULL, NULL );
513 ALOGV("getAudioStreamType: %d (streamtype)", streamtype);
514 return (jint) streamtype;
518 android_media_MediaPlayer_setParameter(JNIEnv *env, jobject thiz, jint key, jobject java_request)
520 ALOGV("setParameter: key %d", key);
521 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
523 jniThrowException(env, "java/lang/IllegalStateException", NULL);
527 Parcel *request = parcelForJavaObject(env, java_request);
528 status_t err = mp->setParameter(key, *request);
537 android_media_MediaPlayer_setLooping(JNIEnv *env, jobject thiz, jboolean looping)
539 ALOGV("setLooping: %d", looping);
540 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
542 jniThrowException(env, "java/lang/IllegalStateException", NULL);
545 process_media_player_call( env, thiz, mp->setLooping(looping), NULL, NULL );
549 android_media_MediaPlayer_isLooping(JNIEnv *env, jobject thiz)
552 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
554 jniThrowException(env, "java/lang/IllegalStateException", NULL);
557 return mp->isLooping() ? JNI_TRUE : JNI_FALSE;
561 android_media_MediaPlayer_setVolume(JNIEnv *env, jobject thiz, jfloat leftVolume, jfloat rightVolume)
563 ALOGV("setVolume: left %f right %f", (float) leftVolume, (float) rightVolume);
564 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
566 jniThrowException(env, "java/lang/IllegalStateException", NULL);
569 process_media_player_call( env, thiz, mp->setVolume((float) leftVolume, (float) rightVolume), NULL, NULL );
572 // Sends the request and reply parcels to the media player via the
575 android_media_MediaPlayer_invoke(JNIEnv *env, jobject thiz,
576 jobject java_request, jobject java_reply)
578 sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
579 if (media_player == NULL ) {
580 jniThrowException(env, "java/lang/IllegalStateException", NULL);
581 return UNKNOWN_ERROR;
584 Parcel *request = parcelForJavaObject(env, java_request);
585 Parcel *reply = parcelForJavaObject(env, java_reply);
587 // Don't use process_media_player_call which use the async loop to
588 // report errors, instead returns the status.
589 return (jint) media_player->invoke(*request, reply);
592 // Sends the new filter to the client.
594 android_media_MediaPlayer_setMetadataFilter(JNIEnv *env, jobject thiz, jobject request)
596 sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
597 if (media_player == NULL ) {
598 jniThrowException(env, "java/lang/IllegalStateException", NULL);
599 return UNKNOWN_ERROR;
602 Parcel *filter = parcelForJavaObject(env, request);
604 if (filter == NULL ) {
605 jniThrowException(env, "java/lang/RuntimeException", "Filter is null");
606 return UNKNOWN_ERROR;
609 return (jint) media_player->setMetadataFilter(*filter);
613 android_media_MediaPlayer_getMetadata(JNIEnv *env, jobject thiz, jboolean update_only,
614 jboolean apply_filter, jobject reply)
616 sp<MediaPlayer> media_player = getMediaPlayer(env, thiz);
617 if (media_player == NULL ) {
618 jniThrowException(env, "java/lang/IllegalStateException", NULL);
622 Parcel *metadata = parcelForJavaObject(env, reply);
624 if (metadata == NULL ) {
625 jniThrowException(env, "java/lang/RuntimeException", "Reply parcel is null");
629 metadata->freeData();
630 // On return metadata is positioned at the beginning of the
631 // metadata. Note however that the parcel actually starts with the
632 // return code so you should not rewind the parcel using
633 // setDataPosition(0).
634 if (media_player->getMetadata(update_only, apply_filter, metadata) == OK) {
641 // This function gets some field IDs, which in turn causes class initialization.
642 // It is called from a static block in MediaPlayer, which won't run until the
643 // first time an instance of this class is used.
645 android_media_MediaPlayer_native_init(JNIEnv *env)
649 clazz = env->FindClass("android/media/MediaPlayer");
654 fields.context = env->GetFieldID(clazz, "mNativeContext", "J");
655 if (fields.context == NULL) {
659 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative",
660 "(Ljava/lang/Object;IIILjava/lang/Object;)V");
661 if (fields.post_event == NULL) {
665 fields.surface_texture = env->GetFieldID(clazz, "mNativeSurfaceTexture", "J");
666 if (fields.surface_texture == NULL) {
670 clazz = env->FindClass("android/net/ProxyInfo");
675 fields.proxyConfigGetHost =
676 env->GetMethodID(clazz, "getHost", "()Ljava/lang/String;");
678 fields.proxyConfigGetPort =
679 env->GetMethodID(clazz, "getPort", "()I");
681 fields.proxyConfigGetExclusionList =
682 env->GetMethodID(clazz, "getExclusionListAsString", "()Ljava/lang/String;");
686 android_media_MediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this)
688 ALOGV("native_setup");
689 sp<MediaPlayer> mp = new MediaPlayer();
691 jniThrowException(env, "java/lang/RuntimeException", "Out of memory");
695 // create new listener and give it to MediaPlayer
696 sp<JNIMediaPlayerListener> listener = new JNIMediaPlayerListener(env, thiz, weak_this);
697 mp->setListener(listener);
699 // Stow our new C++ MediaPlayer in an opaque field in the Java object.
700 setMediaPlayer(env, thiz, mp);
704 android_media_MediaPlayer_release(JNIEnv *env, jobject thiz)
707 decVideoSurfaceRef(env, thiz);
708 sp<MediaPlayer> mp = setMediaPlayer(env, thiz, 0);
710 // this prevents native callbacks after the object is released
717 android_media_MediaPlayer_native_finalize(JNIEnv *env, jobject thiz)
719 ALOGV("native_finalize");
720 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
722 ALOGW("MediaPlayer finalized without being released");
724 android_media_MediaPlayer_release(env, thiz);
727 static void android_media_MediaPlayer_set_audio_session_id(JNIEnv *env, jobject thiz, jint sessionId) {
728 ALOGV("set_session_id(): %d", sessionId);
729 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
731 jniThrowException(env, "java/lang/IllegalStateException", NULL);
734 process_media_player_call( env, thiz, mp->setAudioSessionId(sessionId), NULL, NULL );
737 static jint android_media_MediaPlayer_get_audio_session_id(JNIEnv *env, jobject thiz) {
738 ALOGV("get_session_id()");
739 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
741 jniThrowException(env, "java/lang/IllegalStateException", NULL);
745 return (jint) mp->getAudioSessionId();
749 android_media_MediaPlayer_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level)
751 ALOGV("setAuxEffectSendLevel: level %f", level);
752 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
754 jniThrowException(env, "java/lang/IllegalStateException", NULL);
757 process_media_player_call( env, thiz, mp->setAuxEffectSendLevel(level), NULL, NULL );
760 static void android_media_MediaPlayer_attachAuxEffect(JNIEnv *env, jobject thiz, jint effectId) {
761 ALOGV("attachAuxEffect(): %d", effectId);
762 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
764 jniThrowException(env, "java/lang/IllegalStateException", NULL);
767 process_media_player_call( env, thiz, mp->attachAuxEffect(effectId), NULL, NULL );
771 android_media_MediaPlayer_pullBatteryData(
772 JNIEnv *env, jobject /* thiz */, jobject java_reply)
774 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.player"));
775 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder);
776 if (service.get() == NULL) {
777 jniThrowException(env, "java/lang/RuntimeException", "cannot get MediaPlayerService");
778 return UNKNOWN_ERROR;
781 Parcel *reply = parcelForJavaObject(env, java_reply);
783 return (jint) service->pullBatteryData(reply);
787 android_media_MediaPlayer_setRetransmitEndpoint(JNIEnv *env, jobject thiz,
788 jstring addrString, jint port) {
789 sp<MediaPlayer> mp = getMediaPlayer(env, thiz);
791 jniThrowException(env, "java/lang/IllegalStateException", NULL);
792 return INVALID_OPERATION;
795 const char *cAddrString = NULL;
797 if (NULL != addrString) {
798 cAddrString = env->GetStringUTFChars(addrString, NULL);
799 if (cAddrString == NULL) { // Out of memory
803 ALOGV("setRetransmitEndpoint: %s:%d",
804 cAddrString ? cAddrString : "(null)", port);
807 if (cAddrString && (port > 0xFFFF)) {
810 ret = mp->setRetransmitEndpoint(cAddrString,
811 static_cast<uint16_t>(port));
814 if (NULL != addrString) {
815 env->ReleaseStringUTFChars(addrString, cAddrString);
818 if (ret == INVALID_OPERATION ) {
819 jniThrowException(env, "java/lang/IllegalStateException", NULL);
826 android_media_MediaPlayer_setNextMediaPlayer(JNIEnv *env, jobject thiz, jobject java_player)
828 ALOGV("setNextMediaPlayer");
829 sp<MediaPlayer> thisplayer = getMediaPlayer(env, thiz);
830 if (thisplayer == NULL) {
831 jniThrowException(env, "java/lang/IllegalStateException", "This player not initialized");
834 sp<MediaPlayer> nextplayer = (java_player == NULL) ? NULL : getMediaPlayer(env, java_player);
835 if (nextplayer == NULL && java_player != NULL) {
836 jniThrowException(env, "java/lang/IllegalStateException", "That player not initialized");
840 if (nextplayer == thisplayer) {
841 jniThrowException(env, "java/lang/IllegalArgumentException", "Next player can't be self");
844 // tie the two players together
845 process_media_player_call(
846 env, thiz, thisplayer->setNextMediaPlayer(nextplayer),
847 "java/lang/IllegalArgumentException",
848 "setNextMediaPlayer failed." );
852 // ----------------------------------------------------------------------------
854 static JNINativeMethod gMethods[] = {
856 "nativeSetDataSource",
857 "(Landroid/os/IBinder;Ljava/lang/String;[Ljava/lang/String;"
858 "[Ljava/lang/String;)V",
859 (void *)android_media_MediaPlayer_setDataSourceAndHeaders
862 {"_setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaPlayer_setDataSourceFD},
863 {"_setVideoSurface", "(Landroid/view/Surface;)V", (void *)android_media_MediaPlayer_setVideoSurface},
864 {"_prepare", "()V", (void *)android_media_MediaPlayer_prepare},
865 {"prepareAsync", "()V", (void *)android_media_MediaPlayer_prepareAsync},
866 {"_start", "()V", (void *)android_media_MediaPlayer_start},
867 {"_stop", "()V", (void *)android_media_MediaPlayer_stop},
868 {"getVideoWidth", "()I", (void *)android_media_MediaPlayer_getVideoWidth},
869 {"getVideoHeight", "()I", (void *)android_media_MediaPlayer_getVideoHeight},
870 {"seekTo", "(I)V", (void *)android_media_MediaPlayer_seekTo},
871 {"_pause", "()V", (void *)android_media_MediaPlayer_pause},
872 {"isPlaying", "()Z", (void *)android_media_MediaPlayer_isPlaying},
873 {"getCurrentPosition", "()I", (void *)android_media_MediaPlayer_getCurrentPosition},
874 {"getDuration", "()I", (void *)android_media_MediaPlayer_getDuration},
875 {"_release", "()V", (void *)android_media_MediaPlayer_release},
876 {"_reset", "()V", (void *)android_media_MediaPlayer_reset},
877 {"_setAudioStreamType", "(I)V", (void *)android_media_MediaPlayer_setAudioStreamType},
878 {"_getAudioStreamType", "()I", (void *)android_media_MediaPlayer_getAudioStreamType},
879 {"setParameter", "(ILandroid/os/Parcel;)Z", (void *)android_media_MediaPlayer_setParameter},
880 {"setLooping", "(Z)V", (void *)android_media_MediaPlayer_setLooping},
881 {"isLooping", "()Z", (void *)android_media_MediaPlayer_isLooping},
882 {"_setVolume", "(FF)V", (void *)android_media_MediaPlayer_setVolume},
883 {"native_invoke", "(Landroid/os/Parcel;Landroid/os/Parcel;)I",(void *)android_media_MediaPlayer_invoke},
884 {"native_setMetadataFilter", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_setMetadataFilter},
885 {"native_getMetadata", "(ZZLandroid/os/Parcel;)Z", (void *)android_media_MediaPlayer_getMetadata},
886 {"native_init", "()V", (void *)android_media_MediaPlayer_native_init},
887 {"native_setup", "(Ljava/lang/Object;)V", (void *)android_media_MediaPlayer_native_setup},
888 {"native_finalize", "()V", (void *)android_media_MediaPlayer_native_finalize},
889 {"getAudioSessionId", "()I", (void *)android_media_MediaPlayer_get_audio_session_id},
890 {"setAudioSessionId", "(I)V", (void *)android_media_MediaPlayer_set_audio_session_id},
891 {"_setAuxEffectSendLevel", "(F)V", (void *)android_media_MediaPlayer_setAuxEffectSendLevel},
892 {"attachAuxEffect", "(I)V", (void *)android_media_MediaPlayer_attachAuxEffect},
893 {"native_pullBatteryData", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_pullBatteryData},
894 {"native_setRetransmitEndpoint", "(Ljava/lang/String;I)I", (void *)android_media_MediaPlayer_setRetransmitEndpoint},
895 {"setNextMediaPlayer", "(Landroid/media/MediaPlayer;)V", (void *)android_media_MediaPlayer_setNextMediaPlayer},
898 static const char* const kClassPathName = "android/media/MediaPlayer";
900 // This function only registers the native methods
901 static int register_android_media_MediaPlayer(JNIEnv *env)
903 return AndroidRuntime::registerNativeMethods(env,
904 "android/media/MediaPlayer", gMethods, NELEM(gMethods));
907 extern int register_android_media_ImageReader(JNIEnv *env);
908 extern int register_android_media_Crypto(JNIEnv *env);
909 extern int register_android_media_Drm(JNIEnv *env);
910 extern int register_android_media_MediaCodec(JNIEnv *env);
911 extern int register_android_media_MediaExtractor(JNIEnv *env);
912 extern int register_android_media_MediaCodecList(JNIEnv *env);
913 extern int register_android_media_MediaHTTPConnection(JNIEnv *env);
914 extern int register_android_media_MediaMetadataRetriever(JNIEnv *env);
915 extern int register_android_media_MediaMuxer(JNIEnv *env);
916 extern int register_android_media_MediaRecorder(JNIEnv *env);
917 extern int register_android_media_MediaScanner(JNIEnv *env);
918 extern int register_android_media_ResampleInputStream(JNIEnv *env);
919 extern int register_android_media_MediaProfiles(JNIEnv *env);
920 extern int register_android_media_AmrInputStream(JNIEnv *env);
921 extern int register_android_mtp_MtpDatabase(JNIEnv *env);
922 extern int register_android_mtp_MtpDevice(JNIEnv *env);
923 extern int register_android_mtp_MtpServer(JNIEnv *env);
925 jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)
930 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
931 ALOGE("ERROR: GetEnv failed\n");
936 if (register_android_media_ImageReader(env) < 0) {
937 ALOGE("ERROR: ImageReader native registration failed");
941 if (register_android_media_MediaPlayer(env) < 0) {
942 ALOGE("ERROR: MediaPlayer native registration failed\n");
946 if (register_android_media_MediaRecorder(env) < 0) {
947 ALOGE("ERROR: MediaRecorder native registration failed\n");
951 if (register_android_media_MediaScanner(env) < 0) {
952 ALOGE("ERROR: MediaScanner native registration failed\n");
956 if (register_android_media_MediaMetadataRetriever(env) < 0) {
957 ALOGE("ERROR: MediaMetadataRetriever native registration failed\n");
961 if (register_android_media_AmrInputStream(env) < 0) {
962 ALOGE("ERROR: AmrInputStream native registration failed\n");
966 if (register_android_media_ResampleInputStream(env) < 0) {
967 ALOGE("ERROR: ResampleInputStream native registration failed\n");
971 if (register_android_media_MediaProfiles(env) < 0) {
972 ALOGE("ERROR: MediaProfiles native registration failed");
976 if (register_android_mtp_MtpDatabase(env) < 0) {
977 ALOGE("ERROR: MtpDatabase native registration failed");
981 if (register_android_mtp_MtpDevice(env) < 0) {
982 ALOGE("ERROR: MtpDevice native registration failed");
986 if (register_android_mtp_MtpServer(env) < 0) {
987 ALOGE("ERROR: MtpServer native registration failed");
991 if (register_android_media_MediaCodec(env) < 0) {
992 ALOGE("ERROR: MediaCodec native registration failed");
996 if (register_android_media_MediaExtractor(env) < 0) {
997 ALOGE("ERROR: MediaCodec native registration failed");
1001 if (register_android_media_MediaMuxer(env) < 0) {
1002 ALOGE("ERROR: MediaMuxer native registration failed");
1006 if (register_android_media_MediaCodecList(env) < 0) {
1007 ALOGE("ERROR: MediaCodec native registration failed");
1011 if (register_android_media_Crypto(env) < 0) {
1012 ALOGE("ERROR: MediaCodec native registration failed");
1016 if (register_android_media_Drm(env) < 0) {
1017 ALOGE("ERROR: MediaDrm native registration failed");
1021 if (register_android_media_MediaHTTPConnection(env) < 0) {
1022 ALOGE("ERROR: MediaHTTPConnection native registration failed");
1026 /* success -- return valid version number */
1027 result = JNI_VERSION_1_4;