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