NuPlayer.cpp revision c6e58419e00e6d04882a885101510e667d9c043a
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17//#define LOG_NDEBUG 0 18#define LOG_TAG "NuPlayer" 19#include <utils/Log.h> 20 21#include "NuPlayer.h" 22 23#include "HTTPLiveSource.h" 24#include "NuPlayerCCDecoder.h" 25#include "NuPlayerDecoder.h" 26#include "NuPlayerDecoderBase.h" 27#include "NuPlayerDecoderPassThrough.h" 28#include "NuPlayerDriver.h" 29#include "NuPlayerRenderer.h" 30#include "NuPlayerSource.h" 31#include "RTSPSource.h" 32#include "StreamingSource.h" 33#include "GenericSource.h" 34#include "TextDescriptions.h" 35 36#include "ATSParser.h" 37 38#include <cutils/properties.h> 39 40#include <media/AudioResamplerPublic.h> 41#include <media/AVSyncSettings.h> 42 43#include <media/stagefright/foundation/hexdump.h> 44#include <media/stagefright/foundation/ABuffer.h> 45#include <media/stagefright/foundation/ADebug.h> 46#include <media/stagefright/foundation/AMessage.h> 47#include <media/stagefright/MediaBuffer.h> 48#include <media/stagefright/MediaDefs.h> 49#include <media/stagefright/MediaErrors.h> 50#include <media/stagefright/MetaData.h> 51 52#include <gui/IGraphicBufferProducer.h> 53#include <gui/Surface.h> 54 55#include "avc_utils.h" 56 57#include "ESDS.h" 58#include <media/stagefright/Utils.h> 59 60namespace android { 61 62struct NuPlayer::Action : public RefBase { 63 Action() {} 64 65 virtual void execute(NuPlayer *player) = 0; 66 67private: 68 DISALLOW_EVIL_CONSTRUCTORS(Action); 69}; 70 71struct NuPlayer::SeekAction : public Action { 72 SeekAction(int64_t seekTimeUs) 73 : mSeekTimeUs(seekTimeUs) { 74 } 75 76 virtual void execute(NuPlayer *player) { 77 player->performSeek(mSeekTimeUs); 78 } 79 80private: 81 int64_t mSeekTimeUs; 82 83 DISALLOW_EVIL_CONSTRUCTORS(SeekAction); 84}; 85 86struct NuPlayer::ResumeDecoderAction : public Action { 87 ResumeDecoderAction(bool needNotify) 88 : mNeedNotify(needNotify) { 89 } 90 91 virtual void execute(NuPlayer *player) { 92 player->performResumeDecoders(mNeedNotify); 93 } 94 95private: 96 bool mNeedNotify; 97 98 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction); 99}; 100 101struct NuPlayer::SetSurfaceAction : public Action { 102 SetSurfaceAction(const sp<Surface> &surface) 103 : mSurface(surface) { 104 } 105 106 virtual void execute(NuPlayer *player) { 107 player->performSetSurface(mSurface); 108 } 109 110private: 111 sp<Surface> mSurface; 112 113 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction); 114}; 115 116struct NuPlayer::FlushDecoderAction : public Action { 117 FlushDecoderAction(FlushCommand audio, FlushCommand video) 118 : mAudio(audio), 119 mVideo(video) { 120 } 121 122 virtual void execute(NuPlayer *player) { 123 player->performDecoderFlush(mAudio, mVideo); 124 } 125 126private: 127 FlushCommand mAudio; 128 FlushCommand mVideo; 129 130 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction); 131}; 132 133struct NuPlayer::PostMessageAction : public Action { 134 PostMessageAction(const sp<AMessage> &msg) 135 : mMessage(msg) { 136 } 137 138 virtual void execute(NuPlayer *) { 139 mMessage->post(); 140 } 141 142private: 143 sp<AMessage> mMessage; 144 145 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction); 146}; 147 148// Use this if there's no state necessary to save in order to execute 149// the action. 150struct NuPlayer::SimpleAction : public Action { 151 typedef void (NuPlayer::*ActionFunc)(); 152 153 SimpleAction(ActionFunc func) 154 : mFunc(func) { 155 } 156 157 virtual void execute(NuPlayer *player) { 158 (player->*mFunc)(); 159 } 160 161private: 162 ActionFunc mFunc; 163 164 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction); 165}; 166 167//////////////////////////////////////////////////////////////////////////////// 168 169NuPlayer::NuPlayer() 170 : mUIDValid(false), 171 mSourceFlags(0), 172 mOffloadAudio(false), 173 mAudioDecoderGeneration(0), 174 mVideoDecoderGeneration(0), 175 mRendererGeneration(0), 176 mAudioEOS(false), 177 mVideoEOS(false), 178 mScanSourcesPending(false), 179 mScanSourcesGeneration(0), 180 mPollDurationGeneration(0), 181 mTimedTextGeneration(0), 182 mFlushingAudio(NONE), 183 mFlushingVideo(NONE), 184 mResumePending(false), 185 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW), 186 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT), 187 mVideoFpsHint(-1.f), 188 mStarted(false), 189 mSourceStarted(false), 190 mPaused(false), 191 mPausedByClient(false), 192 mPausedForBuffering(false) { 193 clearFlushComplete(); 194} 195 196NuPlayer::~NuPlayer() { 197} 198 199void NuPlayer::setUID(uid_t uid) { 200 mUIDValid = true; 201 mUID = uid; 202} 203 204void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) { 205 mDriver = driver; 206} 207 208void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) { 209 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 210 211 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 212 213 msg->setObject("source", new StreamingSource(notify, source)); 214 msg->post(); 215} 216 217static bool IsHTTPLiveURL(const char *url) { 218 if (!strncasecmp("http://", url, 7) 219 || !strncasecmp("https://", url, 8) 220 || !strncasecmp("file://", url, 7)) { 221 size_t len = strlen(url); 222 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) { 223 return true; 224 } 225 226 if (strstr(url,"m3u8")) { 227 return true; 228 } 229 } 230 231 return false; 232} 233 234void NuPlayer::setDataSourceAsync( 235 const sp<IMediaHTTPService> &httpService, 236 const char *url, 237 const KeyedVector<String8, String8> *headers) { 238 239 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 240 size_t len = strlen(url); 241 242 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 243 244 sp<Source> source; 245 if (IsHTTPLiveURL(url)) { 246 source = new HTTPLiveSource(notify, httpService, url, headers); 247 } else if (!strncasecmp(url, "rtsp://", 7)) { 248 source = new RTSPSource( 249 notify, httpService, url, headers, mUIDValid, mUID); 250 } else if ((!strncasecmp(url, "http://", 7) 251 || !strncasecmp(url, "https://", 8)) 252 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4])) 253 || strstr(url, ".sdp?"))) { 254 source = new RTSPSource( 255 notify, httpService, url, headers, mUIDValid, mUID, true); 256 } else { 257 sp<GenericSource> genericSource = 258 new GenericSource(notify, mUIDValid, mUID); 259 // Don't set FLAG_SECURE on mSourceFlags here for widevine. 260 // The correct flags will be updated in Source::kWhatFlagsChanged 261 // handler when GenericSource is prepared. 262 263 status_t err = genericSource->setDataSource(httpService, url, headers); 264 265 if (err == OK) { 266 source = genericSource; 267 } else { 268 ALOGE("Failed to set data source!"); 269 } 270 } 271 msg->setObject("source", source); 272 msg->post(); 273} 274 275void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) { 276 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 277 278 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 279 280 sp<GenericSource> source = 281 new GenericSource(notify, mUIDValid, mUID); 282 283 status_t err = source->setDataSource(fd, offset, length); 284 285 if (err != OK) { 286 ALOGE("Failed to set data source!"); 287 source = NULL; 288 } 289 290 msg->setObject("source", source); 291 msg->post(); 292} 293 294void NuPlayer::setDataSourceAsync(const sp<DataSource> &dataSource) { 295 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 296 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 297 298 sp<GenericSource> source = new GenericSource(notify, mUIDValid, mUID); 299 status_t err = source->setDataSource(dataSource); 300 301 if (err != OK) { 302 ALOGE("Failed to set data source!"); 303 source = NULL; 304 } 305 306 msg->setObject("source", source); 307 msg->post(); 308} 309 310void NuPlayer::prepareAsync() { 311 (new AMessage(kWhatPrepare, this))->post(); 312} 313 314void NuPlayer::setVideoSurfaceTextureAsync( 315 const sp<IGraphicBufferProducer> &bufferProducer) { 316 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this); 317 318 if (bufferProducer == NULL) { 319 msg->setObject("surface", NULL); 320 } else { 321 msg->setObject("surface", new Surface(bufferProducer, true /* controlledByApp */)); 322 } 323 324 msg->post(); 325} 326 327void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) { 328 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this); 329 msg->setObject("sink", sink); 330 msg->post(); 331} 332 333void NuPlayer::start() { 334 (new AMessage(kWhatStart, this))->post(); 335} 336 337status_t NuPlayer::setPlaybackSettings(const AudioPlaybackRate &rate) { 338 // do some cursory validation of the settings here. audio modes are 339 // only validated when set on the audiosink. 340 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN) 341 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX 342 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN 343 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) { 344 return BAD_VALUE; 345 } 346 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this); 347 writeToAMessage(msg, rate); 348 sp<AMessage> response; 349 status_t err = msg->postAndAwaitResponse(&response); 350 if (err == OK && response != NULL) { 351 CHECK(response->findInt32("err", &err)); 352 } 353 return err; 354} 355 356status_t NuPlayer::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) { 357 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this); 358 sp<AMessage> response; 359 status_t err = msg->postAndAwaitResponse(&response); 360 if (err == OK && response != NULL) { 361 CHECK(response->findInt32("err", &err)); 362 if (err == OK) { 363 readFromAMessage(response, rate); 364 } 365 } 366 return err; 367} 368 369status_t NuPlayer::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) { 370 sp<AMessage> msg = new AMessage(kWhatConfigSync, this); 371 writeToAMessage(msg, sync, videoFpsHint); 372 sp<AMessage> response; 373 status_t err = msg->postAndAwaitResponse(&response); 374 if (err == OK && response != NULL) { 375 CHECK(response->findInt32("err", &err)); 376 } 377 return err; 378} 379 380status_t NuPlayer::getSyncSettings( 381 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) { 382 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this); 383 sp<AMessage> response; 384 status_t err = msg->postAndAwaitResponse(&response); 385 if (err == OK && response != NULL) { 386 CHECK(response->findInt32("err", &err)); 387 if (err == OK) { 388 readFromAMessage(response, sync, videoFps); 389 } 390 } 391 return err; 392} 393 394void NuPlayer::pause() { 395 (new AMessage(kWhatPause, this))->post(); 396} 397 398void NuPlayer::resetAsync() { 399 if (mSource != NULL) { 400 // During a reset, the data source might be unresponsive already, we need to 401 // disconnect explicitly so that reads exit promptly. 402 // We can't queue the disconnect request to the looper, as it might be 403 // queued behind a stuck read and never gets processed. 404 // Doing a disconnect outside the looper to allows the pending reads to exit 405 // (either successfully or with error). 406 mSource->disconnect(); 407 } 408 409 (new AMessage(kWhatReset, this))->post(); 410} 411 412void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) { 413 sp<AMessage> msg = new AMessage(kWhatSeek, this); 414 msg->setInt64("seekTimeUs", seekTimeUs); 415 msg->setInt32("needNotify", needNotify); 416 msg->post(); 417} 418 419 420void NuPlayer::writeTrackInfo( 421 Parcel* reply, const sp<AMessage> format) const { 422 int32_t trackType; 423 CHECK(format->findInt32("type", &trackType)); 424 425 AString mime; 426 if (!format->findString("mime", &mime)) { 427 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks. 428 // If we can't find the mimetype here it means that we wouldn't be needing 429 // the mimetype on the Java end. We still write a placeholder mime to keep the 430 // (de)serialization logic simple. 431 if (trackType == MEDIA_TRACK_TYPE_AUDIO) { 432 mime = "audio/"; 433 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) { 434 mime = "video/"; 435 } else { 436 TRESPASS(); 437 } 438 } 439 440 AString lang; 441 CHECK(format->findString("language", &lang)); 442 443 reply->writeInt32(2); // write something non-zero 444 reply->writeInt32(trackType); 445 reply->writeString16(String16(mime.c_str())); 446 reply->writeString16(String16(lang.c_str())); 447 448 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { 449 int32_t isAuto, isDefault, isForced; 450 CHECK(format->findInt32("auto", &isAuto)); 451 CHECK(format->findInt32("default", &isDefault)); 452 CHECK(format->findInt32("forced", &isForced)); 453 454 reply->writeInt32(isAuto); 455 reply->writeInt32(isDefault); 456 reply->writeInt32(isForced); 457 } 458} 459 460void NuPlayer::onMessageReceived(const sp<AMessage> &msg) { 461 switch (msg->what()) { 462 case kWhatSetDataSource: 463 { 464 ALOGV("kWhatSetDataSource"); 465 466 CHECK(mSource == NULL); 467 468 status_t err = OK; 469 sp<RefBase> obj; 470 CHECK(msg->findObject("source", &obj)); 471 if (obj != NULL) { 472 mSource = static_cast<Source *>(obj.get()); 473 } else { 474 err = UNKNOWN_ERROR; 475 } 476 477 CHECK(mDriver != NULL); 478 sp<NuPlayerDriver> driver = mDriver.promote(); 479 if (driver != NULL) { 480 driver->notifySetDataSourceCompleted(err); 481 } 482 break; 483 } 484 485 case kWhatPrepare: 486 { 487 mSource->prepareAsync(); 488 break; 489 } 490 491 case kWhatGetTrackInfo: 492 { 493 sp<AReplyToken> replyID; 494 CHECK(msg->senderAwaitsResponse(&replyID)); 495 496 Parcel* reply; 497 CHECK(msg->findPointer("reply", (void**)&reply)); 498 499 size_t inbandTracks = 0; 500 if (mSource != NULL) { 501 inbandTracks = mSource->getTrackCount(); 502 } 503 504 size_t ccTracks = 0; 505 if (mCCDecoder != NULL) { 506 ccTracks = mCCDecoder->getTrackCount(); 507 } 508 509 // total track count 510 reply->writeInt32(inbandTracks + ccTracks); 511 512 // write inband tracks 513 for (size_t i = 0; i < inbandTracks; ++i) { 514 writeTrackInfo(reply, mSource->getTrackInfo(i)); 515 } 516 517 // write CC track 518 for (size_t i = 0; i < ccTracks; ++i) { 519 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i)); 520 } 521 522 sp<AMessage> response = new AMessage; 523 response->postReply(replyID); 524 break; 525 } 526 527 case kWhatGetSelectedTrack: 528 { 529 status_t err = INVALID_OPERATION; 530 if (mSource != NULL) { 531 err = OK; 532 533 int32_t type32; 534 CHECK(msg->findInt32("type", (int32_t*)&type32)); 535 media_track_type type = (media_track_type)type32; 536 ssize_t selectedTrack = mSource->getSelectedTrack(type); 537 538 Parcel* reply; 539 CHECK(msg->findPointer("reply", (void**)&reply)); 540 reply->writeInt32(selectedTrack); 541 } 542 543 sp<AMessage> response = new AMessage; 544 response->setInt32("err", err); 545 546 sp<AReplyToken> replyID; 547 CHECK(msg->senderAwaitsResponse(&replyID)); 548 response->postReply(replyID); 549 break; 550 } 551 552 case kWhatSelectTrack: 553 { 554 sp<AReplyToken> replyID; 555 CHECK(msg->senderAwaitsResponse(&replyID)); 556 557 size_t trackIndex; 558 int32_t select; 559 int64_t timeUs; 560 CHECK(msg->findSize("trackIndex", &trackIndex)); 561 CHECK(msg->findInt32("select", &select)); 562 CHECK(msg->findInt64("timeUs", &timeUs)); 563 564 status_t err = INVALID_OPERATION; 565 566 size_t inbandTracks = 0; 567 if (mSource != NULL) { 568 inbandTracks = mSource->getTrackCount(); 569 } 570 size_t ccTracks = 0; 571 if (mCCDecoder != NULL) { 572 ccTracks = mCCDecoder->getTrackCount(); 573 } 574 575 if (trackIndex < inbandTracks) { 576 err = mSource->selectTrack(trackIndex, select, timeUs); 577 578 if (!select && err == OK) { 579 int32_t type; 580 sp<AMessage> info = mSource->getTrackInfo(trackIndex); 581 if (info != NULL 582 && info->findInt32("type", &type) 583 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) { 584 ++mTimedTextGeneration; 585 } 586 } 587 } else { 588 trackIndex -= inbandTracks; 589 590 if (trackIndex < ccTracks) { 591 err = mCCDecoder->selectTrack(trackIndex, select); 592 } 593 } 594 595 sp<AMessage> response = new AMessage; 596 response->setInt32("err", err); 597 598 response->postReply(replyID); 599 break; 600 } 601 602 case kWhatPollDuration: 603 { 604 int32_t generation; 605 CHECK(msg->findInt32("generation", &generation)); 606 607 if (generation != mPollDurationGeneration) { 608 // stale 609 break; 610 } 611 612 int64_t durationUs; 613 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) { 614 sp<NuPlayerDriver> driver = mDriver.promote(); 615 if (driver != NULL) { 616 driver->notifyDuration(durationUs); 617 } 618 } 619 620 msg->post(1000000ll); // poll again in a second. 621 break; 622 } 623 624 case kWhatSetVideoSurface: 625 { 626 ALOGV("kWhatSetVideoSurface"); 627 628 sp<RefBase> obj; 629 CHECK(msg->findObject("surface", &obj)); 630 sp<Surface> surface = static_cast<Surface *>(obj.get()); 631 if (mSource == NULL || mSource->getFormat(false /* audio */) == NULL) { 632 performSetSurface(surface); 633 break; 634 } 635 636 mDeferredActions.push_back( 637 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */, 638 FLUSH_CMD_SHUTDOWN /* video */)); 639 640 mDeferredActions.push_back(new SetSurfaceAction(surface)); 641 642 if (obj != NULL || mAudioDecoder != NULL) { 643 if (mStarted) { 644 // Issue a seek to refresh the video screen only if started otherwise 645 // the extractor may not yet be started and will assert. 646 // If the video decoder is not set (perhaps audio only in this case) 647 // do not perform a seek as it is not needed. 648 int64_t currentPositionUs = 0; 649 if (getCurrentPosition(¤tPositionUs) == OK) { 650 mDeferredActions.push_back( 651 new SeekAction(currentPositionUs)); 652 } 653 } 654 655 // If there is a new surface texture, instantiate decoders 656 // again if possible. 657 mDeferredActions.push_back( 658 new SimpleAction(&NuPlayer::performScanSources)); 659 } 660 661 // After a flush without shutdown, decoder is paused. 662 // Don't resume it until source seek is done, otherwise it could 663 // start pulling stale data too soon. 664 mDeferredActions.push_back( 665 new ResumeDecoderAction(false /* needNotify */)); 666 667 processDeferredActions(); 668 break; 669 } 670 671 case kWhatSetAudioSink: 672 { 673 ALOGV("kWhatSetAudioSink"); 674 675 sp<RefBase> obj; 676 CHECK(msg->findObject("sink", &obj)); 677 678 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get()); 679 break; 680 } 681 682 case kWhatStart: 683 { 684 ALOGV("kWhatStart"); 685 if (mStarted) { 686 // do not resume yet if the source is still buffering 687 if (!mPausedForBuffering) { 688 onResume(); 689 } 690 } else { 691 onStart(); 692 } 693 mPausedByClient = false; 694 break; 695 } 696 697 case kWhatConfigPlayback: 698 { 699 sp<AReplyToken> replyID; 700 CHECK(msg->senderAwaitsResponse(&replyID)); 701 AudioPlaybackRate rate /* sanitized */; 702 readFromAMessage(msg, &rate); 703 status_t err = OK; 704 if (mRenderer != NULL) { 705 err = mRenderer->setPlaybackSettings(rate); 706 } 707 if (err == OK) { 708 if (rate.mSpeed == 0.f) { 709 onPause(); 710 // save all other settings (using non-paused speed) 711 // so we can restore them on start 712 AudioPlaybackRate newRate = rate; 713 newRate.mSpeed = mPlaybackSettings.mSpeed; 714 mPlaybackSettings = newRate; 715 } else { /* rate.mSpeed != 0.f */ 716 onResume(); 717 mPlaybackSettings = rate; 718 } 719 } 720 721 if (mVideoDecoder != NULL) { 722 float rate = getFrameRate(); 723 if (rate > 0) { 724 sp<AMessage> params = new AMessage(); 725 params->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed); 726 mVideoDecoder->setParameters(params); 727 } 728 } 729 730 sp<AMessage> response = new AMessage; 731 response->setInt32("err", err); 732 response->postReply(replyID); 733 break; 734 } 735 736 case kWhatGetPlaybackSettings: 737 { 738 sp<AReplyToken> replyID; 739 CHECK(msg->senderAwaitsResponse(&replyID)); 740 AudioPlaybackRate rate = mPlaybackSettings; 741 status_t err = OK; 742 if (mRenderer != NULL) { 743 err = mRenderer->getPlaybackSettings(&rate); 744 } 745 if (err == OK) { 746 // get playback settings used by renderer, as it may be 747 // slightly off due to audiosink not taking small changes. 748 mPlaybackSettings = rate; 749 if (mPaused) { 750 rate.mSpeed = 0.f; 751 } 752 } 753 sp<AMessage> response = new AMessage; 754 if (err == OK) { 755 writeToAMessage(response, rate); 756 } 757 response->setInt32("err", err); 758 response->postReply(replyID); 759 break; 760 } 761 762 case kWhatConfigSync: 763 { 764 sp<AReplyToken> replyID; 765 CHECK(msg->senderAwaitsResponse(&replyID)); 766 767 ALOGV("kWhatConfigSync"); 768 AVSyncSettings sync; 769 float videoFpsHint; 770 readFromAMessage(msg, &sync, &videoFpsHint); 771 status_t err = OK; 772 if (mRenderer != NULL) { 773 err = mRenderer->setSyncSettings(sync, videoFpsHint); 774 } 775 if (err == OK) { 776 mSyncSettings = sync; 777 mVideoFpsHint = videoFpsHint; 778 } 779 sp<AMessage> response = new AMessage; 780 response->setInt32("err", err); 781 response->postReply(replyID); 782 break; 783 } 784 785 case kWhatGetSyncSettings: 786 { 787 sp<AReplyToken> replyID; 788 CHECK(msg->senderAwaitsResponse(&replyID)); 789 AVSyncSettings sync = mSyncSettings; 790 float videoFps = mVideoFpsHint; 791 status_t err = OK; 792 if (mRenderer != NULL) { 793 err = mRenderer->getSyncSettings(&sync, &videoFps); 794 if (err == OK) { 795 mSyncSettings = sync; 796 mVideoFpsHint = videoFps; 797 } 798 } 799 sp<AMessage> response = new AMessage; 800 if (err == OK) { 801 writeToAMessage(response, sync, videoFps); 802 } 803 response->setInt32("err", err); 804 response->postReply(replyID); 805 break; 806 } 807 808 case kWhatScanSources: 809 { 810 int32_t generation; 811 CHECK(msg->findInt32("generation", &generation)); 812 if (generation != mScanSourcesGeneration) { 813 // Drop obsolete msg. 814 break; 815 } 816 817 mScanSourcesPending = false; 818 819 ALOGV("scanning sources haveAudio=%d, haveVideo=%d", 820 mAudioDecoder != NULL, mVideoDecoder != NULL); 821 822 bool mHadAnySourcesBefore = 823 (mAudioDecoder != NULL) || (mVideoDecoder != NULL); 824 825 // initialize video before audio because successful initialization of 826 // video may change deep buffer mode of audio. 827 if (mSurface != NULL) { 828 instantiateDecoder(false, &mVideoDecoder); 829 } 830 831 // Don't try to re-open audio sink if there's an existing decoder. 832 if (mAudioSink != NULL && mAudioDecoder == NULL) { 833 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 834 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 835 audio_stream_type_t streamType = mAudioSink->getAudioStreamType(); 836 const bool hasVideo = (videoFormat != NULL); 837 const bool canOffload = canOffloadStream( 838 audioMeta, hasVideo, mSource->isStreaming(), streamType); 839 if (canOffload) { 840 if (!mOffloadAudio) { 841 mRenderer->signalEnableOffloadAudio(); 842 } 843 // open audio sink early under offload mode. 844 sp<AMessage> format = mSource->getFormat(true /*audio*/); 845 tryOpenAudioSinkForOffload(format, hasVideo); 846 } 847 instantiateDecoder(true, &mAudioDecoder); 848 } 849 850 if (!mHadAnySourcesBefore 851 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 852 // This is the first time we've found anything playable. 853 854 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) { 855 schedulePollDuration(); 856 } 857 } 858 859 status_t err; 860 if ((err = mSource->feedMoreTSData()) != OK) { 861 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 862 // We're not currently decoding anything (no audio or 863 // video tracks found) and we just ran out of input data. 864 865 if (err == ERROR_END_OF_STREAM) { 866 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 867 } else { 868 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 869 } 870 } 871 break; 872 } 873 874 if ((mAudioDecoder == NULL && mAudioSink != NULL) 875 || (mVideoDecoder == NULL && mSurface != NULL)) { 876 msg->post(100000ll); 877 mScanSourcesPending = true; 878 } 879 break; 880 } 881 882 case kWhatVideoNotify: 883 case kWhatAudioNotify: 884 { 885 bool audio = msg->what() == kWhatAudioNotify; 886 887 int32_t currentDecoderGeneration = 888 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration); 889 int32_t requesterGeneration = currentDecoderGeneration - 1; 890 CHECK(msg->findInt32("generation", &requesterGeneration)); 891 892 if (requesterGeneration != currentDecoderGeneration) { 893 ALOGV("got message from old %s decoder, generation(%d:%d)", 894 audio ? "audio" : "video", requesterGeneration, 895 currentDecoderGeneration); 896 sp<AMessage> reply; 897 if (!(msg->findMessage("reply", &reply))) { 898 return; 899 } 900 901 reply->setInt32("err", INFO_DISCONTINUITY); 902 reply->post(); 903 return; 904 } 905 906 int32_t what; 907 CHECK(msg->findInt32("what", &what)); 908 909 if (what == DecoderBase::kWhatInputDiscontinuity) { 910 int32_t formatChange; 911 CHECK(msg->findInt32("formatChange", &formatChange)); 912 913 ALOGV("%s discontinuity: formatChange %d", 914 audio ? "audio" : "video", formatChange); 915 916 if (formatChange) { 917 mDeferredActions.push_back( 918 new FlushDecoderAction( 919 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 920 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN)); 921 } 922 923 mDeferredActions.push_back( 924 new SimpleAction( 925 &NuPlayer::performScanSources)); 926 927 processDeferredActions(); 928 } else if (what == DecoderBase::kWhatEOS) { 929 int32_t err; 930 CHECK(msg->findInt32("err", &err)); 931 932 if (err == ERROR_END_OF_STREAM) { 933 ALOGV("got %s decoder EOS", audio ? "audio" : "video"); 934 } else { 935 ALOGV("got %s decoder EOS w/ error %d", 936 audio ? "audio" : "video", 937 err); 938 } 939 940 mRenderer->queueEOS(audio, err); 941 } else if (what == DecoderBase::kWhatFlushCompleted) { 942 ALOGV("decoder %s flush completed", audio ? "audio" : "video"); 943 944 handleFlushComplete(audio, true /* isDecoder */); 945 finishFlushIfPossible(); 946 } else if (what == DecoderBase::kWhatVideoSizeChanged) { 947 sp<AMessage> format; 948 CHECK(msg->findMessage("format", &format)); 949 950 sp<AMessage> inputFormat = 951 mSource->getFormat(false /* audio */); 952 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 ALOGV("renderer %s flush completed.", audio ? "audio" : "video"); 1081 handleFlushComplete(audio, false /* isDecoder */); 1082 finishFlushIfPossible(); 1083 } else if (what == Renderer::kWhatVideoRenderingStart) { 1084 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0); 1085 } else if (what == Renderer::kWhatMediaRenderingStart) { 1086 ALOGV("media rendering started"); 1087 notifyListener(MEDIA_STARTED, 0, 0); 1088 } else if (what == Renderer::kWhatAudioTearDown) { 1089 int64_t positionUs; 1090 CHECK(msg->findInt64("positionUs", &positionUs)); 1091 int32_t reason; 1092 CHECK(msg->findInt32("reason", &reason)); 1093 ALOGV("Tear down audio with reason %d.", reason); 1094 closeAudioSink(); 1095 mAudioDecoder.clear(); 1096 ++mAudioDecoderGeneration; 1097 mRenderer->flush( 1098 true /* audio */, false /* notifyComplete */); 1099 if (mVideoDecoder != NULL) { 1100 mRenderer->flush( 1101 false /* audio */, false /* notifyComplete */); 1102 } 1103 1104 performSeek(positionUs); 1105 1106 if (reason == Renderer::kDueToError) { 1107 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1108 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 1109 audio_stream_type_t streamType = mAudioSink->getAudioStreamType(); 1110 const bool hasVideo = (videoFormat != NULL); 1111 const bool canOffload = canOffloadStream( 1112 audioMeta, hasVideo, mSource->isStreaming(), streamType); 1113 if (canOffload) { 1114 mRenderer->signalEnableOffloadAudio(); 1115 sp<AMessage> format = mSource->getFormat(true /*audio*/); 1116 tryOpenAudioSinkForOffload(format, hasVideo); 1117 } else { 1118 mRenderer->signalDisableOffloadAudio(); 1119 mOffloadAudio = false; 1120 } 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 1452status_t NuPlayer::instantiateDecoder(bool audio, sp<DecoderBase> *decoder) { 1453 if (*decoder != NULL) { 1454 return OK; 1455 } 1456 1457 sp<AMessage> format = mSource->getFormat(audio); 1458 1459 if (format == NULL) { 1460 return -EWOULDBLOCK; 1461 } 1462 1463 format->setInt32("priority", 0 /* realtime */); 1464 1465 if (!audio) { 1466 AString mime; 1467 CHECK(format->findString("mime", &mime)); 1468 1469 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this); 1470 if (mCCDecoder == NULL) { 1471 mCCDecoder = new CCDecoder(ccNotify); 1472 } 1473 1474 if (mSourceFlags & Source::FLAG_SECURE) { 1475 format->setInt32("secure", true); 1476 } 1477 1478 if (mSourceFlags & Source::FLAG_PROTECTED) { 1479 format->setInt32("protected", true); 1480 } 1481 1482 float rate = getFrameRate(); 1483 if (rate > 0) { 1484 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed); 1485 } 1486 } 1487 1488 if (audio) { 1489 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this); 1490 ++mAudioDecoderGeneration; 1491 notify->setInt32("generation", mAudioDecoderGeneration); 1492 1493 if (mOffloadAudio) { 1494 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL); 1495 format->setInt32("has-video", hasVideo); 1496 *decoder = new DecoderPassThrough(notify, mSource, mRenderer); 1497 } else { 1498 *decoder = new Decoder(notify, mSource, mRenderer); 1499 } 1500 } else { 1501 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this); 1502 ++mVideoDecoderGeneration; 1503 notify->setInt32("generation", mVideoDecoderGeneration); 1504 1505 *decoder = new Decoder( 1506 notify, mSource, mRenderer, mSurface, mCCDecoder); 1507 1508 // enable FRC if high-quality AV sync is requested, even if not 1509 // directly queuing to display, as this will even improve textureview 1510 // playback. 1511 { 1512 char value[PROPERTY_VALUE_MAX]; 1513 if (property_get("persist.sys.media.avsync", value, NULL) && 1514 (!strcmp("1", value) || !strcasecmp("true", value))) { 1515 format->setInt32("auto-frc", 1); 1516 } 1517 } 1518 } 1519 (*decoder)->init(); 1520 (*decoder)->configure(format); 1521 1522 // allocate buffers to decrypt widevine source buffers 1523 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) { 1524 Vector<sp<ABuffer> > inputBufs; 1525 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK); 1526 1527 Vector<MediaBuffer *> mediaBufs; 1528 for (size_t i = 0; i < inputBufs.size(); i++) { 1529 const sp<ABuffer> &buffer = inputBufs[i]; 1530 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size()); 1531 mediaBufs.push(mbuf); 1532 } 1533 1534 status_t err = mSource->setBuffers(audio, mediaBufs); 1535 if (err != OK) { 1536 for (size_t i = 0; i < mediaBufs.size(); ++i) { 1537 mediaBufs[i]->release(); 1538 } 1539 mediaBufs.clear(); 1540 ALOGE("Secure source didn't support secure mediaBufs."); 1541 return err; 1542 } 1543 } 1544 return OK; 1545} 1546 1547void NuPlayer::updateVideoSize( 1548 const sp<AMessage> &inputFormat, 1549 const sp<AMessage> &outputFormat) { 1550 if (inputFormat == NULL) { 1551 ALOGW("Unknown video size, reporting 0x0!"); 1552 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0); 1553 return; 1554 } 1555 1556 int32_t displayWidth, displayHeight; 1557 if (outputFormat != NULL) { 1558 int32_t width, height; 1559 CHECK(outputFormat->findInt32("width", &width)); 1560 CHECK(outputFormat->findInt32("height", &height)); 1561 1562 int32_t cropLeft, cropTop, cropRight, cropBottom; 1563 CHECK(outputFormat->findRect( 1564 "crop", 1565 &cropLeft, &cropTop, &cropRight, &cropBottom)); 1566 1567 displayWidth = cropRight - cropLeft + 1; 1568 displayHeight = cropBottom - cropTop + 1; 1569 1570 ALOGV("Video output format changed to %d x %d " 1571 "(crop: %d x %d @ (%d, %d))", 1572 width, height, 1573 displayWidth, 1574 displayHeight, 1575 cropLeft, cropTop); 1576 } else { 1577 CHECK(inputFormat->findInt32("width", &displayWidth)); 1578 CHECK(inputFormat->findInt32("height", &displayHeight)); 1579 1580 ALOGV("Video input format %d x %d", displayWidth, displayHeight); 1581 } 1582 1583 // Take into account sample aspect ratio if necessary: 1584 int32_t sarWidth, sarHeight; 1585 if (inputFormat->findInt32("sar-width", &sarWidth) 1586 && inputFormat->findInt32("sar-height", &sarHeight)) { 1587 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight); 1588 1589 displayWidth = (displayWidth * sarWidth) / sarHeight; 1590 1591 ALOGV("display dimensions %d x %d", displayWidth, displayHeight); 1592 } 1593 1594 int32_t rotationDegrees; 1595 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) { 1596 rotationDegrees = 0; 1597 } 1598 1599 if (rotationDegrees == 90 || rotationDegrees == 270) { 1600 int32_t tmp = displayWidth; 1601 displayWidth = displayHeight; 1602 displayHeight = tmp; 1603 } 1604 1605 notifyListener( 1606 MEDIA_SET_VIDEO_SIZE, 1607 displayWidth, 1608 displayHeight); 1609} 1610 1611void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) { 1612 if (mDriver == NULL) { 1613 return; 1614 } 1615 1616 sp<NuPlayerDriver> driver = mDriver.promote(); 1617 1618 if (driver == NULL) { 1619 return; 1620 } 1621 1622 driver->notifyListener(msg, ext1, ext2, in); 1623} 1624 1625void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 1626 ALOGV("[%s] flushDecoder needShutdown=%d", 1627 audio ? "audio" : "video", needShutdown); 1628 1629 const sp<DecoderBase> &decoder = getDecoder(audio); 1630 if (decoder == NULL) { 1631 ALOGI("flushDecoder %s without decoder present", 1632 audio ? "audio" : "video"); 1633 return; 1634 } 1635 1636 // Make sure we don't continue to scan sources until we finish flushing. 1637 ++mScanSourcesGeneration; 1638 if (mScanSourcesPending) { 1639 mDeferredActions.push_back( 1640 new SimpleAction(&NuPlayer::performScanSources)); 1641 mScanSourcesPending = false; 1642 } 1643 1644 decoder->signalFlush(); 1645 1646 FlushStatus newStatus = 1647 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 1648 1649 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL); 1650 mFlushComplete[audio][true /* isDecoder */] = false; 1651 if (audio) { 1652 ALOGE_IF(mFlushingAudio != NONE, 1653 "audio flushDecoder() is called in state %d", mFlushingAudio); 1654 mFlushingAudio = newStatus; 1655 } else { 1656 ALOGE_IF(mFlushingVideo != NONE, 1657 "video flushDecoder() is called in state %d", mFlushingVideo); 1658 mFlushingVideo = newStatus; 1659 } 1660} 1661 1662void NuPlayer::queueDecoderShutdown( 1663 bool audio, bool video, const sp<AMessage> &reply) { 1664 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video); 1665 1666 mDeferredActions.push_back( 1667 new FlushDecoderAction( 1668 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 1669 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE)); 1670 1671 mDeferredActions.push_back( 1672 new SimpleAction(&NuPlayer::performScanSources)); 1673 1674 mDeferredActions.push_back(new PostMessageAction(reply)); 1675 1676 processDeferredActions(); 1677} 1678 1679status_t NuPlayer::setVideoScalingMode(int32_t mode) { 1680 mVideoScalingMode = mode; 1681 if (mSurface != NULL) { 1682 status_t ret = native_window_set_scaling_mode(mSurface.get(), mVideoScalingMode); 1683 if (ret != OK) { 1684 ALOGE("Failed to set scaling mode (%d): %s", 1685 -ret, strerror(-ret)); 1686 return ret; 1687 } 1688 } 1689 return OK; 1690} 1691 1692status_t NuPlayer::getTrackInfo(Parcel* reply) const { 1693 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this); 1694 msg->setPointer("reply", reply); 1695 1696 sp<AMessage> response; 1697 status_t err = msg->postAndAwaitResponse(&response); 1698 return err; 1699} 1700 1701status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const { 1702 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this); 1703 msg->setPointer("reply", reply); 1704 msg->setInt32("type", type); 1705 1706 sp<AMessage> response; 1707 status_t err = msg->postAndAwaitResponse(&response); 1708 if (err == OK && response != NULL) { 1709 CHECK(response->findInt32("err", &err)); 1710 } 1711 return err; 1712} 1713 1714status_t NuPlayer::selectTrack(size_t trackIndex, bool select, int64_t timeUs) { 1715 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this); 1716 msg->setSize("trackIndex", trackIndex); 1717 msg->setInt32("select", select); 1718 msg->setInt64("timeUs", timeUs); 1719 1720 sp<AMessage> response; 1721 status_t err = msg->postAndAwaitResponse(&response); 1722 1723 if (err != OK) { 1724 return err; 1725 } 1726 1727 if (!response->findInt32("err", &err)) { 1728 err = OK; 1729 } 1730 1731 return err; 1732} 1733 1734status_t NuPlayer::getCurrentPosition(int64_t *mediaUs) { 1735 sp<Renderer> renderer = mRenderer; 1736 if (renderer == NULL) { 1737 return NO_INIT; 1738 } 1739 1740 return renderer->getCurrentPosition(mediaUs); 1741} 1742 1743void NuPlayer::getStats(Vector<sp<AMessage> > *mTrackStats) { 1744 CHECK(mTrackStats != NULL); 1745 1746 mTrackStats->clear(); 1747 if (mVideoDecoder != NULL) { 1748 mTrackStats->push_back(mVideoDecoder->getStats()); 1749 } 1750 if (mAudioDecoder != NULL) { 1751 mTrackStats->push_back(mAudioDecoder->getStats()); 1752 } 1753} 1754 1755sp<MetaData> NuPlayer::getFileMeta() { 1756 return mSource->getFileFormatMeta(); 1757} 1758 1759float NuPlayer::getFrameRate() { 1760 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */); 1761 if (meta == NULL) { 1762 return 0; 1763 } 1764 int32_t rate; 1765 if (!meta->findInt32(kKeyFrameRate, &rate)) { 1766 // fall back to try file meta 1767 sp<MetaData> fileMeta = getFileMeta(); 1768 if (fileMeta == NULL) { 1769 ALOGW("source has video meta but not file meta"); 1770 return -1; 1771 } 1772 int32_t fileMetaRate; 1773 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) { 1774 return -1; 1775 } 1776 return fileMetaRate; 1777 } 1778 return rate; 1779} 1780 1781void NuPlayer::schedulePollDuration() { 1782 sp<AMessage> msg = new AMessage(kWhatPollDuration, this); 1783 msg->setInt32("generation", mPollDurationGeneration); 1784 msg->post(); 1785} 1786 1787void NuPlayer::cancelPollDuration() { 1788 ++mPollDurationGeneration; 1789} 1790 1791void NuPlayer::processDeferredActions() { 1792 while (!mDeferredActions.empty()) { 1793 // We won't execute any deferred actions until we're no longer in 1794 // an intermediate state, i.e. one more more decoders are currently 1795 // flushing or shutting down. 1796 1797 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 1798 // We're currently flushing, postpone the reset until that's 1799 // completed. 1800 1801 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d", 1802 mFlushingAudio, mFlushingVideo); 1803 1804 break; 1805 } 1806 1807 sp<Action> action = *mDeferredActions.begin(); 1808 mDeferredActions.erase(mDeferredActions.begin()); 1809 1810 action->execute(this); 1811 } 1812} 1813 1814void NuPlayer::performSeek(int64_t seekTimeUs) { 1815 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)", 1816 (long long)seekTimeUs, 1817 seekTimeUs / 1E6); 1818 1819 if (mSource == NULL) { 1820 // This happens when reset occurs right before the loop mode 1821 // asynchronously seeks to the start of the stream. 1822 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL, 1823 "mSource is NULL and decoders not NULL audio(%p) video(%p)", 1824 mAudioDecoder.get(), mVideoDecoder.get()); 1825 return; 1826 } 1827 mSource->seekTo(seekTimeUs); 1828 ++mTimedTextGeneration; 1829 1830 // everything's flushed, continue playback. 1831} 1832 1833void NuPlayer::performDecoderFlush(FlushCommand audio, FlushCommand video) { 1834 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video); 1835 1836 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL) 1837 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) { 1838 return; 1839 } 1840 1841 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) { 1842 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN)); 1843 } 1844 1845 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) { 1846 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN)); 1847 } 1848} 1849 1850void NuPlayer::performReset() { 1851 ALOGV("performReset"); 1852 1853 CHECK(mAudioDecoder == NULL); 1854 CHECK(mVideoDecoder == NULL); 1855 1856 cancelPollDuration(); 1857 1858 ++mScanSourcesGeneration; 1859 mScanSourcesPending = false; 1860 1861 if (mRendererLooper != NULL) { 1862 if (mRenderer != NULL) { 1863 mRendererLooper->unregisterHandler(mRenderer->id()); 1864 } 1865 mRendererLooper->stop(); 1866 mRendererLooper.clear(); 1867 } 1868 mRenderer.clear(); 1869 ++mRendererGeneration; 1870 1871 if (mSource != NULL) { 1872 mSource->stop(); 1873 1874 mSource.clear(); 1875 } 1876 1877 if (mDriver != NULL) { 1878 sp<NuPlayerDriver> driver = mDriver.promote(); 1879 if (driver != NULL) { 1880 driver->notifyResetComplete(); 1881 } 1882 } 1883 1884 mStarted = false; 1885 mSourceStarted = false; 1886} 1887 1888void NuPlayer::performScanSources() { 1889 ALOGV("performScanSources"); 1890 1891 if (!mStarted) { 1892 return; 1893 } 1894 1895 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 1896 postScanSources(); 1897 } 1898} 1899 1900void NuPlayer::performSetSurface(const sp<Surface> &surface) { 1901 ALOGV("performSetSurface"); 1902 1903 mSurface = surface; 1904 1905 // XXX - ignore error from setVideoScalingMode for now 1906 setVideoScalingMode(mVideoScalingMode); 1907 1908 if (mDriver != NULL) { 1909 sp<NuPlayerDriver> driver = mDriver.promote(); 1910 if (driver != NULL) { 1911 driver->notifySetSurfaceComplete(); 1912 } 1913 } 1914} 1915 1916void NuPlayer::performResumeDecoders(bool needNotify) { 1917 if (needNotify) { 1918 mResumePending = true; 1919 if (mVideoDecoder == NULL) { 1920 // if audio-only, we can notify seek complete now, 1921 // as the resume operation will be relatively fast. 1922 finishResume(); 1923 } 1924 } 1925 1926 if (mVideoDecoder != NULL) { 1927 // When there is continuous seek, MediaPlayer will cache the seek 1928 // position, and send down new seek request when previous seek is 1929 // complete. Let's wait for at least one video output frame before 1930 // notifying seek complete, so that the video thumbnail gets updated 1931 // when seekbar is dragged. 1932 mVideoDecoder->signalResume(needNotify); 1933 } 1934 1935 if (mAudioDecoder != NULL) { 1936 mAudioDecoder->signalResume(false /* needNotify */); 1937 } 1938} 1939 1940void NuPlayer::finishResume() { 1941 if (mResumePending) { 1942 mResumePending = false; 1943 notifyDriverSeekComplete(); 1944 } 1945} 1946 1947void NuPlayer::notifyDriverSeekComplete() { 1948 if (mDriver != NULL) { 1949 sp<NuPlayerDriver> driver = mDriver.promote(); 1950 if (driver != NULL) { 1951 driver->notifySeekComplete(); 1952 } 1953 } 1954} 1955 1956void NuPlayer::onSourceNotify(const sp<AMessage> &msg) { 1957 int32_t what; 1958 CHECK(msg->findInt32("what", &what)); 1959 1960 switch (what) { 1961 case Source::kWhatInstantiateSecureDecoders: 1962 { 1963 if (mSource == NULL) { 1964 // This is a stale notification from a source that was 1965 // asynchronously preparing when the client called reset(). 1966 // We handled the reset, the source is gone. 1967 break; 1968 } 1969 1970 sp<AMessage> reply; 1971 CHECK(msg->findMessage("reply", &reply)); 1972 status_t err = onInstantiateSecureDecoders(); 1973 reply->setInt32("err", err); 1974 reply->post(); 1975 break; 1976 } 1977 1978 case Source::kWhatPrepared: 1979 { 1980 if (mSource == NULL) { 1981 // This is a stale notification from a source that was 1982 // asynchronously preparing when the client called reset(). 1983 // We handled the reset, the source is gone. 1984 break; 1985 } 1986 1987 int32_t err; 1988 CHECK(msg->findInt32("err", &err)); 1989 1990 if (err != OK) { 1991 // shut down potential secure codecs in case client never calls reset 1992 mDeferredActions.push_back( 1993 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */, 1994 FLUSH_CMD_SHUTDOWN /* video */)); 1995 processDeferredActions(); 1996 } 1997 1998 sp<NuPlayerDriver> driver = mDriver.promote(); 1999 if (driver != NULL) { 2000 // notify duration first, so that it's definitely set when 2001 // the app received the "prepare complete" callback. 2002 int64_t durationUs; 2003 if (mSource->getDuration(&durationUs) == OK) { 2004 driver->notifyDuration(durationUs); 2005 } 2006 driver->notifyPrepareCompleted(err); 2007 } 2008 2009 break; 2010 } 2011 2012 case Source::kWhatFlagsChanged: 2013 { 2014 uint32_t flags; 2015 CHECK(msg->findInt32("flags", (int32_t *)&flags)); 2016 2017 sp<NuPlayerDriver> driver = mDriver.promote(); 2018 if (driver != NULL) { 2019 if ((flags & NuPlayer::Source::FLAG_CAN_SEEK) == 0) { 2020 driver->notifyListener( 2021 MEDIA_INFO, MEDIA_INFO_NOT_SEEKABLE, 0); 2022 } 2023 driver->notifyFlagsChanged(flags); 2024 } 2025 2026 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2027 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) { 2028 cancelPollDuration(); 2029 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2030 && (flags & Source::FLAG_DYNAMIC_DURATION) 2031 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 2032 schedulePollDuration(); 2033 } 2034 2035 mSourceFlags = flags; 2036 break; 2037 } 2038 2039 case Source::kWhatVideoSizeChanged: 2040 { 2041 sp<AMessage> format; 2042 CHECK(msg->findMessage("format", &format)); 2043 2044 updateVideoSize(format); 2045 break; 2046 } 2047 2048 case Source::kWhatBufferingUpdate: 2049 { 2050 int32_t percentage; 2051 CHECK(msg->findInt32("percentage", &percentage)); 2052 2053 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0); 2054 break; 2055 } 2056 2057 case Source::kWhatPauseOnBufferingStart: 2058 { 2059 // ignore if not playing 2060 if (mStarted) { 2061 ALOGI("buffer low, pausing..."); 2062 2063 mPausedForBuffering = true; 2064 onPause(); 2065 } 2066 // fall-thru 2067 } 2068 2069 case Source::kWhatBufferingStart: 2070 { 2071 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0); 2072 break; 2073 } 2074 2075 case Source::kWhatResumeOnBufferingEnd: 2076 { 2077 // ignore if not playing 2078 if (mStarted) { 2079 ALOGI("buffer ready, resuming..."); 2080 2081 mPausedForBuffering = false; 2082 2083 // do not resume yet if client didn't unpause 2084 if (!mPausedByClient) { 2085 onResume(); 2086 } 2087 } 2088 // fall-thru 2089 } 2090 2091 case Source::kWhatBufferingEnd: 2092 { 2093 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0); 2094 break; 2095 } 2096 2097 case Source::kWhatCacheStats: 2098 { 2099 int32_t kbps; 2100 CHECK(msg->findInt32("bandwidth", &kbps)); 2101 2102 notifyListener(MEDIA_INFO, MEDIA_INFO_NETWORK_BANDWIDTH, kbps); 2103 break; 2104 } 2105 2106 case Source::kWhatSubtitleData: 2107 { 2108 sp<ABuffer> buffer; 2109 CHECK(msg->findBuffer("buffer", &buffer)); 2110 2111 sendSubtitleData(buffer, 0 /* baseIndex */); 2112 break; 2113 } 2114 2115 case Source::kWhatTimedMetaData: 2116 { 2117 sp<ABuffer> buffer; 2118 if (!msg->findBuffer("buffer", &buffer)) { 2119 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2120 } else { 2121 sendTimedMetaData(buffer); 2122 } 2123 break; 2124 } 2125 2126 case Source::kWhatTimedTextData: 2127 { 2128 int32_t generation; 2129 if (msg->findInt32("generation", &generation) 2130 && generation != mTimedTextGeneration) { 2131 break; 2132 } 2133 2134 sp<ABuffer> buffer; 2135 CHECK(msg->findBuffer("buffer", &buffer)); 2136 2137 sp<NuPlayerDriver> driver = mDriver.promote(); 2138 if (driver == NULL) { 2139 break; 2140 } 2141 2142 int posMs; 2143 int64_t timeUs, posUs; 2144 driver->getCurrentPosition(&posMs); 2145 posUs = posMs * 1000; 2146 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2147 2148 if (posUs < timeUs) { 2149 if (!msg->findInt32("generation", &generation)) { 2150 msg->setInt32("generation", mTimedTextGeneration); 2151 } 2152 msg->post(timeUs - posUs); 2153 } else { 2154 sendTimedTextData(buffer); 2155 } 2156 break; 2157 } 2158 2159 case Source::kWhatQueueDecoderShutdown: 2160 { 2161 int32_t audio, video; 2162 CHECK(msg->findInt32("audio", &audio)); 2163 CHECK(msg->findInt32("video", &video)); 2164 2165 sp<AMessage> reply; 2166 CHECK(msg->findMessage("reply", &reply)); 2167 2168 queueDecoderShutdown(audio, video, reply); 2169 break; 2170 } 2171 2172 case Source::kWhatDrmNoLicense: 2173 { 2174 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE); 2175 break; 2176 } 2177 2178 default: 2179 TRESPASS(); 2180 } 2181} 2182 2183void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) { 2184 int32_t what; 2185 CHECK(msg->findInt32("what", &what)); 2186 2187 switch (what) { 2188 case NuPlayer::CCDecoder::kWhatClosedCaptionData: 2189 { 2190 sp<ABuffer> buffer; 2191 CHECK(msg->findBuffer("buffer", &buffer)); 2192 2193 size_t inbandTracks = 0; 2194 if (mSource != NULL) { 2195 inbandTracks = mSource->getTrackCount(); 2196 } 2197 2198 sendSubtitleData(buffer, inbandTracks); 2199 break; 2200 } 2201 2202 case NuPlayer::CCDecoder::kWhatTrackAdded: 2203 { 2204 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2205 2206 break; 2207 } 2208 2209 default: 2210 TRESPASS(); 2211 } 2212 2213 2214} 2215 2216void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) { 2217 int32_t trackIndex; 2218 int64_t timeUs, durationUs; 2219 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex)); 2220 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2221 CHECK(buffer->meta()->findInt64("durationUs", &durationUs)); 2222 2223 Parcel in; 2224 in.writeInt32(trackIndex + baseIndex); 2225 in.writeInt64(timeUs); 2226 in.writeInt64(durationUs); 2227 in.writeInt32(buffer->size()); 2228 in.writeInt32(buffer->size()); 2229 in.write(buffer->data(), buffer->size()); 2230 2231 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in); 2232} 2233 2234void NuPlayer::sendTimedMetaData(const sp<ABuffer> &buffer) { 2235 int64_t timeUs; 2236 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2237 2238 Parcel in; 2239 in.writeInt64(timeUs); 2240 in.writeInt32(buffer->size()); 2241 in.writeInt32(buffer->size()); 2242 in.write(buffer->data(), buffer->size()); 2243 2244 notifyListener(MEDIA_META_DATA, 0, 0, &in); 2245} 2246 2247void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) { 2248 const void *data; 2249 size_t size = 0; 2250 int64_t timeUs; 2251 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS; 2252 2253 AString mime; 2254 CHECK(buffer->meta()->findString("mime", &mime)); 2255 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0); 2256 2257 data = buffer->data(); 2258 size = buffer->size(); 2259 2260 Parcel parcel; 2261 if (size > 0) { 2262 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2263 flag |= TextDescriptions::IN_BAND_TEXT_3GPP; 2264 TextDescriptions::getParcelOfDescriptions( 2265 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel); 2266 } 2267 2268 if ((parcel.dataSize() > 0)) { 2269 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel); 2270 } else { // send an empty timed text 2271 notifyListener(MEDIA_TIMED_TEXT, 0, 0); 2272 } 2273} 2274//////////////////////////////////////////////////////////////////////////////// 2275 2276sp<AMessage> NuPlayer::Source::getFormat(bool audio) { 2277 sp<MetaData> meta = getFormatMeta(audio); 2278 2279 if (meta == NULL) { 2280 return NULL; 2281 } 2282 2283 sp<AMessage> msg = new AMessage; 2284 2285 if(convertMetaDataToMessage(meta, &msg) == OK) { 2286 return msg; 2287 } 2288 return NULL; 2289} 2290 2291void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) { 2292 sp<AMessage> notify = dupNotify(); 2293 notify->setInt32("what", kWhatFlagsChanged); 2294 notify->setInt32("flags", flags); 2295 notify->post(); 2296} 2297 2298void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) { 2299 sp<AMessage> notify = dupNotify(); 2300 notify->setInt32("what", kWhatVideoSizeChanged); 2301 notify->setMessage("format", format); 2302 notify->post(); 2303} 2304 2305void NuPlayer::Source::notifyPrepared(status_t err) { 2306 sp<AMessage> notify = dupNotify(); 2307 notify->setInt32("what", kWhatPrepared); 2308 notify->setInt32("err", err); 2309 notify->post(); 2310} 2311 2312void NuPlayer::Source::notifyInstantiateSecureDecoders(const sp<AMessage> &reply) { 2313 sp<AMessage> notify = dupNotify(); 2314 notify->setInt32("what", kWhatInstantiateSecureDecoders); 2315 notify->setMessage("reply", reply); 2316 notify->post(); 2317} 2318 2319void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) { 2320 TRESPASS(); 2321} 2322 2323} // namespace android 2324