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