NuPlayer.cpp revision 29b7dcf6d3cdb97103467dc8106151c6260c239a
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17//#define LOG_NDEBUG 0 18#define LOG_TAG "NuPlayer" 19#include <utils/Log.h> 20 21#include "NuPlayer.h" 22 23#include "HTTPLiveSource.h" 24#include "NuPlayerCCDecoder.h" 25#include "NuPlayerDecoder.h" 26#include "NuPlayerDecoderBase.h" 27#include "NuPlayerDecoderPassThrough.h" 28#include "NuPlayerDriver.h" 29#include "NuPlayerRenderer.h" 30#include "NuPlayerSource.h" 31#include "RTSPSource.h" 32#include "StreamingSource.h" 33#include "GenericSource.h" 34#include "TextDescriptions.h" 35 36#include "ATSParser.h" 37 38#include <cutils/properties.h> 39 40#include <media/AudioResamplerPublic.h> 41#include <media/AVSyncSettings.h> 42#include <media/MediaCodecBuffer.h> 43 44#include <media/stagefright/foundation/hexdump.h> 45#include <media/stagefright/foundation/ABuffer.h> 46#include <media/stagefright/foundation/ADebug.h> 47#include <media/stagefright/foundation/AMessage.h> 48#include <media/stagefright/MediaBuffer.h> 49#include <media/stagefright/MediaDefs.h> 50#include <media/stagefright/MediaErrors.h> 51#include <media/stagefright/MetaData.h> 52 53#include <gui/IGraphicBufferProducer.h> 54#include <gui/Surface.h> 55 56#include "avc_utils.h" 57 58#include "ESDS.h" 59#include <media/stagefright/Utils.h> 60 61namespace android { 62 63struct NuPlayer::Action : public RefBase { 64 Action() {} 65 66 virtual void execute(NuPlayer *player) = 0; 67 68private: 69 DISALLOW_EVIL_CONSTRUCTORS(Action); 70}; 71 72struct NuPlayer::SeekAction : public Action { 73 explicit SeekAction(int64_t seekTimeUs, bool precise) 74 : mSeekTimeUs(seekTimeUs), 75 mPrecise(precise) { 76 } 77 78 virtual void execute(NuPlayer *player) { 79 player->performSeek(mSeekTimeUs, mPrecise); 80 } 81 82private: 83 int64_t mSeekTimeUs; 84 bool mPrecise; 85 86 DISALLOW_EVIL_CONSTRUCTORS(SeekAction); 87}; 88 89struct NuPlayer::ResumeDecoderAction : public Action { 90 explicit ResumeDecoderAction(bool needNotify) 91 : mNeedNotify(needNotify) { 92 } 93 94 virtual void execute(NuPlayer *player) { 95 player->performResumeDecoders(mNeedNotify); 96 } 97 98private: 99 bool mNeedNotify; 100 101 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction); 102}; 103 104struct NuPlayer::SetSurfaceAction : public Action { 105 explicit SetSurfaceAction(const sp<Surface> &surface) 106 : mSurface(surface) { 107 } 108 109 virtual void execute(NuPlayer *player) { 110 player->performSetSurface(mSurface); 111 } 112 113private: 114 sp<Surface> mSurface; 115 116 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction); 117}; 118 119struct NuPlayer::FlushDecoderAction : public Action { 120 FlushDecoderAction(FlushCommand audio, FlushCommand video) 121 : mAudio(audio), 122 mVideo(video) { 123 } 124 125 virtual void execute(NuPlayer *player) { 126 player->performDecoderFlush(mAudio, mVideo); 127 } 128 129private: 130 FlushCommand mAudio; 131 FlushCommand mVideo; 132 133 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction); 134}; 135 136struct NuPlayer::PostMessageAction : public Action { 137 explicit PostMessageAction(const sp<AMessage> &msg) 138 : mMessage(msg) { 139 } 140 141 virtual void execute(NuPlayer *) { 142 mMessage->post(); 143 } 144 145private: 146 sp<AMessage> mMessage; 147 148 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction); 149}; 150 151// Use this if there's no state necessary to save in order to execute 152// the action. 153struct NuPlayer::SimpleAction : public Action { 154 typedef void (NuPlayer::*ActionFunc)(); 155 156 explicit SimpleAction(ActionFunc func) 157 : mFunc(func) { 158 } 159 160 virtual void execute(NuPlayer *player) { 161 (player->*mFunc)(); 162 } 163 164private: 165 ActionFunc mFunc; 166 167 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction); 168}; 169 170//////////////////////////////////////////////////////////////////////////////// 171 172NuPlayer::NuPlayer(pid_t pid) 173 : mUIDValid(false), 174 mPID(pid), 175 mSourceFlags(0), 176 mOffloadAudio(false), 177 mAudioDecoderGeneration(0), 178 mVideoDecoderGeneration(0), 179 mRendererGeneration(0), 180 mPreviousSeekTimeUs(0), 181 mAudioEOS(false), 182 mVideoEOS(false), 183 mScanSourcesPending(false), 184 mScanSourcesGeneration(0), 185 mPollDurationGeneration(0), 186 mTimedTextGeneration(0), 187 mFlushingAudio(NONE), 188 mFlushingVideo(NONE), 189 mResumePending(false), 190 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW), 191 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT), 192 mVideoFpsHint(-1.f), 193 mStarted(false), 194 mPrepared(false), 195 mResetting(false), 196 mSourceStarted(false), 197 mPaused(false), 198 mPausedByClient(true), 199 mPausedForBuffering(false) { 200 clearFlushComplete(); 201} 202 203NuPlayer::~NuPlayer() { 204} 205 206void NuPlayer::setUID(uid_t uid) { 207 mUIDValid = true; 208 mUID = uid; 209} 210 211void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) { 212 mDriver = driver; 213} 214 215void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) { 216 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 217 218 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 219 220 msg->setObject("source", new StreamingSource(notify, source)); 221 msg->post(); 222} 223 224static bool IsHTTPLiveURL(const char *url) { 225 if (!strncasecmp("http://", url, 7) 226 || !strncasecmp("https://", url, 8) 227 || !strncasecmp("file://", url, 7)) { 228 size_t len = strlen(url); 229 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) { 230 return true; 231 } 232 233 if (strstr(url,"m3u8")) { 234 return true; 235 } 236 } 237 238 return false; 239} 240 241void NuPlayer::setDataSourceAsync( 242 const sp<IMediaHTTPService> &httpService, 243 const char *url, 244 const KeyedVector<String8, String8> *headers) { 245 246 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 247 size_t len = strlen(url); 248 249 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 250 251 sp<Source> source; 252 if (IsHTTPLiveURL(url)) { 253 source = new HTTPLiveSource(notify, httpService, url, headers); 254 } else if (!strncasecmp(url, "rtsp://", 7)) { 255 source = new RTSPSource( 256 notify, httpService, url, headers, mUIDValid, mUID); 257 } else if ((!strncasecmp(url, "http://", 7) 258 || !strncasecmp(url, "https://", 8)) 259 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4])) 260 || strstr(url, ".sdp?"))) { 261 source = new RTSPSource( 262 notify, httpService, url, headers, mUIDValid, mUID, true); 263 } else { 264 sp<GenericSource> genericSource = 265 new GenericSource(notify, mUIDValid, mUID); 266 267 status_t err = genericSource->setDataSource(httpService, url, headers); 268 269 if (err == OK) { 270 source = genericSource; 271 } else { 272 ALOGE("Failed to set data source!"); 273 } 274 } 275 msg->setObject("source", source); 276 msg->post(); 277} 278 279void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) { 280 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 281 282 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 283 284 sp<GenericSource> source = 285 new GenericSource(notify, mUIDValid, mUID); 286 287 status_t err = source->setDataSource(fd, offset, length); 288 289 if (err != OK) { 290 ALOGE("Failed to set data source!"); 291 source = NULL; 292 } 293 294 msg->setObject("source", source); 295 msg->post(); 296} 297 298void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) { 299 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 300 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 301 302 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID); 303 status_t err = source->setDataSource(dataSource); 304 305 if (err != OK) { 306 ALOGE("Failed to set data source!"); 307 source = NULL; 308 } 309 310 msg->setObject("source", source); 311 msg->post(); 312} 313 314void NuPlayer::prepareAsync() { 315 (new AMessage(kWhatPrepare, this))->post(); 316} 317 318void NuPlayer::setVideoSurfaceTextureAsync( 319 const sp<IGraphicBufferProducer> &bufferProducer) { 320 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this); 321 322 if (bufferProducer == NULL) { 323 msg->setObject("surface", NULL); 324 } else { 325 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */)); 326 } 327 328 msg->post(); 329} 330 331void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) { 332 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this); 333 msg->setObject("sink", sink); 334 msg->post(); 335} 336 337void NuPlayer::start() { 338 (new AMessage(kWhatStart, this))->post(); 339} 340 341status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) { 342 // do some cursory validation of the settings here. audio modes are 343 // only validated when set on the audiosink. 344 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN) 345 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX 346 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN 347 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) { 348 return BAD_VALUE; 349 } 350 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this); 351 writeToAMessage(msg, rate); 352 sp<AMessage> response; 353 status_t err = msg->postAndAwaitResponse(&response); 354 if (err == OK && response != NULL) { 355 CHECK(response->findInt32("err", &err)); 356 } 357 return err; 358} 359 360status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) { 361 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this); 362 sp<AMessage> response; 363 status_t err = msg->postAndAwaitResponse(&response); 364 if (err == OK && response != NULL) { 365 CHECK(response->findInt32("err", &err)); 366 if (err == OK) { 367 readFromAMessage(response, rate); 368 } 369 } 370 return err; 371} 372 373status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) { 374 sp<AMessage> msg = new AMessage(kWhatConfigSync, this); 375 writeToAMessage(msg, sync, videoFpsHint); 376 sp<AMessage> response; 377 status_t err = msg->postAndAwaitResponse(&response); 378 if (err == OK && response != NULL) { 379 CHECK(response->findInt32("err", &err)); 380 } 381 return err; 382} 383 384status_t NuPlayer::getSyncSettings( 385 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) { 386 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this); 387 sp<AMessage> response; 388 status_t err = msg->postAndAwaitResponse(&response); 389 if (err == OK && response != NULL) { 390 CHECK(response->findInt32("err", &err)); 391 if (err == OK) { 392 readFromAMessage(response, sync, videoFps); 393 } 394 } 395 return err; 396} 397 398void NuPlayer::pause() { 399 (new AMessage(kWhatPause, this))->post(); 400} 401 402void NuPlayer::resetAsync() { 403 sp<Source> source; 404 { 405 Mutex::Autolock autoLock(mSourceLock); 406 source = mSource; 407 } 408 409 if (source != NULL) { 410 // During a reset, the data source might be unresponsive already, we need to 411 // disconnect explicitly so that reads exit promptly. 412 // We can't queue the disconnect request to the looper, as it might be 413 // queued behind a stuck read and never gets processed. 414 // Doing a disconnect outside the looper to allows the pending reads to exit 415 // (either successfully or with error). 416 source->disconnect(); 417 } 418 419 (new AMessage(kWhatReset, this))->post(); 420} 421 422void NuPlayer::seekToAsync(int64_t seekTimeUs, bool precise, bool needNotify) { 423 sp<AMessage> msg = new AMessage(kWhatSeek, this); 424 msg->setInt64("seekTimeUs", seekTimeUs); 425 msg->setInt32("precise", precise); 426 msg->setInt32("needNotify", needNotify); 427 msg->post(); 428} 429 430 431void NuPlayer::writeTrackInfo( 432 Parcel* reply, const sp<AMessage>& format) const { 433 if (format == NULL) { 434 ALOGE("NULL format"); 435 return; 436 } 437 int32_t trackType; 438 if (!format->findInt32("type", &trackType)) { 439 ALOGE("no track type"); 440 return; 441 } 442 443 AString mime; 444 if (!format->findString("mime", &mime)) { 445 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks. 446 // If we can't find the mimetype here it means that we wouldn't be needing 447 // the mimetype on the Java end. We still write a placeholder mime to keep the 448 // (de)serialization logic simple. 449 if (trackType == MEDIA_TRACK_TYPE_AUDIO) { 450 mime = "audio/"; 451 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) { 452 mime = "video/"; 453 } else { 454 ALOGE("unknown track type: %d", trackType); 455 return; 456 } 457 } 458 459 AString lang; 460 if (!format->findString("language", &lang)) { 461 ALOGE("no language"); 462 return; 463 } 464 465 reply->writeInt32(2); // write something non-zero 466 reply->writeInt32(trackType); 467 reply->writeString16(String16(mime.c_str())); 468 reply->writeString16(String16(lang.c_str())); 469 470 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { 471 int32_t isAuto, isDefault, isForced; 472 CHECK(format->findInt32("auto", &isAuto)); 473 CHECK(format->findInt32("default", &isDefault)); 474 CHECK(format->findInt32("forced", &isForced)); 475 476 reply->writeInt32(isAuto); 477 reply->writeInt32(isDefault); 478 reply->writeInt32(isForced); 479 } 480} 481 482void NuPlayer::onMessageReceived(const sp<AMessage> &msg) { 483 switch (msg->what()) { 484 case kWhatSetDataSource: 485 { 486 ALOGV("kWhatSetDataSource"); 487 488 CHECK(mSource == NULL); 489 490 status_t err = OK; 491 sp<RefBase> obj; 492 CHECK(msg->findObject("source", &obj)); 493 if (obj != NULL) { 494 Mutex::Autolock autoLock(mSourceLock); 495 mSource = static_cast<Source *>(obj.get()); 496 } else { 497 err = UNKNOWN_ERROR; 498 } 499 500 CHECK(mDriver != NULL); 501 sp<NuPlayerDriver> driver = mDriver.promote(); 502 if (driver != NULL) { 503 driver->notifySetDataSourceCompleted(err); 504 } 505 break; 506 } 507 508 case kWhatPrepare: 509 { 510 mSource->prepareAsync(); 511 break; 512 } 513 514 case kWhatGetTrackInfo: 515 { 516 sp<AReplyToken> replyID; 517 CHECK(msg->senderAwaitsResponse(&replyID)); 518 519 Parcel* reply; 520 CHECK(msg->findPointer("reply", (void**)&reply)); 521 522 size_t inbandTracks = 0; 523 if (mSource != NULL) { 524 inbandTracks = mSource->getTrackCount(); 525 } 526 527 size_t ccTracks = 0; 528 if (mCCDecoder != NULL) { 529 ccTracks = mCCDecoder->getTrackCount(); 530 } 531 532 // total track count 533 reply->writeInt32(inbandTracks + ccTracks); 534 535 // write inband tracks 536 for (size_t i = 0; i < inbandTracks; ++i) { 537 writeTrackInfo(reply, mSource->getTrackInfo(i)); 538 } 539 540 // write CC track 541 for (size_t i = 0; i < ccTracks; ++i) { 542 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i)); 543 } 544 545 sp<AMessage> response = new AMessage; 546 response->postReply(replyID); 547 break; 548 } 549 550 case kWhatGetSelectedTrack: 551 { 552 status_t err = INVALID_OPERATION; 553 if (mSource != NULL) { 554 err = OK; 555 556 int32_t type32; 557 CHECK(msg->findInt32("type", (int32_t*)&type32)); 558 media_track_type type = (media_track_type)type32; 559 ssize_t selectedTrack = mSource->getSelectedTrack(type); 560 561 Parcel* reply; 562 CHECK(msg->findPointer("reply", (void**)&reply)); 563 reply->writeInt32(selectedTrack); 564 } 565 566 sp<AMessage> response = new AMessage; 567 response->setInt32("err", err); 568 569 sp<AReplyToken> replyID; 570 CHECK(msg->senderAwaitsResponse(&replyID)); 571 response->postReply(replyID); 572 break; 573 } 574 575 case kWhatSelectTrack: 576 { 577 sp<AReplyToken> replyID; 578 CHECK(msg->senderAwaitsResponse(&replyID)); 579 580 size_t trackIndex; 581 int32_t select; 582 int64_t timeUs; 583 CHECK(msg->findSize("trackIndex", &trackIndex)); 584 CHECK(msg->findInt32("select", &select)); 585 CHECK(msg->findInt64("timeUs", &timeUs)); 586 587 status_t err = INVALID_OPERATION; 588 589 size_t inbandTracks = 0; 590 if (mSource != NULL) { 591 inbandTracks = mSource->getTrackCount(); 592 } 593 size_t ccTracks = 0; 594 if (mCCDecoder != NULL) { 595 ccTracks = mCCDecoder->getTrackCount(); 596 } 597 598 if (trackIndex < inbandTracks) { 599 err = mSource->selectTrack(trackIndex, select, timeUs); 600 601 if (!select && err == OK) { 602 int32_t type; 603 sp<AMessage> info = mSource->getTrackInfo(trackIndex); 604 if (info != NULL 605 && info->findInt32("type", &type) 606 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) { 607 ++mTimedTextGeneration; 608 } 609 } 610 } else { 611 trackIndex -= inbandTracks; 612 613 if (trackIndex < ccTracks) { 614 err = mCCDecoder->selectTrack(trackIndex, select); 615 } 616 } 617 618 sp<AMessage> response = new AMessage; 619 response->setInt32("err", err); 620 621 response->postReply(replyID); 622 break; 623 } 624 625 case kWhatPollDuration: 626 { 627 int32_t generation; 628 CHECK(msg->findInt32("generation", &generation)); 629 630 if (generation != mPollDurationGeneration) { 631 // stale 632 break; 633 } 634 635 int64_t durationUs; 636 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) { 637 sp<NuPlayerDriver> driver = mDriver.promote(); 638 if (driver != NULL) { 639 driver->notifyDuration(durationUs); 640 } 641 } 642 643 msg->post(1000000ll); // poll again in a second. 644 break; 645 } 646 647 case kWhatSetVideoSurface: 648 { 649 650 sp<RefBase> obj; 651 CHECK(msg->findObject("surface", &obj)); 652 sp<Surface> surface = static_cast<Surface *>(obj.get()); 653 654 ALOGD("onSetVideoSurface(%p, %s video decoder)", 655 surface.get(), 656 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL 657 && mVideoDecoder != NULL) ? "have" : "no"); 658 659 // Need to check mStarted before calling mSource->getFormat because NuPlayer might 660 // be in preparing state and it could take long time. 661 // When mStarted is true, mSource must have been set. 662 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL 663 // NOTE: mVideoDecoder's mSurface is always non-null 664 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(surface) == OK)) { 665 performSetSurface(surface); 666 break; 667 } 668 669 mDeferredActions.push_back( 670 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */, 671 FLUSH_CMD_SHUTDOWN /* video */)); 672 673 mDeferredActions.push_back(new SetSurfaceAction(surface)); 674 675 if (obj != NULL || mAudioDecoder != NULL) { 676 if (mStarted) { 677 // Issue a seek to refresh the video screen only if started otherwise 678 // the extractor may not yet be started and will assert. 679 // If the video decoder is not set (perhaps audio only in this case) 680 // do not perform a seek as it is not needed. 681 int64_t currentPositionUs = 0; 682 if (getCurrentPosition(¤tPositionUs) == OK) { 683 mDeferredActions.push_back( 684 new SeekAction(currentPositionUs, false /* precise */)); 685 } 686 } 687 688 // If there is a new surface texture, instantiate decoders 689 // again if possible. 690 mDeferredActions.push_back( 691 new SimpleAction(&NuPlayer::performScanSources)); 692 } 693 694 // After a flush without shutdown, decoder is paused. 695 // Don't resume it until source seek is done, otherwise it could 696 // start pulling stale data too soon. 697 mDeferredActions.push_back( 698 new ResumeDecoderAction(false /* needNotify */)); 699 700 processDeferredActions(); 701 break; 702 } 703 704 case kWhatSetAudioSink: 705 { 706 ALOGV("kWhatSetAudioSink"); 707 708 sp<RefBase> obj; 709 CHECK(msg->findObject("sink", &obj)); 710 711 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get()); 712 break; 713 } 714 715 case kWhatStart: 716 { 717 ALOGV("kWhatStart"); 718 if (mStarted) { 719 // do not resume yet if the source is still buffering 720 if (!mPausedForBuffering) { 721 onResume(); 722 } 723 } else { 724 onStart(); 725 } 726 mPausedByClient = false; 727 break; 728 } 729 730 case kWhatConfigPlayback: 731 { 732 sp<AReplyToken> replyID; 733 CHECK(msg->senderAwaitsResponse(&replyID)); 734 AudioPlaybackRate rate /* sanitized */; 735 readFromAMessage(msg, &rate); 736 status_t err = OK; 737 if (mRenderer != NULL) { 738 // AudioSink allows only 1.f and 0.f for offload mode. 739 // For other speed, switch to non-offload mode. 740 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f) 741 || rate.mPitch != 1.f)) { 742 int64_t currentPositionUs; 743 if (getCurrentPosition(¤tPositionUs) != OK) { 744 currentPositionUs = mPreviousSeekTimeUs; 745 } 746 747 // Set mPlaybackSettings so that the new audio decoder can 748 // be created correctly. 749 mPlaybackSettings = rate; 750 if (!mPaused) { 751 mRenderer->pause(); 752 } 753 restartAudio( 754 currentPositionUs, true /* forceNonOffload */, 755 true /* needsToCreateAudioDecoder */); 756 if (!mPaused) { 757 mRenderer->resume(); 758 } 759 } 760 761 err = mRenderer->setPlaybackSettings(rate); 762 } 763 if (err == OK) { 764 if (rate.mSpeed == 0.f) { 765 onPause(); 766 mPausedByClient = true; 767 // save all other settings (using non-paused speed) 768 // so we can restore them on start 769 AudioPlaybackRate newRate = rate; 770 newRate.mSpeed = mPlaybackSettings.mSpeed; 771 mPlaybackSettings = newRate; 772 } else { /* rate.mSpeed != 0.f */ 773 mPlaybackSettings = rate; 774 if (mStarted) { 775 // do not resume yet if the source is still buffering 776 if (!mPausedForBuffering) { 777 onResume(); 778 } 779 } else if (mPrepared) { 780 onStart(); 781 } 782 783 mPausedByClient = false; 784 } 785 } 786 787 if (mVideoDecoder != NULL) { 788 sp<AMessage> params = new AMessage(); 789 params->setFloat("playback-speed", mPlaybackSettings.mSpeed); 790 mVideoDecoder->setParameters(params); 791 } 792 793 sp<AMessage> response = new AMessage; 794 response->setInt32("err", err); 795 response->postReply(replyID); 796 break; 797 } 798 799 case kWhatGetPlaybackSettings: 800 { 801 sp<AReplyToken> replyID; 802 CHECK(msg->senderAwaitsResponse(&replyID)); 803 AudioPlaybackRate rate = mPlaybackSettings; 804 status_t err = OK; 805 if (mRenderer != NULL) { 806 err = mRenderer->getPlaybackSettings(&rate); 807 } 808 if (err == OK) { 809 // get playback settings used by renderer, as it may be 810 // slightly off due to audiosink not taking small changes. 811 mPlaybackSettings = rate; 812 if (mPaused) { 813 rate.mSpeed = 0.f; 814 } 815 } 816 sp<AMessage> response = new AMessage; 817 if (err == OK) { 818 writeToAMessage(response, rate); 819 } 820 response->setInt32("err", err); 821 response->postReply(replyID); 822 break; 823 } 824 825 case kWhatConfigSync: 826 { 827 sp<AReplyToken> replyID; 828 CHECK(msg->senderAwaitsResponse(&replyID)); 829 830 ALOGV("kWhatConfigSync"); 831 AVSyncSettings sync; 832 float videoFpsHint; 833 readFromAMessage(msg, &sync, &videoFpsHint); 834 status_t err = OK; 835 if (mRenderer != NULL) { 836 err = mRenderer->setSyncSettings(sync, videoFpsHint); 837 } 838 if (err == OK) { 839 mSyncSettings = sync; 840 mVideoFpsHint = videoFpsHint; 841 } 842 sp<AMessage> response = new AMessage; 843 response->setInt32("err", err); 844 response->postReply(replyID); 845 break; 846 } 847 848 case kWhatGetSyncSettings: 849 { 850 sp<AReplyToken> replyID; 851 CHECK(msg->senderAwaitsResponse(&replyID)); 852 AVSyncSettings sync = mSyncSettings; 853 float videoFps = mVideoFpsHint; 854 status_t err = OK; 855 if (mRenderer != NULL) { 856 err = mRenderer->getSyncSettings(&sync, &videoFps); 857 if (err == OK) { 858 mSyncSettings = sync; 859 mVideoFpsHint = videoFps; 860 } 861 } 862 sp<AMessage> response = new AMessage; 863 if (err == OK) { 864 writeToAMessage(response, sync, videoFps); 865 } 866 response->setInt32("err", err); 867 response->postReply(replyID); 868 break; 869 } 870 871 case kWhatScanSources: 872 { 873 int32_t generation; 874 CHECK(msg->findInt32("generation", &generation)); 875 if (generation != mScanSourcesGeneration) { 876 // Drop obsolete msg. 877 break; 878 } 879 880 mScanSourcesPending = false; 881 882 ALOGV("scanning sources haveAudio=%d, haveVideo=%d", 883 mAudioDecoder != NULL, mVideoDecoder != NULL); 884 885 bool mHadAnySourcesBefore = 886 (mAudioDecoder != NULL) || (mVideoDecoder != NULL); 887 bool rescan = false; 888 889 // initialize video before audio because successful initialization of 890 // video may change deep buffer mode of audio. 891 if (mSurface != NULL) { 892 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) { 893 rescan = true; 894 } 895 } 896 897 // Don't try to re-open audio sink if there's an existing decoder. 898 if (mAudioSink != NULL && mAudioDecoder == NULL) { 899 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) { 900 rescan = true; 901 } 902 } 903 904 if (!mHadAnySourcesBefore 905 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 906 // This is the first time we've found anything playable. 907 908 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) { 909 schedulePollDuration(); 910 } 911 } 912 913 status_t err; 914 if ((err = mSource->feedMoreTSData()) != OK) { 915 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 916 // We're not currently decoding anything (no audio or 917 // video tracks found) and we just ran out of input data. 918 919 if (err == ERROR_END_OF_STREAM) { 920 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 921 } else { 922 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 923 } 924 } 925 break; 926 } 927 928 if (rescan) { 929 msg->post(100000ll); 930 mScanSourcesPending = true; 931 } 932 break; 933 } 934 935 case kWhatVideoNotify: 936 case kWhatAudioNotify: 937 { 938 bool audio = msg->what() == kWhatAudioNotify; 939 940 int32_t currentDecoderGeneration = 941 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration); 942 int32_t requesterGeneration = currentDecoderGeneration - 1; 943 CHECK(msg->findInt32("generation", &requesterGeneration)); 944 945 if (requesterGeneration != currentDecoderGeneration) { 946 ALOGV("got message from old %s decoder, generation(%d:%d)", 947 audio ? "audio" : "video", requesterGeneration, 948 currentDecoderGeneration); 949 sp<AMessage> reply; 950 if (!(msg->findMessage("reply", &reply))) { 951 return; 952 } 953 954 reply->setInt32("err", INFO_DISCONTINUITY); 955 reply->post(); 956 return; 957 } 958 959 int32_t what; 960 CHECK(msg->findInt32("what", &what)); 961 962 if (what == DecoderBase::kWhatInputDiscontinuity) { 963 int32_t formatChange; 964 CHECK(msg->findInt32("formatChange", &formatChange)); 965 966 ALOGV("%s discontinuity: formatChange %d", 967 audio ? "audio" : "video", formatChange); 968 969 if (formatChange) { 970 mDeferredActions.push_back( 971 new FlushDecoderAction( 972 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 973 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN)); 974 } 975 976 mDeferredActions.push_back( 977 new SimpleAction( 978 &NuPlayer::performScanSources)); 979 980 processDeferredActions(); 981 } else if (what == DecoderBase::kWhatEOS) { 982 int32_t err; 983 CHECK(msg->findInt32("err", &err)); 984 985 if (err == ERROR_END_OF_STREAM) { 986 ALOGV("got %s decoder EOS", audio ? "audio" : "video"); 987 } else { 988 ALOGV("got %s decoder EOS w/ error %d", 989 audio ? "audio" : "video", 990 err); 991 } 992 993 mRenderer->queueEOS(audio, err); 994 } else if (what == DecoderBase::kWhatFlushCompleted) { 995 ALOGV("decoder %s flush completed", audio ? "audio" : "video"); 996 997 handleFlushComplete(audio, true /* isDecoder */); 998 finishFlushIfPossible(); 999 } else if (what == DecoderBase::kWhatVideoSizeChanged) { 1000 sp<AMessage> format; 1001 CHECK(msg->findMessage("format", &format)); 1002 1003 sp<AMessage> inputFormat = 1004 mSource->getFormat(false /* audio */); 1005 1006 setVideoScalingMode(mVideoScalingMode); 1007 updateVideoSize(inputFormat, format); 1008 } else if (what == DecoderBase::kWhatShutdownCompleted) { 1009 ALOGV("%s shutdown completed", audio ? "audio" : "video"); 1010 if (audio) { 1011 mAudioDecoder.clear(); 1012 ++mAudioDecoderGeneration; 1013 1014 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER); 1015 mFlushingAudio = SHUT_DOWN; 1016 } else { 1017 mVideoDecoder.clear(); 1018 ++mVideoDecoderGeneration; 1019 1020 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER); 1021 mFlushingVideo = SHUT_DOWN; 1022 } 1023 1024 finishFlushIfPossible(); 1025 } else if (what == DecoderBase::kWhatResumeCompleted) { 1026 finishResume(); 1027 } else if (what == DecoderBase::kWhatError) { 1028 status_t err; 1029 if (!msg->findInt32("err", &err) || err == OK) { 1030 err = UNKNOWN_ERROR; 1031 } 1032 1033 // Decoder errors can be due to Source (e.g. from streaming), 1034 // or from decoding corrupted bitstreams, or from other decoder 1035 // MediaCodec operations (e.g. from an ongoing reset or seek). 1036 // They may also be due to openAudioSink failure at 1037 // decoder start or after a format change. 1038 // 1039 // We try to gracefully shut down the affected decoder if possible, 1040 // rather than trying to force the shutdown with something 1041 // similar to performReset(). This method can lead to a hang 1042 // if MediaCodec functions block after an error, but they should 1043 // typically return INVALID_OPERATION instead of blocking. 1044 1045 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo; 1046 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down", 1047 err, audio ? "audio" : "video", *flushing); 1048 1049 switch (*flushing) { 1050 case NONE: 1051 mDeferredActions.push_back( 1052 new FlushDecoderAction( 1053 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 1054 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN)); 1055 processDeferredActions(); 1056 break; 1057 case FLUSHING_DECODER: 1058 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush. 1059 break; // Wait for flush to complete. 1060 case FLUSHING_DECODER_SHUTDOWN: 1061 break; // Wait for flush to complete. 1062 case SHUTTING_DOWN_DECODER: 1063 break; // Wait for shutdown to complete. 1064 case FLUSHED: 1065 // Widevine source reads must stop before releasing the video decoder. 1066 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) { 1067 mSource->stop(); 1068 mSourceStarted = false; 1069 } 1070 getDecoder(audio)->initiateShutdown(); // In the middle of a seek. 1071 *flushing = SHUTTING_DOWN_DECODER; // Shut down. 1072 break; 1073 case SHUT_DOWN: 1074 finishFlushIfPossible(); // Should not occur. 1075 break; // Finish anyways. 1076 } 1077 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 1078 } else { 1079 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.", 1080 what, 1081 what >> 24, 1082 (what >> 16) & 0xff, 1083 (what >> 8) & 0xff, 1084 what & 0xff); 1085 } 1086 1087 break; 1088 } 1089 1090 case kWhatRendererNotify: 1091 { 1092 int32_t requesterGeneration = mRendererGeneration - 1; 1093 CHECK(msg->findInt32("generation", &requesterGeneration)); 1094 if (requesterGeneration != mRendererGeneration) { 1095 ALOGV("got message from old renderer, generation(%d:%d)", 1096 requesterGeneration, mRendererGeneration); 1097 return; 1098 } 1099 1100 int32_t what; 1101 CHECK(msg->findInt32("what", &what)); 1102 1103 if (what == Renderer::kWhatEOS) { 1104 int32_t audio; 1105 CHECK(msg->findInt32("audio", &audio)); 1106 1107 int32_t finalResult; 1108 CHECK(msg->findInt32("finalResult", &finalResult)); 1109 1110 if (audio) { 1111 mAudioEOS = true; 1112 } else { 1113 mVideoEOS = true; 1114 } 1115 1116 if (finalResult == ERROR_END_OF_STREAM) { 1117 ALOGV("reached %s EOS", audio ? "audio" : "video"); 1118 } else { 1119 ALOGE("%s track encountered an error (%d)", 1120 audio ? "audio" : "video", finalResult); 1121 1122 notifyListener( 1123 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult); 1124 } 1125 1126 if ((mAudioEOS || mAudioDecoder == NULL) 1127 && (mVideoEOS || mVideoDecoder == NULL)) { 1128 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 1129 } 1130 } else if (what == Renderer::kWhatFlushComplete) { 1131 int32_t audio; 1132 CHECK(msg->findInt32("audio", &audio)); 1133 1134 if (audio) { 1135 mAudioEOS = false; 1136 } else { 1137 mVideoEOS = false; 1138 } 1139 1140 ALOGV("renderer %s flush completed.", audio ? "audio" : "video"); 1141 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED 1142 || mFlushingAudio == SHUT_DOWN)) { 1143 // Flush has been handled by tear down. 1144 break; 1145 } 1146 handleFlushComplete(audio, false /* isDecoder */); 1147 finishFlushIfPossible(); 1148 } else if (what == Renderer::kWhatVideoRenderingStart) { 1149 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0); 1150 } else if (what == Renderer::kWhatMediaRenderingStart) { 1151 ALOGV("media rendering started"); 1152 notifyListener(MEDIA_STARTED, 0, 0); 1153 } else if (what == Renderer::kWhatAudioTearDown) { 1154 int32_t reason; 1155 CHECK(msg->findInt32("reason", &reason)); 1156 ALOGV("Tear down audio with reason %d.", reason); 1157 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) { 1158 // TimeoutWhenPaused is only for offload mode. 1159 ALOGW("Receive a stale message for teardown."); 1160 break; 1161 } 1162 int64_t positionUs; 1163 if (!msg->findInt64("positionUs", &positionUs)) { 1164 positionUs = mPreviousSeekTimeUs; 1165 } 1166 1167 restartAudio( 1168 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */, 1169 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */); 1170 } 1171 break; 1172 } 1173 1174 case kWhatMoreDataQueued: 1175 { 1176 break; 1177 } 1178 1179 case kWhatReset: 1180 { 1181 ALOGV("kWhatReset"); 1182 1183 mResetting = true; 1184 1185 mDeferredActions.push_back( 1186 new FlushDecoderAction( 1187 FLUSH_CMD_SHUTDOWN /* audio */, 1188 FLUSH_CMD_SHUTDOWN /* video */)); 1189 1190 mDeferredActions.push_back( 1191 new SimpleAction(&NuPlayer::performReset)); 1192 1193 processDeferredActions(); 1194 break; 1195 } 1196 1197 case kWhatSeek: 1198 { 1199 int64_t seekTimeUs; 1200 int32_t precise; 1201 int32_t needNotify; 1202 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 1203 CHECK(msg->findInt32("precise", &precise)); 1204 CHECK(msg->findInt32("needNotify", &needNotify)); 1205 1206 ALOGV("kWhatSeek seekTimeUs=%lld us, precise=%d, needNotify=%d", 1207 (long long)seekTimeUs, precise, needNotify); 1208 1209 if (!mStarted) { 1210 // Seek before the player is started. In order to preview video, 1211 // need to start the player and pause it. This branch is called 1212 // only once if needed. After the player is started, any seek 1213 // operation will go through normal path. 1214 // Audio-only cases are handled separately. 1215 onStart(seekTimeUs, precise); 1216 if (mStarted) { 1217 onPause(); 1218 mPausedByClient = true; 1219 } 1220 if (needNotify) { 1221 notifyDriverSeekComplete(); 1222 } 1223 break; 1224 } 1225 1226 mDeferredActions.push_back( 1227 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */, 1228 FLUSH_CMD_FLUSH /* video */)); 1229 1230 mDeferredActions.push_back( 1231 new SeekAction(seekTimeUs, precise)); 1232 1233 // After a flush without shutdown, decoder is paused. 1234 // Don't resume it until source seek is done, otherwise it could 1235 // start pulling stale data too soon. 1236 mDeferredActions.push_back( 1237 new ResumeDecoderAction(needNotify)); 1238 1239 processDeferredActions(); 1240 break; 1241 } 1242 1243 case kWhatPause: 1244 { 1245 onPause(); 1246 mPausedByClient = true; 1247 break; 1248 } 1249 1250 case kWhatSourceNotify: 1251 { 1252 onSourceNotify(msg); 1253 break; 1254 } 1255 1256 case kWhatClosedCaptionNotify: 1257 { 1258 onClosedCaptionNotify(msg); 1259 break; 1260 } 1261 1262 default: 1263 TRESPASS(); 1264 break; 1265 } 1266} 1267 1268void NuPlayer::onResume() { 1269 if (!mPaused || mResetting) { 1270 ALOGD_IF(mResetting, "resetting, onResume discarded"); 1271 return; 1272 } 1273 mPaused = false; 1274 if (mSource != NULL) { 1275 mSource->resume(); 1276 } else { 1277 ALOGW("resume called when source is gone or not set"); 1278 } 1279 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if 1280 // needed. 1281 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) { 1282 instantiateDecoder(true /* audio */, &mAudioDecoder); 1283 } 1284 if (mRenderer != NULL) { 1285 mRenderer->resume(); 1286 } else { 1287 ALOGW("resume called when renderer is gone or not set"); 1288 } 1289} 1290 1291status_t NuPlayer::onInstantiateSecureDecoders() { 1292 status_t err; 1293 if (!(mSourceFlags & Source::FLAG_SECURE)) { 1294 return BAD_TYPE; 1295 } 1296 1297 if (mRenderer != NULL) { 1298 ALOGE("renderer should not be set when instantiating secure decoders"); 1299 return UNKNOWN_ERROR; 1300 } 1301 1302 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting 1303 // data on instantiation. 1304 if (mSurface != NULL) { 1305 err = instantiateDecoder(false, &mVideoDecoder); 1306 if (err != OK) { 1307 return err; 1308 } 1309 } 1310 1311 if (mAudioSink != NULL) { 1312 err = instantiateDecoder(true, &mAudioDecoder); 1313 if (err != OK) { 1314 return err; 1315 } 1316 } 1317 return OK; 1318} 1319 1320void NuPlayer::onStart(int64_t startPositionUs, bool precise) { 1321 if (!mSourceStarted) { 1322 mSourceStarted = true; 1323 mSource->start(); 1324 } 1325 if (startPositionUs > 0) { 1326 performSeek(startPositionUs, precise); 1327 if (mSource->getFormat(false /* audio */) == NULL) { 1328 return; 1329 } 1330 } 1331 1332 mOffloadAudio = false; 1333 mAudioEOS = false; 1334 mVideoEOS = false; 1335 mStarted = true; 1336 mPaused = false; 1337 1338 uint32_t flags = 0; 1339 1340 if (mSource->isRealTime()) { 1341 flags |= Renderer::FLAG_REAL_TIME; 1342 } 1343 1344 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1345 sp<MetaData> videoMeta = mSource->getFormatMeta(false /* audio */); 1346 if (audioMeta == NULL && videoMeta == NULL) { 1347 ALOGE("no metadata for either audio or video source"); 1348 mSource->stop(); 1349 mSourceStarted = false; 1350 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED); 1351 return; 1352 } 1353 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream 1354 1355 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC; 1356 if (mAudioSink != NULL) { 1357 streamType = mAudioSink->getAudioStreamType(); 1358 } 1359 1360 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 1361 1362 mOffloadAudio = 1363 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType) 1364 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f); 1365 if (mOffloadAudio) { 1366 flags |= Renderer::FLAG_OFFLOAD_AUDIO; 1367 } 1368 1369 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this); 1370 ++mRendererGeneration; 1371 notify->setInt32("generation", mRendererGeneration); 1372 mRenderer = new Renderer(mAudioSink, notify, flags); 1373 mRendererLooper = new ALooper; 1374 mRendererLooper->setName("NuPlayerRenderer"); 1375 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO); 1376 mRendererLooper->registerHandler(mRenderer); 1377 1378 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings); 1379 if (err != OK) { 1380 mSource->stop(); 1381 mSourceStarted = false; 1382 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 1383 return; 1384 } 1385 1386 float rate = getFrameRate(); 1387 if (rate > 0) { 1388 mRenderer->setVideoFrameRate(rate); 1389 } 1390 1391 if (mVideoDecoder != NULL) { 1392 mVideoDecoder->setRenderer(mRenderer); 1393 } 1394 if (mAudioDecoder != NULL) { 1395 mAudioDecoder->setRenderer(mRenderer); 1396 } 1397 1398 postScanSources(); 1399} 1400 1401void NuPlayer::onPause() { 1402 if (mPaused) { 1403 return; 1404 } 1405 mPaused = true; 1406 if (mSource != NULL) { 1407 mSource->pause(); 1408 } else { 1409 ALOGW("pause called when source is gone or not set"); 1410 } 1411 if (mRenderer != NULL) { 1412 mRenderer->pause(); 1413 } else { 1414 ALOGW("pause called when renderer is gone or not set"); 1415 } 1416} 1417 1418bool NuPlayer::audioDecoderStillNeeded() { 1419 // Audio decoder is no longer needed if it's in shut/shutting down status. 1420 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER)); 1421} 1422 1423void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) { 1424 // We wait for both the decoder flush and the renderer flush to complete 1425 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state. 1426 1427 mFlushComplete[audio][isDecoder] = true; 1428 if (!mFlushComplete[audio][!isDecoder]) { 1429 return; 1430 } 1431 1432 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo; 1433 switch (*state) { 1434 case FLUSHING_DECODER: 1435 { 1436 *state = FLUSHED; 1437 break; 1438 } 1439 1440 case FLUSHING_DECODER_SHUTDOWN: 1441 { 1442 *state = SHUTTING_DOWN_DECODER; 1443 1444 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video"); 1445 if (!audio) { 1446 // Widevine source reads must stop before releasing the video decoder. 1447 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) { 1448 mSource->stop(); 1449 mSourceStarted = false; 1450 } 1451 } 1452 getDecoder(audio)->initiateShutdown(); 1453 break; 1454 } 1455 1456 default: 1457 // decoder flush completes only occur in a flushing state. 1458 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state); 1459 break; 1460 } 1461} 1462 1463void NuPlayer::finishFlushIfPossible() { 1464 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED 1465 && mFlushingAudio != SHUT_DOWN) { 1466 return; 1467 } 1468 1469 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED 1470 && mFlushingVideo != SHUT_DOWN) { 1471 return; 1472 } 1473 1474 ALOGV("both audio and video are flushed now."); 1475 1476 mFlushingAudio = NONE; 1477 mFlushingVideo = NONE; 1478 1479 clearFlushComplete(); 1480 1481 processDeferredActions(); 1482} 1483 1484void NuPlayer::postScanSources() { 1485 if (mScanSourcesPending) { 1486 return; 1487 } 1488 1489 sp<AMessage> msg = new AMessage(kWhatScanSources, this); 1490 msg->setInt32("generation", mScanSourcesGeneration); 1491 msg->post(); 1492 1493 mScanSourcesPending = true; 1494} 1495 1496void NuPlayer::tryOpenAudioSinkForOffload( 1497 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) { 1498 // Note: This is called early in NuPlayer to determine whether offloading 1499 // is possible; otherwise the decoders call the renderer openAudioSink directly. 1500 1501 status_t err = mRenderer->openAudioSink( 1502 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio); 1503 if (err != OK) { 1504 // Any failure we turn off mOffloadAudio. 1505 mOffloadAudio = false; 1506 } else if (mOffloadAudio) { 1507 sendMetaDataToHal(mAudioSink, audioMeta); 1508 } 1509} 1510 1511void NuPlayer::closeAudioSink() { 1512 mRenderer->closeAudioSink(); 1513} 1514 1515void NuPlayer::restartAudio( 1516 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) { 1517 if (mAudioDecoder != NULL) { 1518 mAudioDecoder->pause(); 1519 mAudioDecoder.clear(); 1520 ++mAudioDecoderGeneration; 1521 } 1522 if (mFlushingAudio == FLUSHING_DECODER) { 1523 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true; 1524 mFlushingAudio = FLUSHED; 1525 finishFlushIfPossible(); 1526 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN 1527 || mFlushingAudio == SHUTTING_DOWN_DECODER) { 1528 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true; 1529 mFlushingAudio = SHUT_DOWN; 1530 finishFlushIfPossible(); 1531 needsToCreateAudioDecoder = false; 1532 } 1533 if (mRenderer == NULL) { 1534 return; 1535 } 1536 closeAudioSink(); 1537 mRenderer->flush(true /* audio */, false /* notifyComplete */); 1538 if (mVideoDecoder != NULL) { 1539 mRenderer->flush(false /* audio */, false /* notifyComplete */); 1540 } 1541 1542 performSeek(currentPositionUs, false /* precise */); 1543 1544 if (forceNonOffload) { 1545 mRenderer->signalDisableOffloadAudio(); 1546 mOffloadAudio = false; 1547 } 1548 if (needsToCreateAudioDecoder) { 1549 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload); 1550 } 1551} 1552 1553void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) { 1554 if (mSource == NULL || mAudioSink == NULL) { 1555 return; 1556 } 1557 1558 if (mRenderer == NULL) { 1559 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety."); 1560 mOffloadAudio = false; 1561 return; 1562 } 1563 1564 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1565 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 1566 audio_stream_type_t streamType = mAudioSink->getAudioStreamType(); 1567 const bool hasVideo = (videoFormat != NULL); 1568 const bool canOffload = canOffloadStream( 1569 audioMeta, hasVideo, mSource->isStreaming(), streamType) 1570 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f); 1571 if (canOffload) { 1572 if (!mOffloadAudio) { 1573 mRenderer->signalEnableOffloadAudio(); 1574 } 1575 // open audio sink early under offload mode. 1576 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo); 1577 } else { 1578 if (mOffloadAudio) { 1579 mRenderer->signalDisableOffloadAudio(); 1580 mOffloadAudio = false; 1581 } 1582 } 1583} 1584 1585status_t NuPlayer::instantiateDecoder( 1586 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) { 1587 // The audio decoder could be cleared by tear down. If still in shut down 1588 // process, no need to create a new audio decoder. 1589 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) { 1590 return OK; 1591 } 1592 1593 sp<AMessage> format = mSource->getFormat(audio); 1594 1595 if (format == NULL) { 1596 return UNKNOWN_ERROR; 1597 } else { 1598 status_t err; 1599 if (format->findInt32("err", &err) && err) { 1600 return err; 1601 } 1602 } 1603 1604 format->setInt32("priority", 0 /* realtime */); 1605 1606 if (!audio) { 1607 AString mime; 1608 CHECK(format->findString("mime", &mime)); 1609 1610 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this); 1611 if (mCCDecoder == NULL) { 1612 mCCDecoder = new CCDecoder(ccNotify); 1613 } 1614 1615 if (mSourceFlags & Source::FLAG_SECURE) { 1616 format->setInt32("secure", true); 1617 } 1618 1619 if (mSourceFlags & Source::FLAG_PROTECTED) { 1620 format->setInt32("protected", true); 1621 } 1622 1623 float rate = getFrameRate(); 1624 if (rate > 0) { 1625 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed); 1626 } 1627 } 1628 1629 if (audio) { 1630 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this); 1631 ++mAudioDecoderGeneration; 1632 notify->setInt32("generation", mAudioDecoderGeneration); 1633 1634 if (checkAudioModeChange) { 1635 determineAudioModeChange(format); 1636 } 1637 if (mOffloadAudio) { 1638 mSource->setOffloadAudio(true /* offload */); 1639 1640 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL); 1641 format->setInt32("has-video", hasVideo); 1642 *decoder = new DecoderPassThrough(notify, mSource, mRenderer); 1643 } else { 1644 mSource->setOffloadAudio(false /* offload */); 1645 1646 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer); 1647 } 1648 } else { 1649 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this); 1650 ++mVideoDecoderGeneration; 1651 notify->setInt32("generation", mVideoDecoderGeneration); 1652 1653 *decoder = new Decoder( 1654 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder); 1655 1656 // enable FRC if high-quality AV sync is requested, even if not 1657 // directly queuing to display, as this will even improve textureview 1658 // playback. 1659 { 1660 char value[PROPERTY_VALUE_MAX]; 1661 if (property_get("persist.sys.media.avsync", value, NULL) && 1662 (!strcmp("1", value) || !strcasecmp("true", value))) { 1663 format->setInt32("auto-frc", 1); 1664 } 1665 } 1666 } 1667 (*decoder)->init(); 1668 (*decoder)->configure(format); 1669 1670 if (!audio) { 1671 sp<AMessage> params = new AMessage(); 1672 float rate = getFrameRate(); 1673 if (rate > 0) { 1674 params->setFloat("frame-rate-total", rate); 1675 } 1676 1677 sp<MetaData> fileMeta = getFileMeta(); 1678 if (fileMeta != NULL) { 1679 int32_t videoTemporalLayerCount; 1680 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount) 1681 && videoTemporalLayerCount > 0) { 1682 params->setInt32("temporal-layer-count", videoTemporalLayerCount); 1683 } 1684 } 1685 1686 if (params->countEntries() > 0) { 1687 (*decoder)->setParameters(params); 1688 } 1689 } 1690 return OK; 1691} 1692 1693void NuPlayer::updateVideoSize( 1694 const sp<AMessage> &inputFormat, 1695 const sp<AMessage> &outputFormat) { 1696 if (inputFormat == NULL) { 1697 ALOGW("Unknown video size, reporting 0x0!"); 1698 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0); 1699 return; 1700 } 1701 1702 int32_t displayWidth, displayHeight; 1703 if (outputFormat != NULL) { 1704 int32_t width, height; 1705 CHECK(outputFormat->findInt32("width", &width)); 1706 CHECK(outputFormat->findInt32("height", &height)); 1707 1708 int32_t cropLeft, cropTop, cropRight, cropBottom; 1709 CHECK(outputFormat->findRect( 1710 "crop", 1711 &cropLeft, &cropTop, &cropRight, &cropBottom)); 1712 1713 displayWidth = cropRight - cropLeft + 1; 1714 displayHeight = cropBottom - cropTop + 1; 1715 1716 ALOGV("Video output format changed to %d x %d " 1717 "(crop: %d x %d @ (%d, %d))", 1718 width, height, 1719 displayWidth, 1720 displayHeight, 1721 cropLeft, cropTop); 1722 } else { 1723 CHECK(inputFormat->findInt32("width", &displayWidth)); 1724 CHECK(inputFormat->findInt32("height", &displayHeight)); 1725 1726 ALOGV("Video input format %d x %d", displayWidth, displayHeight); 1727 } 1728 1729 // Take into account sample aspect ratio if necessary: 1730 int32_t sarWidth, sarHeight; 1731 if (inputFormat->findInt32("sar-width", &sarWidth) 1732 && inputFormat->findInt32("sar-height", &sarHeight)) { 1733 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight); 1734 1735 displayWidth = (displayWidth * sarWidth) / sarHeight; 1736 1737 ALOGV("display dimensions %d x %d", displayWidth, displayHeight); 1738 } else { 1739 int32_t width, height; 1740 if (inputFormat->findInt32("display-width", &width) 1741 && inputFormat->findInt32("display-height", &height) 1742 && width > 0 && height > 0 1743 && displayWidth > 0 && displayHeight > 0) { 1744 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) { 1745 displayHeight = (int32_t)(displayWidth * (int64_t)height / width); 1746 } else { 1747 displayWidth = (int32_t)(displayHeight * (int64_t)width / height); 1748 } 1749 ALOGV("Video display width and height are overridden to %d x %d", 1750 displayWidth, displayHeight); 1751 } 1752 } 1753 1754 int32_t rotationDegrees; 1755 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) { 1756 rotationDegrees = 0; 1757 } 1758 1759 if (rotationDegrees == 90 || rotationDegrees == 270) { 1760 int32_t tmp = displayWidth; 1761 displayWidth = displayHeight; 1762 displayHeight = tmp; 1763 } 1764 1765 notifyListener( 1766 MEDIA_SET_VIDEO_SIZE, 1767 displayWidth, 1768 displayHeight); 1769} 1770 1771void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) { 1772 if (mDriver == NULL) { 1773 return; 1774 } 1775 1776 sp<NuPlayerDriver> driver = mDriver.promote(); 1777 1778 if (driver == NULL) { 1779 return; 1780 } 1781 1782 driver->notifyListener(msg, ext1, ext2, in); 1783} 1784 1785void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 1786 ALOGV("[%s] flushDecoder needShutdown=%d", 1787 audio ? "audio" : "video", needShutdown); 1788 1789 const sp<DecoderBase> &decoder = getDecoder(audio); 1790 if (decoder == NULL) { 1791 ALOGI("flushDecoder %s without decoder present", 1792 audio ? "audio" : "video"); 1793 return; 1794 } 1795 1796 // Make sure we don't continue to scan sources until we finish flushing. 1797 ++mScanSourcesGeneration; 1798 if (mScanSourcesPending) { 1799 if (!needShutdown) { 1800 mDeferredActions.push_back( 1801 new SimpleAction(&NuPlayer::performScanSources)); 1802 } 1803 mScanSourcesPending = false; 1804 } 1805 1806 decoder->signalFlush(); 1807 1808 FlushStatus newStatus = 1809 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 1810 1811 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL); 1812 mFlushComplete[audio][true /* isDecoder */] = false; 1813 if (audio) { 1814 ALOGE_IF(mFlushingAudio != NONE, 1815 "audio flushDecoder() is called in state %d", mFlushingAudio); 1816 mFlushingAudio = newStatus; 1817 } else { 1818 ALOGE_IF(mFlushingVideo != NONE, 1819 "video flushDecoder() is called in state %d", mFlushingVideo); 1820 mFlushingVideo = newStatus; 1821 } 1822} 1823 1824void NuPlayer::queueDecoderShutdown( 1825 bool audio, bool video, const sp<AMessage> &reply) { 1826 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video); 1827 1828 mDeferredActions.push_back( 1829 new FlushDecoderAction( 1830 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 1831 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE)); 1832 1833 mDeferredActions.push_back( 1834 new SimpleAction(&NuPlayer::performScanSources)); 1835 1836 mDeferredActions.push_back(new PostMessageAction(reply)); 1837 1838 processDeferredActions(); 1839} 1840 1841status_t NuPlayer::setVideoScalingMode(int32_t mode) { 1842 mVideoScalingMode = mode; 1843 if (mSurface != NULL) { 1844 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode); 1845 if (ret != OK) { 1846 ALOGE("Failed to set scaling mode (%d): %s", 1847 -ret, strerror(-ret)); 1848 return ret; 1849 } 1850 } 1851 return OK; 1852} 1853 1854status_t NuPlayer::getTrackInfo(Parcel* reply) const { 1855 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this); 1856 msg->setPointer("reply", reply); 1857 1858 sp<AMessage> response; 1859 status_t err = msg->postAndAwaitResponse(&response); 1860 return err; 1861} 1862 1863status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const { 1864 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this); 1865 msg->setPointer("reply", reply); 1866 msg->setInt32("type", type); 1867 1868 sp<AMessage> response; 1869 status_t err = msg->postAndAwaitResponse(&response); 1870 if (err == OK && response != NULL) { 1871 CHECK(response->findInt32("err", &err)); 1872 } 1873 return err; 1874} 1875 1876status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) { 1877 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this); 1878 msg->setSize("trackIndex", trackIndex); 1879 msg->setInt32("select", select); 1880 msg->setInt64("timeUs", timeUs); 1881 1882 sp<AMessage> response; 1883 status_t err = msg->postAndAwaitResponse(&response); 1884 1885 if (err != OK) { 1886 return err; 1887 } 1888 1889 if (!response->findInt32("err", &err)) { 1890 err = OK; 1891 } 1892 1893 return err; 1894} 1895 1896status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) { 1897 sp<Renderer> renderer = mRenderer; 1898 if (renderer == NULL) { 1899 return NO_INIT; 1900 } 1901 1902 return renderer->getCurrentPosition(mediaUs); 1903} 1904 1905void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) { 1906 CHECK(mTrackStats != NULL); 1907 1908 mTrackStats->clear(); 1909 if (mVideoDecoder != NULL) { 1910 mTrackStats->push_back(mVideoDecoder->getStats()); 1911 } 1912 if (mAudioDecoder != NULL) { 1913 mTrackStats->push_back(mAudioDecoder->getStats()); 1914 } 1915} 1916 1917sp<MetaData> NuPlayer::getFileMeta() { 1918 return mSource->getFileFormatMeta(); 1919} 1920 1921float NuPlayer::getFrameRate() { 1922 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */); 1923 if (meta == NULL) { 1924 return 0; 1925 } 1926 int32_t rate; 1927 if (!meta->findInt32(kKeyFrameRate, &rate)) { 1928 // fall back to try file meta 1929 sp<MetaData> fileMeta = getFileMeta(); 1930 if (fileMeta == NULL) { 1931 ALOGW("source has video meta but not file meta"); 1932 return -1; 1933 } 1934 int32_t fileMetaRate; 1935 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) { 1936 return -1; 1937 } 1938 return fileMetaRate; 1939 } 1940 return rate; 1941} 1942 1943void NuPlayer::schedulePollDuration() { 1944 sp<AMessage> msg = new AMessage(kWhatPollDuration, this); 1945 msg->setInt32("generation", mPollDurationGeneration); 1946 msg->post(); 1947} 1948 1949void NuPlayer::cancelPollDuration() { 1950 ++mPollDurationGeneration; 1951} 1952 1953void NuPlayer::processDeferredActions() { 1954 while (!mDeferredActions.empty()) { 1955 // We won't execute any deferred actions until we're no longer in 1956 // an intermediate state, i.e. one more more decoders are currently 1957 // flushing or shutting down. 1958 1959 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 1960 // We're currently flushing, postpone the reset until that's 1961 // completed. 1962 1963 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d", 1964 mFlushingAudio, mFlushingVideo); 1965 1966 break; 1967 } 1968 1969 sp<Action> action = *mDeferredActions.begin(); 1970 mDeferredActions.erase(mDeferredActions.begin()); 1971 1972 action->execute(this); 1973 } 1974} 1975 1976void NuPlayer::performSeek(int64_t seekTimeUs, bool precise) { 1977 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), precise=%d", 1978 (long long)seekTimeUs, seekTimeUs / 1E6, precise); 1979 1980 if (mSource == NULL) { 1981 // This happens when reset occurs right before the loop mode 1982 // asynchronously seeks to the start of the stream. 1983 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL, 1984 "mSource is NULL and decoders not NULL audio(%p) video(%p)", 1985 mAudioDecoder.get(), mVideoDecoder.get()); 1986 return; 1987 } 1988 mPreviousSeekTimeUs = seekTimeUs; 1989 mSource->seekTo(seekTimeUs, precise); 1990 ++mTimedTextGeneration; 1991 1992 // everything's flushed, continue playback. 1993} 1994 1995void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) { 1996 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video); 1997 1998 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL) 1999 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) { 2000 return; 2001 } 2002 2003 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) { 2004 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN)); 2005 } 2006 2007 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) { 2008 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN)); 2009 } 2010} 2011 2012void NuPlayer::performReset() { 2013 ALOGV("performReset"); 2014 2015 CHECK(mAudioDecoder == NULL); 2016 CHECK(mVideoDecoder == NULL); 2017 2018 cancelPollDuration(); 2019 2020 ++mScanSourcesGeneration; 2021 mScanSourcesPending = false; 2022 2023 if (mRendererLooper != NULL) { 2024 if (mRenderer != NULL) { 2025 mRendererLooper->unregisterHandler(mRenderer->id()); 2026 } 2027 mRendererLooper->stop(); 2028 mRendererLooper.clear(); 2029 } 2030 mRenderer.clear(); 2031 ++mRendererGeneration; 2032 2033 if (mSource != NULL) { 2034 mSource->stop(); 2035 2036 Mutex::Autolock autoLock(mSourceLock); 2037 mSource.clear(); 2038 } 2039 2040 if (mDriver != NULL) { 2041 sp<NuPlayerDriver> driver = mDriver.promote(); 2042 if (driver != NULL) { 2043 driver->notifyResetComplete(); 2044 } 2045 } 2046 2047 mStarted = false; 2048 mPrepared = false; 2049 mResetting = false; 2050 mSourceStarted = false; 2051} 2052 2053void NuPlayer::performScanSources() { 2054 ALOGV("performScanSources"); 2055 2056 if (!mStarted) { 2057 return; 2058 } 2059 2060 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 2061 postScanSources(); 2062 } 2063} 2064 2065void NuPlayer::performSetSurface(const sp<Surface> &surface) { 2066 ALOGV("performSetSurface"); 2067 2068 mSurface = surface; 2069 2070 // XXX - ignore error from setVideoScalingMode for now 2071 setVideoScalingMode(mVideoScalingMode); 2072 2073 if (mDriver != NULL) { 2074 sp<NuPlayerDriver> driver = mDriver.promote(); 2075 if (driver != NULL) { 2076 driver->notifySetSurfaceComplete(); 2077 } 2078 } 2079} 2080 2081void NuPlayer::performResumeDecoders(bool needNotify) { 2082 if (needNotify) { 2083 mResumePending = true; 2084 if (mVideoDecoder == NULL) { 2085 // if audio-only, we can notify seek complete now, 2086 // as the resume operation will be relatively fast. 2087 finishResume(); 2088 } 2089 } 2090 2091 if (mVideoDecoder != NULL) { 2092 // When there is continuous seek, MediaPlayer will cache the seek 2093 // position, and send down new seek request when previous seek is 2094 // complete. Let's wait for at least one video output frame before 2095 // notifying seek complete, so that the video thumbnail gets updated 2096 // when seekbar is dragged. 2097 mVideoDecoder->signalResume(needNotify); 2098 } 2099 2100 if (mAudioDecoder != NULL) { 2101 mAudioDecoder->signalResume(false /* needNotify */); 2102 } 2103} 2104 2105void NuPlayer::finishResume() { 2106 if (mResumePending) { 2107 mResumePending = false; 2108 notifyDriverSeekComplete(); 2109 } 2110} 2111 2112void NuPlayer::notifyDriverSeekComplete() { 2113 if (mDriver != NULL) { 2114 sp<NuPlayerDriver> driver = mDriver.promote(); 2115 if (driver != NULL) { 2116 driver->notifySeekComplete(); 2117 } 2118 } 2119} 2120 2121void NuPlayer::onSourceNotify(const sp<AMessage> &msg) { 2122 int32_t what; 2123 CHECK(msg->findInt32("what", &what)); 2124 2125 switch (what) { 2126 case Source::kWhatInstantiateSecureDecoders: 2127 { 2128 if (mSource == NULL) { 2129 // This is a stale notification from a source that was 2130 // asynchronously preparing when the client called reset(). 2131 // We handled the reset, the source is gone. 2132 break; 2133 } 2134 2135 sp<AMessage> reply; 2136 CHECK(msg->findMessage("reply", &reply)); 2137 status_t err = onInstantiateSecureDecoders(); 2138 reply->setInt32("err", err); 2139 reply->post(); 2140 break; 2141 } 2142 2143 case Source::kWhatPrepared: 2144 { 2145 if (mSource == NULL) { 2146 // This is a stale notification from a source that was 2147 // asynchronously preparing when the client called reset(). 2148 // We handled the reset, the source is gone. 2149 break; 2150 } 2151 2152 int32_t err; 2153 CHECK(msg->findInt32("err", &err)); 2154 2155 if (err != OK) { 2156 // shut down potential secure codecs in case client never calls reset 2157 mDeferredActions.push_back( 2158 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */, 2159 FLUSH_CMD_SHUTDOWN /* video */)); 2160 processDeferredActions(); 2161 } else { 2162 mPrepared = true; 2163 } 2164 2165 sp<NuPlayerDriver> driver = mDriver.promote(); 2166 if (driver != NULL) { 2167 // notify duration first, so that it's definitely set when 2168 // the app received the "prepare complete" callback. 2169 int64_t durationUs; 2170 if (mSource->getDuration(&durationUs) == OK) { 2171 driver->notifyDuration(durationUs); 2172 } 2173 driver->notifyPrepareCompleted(err); 2174 } 2175 2176 break; 2177 } 2178 2179 case Source::kWhatFlagsChanged: 2180 { 2181 uint32_t flags; 2182 CHECK(msg->findInt32("flags", (int32_t *)&flags)); 2183 2184 sp<NuPlayerDriver> driver = mDriver.promote(); 2185 if (driver != NULL) { 2186 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) { 2187 driver->notifyListener( 2188 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0); 2189 } 2190 driver->notifyFlagsChanged(flags); 2191 } 2192 2193 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2194 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) { 2195 cancelPollDuration(); 2196 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2197 && (flags & Source::FLAG_DYNAMIC_DURATION) 2198 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 2199 schedulePollDuration(); 2200 } 2201 2202 mSourceFlags = flags; 2203 break; 2204 } 2205 2206 case Source::kWhatVideoSizeChanged: 2207 { 2208 sp<AMessage> format; 2209 CHECK(msg->findMessage("format", &format)); 2210 2211 updateVideoSize(format); 2212 break; 2213 } 2214 2215 case Source::kWhatBufferingUpdate: 2216 { 2217 int32_t percentage; 2218 CHECK(msg->findInt32("percentage", &percentage)); 2219 2220 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0); 2221 break; 2222 } 2223 2224 case Source::kWhatPauseOnBufferingStart: 2225 { 2226 // ignore if not playing 2227 if (mStarted) { 2228 ALOGI("buffer low, pausing..."); 2229 2230 mPausedForBuffering = true; 2231 onPause(); 2232 } 2233 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0); 2234 break; 2235 } 2236 2237 case Source::kWhatResumeOnBufferingEnd: 2238 { 2239 // ignore if not playing 2240 if (mStarted) { 2241 ALOGI("buffer ready, resuming..."); 2242 2243 mPausedForBuffering = false; 2244 2245 // do not resume yet if client didn't unpause 2246 if (!mPausedByClient) { 2247 onResume(); 2248 } 2249 } 2250 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0); 2251 break; 2252 } 2253 2254 case Source::kWhatCacheStats: 2255 { 2256 int32_t kbps; 2257 CHECK(msg->findInt32("bandwidth", &kbps)); 2258 2259 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps); 2260 break; 2261 } 2262 2263 case Source::kWhatSubtitleData: 2264 { 2265 sp<ABuffer> buffer; 2266 CHECK(msg->findBuffer("buffer", &buffer)); 2267 2268 sendSubtitleData(buffer, 0 /* baseIndex */); 2269 break; 2270 } 2271 2272 case Source::kWhatTimedMetaData: 2273 { 2274 sp<ABuffer> buffer; 2275 if (!msg->findBuffer("buffer", &buffer)) { 2276 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2277 } else { 2278 sendTimedMetaData(buffer); 2279 } 2280 break; 2281 } 2282 2283 case Source::kWhatTimedTextData: 2284 { 2285 int32_t generation; 2286 if (msg->findInt32("generation", &generation) 2287 && generation != mTimedTextGeneration) { 2288 break; 2289 } 2290 2291 sp<ABuffer> buffer; 2292 CHECK(msg->findBuffer("buffer", &buffer)); 2293 2294 sp<NuPlayerDriver> driver = mDriver.promote(); 2295 if (driver == NULL) { 2296 break; 2297 } 2298 2299 int posMs; 2300 int64_t timeUs, posUs; 2301 driver->getCurrentPosition(&posMs); 2302 posUs = (int64_t) posMs * 1000ll; 2303 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2304 2305 if (posUs < timeUs) { 2306 if (!msg->findInt32("generation", &generation)) { 2307 msg->setInt32("generation", mTimedTextGeneration); 2308 } 2309 msg->post(timeUs - posUs); 2310 } else { 2311 sendTimedTextData(buffer); 2312 } 2313 break; 2314 } 2315 2316 case Source::kWhatQueueDecoderShutdown: 2317 { 2318 int32_t audio, video; 2319 CHECK(msg->findInt32("audio", &audio)); 2320 CHECK(msg->findInt32("video", &video)); 2321 2322 sp<AMessage> reply; 2323 CHECK(msg->findMessage("reply", &reply)); 2324 2325 queueDecoderShutdown(audio, video, reply); 2326 break; 2327 } 2328 2329 case Source::kWhatDrmNoLicense: 2330 { 2331 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE); 2332 break; 2333 } 2334 2335 default: 2336 TRESPASS(); 2337 } 2338} 2339 2340void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) { 2341 int32_t what; 2342 CHECK(msg->findInt32("what", &what)); 2343 2344 switch (what) { 2345 case NuPlayer::CCDecoder::kWhatClosedCaptionData: 2346 { 2347 sp<ABuffer> buffer; 2348 CHECK(msg->findBuffer("buffer", &buffer)); 2349 2350 size_t inbandTracks = 0; 2351 if (mSource != NULL) { 2352 inbandTracks = mSource->getTrackCount(); 2353 } 2354 2355 sendSubtitleData(buffer, inbandTracks); 2356 break; 2357 } 2358 2359 case NuPlayer::CCDecoder::kWhatTrackAdded: 2360 { 2361 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2362 2363 break; 2364 } 2365 2366 default: 2367 TRESPASS(); 2368 } 2369 2370 2371} 2372 2373void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) { 2374 int32_t trackIndex; 2375 int64_t timeUs, durationUs; 2376 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex)); 2377 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2378 CHECK(buffer->meta()->findInt64("durationUs", &durationUs)); 2379 2380 Parcel in; 2381 in.writeInt32(trackIndex + baseIndex); 2382 in.writeInt64(timeUs); 2383 in.writeInt64(durationUs); 2384 in.writeInt32(buffer->size()); 2385 in.writeInt32(buffer->size()); 2386 in.write(buffer->data(), buffer->size()); 2387 2388 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in); 2389} 2390 2391void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) { 2392 int64_t timeUs; 2393 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2394 2395 Parcel in; 2396 in.writeInt64(timeUs); 2397 in.writeInt32(buffer->size()); 2398 in.writeInt32(buffer->size()); 2399 in.write(buffer->data(), buffer->size()); 2400 2401 notifyListener(MEDIA_META_DATA, 0, 0, &in); 2402} 2403 2404void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) { 2405 const void *data; 2406 size_t size = 0; 2407 int64_t timeUs; 2408 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP; 2409 2410 AString mime; 2411 CHECK(buffer->meta()->findString("mime", &mime)); 2412 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0); 2413 2414 data = buffer->data(); 2415 size = buffer->size(); 2416 2417 Parcel parcel; 2418 if (size > 0) { 2419 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2420 int32_t global = 0; 2421 if (buffer->meta()->findInt32("global", &global) && global) { 2422 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS; 2423 } else { 2424 flag |= TextDescriptions::LOCAL_DESCRIPTIONS; 2425 } 2426 TextDescriptions::getParcelOfDescriptions( 2427 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel); 2428 } 2429 2430 if ((parcel.dataSize() > 0)) { 2431 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel); 2432 } else { // send an empty timed text 2433 notifyListener(MEDIA_TIMED_TEXT, 0, 0); 2434 } 2435} 2436//////////////////////////////////////////////////////////////////////////////// 2437 2438sp<AMessage> NuPlayer::Source::getFormat(bool audio) { 2439 sp<MetaData> meta = getFormatMeta(audio); 2440 2441 if (meta == NULL) { 2442 return NULL; 2443 } 2444 2445 sp<AMessage> msg = new AMessage; 2446 2447 if(convertMetaDataToMessage(meta, &msg) == OK) { 2448 return msg; 2449 } 2450 return NULL; 2451} 2452 2453void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) { 2454 sp<AMessage> notify = dupNotify(); 2455 notify->setInt32("what", kWhatFlagsChanged); 2456 notify->setInt32("flags", flags); 2457 notify->post(); 2458} 2459 2460void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) { 2461 sp<AMessage> notify = dupNotify(); 2462 notify->setInt32("what", kWhatVideoSizeChanged); 2463 notify->setMessage("format", format); 2464 notify->post(); 2465} 2466 2467void NuPlayer::Source::notifyPrepared(status_t err) { 2468 sp<AMessage> notify = dupNotify(); 2469 notify->setInt32("what", kWhatPrepared); 2470 notify->setInt32("err", err); 2471 notify->post(); 2472} 2473 2474void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) { 2475 sp<AMessage> notify = dupNotify(); 2476 notify->setInt32("what", kWhatInstantiateSecureDecoders); 2477 notify->setMessage("reply", reply); 2478 notify->post(); 2479} 2480 2481void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) { 2482 TRESPASS(); 2483} 2484 2485} // namespace android 2486