NuPlayer.cpp revision 2c9a30a341e50f53039e1b7da96dfa91104fdd62
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->pause(); 1118 mAudioDecoder.clear(); 1119 ++mAudioDecoderGeneration; 1120 bool needsToCreateAudioDecoder = true; 1121 if (mFlushingAudio == FLUSHING_DECODER) { 1122 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true; 1123 mFlushingAudio = FLUSHED; 1124 finishFlushIfPossible(); 1125 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN 1126 || mFlushingAudio == SHUTTING_DOWN_DECODER) { 1127 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true; 1128 mFlushingAudio = SHUT_DOWN; 1129 finishFlushIfPossible(); 1130 needsToCreateAudioDecoder = false; 1131 } 1132 if (mRenderer == NULL) { 1133 break; 1134 } 1135 closeAudioSink(); 1136 mRenderer->flush( 1137 true /* audio */, false /* notifyComplete */); 1138 if (mVideoDecoder != NULL) { 1139 mRenderer->flush( 1140 false /* audio */, false /* notifyComplete */); 1141 } 1142 1143 int64_t positionUs; 1144 if (!msg->findInt64("positionUs", &positionUs)) { 1145 positionUs = mPreviousSeekTimeUs; 1146 } 1147 performSeek(positionUs); 1148 1149 if (reason == Renderer::kDueToError && needsToCreateAudioDecoder) { 1150 instantiateDecoder(true /* audio */, &mAudioDecoder); 1151 } 1152 } 1153 break; 1154 } 1155 1156 case kWhatMoreDataQueued: 1157 { 1158 break; 1159 } 1160 1161 case kWhatReset: 1162 { 1163 ALOGV("kWhatReset"); 1164 1165 mResetting = true; 1166 1167 mDeferredActions.push_back( 1168 new FlushDecoderAction( 1169 FLUSH_CMD_SHUTDOWN /* audio */, 1170 FLUSH_CMD_SHUTDOWN /* video */)); 1171 1172 mDeferredActions.push_back( 1173 new SimpleAction(&NuPlayer::performReset)); 1174 1175 processDeferredActions(); 1176 break; 1177 } 1178 1179 case kWhatSeek: 1180 { 1181 int64_t seekTimeUs; 1182 int32_t needNotify; 1183 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 1184 CHECK(msg->findInt32("needNotify", &needNotify)); 1185 1186 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d", 1187 (long long)seekTimeUs, needNotify); 1188 1189 if (!mStarted) { 1190 // Seek before the player is started. In order to preview video, 1191 // need to start the player and pause it. This branch is called 1192 // only once if needed. After the player is started, any seek 1193 // operation will go through normal path. 1194 // Audio-only cases are handled separately. 1195 onStart(seekTimeUs); 1196 if (mStarted) { 1197 onPause(); 1198 mPausedByClient = true; 1199 } 1200 if (needNotify) { 1201 notifyDriverSeekComplete(); 1202 } 1203 break; 1204 } 1205 1206 mDeferredActions.push_back( 1207 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */, 1208 FLUSH_CMD_FLUSH /* video */)); 1209 1210 mDeferredActions.push_back( 1211 new SeekAction(seekTimeUs)); 1212 1213 // After a flush without shutdown, decoder is paused. 1214 // Don't resume it until source seek is done, otherwise it could 1215 // start pulling stale data too soon. 1216 mDeferredActions.push_back( 1217 new ResumeDecoderAction(needNotify)); 1218 1219 processDeferredActions(); 1220 break; 1221 } 1222 1223 case kWhatPause: 1224 { 1225 onPause(); 1226 mPausedByClient = true; 1227 break; 1228 } 1229 1230 case kWhatSourceNotify: 1231 { 1232 onSourceNotify(msg); 1233 break; 1234 } 1235 1236 case kWhatClosedCaptionNotify: 1237 { 1238 onClosedCaptionNotify(msg); 1239 break; 1240 } 1241 1242 default: 1243 TRESPASS(); 1244 break; 1245 } 1246} 1247 1248void NuPlayer::onResume() { 1249 if (!mPaused || mResetting) { 1250 ALOGD_IF(mResetting, "resetting, onResume discarded"); 1251 return; 1252 } 1253 mPaused = false; 1254 if (mSource != NULL) { 1255 mSource->resume(); 1256 } else { 1257 ALOGW("resume called when source is gone or not set"); 1258 } 1259 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if 1260 // needed. 1261 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) { 1262 instantiateDecoder(true /* audio */, &mAudioDecoder); 1263 } 1264 if (mRenderer != NULL) { 1265 mRenderer->resume(); 1266 } else { 1267 ALOGW("resume called when renderer is gone or not set"); 1268 } 1269} 1270 1271status_t NuPlayer::onInstantiateSecureDecoders() { 1272 status_t err; 1273 if (!(mSourceFlags & Source::FLAG_SECURE)) { 1274 return BAD_TYPE; 1275 } 1276 1277 if (mRenderer != NULL) { 1278 ALOGE("renderer should not be set when instantiating secure decoders"); 1279 return UNKNOWN_ERROR; 1280 } 1281 1282 // TRICKY: We rely on mRenderer being null, so that decoder does not start requesting 1283 // data on instantiation. 1284 if (mSurface != NULL) { 1285 err = instantiateDecoder(false, &mVideoDecoder); 1286 if (err != OK) { 1287 return err; 1288 } 1289 } 1290 1291 if (mAudioSink != NULL) { 1292 err = instantiateDecoder(true, &mAudioDecoder); 1293 if (err != OK) { 1294 return err; 1295 } 1296 } 1297 return OK; 1298} 1299 1300void NuPlayer::onStart(int64_t startPositionUs) { 1301 if (!mSourceStarted) { 1302 mSourceStarted = true; 1303 mSource->start(); 1304 } 1305 if (startPositionUs > 0) { 1306 performSeek(startPositionUs); 1307 if (mSource->getFormat(false /* audio */) == NULL) { 1308 return; 1309 } 1310 } 1311 1312 mOffloadAudio = false; 1313 mAudioEOS = false; 1314 mVideoEOS = false; 1315 mStarted = true; 1316 1317 uint32_t flags = 0; 1318 1319 if (mSource->isRealTime()) { 1320 flags |= Renderer::FLAG_REAL_TIME; 1321 } 1322 1323 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1324 ALOGV_IF(audioMeta == NULL, "no metadata for audio source"); // video only stream 1325 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC; 1326 if (mAudioSink != NULL) { 1327 streamType = mAudioSink->getAudioStreamType(); 1328 } 1329 1330 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 1331 1332 mOffloadAudio = 1333 canOffloadStream(audioMeta, (videoFormat != NULL), mSource->isStreaming(), streamType); 1334 if (mOffloadAudio) { 1335 flags |= Renderer::FLAG_OFFLOAD_AUDIO; 1336 } 1337 1338 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this); 1339 ++mRendererGeneration; 1340 notify->setInt32("generation", mRendererGeneration); 1341 mRenderer = new Renderer(mAudioSink, notify, flags); 1342 mRendererLooper = new ALooper; 1343 mRendererLooper->setName("NuPlayerRenderer"); 1344 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO); 1345 mRendererLooper->registerHandler(mRenderer); 1346 1347 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings); 1348 if (err != OK) { 1349 mSource->stop(); 1350 mSourceStarted = false; 1351 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 1352 return; 1353 } 1354 1355 float rate = getFrameRate(); 1356 if (rate > 0) { 1357 mRenderer->setVideoFrameRate(rate); 1358 } 1359 1360 if (mVideoDecoder != NULL) { 1361 mVideoDecoder->setRenderer(mRenderer); 1362 } 1363 if (mAudioDecoder != NULL) { 1364 mAudioDecoder->setRenderer(mRenderer); 1365 } 1366 1367 postScanSources(); 1368} 1369 1370void NuPlayer::onPause() { 1371 if (mPaused) { 1372 return; 1373 } 1374 mPaused = true; 1375 if (mSource != NULL) { 1376 mSource->pause(); 1377 } else { 1378 ALOGW("pause called when source is gone or not set"); 1379 } 1380 if (mRenderer != NULL) { 1381 mRenderer->pause(); 1382 } else { 1383 ALOGW("pause called when renderer is gone or not set"); 1384 } 1385} 1386 1387bool NuPlayer::audioDecoderStillNeeded() { 1388 // Audio decoder is no longer needed if it's in shut/shutting down status. 1389 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER)); 1390} 1391 1392void NuPlayer::handleFlushComplete(bool audio, bool isDecoder) { 1393 // We wait for both the decoder flush and the renderer flush to complete 1394 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state. 1395 1396 mFlushComplete[audio][isDecoder] = true; 1397 if (!mFlushComplete[audio][!isDecoder]) { 1398 return; 1399 } 1400 1401 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo; 1402 switch (*state) { 1403 case FLUSHING_DECODER: 1404 { 1405 *state = FLUSHED; 1406 break; 1407 } 1408 1409 case FLUSHING_DECODER_SHUTDOWN: 1410 { 1411 *state = SHUTTING_DOWN_DECODER; 1412 1413 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video"); 1414 if (!audio) { 1415 // Widevine source reads must stop before releasing the video decoder. 1416 if (mSource != NULL && mSourceFlags & Source::FLAG_SECURE) { 1417 mSource->stop(); 1418 mSourceStarted = false; 1419 } 1420 } 1421 getDecoder(audio)->initiateShutdown(); 1422 break; 1423 } 1424 1425 default: 1426 // decoder flush completes only occur in a flushing state. 1427 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state); 1428 break; 1429 } 1430} 1431 1432void NuPlayer::finishFlushIfPossible() { 1433 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED 1434 && mFlushingAudio != SHUT_DOWN) { 1435 return; 1436 } 1437 1438 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED 1439 && mFlushingVideo != SHUT_DOWN) { 1440 return; 1441 } 1442 1443 ALOGV("both audio and video are flushed now."); 1444 1445 mFlushingAudio = NONE; 1446 mFlushingVideo = NONE; 1447 1448 clearFlushComplete(); 1449 1450 processDeferredActions(); 1451} 1452 1453void NuPlayer::postScanSources() { 1454 if (mScanSourcesPending) { 1455 return; 1456 } 1457 1458 sp<AMessage> msg = new AMessage(kWhatScanSources, this); 1459 msg->setInt32("generation", mScanSourcesGeneration); 1460 msg->post(); 1461 1462 mScanSourcesPending = true; 1463} 1464 1465void NuPlayer::tryOpenAudioSinkForOffload(const sp<AMessage> &format, bool hasVideo) { 1466 // Note: This is called early in NuPlayer to determine whether offloading 1467 // is possible; otherwise the decoders call the renderer openAudioSink directly. 1468 1469 status_t err = mRenderer->openAudioSink( 1470 format, true /* offloadOnly */, hasVideo, AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio); 1471 if (err != OK) { 1472 // Any failure we turn off mOffloadAudio. 1473 mOffloadAudio = false; 1474 } else if (mOffloadAudio) { 1475 sp<MetaData> audioMeta = 1476 mSource->getFormatMeta(true /* audio */); 1477 sendMetaDataToHal(mAudioSink, audioMeta); 1478 } 1479} 1480 1481void NuPlayer::closeAudioSink() { 1482 mRenderer->closeAudioSink(); 1483} 1484 1485void NuPlayer::determineAudioModeChange() { 1486 if (mSource == NULL || mAudioSink == NULL) { 1487 return; 1488 } 1489 1490 if (mRenderer == NULL) { 1491 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety."); 1492 mOffloadAudio = false; 1493 return; 1494 } 1495 1496 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1497 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 1498 audio_stream_type_t streamType = mAudioSink->getAudioStreamType(); 1499 const bool hasVideo = (videoFormat != NULL); 1500 const bool canOffload = canOffloadStream( 1501 audioMeta, hasVideo, mSource->isStreaming(), streamType); 1502 if (canOffload) { 1503 if (!mOffloadAudio) { 1504 mRenderer->signalEnableOffloadAudio(); 1505 } 1506 // open audio sink early under offload mode. 1507 sp<AMessage> format = mSource->getFormat(true /*audio*/); 1508 tryOpenAudioSinkForOffload(format, hasVideo); 1509 } else { 1510 if (mOffloadAudio) { 1511 mRenderer->signalDisableOffloadAudio(); 1512 mOffloadAudio = false; 1513 } 1514 } 1515} 1516 1517status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) { 1518 // The audio decoder could be cleared by tear down. If still in shut down 1519 // process, no need to create a new audio decoder. 1520 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) { 1521 return OK; 1522 } 1523 1524 sp<AMessage> format = mSource->getFormat(audio); 1525 1526 if (format == NULL) { 1527 return UNKNOWN_ERROR; 1528 } else { 1529 status_t err; 1530 if (format->findInt32("err", &err) && err) { 1531 return err; 1532 } 1533 } 1534 1535 format->setInt32("priority", 0 /* realtime */); 1536 1537 if (!audio) { 1538 AString mime; 1539 CHECK(format->findString("mime", &mime)); 1540 1541 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this); 1542 if (mCCDecoder == NULL) { 1543 mCCDecoder = new CCDecoder(ccNotify); 1544 } 1545 1546 if (mSourceFlags & Source::FLAG_SECURE) { 1547 format->setInt32("secure", true); 1548 } 1549 1550 if (mSourceFlags & Source::FLAG_PROTECTED) { 1551 format->setInt32("protected", true); 1552 } 1553 1554 float rate = getFrameRate(); 1555 if (rate > 0) { 1556 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed); 1557 } 1558 } 1559 1560 if (audio) { 1561 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this); 1562 ++mAudioDecoderGeneration; 1563 notify->setInt32("generation", mAudioDecoderGeneration); 1564 1565 determineAudioModeChange(); 1566 if (mOffloadAudio) { 1567 mSource->setOffloadAudio(true /* offload */); 1568 1569 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL); 1570 format->setInt32("has-video", hasVideo); 1571 *decoder = new DecoderPassThrough(notify, mSource, mRenderer); 1572 } else { 1573 mSource->setOffloadAudio(false /* offload */); 1574 1575 *decoder = new Decoder(notify, mSource, mPID, mRenderer); 1576 } 1577 } else { 1578 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this); 1579 ++mVideoDecoderGeneration; 1580 notify->setInt32("generation", mVideoDecoderGeneration); 1581 1582 *decoder = new Decoder( 1583 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder); 1584 1585 // enable FRC if high-quality AV sync is requested, even if not 1586 // directly queuing to display, as this will even improve textureview 1587 // playback. 1588 { 1589 char value[PROPERTY_VALUE_MAX]; 1590 if (property_get("persist.sys.media.avsync", value, NULL) && 1591 (!strcmp("1", value) || !strcasecmp("true", value))) { 1592 format->setInt32("auto-frc", 1); 1593 } 1594 } 1595 } 1596 (*decoder)->init(); 1597 (*decoder)->configure(format); 1598 1599 // allocate buffers to decrypt widevine source buffers 1600 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) { 1601 Vector<sp<ABuffer> > inputBufs; 1602 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK); 1603 1604 Vector<MediaBuffer *> mediaBufs; 1605 for (size_t i = 0; i < inputBufs.size(); i++) { 1606 const sp<ABuffer> &buffer = inputBufs[i]; 1607 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size()); 1608 mediaBufs.push(mbuf); 1609 } 1610 1611 status_t err = mSource->setBuffers(audio, mediaBufs); 1612 if (err != OK) { 1613 for (size_t i = 0; i < mediaBufs.size(); ++i) { 1614 mediaBufs[i]->release(); 1615 } 1616 mediaBufs.clear(); 1617 ALOGE("Secure source didn't support secure mediaBufs."); 1618 return err; 1619 } 1620 } 1621 return OK; 1622} 1623 1624void NuPlayer::updateVideoSize( 1625 const sp<AMessage> &inputFormat, 1626 const sp<AMessage> &outputFormat) { 1627 if (inputFormat == NULL) { 1628 ALOGW("Unknown video size, reporting 0x0!"); 1629 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0); 1630 return; 1631 } 1632 1633 int32_t displayWidth, displayHeight; 1634 if (outputFormat != NULL) { 1635 int32_t width, height; 1636 CHECK(outputFormat->findInt32("width", &width)); 1637 CHECK(outputFormat->findInt32("height", &height)); 1638 1639 int32_t cropLeft, cropTop, cropRight, cropBottom; 1640 CHECK(outputFormat->findRect( 1641 "crop", 1642 &cropLeft, &cropTop, &cropRight, &cropBottom)); 1643 1644 displayWidth = cropRight - cropLeft + 1; 1645 displayHeight = cropBottom - cropTop + 1; 1646 1647 ALOGV("Video output format changed to %d x %d " 1648 "(crop: %d x %d @ (%d, %d))", 1649 width, height, 1650 displayWidth, 1651 displayHeight, 1652 cropLeft, cropTop); 1653 } else { 1654 CHECK(inputFormat->findInt32("width", &displayWidth)); 1655 CHECK(inputFormat->findInt32("height", &displayHeight)); 1656 1657 ALOGV("Video input format %d x %d", displayWidth, displayHeight); 1658 } 1659 1660 // Take into account sample aspect ratio if necessary: 1661 int32_t sarWidth, sarHeight; 1662 if (inputFormat->findInt32("sar-width", &sarWidth) 1663 && inputFormat->findInt32("sar-height", &sarHeight)) { 1664 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight); 1665 1666 displayWidth = (displayWidth * sarWidth) / sarHeight; 1667 1668 ALOGV("display dimensions %d x %d", displayWidth, displayHeight); 1669 } 1670 1671 int32_t rotationDegrees; 1672 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) { 1673 rotationDegrees = 0; 1674 } 1675 1676 if (rotationDegrees == 90 || rotationDegrees == 270) { 1677 int32_t tmp = displayWidth; 1678 displayWidth = displayHeight; 1679 displayHeight = tmp; 1680 } 1681 1682 notifyListener( 1683 MEDIA_SET_VIDEO_SIZE, 1684 displayWidth, 1685 displayHeight); 1686} 1687 1688void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) { 1689 if (mDriver == NULL) { 1690 return; 1691 } 1692 1693 sp<NuPlayerDriver> driver = mDriver.promote(); 1694 1695 if (driver == NULL) { 1696 return; 1697 } 1698 1699 driver->notifyListener(msg, ext1, ext2, in); 1700} 1701 1702void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 1703 ALOGV("[%s] flushDecoder needShutdown=%d", 1704 audio ? "audio" : "video", needShutdown); 1705 1706 const sp<DecoderBase> &decoder = getDecoder(audio); 1707 if (decoder == NULL) { 1708 ALOGI("flushDecoder %s without decoder present", 1709 audio ? "audio" : "video"); 1710 return; 1711 } 1712 1713 // Make sure we don't continue to scan sources until we finish flushing. 1714 ++mScanSourcesGeneration; 1715 if (mScanSourcesPending) { 1716 mDeferredActions.push_back( 1717 new SimpleAction(&NuPlayer::performScanSources)); 1718 mScanSourcesPending = false; 1719 } 1720 1721 decoder->signalFlush(); 1722 1723 FlushStatus newStatus = 1724 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 1725 1726 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL); 1727 mFlushComplete[audio][true /* isDecoder */] = false; 1728 if (audio) { 1729 ALOGE_IF(mFlushingAudio != NONE, 1730 "audio flushDecoder() is called in state %d", mFlushingAudio); 1731 mFlushingAudio = newStatus; 1732 } else { 1733 ALOGE_IF(mFlushingVideo != NONE, 1734 "video flushDecoder() is called in state %d", mFlushingVideo); 1735 mFlushingVideo = newStatus; 1736 } 1737} 1738 1739void NuPlayer::queueDecoderShutdown( 1740 bool audio, bool video, const sp<AMessage> &reply) { 1741 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video); 1742 1743 mDeferredActions.push_back( 1744 new FlushDecoderAction( 1745 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 1746 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE)); 1747 1748 mDeferredActions.push_back( 1749 new SimpleAction(&NuPlayer::performScanSources)); 1750 1751 mDeferredActions.push_back(new PostMessageAction(reply)); 1752 1753 processDeferredActions(); 1754} 1755 1756status_t NuPlayer::setVideoScalingMode(int32_t mode) { 1757 mVideoScalingMode = mode; 1758 if (mSurface != NULL) { 1759 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode); 1760 if (ret != OK) { 1761 ALOGE("Failed to set scaling mode (%d): %s", 1762 -ret, strerror(-ret)); 1763 return ret; 1764 } 1765 } 1766 return OK; 1767} 1768 1769status_t NuPlayer::getTrackInfo(Parcel* reply) const { 1770 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this); 1771 msg->setPointer("reply", reply); 1772 1773 sp<AMessage> response; 1774 status_t err = msg->postAndAwaitResponse(&response); 1775 return err; 1776} 1777 1778status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const { 1779 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this); 1780 msg->setPointer("reply", reply); 1781 msg->setInt32("type", type); 1782 1783 sp<AMessage> response; 1784 status_t err = msg->postAndAwaitResponse(&response); 1785 if (err == OK && response != NULL) { 1786 CHECK(response->findInt32("err", &err)); 1787 } 1788 return err; 1789} 1790 1791status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) { 1792 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this); 1793 msg->setSize("trackIndex", trackIndex); 1794 msg->setInt32("select", select); 1795 msg->setInt64("timeUs", timeUs); 1796 1797 sp<AMessage> response; 1798 status_t err = msg->postAndAwaitResponse(&response); 1799 1800 if (err != OK) { 1801 return err; 1802 } 1803 1804 if (!response->findInt32("err", &err)) { 1805 err = OK; 1806 } 1807 1808 return err; 1809} 1810 1811status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) { 1812 sp<Renderer> renderer = mRenderer; 1813 if (renderer == NULL) { 1814 return NO_INIT; 1815 } 1816 1817 return renderer->getCurrentPosition(mediaUs); 1818} 1819 1820void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) { 1821 CHECK(mTrackStats != NULL); 1822 1823 mTrackStats->clear(); 1824 if (mVideoDecoder != NULL) { 1825 mTrackStats->push_back(mVideoDecoder->getStats()); 1826 } 1827 if (mAudioDecoder != NULL) { 1828 mTrackStats->push_back(mAudioDecoder->getStats()); 1829 } 1830} 1831 1832sp<MetaData> NuPlayer::getFileMeta() { 1833 return mSource->getFileFormatMeta(); 1834} 1835 1836float NuPlayer::getFrameRate() { 1837 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */); 1838 if (meta == NULL) { 1839 return 0; 1840 } 1841 int32_t rate; 1842 if (!meta->findInt32(kKeyFrameRate, &rate)) { 1843 // fall back to try file meta 1844 sp<MetaData> fileMeta = getFileMeta(); 1845 if (fileMeta == NULL) { 1846 ALOGW("source has video meta but not file meta"); 1847 return -1; 1848 } 1849 int32_t fileMetaRate; 1850 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) { 1851 return -1; 1852 } 1853 return fileMetaRate; 1854 } 1855 return rate; 1856} 1857 1858void NuPlayer::schedulePollDuration() { 1859 sp<AMessage> msg = new AMessage(kWhatPollDuration, this); 1860 msg->setInt32("generation", mPollDurationGeneration); 1861 msg->post(); 1862} 1863 1864void NuPlayer::cancelPollDuration() { 1865 ++mPollDurationGeneration; 1866} 1867 1868void NuPlayer::processDeferredActions() { 1869 while (!mDeferredActions.empty()) { 1870 // We won't execute any deferred actions until we're no longer in 1871 // an intermediate state, i.e. one more more decoders are currently 1872 // flushing or shutting down. 1873 1874 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 1875 // We're currently flushing, postpone the reset until that's 1876 // completed. 1877 1878 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d", 1879 mFlushingAudio, mFlushingVideo); 1880 1881 break; 1882 } 1883 1884 sp<Action> action = *mDeferredActions.begin(); 1885 mDeferredActions.erase(mDeferredActions.begin()); 1886 1887 action->execute(this); 1888 } 1889} 1890 1891void NuPlayer::performSeek(int64_t seekTimeUs) { 1892 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)", 1893 (long long)seekTimeUs, 1894 seekTimeUs / 1E6); 1895 1896 if (mSource == NULL) { 1897 // This happens when reset occurs right before the loop mode 1898 // asynchronously seeks to the start of the stream. 1899 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL, 1900 "mSource is NULL and decoders not NULL audio(%p) video(%p)", 1901 mAudioDecoder.get(), mVideoDecoder.get()); 1902 return; 1903 } 1904 mPreviousSeekTimeUs = seekTimeUs; 1905 mSource->seekTo(seekTimeUs); 1906 ++mTimedTextGeneration; 1907 1908 // everything's flushed, continue playback. 1909} 1910 1911void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) { 1912 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video); 1913 1914 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL) 1915 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) { 1916 return; 1917 } 1918 1919 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) { 1920 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN)); 1921 } 1922 1923 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) { 1924 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN)); 1925 } 1926} 1927 1928void NuPlayer::performReset() { 1929 ALOGV("performReset"); 1930 1931 CHECK(mAudioDecoder == NULL); 1932 CHECK(mVideoDecoder == NULL); 1933 1934 cancelPollDuration(); 1935 1936 ++mScanSourcesGeneration; 1937 mScanSourcesPending = false; 1938 1939 if (mRendererLooper != NULL) { 1940 if (mRenderer != NULL) { 1941 mRendererLooper->unregisterHandler(mRenderer->id()); 1942 } 1943 mRendererLooper->stop(); 1944 mRendererLooper.clear(); 1945 } 1946 mRenderer.clear(); 1947 ++mRendererGeneration; 1948 1949 if (mSource != NULL) { 1950 mSource->stop(); 1951 1952 mSource.clear(); 1953 } 1954 1955 if (mDriver != NULL) { 1956 sp<NuPlayerDriver> driver = mDriver.promote(); 1957 if (driver != NULL) { 1958 driver->notifyResetComplete(); 1959 } 1960 } 1961 1962 mStarted = false; 1963 mResetting = false; 1964 mSourceStarted = false; 1965} 1966 1967void NuPlayer::performScanSources() { 1968 ALOGV("performScanSources"); 1969 1970 if (!mStarted) { 1971 return; 1972 } 1973 1974 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 1975 postScanSources(); 1976 } 1977} 1978 1979void NuPlayer::performSetSurface(const sp<Surface> &surface) { 1980 ALOGV("performSetSurface"); 1981 1982 mSurface = surface; 1983 1984 // XXX - ignore error from setVideoScalingMode for now 1985 setVideoScalingMode(mVideoScalingMode); 1986 1987 if (mDriver != NULL) { 1988 sp<NuPlayerDriver> driver = mDriver.promote(); 1989 if (driver != NULL) { 1990 driver->notifySetSurfaceComplete(); 1991 } 1992 } 1993} 1994 1995void NuPlayer::performResumeDecoders(bool needNotify) { 1996 if (needNotify) { 1997 mResumePending = true; 1998 if (mVideoDecoder == NULL) { 1999 // if audio-only, we can notify seek complete now, 2000 // as the resume operation will be relatively fast. 2001 finishResume(); 2002 } 2003 } 2004 2005 if (mVideoDecoder != NULL) { 2006 // When there is continuous seek, MediaPlayer will cache the seek 2007 // position, and send down new seek request when previous seek is 2008 // complete. Let's wait for at least one video output frame before 2009 // notifying seek complete, so that the video thumbnail gets updated 2010 // when seekbar is dragged. 2011 mVideoDecoder->signalResume(needNotify); 2012 } 2013 2014 if (mAudioDecoder != NULL) { 2015 mAudioDecoder->signalResume(false /* needNotify */); 2016 } 2017} 2018 2019void NuPlayer::finishResume() { 2020 if (mResumePending) { 2021 mResumePending = false; 2022 notifyDriverSeekComplete(); 2023 } 2024} 2025 2026void NuPlayer::notifyDriverSeekComplete() { 2027 if (mDriver != NULL) { 2028 sp<NuPlayerDriver> driver = mDriver.promote(); 2029 if (driver != NULL) { 2030 driver->notifySeekComplete(); 2031 } 2032 } 2033} 2034 2035void NuPlayer::onSourceNotify(const sp<AMessage> &msg) { 2036 int32_t what; 2037 CHECK(msg->findInt32("what", &what)); 2038 2039 switch (what) { 2040 case Source::kWhatInstantiateSecureDecoders: 2041 { 2042 if (mSource == NULL) { 2043 // This is a stale notification from a source that was 2044 // asynchronously preparing when the client called reset(). 2045 // We handled the reset, the source is gone. 2046 break; 2047 } 2048 2049 sp<AMessage> reply; 2050 CHECK(msg->findMessage("reply", &reply)); 2051 status_t err = onInstantiateSecureDecoders(); 2052 reply->setInt32("err", err); 2053 reply->post(); 2054 break; 2055 } 2056 2057 case Source::kWhatPrepared: 2058 { 2059 if (mSource == NULL) { 2060 // This is a stale notification from a source that was 2061 // asynchronously preparing when the client called reset(). 2062 // We handled the reset, the source is gone. 2063 break; 2064 } 2065 2066 int32_t err; 2067 CHECK(msg->findInt32("err", &err)); 2068 2069 if (err != OK) { 2070 // shut down potential secure codecs in case client never calls reset 2071 mDeferredActions.push_back( 2072 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */, 2073 FLUSH_CMD_SHUTDOWN /* video */)); 2074 processDeferredActions(); 2075 } 2076 2077 sp<NuPlayerDriver> driver = mDriver.promote(); 2078 if (driver != NULL) { 2079 // notify duration first, so that it's definitely set when 2080 // the app received the "prepare complete" callback. 2081 int64_t durationUs; 2082 if (mSource->getDuration(&durationUs) == OK) { 2083 driver->notifyDuration(durationUs); 2084 } 2085 driver->notifyPrepareCompleted(err); 2086 } 2087 2088 break; 2089 } 2090 2091 case Source::kWhatFlagsChanged: 2092 { 2093 uint32_t flags; 2094 CHECK(msg->findInt32("flags", (int32_t *)&flags)); 2095 2096 sp<NuPlayerDriver> driver = mDriver.promote(); 2097 if (driver != NULL) { 2098 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) { 2099 driver->notifyListener( 2100 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0); 2101 } 2102 driver->notifyFlagsChanged(flags); 2103 } 2104 2105 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2106 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) { 2107 cancelPollDuration(); 2108 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2109 && (flags & Source::FLAG_DYNAMIC_DURATION) 2110 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 2111 schedulePollDuration(); 2112 } 2113 2114 mSourceFlags = flags; 2115 break; 2116 } 2117 2118 case Source::kWhatVideoSizeChanged: 2119 { 2120 sp<AMessage> format; 2121 CHECK(msg->findMessage("format", &format)); 2122 2123 updateVideoSize(format); 2124 break; 2125 } 2126 2127 case Source::kWhatBufferingUpdate: 2128 { 2129 int32_t percentage; 2130 CHECK(msg->findInt32("percentage", &percentage)); 2131 2132 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0); 2133 break; 2134 } 2135 2136 case Source::kWhatPauseOnBufferingStart: 2137 { 2138 // ignore if not playing 2139 if (mStarted) { 2140 ALOGI("buffer low, pausing..."); 2141 2142 mPausedForBuffering = true; 2143 onPause(); 2144 } 2145 2146 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0); 2147 break; 2148 } 2149 2150 case Source::kWhatResumeOnBufferingEnd: 2151 { 2152 // ignore if not playing 2153 if (mStarted) { 2154 ALOGI("buffer ready, resuming..."); 2155 2156 mPausedForBuffering = false; 2157 2158 // do not resume yet if client didn't unpause 2159 if (!mPausedByClient) { 2160 onResume(); 2161 } 2162 } 2163 2164 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0); 2165 break; 2166 } 2167 2168 case Source::kWhatCacheStats: 2169 { 2170 int32_t kbps; 2171 CHECK(msg->findInt32("bandwidth", &kbps)); 2172 2173 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps); 2174 break; 2175 } 2176 2177 case Source::kWhatSubtitleData: 2178 { 2179 sp<ABuffer> buffer; 2180 CHECK(msg->findBuffer("buffer", &buffer)); 2181 2182 sendSubtitleData(buffer, 0 /* baseIndex */); 2183 break; 2184 } 2185 2186 case Source::kWhatTimedMetaData: 2187 { 2188 sp<ABuffer> buffer; 2189 if (!msg->findBuffer("buffer", &buffer)) { 2190 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2191 } else { 2192 sendTimedMetaData(buffer); 2193 } 2194 break; 2195 } 2196 2197 case Source::kWhatTimedTextData: 2198 { 2199 int32_t generation; 2200 if (msg->findInt32("generation", &generation) 2201 && generation != mTimedTextGeneration) { 2202 break; 2203 } 2204 2205 sp<ABuffer> buffer; 2206 CHECK(msg->findBuffer("buffer", &buffer)); 2207 2208 sp<NuPlayerDriver> driver = mDriver.promote(); 2209 if (driver == NULL) { 2210 break; 2211 } 2212 2213 int posMs; 2214 int64_t timeUs, posUs; 2215 driver->getCurrentPosition(&posMs); 2216 posUs = (int64_t) posMs * 1000ll; 2217 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2218 2219 if (posUs < timeUs) { 2220 if (!msg->findInt32("generation", &generation)) { 2221 msg->setInt32("generation", mTimedTextGeneration); 2222 } 2223 msg->post(timeUs - posUs); 2224 } else { 2225 sendTimedTextData(buffer); 2226 } 2227 break; 2228 } 2229 2230 case Source::kWhatQueueDecoderShutdown: 2231 { 2232 int32_t audio, video; 2233 CHECK(msg->findInt32("audio", &audio)); 2234 CHECK(msg->findInt32("video", &video)); 2235 2236 sp<AMessage> reply; 2237 CHECK(msg->findMessage("reply", &reply)); 2238 2239 queueDecoderShutdown(audio, video, reply); 2240 break; 2241 } 2242 2243 case Source::kWhatDrmNoLicense: 2244 { 2245 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE); 2246 break; 2247 } 2248 2249 default: 2250 TRESPASS(); 2251 } 2252} 2253 2254void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) { 2255 int32_t what; 2256 CHECK(msg->findInt32("what", &what)); 2257 2258 switch (what) { 2259 case NuPlayer::CCDecoder::kWhatClosedCaptionData: 2260 { 2261 sp<ABuffer> buffer; 2262 CHECK(msg->findBuffer("buffer", &buffer)); 2263 2264 size_t inbandTracks = 0; 2265 if (mSource != NULL) { 2266 inbandTracks = mSource->getTrackCount(); 2267 } 2268 2269 sendSubtitleData(buffer, inbandTracks); 2270 break; 2271 } 2272 2273 case NuPlayer::CCDecoder::kWhatTrackAdded: 2274 { 2275 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2276 2277 break; 2278 } 2279 2280 default: 2281 TRESPASS(); 2282 } 2283 2284 2285} 2286 2287void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) { 2288 int32_t trackIndex; 2289 int64_t timeUs, durationUs; 2290 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex)); 2291 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2292 CHECK(buffer->meta()->findInt64("durationUs", &durationUs)); 2293 2294 Parcel in; 2295 in.writeInt32(trackIndex + baseIndex); 2296 in.writeInt64(timeUs); 2297 in.writeInt64(durationUs); 2298 in.writeInt32(buffer->size()); 2299 in.writeInt32(buffer->size()); 2300 in.write(buffer->data(), buffer->size()); 2301 2302 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in); 2303} 2304 2305void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) { 2306 int64_t timeUs; 2307 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2308 2309 Parcel in; 2310 in.writeInt64(timeUs); 2311 in.writeInt32(buffer->size()); 2312 in.writeInt32(buffer->size()); 2313 in.write(buffer->data(), buffer->size()); 2314 2315 notifyListener(MEDIA_META_DATA, 0, 0, &in); 2316} 2317 2318void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) { 2319 const void *data; 2320 size_t size = 0; 2321 int64_t timeUs; 2322 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP; 2323 2324 AString mime; 2325 CHECK(buffer->meta()->findString("mime", &mime)); 2326 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0); 2327 2328 data = buffer->data(); 2329 size = buffer->size(); 2330 2331 Parcel parcel; 2332 if (size > 0) { 2333 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2334 int32_t global = 0; 2335 if (buffer->meta()->findInt32("global", &global) && global) { 2336 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS; 2337 } else { 2338 flag |= TextDescriptions::LOCAL_DESCRIPTIONS; 2339 } 2340 TextDescriptions::getParcelOfDescriptions( 2341 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel); 2342 } 2343 2344 if ((parcel.dataSize() > 0)) { 2345 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel); 2346 } else { // send an empty timed text 2347 notifyListener(MEDIA_TIMED_TEXT, 0, 0); 2348 } 2349} 2350//////////////////////////////////////////////////////////////////////////////// 2351 2352sp<AMessage> NuPlayer::Source::getFormat(bool audio) { 2353 sp<MetaData> meta = getFormatMeta(audio); 2354 2355 if (meta == NULL) { 2356 return NULL; 2357 } 2358 2359 sp<AMessage> msg = new AMessage; 2360 2361 if(convertMetaDataToMessage(meta, &msg) == OK) { 2362 return msg; 2363 } 2364 return NULL; 2365} 2366 2367void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) { 2368 sp<AMessage> notify = dupNotify(); 2369 notify->setInt32("what", kWhatFlagsChanged); 2370 notify->setInt32("flags", flags); 2371 notify->post(); 2372} 2373 2374void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) { 2375 sp<AMessage> notify = dupNotify(); 2376 notify->setInt32("what", kWhatVideoSizeChanged); 2377 notify->setMessage("format", format); 2378 notify->post(); 2379} 2380 2381void NuPlayer::Source::notifyPrepared(status_t err) { 2382 sp<AMessage> notify = dupNotify(); 2383 notify->setInt32("what", kWhatPrepared); 2384 notify->setInt32("err", err); 2385 notify->post(); 2386} 2387 2388void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) { 2389 sp<AMessage> notify = dupNotify(); 2390 notify->setInt32("what", kWhatInstantiateSecureDecoders); 2391 notify->setMessage("reply", reply); 2392 notify->post(); 2393} 2394 2395void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) { 2396 TRESPASS(); 2397} 2398 2399} // namespace android 2400