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