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