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