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