RTSPSource.cpp revision 5ec347fcaf7fefe8fd9ccbaa365ffb070921a970
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
35// Buffer Underflow/Prepare/StartServer/Overflow Marks
36const int64_t NuPlayer::RTSPSource::kUnderflowMarkUs   =  1000000ll;
37const int64_t NuPlayer::RTSPSource::kPrepareMarkUs     =  3000000ll;
38const int64_t NuPlayer::RTSPSource::kStartServerMarkUs =  5000000ll;
39const int64_t NuPlayer::RTSPSource::kOverflowMarkUs    = 10000000ll;
40
41NuPlayer::RTSPSource::RTSPSource(
42        const sp<AMessage> &notify,
43        const sp<IMediaHTTPService> &httpService,
44        const char *url,
45        const KeyedVector<String8, String8> *headers,
46        bool uidValid,
47        uid_t uid,
48        bool isSDP)
49    : Source(notify),
50      mHTTPService(httpService),
51      mURL(url),
52      mUIDValid(uidValid),
53      mUID(uid),
54      mFlags(0),
55      mIsSDP(isSDP),
56      mState(DISCONNECTED),
57      mFinalResult(OK),
58      mDisconnectReplyID(0),
59      mBuffering(false),
60      mInPreparationPhase(true),
61      mEOSPending(false),
62      mSeekGeneration(0),
63      mEOSTimeoutAudio(0),
64      mEOSTimeoutVideo(0) {
65    if (headers) {
66        mExtraHeaders = *headers;
67
68        ssize_t index =
69            mExtraHeaders.indexOfKey(String8("x-hide-urls-from-log"));
70
71        if (index >= 0) {
72            mFlags |= kFlagIncognito;
73
74            mExtraHeaders.removeItemsAt(index);
75        }
76    }
77}
78
79NuPlayer::RTSPSource::~RTSPSource() {
80    if (mLooper != NULL) {
81        mLooper->unregisterHandler(id());
82        mLooper->stop();
83    }
84}
85
86void NuPlayer::RTSPSource::prepareAsync() {
87    if (mIsSDP && mHTTPService == NULL) {
88        notifyPrepared(BAD_VALUE);
89        return;
90    }
91
92    if (mLooper == NULL) {
93        mLooper = new ALooper;
94        mLooper->setName("rtsp");
95        mLooper->start();
96
97        mLooper->registerHandler(this);
98    }
99
100    CHECK(mHandler == NULL);
101    CHECK(mSDPLoader == NULL);
102
103    sp<AMessage> notify = new AMessage(kWhatNotify, this);
104
105    CHECK_EQ(mState, (int)DISCONNECTED);
106    mState = CONNECTING;
107
108    if (mIsSDP) {
109        mSDPLoader = new SDPLoader(notify,
110                (mFlags & kFlagIncognito) ? SDPLoader::kFlagIncognito : 0,
111                mHTTPService);
112
113        mSDPLoader->load(
114                mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
115    } else {
116        mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID);
117        mLooper->registerHandler(mHandler);
118
119        mHandler->connect();
120    }
121
122    startBufferingIfNecessary();
123}
124
125void NuPlayer::RTSPSource::start() {
126}
127
128void NuPlayer::RTSPSource::stop() {
129    if (mLooper == NULL) {
130        return;
131    }
132    sp<AMessage> msg = new AMessage(kWhatDisconnect, this);
133
134    sp<AMessage> dummy;
135    msg->postAndAwaitResponse(&dummy);
136}
137
138status_t NuPlayer::RTSPSource::feedMoreTSData() {
139    Mutex::Autolock _l(mBufferingLock);
140    return mFinalResult;
141}
142
143sp<MetaData> NuPlayer::RTSPSource::getFormatMeta(bool audio) {
144    sp<AnotherPacketSource> source = getSource(audio);
145
146    if (source == NULL) {
147        return NULL;
148    }
149
150    return source->getFormat();
151}
152
153bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() {
154    // We're going to buffer at least 2 secs worth data on all tracks before
155    // starting playback (both at startup and after a seek).
156
157    static const int64_t kMinDurationUs = 2000000ll;
158
159    int64_t mediaDurationUs = 0;
160    getDuration(&mediaDurationUs);
161    if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs))
162            || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) {
163        return true;
164    }
165
166    status_t err;
167    int64_t durationUs;
168    if (mAudioTrack != NULL
169            && (durationUs = mAudioTrack->getBufferedDurationUs(&err))
170                    < kMinDurationUs
171            && err == OK) {
172        ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
173              durationUs / 1E6);
174        return false;
175    }
176
177    if (mVideoTrack != NULL
178            && (durationUs = mVideoTrack->getBufferedDurationUs(&err))
179                    < kMinDurationUs
180            && err == OK) {
181        ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
182              durationUs / 1E6);
183        return false;
184    }
185
186    return true;
187}
188
189status_t NuPlayer::RTSPSource::dequeueAccessUnit(
190        bool audio, sp<ABuffer> *accessUnit) {
191    if (!stopBufferingIfNecessary()) {
192        return -EWOULDBLOCK;
193    }
194
195    sp<AnotherPacketSource> source = getSource(audio);
196
197    if (source == NULL) {
198        return -EWOULDBLOCK;
199    }
200
201    status_t finalResult;
202    if (!source->hasBufferAvailable(&finalResult)) {
203        if (finalResult == OK) {
204
205            // If other source already signaled EOS, this source should also return EOS
206            if (sourceReachedEOS(!audio)) {
207                return ERROR_END_OF_STREAM;
208            }
209
210            // If this source has detected near end, give it some time to retrieve more
211            // data before returning EOS
212            int64_t mediaDurationUs = 0;
213            getDuration(&mediaDurationUs);
214            if (source->isFinished(mediaDurationUs)) {
215                int64_t eosTimeout = audio ? mEOSTimeoutAudio : mEOSTimeoutVideo;
216                if (eosTimeout == 0) {
217                    setEOSTimeout(audio, ALooper::GetNowUs());
218                } else if ((ALooper::GetNowUs() - eosTimeout) > kNearEOSTimeoutUs) {
219                    setEOSTimeout(audio, 0);
220                    return ERROR_END_OF_STREAM;
221                }
222                return -EWOULDBLOCK;
223            }
224
225            if (!sourceNearEOS(!audio)) {
226                // We should not enter buffering mode
227                // if any of the sources already have detected EOS.
228                startBufferingIfNecessary();
229            }
230
231            return -EWOULDBLOCK;
232        }
233        return finalResult;
234    }
235
236    setEOSTimeout(audio, 0);
237
238    return source->dequeueAccessUnit(accessUnit);
239}
240
241sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) {
242    if (mTSParser != NULL) {
243        sp<MediaSource> source = mTSParser->getSource(
244                audio ? ATSParser::AUDIO : ATSParser::VIDEO);
245
246        return static_cast<AnotherPacketSource *>(source.get());
247    }
248
249    return audio ? mAudioTrack : mVideoTrack;
250}
251
252void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) {
253    if (audio) {
254        mEOSTimeoutAudio = timeout;
255    } else {
256        mEOSTimeoutVideo = timeout;
257    }
258}
259
260status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) {
261    *durationUs = -1ll;
262
263    int64_t audioDurationUs;
264    if (mAudioTrack != NULL
265            && mAudioTrack->getFormat()->findInt64(
266                kKeyDuration, &audioDurationUs)
267            && audioDurationUs > *durationUs) {
268        *durationUs = audioDurationUs;
269    }
270
271    int64_t videoDurationUs;
272    if (mVideoTrack != NULL
273            && mVideoTrack->getFormat()->findInt64(
274                kKeyDuration, &videoDurationUs)
275            && videoDurationUs > *durationUs) {
276        *durationUs = videoDurationUs;
277    }
278
279    return OK;
280}
281
282status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs, bool precise) {
283    sp<AMessage> msg = new AMessage(kWhatPerformSeek, this);
284    msg->setInt32("generation", ++mSeekGeneration);
285    msg->setInt64("timeUs", seekTimeUs);
286    msg->setInt32("precise", precise);
287
288    sp<AMessage> response;
289    status_t err = msg->postAndAwaitResponse(&response);
290    if (err == OK && response != NULL) {
291        CHECK(response->findInt32("err", &err));
292    }
293
294    return err;
295}
296
297void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
298    if (mState != CONNECTED) {
299        finishSeek(INVALID_OPERATION);
300        return;
301    }
302
303    mState = SEEKING;
304    mHandler->seek(seekTimeUs);
305    mEOSPending = false;
306}
307
308void NuPlayer::RTSPSource::schedulePollBuffering() {
309    sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
310    msg->post(1000000ll); // 1 second intervals
311}
312
313void NuPlayer::RTSPSource::checkBuffering(
314        bool *prepared, bool *underflow, bool *overflow, bool *startServer, bool *finished) {
315    size_t numTracks = mTracks.size();
316    size_t preparedCount, underflowCount, overflowCount, startCount, finishedCount;
317    preparedCount = underflowCount = overflowCount = startCount = finishedCount = 0;
318
319    size_t count = numTracks;
320    for (size_t i = 0; i < count; ++i) {
321        status_t finalResult;
322        TrackInfo *info = &mTracks.editItemAt(i);
323        sp<AnotherPacketSource> src = info->mSource;
324        if (src == NULL) {
325            --numTracks;
326            continue;
327        }
328        int64_t bufferedDurationUs = src->getBufferedDurationUs(&finalResult);
329
330        // isFinished when duration is 0 checks for EOS result only
331        if (bufferedDurationUs > kPrepareMarkUs || src->isFinished(/* duration */ 0)) {
332            ++preparedCount;
333        }
334
335        if (src->isFinished(/* duration */ 0)) {
336            ++overflowCount;
337            ++finishedCount;
338        } else {
339            if (bufferedDurationUs < kUnderflowMarkUs) {
340                ++underflowCount;
341            }
342            if (bufferedDurationUs > kOverflowMarkUs) {
343                ++overflowCount;
344            }
345            if (bufferedDurationUs < kStartServerMarkUs) {
346                ++startCount;
347            }
348        }
349    }
350
351    *prepared    = (preparedCount == numTracks);
352    *underflow   = (underflowCount > 0);
353    *overflow    = (overflowCount == numTracks);
354    *startServer = (startCount > 0);
355    *finished    = (finishedCount > 0);
356}
357
358void NuPlayer::RTSPSource::onPollBuffering() {
359    bool prepared, underflow, overflow, startServer, finished;
360    checkBuffering(&prepared, &underflow, &overflow, &startServer, &finished);
361
362    if (prepared && mInPreparationPhase) {
363        mInPreparationPhase = false;
364        notifyPrepared();
365    }
366
367    if (!mInPreparationPhase && underflow) {
368        startBufferingIfNecessary();
369    }
370
371    if (haveSufficientDataOnAllTracks()) {
372        stopBufferingIfNecessary();
373    }
374
375    if (overflow && mHandler != NULL) {
376        mHandler->pause();
377    }
378
379    if (startServer && mHandler != NULL) {
380        mHandler->resume();
381    }
382
383    if (finished && mHandler != NULL) {
384        mHandler->cancelAccessUnitTimeoutCheck();
385    }
386
387    schedulePollBuffering();
388}
389
390void NuPlayer::RTSPSource::signalSourceEOS(status_t result) {
391    const bool audio = true;
392    const bool video = false;
393
394    sp<AnotherPacketSource> source = getSource(audio);
395    if (source != NULL) {
396        source->signalEOS(result);
397    }
398
399    source = getSource(video);
400    if (source != NULL) {
401        source->signalEOS(result);
402    }
403}
404
405bool NuPlayer::RTSPSource::sourceReachedEOS(bool audio) {
406    sp<AnotherPacketSource> source = getSource(audio);
407    status_t finalResult;
408    return (source != NULL &&
409            !source->hasBufferAvailable(&finalResult) &&
410            finalResult == ERROR_END_OF_STREAM);
411}
412
413bool NuPlayer::RTSPSource::sourceNearEOS(bool audio) {
414    sp<AnotherPacketSource> source = getSource(audio);
415    int64_t mediaDurationUs = 0;
416    getDuration(&mediaDurationUs);
417    return (source != NULL && source->isFinished(mediaDurationUs));
418}
419
420void NuPlayer::RTSPSource::onSignalEOS(const sp<AMessage> &msg) {
421    int32_t generation;
422    CHECK(msg->findInt32("generation", &generation));
423
424    if (generation != mSeekGeneration) {
425        return;
426    }
427
428    if (mEOSPending) {
429        signalSourceEOS(ERROR_END_OF_STREAM);
430        mEOSPending = false;
431    }
432}
433
434void NuPlayer::RTSPSource::postSourceEOSIfNecessary() {
435    const bool audio = true;
436    const bool video = false;
437    // If a source has detected near end, give it some time to retrieve more
438    // data before signaling EOS
439    if (sourceNearEOS(audio) || sourceNearEOS(video)) {
440        if (!mEOSPending) {
441            sp<AMessage> msg = new AMessage(kWhatSignalEOS, this);
442            msg->setInt32("generation", mSeekGeneration);
443            msg->post(kNearEOSTimeoutUs);
444            mEOSPending = true;
445        }
446    }
447}
448
449void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
450    if (msg->what() == kWhatDisconnect) {
451        sp<AReplyToken> replyID;
452        CHECK(msg->senderAwaitsResponse(&replyID));
453
454        mDisconnectReplyID = replyID;
455        finishDisconnectIfPossible();
456        return;
457    } else if (msg->what() == kWhatPerformSeek) {
458        int32_t generation;
459        CHECK(msg->findInt32("generation", &generation));
460        CHECK(msg->senderAwaitsResponse(&mSeekReplyID));
461
462        if (generation != mSeekGeneration) {
463            // obsolete.
464            finishSeek(OK);
465            return;
466        }
467
468        int64_t seekTimeUs;
469        int32_t precise;
470        CHECK(msg->findInt64("timeUs", &seekTimeUs));
471        CHECK(msg->findInt32("precise", &precise));
472
473        // TODO: add "precise" to performSeek.
474        performSeek(seekTimeUs);
475        return;
476    } else if (msg->what() == kWhatPollBuffering) {
477        onPollBuffering();
478        return;
479    } else if (msg->what() == kWhatSignalEOS) {
480        onSignalEOS(msg);
481        return;
482    }
483
484    CHECK_EQ(msg->what(), (int)kWhatNotify);
485
486    int32_t what;
487    CHECK(msg->findInt32("what", &what));
488
489    switch (what) {
490        case MyHandler::kWhatConnected:
491        {
492            onConnected();
493
494            notifyVideoSizeChanged();
495
496            uint32_t flags = 0;
497
498            if (mHandler->isSeekable()) {
499                flags = FLAG_CAN_PAUSE
500                        | FLAG_CAN_SEEK
501                        | FLAG_CAN_SEEK_BACKWARD
502                        | FLAG_CAN_SEEK_FORWARD;
503            }
504
505            notifyFlagsChanged(flags);
506            schedulePollBuffering();
507            break;
508        }
509
510        case MyHandler::kWhatDisconnected:
511        {
512            onDisconnected(msg);
513            break;
514        }
515
516        case MyHandler::kWhatSeekDone:
517        {
518            mState = CONNECTED;
519            // Unblock seekTo here in case we attempted to seek in a live stream
520            finishSeek(OK);
521            break;
522        }
523
524        case MyHandler::kWhatSeekPaused:
525        {
526            sp<AnotherPacketSource> source = getSource(true /* audio */);
527            if (source != NULL) {
528                source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
529                        /* extra */ NULL,
530                        /* discard */ true);
531            }
532            source = getSource(false /* video */);
533            if (source != NULL) {
534                source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
535                        /* extra */ NULL,
536                        /* discard */ true);
537            };
538
539            status_t err = OK;
540            msg->findInt32("err", &err);
541
542            if (err == OK) {
543                int64_t timeUs;
544                CHECK(msg->findInt64("time", &timeUs));
545                mHandler->continueSeekAfterPause(timeUs);
546            } else {
547                finishSeek(err);
548            }
549            break;
550        }
551
552        case MyHandler::kWhatAccessUnit:
553        {
554            size_t trackIndex;
555            CHECK(msg->findSize("trackIndex", &trackIndex));
556
557            if (mTSParser == NULL) {
558                CHECK_LT(trackIndex, mTracks.size());
559            } else {
560                CHECK_EQ(trackIndex, 0u);
561            }
562
563            sp<ABuffer> accessUnit;
564            CHECK(msg->findBuffer("accessUnit", &accessUnit));
565
566            int32_t damaged;
567            if (accessUnit->meta()->findInt32("damaged", &damaged)
568                    && damaged) {
569                ALOGI("dropping damaged access unit.");
570                break;
571            }
572
573            if (mTSParser != NULL) {
574                size_t offset = 0;
575                status_t err = OK;
576                while (offset + 188 <= accessUnit->size()) {
577                    err = mTSParser->feedTSPacket(
578                            accessUnit->data() + offset, 188);
579                    if (err != OK) {
580                        break;
581                    }
582
583                    offset += 188;
584                }
585
586                if (offset < accessUnit->size()) {
587                    err = ERROR_MALFORMED;
588                }
589
590                if (err != OK) {
591                    signalSourceEOS(err);
592                }
593
594                postSourceEOSIfNecessary();
595                break;
596            }
597
598            TrackInfo *info = &mTracks.editItemAt(trackIndex);
599
600            sp<AnotherPacketSource> source = info->mSource;
601            if (source != NULL) {
602                uint32_t rtpTime;
603                CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
604
605                if (!info->mNPTMappingValid) {
606                    // This is a live stream, we didn't receive any normal
607                    // playtime mapping. We won't map to npt time.
608                    source->queueAccessUnit(accessUnit);
609                    break;
610                }
611
612                int64_t nptUs =
613                    ((double)rtpTime - (double)info->mRTPTime)
614                        / info->mTimeScale
615                        * 1000000ll
616                        + info->mNormalPlaytimeUs;
617
618                accessUnit->meta()->setInt64("timeUs", nptUs);
619
620                source->queueAccessUnit(accessUnit);
621            }
622            postSourceEOSIfNecessary();
623            break;
624        }
625
626        case MyHandler::kWhatEOS:
627        {
628            int32_t finalResult;
629            CHECK(msg->findInt32("finalResult", &finalResult));
630            CHECK_NE(finalResult, (status_t)OK);
631
632            if (mTSParser != NULL) {
633                signalSourceEOS(finalResult);
634            }
635
636            size_t trackIndex;
637            CHECK(msg->findSize("trackIndex", &trackIndex));
638            CHECK_LT(trackIndex, mTracks.size());
639
640            TrackInfo *info = &mTracks.editItemAt(trackIndex);
641            sp<AnotherPacketSource> source = info->mSource;
642            if (source != NULL) {
643                source->signalEOS(finalResult);
644            }
645
646            break;
647        }
648
649        case MyHandler::kWhatSeekDiscontinuity:
650        {
651            size_t trackIndex;
652            CHECK(msg->findSize("trackIndex", &trackIndex));
653            CHECK_LT(trackIndex, mTracks.size());
654
655            TrackInfo *info = &mTracks.editItemAt(trackIndex);
656            sp<AnotherPacketSource> source = info->mSource;
657            if (source != NULL) {
658                source->queueDiscontinuity(
659                        ATSParser::DISCONTINUITY_TIME,
660                        NULL,
661                        true /* discard */);
662            }
663
664            break;
665        }
666
667        case MyHandler::kWhatNormalPlayTimeMapping:
668        {
669            size_t trackIndex;
670            CHECK(msg->findSize("trackIndex", &trackIndex));
671            CHECK_LT(trackIndex, mTracks.size());
672
673            uint32_t rtpTime;
674            CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
675
676            int64_t nptUs;
677            CHECK(msg->findInt64("nptUs", &nptUs));
678
679            TrackInfo *info = &mTracks.editItemAt(trackIndex);
680            info->mRTPTime = rtpTime;
681            info->mNormalPlaytimeUs = nptUs;
682            info->mNPTMappingValid = true;
683            break;
684        }
685
686        case SDPLoader::kWhatSDPLoaded:
687        {
688            onSDPLoaded(msg);
689            break;
690        }
691
692        default:
693            TRESPASS();
694    }
695}
696
697void NuPlayer::RTSPSource::onConnected() {
698    CHECK(mAudioTrack == NULL);
699    CHECK(mVideoTrack == NULL);
700
701    size_t numTracks = mHandler->countTracks();
702    for (size_t i = 0; i < numTracks; ++i) {
703        int32_t timeScale;
704        sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
705
706        const char *mime;
707        CHECK(format->findCString(kKeyMIMEType, &mime));
708
709        if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
710            // Very special case for MPEG2 Transport Streams.
711            CHECK_EQ(numTracks, 1u);
712
713            mTSParser = new ATSParser;
714            return;
715        }
716
717        bool isAudio = !strncasecmp(mime, "audio/", 6);
718        bool isVideo = !strncasecmp(mime, "video/", 6);
719
720        TrackInfo info;
721        info.mTimeScale = timeScale;
722        info.mRTPTime = 0;
723        info.mNormalPlaytimeUs = 0ll;
724        info.mNPTMappingValid = false;
725
726        if ((isAudio && mAudioTrack == NULL)
727                || (isVideo && mVideoTrack == NULL)) {
728            sp<AnotherPacketSource> source = new AnotherPacketSource(format);
729
730            if (isAudio) {
731                mAudioTrack = source;
732            } else {
733                mVideoTrack = source;
734            }
735
736            info.mSource = source;
737        }
738
739        mTracks.push(info);
740    }
741
742    mState = CONNECTED;
743}
744
745void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
746    status_t err;
747    CHECK(msg->findInt32("result", &err));
748
749    mSDPLoader.clear();
750
751    if (mDisconnectReplyID != 0) {
752        err = UNKNOWN_ERROR;
753    }
754
755    if (err == OK) {
756        sp<ASessionDescription> desc;
757        sp<RefBase> obj;
758        CHECK(msg->findObject("description", &obj));
759        desc = static_cast<ASessionDescription *>(obj.get());
760
761        AString rtspUri;
762        if (!desc->findAttribute(0, "a=control", &rtspUri)) {
763            ALOGE("Unable to find url in SDP");
764            err = UNKNOWN_ERROR;
765        } else {
766            sp<AMessage> notify = new AMessage(kWhatNotify, this);
767
768            mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
769            mLooper->registerHandler(mHandler);
770
771            mHandler->loadSDP(desc);
772        }
773    }
774
775    if (err != OK) {
776        if (mState == CONNECTING) {
777            // We're still in the preparation phase, signal that it
778            // failed.
779            notifyPrepared(err);
780        }
781
782        mState = DISCONNECTED;
783        setError(err);
784
785        if (mDisconnectReplyID != 0) {
786            finishDisconnectIfPossible();
787        }
788    }
789}
790
791void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
792    if (mState == DISCONNECTED) {
793        return;
794    }
795
796    status_t err;
797    CHECK(msg->findInt32("result", &err));
798    CHECK_NE(err, (status_t)OK);
799
800    mLooper->unregisterHandler(mHandler->id());
801    mHandler.clear();
802
803    if (mState == CONNECTING) {
804        // We're still in the preparation phase, signal that it
805        // failed.
806        notifyPrepared(err);
807    }
808
809    mState = DISCONNECTED;
810    setError(err);
811
812    if (mDisconnectReplyID != 0) {
813        finishDisconnectIfPossible();
814    }
815}
816
817void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
818    if (mState != DISCONNECTED) {
819        if (mHandler != NULL) {
820            mHandler->disconnect();
821        } else if (mSDPLoader != NULL) {
822            mSDPLoader->cancel();
823        }
824        return;
825    }
826
827    (new AMessage)->postReply(mDisconnectReplyID);
828    mDisconnectReplyID = 0;
829}
830
831void NuPlayer::RTSPSource::setError(status_t err) {
832    Mutex::Autolock _l(mBufferingLock);
833    mFinalResult = err;
834}
835
836void NuPlayer::RTSPSource::startBufferingIfNecessary() {
837    Mutex::Autolock _l(mBufferingLock);
838
839    if (!mBuffering) {
840        mBuffering = true;
841
842        sp<AMessage> notify = dupNotify();
843        notify->setInt32("what", kWhatPauseOnBufferingStart);
844        notify->post();
845    }
846}
847
848bool NuPlayer::RTSPSource::stopBufferingIfNecessary() {
849    Mutex::Autolock _l(mBufferingLock);
850
851    if (mBuffering) {
852        if (!haveSufficientDataOnAllTracks()) {
853            return false;
854        }
855
856        mBuffering = false;
857
858        sp<AMessage> notify = dupNotify();
859        notify->setInt32("what", kWhatResumeOnBufferingEnd);
860        notify->post();
861    }
862
863    return true;
864}
865
866void NuPlayer::RTSPSource::finishSeek(status_t err) {
867    if (mSeekReplyID == NULL) {
868        return;
869    }
870    sp<AMessage> seekReply = new AMessage;
871    seekReply->setInt32("err", err);
872    seekReply->postReply(mSeekReplyID);
873    mSeekReplyID = NULL;
874}
875
876}  // namespace android
877