android_media_MediaPlayer.cpp revision 1af30c7ac480e5d335f267a3ac3b2e6c748ce240
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/IMediaHTTPService.h> 24#include <media/MediaPlayerInterface.h> 25#include <stdio.h> 26#include <assert.h> 27#include <limits.h> 28#include <unistd.h> 29#include <fcntl.h> 30#include <utils/threads.h> 31#include "jni.h" 32#include "JNIHelp.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" 40 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> 48 49#include "android_util_Binder.h" 50// ---------------------------------------------------------------------------- 51 52using namespace android; 53 54// ---------------------------------------------------------------------------- 55 56struct fields_t { 57 jfieldID context; 58 jfieldID surface_texture; 59 60 jmethodID post_event; 61 62 jmethodID proxyConfigGetHost; 63 jmethodID proxyConfigGetPort; 64 jmethodID proxyConfigGetExclusionList; 65}; 66static fields_t fields; 67 68static Mutex sLock; 69 70// ---------------------------------------------------------------------------- 71// ref-counted object for callbacks 72class JNIMediaPlayerListener: public MediaPlayerListener 73{ 74public: 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); 78private: 79 JNIMediaPlayerListener(); 80 jclass mClass; // Reference to MediaPlayer class 81 jobject mObject; // Weak ref to MediaPlayer Java object to call on 82}; 83 84JNIMediaPlayerListener::JNIMediaPlayerListener(JNIEnv* env, jobject thiz, jobject weak_thiz) 85{ 86 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); 90 if (clazz == NULL) { 91 ALOGE("Can't find android/media/MediaPlayer"); 92 jniThrowException(env, "java/lang/Exception", NULL); 93 return; 94 } 95 mClass = (jclass)env->NewGlobalRef(clazz); 96 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); 100} 101 102JNIMediaPlayerListener::~JNIMediaPlayerListener() 103{ 104 // remove global references 105 JNIEnv *env = AndroidRuntime::getJNIEnv(); 106 env->DeleteGlobalRef(mObject); 107 env->DeleteGlobalRef(mClass); 108} 109 110void JNIMediaPlayerListener::notify(int msg, int ext1, int ext2, const Parcel *obj) 111{ 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); 121 } 122 } else { 123 env->CallStaticVoidMethod(mClass, fields.post_event, mObject, 124 msg, ext1, ext2, NULL); 125 } 126 if (env->ExceptionCheck()) { 127 ALOGW("An exception occurred while notifying an event."); 128 LOGW_EX(env); 129 env->ExceptionClear(); 130 } 131} 132 133// ---------------------------------------------------------------------------- 134 135static sp<MediaPlayer> getMediaPlayer(JNIEnv* env, jobject thiz) 136{ 137 Mutex::Autolock l(sLock); 138 MediaPlayer* const p = (MediaPlayer*)env->GetLongField(thiz, fields.context); 139 return sp<MediaPlayer>(p); 140} 141 142static sp<MediaPlayer> setMediaPlayer(JNIEnv* env, jobject thiz, const sp<MediaPlayer>& player) 143{ 144 Mutex::Autolock l(sLock); 145 sp<MediaPlayer> old = (MediaPlayer*)env->GetLongField(thiz, fields.context); 146 if (player.get()) { 147 player->incStrong((void*)setMediaPlayer); 148 } 149 if (old != 0) { 150 old->decStrong((void*)setMediaPlayer); 151 } 152 env->SetLongField(thiz, fields.context, (jlong)player.get()); 153 return old; 154} 155 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 159// application. 160static void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message) 161{ 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); 166 } 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); 176 } else { 177 char msg[256]; 178 // append the status code to the message 179 sprintf(msg, "%s: status=0x%X", message, opStatus); 180 jniThrowException( env, exception, msg); 181 } 182 } 183 } 184} 185 186static void 187android_media_MediaPlayer_setDataSourceAndHeaders( 188 JNIEnv *env, jobject thiz, jobject httpServiceBinderObj, jstring path, 189 jobjectArray keys, jobjectArray values) { 190 191 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 192 if (mp == NULL ) { 193 jniThrowException(env, "java/lang/IllegalStateException", NULL); 194 return; 195 } 196 197 if (path == NULL) { 198 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 199 return; 200 } 201 202 const char *tmp = env->GetStringUTFChars(path, NULL); 203 if (tmp == NULL) { // Out of memory 204 return; 205 } 206 ALOGV("setDataSource: path %s", tmp); 207 208 String8 pathStr(tmp); 209 env->ReleaseStringUTFChars(path, tmp); 210 tmp = NULL; 211 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)) { 216 return; 217 } 218 219 sp<IMediaHTTPService> httpService; 220 if (httpServiceBinderObj != NULL) { 221 sp<IBinder> binder = ibinderForJavaObject(env, httpServiceBinderObj); 222 httpService = interface_cast<IMediaHTTPService>(binder); 223 } 224 225 status_t opStatus = 226 mp->setDataSource( 227 httpService, 228 pathStr, 229 headersVector.size() > 0? &headersVector : NULL); 230 231 process_media_player_call( 232 env, thiz, opStatus, "java/io/IOException", 233 "setDataSource failed." ); 234} 235 236static void 237android_media_MediaPlayer_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length) 238{ 239 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 240 if (mp == NULL ) { 241 jniThrowException(env, "java/lang/IllegalStateException", NULL); 242 return; 243 } 244 245 if (fileDescriptor == NULL) { 246 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 247 return; 248 } 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." ); 252} 253 254static sp<IGraphicBufferProducer> 255getVideoSurfaceTexture(JNIEnv* env, jobject thiz) { 256 IGraphicBufferProducer * const p = (IGraphicBufferProducer*)env->GetLongField(thiz, fields.surface_texture); 257 return sp<IGraphicBufferProducer>(p); 258} 259 260static void 261decVideoSurfaceRef(JNIEnv *env, jobject thiz) 262{ 263 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 264 if (mp == NULL) { 265 return; 266 } 267 268 sp<IGraphicBufferProducer> old_st = getVideoSurfaceTexture(env, thiz); 269 if (old_st != NULL) { 270 old_st->decStrong((void*)decVideoSurfaceRef); 271 } 272} 273 274static void 275setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface, jboolean mediaPlayerMustBeAlive) 276{ 277 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 278 if (mp == NULL) { 279 if (mediaPlayerMustBeAlive) { 280 jniThrowException(env, "java/lang/IllegalStateException", NULL); 281 } 282 return; 283 } 284 285 decVideoSurfaceRef(env, thiz); 286 287 sp<IGraphicBufferProducer> new_st; 288 if (jsurface) { 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!"); 295 return; 296 } 297 new_st->incStrong((void*)decVideoSurfaceRef); 298 } else { 299 jniThrowException(env, "java/lang/IllegalArgumentException", 300 "The surface has been released"); 301 return; 302 } 303 } 304 305 env->SetLongField(thiz, fields.surface_texture, (jlong)new_st.get()); 306 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); 312} 313 314static void 315android_media_MediaPlayer_setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface) 316{ 317 setVideoSurface(env, thiz, jsurface, true /* mediaPlayerMustBeAlive */); 318} 319 320static void 321android_media_MediaPlayer_prepare(JNIEnv *env, jobject thiz) 322{ 323 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 324 if (mp == NULL ) { 325 jniThrowException(env, "java/lang/IllegalStateException", NULL); 326 return; 327 } 328 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); 333 334 process_media_player_call( env, thiz, mp->prepare(), "java/io/IOException", "Prepare failed." ); 335} 336 337static void 338android_media_MediaPlayer_prepareAsync(JNIEnv *env, jobject thiz) 339{ 340 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 341 if (mp == NULL ) { 342 jniThrowException(env, "java/lang/IllegalStateException", NULL); 343 return; 344 } 345 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); 350 351 process_media_player_call( env, thiz, mp->prepareAsync(), "java/io/IOException", "Prepare Async failed." ); 352} 353 354static void 355android_media_MediaPlayer_start(JNIEnv *env, jobject thiz) 356{ 357 ALOGV("start"); 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->start(), NULL, NULL ); 364} 365 366static void 367android_media_MediaPlayer_stop(JNIEnv *env, jobject thiz) 368{ 369 ALOGV("stop"); 370 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 371 if (mp == NULL ) { 372 jniThrowException(env, "java/lang/IllegalStateException", NULL); 373 return; 374 } 375 process_media_player_call( env, thiz, mp->stop(), NULL, NULL ); 376} 377 378static void 379android_media_MediaPlayer_pause(JNIEnv *env, jobject thiz) 380{ 381 ALOGV("pause"); 382 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 383 if (mp == NULL ) { 384 jniThrowException(env, "java/lang/IllegalStateException", NULL); 385 return; 386 } 387 process_media_player_call( env, thiz, mp->pause(), NULL, NULL ); 388} 389 390static jboolean 391android_media_MediaPlayer_isPlaying(JNIEnv *env, jobject thiz) 392{ 393 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 394 if (mp == NULL ) { 395 jniThrowException(env, "java/lang/IllegalStateException", NULL); 396 return JNI_FALSE; 397 } 398 const jboolean is_playing = mp->isPlaying(); 399 400 ALOGV("isPlaying: %d", is_playing); 401 return is_playing; 402} 403 404static void 405android_media_MediaPlayer_seekTo(JNIEnv *env, jobject thiz, jint msec) 406{ 407 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 408 if (mp == NULL ) { 409 jniThrowException(env, "java/lang/IllegalStateException", NULL); 410 return; 411 } 412 ALOGV("seekTo: %d(msec)", msec); 413 process_media_player_call( env, thiz, mp->seekTo(msec), NULL, NULL ); 414} 415 416static jint 417android_media_MediaPlayer_getVideoWidth(JNIEnv *env, jobject thiz) 418{ 419 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 420 if (mp == NULL ) { 421 jniThrowException(env, "java/lang/IllegalStateException", NULL); 422 return 0; 423 } 424 int w; 425 if (0 != mp->getVideoWidth(&w)) { 426 ALOGE("getVideoWidth failed"); 427 w = 0; 428 } 429 ALOGV("getVideoWidth: %d", w); 430 return (jint) w; 431} 432 433static jint 434android_media_MediaPlayer_getVideoHeight(JNIEnv *env, jobject thiz) 435{ 436 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 437 if (mp == NULL ) { 438 jniThrowException(env, "java/lang/IllegalStateException", NULL); 439 return 0; 440 } 441 int h; 442 if (0 != mp->getVideoHeight(&h)) { 443 ALOGE("getVideoHeight failed"); 444 h = 0; 445 } 446 ALOGV("getVideoHeight: %d", h); 447 return (jint) h; 448} 449 450 451static jint 452android_media_MediaPlayer_getCurrentPosition(JNIEnv *env, jobject thiz) 453{ 454 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 455 if (mp == NULL ) { 456 jniThrowException(env, "java/lang/IllegalStateException", NULL); 457 return 0; 458 } 459 int msec; 460 process_media_player_call( env, thiz, mp->getCurrentPosition(&msec), NULL, NULL ); 461 ALOGV("getCurrentPosition: %d (msec)", msec); 462 return (jint) msec; 463} 464 465static jint 466android_media_MediaPlayer_getDuration(JNIEnv *env, jobject thiz) 467{ 468 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 469 if (mp == NULL ) { 470 jniThrowException(env, "java/lang/IllegalStateException", NULL); 471 return 0; 472 } 473 int msec; 474 process_media_player_call( env, thiz, mp->getDuration(&msec), NULL, NULL ); 475 ALOGV("getDuration: %d (msec)", msec); 476 return (jint) msec; 477} 478 479static void 480android_media_MediaPlayer_reset(JNIEnv *env, jobject thiz) 481{ 482 ALOGV("reset"); 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->reset(), NULL, NULL ); 489} 490 491static void 492android_media_MediaPlayer_setAudioStreamType(JNIEnv *env, jobject thiz, jint streamtype) 493{ 494 ALOGV("setAudioStreamType: %d", streamtype); 495 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 496 if (mp == NULL ) { 497 jniThrowException(env, "java/lang/IllegalStateException", NULL); 498 return; 499 } 500 process_media_player_call( env, thiz, mp->setAudioStreamType((audio_stream_type_t) streamtype) , NULL, NULL ); 501} 502 503static jint 504android_media_MediaPlayer_getAudioStreamType(JNIEnv *env, jobject thiz) 505{ 506 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 507 if (mp == NULL ) { 508 jniThrowException(env, "java/lang/IllegalStateException", NULL); 509 return 0; 510 } 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; 515} 516 517static void 518android_media_MediaPlayer_setLooping(JNIEnv *env, jobject thiz, jboolean looping) 519{ 520 ALOGV("setLooping: %d", looping); 521 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 522 if (mp == NULL ) { 523 jniThrowException(env, "java/lang/IllegalStateException", NULL); 524 return; 525 } 526 process_media_player_call( env, thiz, mp->setLooping(looping), NULL, NULL ); 527} 528 529static jboolean 530android_media_MediaPlayer_isLooping(JNIEnv *env, jobject thiz) 531{ 532 ALOGV("isLooping"); 533 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 534 if (mp == NULL ) { 535 jniThrowException(env, "java/lang/IllegalStateException", NULL); 536 return JNI_FALSE; 537 } 538 return mp->isLooping() ? JNI_TRUE : JNI_FALSE; 539} 540 541static void 542android_media_MediaPlayer_setVolume(JNIEnv *env, jobject thiz, jfloat leftVolume, jfloat rightVolume) 543{ 544 ALOGV("setVolume: left %f right %f", (float) leftVolume, (float) rightVolume); 545 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 546 if (mp == NULL ) { 547 jniThrowException(env, "java/lang/IllegalStateException", NULL); 548 return; 549 } 550 process_media_player_call( env, thiz, mp->setVolume((float) leftVolume, (float) rightVolume), NULL, NULL ); 551} 552 553// Sends the request and reply parcels to the media player via the 554// binder interface. 555static jint 556android_media_MediaPlayer_invoke(JNIEnv *env, jobject thiz, 557 jobject java_request, jobject java_reply) 558{ 559 sp<MediaPlayer> media_player = getMediaPlayer(env, thiz); 560 if (media_player == NULL ) { 561 jniThrowException(env, "java/lang/IllegalStateException", NULL); 562 return UNKNOWN_ERROR; 563 } 564 565 Parcel *request = parcelForJavaObject(env, java_request); 566 Parcel *reply = parcelForJavaObject(env, java_reply); 567 568 // Don't use process_media_player_call which use the async loop to 569 // report errors, instead returns the status. 570 return (jint) media_player->invoke(*request, reply); 571} 572 573// Sends the new filter to the client. 574static jint 575android_media_MediaPlayer_setMetadataFilter(JNIEnv *env, jobject thiz, jobject request) 576{ 577 sp<MediaPlayer> media_player = getMediaPlayer(env, thiz); 578 if (media_player == NULL ) { 579 jniThrowException(env, "java/lang/IllegalStateException", NULL); 580 return UNKNOWN_ERROR; 581 } 582 583 Parcel *filter = parcelForJavaObject(env, request); 584 585 if (filter == NULL ) { 586 jniThrowException(env, "java/lang/RuntimeException", "Filter is null"); 587 return UNKNOWN_ERROR; 588 } 589 590 return (jint) media_player->setMetadataFilter(*filter); 591} 592 593static jboolean 594android_media_MediaPlayer_getMetadata(JNIEnv *env, jobject thiz, jboolean update_only, 595 jboolean apply_filter, jobject reply) 596{ 597 sp<MediaPlayer> media_player = getMediaPlayer(env, thiz); 598 if (media_player == NULL ) { 599 jniThrowException(env, "java/lang/IllegalStateException", NULL); 600 return JNI_FALSE; 601 } 602 603 Parcel *metadata = parcelForJavaObject(env, reply); 604 605 if (metadata == NULL ) { 606 jniThrowException(env, "java/lang/RuntimeException", "Reply parcel is null"); 607 return JNI_FALSE; 608 } 609 610 metadata->freeData(); 611 // On return metadata is positioned at the beginning of the 612 // metadata. Note however that the parcel actually starts with the 613 // return code so you should not rewind the parcel using 614 // setDataPosition(0). 615 if (media_player->getMetadata(update_only, apply_filter, metadata) == OK) { 616 return JNI_TRUE; 617 } else { 618 return JNI_FALSE; 619 } 620} 621 622// This function gets some field IDs, which in turn causes class initialization. 623// It is called from a static block in MediaPlayer, which won't run until the 624// first time an instance of this class is used. 625static void 626android_media_MediaPlayer_native_init(JNIEnv *env) 627{ 628 jclass clazz; 629 630 clazz = env->FindClass("android/media/MediaPlayer"); 631 if (clazz == NULL) { 632 return; 633 } 634 635 fields.context = env->GetFieldID(clazz, "mNativeContext", "J"); 636 if (fields.context == NULL) { 637 return; 638 } 639 640 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", 641 "(Ljava/lang/Object;IIILjava/lang/Object;)V"); 642 if (fields.post_event == NULL) { 643 return; 644 } 645 646 fields.surface_texture = env->GetFieldID(clazz, "mNativeSurfaceTexture", "J"); 647 if (fields.surface_texture == NULL) { 648 return; 649 } 650 651 clazz = env->FindClass("android/net/ProxyProperties"); 652 if (clazz == NULL) { 653 return; 654 } 655 656 fields.proxyConfigGetHost = 657 env->GetMethodID(clazz, "getHost", "()Ljava/lang/String;"); 658 659 fields.proxyConfigGetPort = 660 env->GetMethodID(clazz, "getPort", "()I"); 661 662 fields.proxyConfigGetExclusionList = 663 env->GetMethodID(clazz, "getExclusionList", "()Ljava/lang/String;"); 664} 665 666static void 667android_media_MediaPlayer_native_setup(JNIEnv *env, jobject thiz, jobject weak_this) 668{ 669 ALOGV("native_setup"); 670 sp<MediaPlayer> mp = new MediaPlayer(); 671 if (mp == NULL) { 672 jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); 673 return; 674 } 675 676 // create new listener and give it to MediaPlayer 677 sp<JNIMediaPlayerListener> listener = new JNIMediaPlayerListener(env, thiz, weak_this); 678 mp->setListener(listener); 679 680 // Stow our new C++ MediaPlayer in an opaque field in the Java object. 681 setMediaPlayer(env, thiz, mp); 682} 683 684static void 685android_media_MediaPlayer_release(JNIEnv *env, jobject thiz) 686{ 687 ALOGV("release"); 688 decVideoSurfaceRef(env, thiz); 689 sp<MediaPlayer> mp = setMediaPlayer(env, thiz, 0); 690 if (mp != NULL) { 691 // this prevents native callbacks after the object is released 692 mp->setListener(0); 693 mp->disconnect(); 694 } 695} 696 697static void 698android_media_MediaPlayer_native_finalize(JNIEnv *env, jobject thiz) 699{ 700 ALOGV("native_finalize"); 701 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 702 if (mp != NULL) { 703 ALOGW("MediaPlayer finalized without being released"); 704 } 705 android_media_MediaPlayer_release(env, thiz); 706} 707 708static void android_media_MediaPlayer_set_audio_session_id(JNIEnv *env, jobject thiz, jint sessionId) { 709 ALOGV("set_session_id(): %d", sessionId); 710 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 711 if (mp == NULL ) { 712 jniThrowException(env, "java/lang/IllegalStateException", NULL); 713 return; 714 } 715 process_media_player_call( env, thiz, mp->setAudioSessionId(sessionId), NULL, NULL ); 716} 717 718static jint android_media_MediaPlayer_get_audio_session_id(JNIEnv *env, jobject thiz) { 719 ALOGV("get_session_id()"); 720 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 721 if (mp == NULL ) { 722 jniThrowException(env, "java/lang/IllegalStateException", NULL); 723 return 0; 724 } 725 726 return (jint) mp->getAudioSessionId(); 727} 728 729static void 730android_media_MediaPlayer_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level) 731{ 732 ALOGV("setAuxEffectSendLevel: level %f", level); 733 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 734 if (mp == NULL ) { 735 jniThrowException(env, "java/lang/IllegalStateException", NULL); 736 return; 737 } 738 process_media_player_call( env, thiz, mp->setAuxEffectSendLevel(level), NULL, NULL ); 739} 740 741static void android_media_MediaPlayer_attachAuxEffect(JNIEnv *env, jobject thiz, jint effectId) { 742 ALOGV("attachAuxEffect(): %d", effectId); 743 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 744 if (mp == NULL ) { 745 jniThrowException(env, "java/lang/IllegalStateException", NULL); 746 return; 747 } 748 process_media_player_call( env, thiz, mp->attachAuxEffect(effectId), NULL, NULL ); 749} 750 751static jint 752android_media_MediaPlayer_pullBatteryData( 753 JNIEnv *env, jobject /* thiz */, jobject java_reply) 754{ 755 sp<IBinder> binder = defaultServiceManager()->getService(String16("media.player")); 756 sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); 757 if (service.get() == NULL) { 758 jniThrowException(env, "java/lang/RuntimeException", "cannot get MediaPlayerService"); 759 return UNKNOWN_ERROR; 760 } 761 762 Parcel *reply = parcelForJavaObject(env, java_reply); 763 764 return (jint) service->pullBatteryData(reply); 765} 766 767static jint 768android_media_MediaPlayer_setRetransmitEndpoint(JNIEnv *env, jobject thiz, 769 jstring addrString, jint port) { 770 sp<MediaPlayer> mp = getMediaPlayer(env, thiz); 771 if (mp == NULL ) { 772 jniThrowException(env, "java/lang/IllegalStateException", NULL); 773 return INVALID_OPERATION; 774 } 775 776 const char *cAddrString = NULL; 777 778 if (NULL != addrString) { 779 cAddrString = env->GetStringUTFChars(addrString, NULL); 780 if (cAddrString == NULL) { // Out of memory 781 return NO_MEMORY; 782 } 783 } 784 ALOGV("setRetransmitEndpoint: %s:%d", 785 cAddrString ? cAddrString : "(null)", port); 786 787 status_t ret; 788 if (cAddrString && (port > 0xFFFF)) { 789 ret = BAD_VALUE; 790 } else { 791 ret = mp->setRetransmitEndpoint(cAddrString, 792 static_cast<uint16_t>(port)); 793 } 794 795 if (NULL != addrString) { 796 env->ReleaseStringUTFChars(addrString, cAddrString); 797 } 798 799 if (ret == INVALID_OPERATION ) { 800 jniThrowException(env, "java/lang/IllegalStateException", NULL); 801 } 802 803 return (jint) ret; 804} 805 806static void 807android_media_MediaPlayer_setNextMediaPlayer(JNIEnv *env, jobject thiz, jobject java_player) 808{ 809 ALOGV("setNextMediaPlayer"); 810 sp<MediaPlayer> thisplayer = getMediaPlayer(env, thiz); 811 if (thisplayer == NULL) { 812 jniThrowException(env, "java/lang/IllegalStateException", "This player not initialized"); 813 return; 814 } 815 sp<MediaPlayer> nextplayer = (java_player == NULL) ? NULL : getMediaPlayer(env, java_player); 816 if (nextplayer == NULL && java_player != NULL) { 817 jniThrowException(env, "java/lang/IllegalStateException", "That player not initialized"); 818 return; 819 } 820 821 if (nextplayer == thisplayer) { 822 jniThrowException(env, "java/lang/IllegalArgumentException", "Next player can't be self"); 823 return; 824 } 825 // tie the two players together 826 process_media_player_call( 827 env, thiz, thisplayer->setNextMediaPlayer(nextplayer), 828 "java/lang/IllegalArgumentException", 829 "setNextMediaPlayer failed." ); 830 ; 831} 832 833// ---------------------------------------------------------------------------- 834 835static JNINativeMethod gMethods[] = { 836 { 837 "nativeSetDataSource", 838 "(Landroid/os/IBinder;Ljava/lang/String;[Ljava/lang/String;" 839 "[Ljava/lang/String;)V", 840 (void *)android_media_MediaPlayer_setDataSourceAndHeaders 841 }, 842 843 {"_setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaPlayer_setDataSourceFD}, 844 {"_setVideoSurface", "(Landroid/view/Surface;)V", (void *)android_media_MediaPlayer_setVideoSurface}, 845 {"prepare", "()V", (void *)android_media_MediaPlayer_prepare}, 846 {"prepareAsync", "()V", (void *)android_media_MediaPlayer_prepareAsync}, 847 {"_start", "()V", (void *)android_media_MediaPlayer_start}, 848 {"_stop", "()V", (void *)android_media_MediaPlayer_stop}, 849 {"getVideoWidth", "()I", (void *)android_media_MediaPlayer_getVideoWidth}, 850 {"getVideoHeight", "()I", (void *)android_media_MediaPlayer_getVideoHeight}, 851 {"seekTo", "(I)V", (void *)android_media_MediaPlayer_seekTo}, 852 {"_pause", "()V", (void *)android_media_MediaPlayer_pause}, 853 {"isPlaying", "()Z", (void *)android_media_MediaPlayer_isPlaying}, 854 {"getCurrentPosition", "()I", (void *)android_media_MediaPlayer_getCurrentPosition}, 855 {"getDuration", "()I", (void *)android_media_MediaPlayer_getDuration}, 856 {"_release", "()V", (void *)android_media_MediaPlayer_release}, 857 {"_reset", "()V", (void *)android_media_MediaPlayer_reset}, 858 {"_setAudioStreamType", "(I)V", (void *)android_media_MediaPlayer_setAudioStreamType}, 859 {"_getAudioStreamType", "()I", (void *)android_media_MediaPlayer_getAudioStreamType}, 860 {"setLooping", "(Z)V", (void *)android_media_MediaPlayer_setLooping}, 861 {"isLooping", "()Z", (void *)android_media_MediaPlayer_isLooping}, 862 {"_setVolume", "(FF)V", (void *)android_media_MediaPlayer_setVolume}, 863 {"native_invoke", "(Landroid/os/Parcel;Landroid/os/Parcel;)I",(void *)android_media_MediaPlayer_invoke}, 864 {"native_setMetadataFilter", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_setMetadataFilter}, 865 {"native_getMetadata", "(ZZLandroid/os/Parcel;)Z", (void *)android_media_MediaPlayer_getMetadata}, 866 {"native_init", "()V", (void *)android_media_MediaPlayer_native_init}, 867 {"native_setup", "(Ljava/lang/Object;)V", (void *)android_media_MediaPlayer_native_setup}, 868 {"native_finalize", "()V", (void *)android_media_MediaPlayer_native_finalize}, 869 {"getAudioSessionId", "()I", (void *)android_media_MediaPlayer_get_audio_session_id}, 870 {"setAudioSessionId", "(I)V", (void *)android_media_MediaPlayer_set_audio_session_id}, 871 {"_setAuxEffectSendLevel", "(F)V", (void *)android_media_MediaPlayer_setAuxEffectSendLevel}, 872 {"attachAuxEffect", "(I)V", (void *)android_media_MediaPlayer_attachAuxEffect}, 873 {"native_pullBatteryData", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer_pullBatteryData}, 874 {"native_setRetransmitEndpoint", "(Ljava/lang/String;I)I", (void *)android_media_MediaPlayer_setRetransmitEndpoint}, 875 {"setNextMediaPlayer", "(Landroid/media/MediaPlayer;)V", (void *)android_media_MediaPlayer_setNextMediaPlayer}, 876}; 877 878static const char* const kClassPathName = "android/media/MediaPlayer"; 879 880// This function only registers the native methods 881static int register_android_media_MediaPlayer(JNIEnv *env) 882{ 883 return AndroidRuntime::registerNativeMethods(env, 884 "android/media/MediaPlayer", gMethods, NELEM(gMethods)); 885} 886 887extern int register_android_media_ImageReader(JNIEnv *env); 888extern int register_android_media_Crypto(JNIEnv *env); 889extern int register_android_media_Drm(JNIEnv *env); 890extern int register_android_media_MediaCodec(JNIEnv *env); 891extern int register_android_media_MediaExtractor(JNIEnv *env); 892extern int register_android_media_MediaCodecList(JNIEnv *env); 893extern int register_android_media_MediaHTTPConnection(JNIEnv *env); 894extern int register_android_media_MediaMetadataRetriever(JNIEnv *env); 895extern int register_android_media_MediaMuxer(JNIEnv *env); 896extern int register_android_media_MediaRecorder(JNIEnv *env); 897extern int register_android_media_MediaScanner(JNIEnv *env); 898extern int register_android_media_ResampleInputStream(JNIEnv *env); 899extern int register_android_media_MediaProfiles(JNIEnv *env); 900extern int register_android_media_AmrInputStream(JNIEnv *env); 901extern int register_android_mtp_MtpDatabase(JNIEnv *env); 902extern int register_android_mtp_MtpDevice(JNIEnv *env); 903extern int register_android_mtp_MtpServer(JNIEnv *env); 904 905jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) 906{ 907 JNIEnv* env = NULL; 908 jint result = -1; 909 910 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { 911 ALOGE("ERROR: GetEnv failed\n"); 912 goto bail; 913 } 914 assert(env != NULL); 915 916 if (register_android_media_ImageReader(env) < 0) { 917 ALOGE("ERROR: ImageReader native registration failed"); 918 goto bail; 919 } 920 921 if (register_android_media_MediaPlayer(env) < 0) { 922 ALOGE("ERROR: MediaPlayer native registration failed\n"); 923 goto bail; 924 } 925 926 if (register_android_media_MediaRecorder(env) < 0) { 927 ALOGE("ERROR: MediaRecorder native registration failed\n"); 928 goto bail; 929 } 930 931 if (register_android_media_MediaScanner(env) < 0) { 932 ALOGE("ERROR: MediaScanner native registration failed\n"); 933 goto bail; 934 } 935 936 if (register_android_media_MediaMetadataRetriever(env) < 0) { 937 ALOGE("ERROR: MediaMetadataRetriever native registration failed\n"); 938 goto bail; 939 } 940 941 if (register_android_media_AmrInputStream(env) < 0) { 942 ALOGE("ERROR: AmrInputStream native registration failed\n"); 943 goto bail; 944 } 945 946 if (register_android_media_ResampleInputStream(env) < 0) { 947 ALOGE("ERROR: ResampleInputStream native registration failed\n"); 948 goto bail; 949 } 950 951 if (register_android_media_MediaProfiles(env) < 0) { 952 ALOGE("ERROR: MediaProfiles native registration failed"); 953 goto bail; 954 } 955 956 if (register_android_mtp_MtpDatabase(env) < 0) { 957 ALOGE("ERROR: MtpDatabase native registration failed"); 958 goto bail; 959 } 960 961 if (register_android_mtp_MtpDevice(env) < 0) { 962 ALOGE("ERROR: MtpDevice native registration failed"); 963 goto bail; 964 } 965 966 if (register_android_mtp_MtpServer(env) < 0) { 967 ALOGE("ERROR: MtpServer native registration failed"); 968 goto bail; 969 } 970 971 if (register_android_media_MediaCodec(env) < 0) { 972 ALOGE("ERROR: MediaCodec native registration failed"); 973 goto bail; 974 } 975 976 if (register_android_media_MediaExtractor(env) < 0) { 977 ALOGE("ERROR: MediaCodec native registration failed"); 978 goto bail; 979 } 980 981 if (register_android_media_MediaMuxer(env) < 0) { 982 ALOGE("ERROR: MediaMuxer native registration failed"); 983 goto bail; 984 } 985 986 if (register_android_media_MediaCodecList(env) < 0) { 987 ALOGE("ERROR: MediaCodec native registration failed"); 988 goto bail; 989 } 990 991 if (register_android_media_Crypto(env) < 0) { 992 ALOGE("ERROR: MediaCodec native registration failed"); 993 goto bail; 994 } 995 996 if (register_android_media_Drm(env) < 0) { 997 ALOGE("ERROR: MediaDrm native registration failed"); 998 goto bail; 999 } 1000 1001 if (register_android_media_MediaHTTPConnection(env) < 0) { 1002 ALOGE("ERROR: MediaHTTPConnection native registration failed"); 1003 goto bail; 1004 } 1005 1006 /* success -- return valid version number */ 1007 result = JNI_VERSION_1_4; 1008 1009bail: 1010 return result; 1011} 1012 1013// KTHXBYE 1014