RTSPSource.cpp revision ae48167abbc1570498ae89759fcd5da1d6191388
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 = 0ll;
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) {
283    sp<AMessage> msg = new AMessage(kWhatPerformSeek, this);
284    msg->setInt32("generation", ++mSeekGeneration);
285    msg->setInt64("timeUs", seekTimeUs);
286
287    sp<AMessage> response;
288    status_t err = msg->postAndAwaitResponse(&response);
289    if (err == OK && response != NULL) {
290        CHECK(response->findInt32("err", &err));
291    }
292
293    return err;
294}
295
296void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
297    if (mState != CONNECTED) {
298        finishSeek(INVALID_OPERATION);
299        return;
300    }
301
302    mState = SEEKING;
303    mHandler->seek(seekTimeUs);
304    mEOSPending = false;
305}
306
307void NuPlayer::RTSPSource::schedulePollBuffering() {
308    sp<AMessage> msg = new AMessage(kWhatPollBuffering, this);
309    msg->post(1000000ll); // 1 second intervals
310}
311
312void NuPlayer::RTSPSource::checkBuffering(
313        bool *prepared, bool *underflow, bool *overflow, bool *startServer, bool *finished) {
314    size_t numTracks = mTracks.size();
315    size_t preparedCount, underflowCount, overflowCount, startCount, finishedCount;
316    preparedCount = underflowCount = overflowCount = startCount = finishedCount = 0;
317
318    size_t count = numTracks;
319    for (size_t i = 0; i < count; ++i) {
320        status_t finalResult;
321        TrackInfo *info = &mTracks.editItemAt(i);
322        sp<AnotherPacketSource> src = info->mSource;
323        if (src == NULL) {
324            --numTracks;
325            continue;
326        }
327        int64_t bufferedDurationUs = src->getBufferedDurationUs(&finalResult);
328
329        // isFinished when duration is 0 checks for EOS result only
330        if (bufferedDurationUs > kPrepareMarkUs || src->isFinished(/* duration */ 0)) {
331            ++preparedCount;
332        }
333
334        if (src->isFinished(/* duration */ 0)) {
335            ++overflowCount;
336            ++finishedCount;
337        } else {
338            if (bufferedDurationUs < kUnderflowMarkUs) {
339                ++underflowCount;
340            }
341            if (bufferedDurationUs > kOverflowMarkUs) {
342                ++overflowCount;
343            }
344            if (bufferedDurationUs < kStartServerMarkUs) {
345                ++startCount;
346            }
347        }
348    }
349
350    *prepared    = (preparedCount == numTracks);
351    *underflow   = (underflowCount > 0);
352    *overflow    = (overflowCount == numTracks);
353    *startServer = (startCount > 0);
354    *finished    = (finishedCount > 0);
355}
356
357void NuPlayer::RTSPSource::onPollBuffering() {
358    bool prepared, underflow, overflow, startServer, finished;
359    checkBuffering(&prepared, &underflow, &overflow, &startServer, &finished);
360
361    if (prepared && mInPreparationPhase) {
362        mInPreparationPhase = false;
363        notifyPrepared();
364    }
365
366    if (!mInPreparationPhase && underflow) {
367        startBufferingIfNecessary();
368    }
369
370    if (overflow && mHandler != NULL) {
371        stopBufferingIfNecessary();
372        mHandler->pause();
373    }
374
375    if (startServer && mHandler != NULL) {
376        mHandler->resume();
377    }
378
379    if (finished && mHandler != NULL) {
380        mHandler->cancelAccessUnitTimeoutCheck();
381    }
382
383    schedulePollBuffering();
384}
385
386void NuPlayer::RTSPSource::signalSourceEOS(status_t result) {
387    const bool audio = true;
388    const bool video = false;
389
390    sp<AnotherPacketSource> source = getSource(audio);
391    if (source != NULL) {
392        source->signalEOS(result);
393    }
394
395    source = getSource(video);
396    if (source != NULL) {
397        source->signalEOS(result);
398    }
399}
400
401bool NuPlayer::RTSPSource::sourceReachedEOS(bool audio) {
402    sp<AnotherPacketSource> source = getSource(audio);
403    status_t finalResult;
404    return (source != NULL &&
405            !source->hasBufferAvailable(&finalResult) &&
406            finalResult == ERROR_END_OF_STREAM);
407}
408
409bool NuPlayer::RTSPSource::sourceNearEOS(bool audio) {
410    sp<AnotherPacketSource> source = getSource(audio);
411    int64_t mediaDurationUs = 0;
412    getDuration(&mediaDurationUs);
413    return (source != NULL && source->isFinished(mediaDurationUs));
414}
415
416void NuPlayer::RTSPSource::onSignalEOS(const sp<AMessage> &msg) {
417    int32_t generation;
418    CHECK(msg->findInt32("generation", &generation));
419
420    if (generation != mSeekGeneration) {
421        return;
422    }
423
424    if (mEOSPending) {
425        signalSourceEOS(ERROR_END_OF_STREAM);
426        mEOSPending = false;
427    }
428}
429
430void NuPlayer::RTSPSource::postSourceEOSIfNecessary() {
431    const bool audio = true;
432    const bool video = false;
433    // If a source has detected near end, give it some time to retrieve more
434    // data before signaling EOS
435    if (sourceNearEOS(audio) || sourceNearEOS(video)) {
436        if (!mEOSPending) {
437            sp<AMessage> msg = new AMessage(kWhatSignalEOS, this);
438            msg->setInt32("generation", mSeekGeneration);
439            msg->post(kNearEOSTimeoutUs);
440            mEOSPending = true;
441        }
442    }
443}
444
445void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
446    if (msg->what() == kWhatDisconnect) {
447        sp<AReplyToken> replyID;
448        CHECK(msg->senderAwaitsResponse(&replyID));
449
450        mDisconnectReplyID = replyID;
451        finishDisconnectIfPossible();
452        return;
453    } else if (msg->what() == kWhatPerformSeek) {
454        int32_t generation;
455        CHECK(msg->findInt32("generation", &generation));
456        CHECK(msg->senderAwaitsResponse(&mSeekReplyID));
457
458        if (generation != mSeekGeneration) {
459            // obsolete.
460            finishSeek(OK);
461            return;
462        }
463
464        int64_t seekTimeUs;
465        CHECK(msg->findInt64("timeUs", &seekTimeUs));
466
467        performSeek(seekTimeUs);
468        return;
469    } else if (msg->what() == kWhatPollBuffering) {
470        onPollBuffering();
471        return;
472    } else if (msg->what() == kWhatSignalEOS) {
473        onSignalEOS(msg);
474        return;
475    }
476
477    CHECK_EQ(msg->what(), (int)kWhatNotify);
478
479    int32_t what;
480    CHECK(msg->findInt32("what", &what));
481
482    switch (what) {
483        case MyHandler::kWhatConnected:
484        {
485            onConnected();
486
487            notifyVideoSizeChanged();
488
489            uint32_t flags = 0;
490
491            if (mHandler->isSeekable()) {
492                flags = FLAG_CAN_PAUSE
493                        | FLAG_CAN_SEEK
494                        | FLAG_CAN_SEEK_BACKWARD
495                        | FLAG_CAN_SEEK_FORWARD;
496            }
497
498            notifyFlagsChanged(flags);
499            schedulePollBuffering();
500            break;
501        }
502
503        case MyHandler::kWhatDisconnected:
504        {
505            onDisconnected(msg);
506            break;
507        }
508
509        case MyHandler::kWhatSeekDone:
510        {
511            mState = CONNECTED;
512            // Unblock seekTo here in case we attempted to seek in a live stream
513            finishSeek(OK);
514            break;
515        }
516
517        case MyHandler::kWhatSeekPaused:
518        {
519            sp<AnotherPacketSource> source = getSource(true /* audio */);
520            if (source != NULL) {
521                source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
522                        /* extra */ NULL,
523                        /* discard */ true);
524            }
525            source = getSource(false /* video */);
526            if (source != NULL) {
527                source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
528                        /* extra */ NULL,
529                        /* discard */ true);
530            };
531
532            status_t err = OK;
533            msg->findInt32("err", &err);
534
535            if (err == OK) {
536                int64_t timeUs;
537                CHECK(msg->findInt64("time", &timeUs));
538                mHandler->continueSeekAfterPause(timeUs);
539            } else {
540                finishSeek(err);
541            }
542            break;
543        }
544
545        case MyHandler::kWhatAccessUnit:
546        {
547            size_t trackIndex;
548            CHECK(msg->findSize("trackIndex", &trackIndex));
549
550            if (mTSParser == NULL) {
551                CHECK_LT(trackIndex, mTracks.size());
552            } else {
553                CHECK_EQ(trackIndex, 0u);
554            }
555
556            sp<ABuffer> accessUnit;
557            CHECK(msg->findBuffer("accessUnit", &accessUnit));
558
559            int32_t damaged;
560            if (accessUnit->meta()->findInt32("damaged", &damaged)
561                    && damaged) {
562                ALOGI("dropping damaged access unit.");
563                break;
564            }
565
566            if (mTSParser != NULL) {
567                size_t offset = 0;
568                status_t err = OK;
569                while (offset + 188 <= accessUnit->size()) {
570                    err = mTSParser->feedTSPacket(
571                            accessUnit->data() + offset, 188);
572                    if (err != OK) {
573                        break;
574                    }
575
576                    offset += 188;
577                }
578
579                if (offset < accessUnit->size()) {
580                    err = ERROR_MALFORMED;
581                }
582
583                if (err != OK) {
584                    signalSourceEOS(err);
585                }
586
587                postSourceEOSIfNecessary();
588                break;
589            }
590
591            TrackInfo *info = &mTracks.editItemAt(trackIndex);
592
593            sp<AnotherPacketSource> source = info->mSource;
594            if (source != NULL) {
595                uint32_t rtpTime;
596                CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
597
598                if (!info->mNPTMappingValid) {
599                    // This is a live stream, we didn't receive any normal
600                    // playtime mapping. We won't map to npt time.
601                    source->queueAccessUnit(accessUnit);
602                    break;
603                }
604
605                int64_t nptUs =
606                    ((double)rtpTime - (double)info->mRTPTime)
607                        / info->mTimeScale
608                        * 1000000ll
609                        + info->mNormalPlaytimeUs;
610
611                accessUnit->meta()->setInt64("timeUs", nptUs);
612
613                source->queueAccessUnit(accessUnit);
614            }
615            postSourceEOSIfNecessary();
616            break;
617        }
618
619        case MyHandler::kWhatEOS:
620        {
621            int32_t finalResult;
622            CHECK(msg->findInt32("finalResult", &finalResult));
623            CHECK_NE(finalResult, (status_t)OK);
624
625            if (mTSParser != NULL) {
626                signalSourceEOS(finalResult);
627            }
628
629            size_t trackIndex;
630            CHECK(msg->findSize("trackIndex", &trackIndex));
631            CHECK_LT(trackIndex, mTracks.size());
632
633            TrackInfo *info = &mTracks.editItemAt(trackIndex);
634            sp<AnotherPacketSource> source = info->mSource;
635            if (source != NULL) {
636                source->signalEOS(finalResult);
637            }
638
639            break;
640        }
641
642        case MyHandler::kWhatSeekDiscontinuity:
643        {
644            size_t trackIndex;
645            CHECK(msg->findSize("trackIndex", &trackIndex));
646            CHECK_LT(trackIndex, mTracks.size());
647
648            TrackInfo *info = &mTracks.editItemAt(trackIndex);
649            sp<AnotherPacketSource> source = info->mSource;
650            if (source != NULL) {
651                source->queueDiscontinuity(
652                        ATSParser::DISCONTINUITY_TIME,
653                        NULL,
654                        true /* discard */);
655            }
656
657            break;
658        }
659
660        case MyHandler::kWhatNormalPlayTimeMapping:
661        {
662            size_t trackIndex;
663            CHECK(msg->findSize("trackIndex", &trackIndex));
664            CHECK_LT(trackIndex, mTracks.size());
665
666            uint32_t rtpTime;
667            CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
668
669            int64_t nptUs;
670            CHECK(msg->findInt64("nptUs", &nptUs));
671
672            TrackInfo *info = &mTracks.editItemAt(trackIndex);
673            info->mRTPTime = rtpTime;
674            info->mNormalPlaytimeUs = nptUs;
675            info->mNPTMappingValid = true;
676            break;
677        }
678
679        case SDPLoader::kWhatSDPLoaded:
680        {
681            onSDPLoaded(msg);
682            break;
683        }
684
685        default:
686            TRESPASS();
687    }
688}
689
690void NuPlayer::RTSPSource::onConnected() {
691    CHECK(mAudioTrack == NULL);
692    CHECK(mVideoTrack == NULL);
693
694    size_t numTracks = mHandler->countTracks();
695    for (size_t i = 0; i < numTracks; ++i) {
696        int32_t timeScale;
697        sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
698
699        const char *mime;
700        CHECK(format->findCString(kKeyMIMEType, &mime));
701
702        if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
703            // Very special case for MPEG2 Transport Streams.
704            CHECK_EQ(numTracks, 1u);
705
706            mTSParser = new ATSParser;
707            return;
708        }
709
710        bool isAudio = !strncasecmp(mime, "audio/", 6);
711        bool isVideo = !strncasecmp(mime, "video/", 6);
712
713        TrackInfo info;
714        info.mTimeScale = timeScale;
715        info.mRTPTime = 0;
716        info.mNormalPlaytimeUs = 0ll;
717        info.mNPTMappingValid = false;
718
719        if ((isAudio && mAudioTrack == NULL)
720                || (isVideo && mVideoTrack == NULL)) {
721            sp<AnotherPacketSource> source = new AnotherPacketSource(format);
722
723            if (isAudio) {
724                mAudioTrack = source;
725            } else {
726                mVideoTrack = source;
727            }
728
729            info.mSource = source;
730        }
731
732        mTracks.push(info);
733    }
734
735    mState = CONNECTED;
736}
737
738void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
739    status_t err;
740    CHECK(msg->findInt32("result", &err));
741
742    mSDPLoader.clear();
743
744    if (mDisconnectReplyID != 0) {
745        err = UNKNOWN_ERROR;
746    }
747
748    if (err == OK) {
749        sp<ASessionDescription> desc;
750        sp<RefBase> obj;
751        CHECK(msg->findObject("description", &obj));
752        desc = static_cast<ASessionDescription *>(obj.get());
753
754        AString rtspUri;
755        if (!desc->findAttribute(0, "a=control", &rtspUri)) {
756            ALOGE("Unable to find url in SDP");
757            err = UNKNOWN_ERROR;
758        } else {
759            sp<AMessage> notify = new AMessage(kWhatNotify, this);
760
761            mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
762            mLooper->registerHandler(mHandler);
763
764            mHandler->loadSDP(desc);
765        }
766    }
767
768    if (err != OK) {
769        if (mState == CONNECTING) {
770            // We're still in the preparation phase, signal that it
771            // failed.
772            notifyPrepared(err);
773        }
774
775        mState = DISCONNECTED;
776        setError(err);
777
778        if (mDisconnectReplyID != 0) {
779            finishDisconnectIfPossible();
780        }
781    }
782}
783
784void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
785    if (mState == DISCONNECTED) {
786        return;
787    }
788
789    status_t err;
790    CHECK(msg->findInt32("result", &err));
791    CHECK_NE(err, (status_t)OK);
792
793    mLooper->unregisterHandler(mHandler->id());
794    mHandler.clear();
795
796    if (mState == CONNECTING) {
797        // We're still in the preparation phase, signal that it
798        // failed.
799        notifyPrepared(err);
800    }
801
802    mState = DISCONNECTED;
803    setError(err);
804
805    if (mDisconnectReplyID != 0) {
806        finishDisconnectIfPossible();
807    }
808}
809
810void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
811    if (mState != DISCONNECTED) {
812        if (mHandler != NULL) {
813            mHandler->disconnect();
814        } else if (mSDPLoader != NULL) {
815            mSDPLoader->cancel();
816        }
817        return;
818    }
819
820    (new AMessage)->postReply(mDisconnectReplyID);
821    mDisconnectReplyID = 0;
822}
823
824void NuPlayer::RTSPSource::setError(status_t err) {
825    Mutex::Autolock _l(mBufferingLock);
826    mFinalResult = err;
827}
828
829void NuPlayer::RTSPSource::startBufferingIfNecessary() {
830    Mutex::Autolock _l(mBufferingLock);
831
832    if (!mBuffering) {
833        mBuffering = true;
834
835        sp<AMessage> notify = dupNotify();
836        notify->setInt32("what", kWhatPauseOnBufferingStart);
837        notify->post();
838    }
839}
840
841bool NuPlayer::RTSPSource::stopBufferingIfNecessary() {
842    Mutex::Autolock _l(mBufferingLock);
843
844    if (mBuffering) {
845        if (!haveSufficientDataOnAllTracks()) {
846            return false;
847        }
848
849        mBuffering = false;
850
851        sp<AMessage> notify = dupNotify();
852        notify->setInt32("what", kWhatResumeOnBufferingEnd);
853        notify->post();
854    }
855
856    return true;
857}
858
859void NuPlayer::RTSPSource::finishSeek(status_t err) {
860    if (mSeekReplyID == NULL) {
861        return;
862    }
863    sp<AMessage> seekReply = new AMessage;
864    seekReply->setInt32("err", err);
865    seekReply->postReply(mSeekReplyID);
866    mSeekReplyID = NULL;
867}
868
869}  // namespace android
870