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