android_media_MediaPlayer2.cpp revision 0a8a8f0b26634395ce64123e2a385670d6b07c00
1/* 2** 3** Copyright 2017, 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 "MediaPlayer2-JNI" 20#include "utils/Log.h" 21 22#include <media/mediaplayer2.h> 23#include <media/AudioResamplerPublic.h> 24#include <media/MediaHTTPService.h> 25#include <media/MediaPlayer2Interface.h> 26#include <media/MediaAnalyticsItem.h> 27#include <media/NdkWrapper.h> 28#include <media/stagefright/foundation/ByteUtils.h> // for FOURCC definition 29#include <stdio.h> 30#include <assert.h> 31#include <limits.h> 32#include <unistd.h> 33#include <fcntl.h> 34#include <utils/threads.h> 35#include "jni.h" 36#include <nativehelper/JNIHelp.h> 37#include "android/native_window_jni.h" 38#include "android_runtime/Log.h" 39#include "utils/Errors.h" // for status_t 40#include "utils/KeyedVector.h" 41#include "utils/String8.h" 42#include "android_media_BufferingParams.h" 43#include "android_media_Media2HTTPService.h" 44#include "android_media_Media2DataSource.h" 45#include "android_media_MediaMetricsJNI.h" 46#include "android_media_PlaybackParams.h" 47#include "android_media_SyncParams.h" 48#include "android_media_VolumeShaper.h" 49 50#include "android_os_Parcel.h" 51#include "android_util_Binder.h" 52#include <binder/Parcel.h> 53 54// Modular DRM begin 55#define FIND_CLASS(var, className) \ 56var = env->FindClass(className); \ 57LOG_FATAL_IF(! (var), "Unable to find class " className); 58 59#define GET_METHOD_ID(var, clazz, fieldName, fieldDescriptor) \ 60var = env->GetMethodID(clazz, fieldName, fieldDescriptor); \ 61LOG_FATAL_IF(! (var), "Unable to find method " fieldName); 62 63struct StateExceptionFields { 64 jmethodID init; 65 jclass classId; 66}; 67 68static StateExceptionFields gStateExceptionFields; 69// Modular DRM end 70 71// ---------------------------------------------------------------------------- 72 73using namespace android; 74 75using media::VolumeShaper; 76 77// ---------------------------------------------------------------------------- 78 79struct fields_t { 80 jfieldID context; 81 jfieldID surface_texture; 82 83 jmethodID post_event; 84 85 jmethodID proxyConfigGetHost; 86 jmethodID proxyConfigGetPort; 87 jmethodID proxyConfigGetExclusionList; 88}; 89static fields_t fields; 90 91static BufferingParams::fields_t gBufferingParamsFields; 92static PlaybackParams::fields_t gPlaybackParamsFields; 93static SyncParams::fields_t gSyncParamsFields; 94static VolumeShaperHelper::fields_t gVolumeShaperFields; 95 96static Mutex sLock; 97 98static bool ConvertKeyValueArraysToKeyedVector( 99 JNIEnv *env, jobjectArray keys, jobjectArray values, 100 KeyedVector<String8, String8>* keyedVector) { 101 102 int nKeyValuePairs = 0; 103 bool failed = false; 104 if (keys != NULL && values != NULL) { 105 nKeyValuePairs = env->GetArrayLength(keys); 106 failed = (nKeyValuePairs != env->GetArrayLength(values)); 107 } 108 109 if (!failed) { 110 failed = ((keys != NULL && values == NULL) || 111 (keys == NULL && values != NULL)); 112 } 113 114 if (failed) { 115 ALOGE("keys and values arrays have different length"); 116 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 117 return false; 118 } 119 120 for (int i = 0; i < nKeyValuePairs; ++i) { 121 // No need to check on the ArrayIndexOutOfBoundsException, since 122 // it won't happen here. 123 jstring key = (jstring) env->GetObjectArrayElement(keys, i); 124 jstring value = (jstring) env->GetObjectArrayElement(values, i); 125 126 const char* keyStr = env->GetStringUTFChars(key, NULL); 127 if (!keyStr) { // OutOfMemoryError 128 return false; 129 } 130 131 const char* valueStr = env->GetStringUTFChars(value, NULL); 132 if (!valueStr) { // OutOfMemoryError 133 env->ReleaseStringUTFChars(key, keyStr); 134 return false; 135 } 136 137 keyedVector->add(String8(keyStr), String8(valueStr)); 138 139 env->ReleaseStringUTFChars(key, keyStr); 140 env->ReleaseStringUTFChars(value, valueStr); 141 env->DeleteLocalRef(key); 142 env->DeleteLocalRef(value); 143 } 144 return true; 145} 146 147// ---------------------------------------------------------------------------- 148// ref-counted object for callbacks 149class JNIMediaPlayer2Listener: public MediaPlayer2Listener 150{ 151public: 152 JNIMediaPlayer2Listener(JNIEnv* env, jobject thiz, jobject weak_thiz); 153 ~JNIMediaPlayer2Listener(); 154 virtual void notify(int msg, int ext1, int ext2, const Parcel *obj = NULL); 155private: 156 JNIMediaPlayer2Listener(); 157 jclass mClass; // Reference to MediaPlayer2 class 158 jobject mObject; // Weak ref to MediaPlayer2 Java object to call on 159}; 160 161JNIMediaPlayer2Listener::JNIMediaPlayer2Listener(JNIEnv* env, jobject thiz, jobject weak_thiz) 162{ 163 164 // Hold onto the MediaPlayer2 class for use in calling the static method 165 // that posts events to the application thread. 166 jclass clazz = env->GetObjectClass(thiz); 167 if (clazz == NULL) { 168 ALOGE("Can't find android/media/MediaPlayer2Impl"); 169 jniThrowException(env, "java/lang/Exception", NULL); 170 return; 171 } 172 mClass = (jclass)env->NewGlobalRef(clazz); 173 174 // We use a weak reference so the MediaPlayer2 object can be garbage collected. 175 // The reference is only used as a proxy for callbacks. 176 mObject = env->NewGlobalRef(weak_thiz); 177} 178 179JNIMediaPlayer2Listener::~JNIMediaPlayer2Listener() 180{ 181 // remove global references 182 JNIEnv *env = AndroidRuntime::getJNIEnv(); 183 env->DeleteGlobalRef(mObject); 184 env->DeleteGlobalRef(mClass); 185} 186 187void JNIMediaPlayer2Listener::notify(int msg, int ext1, int ext2, const Parcel *obj) 188{ 189 JNIEnv *env = AndroidRuntime::getJNIEnv(); 190 if (obj && obj->dataSize() > 0) { 191 jobject jParcel = createJavaParcelObject(env); 192 if (jParcel != NULL) { 193 Parcel* nativeParcel = parcelForJavaObject(env, jParcel); 194 nativeParcel->setData(obj->data(), obj->dataSize()); 195 env->CallStaticVoidMethod(mClass, fields.post_event, mObject, 196 msg, ext1, ext2, jParcel); 197 env->DeleteLocalRef(jParcel); 198 } 199 } else { 200 env->CallStaticVoidMethod(mClass, fields.post_event, mObject, 201 msg, ext1, ext2, NULL); 202 } 203 if (env->ExceptionCheck()) { 204 ALOGW("An exception occurred while notifying an event."); 205 LOGW_EX(env); 206 env->ExceptionClear(); 207 } 208} 209 210// ---------------------------------------------------------------------------- 211 212static sp<MediaPlayer2> getMediaPlayer(JNIEnv* env, jobject thiz) 213{ 214 Mutex::Autolock l(sLock); 215 MediaPlayer2* const p = (MediaPlayer2*)env->GetLongField(thiz, fields.context); 216 return sp<MediaPlayer2>(p); 217} 218 219static sp<MediaPlayer2> setMediaPlayer(JNIEnv* env, jobject thiz, const sp<MediaPlayer2>& player) 220{ 221 Mutex::Autolock l(sLock); 222 sp<MediaPlayer2> old = (MediaPlayer2*)env->GetLongField(thiz, fields.context); 223 if (player.get()) { 224 player->incStrong((void*)setMediaPlayer); 225 } 226 if (old != 0) { 227 old->decStrong((void*)setMediaPlayer); 228 } 229 env->SetLongField(thiz, fields.context, (jlong)player.get()); 230 return old; 231} 232 233// If exception is NULL and opStatus is not OK, this method sends an error 234// event to the client application; otherwise, if exception is not NULL and 235// opStatus is not OK, this method throws the given exception to the client 236// application. 237static void process_media_player_call(JNIEnv *env, jobject thiz, status_t opStatus, const char* exception, const char *message) 238{ 239 if (exception == NULL) { // Don't throw exception. Instead, send an event. 240 if (opStatus != (status_t) OK) { 241 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 242 if (mp != 0) mp->notify(MEDIA2_ERROR, opStatus, 0); 243 } 244 } else { // Throw exception! 245 if ( opStatus == (status_t) INVALID_OPERATION ) { 246 jniThrowException(env, "java/lang/IllegalStateException", NULL); 247 } else if ( opStatus == (status_t) BAD_VALUE ) { 248 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 249 } else if ( opStatus == (status_t) PERMISSION_DENIED ) { 250 jniThrowException(env, "java/lang/SecurityException", NULL); 251 } else if ( opStatus != (status_t) OK ) { 252 if (strlen(message) > 230) { 253 // if the message is too long, don't bother displaying the status code 254 jniThrowException( env, exception, message); 255 } else { 256 char msg[256]; 257 // append the status code to the message 258 sprintf(msg, "%s: status=0x%X", message, opStatus); 259 jniThrowException( env, exception, msg); 260 } 261 } 262 } 263} 264 265static void 266android_media_MediaPlayer2_setDataSourceAndHeaders( 267 JNIEnv *env, jobject thiz, jobject httpServiceObj, jstring path, 268 jobjectArray keys, jobjectArray values) { 269 270 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 271 if (mp == NULL ) { 272 jniThrowException(env, "java/lang/IllegalStateException", NULL); 273 return; 274 } 275 276 if (path == NULL) { 277 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 278 return; 279 } 280 281 const char *tmp = env->GetStringUTFChars(path, NULL); 282 if (tmp == NULL) { // Out of memory 283 return; 284 } 285 ALOGV("setDataSource: path %s", tmp); 286 287 String8 pathStr(tmp); 288 env->ReleaseStringUTFChars(path, tmp); 289 tmp = NULL; 290 291 // We build a KeyedVector out of the key and val arrays 292 KeyedVector<String8, String8> headersVector; 293 if (!ConvertKeyValueArraysToKeyedVector( 294 env, keys, values, &headersVector)) { 295 return; 296 } 297 298 sp<MediaHTTPService> httpService; 299 if (httpServiceObj != NULL) { 300 httpService = new JMedia2HTTPService(env, httpServiceObj); 301 } 302 303 status_t opStatus = 304 mp->setDataSource( 305 httpService, 306 pathStr, 307 headersVector.size() > 0? &headersVector : NULL); 308 309 process_media_player_call( 310 env, thiz, opStatus, "java/io/IOException", 311 "setDataSource failed." ); 312} 313 314static void 315android_media_MediaPlayer2_setDataSourceFD(JNIEnv *env, jobject thiz, jobject fileDescriptor, jlong offset, jlong length) 316{ 317 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 318 if (mp == NULL ) { 319 jniThrowException(env, "java/lang/IllegalStateException", NULL); 320 return; 321 } 322 323 if (fileDescriptor == NULL) { 324 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 325 return; 326 } 327 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); 328 ALOGV("setDataSourceFD: fd %d", fd); 329 process_media_player_call( env, thiz, mp->setDataSource(fd, offset, length), "java/io/IOException", "setDataSourceFD failed." ); 330} 331 332static void 333android_media_MediaPlayer2_setDataSourceCallback(JNIEnv *env, jobject thiz, jobject dataSource) 334{ 335 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 336 if (mp == NULL ) { 337 jniThrowException(env, "java/lang/IllegalStateException", NULL); 338 return; 339 } 340 341 if (dataSource == NULL) { 342 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 343 return; 344 } 345 sp<DataSource> callbackDataSource = new JMedia2DataSource(env, dataSource); 346 process_media_player_call(env, thiz, mp->setDataSource(callbackDataSource), "java/lang/RuntimeException", "setDataSourceCallback failed." ); 347} 348 349static sp<ANativeWindowWrapper> 350getVideoSurfaceTexture(JNIEnv* env, jobject thiz) { 351 ANativeWindow * const p = (ANativeWindow*)env->GetLongField(thiz, fields.surface_texture); 352 return new ANativeWindowWrapper(p); 353} 354 355static void 356decVideoSurfaceRef(JNIEnv *env, jobject thiz) 357{ 358 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 359 if (mp == NULL) { 360 return; 361 } 362 363 ANativeWindow * const old_anw = (ANativeWindow*)env->GetLongField(thiz, fields.surface_texture); 364 if (old_anw != NULL) { 365 ANativeWindow_release(old_anw); 366 env->SetLongField(thiz, fields.surface_texture, (jlong)NULL); 367 } 368} 369 370static void 371setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface, jboolean mediaPlayerMustBeAlive) 372{ 373 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 374 if (mp == NULL) { 375 if (mediaPlayerMustBeAlive) { 376 jniThrowException(env, "java/lang/IllegalStateException", NULL); 377 } 378 return; 379 } 380 381 decVideoSurfaceRef(env, thiz); 382 383 ANativeWindow* anw = NULL; 384 if (jsurface) { 385 anw = ANativeWindow_fromSurface(env, jsurface); 386 if (anw == NULL) { 387 jniThrowException(env, "java/lang/IllegalArgumentException", 388 "The surface has been released"); 389 return; 390 } 391 } 392 393 env->SetLongField(thiz, fields.surface_texture, (jlong)anw); 394 395 // This will fail if the media player has not been initialized yet. This 396 // can be the case if setDisplay() on MediaPlayer2Impl.java has been called 397 // before setDataSource(). The redundant call to setVideoSurfaceTexture() 398 // in prepare/prepareAsync covers for this case. 399 mp->setVideoSurfaceTexture(new ANativeWindowWrapper(anw)); 400} 401 402static void 403android_media_MediaPlayer2_setVideoSurface(JNIEnv *env, jobject thiz, jobject jsurface) 404{ 405 setVideoSurface(env, thiz, jsurface, true /* mediaPlayerMustBeAlive */); 406} 407 408static jobject 409android_media_MediaPlayer2_getBufferingParams(JNIEnv *env, jobject thiz) 410{ 411 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 412 if (mp == NULL) { 413 jniThrowException(env, "java/lang/IllegalStateException", NULL); 414 return NULL; 415 } 416 417 BufferingParams bp; 418 BufferingSettings &settings = bp.settings; 419 process_media_player_call( 420 env, thiz, mp->getBufferingSettings(&settings), 421 "java/lang/IllegalStateException", "unexpected error"); 422 ALOGV("getBufferingSettings:{%s}", settings.toString().string()); 423 424 return bp.asJobject(env, gBufferingParamsFields); 425} 426 427static void 428android_media_MediaPlayer2_setBufferingParams(JNIEnv *env, jobject thiz, jobject params) 429{ 430 if (params == NULL) { 431 return; 432 } 433 434 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 435 if (mp == NULL) { 436 jniThrowException(env, "java/lang/IllegalStateException", NULL); 437 return; 438 } 439 440 BufferingParams bp; 441 bp.fillFromJobject(env, gBufferingParamsFields, params); 442 ALOGV("setBufferingParams:{%s}", bp.settings.toString().string()); 443 444 process_media_player_call( 445 env, thiz, mp->setBufferingSettings(bp.settings), 446 "java/lang/IllegalStateException", "unexpected error"); 447} 448 449static void 450android_media_MediaPlayer2_prepare(JNIEnv *env, jobject thiz) 451{ 452 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 453 if (mp == NULL ) { 454 jniThrowException(env, "java/lang/IllegalStateException", NULL); 455 return; 456 } 457 458 // Handle the case where the display surface was set before the mp was 459 // initialized. We try again to make it stick. 460 sp<ANativeWindowWrapper> st = getVideoSurfaceTexture(env, thiz); 461 mp->setVideoSurfaceTexture(st); 462 463 process_media_player_call( env, thiz, mp->prepare(), "java/io/IOException", "Prepare failed." ); 464} 465 466static void 467android_media_MediaPlayer2_prepareAsync(JNIEnv *env, jobject thiz) 468{ 469 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 470 if (mp == NULL ) { 471 jniThrowException(env, "java/lang/IllegalStateException", NULL); 472 return; 473 } 474 475 // Handle the case where the display surface was set before the mp was 476 // initialized. We try again to make it stick. 477 sp<ANativeWindowWrapper> st = getVideoSurfaceTexture(env, thiz); 478 mp->setVideoSurfaceTexture(st); 479 480 process_media_player_call( env, thiz, mp->prepareAsync(), "java/io/IOException", "Prepare Async failed." ); 481} 482 483static void 484android_media_MediaPlayer2_start(JNIEnv *env, jobject thiz) 485{ 486 ALOGV("start"); 487 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 488 if (mp == NULL ) { 489 jniThrowException(env, "java/lang/IllegalStateException", NULL); 490 return; 491 } 492 process_media_player_call( env, thiz, mp->start(), NULL, NULL ); 493} 494 495static void 496android_media_MediaPlayer2_stop(JNIEnv *env, jobject thiz) 497{ 498 ALOGV("stop"); 499 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 500 if (mp == NULL ) { 501 jniThrowException(env, "java/lang/IllegalStateException", NULL); 502 return; 503 } 504 process_media_player_call( env, thiz, mp->stop(), NULL, NULL ); 505} 506 507static void 508android_media_MediaPlayer2_pause(JNIEnv *env, jobject thiz) 509{ 510 ALOGV("pause"); 511 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 512 if (mp == NULL ) { 513 jniThrowException(env, "java/lang/IllegalStateException", NULL); 514 return; 515 } 516 process_media_player_call( env, thiz, mp->pause(), NULL, NULL ); 517} 518 519static jboolean 520android_media_MediaPlayer2_isPlaying(JNIEnv *env, jobject thiz) 521{ 522 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 523 if (mp == NULL ) { 524 jniThrowException(env, "java/lang/IllegalStateException", NULL); 525 return JNI_FALSE; 526 } 527 const jboolean is_playing = mp->isPlaying(); 528 529 ALOGV("isPlaying: %d", is_playing); 530 return is_playing; 531} 532 533static void 534android_media_MediaPlayer2_setPlaybackParams(JNIEnv *env, jobject thiz, jobject params) 535{ 536 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 537 if (mp == NULL) { 538 jniThrowException(env, "java/lang/IllegalStateException", NULL); 539 return; 540 } 541 542 PlaybackParams pbp; 543 pbp.fillFromJobject(env, gPlaybackParamsFields, params); 544 ALOGV("setPlaybackParams: %d:%f %d:%f %d:%u %d:%u", 545 pbp.speedSet, pbp.audioRate.mSpeed, 546 pbp.pitchSet, pbp.audioRate.mPitch, 547 pbp.audioFallbackModeSet, pbp.audioRate.mFallbackMode, 548 pbp.audioStretchModeSet, pbp.audioRate.mStretchMode); 549 550 AudioPlaybackRate rate; 551 status_t err = mp->getPlaybackSettings(&rate); 552 if (err == OK) { 553 bool updatedRate = false; 554 if (pbp.speedSet) { 555 rate.mSpeed = pbp.audioRate.mSpeed; 556 updatedRate = true; 557 } 558 if (pbp.pitchSet) { 559 rate.mPitch = pbp.audioRate.mPitch; 560 updatedRate = true; 561 } 562 if (pbp.audioFallbackModeSet) { 563 rate.mFallbackMode = pbp.audioRate.mFallbackMode; 564 updatedRate = true; 565 } 566 if (pbp.audioStretchModeSet) { 567 rate.mStretchMode = pbp.audioRate.mStretchMode; 568 updatedRate = true; 569 } 570 if (updatedRate) { 571 err = mp->setPlaybackSettings(rate); 572 } 573 } 574 process_media_player_call( 575 env, thiz, err, 576 "java/lang/IllegalStateException", "unexpected error"); 577} 578 579static jobject 580android_media_MediaPlayer2_getPlaybackParams(JNIEnv *env, jobject thiz) 581{ 582 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 583 if (mp == NULL) { 584 jniThrowException(env, "java/lang/IllegalStateException", NULL); 585 return NULL; 586 } 587 588 PlaybackParams pbp; 589 AudioPlaybackRate &audioRate = pbp.audioRate; 590 process_media_player_call( 591 env, thiz, mp->getPlaybackSettings(&audioRate), 592 "java/lang/IllegalStateException", "unexpected error"); 593 ALOGV("getPlaybackSettings: %f %f %d %d", 594 audioRate.mSpeed, audioRate.mPitch, audioRate.mFallbackMode, audioRate.mStretchMode); 595 596 pbp.speedSet = true; 597 pbp.pitchSet = true; 598 pbp.audioFallbackModeSet = true; 599 pbp.audioStretchModeSet = true; 600 601 return pbp.asJobject(env, gPlaybackParamsFields); 602} 603 604static void 605android_media_MediaPlayer2_setSyncParams(JNIEnv *env, jobject thiz, jobject params) 606{ 607 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 608 if (mp == NULL) { 609 jniThrowException(env, "java/lang/IllegalStateException", NULL); 610 return; 611 } 612 613 SyncParams scp; 614 scp.fillFromJobject(env, gSyncParamsFields, params); 615 ALOGV("setSyncParams: %d:%d %d:%d %d:%f %d:%f", 616 scp.syncSourceSet, scp.sync.mSource, 617 scp.audioAdjustModeSet, scp.sync.mAudioAdjustMode, 618 scp.toleranceSet, scp.sync.mTolerance, 619 scp.frameRateSet, scp.frameRate); 620 621 AVSyncSettings avsync; 622 float videoFrameRate; 623 status_t err = mp->getSyncSettings(&avsync, &videoFrameRate); 624 if (err == OK) { 625 bool updatedSync = scp.frameRateSet; 626 if (scp.syncSourceSet) { 627 avsync.mSource = scp.sync.mSource; 628 updatedSync = true; 629 } 630 if (scp.audioAdjustModeSet) { 631 avsync.mAudioAdjustMode = scp.sync.mAudioAdjustMode; 632 updatedSync = true; 633 } 634 if (scp.toleranceSet) { 635 avsync.mTolerance = scp.sync.mTolerance; 636 updatedSync = true; 637 } 638 if (updatedSync) { 639 err = mp->setSyncSettings(avsync, scp.frameRateSet ? scp.frameRate : -1.f); 640 } 641 } 642 process_media_player_call( 643 env, thiz, err, 644 "java/lang/IllegalStateException", "unexpected error"); 645} 646 647static jobject 648android_media_MediaPlayer2_getSyncParams(JNIEnv *env, jobject thiz) 649{ 650 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 651 if (mp == NULL) { 652 jniThrowException(env, "java/lang/IllegalStateException", NULL); 653 return NULL; 654 } 655 656 SyncParams scp; 657 scp.frameRate = -1.f; 658 process_media_player_call( 659 env, thiz, mp->getSyncSettings(&scp.sync, &scp.frameRate), 660 "java/lang/IllegalStateException", "unexpected error"); 661 662 ALOGV("getSyncSettings: %d %d %f %f", 663 scp.sync.mSource, scp.sync.mAudioAdjustMode, scp.sync.mTolerance, scp.frameRate); 664 665 // sanity check params 666 if (scp.sync.mSource >= AVSYNC_SOURCE_MAX 667 || scp.sync.mAudioAdjustMode >= AVSYNC_AUDIO_ADJUST_MODE_MAX 668 || scp.sync.mTolerance < 0.f 669 || scp.sync.mTolerance >= AVSYNC_TOLERANCE_MAX) { 670 jniThrowException(env, "java/lang/IllegalStateException", NULL); 671 return NULL; 672 } 673 674 scp.syncSourceSet = true; 675 scp.audioAdjustModeSet = true; 676 scp.toleranceSet = true; 677 scp.frameRateSet = scp.frameRate >= 0.f; 678 679 return scp.asJobject(env, gSyncParamsFields); 680} 681 682static void 683android_media_MediaPlayer2_seekTo(JNIEnv *env, jobject thiz, jlong msec, jint mode) 684{ 685 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 686 if (mp == NULL ) { 687 jniThrowException(env, "java/lang/IllegalStateException", NULL); 688 return; 689 } 690 ALOGV("seekTo: %lld(msec), mode=%d", (long long)msec, mode); 691 process_media_player_call( env, thiz, mp->seekTo((int)msec, (MediaPlayer2SeekMode)mode), NULL, NULL ); 692} 693 694static void 695android_media_MediaPlayer2_notifyAt(JNIEnv *env, jobject thiz, jlong mediaTimeUs) 696{ 697 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 698 if (mp == NULL) { 699 jniThrowException(env, "java/lang/IllegalStateException", NULL); 700 return; 701 } 702 ALOGV("notifyAt: %lld", (long long)mediaTimeUs); 703 process_media_player_call( env, thiz, mp->notifyAt((int64_t)mediaTimeUs), NULL, NULL ); 704} 705 706static jint 707android_media_MediaPlayer2_getVideoWidth(JNIEnv *env, jobject thiz) 708{ 709 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 710 if (mp == NULL ) { 711 jniThrowException(env, "java/lang/IllegalStateException", NULL); 712 return 0; 713 } 714 int w; 715 if (0 != mp->getVideoWidth(&w)) { 716 ALOGE("getVideoWidth failed"); 717 w = 0; 718 } 719 ALOGV("getVideoWidth: %d", w); 720 return (jint) w; 721} 722 723static jint 724android_media_MediaPlayer2_getVideoHeight(JNIEnv *env, jobject thiz) 725{ 726 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 727 if (mp == NULL ) { 728 jniThrowException(env, "java/lang/IllegalStateException", NULL); 729 return 0; 730 } 731 int h; 732 if (0 != mp->getVideoHeight(&h)) { 733 ALOGE("getVideoHeight failed"); 734 h = 0; 735 } 736 ALOGV("getVideoHeight: %d", h); 737 return (jint) h; 738} 739 740static jobject 741android_media_MediaPlayer2_native_getMetrics(JNIEnv *env, jobject thiz) 742{ 743 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 744 if (mp == NULL ) { 745 jniThrowException(env, "java/lang/IllegalStateException", NULL); 746 return 0; 747 } 748 749 Parcel p; 750 int key = FOURCC('m','t','r','X'); 751 status_t status = mp->getParameter(key, &p); 752 if (status != OK) { 753 ALOGD("getMetrics() failed: %d", status); 754 return (jobject) NULL; 755 } 756 757 p.setDataPosition(0); 758 MediaAnalyticsItem *item = new MediaAnalyticsItem; 759 item->readFromParcel(p); 760 jobject mybundle = MediaMetricsJNI::writeMetricsToBundle(env, item, NULL); 761 762 // housekeeping 763 delete item; 764 item = NULL; 765 766 return mybundle; 767} 768 769static jint 770android_media_MediaPlayer2_getCurrentPosition(JNIEnv *env, jobject thiz) 771{ 772 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 773 if (mp == NULL ) { 774 jniThrowException(env, "java/lang/IllegalStateException", NULL); 775 return 0; 776 } 777 int msec; 778 process_media_player_call( env, thiz, mp->getCurrentPosition(&msec), NULL, NULL ); 779 ALOGV("getCurrentPosition: %d (msec)", msec); 780 return (jint) msec; 781} 782 783static jint 784android_media_MediaPlayer2_getDuration(JNIEnv *env, jobject thiz) 785{ 786 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 787 if (mp == NULL ) { 788 jniThrowException(env, "java/lang/IllegalStateException", NULL); 789 return 0; 790 } 791 int msec; 792 process_media_player_call( env, thiz, mp->getDuration(&msec), NULL, NULL ); 793 ALOGV("getDuration: %d (msec)", msec); 794 return (jint) msec; 795} 796 797static void 798android_media_MediaPlayer2_reset(JNIEnv *env, jobject thiz) 799{ 800 ALOGV("reset"); 801 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 802 if (mp == NULL ) { 803 jniThrowException(env, "java/lang/IllegalStateException", NULL); 804 return; 805 } 806 process_media_player_call( env, thiz, mp->reset(), NULL, NULL ); 807} 808 809static jint 810android_media_MediaPlayer2_getAudioStreamType(JNIEnv *env, jobject thiz) 811{ 812 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 813 if (mp == NULL ) { 814 jniThrowException(env, "java/lang/IllegalStateException", NULL); 815 return 0; 816 } 817 audio_stream_type_t streamtype; 818 process_media_player_call( env, thiz, mp->getAudioStreamType(&streamtype), NULL, NULL ); 819 ALOGV("getAudioStreamType: %d (streamtype)", streamtype); 820 return (jint) streamtype; 821} 822 823static jboolean 824android_media_MediaPlayer2_setParameter(JNIEnv *env, jobject thiz, jint key, jobject java_request) 825{ 826 ALOGV("setParameter: key %d", key); 827 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 828 if (mp == NULL ) { 829 jniThrowException(env, "java/lang/IllegalStateException", NULL); 830 return false; 831 } 832 833 Parcel *request = parcelForJavaObject(env, java_request); 834 status_t err = mp->setParameter(key, *request); 835 if (err == OK) { 836 return true; 837 } else { 838 return false; 839 } 840} 841 842static void 843android_media_MediaPlayer2_setLooping(JNIEnv *env, jobject thiz, jboolean looping) 844{ 845 ALOGV("setLooping: %d", looping); 846 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 847 if (mp == NULL ) { 848 jniThrowException(env, "java/lang/IllegalStateException", NULL); 849 return; 850 } 851 process_media_player_call( env, thiz, mp->setLooping(looping), NULL, NULL ); 852} 853 854static jboolean 855android_media_MediaPlayer2_isLooping(JNIEnv *env, jobject thiz) 856{ 857 ALOGV("isLooping"); 858 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 859 if (mp == NULL ) { 860 jniThrowException(env, "java/lang/IllegalStateException", NULL); 861 return JNI_FALSE; 862 } 863 return mp->isLooping() ? JNI_TRUE : JNI_FALSE; 864} 865 866static void 867android_media_MediaPlayer2_setVolume(JNIEnv *env, jobject thiz, jfloat leftVolume, jfloat rightVolume) 868{ 869 ALOGV("setVolume: left %f right %f", (float) leftVolume, (float) rightVolume); 870 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 871 if (mp == NULL ) { 872 jniThrowException(env, "java/lang/IllegalStateException", NULL); 873 return; 874 } 875 process_media_player_call( env, thiz, mp->setVolume((float) leftVolume, (float) rightVolume), NULL, NULL ); 876} 877 878// Sends the request and reply parcels to the media player via the 879// binder interface. 880static jint 881android_media_MediaPlayer2_invoke(JNIEnv *env, jobject thiz, 882 jobject java_request, jobject java_reply) 883{ 884 sp<MediaPlayer2> media_player = getMediaPlayer(env, thiz); 885 if (media_player == NULL ) { 886 jniThrowException(env, "java/lang/IllegalStateException", NULL); 887 return UNKNOWN_ERROR; 888 } 889 890 Parcel *request = parcelForJavaObject(env, java_request); 891 Parcel *reply = parcelForJavaObject(env, java_reply); 892 893 request->setDataPosition(0); 894 895 // Don't use process_media_player_call which use the async loop to 896 // report errors, instead returns the status. 897 return (jint) media_player->invoke(*request, reply); 898} 899 900// Sends the new filter to the client. 901static jint 902android_media_MediaPlayer2_setMetadataFilter(JNIEnv *env, jobject thiz, jobject request) 903{ 904 sp<MediaPlayer2> media_player = getMediaPlayer(env, thiz); 905 if (media_player == NULL ) { 906 jniThrowException(env, "java/lang/IllegalStateException", NULL); 907 return UNKNOWN_ERROR; 908 } 909 910 Parcel *filter = parcelForJavaObject(env, request); 911 912 if (filter == NULL ) { 913 jniThrowException(env, "java/lang/RuntimeException", "Filter is null"); 914 return UNKNOWN_ERROR; 915 } 916 917 return (jint) media_player->setMetadataFilter(*filter); 918} 919 920static jboolean 921android_media_MediaPlayer2_getMetadata(JNIEnv *env, jobject thiz, jboolean update_only, 922 jboolean apply_filter, jobject reply) 923{ 924 sp<MediaPlayer2> media_player = getMediaPlayer(env, thiz); 925 if (media_player == NULL ) { 926 jniThrowException(env, "java/lang/IllegalStateException", NULL); 927 return JNI_FALSE; 928 } 929 930 Parcel *metadata = parcelForJavaObject(env, reply); 931 932 if (metadata == NULL ) { 933 jniThrowException(env, "java/lang/RuntimeException", "Reply parcel is null"); 934 return JNI_FALSE; 935 } 936 937 metadata->freeData(); 938 // On return metadata is positioned at the beginning of the 939 // metadata. Note however that the parcel actually starts with the 940 // return code so you should not rewind the parcel using 941 // setDataPosition(0). 942 if (media_player->getMetadata(update_only, apply_filter, metadata) == OK) { 943 return JNI_TRUE; 944 } else { 945 return JNI_FALSE; 946 } 947} 948 949// This function gets some field IDs, which in turn causes class initialization. 950// It is called from a static block in MediaPlayer2, which won't run until the 951// first time an instance of this class is used. 952static void 953android_media_MediaPlayer2_native_init(JNIEnv *env) 954{ 955 jclass clazz; 956 957 clazz = env->FindClass("android/media/MediaPlayer2Impl"); 958 if (clazz == NULL) { 959 return; 960 } 961 962 fields.context = env->GetFieldID(clazz, "mNativeContext", "J"); 963 if (fields.context == NULL) { 964 return; 965 } 966 967 fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", 968 "(Ljava/lang/Object;IIILjava/lang/Object;)V"); 969 if (fields.post_event == NULL) { 970 return; 971 } 972 973 fields.surface_texture = env->GetFieldID(clazz, "mNativeSurfaceTexture", "J"); 974 if (fields.surface_texture == NULL) { 975 return; 976 } 977 978 env->DeleteLocalRef(clazz); 979 980 clazz = env->FindClass("android/net/ProxyInfo"); 981 if (clazz == NULL) { 982 return; 983 } 984 985 fields.proxyConfigGetHost = 986 env->GetMethodID(clazz, "getHost", "()Ljava/lang/String;"); 987 988 fields.proxyConfigGetPort = 989 env->GetMethodID(clazz, "getPort", "()I"); 990 991 fields.proxyConfigGetExclusionList = 992 env->GetMethodID(clazz, "getExclusionListAsString", "()Ljava/lang/String;"); 993 994 env->DeleteLocalRef(clazz); 995 996 gBufferingParamsFields.init(env); 997 998 // Modular DRM 999 FIND_CLASS(clazz, "android/media/MediaDrm$MediaDrmStateException"); 1000 if (clazz) { 1001 GET_METHOD_ID(gStateExceptionFields.init, clazz, "<init>", "(ILjava/lang/String;)V"); 1002 gStateExceptionFields.classId = static_cast<jclass>(env->NewGlobalRef(clazz)); 1003 1004 env->DeleteLocalRef(clazz); 1005 } else { 1006 ALOGE("JNI android_media_MediaPlayer2_native_init couldn't " 1007 "get clazz android/media/MediaDrm$MediaDrmStateException"); 1008 } 1009 1010 gPlaybackParamsFields.init(env); 1011 gSyncParamsFields.init(env); 1012 gVolumeShaperFields.init(env); 1013} 1014 1015static void 1016android_media_MediaPlayer2_native_setup(JNIEnv *env, jobject thiz, jobject weak_this) 1017{ 1018 ALOGV("native_setup"); 1019 sp<MediaPlayer2> mp = new MediaPlayer2(); 1020 if (mp == NULL) { 1021 jniThrowException(env, "java/lang/RuntimeException", "Out of memory"); 1022 return; 1023 } 1024 1025 // create new listener and give it to MediaPlayer2 1026 sp<JNIMediaPlayer2Listener> listener = new JNIMediaPlayer2Listener(env, thiz, weak_this); 1027 mp->setListener(listener); 1028 1029 // Stow our new C++ MediaPlayer2 in an opaque field in the Java object. 1030 setMediaPlayer(env, thiz, mp); 1031} 1032 1033static void 1034android_media_MediaPlayer2_release(JNIEnv *env, jobject thiz) 1035{ 1036 ALOGV("release"); 1037 decVideoSurfaceRef(env, thiz); 1038 sp<MediaPlayer2> mp = setMediaPlayer(env, thiz, 0); 1039 if (mp != NULL) { 1040 // this prevents native callbacks after the object is released 1041 mp->setListener(0); 1042 mp->disconnect(); 1043 } 1044} 1045 1046static void 1047android_media_MediaPlayer2_native_finalize(JNIEnv *env, jobject thiz) 1048{ 1049 ALOGV("native_finalize"); 1050 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1051 if (mp != NULL) { 1052 ALOGW("MediaPlayer2 finalized without being released"); 1053 } 1054 android_media_MediaPlayer2_release(env, thiz); 1055} 1056 1057static void android_media_MediaPlayer2_set_audio_session_id(JNIEnv *env, jobject thiz, 1058 jint sessionId) { 1059 ALOGV("set_session_id(): %d", sessionId); 1060 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1061 if (mp == NULL ) { 1062 jniThrowException(env, "java/lang/IllegalStateException", NULL); 1063 return; 1064 } 1065 process_media_player_call( env, thiz, mp->setAudioSessionId((audio_session_t) sessionId), NULL, 1066 NULL); 1067} 1068 1069static jint android_media_MediaPlayer2_get_audio_session_id(JNIEnv *env, jobject thiz) { 1070 ALOGV("get_session_id()"); 1071 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1072 if (mp == NULL ) { 1073 jniThrowException(env, "java/lang/IllegalStateException", NULL); 1074 return 0; 1075 } 1076 1077 return (jint) mp->getAudioSessionId(); 1078} 1079 1080static void 1081android_media_MediaPlayer2_setAuxEffectSendLevel(JNIEnv *env, jobject thiz, jfloat level) 1082{ 1083 ALOGV("setAuxEffectSendLevel: level %f", level); 1084 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1085 if (mp == NULL ) { 1086 jniThrowException(env, "java/lang/IllegalStateException", NULL); 1087 return; 1088 } 1089 process_media_player_call( env, thiz, mp->setAuxEffectSendLevel(level), NULL, NULL ); 1090} 1091 1092static void android_media_MediaPlayer2_attachAuxEffect(JNIEnv *env, jobject thiz, jint effectId) { 1093 ALOGV("attachAuxEffect(): %d", effectId); 1094 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1095 if (mp == NULL ) { 1096 jniThrowException(env, "java/lang/IllegalStateException", NULL); 1097 return; 1098 } 1099 process_media_player_call( env, thiz, mp->attachAuxEffect(effectId), NULL, NULL ); 1100} 1101 1102static jint 1103android_media_MediaPlayer2_setRetransmitEndpoint(JNIEnv *env, jobject thiz, 1104 jstring addrString, jint port) { 1105 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1106 if (mp == NULL ) { 1107 jniThrowException(env, "java/lang/IllegalStateException", NULL); 1108 return INVALID_OPERATION; 1109 } 1110 1111 const char *cAddrString = NULL; 1112 1113 if (NULL != addrString) { 1114 cAddrString = env->GetStringUTFChars(addrString, NULL); 1115 if (cAddrString == NULL) { // Out of memory 1116 return NO_MEMORY; 1117 } 1118 } 1119 ALOGV("setRetransmitEndpoint: %s:%d", 1120 cAddrString ? cAddrString : "(null)", port); 1121 1122 status_t ret; 1123 if (cAddrString && (port > 0xFFFF)) { 1124 ret = BAD_VALUE; 1125 } else { 1126 ret = mp->setRetransmitEndpoint(cAddrString, 1127 static_cast<uint16_t>(port)); 1128 } 1129 1130 if (NULL != addrString) { 1131 env->ReleaseStringUTFChars(addrString, cAddrString); 1132 } 1133 1134 if (ret == INVALID_OPERATION ) { 1135 jniThrowException(env, "java/lang/IllegalStateException", NULL); 1136 } 1137 1138 return (jint) ret; 1139} 1140 1141static void 1142android_media_MediaPlayer2_setNextMediaPlayer(JNIEnv *env, jobject thiz, jobject java_player) 1143{ 1144 ALOGV("setNextMediaPlayer"); 1145 sp<MediaPlayer2> thisplayer = getMediaPlayer(env, thiz); 1146 if (thisplayer == NULL) { 1147 jniThrowException(env, "java/lang/IllegalStateException", "This player not initialized"); 1148 return; 1149 } 1150 sp<MediaPlayer2> nextplayer = (java_player == NULL) ? NULL : getMediaPlayer(env, java_player); 1151 if (nextplayer == NULL && java_player != NULL) { 1152 jniThrowException(env, "java/lang/IllegalStateException", "That player not initialized"); 1153 return; 1154 } 1155 1156 if (nextplayer == thisplayer) { 1157 jniThrowException(env, "java/lang/IllegalArgumentException", "Next player can't be self"); 1158 return; 1159 } 1160 // tie the two players together 1161 process_media_player_call( 1162 env, thiz, thisplayer->setNextMediaPlayer(nextplayer), 1163 "java/lang/IllegalArgumentException", 1164 "setNextMediaPlayer failed." ); 1165 ; 1166} 1167 1168// Pass through the arguments to the MediaServer player implementation. 1169static jint android_media_MediaPlayer2_applyVolumeShaper(JNIEnv *env, jobject thiz, 1170 jobject jconfig, jobject joperation) { 1171 // NOTE: hard code here to prevent platform issues. Must match VolumeShaper.java 1172 const int VOLUME_SHAPER_INVALID_OPERATION = -38; 1173 1174 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1175 if (mp == nullptr) { 1176 return (jint)VOLUME_SHAPER_INVALID_OPERATION; 1177 } 1178 1179 sp<VolumeShaper::Configuration> configuration; 1180 sp<VolumeShaper::Operation> operation; 1181 if (jconfig != nullptr) { 1182 configuration = VolumeShaperHelper::convertJobjectToConfiguration( 1183 env, gVolumeShaperFields, jconfig); 1184 ALOGV("applyVolumeShaper configuration: %s", configuration->toString().c_str()); 1185 } 1186 if (joperation != nullptr) { 1187 operation = VolumeShaperHelper::convertJobjectToOperation( 1188 env, gVolumeShaperFields, joperation); 1189 ALOGV("applyVolumeShaper operation: %s", operation->toString().c_str()); 1190 } 1191 VolumeShaper::Status status = mp->applyVolumeShaper(configuration, operation); 1192 if (status == INVALID_OPERATION) { 1193 status = VOLUME_SHAPER_INVALID_OPERATION; 1194 } 1195 return (jint)status; // if status < 0 an error, else a VolumeShaper id 1196} 1197 1198// Pass through the arguments to the MediaServer player implementation. 1199static jobject android_media_MediaPlayer2_getVolumeShaperState(JNIEnv *env, jobject thiz, 1200 jint id) { 1201 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1202 if (mp == nullptr) { 1203 return (jobject)nullptr; 1204 } 1205 1206 sp<VolumeShaper::State> state = mp->getVolumeShaperState((int)id); 1207 if (state.get() == nullptr) { 1208 return (jobject)nullptr; 1209 } 1210 return VolumeShaperHelper::convertStateToJobject(env, gVolumeShaperFields, state); 1211} 1212 1213///////////////////////////////////////////////////////////////////////////////////// 1214// Modular DRM begin 1215 1216// TODO: investigate if these can be shared with their MediaDrm counterparts 1217static void throwDrmStateException(JNIEnv *env, const char *msg, status_t err) 1218{ 1219 ALOGE("Illegal DRM state exception: %s (%d)", msg, err); 1220 1221 jobject exception = env->NewObject(gStateExceptionFields.classId, 1222 gStateExceptionFields.init, static_cast<int>(err), 1223 env->NewStringUTF(msg)); 1224 env->Throw(static_cast<jthrowable>(exception)); 1225} 1226 1227// TODO: investigate if these can be shared with their MediaDrm counterparts 1228static bool throwDrmExceptionAsNecessary(JNIEnv *env, status_t err, const char *msg = NULL) 1229{ 1230 const char *drmMessage = "Unknown DRM Msg"; 1231 1232 switch (err) { 1233 case ERROR_DRM_UNKNOWN: 1234 drmMessage = "General DRM error"; 1235 break; 1236 case ERROR_DRM_NO_LICENSE: 1237 drmMessage = "No license"; 1238 break; 1239 case ERROR_DRM_LICENSE_EXPIRED: 1240 drmMessage = "License expired"; 1241 break; 1242 case ERROR_DRM_SESSION_NOT_OPENED: 1243 drmMessage = "Session not opened"; 1244 break; 1245 case ERROR_DRM_DECRYPT_UNIT_NOT_INITIALIZED: 1246 drmMessage = "Not initialized"; 1247 break; 1248 case ERROR_DRM_DECRYPT: 1249 drmMessage = "Decrypt error"; 1250 break; 1251 case ERROR_DRM_CANNOT_HANDLE: 1252 drmMessage = "Unsupported scheme or data format"; 1253 break; 1254 case ERROR_DRM_TAMPER_DETECTED: 1255 drmMessage = "Invalid state"; 1256 break; 1257 default: 1258 break; 1259 } 1260 1261 String8 vendorMessage; 1262 if (err >= ERROR_DRM_VENDOR_MIN && err <= ERROR_DRM_VENDOR_MAX) { 1263 vendorMessage = String8::format("DRM vendor-defined error: %d", err); 1264 drmMessage = vendorMessage.string(); 1265 } 1266 1267 if (err == BAD_VALUE) { 1268 jniThrowException(env, "java/lang/IllegalArgumentException", msg); 1269 return true; 1270 } else if (err == ERROR_DRM_NOT_PROVISIONED) { 1271 jniThrowException(env, "android/media/NotProvisionedException", msg); 1272 return true; 1273 } else if (err == ERROR_DRM_RESOURCE_BUSY) { 1274 jniThrowException(env, "android/media/ResourceBusyException", msg); 1275 return true; 1276 } else if (err == ERROR_DRM_DEVICE_REVOKED) { 1277 jniThrowException(env, "android/media/DeniedByServerException", msg); 1278 return true; 1279 } else if (err == DEAD_OBJECT) { 1280 jniThrowException(env, "android/media/MediaDrmResetException", 1281 "mediaserver died"); 1282 return true; 1283 } else if (err != OK) { 1284 String8 errbuf; 1285 if (drmMessage != NULL) { 1286 if (msg == NULL) { 1287 msg = drmMessage; 1288 } else { 1289 errbuf = String8::format("%s: %s", msg, drmMessage); 1290 msg = errbuf.string(); 1291 } 1292 } 1293 throwDrmStateException(env, msg, err); 1294 return true; 1295 } 1296 return false; 1297} 1298 1299static Vector<uint8_t> JByteArrayToVector(JNIEnv *env, jbyteArray const &byteArray) 1300{ 1301 Vector<uint8_t> vector; 1302 size_t length = env->GetArrayLength(byteArray); 1303 vector.insertAt((size_t)0, length); 1304 env->GetByteArrayRegion(byteArray, 0, length, (jbyte *)vector.editArray()); 1305 return vector; 1306} 1307 1308static void android_media_MediaPlayer2_prepareDrm(JNIEnv *env, jobject thiz, 1309 jbyteArray uuidObj, jbyteArray drmSessionIdObj) 1310{ 1311 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1312 if (mp == NULL) { 1313 jniThrowException(env, "java/lang/IllegalStateException", NULL); 1314 return; 1315 } 1316 1317 if (uuidObj == NULL) { 1318 jniThrowException(env, "java/lang/IllegalArgumentException", NULL); 1319 return; 1320 } 1321 1322 Vector<uint8_t> uuid = JByteArrayToVector(env, uuidObj); 1323 1324 if (uuid.size() != 16) { 1325 jniThrowException( 1326 env, 1327 "java/lang/IllegalArgumentException", 1328 "invalid UUID size, expected 16 bytes"); 1329 return; 1330 } 1331 1332 Vector<uint8_t> drmSessionId = JByteArrayToVector(env, drmSessionIdObj); 1333 1334 if (drmSessionId.size() == 0) { 1335 jniThrowException( 1336 env, 1337 "java/lang/IllegalArgumentException", 1338 "empty drmSessionId"); 1339 return; 1340 } 1341 1342 status_t err = mp->prepareDrm(uuid.array(), drmSessionId); 1343 if (err != OK) { 1344 if (err == INVALID_OPERATION) { 1345 jniThrowException( 1346 env, 1347 "java/lang/IllegalStateException", 1348 "The player must be in prepared state."); 1349 } else if (err == ERROR_DRM_CANNOT_HANDLE) { 1350 jniThrowException( 1351 env, 1352 "android/media/UnsupportedSchemeException", 1353 "Failed to instantiate drm object."); 1354 } else { 1355 throwDrmExceptionAsNecessary(env, err, "Failed to prepare DRM scheme"); 1356 } 1357 } 1358} 1359 1360static void android_media_MediaPlayer2_releaseDrm(JNIEnv *env, jobject thiz) 1361{ 1362 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1363 if (mp == NULL ) { 1364 jniThrowException(env, "java/lang/IllegalStateException", NULL); 1365 return; 1366 } 1367 1368 status_t err = mp->releaseDrm(); 1369 if (err != OK) { 1370 if (err == INVALID_OPERATION) { 1371 jniThrowException( 1372 env, 1373 "java/lang/IllegalStateException", 1374 "Can not release DRM in an active player state."); 1375 } 1376 } 1377} 1378// Modular DRM end 1379// ---------------------------------------------------------------------------- 1380 1381///////////////////////////////////////////////////////////////////////////////////// 1382// AudioRouting begin 1383static jboolean android_media_MediaPlayer2_setOutputDevice(JNIEnv *env, jobject thiz, jint device_id) 1384{ 1385 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1386 if (mp == NULL) { 1387 return false; 1388 } 1389 return mp->setOutputDevice(device_id) == NO_ERROR; 1390} 1391 1392static jint android_media_MediaPlayer2_getRoutedDeviceId(JNIEnv *env, jobject thiz) 1393{ 1394 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1395 if (mp == NULL) { 1396 return AUDIO_PORT_HANDLE_NONE; 1397 } 1398 return mp->getRoutedDeviceId(); 1399} 1400 1401static void android_media_MediaPlayer2_enableDeviceCallback( 1402 JNIEnv* env, jobject thiz, jboolean enabled) 1403{ 1404 sp<MediaPlayer2> mp = getMediaPlayer(env, thiz); 1405 if (mp == NULL) { 1406 return; 1407 } 1408 1409 status_t status = mp->enableAudioDeviceCallback(enabled); 1410 if (status != NO_ERROR) { 1411 jniThrowException(env, "java/lang/IllegalStateException", NULL); 1412 ALOGE("enable device callback failed: %d", status); 1413 } 1414} 1415 1416// AudioRouting end 1417// ---------------------------------------------------------------------------- 1418 1419static const JNINativeMethod gMethods[] = { 1420 { 1421 "nativeSetDataSource", 1422 "(Landroid/media/Media2HTTPService;Ljava/lang/String;[Ljava/lang/String;" 1423 "[Ljava/lang/String;)V", 1424 (void *)android_media_MediaPlayer2_setDataSourceAndHeaders 1425 }, 1426 1427 {"_setDataSource", "(Ljava/io/FileDescriptor;JJ)V", (void *)android_media_MediaPlayer2_setDataSourceFD}, 1428 {"_setDataSource", "(Landroid/media/Media2DataSource;)V",(void *)android_media_MediaPlayer2_setDataSourceCallback }, 1429 {"_setVideoSurface", "(Landroid/view/Surface;)V", (void *)android_media_MediaPlayer2_setVideoSurface}, 1430 {"getBufferingParams", "()Landroid/media/BufferingParams;", (void *)android_media_MediaPlayer2_getBufferingParams}, 1431 {"setBufferingParams", "(Landroid/media/BufferingParams;)V", (void *)android_media_MediaPlayer2_setBufferingParams}, 1432 {"_prepare", "()V", (void *)android_media_MediaPlayer2_prepare}, 1433 {"prepareAsync", "()V", (void *)android_media_MediaPlayer2_prepareAsync}, 1434 {"_start", "()V", (void *)android_media_MediaPlayer2_start}, 1435 {"_stop", "()V", (void *)android_media_MediaPlayer2_stop}, 1436 {"getVideoWidth", "()I", (void *)android_media_MediaPlayer2_getVideoWidth}, 1437 {"getVideoHeight", "()I", (void *)android_media_MediaPlayer2_getVideoHeight}, 1438 {"native_getMetrics", "()Landroid/os/PersistableBundle;", (void *)android_media_MediaPlayer2_native_getMetrics}, 1439 {"setPlaybackParams", "(Landroid/media/PlaybackParams;)V", (void *)android_media_MediaPlayer2_setPlaybackParams}, 1440 {"getPlaybackParams", "()Landroid/media/PlaybackParams;", (void *)android_media_MediaPlayer2_getPlaybackParams}, 1441 {"setSyncParams", "(Landroid/media/SyncParams;)V", (void *)android_media_MediaPlayer2_setSyncParams}, 1442 {"getSyncParams", "()Landroid/media/SyncParams;", (void *)android_media_MediaPlayer2_getSyncParams}, 1443 {"_seekTo", "(JI)V", (void *)android_media_MediaPlayer2_seekTo}, 1444 {"_notifyAt", "(J)V", (void *)android_media_MediaPlayer2_notifyAt}, 1445 {"_pause", "()V", (void *)android_media_MediaPlayer2_pause}, 1446 {"isPlaying", "()Z", (void *)android_media_MediaPlayer2_isPlaying}, 1447 {"getCurrentPosition", "()I", (void *)android_media_MediaPlayer2_getCurrentPosition}, 1448 {"getDuration", "()I", (void *)android_media_MediaPlayer2_getDuration}, 1449 {"_release", "()V", (void *)android_media_MediaPlayer2_release}, 1450 {"_reset", "()V", (void *)android_media_MediaPlayer2_reset}, 1451 {"_getAudioStreamType", "()I", (void *)android_media_MediaPlayer2_getAudioStreamType}, 1452 {"setParameter", "(ILandroid/os/Parcel;)Z", (void *)android_media_MediaPlayer2_setParameter}, 1453 {"setLooping", "(Z)V", (void *)android_media_MediaPlayer2_setLooping}, 1454 {"isLooping", "()Z", (void *)android_media_MediaPlayer2_isLooping}, 1455 {"_setVolume", "(FF)V", (void *)android_media_MediaPlayer2_setVolume}, 1456 {"native_invoke", "(Landroid/os/Parcel;Landroid/os/Parcel;)I",(void *)android_media_MediaPlayer2_invoke}, 1457 {"native_setMetadataFilter", "(Landroid/os/Parcel;)I", (void *)android_media_MediaPlayer2_setMetadataFilter}, 1458 {"native_getMetadata", "(ZZLandroid/os/Parcel;)Z", (void *)android_media_MediaPlayer2_getMetadata}, 1459 {"native_init", "()V", (void *)android_media_MediaPlayer2_native_init}, 1460 {"native_setup", "(Ljava/lang/Object;)V", (void *)android_media_MediaPlayer2_native_setup}, 1461 {"native_finalize", "()V", (void *)android_media_MediaPlayer2_native_finalize}, 1462 {"getAudioSessionId", "()I", (void *)android_media_MediaPlayer2_get_audio_session_id}, 1463 {"setAudioSessionId", "(I)V", (void *)android_media_MediaPlayer2_set_audio_session_id}, 1464 {"_setAuxEffectSendLevel", "(F)V", (void *)android_media_MediaPlayer2_setAuxEffectSendLevel}, 1465 {"attachAuxEffect", "(I)V", (void *)android_media_MediaPlayer2_attachAuxEffect}, 1466 {"native_setRetransmitEndpoint", "(Ljava/lang/String;I)I", (void *)android_media_MediaPlayer2_setRetransmitEndpoint}, 1467 {"setNextMediaPlayer", "(Landroid/media/MediaPlayer2;)V", (void *)android_media_MediaPlayer2_setNextMediaPlayer}, 1468 {"native_applyVolumeShaper", 1469 "(Landroid/media/VolumeShaper$Configuration;Landroid/media/VolumeShaper$Operation;)I", 1470 (void *)android_media_MediaPlayer2_applyVolumeShaper}, 1471 {"native_getVolumeShaperState", 1472 "(I)Landroid/media/VolumeShaper$State;", 1473 (void *)android_media_MediaPlayer2_getVolumeShaperState}, 1474 // Modular DRM 1475 { "_prepareDrm", "([B[B)V", (void *)android_media_MediaPlayer2_prepareDrm }, 1476 { "_releaseDrm", "()V", (void *)android_media_MediaPlayer2_releaseDrm }, 1477 1478 // AudioRouting 1479 {"native_setOutputDevice", "(I)Z", (void *)android_media_MediaPlayer2_setOutputDevice}, 1480 {"native_getRoutedDeviceId", "()I", (void *)android_media_MediaPlayer2_getRoutedDeviceId}, 1481 {"native_enableDeviceCallback", "(Z)V", (void *)android_media_MediaPlayer2_enableDeviceCallback}, 1482}; 1483 1484// This function only registers the native methods 1485static int register_android_media_MediaPlayer2Impl(JNIEnv *env) 1486{ 1487 return AndroidRuntime::registerNativeMethods(env, 1488 "android/media/MediaPlayer2Impl", gMethods, NELEM(gMethods)); 1489} 1490 1491jint JNI_OnLoad(JavaVM* vm, void* /* reserved */) 1492{ 1493 JNIEnv* env = NULL; 1494 jint result = -1; 1495 1496 if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) { 1497 ALOGE("ERROR: GetEnv failed\n"); 1498 goto bail; 1499 } 1500 assert(env != NULL); 1501 1502 if (register_android_media_MediaPlayer2Impl(env) < 0) { 1503 ALOGE("ERROR: MediaPlayer2 native registration failed\n"); 1504 goto bail; 1505 } 1506 1507 /* success -- return valid version number */ 1508 result = JNI_VERSION_1_4; 1509 1510bail: 1511 return result; 1512} 1513 1514// KTHXBYE 1515