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