mediaplayer.cpp revision d9d7fa0873796ac661c44a7fcd6ad5ff697ff01f
1/* 2** 3** Copyright 2006, 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" 20 21#include <fcntl.h> 22#include <inttypes.h> 23#include <sys/stat.h> 24#include <sys/types.h> 25#include <unistd.h> 26 27#include <utils/Log.h> 28 29#include <binder/IServiceManager.h> 30#include <binder/IPCThreadState.h> 31 32#include <gui/Surface.h> 33 34#include <media/mediaplayer.h> 35#include <media/AudioSystem.h> 36 37#include <binder/MemoryBase.h> 38 39#include <utils/KeyedVector.h> 40#include <utils/String8.h> 41 42#include <system/audio.h> 43#include <system/window.h> 44 45namespace android { 46 47MediaPlayer::MediaPlayer() 48{ 49 ALOGV("constructor"); 50 mListener = NULL; 51 mCookie = NULL; 52 mStreamType = AUDIO_STREAM_MUSIC; 53 mCurrentPosition = -1; 54 mSeekPosition = -1; 55 mCurrentState = MEDIA_PLAYER_IDLE; 56 mPrepareSync = false; 57 mPrepareStatus = NO_ERROR; 58 mLoop = false; 59 mLeftVolume = mRightVolume = 1.0; 60 mVideoWidth = mVideoHeight = 0; 61 mLockThreadId = 0; 62 mAudioSessionId = AudioSystem::newAudioSessionId(); 63 AudioSystem::acquireAudioSessionId(mAudioSessionId, -1); 64 mSendLevel = 0; 65 mRetransmitEndpointValid = false; 66} 67 68MediaPlayer::~MediaPlayer() 69{ 70 ALOGV("destructor"); 71 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1); 72 disconnect(); 73 IPCThreadState::self()->flushCommands(); 74} 75 76void MediaPlayer::disconnect() 77{ 78 ALOGV("disconnect"); 79 sp<IMediaPlayer> p; 80 { 81 Mutex::Autolock _l(mLock); 82 p = mPlayer; 83 mPlayer.clear(); 84 } 85 86 if (p != 0) { 87 p->disconnect(); 88 } 89} 90 91// always call with lock held 92void MediaPlayer::clear_l() 93{ 94 mCurrentPosition = -1; 95 mSeekPosition = -1; 96 mVideoWidth = mVideoHeight = 0; 97 mRetransmitEndpointValid = false; 98} 99 100status_t MediaPlayer::setListener(const sp<MediaPlayerListener>& listener) 101{ 102 ALOGV("setListener"); 103 Mutex::Autolock _l(mLock); 104 mListener = listener; 105 return NO_ERROR; 106} 107 108 109status_t MediaPlayer::attachNewPlayer(const sp<IMediaPlayer>& player) 110{ 111 status_t err = UNKNOWN_ERROR; 112 sp<IMediaPlayer> p; 113 { // scope for the lock 114 Mutex::Autolock _l(mLock); 115 116 if ( !( (mCurrentState & MEDIA_PLAYER_IDLE) || 117 (mCurrentState == MEDIA_PLAYER_STATE_ERROR ) ) ) { 118 ALOGE("attachNewPlayer called in state %d", mCurrentState); 119 return INVALID_OPERATION; 120 } 121 122 clear_l(); 123 p = mPlayer; 124 mPlayer = player; 125 if (player != 0) { 126 mCurrentState = MEDIA_PLAYER_INITIALIZED; 127 err = NO_ERROR; 128 } else { 129 ALOGE("Unable to create media player"); 130 } 131 } 132 133 if (p != 0) { 134 p->disconnect(); 135 } 136 137 return err; 138} 139 140status_t MediaPlayer::setDataSource( 141 const sp<IMediaHTTPService> &httpService, 142 const char *url, const KeyedVector<String8, String8> *headers) 143{ 144 ALOGV("setDataSource(%s)", url); 145 status_t err = BAD_VALUE; 146 if (url != NULL) { 147 const sp<IMediaPlayerService>& service(getMediaPlayerService()); 148 if (service != 0) { 149 sp<IMediaPlayer> player(service->create(this, mAudioSessionId)); 150 if ((NO_ERROR != doSetRetransmitEndpoint(player)) || 151 (NO_ERROR != player->setDataSource(httpService, url, headers))) { 152 player.clear(); 153 } 154 err = attachNewPlayer(player); 155 } 156 } 157 return err; 158} 159 160status_t MediaPlayer::setDataSource(int fd, int64_t offset, int64_t length) 161{ 162 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length); 163 status_t err = UNKNOWN_ERROR; 164 const sp<IMediaPlayerService>& service(getMediaPlayerService()); 165 if (service != 0) { 166 sp<IMediaPlayer> player(service->create(this, mAudioSessionId)); 167 if ((NO_ERROR != doSetRetransmitEndpoint(player)) || 168 (NO_ERROR != player->setDataSource(fd, offset, length))) { 169 player.clear(); 170 } 171 err = attachNewPlayer(player); 172 } 173 return err; 174} 175 176status_t MediaPlayer::setDataSource(const sp<IStreamSource> &source) 177{ 178 ALOGV("setDataSource"); 179 status_t err = UNKNOWN_ERROR; 180 const sp<IMediaPlayerService>& service(getMediaPlayerService()); 181 if (service != 0) { 182 sp<IMediaPlayer> player(service->create(this, mAudioSessionId)); 183 if ((NO_ERROR != doSetRetransmitEndpoint(player)) || 184 (NO_ERROR != player->setDataSource(source))) { 185 player.clear(); 186 } 187 err = attachNewPlayer(player); 188 } 189 return err; 190} 191 192status_t MediaPlayer::invoke(const Parcel& request, Parcel *reply) 193{ 194 Mutex::Autolock _l(mLock); 195 const bool hasBeenInitialized = 196 (mCurrentState != MEDIA_PLAYER_STATE_ERROR) && 197 ((mCurrentState & MEDIA_PLAYER_IDLE) != MEDIA_PLAYER_IDLE); 198 if ((mPlayer != NULL) && hasBeenInitialized) { 199 ALOGV("invoke %zu", request.dataSize()); 200 return mPlayer->invoke(request, reply); 201 } 202 ALOGE("invoke failed: wrong state %X", mCurrentState); 203 return INVALID_OPERATION; 204} 205 206status_t MediaPlayer::setMetadataFilter(const Parcel& filter) 207{ 208 ALOGD("setMetadataFilter"); 209 Mutex::Autolock lock(mLock); 210 if (mPlayer == NULL) { 211 return NO_INIT; 212 } 213 return mPlayer->setMetadataFilter(filter); 214} 215 216status_t MediaPlayer::getMetadata(bool update_only, bool apply_filter, Parcel *metadata) 217{ 218 ALOGD("getMetadata"); 219 Mutex::Autolock lock(mLock); 220 if (mPlayer == NULL) { 221 return NO_INIT; 222 } 223 return mPlayer->getMetadata(update_only, apply_filter, metadata); 224} 225 226status_t MediaPlayer::setVideoSurfaceTexture( 227 const sp<IGraphicBufferProducer>& bufferProducer) 228{ 229 ALOGV("setVideoSurfaceTexture"); 230 Mutex::Autolock _l(mLock); 231 if (mPlayer == 0) return NO_INIT; 232 return mPlayer->setVideoSurfaceTexture(bufferProducer); 233} 234 235// must call with lock held 236status_t MediaPlayer::prepareAsync_l() 237{ 238 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_INITIALIZED | MEDIA_PLAYER_STOPPED) ) ) { 239 mPlayer->setAudioStreamType(mStreamType); 240 mCurrentState = MEDIA_PLAYER_PREPARING; 241 return mPlayer->prepareAsync(); 242 } 243 ALOGE("prepareAsync called in state %d", mCurrentState); 244 return INVALID_OPERATION; 245} 246 247// TODO: In case of error, prepareAsync provides the caller with 2 error codes, 248// one defined in the Android framework and one provided by the implementation 249// that generated the error. The sync version of prepare returns only 1 error 250// code. 251status_t MediaPlayer::prepare() 252{ 253 ALOGV("prepare"); 254 Mutex::Autolock _l(mLock); 255 mLockThreadId = getThreadId(); 256 if (mPrepareSync) { 257 mLockThreadId = 0; 258 return -EALREADY; 259 } 260 mPrepareSync = true; 261 status_t ret = prepareAsync_l(); 262 if (ret != NO_ERROR) { 263 mLockThreadId = 0; 264 return ret; 265 } 266 267 if (mPrepareSync) { 268 mSignal.wait(mLock); // wait for prepare done 269 mPrepareSync = false; 270 } 271 ALOGV("prepare complete - status=%d", mPrepareStatus); 272 mLockThreadId = 0; 273 return mPrepareStatus; 274} 275 276status_t MediaPlayer::prepareAsync() 277{ 278 ALOGV("prepareAsync"); 279 Mutex::Autolock _l(mLock); 280 return prepareAsync_l(); 281} 282 283status_t MediaPlayer::start() 284{ 285 ALOGV("start"); 286 Mutex::Autolock _l(mLock); 287 if (mCurrentState & MEDIA_PLAYER_STARTED) 288 return NO_ERROR; 289 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_PREPARED | 290 MEDIA_PLAYER_PLAYBACK_COMPLETE | MEDIA_PLAYER_PAUSED ) ) ) { 291 mPlayer->setLooping(mLoop); 292 mPlayer->setVolume(mLeftVolume, mRightVolume); 293 mPlayer->setAuxEffectSendLevel(mSendLevel); 294 mCurrentState = MEDIA_PLAYER_STARTED; 295 status_t ret = mPlayer->start(); 296 if (ret != NO_ERROR) { 297 mCurrentState = MEDIA_PLAYER_STATE_ERROR; 298 } else { 299 if (mCurrentState == MEDIA_PLAYER_PLAYBACK_COMPLETE) { 300 ALOGV("playback completed immediately following start()"); 301 } 302 } 303 return ret; 304 } 305 ALOGE("start called in state %d", mCurrentState); 306 return INVALID_OPERATION; 307} 308 309status_t MediaPlayer::stop() 310{ 311 ALOGV("stop"); 312 Mutex::Autolock _l(mLock); 313 if (mCurrentState & MEDIA_PLAYER_STOPPED) return NO_ERROR; 314 if ( (mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | 315 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) ) { 316 status_t ret = mPlayer->stop(); 317 if (ret != NO_ERROR) { 318 mCurrentState = MEDIA_PLAYER_STATE_ERROR; 319 } else { 320 mCurrentState = MEDIA_PLAYER_STOPPED; 321 } 322 return ret; 323 } 324 ALOGE("stop called in state %d", mCurrentState); 325 return INVALID_OPERATION; 326} 327 328status_t MediaPlayer::pause() 329{ 330 ALOGV("pause"); 331 Mutex::Autolock _l(mLock); 332 if (mCurrentState & (MEDIA_PLAYER_PAUSED|MEDIA_PLAYER_PLAYBACK_COMPLETE)) 333 return NO_ERROR; 334 if ((mPlayer != 0) && (mCurrentState & MEDIA_PLAYER_STARTED)) { 335 status_t ret = mPlayer->pause(); 336 if (ret != NO_ERROR) { 337 mCurrentState = MEDIA_PLAYER_STATE_ERROR; 338 } else { 339 mCurrentState = MEDIA_PLAYER_PAUSED; 340 } 341 return ret; 342 } 343 ALOGE("pause called in state %d", mCurrentState); 344 return INVALID_OPERATION; 345} 346 347bool MediaPlayer::isPlaying() 348{ 349 Mutex::Autolock _l(mLock); 350 if (mPlayer != 0) { 351 bool temp = false; 352 mPlayer->isPlaying(&temp); 353 ALOGV("isPlaying: %d", temp); 354 if ((mCurrentState & MEDIA_PLAYER_STARTED) && ! temp) { 355 ALOGE("internal/external state mismatch corrected"); 356 mCurrentState = MEDIA_PLAYER_PAUSED; 357 } 358 return temp; 359 } 360 ALOGV("isPlaying: no active player"); 361 return false; 362} 363 364status_t MediaPlayer::getVideoWidth(int *w) 365{ 366 ALOGV("getVideoWidth"); 367 Mutex::Autolock _l(mLock); 368 if (mPlayer == 0) return INVALID_OPERATION; 369 *w = mVideoWidth; 370 return NO_ERROR; 371} 372 373status_t MediaPlayer::getVideoHeight(int *h) 374{ 375 ALOGV("getVideoHeight"); 376 Mutex::Autolock _l(mLock); 377 if (mPlayer == 0) return INVALID_OPERATION; 378 *h = mVideoHeight; 379 return NO_ERROR; 380} 381 382status_t MediaPlayer::getCurrentPosition(int *msec) 383{ 384 ALOGV("getCurrentPosition"); 385 Mutex::Autolock _l(mLock); 386 if (mPlayer != 0) { 387 if (mCurrentPosition >= 0) { 388 ALOGV("Using cached seek position: %d", mCurrentPosition); 389 *msec = mCurrentPosition; 390 return NO_ERROR; 391 } 392 return mPlayer->getCurrentPosition(msec); 393 } 394 return INVALID_OPERATION; 395} 396 397status_t MediaPlayer::getDuration_l(int *msec) 398{ 399 ALOGV("getDuration_l"); 400 bool isValidState = (mCurrentState & (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_STOPPED | MEDIA_PLAYER_PLAYBACK_COMPLETE)); 401 if (mPlayer != 0 && isValidState) { 402 int durationMs; 403 status_t ret = mPlayer->getDuration(&durationMs); 404 405 if (ret != OK) { 406 // Do not enter error state just because no duration was available. 407 durationMs = -1; 408 ret = OK; 409 } 410 411 if (msec) { 412 *msec = durationMs; 413 } 414 return ret; 415 } 416 ALOGE("Attempt to call getDuration without a valid mediaplayer"); 417 return INVALID_OPERATION; 418} 419 420status_t MediaPlayer::getDuration(int *msec) 421{ 422 Mutex::Autolock _l(mLock); 423 return getDuration_l(msec); 424} 425 426status_t MediaPlayer::seekTo_l(int msec) 427{ 428 ALOGV("seekTo %d", msec); 429 if ((mPlayer != 0) && ( mCurrentState & ( MEDIA_PLAYER_STARTED | MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) ) { 430 if ( msec < 0 ) { 431 ALOGW("Attempt to seek to invalid position: %d", msec); 432 msec = 0; 433 } 434 435 int durationMs; 436 status_t err = mPlayer->getDuration(&durationMs); 437 438 if (err != OK) { 439 ALOGW("Stream has no duration and is therefore not seekable."); 440 return err; 441 } 442 443 if (msec > durationMs) { 444 ALOGW("Attempt to seek to past end of file: request = %d, " 445 "durationMs = %d", 446 msec, 447 durationMs); 448 449 msec = durationMs; 450 } 451 452 // cache duration 453 mCurrentPosition = msec; 454 if (mSeekPosition < 0) { 455 mSeekPosition = msec; 456 return mPlayer->seekTo(msec); 457 } 458 else { 459 ALOGV("Seek in progress - queue up seekTo[%d]", msec); 460 return NO_ERROR; 461 } 462 } 463 ALOGE("Attempt to perform seekTo in wrong state: mPlayer=%p, mCurrentState=%u", mPlayer.get(), mCurrentState); 464 return INVALID_OPERATION; 465} 466 467status_t MediaPlayer::seekTo(int msec) 468{ 469 mLockThreadId = getThreadId(); 470 Mutex::Autolock _l(mLock); 471 status_t result = seekTo_l(msec); 472 mLockThreadId = 0; 473 474 return result; 475} 476 477status_t MediaPlayer::reset_l() 478{ 479 mLoop = false; 480 if (mCurrentState == MEDIA_PLAYER_IDLE) return NO_ERROR; 481 mPrepareSync = false; 482 if (mPlayer != 0) { 483 status_t ret = mPlayer->reset(); 484 if (ret != NO_ERROR) { 485 ALOGE("reset() failed with return code (%d)", ret); 486 mCurrentState = MEDIA_PLAYER_STATE_ERROR; 487 } else { 488 mCurrentState = MEDIA_PLAYER_IDLE; 489 } 490 // setDataSource has to be called again to create a 491 // new mediaplayer. 492 mPlayer = 0; 493 return ret; 494 } 495 clear_l(); 496 return NO_ERROR; 497} 498 499status_t MediaPlayer::doSetRetransmitEndpoint(const sp<IMediaPlayer>& player) { 500 Mutex::Autolock _l(mLock); 501 502 if (player == NULL) { 503 return UNKNOWN_ERROR; 504 } 505 506 if (mRetransmitEndpointValid) { 507 return player->setRetransmitEndpoint(&mRetransmitEndpoint); 508 } 509 510 return OK; 511} 512 513status_t MediaPlayer::reset() 514{ 515 ALOGV("reset"); 516 Mutex::Autolock _l(mLock); 517 return reset_l(); 518} 519 520status_t MediaPlayer::setAudioStreamType(audio_stream_type_t type) 521{ 522 ALOGV("MediaPlayer::setAudioStreamType"); 523 Mutex::Autolock _l(mLock); 524 if (mStreamType == type) return NO_ERROR; 525 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | 526 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE ) ) { 527 // Can't change the stream type after prepare 528 ALOGE("setAudioStream called in state %d", mCurrentState); 529 return INVALID_OPERATION; 530 } 531 // cache 532 mStreamType = type; 533 return OK; 534} 535 536status_t MediaPlayer::getAudioStreamType(audio_stream_type_t *type) 537{ 538 ALOGV("getAudioStreamType"); 539 Mutex::Autolock _l(mLock); 540 *type = mStreamType; 541 return OK; 542} 543 544status_t MediaPlayer::setLooping(int loop) 545{ 546 ALOGV("MediaPlayer::setLooping"); 547 Mutex::Autolock _l(mLock); 548 mLoop = (loop != 0); 549 if (mPlayer != 0) { 550 return mPlayer->setLooping(loop); 551 } 552 return OK; 553} 554 555bool MediaPlayer::isLooping() { 556 ALOGV("isLooping"); 557 Mutex::Autolock _l(mLock); 558 if (mPlayer != 0) { 559 return mLoop; 560 } 561 ALOGV("isLooping: no active player"); 562 return false; 563} 564 565status_t MediaPlayer::setVolume(float leftVolume, float rightVolume) 566{ 567 ALOGV("MediaPlayer::setVolume(%f, %f)", leftVolume, rightVolume); 568 Mutex::Autolock _l(mLock); 569 mLeftVolume = leftVolume; 570 mRightVolume = rightVolume; 571 if (mPlayer != 0) { 572 return mPlayer->setVolume(leftVolume, rightVolume); 573 } 574 return OK; 575} 576 577status_t MediaPlayer::setAudioSessionId(int sessionId) 578{ 579 ALOGV("MediaPlayer::setAudioSessionId(%d)", sessionId); 580 Mutex::Autolock _l(mLock); 581 if (!(mCurrentState & MEDIA_PLAYER_IDLE)) { 582 ALOGE("setAudioSessionId called in state %d", mCurrentState); 583 return INVALID_OPERATION; 584 } 585 if (sessionId < 0) { 586 return BAD_VALUE; 587 } 588 if (sessionId != mAudioSessionId) { 589 AudioSystem::acquireAudioSessionId(sessionId, -1); 590 AudioSystem::releaseAudioSessionId(mAudioSessionId, -1); 591 mAudioSessionId = sessionId; 592 } 593 return NO_ERROR; 594} 595 596int MediaPlayer::getAudioSessionId() 597{ 598 Mutex::Autolock _l(mLock); 599 return mAudioSessionId; 600} 601 602status_t MediaPlayer::setAuxEffectSendLevel(float level) 603{ 604 ALOGV("MediaPlayer::setAuxEffectSendLevel(%f)", level); 605 Mutex::Autolock _l(mLock); 606 mSendLevel = level; 607 if (mPlayer != 0) { 608 return mPlayer->setAuxEffectSendLevel(level); 609 } 610 return OK; 611} 612 613status_t MediaPlayer::attachAuxEffect(int effectId) 614{ 615 ALOGV("MediaPlayer::attachAuxEffect(%d)", effectId); 616 Mutex::Autolock _l(mLock); 617 if (mPlayer == 0 || 618 (mCurrentState & MEDIA_PLAYER_IDLE) || 619 (mCurrentState == MEDIA_PLAYER_STATE_ERROR )) { 620 ALOGE("attachAuxEffect called in state %d", mCurrentState); 621 return INVALID_OPERATION; 622 } 623 624 return mPlayer->attachAuxEffect(effectId); 625} 626 627// always call with lock held 628status_t MediaPlayer::checkStateForKeySet_l(int key) 629{ 630 switch(key) { 631 case KEY_PARAMETER_AUDIO_ATTRIBUTES: 632 if (mCurrentState & ( MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_STARTED | 633 MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE) ) { 634 // Can't change the audio attributes after prepare 635 ALOGE("trying to set audio attributes called in state %d", mCurrentState); 636 return INVALID_OPERATION; 637 } 638 break; 639 default: 640 // parameter doesn't require player state check 641 break; 642 } 643 return OK; 644} 645 646status_t MediaPlayer::setParameter(int key, const Parcel& request) 647{ 648 ALOGV("MediaPlayer::setParameter(%d)", key); 649 Mutex::Autolock _l(mLock); 650 if (checkStateForKeySet_l(key) != OK) { 651 return INVALID_OPERATION; 652 } 653 if (mPlayer != NULL) { 654 return mPlayer->setParameter(key, request); 655 } 656 ALOGV("setParameter: no active player"); 657 return INVALID_OPERATION; 658} 659 660status_t MediaPlayer::getParameter(int key, Parcel *reply) 661{ 662 ALOGV("MediaPlayer::getParameter(%d)", key); 663 Mutex::Autolock _l(mLock); 664 if (mPlayer != NULL) { 665 return mPlayer->getParameter(key, reply); 666 } 667 ALOGV("getParameter: no active player"); 668 return INVALID_OPERATION; 669} 670 671status_t MediaPlayer::setRetransmitEndpoint(const char* addrString, 672 uint16_t port) { 673 ALOGV("MediaPlayer::setRetransmitEndpoint(%s:%hu)", 674 addrString ? addrString : "(null)", port); 675 676 Mutex::Autolock _l(mLock); 677 if ((mPlayer != NULL) || (mCurrentState != MEDIA_PLAYER_IDLE)) 678 return INVALID_OPERATION; 679 680 if (NULL == addrString) { 681 mRetransmitEndpointValid = false; 682 return OK; 683 } 684 685 struct in_addr saddr; 686 if(!inet_aton(addrString, &saddr)) { 687 return BAD_VALUE; 688 } 689 690 memset(&mRetransmitEndpoint, 0, sizeof(mRetransmitEndpoint)); 691 mRetransmitEndpoint.sin_family = AF_INET; 692 mRetransmitEndpoint.sin_addr = saddr; 693 mRetransmitEndpoint.sin_port = htons(port); 694 mRetransmitEndpointValid = true; 695 696 return OK; 697} 698 699void MediaPlayer::notify(int msg, int ext1, int ext2, const Parcel *obj) 700{ 701 ALOGV("message received msg=%d, ext1=%d, ext2=%d", msg, ext1, ext2); 702 bool send = true; 703 bool locked = false; 704 705 // TODO: In the future, we might be on the same thread if the app is 706 // running in the same process as the media server. In that case, 707 // this will deadlock. 708 // 709 // The threadId hack below works around this for the care of prepare 710 // and seekTo within the same process. 711 // FIXME: Remember, this is a hack, it's not even a hack that is applied 712 // consistently for all use-cases, this needs to be revisited. 713 if (mLockThreadId != getThreadId()) { 714 mLock.lock(); 715 locked = true; 716 } 717 718 // Allows calls from JNI in idle state to notify errors 719 if (!(msg == MEDIA_ERROR && mCurrentState == MEDIA_PLAYER_IDLE) && mPlayer == 0) { 720 ALOGV("notify(%d, %d, %d) callback on disconnected mediaplayer", msg, ext1, ext2); 721 if (locked) mLock.unlock(); // release the lock when done. 722 return; 723 } 724 725 switch (msg) { 726 case MEDIA_NOP: // interface test message 727 break; 728 case MEDIA_PREPARED: 729 ALOGV("prepared"); 730 mCurrentState = MEDIA_PLAYER_PREPARED; 731 if (mPrepareSync) { 732 ALOGV("signal application thread"); 733 mPrepareSync = false; 734 mPrepareStatus = NO_ERROR; 735 mSignal.signal(); 736 } 737 break; 738 case MEDIA_PLAYBACK_COMPLETE: 739 ALOGV("playback complete"); 740 if (mCurrentState == MEDIA_PLAYER_IDLE) { 741 ALOGE("playback complete in idle state"); 742 } 743 if (!mLoop) { 744 mCurrentState = MEDIA_PLAYER_PLAYBACK_COMPLETE; 745 } 746 break; 747 case MEDIA_ERROR: 748 // Always log errors. 749 // ext1: Media framework error code. 750 // ext2: Implementation dependant error code. 751 ALOGE("error (%d, %d)", ext1, ext2); 752 mCurrentState = MEDIA_PLAYER_STATE_ERROR; 753 if (mPrepareSync) 754 { 755 ALOGV("signal application thread"); 756 mPrepareSync = false; 757 mPrepareStatus = ext1; 758 mSignal.signal(); 759 send = false; 760 } 761 break; 762 case MEDIA_INFO: 763 // ext1: Media framework error code. 764 // ext2: Implementation dependant error code. 765 if (ext1 != MEDIA_INFO_VIDEO_TRACK_LAGGING) { 766 ALOGW("info/warning (%d, %d)", ext1, ext2); 767 } 768 break; 769 case MEDIA_SEEK_COMPLETE: 770 ALOGV("Received seek complete"); 771 if (mSeekPosition != mCurrentPosition) { 772 ALOGV("Executing queued seekTo(%d)", mSeekPosition); 773 mSeekPosition = -1; 774 seekTo_l(mCurrentPosition); 775 } 776 else { 777 ALOGV("All seeks complete - return to regularly scheduled program"); 778 mCurrentPosition = mSeekPosition = -1; 779 } 780 break; 781 case MEDIA_BUFFERING_UPDATE: 782 ALOGV("buffering %d", ext1); 783 break; 784 case MEDIA_SET_VIDEO_SIZE: 785 ALOGV("New video size %d x %d", ext1, ext2); 786 mVideoWidth = ext1; 787 mVideoHeight = ext2; 788 break; 789 case MEDIA_TIMED_TEXT: 790 ALOGV("Received timed text message"); 791 break; 792 case MEDIA_SUBTITLE_DATA: 793 ALOGV("Received subtitle data message"); 794 break; 795 default: 796 ALOGV("unrecognized message: (%d, %d, %d)", msg, ext1, ext2); 797 break; 798 } 799 800 sp<MediaPlayerListener> listener = mListener; 801 if (locked) mLock.unlock(); 802 803 // this prevents re-entrant calls into client code 804 if ((listener != 0) && send) { 805 Mutex::Autolock _l(mNotifyLock); 806 ALOGV("callback application"); 807 listener->notify(msg, ext1, ext2, obj); 808 ALOGV("back from callback"); 809 } 810} 811 812/*static*/ status_t MediaPlayer::decode( 813 const sp<IMediaHTTPService> &httpService, 814 const char* url, 815 uint32_t *pSampleRate, 816 int* pNumChannels, 817 audio_format_t* pFormat, 818 const sp<IMemoryHeap>& heap, 819 size_t *pSize) 820{ 821 ALOGV("decode(%s)", url); 822 status_t status; 823 const sp<IMediaPlayerService>& service = getMediaPlayerService(); 824 if (service != 0) { 825 status = service->decode(httpService, url, pSampleRate, pNumChannels, pFormat, heap, pSize); 826 } else { 827 ALOGE("Unable to locate media service"); 828 status = DEAD_OBJECT; 829 } 830 return status; 831 832} 833 834void MediaPlayer::died() 835{ 836 ALOGV("died"); 837 notify(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, 0); 838} 839 840/*static*/ status_t MediaPlayer::decode(int fd, int64_t offset, int64_t length, 841 uint32_t *pSampleRate, int* pNumChannels, 842 audio_format_t* pFormat, 843 const sp<IMemoryHeap>& heap, size_t *pSize) 844{ 845 ALOGV("decode(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length); 846 status_t status; 847 const sp<IMediaPlayerService>& service = getMediaPlayerService(); 848 if (service != 0) { 849 status = service->decode(fd, offset, length, pSampleRate, 850 pNumChannels, pFormat, heap, pSize); 851 } else { 852 ALOGE("Unable to locate media service"); 853 status = DEAD_OBJECT; 854 } 855 return status; 856 857} 858 859status_t MediaPlayer::setNextMediaPlayer(const sp<MediaPlayer>& next) { 860 if (mPlayer == NULL) { 861 return NO_INIT; 862 } 863 864 if (next != NULL && !(next->mCurrentState & 865 (MEDIA_PLAYER_PREPARED | MEDIA_PLAYER_PAUSED | MEDIA_PLAYER_PLAYBACK_COMPLETE))) { 866 ALOGE("next player is not prepared"); 867 return INVALID_OPERATION; 868 } 869 870 return mPlayer->setNextPlayer(next == NULL ? NULL : next->mPlayer); 871} 872 873}; // namespace android 874