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