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