diff options
Diffstat (limited to 'media/libmediaplayerservice/nuplayer/RTSPSource.cpp')
-rw-r--r-- | media/libmediaplayerservice/nuplayer/RTSPSource.cpp | 224 |
1 files changed, 206 insertions, 18 deletions
diff --git a/media/libmediaplayerservice/nuplayer/RTSPSource.cpp b/media/libmediaplayerservice/nuplayer/RTSPSource.cpp index 5a7a785..18cf6d1 100644 --- a/media/libmediaplayerservice/nuplayer/RTSPSource.cpp +++ b/media/libmediaplayerservice/nuplayer/RTSPSource.cpp @@ -22,26 +22,35 @@ #include "AnotherPacketSource.h" #include "MyHandler.h" +#include "SDPLoader.h" #include <media/stagefright/MediaDefs.h> #include <media/stagefright/MetaData.h> namespace android { +const int64_t kNearEOSTimeoutUs = 2000000ll; // 2 secs + NuPlayer::RTSPSource::RTSPSource( + const sp<AMessage> ¬ify, const char *url, const KeyedVector<String8, String8> *headers, bool uidValid, - uid_t uid) - : mURL(url), + uid_t uid, + bool isSDP) + : Source(notify), + mURL(url), mUIDValid(uidValid), mUID(uid), mFlags(0), + mIsSDP(isSDP), mState(DISCONNECTED), mFinalResult(OK), mDisconnectReplyID(0), - mStartingUp(true), - mSeekGeneration(0) { + mBuffering(true), + mSeekGeneration(0), + mEOSTimeoutAudio(0), + mEOSTimeoutVideo(0) { if (headers) { mExtraHeaders = *headers; @@ -62,7 +71,7 @@ NuPlayer::RTSPSource::~RTSPSource() { } } -void NuPlayer::RTSPSource::start() { +void NuPlayer::RTSPSource::prepareAsync() { if (mLooper == NULL) { mLooper = new ALooper; mLooper->setName("rtsp"); @@ -73,25 +82,64 @@ void NuPlayer::RTSPSource::start() { } CHECK(mHandler == NULL); + CHECK(mSDPLoader == NULL); sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id()); - mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID); - mLooper->registerHandler(mHandler); - CHECK_EQ(mState, (int)DISCONNECTED); mState = CONNECTING; - mHandler->connect(); + if (mIsSDP) { + mSDPLoader = new SDPLoader(notify, + (mFlags & kFlagIncognito) ? SDPLoader::kFlagIncognito : 0, + mUIDValid, mUID); + + mSDPLoader->load( + mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders); + } else { + mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID); + mLooper->registerHandler(mHandler); + + mHandler->connect(); + } + + sp<AMessage> notifyStart = dupNotify(); + notifyStart->setInt32("what", kWhatBufferingStart); + notifyStart->post(); +} + +void NuPlayer::RTSPSource::start() { } void NuPlayer::RTSPSource::stop() { + if (mLooper == NULL) { + return; + } sp<AMessage> msg = new AMessage(kWhatDisconnect, mReflector->id()); sp<AMessage> dummy; msg->postAndAwaitResponse(&dummy); } +void NuPlayer::RTSPSource::pause() { + int64_t mediaDurationUs = 0; + getDuration(&mediaDurationUs); + for (size_t index = 0; index < mTracks.size(); index++) { + TrackInfo *info = &mTracks.editItemAt(index); + sp<AnotherPacketSource> source = info->mSource; + + // Check if EOS or ERROR is received + if (source != NULL && source->isFinished(mediaDurationUs)) { + return; + } + } + mHandler->pause(); +} + +void NuPlayer::RTSPSource::resume() { + mHandler->resume(); +} + status_t NuPlayer::RTSPSource::feedMoreTSData() { return mFinalResult; } @@ -112,6 +160,13 @@ bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() { static const int64_t kMinDurationUs = 2000000ll; + int64_t mediaDurationUs = 0; + getDuration(&mediaDurationUs); + if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs)) + || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) { + return true; + } + status_t err; int64_t durationUs; if (mAudioTrack != NULL @@ -137,12 +192,16 @@ bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() { status_t NuPlayer::RTSPSource::dequeueAccessUnit( bool audio, sp<ABuffer> *accessUnit) { - if (mStartingUp) { + if (mBuffering) { if (!haveSufficientDataOnAllTracks()) { return -EWOULDBLOCK; } - mStartingUp = false; + mBuffering = false; + + sp<AMessage> notify = dupNotify(); + notify->setInt32("what", kWhatBufferingEnd); + notify->post(); } sp<AnotherPacketSource> source = getSource(audio); @@ -153,9 +212,51 @@ status_t NuPlayer::RTSPSource::dequeueAccessUnit( status_t finalResult; if (!source->hasBufferAvailable(&finalResult)) { - return finalResult == OK ? -EWOULDBLOCK : finalResult; + if (finalResult == OK) { + int64_t mediaDurationUs = 0; + getDuration(&mediaDurationUs); + sp<AnotherPacketSource> otherSource = getSource(!audio); + status_t otherFinalResult; + + // If other source already signaled EOS, this source should also signal EOS + if (otherSource != NULL && + !otherSource->hasBufferAvailable(&otherFinalResult) && + otherFinalResult == ERROR_END_OF_STREAM) { + source->signalEOS(ERROR_END_OF_STREAM); + return ERROR_END_OF_STREAM; + } + + // If this source has detected near end, give it some time to retrieve more + // data before signaling EOS + if (source->isFinished(mediaDurationUs)) { + int64_t eosTimeout = audio ? mEOSTimeoutAudio : mEOSTimeoutVideo; + if (eosTimeout == 0) { + setEOSTimeout(audio, ALooper::GetNowUs()); + } else if ((ALooper::GetNowUs() - eosTimeout) > kNearEOSTimeoutUs) { + setEOSTimeout(audio, 0); + source->signalEOS(ERROR_END_OF_STREAM); + return ERROR_END_OF_STREAM; + } + return -EWOULDBLOCK; + } + + if (!(otherSource != NULL && otherSource->isFinished(mediaDurationUs))) { + // We should not enter buffering mode + // if any of the sources already have detected EOS. + mBuffering = true; + + sp<AMessage> notify = dupNotify(); + notify->setInt32("what", kWhatBufferingStart); + notify->post(); + } + + return -EWOULDBLOCK; + } + return finalResult; } + setEOSTimeout(audio, 0); + return source->dequeueAccessUnit(accessUnit); } @@ -170,6 +271,14 @@ sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) { return audio ? mAudioTrack : mVideoTrack; } +void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) { + if (audio) { + mEOSTimeoutAudio = timeout; + } else { + mEOSTimeoutVideo = timeout; + } +} + status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) { *durationUs = 0ll; @@ -210,10 +319,6 @@ void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) { mHandler->seek(seekTimeUs); } -bool NuPlayer::RTSPSource::isSeekable() { - return true; -} - void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) { if (msg->what() == kWhatDisconnect) { uint32_t replyID; @@ -245,17 +350,34 @@ void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) { switch (what) { case MyHandler::kWhatConnected: + { onConnected(); + + notifyVideoSizeChanged(0, 0); + + uint32_t flags = 0; + + if (mHandler->isSeekable()) { + flags = FLAG_CAN_PAUSE + | FLAG_CAN_SEEK + | FLAG_CAN_SEEK_BACKWARD + | FLAG_CAN_SEEK_FORWARD; + } + + notifyFlagsChanged(flags); + notifyPrepared(); break; + } case MyHandler::kWhatDisconnected: + { onDisconnected(msg); break; + } case MyHandler::kWhatSeekDone: { mState = CONNECTED; - mStartingUp = true; break; } @@ -405,6 +527,12 @@ void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) { break; } + case SDPLoader::kWhatSDPLoaded: + { + onSDPLoaded(msg); + break; + } + default: TRESPASS(); } @@ -458,7 +586,57 @@ void NuPlayer::RTSPSource::onConnected() { mState = CONNECTED; } +void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) { + status_t err; + CHECK(msg->findInt32("result", &err)); + + mSDPLoader.clear(); + + if (mDisconnectReplyID != 0) { + err = UNKNOWN_ERROR; + } + + if (err == OK) { + sp<ASessionDescription> desc; + sp<RefBase> obj; + CHECK(msg->findObject("description", &obj)); + desc = static_cast<ASessionDescription *>(obj.get()); + + AString rtspUri; + if (!desc->findAttribute(0, "a=control", &rtspUri)) { + ALOGE("Unable to find url in SDP"); + err = UNKNOWN_ERROR; + } else { + sp<AMessage> notify = new AMessage(kWhatNotify, mReflector->id()); + + mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID); + mLooper->registerHandler(mHandler); + + mHandler->loadSDP(desc); + } + } + + if (err != OK) { + if (mState == CONNECTING) { + // We're still in the preparation phase, signal that it + // failed. + notifyPrepared(err); + } + + mState = DISCONNECTED; + mFinalResult = err; + + if (mDisconnectReplyID != 0) { + finishDisconnectIfPossible(); + } + } +} + void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) { + if (mState == DISCONNECTED) { + return; + } + status_t err; CHECK(msg->findInt32("result", &err)); CHECK_NE(err, (status_t)OK); @@ -466,6 +644,12 @@ void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) { mLooper->unregisterHandler(mHandler->id()); mHandler.clear(); + if (mState == CONNECTING) { + // We're still in the preparation phase, signal that it + // failed. + notifyPrepared(err); + } + mState = DISCONNECTED; mFinalResult = err; @@ -476,7 +660,11 @@ void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) { void NuPlayer::RTSPSource::finishDisconnectIfPossible() { if (mState != DISCONNECTED) { - mHandler->disconnect(); + if (mHandler != NULL) { + mHandler->disconnect(); + } else if (mSDPLoader != NULL) { + mSDPLoader->cancel(); + } return; } |