1/* 2 * Copyright 2017 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 "NuPlayer2" 19 20#include <inttypes.h> 21 22#include <utils/Log.h> 23 24#include "NuPlayer2.h" 25 26#include "HTTPLiveSource2.h" 27#include "NuPlayer2CCDecoder.h" 28#include "NuPlayer2Decoder.h" 29#include "NuPlayer2DecoderBase.h" 30#include "NuPlayer2DecoderPassThrough.h" 31#include "NuPlayer2Driver.h" 32#include "NuPlayer2Renderer.h" 33#include "NuPlayer2Source.h" 34#include "RTSPSource2.h" 35#include "GenericSource2.h" 36#include "TextDescriptions.h" 37 38#include "ATSParser.h" 39 40#include <cutils/properties.h> 41 42#include <media/AudioParameter.h> 43#include <media/AudioResamplerPublic.h> 44#include <media/AVSyncSettings.h> 45#include <media/DataSourceDesc.h> 46#include <media/MediaCodecBuffer.h> 47#include <media/NdkWrapper.h> 48 49#include <media/stagefright/foundation/hexdump.h> 50#include <media/stagefright/foundation/ABuffer.h> 51#include <media/stagefright/foundation/ADebug.h> 52#include <media/stagefright/foundation/AMessage.h> 53#include <media/stagefright/foundation/avc_utils.h> 54#include <media/stagefright/MediaBuffer.h> 55#include <media/stagefright/MediaClock.h> 56#include <media/stagefright/MediaDefs.h> 57#include <media/stagefright/MediaErrors.h> 58#include <media/stagefright/MetaData.h> 59 60#include "ESDS.h" 61#include <media/stagefright/Utils.h> 62 63#include <system/window.h> 64 65namespace android { 66 67static status_t sendMetaDataToHal(sp<MediaPlayer2Interface::AudioSink>& sink, 68 const sp<MetaData>& meta) { 69 int32_t sampleRate = 0; 70 int32_t bitRate = 0; 71 int32_t channelMask = 0; 72 int32_t delaySamples = 0; 73 int32_t paddingSamples = 0; 74 75 AudioParameter param = AudioParameter(); 76 77 if (meta->findInt32(kKeySampleRate, &sampleRate)) { 78 param.addInt(String8(AUDIO_OFFLOAD_CODEC_SAMPLE_RATE), sampleRate); 79 } 80 if (meta->findInt32(kKeyChannelMask, &channelMask)) { 81 param.addInt(String8(AUDIO_OFFLOAD_CODEC_NUM_CHANNEL), channelMask); 82 } 83 if (meta->findInt32(kKeyBitRate, &bitRate)) { 84 param.addInt(String8(AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE), bitRate); 85 } 86 if (meta->findInt32(kKeyEncoderDelay, &delaySamples)) { 87 param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), delaySamples); 88 } 89 if (meta->findInt32(kKeyEncoderPadding, &paddingSamples)) { 90 param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), paddingSamples); 91 } 92 93 ALOGV("sendMetaDataToHal: bitRate %d, sampleRate %d, chanMask %d," 94 "delaySample %d, paddingSample %d", bitRate, sampleRate, 95 channelMask, delaySamples, paddingSamples); 96 97 sink->setParameters(param.toString()); 98 return OK; 99} 100 101 102struct NuPlayer2::Action : public RefBase { 103 Action() {} 104 105 virtual void execute(NuPlayer2 *player) = 0; 106 107private: 108 DISALLOW_EVIL_CONSTRUCTORS(Action); 109}; 110 111struct NuPlayer2::SeekAction : public Action { 112 explicit SeekAction(int64_t seekTimeUs, MediaPlayer2SeekMode mode) 113 : mSeekTimeUs(seekTimeUs), 114 mMode(mode) { 115 } 116 117 virtual void execute(NuPlayer2 *player) { 118 player->performSeek(mSeekTimeUs, mMode); 119 } 120 121private: 122 int64_t mSeekTimeUs; 123 MediaPlayer2SeekMode mMode; 124 125 DISALLOW_EVIL_CONSTRUCTORS(SeekAction); 126}; 127 128struct NuPlayer2::ResumeDecoderAction : public Action { 129 explicit ResumeDecoderAction(bool needNotify) 130 : mNeedNotify(needNotify) { 131 } 132 133 virtual void execute(NuPlayer2 *player) { 134 player->performResumeDecoders(mNeedNotify); 135 } 136 137private: 138 bool mNeedNotify; 139 140 DISALLOW_EVIL_CONSTRUCTORS(ResumeDecoderAction); 141}; 142 143struct NuPlayer2::SetSurfaceAction : public Action { 144 explicit SetSurfaceAction(const sp<ANativeWindowWrapper> &nww) 145 : mNativeWindow(nww) { 146 } 147 148 virtual void execute(NuPlayer2 *player) { 149 player->performSetSurface(mNativeWindow); 150 } 151 152private: 153 sp<ANativeWindowWrapper> mNativeWindow; 154 155 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction); 156}; 157 158struct NuPlayer2::FlushDecoderAction : public Action { 159 FlushDecoderAction(FlushCommand audio, FlushCommand video) 160 : mAudio(audio), 161 mVideo(video) { 162 } 163 164 virtual void execute(NuPlayer2 *player) { 165 player->performDecoderFlush(mAudio, mVideo); 166 } 167 168private: 169 FlushCommand mAudio; 170 FlushCommand mVideo; 171 172 DISALLOW_EVIL_CONSTRUCTORS(FlushDecoderAction); 173}; 174 175struct NuPlayer2::PostMessageAction : public Action { 176 explicit PostMessageAction(const sp<AMessage> &msg) 177 : mMessage(msg) { 178 } 179 180 virtual void execute(NuPlayer2 *) { 181 mMessage->post(); 182 } 183 184private: 185 sp<AMessage> mMessage; 186 187 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction); 188}; 189 190// Use this if there's no state necessary to save in order to execute 191// the action. 192struct NuPlayer2::SimpleAction : public Action { 193 typedef void (NuPlayer2::*ActionFunc)(); 194 195 explicit SimpleAction(ActionFunc func) 196 : mFunc(func) { 197 } 198 199 virtual void execute(NuPlayer2 *player) { 200 (player->*mFunc)(); 201 } 202 203private: 204 ActionFunc mFunc; 205 206 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction); 207}; 208 209//////////////////////////////////////////////////////////////////////////////// 210 211NuPlayer2::NuPlayer2(pid_t pid, uid_t uid, const sp<MediaClock> &mediaClock) 212 : mPID(pid), 213 mUID(uid), 214 mMediaClock(mediaClock), 215 mSourceFlags(0), 216 mOffloadAudio(false), 217 mAudioDecoderGeneration(0), 218 mVideoDecoderGeneration(0), 219 mRendererGeneration(0), 220 mLastStartedPlayingTimeNs(0), 221 mPreviousSeekTimeUs(0), 222 mAudioEOS(false), 223 mVideoEOS(false), 224 mScanSourcesPending(false), 225 mScanSourcesGeneration(0), 226 mPollDurationGeneration(0), 227 mTimedTextGeneration(0), 228 mFlushingAudio(NONE), 229 mFlushingVideo(NONE), 230 mResumePending(false), 231 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW), 232 mPlaybackSettings(AUDIO_PLAYBACK_RATE_DEFAULT), 233 mVideoFpsHint(-1.f), 234 mStarted(false), 235 mPrepared(false), 236 mResetting(false), 237 mSourceStarted(false), 238 mAudioDecoderError(false), 239 mVideoDecoderError(false), 240 mPaused(false), 241 mPausedByClient(true), 242 mPausedForBuffering(false), 243 mIsDrmProtected(false), 244 mDataSourceType(DATA_SOURCE_TYPE_NONE) { 245 CHECK(mediaClock != NULL); 246 clearFlushComplete(); 247} 248 249NuPlayer2::~NuPlayer2() { 250} 251 252void NuPlayer2::setDriver(const wp<NuPlayer2Driver> &driver) { 253 mDriver = driver; 254} 255 256static bool IsHTTPLiveURL(const char *url) { 257 if (!strncasecmp("http://", url, 7) 258 || !strncasecmp("https://", url, 8) 259 || !strncasecmp("file://", url, 7)) { 260 size_t len = strlen(url); 261 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) { 262 return true; 263 } 264 265 if (strstr(url,"m3u8")) { 266 return true; 267 } 268 } 269 270 return false; 271} 272 273status_t NuPlayer2::createNuPlayer2Source(const sp<DataSourceDesc> &dsd, 274 sp<Source> *source, 275 DATA_SOURCE_TYPE *dataSourceType) { 276 status_t err = NO_ERROR; 277 sp<AMessage> notify = new AMessage(kWhatSourceNotify, this); 278 notify->setInt64("srcId", dsd->mId); 279 280 switch (dsd->mType) { 281 case DataSourceDesc::TYPE_URL: 282 { 283 const char *url = dsd->mUrl.c_str(); 284 size_t len = strlen(url); 285 286 const sp<MediaHTTPService> &httpService = dsd->mHttpService; 287 KeyedVector<String8, String8> *headers = &(dsd->mHeaders); 288 289 if (IsHTTPLiveURL(url)) { 290 *source = new HTTPLiveSource2(notify, httpService, url, headers); 291 ALOGV("createNuPlayer2Source HTTPLiveSource2 %s", url); 292 *dataSourceType = DATA_SOURCE_TYPE_HTTP_LIVE; 293 } else if (!strncasecmp(url, "rtsp://", 7)) { 294 *source = new RTSPSource2( 295 notify, httpService, url, headers, mUID); 296 ALOGV("createNuPlayer2Source RTSPSource2 %s", url); 297 *dataSourceType = DATA_SOURCE_TYPE_RTSP; 298 } else if ((!strncasecmp(url, "http://", 7) 299 || !strncasecmp(url, "https://", 8)) 300 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4])) 301 || strstr(url, ".sdp?"))) { 302 *source = new RTSPSource2( 303 notify, httpService, url, headers, mUID, true); 304 ALOGV("createNuPlayer2Source RTSPSource2 http/https/.sdp %s", url); 305 *dataSourceType = DATA_SOURCE_TYPE_RTSP; 306 } else { 307 ALOGV("createNuPlayer2Source GenericSource2 %s", url); 308 309 sp<GenericSource2> genericSource = 310 new GenericSource2(notify, mUID, mMediaClock); 311 312 err = genericSource->setDataSource(httpService, url, headers); 313 314 if (err == OK) { 315 *source = genericSource; 316 } else { 317 *source = NULL; 318 ALOGE("Failed to create NuPlayer2Source!"); 319 } 320 321 // regardless of success/failure 322 *dataSourceType = DATA_SOURCE_TYPE_GENERIC_URL; 323 } 324 break; 325 } 326 327 case DataSourceDesc::TYPE_FD: 328 { 329 sp<GenericSource2> genericSource = 330 new GenericSource2(notify, mUID, mMediaClock); 331 332 ALOGV("createNuPlayer2Source fd %d/%lld/%lld source: %p", 333 dsd->mFD, (long long)dsd->mFDOffset, (long long)dsd->mFDLength, 334 genericSource.get()); 335 336 err = genericSource->setDataSource(dsd->mFD, dsd->mFDOffset, dsd->mFDLength); 337 338 if (err != OK) { 339 ALOGE("Failed to create NuPlayer2Source!"); 340 *source = NULL; 341 } else { 342 *source = genericSource; 343 } 344 345 *dataSourceType = DATA_SOURCE_TYPE_GENERIC_FD; 346 break; 347 } 348 349 case DataSourceDesc::TYPE_CALLBACK: 350 { 351 sp<GenericSource2> genericSource = 352 new GenericSource2(notify, mUID, mMediaClock); 353 err = genericSource->setDataSource(dsd->mCallbackSource); 354 355 if (err != OK) { 356 ALOGE("Failed to create NuPlayer2Source!"); 357 *source = NULL; 358 } else { 359 *source = genericSource; 360 } 361 362 *dataSourceType = DATA_SOURCE_TYPE_MEDIA; 363 break; 364 } 365 366 default: 367 err = BAD_TYPE; 368 *source = NULL; 369 *dataSourceType = DATA_SOURCE_TYPE_NONE; 370 ALOGE("invalid data source type!"); 371 break; 372 } 373 374 return err; 375} 376 377void NuPlayer2::setDataSourceAsync(const sp<DataSourceDesc> &dsd) { 378 DATA_SOURCE_TYPE dataSourceType; 379 sp<Source> source; 380 createNuPlayer2Source(dsd, &source, &dataSourceType); 381 382 // TODO: currently NuPlayer2Driver makes blocking call to setDataSourceAsync 383 // and expects notifySetDataSourceCompleted regardless of success or failure. 384 // This will be changed since setDataSource should be asynchronous at JAVA level. 385 // When it succeeds, app will get onInfo notification. Otherwise, onError 386 // will be called. 387 /* 388 if (err != OK) { 389 notifyListener(dsd->mId, MEDIA2_ERROR, MEDIA2_ERROR_FAILED_TO_SET_DATA_SOURCE, err); 390 return; 391 } 392 393 // Now, source != NULL. 394 */ 395 396 mDataSourceType = dataSourceType; 397 398 sp<AMessage> msg = new AMessage(kWhatSetDataSource, this); 399 msg->setObject("source", source); 400 msg->setInt64("srcId", dsd->mId); 401 msg->post(); 402} 403 404void NuPlayer2::prepareNextDataSourceAsync(const sp<DataSourceDesc> &dsd) { 405 DATA_SOURCE_TYPE dataSourceType; 406 sp<Source> source; 407 createNuPlayer2Source(dsd, &source, &dataSourceType); 408 409 /* 410 if (err != OK) { 411 notifyListener(dsd->mId, MEDIA2_ERROR, MEDIA2_ERROR_FAILED_TO_SET_DATA_SOURCE, err); 412 return; 413 } 414 415 // Now, source != NULL. 416 */ 417 418 mNextDataSourceType = dataSourceType; 419 420 sp<AMessage> msg = new AMessage(kWhatPrepareNextDataSource, this); 421 msg->setObject("source", source); 422 msg->setInt64("srcId", dsd->mId); 423 msg->post(); 424} 425 426void NuPlayer2::playNextDataSource(int64_t srcId) { 427 disconnectSource(); 428 429 sp<AMessage> msg = new AMessage(kWhatPlayNextDataSource, this); 430 msg->setInt64("srcId", srcId); 431 msg->post(); 432} 433 434status_t NuPlayer2::getBufferingSettings( 435 BufferingSettings *buffering /* nonnull */) { 436 sp<AMessage> msg = new AMessage(kWhatGetBufferingSettings, this); 437 sp<AMessage> response; 438 status_t err = msg->postAndAwaitResponse(&response); 439 if (err == OK && response != NULL) { 440 CHECK(response->findInt32("err", &err)); 441 if (err == OK) { 442 readFromAMessage(response, buffering); 443 } 444 } 445 return err; 446} 447 448status_t NuPlayer2::setBufferingSettings(const BufferingSettings& buffering) { 449 sp<AMessage> msg = new AMessage(kWhatSetBufferingSettings, this); 450 writeToAMessage(msg, buffering); 451 sp<AMessage> response; 452 status_t err = msg->postAndAwaitResponse(&response); 453 if (err == OK && response != NULL) { 454 CHECK(response->findInt32("err", &err)); 455 } 456 return err; 457} 458 459void NuPlayer2::prepareAsync() { 460 ALOGV("prepareAsync"); 461 462 (new AMessage(kWhatPrepare, this))->post(); 463} 464 465void NuPlayer2::setVideoSurfaceTextureAsync(const sp<ANativeWindowWrapper> &nww) { 466 sp<AMessage> msg = new AMessage(kWhatSetVideoSurface, this); 467 468 if (nww == NULL || nww->getANativeWindow() == NULL) { 469 msg->setObject("surface", NULL); 470 } else { 471 msg->setObject("surface", nww); 472 } 473 474 msg->post(); 475} 476 477void NuPlayer2::setAudioSink(const sp<MediaPlayer2Interface::AudioSink> &sink) { 478 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, this); 479 msg->setObject("sink", sink); 480 msg->post(); 481} 482 483void NuPlayer2::start() { 484 (new AMessage(kWhatStart, this))->post(); 485} 486 487status_t NuPlayer2::setPlaybackSettings(const AudioPlaybackRate &rate) { 488 // do some cursory validation of the settings here. audio modes are 489 // only validated when set on the audiosink. 490 if ((rate.mSpeed != 0.f && rate.mSpeed < AUDIO_TIMESTRETCH_SPEED_MIN) 491 || rate.mSpeed > AUDIO_TIMESTRETCH_SPEED_MAX 492 || rate.mPitch < AUDIO_TIMESTRETCH_SPEED_MIN 493 || rate.mPitch > AUDIO_TIMESTRETCH_SPEED_MAX) { 494 return BAD_VALUE; 495 } 496 sp<AMessage> msg = new AMessage(kWhatConfigPlayback, this); 497 writeToAMessage(msg, rate); 498 sp<AMessage> response; 499 status_t err = msg->postAndAwaitResponse(&response); 500 if (err == OK && response != NULL) { 501 CHECK(response->findInt32("err", &err)); 502 } 503 return err; 504} 505 506status_t NuPlayer2::getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */) { 507 sp<AMessage> msg = new AMessage(kWhatGetPlaybackSettings, this); 508 sp<AMessage> response; 509 status_t err = msg->postAndAwaitResponse(&response); 510 if (err == OK && response != NULL) { 511 CHECK(response->findInt32("err", &err)); 512 if (err == OK) { 513 readFromAMessage(response, rate); 514 } 515 } 516 return err; 517} 518 519status_t NuPlayer2::setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) { 520 sp<AMessage> msg = new AMessage(kWhatConfigSync, this); 521 writeToAMessage(msg, sync, videoFpsHint); 522 sp<AMessage> response; 523 status_t err = msg->postAndAwaitResponse(&response); 524 if (err == OK && response != NULL) { 525 CHECK(response->findInt32("err", &err)); 526 } 527 return err; 528} 529 530status_t NuPlayer2::getSyncSettings( 531 AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */) { 532 sp<AMessage> msg = new AMessage(kWhatGetSyncSettings, this); 533 sp<AMessage> response; 534 status_t err = msg->postAndAwaitResponse(&response); 535 if (err == OK && response != NULL) { 536 CHECK(response->findInt32("err", &err)); 537 if (err == OK) { 538 readFromAMessage(response, sync, videoFps); 539 } 540 } 541 return err; 542} 543 544void NuPlayer2::pause() { 545 (new AMessage(kWhatPause, this))->post(); 546} 547 548void NuPlayer2::resetAsync() { 549 disconnectSource(); 550 (new AMessage(kWhatReset, this))->post(); 551} 552 553void NuPlayer2::disconnectSource() { 554 sp<Source> source; 555 { 556 Mutex::Autolock autoLock(mSourceLock); 557 source = mSource; 558 } 559 560 if (source != NULL) { 561 // During a reset, the data source might be unresponsive already, we need to 562 // disconnect explicitly so that reads exit promptly. 563 // We can't queue the disconnect request to the looper, as it might be 564 // queued behind a stuck read and never gets processed. 565 // Doing a disconnect outside the looper to allows the pending reads to exit 566 // (either successfully or with error). 567 source->disconnect(); 568 } 569 570} 571 572status_t NuPlayer2::notifyAt(int64_t mediaTimeUs) { 573 sp<AMessage> notify = new AMessage(kWhatNotifyTime, this); 574 notify->setInt64("timerUs", mediaTimeUs); 575 mMediaClock->addTimer(notify, mediaTimeUs); 576 return OK; 577} 578 579void NuPlayer2::seekToAsync(int64_t seekTimeUs, MediaPlayer2SeekMode mode, bool needNotify) { 580 sp<AMessage> msg = new AMessage(kWhatSeek, this); 581 msg->setInt64("seekTimeUs", seekTimeUs); 582 msg->setInt32("mode", mode); 583 msg->setInt32("needNotify", needNotify); 584 msg->post(); 585} 586 587 588void NuPlayer2::writeTrackInfo( 589 Parcel* reply, const sp<AMessage>& format) const { 590 if (format == NULL) { 591 ALOGE("NULL format"); 592 return; 593 } 594 int32_t trackType; 595 if (!format->findInt32("type", &trackType)) { 596 ALOGE("no track type"); 597 return; 598 } 599 600 AString mime; 601 if (!format->findString("mime", &mime)) { 602 // Java MediaPlayer only uses mimetype for subtitle and timedtext tracks. 603 // If we can't find the mimetype here it means that we wouldn't be needing 604 // the mimetype on the Java end. We still write a placeholder mime to keep the 605 // (de)serialization logic simple. 606 if (trackType == MEDIA_TRACK_TYPE_AUDIO) { 607 mime = "audio/"; 608 } else if (trackType == MEDIA_TRACK_TYPE_VIDEO) { 609 mime = "video/"; 610 } else { 611 ALOGE("unknown track type: %d", trackType); 612 return; 613 } 614 } 615 616 AString lang; 617 if (!format->findString("language", &lang)) { 618 ALOGE("no language"); 619 return; 620 } 621 622 reply->writeInt32(2); // write something non-zero 623 reply->writeInt32(trackType); 624 reply->writeString16(String16(mime.c_str())); 625 reply->writeString16(String16(lang.c_str())); 626 627 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { 628 int32_t isAuto, isDefault, isForced; 629 CHECK(format->findInt32("auto", &isAuto)); 630 CHECK(format->findInt32("default", &isDefault)); 631 CHECK(format->findInt32("forced", &isForced)); 632 633 reply->writeInt32(isAuto); 634 reply->writeInt32(isDefault); 635 reply->writeInt32(isForced); 636 } 637} 638 639void NuPlayer2::onMessageReceived(const sp<AMessage> &msg) { 640 switch (msg->what()) { 641 case kWhatSetDataSource: 642 { 643 ALOGV("kWhatSetDataSource"); 644 645 CHECK(mSource == NULL); 646 647 status_t err = OK; 648 sp<RefBase> obj; 649 CHECK(msg->findObject("source", &obj)); 650 if (obj != NULL) { 651 Mutex::Autolock autoLock(mSourceLock); 652 CHECK(msg->findInt64("srcId", &mSrcId)); 653 mSource = static_cast<Source *>(obj.get()); 654 } else { 655 err = UNKNOWN_ERROR; 656 ALOGE("kWhatSetDataSource, source should not be NULL"); 657 } 658 659 CHECK(mDriver != NULL); 660 sp<NuPlayer2Driver> driver = mDriver.promote(); 661 if (driver != NULL) { 662 driver->notifySetDataSourceCompleted(mSrcId, err); 663 } 664 break; 665 } 666 667 case kWhatPrepareNextDataSource: 668 { 669 ALOGV("kWhatPrepareNextDataSource"); 670 671 status_t err = OK; 672 sp<RefBase> obj; 673 CHECK(msg->findObject("source", &obj)); 674 if (obj != NULL) { 675 Mutex::Autolock autoLock(mSourceLock); 676 CHECK(msg->findInt64("srcId", &mNextSrcId)); 677 mNextSource = static_cast<Source *>(obj.get()); 678 mNextSource->prepareAsync(); 679 } else { 680 err = UNKNOWN_ERROR; 681 } 682 683 break; 684 } 685 686 case kWhatPlayNextDataSource: 687 { 688 ALOGV("kWhatPlayNextDataSource"); 689 int64_t srcId; 690 CHECK(msg->findInt64("srcId", &srcId)); 691 if (srcId != mNextSrcId) { 692 notifyListener(srcId, MEDIA2_ERROR, MEDIA2_ERROR_UNKNOWN, 0); 693 return; 694 } 695 696 mResetting = true; 697 stopPlaybackTimer("kWhatPlayNextDataSource"); 698 stopRebufferingTimer(true); 699 700 mDeferredActions.push_back( 701 new FlushDecoderAction( 702 FLUSH_CMD_SHUTDOWN /* audio */, 703 FLUSH_CMD_SHUTDOWN /* video */)); 704 705 mDeferredActions.push_back( 706 new SimpleAction(&NuPlayer2::performPlayNextDataSource)); 707 708 processDeferredActions(); 709 break; 710 } 711 712 case kWhatGetBufferingSettings: 713 { 714 sp<AReplyToken> replyID; 715 CHECK(msg->senderAwaitsResponse(&replyID)); 716 717 ALOGV("kWhatGetBufferingSettings"); 718 BufferingSettings buffering; 719 status_t err = OK; 720 if (mSource != NULL) { 721 err = mSource->getBufferingSettings(&buffering); 722 } else { 723 err = INVALID_OPERATION; 724 } 725 sp<AMessage> response = new AMessage; 726 if (err == OK) { 727 writeToAMessage(response, buffering); 728 } 729 response->setInt32("err", err); 730 response->postReply(replyID); 731 break; 732 } 733 734 case kWhatSetBufferingSettings: 735 { 736 sp<AReplyToken> replyID; 737 CHECK(msg->senderAwaitsResponse(&replyID)); 738 739 ALOGV("kWhatSetBufferingSettings"); 740 BufferingSettings buffering; 741 readFromAMessage(msg, &buffering); 742 status_t err = OK; 743 if (mSource != NULL) { 744 err = mSource->setBufferingSettings(buffering); 745 } else { 746 err = INVALID_OPERATION; 747 } 748 sp<AMessage> response = new AMessage; 749 response->setInt32("err", err); 750 response->postReply(replyID); 751 break; 752 } 753 754 case kWhatPrepare: 755 { 756 ALOGV("onMessageReceived kWhatPrepare"); 757 758 mSource->prepareAsync(); 759 break; 760 } 761 762 case kWhatGetTrackInfo: 763 { 764 sp<AReplyToken> replyID; 765 CHECK(msg->senderAwaitsResponse(&replyID)); 766 767 Parcel* reply; 768 CHECK(msg->findPointer("reply", (void**)&reply)); 769 770 size_t inbandTracks = 0; 771 if (mSource != NULL) { 772 inbandTracks = mSource->getTrackCount(); 773 } 774 775 size_t ccTracks = 0; 776 if (mCCDecoder != NULL) { 777 ccTracks = mCCDecoder->getTrackCount(); 778 } 779 780 // total track count 781 reply->writeInt32(inbandTracks + ccTracks); 782 783 // write inband tracks 784 for (size_t i = 0; i < inbandTracks; ++i) { 785 writeTrackInfo(reply, mSource->getTrackInfo(i)); 786 } 787 788 // write CC track 789 for (size_t i = 0; i < ccTracks; ++i) { 790 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i)); 791 } 792 793 sp<AMessage> response = new AMessage; 794 response->postReply(replyID); 795 break; 796 } 797 798 case kWhatGetSelectedTrack: 799 { 800 status_t err = INVALID_OPERATION; 801 if (mSource != NULL) { 802 err = OK; 803 804 int32_t type32; 805 CHECK(msg->findInt32("type", (int32_t*)&type32)); 806 media_track_type type = (media_track_type)type32; 807 ssize_t selectedTrack = mSource->getSelectedTrack(type); 808 809 Parcel* reply; 810 CHECK(msg->findPointer("reply", (void**)&reply)); 811 reply->writeInt32(selectedTrack); 812 } 813 814 sp<AMessage> response = new AMessage; 815 response->setInt32("err", err); 816 817 sp<AReplyToken> replyID; 818 CHECK(msg->senderAwaitsResponse(&replyID)); 819 response->postReply(replyID); 820 break; 821 } 822 823 case kWhatSelectTrack: 824 { 825 sp<AReplyToken> replyID; 826 CHECK(msg->senderAwaitsResponse(&replyID)); 827 828 size_t trackIndex; 829 int32_t select; 830 int64_t timeUs; 831 CHECK(msg->findSize("trackIndex", &trackIndex)); 832 CHECK(msg->findInt32("select", &select)); 833 CHECK(msg->findInt64("timeUs", &timeUs)); 834 835 status_t err = INVALID_OPERATION; 836 837 size_t inbandTracks = 0; 838 if (mSource != NULL) { 839 inbandTracks = mSource->getTrackCount(); 840 } 841 size_t ccTracks = 0; 842 if (mCCDecoder != NULL) { 843 ccTracks = mCCDecoder->getTrackCount(); 844 } 845 846 if (trackIndex < inbandTracks) { 847 err = mSource->selectTrack(trackIndex, select, timeUs); 848 849 if (!select && err == OK) { 850 int32_t type; 851 sp<AMessage> info = mSource->getTrackInfo(trackIndex); 852 if (info != NULL 853 && info->findInt32("type", &type) 854 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) { 855 ++mTimedTextGeneration; 856 } 857 } 858 } else { 859 trackIndex -= inbandTracks; 860 861 if (trackIndex < ccTracks) { 862 err = mCCDecoder->selectTrack(trackIndex, select); 863 } 864 } 865 866 sp<AMessage> response = new AMessage; 867 response->setInt32("err", err); 868 869 response->postReply(replyID); 870 break; 871 } 872 873 case kWhatPollDuration: 874 { 875 int32_t generation; 876 CHECK(msg->findInt32("generation", &generation)); 877 878 if (generation != mPollDurationGeneration) { 879 // stale 880 break; 881 } 882 883 int64_t durationUs; 884 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) { 885 sp<NuPlayer2Driver> driver = mDriver.promote(); 886 if (driver != NULL) { 887 driver->notifyDuration(mSrcId, durationUs); 888 } 889 } 890 891 msg->post(1000000ll); // poll again in a second. 892 break; 893 } 894 895 case kWhatSetVideoSurface: 896 { 897 898 sp<RefBase> obj; 899 CHECK(msg->findObject("surface", &obj)); 900 sp<ANativeWindowWrapper> nww = static_cast<ANativeWindowWrapper *>(obj.get()); 901 902 ALOGD("onSetVideoSurface(%p, %s video decoder)", 903 (nww == NULL ? NULL : nww->getANativeWindow()), 904 (mSource != NULL && mStarted && mSource->getFormat(false /* audio */) != NULL 905 && mVideoDecoder != NULL) ? "have" : "no"); 906 907 // Need to check mStarted before calling mSource->getFormat because NuPlayer2 might 908 // be in preparing state and it could take long time. 909 // When mStarted is true, mSource must have been set. 910 if (mSource == NULL || !mStarted || mSource->getFormat(false /* audio */) == NULL 911 // NOTE: mVideoDecoder's mNativeWindow is always non-null 912 || (mVideoDecoder != NULL && mVideoDecoder->setVideoSurface(nww) == OK)) { 913 performSetSurface(nww); 914 break; 915 } 916 917 mDeferredActions.push_back( 918 new FlushDecoderAction( 919 (obj != NULL ? FLUSH_CMD_FLUSH : FLUSH_CMD_NONE) /* audio */, 920 FLUSH_CMD_SHUTDOWN /* video */)); 921 922 mDeferredActions.push_back(new SetSurfaceAction(nww)); 923 924 if (obj != NULL) { 925 if (mStarted) { 926 // Issue a seek to refresh the video screen only if started otherwise 927 // the extractor may not yet be started and will assert. 928 // If the video decoder is not set (perhaps audio only in this case) 929 // do not perform a seek as it is not needed. 930 int64_t currentPositionUs = 0; 931 if (getCurrentPosition(¤tPositionUs) == OK) { 932 mDeferredActions.push_back( 933 new SeekAction(currentPositionUs, 934 MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC /* mode */)); 935 } 936 } 937 938 // If there is a new surface texture, instantiate decoders 939 // again if possible. 940 mDeferredActions.push_back( 941 new SimpleAction(&NuPlayer2::performScanSources)); 942 943 // After a flush without shutdown, decoder is paused. 944 // Don't resume it until source seek is done, otherwise it could 945 // start pulling stale data too soon. 946 mDeferredActions.push_back( 947 new ResumeDecoderAction(false /* needNotify */)); 948 } 949 950 processDeferredActions(); 951 break; 952 } 953 954 case kWhatSetAudioSink: 955 { 956 ALOGV("kWhatSetAudioSink"); 957 958 sp<RefBase> obj; 959 CHECK(msg->findObject("sink", &obj)); 960 961 mAudioSink = static_cast<MediaPlayer2Interface::AudioSink *>(obj.get()); 962 break; 963 } 964 965 case kWhatStart: 966 { 967 ALOGV("kWhatStart"); 968 if (mStarted) { 969 // do not resume yet if the source is still buffering 970 if (!mPausedForBuffering) { 971 onResume(); 972 } 973 } else { 974 onStart(); 975 } 976 mPausedByClient = false; 977 notifyListener(mSrcId, MEDIA2_STARTED, 0, 0); 978 break; 979 } 980 981 case kWhatConfigPlayback: 982 { 983 sp<AReplyToken> replyID; 984 CHECK(msg->senderAwaitsResponse(&replyID)); 985 AudioPlaybackRate rate /* sanitized */; 986 readFromAMessage(msg, &rate); 987 status_t err = OK; 988 if (mRenderer != NULL) { 989 // AudioSink allows only 1.f and 0.f for offload mode. 990 // For other speed, switch to non-offload mode. 991 if (mOffloadAudio && ((rate.mSpeed != 0.f && rate.mSpeed != 1.f) 992 || rate.mPitch != 1.f)) { 993 int64_t currentPositionUs; 994 if (getCurrentPosition(¤tPositionUs) != OK) { 995 currentPositionUs = mPreviousSeekTimeUs; 996 } 997 998 // Set mPlaybackSettings so that the new audio decoder can 999 // be created correctly. 1000 mPlaybackSettings = rate; 1001 if (!mPaused) { 1002 mRenderer->pause(); 1003 } 1004 restartAudio( 1005 currentPositionUs, true /* forceNonOffload */, 1006 true /* needsToCreateAudioDecoder */); 1007 if (!mPaused) { 1008 mRenderer->resume(); 1009 } 1010 } 1011 1012 err = mRenderer->setPlaybackSettings(rate); 1013 } 1014 if (err == OK) { 1015 if (rate.mSpeed == 0.f) { 1016 onPause(); 1017 notifyListener(mSrcId, MEDIA2_PAUSED, 0, 0); 1018 mPausedByClient = true; 1019 // save all other settings (using non-paused speed) 1020 // so we can restore them on start 1021 AudioPlaybackRate newRate = rate; 1022 newRate.mSpeed = mPlaybackSettings.mSpeed; 1023 mPlaybackSettings = newRate; 1024 } else { /* rate.mSpeed != 0.f */ 1025 mPlaybackSettings = rate; 1026 if (mStarted) { 1027 // do not resume yet if the source is still buffering 1028 if (!mPausedForBuffering) { 1029 onResume(); 1030 } 1031 } else if (mPrepared) { 1032 onStart(); 1033 } 1034 1035 mPausedByClient = false; 1036 } 1037 } 1038 1039 if (mVideoDecoder != NULL) { 1040 sp<AMessage> params = new AMessage(); 1041 params->setFloat("playback-speed", mPlaybackSettings.mSpeed); 1042 mVideoDecoder->setParameters(params); 1043 } 1044 1045 sp<AMessage> response = new AMessage; 1046 response->setInt32("err", err); 1047 response->postReply(replyID); 1048 break; 1049 } 1050 1051 case kWhatGetPlaybackSettings: 1052 { 1053 sp<AReplyToken> replyID; 1054 CHECK(msg->senderAwaitsResponse(&replyID)); 1055 AudioPlaybackRate rate = mPlaybackSettings; 1056 status_t err = OK; 1057 if (mRenderer != NULL) { 1058 err = mRenderer->getPlaybackSettings(&rate); 1059 } 1060 if (err == OK) { 1061 // get playback settings used by renderer, as it may be 1062 // slightly off due to audiosink not taking small changes. 1063 mPlaybackSettings = rate; 1064 if (mPaused) { 1065 rate.mSpeed = 0.f; 1066 } 1067 } 1068 sp<AMessage> response = new AMessage; 1069 if (err == OK) { 1070 writeToAMessage(response, rate); 1071 } 1072 response->setInt32("err", err); 1073 response->postReply(replyID); 1074 break; 1075 } 1076 1077 case kWhatConfigSync: 1078 { 1079 sp<AReplyToken> replyID; 1080 CHECK(msg->senderAwaitsResponse(&replyID)); 1081 1082 ALOGV("kWhatConfigSync"); 1083 AVSyncSettings sync; 1084 float videoFpsHint; 1085 readFromAMessage(msg, &sync, &videoFpsHint); 1086 status_t err = OK; 1087 if (mRenderer != NULL) { 1088 err = mRenderer->setSyncSettings(sync, videoFpsHint); 1089 } 1090 if (err == OK) { 1091 mSyncSettings = sync; 1092 mVideoFpsHint = videoFpsHint; 1093 } 1094 sp<AMessage> response = new AMessage; 1095 response->setInt32("err", err); 1096 response->postReply(replyID); 1097 break; 1098 } 1099 1100 case kWhatGetSyncSettings: 1101 { 1102 sp<AReplyToken> replyID; 1103 CHECK(msg->senderAwaitsResponse(&replyID)); 1104 AVSyncSettings sync = mSyncSettings; 1105 float videoFps = mVideoFpsHint; 1106 status_t err = OK; 1107 if (mRenderer != NULL) { 1108 err = mRenderer->getSyncSettings(&sync, &videoFps); 1109 if (err == OK) { 1110 mSyncSettings = sync; 1111 mVideoFpsHint = videoFps; 1112 } 1113 } 1114 sp<AMessage> response = new AMessage; 1115 if (err == OK) { 1116 writeToAMessage(response, sync, videoFps); 1117 } 1118 response->setInt32("err", err); 1119 response->postReply(replyID); 1120 break; 1121 } 1122 1123 case kWhatScanSources: 1124 { 1125 int32_t generation; 1126 CHECK(msg->findInt32("generation", &generation)); 1127 if (generation != mScanSourcesGeneration) { 1128 // Drop obsolete msg. 1129 break; 1130 } 1131 1132 mScanSourcesPending = false; 1133 1134 ALOGV("scanning sources haveAudio=%d, haveVideo=%d", 1135 mAudioDecoder != NULL, mVideoDecoder != NULL); 1136 1137 bool mHadAnySourcesBefore = 1138 (mAudioDecoder != NULL) || (mVideoDecoder != NULL); 1139 bool rescan = false; 1140 1141 // initialize video before audio because successful initialization of 1142 // video may change deep buffer mode of audio. 1143 if (mNativeWindow != NULL && mNativeWindow->getANativeWindow() != NULL) { 1144 if (instantiateDecoder(false, &mVideoDecoder) == -EWOULDBLOCK) { 1145 rescan = true; 1146 } 1147 } 1148 1149 // Don't try to re-open audio sink if there's an existing decoder. 1150 if (mAudioSink != NULL && mAudioDecoder == NULL) { 1151 if (instantiateDecoder(true, &mAudioDecoder) == -EWOULDBLOCK) { 1152 rescan = true; 1153 } 1154 } 1155 1156 if (!mHadAnySourcesBefore 1157 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 1158 // This is the first time we've found anything playable. 1159 1160 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) { 1161 schedulePollDuration(); 1162 } 1163 } 1164 1165 status_t err; 1166 if ((err = mSource->feedMoreTSData()) != OK) { 1167 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 1168 // We're not currently decoding anything (no audio or 1169 // video tracks found) and we just ran out of input data. 1170 1171 if (err == ERROR_END_OF_STREAM) { 1172 notifyListener(mSrcId, MEDIA2_PLAYBACK_COMPLETE, 0, 0); 1173 } else { 1174 notifyListener(mSrcId, MEDIA2_ERROR, MEDIA2_ERROR_UNKNOWN, err); 1175 } 1176 } 1177 break; 1178 } 1179 1180 if (rescan) { 1181 msg->post(100000ll); 1182 mScanSourcesPending = true; 1183 } 1184 break; 1185 } 1186 1187 case kWhatVideoNotify: 1188 case kWhatAudioNotify: 1189 { 1190 bool audio = msg->what() == kWhatAudioNotify; 1191 1192 int32_t currentDecoderGeneration = 1193 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration); 1194 int32_t requesterGeneration = currentDecoderGeneration - 1; 1195 CHECK(msg->findInt32("generation", &requesterGeneration)); 1196 1197 if (requesterGeneration != currentDecoderGeneration) { 1198 ALOGV("got message from old %s decoder, generation(%d:%d)", 1199 audio ? "audio" : "video", requesterGeneration, 1200 currentDecoderGeneration); 1201 sp<AMessage> reply; 1202 if (!(msg->findMessage("reply", &reply))) { 1203 return; 1204 } 1205 1206 reply->setInt32("err", INFO_DISCONTINUITY); 1207 reply->post(); 1208 return; 1209 } 1210 1211 int32_t what; 1212 CHECK(msg->findInt32("what", &what)); 1213 1214 if (what == DecoderBase::kWhatInputDiscontinuity) { 1215 int32_t formatChange; 1216 CHECK(msg->findInt32("formatChange", &formatChange)); 1217 1218 ALOGV("%s discontinuity: formatChange %d", 1219 audio ? "audio" : "video", formatChange); 1220 1221 if (formatChange) { 1222 mDeferredActions.push_back( 1223 new FlushDecoderAction( 1224 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 1225 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN)); 1226 } 1227 1228 mDeferredActions.push_back( 1229 new SimpleAction( 1230 &NuPlayer2::performScanSources)); 1231 1232 processDeferredActions(); 1233 } else if (what == DecoderBase::kWhatEOS) { 1234 int32_t err; 1235 CHECK(msg->findInt32("err", &err)); 1236 1237 if (err == ERROR_END_OF_STREAM) { 1238 ALOGV("got %s decoder EOS", audio ? "audio" : "video"); 1239 } else { 1240 ALOGV("got %s decoder EOS w/ error %d", 1241 audio ? "audio" : "video", 1242 err); 1243 } 1244 1245 mRenderer->queueEOS(audio, err); 1246 } else if (what == DecoderBase::kWhatFlushCompleted) { 1247 ALOGV("decoder %s flush completed", audio ? "audio" : "video"); 1248 1249 handleFlushComplete(audio, true /* isDecoder */); 1250 finishFlushIfPossible(); 1251 } else if (what == DecoderBase::kWhatVideoSizeChanged) { 1252 sp<AMessage> format; 1253 CHECK(msg->findMessage("format", &format)); 1254 1255 sp<AMessage> inputFormat = 1256 mSource->getFormat(false /* audio */); 1257 1258 setVideoScalingMode(mVideoScalingMode); 1259 updateVideoSize(mSrcId, inputFormat, format); 1260 } else if (what == DecoderBase::kWhatShutdownCompleted) { 1261 ALOGV("%s shutdown completed", audio ? "audio" : "video"); 1262 if (audio) { 1263 mAudioDecoder.clear(); 1264 mAudioDecoderError = false; 1265 ++mAudioDecoderGeneration; 1266 1267 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER); 1268 mFlushingAudio = SHUT_DOWN; 1269 } else { 1270 mVideoDecoder.clear(); 1271 mVideoDecoderError = false; 1272 ++mVideoDecoderGeneration; 1273 1274 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER); 1275 mFlushingVideo = SHUT_DOWN; 1276 } 1277 1278 finishFlushIfPossible(); 1279 } else if (what == DecoderBase::kWhatResumeCompleted) { 1280 finishResume(); 1281 } else if (what == DecoderBase::kWhatError) { 1282 status_t err; 1283 if (!msg->findInt32("err", &err) || err == OK) { 1284 err = UNKNOWN_ERROR; 1285 } 1286 1287 // Decoder errors can be due to Source (e.g. from streaming), 1288 // or from decoding corrupted bitstreams, or from other decoder 1289 // MediaCodec operations (e.g. from an ongoing reset or seek). 1290 // They may also be due to openAudioSink failure at 1291 // decoder start or after a format change. 1292 // 1293 // We try to gracefully shut down the affected decoder if possible, 1294 // rather than trying to force the shutdown with something 1295 // similar to performReset(). This method can lead to a hang 1296 // if MediaCodec functions block after an error, but they should 1297 // typically return INVALID_OPERATION instead of blocking. 1298 1299 FlushStatus *flushing = audio ? &mFlushingAudio : &mFlushingVideo; 1300 ALOGE("received error(%#x) from %s decoder, flushing(%d), now shutting down", 1301 err, audio ? "audio" : "video", *flushing); 1302 1303 switch (*flushing) { 1304 case NONE: 1305 mDeferredActions.push_back( 1306 new FlushDecoderAction( 1307 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 1308 audio ? FLUSH_CMD_NONE : FLUSH_CMD_SHUTDOWN)); 1309 processDeferredActions(); 1310 break; 1311 case FLUSHING_DECODER: 1312 *flushing = FLUSHING_DECODER_SHUTDOWN; // initiate shutdown after flush. 1313 break; // Wait for flush to complete. 1314 case FLUSHING_DECODER_SHUTDOWN: 1315 break; // Wait for flush to complete. 1316 case SHUTTING_DOWN_DECODER: 1317 break; // Wait for shutdown to complete. 1318 case FLUSHED: 1319 getDecoder(audio)->initiateShutdown(); // In the middle of a seek. 1320 *flushing = SHUTTING_DOWN_DECODER; // Shut down. 1321 break; 1322 case SHUT_DOWN: 1323 finishFlushIfPossible(); // Should not occur. 1324 break; // Finish anyways. 1325 } 1326 if (mSource != nullptr) { 1327 if (audio) { 1328 if (mVideoDecoderError || mSource->getFormat(false /* audio */) == NULL 1329 || mNativeWindow == NULL || mNativeWindow->getANativeWindow() == NULL 1330 || mVideoDecoder == NULL) { 1331 // When both audio and video have error, or this stream has only audio 1332 // which has error, notify client of error. 1333 notifyListener(mSrcId, MEDIA2_ERROR, MEDIA2_ERROR_UNKNOWN, err); 1334 } else { 1335 // Only audio track has error. Video track could be still good to play. 1336 notifyListener(mSrcId, MEDIA2_INFO, MEDIA2_INFO_PLAY_AUDIO_ERROR, err); 1337 } 1338 mAudioDecoderError = true; 1339 } else { 1340 if (mAudioDecoderError || mSource->getFormat(true /* audio */) == NULL 1341 || mAudioSink == NULL || mAudioDecoder == NULL) { 1342 // When both audio and video have error, or this stream has only video 1343 // which has error, notify client of error. 1344 notifyListener(mSrcId, MEDIA2_ERROR, MEDIA2_ERROR_UNKNOWN, err); 1345 } else { 1346 // Only video track has error. Audio track could be still good to play. 1347 notifyListener(mSrcId, MEDIA2_INFO, MEDIA2_INFO_PLAY_VIDEO_ERROR, err); 1348 } 1349 mVideoDecoderError = true; 1350 } 1351 } 1352 } else { 1353 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.", 1354 what, 1355 what >> 24, 1356 (what >> 16) & 0xff, 1357 (what >> 8) & 0xff, 1358 what & 0xff); 1359 } 1360 1361 break; 1362 } 1363 1364 case kWhatRendererNotify: 1365 { 1366 int32_t requesterGeneration = mRendererGeneration - 1; 1367 CHECK(msg->findInt32("generation", &requesterGeneration)); 1368 if (requesterGeneration != mRendererGeneration) { 1369 ALOGV("got message from old renderer, generation(%d:%d)", 1370 requesterGeneration, mRendererGeneration); 1371 return; 1372 } 1373 1374 int32_t what; 1375 CHECK(msg->findInt32("what", &what)); 1376 1377 if (what == Renderer::kWhatEOS) { 1378 int32_t audio; 1379 CHECK(msg->findInt32("audio", &audio)); 1380 1381 int32_t finalResult; 1382 CHECK(msg->findInt32("finalResult", &finalResult)); 1383 1384 if (audio) { 1385 mAudioEOS = true; 1386 } else { 1387 mVideoEOS = true; 1388 } 1389 1390 if (finalResult == ERROR_END_OF_STREAM) { 1391 ALOGV("reached %s EOS", audio ? "audio" : "video"); 1392 } else { 1393 ALOGE("%s track encountered an error (%d)", 1394 audio ? "audio" : "video", finalResult); 1395 1396 notifyListener( 1397 mSrcId, MEDIA2_ERROR, MEDIA2_ERROR_UNKNOWN, finalResult); 1398 } 1399 1400 if ((mAudioEOS || mAudioDecoder == NULL) 1401 && (mVideoEOS || mVideoDecoder == NULL)) { 1402 notifyListener(mSrcId, MEDIA2_PLAYBACK_COMPLETE, 0, 0); 1403 } 1404 } else if (what == Renderer::kWhatFlushComplete) { 1405 int32_t audio; 1406 CHECK(msg->findInt32("audio", &audio)); 1407 1408 if (audio) { 1409 mAudioEOS = false; 1410 } else { 1411 mVideoEOS = false; 1412 } 1413 1414 ALOGV("renderer %s flush completed.", audio ? "audio" : "video"); 1415 if (audio && (mFlushingAudio == NONE || mFlushingAudio == FLUSHED 1416 || mFlushingAudio == SHUT_DOWN)) { 1417 // Flush has been handled by tear down. 1418 break; 1419 } 1420 handleFlushComplete(audio, false /* isDecoder */); 1421 finishFlushIfPossible(); 1422 } else if (what == Renderer::kWhatVideoRenderingStart) { 1423 notifyListener(mSrcId, MEDIA2_INFO, MEDIA2_INFO_VIDEO_RENDERING_START, 0); 1424 } else if (what == Renderer::kWhatMediaRenderingStart) { 1425 ALOGV("media rendering started"); 1426 notifyListener(mSrcId, MEDIA2_STARTED, 0, 0); 1427 } else if (what == Renderer::kWhatAudioTearDown) { 1428 int32_t reason; 1429 CHECK(msg->findInt32("reason", &reason)); 1430 ALOGV("Tear down audio with reason %d.", reason); 1431 if (reason == Renderer::kDueToTimeout && !(mPaused && mOffloadAudio)) { 1432 // TimeoutWhenPaused is only for offload mode. 1433 ALOGW("Receive a stale message for teardown."); 1434 break; 1435 } 1436 int64_t positionUs; 1437 if (!msg->findInt64("positionUs", &positionUs)) { 1438 positionUs = mPreviousSeekTimeUs; 1439 } 1440 1441 restartAudio( 1442 positionUs, reason == Renderer::kForceNonOffload /* forceNonOffload */, 1443 reason != Renderer::kDueToTimeout /* needsToCreateAudioDecoder */); 1444 } 1445 break; 1446 } 1447 1448 case kWhatMoreDataQueued: 1449 { 1450 break; 1451 } 1452 1453 case kWhatReset: 1454 { 1455 ALOGV("kWhatReset"); 1456 1457 mResetting = true; 1458 stopPlaybackTimer("kWhatReset"); 1459 stopRebufferingTimer(true); 1460 1461 mDeferredActions.push_back( 1462 new FlushDecoderAction( 1463 FLUSH_CMD_SHUTDOWN /* audio */, 1464 FLUSH_CMD_SHUTDOWN /* video */)); 1465 1466 mDeferredActions.push_back( 1467 new SimpleAction(&NuPlayer2::performReset)); 1468 1469 processDeferredActions(); 1470 break; 1471 } 1472 1473 case kWhatNotifyTime: 1474 { 1475 ALOGV("kWhatNotifyTime"); 1476 int64_t timerUs; 1477 CHECK(msg->findInt64("timerUs", &timerUs)); 1478 1479 notifyListener(mSrcId, MEDIA2_NOTIFY_TIME, timerUs, 0); 1480 break; 1481 } 1482 1483 case kWhatSeek: 1484 { 1485 int64_t seekTimeUs; 1486 int32_t mode; 1487 int32_t needNotify; 1488 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 1489 CHECK(msg->findInt32("mode", &mode)); 1490 CHECK(msg->findInt32("needNotify", &needNotify)); 1491 1492 ALOGV("kWhatSeek seekTimeUs=%lld us, mode=%d, needNotify=%d", 1493 (long long)seekTimeUs, mode, needNotify); 1494 1495 if (!mStarted) { 1496 if (!mSourceStarted) { 1497 mSourceStarted = true; 1498 mSource->start(); 1499 } 1500 if (seekTimeUs > 0) { 1501 performSeek(seekTimeUs, (MediaPlayer2SeekMode)mode); 1502 } 1503 1504 if (needNotify) { 1505 notifyDriverSeekComplete(mSrcId); 1506 } 1507 break; 1508 } 1509 1510 // seeks can take a while, so we essentially paused 1511 notifyListener(mSrcId, MEDIA2_PAUSED, 0, 0); 1512 1513 mDeferredActions.push_back( 1514 new FlushDecoderAction(FLUSH_CMD_FLUSH /* audio */, 1515 FLUSH_CMD_FLUSH /* video */)); 1516 1517 mDeferredActions.push_back( 1518 new SeekAction(seekTimeUs, (MediaPlayer2SeekMode)mode)); 1519 1520 // After a flush without shutdown, decoder is paused. 1521 // Don't resume it until source seek is done, otherwise it could 1522 // start pulling stale data too soon. 1523 mDeferredActions.push_back( 1524 new ResumeDecoderAction(needNotify)); 1525 1526 processDeferredActions(); 1527 break; 1528 } 1529 1530 case kWhatPause: 1531 { 1532 onPause(); 1533 notifyListener(mSrcId, MEDIA2_PAUSED, 0, 0); 1534 mPausedByClient = true; 1535 break; 1536 } 1537 1538 case kWhatSourceNotify: 1539 { 1540 onSourceNotify(msg); 1541 break; 1542 } 1543 1544 case kWhatClosedCaptionNotify: 1545 { 1546 onClosedCaptionNotify(msg); 1547 break; 1548 } 1549 1550 case kWhatPrepareDrm: 1551 { 1552 status_t status = onPrepareDrm(msg); 1553 1554 sp<AMessage> response = new AMessage; 1555 response->setInt32("status", status); 1556 sp<AReplyToken> replyID; 1557 CHECK(msg->senderAwaitsResponse(&replyID)); 1558 response->postReply(replyID); 1559 break; 1560 } 1561 1562 case kWhatReleaseDrm: 1563 { 1564 status_t status = onReleaseDrm(); 1565 1566 sp<AMessage> response = new AMessage; 1567 response->setInt32("status", status); 1568 sp<AReplyToken> replyID; 1569 CHECK(msg->senderAwaitsResponse(&replyID)); 1570 response->postReply(replyID); 1571 break; 1572 } 1573 1574 default: 1575 TRESPASS(); 1576 break; 1577 } 1578} 1579 1580void NuPlayer2::onResume() { 1581 if (!mPaused || mResetting) { 1582 ALOGD_IF(mResetting, "resetting, onResume discarded"); 1583 return; 1584 } 1585 mPaused = false; 1586 if (mSource != NULL) { 1587 mSource->resume(); 1588 } else { 1589 ALOGW("resume called when source is gone or not set"); 1590 } 1591 // |mAudioDecoder| may have been released due to the pause timeout, so re-create it if 1592 // needed. 1593 if (audioDecoderStillNeeded() && mAudioDecoder == NULL) { 1594 instantiateDecoder(true /* audio */, &mAudioDecoder); 1595 } 1596 if (mRenderer != NULL) { 1597 mRenderer->resume(); 1598 } else { 1599 ALOGW("resume called when renderer is gone or not set"); 1600 } 1601 1602 startPlaybackTimer("onresume"); 1603} 1604 1605void NuPlayer2::onStart() { 1606 ALOGV("onStart: mCrypto: %p", mCrypto.get()); 1607 1608 if (!mSourceStarted) { 1609 mSourceStarted = true; 1610 mSource->start(); 1611 } 1612 1613 mOffloadAudio = false; 1614 mAudioEOS = false; 1615 mVideoEOS = false; 1616 mStarted = true; 1617 mPaused = false; 1618 1619 uint32_t flags = 0; 1620 1621 if (mSource->isRealTime()) { 1622 flags |= Renderer::FLAG_REAL_TIME; 1623 } 1624 1625 bool hasAudio = (mSource->getFormat(true /* audio */) != NULL); 1626 bool hasVideo = (mSource->getFormat(false /* audio */) != NULL); 1627 if (!hasAudio && !hasVideo) { 1628 ALOGE("no metadata for either audio or video source"); 1629 mSource->stop(); 1630 mSourceStarted = false; 1631 notifyListener(mSrcId, MEDIA2_ERROR, MEDIA2_ERROR_UNKNOWN, ERROR_MALFORMED); 1632 return; 1633 } 1634 ALOGV_IF(!hasAudio, "no metadata for audio source"); // video only stream 1635 1636 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1637 1638 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC; 1639 if (mAudioSink != NULL) { 1640 streamType = mAudioSink->getAudioStreamType(); 1641 } 1642 1643 mOffloadAudio = 1644 canOffloadStream(audioMeta, hasVideo, mSource->isStreaming(), streamType) 1645 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f); 1646 1647 // Modular DRM: Disabling audio offload if the source is protected 1648 if (mOffloadAudio && mIsDrmProtected) { 1649 mOffloadAudio = false; 1650 ALOGV("onStart: Disabling mOffloadAudio now that the source is protected."); 1651 } 1652 1653 if (mOffloadAudio) { 1654 flags |= Renderer::FLAG_OFFLOAD_AUDIO; 1655 } 1656 1657 sp<AMessage> notify = new AMessage(kWhatRendererNotify, this); 1658 ++mRendererGeneration; 1659 notify->setInt32("generation", mRendererGeneration); 1660 mRenderer = new Renderer(mAudioSink, mMediaClock, notify, flags); 1661 mRendererLooper = new ALooper; 1662 mRendererLooper->setName("NuPlayerRenderer"); 1663 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO); 1664 mRendererLooper->registerHandler(mRenderer); 1665 1666 status_t err = mRenderer->setPlaybackSettings(mPlaybackSettings); 1667 if (err != OK) { 1668 mSource->stop(); 1669 mSourceStarted = false; 1670 notifyListener(mSrcId, MEDIA2_ERROR, MEDIA2_ERROR_UNKNOWN, err); 1671 return; 1672 } 1673 1674 float rate = getFrameRate(); 1675 if (rate > 0) { 1676 mRenderer->setVideoFrameRate(rate); 1677 } 1678 1679 if (mVideoDecoder != NULL) { 1680 mVideoDecoder->setRenderer(mRenderer); 1681 } 1682 if (mAudioDecoder != NULL) { 1683 mAudioDecoder->setRenderer(mRenderer); 1684 } 1685 1686 startPlaybackTimer("onstart"); 1687 1688 postScanSources(); 1689} 1690 1691void NuPlayer2::startPlaybackTimer(const char *where) { 1692 Mutex::Autolock autoLock(mPlayingTimeLock); 1693 if (mLastStartedPlayingTimeNs == 0) { 1694 mLastStartedPlayingTimeNs = systemTime(); 1695 ALOGV("startPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where); 1696 } 1697} 1698 1699void NuPlayer2::stopPlaybackTimer(const char *where) { 1700 Mutex::Autolock autoLock(mPlayingTimeLock); 1701 1702 ALOGV("stopPlaybackTimer() time %20" PRId64 " (%s)", mLastStartedPlayingTimeNs, where); 1703 1704 if (mLastStartedPlayingTimeNs != 0) { 1705 sp<NuPlayer2Driver> driver = mDriver.promote(); 1706 if (driver != NULL) { 1707 int64_t now = systemTime(); 1708 int64_t played = now - mLastStartedPlayingTimeNs; 1709 ALOGV("stopPlaybackTimer() log %20" PRId64 "", played); 1710 1711 if (played > 0) { 1712 driver->notifyMorePlayingTimeUs(mSrcId, (played+500)/1000); 1713 } 1714 } 1715 mLastStartedPlayingTimeNs = 0; 1716 } 1717} 1718 1719void NuPlayer2::startRebufferingTimer() { 1720 Mutex::Autolock autoLock(mPlayingTimeLock); 1721 if (mLastStartedRebufferingTimeNs == 0) { 1722 mLastStartedRebufferingTimeNs = systemTime(); 1723 ALOGV("startRebufferingTimer() time %20" PRId64 "", mLastStartedRebufferingTimeNs); 1724 } 1725} 1726 1727void NuPlayer2::stopRebufferingTimer(bool exitingPlayback) { 1728 Mutex::Autolock autoLock(mPlayingTimeLock); 1729 1730 ALOGV("stopRebufferTimer() time %20" PRId64 " (exiting %d)", mLastStartedRebufferingTimeNs, exitingPlayback); 1731 1732 if (mLastStartedRebufferingTimeNs != 0) { 1733 sp<NuPlayer2Driver> driver = mDriver.promote(); 1734 if (driver != NULL) { 1735 int64_t now = systemTime(); 1736 int64_t rebuffered = now - mLastStartedRebufferingTimeNs; 1737 ALOGV("stopRebufferingTimer() log %20" PRId64 "", rebuffered); 1738 1739 if (rebuffered > 0) { 1740 driver->notifyMoreRebufferingTimeUs(mSrcId, (rebuffered+500)/1000); 1741 if (exitingPlayback) { 1742 driver->notifyRebufferingWhenExit(mSrcId, true); 1743 } 1744 } 1745 } 1746 mLastStartedRebufferingTimeNs = 0; 1747 } 1748} 1749 1750void NuPlayer2::onPause() { 1751 1752 stopPlaybackTimer("onPause"); 1753 1754 if (mPaused) { 1755 return; 1756 } 1757 mPaused = true; 1758 if (mSource != NULL) { 1759 mSource->pause(); 1760 } else { 1761 ALOGW("pause called when source is gone or not set"); 1762 } 1763 if (mRenderer != NULL) { 1764 mRenderer->pause(); 1765 } else { 1766 ALOGW("pause called when renderer is gone or not set"); 1767 } 1768 1769} 1770 1771bool NuPlayer2::audioDecoderStillNeeded() { 1772 // Audio decoder is no longer needed if it's in shut/shutting down status. 1773 return ((mFlushingAudio != SHUT_DOWN) && (mFlushingAudio != SHUTTING_DOWN_DECODER)); 1774} 1775 1776void NuPlayer2::handleFlushComplete(bool audio, bool isDecoder) { 1777 // We wait for both the decoder flush and the renderer flush to complete 1778 // before entering either the FLUSHED or the SHUTTING_DOWN_DECODER state. 1779 1780 mFlushComplete[audio][isDecoder] = true; 1781 if (!mFlushComplete[audio][!isDecoder]) { 1782 return; 1783 } 1784 1785 FlushStatus *state = audio ? &mFlushingAudio : &mFlushingVideo; 1786 switch (*state) { 1787 case FLUSHING_DECODER: 1788 { 1789 *state = FLUSHED; 1790 break; 1791 } 1792 1793 case FLUSHING_DECODER_SHUTDOWN: 1794 { 1795 *state = SHUTTING_DOWN_DECODER; 1796 1797 ALOGV("initiating %s decoder shutdown", audio ? "audio" : "video"); 1798 getDecoder(audio)->initiateShutdown(); 1799 break; 1800 } 1801 1802 default: 1803 // decoder flush completes only occur in a flushing state. 1804 LOG_ALWAYS_FATAL_IF(isDecoder, "decoder flush in invalid state %d", *state); 1805 break; 1806 } 1807} 1808 1809void NuPlayer2::finishFlushIfPossible() { 1810 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED 1811 && mFlushingAudio != SHUT_DOWN) { 1812 return; 1813 } 1814 1815 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED 1816 && mFlushingVideo != SHUT_DOWN) { 1817 return; 1818 } 1819 1820 ALOGV("both audio and video are flushed now."); 1821 1822 mFlushingAudio = NONE; 1823 mFlushingVideo = NONE; 1824 1825 clearFlushComplete(); 1826 1827 processDeferredActions(); 1828} 1829 1830void NuPlayer2::postScanSources() { 1831 if (mScanSourcesPending) { 1832 return; 1833 } 1834 1835 sp<AMessage> msg = new AMessage(kWhatScanSources, this); 1836 msg->setInt32("generation", mScanSourcesGeneration); 1837 msg->post(); 1838 1839 mScanSourcesPending = true; 1840} 1841 1842void NuPlayer2::tryOpenAudioSinkForOffload( 1843 const sp<AMessage> &format, const sp<MetaData> &audioMeta, bool hasVideo) { 1844 // Note: This is called early in NuPlayer2 to determine whether offloading 1845 // is possible; otherwise the decoders call the renderer openAudioSink directly. 1846 1847 status_t err = mRenderer->openAudioSink( 1848 format, true /* offloadOnly */, hasVideo, 1849 AUDIO_OUTPUT_FLAG_NONE, &mOffloadAudio, mSource->isStreaming()); 1850 if (err != OK) { 1851 // Any failure we turn off mOffloadAudio. 1852 mOffloadAudio = false; 1853 } else if (mOffloadAudio) { 1854 sendMetaDataToHal(mAudioSink, audioMeta); 1855 } 1856} 1857 1858void NuPlayer2::closeAudioSink() { 1859 mRenderer->closeAudioSink(); 1860} 1861 1862void NuPlayer2::restartAudio( 1863 int64_t currentPositionUs, bool forceNonOffload, bool needsToCreateAudioDecoder) { 1864 if (mAudioDecoder != NULL) { 1865 mAudioDecoder->pause(); 1866 mAudioDecoder.clear(); 1867 mAudioDecoderError = false; 1868 ++mAudioDecoderGeneration; 1869 } 1870 if (mFlushingAudio == FLUSHING_DECODER) { 1871 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true; 1872 mFlushingAudio = FLUSHED; 1873 finishFlushIfPossible(); 1874 } else if (mFlushingAudio == FLUSHING_DECODER_SHUTDOWN 1875 || mFlushingAudio == SHUTTING_DOWN_DECODER) { 1876 mFlushComplete[1 /* audio */][1 /* isDecoder */] = true; 1877 mFlushingAudio = SHUT_DOWN; 1878 finishFlushIfPossible(); 1879 needsToCreateAudioDecoder = false; 1880 } 1881 if (mRenderer == NULL) { 1882 return; 1883 } 1884 closeAudioSink(); 1885 mRenderer->flush(true /* audio */, false /* notifyComplete */); 1886 if (mVideoDecoder != NULL) { 1887 mRenderer->flush(false /* audio */, false /* notifyComplete */); 1888 } 1889 1890 performSeek(currentPositionUs, MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC /* mode */); 1891 1892 if (forceNonOffload) { 1893 mRenderer->signalDisableOffloadAudio(); 1894 mOffloadAudio = false; 1895 } 1896 if (needsToCreateAudioDecoder) { 1897 instantiateDecoder(true /* audio */, &mAudioDecoder, !forceNonOffload); 1898 } 1899} 1900 1901void NuPlayer2::determineAudioModeChange(const sp<AMessage> &audioFormat) { 1902 if (mSource == NULL || mAudioSink == NULL) { 1903 return; 1904 } 1905 1906 if (mRenderer == NULL) { 1907 ALOGW("No renderer can be used to determine audio mode. Use non-offload for safety."); 1908 mOffloadAudio = false; 1909 return; 1910 } 1911 1912 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 1913 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 1914 audio_stream_type_t streamType = mAudioSink->getAudioStreamType(); 1915 const bool hasVideo = (videoFormat != NULL); 1916 bool canOffload = canOffloadStream( 1917 audioMeta, hasVideo, mSource->isStreaming(), streamType) 1918 && (mPlaybackSettings.mSpeed == 1.f && mPlaybackSettings.mPitch == 1.f); 1919 1920 // Modular DRM: Disabling audio offload if the source is protected 1921 if (canOffload && mIsDrmProtected) { 1922 canOffload = false; 1923 ALOGV("determineAudioModeChange: Disabling mOffloadAudio b/c the source is protected."); 1924 } 1925 1926 if (canOffload) { 1927 if (!mOffloadAudio) { 1928 mRenderer->signalEnableOffloadAudio(); 1929 } 1930 // open audio sink early under offload mode. 1931 tryOpenAudioSinkForOffload(audioFormat, audioMeta, hasVideo); 1932 } else { 1933 if (mOffloadAudio) { 1934 mRenderer->signalDisableOffloadAudio(); 1935 mOffloadAudio = false; 1936 } 1937 } 1938} 1939 1940status_t NuPlayer2::instantiateDecoder( 1941 bool audio, sp<DecoderBase> *decoder, bool checkAudioModeChange) { 1942 // The audio decoder could be cleared by tear down. If still in shut down 1943 // process, no need to create a new audio decoder. 1944 if (*decoder != NULL || (audio && mFlushingAudio == SHUT_DOWN)) { 1945 return OK; 1946 } 1947 1948 sp<AMessage> format = mSource->getFormat(audio); 1949 1950 if (format == NULL) { 1951 return UNKNOWN_ERROR; 1952 } else { 1953 status_t err; 1954 if (format->findInt32("err", &err) && err) { 1955 return err; 1956 } 1957 } 1958 1959 format->setInt32("priority", 0 /* realtime */); 1960 1961 if (!audio) { 1962 AString mime; 1963 CHECK(format->findString("mime", &mime)); 1964 1965 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, this); 1966 if (mCCDecoder == NULL) { 1967 mCCDecoder = new CCDecoder(ccNotify); 1968 } 1969 1970 if (mSourceFlags & Source::FLAG_SECURE) { 1971 format->setInt32("secure", true); 1972 } 1973 1974 if (mSourceFlags & Source::FLAG_PROTECTED) { 1975 format->setInt32("protected", true); 1976 } 1977 1978 float rate = getFrameRate(); 1979 if (rate > 0) { 1980 format->setFloat("operating-rate", rate * mPlaybackSettings.mSpeed); 1981 } 1982 } 1983 1984 if (audio) { 1985 sp<AMessage> notify = new AMessage(kWhatAudioNotify, this); 1986 ++mAudioDecoderGeneration; 1987 notify->setInt32("generation", mAudioDecoderGeneration); 1988 1989 if (checkAudioModeChange) { 1990 determineAudioModeChange(format); 1991 } 1992 if (mOffloadAudio) { 1993 mSource->setOffloadAudio(true /* offload */); 1994 1995 const bool hasVideo = (mSource->getFormat(false /*audio */) != NULL); 1996 format->setInt32("has-video", hasVideo); 1997 *decoder = new DecoderPassThrough(notify, mSource, mRenderer); 1998 ALOGV("instantiateDecoder audio DecoderPassThrough hasVideo: %d", hasVideo); 1999 } else { 2000 mSource->setOffloadAudio(false /* offload */); 2001 2002 *decoder = new Decoder(notify, mSource, mPID, mUID, mRenderer); 2003 ALOGV("instantiateDecoder audio Decoder"); 2004 } 2005 mAudioDecoderError = false; 2006 } else { 2007 sp<AMessage> notify = new AMessage(kWhatVideoNotify, this); 2008 ++mVideoDecoderGeneration; 2009 notify->setInt32("generation", mVideoDecoderGeneration); 2010 2011 *decoder = new Decoder( 2012 notify, mSource, mPID, mUID, mRenderer, mNativeWindow, mCCDecoder); 2013 mVideoDecoderError = false; 2014 2015 // enable FRC if high-quality AV sync is requested, even if not 2016 // directly queuing to display, as this will even improve textureview 2017 // playback. 2018 { 2019 if (property_get_bool("persist.sys.media.avsync", false)) { 2020 format->setInt32("auto-frc", 1); 2021 } 2022 } 2023 } 2024 (*decoder)->init(); 2025 2026 // Modular DRM 2027 if (mIsDrmProtected) { 2028 format->setObject("crypto", mCrypto); 2029 ALOGV("instantiateDecoder: mCrypto: %p isSecure: %d", mCrypto.get(), 2030 (mSourceFlags & Source::FLAG_SECURE) != 0); 2031 } 2032 2033 (*decoder)->configure(format); 2034 2035 if (!audio) { 2036 sp<AMessage> params = new AMessage(); 2037 float rate = getFrameRate(); 2038 if (rate > 0) { 2039 params->setFloat("frame-rate-total", rate); 2040 } 2041 2042 sp<MetaData> fileMeta = getFileMeta(); 2043 if (fileMeta != NULL) { 2044 int32_t videoTemporalLayerCount; 2045 if (fileMeta->findInt32(kKeyTemporalLayerCount, &videoTemporalLayerCount) 2046 && videoTemporalLayerCount > 0) { 2047 params->setInt32("temporal-layer-count", videoTemporalLayerCount); 2048 } 2049 } 2050 2051 if (params->countEntries() > 0) { 2052 (*decoder)->setParameters(params); 2053 } 2054 } 2055 return OK; 2056} 2057 2058void NuPlayer2::updateVideoSize( 2059 int64_t srcId, 2060 const sp<AMessage> &inputFormat, 2061 const sp<AMessage> &outputFormat) { 2062 if (inputFormat == NULL) { 2063 ALOGW("Unknown video size, reporting 0x0!"); 2064 notifyListener(srcId, MEDIA2_SET_VIDEO_SIZE, 0, 0); 2065 return; 2066 } 2067 int32_t err = OK; 2068 inputFormat->findInt32("err", &err); 2069 if (err == -EWOULDBLOCK) { 2070 ALOGW("Video meta is not available yet!"); 2071 return; 2072 } 2073 if (err != OK) { 2074 ALOGW("Something is wrong with video meta!"); 2075 return; 2076 } 2077 2078 int32_t displayWidth, displayHeight; 2079 if (outputFormat != NULL) { 2080 int32_t width, height; 2081 CHECK(outputFormat->findInt32("width", &width)); 2082 CHECK(outputFormat->findInt32("height", &height)); 2083 2084 int32_t cropLeft, cropTop, cropRight, cropBottom; 2085 CHECK(outputFormat->findRect( 2086 "crop", 2087 &cropLeft, &cropTop, &cropRight, &cropBottom)); 2088 2089 displayWidth = cropRight - cropLeft + 1; 2090 displayHeight = cropBottom - cropTop + 1; 2091 2092 ALOGV("Video output format changed to %d x %d " 2093 "(crop: %d x %d @ (%d, %d))", 2094 width, height, 2095 displayWidth, 2096 displayHeight, 2097 cropLeft, cropTop); 2098 } else { 2099 CHECK(inputFormat->findInt32("width", &displayWidth)); 2100 CHECK(inputFormat->findInt32("height", &displayHeight)); 2101 2102 ALOGV("Video input format %d x %d", displayWidth, displayHeight); 2103 } 2104 2105 // Take into account sample aspect ratio if necessary: 2106 int32_t sarWidth, sarHeight; 2107 if (inputFormat->findInt32("sar-width", &sarWidth) 2108 && inputFormat->findInt32("sar-height", &sarHeight) 2109 && sarWidth > 0 && sarHeight > 0) { 2110 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight); 2111 2112 displayWidth = (displayWidth * sarWidth) / sarHeight; 2113 2114 ALOGV("display dimensions %d x %d", displayWidth, displayHeight); 2115 } else { 2116 int32_t width, height; 2117 if (inputFormat->findInt32("display-width", &width) 2118 && inputFormat->findInt32("display-height", &height) 2119 && width > 0 && height > 0 2120 && displayWidth > 0 && displayHeight > 0) { 2121 if (displayHeight * (int64_t)width / height > (int64_t)displayWidth) { 2122 displayHeight = (int32_t)(displayWidth * (int64_t)height / width); 2123 } else { 2124 displayWidth = (int32_t)(displayHeight * (int64_t)width / height); 2125 } 2126 ALOGV("Video display width and height are overridden to %d x %d", 2127 displayWidth, displayHeight); 2128 } 2129 } 2130 2131 int32_t rotationDegrees; 2132 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) { 2133 rotationDegrees = 0; 2134 } 2135 2136 if (rotationDegrees == 90 || rotationDegrees == 270) { 2137 int32_t tmp = displayWidth; 2138 displayWidth = displayHeight; 2139 displayHeight = tmp; 2140 } 2141 2142 notifyListener( 2143 srcId, 2144 MEDIA2_SET_VIDEO_SIZE, 2145 displayWidth, 2146 displayHeight); 2147} 2148 2149void NuPlayer2::notifyListener(int64_t srcId, int msg, int ext1, int ext2, const Parcel *in) { 2150 if (mDriver == NULL) { 2151 return; 2152 } 2153 2154 sp<NuPlayer2Driver> driver = mDriver.promote(); 2155 2156 if (driver == NULL) { 2157 return; 2158 } 2159 2160 driver->notifyListener(srcId, msg, ext1, ext2, in); 2161} 2162 2163void NuPlayer2::flushDecoder(bool audio, bool needShutdown) { 2164 ALOGV("[%s] flushDecoder needShutdown=%d", 2165 audio ? "audio" : "video", needShutdown); 2166 2167 const sp<DecoderBase> &decoder = getDecoder(audio); 2168 if (decoder == NULL) { 2169 ALOGI("flushDecoder %s without decoder present", 2170 audio ? "audio" : "video"); 2171 return; 2172 } 2173 2174 // Make sure we don't continue to scan sources until we finish flushing. 2175 ++mScanSourcesGeneration; 2176 if (mScanSourcesPending) { 2177 if (!needShutdown) { 2178 mDeferredActions.push_back( 2179 new SimpleAction(&NuPlayer2::performScanSources)); 2180 } 2181 mScanSourcesPending = false; 2182 } 2183 2184 decoder->signalFlush(); 2185 2186 FlushStatus newStatus = 2187 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 2188 2189 mFlushComplete[audio][false /* isDecoder */] = (mRenderer == NULL); 2190 mFlushComplete[audio][true /* isDecoder */] = false; 2191 if (audio) { 2192 ALOGE_IF(mFlushingAudio != NONE, 2193 "audio flushDecoder() is called in state %d", mFlushingAudio); 2194 mFlushingAudio = newStatus; 2195 } else { 2196 ALOGE_IF(mFlushingVideo != NONE, 2197 "video flushDecoder() is called in state %d", mFlushingVideo); 2198 mFlushingVideo = newStatus; 2199 } 2200} 2201 2202void NuPlayer2::queueDecoderShutdown( 2203 bool audio, bool video, const sp<AMessage> &reply) { 2204 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video); 2205 2206 mDeferredActions.push_back( 2207 new FlushDecoderAction( 2208 audio ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE, 2209 video ? FLUSH_CMD_SHUTDOWN : FLUSH_CMD_NONE)); 2210 2211 mDeferredActions.push_back( 2212 new SimpleAction(&NuPlayer2::performScanSources)); 2213 2214 mDeferredActions.push_back(new PostMessageAction(reply)); 2215 2216 processDeferredActions(); 2217} 2218 2219status_t NuPlayer2::setVideoScalingMode(int32_t mode) { 2220 mVideoScalingMode = mode; 2221 if (mNativeWindow != NULL && mNativeWindow->getANativeWindow() != NULL) { 2222 status_t ret = native_window_set_scaling_mode( 2223 mNativeWindow->getANativeWindow(), mVideoScalingMode); 2224 if (ret != OK) { 2225 ALOGE("Failed to set scaling mode (%d): %s", 2226 -ret, strerror(-ret)); 2227 return ret; 2228 } 2229 } 2230 return OK; 2231} 2232 2233status_t NuPlayer2::getTrackInfo(Parcel* reply) const { 2234 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, this); 2235 msg->setPointer("reply", reply); 2236 2237 sp<AMessage> response; 2238 status_t err = msg->postAndAwaitResponse(&response); 2239 return err; 2240} 2241 2242status_t NuPlayer2::getSelectedTrack(int32_t type, Parcel* reply) const { 2243 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, this); 2244 msg->setPointer("reply", reply); 2245 msg->setInt32("type", type); 2246 2247 sp<AMessage> response; 2248 status_t err = msg->postAndAwaitResponse(&response); 2249 if (err == OK && response != NULL) { 2250 CHECK(response->findInt32("err", &err)); 2251 } 2252 return err; 2253} 2254 2255status_t NuPlayer2::selectTrack(size_t trackIndex, bool select, int64_t timeUs) { 2256 sp<AMessage> msg = new AMessage(kWhatSelectTrack, this); 2257 msg->setSize("trackIndex", trackIndex); 2258 msg->setInt32("select", select); 2259 msg->setInt64("timeUs", timeUs); 2260 2261 sp<AMessage> response; 2262 status_t err = msg->postAndAwaitResponse(&response); 2263 2264 if (err != OK) { 2265 return err; 2266 } 2267 2268 if (!response->findInt32("err", &err)) { 2269 err = OK; 2270 } 2271 2272 return err; 2273} 2274 2275status_t NuPlayer2::getCurrentPosition(int64_t *mediaUs) { 2276 sp<Renderer> renderer = mRenderer; 2277 if (renderer == NULL) { 2278 return NO_INIT; 2279 } 2280 2281 return renderer->getCurrentPosition(mediaUs); 2282} 2283 2284void NuPlayer2::getStats(Vector<sp<AMessage> > *mTrackStats) { 2285 CHECK(mTrackStats != NULL); 2286 2287 mTrackStats->clear(); 2288 if (mVideoDecoder != NULL) { 2289 mTrackStats->push_back(mVideoDecoder->getStats()); 2290 } 2291 if (mAudioDecoder != NULL) { 2292 mTrackStats->push_back(mAudioDecoder->getStats()); 2293 } 2294} 2295 2296sp<MetaData> NuPlayer2::getFileMeta() { 2297 return mSource->getFileFormatMeta(); 2298} 2299 2300float NuPlayer2::getFrameRate() { 2301 sp<MetaData> meta = mSource->getFormatMeta(false /* audio */); 2302 if (meta == NULL) { 2303 return 0; 2304 } 2305 int32_t rate; 2306 if (!meta->findInt32(kKeyFrameRate, &rate)) { 2307 // fall back to try file meta 2308 sp<MetaData> fileMeta = getFileMeta(); 2309 if (fileMeta == NULL) { 2310 ALOGW("source has video meta but not file meta"); 2311 return -1; 2312 } 2313 int32_t fileMetaRate; 2314 if (!fileMeta->findInt32(kKeyFrameRate, &fileMetaRate)) { 2315 return -1; 2316 } 2317 return fileMetaRate; 2318 } 2319 return rate; 2320} 2321 2322void NuPlayer2::schedulePollDuration() { 2323 sp<AMessage> msg = new AMessage(kWhatPollDuration, this); 2324 msg->setInt32("generation", mPollDurationGeneration); 2325 msg->post(); 2326} 2327 2328void NuPlayer2::cancelPollDuration() { 2329 ++mPollDurationGeneration; 2330} 2331 2332void NuPlayer2::processDeferredActions() { 2333 while (!mDeferredActions.empty()) { 2334 // We won't execute any deferred actions until we're no longer in 2335 // an intermediate state, i.e. one more more decoders are currently 2336 // flushing or shutting down. 2337 2338 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 2339 // We're currently flushing, postpone the reset until that's 2340 // completed. 2341 2342 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d", 2343 mFlushingAudio, mFlushingVideo); 2344 2345 break; 2346 } 2347 2348 sp<Action> action = *mDeferredActions.begin(); 2349 mDeferredActions.erase(mDeferredActions.begin()); 2350 2351 action->execute(this); 2352 } 2353} 2354 2355void NuPlayer2::performSeek(int64_t seekTimeUs, MediaPlayer2SeekMode mode) { 2356 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), mode=%d", 2357 (long long)seekTimeUs, seekTimeUs / 1E6, mode); 2358 2359 if (mSource == NULL) { 2360 // This happens when reset occurs right before the loop mode 2361 // asynchronously seeks to the start of the stream. 2362 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL, 2363 "mSource is NULL and decoders not NULL audio(%p) video(%p)", 2364 mAudioDecoder.get(), mVideoDecoder.get()); 2365 return; 2366 } 2367 mPreviousSeekTimeUs = seekTimeUs; 2368 mSource->seekTo(seekTimeUs, mode); 2369 ++mTimedTextGeneration; 2370 2371 // everything's flushed, continue playback. 2372} 2373 2374void NuPlayer2::performDecoderFlush(FlushCommand audio, FlushCommand video) { 2375 ALOGV("performDecoderFlush audio=%d, video=%d", audio, video); 2376 2377 if ((audio == FLUSH_CMD_NONE || mAudioDecoder == NULL) 2378 && (video == FLUSH_CMD_NONE || mVideoDecoder == NULL)) { 2379 return; 2380 } 2381 2382 if (audio != FLUSH_CMD_NONE && mAudioDecoder != NULL) { 2383 flushDecoder(true /* audio */, (audio == FLUSH_CMD_SHUTDOWN)); 2384 } 2385 2386 if (video != FLUSH_CMD_NONE && mVideoDecoder != NULL) { 2387 flushDecoder(false /* audio */, (video == FLUSH_CMD_SHUTDOWN)); 2388 } 2389} 2390 2391void NuPlayer2::performReset() { 2392 ALOGV("performReset"); 2393 2394 CHECK(mAudioDecoder == NULL); 2395 CHECK(mVideoDecoder == NULL); 2396 2397 stopPlaybackTimer("performReset"); 2398 stopRebufferingTimer(true); 2399 2400 cancelPollDuration(); 2401 2402 ++mScanSourcesGeneration; 2403 mScanSourcesPending = false; 2404 2405 if (mRendererLooper != NULL) { 2406 if (mRenderer != NULL) { 2407 mRendererLooper->unregisterHandler(mRenderer->id()); 2408 } 2409 mRendererLooper->stop(); 2410 mRendererLooper.clear(); 2411 } 2412 mRenderer.clear(); 2413 ++mRendererGeneration; 2414 2415 if (mSource != NULL) { 2416 mSource->stop(); 2417 2418 Mutex::Autolock autoLock(mSourceLock); 2419 mSource.clear(); 2420 } 2421 2422 if (mDriver != NULL) { 2423 sp<NuPlayer2Driver> driver = mDriver.promote(); 2424 if (driver != NULL) { 2425 driver->notifyResetComplete(mSrcId); 2426 } 2427 } 2428 2429 mStarted = false; 2430 mPrepared = false; 2431 mResetting = false; 2432 mSourceStarted = false; 2433 2434 // Modular DRM 2435 if (mCrypto != NULL) { 2436 // decoders will be flushed before this so their mCrypto would go away on their own 2437 // TODO change to ALOGV 2438 ALOGD("performReset mCrypto: %p", mCrypto.get()); 2439 mCrypto.clear(); 2440 } 2441 mIsDrmProtected = false; 2442} 2443 2444void NuPlayer2::performPlayNextDataSource() { 2445 ALOGV("performPlayNextDataSource"); 2446 2447 CHECK(mAudioDecoder == NULL); 2448 CHECK(mVideoDecoder == NULL); 2449 2450 stopPlaybackTimer("performPlayNextDataSource"); 2451 stopRebufferingTimer(true); 2452 2453 cancelPollDuration(); 2454 2455 ++mScanSourcesGeneration; 2456 mScanSourcesPending = false; 2457 2458 ++mRendererGeneration; 2459 2460 if (mSource != NULL) { 2461 mSource->stop(); 2462 } 2463 2464 long previousSrcId; 2465 { 2466 Mutex::Autolock autoLock(mSourceLock); 2467 mSource = mNextSource; 2468 mNextSource = NULL; 2469 previousSrcId = mSrcId; 2470 mSrcId = mNextSrcId; 2471 ++mNextSrcId; // to distinguish the two sources. 2472 } 2473 2474 if (mDriver != NULL) { 2475 sp<NuPlayer2Driver> driver = mDriver.promote(); 2476 if (driver != NULL) { 2477 notifyListener(previousSrcId, MEDIA2_INFO, MEDIA2_INFO_PLAYBACK_COMPLETE, 0); 2478 notifyListener(mSrcId, MEDIA2_INFO, MEDIA2_INFO_STARTED_AS_NEXT, 0); 2479 } 2480 } 2481 2482 mStarted = false; 2483 mPrepared = true; // TODO: what if it's not prepared 2484 mResetting = false; 2485 mSourceStarted = false; 2486 2487 // Modular DRM 2488 if (mCrypto != NULL) { 2489 // decoders will be flushed before this so their mCrypto would go away on their own 2490 // TODO change to ALOGV 2491 ALOGD("performReset mCrypto: %p", mCrypto.get()); 2492 mCrypto.clear(); 2493 } 2494 mIsDrmProtected = false; 2495 2496 if (mRenderer != NULL) { 2497 mRenderer->resume(); 2498 } 2499 2500 onStart(); 2501 mPausedByClient = false; 2502 notifyListener(mSrcId, MEDIA2_STARTED, 0, 0); 2503} 2504 2505void NuPlayer2::performScanSources() { 2506 ALOGV("performScanSources"); 2507 2508 if (!mStarted) { 2509 return; 2510 } 2511 2512 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 2513 postScanSources(); 2514 } 2515} 2516 2517void NuPlayer2::performSetSurface(const sp<ANativeWindowWrapper> &nww) { 2518 ALOGV("performSetSurface"); 2519 2520 mNativeWindow = nww; 2521 2522 // XXX - ignore error from setVideoScalingMode for now 2523 setVideoScalingMode(mVideoScalingMode); 2524 2525 if (mDriver != NULL) { 2526 sp<NuPlayer2Driver> driver = mDriver.promote(); 2527 if (driver != NULL) { 2528 driver->notifySetSurfaceComplete(mSrcId); 2529 } 2530 } 2531} 2532 2533void NuPlayer2::performResumeDecoders(bool needNotify) { 2534 if (needNotify) { 2535 mResumePending = true; 2536 if (mVideoDecoder == NULL) { 2537 // if audio-only, we can notify seek complete now, 2538 // as the resume operation will be relatively fast. 2539 finishResume(); 2540 } 2541 } 2542 2543 if (mVideoDecoder != NULL) { 2544 // When there is continuous seek, MediaPlayer will cache the seek 2545 // position, and send down new seek request when previous seek is 2546 // complete. Let's wait for at least one video output frame before 2547 // notifying seek complete, so that the video thumbnail gets updated 2548 // when seekbar is dragged. 2549 mVideoDecoder->signalResume(needNotify); 2550 } 2551 2552 if (mAudioDecoder != NULL) { 2553 mAudioDecoder->signalResume(false /* needNotify */); 2554 } 2555} 2556 2557void NuPlayer2::finishResume() { 2558 if (mResumePending) { 2559 mResumePending = false; 2560 notifyDriverSeekComplete(mSrcId); 2561 } 2562} 2563 2564void NuPlayer2::notifyDriverSeekComplete(int64_t srcId) { 2565 if (mDriver != NULL) { 2566 sp<NuPlayer2Driver> driver = mDriver.promote(); 2567 if (driver != NULL) { 2568 driver->notifySeekComplete(srcId); 2569 } 2570 } 2571} 2572 2573void NuPlayer2::onSourceNotify(const sp<AMessage> &msg) { 2574 int32_t what; 2575 CHECK(msg->findInt32("what", &what)); 2576 2577 int64_t srcId; 2578 CHECK(msg->findInt64("srcId", &srcId)); 2579 switch (what) { 2580 case Source::kWhatPrepared: 2581 { 2582 ALOGV("NuPlayer2::onSourceNotify Source::kWhatPrepared source: %p", mSource.get()); 2583 if (mSource == NULL) { 2584 // This is a stale notification from a source that was 2585 // asynchronously preparing when the client called reset(). 2586 // We handled the reset, the source is gone. 2587 break; 2588 } 2589 2590 int32_t err; 2591 CHECK(msg->findInt32("err", &err)); 2592 2593 if (err != OK) { 2594 // shut down potential secure codecs in case client never calls reset 2595 mDeferredActions.push_back( 2596 new FlushDecoderAction(FLUSH_CMD_SHUTDOWN /* audio */, 2597 FLUSH_CMD_SHUTDOWN /* video */)); 2598 processDeferredActions(); 2599 } else { 2600 mPrepared = true; 2601 } 2602 2603 sp<NuPlayer2Driver> driver = mDriver.promote(); 2604 if (driver != NULL) { 2605 // notify duration first, so that it's definitely set when 2606 // the app received the "prepare complete" callback. 2607 int64_t durationUs; 2608 if (mSource->getDuration(&durationUs) == OK) { 2609 driver->notifyDuration(srcId, durationUs); 2610 } 2611 driver->notifyPrepareCompleted(srcId, err); 2612 } 2613 2614 break; 2615 } 2616 2617 // Modular DRM 2618 case Source::kWhatDrmInfo: 2619 { 2620 Parcel parcel; 2621 sp<ABuffer> drmInfo; 2622 CHECK(msg->findBuffer("drmInfo", &drmInfo)); 2623 parcel.setData(drmInfo->data(), drmInfo->size()); 2624 2625 ALOGV("onSourceNotify() kWhatDrmInfo MEDIA2_DRM_INFO drmInfo: %p parcel size: %zu", 2626 drmInfo.get(), parcel.dataSize()); 2627 2628 notifyListener(srcId, MEDIA2_DRM_INFO, 0 /* ext1 */, 0 /* ext2 */, &parcel); 2629 2630 break; 2631 } 2632 2633 case Source::kWhatFlagsChanged: 2634 { 2635 uint32_t flags; 2636 CHECK(msg->findInt32("flags", (int32_t *)&flags)); 2637 2638 sp<NuPlayer2Driver> driver = mDriver.promote(); 2639 if (driver != NULL) { 2640 2641 ALOGV("onSourceNotify() kWhatFlagsChanged FLAG_CAN_PAUSE: %d " 2642 "FLAG_CAN_SEEK_BACKWARD: %d \n\t\t\t\t FLAG_CAN_SEEK_FORWARD: %d " 2643 "FLAG_CAN_SEEK: %d FLAG_DYNAMIC_DURATION: %d \n" 2644 "\t\t\t\t FLAG_SECURE: %d FLAG_PROTECTED: %d", 2645 (flags & Source::FLAG_CAN_PAUSE) != 0, 2646 (flags & Source::FLAG_CAN_SEEK_BACKWARD) != 0, 2647 (flags & Source::FLAG_CAN_SEEK_FORWARD) != 0, 2648 (flags & Source::FLAG_CAN_SEEK) != 0, 2649 (flags & Source::FLAG_DYNAMIC_DURATION) != 0, 2650 (flags & Source::FLAG_SECURE) != 0, 2651 (flags & Source::FLAG_PROTECTED) != 0); 2652 2653 if ((flags & NuPlayer2::Source::FLAG_CAN_SEEK) == 0) { 2654 driver->notifyListener( 2655 srcId, MEDIA2_INFO, MEDIA2_INFO_NOT_SEEKABLE, 0); 2656 } 2657 driver->notifyFlagsChanged(srcId, flags); 2658 } 2659 2660 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2661 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) { 2662 cancelPollDuration(); 2663 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 2664 && (flags & Source::FLAG_DYNAMIC_DURATION) 2665 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 2666 schedulePollDuration(); 2667 } 2668 2669 mSourceFlags = flags; 2670 break; 2671 } 2672 2673 case Source::kWhatVideoSizeChanged: 2674 { 2675 sp<AMessage> format; 2676 CHECK(msg->findMessage("format", &format)); 2677 2678 updateVideoSize(srcId, format); 2679 break; 2680 } 2681 2682 case Source::kWhatBufferingUpdate: 2683 { 2684 int32_t percentage; 2685 CHECK(msg->findInt32("percentage", &percentage)); 2686 2687 notifyListener(srcId, MEDIA2_BUFFERING_UPDATE, percentage, 0); 2688 break; 2689 } 2690 2691 case Source::kWhatPauseOnBufferingStart: 2692 { 2693 // ignore if not playing 2694 if (mStarted) { 2695 ALOGI("buffer low, pausing..."); 2696 2697 startRebufferingTimer(); 2698 mPausedForBuffering = true; 2699 onPause(); 2700 } 2701 notifyListener(srcId, MEDIA2_INFO, MEDIA2_INFO_BUFFERING_START, 0); 2702 break; 2703 } 2704 2705 case Source::kWhatResumeOnBufferingEnd: 2706 { 2707 // ignore if not playing 2708 if (mStarted) { 2709 ALOGI("buffer ready, resuming..."); 2710 2711 stopRebufferingTimer(false); 2712 mPausedForBuffering = false; 2713 2714 // do not resume yet if client didn't unpause 2715 if (!mPausedByClient) { 2716 onResume(); 2717 } 2718 } 2719 notifyListener(srcId, MEDIA2_INFO, MEDIA2_INFO_BUFFERING_END, 0); 2720 break; 2721 } 2722 2723 case Source::kWhatCacheStats: 2724 { 2725 int32_t kbps; 2726 CHECK(msg->findInt32("bandwidth", &kbps)); 2727 2728 notifyListener(srcId, MEDIA2_INFO, MEDIA2_INFO_NETWORK_BANDWIDTH, kbps); 2729 break; 2730 } 2731 2732 case Source::kWhatSubtitleData: 2733 { 2734 sp<ABuffer> buffer; 2735 CHECK(msg->findBuffer("buffer", &buffer)); 2736 2737 sendSubtitleData(buffer, 0 /* baseIndex */); 2738 break; 2739 } 2740 2741 case Source::kWhatTimedMetaData: 2742 { 2743 sp<ABuffer> buffer; 2744 if (!msg->findBuffer("buffer", &buffer)) { 2745 notifyListener(srcId, MEDIA2_INFO, MEDIA2_INFO_METADATA_UPDATE, 0); 2746 } else { 2747 sendTimedMetaData(buffer); 2748 } 2749 break; 2750 } 2751 2752 case Source::kWhatTimedTextData: 2753 { 2754 int32_t generation; 2755 if (msg->findInt32("generation", &generation) 2756 && generation != mTimedTextGeneration) { 2757 break; 2758 } 2759 2760 sp<ABuffer> buffer; 2761 CHECK(msg->findBuffer("buffer", &buffer)); 2762 2763 sp<NuPlayer2Driver> driver = mDriver.promote(); 2764 if (driver == NULL) { 2765 break; 2766 } 2767 2768 int64_t posMs; 2769 int64_t timeUs, posUs; 2770 driver->getCurrentPosition(&posMs); 2771 posUs = posMs * 1000ll; 2772 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2773 2774 if (posUs < timeUs) { 2775 if (!msg->findInt32("generation", &generation)) { 2776 msg->setInt32("generation", mTimedTextGeneration); 2777 } 2778 msg->post(timeUs - posUs); 2779 } else { 2780 sendTimedTextData(buffer); 2781 } 2782 break; 2783 } 2784 2785 case Source::kWhatQueueDecoderShutdown: 2786 { 2787 int32_t audio, video; 2788 CHECK(msg->findInt32("audio", &audio)); 2789 CHECK(msg->findInt32("video", &video)); 2790 2791 sp<AMessage> reply; 2792 CHECK(msg->findMessage("reply", &reply)); 2793 2794 queueDecoderShutdown(audio, video, reply); 2795 break; 2796 } 2797 2798 case Source::kWhatDrmNoLicense: 2799 { 2800 notifyListener(srcId, MEDIA2_ERROR, MEDIA2_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE); 2801 break; 2802 } 2803 2804 default: 2805 TRESPASS(); 2806 } 2807} 2808 2809void NuPlayer2::onClosedCaptionNotify(const sp<AMessage> &msg) { 2810 int32_t what; 2811 CHECK(msg->findInt32("what", &what)); 2812 2813 switch (what) { 2814 case NuPlayer2::CCDecoder::kWhatClosedCaptionData: 2815 { 2816 sp<ABuffer> buffer; 2817 CHECK(msg->findBuffer("buffer", &buffer)); 2818 2819 size_t inbandTracks = 0; 2820 if (mSource != NULL) { 2821 inbandTracks = mSource->getTrackCount(); 2822 } 2823 2824 sendSubtitleData(buffer, inbandTracks); 2825 break; 2826 } 2827 2828 case NuPlayer2::CCDecoder::kWhatTrackAdded: 2829 { 2830 notifyListener(mSrcId, MEDIA2_INFO, MEDIA2_INFO_METADATA_UPDATE, 0); 2831 2832 break; 2833 } 2834 2835 default: 2836 TRESPASS(); 2837 } 2838 2839 2840} 2841 2842void NuPlayer2::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) { 2843 int32_t trackIndex; 2844 int64_t timeUs, durationUs; 2845 CHECK(buffer->meta()->findInt32(AMEDIAFORMAT_KEY_TRACK_INDEX, &trackIndex)); 2846 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2847 CHECK(buffer->meta()->findInt64("durationUs", &durationUs)); 2848 2849 Parcel in; 2850 in.writeInt32(trackIndex + baseIndex); 2851 in.writeInt64(timeUs); 2852 in.writeInt64(durationUs); 2853 in.writeInt32(buffer->size()); 2854 in.writeInt32(buffer->size()); 2855 in.write(buffer->data(), buffer->size()); 2856 2857 notifyListener(mSrcId, MEDIA2_SUBTITLE_DATA, 0, 0, &in); 2858} 2859 2860void NuPlayer2::sendTimedMetaData(const sp<ABuffer> &buffer) { 2861 int64_t timeUs; 2862 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2863 2864 Parcel in; 2865 in.writeInt64(timeUs); 2866 in.writeInt32(buffer->size()); 2867 in.writeInt32(buffer->size()); 2868 in.write(buffer->data(), buffer->size()); 2869 2870 notifyListener(mSrcId, MEDIA2_META_DATA, 0, 0, &in); 2871} 2872 2873void NuPlayer2::sendTimedTextData(const sp<ABuffer> &buffer) { 2874 const void *data; 2875 size_t size = 0; 2876 int64_t timeUs; 2877 int32_t flag = TextDescriptions::IN_BAND_TEXT_3GPP; 2878 2879 AString mime; 2880 CHECK(buffer->meta()->findString("mime", &mime)); 2881 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0); 2882 2883 data = buffer->data(); 2884 size = buffer->size(); 2885 2886 Parcel parcel; 2887 if (size > 0) { 2888 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2889 int32_t global = 0; 2890 if (buffer->meta()->findInt32("global", &global) && global) { 2891 flag |= TextDescriptions::GLOBAL_DESCRIPTIONS; 2892 } else { 2893 flag |= TextDescriptions::LOCAL_DESCRIPTIONS; 2894 } 2895 TextDescriptions::getParcelOfDescriptions( 2896 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel); 2897 } 2898 2899 if ((parcel.dataSize() > 0)) { 2900 notifyListener(mSrcId, MEDIA2_TIMED_TEXT, 0, 0, &parcel); 2901 } else { // send an empty timed text 2902 notifyListener(mSrcId, MEDIA2_TIMED_TEXT, 0, 0); 2903 } 2904} 2905 2906const char *NuPlayer2::getDataSourceType() { 2907 switch (mDataSourceType) { 2908 case DATA_SOURCE_TYPE_HTTP_LIVE: 2909 return "HTTPLive"; 2910 2911 case DATA_SOURCE_TYPE_RTSP: 2912 return "RTSP"; 2913 2914 case DATA_SOURCE_TYPE_GENERIC_URL: 2915 return "GenURL"; 2916 2917 case DATA_SOURCE_TYPE_GENERIC_FD: 2918 return "GenFD"; 2919 2920 case DATA_SOURCE_TYPE_MEDIA: 2921 return "Media"; 2922 2923 case DATA_SOURCE_TYPE_NONE: 2924 default: 2925 return "None"; 2926 } 2927 } 2928 2929// Modular DRM begin 2930status_t NuPlayer2::prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId) 2931{ 2932 ALOGV("prepareDrm "); 2933 2934 // Passing to the looper anyway; called in a pre-config prepared state so no race on mCrypto 2935 sp<AMessage> msg = new AMessage(kWhatPrepareDrm, this); 2936 // synchronous call so just passing the address but with local copies of "const" args 2937 uint8_t UUID[16]; 2938 memcpy(UUID, uuid, sizeof(UUID)); 2939 Vector<uint8_t> sessionId = drmSessionId; 2940 msg->setPointer("uuid", (void*)UUID); 2941 msg->setPointer("drmSessionId", (void*)&sessionId); 2942 2943 sp<AMessage> response; 2944 status_t status = msg->postAndAwaitResponse(&response); 2945 2946 if (status == OK && response != NULL) { 2947 CHECK(response->findInt32("status", &status)); 2948 ALOGV("prepareDrm ret: %d ", status); 2949 } else { 2950 ALOGE("prepareDrm err: %d", status); 2951 } 2952 2953 return status; 2954} 2955 2956status_t NuPlayer2::releaseDrm() 2957{ 2958 ALOGV("releaseDrm "); 2959 2960 sp<AMessage> msg = new AMessage(kWhatReleaseDrm, this); 2961 2962 sp<AMessage> response; 2963 status_t status = msg->postAndAwaitResponse(&response); 2964 2965 if (status == OK && response != NULL) { 2966 CHECK(response->findInt32("status", &status)); 2967 ALOGV("releaseDrm ret: %d ", status); 2968 } else { 2969 ALOGE("releaseDrm err: %d", status); 2970 } 2971 2972 return status; 2973} 2974 2975status_t NuPlayer2::onPrepareDrm(const sp<AMessage> &msg) 2976{ 2977 // TODO change to ALOGV 2978 ALOGD("onPrepareDrm "); 2979 2980 status_t status = INVALID_OPERATION; 2981 if (mSource == NULL) { 2982 ALOGE("onPrepareDrm: No source. onPrepareDrm failed with %d.", status); 2983 return status; 2984 } 2985 2986 uint8_t *uuid; 2987 Vector<uint8_t> *drmSessionId; 2988 CHECK(msg->findPointer("uuid", (void**)&uuid)); 2989 CHECK(msg->findPointer("drmSessionId", (void**)&drmSessionId)); 2990 2991 status = OK; 2992 sp<AMediaCryptoWrapper> crypto = NULL; 2993 2994 status = mSource->prepareDrm(uuid, *drmSessionId, &crypto); 2995 if (crypto == NULL) { 2996 ALOGE("onPrepareDrm: mSource->prepareDrm failed. status: %d", status); 2997 return status; 2998 } 2999 ALOGV("onPrepareDrm: mSource->prepareDrm succeeded"); 3000 3001 if (mCrypto != NULL) { 3002 ALOGE("onPrepareDrm: Unexpected. Already having mCrypto: %p", mCrypto.get()); 3003 mCrypto.clear(); 3004 } 3005 3006 mCrypto = crypto; 3007 mIsDrmProtected = true; 3008 // TODO change to ALOGV 3009 ALOGD("onPrepareDrm: mCrypto: %p", mCrypto.get()); 3010 3011 return status; 3012} 3013 3014status_t NuPlayer2::onReleaseDrm() 3015{ 3016 // TODO change to ALOGV 3017 ALOGD("onReleaseDrm "); 3018 3019 if (!mIsDrmProtected) { 3020 ALOGW("onReleaseDrm: Unexpected. mIsDrmProtected is already false."); 3021 } 3022 3023 mIsDrmProtected = false; 3024 3025 status_t status; 3026 if (mCrypto != NULL) { 3027 // notifying the source first before removing crypto from codec 3028 if (mSource != NULL) { 3029 mSource->releaseDrm(); 3030 } 3031 3032 status=OK; 3033 // first making sure the codecs have released their crypto reference 3034 const sp<DecoderBase> &videoDecoder = getDecoder(false/*audio*/); 3035 if (videoDecoder != NULL) { 3036 status = videoDecoder->releaseCrypto(); 3037 ALOGV("onReleaseDrm: video decoder ret: %d", status); 3038 } 3039 3040 const sp<DecoderBase> &audioDecoder = getDecoder(true/*audio*/); 3041 if (audioDecoder != NULL) { 3042 status_t status_audio = audioDecoder->releaseCrypto(); 3043 if (status == OK) { // otherwise, returning the first error 3044 status = status_audio; 3045 } 3046 ALOGV("onReleaseDrm: audio decoder ret: %d", status_audio); 3047 } 3048 3049 // TODO change to ALOGV 3050 ALOGD("onReleaseDrm: mCrypto: %p", mCrypto.get()); 3051 mCrypto.clear(); 3052 } else { // mCrypto == NULL 3053 ALOGE("onReleaseDrm: Unexpected. There is no crypto."); 3054 status = INVALID_OPERATION; 3055 } 3056 3057 return status; 3058} 3059// Modular DRM end 3060//////////////////////////////////////////////////////////////////////////////// 3061 3062sp<AMessage> NuPlayer2::Source::getFormat(bool audio) { 3063 sp<MetaData> meta = getFormatMeta(audio); 3064 3065 if (meta == NULL) { 3066 return NULL; 3067 } 3068 3069 sp<AMessage> msg = new AMessage; 3070 3071 if(convertMetaDataToMessage(meta, &msg) == OK) { 3072 return msg; 3073 } 3074 return NULL; 3075} 3076 3077void NuPlayer2::Source::notifyFlagsChanged(uint32_t flags) { 3078 sp<AMessage> notify = dupNotify(); 3079 notify->setInt32("what", kWhatFlagsChanged); 3080 notify->setInt32("flags", flags); 3081 notify->post(); 3082} 3083 3084void NuPlayer2::Source::notifyVideoSizeChanged(const sp<AMessage> &format) { 3085 sp<AMessage> notify = dupNotify(); 3086 notify->setInt32("what", kWhatVideoSizeChanged); 3087 notify->setMessage("format", format); 3088 notify->post(); 3089} 3090 3091void NuPlayer2::Source::notifyPrepared(status_t err) { 3092 ALOGV("Source::notifyPrepared %d", err); 3093 sp<AMessage> notify = dupNotify(); 3094 notify->setInt32("what", kWhatPrepared); 3095 notify->setInt32("err", err); 3096 notify->post(); 3097} 3098 3099void NuPlayer2::Source::notifyDrmInfo(const sp<ABuffer> &drmInfoBuffer) 3100{ 3101 ALOGV("Source::notifyDrmInfo"); 3102 3103 sp<AMessage> notify = dupNotify(); 3104 notify->setInt32("what", kWhatDrmInfo); 3105 notify->setBuffer("drmInfo", drmInfoBuffer); 3106 3107 notify->post(); 3108} 3109 3110void NuPlayer2::Source::onMessageReceived(const sp<AMessage> & /* msg */) { 3111 TRESPASS(); 3112} 3113 3114} // namespace android 3115