RTSPSource.cpp revision 81e68448f3361eaf8618930471fdc3c21bdf5cbc
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 "RTSPSource" 19#include <utils/Log.h> 20 21#include "RTSPSource.h" 22 23#include "AnotherPacketSource.h" 24#include "MyHandler.h" 25#include "SDPLoader.h" 26 27#include <media/IMediaHTTPService.h> 28#include <media/stagefright/MediaDefs.h> 29#include <media/stagefright/MetaData.h> 30 31namespace android { 32 33const int64_t kNearEOSTimeoutUs = 2000000ll; // 2 secs 34 35NuPlayer::RTSPSource::RTSPSource( 36 const sp<AMessage> ¬ify, 37 const sp<IMediaHTTPService> &httpService, 38 const char *url, 39 const KeyedVector<String8, String8> *headers, 40 bool uidValid, 41 uid_t uid, 42 bool isSDP) 43 : Source(notify), 44 mHTTPService(httpService), 45 mURL(url), 46 mUIDValid(uidValid), 47 mUID(uid), 48 mFlags(0), 49 mIsSDP(isSDP), 50 mState(DISCONNECTED), 51 mFinalResult(OK), 52 mDisconnectReplyID(0), 53 mBuffering(true), 54 mSeekGeneration(0), 55 mEOSTimeoutAudio(0), 56 mEOSTimeoutVideo(0) { 57 if (headers) { 58 mExtraHeaders = *headers; 59 60 ssize_t index = 61 mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log")); 62 63 if (index >= 0) { 64 mFlags |= kFlagIncognito; 65 66 mExtraHeaders.removeItemsAt(index); 67 } 68 } 69} 70 71NuPlayer::RTSPSource::~RTSPSource() { 72 if (mLooper != NULL) { 73 mLooper->stop(); 74 } 75} 76 77void NuPlayer::RTSPSource::prepareAsync() { 78 if (mLooper == NULL) { 79 mLooper = new ALooper; 80 mLooper->setName("rtsp"); 81 mLooper->start(); 82 83 mReflector = new AHandlerReflector<RTSPSource>(this); 84 mLooper->registerHandler(mReflector); 85 } 86 87 CHECK(mHandler == NULL); 88 CHECK(mSDPLoader == NULL); 89 90 sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id()); 91 92 CHECK_EQ(mState, (int)DISCONNECTED); 93 mState = CONNECTING; 94 95 if (mIsSDP) { 96 mSDPLoader = new SDPLoader(notify, 97 (mFlags & kFlagIncognito) ? SDPLoader::kFlagIncognito : 0, 98 mHTTPService); 99 100 mSDPLoader->load( 101 mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders); 102 } else { 103 mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID); 104 mLooper->registerHandler(mHandler); 105 106 mHandler->connect(); 107 } 108 109 sp<AMessage> notifyStart = dupNotify(); 110 notifyStart->setInt32("what", kWhatBufferingStart); 111 notifyStart->post(); 112} 113 114void NuPlayer::RTSPSource::start() { 115} 116 117void NuPlayer::RTSPSource::stop() { 118 if (mLooper == NULL) { 119 return; 120 } 121 sp<AMessage> msg = new AMessage(kWhatDisconnect, mReflector->id()); 122 123 sp<AMessage> dummy; 124 msg->postAndAwaitResponse(&dummy); 125} 126 127void NuPlayer::RTSPSource::pause() { 128 int64_t mediaDurationUs = 0; 129 getDuration(&mediaDurationUs); 130 for (size_t index = 0; index < mTracks.size(); index++) { 131 TrackInfo *info = &mTracks.editItemAt(index); 132 sp<AnotherPacketSource> source = info->mSource; 133 134 // Check if EOS or ERROR is received 135 if (source != NULL && source->isFinished(mediaDurationUs)) { 136 return; 137 } 138 } 139 mHandler->pause(); 140} 141 142void NuPlayer::RTSPSource::resume() { 143 mHandler->resume(); 144} 145 146status_t NuPlayer::RTSPSource::feedMoreTSData() { 147 return mFinalResult; 148} 149 150sp<MetaData> NuPlayer::RTSPSource::getFormatMeta(bool audio) { 151 sp<AnotherPacketSource> source = getSource(audio); 152 153 if (source == NULL) { 154 return NULL; 155 } 156 157 return source->getFormat(); 158} 159 160bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() { 161 // We're going to buffer at least 2 secs worth data on all tracks before 162 // starting playback (both at startup and after a seek). 163 164 static const int64_t kMinDurationUs = 2000000ll; 165 166 int64_t mediaDurationUs = 0; 167 getDuration(&mediaDurationUs); 168 if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs)) 169 || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) { 170 return true; 171 } 172 173 status_t err; 174 int64_t durationUs; 175 if (mAudioTrack != NULL 176 && (durationUs = mAudioTrack->getBufferedDurationUs(&err)) 177 < kMinDurationUs 178 && err == OK) { 179 ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)", 180 durationUs / 1E6); 181 return false; 182 } 183 184 if (mVideoTrack != NULL 185 && (durationUs = mVideoTrack->getBufferedDurationUs(&err)) 186 < kMinDurationUs 187 && err == OK) { 188 ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)", 189 durationUs / 1E6); 190 return false; 191 } 192 193 return true; 194} 195 196status_t NuPlayer::RTSPSource::dequeueAccessUnit( 197 bool audio, sp<ABuffer> *accessUnit) { 198 if (mBuffering) { 199 if (!haveSufficientDataOnAllTracks()) { 200 return -EWOULDBLOCK; 201 } 202 203 mBuffering = false; 204 205 sp<AMessage> notify = dupNotify(); 206 notify->setInt32("what", kWhatBufferingEnd); 207 notify->post(); 208 } 209 210 sp<AnotherPacketSource> source = getSource(audio); 211 212 if (source == NULL) { 213 return -EWOULDBLOCK; 214 } 215 216 status_t finalResult; 217 if (!source->hasBufferAvailable(&finalResult)) { 218 if (finalResult == OK) { 219 int64_t mediaDurationUs = 0; 220 getDuration(&mediaDurationUs); 221 sp<AnotherPacketSource> otherSource = getSource(!audio); 222 status_t otherFinalResult; 223 224 // If other source already signaled EOS, this source should also signal EOS 225 if (otherSource != NULL && 226 !otherSource->hasBufferAvailable(&otherFinalResult) && 227 otherFinalResult == ERROR_END_OF_STREAM) { 228 source->signalEOS(ERROR_END_OF_STREAM); 229 return ERROR_END_OF_STREAM; 230 } 231 232 // If this source has detected near end, give it some time to retrieve more 233 // data before signaling EOS 234 if (source->isFinished(mediaDurationUs)) { 235 int64_t eosTimeout = audio ? mEOSTimeoutAudio : mEOSTimeoutVideo; 236 if (eosTimeout == 0) { 237 setEOSTimeout(audio, ALooper::GetNowUs()); 238 } else if ((ALooper::GetNowUs() - eosTimeout) > kNearEOSTimeoutUs) { 239 setEOSTimeout(audio, 0); 240 source->signalEOS(ERROR_END_OF_STREAM); 241 return ERROR_END_OF_STREAM; 242 } 243 return -EWOULDBLOCK; 244 } 245 246 if (!(otherSource != NULL && otherSource->isFinished(mediaDurationUs))) { 247 // We should not enter buffering mode 248 // if any of the sources already have detected EOS. 249 mBuffering = true; 250 251 sp<AMessage> notify = dupNotify(); 252 notify->setInt32("what", kWhatBufferingStart); 253 notify->post(); 254 } 255 256 return -EWOULDBLOCK; 257 } 258 return finalResult; 259 } 260 261 setEOSTimeout(audio, 0); 262 263 return source->dequeueAccessUnit(accessUnit); 264} 265 266sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) { 267 if (mTSParser != NULL) { 268 sp<MediaSource> source = mTSParser->getSource( 269 audio ? ATSParser::AUDIO : ATSParser::VIDEO); 270 271 return static_cast<AnotherPacketSource *>(source.get()); 272 } 273 274 return audio ? mAudioTrack : mVideoTrack; 275} 276 277void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) { 278 if (audio) { 279 mEOSTimeoutAudio = timeout; 280 } else { 281 mEOSTimeoutVideo = timeout; 282 } 283} 284 285status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) { 286 *durationUs = 0ll; 287 288 int64_t audioDurationUs; 289 if (mAudioTrack != NULL 290 && mAudioTrack->getFormat()->findInt64( 291 kKeyDuration, &audioDurationUs) 292 && audioDurationUs > *durationUs) { 293 *durationUs = audioDurationUs; 294 } 295 296 int64_t videoDurationUs; 297 if (mVideoTrack != NULL 298 && mVideoTrack->getFormat()->findInt64( 299 kKeyDuration, &videoDurationUs) 300 && videoDurationUs > *durationUs) { 301 *durationUs = videoDurationUs; 302 } 303 304 return OK; 305} 306 307status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs) { 308 sp<AMessage> msg = new AMessage(kWhatPerformSeek, mReflector->id()); 309 msg->setInt32("generation", ++mSeekGeneration); 310 msg->setInt64("timeUs", seekTimeUs); 311 msg->post(200000ll); 312 313 return OK; 314} 315 316void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) { 317 if (mState != CONNECTED) { 318 return; 319 } 320 321 mState = SEEKING; 322 mHandler->seek(seekTimeUs); 323} 324 325void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) { 326 if (msg->what() == kWhatDisconnect) { 327 uint32_t replyID; 328 CHECK(msg->senderAwaitsResponse(&replyID)); 329 330 mDisconnectReplyID = replyID; 331 finishDisconnectIfPossible(); 332 return; 333 } else if (msg->what() == kWhatPerformSeek) { 334 int32_t generation; 335 CHECK(msg->findInt32("generation", &generation)); 336 337 if (generation != mSeekGeneration) { 338 // obsolete. 339 return; 340 } 341 342 int64_t seekTimeUs; 343 CHECK(msg->findInt64("timeUs", &seekTimeUs)); 344 345 performSeek(seekTimeUs); 346 return; 347 } 348 349 CHECK_EQ(msg->what(), (int)kWhatNotify); 350 351 int32_t what; 352 CHECK(msg->findInt32("what", &what)); 353 354 switch (what) { 355 case MyHandler::kWhatConnected: 356 { 357 onConnected(); 358 359 notifyVideoSizeChanged(0, 0); 360 361 uint32_t flags = 0; 362 363 if (mHandler->isSeekable()) { 364 flags = FLAG_CAN_PAUSE 365 | FLAG_CAN_SEEK 366 | FLAG_CAN_SEEK_BACKWARD 367 | FLAG_CAN_SEEK_FORWARD; 368 } 369 370 notifyFlagsChanged(flags); 371 notifyPrepared(); 372 break; 373 } 374 375 case MyHandler::kWhatDisconnected: 376 { 377 onDisconnected(msg); 378 break; 379 } 380 381 case MyHandler::kWhatSeekDone: 382 { 383 mState = CONNECTED; 384 break; 385 } 386 387 case MyHandler::kWhatAccessUnit: 388 { 389 size_t trackIndex; 390 CHECK(msg->findSize("trackIndex", &trackIndex)); 391 392 if (mTSParser == NULL) { 393 CHECK_LT(trackIndex, mTracks.size()); 394 } else { 395 CHECK_EQ(trackIndex, 0u); 396 } 397 398 sp<ABuffer> accessUnit; 399 CHECK(msg->findBuffer("accessUnit", &accessUnit)); 400 401 int32_t damaged; 402 if (accessUnit->meta()->findInt32("damaged", &damaged) 403 && damaged) { 404 ALOGI("dropping damaged access unit."); 405 break; 406 } 407 408 if (mTSParser != NULL) { 409 size_t offset = 0; 410 status_t err = OK; 411 while (offset + 188 <= accessUnit->size()) { 412 err = mTSParser->feedTSPacket( 413 accessUnit->data() + offset, 188); 414 if (err != OK) { 415 break; 416 } 417 418 offset += 188; 419 } 420 421 if (offset < accessUnit->size()) { 422 err = ERROR_MALFORMED; 423 } 424 425 if (err != OK) { 426 sp<AnotherPacketSource> source = getSource(false /* audio */); 427 if (source != NULL) { 428 source->signalEOS(err); 429 } 430 431 source = getSource(true /* audio */); 432 if (source != NULL) { 433 source->signalEOS(err); 434 } 435 } 436 break; 437 } 438 439 TrackInfo *info = &mTracks.editItemAt(trackIndex); 440 441 sp<AnotherPacketSource> source = info->mSource; 442 if (source != NULL) { 443 uint32_t rtpTime; 444 CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime)); 445 446 if (!info->mNPTMappingValid) { 447 // This is a live stream, we didn't receive any normal 448 // playtime mapping. We won't map to npt time. 449 source->queueAccessUnit(accessUnit); 450 break; 451 } 452 453 int64_t nptUs = 454 ((double)rtpTime - (double)info->mRTPTime) 455 / info->mTimeScale 456 * 1000000ll 457 + info->mNormalPlaytimeUs; 458 459 accessUnit->meta()->setInt64("timeUs", nptUs); 460 461 source->queueAccessUnit(accessUnit); 462 } 463 break; 464 } 465 466 case MyHandler::kWhatEOS: 467 { 468 int32_t finalResult; 469 CHECK(msg->findInt32("finalResult", &finalResult)); 470 CHECK_NE(finalResult, (status_t)OK); 471 472 if (mTSParser != NULL) { 473 sp<AnotherPacketSource> source = getSource(false /* audio */); 474 if (source != NULL) { 475 source->signalEOS(finalResult); 476 } 477 478 source = getSource(true /* audio */); 479 if (source != NULL) { 480 source->signalEOS(finalResult); 481 } 482 483 return; 484 } 485 486 size_t trackIndex; 487 CHECK(msg->findSize("trackIndex", &trackIndex)); 488 CHECK_LT(trackIndex, mTracks.size()); 489 490 TrackInfo *info = &mTracks.editItemAt(trackIndex); 491 sp<AnotherPacketSource> source = info->mSource; 492 if (source != NULL) { 493 source->signalEOS(finalResult); 494 } 495 496 break; 497 } 498 499 case MyHandler::kWhatSeekDiscontinuity: 500 { 501 size_t trackIndex; 502 CHECK(msg->findSize("trackIndex", &trackIndex)); 503 CHECK_LT(trackIndex, mTracks.size()); 504 505 TrackInfo *info = &mTracks.editItemAt(trackIndex); 506 sp<AnotherPacketSource> source = info->mSource; 507 if (source != NULL) { 508 source->queueDiscontinuity(ATSParser::DISCONTINUITY_SEEK, NULL); 509 } 510 511 break; 512 } 513 514 case MyHandler::kWhatNormalPlayTimeMapping: 515 { 516 size_t trackIndex; 517 CHECK(msg->findSize("trackIndex", &trackIndex)); 518 CHECK_LT(trackIndex, mTracks.size()); 519 520 uint32_t rtpTime; 521 CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime)); 522 523 int64_t nptUs; 524 CHECK(msg->findInt64("nptUs", &nptUs)); 525 526 TrackInfo *info = &mTracks.editItemAt(trackIndex); 527 info->mRTPTime = rtpTime; 528 info->mNormalPlaytimeUs = nptUs; 529 info->mNPTMappingValid = true; 530 break; 531 } 532 533 case SDPLoader::kWhatSDPLoaded: 534 { 535 onSDPLoaded(msg); 536 break; 537 } 538 539 default: 540 TRESPASS(); 541 } 542} 543 544void NuPlayer::RTSPSource::onConnected() { 545 CHECK(mAudioTrack == NULL); 546 CHECK(mVideoTrack == NULL); 547 548 size_t numTracks = mHandler->countTracks(); 549 for (size_t i = 0; i < numTracks; ++i) { 550 int32_t timeScale; 551 sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale); 552 553 const char *mime; 554 CHECK(format->findCString(kKeyMIMEType, &mime)); 555 556 if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) { 557 // Very special case for MPEG2 Transport Streams. 558 CHECK_EQ(numTracks, 1u); 559 560 mTSParser = new ATSParser; 561 return; 562 } 563 564 bool isAudio = !strncasecmp(mime, "audio/", 6); 565 bool isVideo = !strncasecmp(mime, "video/", 6); 566 567 TrackInfo info; 568 info.mTimeScale = timeScale; 569 info.mRTPTime = 0; 570 info.mNormalPlaytimeUs = 0ll; 571 info.mNPTMappingValid = false; 572 573 if ((isAudio && mAudioTrack == NULL) 574 || (isVideo && mVideoTrack == NULL)) { 575 sp<AnotherPacketSource> source = new AnotherPacketSource(format); 576 577 if (isAudio) { 578 mAudioTrack = source; 579 } else { 580 mVideoTrack = source; 581 } 582 583 info.mSource = source; 584 } 585 586 mTracks.push(info); 587 } 588 589 mState = CONNECTED; 590} 591 592void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) { 593 status_t err; 594 CHECK(msg->findInt32("result", &err)); 595 596 mSDPLoader.clear(); 597 598 if (mDisconnectReplyID != 0) { 599 err = UNKNOWN_ERROR; 600 } 601 602 if (err == OK) { 603 sp<ASessionDescription> desc; 604 sp<RefBase> obj; 605 CHECK(msg->findObject("description", &obj)); 606 desc = static_cast<ASessionDescription *>(obj.get()); 607 608 AString rtspUri; 609 if (!desc->findAttribute(0, "a=control", &rtspUri)) { 610 ALOGE("Unable to find url in SDP"); 611 err = UNKNOWN_ERROR; 612 } else { 613 sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id()); 614 615 mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID); 616 mLooper->registerHandler(mHandler); 617 618 mHandler->loadSDP(desc); 619 } 620 } 621 622 if (err != OK) { 623 if (mState == CONNECTING) { 624 // We're still in the preparation phase, signal that it 625 // failed. 626 notifyPrepared(err); 627 } 628 629 mState = DISCONNECTED; 630 mFinalResult = err; 631 632 if (mDisconnectReplyID != 0) { 633 finishDisconnectIfPossible(); 634 } 635 } 636} 637 638void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) { 639 if (mState == DISCONNECTED) { 640 return; 641 } 642 643 status_t err; 644 CHECK(msg->findInt32("result", &err)); 645 CHECK_NE(err, (status_t)OK); 646 647 mLooper->unregisterHandler(mHandler->id()); 648 mHandler.clear(); 649 650 if (mState == CONNECTING) { 651 // We're still in the preparation phase, signal that it 652 // failed. 653 notifyPrepared(err); 654 } 655 656 mState = DISCONNECTED; 657 mFinalResult = err; 658 659 if (mDisconnectReplyID != 0) { 660 finishDisconnectIfPossible(); 661 } 662} 663 664void NuPlayer::RTSPSource::finishDisconnectIfPossible() { 665 if (mState != DISCONNECTED) { 666 if (mHandler != NULL) { 667 mHandler->disconnect(); 668 } else if (mSDPLoader != NULL) { 669 mSDPLoader->cancel(); 670 } 671 return; 672 } 673 674 (new AMessage)->postReply(mDisconnectReplyID); 675 mDisconnectReplyID = 0; 676} 677 678} // namespace android 679