NuPlayer.cpp revision e427abf1ea252ff305fc33aacdd2e83cf34891b5
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 "NuPlayerDecoder.h" 25#include "NuPlayerDecoderPassThrough.h" 26#include "NuPlayerDriver.h" 27#include "NuPlayerRenderer.h" 28#include "NuPlayerSource.h" 29#include "RTSPSource.h" 30#include "StreamingSource.h" 31#include "GenericSource.h" 32#include "TextDescriptions.h" 33 34#include "ATSParser.h" 35 36#include <media/stagefright/foundation/hexdump.h> 37#include <media/stagefright/foundation/ABuffer.h> 38#include <media/stagefright/foundation/ADebug.h> 39#include <media/stagefright/foundation/AMessage.h> 40#include <media/stagefright/MediaBuffer.h> 41#include <media/stagefright/MediaDefs.h> 42#include <media/stagefright/MediaErrors.h> 43#include <media/stagefright/MetaData.h> 44#include <gui/IGraphicBufferProducer.h> 45 46#include "avc_utils.h" 47 48#include "ESDS.h" 49#include <media/stagefright/Utils.h> 50 51namespace android { 52 53// TODO optimize buffer size for power consumption 54// The offload read buffer size is 32 KB but 24 KB uses less power. 55const size_t NuPlayer::kAggregateBufferSizeBytes = 24 * 1024; 56 57struct NuPlayer::Action : public RefBase { 58 Action() {} 59 60 virtual void execute(NuPlayer *player) = 0; 61 62private: 63 DISALLOW_EVIL_CONSTRUCTORS(Action); 64}; 65 66struct NuPlayer::SeekAction : public Action { 67 SeekAction(int64_t seekTimeUs, bool needNotify) 68 : mSeekTimeUs(seekTimeUs), 69 mNeedNotify(needNotify) { 70 } 71 72 virtual void execute(NuPlayer *player) { 73 player->performSeek(mSeekTimeUs, mNeedNotify); 74 } 75 76private: 77 int64_t mSeekTimeUs; 78 bool mNeedNotify; 79 80 DISALLOW_EVIL_CONSTRUCTORS(SeekAction); 81}; 82 83struct NuPlayer::SetSurfaceAction : public Action { 84 SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper) 85 : mWrapper(wrapper) { 86 } 87 88 virtual void execute(NuPlayer *player) { 89 player->performSetSurface(mWrapper); 90 } 91 92private: 93 sp<NativeWindowWrapper> mWrapper; 94 95 DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction); 96}; 97 98struct NuPlayer::ShutdownDecoderAction : public Action { 99 ShutdownDecoderAction(bool audio, bool video) 100 : mAudio(audio), 101 mVideo(video) { 102 } 103 104 virtual void execute(NuPlayer *player) { 105 player->performDecoderShutdown(mAudio, mVideo); 106 } 107 108private: 109 bool mAudio; 110 bool mVideo; 111 112 DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction); 113}; 114 115struct NuPlayer::PostMessageAction : public Action { 116 PostMessageAction(const sp<AMessage> &msg) 117 : mMessage(msg) { 118 } 119 120 virtual void execute(NuPlayer *) { 121 mMessage->post(); 122 } 123 124private: 125 sp<AMessage> mMessage; 126 127 DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction); 128}; 129 130// Use this if there's no state necessary to save in order to execute 131// the action. 132struct NuPlayer::SimpleAction : public Action { 133 typedef void (NuPlayer::*ActionFunc)(); 134 135 SimpleAction(ActionFunc func) 136 : mFunc(func) { 137 } 138 139 virtual void execute(NuPlayer *player) { 140 (player->*mFunc)(); 141 } 142 143private: 144 ActionFunc mFunc; 145 146 DISALLOW_EVIL_CONSTRUCTORS(SimpleAction); 147}; 148 149//////////////////////////////////////////////////////////////////////////////// 150 151NuPlayer::NuPlayer() 152 : mUIDValid(false), 153 mSourceFlags(0), 154 mCurrentPositionUs(0), 155 mVideoIsAVC(false), 156 mOffloadAudio(false), 157 mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER), 158 mAudioDecoderGeneration(0), 159 mVideoDecoderGeneration(0), 160 mAudioEOS(false), 161 mVideoEOS(false), 162 mScanSourcesPending(false), 163 mScanSourcesGeneration(0), 164 mPollDurationGeneration(0), 165 mTimedTextGeneration(0), 166 mTimeDiscontinuityPending(false), 167 mFlushingAudio(NONE), 168 mFlushingVideo(NONE), 169 mSkipRenderingAudioUntilMediaTimeUs(-1ll), 170 mSkipRenderingVideoUntilMediaTimeUs(-1ll), 171 mVideoLateByUs(0ll), 172 mNumFramesTotal(0ll), 173 mNumFramesDropped(0ll), 174 mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW), 175 mStarted(false) { 176} 177 178NuPlayer::~NuPlayer() { 179} 180 181void NuPlayer::setUID(uid_t uid) { 182 mUIDValid = true; 183 mUID = uid; 184} 185 186void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) { 187 mDriver = driver; 188} 189 190void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) { 191 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); 192 193 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id()); 194 195 msg->setObject("source", new StreamingSource(notify, source)); 196 msg->post(); 197} 198 199static bool IsHTTPLiveURL(const char *url) { 200 if (!strncasecmp("http://", url, 7) 201 || !strncasecmp("https://", url, 8) 202 || !strncasecmp("file://", url, 7)) { 203 size_t len = strlen(url); 204 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) { 205 return true; 206 } 207 208 if (strstr(url,"m3u8")) { 209 return true; 210 } 211 } 212 213 return false; 214} 215 216void NuPlayer::setDataSourceAsync( 217 const sp<IMediaHTTPService> &httpService, 218 const char *url, 219 const KeyedVector<String8, String8> *headers) { 220 221 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); 222 size_t len = strlen(url); 223 224 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id()); 225 226 sp<Source> source; 227 if (IsHTTPLiveURL(url)) { 228 source = new HTTPLiveSource(notify, httpService, url, headers); 229 } else if (!strncasecmp(url, "rtsp://", 7)) { 230 source = new RTSPSource( 231 notify, httpService, url, headers, mUIDValid, mUID); 232 } else if ((!strncasecmp(url, "http://", 7) 233 || !strncasecmp(url, "https://", 8)) 234 && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4])) 235 || strstr(url, ".sdp?"))) { 236 source = new RTSPSource( 237 notify, httpService, url, headers, mUIDValid, mUID, true); 238 } else { 239 sp<GenericSource> genericSource = 240 new GenericSource(notify, mUIDValid, mUID); 241 // Don't set FLAG_SECURE on mSourceFlags here for widevine. 242 // The correct flags will be updated in Source::kWhatFlagsChanged 243 // handler when GenericSource is prepared. 244 245 status_t err = genericSource->setDataSource(httpService, url, headers); 246 247 if (err == OK) { 248 source = genericSource; 249 } else { 250 ALOGE("Failed to set data source!"); 251 } 252 } 253 msg->setObject("source", source); 254 msg->post(); 255} 256 257void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) { 258 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); 259 260 sp<AMessage> notify = new AMessage(kWhatSourceNotify, id()); 261 262 sp<GenericSource> source = 263 new GenericSource(notify, mUIDValid, mUID); 264 265 status_t err = source->setDataSource(fd, offset, length); 266 267 if (err != OK) { 268 ALOGE("Failed to set data source!"); 269 source = NULL; 270 } 271 272 msg->setObject("source", source); 273 msg->post(); 274} 275 276void NuPlayer::prepareAsync() { 277 (new AMessage(kWhatPrepare, id()))->post(); 278} 279 280void NuPlayer::setVideoSurfaceTextureAsync( 281 const sp<IGraphicBufferProducer> &bufferProducer) { 282 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id()); 283 284 if (bufferProducer == NULL) { 285 msg->setObject("native-window", NULL); 286 } else { 287 msg->setObject( 288 "native-window", 289 new NativeWindowWrapper( 290 new Surface(bufferProducer, true /* controlledByApp */))); 291 } 292 293 msg->post(); 294} 295 296void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) { 297 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id()); 298 msg->setObject("sink", sink); 299 msg->post(); 300} 301 302void NuPlayer::start() { 303 (new AMessage(kWhatStart, id()))->post(); 304} 305 306void NuPlayer::pause() { 307 (new AMessage(kWhatPause, id()))->post(); 308} 309 310void NuPlayer::resume() { 311 (new AMessage(kWhatResume, id()))->post(); 312} 313 314void NuPlayer::resetAsync() { 315 if (mSource != NULL) { 316 // During a reset, the data source might be unresponsive already, we need to 317 // disconnect explicitly so that reads exit promptly. 318 // We can't queue the disconnect request to the looper, as it might be 319 // queued behind a stuck read and never gets processed. 320 // Doing a disconnect outside the looper to allows the pending reads to exit 321 // (either successfully or with error). 322 mSource->disconnect(); 323 } 324 325 (new AMessage(kWhatReset, id()))->post(); 326} 327 328void NuPlayer::seekToAsync(int64_t seekTimeUs, bool needNotify) { 329 sp<AMessage> msg = new AMessage(kWhatSeek, id()); 330 msg->setInt64("seekTimeUs", seekTimeUs); 331 msg->setInt32("needNotify", needNotify); 332 msg->post(); 333} 334 335// static 336bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) { 337 switch (state) { 338 case FLUSHING_DECODER: 339 if (needShutdown != NULL) { 340 *needShutdown = false; 341 } 342 return true; 343 344 case FLUSHING_DECODER_SHUTDOWN: 345 if (needShutdown != NULL) { 346 *needShutdown = true; 347 } 348 return true; 349 350 default: 351 return false; 352 } 353} 354 355void NuPlayer::writeTrackInfo( 356 Parcel* reply, const sp<AMessage> format) const { 357 int32_t trackType; 358 CHECK(format->findInt32("type", &trackType)); 359 360 AString lang; 361 CHECK(format->findString("language", &lang)); 362 363 reply->writeInt32(2); // write something non-zero 364 reply->writeInt32(trackType); 365 reply->writeString16(String16(lang.c_str())); 366 367 if (trackType == MEDIA_TRACK_TYPE_SUBTITLE) { 368 AString mime; 369 CHECK(format->findString("mime", &mime)); 370 371 int32_t isAuto, isDefault, isForced; 372 CHECK(format->findInt32("auto", &isAuto)); 373 CHECK(format->findInt32("default", &isDefault)); 374 CHECK(format->findInt32("forced", &isForced)); 375 376 reply->writeString16(String16(mime.c_str())); 377 reply->writeInt32(isAuto); 378 reply->writeInt32(isDefault); 379 reply->writeInt32(isForced); 380 } 381} 382 383void NuPlayer::onMessageReceived(const sp<AMessage> &msg) { 384 switch (msg->what()) { 385 case kWhatSetDataSource: 386 { 387 ALOGV("kWhatSetDataSource"); 388 389 CHECK(mSource == NULL); 390 391 status_t err = OK; 392 sp<RefBase> obj; 393 CHECK(msg->findObject("source", &obj)); 394 if (obj != NULL) { 395 mSource = static_cast<Source *>(obj.get()); 396 } else { 397 err = UNKNOWN_ERROR; 398 } 399 400 CHECK(mDriver != NULL); 401 sp<NuPlayerDriver> driver = mDriver.promote(); 402 if (driver != NULL) { 403 driver->notifySetDataSourceCompleted(err); 404 } 405 break; 406 } 407 408 case kWhatPrepare: 409 { 410 mSource->prepareAsync(); 411 break; 412 } 413 414 case kWhatGetTrackInfo: 415 { 416 uint32_t replyID; 417 CHECK(msg->senderAwaitsResponse(&replyID)); 418 419 Parcel* reply; 420 CHECK(msg->findPointer("reply", (void**)&reply)); 421 422 size_t inbandTracks = 0; 423 if (mSource != NULL) { 424 inbandTracks = mSource->getTrackCount(); 425 } 426 427 size_t ccTracks = 0; 428 if (mCCDecoder != NULL) { 429 ccTracks = mCCDecoder->getTrackCount(); 430 } 431 432 // total track count 433 reply->writeInt32(inbandTracks + ccTracks); 434 435 // write inband tracks 436 for (size_t i = 0; i < inbandTracks; ++i) { 437 writeTrackInfo(reply, mSource->getTrackInfo(i)); 438 } 439 440 // write CC track 441 for (size_t i = 0; i < ccTracks; ++i) { 442 writeTrackInfo(reply, mCCDecoder->getTrackInfo(i)); 443 } 444 445 sp<AMessage> response = new AMessage; 446 response->postReply(replyID); 447 break; 448 } 449 450 case kWhatGetSelectedTrack: 451 { 452 status_t err = INVALID_OPERATION; 453 if (mSource != NULL) { 454 err = OK; 455 456 int32_t type32; 457 CHECK(msg->findInt32("type", (int32_t*)&type32)); 458 media_track_type type = (media_track_type)type32; 459 ssize_t selectedTrack = mSource->getSelectedTrack(type); 460 461 Parcel* reply; 462 CHECK(msg->findPointer("reply", (void**)&reply)); 463 reply->writeInt32(selectedTrack); 464 } 465 466 sp<AMessage> response = new AMessage; 467 response->setInt32("err", err); 468 469 uint32_t replyID; 470 CHECK(msg->senderAwaitsResponse(&replyID)); 471 response->postReply(replyID); 472 break; 473 } 474 475 case kWhatSelectTrack: 476 { 477 uint32_t replyID; 478 CHECK(msg->senderAwaitsResponse(&replyID)); 479 480 size_t trackIndex; 481 int32_t select; 482 CHECK(msg->findSize("trackIndex", &trackIndex)); 483 CHECK(msg->findInt32("select", &select)); 484 485 status_t err = INVALID_OPERATION; 486 487 size_t inbandTracks = 0; 488 if (mSource != NULL) { 489 inbandTracks = mSource->getTrackCount(); 490 } 491 size_t ccTracks = 0; 492 if (mCCDecoder != NULL) { 493 ccTracks = mCCDecoder->getTrackCount(); 494 } 495 496 if (trackIndex < inbandTracks) { 497 err = mSource->selectTrack(trackIndex, select); 498 499 if (!select && err == OK) { 500 int32_t type; 501 sp<AMessage> info = mSource->getTrackInfo(trackIndex); 502 if (info != NULL 503 && info->findInt32("type", &type) 504 && type == MEDIA_TRACK_TYPE_TIMEDTEXT) { 505 ++mTimedTextGeneration; 506 } 507 } 508 } else { 509 trackIndex -= inbandTracks; 510 511 if (trackIndex < ccTracks) { 512 err = mCCDecoder->selectTrack(trackIndex, select); 513 } 514 } 515 516 sp<AMessage> response = new AMessage; 517 response->setInt32("err", err); 518 519 response->postReply(replyID); 520 break; 521 } 522 523 case kWhatPollDuration: 524 { 525 int32_t generation; 526 CHECK(msg->findInt32("generation", &generation)); 527 528 if (generation != mPollDurationGeneration) { 529 // stale 530 break; 531 } 532 533 int64_t durationUs; 534 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) { 535 sp<NuPlayerDriver> driver = mDriver.promote(); 536 if (driver != NULL) { 537 driver->notifyDuration(durationUs); 538 } 539 } 540 541 msg->post(1000000ll); // poll again in a second. 542 break; 543 } 544 545 case kWhatSetVideoNativeWindow: 546 { 547 ALOGV("kWhatSetVideoNativeWindow"); 548 549 mDeferredActions.push_back( 550 new ShutdownDecoderAction( 551 false /* audio */, true /* video */)); 552 553 sp<RefBase> obj; 554 CHECK(msg->findObject("native-window", &obj)); 555 556 mDeferredActions.push_back( 557 new SetSurfaceAction( 558 static_cast<NativeWindowWrapper *>(obj.get()))); 559 560 if (obj != NULL) { 561 if (mStarted && mVideoDecoder != NULL) { 562 // Issue a seek to refresh the video screen only if started otherwise 563 // the extractor may not yet be started and will assert. 564 // If the video decoder is not set (perhaps audio only in this case) 565 // do not perform a seek as it is not needed. 566 mDeferredActions.push_back( 567 new SeekAction(mCurrentPositionUs, false /* needNotify */)); 568 } 569 570 // If there is a new surface texture, instantiate decoders 571 // again if possible. 572 mDeferredActions.push_back( 573 new SimpleAction(&NuPlayer::performScanSources)); 574 } 575 576 processDeferredActions(); 577 break; 578 } 579 580 case kWhatSetAudioSink: 581 { 582 ALOGV("kWhatSetAudioSink"); 583 584 sp<RefBase> obj; 585 CHECK(msg->findObject("sink", &obj)); 586 587 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get()); 588 break; 589 } 590 591 case kWhatStart: 592 { 593 ALOGV("kWhatStart"); 594 595 mVideoIsAVC = false; 596 mOffloadAudio = false; 597 mAudioEOS = false; 598 mVideoEOS = false; 599 mSkipRenderingAudioUntilMediaTimeUs = -1; 600 mSkipRenderingVideoUntilMediaTimeUs = -1; 601 mVideoLateByUs = 0; 602 mNumFramesTotal = 0; 603 mNumFramesDropped = 0; 604 mStarted = true; 605 606 /* instantiate decoders now for secure playback */ 607 if (mSourceFlags & Source::FLAG_SECURE) { 608 if (mNativeWindow != NULL) { 609 instantiateDecoder(false, &mVideoDecoder); 610 } 611 612 if (mAudioSink != NULL) { 613 instantiateDecoder(true, &mAudioDecoder); 614 } 615 } 616 617 mSource->start(); 618 619 uint32_t flags = 0; 620 621 if (mSource->isRealTime()) { 622 flags |= Renderer::FLAG_REAL_TIME; 623 } 624 625 sp<MetaData> audioMeta = mSource->getFormatMeta(true /* audio */); 626 audio_stream_type_t streamType = AUDIO_STREAM_MUSIC; 627 if (mAudioSink != NULL) { 628 streamType = mAudioSink->getAudioStreamType(); 629 } 630 631 sp<AMessage> videoFormat = mSource->getFormat(false /* audio */); 632 633 mOffloadAudio = 634 canOffloadStream(audioMeta, (videoFormat != NULL), 635 true /* is_streaming */, streamType); 636 if (mOffloadAudio) { 637 flags |= Renderer::FLAG_OFFLOAD_AUDIO; 638 } 639 640 mRenderer = new Renderer( 641 mAudioSink, 642 new AMessage(kWhatRendererNotify, id()), 643 flags); 644 645 mRendererLooper = new ALooper; 646 mRendererLooper->setName("NuPlayerRenderer"); 647 mRendererLooper->start(false, false, ANDROID_PRIORITY_AUDIO); 648 mRendererLooper->registerHandler(mRenderer); 649 650 sp<MetaData> meta = getFileMeta(); 651 int32_t rate; 652 if (meta != NULL 653 && meta->findInt32(kKeyFrameRate, &rate) && rate > 0) { 654 mRenderer->setVideoFrameRate(rate); 655 } 656 657 postScanSources(); 658 break; 659 } 660 661 case kWhatScanSources: 662 { 663 int32_t generation; 664 CHECK(msg->findInt32("generation", &generation)); 665 if (generation != mScanSourcesGeneration) { 666 // Drop obsolete msg. 667 break; 668 } 669 670 mScanSourcesPending = false; 671 672 ALOGV("scanning sources haveAudio=%d, haveVideo=%d", 673 mAudioDecoder != NULL, mVideoDecoder != NULL); 674 675 bool mHadAnySourcesBefore = 676 (mAudioDecoder != NULL) || (mVideoDecoder != NULL); 677 678 // initialize video before audio because successful initialization of 679 // video may change deep buffer mode of audio. 680 if (mNativeWindow != NULL) { 681 instantiateDecoder(false, &mVideoDecoder); 682 } 683 684 if (mAudioSink != NULL) { 685 if (mOffloadAudio) { 686 // open audio sink early under offload mode. 687 sp<AMessage> format = mSource->getFormat(true /*audio*/); 688 openAudioSink(format, true /*offloadOnly*/); 689 } 690 instantiateDecoder(true, &mAudioDecoder); 691 } 692 693 if (!mHadAnySourcesBefore 694 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 695 // This is the first time we've found anything playable. 696 697 if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) { 698 schedulePollDuration(); 699 } 700 } 701 702 status_t err; 703 if ((err = mSource->feedMoreTSData()) != OK) { 704 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 705 // We're not currently decoding anything (no audio or 706 // video tracks found) and we just ran out of input data. 707 708 if (err == ERROR_END_OF_STREAM) { 709 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 710 } else { 711 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 712 } 713 } 714 break; 715 } 716 717 if ((mAudioDecoder == NULL && mAudioSink != NULL) 718 || (mVideoDecoder == NULL && mNativeWindow != NULL)) { 719 msg->post(100000ll); 720 mScanSourcesPending = true; 721 } 722 break; 723 } 724 725 case kWhatVideoNotify: 726 case kWhatAudioNotify: 727 { 728 bool audio = msg->what() == kWhatAudioNotify; 729 730 int32_t currentDecoderGeneration = 731 (audio? mAudioDecoderGeneration : mVideoDecoderGeneration); 732 int32_t requesterGeneration = currentDecoderGeneration - 1; 733 CHECK(msg->findInt32("generation", &requesterGeneration)); 734 735 if (requesterGeneration != currentDecoderGeneration) { 736 ALOGV("got message from old %s decoder, generation(%d:%d)", 737 audio ? "audio" : "video", requesterGeneration, 738 currentDecoderGeneration); 739 sp<AMessage> reply; 740 if (!(msg->findMessage("reply", &reply))) { 741 return; 742 } 743 744 reply->setInt32("err", INFO_DISCONTINUITY); 745 reply->post(); 746 return; 747 } 748 749 int32_t what; 750 CHECK(msg->findInt32("what", &what)); 751 752 if (what == Decoder::kWhatFillThisBuffer) { 753 status_t err = feedDecoderInputData( 754 audio, msg); 755 756 if (err == -EWOULDBLOCK) { 757 if (mSource->feedMoreTSData() == OK) { 758 msg->post(10 * 1000ll); 759 } 760 } 761 } else if (what == Decoder::kWhatEOS) { 762 int32_t err; 763 CHECK(msg->findInt32("err", &err)); 764 765 if (err == ERROR_END_OF_STREAM) { 766 ALOGV("got %s decoder EOS", audio ? "audio" : "video"); 767 } else { 768 ALOGV("got %s decoder EOS w/ error %d", 769 audio ? "audio" : "video", 770 err); 771 } 772 773 mRenderer->queueEOS(audio, err); 774 } else if (what == Decoder::kWhatFlushCompleted) { 775 bool needShutdown; 776 777 if (audio) { 778 CHECK(IsFlushingState(mFlushingAudio, &needShutdown)); 779 mFlushingAudio = FLUSHED; 780 } else { 781 CHECK(IsFlushingState(mFlushingVideo, &needShutdown)); 782 mFlushingVideo = FLUSHED; 783 784 mVideoLateByUs = 0; 785 } 786 787 ALOGV("decoder %s flush completed", audio ? "audio" : "video"); 788 789 if (needShutdown) { 790 ALOGV("initiating %s decoder shutdown", 791 audio ? "audio" : "video"); 792 793 getDecoder(audio)->initiateShutdown(); 794 795 if (audio) { 796 mFlushingAudio = SHUTTING_DOWN_DECODER; 797 } else { 798 mFlushingVideo = SHUTTING_DOWN_DECODER; 799 } 800 } 801 802 finishFlushIfPossible(); 803 } else if (what == Decoder::kWhatOutputFormatChanged) { 804 sp<AMessage> format; 805 CHECK(msg->findMessage("format", &format)); 806 807 if (audio) { 808 openAudioSink(format, false /*offloadOnly*/); 809 } else { 810 // video 811 sp<AMessage> inputFormat = 812 mSource->getFormat(false /* audio */); 813 814 updateVideoSize(inputFormat, format); 815 } 816 } else if (what == Decoder::kWhatShutdownCompleted) { 817 ALOGV("%s shutdown completed", audio ? "audio" : "video"); 818 if (audio) { 819 mAudioDecoder.clear(); 820 821 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER); 822 mFlushingAudio = SHUT_DOWN; 823 } else { 824 mVideoDecoder.clear(); 825 826 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER); 827 mFlushingVideo = SHUT_DOWN; 828 } 829 830 finishFlushIfPossible(); 831 } else if (what == Decoder::kWhatError) { 832 ALOGE("Received error from %s decoder, aborting playback.", 833 audio ? "audio" : "video"); 834 835 status_t err; 836 if (!msg->findInt32("err", &err)) { 837 err = UNKNOWN_ERROR; 838 } 839 mRenderer->queueEOS(audio, err); 840 if (audio && mFlushingAudio != NONE) { 841 mAudioDecoder.clear(); 842 mFlushingAudio = SHUT_DOWN; 843 } else if (!audio && mFlushingVideo != NONE){ 844 mVideoDecoder.clear(); 845 mFlushingVideo = SHUT_DOWN; 846 } 847 finishFlushIfPossible(); 848 } else if (what == Decoder::kWhatDrainThisBuffer) { 849 renderBuffer(audio, msg); 850 } else { 851 ALOGV("Unhandled decoder notification %d '%c%c%c%c'.", 852 what, 853 what >> 24, 854 (what >> 16) & 0xff, 855 (what >> 8) & 0xff, 856 what & 0xff); 857 } 858 859 break; 860 } 861 862 case kWhatRendererNotify: 863 { 864 int32_t what; 865 CHECK(msg->findInt32("what", &what)); 866 867 if (what == Renderer::kWhatEOS) { 868 int32_t audio; 869 CHECK(msg->findInt32("audio", &audio)); 870 871 int32_t finalResult; 872 CHECK(msg->findInt32("finalResult", &finalResult)); 873 874 if (audio) { 875 mAudioEOS = true; 876 } else { 877 mVideoEOS = true; 878 } 879 880 if (finalResult == ERROR_END_OF_STREAM) { 881 ALOGV("reached %s EOS", audio ? "audio" : "video"); 882 } else { 883 ALOGE("%s track encountered an error (%d)", 884 audio ? "audio" : "video", finalResult); 885 886 notifyListener( 887 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult); 888 } 889 890 if ((mAudioEOS || mAudioDecoder == NULL) 891 && (mVideoEOS || mVideoDecoder == NULL)) { 892 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 893 } 894 } else if (what == Renderer::kWhatPosition) { 895 int64_t positionUs; 896 CHECK(msg->findInt64("positionUs", &positionUs)); 897 mCurrentPositionUs = positionUs; 898 899 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs)); 900 901 if (mDriver != NULL) { 902 sp<NuPlayerDriver> driver = mDriver.promote(); 903 if (driver != NULL) { 904 driver->notifyPosition(positionUs); 905 906 driver->notifyFrameStats( 907 mNumFramesTotal, mNumFramesDropped); 908 } 909 } 910 } else if (what == Renderer::kWhatFlushComplete) { 911 int32_t audio; 912 CHECK(msg->findInt32("audio", &audio)); 913 914 ALOGV("renderer %s flush completed.", audio ? "audio" : "video"); 915 } else if (what == Renderer::kWhatVideoRenderingStart) { 916 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0); 917 } else if (what == Renderer::kWhatMediaRenderingStart) { 918 ALOGV("media rendering started"); 919 notifyListener(MEDIA_STARTED, 0, 0); 920 } else if (what == Renderer::kWhatAudioOffloadTearDown) { 921 ALOGV("Tear down audio offload, fall back to s/w path"); 922 int64_t positionUs; 923 CHECK(msg->findInt64("positionUs", &positionUs)); 924 closeAudioSink(); 925 mAudioDecoder.clear(); 926 mRenderer->flush(true /* audio */); 927 if (mVideoDecoder != NULL) { 928 mRenderer->flush(false /* audio */); 929 } 930 mRenderer->signalDisableOffloadAudio(); 931 mOffloadAudio = false; 932 933 performSeek(positionUs, false /* needNotify */); 934 instantiateDecoder(true /* audio */, &mAudioDecoder); 935 } 936 break; 937 } 938 939 case kWhatMoreDataQueued: 940 { 941 break; 942 } 943 944 case kWhatReset: 945 { 946 ALOGV("kWhatReset"); 947 948 mDeferredActions.push_back( 949 new ShutdownDecoderAction( 950 true /* audio */, true /* video */)); 951 952 mDeferredActions.push_back( 953 new SimpleAction(&NuPlayer::performReset)); 954 955 processDeferredActions(); 956 break; 957 } 958 959 case kWhatSeek: 960 { 961 int64_t seekTimeUs; 962 int32_t needNotify; 963 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 964 CHECK(msg->findInt32("needNotify", &needNotify)); 965 966 ALOGV("kWhatSeek seekTimeUs=%lld us, needNotify=%d", 967 seekTimeUs, needNotify); 968 969 mDeferredActions.push_back( 970 new SimpleAction(&NuPlayer::performDecoderFlush)); 971 972 mDeferredActions.push_back( 973 new SeekAction(seekTimeUs, needNotify)); 974 975 processDeferredActions(); 976 break; 977 } 978 979 case kWhatPause: 980 { 981 CHECK(mRenderer != NULL); 982 mSource->pause(); 983 mRenderer->pause(); 984 break; 985 } 986 987 case kWhatResume: 988 { 989 CHECK(mRenderer != NULL); 990 mSource->resume(); 991 mRenderer->resume(); 992 break; 993 } 994 995 case kWhatSourceNotify: 996 { 997 onSourceNotify(msg); 998 break; 999 } 1000 1001 case kWhatClosedCaptionNotify: 1002 { 1003 onClosedCaptionNotify(msg); 1004 break; 1005 } 1006 1007 default: 1008 TRESPASS(); 1009 break; 1010 } 1011} 1012 1013void NuPlayer::finishFlushIfPossible() { 1014 if (mFlushingAudio != NONE && mFlushingAudio != FLUSHED 1015 && mFlushingAudio != SHUT_DOWN) { 1016 return; 1017 } 1018 1019 if (mFlushingVideo != NONE && mFlushingVideo != FLUSHED 1020 && mFlushingVideo != SHUT_DOWN) { 1021 return; 1022 } 1023 1024 ALOGV("both audio and video are flushed now."); 1025 1026 mPendingAudioAccessUnit.clear(); 1027 mAggregateBuffer.clear(); 1028 1029 if (mTimeDiscontinuityPending) { 1030 mRenderer->signalTimeDiscontinuity(); 1031 mTimeDiscontinuityPending = false; 1032 } 1033 1034 if (mAudioDecoder != NULL && mFlushingAudio == FLUSHED) { 1035 mAudioDecoder->signalResume(); 1036 } 1037 1038 if (mVideoDecoder != NULL && mFlushingVideo == FLUSHED) { 1039 mVideoDecoder->signalResume(); 1040 } 1041 1042 mFlushingAudio = NONE; 1043 mFlushingVideo = NONE; 1044 1045 processDeferredActions(); 1046} 1047 1048void NuPlayer::postScanSources() { 1049 if (mScanSourcesPending) { 1050 return; 1051 } 1052 1053 sp<AMessage> msg = new AMessage(kWhatScanSources, id()); 1054 msg->setInt32("generation", mScanSourcesGeneration); 1055 msg->post(); 1056 1057 mScanSourcesPending = true; 1058} 1059 1060void NuPlayer::openAudioSink(const sp<AMessage> &format, bool offloadOnly) { 1061 ALOGV("openAudioSink: offloadOnly(%d) mOffloadAudio(%d)", 1062 offloadOnly, mOffloadAudio); 1063 bool audioSinkChanged = false; 1064 1065 int32_t numChannels; 1066 CHECK(format->findInt32("channel-count", &numChannels)); 1067 1068 int32_t channelMask; 1069 if (!format->findInt32("channel-mask", &channelMask)) { 1070 // signal to the AudioSink to derive the mask from count. 1071 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER; 1072 } 1073 1074 int32_t sampleRate; 1075 CHECK(format->findInt32("sample-rate", &sampleRate)); 1076 1077 uint32_t flags; 1078 int64_t durationUs; 1079 // FIXME: we should handle the case where the video decoder 1080 // is created after we receive the format change indication. 1081 // Current code will just make that we select deep buffer 1082 // with video which should not be a problem as it should 1083 // not prevent from keeping A/V sync. 1084 if (mVideoDecoder == NULL && 1085 mSource->getDuration(&durationUs) == OK && 1086 durationUs 1087 > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) { 1088 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER; 1089 } else { 1090 flags = AUDIO_OUTPUT_FLAG_NONE; 1091 } 1092 1093 if (mOffloadAudio) { 1094 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT; 1095 AString mime; 1096 CHECK(format->findString("mime", &mime)); 1097 status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str()); 1098 1099 if (err != OK) { 1100 ALOGE("Couldn't map mime \"%s\" to a valid " 1101 "audio_format", mime.c_str()); 1102 mOffloadAudio = false; 1103 } else { 1104 ALOGV("Mime \"%s\" mapped to audio_format 0x%x", 1105 mime.c_str(), audioFormat); 1106 1107 int avgBitRate = -1; 1108 format->findInt32("bit-rate", &avgBitRate); 1109 1110 int32_t aacProfile = -1; 1111 if (audioFormat == AUDIO_FORMAT_AAC 1112 && format->findInt32("aac-profile", &aacProfile)) { 1113 // Redefine AAC format as per aac profile 1114 mapAACProfileToAudioFormat( 1115 audioFormat, 1116 aacProfile); 1117 } 1118 1119 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER; 1120 offloadInfo.duration_us = -1; 1121 format->findInt64( 1122 "durationUs", &offloadInfo.duration_us); 1123 offloadInfo.sample_rate = sampleRate; 1124 offloadInfo.channel_mask = channelMask; 1125 offloadInfo.format = audioFormat; 1126 offloadInfo.stream_type = AUDIO_STREAM_MUSIC; 1127 offloadInfo.bit_rate = avgBitRate; 1128 offloadInfo.has_video = (mVideoDecoder != NULL); 1129 offloadInfo.is_streaming = true; 1130 1131 if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) { 1132 ALOGV("openAudioSink: no change in offload mode"); 1133 return; // no change from previous configuration, everything ok. 1134 } 1135 ALOGV("openAudioSink: try to open AudioSink in offload mode"); 1136 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD; 1137 flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER; 1138 audioSinkChanged = true; 1139 mAudioSink->close(); 1140 err = mAudioSink->open( 1141 sampleRate, 1142 numChannels, 1143 (audio_channel_mask_t)channelMask, 1144 audioFormat, 1145 8 /* bufferCount */, 1146 &NuPlayer::Renderer::AudioSinkCallback, 1147 mRenderer.get(), 1148 (audio_output_flags_t)flags, 1149 &offloadInfo); 1150 1151 if (err == OK) { 1152 // If the playback is offloaded to h/w, we pass 1153 // the HAL some metadata information. 1154 // We don't want to do this for PCM because it 1155 // will be going through the AudioFlinger mixer 1156 // before reaching the hardware. 1157 sp<MetaData> audioMeta = 1158 mSource->getFormatMeta(true /* audio */); 1159 sendMetaDataToHal(mAudioSink, audioMeta); 1160 mCurrentOffloadInfo = offloadInfo; 1161 err = mAudioSink->start(); 1162 ALOGV_IF(err == OK, "openAudioSink: offload succeeded"); 1163 } 1164 if (err != OK) { 1165 // Clean up, fall back to non offload mode. 1166 mAudioSink->close(); 1167 mRenderer->signalDisableOffloadAudio(); 1168 mOffloadAudio = false; 1169 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER; 1170 ALOGV("openAudioSink: offload failed"); 1171 } 1172 } 1173 } 1174 if (!offloadOnly && !mOffloadAudio) { 1175 flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD; 1176 ALOGV("openAudioSink: open AudioSink in NON-offload mode"); 1177 1178 audioSinkChanged = true; 1179 mAudioSink->close(); 1180 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER; 1181 CHECK_EQ(mAudioSink->open( 1182 sampleRate, 1183 numChannels, 1184 (audio_channel_mask_t)channelMask, 1185 AUDIO_FORMAT_PCM_16_BIT, 1186 8 /* bufferCount */, 1187 NULL, 1188 NULL, 1189 (audio_output_flags_t)flags), 1190 (status_t)OK); 1191 mAudioSink->start(); 1192 } 1193 if (audioSinkChanged) { 1194 mRenderer->signalAudioSinkChanged(); 1195 } 1196} 1197 1198void NuPlayer::closeAudioSink() { 1199 mAudioSink->close(); 1200 mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER; 1201} 1202 1203status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) { 1204 if (*decoder != NULL) { 1205 return OK; 1206 } 1207 1208 sp<AMessage> format = mSource->getFormat(audio); 1209 1210 if (format == NULL) { 1211 return -EWOULDBLOCK; 1212 } 1213 1214 if (!audio) { 1215 AString mime; 1216 CHECK(format->findString("mime", &mime)); 1217 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str()); 1218 1219 sp<AMessage> ccNotify = new AMessage(kWhatClosedCaptionNotify, id()); 1220 mCCDecoder = new CCDecoder(ccNotify); 1221 1222 if (mSourceFlags & Source::FLAG_SECURE) { 1223 format->setInt32("secure", true); 1224 } 1225 } 1226 1227 if (audio) { 1228 sp<AMessage> notify = new AMessage(kWhatAudioNotify, id()); 1229 ++mAudioDecoderGeneration; 1230 notify->setInt32("generation", mAudioDecoderGeneration); 1231 1232 if (mOffloadAudio) { 1233 *decoder = new DecoderPassThrough(notify); 1234 } else { 1235 *decoder = new Decoder(notify); 1236 } 1237 } else { 1238 sp<AMessage> notify = new AMessage(kWhatVideoNotify, id()); 1239 ++mVideoDecoderGeneration; 1240 notify->setInt32("generation", mVideoDecoderGeneration); 1241 1242 *decoder = new Decoder(notify, mNativeWindow); 1243 } 1244 (*decoder)->init(); 1245 (*decoder)->configure(format); 1246 1247 // allocate buffers to decrypt widevine source buffers 1248 if (!audio && (mSourceFlags & Source::FLAG_SECURE)) { 1249 Vector<sp<ABuffer> > inputBufs; 1250 CHECK_EQ((*decoder)->getInputBuffers(&inputBufs), (status_t)OK); 1251 1252 Vector<MediaBuffer *> mediaBufs; 1253 for (size_t i = 0; i < inputBufs.size(); i++) { 1254 const sp<ABuffer> &buffer = inputBufs[i]; 1255 MediaBuffer *mbuf = new MediaBuffer(buffer->data(), buffer->size()); 1256 mediaBufs.push(mbuf); 1257 } 1258 1259 status_t err = mSource->setBuffers(audio, mediaBufs); 1260 if (err != OK) { 1261 for (size_t i = 0; i < mediaBufs.size(); ++i) { 1262 mediaBufs[i]->release(); 1263 } 1264 mediaBufs.clear(); 1265 ALOGE("Secure source didn't support secure mediaBufs."); 1266 return err; 1267 } 1268 } 1269 return OK; 1270} 1271 1272status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) { 1273 sp<AMessage> reply; 1274 CHECK(msg->findMessage("reply", &reply)); 1275 1276 if ((audio && mFlushingAudio != NONE) 1277 || (!audio && mFlushingVideo != NONE) 1278 || mSource == NULL) { 1279 reply->setInt32("err", INFO_DISCONTINUITY); 1280 reply->post(); 1281 return OK; 1282 } 1283 1284 sp<ABuffer> accessUnit; 1285 1286 // Aggregate smaller buffers into a larger buffer. 1287 // The goal is to reduce power consumption. 1288 // Note this will not work if the decoder requires one frame per buffer. 1289 bool doBufferAggregation = (audio && mOffloadAudio); 1290 bool needMoreData = false; 1291 1292 bool dropAccessUnit; 1293 do { 1294 status_t err; 1295 // Did we save an accessUnit earlier because of a discontinuity? 1296 if (audio && (mPendingAudioAccessUnit != NULL)) { 1297 accessUnit = mPendingAudioAccessUnit; 1298 mPendingAudioAccessUnit.clear(); 1299 err = mPendingAudioErr; 1300 ALOGV("feedDecoderInputData() use mPendingAudioAccessUnit"); 1301 } else { 1302 err = mSource->dequeueAccessUnit(audio, &accessUnit); 1303 } 1304 1305 if (err == -EWOULDBLOCK) { 1306 return err; 1307 } else if (err != OK) { 1308 if (err == INFO_DISCONTINUITY) { 1309 if (doBufferAggregation && (mAggregateBuffer != NULL)) { 1310 // We already have some data so save this for later. 1311 mPendingAudioErr = err; 1312 mPendingAudioAccessUnit = accessUnit; 1313 accessUnit.clear(); 1314 ALOGD("feedDecoderInputData() save discontinuity for later"); 1315 break; 1316 } 1317 int32_t type; 1318 CHECK(accessUnit->meta()->findInt32("discontinuity", &type)); 1319 1320 bool formatChange = 1321 (audio && 1322 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT)) 1323 || (!audio && 1324 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT)); 1325 1326 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0; 1327 1328 ALOGI("%s discontinuity (formatChange=%d, time=%d)", 1329 audio ? "audio" : "video", formatChange, timeChange); 1330 1331 if (audio) { 1332 mSkipRenderingAudioUntilMediaTimeUs = -1; 1333 } else { 1334 mSkipRenderingVideoUntilMediaTimeUs = -1; 1335 } 1336 1337 if (timeChange) { 1338 sp<AMessage> extra; 1339 if (accessUnit->meta()->findMessage("extra", &extra) 1340 && extra != NULL) { 1341 int64_t resumeAtMediaTimeUs; 1342 if (extra->findInt64( 1343 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) { 1344 ALOGI("suppressing rendering of %s until %lld us", 1345 audio ? "audio" : "video", resumeAtMediaTimeUs); 1346 1347 if (audio) { 1348 mSkipRenderingAudioUntilMediaTimeUs = 1349 resumeAtMediaTimeUs; 1350 } else { 1351 mSkipRenderingVideoUntilMediaTimeUs = 1352 resumeAtMediaTimeUs; 1353 } 1354 } 1355 } 1356 } 1357 1358 mTimeDiscontinuityPending = 1359 mTimeDiscontinuityPending || timeChange; 1360 1361 bool seamlessFormatChange = false; 1362 sp<AMessage> newFormat = mSource->getFormat(audio); 1363 if (formatChange) { 1364 seamlessFormatChange = 1365 getDecoder(audio)->supportsSeamlessFormatChange(newFormat); 1366 // treat seamless format change separately 1367 formatChange = !seamlessFormatChange; 1368 } 1369 bool shutdownOrFlush = formatChange || timeChange; 1370 1371 // We want to queue up scan-sources only once per discontinuity. 1372 // We control this by doing it only if neither audio nor video are 1373 // flushing or shutting down. (After handling 1st discontinuity, one 1374 // of the flushing states will not be NONE.) 1375 // No need to scan sources if this discontinuity does not result 1376 // in a flush or shutdown, as the flushing state will stay NONE. 1377 if (mFlushingAudio == NONE && mFlushingVideo == NONE && 1378 shutdownOrFlush) { 1379 // And we'll resume scanning sources once we're done 1380 // flushing. 1381 mDeferredActions.push_front( 1382 new SimpleAction( 1383 &NuPlayer::performScanSources)); 1384 } 1385 1386 if (formatChange /* not seamless */) { 1387 // must change decoder 1388 flushDecoder(audio, /* needShutdown = */ true); 1389 } else if (timeChange) { 1390 // need to flush 1391 flushDecoder(audio, /* needShutdown = */ false, newFormat); 1392 err = OK; 1393 } else if (seamlessFormatChange) { 1394 // reuse existing decoder and don't flush 1395 updateDecoderFormatWithoutFlush(audio, newFormat); 1396 err = OK; 1397 } else { 1398 // This stream is unaffected by the discontinuity 1399 return -EWOULDBLOCK; 1400 } 1401 } 1402 1403 reply->setInt32("err", err); 1404 reply->post(); 1405 return OK; 1406 } 1407 1408 if (!audio) { 1409 ++mNumFramesTotal; 1410 } 1411 1412 dropAccessUnit = false; 1413 if (!audio 1414 && !(mSourceFlags & Source::FLAG_SECURE) 1415 && mVideoLateByUs > 100000ll 1416 && mVideoIsAVC 1417 && !IsAVCReferenceFrame(accessUnit)) { 1418 dropAccessUnit = true; 1419 ++mNumFramesDropped; 1420 } 1421 1422 size_t smallSize = accessUnit->size(); 1423 needMoreData = false; 1424 if (doBufferAggregation && (mAggregateBuffer == NULL) 1425 // Don't bother if only room for a few small buffers. 1426 && (smallSize < (kAggregateBufferSizeBytes / 3))) { 1427 // Create a larger buffer for combining smaller buffers from the extractor. 1428 mAggregateBuffer = new ABuffer(kAggregateBufferSizeBytes); 1429 mAggregateBuffer->setRange(0, 0); // start empty 1430 } 1431 1432 if (doBufferAggregation && (mAggregateBuffer != NULL)) { 1433 int64_t timeUs; 1434 int64_t dummy; 1435 bool smallTimestampValid = accessUnit->meta()->findInt64("timeUs", &timeUs); 1436 bool bigTimestampValid = mAggregateBuffer->meta()->findInt64("timeUs", &dummy); 1437 // Will the smaller buffer fit? 1438 size_t bigSize = mAggregateBuffer->size(); 1439 size_t roomLeft = mAggregateBuffer->capacity() - bigSize; 1440 // Should we save this small buffer for the next big buffer? 1441 // If the first small buffer did not have a timestamp then save 1442 // any buffer that does have a timestamp until the next big buffer. 1443 if ((smallSize > roomLeft) 1444 || (!bigTimestampValid && (bigSize > 0) && smallTimestampValid)) { 1445 mPendingAudioErr = err; 1446 mPendingAudioAccessUnit = accessUnit; 1447 accessUnit.clear(); 1448 } else { 1449 // Grab time from first small buffer if available. 1450 if ((bigSize == 0) && smallTimestampValid) { 1451 mAggregateBuffer->meta()->setInt64("timeUs", timeUs); 1452 } 1453 // Append small buffer to the bigger buffer. 1454 memcpy(mAggregateBuffer->base() + bigSize, accessUnit->data(), smallSize); 1455 bigSize += smallSize; 1456 mAggregateBuffer->setRange(0, bigSize); 1457 1458 // Keep looping until we run out of room in the mAggregateBuffer. 1459 needMoreData = true; 1460 1461 ALOGV("feedDecoderInputData() smallSize = %zu, bigSize = %zu, capacity = %zu", 1462 smallSize, bigSize, mAggregateBuffer->capacity()); 1463 } 1464 } 1465 } while (dropAccessUnit || needMoreData); 1466 1467 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video"); 1468 1469#if 0 1470 int64_t mediaTimeUs; 1471 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs)); 1472 ALOGV("feeding %s input buffer at media time %.2f secs", 1473 audio ? "audio" : "video", 1474 mediaTimeUs / 1E6); 1475#endif 1476 1477 if (!audio) { 1478 mCCDecoder->decode(accessUnit); 1479 } 1480 1481 if (doBufferAggregation && (mAggregateBuffer != NULL)) { 1482 ALOGV("feedDecoderInputData() reply with aggregated buffer, %zu", 1483 mAggregateBuffer->size()); 1484 reply->setBuffer("buffer", mAggregateBuffer); 1485 mAggregateBuffer.clear(); 1486 } else { 1487 reply->setBuffer("buffer", accessUnit); 1488 } 1489 1490 reply->post(); 1491 1492 return OK; 1493} 1494 1495void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) { 1496 // ALOGV("renderBuffer %s", audio ? "audio" : "video"); 1497 1498 sp<AMessage> reply; 1499 CHECK(msg->findMessage("reply", &reply)); 1500 1501 if ((audio && mFlushingAudio != NONE) 1502 || (!audio && mFlushingVideo != NONE)) { 1503 // We're currently attempting to flush the decoder, in order 1504 // to complete this, the decoder wants all its buffers back, 1505 // so we don't want any output buffers it sent us (from before 1506 // we initiated the flush) to be stuck in the renderer's queue. 1507 1508 ALOGV("we're still flushing the %s decoder, sending its output buffer" 1509 " right back.", audio ? "audio" : "video"); 1510 1511 reply->post(); 1512 return; 1513 } 1514 1515 sp<ABuffer> buffer; 1516 CHECK(msg->findBuffer("buffer", &buffer)); 1517 1518 int64_t mediaTimeUs; 1519 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs)); 1520 1521 int64_t &skipUntilMediaTimeUs = 1522 audio 1523 ? mSkipRenderingAudioUntilMediaTimeUs 1524 : mSkipRenderingVideoUntilMediaTimeUs; 1525 1526 if (skipUntilMediaTimeUs >= 0) { 1527 1528 if (mediaTimeUs < skipUntilMediaTimeUs) { 1529 ALOGV("dropping %s buffer at time %lld as requested.", 1530 audio ? "audio" : "video", 1531 mediaTimeUs); 1532 1533 reply->post(); 1534 return; 1535 } 1536 1537 skipUntilMediaTimeUs = -1; 1538 } 1539 1540 if (!audio && mCCDecoder->isSelected()) { 1541 mCCDecoder->display(mediaTimeUs); 1542 } 1543 1544 mRenderer->queueBuffer(audio, buffer, reply); 1545} 1546 1547void NuPlayer::updateVideoSize( 1548 const sp<AMessage> &inputFormat, 1549 const sp<AMessage> &outputFormat) { 1550 if (inputFormat == NULL) { 1551 ALOGW("Unknown video size, reporting 0x0!"); 1552 notifyListener(MEDIA_SET_VIDEO_SIZE, 0, 0); 1553 return; 1554 } 1555 1556 int32_t displayWidth, displayHeight; 1557 int32_t cropLeft, cropTop, cropRight, cropBottom; 1558 1559 if (outputFormat != NULL) { 1560 int32_t width, height; 1561 CHECK(outputFormat->findInt32("width", &width)); 1562 CHECK(outputFormat->findInt32("height", &height)); 1563 1564 int32_t cropLeft, cropTop, cropRight, cropBottom; 1565 CHECK(outputFormat->findRect( 1566 "crop", 1567 &cropLeft, &cropTop, &cropRight, &cropBottom)); 1568 1569 displayWidth = cropRight - cropLeft + 1; 1570 displayHeight = cropBottom - cropTop + 1; 1571 1572 ALOGV("Video output format changed to %d x %d " 1573 "(crop: %d x %d @ (%d, %d))", 1574 width, height, 1575 displayWidth, 1576 displayHeight, 1577 cropLeft, cropTop); 1578 } else { 1579 CHECK(inputFormat->findInt32("width", &displayWidth)); 1580 CHECK(inputFormat->findInt32("height", &displayHeight)); 1581 1582 ALOGV("Video input format %d x %d", displayWidth, displayHeight); 1583 } 1584 1585 // Take into account sample aspect ratio if necessary: 1586 int32_t sarWidth, sarHeight; 1587 if (inputFormat->findInt32("sar-width", &sarWidth) 1588 && inputFormat->findInt32("sar-height", &sarHeight)) { 1589 ALOGV("Sample aspect ratio %d : %d", sarWidth, sarHeight); 1590 1591 displayWidth = (displayWidth * sarWidth) / sarHeight; 1592 1593 ALOGV("display dimensions %d x %d", displayWidth, displayHeight); 1594 } 1595 1596 int32_t rotationDegrees; 1597 if (!inputFormat->findInt32("rotation-degrees", &rotationDegrees)) { 1598 rotationDegrees = 0; 1599 } 1600 1601 if (rotationDegrees == 90 || rotationDegrees == 270) { 1602 int32_t tmp = displayWidth; 1603 displayWidth = displayHeight; 1604 displayHeight = tmp; 1605 } 1606 1607 notifyListener( 1608 MEDIA_SET_VIDEO_SIZE, 1609 displayWidth, 1610 displayHeight); 1611} 1612 1613void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) { 1614 if (mDriver == NULL) { 1615 return; 1616 } 1617 1618 sp<NuPlayerDriver> driver = mDriver.promote(); 1619 1620 if (driver == NULL) { 1621 return; 1622 } 1623 1624 driver->notifyListener(msg, ext1, ext2, in); 1625} 1626 1627void NuPlayer::flushDecoder( 1628 bool audio, bool needShutdown, const sp<AMessage> &newFormat) { 1629 ALOGV("[%s] flushDecoder needShutdown=%d", 1630 audio ? "audio" : "video", needShutdown); 1631 1632 const sp<Decoder> &decoder = getDecoder(audio); 1633 if (decoder == NULL) { 1634 ALOGI("flushDecoder %s without decoder present", 1635 audio ? "audio" : "video"); 1636 return; 1637 } 1638 1639 // Make sure we don't continue to scan sources until we finish flushing. 1640 ++mScanSourcesGeneration; 1641 mScanSourcesPending = false; 1642 1643 decoder->signalFlush(newFormat); 1644 mRenderer->flush(audio); 1645 1646 FlushStatus newStatus = 1647 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 1648 1649 if (audio) { 1650 ALOGE_IF(mFlushingAudio != NONE, 1651 "audio flushDecoder() is called in state %d", mFlushingAudio); 1652 mFlushingAudio = newStatus; 1653 } else { 1654 ALOGE_IF(mFlushingVideo != NONE, 1655 "video flushDecoder() is called in state %d", mFlushingVideo); 1656 mFlushingVideo = newStatus; 1657 1658 if (mCCDecoder != NULL) { 1659 mCCDecoder->flush(); 1660 } 1661 } 1662} 1663 1664void NuPlayer::updateDecoderFormatWithoutFlush( 1665 bool audio, const sp<AMessage> &format) { 1666 ALOGV("[%s] updateDecoderFormatWithoutFlush", audio ? "audio" : "video"); 1667 1668 const sp<Decoder> &decoder = getDecoder(audio); 1669 if (decoder == NULL) { 1670 ALOGI("updateDecoderFormatWithoutFlush %s without decoder present", 1671 audio ? "audio" : "video"); 1672 return; 1673 } 1674 1675 decoder->signalUpdateFormat(format); 1676} 1677 1678void NuPlayer::queueDecoderShutdown( 1679 bool audio, bool video, const sp<AMessage> &reply) { 1680 ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video); 1681 1682 mDeferredActions.push_back( 1683 new ShutdownDecoderAction(audio, video)); 1684 1685 mDeferredActions.push_back( 1686 new SimpleAction(&NuPlayer::performScanSources)); 1687 1688 mDeferredActions.push_back(new PostMessageAction(reply)); 1689 1690 processDeferredActions(); 1691} 1692 1693status_t NuPlayer::setVideoScalingMode(int32_t mode) { 1694 mVideoScalingMode = mode; 1695 if (mNativeWindow != NULL) { 1696 status_t ret = native_window_set_scaling_mode( 1697 mNativeWindow->getNativeWindow().get(), mVideoScalingMode); 1698 if (ret != OK) { 1699 ALOGE("Failed to set scaling mode (%d): %s", 1700 -ret, strerror(-ret)); 1701 return ret; 1702 } 1703 } 1704 return OK; 1705} 1706 1707status_t NuPlayer::getTrackInfo(Parcel* reply) const { 1708 sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id()); 1709 msg->setPointer("reply", reply); 1710 1711 sp<AMessage> response; 1712 status_t err = msg->postAndAwaitResponse(&response); 1713 return err; 1714} 1715 1716status_t NuPlayer::getSelectedTrack(int32_t type, Parcel* reply) const { 1717 sp<AMessage> msg = new AMessage(kWhatGetSelectedTrack, id()); 1718 msg->setPointer("reply", reply); 1719 msg->setInt32("type", type); 1720 1721 sp<AMessage> response; 1722 status_t err = msg->postAndAwaitResponse(&response); 1723 if (err == OK && response != NULL) { 1724 CHECK(response->findInt32("err", &err)); 1725 } 1726 return err; 1727} 1728 1729status_t NuPlayer::selectTrack(size_t trackIndex, bool select) { 1730 sp<AMessage> msg = new AMessage(kWhatSelectTrack, id()); 1731 msg->setSize("trackIndex", trackIndex); 1732 msg->setInt32("select", select); 1733 1734 sp<AMessage> response; 1735 status_t err = msg->postAndAwaitResponse(&response); 1736 1737 if (err != OK) { 1738 return err; 1739 } 1740 1741 if (!response->findInt32("err", &err)) { 1742 err = OK; 1743 } 1744 1745 return err; 1746} 1747 1748sp<MetaData> NuPlayer::getFileMeta() { 1749 return mSource->getFileFormatMeta(); 1750} 1751 1752void NuPlayer::schedulePollDuration() { 1753 sp<AMessage> msg = new AMessage(kWhatPollDuration, id()); 1754 msg->setInt32("generation", mPollDurationGeneration); 1755 msg->post(); 1756} 1757 1758void NuPlayer::cancelPollDuration() { 1759 ++mPollDurationGeneration; 1760} 1761 1762void NuPlayer::processDeferredActions() { 1763 while (!mDeferredActions.empty()) { 1764 // We won't execute any deferred actions until we're no longer in 1765 // an intermediate state, i.e. one more more decoders are currently 1766 // flushing or shutting down. 1767 1768 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 1769 // We're currently flushing, postpone the reset until that's 1770 // completed. 1771 1772 ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d", 1773 mFlushingAudio, mFlushingVideo); 1774 1775 break; 1776 } 1777 1778 sp<Action> action = *mDeferredActions.begin(); 1779 mDeferredActions.erase(mDeferredActions.begin()); 1780 1781 action->execute(this); 1782 } 1783} 1784 1785void NuPlayer::performSeek(int64_t seekTimeUs, bool needNotify) { 1786 ALOGV("performSeek seekTimeUs=%lld us (%.2f secs), needNotify(%d)", 1787 seekTimeUs, 1788 seekTimeUs / 1E6, 1789 needNotify); 1790 1791 if (mSource == NULL) { 1792 // This happens when reset occurs right before the loop mode 1793 // asynchronously seeks to the start of the stream. 1794 LOG_ALWAYS_FATAL_IF(mAudioDecoder != NULL || mVideoDecoder != NULL, 1795 "mSource is NULL and decoders not NULL audio(%p) video(%p)", 1796 mAudioDecoder.get(), mVideoDecoder.get()); 1797 return; 1798 } 1799 mSource->seekTo(seekTimeUs); 1800 ++mTimedTextGeneration; 1801 1802 if (mDriver != NULL) { 1803 sp<NuPlayerDriver> driver = mDriver.promote(); 1804 if (driver != NULL) { 1805 driver->notifyPosition(seekTimeUs); 1806 if (needNotify) { 1807 driver->notifySeekComplete(); 1808 } 1809 } 1810 } 1811 1812 // everything's flushed, continue playback. 1813} 1814 1815void NuPlayer::performDecoderFlush() { 1816 ALOGV("performDecoderFlush"); 1817 1818 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 1819 return; 1820 } 1821 1822 mTimeDiscontinuityPending = true; 1823 1824 if (mAudioDecoder != NULL) { 1825 flushDecoder(true /* audio */, false /* needShutdown */); 1826 } 1827 1828 if (mVideoDecoder != NULL) { 1829 flushDecoder(false /* audio */, false /* needShutdown */); 1830 } 1831} 1832 1833void NuPlayer::performDecoderShutdown(bool audio, bool video) { 1834 ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video); 1835 1836 if ((!audio || mAudioDecoder == NULL) 1837 && (!video || mVideoDecoder == NULL)) { 1838 return; 1839 } 1840 1841 mTimeDiscontinuityPending = true; 1842 1843 if (audio && mAudioDecoder != NULL) { 1844 flushDecoder(true /* audio */, true /* needShutdown */); 1845 } 1846 1847 if (video && mVideoDecoder != NULL) { 1848 flushDecoder(false /* audio */, true /* needShutdown */); 1849 } 1850} 1851 1852void NuPlayer::performReset() { 1853 ALOGV("performReset"); 1854 1855 CHECK(mAudioDecoder == NULL); 1856 CHECK(mVideoDecoder == NULL); 1857 1858 cancelPollDuration(); 1859 1860 ++mScanSourcesGeneration; 1861 mScanSourcesPending = false; 1862 1863 ++mAudioDecoderGeneration; 1864 ++mVideoDecoderGeneration; 1865 1866 if (mRendererLooper != NULL) { 1867 if (mRenderer != NULL) { 1868 mRendererLooper->unregisterHandler(mRenderer->id()); 1869 } 1870 mRendererLooper->stop(); 1871 mRendererLooper.clear(); 1872 } 1873 mRenderer.clear(); 1874 1875 if (mSource != NULL) { 1876 mSource->stop(); 1877 1878 mSource.clear(); 1879 } 1880 1881 if (mDriver != NULL) { 1882 sp<NuPlayerDriver> driver = mDriver.promote(); 1883 if (driver != NULL) { 1884 driver->notifyResetComplete(); 1885 } 1886 } 1887 1888 mStarted = false; 1889} 1890 1891void NuPlayer::performScanSources() { 1892 ALOGV("performScanSources"); 1893 1894 if (!mStarted) { 1895 return; 1896 } 1897 1898 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 1899 postScanSources(); 1900 } 1901} 1902 1903void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) { 1904 ALOGV("performSetSurface"); 1905 1906 mNativeWindow = wrapper; 1907 1908 // XXX - ignore error from setVideoScalingMode for now 1909 setVideoScalingMode(mVideoScalingMode); 1910 1911 if (mDriver != NULL) { 1912 sp<NuPlayerDriver> driver = mDriver.promote(); 1913 if (driver != NULL) { 1914 driver->notifySetSurfaceComplete(); 1915 } 1916 } 1917} 1918 1919void NuPlayer::onSourceNotify(const sp<AMessage> &msg) { 1920 int32_t what; 1921 CHECK(msg->findInt32("what", &what)); 1922 1923 switch (what) { 1924 case Source::kWhatPrepared: 1925 { 1926 if (mSource == NULL) { 1927 // This is a stale notification from a source that was 1928 // asynchronously preparing when the client called reset(). 1929 // We handled the reset, the source is gone. 1930 break; 1931 } 1932 1933 int32_t err; 1934 CHECK(msg->findInt32("err", &err)); 1935 1936 sp<NuPlayerDriver> driver = mDriver.promote(); 1937 if (driver != NULL) { 1938 // notify duration first, so that it's definitely set when 1939 // the app received the "prepare complete" callback. 1940 int64_t durationUs; 1941 if (mSource->getDuration(&durationUs) == OK) { 1942 driver->notifyDuration(durationUs); 1943 } 1944 driver->notifyPrepareCompleted(err); 1945 } 1946 1947 break; 1948 } 1949 1950 case Source::kWhatFlagsChanged: 1951 { 1952 uint32_t flags; 1953 CHECK(msg->findInt32("flags", (int32_t *)&flags)); 1954 1955 sp<NuPlayerDriver> driver = mDriver.promote(); 1956 if (driver != NULL) { 1957 driver->notifyFlagsChanged(flags); 1958 } 1959 1960 if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 1961 && (!(flags & Source::FLAG_DYNAMIC_DURATION))) { 1962 cancelPollDuration(); 1963 } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION) 1964 && (flags & Source::FLAG_DYNAMIC_DURATION) 1965 && (mAudioDecoder != NULL || mVideoDecoder != NULL)) { 1966 schedulePollDuration(); 1967 } 1968 1969 mSourceFlags = flags; 1970 break; 1971 } 1972 1973 case Source::kWhatVideoSizeChanged: 1974 { 1975 sp<AMessage> format; 1976 CHECK(msg->findMessage("format", &format)); 1977 1978 updateVideoSize(format); 1979 break; 1980 } 1981 1982 case Source::kWhatBufferingUpdate: 1983 { 1984 int32_t percentage; 1985 CHECK(msg->findInt32("percentage", &percentage)); 1986 1987 notifyListener(MEDIA_BUFFERING_UPDATE, percentage, 0); 1988 break; 1989 } 1990 1991 case Source::kWhatBufferingStart: 1992 { 1993 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0); 1994 break; 1995 } 1996 1997 case Source::kWhatBufferingEnd: 1998 { 1999 notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0); 2000 break; 2001 } 2002 2003 case Source::kWhatSubtitleData: 2004 { 2005 sp<ABuffer> buffer; 2006 CHECK(msg->findBuffer("buffer", &buffer)); 2007 2008 sendSubtitleData(buffer, 0 /* baseIndex */); 2009 break; 2010 } 2011 2012 case Source::kWhatTimedTextData: 2013 { 2014 int32_t generation; 2015 if (msg->findInt32("generation", &generation) 2016 && generation != mTimedTextGeneration) { 2017 break; 2018 } 2019 2020 sp<ABuffer> buffer; 2021 CHECK(msg->findBuffer("buffer", &buffer)); 2022 2023 sp<NuPlayerDriver> driver = mDriver.promote(); 2024 if (driver == NULL) { 2025 break; 2026 } 2027 2028 int posMs; 2029 int64_t timeUs, posUs; 2030 driver->getCurrentPosition(&posMs); 2031 posUs = posMs * 1000; 2032 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2033 2034 if (posUs < timeUs) { 2035 if (!msg->findInt32("generation", &generation)) { 2036 msg->setInt32("generation", mTimedTextGeneration); 2037 } 2038 msg->post(timeUs - posUs); 2039 } else { 2040 sendTimedTextData(buffer); 2041 } 2042 break; 2043 } 2044 2045 case Source::kWhatQueueDecoderShutdown: 2046 { 2047 int32_t audio, video; 2048 CHECK(msg->findInt32("audio", &audio)); 2049 CHECK(msg->findInt32("video", &video)); 2050 2051 sp<AMessage> reply; 2052 CHECK(msg->findMessage("reply", &reply)); 2053 2054 queueDecoderShutdown(audio, video, reply); 2055 break; 2056 } 2057 2058 case Source::kWhatDrmNoLicense: 2059 { 2060 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, ERROR_DRM_NO_LICENSE); 2061 break; 2062 } 2063 2064 default: 2065 TRESPASS(); 2066 } 2067} 2068 2069void NuPlayer::onClosedCaptionNotify(const sp<AMessage> &msg) { 2070 int32_t what; 2071 CHECK(msg->findInt32("what", &what)); 2072 2073 switch (what) { 2074 case NuPlayer::CCDecoder::kWhatClosedCaptionData: 2075 { 2076 sp<ABuffer> buffer; 2077 CHECK(msg->findBuffer("buffer", &buffer)); 2078 2079 size_t inbandTracks = 0; 2080 if (mSource != NULL) { 2081 inbandTracks = mSource->getTrackCount(); 2082 } 2083 2084 sendSubtitleData(buffer, inbandTracks); 2085 break; 2086 } 2087 2088 case NuPlayer::CCDecoder::kWhatTrackAdded: 2089 { 2090 notifyListener(MEDIA_INFO, MEDIA_INFO_METADATA_UPDATE, 0); 2091 2092 break; 2093 } 2094 2095 default: 2096 TRESPASS(); 2097 } 2098 2099 2100} 2101 2102void NuPlayer::sendSubtitleData(const sp<ABuffer> &buffer, int32_t baseIndex) { 2103 int32_t trackIndex; 2104 int64_t timeUs, durationUs; 2105 CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex)); 2106 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2107 CHECK(buffer->meta()->findInt64("durationUs", &durationUs)); 2108 2109 Parcel in; 2110 in.writeInt32(trackIndex + baseIndex); 2111 in.writeInt64(timeUs); 2112 in.writeInt64(durationUs); 2113 in.writeInt32(buffer->size()); 2114 in.writeInt32(buffer->size()); 2115 in.write(buffer->data(), buffer->size()); 2116 2117 notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in); 2118} 2119 2120void NuPlayer::sendTimedTextData(const sp<ABuffer> &buffer) { 2121 const void *data; 2122 size_t size = 0; 2123 int64_t timeUs; 2124 int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS; 2125 2126 AString mime; 2127 CHECK(buffer->meta()->findString("mime", &mime)); 2128 CHECK(strcasecmp(mime.c_str(), MEDIA_MIMETYPE_TEXT_3GPP) == 0); 2129 2130 data = buffer->data(); 2131 size = buffer->size(); 2132 2133 Parcel parcel; 2134 if (size > 0) { 2135 CHECK(buffer->meta()->findInt64("timeUs", &timeUs)); 2136 flag |= TextDescriptions::IN_BAND_TEXT_3GPP; 2137 TextDescriptions::getParcelOfDescriptions( 2138 (const uint8_t *)data, size, flag, timeUs / 1000, &parcel); 2139 } 2140 2141 if ((parcel.dataSize() > 0)) { 2142 notifyListener(MEDIA_TIMED_TEXT, 0, 0, &parcel); 2143 } else { // send an empty timed text 2144 notifyListener(MEDIA_TIMED_TEXT, 0, 0); 2145 } 2146} 2147//////////////////////////////////////////////////////////////////////////////// 2148 2149sp<AMessage> NuPlayer::Source::getFormat(bool audio) { 2150 sp<MetaData> meta = getFormatMeta(audio); 2151 2152 if (meta == NULL) { 2153 return NULL; 2154 } 2155 2156 sp<AMessage> msg = new AMessage; 2157 2158 if(convertMetaDataToMessage(meta, &msg) == OK) { 2159 return msg; 2160 } 2161 return NULL; 2162} 2163 2164void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) { 2165 sp<AMessage> notify = dupNotify(); 2166 notify->setInt32("what", kWhatFlagsChanged); 2167 notify->setInt32("flags", flags); 2168 notify->post(); 2169} 2170 2171void NuPlayer::Source::notifyVideoSizeChanged(const sp<AMessage> &format) { 2172 sp<AMessage> notify = dupNotify(); 2173 notify->setInt32("what", kWhatVideoSizeChanged); 2174 notify->setMessage("format", format); 2175 notify->post(); 2176} 2177 2178void NuPlayer::Source::notifyPrepared(status_t err) { 2179 sp<AMessage> notify = dupNotify(); 2180 notify->setInt32("what", kWhatPrepared); 2181 notify->setInt32("err", err); 2182 notify->post(); 2183} 2184 2185void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) { 2186 TRESPASS(); 2187} 2188 2189} // namespace android 2190