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