RTSPSource.cpp revision e67ba383dd585d2c253986a39225e0d6d05755f3
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> &notify,
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(false),
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->unregisterHandler(id());
74        mLooper->stop();
75    }
76}
77
78void NuPlayer::RTSPSource::prepareAsync() {
79    if (mIsSDP && mHTTPService == NULL) {
80        notifyPrepared(BAD_VALUE);
81        return;
82    }
83
84    if (mLooper == NULL) {
85        mLooper = new ALooper;
86        mLooper->setName("rtsp");
87        mLooper->start();
88
89        mLooper->registerHandler(this);
90    }
91
92    CHECK(mHandler == NULL);
93    CHECK(mSDPLoader == NULL);
94
95    sp<AMessage> notify = new AMessage(kWhatNotify, this);
96
97    CHECK_EQ(mState, (int)DISCONNECTED);
98    mState = CONNECTING;
99
100    if (mIsSDP) {
101        mSDPLoader = new SDPLoader(notify,
102                (mFlags & kFlagIncognito) ? SDPLoader::kFlagIncognito : 0,
103                mHTTPService);
104
105        mSDPLoader->load(
106                mURL.c_str(), mExtraHeaders.isEmpty() ? NULL : &mExtraHeaders);
107    } else {
108        mHandler = new MyHandler(mURL.c_str(), notify, mUIDValid, mUID);
109        mLooper->registerHandler(mHandler);
110
111        mHandler->connect();
112    }
113
114    startBufferingIfNecessary();
115}
116
117void NuPlayer::RTSPSource::start() {
118}
119
120void NuPlayer::RTSPSource::stop() {
121    if (mLooper == NULL) {
122        return;
123    }
124    sp<AMessage> msg = new AMessage(kWhatDisconnect, this);
125
126    sp<AMessage> dummy;
127    msg->postAndAwaitResponse(&dummy);
128}
129
130void NuPlayer::RTSPSource::pause() {
131    int64_t mediaDurationUs = 0;
132    getDuration(&mediaDurationUs);
133    for (size_t index = 0; index < mTracks.size(); index++) {
134        TrackInfo *info = &mTracks.editItemAt(index);
135        sp<AnotherPacketSource> source = info->mSource;
136
137        // Check if EOS or ERROR is received
138        if (source != NULL && source->isFinished(mediaDurationUs)) {
139            return;
140        }
141    }
142    if (mHandler != NULL) {
143        mHandler->pause();
144    }
145}
146
147void NuPlayer::RTSPSource::resume() {
148    if (mHandler != NULL) {
149        mHandler->resume();
150    }
151}
152
153status_t NuPlayer::RTSPSource::feedMoreTSData() {
154    Mutex::Autolock _l(mBufferingLock);
155    return mFinalResult;
156}
157
158sp<MetaData> NuPlayer::RTSPSource::getFormatMeta(bool audio) {
159    sp<AnotherPacketSource> source = getSource(audio);
160
161    if (source == NULL) {
162        return NULL;
163    }
164
165    return source->getFormat();
166}
167
168bool NuPlayer::RTSPSource::haveSufficientDataOnAllTracks() {
169    // We're going to buffer at least 2 secs worth data on all tracks before
170    // starting playback (both at startup and after a seek).
171
172    static const int64_t kMinDurationUs = 2000000ll;
173
174    int64_t mediaDurationUs = 0;
175    getDuration(&mediaDurationUs);
176    if ((mAudioTrack != NULL && mAudioTrack->isFinished(mediaDurationUs))
177            || (mVideoTrack != NULL && mVideoTrack->isFinished(mediaDurationUs))) {
178        return true;
179    }
180
181    status_t err;
182    int64_t durationUs;
183    if (mAudioTrack != NULL
184            && (durationUs = mAudioTrack->getBufferedDurationUs(&err))
185                    < kMinDurationUs
186            && err == OK) {
187        ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
188              durationUs / 1E6);
189        return false;
190    }
191
192    if (mVideoTrack != NULL
193            && (durationUs = mVideoTrack->getBufferedDurationUs(&err))
194                    < kMinDurationUs
195            && err == OK) {
196        ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
197              durationUs / 1E6);
198        return false;
199    }
200
201    return true;
202}
203
204status_t NuPlayer::RTSPSource::dequeueAccessUnit(
205        bool audio, sp<ABuffer> *accessUnit) {
206    if (!stopBufferingIfNecessary()) {
207        return -EWOULDBLOCK;
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                startBufferingIfNecessary();
250            }
251
252            return -EWOULDBLOCK;
253        }
254        return finalResult;
255    }
256
257    setEOSTimeout(audio, 0);
258
259    return source->dequeueAccessUnit(accessUnit);
260}
261
262sp<AnotherPacketSource> NuPlayer::RTSPSource::getSource(bool audio) {
263    if (mTSParser != NULL) {
264        sp<MediaSource> source = mTSParser->getSource(
265                audio ? ATSParser::AUDIO : ATSParser::VIDEO);
266
267        return static_cast<AnotherPacketSource *>(source.get());
268    }
269
270    return audio ? mAudioTrack : mVideoTrack;
271}
272
273void NuPlayer::RTSPSource::setEOSTimeout(bool audio, int64_t timeout) {
274    if (audio) {
275        mEOSTimeoutAudio = timeout;
276    } else {
277        mEOSTimeoutVideo = timeout;
278    }
279}
280
281status_t NuPlayer::RTSPSource::getDuration(int64_t *durationUs) {
282    *durationUs = 0ll;
283
284    int64_t audioDurationUs;
285    if (mAudioTrack != NULL
286            && mAudioTrack->getFormat()->findInt64(
287                kKeyDuration, &audioDurationUs)
288            && audioDurationUs > *durationUs) {
289        *durationUs = audioDurationUs;
290    }
291
292    int64_t videoDurationUs;
293    if (mVideoTrack != NULL
294            && mVideoTrack->getFormat()->findInt64(
295                kKeyDuration, &videoDurationUs)
296            && videoDurationUs > *durationUs) {
297        *durationUs = videoDurationUs;
298    }
299
300    return OK;
301}
302
303status_t NuPlayer::RTSPSource::seekTo(int64_t seekTimeUs) {
304    sp<AMessage> msg = new AMessage(kWhatPerformSeek, this);
305    msg->setInt32("generation", ++mSeekGeneration);
306    msg->setInt64("timeUs", seekTimeUs);
307
308    sp<AMessage> response;
309    status_t err = msg->postAndAwaitResponse(&response);
310    if (err == OK && response != NULL) {
311        CHECK(response->findInt32("err", &err));
312    }
313
314    return err;
315}
316
317void NuPlayer::RTSPSource::performSeek(int64_t seekTimeUs) {
318    if (mState != CONNECTED) {
319        finishSeek(INVALID_OPERATION);
320        return;
321    }
322
323    mState = SEEKING;
324    mHandler->seek(seekTimeUs);
325}
326
327void NuPlayer::RTSPSource::onMessageReceived(const sp<AMessage> &msg) {
328    if (msg->what() == kWhatDisconnect) {
329        sp<AReplyToken> replyID;
330        CHECK(msg->senderAwaitsResponse(&replyID));
331
332        mDisconnectReplyID = replyID;
333        finishDisconnectIfPossible();
334        return;
335    } else if (msg->what() == kWhatPerformSeek) {
336        int32_t generation;
337        CHECK(msg->findInt32("generation", &generation));
338        CHECK(msg->senderAwaitsResponse(&mSeekReplyID));
339
340        if (generation != mSeekGeneration) {
341            // obsolete.
342            finishSeek(OK);
343            return;
344        }
345
346        int64_t seekTimeUs;
347        CHECK(msg->findInt64("timeUs", &seekTimeUs));
348
349        performSeek(seekTimeUs);
350        return;
351    }
352
353    CHECK_EQ(msg->what(), (int)kWhatNotify);
354
355    int32_t what;
356    CHECK(msg->findInt32("what", &what));
357
358    switch (what) {
359        case MyHandler::kWhatConnected:
360        {
361            onConnected();
362
363            notifyVideoSizeChanged();
364
365            uint32_t flags = 0;
366
367            if (mHandler->isSeekable()) {
368                flags = FLAG_CAN_PAUSE
369                        | FLAG_CAN_SEEK
370                        | FLAG_CAN_SEEK_BACKWARD
371                        | FLAG_CAN_SEEK_FORWARD;
372            }
373
374            notifyFlagsChanged(flags);
375            notifyPrepared();
376            break;
377        }
378
379        case MyHandler::kWhatDisconnected:
380        {
381            onDisconnected(msg);
382            break;
383        }
384
385        case MyHandler::kWhatSeekDone:
386        {
387            mState = CONNECTED;
388            if (mSeekReplyID != NULL) {
389                // Unblock seekTo here in case we attempted to seek in a live stream
390                finishSeek(OK);
391            }
392            break;
393        }
394
395        case MyHandler::kWhatSeekPaused:
396        {
397            sp<AnotherPacketSource> source = getSource(true /* audio */);
398            if (source != NULL) {
399                source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
400                        /* extra */ NULL,
401                        /* discard */ true);
402            }
403            source = getSource(false /* video */);
404            if (source != NULL) {
405                source->queueDiscontinuity(ATSParser::DISCONTINUITY_NONE,
406                        /* extra */ NULL,
407                        /* discard */ true);
408            };
409
410            status_t err = OK;
411            msg->findInt32("err", &err);
412            finishSeek(err);
413
414            if (err == OK) {
415                int64_t timeUs;
416                CHECK(msg->findInt64("time", &timeUs));
417                mHandler->continueSeekAfterPause(timeUs);
418            }
419            break;
420        }
421
422        case MyHandler::kWhatAccessUnit:
423        {
424            size_t trackIndex;
425            CHECK(msg->findSize("trackIndex", &trackIndex));
426
427            if (mTSParser == NULL) {
428                CHECK_LT(trackIndex, mTracks.size());
429            } else {
430                CHECK_EQ(trackIndex, 0u);
431            }
432
433            sp<ABuffer> accessUnit;
434            CHECK(msg->findBuffer("accessUnit", &accessUnit));
435
436            int32_t damaged;
437            if (accessUnit->meta()->findInt32("damaged", &damaged)
438                    && damaged) {
439                ALOGI("dropping damaged access unit.");
440                break;
441            }
442
443            if (mTSParser != NULL) {
444                size_t offset = 0;
445                status_t err = OK;
446                while (offset + 188 <= accessUnit->size()) {
447                    err = mTSParser->feedTSPacket(
448                            accessUnit->data() + offset, 188);
449                    if (err != OK) {
450                        break;
451                    }
452
453                    offset += 188;
454                }
455
456                if (offset < accessUnit->size()) {
457                    err = ERROR_MALFORMED;
458                }
459
460                if (err != OK) {
461                    sp<AnotherPacketSource> source = getSource(false /* audio */);
462                    if (source != NULL) {
463                        source->signalEOS(err);
464                    }
465
466                    source = getSource(true /* audio */);
467                    if (source != NULL) {
468                        source->signalEOS(err);
469                    }
470                }
471                break;
472            }
473
474            TrackInfo *info = &mTracks.editItemAt(trackIndex);
475
476            sp<AnotherPacketSource> source = info->mSource;
477            if (source != NULL) {
478                uint32_t rtpTime;
479                CHECK(accessUnit->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
480
481                if (!info->mNPTMappingValid) {
482                    // This is a live stream, we didn't receive any normal
483                    // playtime mapping. We won't map to npt time.
484                    source->queueAccessUnit(accessUnit);
485                    break;
486                }
487
488                int64_t nptUs =
489                    ((double)rtpTime - (double)info->mRTPTime)
490                        / info->mTimeScale
491                        * 1000000ll
492                        + info->mNormalPlaytimeUs;
493
494                accessUnit->meta()->setInt64("timeUs", nptUs);
495
496                source->queueAccessUnit(accessUnit);
497            }
498            break;
499        }
500
501        case MyHandler::kWhatEOS:
502        {
503            int32_t finalResult;
504            CHECK(msg->findInt32("finalResult", &finalResult));
505            CHECK_NE(finalResult, (status_t)OK);
506
507            if (mTSParser != NULL) {
508                sp<AnotherPacketSource> source = getSource(false /* audio */);
509                if (source != NULL) {
510                    source->signalEOS(finalResult);
511                }
512
513                source = getSource(true /* audio */);
514                if (source != NULL) {
515                    source->signalEOS(finalResult);
516                }
517
518                return;
519            }
520
521            size_t trackIndex;
522            CHECK(msg->findSize("trackIndex", &trackIndex));
523            CHECK_LT(trackIndex, mTracks.size());
524
525            TrackInfo *info = &mTracks.editItemAt(trackIndex);
526            sp<AnotherPacketSource> source = info->mSource;
527            if (source != NULL) {
528                source->signalEOS(finalResult);
529            }
530
531            break;
532        }
533
534        case MyHandler::kWhatSeekDiscontinuity:
535        {
536            size_t trackIndex;
537            CHECK(msg->findSize("trackIndex", &trackIndex));
538            CHECK_LT(trackIndex, mTracks.size());
539
540            TrackInfo *info = &mTracks.editItemAt(trackIndex);
541            sp<AnotherPacketSource> source = info->mSource;
542            if (source != NULL) {
543                source->queueDiscontinuity(
544                        ATSParser::DISCONTINUITY_TIME,
545                        NULL,
546                        true /* discard */);
547            }
548
549            break;
550        }
551
552        case MyHandler::kWhatNormalPlayTimeMapping:
553        {
554            size_t trackIndex;
555            CHECK(msg->findSize("trackIndex", &trackIndex));
556            CHECK_LT(trackIndex, mTracks.size());
557
558            uint32_t rtpTime;
559            CHECK(msg->findInt32("rtpTime", (int32_t *)&rtpTime));
560
561            int64_t nptUs;
562            CHECK(msg->findInt64("nptUs", &nptUs));
563
564            TrackInfo *info = &mTracks.editItemAt(trackIndex);
565            info->mRTPTime = rtpTime;
566            info->mNormalPlaytimeUs = nptUs;
567            info->mNPTMappingValid = true;
568            break;
569        }
570
571        case SDPLoader::kWhatSDPLoaded:
572        {
573            onSDPLoaded(msg);
574            break;
575        }
576
577        default:
578            TRESPASS();
579    }
580}
581
582void NuPlayer::RTSPSource::onConnected() {
583    CHECK(mAudioTrack == NULL);
584    CHECK(mVideoTrack == NULL);
585
586    size_t numTracks = mHandler->countTracks();
587    for (size_t i = 0; i < numTracks; ++i) {
588        int32_t timeScale;
589        sp<MetaData> format = mHandler->getTrackFormat(i, &timeScale);
590
591        const char *mime;
592        CHECK(format->findCString(kKeyMIMEType, &mime));
593
594        if (!strcasecmp(mime, MEDIA_MIMETYPE_CONTAINER_MPEG2TS)) {
595            // Very special case for MPEG2 Transport Streams.
596            CHECK_EQ(numTracks, 1u);
597
598            mTSParser = new ATSParser;
599            return;
600        }
601
602        bool isAudio = !strncasecmp(mime, "audio/", 6);
603        bool isVideo = !strncasecmp(mime, "video/", 6);
604
605        TrackInfo info;
606        info.mTimeScale = timeScale;
607        info.mRTPTime = 0;
608        info.mNormalPlaytimeUs = 0ll;
609        info.mNPTMappingValid = false;
610
611        if ((isAudio && mAudioTrack == NULL)
612                || (isVideo && mVideoTrack == NULL)) {
613            sp<AnotherPacketSource> source = new AnotherPacketSource(format);
614
615            if (isAudio) {
616                mAudioTrack = source;
617            } else {
618                mVideoTrack = source;
619            }
620
621            info.mSource = source;
622        }
623
624        mTracks.push(info);
625    }
626
627    mState = CONNECTED;
628}
629
630void NuPlayer::RTSPSource::onSDPLoaded(const sp<AMessage> &msg) {
631    status_t err;
632    CHECK(msg->findInt32("result", &err));
633
634    mSDPLoader.clear();
635
636    if (mDisconnectReplyID != 0) {
637        err = UNKNOWN_ERROR;
638    }
639
640    if (err == OK) {
641        sp<ASessionDescription> desc;
642        sp<RefBase> obj;
643        CHECK(msg->findObject("description", &obj));
644        desc = static_cast<ASessionDescription *>(obj.get());
645
646        AString rtspUri;
647        if (!desc->findAttribute(0, "a=control", &rtspUri)) {
648            ALOGE("Unable to find url in SDP");
649            err = UNKNOWN_ERROR;
650        } else {
651            sp<AMessage> notify = new AMessage(kWhatNotify, this);
652
653            mHandler = new MyHandler(rtspUri.c_str(), notify, mUIDValid, mUID);
654            mLooper->registerHandler(mHandler);
655
656            mHandler->loadSDP(desc);
657        }
658    }
659
660    if (err != OK) {
661        if (mState == CONNECTING) {
662            // We're still in the preparation phase, signal that it
663            // failed.
664            notifyPrepared(err);
665        }
666
667        mState = DISCONNECTED;
668        setError(err);
669
670        if (mDisconnectReplyID != 0) {
671            finishDisconnectIfPossible();
672        }
673    }
674}
675
676void NuPlayer::RTSPSource::onDisconnected(const sp<AMessage> &msg) {
677    if (mState == DISCONNECTED) {
678        return;
679    }
680
681    status_t err;
682    CHECK(msg->findInt32("result", &err));
683    CHECK_NE(err, (status_t)OK);
684
685    mLooper->unregisterHandler(mHandler->id());
686    mHandler.clear();
687
688    if (mState == CONNECTING) {
689        // We're still in the preparation phase, signal that it
690        // failed.
691        notifyPrepared(err);
692    }
693
694    mState = DISCONNECTED;
695    setError(err);
696
697    if (mDisconnectReplyID != 0) {
698        finishDisconnectIfPossible();
699    }
700}
701
702void NuPlayer::RTSPSource::finishDisconnectIfPossible() {
703    if (mState != DISCONNECTED) {
704        if (mHandler != NULL) {
705            mHandler->disconnect();
706        } else if (mSDPLoader != NULL) {
707            mSDPLoader->cancel();
708        }
709        return;
710    }
711
712    (new AMessage)->postReply(mDisconnectReplyID);
713    mDisconnectReplyID = 0;
714}
715
716void NuPlayer::RTSPSource::setError(status_t err) {
717    Mutex::Autolock _l(mBufferingLock);
718    mFinalResult = err;
719}
720
721void NuPlayer::RTSPSource::startBufferingIfNecessary() {
722    Mutex::Autolock _l(mBufferingLock);
723
724    if (!mBuffering) {
725        mBuffering = true;
726
727        sp<AMessage> notify = dupNotify();
728        notify->setInt32("what", kWhatBufferingStart);
729        notify->post();
730    }
731}
732
733bool NuPlayer::RTSPSource::stopBufferingIfNecessary() {
734    Mutex::Autolock _l(mBufferingLock);
735
736    if (mBuffering) {
737        if (!haveSufficientDataOnAllTracks()) {
738            return false;
739        }
740
741        mBuffering = false;
742
743        sp<AMessage> notify = dupNotify();
744        notify->setInt32("what", kWhatBufferingEnd);
745        notify->post();
746    }
747
748    return true;
749}
750
751void NuPlayer::RTSPSource::finishSeek(status_t err) {
752    CHECK(mSeekReplyID != NULL);
753    sp<AMessage> seekReply = new AMessage;
754    seekReply->setInt32("err", err);
755    seekReply->postReply(mSeekReplyID);
756    mSeekReplyID = NULL;
757}
758
759}  // namespace android
760