NuPlayer.cpp revision f57b4ea3e409537b1d5f9aaea93d356b1cebbc6a
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 "NuPlayerDriver.h" 26#include "NuPlayerRenderer.h" 27#include "NuPlayerSource.h" 28#include "RTSPSource.h" 29#include "StreamingSource.h" 30#include "GenericSource.h" 31 32#include "ATSParser.h" 33 34#include <media/stagefright/foundation/hexdump.h> 35#include <media/stagefright/foundation/ABuffer.h> 36#include <media/stagefright/foundation/ADebug.h> 37#include <media/stagefright/foundation/AMessage.h> 38#include <media/stagefright/ACodec.h> 39#include <media/stagefright/MediaDefs.h> 40#include <media/stagefright/MediaErrors.h> 41#include <media/stagefright/MetaData.h> 42#include <gui/ISurfaceTexture.h> 43 44#include "avc_utils.h" 45 46namespace android { 47 48//////////////////////////////////////////////////////////////////////////////// 49 50NuPlayer::NuPlayer() 51 : mUIDValid(false), 52 mVideoIsAVC(false), 53 mAudioEOS(false), 54 mVideoEOS(false), 55 mScanSourcesPending(false), 56 mScanSourcesGeneration(0), 57 mTimeDiscontinuityPending(false), 58 mFlushingAudio(NONE), 59 mFlushingVideo(NONE), 60 mResetInProgress(false), 61 mResetPostponed(false), 62 mSkipRenderingAudioUntilMediaTimeUs(-1ll), 63 mSkipRenderingVideoUntilMediaTimeUs(-1ll), 64 mVideoLateByUs(0ll), 65 mNumFramesTotal(0ll), 66 mNumFramesDropped(0ll) { 67} 68 69NuPlayer::~NuPlayer() { 70} 71 72void NuPlayer::setUID(uid_t uid) { 73 mUIDValid = true; 74 mUID = uid; 75} 76 77void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) { 78 mDriver = driver; 79} 80 81void NuPlayer::setDataSource(const sp<IStreamSource> &source) { 82 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); 83 84 msg->setObject("source", new StreamingSource(source)); 85 msg->post(); 86} 87 88static bool IsHTTPLiveURL(const char *url) { 89 if (!strncasecmp("http://", url, 7) 90 || !strncasecmp("https://", url, 8)) { 91 size_t len = strlen(url); 92 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) { 93 return true; 94 } 95 96 if (strstr(url,"m3u8")) { 97 return true; 98 } 99 } 100 101 return false; 102} 103 104void NuPlayer::setDataSource( 105 const char *url, const KeyedVector<String8, String8> *headers) { 106 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); 107 108 sp<Source> source; 109 if (IsHTTPLiveURL(url)) { 110 source = new HTTPLiveSource(url, headers, mUIDValid, mUID); 111 } else if (!strncasecmp(url, "rtsp://", 7)) { 112 source = new RTSPSource(url, headers, mUIDValid, mUID); 113 } else { 114 source = new GenericSource(url, headers, mUIDValid, mUID); 115 } 116 117 msg->setObject("source", source); 118 msg->post(); 119} 120 121void NuPlayer::setDataSource(int fd, int64_t offset, int64_t length) { 122 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); 123 124 sp<Source> source = new GenericSource(fd, offset, length); 125 msg->setObject("source", source); 126 msg->post(); 127} 128 129void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) { 130 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id()); 131 sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ? 132 new SurfaceTextureClient(surfaceTexture) : NULL); 133 msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient)); 134 msg->post(); 135} 136 137void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) { 138 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id()); 139 msg->setObject("sink", sink); 140 msg->post(); 141} 142 143void NuPlayer::start() { 144 (new AMessage(kWhatStart, id()))->post(); 145} 146 147void NuPlayer::pause() { 148 (new AMessage(kWhatPause, id()))->post(); 149} 150 151void NuPlayer::resume() { 152 (new AMessage(kWhatResume, id()))->post(); 153} 154 155void NuPlayer::resetAsync() { 156 (new AMessage(kWhatReset, id()))->post(); 157} 158 159void NuPlayer::seekToAsync(int64_t seekTimeUs) { 160 sp<AMessage> msg = new AMessage(kWhatSeek, id()); 161 msg->setInt64("seekTimeUs", seekTimeUs); 162 msg->post(); 163} 164 165// static 166bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) { 167 switch (state) { 168 case FLUSHING_DECODER: 169 if (needShutdown != NULL) { 170 *needShutdown = false; 171 } 172 return true; 173 174 case FLUSHING_DECODER_SHUTDOWN: 175 if (needShutdown != NULL) { 176 *needShutdown = true; 177 } 178 return true; 179 180 default: 181 return false; 182 } 183} 184 185void NuPlayer::onMessageReceived(const sp<AMessage> &msg) { 186 switch (msg->what()) { 187 case kWhatSetDataSource: 188 { 189 ALOGV("kWhatSetDataSource"); 190 191 CHECK(mSource == NULL); 192 193 sp<RefBase> obj; 194 CHECK(msg->findObject("source", &obj)); 195 196 mSource = static_cast<Source *>(obj.get()); 197 break; 198 } 199 200 case kWhatSetVideoNativeWindow: 201 { 202 ALOGV("kWhatSetVideoNativeWindow"); 203 204 sp<RefBase> obj; 205 CHECK(msg->findObject("native-window", &obj)); 206 207 mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get()); 208 break; 209 } 210 211 case kWhatSetAudioSink: 212 { 213 ALOGV("kWhatSetAudioSink"); 214 215 sp<RefBase> obj; 216 CHECK(msg->findObject("sink", &obj)); 217 218 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get()); 219 break; 220 } 221 222 case kWhatStart: 223 { 224 ALOGV("kWhatStart"); 225 226 mVideoIsAVC = false; 227 mAudioEOS = false; 228 mVideoEOS = false; 229 mSkipRenderingAudioUntilMediaTimeUs = -1; 230 mSkipRenderingVideoUntilMediaTimeUs = -1; 231 mVideoLateByUs = 0; 232 mNumFramesTotal = 0; 233 mNumFramesDropped = 0; 234 235 mSource->start(); 236 237 mRenderer = new Renderer( 238 mAudioSink, 239 new AMessage(kWhatRendererNotify, id())); 240 241 looper()->registerHandler(mRenderer); 242 243 postScanSources(); 244 break; 245 } 246 247 case kWhatScanSources: 248 { 249 int32_t generation; 250 CHECK(msg->findInt32("generation", &generation)); 251 if (generation != mScanSourcesGeneration) { 252 // Drop obsolete msg. 253 break; 254 } 255 256 mScanSourcesPending = false; 257 258 ALOGV("scanning sources haveAudio=%d, haveVideo=%d", 259 mAudioDecoder != NULL, mVideoDecoder != NULL); 260 261 instantiateDecoder(false, &mVideoDecoder); 262 263 if (mAudioSink != NULL) { 264 instantiateDecoder(true, &mAudioDecoder); 265 } 266 267 status_t err; 268 if ((err = mSource->feedMoreTSData()) != OK) { 269 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 270 // We're not currently decoding anything (no audio or 271 // video tracks found) and we just ran out of input data. 272 273 if (err == ERROR_END_OF_STREAM) { 274 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 275 } else { 276 notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err); 277 } 278 } 279 break; 280 } 281 282 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 283 msg->post(100000ll); 284 mScanSourcesPending = true; 285 } 286 break; 287 } 288 289 case kWhatVideoNotify: 290 case kWhatAudioNotify: 291 { 292 bool audio = msg->what() == kWhatAudioNotify; 293 294 sp<AMessage> codecRequest; 295 CHECK(msg->findMessage("codec-request", &codecRequest)); 296 297 int32_t what; 298 CHECK(codecRequest->findInt32("what", &what)); 299 300 if (what == ACodec::kWhatFillThisBuffer) { 301 status_t err = feedDecoderInputData( 302 audio, codecRequest); 303 304 if (err == -EWOULDBLOCK) { 305 if (mSource->feedMoreTSData() == OK) { 306 msg->post(10000ll); 307 } 308 } 309 } else if (what == ACodec::kWhatEOS) { 310 int32_t err; 311 CHECK(codecRequest->findInt32("err", &err)); 312 313 if (err == ERROR_END_OF_STREAM) { 314 ALOGV("got %s decoder EOS", audio ? "audio" : "video"); 315 } else { 316 ALOGV("got %s decoder EOS w/ error %d", 317 audio ? "audio" : "video", 318 err); 319 } 320 321 mRenderer->queueEOS(audio, err); 322 } else if (what == ACodec::kWhatFlushCompleted) { 323 bool needShutdown; 324 325 if (audio) { 326 CHECK(IsFlushingState(mFlushingAudio, &needShutdown)); 327 mFlushingAudio = FLUSHED; 328 } else { 329 CHECK(IsFlushingState(mFlushingVideo, &needShutdown)); 330 mFlushingVideo = FLUSHED; 331 332 mVideoLateByUs = 0; 333 } 334 335 ALOGV("decoder %s flush completed", audio ? "audio" : "video"); 336 337 if (needShutdown) { 338 ALOGV("initiating %s decoder shutdown", 339 audio ? "audio" : "video"); 340 341 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown(); 342 343 if (audio) { 344 mFlushingAudio = SHUTTING_DOWN_DECODER; 345 } else { 346 mFlushingVideo = SHUTTING_DOWN_DECODER; 347 } 348 } 349 350 finishFlushIfPossible(); 351 } else if (what == ACodec::kWhatOutputFormatChanged) { 352 if (audio) { 353 int32_t numChannels; 354 CHECK(codecRequest->findInt32("channel-count", &numChannels)); 355 356 int32_t sampleRate; 357 CHECK(codecRequest->findInt32("sample-rate", &sampleRate)); 358 359 ALOGV("Audio output format changed to %d Hz, %d channels", 360 sampleRate, numChannels); 361 362 mAudioSink->close(); 363 364 audio_output_flags_t flags; 365 int64_t durationUs; 366 // FIXME: we should handle the case where the video decoder is created after 367 // we receive the format change indication. Current code will just make that 368 // we select deep buffer with video which should not be a problem as it should 369 // not prevent from keeping A/V sync. 370 if (mVideoDecoder == NULL && 371 mSource->getDuration(&durationUs) == OK && 372 durationUs > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) { 373 flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER; 374 } else { 375 flags = AUDIO_OUTPUT_FLAG_NONE; 376 } 377 378 int32_t channelMask; 379 if (!codecRequest->findInt32("channel-mask", &channelMask)) { 380 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER; 381 } 382 383 CHECK_EQ(mAudioSink->open( 384 sampleRate, 385 numChannels, 386 (audio_channel_mask_t)channelMask, 387 AUDIO_FORMAT_PCM_16_BIT, 388 8 /* bufferCount */, 389 NULL, 390 NULL, 391 flags), 392 (status_t)OK); 393 mAudioSink->start(); 394 395 mRenderer->signalAudioSinkChanged(); 396 } else { 397 // video 398 399 int32_t width, height; 400 CHECK(codecRequest->findInt32("width", &width)); 401 CHECK(codecRequest->findInt32("height", &height)); 402 403 int32_t cropLeft, cropTop, cropRight, cropBottom; 404 CHECK(codecRequest->findRect( 405 "crop", 406 &cropLeft, &cropTop, &cropRight, &cropBottom)); 407 408 ALOGV("Video output format changed to %d x %d " 409 "(crop: %d x %d @ (%d, %d))", 410 width, height, 411 (cropRight - cropLeft + 1), 412 (cropBottom - cropTop + 1), 413 cropLeft, cropTop); 414 415 notifyListener( 416 MEDIA_SET_VIDEO_SIZE, 417 cropRight - cropLeft + 1, 418 cropBottom - cropTop + 1); 419 } 420 } else if (what == ACodec::kWhatShutdownCompleted) { 421 ALOGV("%s shutdown completed", audio ? "audio" : "video"); 422 if (audio) { 423 mAudioDecoder.clear(); 424 425 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER); 426 mFlushingAudio = SHUT_DOWN; 427 } else { 428 mVideoDecoder.clear(); 429 430 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER); 431 mFlushingVideo = SHUT_DOWN; 432 } 433 434 finishFlushIfPossible(); 435 } else if (what == ACodec::kWhatError) { 436 ALOGE("Received error from %s decoder, aborting playback.", 437 audio ? "audio" : "video"); 438 439 mRenderer->queueEOS(audio, UNKNOWN_ERROR); 440 } else if (what == ACodec::kWhatDrainThisBuffer) { 441 renderBuffer(audio, codecRequest); 442 } else { 443 ALOGV("Unhandled codec notification %d.", what); 444 } 445 446 break; 447 } 448 449 case kWhatRendererNotify: 450 { 451 int32_t what; 452 CHECK(msg->findInt32("what", &what)); 453 454 if (what == Renderer::kWhatEOS) { 455 int32_t audio; 456 CHECK(msg->findInt32("audio", &audio)); 457 458 int32_t finalResult; 459 CHECK(msg->findInt32("finalResult", &finalResult)); 460 461 if (audio) { 462 mAudioEOS = true; 463 } else { 464 mVideoEOS = true; 465 } 466 467 if (finalResult == ERROR_END_OF_STREAM) { 468 ALOGV("reached %s EOS", audio ? "audio" : "video"); 469 } else { 470 ALOGE("%s track encountered an error (%d)", 471 audio ? "audio" : "video", finalResult); 472 473 notifyListener( 474 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult); 475 } 476 477 if ((mAudioEOS || mAudioDecoder == NULL) 478 && (mVideoEOS || mVideoDecoder == NULL)) { 479 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 480 } 481 } else if (what == Renderer::kWhatPosition) { 482 int64_t positionUs; 483 CHECK(msg->findInt64("positionUs", &positionUs)); 484 485 CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs)); 486 487 if (mDriver != NULL) { 488 sp<NuPlayerDriver> driver = mDriver.promote(); 489 if (driver != NULL) { 490 driver->notifyPosition(positionUs); 491 492 driver->notifyFrameStats( 493 mNumFramesTotal, mNumFramesDropped); 494 } 495 } 496 } else if (what == Renderer::kWhatFlushComplete) { 497 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete); 498 499 int32_t audio; 500 CHECK(msg->findInt32("audio", &audio)); 501 502 ALOGV("renderer %s flush completed.", audio ? "audio" : "video"); 503 } else if (what == Renderer::kWhatVideoRenderingStart) { 504 notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0); 505 } 506 break; 507 } 508 509 case kWhatMoreDataQueued: 510 { 511 break; 512 } 513 514 case kWhatReset: 515 { 516 ALOGV("kWhatReset"); 517 518 if (mRenderer != NULL) { 519 // There's an edge case where the renderer owns all output 520 // buffers and is paused, therefore the decoder will not read 521 // more input data and will never encounter the matching 522 // discontinuity. To avoid this, we resume the renderer. 523 524 if (mFlushingAudio == AWAITING_DISCONTINUITY 525 || mFlushingVideo == AWAITING_DISCONTINUITY) { 526 mRenderer->resume(); 527 } 528 } 529 530 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 531 // We're currently flushing, postpone the reset until that's 532 // completed. 533 534 ALOGV("postponing reset mFlushingAudio=%d, mFlushingVideo=%d", 535 mFlushingAudio, mFlushingVideo); 536 537 mResetPostponed = true; 538 break; 539 } 540 541 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 542 finishReset(); 543 break; 544 } 545 546 mTimeDiscontinuityPending = true; 547 548 if (mAudioDecoder != NULL) { 549 flushDecoder(true /* audio */, true /* needShutdown */); 550 } 551 552 if (mVideoDecoder != NULL) { 553 flushDecoder(false /* audio */, true /* needShutdown */); 554 } 555 556 mResetInProgress = true; 557 break; 558 } 559 560 case kWhatSeek: 561 { 562 int64_t seekTimeUs; 563 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 564 565 ALOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)", 566 seekTimeUs, seekTimeUs / 1E6); 567 568 mSource->seekTo(seekTimeUs); 569 570 if (mDriver != NULL) { 571 sp<NuPlayerDriver> driver = mDriver.promote(); 572 if (driver != NULL) { 573 driver->notifySeekComplete(); 574 } 575 } 576 577 break; 578 } 579 580 case kWhatPause: 581 { 582 CHECK(mRenderer != NULL); 583 mRenderer->pause(); 584 break; 585 } 586 587 case kWhatResume: 588 { 589 CHECK(mRenderer != NULL); 590 mRenderer->resume(); 591 break; 592 } 593 594 default: 595 TRESPASS(); 596 break; 597 } 598} 599 600void NuPlayer::finishFlushIfPossible() { 601 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) { 602 return; 603 } 604 605 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) { 606 return; 607 } 608 609 ALOGV("both audio and video are flushed now."); 610 611 if (mTimeDiscontinuityPending) { 612 mRenderer->signalTimeDiscontinuity(); 613 mTimeDiscontinuityPending = false; 614 } 615 616 if (mAudioDecoder != NULL) { 617 mAudioDecoder->signalResume(); 618 } 619 620 if (mVideoDecoder != NULL) { 621 mVideoDecoder->signalResume(); 622 } 623 624 mFlushingAudio = NONE; 625 mFlushingVideo = NONE; 626 627 if (mResetInProgress) { 628 ALOGV("reset completed"); 629 630 mResetInProgress = false; 631 finishReset(); 632 } else if (mResetPostponed) { 633 (new AMessage(kWhatReset, id()))->post(); 634 mResetPostponed = false; 635 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 636 postScanSources(); 637 } 638} 639 640void NuPlayer::finishReset() { 641 CHECK(mAudioDecoder == NULL); 642 CHECK(mVideoDecoder == NULL); 643 644 ++mScanSourcesGeneration; 645 mScanSourcesPending = false; 646 647 mRenderer.clear(); 648 649 if (mSource != NULL) { 650 mSource->stop(); 651 mSource.clear(); 652 } 653 654 if (mDriver != NULL) { 655 sp<NuPlayerDriver> driver = mDriver.promote(); 656 if (driver != NULL) { 657 driver->notifyResetComplete(); 658 } 659 } 660} 661 662void NuPlayer::postScanSources() { 663 if (mScanSourcesPending) { 664 return; 665 } 666 667 sp<AMessage> msg = new AMessage(kWhatScanSources, id()); 668 msg->setInt32("generation", mScanSourcesGeneration); 669 msg->post(); 670 671 mScanSourcesPending = true; 672} 673 674status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) { 675 if (*decoder != NULL) { 676 return OK; 677 } 678 679 sp<MetaData> meta = mSource->getFormat(audio); 680 681 if (meta == NULL) { 682 return -EWOULDBLOCK; 683 } 684 685 if (!audio) { 686 const char *mime; 687 CHECK(meta->findCString(kKeyMIMEType, &mime)); 688 mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime); 689 } 690 691 sp<AMessage> notify = 692 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify, 693 id()); 694 695 *decoder = audio ? new Decoder(notify) : 696 new Decoder(notify, mNativeWindow); 697 looper()->registerHandler(*decoder); 698 699 (*decoder)->configure(meta); 700 701 int64_t durationUs; 702 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) { 703 sp<NuPlayerDriver> driver = mDriver.promote(); 704 if (driver != NULL) { 705 driver->notifyDuration(durationUs); 706 } 707 } 708 709 return OK; 710} 711 712status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) { 713 sp<AMessage> reply; 714 CHECK(msg->findMessage("reply", &reply)); 715 716 if ((audio && IsFlushingState(mFlushingAudio)) 717 || (!audio && IsFlushingState(mFlushingVideo))) { 718 reply->setInt32("err", INFO_DISCONTINUITY); 719 reply->post(); 720 return OK; 721 } 722 723 sp<ABuffer> accessUnit; 724 725 bool dropAccessUnit; 726 do { 727 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit); 728 729 if (err == -EWOULDBLOCK) { 730 return err; 731 } else if (err != OK) { 732 if (err == INFO_DISCONTINUITY) { 733 int32_t type; 734 CHECK(accessUnit->meta()->findInt32("discontinuity", &type)); 735 736 bool formatChange = 737 (audio && 738 (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT)) 739 || (!audio && 740 (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT)); 741 742 bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0; 743 744 ALOGI("%s discontinuity (formatChange=%d, time=%d)", 745 audio ? "audio" : "video", formatChange, timeChange); 746 747 if (audio) { 748 mSkipRenderingAudioUntilMediaTimeUs = -1; 749 } else { 750 mSkipRenderingVideoUntilMediaTimeUs = -1; 751 } 752 753 if (timeChange) { 754 sp<AMessage> extra; 755 if (accessUnit->meta()->findMessage("extra", &extra) 756 && extra != NULL) { 757 int64_t resumeAtMediaTimeUs; 758 if (extra->findInt64( 759 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) { 760 ALOGI("suppressing rendering of %s until %lld us", 761 audio ? "audio" : "video", resumeAtMediaTimeUs); 762 763 if (audio) { 764 mSkipRenderingAudioUntilMediaTimeUs = 765 resumeAtMediaTimeUs; 766 } else { 767 mSkipRenderingVideoUntilMediaTimeUs = 768 resumeAtMediaTimeUs; 769 } 770 } 771 } 772 } 773 774 mTimeDiscontinuityPending = 775 mTimeDiscontinuityPending || timeChange; 776 777 if (formatChange || timeChange) { 778 flushDecoder(audio, formatChange); 779 } else { 780 // This stream is unaffected by the discontinuity 781 782 if (audio) { 783 mFlushingAudio = FLUSHED; 784 } else { 785 mFlushingVideo = FLUSHED; 786 } 787 788 finishFlushIfPossible(); 789 790 return -EWOULDBLOCK; 791 } 792 } 793 794 reply->setInt32("err", err); 795 reply->post(); 796 return OK; 797 } 798 799 if (!audio) { 800 ++mNumFramesTotal; 801 } 802 803 dropAccessUnit = false; 804 if (!audio 805 && mVideoLateByUs > 100000ll 806 && mVideoIsAVC 807 && !IsAVCReferenceFrame(accessUnit)) { 808 dropAccessUnit = true; 809 ++mNumFramesDropped; 810 } 811 } while (dropAccessUnit); 812 813 // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video"); 814 815#if 0 816 int64_t mediaTimeUs; 817 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs)); 818 ALOGV("feeding %s input buffer at media time %.2f secs", 819 audio ? "audio" : "video", 820 mediaTimeUs / 1E6); 821#endif 822 823 reply->setBuffer("buffer", accessUnit); 824 reply->post(); 825 826 return OK; 827} 828 829void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) { 830 // ALOGV("renderBuffer %s", audio ? "audio" : "video"); 831 832 sp<AMessage> reply; 833 CHECK(msg->findMessage("reply", &reply)); 834 835 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) { 836 // We're currently attempting to flush the decoder, in order 837 // to complete this, the decoder wants all its buffers back, 838 // so we don't want any output buffers it sent us (from before 839 // we initiated the flush) to be stuck in the renderer's queue. 840 841 ALOGV("we're still flushing the %s decoder, sending its output buffer" 842 " right back.", audio ? "audio" : "video"); 843 844 reply->post(); 845 return; 846 } 847 848 sp<ABuffer> buffer; 849 CHECK(msg->findBuffer("buffer", &buffer)); 850 851 int64_t &skipUntilMediaTimeUs = 852 audio 853 ? mSkipRenderingAudioUntilMediaTimeUs 854 : mSkipRenderingVideoUntilMediaTimeUs; 855 856 if (skipUntilMediaTimeUs >= 0) { 857 int64_t mediaTimeUs; 858 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs)); 859 860 if (mediaTimeUs < skipUntilMediaTimeUs) { 861 ALOGV("dropping %s buffer at time %lld as requested.", 862 audio ? "audio" : "video", 863 mediaTimeUs); 864 865 reply->post(); 866 return; 867 } 868 869 skipUntilMediaTimeUs = -1; 870 } 871 872 mRenderer->queueBuffer(audio, buffer, reply); 873} 874 875void NuPlayer::notifyListener(int msg, int ext1, int ext2) { 876 if (mDriver == NULL) { 877 return; 878 } 879 880 sp<NuPlayerDriver> driver = mDriver.promote(); 881 882 if (driver == NULL) { 883 return; 884 } 885 886 driver->notifyListener(msg, ext1, ext2); 887} 888 889void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 890 if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) { 891 ALOGI("flushDecoder %s without decoder present", 892 audio ? "audio" : "video"); 893 } 894 895 // Make sure we don't continue to scan sources until we finish flushing. 896 ++mScanSourcesGeneration; 897 mScanSourcesPending = false; 898 899 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush(); 900 mRenderer->flush(audio); 901 902 FlushStatus newStatus = 903 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 904 905 if (audio) { 906 CHECK(mFlushingAudio == NONE 907 || mFlushingAudio == AWAITING_DISCONTINUITY); 908 909 mFlushingAudio = newStatus; 910 911 if (mFlushingVideo == NONE) { 912 mFlushingVideo = (mVideoDecoder != NULL) 913 ? AWAITING_DISCONTINUITY 914 : FLUSHED; 915 } 916 } else { 917 CHECK(mFlushingVideo == NONE 918 || mFlushingVideo == AWAITING_DISCONTINUITY); 919 920 mFlushingVideo = newStatus; 921 922 if (mFlushingAudio == NONE) { 923 mFlushingAudio = (mAudioDecoder != NULL) 924 ? AWAITING_DISCONTINUITY 925 : FLUSHED; 926 } 927 } 928} 929 930} // namespace android 931