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