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