NuPlayer.cpp revision f2ae3e19080938db8cbf29a963fd744a3964fcc2
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) 74 : mSeekTimeUs(seekTimeUs) { 75 } 76 77 virtual void execute(NuPlayer *player) { 78 player->performSeek(mSeekTimeUs); 79 } 80 81private: 82 int64_t mSeekTimeUs; 83 84 DISALLOW_EVIL_CONSTRUCTORS(SeekAction); 85}; 86 87struct NuPlayer::ResumeDecoderAction : public Action { 88 explicit ResumeDecoderAction(bool needNotify) 89 : mNeedNotify(needNotify) { 90 } 91 92 virtual void execute(NuPlayer *player) { 93 player->performResumeDecoders(mNeedNotify); 94 } 95 96private: 97 bool mNeedNotify; 98 99 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction); 100}; 101 102struct NuPlayer::SetSurfaceAction : public Action { 103 explicit SetSurfaceAction(const sp<Surface> &surface) 104 : mSurface(surface) { 105 } 106 107 virtual void execute(NuPlayer *player) { 108 player->performSetSurface(mSurface); 109 } 110 111private: 112 sp<Surface> mSurface; 113 114 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction); 115}; 116 117struct NuPlayer::FlushDecoderAction : public Action { 118 FlushDecoderAction(FlushCommand audio, FlushCommand video) 119 : mAudio(audio), 120 mVideo(video) { 121 } 122 123 virtual void execute(NuPlayer *player) { 124 player->performDecoderFlush(mAudio, mVideo); 125 } 126 127private: 128 FlushCommand mAudio; 129 FlushCommand mVideo; 130 131 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction); 132}; 133 134struct NuPlayer::PostMessageAction : public Action { 135 explicit PostMessageAction(const sp<AMessage> &msg) 136 : mMessage(msg) { 137 } 138 139 virtual void execute(NuPlayer *) { 140 mMessage->post(); 141 } 142 143private: 144 sp<AMessage> mMessage; 145 146 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction); 147}; 148 149// Use this if there's no state necessary to save in order to execute 150// the action. 151struct NuPlayer::SimpleAction : public Action { 152 typedef void (NuPlayer::*ActionFunc)(); 153 154 explicit SimpleAction(ActionFunc func) 155 : mFunc(func) { 156 } 157 158 virtual void execute(NuPlayer *player) { 159 (player->*mFunc)(); 160 } 161 162private: 163 ActionFunc mFunc; 164 165 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction); 166}; 167 168//////////////////////////////////////////////////////////////////////////////// 169 170NuPlayer::NuPlayer(pid_t pid) 171 : mUIDValid(false), 172 mPID(pid), 173 mSourceFlags(0), 174 mOffloadAudio(false), 175 mAudioDecoderGeneration(0), 176 mVideoDecoderGeneration(0), 177 mRendererGeneration(0), 178 mPreviousSeekTimeUs(0), 179 mAudioEOS(false), 180 mVideoEOS(false), 181 mScanSourcesPending(false), 182 mScanSourcesGeneration(0), 183 mPollDurationGeneration(0), 184 mTimedTextGeneration(0), 185 mFlushingAudio(NONE), 186 mFlushingVideo(NONE), 187 mResumePending(false), 188 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW), 189 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT), 190 mVideoFpsHint(-1.f), 191 mStarted(false), 192 mPrepared(false), 193 mResetting(false), 194 mSourceStarted(false), 195 mPaused(false), 196 mPausedByClient(true), 197 mPausedForBuffering(false) { 198 clearFlushComplete(); 199} 200 201NuPlayer::~NuPlayer() { 202} 203 204void NuPlayer::setUID(uid_t uid) { 205 mUIDValid = true; 206 mUID = uid; 207} 208 209void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) { 210 mDriver = driver; 211} 212 213void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) { 214 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 215 216 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 217 218 msg->setObject("source", new StreamingSource(notify, source)); 219 msg->post(); 220} 221 222static bool IsHTTPLiveURL(const char *url) { 223 if (!strncasecmp("http://", url, 7) 224 || !strncasecmp("https://", url, 8) 225 || !strncasecmp("file://", url, 7)) { 226 size_t len = strlen(url); 227 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) { 228 return true; 229 } 230 231 if (strstr(url,"m3u8")) { 232 return true; 233 } 234 } 235 236 return false; 237} 238 239void NuPlayer::setDataSourceAsync( 240 const sp<IMediaHTTPService> &httpService, 241 const char *url, 242 const KeyedVector<String8, String8> *headers) { 243 244 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 245 size_t len = strlen(url); 246 247 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 248 249 sp<Source> source; 250 if (IsHTTPLiveURL(url)) { 251 source = new HTTPLiveSource(notify, httpService, url, headers); 252 } else if (!strncasecmp(url, "rtsp://", 7)) { 253 source = new RTSPSource( 254 notify, httpService, url, headers, mUIDValid, mUID); 255 } else if ((!strncasecmp(url, "http://", 7) 256 || !strncasecmp(url, "https://", 8)) 257 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4])) 258 || strstr(url, ".sdp?"))) { 259 source = new RTSPSource( 260 notify, httpService, url, headers, mUIDValid, mUID, true); 261 } else { 262 sp<GenericSource> genericSource = 263 new GenericSource(notify, mUIDValid, mUID); 264 // Don't set FLAG_SECURE on mSourceFlags here for widevine. 265 // The correct flags will be updated in Source::kWhatFlagsChanged 266 // handler when GenericSource is prepared. 267 268 status_t err = genericSource->setDataSource(httpService, url, headers); 269 270 if (err == OK) { 271 source = genericSource; 272 } else { 273 ALOGE("Failed to set data source!"); 274 } 275 } 276 msg->setObject("source", source); 277 msg->post(); 278} 279 280void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) { 281 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 282 283 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 284 285 sp<GenericSource> source = 286 new GenericSource(notify, mUIDValid, mUID); 287 288 status_t err = source->setDataSource(fd, offset, length); 289 290 if (err != OK) { 291 ALOGE("Failed to set data source!"); 292 source = NULL; 293 } 294 295 msg->setObject("source", source); 296 msg->post(); 297} 298 299void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) { 300 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 301 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 302 303 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID); 304 status_t err = source->setDataSource(dataSource); 305 306 if (err != OK) { 307 ALOGE("Failed to set data source!"); 308 source = NULL; 309 } 310 311 msg->setObject("source", source); 312 msg->post(); 313} 314 315void NuPlayer::prepareAsync() { 316 (new AMessage(kWhatPrepare, this))->post(); 317} 318 319void NuPlayer::setVideoSurfaceTextureAsync( 320 const sp<IGraphicBufferProducer> &bufferProducer) { 321 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this); 322 323 if (bufferProducer == NULL) { 324 msg->setObject("surface", NULL); 325 } else { 326 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */)); 327 } 328 329 msg->post(); 330} 331 332void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) { 333 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this); 334 msg->setObject("sink", sink); 335 msg->post(); 336} 337 338void NuPlayer::start() { 339 (new AMessage(kWhatStart, this))->post(); 340} 341 342status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) { 343 // do some cursory validation of the settings here. audio modes are 344 // only validated when set on the audiosink. 345 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN) 346 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX 347 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN 348 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) { 349 return BAD_VALUE; 350 } 351 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this); 352 writeToAMessage(msg, rate); 353 sp<AMessage> response; 354 status_t err = msg->postAndAwaitResponse(&response); 355 if (err == OK && response != NULL) { 356 CHECK(response->findInt32("err", &err)); 357 } 358 return err; 359} 360 361status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) { 362 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this); 363 sp<AMessage> response; 364 status_t err = msg->postAndAwaitResponse(&response); 365 if (err == OK && response != NULL) { 366 CHECK(response->findInt32("err", &err)); 367 if (err == OK) { 368 readFromAMessage(response, rate); 369 } 370 } 371 return err; 372} 373 374status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) { 375 sp<AMessage> msg = new AMessage(kWhatConfigSync, this); 376 writeToAMessage(msg, sync, videoFpsHint); 377 sp<AMessage> response; 378 status_t err = msg->postAndAwaitResponse(&response); 379 if (err == OK && response != NULL) { 380 CHECK(response->findInt32("err", &err)); 381 } 382 return err; 383} 384 385status_t NuPlayer::getSyncSettings( 386 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) { 387 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this); 388 sp<AMessage> response; 389 status_t err = msg->postAndAwaitResponse(&response); 390 if (err == OK && response != NULL) { 391 CHECK(response->findInt32("err", &err)); 392 if (err == OK) { 393 readFromAMessage(response, sync, videoFps); 394 } 395 } 396 return err; 397} 398 399void NuPlayer::pause() { 400 (new AMessage(kWhatPause, this))->post(); 401} 402 403void NuPlayer::resetAsync() { 404 sp<Source> source; 405 { 406 Mutex::Autolock autoLock(mSourceLock); 407 source = mSource; 408 } 409 410 if (source != NULL) { 411 // During a reset, the data source might be unresponsive already, we need to 412 // disconnect explicitly so that reads exit promptly. 413 // We can't queue the disconnect request to the looper, as it might be 414 // queued behind a stuck read and never gets processed. 415 // Doing a disconnect outside the looper to allows the pending reads to exit 416 // (either successfully or with error). 417 source->disconnect(); 418 } 419 420 (new AMessage(kWhatReset, this))->post(); 421} 422 423void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) { 424 sp<AMessage> msg = new AMessage(kWhatSeek, this); 425 msg->setInt64("seekTimeUs", seekTimeUs); 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)); 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 needNotify; 1201 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 1202 CHECK(msg->findInt32("needNotify", &needNotify)); 1203 1204 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d", 1205 (long long)seekTimeUs, needNotify); 1206 1207 if (!mStarted) { 1208 // Seek before the player is started. In order to preview video, 1209 // need to start the player and pause it. This branch is called 1210 // only once if needed. After the player is started, any seek 1211 // operation will go through normal path. 1212 // Audio-only cases are handled separately. 1213 onStart(seekTimeUs); 1214 if (mStarted) { 1215 onPause(); 1216 mPausedByClient = true; 1217 } 1218 if (needNotify) { 1219 notifyDriverSeekComplete(); 1220 } 1221 break; 1222 } 1223 1224 mDeferredActions.push_back( 1225 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */, 1226 FLUSH_CMD_FLUSH /* video */)); 1227 1228 mDeferredActions.push_back( 1229 new SeekAction(seekTimeUs)); 1230 1231 // After a flush without shutdown, decoder is paused. 1232 // Don't resume it until source seek is done, otherwise it could 1233 // start pulling stale data too soon. 1234 mDeferredActions.push_back( 1235 new ResumeDecoderAction(needNotify)); 1236 1237 processDeferredActions(); 1238 break; 1239 } 1240 1241 case kWhatPause: 1242 { 1243 onPause(); 1244 mPausedByClient = true; 1245 break; 1246 } 1247 1248 case kWhatSourceNotify: 1249 { 1250 onSourceNotify(msg); 1251 break; 1252 } 1253 1254 case kWhatClosedCaptionNotify: 1255 { 1256 onClosedCaptionNotify(msg); 1257 break; 1258 } 1259 1260 default: 1261 TRESPASS(); 1262 break; 1263 } 1264} 1265 1266void NuPlayer::onResume() { 1267 if (!mPaused || mResetting) { 1268 ALOGD_IF(mResetting, "resetting, onResume discarded"); 1269 return; 1270 } 1271 mPaused = false; 1272 if (mSource != NULL) { 1273 mSource->resume(); 1274 } else { 1275 ALOGW("resume called when source is gone or not set"); 1276 } 1277 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if 1278 // needed. 1279 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) { 1280 instantiateDecoder(true /* audio */, &mAudioDecoder); 1281 } 1282 if (mRenderer != NULL) { 1283 mRenderer->resume(); 1284 } else { 1285 ALOGW("resume called when renderer is gone or not set"); 1286 } 1287} 1288 1289status_t NuPlayer::onInstantiateSecureDecoders() { 1290 status_t err; 1291 if (!(mSourceFlags & Source::FLAG_SECURE)) { 1292 return BAD_TYPE; 1293 } 1294 1295 if (mRenderer != NULL) { 1296 ALOGE("renderer should not be set when instantiating secure decoders"); 1297 return UNKNOWN_ERROR; 1298 } 1299 1300 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting 1301 // data on instantiation. 1302 if (mSurface != NULL) { 1303 err = instantiateDecoder(false, &mVideoDecoder); 1304 if (err != OK) { 1305 return err; 1306 } 1307 } 1308 1309 if (mAudioSink != NULL) { 1310 err = instantiateDecoder(true, &mAudioDecoder); 1311 if (err != OK) { 1312 return err; 1313 } 1314 } 1315 return OK; 1316} 1317 1318void NuPlayer::onStart(int64_t startPositionUs) { 1319 if (!mSourceStarted) { 1320 mSourceStarted = true; 1321 mSource->start(); 1322 } 1323 if (startPositionUs > 0) { 1324 performSeek(startPositionUs); 1325 if (mSource->getFormat(false /* audio */) == NULL) { 1326 return; 1327 } 1328 } 1329 1330 mOffloadAudio = false; 1331 mAudioEOS = false; 1332 mVideoEOS = false; 1333 mStarted = true; 1334 mPaused = false; 1335 1336 uint32_t flags = 0; 1337 1338 if (mSource->isRealTime()) { 1339 flags |= Renderer::FLAG_REAL_TIME; 1340 } 1341 1342 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1343 sp<MetaData> videoMeta = mSource->getFormatMeta(false /* audio */); 1344 if (audioMeta == NULL && videoMeta == NULL) { 1345 ALOGE("no metadata for either audio or video source"); 1346 mSource->stop(); 1347 mSourceStarted = false; 1348 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_MALFORMED); 1349 return; 1350 } 1351 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream 1352 1353 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC; 1354 if (mAudioSink != NULL) { 1355 streamType = mAudioSink->getAudioStreamType(); 1356 } 1357 1358 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 1359 1360 mOffloadAudio = 1361 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType) 1362 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f); 1363 if (mOffloadAudio) { 1364 flags |= Renderer::FLAG_OFFLOAD_AUDIO; 1365 } 1366 1367 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this); 1368 ++mRendererGeneration; 1369 notify->setInt32("generation", mRendererGeneration); 1370 mRenderer = new Renderer(mAudioSink, notify, flags); 1371 mRendererLooper = new ALooper; 1372 mRendererLooper->setName("NuPlayerRenderer"); 1373 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO); 1374 mRendererLooper->registerHandler(mRenderer); 1375 1376 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings); 1377 if (err != OK) { 1378 mSource->stop(); 1379 mSourceStarted = false; 1380 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 1381 return; 1382 } 1383 1384 float rate = getFrameRate(); 1385 if (rate > 0) { 1386 mRenderer->setVideoFrameRate(rate); 1387 } 1388 1389 if (mVideoDecoder != NULL) { 1390 mVideoDecoder->setRenderer(mRenderer); 1391 } 1392 if (mAudioDecoder != NULL) { 1393 mAudioDecoder->setRenderer(mRenderer); 1394 } 1395 1396 postScanSources(); 1397} 1398 1399void NuPlayer::onPause() { 1400 if (mPaused) { 1401 return; 1402 } 1403 mPaused = true; 1404 if (mSource != NULL) { 1405 mSource->pause(); 1406 } else { 1407 ALOGW("pause called when source is gone or not set"); 1408 } 1409 if (mRenderer != NULL) { 1410 mRenderer->pause(); 1411 } else { 1412 ALOGW("pause called when renderer is gone or not set"); 1413 } 1414} 1415 1416bool NuPlayer::audioDecoderStillNeeded() { 1417 // Audio decoder is no longer needed if it's in shut/shutting down status. 1418 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER)); 1419} 1420 1421void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) { 1422 // We wait for both the decoder flush and the renderer flush to complete 1423 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state. 1424 1425 mFlushComplete[audio][isDecoder] = true; 1426 if (!mFlushComplete[audio][!isDecoder]) { 1427 return; 1428 } 1429 1430 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo; 1431 switch (*state) { 1432 case FLUSHING_DECODER: 1433 { 1434 *state = FLUSHED; 1435 break; 1436 } 1437 1438 case FLUSHING_DECODER_SHUTDOWN: 1439 { 1440 *state = SHUTTING_DOWN_DECODER; 1441 1442 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video"); 1443 if (!audio) { 1444 // Widevine source reads must stop before releasing the video decoder. 1445 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) { 1446 mSource->stop(); 1447 mSourceStarted = false; 1448 } 1449 } 1450 getDecoder(audio)->initiateShutdown(); 1451 break; 1452 } 1453 1454 default: 1455 // decoder flush completes only occur in a flushing state. 1456 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state); 1457 break; 1458 } 1459} 1460 1461void NuPlayer::finishFlushIfPossible() { 1462 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED 1463 && mFlushingAudio != SHUT_DOWN) { 1464 return; 1465 } 1466 1467 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED 1468 && mFlushingVideo != SHUT_DOWN) { 1469 return; 1470 } 1471 1472 ALOGV("both audio and video are flushed now."); 1473 1474 mFlushingAudio = NONE; 1475 mFlushingVideo = NONE; 1476 1477 clearFlushComplete(); 1478 1479 processDeferredActions(); 1480} 1481 1482void NuPlayer::postScanSources() { 1483 if (mScanSourcesPending) { 1484 return; 1485 } 1486 1487 sp<AMessage> msg = new AMessage(kWhatScanSources, this); 1488 msg->setInt32("generation", mScanSourcesGeneration); 1489 msg->post(); 1490 1491 mScanSourcesPending = true; 1492} 1493 1494void NuPlayer::tryOpenAudioSinkForOffload( 1495 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) { 1496 // Note: This is called early in NuPlayer to determine whether offloading 1497 // is possible; otherwise the decoders call the renderer openAudioSink directly. 1498 1499 status_t err = mRenderer->openAudioSink( 1500 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio); 1501 if (err != OK) { 1502 // Any failure we turn off mOffloadAudio. 1503 mOffloadAudio = false; 1504 } else if (mOffloadAudio) { 1505 sendMetaDataToHal(mAudioSink, audioMeta); 1506 } 1507} 1508 1509void NuPlayer::closeAudioSink() { 1510 mRenderer->closeAudioSink(); 1511} 1512 1513void NuPlayer::restartAudio( 1514 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) { 1515 if (mAudioDecoder != NULL) { 1516 mAudioDecoder->pause(); 1517 mAudioDecoder.clear(); 1518 ++mAudioDecoderGeneration; 1519 } 1520 if (mFlushingAudio == FLUSHING_DECODER) { 1521 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true; 1522 mFlushingAudio = FLUSHED; 1523 finishFlushIfPossible(); 1524 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN 1525 || mFlushingAudio == SHUTTING_DOWN_DECODER) { 1526 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true; 1527 mFlushingAudio = SHUT_DOWN; 1528 finishFlushIfPossible(); 1529 needsToCreateAudioDecoder = false; 1530 } 1531 if (mRenderer == NULL) { 1532 return; 1533 } 1534 closeAudioSink(); 1535 mRenderer->flush(true /* audio */, false /* notifyComplete */); 1536 if (mVideoDecoder != NULL) { 1537 mRenderer->flush(false /* audio */, false /* notifyComplete */); 1538 } 1539 1540 performSeek(currentPositionUs); 1541 1542 if (forceNonOffload) { 1543 mRenderer->signalDisableOffloadAudio(); 1544 mOffloadAudio = false; 1545 } 1546 if (needsToCreateAudioDecoder) { 1547 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload); 1548 } 1549} 1550 1551void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) { 1552 if (mSource == NULL || mAudioSink == NULL) { 1553 return; 1554 } 1555 1556 if (mRenderer == NULL) { 1557 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety."); 1558 mOffloadAudio = false; 1559 return; 1560 } 1561 1562 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1563 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 1564 audio_stream_type_t streamType = mAudioSink->getAudioStreamType(); 1565 const bool hasVideo = (videoFormat != NULL); 1566 const bool canOffload = canOffloadStream( 1567 audioMeta, hasVideo, mSource->isStreaming(), streamType) 1568 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f); 1569 if (canOffload) { 1570 if (!mOffloadAudio) { 1571 mRenderer->signalEnableOffloadAudio(); 1572 } 1573 // open audio sink early under offload mode. 1574 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo); 1575 } else { 1576 if (mOffloadAudio) { 1577 mRenderer->signalDisableOffloadAudio(); 1578 mOffloadAudio = false; 1579 } 1580 } 1581} 1582 1583status_t NuPlayer::instantiateDecoder( 1584 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) { 1585 // The audio decoder could be cleared by tear down. If still in shut down 1586 // process, no need to create a new audio decoder. 1587 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) { 1588 return OK; 1589 } 1590 1591 sp<AMessage> format = mSource->getFormat(audio); 1592 1593 if (format == NULL) { 1594 return UNKNOWN_ERROR; 1595 } else { 1596 status_t err; 1597 if (format->findInt32("err", &err) && err) { 1598 return err; 1599 } 1600 } 1601 1602 format->setInt32("priority", 0 /* realtime */); 1603 1604 if (!audio) { 1605 AString mime; 1606 CHECK(format->findString("mime", &mime)); 1607 1608 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this); 1609 if (mCCDecoder == NULL) { 1610 mCCDecoder = new CCDecoder(ccNotify); 1611 } 1612 1613 if (mSourceFlags & Source::FLAG_SECURE) { 1614 format->setInt32("secure", true); 1615 } 1616 1617 if (mSourceFlags & Source::FLAG_PROTECTED) { 1618 format->setInt32("protected", true); 1619 } 1620 1621 float rate = getFrameRate(); 1622 if (rate > 0) { 1623 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed); 1624 } 1625 } 1626 1627 if (audio) { 1628 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this); 1629 ++mAudioDecoderGeneration; 1630 notify->setInt32("generation", mAudioDecoderGeneration); 1631 1632 if (checkAudioModeChange) { 1633 determineAudioModeChange(format); 1634 } 1635 if (mOffloadAudio) { 1636 mSource->setOffloadAudio(true /* offload */); 1637 1638 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL); 1639 format->setInt32("has-video", hasVideo); 1640 *decoder = new DecoderPassThrough(notify, mSource, mRenderer); 1641 } else { 1642 mSource->setOffloadAudio(false /* offload */); 1643 1644 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer); 1645 } 1646 } else { 1647 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this); 1648 ++mVideoDecoderGeneration; 1649 notify->setInt32("generation", mVideoDecoderGeneration); 1650 1651 *decoder = new Decoder( 1652 notify, mSource, mPID, mUID, mRenderer, mSurface, mCCDecoder); 1653 1654 // enable FRC if high-quality AV sync is requested, even if not 1655 // directly queuing to display, as this will even improve textureview 1656 // playback. 1657 { 1658 char value[PROPERTY_VALUE_MAX]; 1659 if (property_get("persist.sys.media.avsync", value, NULL) && 1660 (!strcmp("1", value) || !strcasecmp("true", value))) { 1661 format->setInt32("auto-frc", 1); 1662 } 1663 } 1664 } 1665 (*decoder)->init(); 1666 (*decoder)->configure(format); 1667 1668 // allocate buffers to decrypt widevine source buffers 1669 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) { 1670 Vector<sp<MediaCodecBuffer> > inputBufs; 1671 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK); 1672 1673 Vector<MediaBuffer *> mediaBufs; 1674 for (size_t i = 0; i < inputBufs.size(); i++) { 1675 const sp<MediaCodecBuffer> &buffer = inputBufs[i]; 1676 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size()); 1677 mediaBufs.push(mbuf); 1678 } 1679 1680 status_t err = mSource->setBuffers(audio, mediaBufs); 1681 if (err != OK) { 1682 for (size_t i = 0; i < mediaBufs.size(); ++i) { 1683 mediaBufs[i]->release(); 1684 } 1685 mediaBufs.clear(); 1686 ALOGE("Secure source didn't support secure mediaBufs."); 1687 return err; 1688 } 1689 } 1690 1691 if (!audio) { 1692 sp<AMessage> params = new AMessage(); 1693 float rate = getFrameRate(); 1694 if (rate > 0) { 1695 params->setFloat("frame-rate-total", rate); 1696 } 1697 1698 sp<MetaData> fileMeta = getFileMeta(); 1699 if (fileMeta != NULL) { 1700 int32_t videoTemporalLayerCount; 1701 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount) 1702 && videoTemporalLayerCount > 0) { 1703 params->setInt32("temporal-layer-count", videoTemporalLayerCount); 1704 } 1705 } 1706 1707 if (params->countEntries() > 0) { 1708 (*decoder)->setParameters(params); 1709 } 1710 } 1711 return OK; 1712} 1713 1714void NuPlayer::updateVideoSize( 1715 const sp<AMessage> &inputFormat, 1716 const sp<AMessage> &outputFormat) { 1717 if (inputFormat == NULL) { 1718 ALOGW("Unknown video size, reporting 0x0!"); 1719 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0); 1720 return; 1721 } 1722 1723 int32_t displayWidth, displayHeight; 1724 if (outputFormat != NULL) { 1725 int32_t width, height; 1726 CHECK(outputFormat->findInt32("width", &width)); 1727 CHECK(outputFormat->findInt32("height", &height)); 1728 1729 int32_t cropLeft, cropTop, cropRight, cropBottom; 1730 CHECK(outputFormat->findRect( 1731 "crop", 1732 &cropLeft, &cropTop, &cropRight, &cropBottom)); 1733 1734 displayWidth = cropRight - cropLeft + 1; 1735 displayHeight = cropBottom - cropTop + 1; 1736 1737 ALOGV("Video output format changed to %d x %d " 1738 "(crop: %d x %d @ (%d, %d))", 1739 width, height, 1740 displayWidth, 1741 displayHeight, 1742 cropLeft, cropTop); 1743 } else { 1744 CHECK(inputFormat->findInt32("width", &displayWidth)); 1745 CHECK(inputFormat->findInt32("height", &displayHeight)); 1746 1747 ALOGV("Video input format %d x %d", displayWidth, displayHeight); 1748 } 1749 1750 // Take into account sample aspect ratio if necessary: 1751 int32_t sarWidth, sarHeight; 1752 if (inputFormat->findInt32("sar-width", &sarWidth) 1753 && inputFormat->findInt32("sar-height", &sarHeight)) { 1754 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight); 1755 1756 displayWidth = (displayWidth * sarWidth) / sarHeight; 1757 1758 ALOGV("display dimensions %d x %d", displayWidth, displayHeight); 1759 } else { 1760 int32_t width, height; 1761 if (inputFormat->findInt32("display-width", &width) 1762 && inputFormat->findInt32("display-height", &height) 1763 && width > 0 && height > 0 1764 && displayWidth > 0 && displayHeight > 0) { 1765 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) { 1766 displayHeight = (int32_t)(displayWidth * (int64_t)height / width); 1767 } else { 1768 displayWidth = (int32_t)(displayHeight * (int64_t)width / height); 1769 } 1770 ALOGV("Video display width and height are overridden to %d x %d", 1771 displayWidth, displayHeight); 1772 } 1773 } 1774 1775 int32_t rotationDegrees; 1776 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) { 1777 rotationDegrees = 0; 1778 } 1779 1780 if (rotationDegrees == 90 || rotationDegrees == 270) { 1781 int32_t tmp = displayWidth; 1782 displayWidth = displayHeight; 1783 displayHeight = tmp; 1784 } 1785 1786 notifyListener( 1787 MEDIA_SET_VIDEO_SIZE, 1788 displayWidth, 1789 displayHeight); 1790} 1791 1792void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) { 1793 if (mDriver == NULL) { 1794 return; 1795 } 1796 1797 sp<NuPlayerDriver> driver = mDriver.promote(); 1798 1799 if (driver == NULL) { 1800 return; 1801 } 1802 1803 driver->notifyListener(msg, ext1, ext2, in); 1804} 1805 1806void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 1807 ALOGV("[%s] flushDecoder needShutdown=%d", 1808 audio ? "audio" : "video", needShutdown); 1809 1810 const sp<DecoderBase> &decoder = getDecoder(audio); 1811 if (decoder == NULL) { 1812 ALOGI("flushDecoder %s without decoder present", 1813 audio ? "audio" : "video"); 1814 return; 1815 } 1816 1817 // Make sure we don't continue to scan sources until we finish flushing. 1818 ++mScanSourcesGeneration; 1819 if (mScanSourcesPending) { 1820 if (!needShutdown) { 1821 mDeferredActions.push_back( 1822 new SimpleAction(&NuPlayer::performScanSources)); 1823 } 1824 mScanSourcesPending = false; 1825 } 1826 1827 decoder->signalFlush(); 1828 1829 FlushStatus newStatus = 1830 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 1831 1832 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL); 1833 mFlushComplete[audio][true /* isDecoder */] = false; 1834 if (audio) { 1835 ALOGE_IF(mFlushingAudio != NONE, 1836 "audio flushDecoder() is called in state %d", mFlushingAudio); 1837 mFlushingAudio = newStatus; 1838 } else { 1839 ALOGE_IF(mFlushingVideo != NONE, 1840 "video flushDecoder() is called in state %d", mFlushingVideo); 1841 mFlushingVideo = newStatus; 1842 } 1843} 1844 1845void NuPlayer::queueDecoderShutdown( 1846 bool audio, bool video, const sp<AMessage> &reply) { 1847 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video); 1848 1849 mDeferredActions.push_back( 1850 new FlushDecoderAction( 1851 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 1852 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE)); 1853 1854 mDeferredActions.push_back( 1855 new SimpleAction(&NuPlayer::performScanSources)); 1856 1857 mDeferredActions.push_back(new PostMessageAction(reply)); 1858 1859 processDeferredActions(); 1860} 1861 1862status_t NuPlayer::setVideoScalingMode(int32_t mode) { 1863 mVideoScalingMode = mode; 1864 if (mSurface != NULL) { 1865 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode); 1866 if (ret != OK) { 1867 ALOGE("Failed to set scaling mode (%d): %s", 1868 -ret, strerror(-ret)); 1869 return ret; 1870 } 1871 } 1872 return OK; 1873} 1874 1875status_t NuPlayer::getTrackInfo(Parcel* reply) const { 1876 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this); 1877 msg->setPointer("reply", reply); 1878 1879 sp<AMessage> response; 1880 status_t err = msg->postAndAwaitResponse(&response); 1881 return err; 1882} 1883 1884status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const { 1885 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this); 1886 msg->setPointer("reply", reply); 1887 msg->setInt32("type", type); 1888 1889 sp<AMessage> response; 1890 status_t err = msg->postAndAwaitResponse(&response); 1891 if (err == OK && response != NULL) { 1892 CHECK(response->findInt32("err", &err)); 1893 } 1894 return err; 1895} 1896 1897status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) { 1898 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this); 1899 msg->setSize("trackIndex", trackIndex); 1900 msg->setInt32("select", select); 1901 msg->setInt64("timeUs", timeUs); 1902 1903 sp<AMessage> response; 1904 status_t err = msg->postAndAwaitResponse(&response); 1905 1906 if (err != OK) { 1907 return err; 1908 } 1909 1910 if (!response->findInt32("err", &err)) { 1911 err = OK; 1912 } 1913 1914 return err; 1915} 1916 1917status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) { 1918 sp<Renderer> renderer = mRenderer; 1919 if (renderer == NULL) { 1920 return NO_INIT; 1921 } 1922 1923 return renderer->getCurrentPosition(mediaUs); 1924} 1925 1926void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) { 1927 CHECK(mTrackStats != NULL); 1928 1929 mTrackStats->clear(); 1930 if (mVideoDecoder != NULL) { 1931 mTrackStats->push_back(mVideoDecoder->getStats()); 1932 } 1933 if (mAudioDecoder != NULL) { 1934 mTrackStats->push_back(mAudioDecoder->getStats()); 1935 } 1936} 1937 1938sp<MetaData> NuPlayer::getFileMeta() { 1939 return mSource->getFileFormatMeta(); 1940} 1941 1942float NuPlayer::getFrameRate() { 1943 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */); 1944 if (meta == NULL) { 1945 return 0; 1946 } 1947 int32_t rate; 1948 if (!meta->findInt32(kKeyFrameRate, &rate)) { 1949 // fall back to try file meta 1950 sp<MetaData> fileMeta = getFileMeta(); 1951 if (fileMeta == NULL) { 1952 ALOGW("source has video meta but not file meta"); 1953 return -1; 1954 } 1955 int32_t fileMetaRate; 1956 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) { 1957 return -1; 1958 } 1959 return fileMetaRate; 1960 } 1961 return rate; 1962} 1963 1964void NuPlayer::schedulePollDuration() { 1965 sp<AMessage> msg = new AMessage(kWhatPollDuration, this); 1966 msg->setInt32("generation", mPollDurationGeneration); 1967 msg->post(); 1968} 1969 1970void NuPlayer::cancelPollDuration() { 1971 ++mPollDurationGeneration; 1972} 1973 1974void NuPlayer::processDeferredActions() { 1975 while (!mDeferredActions.empty()) { 1976 // We won't execute any deferred actions until we're no longer in 1977 // an intermediate state, i.e. one more more decoders are currently 1978 // flushing or shutting down. 1979 1980 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 1981 // We're currently flushing, postpone the reset until that's 1982 // completed. 1983 1984 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d", 1985 mFlushingAudio, mFlushingVideo); 1986 1987 break; 1988 } 1989 1990 sp<Action> action = *mDeferredActions.begin(); 1991 mDeferredActions.erase(mDeferredActions.begin()); 1992 1993 action->execute(this); 1994 } 1995} 1996 1997void NuPlayer::performSeek(int64_t seekTimeUs) { 1998 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)", 1999 (long long)seekTimeUs, 2000 seekTimeUs / 1E6); 2001 2002 if (mSource == NULL) { 2003 // This happens when reset occurs right before the loop mode 2004 // asynchronously seeks to the start of the stream. 2005 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL, 2006 "mSource is NULL and decoders not NULL audio(%p) video(%p)", 2007 mAudioDecoder.get(), mVideoDecoder.get()); 2008 return; 2009 } 2010 mPreviousSeekTimeUs = seekTimeUs; 2011 mSource->seekTo(seekTimeUs); 2012 ++mTimedTextGeneration; 2013 2014 // everything's flushed, continue playback. 2015} 2016 2017void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) { 2018 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video); 2019 2020 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL) 2021 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) { 2022 return; 2023 } 2024 2025 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) { 2026 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN)); 2027 } 2028 2029 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) { 2030 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN)); 2031 } 2032} 2033 2034void NuPlayer::performReset() { 2035 ALOGV("performReset"); 2036 2037 CHECK(mAudioDecoder == NULL); 2038 CHECK(mVideoDecoder == NULL); 2039 2040 cancelPollDuration(); 2041 2042 ++mScanSourcesGeneration; 2043 mScanSourcesPending = false; 2044 2045 if (mRendererLooper != NULL) { 2046 if (mRenderer != NULL) { 2047 mRendererLooper->unregisterHandler(mRenderer->id()); 2048 } 2049 mRendererLooper->stop(); 2050 mRendererLooper.clear(); 2051 } 2052 mRenderer.clear(); 2053 ++mRendererGeneration; 2054 2055 if (mSource != NULL) { 2056 mSource->stop(); 2057 2058 Mutex::Autolock autoLock(mSourceLock); 2059 mSource.clear(); 2060 } 2061 2062 if (mDriver != NULL) { 2063 sp<NuPlayerDriver> driver = mDriver.promote(); 2064 if (driver != NULL) { 2065 driver->notifyResetComplete(); 2066 } 2067 } 2068 2069 mStarted = false; 2070 mPrepared = false; 2071 mResetting = false; 2072 mSourceStarted = false; 2073} 2074 2075void NuPlayer::performScanSources() { 2076 ALOGV("performScanSources"); 2077 2078 if (!mStarted) { 2079 return; 2080 } 2081 2082 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 2083 postScanSources(); 2084 } 2085} 2086 2087void NuPlayer::performSetSurface(const sp<Surface> &surface) { 2088 ALOGV("performSetSurface"); 2089 2090 mSurface = surface; 2091 2092 // XXX - ignore error from setVideoScalingMode for now 2093 setVideoScalingMode(mVideoScalingMode); 2094 2095 if (mDriver != NULL) { 2096 sp<NuPlayerDriver> driver = mDriver.promote(); 2097 if (driver != NULL) { 2098 driver->notifySetSurfaceComplete(); 2099 } 2100 } 2101} 2102 2103void NuPlayer::performResumeDecoders(bool needNotify) { 2104 if (needNotify) { 2105 mResumePending = true; 2106 if (mVideoDecoder == NULL) { 2107 // if audio-only, we can notify seek complete now, 2108 // as the resume operation will be relatively fast. 2109 finishResume(); 2110 } 2111 } 2112 2113 if (mVideoDecoder != NULL) { 2114 // When there is continuous seek, MediaPlayer will cache the seek 2115 // position, and send down new seek request when previous seek is 2116 // complete. Let's wait for at least one video output frame before 2117 // notifying seek complete, so that the video thumbnail gets updated 2118 // when seekbar is dragged. 2119 mVideoDecoder->signalResume(needNotify); 2120 } 2121 2122 if (mAudioDecoder != NULL) { 2123 mAudioDecoder->signalResume(false /* needNotify */); 2124 } 2125} 2126 2127void NuPlayer::finishResume() { 2128 if (mResumePending) { 2129 mResumePending = false; 2130 notifyDriverSeekComplete(); 2131 } 2132} 2133 2134void NuPlayer::notifyDriverSeekComplete() { 2135 if (mDriver != NULL) { 2136 sp<NuPlayerDriver> driver = mDriver.promote(); 2137 if (driver != NULL) { 2138 driver->notifySeekComplete(); 2139 } 2140 } 2141} 2142 2143void NuPlayer::onSourceNotify(const sp<AMessage> &msg) { 2144 int32_t what; 2145 CHECK(msg->findInt32("what", &what)); 2146 2147 switch (what) { 2148 case Source::kWhatInstantiateSecureDecoders: 2149 { 2150 if (mSource == NULL) { 2151 // This is a stale notification from a source that was 2152 // asynchronously preparing when the client called reset(). 2153 // We handled the reset, the source is gone. 2154 break; 2155 } 2156 2157 sp<AMessage> reply; 2158 CHECK(msg->findMessage("reply", &reply)); 2159 status_t err = onInstantiateSecureDecoders(); 2160 reply->setInt32("err", err); 2161 reply->post(); 2162 break; 2163 } 2164 2165 case Source::kWhatPrepared: 2166 { 2167 if (mSource == NULL) { 2168 // This is a stale notification from a source that was 2169 // asynchronously preparing when the client called reset(). 2170 // We handled the reset, the source is gone. 2171 break; 2172 } 2173 2174 int32_t err; 2175 CHECK(msg->findInt32("err", &err)); 2176 2177 if (err != OK) { 2178 // shut down potential secure codecs in case client never calls reset 2179 mDeferredActions.push_back( 2180 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */, 2181 FLUSH_CMD_SHUTDOWN /* video */)); 2182 processDeferredActions(); 2183 } else { 2184 mPrepared = true; 2185 } 2186 2187 sp<NuPlayerDriver> driver = mDriver.promote(); 2188 if (driver != NULL) { 2189 // notify duration first, so that it's definitely set when 2190 // the app received the "prepare complete" callback. 2191 int64_t durationUs; 2192 if (mSource->getDuration(&durationUs) == OK) { 2193 driver->notifyDuration(durationUs); 2194 } 2195 driver->notifyPrepareCompleted(err); 2196 } 2197 2198 break; 2199 } 2200 2201 case Source::kWhatFlagsChanged: 2202 { 2203 uint32_t flags; 2204 CHECK(msg->findInt32("flags", (int32_t *)&flags)); 2205 2206 sp<NuPlayerDriver> driver = mDriver.promote(); 2207 if (driver != NULL) { 2208 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) { 2209 driver->notifyListener( 2210 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0); 2211 } 2212 driver->notifyFlagsChanged(flags); 2213 } 2214 2215 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2216 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) { 2217 cancelPollDuration(); 2218 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2219 && (flags & Source::FLAG_DYNAMIC_DURATION) 2220 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 2221 schedulePollDuration(); 2222 } 2223 2224 mSourceFlags = flags; 2225 break; 2226 } 2227 2228 case Source::kWhatVideoSizeChanged: 2229 { 2230 sp<AMessage> format; 2231 CHECK(msg->findMessage("format", &format)); 2232 2233 updateVideoSize(format); 2234 break; 2235 } 2236 2237 case Source::kWhatBufferingUpdate: 2238 { 2239 int32_t percentage; 2240 CHECK(msg->findInt32("percentage", &percentage)); 2241 2242 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0); 2243 break; 2244 } 2245 2246 case Source::kWhatPauseOnBufferingStart: 2247 { 2248 // ignore if not playing 2249 if (mStarted) { 2250 ALOGI("buffer low, pausing..."); 2251 2252 mPausedForBuffering = true; 2253 onPause(); 2254 } 2255 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0); 2256 break; 2257 } 2258 2259 case Source::kWhatResumeOnBufferingEnd: 2260 { 2261 // ignore if not playing 2262 if (mStarted) { 2263 ALOGI("buffer ready, resuming..."); 2264 2265 mPausedForBuffering = false; 2266 2267 // do not resume yet if client didn't unpause 2268 if (!mPausedByClient) { 2269 onResume(); 2270 } 2271 } 2272 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0); 2273 break; 2274 } 2275 2276 case Source::kWhatCacheStats: 2277 { 2278 int32_t kbps; 2279 CHECK(msg->findInt32("bandwidth", &kbps)); 2280 2281 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps); 2282 break; 2283 } 2284 2285 case Source::kWhatSubtitleData: 2286 { 2287 sp<ABuffer> buffer; 2288 CHECK(msg->findBuffer("buffer", &buffer)); 2289 2290 sendSubtitleData(buffer, 0 /* baseIndex */); 2291 break; 2292 } 2293 2294 case Source::kWhatTimedMetaData: 2295 { 2296 sp<ABuffer> buffer; 2297 if (!msg->findBuffer("buffer", &buffer)) { 2298 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2299 } else { 2300 sendTimedMetaData(buffer); 2301 } 2302 break; 2303 } 2304 2305 case Source::kWhatTimedTextData: 2306 { 2307 int32_t generation; 2308 if (msg->findInt32("generation", &generation) 2309 && generation != mTimedTextGeneration) { 2310 break; 2311 } 2312 2313 sp<ABuffer> buffer; 2314 CHECK(msg->findBuffer("buffer", &buffer)); 2315 2316 sp<NuPlayerDriver> driver = mDriver.promote(); 2317 if (driver == NULL) { 2318 break; 2319 } 2320 2321 int posMs; 2322 int64_t timeUs, posUs; 2323 driver->getCurrentPosition(&posMs); 2324 posUs = (int64_t) posMs * 1000ll; 2325 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2326 2327 if (posUs < timeUs) { 2328 if (!msg->findInt32("generation", &generation)) { 2329 msg->setInt32("generation", mTimedTextGeneration); 2330 } 2331 msg->post(timeUs - posUs); 2332 } else { 2333 sendTimedTextData(buffer); 2334 } 2335 break; 2336 } 2337 2338 case Source::kWhatQueueDecoderShutdown: 2339 { 2340 int32_t audio, video; 2341 CHECK(msg->findInt32("audio", &audio)); 2342 CHECK(msg->findInt32("video", &video)); 2343 2344 sp<AMessage> reply; 2345 CHECK(msg->findMessage("reply", &reply)); 2346 2347 queueDecoderShutdown(audio, video, reply); 2348 break; 2349 } 2350 2351 case Source::kWhatDrmNoLicense: 2352 { 2353 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE); 2354 break; 2355 } 2356 2357 default: 2358 TRESPASS(); 2359 } 2360} 2361 2362void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) { 2363 int32_t what; 2364 CHECK(msg->findInt32("what", &what)); 2365 2366 switch (what) { 2367 case NuPlayer::CCDecoder::kWhatClosedCaptionData: 2368 { 2369 sp<ABuffer> buffer; 2370 CHECK(msg->findBuffer("buffer", &buffer)); 2371 2372 size_t inbandTracks = 0; 2373 if (mSource != NULL) { 2374 inbandTracks = mSource->getTrackCount(); 2375 } 2376 2377 sendSubtitleData(buffer, inbandTracks); 2378 break; 2379 } 2380 2381 case NuPlayer::CCDecoder::kWhatTrackAdded: 2382 { 2383 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2384 2385 break; 2386 } 2387 2388 default: 2389 TRESPASS(); 2390 } 2391 2392 2393} 2394 2395void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) { 2396 int32_t trackIndex; 2397 int64_t timeUs, durationUs; 2398 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex)); 2399 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2400 CHECK(buffer->meta()->findInt64("durationUs", &durationUs)); 2401 2402 Parcel in; 2403 in.writeInt32(trackIndex + baseIndex); 2404 in.writeInt64(timeUs); 2405 in.writeInt64(durationUs); 2406 in.writeInt32(buffer->size()); 2407 in.writeInt32(buffer->size()); 2408 in.write(buffer->data(), buffer->size()); 2409 2410 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in); 2411} 2412 2413void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) { 2414 int64_t timeUs; 2415 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2416 2417 Parcel in; 2418 in.writeInt64(timeUs); 2419 in.writeInt32(buffer->size()); 2420 in.writeInt32(buffer->size()); 2421 in.write(buffer->data(), buffer->size()); 2422 2423 notifyListener(MEDIA_META_DATA, 0, 0, &in); 2424} 2425 2426void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) { 2427 const void *data; 2428 size_t size = 0; 2429 int64_t timeUs; 2430 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP; 2431 2432 AString mime; 2433 CHECK(buffer->meta()->findString("mime", &mime)); 2434 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0); 2435 2436 data = buffer->data(); 2437 size = buffer->size(); 2438 2439 Parcel parcel; 2440 if (size > 0) { 2441 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2442 int32_t global = 0; 2443 if (buffer->meta()->findInt32("global", &global) && global) { 2444 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS; 2445 } else { 2446 flag |= TextDescriptions::LOCAL_DESCRIPTIONS; 2447 } 2448 TextDescriptions::getParcelOfDescriptions( 2449 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel); 2450 } 2451 2452 if ((parcel.dataSize() > 0)) { 2453 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel); 2454 } else { // send an empty timed text 2455 notifyListener(MEDIA_TIMED_TEXT, 0, 0); 2456 } 2457} 2458//////////////////////////////////////////////////////////////////////////////// 2459 2460sp<AMessage> NuPlayer::Source::getFormat(bool audio) { 2461 sp<MetaData> meta = getFormatMeta(audio); 2462 2463 if (meta == NULL) { 2464 return NULL; 2465 } 2466 2467 sp<AMessage> msg = new AMessage; 2468 2469 if(convertMetaDataToMessage(meta, &msg) == OK) { 2470 return msg; 2471 } 2472 return NULL; 2473} 2474 2475void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) { 2476 sp<AMessage> notify = dupNotify(); 2477 notify->setInt32("what", kWhatFlagsChanged); 2478 notify->setInt32("flags", flags); 2479 notify->post(); 2480} 2481 2482void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) { 2483 sp<AMessage> notify = dupNotify(); 2484 notify->setInt32("what", kWhatVideoSizeChanged); 2485 notify->setMessage("format", format); 2486 notify->post(); 2487} 2488 2489void NuPlayer::Source::notifyPrepared(status_t err) { 2490 sp<AMessage> notify = dupNotify(); 2491 notify->setInt32("what", kWhatPrepared); 2492 notify->setInt32("err", err); 2493 notify->post(); 2494} 2495 2496void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) { 2497 sp<AMessage> notify = dupNotify(); 2498 notify->setInt32("what", kWhatInstantiateSecureDecoders); 2499 notify->setMessage("reply", reply); 2500 notify->post(); 2501} 2502 2503void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) { 2504 TRESPASS(); 2505} 2506 2507} // namespace android 2508