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