NuPlayer.cpp revision e1d701902765c710398133025cfeee3ea8b6d280
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 if (mOffloadAudio) { 1478 mRenderer->signalDisableOffloadAudio(); 1479 mOffloadAudio = false; 1480 } 1481 } 1482} 1483 1484status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) { 1485 if (*decoder != NULL) { 1486 return OK; 1487 } 1488 1489 sp<AMessage> format = mSource->getFormat(audio); 1490 1491 if (format == NULL) { 1492 return -EWOULDBLOCK; 1493 } 1494 1495 format->setInt32("priority", 0 /* realtime */); 1496 1497 if (!audio) { 1498 AString mime; 1499 CHECK(format->findString("mime", &mime)); 1500 1501 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this); 1502 if (mCCDecoder == NULL) { 1503 mCCDecoder = new CCDecoder(ccNotify); 1504 } 1505 1506 if (mSourceFlags & Source::FLAG_SECURE) { 1507 format->setInt32("secure", true); 1508 } 1509 1510 if (mSourceFlags & Source::FLAG_PROTECTED) { 1511 format->setInt32("protected", true); 1512 } 1513 1514 float rate = getFrameRate(); 1515 if (rate > 0) { 1516 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed); 1517 } 1518 } 1519 1520 if (audio) { 1521 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this); 1522 ++mAudioDecoderGeneration; 1523 notify->setInt32("generation", mAudioDecoderGeneration); 1524 1525 determineAudioModeChange(); 1526 if (mOffloadAudio) { 1527 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL); 1528 format->setInt32("has-video", hasVideo); 1529 *decoder = new DecoderPassThrough(notify, mSource, mRenderer); 1530 } else { 1531 *decoder = new Decoder(notify, mSource, mPID, mRenderer); 1532 } 1533 } else { 1534 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this); 1535 ++mVideoDecoderGeneration; 1536 notify->setInt32("generation", mVideoDecoderGeneration); 1537 1538 *decoder = new Decoder( 1539 notify, mSource, mPID, mRenderer, mSurface, mCCDecoder); 1540 1541 // enable FRC if high-quality AV sync is requested, even if not 1542 // directly queuing to display, as this will even improve textureview 1543 // playback. 1544 { 1545 char value[PROPERTY_VALUE_MAX]; 1546 if (property_get("persist.sys.media.avsync", value, NULL) && 1547 (!strcmp("1", value) || !strcasecmp("true", value))) { 1548 format->setInt32("auto-frc", 1); 1549 } 1550 } 1551 } 1552 (*decoder)->init(); 1553 (*decoder)->configure(format); 1554 1555 // allocate buffers to decrypt widevine source buffers 1556 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) { 1557 Vector<sp<ABuffer> > inputBufs; 1558 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK); 1559 1560 Vector<MediaBuffer *> mediaBufs; 1561 for (size_t i = 0; i < inputBufs.size(); i++) { 1562 const sp<ABuffer> &buffer = inputBufs[i]; 1563 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size()); 1564 mediaBufs.push(mbuf); 1565 } 1566 1567 status_t err = mSource->setBuffers(audio, mediaBufs); 1568 if (err != OK) { 1569 for (size_t i = 0; i < mediaBufs.size(); ++i) { 1570 mediaBufs[i]->release(); 1571 } 1572 mediaBufs.clear(); 1573 ALOGE("Secure source didn't support secure mediaBufs."); 1574 return err; 1575 } 1576 } 1577 return OK; 1578} 1579 1580void NuPlayer::updateVideoSize( 1581 const sp<AMessage> &inputFormat, 1582 const sp<AMessage> &outputFormat) { 1583 if (inputFormat == NULL) { 1584 ALOGW("Unknown video size, reporting 0x0!"); 1585 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0); 1586 return; 1587 } 1588 1589 int32_t displayWidth, displayHeight; 1590 if (outputFormat != NULL) { 1591 int32_t width, height; 1592 CHECK(outputFormat->findInt32("width", &width)); 1593 CHECK(outputFormat->findInt32("height", &height)); 1594 1595 int32_t cropLeft, cropTop, cropRight, cropBottom; 1596 CHECK(outputFormat->findRect( 1597 "crop", 1598 &cropLeft, &cropTop, &cropRight, &cropBottom)); 1599 1600 displayWidth = cropRight - cropLeft + 1; 1601 displayHeight = cropBottom - cropTop + 1; 1602 1603 ALOGV("Video output format changed to %d x %d " 1604 "(crop: %d x %d @ (%d, %d))", 1605 width, height, 1606 displayWidth, 1607 displayHeight, 1608 cropLeft, cropTop); 1609 } else { 1610 CHECK(inputFormat->findInt32("width", &displayWidth)); 1611 CHECK(inputFormat->findInt32("height", &displayHeight)); 1612 1613 ALOGV("Video input format %d x %d", displayWidth, displayHeight); 1614 } 1615 1616 // Take into account sample aspect ratio if necessary: 1617 int32_t sarWidth, sarHeight; 1618 if (inputFormat->findInt32("sar-width", &sarWidth) 1619 && inputFormat->findInt32("sar-height", &sarHeight)) { 1620 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight); 1621 1622 displayWidth = (displayWidth * sarWidth) / sarHeight; 1623 1624 ALOGV("display dimensions %d x %d", displayWidth, displayHeight); 1625 } 1626 1627 int32_t rotationDegrees; 1628 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) { 1629 rotationDegrees = 0; 1630 } 1631 1632 if (rotationDegrees == 90 || rotationDegrees == 270) { 1633 int32_t tmp = displayWidth; 1634 displayWidth = displayHeight; 1635 displayHeight = tmp; 1636 } 1637 1638 notifyListener( 1639 MEDIA_SET_VIDEO_SIZE, 1640 displayWidth, 1641 displayHeight); 1642} 1643 1644void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) { 1645 if (mDriver == NULL) { 1646 return; 1647 } 1648 1649 sp<NuPlayerDriver> driver = mDriver.promote(); 1650 1651 if (driver == NULL) { 1652 return; 1653 } 1654 1655 driver->notifyListener(msg, ext1, ext2, in); 1656} 1657 1658void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 1659 ALOGV("[%s] flushDecoder needShutdown=%d", 1660 audio ? "audio" : "video", needShutdown); 1661 1662 const sp<DecoderBase> &decoder = getDecoder(audio); 1663 if (decoder == NULL) { 1664 ALOGI("flushDecoder %s without decoder present", 1665 audio ? "audio" : "video"); 1666 return; 1667 } 1668 1669 // Make sure we don't continue to scan sources until we finish flushing. 1670 ++mScanSourcesGeneration; 1671 if (mScanSourcesPending) { 1672 mDeferredActions.push_back( 1673 new SimpleAction(&NuPlayer::performScanSources)); 1674 mScanSourcesPending = false; 1675 } 1676 1677 decoder->signalFlush(); 1678 1679 FlushStatus newStatus = 1680 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 1681 1682 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL); 1683 mFlushComplete[audio][true /* isDecoder */] = false; 1684 if (audio) { 1685 ALOGE_IF(mFlushingAudio != NONE, 1686 "audio flushDecoder() is called in state %d", mFlushingAudio); 1687 mFlushingAudio = newStatus; 1688 } else { 1689 ALOGE_IF(mFlushingVideo != NONE, 1690 "video flushDecoder() is called in state %d", mFlushingVideo); 1691 mFlushingVideo = newStatus; 1692 } 1693} 1694 1695void NuPlayer::queueDecoderShutdown( 1696 bool audio, bool video, const sp<AMessage> &reply) { 1697 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video); 1698 1699 mDeferredActions.push_back( 1700 new FlushDecoderAction( 1701 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 1702 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE)); 1703 1704 mDeferredActions.push_back( 1705 new SimpleAction(&NuPlayer::performScanSources)); 1706 1707 mDeferredActions.push_back(new PostMessageAction(reply)); 1708 1709 processDeferredActions(); 1710} 1711 1712status_t NuPlayer::setVideoScalingMode(int32_t mode) { 1713 mVideoScalingMode = mode; 1714 if (mSurface != NULL) { 1715 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode); 1716 if (ret != OK) { 1717 ALOGE("Failed to set scaling mode (%d): %s", 1718 -ret, strerror(-ret)); 1719 return ret; 1720 } 1721 } 1722 return OK; 1723} 1724 1725status_t NuPlayer::getTrackInfo(Parcel* reply) const { 1726 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this); 1727 msg->setPointer("reply", reply); 1728 1729 sp<AMessage> response; 1730 status_t err = msg->postAndAwaitResponse(&response); 1731 return err; 1732} 1733 1734status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const { 1735 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this); 1736 msg->setPointer("reply", reply); 1737 msg->setInt32("type", type); 1738 1739 sp<AMessage> response; 1740 status_t err = msg->postAndAwaitResponse(&response); 1741 if (err == OK && response != NULL) { 1742 CHECK(response->findInt32("err", &err)); 1743 } 1744 return err; 1745} 1746 1747status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) { 1748 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this); 1749 msg->setSize("trackIndex", trackIndex); 1750 msg->setInt32("select", select); 1751 msg->setInt64("timeUs", timeUs); 1752 1753 sp<AMessage> response; 1754 status_t err = msg->postAndAwaitResponse(&response); 1755 1756 if (err != OK) { 1757 return err; 1758 } 1759 1760 if (!response->findInt32("err", &err)) { 1761 err = OK; 1762 } 1763 1764 return err; 1765} 1766 1767status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) { 1768 sp<Renderer> renderer = mRenderer; 1769 if (renderer == NULL) { 1770 return NO_INIT; 1771 } 1772 1773 return renderer->getCurrentPosition(mediaUs); 1774} 1775 1776void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) { 1777 CHECK(mTrackStats != NULL); 1778 1779 mTrackStats->clear(); 1780 if (mVideoDecoder != NULL) { 1781 mTrackStats->push_back(mVideoDecoder->getStats()); 1782 } 1783 if (mAudioDecoder != NULL) { 1784 mTrackStats->push_back(mAudioDecoder->getStats()); 1785 } 1786} 1787 1788sp<MetaData> NuPlayer::getFileMeta() { 1789 return mSource->getFileFormatMeta(); 1790} 1791 1792float NuPlayer::getFrameRate() { 1793 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */); 1794 if (meta == NULL) { 1795 return 0; 1796 } 1797 int32_t rate; 1798 if (!meta->findInt32(kKeyFrameRate, &rate)) { 1799 // fall back to try file meta 1800 sp<MetaData> fileMeta = getFileMeta(); 1801 if (fileMeta == NULL) { 1802 ALOGW("source has video meta but not file meta"); 1803 return -1; 1804 } 1805 int32_t fileMetaRate; 1806 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) { 1807 return -1; 1808 } 1809 return fileMetaRate; 1810 } 1811 return rate; 1812} 1813 1814void NuPlayer::schedulePollDuration() { 1815 sp<AMessage> msg = new AMessage(kWhatPollDuration, this); 1816 msg->setInt32("generation", mPollDurationGeneration); 1817 msg->post(); 1818} 1819 1820void NuPlayer::cancelPollDuration() { 1821 ++mPollDurationGeneration; 1822} 1823 1824void NuPlayer::processDeferredActions() { 1825 while (!mDeferredActions.empty()) { 1826 // We won't execute any deferred actions until we're no longer in 1827 // an intermediate state, i.e. one more more decoders are currently 1828 // flushing or shutting down. 1829 1830 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 1831 // We're currently flushing, postpone the reset until that's 1832 // completed. 1833 1834 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d", 1835 mFlushingAudio, mFlushingVideo); 1836 1837 break; 1838 } 1839 1840 sp<Action> action = *mDeferredActions.begin(); 1841 mDeferredActions.erase(mDeferredActions.begin()); 1842 1843 action->execute(this); 1844 } 1845} 1846 1847void NuPlayer::performSeek(int64_t seekTimeUs) { 1848 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)", 1849 (long long)seekTimeUs, 1850 seekTimeUs / 1E6); 1851 1852 if (mSource == NULL) { 1853 // This happens when reset occurs right before the loop mode 1854 // asynchronously seeks to the start of the stream. 1855 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL, 1856 "mSource is NULL and decoders not NULL audio(%p) video(%p)", 1857 mAudioDecoder.get(), mVideoDecoder.get()); 1858 return; 1859 } 1860 mSource->seekTo(seekTimeUs); 1861 ++mTimedTextGeneration; 1862 1863 // everything's flushed, continue playback. 1864} 1865 1866void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) { 1867 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video); 1868 1869 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL) 1870 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) { 1871 return; 1872 } 1873 1874 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) { 1875 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN)); 1876 } 1877 1878 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) { 1879 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN)); 1880 } 1881} 1882 1883void NuPlayer::performReset() { 1884 ALOGV("performReset"); 1885 1886 CHECK(mAudioDecoder == NULL); 1887 CHECK(mVideoDecoder == NULL); 1888 1889 cancelPollDuration(); 1890 1891 ++mScanSourcesGeneration; 1892 mScanSourcesPending = false; 1893 1894 if (mRendererLooper != NULL) { 1895 if (mRenderer != NULL) { 1896 mRendererLooper->unregisterHandler(mRenderer->id()); 1897 } 1898 mRendererLooper->stop(); 1899 mRendererLooper.clear(); 1900 } 1901 mRenderer.clear(); 1902 ++mRendererGeneration; 1903 1904 if (mSource != NULL) { 1905 mSource->stop(); 1906 1907 mSource.clear(); 1908 } 1909 1910 if (mDriver != NULL) { 1911 sp<NuPlayerDriver> driver = mDriver.promote(); 1912 if (driver != NULL) { 1913 driver->notifyResetComplete(); 1914 } 1915 } 1916 1917 mStarted = false; 1918 mSourceStarted = false; 1919} 1920 1921void NuPlayer::performScanSources() { 1922 ALOGV("performScanSources"); 1923 1924 if (!mStarted) { 1925 return; 1926 } 1927 1928 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 1929 postScanSources(); 1930 } 1931} 1932 1933void NuPlayer::performSetSurface(const sp<Surface> &surface) { 1934 ALOGV("performSetSurface"); 1935 1936 mSurface = surface; 1937 1938 // XXX - ignore error from setVideoScalingMode for now 1939 setVideoScalingMode(mVideoScalingMode); 1940 1941 if (mDriver != NULL) { 1942 sp<NuPlayerDriver> driver = mDriver.promote(); 1943 if (driver != NULL) { 1944 driver->notifySetSurfaceComplete(); 1945 } 1946 } 1947} 1948 1949void NuPlayer::performResumeDecoders(bool needNotify) { 1950 if (needNotify) { 1951 mResumePending = true; 1952 if (mVideoDecoder == NULL) { 1953 // if audio-only, we can notify seek complete now, 1954 // as the resume operation will be relatively fast. 1955 finishResume(); 1956 } 1957 } 1958 1959 if (mVideoDecoder != NULL) { 1960 // When there is continuous seek, MediaPlayer will cache the seek 1961 // position, and send down new seek request when previous seek is 1962 // complete. Let's wait for at least one video output frame before 1963 // notifying seek complete, so that the video thumbnail gets updated 1964 // when seekbar is dragged. 1965 mVideoDecoder->signalResume(needNotify); 1966 } 1967 1968 if (mAudioDecoder != NULL) { 1969 mAudioDecoder->signalResume(false /* needNotify */); 1970 } 1971} 1972 1973void NuPlayer::finishResume() { 1974 if (mResumePending) { 1975 mResumePending = false; 1976 notifyDriverSeekComplete(); 1977 } 1978} 1979 1980void NuPlayer::notifyDriverSeekComplete() { 1981 if (mDriver != NULL) { 1982 sp<NuPlayerDriver> driver = mDriver.promote(); 1983 if (driver != NULL) { 1984 driver->notifySeekComplete(); 1985 } 1986 } 1987} 1988 1989void NuPlayer::onSourceNotify(const sp<AMessage> &msg) { 1990 int32_t what; 1991 CHECK(msg->findInt32("what", &what)); 1992 1993 switch (what) { 1994 case Source::kWhatInstantiateSecureDecoders: 1995 { 1996 if (mSource == NULL) { 1997 // This is a stale notification from a source that was 1998 // asynchronously preparing when the client called reset(). 1999 // We handled the reset, the source is gone. 2000 break; 2001 } 2002 2003 sp<AMessage> reply; 2004 CHECK(msg->findMessage("reply", &reply)); 2005 status_t err = onInstantiateSecureDecoders(); 2006 reply->setInt32("err", err); 2007 reply->post(); 2008 break; 2009 } 2010 2011 case Source::kWhatPrepared: 2012 { 2013 if (mSource == NULL) { 2014 // This is a stale notification from a source that was 2015 // asynchronously preparing when the client called reset(). 2016 // We handled the reset, the source is gone. 2017 break; 2018 } 2019 2020 int32_t err; 2021 CHECK(msg->findInt32("err", &err)); 2022 2023 if (err != OK) { 2024 // shut down potential secure codecs in case client never calls reset 2025 mDeferredActions.push_back( 2026 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */, 2027 FLUSH_CMD_SHUTDOWN /* video */)); 2028 processDeferredActions(); 2029 } 2030 2031 sp<NuPlayerDriver> driver = mDriver.promote(); 2032 if (driver != NULL) { 2033 // notify duration first, so that it's definitely set when 2034 // the app received the "prepare complete" callback. 2035 int64_t durationUs; 2036 if (mSource->getDuration(&durationUs) == OK) { 2037 driver->notifyDuration(durationUs); 2038 } 2039 driver->notifyPrepareCompleted(err); 2040 } 2041 2042 break; 2043 } 2044 2045 case Source::kWhatFlagsChanged: 2046 { 2047 uint32_t flags; 2048 CHECK(msg->findInt32("flags", (int32_t *)&flags)); 2049 2050 sp<NuPlayerDriver> driver = mDriver.promote(); 2051 if (driver != NULL) { 2052 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) { 2053 driver->notifyListener( 2054 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0); 2055 } 2056 driver->notifyFlagsChanged(flags); 2057 } 2058 2059 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2060 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) { 2061 cancelPollDuration(); 2062 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2063 && (flags & Source::FLAG_DYNAMIC_DURATION) 2064 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 2065 schedulePollDuration(); 2066 } 2067 2068 mSourceFlags = flags; 2069 break; 2070 } 2071 2072 case Source::kWhatVideoSizeChanged: 2073 { 2074 sp<AMessage> format; 2075 CHECK(msg->findMessage("format", &format)); 2076 2077 updateVideoSize(format); 2078 break; 2079 } 2080 2081 case Source::kWhatBufferingUpdate: 2082 { 2083 int32_t percentage; 2084 CHECK(msg->findInt32("percentage", &percentage)); 2085 2086 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0); 2087 break; 2088 } 2089 2090 case Source::kWhatPauseOnBufferingStart: 2091 { 2092 // ignore if not playing 2093 if (mStarted) { 2094 ALOGI("buffer low, pausing..."); 2095 2096 mPausedForBuffering = true; 2097 onPause(); 2098 } 2099 // fall-thru 2100 } 2101 2102 case Source::kWhatBufferingStart: 2103 { 2104 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0); 2105 break; 2106 } 2107 2108 case Source::kWhatResumeOnBufferingEnd: 2109 { 2110 // ignore if not playing 2111 if (mStarted) { 2112 ALOGI("buffer ready, resuming..."); 2113 2114 mPausedForBuffering = false; 2115 2116 // do not resume yet if client didn't unpause 2117 if (!mPausedByClient) { 2118 onResume(); 2119 } 2120 } 2121 // fall-thru 2122 } 2123 2124 case Source::kWhatBufferingEnd: 2125 { 2126 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0); 2127 break; 2128 } 2129 2130 case Source::kWhatCacheStats: 2131 { 2132 int32_t kbps; 2133 CHECK(msg->findInt32("bandwidth", &kbps)); 2134 2135 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps); 2136 break; 2137 } 2138 2139 case Source::kWhatSubtitleData: 2140 { 2141 sp<ABuffer> buffer; 2142 CHECK(msg->findBuffer("buffer", &buffer)); 2143 2144 sendSubtitleData(buffer, 0 /* baseIndex */); 2145 break; 2146 } 2147 2148 case Source::kWhatTimedMetaData: 2149 { 2150 sp<ABuffer> buffer; 2151 if (!msg->findBuffer("buffer", &buffer)) { 2152 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2153 } else { 2154 sendTimedMetaData(buffer); 2155 } 2156 break; 2157 } 2158 2159 case Source::kWhatTimedTextData: 2160 { 2161 int32_t generation; 2162 if (msg->findInt32("generation", &generation) 2163 && generation != mTimedTextGeneration) { 2164 break; 2165 } 2166 2167 sp<ABuffer> buffer; 2168 CHECK(msg->findBuffer("buffer", &buffer)); 2169 2170 sp<NuPlayerDriver> driver = mDriver.promote(); 2171 if (driver == NULL) { 2172 break; 2173 } 2174 2175 int posMs; 2176 int64_t timeUs, posUs; 2177 driver->getCurrentPosition(&posMs); 2178 posUs = posMs * 1000; 2179 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2180 2181 if (posUs < timeUs) { 2182 if (!msg->findInt32("generation", &generation)) { 2183 msg->setInt32("generation", mTimedTextGeneration); 2184 } 2185 msg->post(timeUs - posUs); 2186 } else { 2187 sendTimedTextData(buffer); 2188 } 2189 break; 2190 } 2191 2192 case Source::kWhatQueueDecoderShutdown: 2193 { 2194 int32_t audio, video; 2195 CHECK(msg->findInt32("audio", &audio)); 2196 CHECK(msg->findInt32("video", &video)); 2197 2198 sp<AMessage> reply; 2199 CHECK(msg->findMessage("reply", &reply)); 2200 2201 queueDecoderShutdown(audio, video, reply); 2202 break; 2203 } 2204 2205 case Source::kWhatDrmNoLicense: 2206 { 2207 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE); 2208 break; 2209 } 2210 2211 default: 2212 TRESPASS(); 2213 } 2214} 2215 2216void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) { 2217 int32_t what; 2218 CHECK(msg->findInt32("what", &what)); 2219 2220 switch (what) { 2221 case NuPlayer::CCDecoder::kWhatClosedCaptionData: 2222 { 2223 sp<ABuffer> buffer; 2224 CHECK(msg->findBuffer("buffer", &buffer)); 2225 2226 size_t inbandTracks = 0; 2227 if (mSource != NULL) { 2228 inbandTracks = mSource->getTrackCount(); 2229 } 2230 2231 sendSubtitleData(buffer, inbandTracks); 2232 break; 2233 } 2234 2235 case NuPlayer::CCDecoder::kWhatTrackAdded: 2236 { 2237 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2238 2239 break; 2240 } 2241 2242 default: 2243 TRESPASS(); 2244 } 2245 2246 2247} 2248 2249void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) { 2250 int32_t trackIndex; 2251 int64_t timeUs, durationUs; 2252 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex)); 2253 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2254 CHECK(buffer->meta()->findInt64("durationUs", &durationUs)); 2255 2256 Parcel in; 2257 in.writeInt32(trackIndex + baseIndex); 2258 in.writeInt64(timeUs); 2259 in.writeInt64(durationUs); 2260 in.writeInt32(buffer->size()); 2261 in.writeInt32(buffer->size()); 2262 in.write(buffer->data(), buffer->size()); 2263 2264 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in); 2265} 2266 2267void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) { 2268 int64_t timeUs; 2269 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2270 2271 Parcel in; 2272 in.writeInt64(timeUs); 2273 in.writeInt32(buffer->size()); 2274 in.writeInt32(buffer->size()); 2275 in.write(buffer->data(), buffer->size()); 2276 2277 notifyListener(MEDIA_META_DATA, 0, 0, &in); 2278} 2279 2280void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) { 2281 const void *data; 2282 size_t size = 0; 2283 int64_t timeUs; 2284 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS; 2285 2286 AString mime; 2287 CHECK(buffer->meta()->findString("mime", &mime)); 2288 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0); 2289 2290 data = buffer->data(); 2291 size = buffer->size(); 2292 2293 Parcel parcel; 2294 if (size > 0) { 2295 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2296 flag |= TextDescriptions::IN_BAND_TEXT_3GPP; 2297 TextDescriptions::getParcelOfDescriptions( 2298 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel); 2299 } 2300 2301 if ((parcel.dataSize() > 0)) { 2302 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel); 2303 } else { // send an empty timed text 2304 notifyListener(MEDIA_TIMED_TEXT, 0, 0); 2305 } 2306} 2307//////////////////////////////////////////////////////////////////////////////// 2308 2309sp<AMessage> NuPlayer::Source::getFormat(bool audio) { 2310 sp<MetaData> meta = getFormatMeta(audio); 2311 2312 if (meta == NULL) { 2313 return NULL; 2314 } 2315 2316 sp<AMessage> msg = new AMessage; 2317 2318 if(convertMetaDataToMessage(meta, &msg) == OK) { 2319 return msg; 2320 } 2321 return NULL; 2322} 2323 2324void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) { 2325 sp<AMessage> notify = dupNotify(); 2326 notify->setInt32("what", kWhatFlagsChanged); 2327 notify->setInt32("flags", flags); 2328 notify->post(); 2329} 2330 2331void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) { 2332 sp<AMessage> notify = dupNotify(); 2333 notify->setInt32("what", kWhatVideoSizeChanged); 2334 notify->setMessage("format", format); 2335 notify->post(); 2336} 2337 2338void NuPlayer::Source::notifyPrepared(status_t err) { 2339 sp<AMessage> notify = dupNotify(); 2340 notify->setInt32("what", kWhatPrepared); 2341 notify->setInt32("err", err); 2342 notify->post(); 2343} 2344 2345void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) { 2346 sp<AMessage> notify = dupNotify(); 2347 notify->setInt32("what", kWhatInstantiateSecureDecoders); 2348 notify->setMessage("reply", reply); 2349 notify->post(); 2350} 2351 2352void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) { 2353 TRESPASS(); 2354} 2355 2356} // namespace android 2357