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