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