NuPlayer.cpp revision 18ac5407da14dad9731f40ffc9a56bee73830019
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 "StreamingSource.h" 29 30#include "ATSParser.h" 31 32#include <media/stagefright/foundation/hexdump.h> 33#include <media/stagefright/foundation/ABuffer.h> 34#include <media/stagefright/foundation/ADebug.h> 35#include <media/stagefright/foundation/AMessage.h> 36#include <media/stagefright/ACodec.h> 37#include <media/stagefright/MediaErrors.h> 38#include <media/stagefright/MetaData.h> 39#include <surfaceflinger/Surface.h> 40#include <gui/ISurfaceTexture.h> 41 42namespace android { 43 44//////////////////////////////////////////////////////////////////////////////// 45 46NuPlayer::NuPlayer() 47 : mUIDValid(false), 48 mAudioEOS(false), 49 mVideoEOS(false), 50 mScanSourcesPending(false), 51 mScanSourcesGeneration(0), 52 mFlushingAudio(NONE), 53 mFlushingVideo(NONE), 54 mResetInProgress(false), 55 mResetPostponed(false) { 56} 57 58NuPlayer::~NuPlayer() { 59} 60 61void NuPlayer::setUID(uid_t uid) { 62 mUIDValid = true; 63 mUID = uid; 64} 65 66void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) { 67 mDriver = driver; 68} 69 70void NuPlayer::setDataSource(const sp<IStreamSource> &source) { 71 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); 72 73 msg->setObject("source", new StreamingSource(source)); 74 msg->post(); 75} 76 77void NuPlayer::setDataSource( 78 const char *url, const KeyedVector<String8, String8> *headers) { 79 sp<AMessage> msg = new AMessage(kWhatSetDataSource, id()); 80 81 msg->setObject("source", new HTTPLiveSource(url, headers, mUIDValid, mUID)); 82 msg->post(); 83} 84 85void NuPlayer::setVideoSurface(const sp<Surface> &surface) { 86 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id()); 87 msg->setObject("native-window", new NativeWindowWrapper(surface)); 88 msg->post(); 89} 90 91void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) { 92 sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id()); 93 sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ? 94 new SurfaceTextureClient(surfaceTexture) : NULL); 95 msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient)); 96 msg->post(); 97} 98 99void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) { 100 sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id()); 101 msg->setObject("sink", sink); 102 msg->post(); 103} 104 105void NuPlayer::start() { 106 (new AMessage(kWhatStart, id()))->post(); 107} 108 109void NuPlayer::pause() { 110 (new AMessage(kWhatPause, id()))->post(); 111} 112 113void NuPlayer::resume() { 114 (new AMessage(kWhatResume, id()))->post(); 115} 116 117void NuPlayer::resetAsync() { 118 (new AMessage(kWhatReset, id()))->post(); 119} 120 121void NuPlayer::seekToAsync(int64_t seekTimeUs) { 122 sp<AMessage> msg = new AMessage(kWhatSeek, id()); 123 msg->setInt64("seekTimeUs", seekTimeUs); 124 msg->post(); 125} 126 127// static 128bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) { 129 switch (state) { 130 case FLUSHING_DECODER: 131 if (needShutdown != NULL) { 132 *needShutdown = false; 133 } 134 return true; 135 136 case FLUSHING_DECODER_SHUTDOWN: 137 if (needShutdown != NULL) { 138 *needShutdown = true; 139 } 140 return true; 141 142 default: 143 return false; 144 } 145} 146 147void NuPlayer::onMessageReceived(const sp<AMessage> &msg) { 148 switch (msg->what()) { 149 case kWhatSetDataSource: 150 { 151 LOGV("kWhatSetDataSource"); 152 153 CHECK(mSource == NULL); 154 155 sp<RefBase> obj; 156 CHECK(msg->findObject("source", &obj)); 157 158 mSource = static_cast<Source *>(obj.get()); 159 break; 160 } 161 162 case kWhatSetVideoNativeWindow: 163 { 164 LOGV("kWhatSetVideoNativeWindow"); 165 166 sp<RefBase> obj; 167 CHECK(msg->findObject("native-window", &obj)); 168 169 mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get()); 170 break; 171 } 172 173 case kWhatSetAudioSink: 174 { 175 LOGV("kWhatSetAudioSink"); 176 177 sp<RefBase> obj; 178 CHECK(msg->findObject("sink", &obj)); 179 180 mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get()); 181 break; 182 } 183 184 case kWhatStart: 185 { 186 LOGV("kWhatStart"); 187 188 mAudioEOS = false; 189 mVideoEOS = false; 190 mSkipRenderingAudioUntilMediaTimeUs = -1; 191 mSkipRenderingVideoUntilMediaTimeUs = -1; 192 193 mSource->start(); 194 195 mRenderer = new Renderer( 196 mAudioSink, 197 new AMessage(kWhatRendererNotify, id())); 198 199 looper()->registerHandler(mRenderer); 200 201 postScanSources(); 202 break; 203 } 204 205 case kWhatScanSources: 206 { 207 int32_t generation; 208 CHECK(msg->findInt32("generation", &generation)); 209 if (generation != mScanSourcesGeneration) { 210 // Drop obsolete msg. 211 break; 212 } 213 214 mScanSourcesPending = false; 215 216 LOGV("scanning sources haveAudio=%d, haveVideo=%d", 217 mAudioDecoder != NULL, mVideoDecoder != NULL); 218 219 instantiateDecoder(false, &mVideoDecoder); 220 221 if (mAudioSink != NULL) { 222 instantiateDecoder(true, &mAudioDecoder); 223 } 224 225 if (!mSource->feedMoreTSData()) { 226 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 227 // We're not currently decoding anything (no audio or 228 // video tracks found) and we just ran out of input data. 229 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 230 } 231 break; 232 } 233 234 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 235 msg->post(100000ll); 236 mScanSourcesPending = true; 237 } 238 break; 239 } 240 241 case kWhatVideoNotify: 242 case kWhatAudioNotify: 243 { 244 bool audio = msg->what() == kWhatAudioNotify; 245 246 sp<AMessage> codecRequest; 247 CHECK(msg->findMessage("codec-request", &codecRequest)); 248 249 int32_t what; 250 CHECK(codecRequest->findInt32("what", &what)); 251 252 if (what == ACodec::kWhatFillThisBuffer) { 253 status_t err = feedDecoderInputData( 254 audio, codecRequest); 255 256 if (err == -EWOULDBLOCK) { 257 if (mSource->feedMoreTSData()) { 258 msg->post(); 259 } 260 } 261 } else if (what == ACodec::kWhatEOS) { 262 mRenderer->queueEOS(audio, ERROR_END_OF_STREAM); 263 } else if (what == ACodec::kWhatFlushCompleted) { 264 bool needShutdown; 265 266 if (audio) { 267 CHECK(IsFlushingState(mFlushingAudio, &needShutdown)); 268 mFlushingAudio = FLUSHED; 269 } else { 270 CHECK(IsFlushingState(mFlushingVideo, &needShutdown)); 271 mFlushingVideo = FLUSHED; 272 } 273 274 LOGV("decoder %s flush completed", audio ? "audio" : "video"); 275 276 if (needShutdown) { 277 LOGV("initiating %s decoder shutdown", 278 audio ? "audio" : "video"); 279 280 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown(); 281 282 if (audio) { 283 mFlushingAudio = SHUTTING_DOWN_DECODER; 284 } else { 285 mFlushingVideo = SHUTTING_DOWN_DECODER; 286 } 287 } 288 289 finishFlushIfPossible(); 290 } else if (what == ACodec::kWhatOutputFormatChanged) { 291 if (audio) { 292 int32_t numChannels; 293 CHECK(codecRequest->findInt32("channel-count", &numChannels)); 294 295 int32_t sampleRate; 296 CHECK(codecRequest->findInt32("sample-rate", &sampleRate)); 297 298 LOGV("Audio output format changed to %d Hz, %d channels", 299 sampleRate, numChannels); 300 301 mAudioSink->close(); 302 CHECK_EQ(mAudioSink->open(sampleRate, numChannels), (status_t)OK); 303 mAudioSink->start(); 304 305 mRenderer->signalAudioSinkChanged(); 306 } else { 307 // video 308 309 int32_t width, height; 310 CHECK(codecRequest->findInt32("width", &width)); 311 CHECK(codecRequest->findInt32("height", &height)); 312 313 int32_t cropLeft, cropTop, cropRight, cropBottom; 314 CHECK(codecRequest->findRect( 315 "crop", 316 &cropLeft, &cropTop, &cropRight, &cropBottom)); 317 318 LOGV("Video output format changed to %d x %d " 319 "(crop: %d x %d @ (%d, %d))", 320 width, height, 321 (cropRight - cropLeft + 1), 322 (cropBottom - cropTop + 1), 323 cropLeft, cropTop); 324 325 notifyListener( 326 MEDIA_SET_VIDEO_SIZE, 327 cropRight - cropLeft + 1, 328 cropBottom - cropTop + 1); 329 } 330 } else if (what == ACodec::kWhatShutdownCompleted) { 331 LOGV("%s shutdown completed", audio ? "audio" : "video"); 332 if (audio) { 333 mAudioDecoder.clear(); 334 335 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER); 336 mFlushingAudio = SHUT_DOWN; 337 } else { 338 mVideoDecoder.clear(); 339 340 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER); 341 mFlushingVideo = SHUT_DOWN; 342 } 343 344 finishFlushIfPossible(); 345 } else if (what == ACodec::kWhatError) { 346 LOGE("Received error from %s decoder, aborting playback.", 347 audio ? "audio" : "video"); 348 349 mRenderer->queueEOS(audio, UNKNOWN_ERROR); 350 } else { 351 CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer); 352 353 renderBuffer(audio, codecRequest); 354 } 355 356 break; 357 } 358 359 case kWhatRendererNotify: 360 { 361 int32_t what; 362 CHECK(msg->findInt32("what", &what)); 363 364 if (what == Renderer::kWhatEOS) { 365 int32_t audio; 366 CHECK(msg->findInt32("audio", &audio)); 367 368 int32_t finalResult; 369 CHECK(msg->findInt32("finalResult", &finalResult)); 370 371 if (audio) { 372 mAudioEOS = true; 373 } else { 374 mVideoEOS = true; 375 } 376 377 if (finalResult == ERROR_END_OF_STREAM) { 378 LOGV("reached %s EOS", audio ? "audio" : "video"); 379 } else { 380 LOGE("%s track encountered an error (0x%08x)", 381 audio ? "audio" : "video", finalResult); 382 383 notifyListener( 384 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult); 385 } 386 387 if ((mAudioEOS || mAudioDecoder == NULL) 388 && (mVideoEOS || mVideoDecoder == NULL)) { 389 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 390 } 391 } else if (what == Renderer::kWhatPosition) { 392 int64_t positionUs; 393 CHECK(msg->findInt64("positionUs", &positionUs)); 394 395 if (mDriver != NULL) { 396 sp<NuPlayerDriver> driver = mDriver.promote(); 397 if (driver != NULL) { 398 driver->notifyPosition(positionUs); 399 } 400 } 401 } else { 402 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete); 403 404 int32_t audio; 405 CHECK(msg->findInt32("audio", &audio)); 406 407 LOGV("renderer %s flush completed.", audio ? "audio" : "video"); 408 } 409 break; 410 } 411 412 case kWhatMoreDataQueued: 413 { 414 break; 415 } 416 417 case kWhatReset: 418 { 419 LOGV("kWhatReset"); 420 421 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 422 // We're currently flushing, postpone the reset until that's 423 // completed. 424 425 LOGV("postponing reset"); 426 427 mResetPostponed = true; 428 break; 429 } 430 431 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 432 finishReset(); 433 break; 434 } 435 436 if (mAudioDecoder != NULL) { 437 flushDecoder(true /* audio */, true /* needShutdown */); 438 } 439 440 if (mVideoDecoder != NULL) { 441 flushDecoder(false /* audio */, true /* needShutdown */); 442 } 443 444 mResetInProgress = true; 445 break; 446 } 447 448 case kWhatSeek: 449 { 450 int64_t seekTimeUs; 451 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 452 453 LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)", 454 seekTimeUs, seekTimeUs / 1E6); 455 456 mSource->seekTo(seekTimeUs); 457 458 if (mDriver != NULL) { 459 sp<NuPlayerDriver> driver = mDriver.promote(); 460 if (driver != NULL) { 461 driver->notifySeekComplete(); 462 } 463 } 464 465 break; 466 } 467 468 case kWhatPause: 469 { 470 CHECK(mRenderer != NULL); 471 mRenderer->pause(); 472 break; 473 } 474 475 case kWhatResume: 476 { 477 CHECK(mRenderer != NULL); 478 mRenderer->resume(); 479 break; 480 } 481 482 default: 483 TRESPASS(); 484 break; 485 } 486} 487 488void NuPlayer::finishFlushIfPossible() { 489 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) { 490 return; 491 } 492 493 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) { 494 return; 495 } 496 497 LOGV("both audio and video are flushed now."); 498 499 mRenderer->signalTimeDiscontinuity(); 500 501 if (mAudioDecoder != NULL) { 502 mAudioDecoder->signalResume(); 503 } 504 505 if (mVideoDecoder != NULL) { 506 mVideoDecoder->signalResume(); 507 } 508 509 mFlushingAudio = NONE; 510 mFlushingVideo = NONE; 511 512 if (mResetInProgress) { 513 LOGV("reset completed"); 514 515 mResetInProgress = false; 516 finishReset(); 517 } else if (mResetPostponed) { 518 (new AMessage(kWhatReset, id()))->post(); 519 mResetPostponed = false; 520 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 521 postScanSources(); 522 } 523} 524 525void NuPlayer::finishReset() { 526 CHECK(mAudioDecoder == NULL); 527 CHECK(mVideoDecoder == NULL); 528 529 mRenderer.clear(); 530 mSource.clear(); 531 532 if (mDriver != NULL) { 533 sp<NuPlayerDriver> driver = mDriver.promote(); 534 if (driver != NULL) { 535 driver->notifyResetComplete(); 536 } 537 } 538} 539 540void NuPlayer::postScanSources() { 541 if (mScanSourcesPending) { 542 return; 543 } 544 545 sp<AMessage> msg = new AMessage(kWhatScanSources, id()); 546 msg->setInt32("generation", mScanSourcesGeneration); 547 msg->post(); 548 549 mScanSourcesPending = true; 550} 551 552status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) { 553 if (*decoder != NULL) { 554 return OK; 555 } 556 557 sp<MetaData> meta = mSource->getFormat(audio); 558 559 if (meta == NULL) { 560 return -EWOULDBLOCK; 561 } 562 563 sp<AMessage> notify = 564 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify, 565 id()); 566 567 *decoder = audio ? new Decoder(notify) : 568 new Decoder(notify, mNativeWindow); 569 looper()->registerHandler(*decoder); 570 571 (*decoder)->configure(meta); 572 573 int64_t durationUs; 574 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) { 575 sp<NuPlayerDriver> driver = mDriver.promote(); 576 if (driver != NULL) { 577 driver->notifyDuration(durationUs); 578 } 579 } 580 581 return OK; 582} 583 584status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) { 585 sp<AMessage> reply; 586 CHECK(msg->findMessage("reply", &reply)); 587 588 if ((audio && IsFlushingState(mFlushingAudio)) 589 || (!audio && IsFlushingState(mFlushingVideo))) { 590 reply->setInt32("err", INFO_DISCONTINUITY); 591 reply->post(); 592 return OK; 593 } 594 595 sp<ABuffer> accessUnit; 596 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit); 597 598 if (err == -EWOULDBLOCK) { 599 return err; 600 } else if (err != OK) { 601 if (err == INFO_DISCONTINUITY) { 602 int32_t type; 603 CHECK(accessUnit->meta()->findInt32("discontinuity", &type)); 604 605 bool formatChange = 606 type == ATSParser::DISCONTINUITY_FORMATCHANGE; 607 608 LOGV("%s discontinuity (formatChange=%d)", 609 audio ? "audio" : "video", formatChange); 610 611 if (audio) { 612 mSkipRenderingAudioUntilMediaTimeUs = -1; 613 } else { 614 mSkipRenderingVideoUntilMediaTimeUs = -1; 615 } 616 617 sp<AMessage> extra; 618 if (accessUnit->meta()->findMessage("extra", &extra) 619 && extra != NULL) { 620 int64_t resumeAtMediaTimeUs; 621 if (extra->findInt64( 622 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) { 623 LOGI("suppressing rendering of %s until %lld us", 624 audio ? "audio" : "video", resumeAtMediaTimeUs); 625 626 if (audio) { 627 mSkipRenderingAudioUntilMediaTimeUs = 628 resumeAtMediaTimeUs; 629 } else { 630 mSkipRenderingVideoUntilMediaTimeUs = 631 resumeAtMediaTimeUs; 632 } 633 } 634 } 635 636 flushDecoder(audio, formatChange); 637 } 638 639 reply->setInt32("err", err); 640 reply->post(); 641 return OK; 642 } 643 644 // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video"); 645 646#if 0 647 int64_t mediaTimeUs; 648 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs)); 649 LOGV("feeding %s input buffer at media time %.2f secs", 650 audio ? "audio" : "video", 651 mediaTimeUs / 1E6); 652#endif 653 654 reply->setObject("buffer", accessUnit); 655 reply->post(); 656 657 return OK; 658} 659 660void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) { 661 // LOGV("renderBuffer %s", audio ? "audio" : "video"); 662 663 sp<AMessage> reply; 664 CHECK(msg->findMessage("reply", &reply)); 665 666 if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) { 667 // We're currently attempting to flush the decoder, in order 668 // to complete this, the decoder wants all its buffers back, 669 // so we don't want any output buffers it sent us (from before 670 // we initiated the flush) to be stuck in the renderer's queue. 671 672 LOGV("we're still flushing the %s decoder, sending its output buffer" 673 " right back.", audio ? "audio" : "video"); 674 675 reply->post(); 676 return; 677 } 678 679 sp<RefBase> obj; 680 CHECK(msg->findObject("buffer", &obj)); 681 682 sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get()); 683 684 int64_t &skipUntilMediaTimeUs = 685 audio 686 ? mSkipRenderingAudioUntilMediaTimeUs 687 : mSkipRenderingVideoUntilMediaTimeUs; 688 689 if (skipUntilMediaTimeUs >= 0) { 690 int64_t mediaTimeUs; 691 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs)); 692 693 if (mediaTimeUs < skipUntilMediaTimeUs) { 694 LOGV("dropping %s buffer at time %lld as requested.", 695 audio ? "audio" : "video", 696 mediaTimeUs); 697 698 reply->post(); 699 return; 700 } 701 702 skipUntilMediaTimeUs = -1; 703 } 704 705 mRenderer->queueBuffer(audio, buffer, reply); 706} 707 708void NuPlayer::notifyListener(int msg, int ext1, int ext2) { 709 if (mDriver == NULL) { 710 return; 711 } 712 713 sp<NuPlayerDriver> driver = mDriver.promote(); 714 715 if (driver == NULL) { 716 return; 717 } 718 719 driver->sendEvent(msg, ext1, ext2); 720} 721 722void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 723 // Make sure we don't continue to scan sources until we finish flushing. 724 ++mScanSourcesGeneration; 725 mScanSourcesPending = false; 726 727 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush(); 728 mRenderer->flush(audio); 729 730 FlushStatus newStatus = 731 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 732 733 if (audio) { 734 CHECK(mFlushingAudio == NONE 735 || mFlushingAudio == AWAITING_DISCONTINUITY); 736 737 mFlushingAudio = newStatus; 738 739 if (mFlushingVideo == NONE) { 740 mFlushingVideo = (mVideoDecoder != NULL) 741 ? AWAITING_DISCONTINUITY 742 : FLUSHED; 743 } 744 } else { 745 CHECK(mFlushingVideo == NONE 746 || mFlushingVideo == AWAITING_DISCONTINUITY); 747 748 mFlushingVideo = newStatus; 749 750 if (mFlushingAudio == NONE) { 751 mFlushingAudio = (mAudioDecoder != NULL) 752 ? AWAITING_DISCONTINUITY 753 : FLUSHED; 754 } 755 } 756} 757 758} // namespace android 759