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