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