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