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