NuPlayer.cpp revision e5d45332a2d8abb44e4ec2964c3d965c28144030
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 mPrepared(false), 192 mResetting(false), 193 mSourceStarted(false), 194 mPaused(false), 195 mPausedByClient(true), 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 break; 727 } 728 729 case kWhatConfigPlayback: 730 { 731 sp<AReplyToken> replyID; 732 CHECK(msg->senderAwaitsResponse(&replyID)); 733 AudioPlaybackRate rate /* sanitized */; 734 readFromAMessage(msg, &rate); 735 status_t err = OK; 736 if (mRenderer != NULL) { 737 // AudioSink allows only 1.f and 0.f for offload mode. 738 // For other speed, switch to non-offload mode. 739 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f) 740 || rate.mPitch != 1.f)) { 741 int64_t currentPositionUs; 742 if (getCurrentPosition(¤tPositionUs) != OK) { 743 currentPositionUs = mPreviousSeekTimeUs; 744 } 745 746 // Set mPlaybackSettings so that the new audio decoder can 747 // be created correctly. 748 mPlaybackSettings = rate; 749 if (!mPaused) { 750 mRenderer->pause(); 751 } 752 restartAudio( 753 currentPositionUs, true /* forceNonOffload */, 754 true /* needsToCreateAudioDecoder */); 755 if (!mPaused) { 756 mRenderer->resume(); 757 } 758 } 759 760 err = mRenderer->setPlaybackSettings(rate); 761 } 762 if (err == OK) { 763 if (rate.mSpeed == 0.f) { 764 onPause(); 765 mPausedByClient = true; 766 // save all other settings (using non-paused speed) 767 // so we can restore them on start 768 AudioPlaybackRate newRate = rate; 769 newRate.mSpeed = mPlaybackSettings.mSpeed; 770 mPlaybackSettings = newRate; 771 } else { /* rate.mSpeed != 0.f */ 772 mPlaybackSettings = rate; 773 if (mStarted) { 774 // do not resume yet if the source is still buffering 775 if (!mPausedForBuffering) { 776 onResume(); 777 } 778 } else if (mPrepared) { 779 onStart(); 780 } 781 782 mPausedByClient = false; 783 } 784 } 785 786 if (mVideoDecoder != NULL) { 787 float rate = getFrameRate(); 788 if (rate > 0) { 789 sp<AMessage> params = new AMessage(); 790 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed); 791 mVideoDecoder->setParameters(params); 792 } 793 } 794 795 sp<AMessage> response = new AMessage; 796 response->setInt32("err", err); 797 response->postReply(replyID); 798 break; 799 } 800 801 case kWhatGetPlaybackSettings: 802 { 803 sp<AReplyToken> replyID; 804 CHECK(msg->senderAwaitsResponse(&replyID)); 805 AudioPlaybackRate rate = mPlaybackSettings; 806 status_t err = OK; 807 if (mRenderer != NULL) { 808 err = mRenderer->getPlaybackSettings(&rate); 809 } 810 if (err == OK) { 811 // get playback settings used by renderer, as it may be 812 // slightly off due to audiosink not taking small changes. 813 mPlaybackSettings = rate; 814 if (mPaused) { 815 rate.mSpeed = 0.f; 816 } 817 } 818 sp<AMessage> response = new AMessage; 819 if (err == OK) { 820 writeToAMessage(response, rate); 821 } 822 response->setInt32("err", err); 823 response->postReply(replyID); 824 break; 825 } 826 827 case kWhatConfigSync: 828 { 829 sp<AReplyToken> replyID; 830 CHECK(msg->senderAwaitsResponse(&replyID)); 831 832 ALOGV("kWhatConfigSync"); 833 AVSyncSettings sync; 834 float videoFpsHint; 835 readFromAMessage(msg, &sync, &videoFpsHint); 836 status_t err = OK; 837 if (mRenderer != NULL) { 838 err = mRenderer->setSyncSettings(sync, videoFpsHint); 839 } 840 if (err == OK) { 841 mSyncSettings = sync; 842 mVideoFpsHint = videoFpsHint; 843 } 844 sp<AMessage> response = new AMessage; 845 response->setInt32("err", err); 846 response->postReply(replyID); 847 break; 848 } 849 850 case kWhatGetSyncSettings: 851 { 852 sp<AReplyToken> replyID; 853 CHECK(msg->senderAwaitsResponse(&replyID)); 854 AVSyncSettings sync = mSyncSettings; 855 float videoFps = mVideoFpsHint; 856 status_t err = OK; 857 if (mRenderer != NULL) { 858 err = mRenderer->getSyncSettings(&sync, &videoFps); 859 if (err == OK) { 860 mSyncSettings = sync; 861 mVideoFpsHint = videoFps; 862 } 863 } 864 sp<AMessage> response = new AMessage; 865 if (err == OK) { 866 writeToAMessage(response, sync, videoFps); 867 } 868 response->setInt32("err", err); 869 response->postReply(replyID); 870 break; 871 } 872 873 case kWhatScanSources: 874 { 875 int32_t generation; 876 CHECK(msg->findInt32("generation", &generation)); 877 if (generation != mScanSourcesGeneration) { 878 // Drop obsolete msg. 879 break; 880 } 881 882 mScanSourcesPending = false; 883 884 ALOGV("scanning sources haveAudio=%d, haveVideo=%d", 885 mAudioDecoder != NULL, mVideoDecoder != NULL); 886 887 bool mHadAnySourcesBefore = 888 (mAudioDecoder != NULL) || (mVideoDecoder != NULL); 889 bool rescan = false; 890 891 // initialize video before audio because successful initialization of 892 // video may change deep buffer mode of audio. 893 if (mSurface != NULL) { 894 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) { 895 rescan = true; 896 } 897 } 898 899 // Don't try to re-open audio sink if there's an existing decoder. 900 if (mAudioSink != NULL && mAudioDecoder == NULL) { 901 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) { 902 rescan = true; 903 } 904 } 905 906 if (!mHadAnySourcesBefore 907 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 908 // This is the first time we've found anything playable. 909 910 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) { 911 schedulePollDuration(); 912 } 913 } 914 915 status_t err; 916 if ((err = mSource->feedMoreTSData()) != OK) { 917 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 918 // We're not currently decoding anything (no audio or 919 // video tracks found) and we just ran out of input data. 920 921 if (err == ERROR_END_OF_STREAM) { 922 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 923 } else { 924 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 925 } 926 } 927 break; 928 } 929 930 if (rescan) { 931 msg->post(100000ll); 932 mScanSourcesPending = true; 933 } 934 break; 935 } 936 937 case kWhatVideoNotify: 938 case kWhatAudioNotify: 939 { 940 bool audio = msg->what() == kWhatAudioNotify; 941 942 int32_t currentDecoderGeneration = 943 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration); 944 int32_t requesterGeneration = currentDecoderGeneration - 1; 945 CHECK(msg->findInt32("generation", &requesterGeneration)); 946 947 if (requesterGeneration != currentDecoderGeneration) { 948 ALOGV("got message from old %s decoder, generation(%d:%d)", 949 audio ? "audio" : "video", requesterGeneration, 950 currentDecoderGeneration); 951 sp<AMessage> reply; 952 if (!(msg->findMessage("reply", &reply))) { 953 return; 954 } 955 956 reply->setInt32("err", INFO_DISCONTINUITY); 957 reply->post(); 958 return; 959 } 960 961 int32_t what; 962 CHECK(msg->findInt32("what", &what)); 963 964 if (what == DecoderBase::kWhatInputDiscontinuity) { 965 int32_t formatChange; 966 CHECK(msg->findInt32("formatChange", &formatChange)); 967 968 ALOGV("%s discontinuity: formatChange %d", 969 audio ? "audio" : "video", formatChange); 970 971 if (formatChange) { 972 mDeferredActions.push_back( 973 new FlushDecoderAction( 974 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 975 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN)); 976 } 977 978 mDeferredActions.push_back( 979 new SimpleAction( 980 &NuPlayer::performScanSources)); 981 982 processDeferredActions(); 983 } else if (what == DecoderBase::kWhatEOS) { 984 int32_t err; 985 CHECK(msg->findInt32("err", &err)); 986 987 if (err == ERROR_END_OF_STREAM) { 988 ALOGV("got %s decoder EOS", audio ? "audio" : "video"); 989 } else { 990 ALOGV("got %s decoder EOS w/ error %d", 991 audio ? "audio" : "video", 992 err); 993 } 994 995 mRenderer->queueEOS(audio, err); 996 } else if (what == DecoderBase::kWhatFlushCompleted) { 997 ALOGV("decoder %s flush completed", audio ? "audio" : "video"); 998 999 handleFlushComplete(audio, true /* isDecoder */); 1000 finishFlushIfPossible(); 1001 } else if (what == DecoderBase::kWhatVideoSizeChanged) { 1002 sp<AMessage> format; 1003 CHECK(msg->findMessage("format", &format)); 1004 1005 sp<AMessage> inputFormat = 1006 mSource->getFormat(false /* audio */); 1007 1008 setVideoScalingMode(mVideoScalingMode); 1009 updateVideoSize(inputFormat, format); 1010 } else if (what == DecoderBase::kWhatShutdownCompleted) { 1011 ALOGV("%s shutdown completed", audio ? "audio" : "video"); 1012 if (audio) { 1013 mAudioDecoder.clear(); 1014 ++mAudioDecoderGeneration; 1015 1016 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER); 1017 mFlushingAudio = SHUT_DOWN; 1018 } else { 1019 mVideoDecoder.clear(); 1020 ++mVideoDecoderGeneration; 1021 1022 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER); 1023 mFlushingVideo = SHUT_DOWN; 1024 } 1025 1026 finishFlushIfPossible(); 1027 } else if (what == DecoderBase::kWhatResumeCompleted) { 1028 finishResume(); 1029 } else if (what == DecoderBase::kWhatError) { 1030 status_t err; 1031 if (!msg->findInt32("err", &err) || err == OK) { 1032 err = UNKNOWN_ERROR; 1033 } 1034 1035 // Decoder errors can be due to Source (e.g. from streaming), 1036 // or from decoding corrupted bitstreams, or from other decoder 1037 // MediaCodec operations (e.g. from an ongoing reset or seek). 1038 // They may also be due to openAudioSink failure at 1039 // decoder start or after a format change. 1040 // 1041 // We try to gracefully shut down the affected decoder if possible, 1042 // rather than trying to force the shutdown with something 1043 // similar to performReset(). This method can lead to a hang 1044 // if MediaCodec functions block after an error, but they should 1045 // typically return INVALID_OPERATION instead of blocking. 1046 1047 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo; 1048 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down", 1049 err, audio ? "audio" : "video", *flushing); 1050 1051 switch (*flushing) { 1052 case NONE: 1053 mDeferredActions.push_back( 1054 new FlushDecoderAction( 1055 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 1056 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN)); 1057 processDeferredActions(); 1058 break; 1059 case FLUSHING_DECODER: 1060 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush. 1061 break; // Wait for flush to complete. 1062 case FLUSHING_DECODER_SHUTDOWN: 1063 break; // Wait for flush to complete. 1064 case SHUTTING_DOWN_DECODER: 1065 break; // Wait for shutdown to complete. 1066 case FLUSHED: 1067 // Widevine source reads must stop before releasing the video decoder. 1068 if (!audio && mSource != NULL && mSourceFlags & Source::FLAG_SECURE) { 1069 mSource->stop(); 1070 mSourceStarted = false; 1071 } 1072 getDecoder(audio)->initiateShutdown(); // In the middle of a seek. 1073 *flushing = SHUTTING_DOWN_DECODER; // Shut down. 1074 break; 1075 case SHUT_DOWN: 1076 finishFlushIfPossible(); // Should not occur. 1077 break; // Finish anyways. 1078 } 1079 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 1080 } else { 1081 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.", 1082 what, 1083 what >> 24, 1084 (what >> 16) & 0xff, 1085 (what >> 8) & 0xff, 1086 what & 0xff); 1087 } 1088 1089 break; 1090 } 1091 1092 case kWhatRendererNotify: 1093 { 1094 int32_t requesterGeneration = mRendererGeneration - 1; 1095 CHECK(msg->findInt32("generation", &requesterGeneration)); 1096 if (requesterGeneration != mRendererGeneration) { 1097 ALOGV("got message from old renderer, generation(%d:%d)", 1098 requesterGeneration, mRendererGeneration); 1099 return; 1100 } 1101 1102 int32_t what; 1103 CHECK(msg->findInt32("what", &what)); 1104 1105 if (what == Renderer::kWhatEOS) { 1106 int32_t audio; 1107 CHECK(msg->findInt32("audio", &audio)); 1108 1109 int32_t finalResult; 1110 CHECK(msg->findInt32("finalResult", &finalResult)); 1111 1112 if (audio) { 1113 mAudioEOS = true; 1114 } else { 1115 mVideoEOS = true; 1116 } 1117 1118 if (finalResult == ERROR_END_OF_STREAM) { 1119 ALOGV("reached %s EOS", audio ? "audio" : "video"); 1120 } else { 1121 ALOGE("%s track encountered an error (%d)", 1122 audio ? "audio" : "video", finalResult); 1123 1124 notifyListener( 1125 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult); 1126 } 1127 1128 if ((mAudioEOS || mAudioDecoder == NULL) 1129 && (mVideoEOS || mVideoDecoder == NULL)) { 1130 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 1131 } 1132 } else if (what == Renderer::kWhatFlushComplete) { 1133 int32_t audio; 1134 CHECK(msg->findInt32("audio", &audio)); 1135 1136 if (audio) { 1137 mAudioEOS = false; 1138 } else { 1139 mVideoEOS = false; 1140 } 1141 1142 ALOGV("renderer %s flush completed.", audio ? "audio" : "video"); 1143 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED 1144 || mFlushingAudio == SHUT_DOWN)) { 1145 // Flush has been handled by tear down. 1146 break; 1147 } 1148 handleFlushComplete(audio, false /* isDecoder */); 1149 finishFlushIfPossible(); 1150 } else if (what == Renderer::kWhatVideoRenderingStart) { 1151 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0); 1152 } else if (what == Renderer::kWhatMediaRenderingStart) { 1153 ALOGV("media rendering started"); 1154 notifyListener(MEDIA_STARTED, 0, 0); 1155 } else if (what == Renderer::kWhatAudioTearDown) { 1156 int32_t reason; 1157 CHECK(msg->findInt32("reason", &reason)); 1158 ALOGV("Tear down audio with reason %d.", reason); 1159 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) { 1160 // TimeoutWhenPaused is only for offload mode. 1161 ALOGW("Receive a stale message for teardown."); 1162 break; 1163 } 1164 int64_t positionUs; 1165 if (!msg->findInt64("positionUs", &positionUs)) { 1166 positionUs = mPreviousSeekTimeUs; 1167 } 1168 1169 restartAudio( 1170 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */, 1171 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */); 1172 } 1173 break; 1174 } 1175 1176 case kWhatMoreDataQueued: 1177 { 1178 break; 1179 } 1180 1181 case kWhatReset: 1182 { 1183 ALOGV("kWhatReset"); 1184 1185 mResetting = true; 1186 1187 mDeferredActions.push_back( 1188 new FlushDecoderAction( 1189 FLUSH_CMD_SHUTDOWN /* audio */, 1190 FLUSH_CMD_SHUTDOWN /* video */)); 1191 1192 mDeferredActions.push_back( 1193 new SimpleAction(&NuPlayer::performReset)); 1194 1195 processDeferredActions(); 1196 break; 1197 } 1198 1199 case kWhatSeek: 1200 { 1201 int64_t seekTimeUs; 1202 int32_t needNotify; 1203 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 1204 CHECK(msg->findInt32("needNotify", &needNotify)); 1205 1206 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d", 1207 (long long)seekTimeUs, needNotify); 1208 1209 if (!mStarted) { 1210 // Seek before the player is started. In order to preview video, 1211 // need to start the player and pause it. This branch is called 1212 // only once if needed. After the player is started, any seek 1213 // operation will go through normal path. 1214 // Audio-only cases are handled separately. 1215 onStart(seekTimeUs); 1216 if (mStarted) { 1217 onPause(); 1218 mPausedByClient = true; 1219 } 1220 if (needNotify) { 1221 notifyDriverSeekComplete(); 1222 } 1223 break; 1224 } 1225 1226 mDeferredActions.push_back( 1227 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */, 1228 FLUSH_CMD_FLUSH /* video */)); 1229 1230 mDeferredActions.push_back( 1231 new SeekAction(seekTimeUs)); 1232 1233 // After a flush without shutdown, decoder is paused. 1234 // Don't resume it until source seek is done, otherwise it could 1235 // start pulling stale data too soon. 1236 mDeferredActions.push_back( 1237 new ResumeDecoderAction(needNotify)); 1238 1239 processDeferredActions(); 1240 break; 1241 } 1242 1243 case kWhatPause: 1244 { 1245 onPause(); 1246 mPausedByClient = true; 1247 break; 1248 } 1249 1250 case kWhatSourceNotify: 1251 { 1252 onSourceNotify(msg); 1253 break; 1254 } 1255 1256 case kWhatClosedCaptionNotify: 1257 { 1258 onClosedCaptionNotify(msg); 1259 break; 1260 } 1261 1262 default: 1263 TRESPASS(); 1264 break; 1265 } 1266} 1267 1268void NuPlayer::onResume() { 1269 if (!mPaused || mResetting) { 1270 ALOGD_IF(mResetting, "resetting, onResume discarded"); 1271 return; 1272 } 1273 mPaused = false; 1274 if (mSource != NULL) { 1275 mSource->resume(); 1276 } else { 1277 ALOGW("resume called when source is gone or not set"); 1278 } 1279 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if 1280 // needed. 1281 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) { 1282 instantiateDecoder(true /* audio */, &mAudioDecoder); 1283 } 1284 if (mRenderer != NULL) { 1285 mRenderer->resume(); 1286 } else { 1287 ALOGW("resume called when renderer is gone or not set"); 1288 } 1289} 1290 1291status_t NuPlayer::onInstantiateSecureDecoders() { 1292 status_t err; 1293 if (!(mSourceFlags & Source::FLAG_SECURE)) { 1294 return BAD_TYPE; 1295 } 1296 1297 if (mRenderer != NULL) { 1298 ALOGE("renderer should not be set when instantiating secure decoders"); 1299 return UNKNOWN_ERROR; 1300 } 1301 1302 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting 1303 // data on instantiation. 1304 if (mSurface != NULL) { 1305 err = instantiateDecoder(false, &mVideoDecoder); 1306 if (err != OK) { 1307 return err; 1308 } 1309 } 1310 1311 if (mAudioSink != NULL) { 1312 err = instantiateDecoder(true, &mAudioDecoder); 1313 if (err != OK) { 1314 return err; 1315 } 1316 } 1317 return OK; 1318} 1319 1320void NuPlayer::onStart(int64_t startPositionUs) { 1321 if (!mSourceStarted) { 1322 mSourceStarted = true; 1323 mSource->start(); 1324 } 1325 if (startPositionUs > 0) { 1326 performSeek(startPositionUs); 1327 if (mSource->getFormat(false /* audio */) == NULL) { 1328 return; 1329 } 1330 } 1331 1332 mOffloadAudio = false; 1333 mAudioEOS = false; 1334 mVideoEOS = false; 1335 mStarted = true; 1336 mPaused = false; 1337 1338 uint32_t flags = 0; 1339 1340 if (mSource->isRealTime()) { 1341 flags |= Renderer::FLAG_REAL_TIME; 1342 } 1343 1344 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1345 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream 1346 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC; 1347 if (mAudioSink != NULL) { 1348 streamType = mAudioSink->getAudioStreamType(); 1349 } 1350 1351 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 1352 1353 mOffloadAudio = 1354 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType) 1355 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f); 1356 if (mOffloadAudio) { 1357 flags |= Renderer::FLAG_OFFLOAD_AUDIO; 1358 } 1359 1360 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this); 1361 ++mRendererGeneration; 1362 notify->setInt32("generation", mRendererGeneration); 1363 mRenderer = new Renderer(mAudioSink, notify, flags); 1364 mRendererLooper = new ALooper; 1365 mRendererLooper->setName("NuPlayerRenderer"); 1366 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO); 1367 mRendererLooper->registerHandler(mRenderer); 1368 1369 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings); 1370 if (err != OK) { 1371 mSource->stop(); 1372 mSourceStarted = false; 1373 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 1374 return; 1375 } 1376 1377 float rate = getFrameRate(); 1378 if (rate > 0) { 1379 mRenderer->setVideoFrameRate(rate); 1380 } 1381 1382 if (mVideoDecoder != NULL) { 1383 mVideoDecoder->setRenderer(mRenderer); 1384 } 1385 if (mAudioDecoder != NULL) { 1386 mAudioDecoder->setRenderer(mRenderer); 1387 } 1388 1389 postScanSources(); 1390} 1391 1392void NuPlayer::onPause() { 1393 if (mPaused) { 1394 return; 1395 } 1396 mPaused = true; 1397 if (mSource != NULL) { 1398 mSource->pause(); 1399 } else { 1400 ALOGW("pause called when source is gone or not set"); 1401 } 1402 if (mRenderer != NULL) { 1403 mRenderer->pause(); 1404 } else { 1405 ALOGW("pause called when renderer is gone or not set"); 1406 } 1407} 1408 1409bool NuPlayer::audioDecoderStillNeeded() { 1410 // Audio decoder is no longer needed if it's in shut/shutting down status. 1411 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER)); 1412} 1413 1414void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) { 1415 // We wait for both the decoder flush and the renderer flush to complete 1416 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state. 1417 1418 mFlushComplete[audio][isDecoder] = true; 1419 if (!mFlushComplete[audio][!isDecoder]) { 1420 return; 1421 } 1422 1423 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo; 1424 switch (*state) { 1425 case FLUSHING_DECODER: 1426 { 1427 *state = FLUSHED; 1428 break; 1429 } 1430 1431 case FLUSHING_DECODER_SHUTDOWN: 1432 { 1433 *state = SHUTTING_DOWN_DECODER; 1434 1435 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video"); 1436 if (!audio) { 1437 // Widevine source reads must stop before releasing the video decoder. 1438 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) { 1439 mSource->stop(); 1440 mSourceStarted = false; 1441 } 1442 } 1443 getDecoder(audio)->initiateShutdown(); 1444 break; 1445 } 1446 1447 default: 1448 // decoder flush completes only occur in a flushing state. 1449 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state); 1450 break; 1451 } 1452} 1453 1454void NuPlayer::finishFlushIfPossible() { 1455 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED 1456 && mFlushingAudio != SHUT_DOWN) { 1457 return; 1458 } 1459 1460 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED 1461 && mFlushingVideo != SHUT_DOWN) { 1462 return; 1463 } 1464 1465 ALOGV("both audio and video are flushed now."); 1466 1467 mFlushingAudio = NONE; 1468 mFlushingVideo = NONE; 1469 1470 clearFlushComplete(); 1471 1472 processDeferredActions(); 1473} 1474 1475void NuPlayer::postScanSources() { 1476 if (mScanSourcesPending) { 1477 return; 1478 } 1479 1480 sp<AMessage> msg = new AMessage(kWhatScanSources, this); 1481 msg->setInt32("generation", mScanSourcesGeneration); 1482 msg->post(); 1483 1484 mScanSourcesPending = true; 1485} 1486 1487void NuPlayer::tryOpenAudioSinkForOffload( 1488 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) { 1489 // Note: This is called early in NuPlayer to determine whether offloading 1490 // is possible; otherwise the decoders call the renderer openAudioSink directly. 1491 1492 status_t err = mRenderer->openAudioSink( 1493 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio); 1494 if (err != OK) { 1495 // Any failure we turn off mOffloadAudio. 1496 mOffloadAudio = false; 1497 } else if (mOffloadAudio) { 1498 sendMetaDataToHal(mAudioSink, audioMeta); 1499 } 1500} 1501 1502void NuPlayer::closeAudioSink() { 1503 mRenderer->closeAudioSink(); 1504} 1505 1506void NuPlayer::restartAudio( 1507 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) { 1508 if (mAudioDecoder != NULL) { 1509 mAudioDecoder->pause(); 1510 mAudioDecoder.clear(); 1511 ++mAudioDecoderGeneration; 1512 } 1513 if (mFlushingAudio == FLUSHING_DECODER) { 1514 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true; 1515 mFlushingAudio = FLUSHED; 1516 finishFlushIfPossible(); 1517 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN 1518 || mFlushingAudio == SHUTTING_DOWN_DECODER) { 1519 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true; 1520 mFlushingAudio = SHUT_DOWN; 1521 finishFlushIfPossible(); 1522 needsToCreateAudioDecoder = false; 1523 } 1524 if (mRenderer == NULL) { 1525 return; 1526 } 1527 closeAudioSink(); 1528 mRenderer->flush(true /* audio */, false /* notifyComplete */); 1529 if (mVideoDecoder != NULL) { 1530 mRenderer->flush(false /* audio */, false /* notifyComplete */); 1531 } 1532 1533 performSeek(currentPositionUs); 1534 1535 if (forceNonOffload) { 1536 mRenderer->signalDisableOffloadAudio(); 1537 mOffloadAudio = false; 1538 } 1539 if (needsToCreateAudioDecoder) { 1540 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload); 1541 } 1542} 1543 1544void NuPlayer::determineAudioModeChange(const sp<AMessage> &audioFormat) { 1545 if (mSource == NULL || mAudioSink == NULL) { 1546 return; 1547 } 1548 1549 if (mRenderer == NULL) { 1550 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety."); 1551 mOffloadAudio = false; 1552 return; 1553 } 1554 1555 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1556 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 1557 audio_stream_type_t streamType = mAudioSink->getAudioStreamType(); 1558 const bool hasVideo = (videoFormat != NULL); 1559 const bool canOffload = canOffloadStream( 1560 audioMeta, hasVideo, mSource->isStreaming(), streamType) 1561 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f); 1562 if (canOffload) { 1563 if (!mOffloadAudio) { 1564 mRenderer->signalEnableOffloadAudio(); 1565 } 1566 // open audio sink early under offload mode. 1567 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo); 1568 } else { 1569 if (mOffloadAudio) { 1570 mRenderer->signalDisableOffloadAudio(); 1571 mOffloadAudio = false; 1572 } 1573 } 1574} 1575 1576status_t NuPlayer::instantiateDecoder( 1577 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) { 1578 // The audio decoder could be cleared by tear down. If still in shut down 1579 // process, no need to create a new audio decoder. 1580 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) { 1581 return OK; 1582 } 1583 1584 sp<AMessage> format = mSource->getFormat(audio); 1585 1586 if (format == NULL) { 1587 return UNKNOWN_ERROR; 1588 } else { 1589 status_t err; 1590 if (format->findInt32("err", &err) && err) { 1591 return err; 1592 } 1593 } 1594 1595 format->setInt32("priority", 0 /* realtime */); 1596 1597 if (!audio) { 1598 AString mime; 1599 CHECK(format->findString("mime", &mime)); 1600 1601 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this); 1602 if (mCCDecoder == NULL) { 1603 mCCDecoder = new CCDecoder(ccNotify); 1604 } 1605 1606 if (mSourceFlags & Source::FLAG_SECURE) { 1607 format->setInt32("secure", true); 1608 } 1609 1610 if (mSourceFlags & Source::FLAG_PROTECTED) { 1611 format->setInt32("protected", true); 1612 } 1613 1614 float rate = getFrameRate(); 1615 if (rate > 0) { 1616 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed); 1617 } 1618 } 1619 1620 if (audio) { 1621 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this); 1622 ++mAudioDecoderGeneration; 1623 notify->setInt32("generation", mAudioDecoderGeneration); 1624 1625 if (checkAudioModeChange) { 1626 determineAudioModeChange(format); 1627 } 1628 if (mOffloadAudio) { 1629 mSource->setOffloadAudio(true /* offload */); 1630 1631 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL); 1632 format->setInt32("has-video", hasVideo); 1633 *decoder = new DecoderPassThrough(notify, mSource, mRenderer); 1634 } else { 1635 mSource->setOffloadAudio(false /* offload */); 1636 1637 *decoder = new Decoder(notify, mSource, mPID, mRenderer); 1638 } 1639 } else { 1640 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this); 1641 ++mVideoDecoderGeneration; 1642 notify->setInt32("generation", mVideoDecoderGeneration); 1643 1644 *decoder = new Decoder( 1645 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder); 1646 1647 // enable FRC if high-quality AV sync is requested, even if not 1648 // directly queuing to display, as this will even improve textureview 1649 // playback. 1650 { 1651 char value[PROPERTY_VALUE_MAX]; 1652 if (property_get("persist.sys.media.avsync", value, NULL) && 1653 (!strcmp("1", value) || !strcasecmp("true", value))) { 1654 format->setInt32("auto-frc", 1); 1655 } 1656 } 1657 } 1658 (*decoder)->init(); 1659 (*decoder)->configure(format); 1660 1661 // allocate buffers to decrypt widevine source buffers 1662 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) { 1663 Vector<sp<ABuffer> > inputBufs; 1664 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK); 1665 1666 Vector<MediaBuffer *> mediaBufs; 1667 for (size_t i = 0; i < inputBufs.size(); i++) { 1668 const sp<ABuffer> &buffer = inputBufs[i]; 1669 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size()); 1670 mediaBufs.push(mbuf); 1671 } 1672 1673 status_t err = mSource->setBuffers(audio, mediaBufs); 1674 if (err != OK) { 1675 for (size_t i = 0; i < mediaBufs.size(); ++i) { 1676 mediaBufs[i]->release(); 1677 } 1678 mediaBufs.clear(); 1679 ALOGE("Secure source didn't support secure mediaBufs."); 1680 return err; 1681 } 1682 } 1683 return OK; 1684} 1685 1686void NuPlayer::updateVideoSize( 1687 const sp<AMessage> &inputFormat, 1688 const sp<AMessage> &outputFormat) { 1689 if (inputFormat == NULL) { 1690 ALOGW("Unknown video size, reporting 0x0!"); 1691 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0); 1692 return; 1693 } 1694 1695 int32_t displayWidth, displayHeight; 1696 if (outputFormat != NULL) { 1697 int32_t width, height; 1698 CHECK(outputFormat->findInt32("width", &width)); 1699 CHECK(outputFormat->findInt32("height", &height)); 1700 1701 int32_t cropLeft, cropTop, cropRight, cropBottom; 1702 CHECK(outputFormat->findRect( 1703 "crop", 1704 &cropLeft, &cropTop, &cropRight, &cropBottom)); 1705 1706 displayWidth = cropRight - cropLeft + 1; 1707 displayHeight = cropBottom - cropTop + 1; 1708 1709 ALOGV("Video output format changed to %d x %d " 1710 "(crop: %d x %d @ (%d, %d))", 1711 width, height, 1712 displayWidth, 1713 displayHeight, 1714 cropLeft, cropTop); 1715 } else { 1716 CHECK(inputFormat->findInt32("width", &displayWidth)); 1717 CHECK(inputFormat->findInt32("height", &displayHeight)); 1718 1719 ALOGV("Video input format %d x %d", displayWidth, displayHeight); 1720 } 1721 1722 // Take into account sample aspect ratio if necessary: 1723 int32_t sarWidth, sarHeight; 1724 if (inputFormat->findInt32("sar-width", &sarWidth) 1725 && inputFormat->findInt32("sar-height", &sarHeight)) { 1726 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight); 1727 1728 displayWidth = (displayWidth * sarWidth) / sarHeight; 1729 1730 ALOGV("display dimensions %d x %d", displayWidth, displayHeight); 1731 } 1732 1733 int32_t rotationDegrees; 1734 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) { 1735 rotationDegrees = 0; 1736 } 1737 1738 if (rotationDegrees == 90 || rotationDegrees == 270) { 1739 int32_t tmp = displayWidth; 1740 displayWidth = displayHeight; 1741 displayHeight = tmp; 1742 } 1743 1744 notifyListener( 1745 MEDIA_SET_VIDEO_SIZE, 1746 displayWidth, 1747 displayHeight); 1748} 1749 1750void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) { 1751 if (mDriver == NULL) { 1752 return; 1753 } 1754 1755 sp<NuPlayerDriver> driver = mDriver.promote(); 1756 1757 if (driver == NULL) { 1758 return; 1759 } 1760 1761 driver->notifyListener(msg, ext1, ext2, in); 1762} 1763 1764void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 1765 ALOGV("[%s] flushDecoder needShutdown=%d", 1766 audio ? "audio" : "video", needShutdown); 1767 1768 const sp<DecoderBase> &decoder = getDecoder(audio); 1769 if (decoder == NULL) { 1770 ALOGI("flushDecoder %s without decoder present", 1771 audio ? "audio" : "video"); 1772 return; 1773 } 1774 1775 // Make sure we don't continue to scan sources until we finish flushing. 1776 ++mScanSourcesGeneration; 1777 if (mScanSourcesPending) { 1778 mDeferredActions.push_back( 1779 new SimpleAction(&NuPlayer::performScanSources)); 1780 mScanSourcesPending = false; 1781 } 1782 1783 decoder->signalFlush(); 1784 1785 FlushStatus newStatus = 1786 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 1787 1788 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL); 1789 mFlushComplete[audio][true /* isDecoder */] = false; 1790 if (audio) { 1791 ALOGE_IF(mFlushingAudio != NONE, 1792 "audio flushDecoder() is called in state %d", mFlushingAudio); 1793 mFlushingAudio = newStatus; 1794 } else { 1795 ALOGE_IF(mFlushingVideo != NONE, 1796 "video flushDecoder() is called in state %d", mFlushingVideo); 1797 mFlushingVideo = newStatus; 1798 } 1799} 1800 1801void NuPlayer::queueDecoderShutdown( 1802 bool audio, bool video, const sp<AMessage> &reply) { 1803 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video); 1804 1805 mDeferredActions.push_back( 1806 new FlushDecoderAction( 1807 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 1808 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE)); 1809 1810 mDeferredActions.push_back( 1811 new SimpleAction(&NuPlayer::performScanSources)); 1812 1813 mDeferredActions.push_back(new PostMessageAction(reply)); 1814 1815 processDeferredActions(); 1816} 1817 1818status_t NuPlayer::setVideoScalingMode(int32_t mode) { 1819 mVideoScalingMode = mode; 1820 if (mSurface != NULL) { 1821 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode); 1822 if (ret != OK) { 1823 ALOGE("Failed to set scaling mode (%d): %s", 1824 -ret, strerror(-ret)); 1825 return ret; 1826 } 1827 } 1828 return OK; 1829} 1830 1831status_t NuPlayer::getTrackInfo(Parcel* reply) const { 1832 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this); 1833 msg->setPointer("reply", reply); 1834 1835 sp<AMessage> response; 1836 status_t err = msg->postAndAwaitResponse(&response); 1837 return err; 1838} 1839 1840status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const { 1841 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this); 1842 msg->setPointer("reply", reply); 1843 msg->setInt32("type", type); 1844 1845 sp<AMessage> response; 1846 status_t err = msg->postAndAwaitResponse(&response); 1847 if (err == OK && response != NULL) { 1848 CHECK(response->findInt32("err", &err)); 1849 } 1850 return err; 1851} 1852 1853status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) { 1854 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this); 1855 msg->setSize("trackIndex", trackIndex); 1856 msg->setInt32("select", select); 1857 msg->setInt64("timeUs", timeUs); 1858 1859 sp<AMessage> response; 1860 status_t err = msg->postAndAwaitResponse(&response); 1861 1862 if (err != OK) { 1863 return err; 1864 } 1865 1866 if (!response->findInt32("err", &err)) { 1867 err = OK; 1868 } 1869 1870 return err; 1871} 1872 1873status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) { 1874 sp<Renderer> renderer = mRenderer; 1875 if (renderer == NULL) { 1876 return NO_INIT; 1877 } 1878 1879 return renderer->getCurrentPosition(mediaUs); 1880} 1881 1882void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) { 1883 CHECK(mTrackStats != NULL); 1884 1885 mTrackStats->clear(); 1886 if (mVideoDecoder != NULL) { 1887 mTrackStats->push_back(mVideoDecoder->getStats()); 1888 } 1889 if (mAudioDecoder != NULL) { 1890 mTrackStats->push_back(mAudioDecoder->getStats()); 1891 } 1892} 1893 1894sp<MetaData> NuPlayer::getFileMeta() { 1895 return mSource->getFileFormatMeta(); 1896} 1897 1898float NuPlayer::getFrameRate() { 1899 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */); 1900 if (meta == NULL) { 1901 return 0; 1902 } 1903 int32_t rate; 1904 if (!meta->findInt32(kKeyFrameRate, &rate)) { 1905 // fall back to try file meta 1906 sp<MetaData> fileMeta = getFileMeta(); 1907 if (fileMeta == NULL) { 1908 ALOGW("source has video meta but not file meta"); 1909 return -1; 1910 } 1911 int32_t fileMetaRate; 1912 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) { 1913 return -1; 1914 } 1915 return fileMetaRate; 1916 } 1917 return rate; 1918} 1919 1920void NuPlayer::schedulePollDuration() { 1921 sp<AMessage> msg = new AMessage(kWhatPollDuration, this); 1922 msg->setInt32("generation", mPollDurationGeneration); 1923 msg->post(); 1924} 1925 1926void NuPlayer::cancelPollDuration() { 1927 ++mPollDurationGeneration; 1928} 1929 1930void NuPlayer::processDeferredActions() { 1931 while (!mDeferredActions.empty()) { 1932 // We won't execute any deferred actions until we're no longer in 1933 // an intermediate state, i.e. one more more decoders are currently 1934 // flushing or shutting down. 1935 1936 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 1937 // We're currently flushing, postpone the reset until that's 1938 // completed. 1939 1940 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d", 1941 mFlushingAudio, mFlushingVideo); 1942 1943 break; 1944 } 1945 1946 sp<Action> action = *mDeferredActions.begin(); 1947 mDeferredActions.erase(mDeferredActions.begin()); 1948 1949 action->execute(this); 1950 } 1951} 1952 1953void NuPlayer::performSeek(int64_t seekTimeUs) { 1954 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)", 1955 (long long)seekTimeUs, 1956 seekTimeUs / 1E6); 1957 1958 if (mSource == NULL) { 1959 // This happens when reset occurs right before the loop mode 1960 // asynchronously seeks to the start of the stream. 1961 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL, 1962 "mSource is NULL and decoders not NULL audio(%p) video(%p)", 1963 mAudioDecoder.get(), mVideoDecoder.get()); 1964 return; 1965 } 1966 mPreviousSeekTimeUs = seekTimeUs; 1967 mSource->seekTo(seekTimeUs); 1968 ++mTimedTextGeneration; 1969 1970 // everything's flushed, continue playback. 1971} 1972 1973void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) { 1974 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video); 1975 1976 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL) 1977 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) { 1978 return; 1979 } 1980 1981 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) { 1982 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN)); 1983 } 1984 1985 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) { 1986 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN)); 1987 } 1988} 1989 1990void NuPlayer::performReset() { 1991 ALOGV("performReset"); 1992 1993 CHECK(mAudioDecoder == NULL); 1994 CHECK(mVideoDecoder == NULL); 1995 1996 cancelPollDuration(); 1997 1998 ++mScanSourcesGeneration; 1999 mScanSourcesPending = false; 2000 2001 if (mRendererLooper != NULL) { 2002 if (mRenderer != NULL) { 2003 mRendererLooper->unregisterHandler(mRenderer->id()); 2004 } 2005 mRendererLooper->stop(); 2006 mRendererLooper.clear(); 2007 } 2008 mRenderer.clear(); 2009 ++mRendererGeneration; 2010 2011 if (mSource != NULL) { 2012 mSource->stop(); 2013 2014 Mutex::Autolock autoLock(mSourceLock); 2015 mSource.clear(); 2016 } 2017 2018 if (mDriver != NULL) { 2019 sp<NuPlayerDriver> driver = mDriver.promote(); 2020 if (driver != NULL) { 2021 driver->notifyResetComplete(); 2022 } 2023 } 2024 2025 mStarted = false; 2026 mPrepared = false; 2027 mResetting = false; 2028 mSourceStarted = false; 2029} 2030 2031void NuPlayer::performScanSources() { 2032 ALOGV("performScanSources"); 2033 2034 if (!mStarted) { 2035 return; 2036 } 2037 2038 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 2039 postScanSources(); 2040 } 2041} 2042 2043void NuPlayer::performSetSurface(const sp<Surface> &surface) { 2044 ALOGV("performSetSurface"); 2045 2046 mSurface = surface; 2047 2048 // XXX - ignore error from setVideoScalingMode for now 2049 setVideoScalingMode(mVideoScalingMode); 2050 2051 if (mDriver != NULL) { 2052 sp<NuPlayerDriver> driver = mDriver.promote(); 2053 if (driver != NULL) { 2054 driver->notifySetSurfaceComplete(); 2055 } 2056 } 2057} 2058 2059void NuPlayer::performResumeDecoders(bool needNotify) { 2060 if (needNotify) { 2061 mResumePending = true; 2062 if (mVideoDecoder == NULL) { 2063 // if audio-only, we can notify seek complete now, 2064 // as the resume operation will be relatively fast. 2065 finishResume(); 2066 } 2067 } 2068 2069 if (mVideoDecoder != NULL) { 2070 // When there is continuous seek, MediaPlayer will cache the seek 2071 // position, and send down new seek request when previous seek is 2072 // complete. Let's wait for at least one video output frame before 2073 // notifying seek complete, so that the video thumbnail gets updated 2074 // when seekbar is dragged. 2075 mVideoDecoder->signalResume(needNotify); 2076 } 2077 2078 if (mAudioDecoder != NULL) { 2079 mAudioDecoder->signalResume(false /* needNotify */); 2080 } 2081} 2082 2083void NuPlayer::finishResume() { 2084 if (mResumePending) { 2085 mResumePending = false; 2086 notifyDriverSeekComplete(); 2087 } 2088} 2089 2090void NuPlayer::notifyDriverSeekComplete() { 2091 if (mDriver != NULL) { 2092 sp<NuPlayerDriver> driver = mDriver.promote(); 2093 if (driver != NULL) { 2094 driver->notifySeekComplete(); 2095 } 2096 } 2097} 2098 2099void NuPlayer::onSourceNotify(const sp<AMessage> &msg) { 2100 int32_t what; 2101 CHECK(msg->findInt32("what", &what)); 2102 2103 switch (what) { 2104 case Source::kWhatInstantiateSecureDecoders: 2105 { 2106 if (mSource == NULL) { 2107 // This is a stale notification from a source that was 2108 // asynchronously preparing when the client called reset(). 2109 // We handled the reset, the source is gone. 2110 break; 2111 } 2112 2113 sp<AMessage> reply; 2114 CHECK(msg->findMessage("reply", &reply)); 2115 status_t err = onInstantiateSecureDecoders(); 2116 reply->setInt32("err", err); 2117 reply->post(); 2118 break; 2119 } 2120 2121 case Source::kWhatPrepared: 2122 { 2123 if (mSource == NULL) { 2124 // This is a stale notification from a source that was 2125 // asynchronously preparing when the client called reset(). 2126 // We handled the reset, the source is gone. 2127 break; 2128 } 2129 2130 int32_t err; 2131 CHECK(msg->findInt32("err", &err)); 2132 2133 if (err != OK) { 2134 // shut down potential secure codecs in case client never calls reset 2135 mDeferredActions.push_back( 2136 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */, 2137 FLUSH_CMD_SHUTDOWN /* video */)); 2138 processDeferredActions(); 2139 } else { 2140 mPrepared = true; 2141 } 2142 2143 sp<NuPlayerDriver> driver = mDriver.promote(); 2144 if (driver != NULL) { 2145 // notify duration first, so that it's definitely set when 2146 // the app received the "prepare complete" callback. 2147 int64_t durationUs; 2148 if (mSource->getDuration(&durationUs) == OK) { 2149 driver->notifyDuration(durationUs); 2150 } 2151 driver->notifyPrepareCompleted(err); 2152 } 2153 2154 break; 2155 } 2156 2157 case Source::kWhatFlagsChanged: 2158 { 2159 uint32_t flags; 2160 CHECK(msg->findInt32("flags", (int32_t *)&flags)); 2161 2162 sp<NuPlayerDriver> driver = mDriver.promote(); 2163 if (driver != NULL) { 2164 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) { 2165 driver->notifyListener( 2166 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0); 2167 } 2168 driver->notifyFlagsChanged(flags); 2169 } 2170 2171 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2172 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) { 2173 cancelPollDuration(); 2174 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2175 && (flags & Source::FLAG_DYNAMIC_DURATION) 2176 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 2177 schedulePollDuration(); 2178 } 2179 2180 mSourceFlags = flags; 2181 break; 2182 } 2183 2184 case Source::kWhatVideoSizeChanged: 2185 { 2186 sp<AMessage> format; 2187 CHECK(msg->findMessage("format", &format)); 2188 2189 updateVideoSize(format); 2190 break; 2191 } 2192 2193 case Source::kWhatBufferingUpdate: 2194 { 2195 int32_t percentage; 2196 CHECK(msg->findInt32("percentage", &percentage)); 2197 2198 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0); 2199 break; 2200 } 2201 2202 case Source::kWhatPauseOnBufferingStart: 2203 { 2204 // ignore if not playing 2205 if (mStarted) { 2206 ALOGI("buffer low, pausing..."); 2207 2208 mPausedForBuffering = true; 2209 onPause(); 2210 } 2211 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0); 2212 break; 2213 } 2214 2215 case Source::kWhatResumeOnBufferingEnd: 2216 { 2217 // ignore if not playing 2218 if (mStarted) { 2219 ALOGI("buffer ready, resuming..."); 2220 2221 mPausedForBuffering = false; 2222 2223 // do not resume yet if client didn't unpause 2224 if (!mPausedByClient) { 2225 onResume(); 2226 } 2227 } 2228 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0); 2229 break; 2230 } 2231 2232 case Source::kWhatCacheStats: 2233 { 2234 int32_t kbps; 2235 CHECK(msg->findInt32("bandwidth", &kbps)); 2236 2237 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps); 2238 break; 2239 } 2240 2241 case Source::kWhatSubtitleData: 2242 { 2243 sp<ABuffer> buffer; 2244 CHECK(msg->findBuffer("buffer", &buffer)); 2245 2246 sendSubtitleData(buffer, 0 /* baseIndex */); 2247 break; 2248 } 2249 2250 case Source::kWhatTimedMetaData: 2251 { 2252 sp<ABuffer> buffer; 2253 if (!msg->findBuffer("buffer", &buffer)) { 2254 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2255 } else { 2256 sendTimedMetaData(buffer); 2257 } 2258 break; 2259 } 2260 2261 case Source::kWhatTimedTextData: 2262 { 2263 int32_t generation; 2264 if (msg->findInt32("generation", &generation) 2265 && generation != mTimedTextGeneration) { 2266 break; 2267 } 2268 2269 sp<ABuffer> buffer; 2270 CHECK(msg->findBuffer("buffer", &buffer)); 2271 2272 sp<NuPlayerDriver> driver = mDriver.promote(); 2273 if (driver == NULL) { 2274 break; 2275 } 2276 2277 int posMs; 2278 int64_t timeUs, posUs; 2279 driver->getCurrentPosition(&posMs); 2280 posUs = (int64_t) posMs * 1000ll; 2281 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2282 2283 if (posUs < timeUs) { 2284 if (!msg->findInt32("generation", &generation)) { 2285 msg->setInt32("generation", mTimedTextGeneration); 2286 } 2287 msg->post(timeUs - posUs); 2288 } else { 2289 sendTimedTextData(buffer); 2290 } 2291 break; 2292 } 2293 2294 case Source::kWhatQueueDecoderShutdown: 2295 { 2296 int32_t audio, video; 2297 CHECK(msg->findInt32("audio", &audio)); 2298 CHECK(msg->findInt32("video", &video)); 2299 2300 sp<AMessage> reply; 2301 CHECK(msg->findMessage("reply", &reply)); 2302 2303 queueDecoderShutdown(audio, video, reply); 2304 break; 2305 } 2306 2307 case Source::kWhatDrmNoLicense: 2308 { 2309 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE); 2310 break; 2311 } 2312 2313 default: 2314 TRESPASS(); 2315 } 2316} 2317 2318void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) { 2319 int32_t what; 2320 CHECK(msg->findInt32("what", &what)); 2321 2322 switch (what) { 2323 case NuPlayer::CCDecoder::kWhatClosedCaptionData: 2324 { 2325 sp<ABuffer> buffer; 2326 CHECK(msg->findBuffer("buffer", &buffer)); 2327 2328 size_t inbandTracks = 0; 2329 if (mSource != NULL) { 2330 inbandTracks = mSource->getTrackCount(); 2331 } 2332 2333 sendSubtitleData(buffer, inbandTracks); 2334 break; 2335 } 2336 2337 case NuPlayer::CCDecoder::kWhatTrackAdded: 2338 { 2339 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2340 2341 break; 2342 } 2343 2344 default: 2345 TRESPASS(); 2346 } 2347 2348 2349} 2350 2351void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) { 2352 int32_t trackIndex; 2353 int64_t timeUs, durationUs; 2354 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex)); 2355 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2356 CHECK(buffer->meta()->findInt64("durationUs", &durationUs)); 2357 2358 Parcel in; 2359 in.writeInt32(trackIndex + baseIndex); 2360 in.writeInt64(timeUs); 2361 in.writeInt64(durationUs); 2362 in.writeInt32(buffer->size()); 2363 in.writeInt32(buffer->size()); 2364 in.write(buffer->data(), buffer->size()); 2365 2366 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in); 2367} 2368 2369void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) { 2370 int64_t timeUs; 2371 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2372 2373 Parcel in; 2374 in.writeInt64(timeUs); 2375 in.writeInt32(buffer->size()); 2376 in.writeInt32(buffer->size()); 2377 in.write(buffer->data(), buffer->size()); 2378 2379 notifyListener(MEDIA_META_DATA, 0, 0, &in); 2380} 2381 2382void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) { 2383 const void *data; 2384 size_t size = 0; 2385 int64_t timeUs; 2386 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP; 2387 2388 AString mime; 2389 CHECK(buffer->meta()->findString("mime", &mime)); 2390 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0); 2391 2392 data = buffer->data(); 2393 size = buffer->size(); 2394 2395 Parcel parcel; 2396 if (size > 0) { 2397 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2398 int32_t global = 0; 2399 if (buffer->meta()->findInt32("global", &global) && global) { 2400 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS; 2401 } else { 2402 flag |= TextDescriptions::LOCAL_DESCRIPTIONS; 2403 } 2404 TextDescriptions::getParcelOfDescriptions( 2405 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel); 2406 } 2407 2408 if ((parcel.dataSize() > 0)) { 2409 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel); 2410 } else { // send an empty timed text 2411 notifyListener(MEDIA_TIMED_TEXT, 0, 0); 2412 } 2413} 2414//////////////////////////////////////////////////////////////////////////////// 2415 2416sp<AMessage> NuPlayer::Source::getFormat(bool audio) { 2417 sp<MetaData> meta = getFormatMeta(audio); 2418 2419 if (meta == NULL) { 2420 return NULL; 2421 } 2422 2423 sp<AMessage> msg = new AMessage; 2424 2425 if(convertMetaDataToMessage(meta, &msg) == OK) { 2426 return msg; 2427 } 2428 return NULL; 2429} 2430 2431void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) { 2432 sp<AMessage> notify = dupNotify(); 2433 notify->setInt32("what", kWhatFlagsChanged); 2434 notify->setInt32("flags", flags); 2435 notify->post(); 2436} 2437 2438void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) { 2439 sp<AMessage> notify = dupNotify(); 2440 notify->setInt32("what", kWhatVideoSizeChanged); 2441 notify->setMessage("format", format); 2442 notify->post(); 2443} 2444 2445void NuPlayer::Source::notifyPrepared(status_t err) { 2446 sp<AMessage> notify = dupNotify(); 2447 notify->setInt32("what", kWhatPrepared); 2448 notify->setInt32("err", err); 2449 notify->post(); 2450} 2451 2452void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) { 2453 sp<AMessage> notify = dupNotify(); 2454 notify->setInt32("what", kWhatInstantiateSecureDecoders); 2455 notify->setMessage("reply", reply); 2456 notify->post(); 2457} 2458 2459void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) { 2460 TRESPASS(); 2461} 2462 2463} // namespace android 2464