NuPlayer.cpp revision 9b80c2bdb205bc143104f54d0743b6eedd67b14e
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, %d, %d, %d)", 320 width, height, 321 cropLeft, cropTop, cropRight, cropBottom); 322 323 notifyListener( 324 MEDIA_SET_VIDEO_SIZE, 325 cropRight - cropLeft + 1, 326 cropBottom - cropTop + 1); 327 } 328 } else if (what == ACodec::kWhatShutdownCompleted) { 329 LOGV("%s shutdown completed", audio ? "audio" : "video"); 330 if (audio) { 331 mAudioDecoder.clear(); 332 333 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER); 334 mFlushingAudio = SHUT_DOWN; 335 } else { 336 mVideoDecoder.clear(); 337 338 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER); 339 mFlushingVideo = SHUT_DOWN; 340 } 341 342 finishFlushIfPossible(); 343 } else { 344 CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer); 345 346 renderBuffer(audio, codecRequest); 347 } 348 349 break; 350 } 351 352 case kWhatRendererNotify: 353 { 354 int32_t what; 355 CHECK(msg->findInt32("what", &what)); 356 357 if (what == Renderer::kWhatEOS) { 358 int32_t audio; 359 CHECK(msg->findInt32("audio", &audio)); 360 361 if (audio) { 362 mAudioEOS = true; 363 } else { 364 mVideoEOS = true; 365 } 366 367 LOGV("reached %s EOS", audio ? "audio" : "video"); 368 369 if ((mAudioEOS || mAudioDecoder == NULL) 370 && (mVideoEOS || mVideoDecoder == NULL)) { 371 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 372 } 373 } else if (what == Renderer::kWhatPosition) { 374 int64_t positionUs; 375 CHECK(msg->findInt64("positionUs", &positionUs)); 376 377 if (mDriver != NULL) { 378 sp<NuPlayerDriver> driver = mDriver.promote(); 379 if (driver != NULL) { 380 driver->notifyPosition(positionUs); 381 } 382 } 383 } else { 384 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete); 385 386 int32_t audio; 387 CHECK(msg->findInt32("audio", &audio)); 388 389 LOGV("renderer %s flush completed.", audio ? "audio" : "video"); 390 } 391 break; 392 } 393 394 case kWhatMoreDataQueued: 395 { 396 break; 397 } 398 399 case kWhatReset: 400 { 401 LOGV("kWhatReset"); 402 403 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 404 // We're currently flushing, postpone the reset until that's 405 // completed. 406 407 LOGV("postponing reset"); 408 409 mResetPostponed = true; 410 break; 411 } 412 413 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 414 finishReset(); 415 break; 416 } 417 418 if (mAudioDecoder != NULL) { 419 flushDecoder(true /* audio */, true /* needShutdown */); 420 } 421 422 if (mVideoDecoder != NULL) { 423 flushDecoder(false /* audio */, true /* needShutdown */); 424 } 425 426 mResetInProgress = true; 427 break; 428 } 429 430 case kWhatSeek: 431 { 432 int64_t seekTimeUs; 433 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 434 435 LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)", 436 seekTimeUs, seekTimeUs / 1E6); 437 438 mSource->seekTo(seekTimeUs); 439 440 if (mDriver != NULL) { 441 sp<NuPlayerDriver> driver = mDriver.promote(); 442 if (driver != NULL) { 443 driver->notifySeekComplete(); 444 } 445 } 446 447 break; 448 } 449 450 case kWhatPause: 451 { 452 CHECK(mRenderer != NULL); 453 mRenderer->pause(); 454 break; 455 } 456 457 case kWhatResume: 458 { 459 CHECK(mRenderer != NULL); 460 mRenderer->resume(); 461 break; 462 } 463 464 default: 465 TRESPASS(); 466 break; 467 } 468} 469 470void NuPlayer::finishFlushIfPossible() { 471 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) { 472 return; 473 } 474 475 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) { 476 return; 477 } 478 479 LOGV("both audio and video are flushed now."); 480 481 mRenderer->signalTimeDiscontinuity(); 482 483 if (mAudioDecoder != NULL) { 484 mAudioDecoder->signalResume(); 485 } 486 487 if (mVideoDecoder != NULL) { 488 mVideoDecoder->signalResume(); 489 } 490 491 mFlushingAudio = NONE; 492 mFlushingVideo = NONE; 493 494 if (mResetInProgress) { 495 LOGV("reset completed"); 496 497 mResetInProgress = false; 498 finishReset(); 499 } else if (mResetPostponed) { 500 (new AMessage(kWhatReset, id()))->post(); 501 mResetPostponed = false; 502 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 503 postScanSources(); 504 } 505} 506 507void NuPlayer::finishReset() { 508 CHECK(mAudioDecoder == NULL); 509 CHECK(mVideoDecoder == NULL); 510 511 mRenderer.clear(); 512 mSource.clear(); 513 514 if (mDriver != NULL) { 515 sp<NuPlayerDriver> driver = mDriver.promote(); 516 if (driver != NULL) { 517 driver->notifyResetComplete(); 518 } 519 } 520} 521 522void NuPlayer::postScanSources() { 523 if (mScanSourcesPending) { 524 return; 525 } 526 527 sp<AMessage> msg = new AMessage(kWhatScanSources, id()); 528 msg->setInt32("generation", mScanSourcesGeneration); 529 msg->post(); 530 531 mScanSourcesPending = true; 532} 533 534status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) { 535 if (*decoder != NULL) { 536 return OK; 537 } 538 539 sp<MetaData> meta = mSource->getFormat(audio); 540 541 if (meta == NULL) { 542 return -EWOULDBLOCK; 543 } 544 545 sp<AMessage> notify = 546 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify, 547 id()); 548 549 *decoder = audio ? new Decoder(notify) : 550 new Decoder(notify, mNativeWindow); 551 looper()->registerHandler(*decoder); 552 553 (*decoder)->configure(meta); 554 555 int64_t durationUs; 556 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) { 557 sp<NuPlayerDriver> driver = mDriver.promote(); 558 if (driver != NULL) { 559 driver->notifyDuration(durationUs); 560 } 561 } 562 563 return OK; 564} 565 566status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) { 567 sp<AMessage> reply; 568 CHECK(msg->findMessage("reply", &reply)); 569 570 if ((audio && IsFlushingState(mFlushingAudio)) 571 || (!audio && IsFlushingState(mFlushingVideo))) { 572 reply->setInt32("err", INFO_DISCONTINUITY); 573 reply->post(); 574 return OK; 575 } 576 577 sp<ABuffer> accessUnit; 578 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit); 579 580 if (err == -EWOULDBLOCK) { 581 return err; 582 } else if (err != OK) { 583 if (err == INFO_DISCONTINUITY) { 584 int32_t type; 585 CHECK(accessUnit->meta()->findInt32("discontinuity", &type)); 586 587 bool formatChange = 588 type == ATSParser::DISCONTINUITY_FORMATCHANGE; 589 590 LOGV("%s discontinuity (formatChange=%d)", 591 audio ? "audio" : "video", formatChange); 592 593 if (audio) { 594 mSkipRenderingAudioUntilMediaTimeUs = -1; 595 } else { 596 mSkipRenderingVideoUntilMediaTimeUs = -1; 597 } 598 599 sp<AMessage> extra; 600 if (accessUnit->meta()->findMessage("extra", &extra) 601 && extra != NULL) { 602 int64_t resumeAtMediaTimeUs; 603 if (extra->findInt64( 604 "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) { 605 LOGI("suppressing rendering of %s until %lld us", 606 audio ? "audio" : "video", resumeAtMediaTimeUs); 607 608 if (audio) { 609 mSkipRenderingAudioUntilMediaTimeUs = 610 resumeAtMediaTimeUs; 611 } else { 612 mSkipRenderingVideoUntilMediaTimeUs = 613 resumeAtMediaTimeUs; 614 } 615 } 616 } 617 618 flushDecoder(audio, formatChange); 619 } 620 621 reply->setInt32("err", err); 622 reply->post(); 623 return OK; 624 } 625 626 // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video"); 627 628#if 0 629 int64_t mediaTimeUs; 630 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs)); 631 LOGV("feeding %s input buffer at media time %.2f secs", 632 audio ? "audio" : "video", 633 mediaTimeUs / 1E6); 634#endif 635 636 reply->setObject("buffer", accessUnit); 637 reply->post(); 638 639 return OK; 640} 641 642void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) { 643 // LOGV("renderBuffer %s", audio ? "audio" : "video"); 644 645 sp<AMessage> reply; 646 CHECK(msg->findMessage("reply", &reply)); 647 648 sp<RefBase> obj; 649 CHECK(msg->findObject("buffer", &obj)); 650 651 sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get()); 652 653 int64_t &skipUntilMediaTimeUs = 654 audio 655 ? mSkipRenderingAudioUntilMediaTimeUs 656 : mSkipRenderingVideoUntilMediaTimeUs; 657 658 if (skipUntilMediaTimeUs >= 0) { 659 int64_t mediaTimeUs; 660 CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs)); 661 662 if (mediaTimeUs < skipUntilMediaTimeUs) { 663 LOGV("dropping %s buffer at time %lld as requested.", 664 audio ? "audio" : "video", 665 mediaTimeUs); 666 667 reply->post(); 668 return; 669 } 670 671 skipUntilMediaTimeUs = -1; 672 } 673 674 mRenderer->queueBuffer(audio, buffer, reply); 675} 676 677void NuPlayer::notifyListener(int msg, int ext1, int ext2) { 678 if (mDriver == NULL) { 679 return; 680 } 681 682 sp<NuPlayerDriver> driver = mDriver.promote(); 683 684 if (driver == NULL) { 685 return; 686 } 687 688 driver->sendEvent(msg, ext1, ext2); 689} 690 691void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 692 // Make sure we don't continue to scan sources until we finish flushing. 693 ++mScanSourcesGeneration; 694 mScanSourcesPending = false; 695 696 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush(); 697 mRenderer->flush(audio); 698 699 FlushStatus newStatus = 700 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 701 702 if (audio) { 703 CHECK(mFlushingAudio == NONE 704 || mFlushingAudio == AWAITING_DISCONTINUITY); 705 706 mFlushingAudio = newStatus; 707 708 if (mFlushingVideo == NONE) { 709 mFlushingVideo = (mVideoDecoder != NULL) 710 ? AWAITING_DISCONTINUITY 711 : FLUSHED; 712 } 713 } else { 714 CHECK(mFlushingVideo == NONE 715 || mFlushingVideo == AWAITING_DISCONTINUITY); 716 717 mFlushingVideo = newStatus; 718 719 if (mFlushingAudio == NONE) { 720 mFlushingAudio = (mAudioDecoder != NULL) 721 ? AWAITING_DISCONTINUITY 722 : FLUSHED; 723 } 724 } 725} 726 727} // namespace android 728