NuPlayer.cpp revision 1173118eace0e9e347cb007f0da817cee87579ed
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)); 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 185 mSource->start(); 186 187 mRenderer = new Renderer( 188 mAudioSink, 189 new AMessage(kWhatRendererNotify, id())); 190 191 looper()->registerHandler(mRenderer); 192 193 postScanSources(); 194 break; 195 } 196 197 case kWhatScanSources: 198 { 199 int32_t generation; 200 CHECK(msg->findInt32("generation", &generation)); 201 if (generation != mScanSourcesGeneration) { 202 // Drop obsolete msg. 203 break; 204 } 205 206 mScanSourcesPending = false; 207 208 LOGV("scanning sources haveAudio=%d, haveVideo=%d", 209 mAudioDecoder != NULL, mVideoDecoder != NULL); 210 211 instantiateDecoder(false, &mVideoDecoder); 212 213 if (mAudioSink != NULL) { 214 instantiateDecoder(true, &mAudioDecoder); 215 } 216 217 if (!mSource->feedMoreTSData()) { 218 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 219 // We're not currently decoding anything (no audio or 220 // video tracks found) and we just ran out of input data. 221 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 222 } 223 break; 224 } 225 226 if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 227 msg->post(100000ll); 228 mScanSourcesPending = true; 229 } 230 break; 231 } 232 233 case kWhatVideoNotify: 234 case kWhatAudioNotify: 235 { 236 bool audio = msg->what() == kWhatAudioNotify; 237 238 sp<AMessage> codecRequest; 239 CHECK(msg->findMessage("codec-request", &codecRequest)); 240 241 int32_t what; 242 CHECK(codecRequest->findInt32("what", &what)); 243 244 if (what == ACodec::kWhatFillThisBuffer) { 245 status_t err = feedDecoderInputData( 246 audio, codecRequest); 247 248 if (err == -EWOULDBLOCK) { 249 if (mSource->feedMoreTSData()) { 250 msg->post(); 251 } 252 } 253 } else if (what == ACodec::kWhatEOS) { 254 mRenderer->queueEOS(audio, ERROR_END_OF_STREAM); 255 } else if (what == ACodec::kWhatFlushCompleted) { 256 bool needShutdown; 257 258 if (audio) { 259 CHECK(IsFlushingState(mFlushingAudio, &needShutdown)); 260 mFlushingAudio = FLUSHED; 261 } else { 262 CHECK(IsFlushingState(mFlushingVideo, &needShutdown)); 263 mFlushingVideo = FLUSHED; 264 } 265 266 LOGV("decoder %s flush completed", audio ? "audio" : "video"); 267 268 if (needShutdown) { 269 LOGV("initiating %s decoder shutdown", 270 audio ? "audio" : "video"); 271 272 (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown(); 273 274 if (audio) { 275 mFlushingAudio = SHUTTING_DOWN_DECODER; 276 } else { 277 mFlushingVideo = SHUTTING_DOWN_DECODER; 278 } 279 } 280 281 finishFlushIfPossible(); 282 } else if (what == ACodec::kWhatOutputFormatChanged) { 283 if (audio) { 284 int32_t numChannels; 285 CHECK(codecRequest->findInt32("channel-count", &numChannels)); 286 287 int32_t sampleRate; 288 CHECK(codecRequest->findInt32("sample-rate", &sampleRate)); 289 290 LOGV("Audio output format changed to %d Hz, %d channels", 291 sampleRate, numChannels); 292 293 mAudioSink->close(); 294 CHECK_EQ(mAudioSink->open(sampleRate, numChannels), (status_t)OK); 295 mAudioSink->start(); 296 297 mRenderer->signalAudioSinkChanged(); 298 } else { 299 // video 300 301 int32_t width, height; 302 CHECK(codecRequest->findInt32("width", &width)); 303 CHECK(codecRequest->findInt32("height", &height)); 304 305 int32_t cropLeft, cropTop, cropRight, cropBottom; 306 CHECK(codecRequest->findRect( 307 "crop", 308 &cropLeft, &cropTop, &cropRight, &cropBottom)); 309 310 LOGV("Video output format changed to %d x %d " 311 "(crop: %d, %d, %d, %d)", 312 width, height, 313 cropLeft, cropTop, cropRight, cropBottom); 314 315 notifyListener( 316 MEDIA_SET_VIDEO_SIZE, 317 cropRight - cropLeft + 1, 318 cropBottom - cropTop + 1); 319 } 320 } else if (what == ACodec::kWhatShutdownCompleted) { 321 LOGV("%s shutdown completed", audio ? "audio" : "video"); 322 if (audio) { 323 mAudioDecoder.clear(); 324 325 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER); 326 mFlushingAudio = SHUT_DOWN; 327 } else { 328 mVideoDecoder.clear(); 329 330 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER); 331 mFlushingVideo = SHUT_DOWN; 332 } 333 334 finishFlushIfPossible(); 335 } else { 336 CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer); 337 338 renderBuffer(audio, codecRequest); 339 } 340 341 break; 342 } 343 344 case kWhatRendererNotify: 345 { 346 int32_t what; 347 CHECK(msg->findInt32("what", &what)); 348 349 if (what == Renderer::kWhatEOS) { 350 int32_t audio; 351 CHECK(msg->findInt32("audio", &audio)); 352 353 if (audio) { 354 mAudioEOS = true; 355 } else { 356 mVideoEOS = true; 357 } 358 359 LOGV("reached %s EOS", audio ? "audio" : "video"); 360 361 if ((mAudioEOS || mAudioDecoder == NULL) 362 && (mVideoEOS || mVideoDecoder == NULL)) { 363 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 364 } 365 } else if (what == Renderer::kWhatPosition) { 366 int64_t positionUs; 367 CHECK(msg->findInt64("positionUs", &positionUs)); 368 369 if (mDriver != NULL) { 370 sp<NuPlayerDriver> driver = mDriver.promote(); 371 if (driver != NULL) { 372 driver->notifyPosition(positionUs); 373 } 374 } 375 } else { 376 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete); 377 378 int32_t audio; 379 CHECK(msg->findInt32("audio", &audio)); 380 381 LOGV("renderer %s flush completed.", audio ? "audio" : "video"); 382 } 383 break; 384 } 385 386 case kWhatMoreDataQueued: 387 { 388 break; 389 } 390 391 case kWhatReset: 392 { 393 LOGV("kWhatReset"); 394 395 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 396 // We're currently flushing, postpone the reset until that's 397 // completed. 398 399 LOGV("postponing reset"); 400 401 mResetPostponed = true; 402 break; 403 } 404 405 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 406 finishReset(); 407 break; 408 } 409 410 if (mAudioDecoder != NULL) { 411 flushDecoder(true /* audio */, true /* needShutdown */); 412 } 413 414 if (mVideoDecoder != NULL) { 415 flushDecoder(false /* audio */, true /* needShutdown */); 416 } 417 418 mResetInProgress = true; 419 break; 420 } 421 422 case kWhatSeek: 423 { 424 int64_t seekTimeUs; 425 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 426 427 LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)", 428 seekTimeUs, seekTimeUs / 1E6); 429 430 mSource->seekTo(seekTimeUs); 431 432 if (mDriver != NULL) { 433 sp<NuPlayerDriver> driver = mDriver.promote(); 434 if (driver != NULL) { 435 driver->notifySeekComplete(); 436 } 437 } 438 439 break; 440 } 441 442 case kWhatPause: 443 { 444 CHECK(mRenderer != NULL); 445 mRenderer->pause(); 446 break; 447 } 448 449 case kWhatResume: 450 { 451 CHECK(mRenderer != NULL); 452 mRenderer->resume(); 453 break; 454 } 455 456 default: 457 TRESPASS(); 458 break; 459 } 460} 461 462void NuPlayer::finishFlushIfPossible() { 463 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) { 464 return; 465 } 466 467 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) { 468 return; 469 } 470 471 LOGV("both audio and video are flushed now."); 472 473 mRenderer->signalTimeDiscontinuity(); 474 475 if (mAudioDecoder != NULL) { 476 mAudioDecoder->signalResume(); 477 } 478 479 if (mVideoDecoder != NULL) { 480 mVideoDecoder->signalResume(); 481 } 482 483 mFlushingAudio = NONE; 484 mFlushingVideo = NONE; 485 486 if (mResetInProgress) { 487 LOGV("reset completed"); 488 489 mResetInProgress = false; 490 finishReset(); 491 } else if (mResetPostponed) { 492 (new AMessage(kWhatReset, id()))->post(); 493 mResetPostponed = false; 494 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 495 postScanSources(); 496 } 497} 498 499void NuPlayer::finishReset() { 500 CHECK(mAudioDecoder == NULL); 501 CHECK(mVideoDecoder == NULL); 502 503 mRenderer.clear(); 504 mSource.clear(); 505 506 if (mDriver != NULL) { 507 sp<NuPlayerDriver> driver = mDriver.promote(); 508 if (driver != NULL) { 509 driver->notifyResetComplete(); 510 } 511 } 512} 513 514void NuPlayer::postScanSources() { 515 if (mScanSourcesPending) { 516 return; 517 } 518 519 sp<AMessage> msg = new AMessage(kWhatScanSources, id()); 520 msg->setInt32("generation", mScanSourcesGeneration); 521 msg->post(); 522 523 mScanSourcesPending = true; 524} 525 526status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) { 527 if (*decoder != NULL) { 528 return OK; 529 } 530 531 sp<MetaData> meta = mSource->getFormat(audio); 532 533 if (meta == NULL) { 534 return -EWOULDBLOCK; 535 } 536 537 sp<AMessage> notify = 538 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify, 539 id()); 540 541 *decoder = audio ? new Decoder(notify) : 542 new Decoder(notify, mNativeWindow); 543 looper()->registerHandler(*decoder); 544 545 (*decoder)->configure(meta); 546 547 int64_t durationUs; 548 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) { 549 sp<NuPlayerDriver> driver = mDriver.promote(); 550 if (driver != NULL) { 551 driver->notifyDuration(durationUs); 552 } 553 } 554 555 return OK; 556} 557 558status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) { 559 sp<AMessage> reply; 560 CHECK(msg->findMessage("reply", &reply)); 561 562 if ((audio && IsFlushingState(mFlushingAudio)) 563 || (!audio && IsFlushingState(mFlushingVideo))) { 564 reply->setInt32("err", INFO_DISCONTINUITY); 565 reply->post(); 566 return OK; 567 } 568 569 sp<ABuffer> accessUnit; 570 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit); 571 572 if (err == -EWOULDBLOCK) { 573 return err; 574 } else if (err != OK) { 575 if (err == INFO_DISCONTINUITY) { 576 int32_t type; 577 CHECK(accessUnit->meta()->findInt32("discontinuity", &type)); 578 579 bool formatChange = 580 type == ATSParser::DISCONTINUITY_FORMATCHANGE; 581 582 LOGV("%s discontinuity (formatChange=%d)", 583 audio ? "audio" : "video", formatChange); 584 585 flushDecoder(audio, formatChange); 586 } 587 588 reply->setInt32("err", err); 589 reply->post(); 590 return OK; 591 } 592 593 // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video"); 594 595#if 0 596 int64_t mediaTimeUs; 597 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs)); 598 LOGV("feeding %s input buffer at media time %.2f secs", 599 audio ? "audio" : "video", 600 mediaTimeUs / 1E6); 601#endif 602 603 reply->setObject("buffer", accessUnit); 604 reply->post(); 605 606 return OK; 607} 608 609void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) { 610 // LOGV("renderBuffer %s", audio ? "audio" : "video"); 611 612 sp<AMessage> reply; 613 CHECK(msg->findMessage("reply", &reply)); 614 615 sp<RefBase> obj; 616 CHECK(msg->findObject("buffer", &obj)); 617 618 sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get()); 619 620 mRenderer->queueBuffer(audio, buffer, reply); 621} 622 623void NuPlayer::notifyListener(int msg, int ext1, int ext2) { 624 if (mDriver == NULL) { 625 return; 626 } 627 628 sp<NuPlayerDriver> driver = mDriver.promote(); 629 630 if (driver == NULL) { 631 return; 632 } 633 634 driver->sendEvent(msg, ext1, ext2); 635} 636 637void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 638 // Make sure we don't continue to scan sources until we finish flushing. 639 ++mScanSourcesGeneration; 640 mScanSourcesPending = false; 641 642 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush(); 643 mRenderer->flush(audio); 644 645 FlushStatus newStatus = 646 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 647 648 if (audio) { 649 CHECK(mFlushingAudio == NONE 650 || mFlushingAudio == AWAITING_DISCONTINUITY); 651 652 mFlushingAudio = newStatus; 653 654 if (mFlushingVideo == NONE) { 655 mFlushingVideo = (mVideoDecoder != NULL) 656 ? AWAITING_DISCONTINUITY 657 : FLUSHED; 658 } 659 } else { 660 CHECK(mFlushingVideo == NONE 661 || mFlushingVideo == AWAITING_DISCONTINUITY); 662 663 mFlushingVideo = newStatus; 664 665 if (mFlushingAudio == NONE) { 666 mFlushingAudio = (mAudioDecoder != NULL) 667 ? AWAITING_DISCONTINUITY 668 : FLUSHED; 669 } 670 } 671} 672 673} // namespace android 674