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