NuPlayer.cpp revision 22fc52f6f72f39e33c3970d0291de3569118aa5c
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 // XXX to be implemented 96} 97 98void NuPlayer::resume() { 99 // XXX to be implemented 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 CHECK(audio); 275 276 int32_t numChannels; 277 CHECK(codecRequest->findInt32("channel-count", &numChannels)); 278 279 int32_t sampleRate; 280 CHECK(codecRequest->findInt32("sample-rate", &sampleRate)); 281 282 LOGV("Audio output format changed to %d Hz, %d channels", 283 sampleRate, numChannels); 284 285 mAudioSink->close(); 286 CHECK_EQ(mAudioSink->open(sampleRate, numChannels), (status_t)OK); 287 mAudioSink->start(); 288 289 mRenderer->signalAudioSinkChanged(); 290 } else if (what == ACodec::kWhatShutdownCompleted) { 291 LOGV("%s shutdown completed", audio ? "audio" : "video"); 292 if (audio) { 293 mAudioDecoder.clear(); 294 295 CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER); 296 mFlushingAudio = SHUT_DOWN; 297 } else { 298 mVideoDecoder.clear(); 299 300 CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER); 301 mFlushingVideo = SHUT_DOWN; 302 } 303 304 finishFlushIfPossible(); 305 } else { 306 CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer); 307 308 renderBuffer(audio, codecRequest); 309 } 310 311 break; 312 } 313 314 case kWhatRendererNotify: 315 { 316 int32_t what; 317 CHECK(msg->findInt32("what", &what)); 318 319 if (what == Renderer::kWhatEOS) { 320 int32_t audio; 321 CHECK(msg->findInt32("audio", &audio)); 322 323 if (audio) { 324 mAudioEOS = true; 325 } else { 326 mVideoEOS = true; 327 } 328 329 LOGV("reached %s EOS", audio ? "audio" : "video"); 330 331 if ((mAudioEOS || mAudioDecoder == NULL) 332 && (mVideoEOS || mVideoDecoder == NULL)) { 333 notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0); 334 } 335 } else if (what == Renderer::kWhatPosition) { 336 int64_t positionUs; 337 CHECK(msg->findInt64("positionUs", &positionUs)); 338 339 if (mDriver != NULL) { 340 sp<NuPlayerDriver> driver = mDriver.promote(); 341 if (driver != NULL) { 342 driver->notifyPosition(positionUs); 343 } 344 } 345 } else { 346 CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete); 347 348 int32_t audio; 349 CHECK(msg->findInt32("audio", &audio)); 350 351 LOGV("renderer %s flush completed.", audio ? "audio" : "video"); 352 } 353 break; 354 } 355 356 case kWhatMoreDataQueued: 357 { 358 break; 359 } 360 361 case kWhatReset: 362 { 363 LOGV("kWhatReset"); 364 365 if (mFlushingAudio != NONE || mFlushingVideo != NONE) { 366 // We're currently flushing, postpone the reset until that's 367 // completed. 368 369 LOGV("postponing reset"); 370 371 mResetPostponed = true; 372 break; 373 } 374 375 if (mAudioDecoder == NULL && mVideoDecoder == NULL) { 376 finishReset(); 377 break; 378 } 379 380 if (mAudioDecoder != NULL) { 381 flushDecoder(true /* audio */, true /* needShutdown */); 382 } 383 384 if (mVideoDecoder != NULL) { 385 flushDecoder(false /* audio */, true /* needShutdown */); 386 } 387 388 mResetInProgress = true; 389 break; 390 } 391 392 case kWhatSeek: 393 { 394 int64_t seekTimeUs; 395 CHECK(msg->findInt64("seekTimeUs", &seekTimeUs)); 396 397 LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)", 398 seekTimeUs, seekTimeUs / 1E6); 399 400 mSource->seekTo(seekTimeUs); 401 402 if (mDriver != NULL) { 403 sp<NuPlayerDriver> driver = mDriver.promote(); 404 if (driver != NULL) { 405 driver->notifySeekComplete(); 406 } 407 } 408 409 break; 410 } 411 412 default: 413 TRESPASS(); 414 break; 415 } 416} 417 418void NuPlayer::finishFlushIfPossible() { 419 if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) { 420 return; 421 } 422 423 if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) { 424 return; 425 } 426 427 LOGV("both audio and video are flushed now."); 428 429 mRenderer->signalTimeDiscontinuity(); 430 431 if (mAudioDecoder != NULL) { 432 mAudioDecoder->signalResume(); 433 } 434 435 if (mVideoDecoder != NULL) { 436 mVideoDecoder->signalResume(); 437 } 438 439 mFlushingAudio = NONE; 440 mFlushingVideo = NONE; 441 442 if (mResetInProgress) { 443 LOGV("reset completed"); 444 445 mResetInProgress = false; 446 finishReset(); 447 } else if (mResetPostponed) { 448 (new AMessage(kWhatReset, id()))->post(); 449 mResetPostponed = false; 450 } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) { 451 postScanSources(); 452 } 453} 454 455void NuPlayer::finishReset() { 456 CHECK(mAudioDecoder == NULL); 457 CHECK(mVideoDecoder == NULL); 458 459 mRenderer.clear(); 460 mSource.clear(); 461 462 if (mDriver != NULL) { 463 sp<NuPlayerDriver> driver = mDriver.promote(); 464 if (driver != NULL) { 465 driver->notifyResetComplete(); 466 } 467 } 468} 469 470void NuPlayer::postScanSources() { 471 if (mScanSourcesPending) { 472 return; 473 } 474 475 sp<AMessage> msg = new AMessage(kWhatScanSources, id()); 476 msg->setInt32("generation", mScanSourcesGeneration); 477 msg->post(); 478 479 mScanSourcesPending = true; 480} 481 482status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) { 483 if (*decoder != NULL) { 484 return OK; 485 } 486 487 sp<MetaData> meta = mSource->getFormat(audio); 488 489 if (meta == NULL) { 490 return -EWOULDBLOCK; 491 } 492 493 sp<AMessage> notify = 494 new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify, 495 id()); 496 497 *decoder = new Decoder(notify, audio ? NULL : mSurface); 498 looper()->registerHandler(*decoder); 499 500 (*decoder)->configure(meta); 501 502 int64_t durationUs; 503 if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) { 504 sp<NuPlayerDriver> driver = mDriver.promote(); 505 if (driver != NULL) { 506 driver->notifyDuration(durationUs); 507 } 508 } 509 510 return OK; 511} 512 513status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) { 514 sp<AMessage> reply; 515 CHECK(msg->findMessage("reply", &reply)); 516 517 if ((audio && IsFlushingState(mFlushingAudio)) 518 || (!audio && IsFlushingState(mFlushingVideo))) { 519 reply->setInt32("err", INFO_DISCONTINUITY); 520 reply->post(); 521 return OK; 522 } 523 524 sp<ABuffer> accessUnit; 525 status_t err = mSource->dequeueAccessUnit(audio, &accessUnit); 526 527 if (err == -EWOULDBLOCK) { 528 return err; 529 } else if (err != OK) { 530 if (err == INFO_DISCONTINUITY) { 531 int32_t type; 532 CHECK(accessUnit->meta()->findInt32("discontinuity", &type)); 533 534 bool formatChange = 535 type == ATSParser::DISCONTINUITY_FORMATCHANGE; 536 537 LOGV("%s discontinuity (formatChange=%d)", 538 audio ? "audio" : "video", formatChange); 539 540 flushDecoder(audio, formatChange); 541 } 542 543 reply->setInt32("err", err); 544 reply->post(); 545 return OK; 546 } 547 548 // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video"); 549 550#if 0 551 int64_t mediaTimeUs; 552 CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs)); 553 LOGV("feeding %s input buffer at media time %.2f secs", 554 audio ? "audio" : "video", 555 mediaTimeUs / 1E6); 556#endif 557 558 reply->setObject("buffer", accessUnit); 559 reply->post(); 560 561 return OK; 562} 563 564void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) { 565 // LOGV("renderBuffer %s", audio ? "audio" : "video"); 566 567 sp<AMessage> reply; 568 CHECK(msg->findMessage("reply", &reply)); 569 570 sp<RefBase> obj; 571 CHECK(msg->findObject("buffer", &obj)); 572 573 sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get()); 574 575 mRenderer->queueBuffer(audio, buffer, reply); 576} 577 578void NuPlayer::notifyListener(int msg, int ext1, int ext2) { 579 if (mDriver == NULL) { 580 return; 581 } 582 583 sp<NuPlayerDriver> driver = mDriver.promote(); 584 585 if (driver == NULL) { 586 return; 587 } 588 589 driver->sendEvent(msg, ext1, ext2); 590} 591 592void NuPlayer::flushDecoder(bool audio, bool needShutdown) { 593 // Make sure we don't continue to scan sources until we finish flushing. 594 ++mScanSourcesGeneration; 595 mScanSourcesPending = false; 596 597 (audio ? mAudioDecoder : mVideoDecoder)->signalFlush(); 598 mRenderer->flush(audio); 599 600 FlushStatus newStatus = 601 needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER; 602 603 if (audio) { 604 CHECK(mFlushingAudio == NONE 605 || mFlushingAudio == AWAITING_DISCONTINUITY); 606 607 mFlushingAudio = newStatus; 608 609 if (mFlushingVideo == NONE) { 610 mFlushingVideo = (mVideoDecoder != NULL) 611 ? AWAITING_DISCONTINUITY 612 : FLUSHED; 613 } 614 } else { 615 CHECK(mFlushingVideo == NONE 616 || mFlushingVideo == AWAITING_DISCONTINUITY); 617 618 mFlushingVideo = newStatus; 619 620 if (mFlushingAudio == NONE) { 621 mFlushingAudio = (mAudioDecoder != NULL) 622 ? AWAITING_DISCONTINUITY 623 : FLUSHED; 624 } 625 } 626} 627 628} // namespace android 629