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