PlaylistFetcher.cpp revision afcc4fcbb3a094ec2221d6e523772e76894d1f00
1/*
2 * Copyright (C) 2012 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 "PlaylistFetcher"
19#include <utils/Log.h>
20
21#include "PlaylistFetcher.h"
22
23#include "LiveDataSource.h"
24#include "LiveSession.h"
25#include "M3UParser.h"
26
27#include "include/avc_utils.h"
28#include "include/HTTPBase.h"
29#include "include/ID3.h"
30#include "mpeg2ts/AnotherPacketSource.h"
31
32#include <media/IStreamSource.h>
33#include <media/stagefright/foundation/ABitReader.h>
34#include <media/stagefright/foundation/ABuffer.h>
35#include <media/stagefright/foundation/ADebug.h>
36#include <media/stagefright/foundation/hexdump.h>
37#include <media/stagefright/FileSource.h>
38#include <media/stagefright/MediaDefs.h>
39#include <media/stagefright/MetaData.h>
40#include <media/stagefright/Utils.h>
41
42#include <ctype.h>
43#include <inttypes.h>
44#include <openssl/aes.h>
45#include <openssl/md5.h>
46
47namespace android {
48
49// static
50const int64_t PlaylistFetcher::kMinBufferedDurationUs = 10000000ll;
51const int64_t PlaylistFetcher::kMaxMonitorDelayUs = 3000000ll;
52// LCM of 188 (size of a TS packet) & 1k works well
53const int32_t PlaylistFetcher::kDownloadBlockSize = 47 * 1024;
54const int32_t PlaylistFetcher::kNumSkipFrames = 5;
55
56PlaylistFetcher::PlaylistFetcher(
57        const sp<AMessage> &notify,
58        const sp<LiveSession> &session,
59        const char *uri,
60        int32_t subtitleGeneration)
61    : mNotify(notify),
62      mStartTimeUsNotify(notify->dup()),
63      mSession(session),
64      mURI(uri),
65      mStreamTypeMask(0),
66      mStartTimeUs(-1ll),
67      mSegmentStartTimeUs(-1ll),
68      mDiscontinuitySeq(-1ll),
69      mStartTimeUsRelative(false),
70      mLastPlaylistFetchTimeUs(-1ll),
71      mSeqNumber(-1),
72      mNumRetries(0),
73      mStartup(true),
74      mAdaptive(false),
75      mPrepared(false),
76      mNextPTSTimeUs(-1ll),
77      mMonitorQueueGeneration(0),
78      mSubtitleGeneration(subtitleGeneration),
79      mRefreshState(INITIAL_MINIMUM_RELOAD_DELAY),
80      mFirstPTSValid(false),
81      mAbsoluteTimeAnchorUs(0ll),
82      mVideoBuffer(new AnotherPacketSource(NULL)) {
83    memset(mPlaylistHash, 0, sizeof(mPlaylistHash));
84    mStartTimeUsNotify->setInt32("what", kWhatStartedAt);
85    mStartTimeUsNotify->setInt32("streamMask", 0);
86}
87
88PlaylistFetcher::~PlaylistFetcher() {
89}
90
91int64_t PlaylistFetcher::getSegmentStartTimeUs(int32_t seqNumber) const {
92    CHECK(mPlaylist != NULL);
93
94    int32_t firstSeqNumberInPlaylist;
95    if (mPlaylist->meta() == NULL || !mPlaylist->meta()->findInt32(
96                "media-sequence", &firstSeqNumberInPlaylist)) {
97        firstSeqNumberInPlaylist = 0;
98    }
99
100    int32_t lastSeqNumberInPlaylist =
101        firstSeqNumberInPlaylist + (int32_t)mPlaylist->size() - 1;
102
103    CHECK_GE(seqNumber, firstSeqNumberInPlaylist);
104    CHECK_LE(seqNumber, lastSeqNumberInPlaylist);
105
106    int64_t segmentStartUs = 0ll;
107    for (int32_t index = 0;
108            index < seqNumber - firstSeqNumberInPlaylist; ++index) {
109        sp<AMessage> itemMeta;
110        CHECK(mPlaylist->itemAt(
111                    index, NULL /* uri */, &itemMeta));
112
113        int64_t itemDurationUs;
114        CHECK(itemMeta->findInt64("durationUs", &itemDurationUs));
115
116        segmentStartUs += itemDurationUs;
117    }
118
119    return segmentStartUs;
120}
121
122int64_t PlaylistFetcher::delayUsToRefreshPlaylist() const {
123    int64_t nowUs = ALooper::GetNowUs();
124
125    if (mPlaylist == NULL || mLastPlaylistFetchTimeUs < 0ll) {
126        CHECK_EQ((int)mRefreshState, (int)INITIAL_MINIMUM_RELOAD_DELAY);
127        return 0ll;
128    }
129
130    if (mPlaylist->isComplete()) {
131        return (~0llu >> 1);
132    }
133
134    int32_t targetDurationSecs;
135    CHECK(mPlaylist->meta()->findInt32("target-duration", &targetDurationSecs));
136
137    int64_t targetDurationUs = targetDurationSecs * 1000000ll;
138
139    int64_t minPlaylistAgeUs;
140
141    switch (mRefreshState) {
142        case INITIAL_MINIMUM_RELOAD_DELAY:
143        {
144            size_t n = mPlaylist->size();
145            if (n > 0) {
146                sp<AMessage> itemMeta;
147                CHECK(mPlaylist->itemAt(n - 1, NULL /* uri */, &itemMeta));
148
149                int64_t itemDurationUs;
150                CHECK(itemMeta->findInt64("durationUs", &itemDurationUs));
151
152                minPlaylistAgeUs = itemDurationUs;
153                break;
154            }
155
156            // fall through
157        }
158
159        case FIRST_UNCHANGED_RELOAD_ATTEMPT:
160        {
161            minPlaylistAgeUs = targetDurationUs / 2;
162            break;
163        }
164
165        case SECOND_UNCHANGED_RELOAD_ATTEMPT:
166        {
167            minPlaylistAgeUs = (targetDurationUs * 3) / 2;
168            break;
169        }
170
171        case THIRD_UNCHANGED_RELOAD_ATTEMPT:
172        {
173            minPlaylistAgeUs = targetDurationUs * 3;
174            break;
175        }
176
177        default:
178            TRESPASS();
179            break;
180    }
181
182    int64_t delayUs = mLastPlaylistFetchTimeUs + minPlaylistAgeUs - nowUs;
183    return delayUs > 0ll ? delayUs : 0ll;
184}
185
186status_t PlaylistFetcher::decryptBuffer(
187        size_t playlistIndex, const sp<ABuffer> &buffer,
188        bool first) {
189    sp<AMessage> itemMeta;
190    bool found = false;
191    AString method;
192
193    for (ssize_t i = playlistIndex; i >= 0; --i) {
194        AString uri;
195        CHECK(mPlaylist->itemAt(i, &uri, &itemMeta));
196
197        if (itemMeta->findString("cipher-method", &method)) {
198            found = true;
199            break;
200        }
201    }
202
203    if (!found) {
204        method = "NONE";
205    }
206    buffer->meta()->setString("cipher-method", method.c_str());
207
208    if (method == "NONE") {
209        return OK;
210    } else if (!(method == "AES-128")) {
211        ALOGE("Unsupported cipher method '%s'", method.c_str());
212        return ERROR_UNSUPPORTED;
213    }
214
215    AString keyURI;
216    if (!itemMeta->findString("cipher-uri", &keyURI)) {
217        ALOGE("Missing key uri");
218        return ERROR_MALFORMED;
219    }
220
221    ssize_t index = mAESKeyForURI.indexOfKey(keyURI);
222
223    sp<ABuffer> key;
224    if (index >= 0) {
225        key = mAESKeyForURI.valueAt(index);
226    } else {
227        ssize_t err = mSession->fetchFile(keyURI.c_str(), &key);
228
229        if (err < 0) {
230            ALOGE("failed to fetch cipher key from '%s'.", keyURI.c_str());
231            return ERROR_IO;
232        } else if (key->size() != 16) {
233            ALOGE("key file '%s' wasn't 16 bytes in size.", keyURI.c_str());
234            return ERROR_MALFORMED;
235        }
236
237        mAESKeyForURI.add(keyURI, key);
238    }
239
240    AES_KEY aes_key;
241    if (AES_set_decrypt_key(key->data(), 128, &aes_key) != 0) {
242        ALOGE("failed to set AES decryption key.");
243        return UNKNOWN_ERROR;
244    }
245
246    size_t n = buffer->size();
247    if (!n) {
248        return OK;
249    }
250    CHECK(n % 16 == 0);
251
252    if (first) {
253        // If decrypting the first block in a file, read the iv from the manifest
254        // or derive the iv from the file's sequence number.
255
256        AString iv;
257        if (itemMeta->findString("cipher-iv", &iv)) {
258            if ((!iv.startsWith("0x") && !iv.startsWith("0X"))
259                    || iv.size() != 16 * 2 + 2) {
260                ALOGE("malformed cipher IV '%s'.", iv.c_str());
261                return ERROR_MALFORMED;
262            }
263
264            memset(mAESInitVec, 0, sizeof(mAESInitVec));
265            for (size_t i = 0; i < 16; ++i) {
266                char c1 = tolower(iv.c_str()[2 + 2 * i]);
267                char c2 = tolower(iv.c_str()[3 + 2 * i]);
268                if (!isxdigit(c1) || !isxdigit(c2)) {
269                    ALOGE("malformed cipher IV '%s'.", iv.c_str());
270                    return ERROR_MALFORMED;
271                }
272                uint8_t nibble1 = isdigit(c1) ? c1 - '0' : c1 - 'a' + 10;
273                uint8_t nibble2 = isdigit(c2) ? c2 - '0' : c2 - 'a' + 10;
274
275                mAESInitVec[i] = nibble1 << 4 | nibble2;
276            }
277        } else {
278            memset(mAESInitVec, 0, sizeof(mAESInitVec));
279            mAESInitVec[15] = mSeqNumber & 0xff;
280            mAESInitVec[14] = (mSeqNumber >> 8) & 0xff;
281            mAESInitVec[13] = (mSeqNumber >> 16) & 0xff;
282            mAESInitVec[12] = (mSeqNumber >> 24) & 0xff;
283        }
284    }
285
286    AES_cbc_encrypt(
287            buffer->data(), buffer->data(), buffer->size(),
288            &aes_key, mAESInitVec, AES_DECRYPT);
289
290    return OK;
291}
292
293status_t PlaylistFetcher::checkDecryptPadding(const sp<ABuffer> &buffer) {
294    status_t err;
295    AString method;
296    CHECK(buffer->meta()->findString("cipher-method", &method));
297    if (method == "NONE") {
298        return OK;
299    }
300
301    uint8_t padding = 0;
302    if (buffer->size() > 0) {
303        padding = buffer->data()[buffer->size() - 1];
304    }
305
306    if (padding > 16) {
307        return ERROR_MALFORMED;
308    }
309
310    for (size_t i = buffer->size() - padding; i < padding; i++) {
311        if (buffer->data()[i] != padding) {
312            return ERROR_MALFORMED;
313        }
314    }
315
316    buffer->setRange(buffer->offset(), buffer->size() - padding);
317    return OK;
318}
319
320void PlaylistFetcher::postMonitorQueue(int64_t delayUs, int64_t minDelayUs) {
321    int64_t maxDelayUs = delayUsToRefreshPlaylist();
322    if (maxDelayUs < minDelayUs) {
323        maxDelayUs = minDelayUs;
324    }
325    if (delayUs > maxDelayUs) {
326        ALOGV("Need to refresh playlist in %" PRId64 , maxDelayUs);
327        delayUs = maxDelayUs;
328    }
329    sp<AMessage> msg = new AMessage(kWhatMonitorQueue, id());
330    msg->setInt32("generation", mMonitorQueueGeneration);
331    msg->post(delayUs);
332}
333
334void PlaylistFetcher::cancelMonitorQueue() {
335    ++mMonitorQueueGeneration;
336}
337
338void PlaylistFetcher::startAsync(
339        const sp<AnotherPacketSource> &audioSource,
340        const sp<AnotherPacketSource> &videoSource,
341        const sp<AnotherPacketSource> &subtitleSource,
342        int64_t startTimeUs,
343        int64_t segmentStartTimeUs,
344        int32_t startDiscontinuitySeq,
345        bool adaptive) {
346    sp<AMessage> msg = new AMessage(kWhatStart, id());
347
348    uint32_t streamTypeMask = 0ul;
349
350    if (audioSource != NULL) {
351        msg->setPointer("audioSource", audioSource.get());
352        streamTypeMask |= LiveSession::STREAMTYPE_AUDIO;
353    }
354
355    if (videoSource != NULL) {
356        msg->setPointer("videoSource", videoSource.get());
357        streamTypeMask |= LiveSession::STREAMTYPE_VIDEO;
358    }
359
360    if (subtitleSource != NULL) {
361        msg->setPointer("subtitleSource", subtitleSource.get());
362        streamTypeMask |= LiveSession::STREAMTYPE_SUBTITLES;
363    }
364
365    msg->setInt32("streamTypeMask", streamTypeMask);
366    msg->setInt64("startTimeUs", startTimeUs);
367    msg->setInt64("segmentStartTimeUs", segmentStartTimeUs);
368    msg->setInt32("startDiscontinuitySeq", startDiscontinuitySeq);
369    msg->setInt32("adaptive", adaptive);
370    msg->post();
371}
372
373void PlaylistFetcher::pauseAsync() {
374    (new AMessage(kWhatPause, id()))->post();
375}
376
377void PlaylistFetcher::stopAsync(bool clear) {
378    sp<AMessage> msg = new AMessage(kWhatStop, id());
379    msg->setInt32("clear", clear);
380    msg->post();
381}
382
383void PlaylistFetcher::resumeUntilAsync(const sp<AMessage> &params) {
384    AMessage* msg = new AMessage(kWhatResumeUntil, id());
385    msg->setMessage("params", params);
386    msg->post();
387}
388
389void PlaylistFetcher::onMessageReceived(const sp<AMessage> &msg) {
390    switch (msg->what()) {
391        case kWhatStart:
392        {
393            status_t err = onStart(msg);
394
395            sp<AMessage> notify = mNotify->dup();
396            notify->setInt32("what", kWhatStarted);
397            notify->setInt32("err", err);
398            notify->post();
399            break;
400        }
401
402        case kWhatPause:
403        {
404            onPause();
405
406            sp<AMessage> notify = mNotify->dup();
407            notify->setInt32("what", kWhatPaused);
408            notify->post();
409            break;
410        }
411
412        case kWhatStop:
413        {
414            onStop(msg);
415
416            sp<AMessage> notify = mNotify->dup();
417            notify->setInt32("what", kWhatStopped);
418            notify->post();
419            break;
420        }
421
422        case kWhatMonitorQueue:
423        case kWhatDownloadNext:
424        {
425            int32_t generation;
426            CHECK(msg->findInt32("generation", &generation));
427
428            if (generation != mMonitorQueueGeneration) {
429                // Stale event
430                break;
431            }
432
433            if (msg->what() == kWhatMonitorQueue) {
434                onMonitorQueue();
435            } else {
436                onDownloadNext();
437            }
438            break;
439        }
440
441        case kWhatResumeUntil:
442        {
443            onResumeUntil(msg);
444            break;
445        }
446
447        default:
448            TRESPASS();
449    }
450}
451
452status_t PlaylistFetcher::onStart(const sp<AMessage> &msg) {
453    mPacketSources.clear();
454
455    uint32_t streamTypeMask;
456    CHECK(msg->findInt32("streamTypeMask", (int32_t *)&streamTypeMask));
457
458    int64_t startTimeUs;
459    int64_t segmentStartTimeUs;
460    int32_t startDiscontinuitySeq;
461    int32_t adaptive;
462    CHECK(msg->findInt64("startTimeUs", &startTimeUs));
463    CHECK(msg->findInt64("segmentStartTimeUs", &segmentStartTimeUs));
464    CHECK(msg->findInt32("startDiscontinuitySeq", &startDiscontinuitySeq));
465    CHECK(msg->findInt32("adaptive", &adaptive));
466
467    if (streamTypeMask & LiveSession::STREAMTYPE_AUDIO) {
468        void *ptr;
469        CHECK(msg->findPointer("audioSource", &ptr));
470
471        mPacketSources.add(
472                LiveSession::STREAMTYPE_AUDIO,
473                static_cast<AnotherPacketSource *>(ptr));
474    }
475
476    if (streamTypeMask & LiveSession::STREAMTYPE_VIDEO) {
477        void *ptr;
478        CHECK(msg->findPointer("videoSource", &ptr));
479
480        mPacketSources.add(
481                LiveSession::STREAMTYPE_VIDEO,
482                static_cast<AnotherPacketSource *>(ptr));
483    }
484
485    if (streamTypeMask & LiveSession::STREAMTYPE_SUBTITLES) {
486        void *ptr;
487        CHECK(msg->findPointer("subtitleSource", &ptr));
488
489        mPacketSources.add(
490                LiveSession::STREAMTYPE_SUBTITLES,
491                static_cast<AnotherPacketSource *>(ptr));
492    }
493
494    mStreamTypeMask = streamTypeMask;
495
496    mSegmentStartTimeUs = segmentStartTimeUs;
497    mDiscontinuitySeq = startDiscontinuitySeq;
498
499    if (startTimeUs >= 0) {
500        mStartTimeUs = startTimeUs;
501        mSeqNumber = -1;
502        mStartup = true;
503        mPrepared = false;
504        mAdaptive = adaptive;
505    }
506
507    postMonitorQueue();
508
509    return OK;
510}
511
512void PlaylistFetcher::onPause() {
513    cancelMonitorQueue();
514}
515
516void PlaylistFetcher::onStop(const sp<AMessage> &msg) {
517    cancelMonitorQueue();
518
519    int32_t clear;
520    CHECK(msg->findInt32("clear", &clear));
521    if (clear) {
522        for (size_t i = 0; i < mPacketSources.size(); i++) {
523            sp<AnotherPacketSource> packetSource = mPacketSources.valueAt(i);
524            packetSource->clear();
525        }
526    }
527
528    mPacketSources.clear();
529    mStreamTypeMask = 0;
530}
531
532// Resume until we have reached the boundary timestamps listed in `msg`; when
533// the remaining time is too short (within a resume threshold) stop immediately
534// instead.
535status_t PlaylistFetcher::onResumeUntil(const sp<AMessage> &msg) {
536    sp<AMessage> params;
537    CHECK(msg->findMessage("params", &params));
538
539    bool stop = false;
540    for (size_t i = 0; i < mPacketSources.size(); i++) {
541        sp<AnotherPacketSource> packetSource = mPacketSources.valueAt(i);
542
543        const char *stopKey;
544        int streamType = mPacketSources.keyAt(i);
545        switch (streamType) {
546        case LiveSession::STREAMTYPE_VIDEO:
547            stopKey = "timeUsVideo";
548            break;
549
550        case LiveSession::STREAMTYPE_AUDIO:
551            stopKey = "timeUsAudio";
552            break;
553
554        case LiveSession::STREAMTYPE_SUBTITLES:
555            stopKey = "timeUsSubtitle";
556            break;
557
558        default:
559            TRESPASS();
560        }
561
562        // Don't resume if we would stop within a resume threshold.
563        int32_t discontinuitySeq;
564        int64_t latestTimeUs = 0, stopTimeUs = 0;
565        sp<AMessage> latestMeta = packetSource->getLatestEnqueuedMeta();
566        if (latestMeta != NULL
567                && latestMeta->findInt32("discontinuitySeq", &discontinuitySeq)
568                && discontinuitySeq == mDiscontinuitySeq
569                && latestMeta->findInt64("timeUs", &latestTimeUs)
570                && params->findInt64(stopKey, &stopTimeUs)
571                && stopTimeUs - latestTimeUs < resumeThreshold(latestMeta)) {
572            stop = true;
573        }
574    }
575
576    if (stop) {
577        for (size_t i = 0; i < mPacketSources.size(); i++) {
578            mPacketSources.valueAt(i)->queueAccessUnit(mSession->createFormatChangeBuffer());
579        }
580        stopAsync(/* clear = */ false);
581        return OK;
582    }
583
584    mStopParams = params;
585    postMonitorQueue();
586
587    return OK;
588}
589
590void PlaylistFetcher::notifyError(status_t err) {
591    sp<AMessage> notify = mNotify->dup();
592    notify->setInt32("what", kWhatError);
593    notify->setInt32("err", err);
594    notify->post();
595}
596
597void PlaylistFetcher::queueDiscontinuity(
598        ATSParser::DiscontinuityType type, const sp<AMessage> &extra) {
599    for (size_t i = 0; i < mPacketSources.size(); ++i) {
600        // do not discard buffer upon #EXT-X-DISCONTINUITY tag
601        // (seek will discard buffer by abandoning old fetchers)
602        mPacketSources.valueAt(i)->queueDiscontinuity(
603                type, extra, false /* discard */);
604    }
605}
606
607void PlaylistFetcher::onMonitorQueue() {
608    bool downloadMore = false;
609    refreshPlaylist();
610
611    int32_t targetDurationSecs;
612    int64_t targetDurationUs = kMinBufferedDurationUs;
613    if (mPlaylist != NULL) {
614        if (mPlaylist->meta() == NULL || !mPlaylist->meta()->findInt32(
615                "target-duration", &targetDurationSecs)) {
616            ALOGE("Playlist is missing required EXT-X-TARGETDURATION tag");
617            notifyError(ERROR_MALFORMED);
618            return;
619        }
620        targetDurationUs = targetDurationSecs * 1000000ll;
621    }
622
623    // buffer at least 3 times the target duration, or up to 10 seconds
624    int64_t durationToBufferUs = targetDurationUs * 3;
625    if (durationToBufferUs > kMinBufferedDurationUs)  {
626        durationToBufferUs = kMinBufferedDurationUs;
627    }
628
629    int64_t bufferedDurationUs = 0ll;
630    status_t finalResult = NOT_ENOUGH_DATA;
631    if (mStreamTypeMask == LiveSession::STREAMTYPE_SUBTITLES) {
632        sp<AnotherPacketSource> packetSource =
633            mPacketSources.valueFor(LiveSession::STREAMTYPE_SUBTITLES);
634
635        bufferedDurationUs =
636                packetSource->getBufferedDurationUs(&finalResult);
637        finalResult = OK;
638    } else {
639        // Use max stream duration to prevent us from waiting on a non-existent stream;
640        // when we cannot make out from the manifest what streams are included in a playlist
641        // we might assume extra streams.
642        for (size_t i = 0; i < mPacketSources.size(); ++i) {
643            if ((mStreamTypeMask & mPacketSources.keyAt(i)) == 0) {
644                continue;
645            }
646
647            int64_t bufferedStreamDurationUs =
648                mPacketSources.valueAt(i)->getBufferedDurationUs(&finalResult);
649            ALOGV("buffered %" PRId64 " for stream %d",
650                    bufferedStreamDurationUs, mPacketSources.keyAt(i));
651            if (bufferedStreamDurationUs > bufferedDurationUs) {
652                bufferedDurationUs = bufferedStreamDurationUs;
653            }
654        }
655    }
656    downloadMore = (bufferedDurationUs < durationToBufferUs);
657
658    // signal start if buffered up at least the target size
659    if (!mPrepared && bufferedDurationUs > targetDurationUs && downloadMore) {
660        mPrepared = true;
661
662        ALOGV("prepared, buffered=%" PRId64 " > %" PRId64 "",
663                bufferedDurationUs, targetDurationUs);
664        sp<AMessage> msg = mNotify->dup();
665        msg->setInt32("what", kWhatTemporarilyDoneFetching);
666        msg->post();
667    }
668
669    if (finalResult == OK && downloadMore) {
670        ALOGV("monitoring, buffered=%" PRId64 " < %" PRId64 "",
671                bufferedDurationUs, durationToBufferUs);
672        // delay the next download slightly; hopefully this gives other concurrent fetchers
673        // a better chance to run.
674        // onDownloadNext();
675        sp<AMessage> msg = new AMessage(kWhatDownloadNext, id());
676        msg->setInt32("generation", mMonitorQueueGeneration);
677        msg->post(1000l);
678    } else {
679        // Nothing to do yet, try again in a second.
680
681        sp<AMessage> msg = mNotify->dup();
682        msg->setInt32("what", kWhatTemporarilyDoneFetching);
683        msg->post();
684
685        int64_t delayUs = mPrepared ? kMaxMonitorDelayUs : targetDurationUs / 2;
686        ALOGV("pausing for %" PRId64 ", buffered=%" PRId64 " > %" PRId64 "",
687                delayUs, bufferedDurationUs, durationToBufferUs);
688        // :TRICKY: need to enforce minimum delay because the delay to
689        // refresh the playlist will become 0
690        postMonitorQueue(delayUs, mPrepared ? targetDurationUs * 2 : 0);
691    }
692}
693
694status_t PlaylistFetcher::refreshPlaylist() {
695    if (delayUsToRefreshPlaylist() <= 0) {
696        bool unchanged;
697        sp<M3UParser> playlist = mSession->fetchPlaylist(
698                mURI.c_str(), mPlaylistHash, &unchanged);
699
700        if (playlist == NULL) {
701            if (unchanged) {
702                // We succeeded in fetching the playlist, but it was
703                // unchanged from the last time we tried.
704
705                if (mRefreshState != THIRD_UNCHANGED_RELOAD_ATTEMPT) {
706                    mRefreshState = (RefreshState)(mRefreshState + 1);
707                }
708            } else {
709                ALOGE("failed to load playlist at url '%s'", uriDebugString(mURI).c_str());
710                return ERROR_IO;
711            }
712        } else {
713            mRefreshState = INITIAL_MINIMUM_RELOAD_DELAY;
714            mPlaylist = playlist;
715
716            if (mPlaylist->isComplete() || mPlaylist->isEvent()) {
717                updateDuration();
718            }
719        }
720
721        mLastPlaylistFetchTimeUs = ALooper::GetNowUs();
722    }
723    return OK;
724}
725
726// static
727bool PlaylistFetcher::bufferStartsWithTsSyncByte(const sp<ABuffer>& buffer) {
728    return buffer->size() > 0 && buffer->data()[0] == 0x47;
729}
730
731void PlaylistFetcher::onDownloadNext() {
732    status_t err = refreshPlaylist();
733    int32_t firstSeqNumberInPlaylist = 0;
734    int32_t lastSeqNumberInPlaylist = 0;
735    bool discontinuity = false;
736
737    if (mPlaylist != NULL) {
738        if (mPlaylist->meta() != NULL) {
739            mPlaylist->meta()->findInt32("media-sequence", &firstSeqNumberInPlaylist);
740        }
741
742        lastSeqNumberInPlaylist =
743                firstSeqNumberInPlaylist + (int32_t)mPlaylist->size() - 1;
744
745        if (mDiscontinuitySeq < 0) {
746            mDiscontinuitySeq = mPlaylist->getDiscontinuitySeq();
747        }
748    }
749
750    if (mPlaylist != NULL && mSeqNumber < 0) {
751        CHECK_GE(mStartTimeUs, 0ll);
752
753        if (mSegmentStartTimeUs < 0) {
754            if (!mPlaylist->isComplete() && !mPlaylist->isEvent()) {
755                // If this is a live session, start 3 segments from the end on connect
756                mSeqNumber = lastSeqNumberInPlaylist - 3;
757                if (mSeqNumber < firstSeqNumberInPlaylist) {
758                    mSeqNumber = firstSeqNumberInPlaylist;
759                }
760            } else {
761                // When seeking mSegmentStartTimeUs is unavailable (< 0), we
762                // use mStartTimeUs (client supplied timestamp) to determine both start segment
763                // and relative position inside a segment
764                mSeqNumber = getSeqNumberForTime(mStartTimeUs);
765                mStartTimeUs -= getSegmentStartTimeUs(mSeqNumber);
766            }
767            mStartTimeUsRelative = true;
768            ALOGV("Initial sequence number for time %" PRId64 " is %d from (%d .. %d)",
769                    mStartTimeUs, mSeqNumber, firstSeqNumberInPlaylist,
770                    lastSeqNumberInPlaylist);
771        } else {
772            // When adapting or track switching, mSegmentStartTimeUs (relative
773            // to media time 0) is used to determine the start segment; mStartTimeUs (absolute
774            // timestamps coming from the media container) is used to determine the position
775            // inside a segments.
776            mSeqNumber = getSeqNumberForTime(mSegmentStartTimeUs);
777            if (mAdaptive) {
778                // avoid double fetch/decode
779                mSeqNumber += 1;
780            }
781            ssize_t minSeq = getSeqNumberForDiscontinuity(mDiscontinuitySeq);
782            if (mSeqNumber < minSeq) {
783                mSeqNumber = minSeq;
784            }
785
786            if (mSeqNumber < firstSeqNumberInPlaylist) {
787                mSeqNumber = firstSeqNumberInPlaylist;
788            }
789
790            if (mSeqNumber > lastSeqNumberInPlaylist) {
791                mSeqNumber = lastSeqNumberInPlaylist;
792            }
793            ALOGV("Initial sequence number for live event %d from (%d .. %d)",
794                    mSeqNumber, firstSeqNumberInPlaylist,
795                    lastSeqNumberInPlaylist);
796        }
797    }
798
799    // if mPlaylist is NULL then err must be non-OK; but the other way around might not be true
800    if (mSeqNumber < firstSeqNumberInPlaylist
801            || mSeqNumber > lastSeqNumberInPlaylist
802            || err != OK) {
803        if ((err != OK || !mPlaylist->isComplete()) && mNumRetries < kMaxNumRetries) {
804            ++mNumRetries;
805
806            if (mSeqNumber > lastSeqNumberInPlaylist || err != OK) {
807                // make sure we reach this retry logic on refresh failures
808                // by adding an err != OK clause to all enclosing if's.
809
810                // refresh in increasing fraction (1/2, 1/3, ...) of the
811                // playlist's target duration or 3 seconds, whichever is less
812                int64_t delayUs = kMaxMonitorDelayUs;
813                if (mPlaylist != NULL && mPlaylist->meta() != NULL) {
814                    int32_t targetDurationSecs;
815                    CHECK(mPlaylist->meta()->findInt32("target-duration", &targetDurationSecs));
816                    delayUs = mPlaylist->size() * targetDurationSecs *
817                            1000000ll / (1 + mNumRetries);
818                }
819                if (delayUs > kMaxMonitorDelayUs) {
820                    delayUs = kMaxMonitorDelayUs;
821                }
822                ALOGV("sequence number high: %d from (%d .. %d), "
823                      "monitor in %" PRId64 " (retry=%d)",
824                        mSeqNumber, firstSeqNumberInPlaylist,
825                        lastSeqNumberInPlaylist, delayUs, mNumRetries);
826                postMonitorQueue(delayUs);
827                return;
828            }
829
830            if (err != OK) {
831                notifyError(err);
832                return;
833            }
834
835            // we've missed the boat, let's start 3 segments prior to the latest sequence
836            // number available and signal a discontinuity.
837
838            ALOGI("We've missed the boat, restarting playback."
839                  "  mStartup=%d, was  looking for %d in %d-%d",
840                    mStartup, mSeqNumber, firstSeqNumberInPlaylist,
841                    lastSeqNumberInPlaylist);
842            if (mStopParams != NULL) {
843                // we should have kept on fetching until we hit the boundaries in mStopParams,
844                // but since the segments we are supposed to fetch have already rolled off
845                // the playlist, i.e. we have already missed the boat, we inevitably have to
846                // skip.
847                for (size_t i = 0; i < mPacketSources.size(); i++) {
848                    sp<ABuffer> formatChange = mSession->createFormatChangeBuffer();
849                    mPacketSources.valueAt(i)->queueAccessUnit(formatChange);
850                }
851                stopAsync(/* clear = */ false);
852                return;
853            }
854            mSeqNumber = lastSeqNumberInPlaylist - 3;
855            if (mSeqNumber < firstSeqNumberInPlaylist) {
856                mSeqNumber = firstSeqNumberInPlaylist;
857            }
858            discontinuity = true;
859
860            // fall through
861        } else {
862            ALOGE("Cannot find sequence number %d in playlist "
863                 "(contains %d - %d)",
864                 mSeqNumber, firstSeqNumberInPlaylist,
865                  firstSeqNumberInPlaylist + (int32_t)mPlaylist->size() - 1);
866
867            notifyError(ERROR_END_OF_STREAM);
868            return;
869        }
870    }
871
872    mNumRetries = 0;
873
874    AString uri;
875    sp<AMessage> itemMeta;
876    CHECK(mPlaylist->itemAt(
877                mSeqNumber - firstSeqNumberInPlaylist,
878                &uri,
879                &itemMeta));
880
881    int32_t val;
882    if (itemMeta->findInt32("discontinuity", &val) && val != 0) {
883        mDiscontinuitySeq++;
884        discontinuity = true;
885    }
886
887    int64_t range_offset, range_length;
888    if (!itemMeta->findInt64("range-offset", &range_offset)
889            || !itemMeta->findInt64("range-length", &range_length)) {
890        range_offset = 0;
891        range_length = -1;
892    }
893
894    ALOGV("fetching segment %d from (%d .. %d)",
895          mSeqNumber, firstSeqNumberInPlaylist, lastSeqNumberInPlaylist);
896
897    ALOGV("fetching '%s'", uri.c_str());
898
899    sp<DataSource> source;
900    sp<ABuffer> buffer, tsBuffer;
901    // decrypt a junk buffer to prefetch key; since a session uses only one http connection,
902    // this avoids interleaved connections to the key and segment file.
903    {
904        sp<ABuffer> junk = new ABuffer(16);
905        junk->setRange(0, 16);
906        status_t err = decryptBuffer(mSeqNumber - firstSeqNumberInPlaylist, junk,
907                true /* first */);
908        if (err != OK) {
909            notifyError(err);
910            return;
911        }
912    }
913
914    // block-wise download
915    bool startup = mStartup;
916    ssize_t bytesRead;
917    do {
918        bytesRead = mSession->fetchFile(
919                uri.c_str(), &buffer, range_offset, range_length, kDownloadBlockSize, &source);
920
921        if (bytesRead < 0) {
922            status_t err = bytesRead;
923            ALOGE("failed to fetch .ts segment at url '%s'", uri.c_str());
924            notifyError(err);
925            return;
926        }
927
928        CHECK(buffer != NULL);
929
930        size_t size = buffer->size();
931        // Set decryption range.
932        buffer->setRange(size - bytesRead, bytesRead);
933        status_t err = decryptBuffer(mSeqNumber - firstSeqNumberInPlaylist, buffer,
934                buffer->offset() == 0 /* first */);
935        // Unset decryption range.
936        buffer->setRange(0, size);
937
938        if (err != OK) {
939            ALOGE("decryptBuffer failed w/ error %d", err);
940
941            notifyError(err);
942            return;
943        }
944
945        if (startup || discontinuity) {
946            // Signal discontinuity.
947
948            if (mPlaylist->isComplete() || mPlaylist->isEvent()) {
949                // If this was a live event this made no sense since
950                // we don't have access to all the segment before the current
951                // one.
952                mNextPTSTimeUs = getSegmentStartTimeUs(mSeqNumber);
953            }
954
955            if (discontinuity) {
956                ALOGI("queueing discontinuity (explicit=%d)", discontinuity);
957
958                queueDiscontinuity(
959                        ATSParser::DISCONTINUITY_FORMATCHANGE,
960                        NULL /* extra */);
961
962                discontinuity = false;
963            }
964
965            startup = false;
966        }
967
968        err = OK;
969        if (bufferStartsWithTsSyncByte(buffer)) {
970            // Incremental extraction is only supported for MPEG2 transport streams.
971            if (tsBuffer == NULL) {
972                tsBuffer = new ABuffer(buffer->data(), buffer->capacity());
973                tsBuffer->setRange(0, 0);
974            } else if (tsBuffer->capacity() != buffer->capacity()) {
975                size_t tsOff = tsBuffer->offset(), tsSize = tsBuffer->size();
976                tsBuffer = new ABuffer(buffer->data(), buffer->capacity());
977                tsBuffer->setRange(tsOff, tsSize);
978            }
979            tsBuffer->setRange(tsBuffer->offset(), tsBuffer->size() + bytesRead);
980
981            err = extractAndQueueAccessUnitsFromTs(tsBuffer);
982        }
983
984        if (err == -EAGAIN) {
985            // starting sequence number too low/high
986            mTSParser.clear();
987            postMonitorQueue();
988            return;
989        } else if (err == ERROR_OUT_OF_RANGE) {
990            // reached stopping point
991            stopAsync(/* clear = */ false);
992            return;
993        } else if (err != OK) {
994            notifyError(err);
995            return;
996        }
997
998    } while (bytesRead != 0);
999
1000    if (bufferStartsWithTsSyncByte(buffer)) {
1001        // If we don't see a stream in the program table after fetching a full ts segment
1002        // mark it as nonexistent.
1003        const size_t kNumTypes = ATSParser::NUM_SOURCE_TYPES;
1004        ATSParser::SourceType srcTypes[kNumTypes] =
1005                { ATSParser::VIDEO, ATSParser::AUDIO };
1006        LiveSession::StreamType streamTypes[kNumTypes] =
1007                { LiveSession::STREAMTYPE_VIDEO, LiveSession::STREAMTYPE_AUDIO };
1008
1009        for (size_t i = 0; i < kNumTypes; i++) {
1010            ATSParser::SourceType srcType = srcTypes[i];
1011            LiveSession::StreamType streamType = streamTypes[i];
1012
1013            sp<AnotherPacketSource> source =
1014                static_cast<AnotherPacketSource *>(
1015                    mTSParser->getSource(srcType).get());
1016
1017            if (!mTSParser->hasSource(srcType)) {
1018                ALOGW("MPEG2 Transport stream does not contain %s data.",
1019                      srcType == ATSParser::VIDEO ? "video" : "audio");
1020
1021                mStreamTypeMask &= ~streamType;
1022                mPacketSources.removeItem(streamType);
1023            }
1024        }
1025
1026    }
1027
1028    if (checkDecryptPadding(buffer) != OK) {
1029        ALOGE("Incorrect padding bytes after decryption.");
1030        notifyError(ERROR_MALFORMED);
1031        return;
1032    }
1033
1034    err = OK;
1035    if (tsBuffer != NULL) {
1036        AString method;
1037        CHECK(buffer->meta()->findString("cipher-method", &method));
1038        if ((tsBuffer->size() > 0 && method == "NONE")
1039                || tsBuffer->size() > 16) {
1040            ALOGE("MPEG2 transport stream is not an even multiple of 188 "
1041                    "bytes in length.");
1042            notifyError(ERROR_MALFORMED);
1043            return;
1044        }
1045    }
1046
1047    // bulk extract non-ts files
1048    if (tsBuffer == NULL) {
1049        err = extractAndQueueAccessUnits(buffer, itemMeta);
1050        if (err == -EAGAIN) {
1051            // starting sequence number too low/high
1052            postMonitorQueue();
1053            return;
1054        } else if (err == ERROR_OUT_OF_RANGE) {
1055            // reached stopping point
1056            stopAsync(/* clear = */false);
1057            return;
1058        }
1059    }
1060
1061    if (err != OK) {
1062        notifyError(err);
1063        return;
1064    }
1065
1066    ++mSeqNumber;
1067
1068    postMonitorQueue();
1069}
1070
1071int32_t PlaylistFetcher::getSeqNumberWithAnchorTime(int64_t anchorTimeUs) const {
1072    int32_t firstSeqNumberInPlaylist, lastSeqNumberInPlaylist;
1073    if (mPlaylist->meta() == NULL
1074            || !mPlaylist->meta()->findInt32("media-sequence", &firstSeqNumberInPlaylist)) {
1075        firstSeqNumberInPlaylist = 0;
1076    }
1077    lastSeqNumberInPlaylist = firstSeqNumberInPlaylist + mPlaylist->size() - 1;
1078
1079    int32_t index = mSeqNumber - firstSeqNumberInPlaylist - 1;
1080    while (index >= 0 && anchorTimeUs > mStartTimeUs) {
1081        sp<AMessage> itemMeta;
1082        CHECK(mPlaylist->itemAt(index, NULL /* uri */, &itemMeta));
1083
1084        int64_t itemDurationUs;
1085        CHECK(itemMeta->findInt64("durationUs", &itemDurationUs));
1086
1087        anchorTimeUs -= itemDurationUs;
1088        --index;
1089    }
1090
1091    int32_t newSeqNumber = firstSeqNumberInPlaylist + index + 1;
1092    if (newSeqNumber <= lastSeqNumberInPlaylist) {
1093        return newSeqNumber;
1094    } else {
1095        return lastSeqNumberInPlaylist;
1096    }
1097}
1098
1099int32_t PlaylistFetcher::getSeqNumberForDiscontinuity(size_t discontinuitySeq) const {
1100    int32_t firstSeqNumberInPlaylist;
1101    if (mPlaylist->meta() == NULL
1102            || !mPlaylist->meta()->findInt32("media-sequence", &firstSeqNumberInPlaylist)) {
1103        firstSeqNumberInPlaylist = 0;
1104    }
1105
1106    size_t curDiscontinuitySeq = mPlaylist->getDiscontinuitySeq();
1107    if (discontinuitySeq < curDiscontinuitySeq) {
1108        return firstSeqNumberInPlaylist <= 0 ? 0 : (firstSeqNumberInPlaylist - 1);
1109    }
1110
1111    size_t index = 0;
1112    while (index < mPlaylist->size()) {
1113        sp<AMessage> itemMeta;
1114        CHECK(mPlaylist->itemAt( index, NULL /* uri */, &itemMeta));
1115
1116        int64_t discontinuity;
1117        if (itemMeta->findInt64("discontinuity", &discontinuity)) {
1118            curDiscontinuitySeq++;
1119        }
1120
1121        if (curDiscontinuitySeq == discontinuitySeq) {
1122            return firstSeqNumberInPlaylist + index;
1123        }
1124
1125        ++index;
1126    }
1127
1128    return firstSeqNumberInPlaylist + mPlaylist->size();
1129}
1130
1131int32_t PlaylistFetcher::getSeqNumberForTime(int64_t timeUs) const {
1132    int32_t firstSeqNumberInPlaylist;
1133    if (mPlaylist->meta() == NULL || !mPlaylist->meta()->findInt32(
1134                "media-sequence", &firstSeqNumberInPlaylist)) {
1135        firstSeqNumberInPlaylist = 0;
1136    }
1137
1138    size_t index = 0;
1139    int64_t segmentStartUs = 0;
1140    while (index < mPlaylist->size()) {
1141        sp<AMessage> itemMeta;
1142        CHECK(mPlaylist->itemAt(
1143                    index, NULL /* uri */, &itemMeta));
1144
1145        int64_t itemDurationUs;
1146        CHECK(itemMeta->findInt64("durationUs", &itemDurationUs));
1147
1148        if (timeUs < segmentStartUs + itemDurationUs) {
1149            break;
1150        }
1151
1152        segmentStartUs += itemDurationUs;
1153        ++index;
1154    }
1155
1156    if (index >= mPlaylist->size()) {
1157        index = mPlaylist->size() - 1;
1158    }
1159
1160    return firstSeqNumberInPlaylist + index;
1161}
1162
1163const sp<ABuffer> &PlaylistFetcher::setAccessUnitProperties(
1164        const sp<ABuffer> &accessUnit, const sp<AnotherPacketSource> &source, bool discard) {
1165    sp<MetaData> format = source->getFormat();
1166    if (format != NULL) {
1167        // for simplicity, store a reference to the format in each unit
1168        accessUnit->meta()->setObject("format", format);
1169    }
1170
1171    if (discard) {
1172        accessUnit->meta()->setInt32("discard", discard);
1173    }
1174
1175    int32_t targetDurationSecs;
1176    if (mPlaylist->meta()->findInt32("target-duration", &targetDurationSecs)) {
1177        accessUnit->meta()->setInt32("targetDuration", targetDurationSecs);
1178    }
1179
1180    accessUnit->meta()->setInt32("discontinuitySeq", mDiscontinuitySeq);
1181    accessUnit->meta()->setInt64("segmentStartTimeUs", getSegmentStartTimeUs(mSeqNumber));
1182    return accessUnit;
1183}
1184
1185status_t PlaylistFetcher::extractAndQueueAccessUnitsFromTs(const sp<ABuffer> &buffer) {
1186    if (mTSParser == NULL) {
1187        // Use TS_TIMESTAMPS_ARE_ABSOLUTE so pts carry over between fetchers.
1188        mTSParser = new ATSParser(ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE);
1189    }
1190
1191    if (mNextPTSTimeUs >= 0ll) {
1192        sp<AMessage> extra = new AMessage;
1193        // Since we are using absolute timestamps, signal an offset of 0 to prevent
1194        // ATSParser from skewing the timestamps of access units.
1195        extra->setInt64(IStreamListener::kKeyMediaTimeUs, 0);
1196
1197        mTSParser->signalDiscontinuity(
1198                ATSParser::DISCONTINUITY_TIME, extra);
1199
1200        mAbsoluteTimeAnchorUs = mNextPTSTimeUs;
1201        mNextPTSTimeUs = -1ll;
1202        mFirstPTSValid = false;
1203    }
1204
1205    size_t offset = 0;
1206    while (offset + 188 <= buffer->size()) {
1207        status_t err = mTSParser->feedTSPacket(buffer->data() + offset, 188);
1208
1209        if (err != OK) {
1210            return err;
1211        }
1212
1213        offset += 188;
1214    }
1215    // setRange to indicate consumed bytes.
1216    buffer->setRange(buffer->offset() + offset, buffer->size() - offset);
1217
1218    status_t err = OK;
1219    for (size_t i = mPacketSources.size(); i-- > 0;) {
1220        sp<AnotherPacketSource> packetSource = mPacketSources.valueAt(i);
1221
1222        const char *key;
1223        ATSParser::SourceType type;
1224        const LiveSession::StreamType stream = mPacketSources.keyAt(i);
1225        switch (stream) {
1226            case LiveSession::STREAMTYPE_VIDEO:
1227                type = ATSParser::VIDEO;
1228                key = "timeUsVideo";
1229                break;
1230
1231            case LiveSession::STREAMTYPE_AUDIO:
1232                type = ATSParser::AUDIO;
1233                key = "timeUsAudio";
1234                break;
1235
1236            case LiveSession::STREAMTYPE_SUBTITLES:
1237            {
1238                ALOGE("MPEG2 Transport streams do not contain subtitles.");
1239                return ERROR_MALFORMED;
1240                break;
1241            }
1242
1243            default:
1244                TRESPASS();
1245        }
1246
1247        sp<AnotherPacketSource> source =
1248            static_cast<AnotherPacketSource *>(
1249                    mTSParser->getSource(type).get());
1250
1251        if (source == NULL) {
1252            continue;
1253        }
1254
1255        int64_t timeUs;
1256        sp<ABuffer> accessUnit;
1257        status_t finalResult;
1258        while (source->hasBufferAvailable(&finalResult)
1259                && source->dequeueAccessUnit(&accessUnit) == OK) {
1260
1261            CHECK(accessUnit->meta()->findInt64("timeUs", &timeUs));
1262
1263            if (mStartup) {
1264                if (!mFirstPTSValid) {
1265                    mFirstTimeUs = timeUs;
1266                    mFirstPTSValid = true;
1267                }
1268                if (mStartTimeUsRelative) {
1269                    timeUs -= mFirstTimeUs;
1270                    if (timeUs < 0) {
1271                        timeUs = 0;
1272                    }
1273                }
1274
1275                if (timeUs < mStartTimeUs) {
1276                    // buffer up to the closest preceding IDR frame
1277                    ALOGV("timeUs %" PRId64 " us < mStartTimeUs %" PRId64 " us",
1278                            timeUs, mStartTimeUs);
1279                    const char *mime;
1280                    sp<MetaData> format  = source->getFormat();
1281                    bool isAvc = false;
1282                    if (format != NULL && format->findCString(kKeyMIMEType, &mime)
1283                            && !strcasecmp(mime, MEDIA_MIMETYPE_VIDEO_AVC)) {
1284                        isAvc = true;
1285                    }
1286                    if (isAvc && IsIDR(accessUnit)) {
1287                        mVideoBuffer->clear();
1288                    }
1289                    if (isAvc) {
1290                        mVideoBuffer->queueAccessUnit(accessUnit);
1291                    }
1292
1293                    continue;
1294                }
1295            }
1296
1297            CHECK(accessUnit->meta()->findInt64("timeUs", &timeUs));
1298            if (mStartTimeUsNotify != NULL && timeUs > mStartTimeUs) {
1299                int32_t firstSeqNumberInPlaylist;
1300                if (mPlaylist->meta() == NULL || !mPlaylist->meta()->findInt32(
1301                            "media-sequence", &firstSeqNumberInPlaylist)) {
1302                    firstSeqNumberInPlaylist = 0;
1303                }
1304
1305                int32_t targetDurationSecs;
1306                CHECK(mPlaylist->meta()->findInt32("target-duration", &targetDurationSecs));
1307                int64_t targetDurationUs = targetDurationSecs * 1000000ll;
1308                // mStartup
1309                //   mStartup is true until we have queued a packet for all the streams
1310                //   we are fetching. We queue packets whose timestamps are greater than
1311                //   mStartTimeUs.
1312                // mSegmentStartTimeUs >= 0
1313                //   mSegmentStartTimeUs is non-negative when adapting or switching tracks
1314                // mSeqNumber > firstSeqNumberInPlaylist
1315                //   don't decrement mSeqNumber if it already points to the 1st segment
1316                // timeUs - mStartTimeUs > targetDurationUs:
1317                //   This and the 2 above conditions should only happen when adapting in a live
1318                //   stream; the old fetcher has already fetched to mStartTimeUs; the new fetcher
1319                //   would start fetching after timeUs, which should be greater than mStartTimeUs;
1320                //   the old fetcher would then continue fetching data until timeUs. We don't want
1321                //   timeUs to be too far ahead of mStartTimeUs because we want the old fetcher to
1322                //   stop as early as possible. The definition of being "too far ahead" is
1323                //   arbitrary; here we use targetDurationUs as threshold.
1324                if (mStartup && mSegmentStartTimeUs >= 0
1325                        && mSeqNumber > firstSeqNumberInPlaylist
1326                        && timeUs - mStartTimeUs > targetDurationUs) {
1327                    // we just guessed a starting timestamp that is too high when adapting in a
1328                    // live stream; re-adjust based on the actual timestamp extracted from the
1329                    // media segment; if we didn't move backward after the re-adjustment
1330                    // (newSeqNumber), start at least 1 segment prior.
1331                    int32_t newSeqNumber = getSeqNumberWithAnchorTime(timeUs);
1332                    if (newSeqNumber >= mSeqNumber) {
1333                        --mSeqNumber;
1334                    } else {
1335                        mSeqNumber = newSeqNumber;
1336                    }
1337                    mStartTimeUsNotify = mNotify->dup();
1338                    mStartTimeUsNotify->setInt32("what", kWhatStartedAt);
1339                    return -EAGAIN;
1340                }
1341
1342                int32_t seq;
1343                if (!mStartTimeUsNotify->findInt32("discontinuitySeq", &seq)) {
1344                    mStartTimeUsNotify->setInt32("discontinuitySeq", mDiscontinuitySeq);
1345                }
1346                int64_t startTimeUs;
1347                if (!mStartTimeUsNotify->findInt64(key, &startTimeUs)) {
1348                    mStartTimeUsNotify->setInt64(key, timeUs);
1349
1350                    uint32_t streamMask = 0;
1351                    mStartTimeUsNotify->findInt32("streamMask", (int32_t *) &streamMask);
1352                    streamMask |= mPacketSources.keyAt(i);
1353                    mStartTimeUsNotify->setInt32("streamMask", streamMask);
1354
1355                    if (streamMask == mStreamTypeMask) {
1356                        mStartup = false;
1357                        mStartTimeUsNotify->post();
1358                        mStartTimeUsNotify.clear();
1359                    }
1360                }
1361            }
1362
1363            if (mStopParams != NULL) {
1364                // Queue discontinuity in original stream.
1365                int32_t discontinuitySeq;
1366                int64_t stopTimeUs;
1367                if (!mStopParams->findInt32("discontinuitySeq", &discontinuitySeq)
1368                        || discontinuitySeq > mDiscontinuitySeq
1369                        || !mStopParams->findInt64(key, &stopTimeUs)
1370                        || (discontinuitySeq == mDiscontinuitySeq
1371                                && timeUs >= stopTimeUs)) {
1372                    packetSource->queueAccessUnit(mSession->createFormatChangeBuffer());
1373                    mStreamTypeMask &= ~stream;
1374                    mPacketSources.removeItemsAt(i);
1375                    break;
1376                }
1377            }
1378
1379            // Note that we do NOT dequeue any discontinuities except for format change.
1380            if (stream == LiveSession::STREAMTYPE_VIDEO) {
1381                const bool discard = true;
1382                status_t status;
1383                while (mVideoBuffer->hasBufferAvailable(&status)) {
1384                    sp<ABuffer> videoBuffer;
1385                    mVideoBuffer->dequeueAccessUnit(&videoBuffer);
1386                    setAccessUnitProperties(videoBuffer, source, discard);
1387                    packetSource->queueAccessUnit(videoBuffer);
1388                }
1389            }
1390
1391            setAccessUnitProperties(accessUnit, source);
1392            packetSource->queueAccessUnit(accessUnit);
1393        }
1394
1395        if (err != OK) {
1396            break;
1397        }
1398    }
1399
1400    if (err != OK) {
1401        for (size_t i = mPacketSources.size(); i-- > 0;) {
1402            sp<AnotherPacketSource> packetSource = mPacketSources.valueAt(i);
1403            packetSource->clear();
1404        }
1405        return err;
1406    }
1407
1408    if (!mStreamTypeMask) {
1409        // Signal gap is filled between original and new stream.
1410        ALOGV("ERROR OUT OF RANGE");
1411        return ERROR_OUT_OF_RANGE;
1412    }
1413
1414    return OK;
1415}
1416
1417/* static */
1418bool PlaylistFetcher::bufferStartsWithWebVTTMagicSequence(
1419        const sp<ABuffer> &buffer) {
1420    size_t pos = 0;
1421
1422    // skip possible BOM
1423    if (buffer->size() >= pos + 3 &&
1424            !memcmp("\xef\xbb\xbf", buffer->data() + pos, 3)) {
1425        pos += 3;
1426    }
1427
1428    // accept WEBVTT followed by SPACE, TAB or (CR) LF
1429    if (buffer->size() < pos + 6 ||
1430            memcmp("WEBVTT", buffer->data() + pos, 6)) {
1431        return false;
1432    }
1433    pos += 6;
1434
1435    if (buffer->size() == pos) {
1436        return true;
1437    }
1438
1439    uint8_t sep = buffer->data()[pos];
1440    return sep == ' ' || sep == '\t' || sep == '\n' || sep == '\r';
1441}
1442
1443status_t PlaylistFetcher::extractAndQueueAccessUnits(
1444        const sp<ABuffer> &buffer, const sp<AMessage> &itemMeta) {
1445    if (bufferStartsWithWebVTTMagicSequence(buffer)) {
1446        if (mStreamTypeMask != LiveSession::STREAMTYPE_SUBTITLES) {
1447            ALOGE("This stream only contains subtitles.");
1448            return ERROR_MALFORMED;
1449        }
1450
1451        const sp<AnotherPacketSource> packetSource =
1452            mPacketSources.valueFor(LiveSession::STREAMTYPE_SUBTITLES);
1453
1454        int64_t durationUs;
1455        CHECK(itemMeta->findInt64("durationUs", &durationUs));
1456        buffer->meta()->setInt64("timeUs", getSegmentStartTimeUs(mSeqNumber));
1457        buffer->meta()->setInt64("durationUs", durationUs);
1458        buffer->meta()->setInt64("segmentStartTimeUs", getSegmentStartTimeUs(mSeqNumber));
1459        buffer->meta()->setInt32("discontinuitySeq", mDiscontinuitySeq);
1460        buffer->meta()->setInt32("subtitleGeneration", mSubtitleGeneration);
1461
1462        packetSource->queueAccessUnit(buffer);
1463        return OK;
1464    }
1465
1466    if (mNextPTSTimeUs >= 0ll) {
1467        mFirstPTSValid = false;
1468        mAbsoluteTimeAnchorUs = mNextPTSTimeUs;
1469        mNextPTSTimeUs = -1ll;
1470    }
1471
1472    // This better be an ISO 13818-7 (AAC) or ISO 13818-1 (MPEG) audio
1473    // stream prefixed by an ID3 tag.
1474
1475    bool firstID3Tag = true;
1476    uint64_t PTS = 0;
1477
1478    for (;;) {
1479        // Make sure to skip all ID3 tags preceding the audio data.
1480        // At least one must be present to provide the PTS timestamp.
1481
1482        ID3 id3(buffer->data(), buffer->size(), true /* ignoreV1 */);
1483        if (!id3.isValid()) {
1484            if (firstID3Tag) {
1485                ALOGE("Unable to parse ID3 tag.");
1486                return ERROR_MALFORMED;
1487            } else {
1488                break;
1489            }
1490        }
1491
1492        if (firstID3Tag) {
1493            bool found = false;
1494
1495            ID3::Iterator it(id3, "PRIV");
1496            while (!it.done()) {
1497                size_t length;
1498                const uint8_t *data = it.getData(&length);
1499
1500                static const char *kMatchName =
1501                    "com.apple.streaming.transportStreamTimestamp";
1502                static const size_t kMatchNameLen = strlen(kMatchName);
1503
1504                if (length == kMatchNameLen + 1 + 8
1505                        && !strncmp((const char *)data, kMatchName, kMatchNameLen)) {
1506                    found = true;
1507                    PTS = U64_AT(&data[kMatchNameLen + 1]);
1508                }
1509
1510                it.next();
1511            }
1512
1513            if (!found) {
1514                ALOGE("Unable to extract transportStreamTimestamp from ID3 tag.");
1515                return ERROR_MALFORMED;
1516            }
1517        }
1518
1519        // skip the ID3 tag
1520        buffer->setRange(
1521                buffer->offset() + id3.rawSize(), buffer->size() - id3.rawSize());
1522
1523        firstID3Tag = false;
1524    }
1525
1526    if (mStreamTypeMask != LiveSession::STREAMTYPE_AUDIO) {
1527        ALOGW("This stream only contains audio data!");
1528
1529        mStreamTypeMask &= LiveSession::STREAMTYPE_AUDIO;
1530
1531        if (mStreamTypeMask == 0) {
1532            return OK;
1533        }
1534    }
1535
1536    sp<AnotherPacketSource> packetSource =
1537        mPacketSources.valueFor(LiveSession::STREAMTYPE_AUDIO);
1538
1539    if (packetSource->getFormat() == NULL && buffer->size() >= 7) {
1540        ABitReader bits(buffer->data(), buffer->size());
1541
1542        // adts_fixed_header
1543
1544        CHECK_EQ(bits.getBits(12), 0xfffu);
1545        bits.skipBits(3);  // ID, layer
1546        bool protection_absent = bits.getBits(1) != 0;
1547
1548        unsigned profile = bits.getBits(2);
1549        CHECK_NE(profile, 3u);
1550        unsigned sampling_freq_index = bits.getBits(4);
1551        bits.getBits(1);  // private_bit
1552        unsigned channel_configuration = bits.getBits(3);
1553        CHECK_NE(channel_configuration, 0u);
1554        bits.skipBits(2);  // original_copy, home
1555
1556        sp<MetaData> meta = MakeAACCodecSpecificData(
1557                profile, sampling_freq_index, channel_configuration);
1558
1559        meta->setInt32(kKeyIsADTS, true);
1560
1561        packetSource->setFormat(meta);
1562    }
1563
1564    int64_t numSamples = 0ll;
1565    int32_t sampleRate;
1566    CHECK(packetSource->getFormat()->findInt32(kKeySampleRate, &sampleRate));
1567
1568    int64_t timeUs = (PTS * 100ll) / 9ll;
1569    if (!mFirstPTSValid) {
1570        mFirstPTSValid = true;
1571        mFirstTimeUs = timeUs;
1572    }
1573
1574    size_t offset = 0;
1575    while (offset < buffer->size()) {
1576        const uint8_t *adtsHeader = buffer->data() + offset;
1577        CHECK_LT(offset + 5, buffer->size());
1578
1579        unsigned aac_frame_length =
1580            ((adtsHeader[3] & 3) << 11)
1581            | (adtsHeader[4] << 3)
1582            | (adtsHeader[5] >> 5);
1583
1584        if (aac_frame_length == 0) {
1585            const uint8_t *id3Header = adtsHeader;
1586            if (!memcmp(id3Header, "ID3", 3)) {
1587                ID3 id3(id3Header, buffer->size() - offset, true);
1588                if (id3.isValid()) {
1589                    offset += id3.rawSize();
1590                    continue;
1591                };
1592            }
1593            return ERROR_MALFORMED;
1594        }
1595
1596        CHECK_LE(offset + aac_frame_length, buffer->size());
1597
1598        int64_t unitTimeUs = timeUs + numSamples * 1000000ll / sampleRate;
1599        offset += aac_frame_length;
1600
1601        // Each AAC frame encodes 1024 samples.
1602        numSamples += 1024;
1603
1604        if (mStartup) {
1605            int64_t startTimeUs = unitTimeUs;
1606            if (mStartTimeUsRelative) {
1607                startTimeUs -= mFirstTimeUs;
1608                if (startTimeUs  < 0) {
1609                    startTimeUs = 0;
1610                }
1611            }
1612            if (startTimeUs < mStartTimeUs) {
1613                continue;
1614            }
1615
1616            if (mStartTimeUsNotify != NULL) {
1617                int32_t targetDurationSecs;
1618                CHECK(mPlaylist->meta()->findInt32("target-duration", &targetDurationSecs));
1619                int64_t targetDurationUs = targetDurationSecs * 1000000ll;
1620
1621                // Duplicated logic from how we handle .ts playlists.
1622                if (mStartup && mSegmentStartTimeUs >= 0
1623                        && timeUs - mStartTimeUs > targetDurationUs) {
1624                    int32_t newSeqNumber = getSeqNumberWithAnchorTime(timeUs);
1625                    if (newSeqNumber >= mSeqNumber) {
1626                        --mSeqNumber;
1627                    } else {
1628                        mSeqNumber = newSeqNumber;
1629                    }
1630                    return -EAGAIN;
1631                }
1632
1633                mStartTimeUsNotify->setInt64("timeUsAudio", timeUs);
1634                mStartTimeUsNotify->setInt32("discontinuitySeq", mDiscontinuitySeq);
1635                mStartTimeUsNotify->setInt32("streamMask", LiveSession::STREAMTYPE_AUDIO);
1636                mStartTimeUsNotify->post();
1637                mStartTimeUsNotify.clear();
1638                mStartup = false;
1639            }
1640        }
1641
1642        if (mStopParams != NULL) {
1643            // Queue discontinuity in original stream.
1644            int32_t discontinuitySeq;
1645            int64_t stopTimeUs;
1646            if (!mStopParams->findInt32("discontinuitySeq", &discontinuitySeq)
1647                    || discontinuitySeq > mDiscontinuitySeq
1648                    || !mStopParams->findInt64("timeUsAudio", &stopTimeUs)
1649                    || (discontinuitySeq == mDiscontinuitySeq && unitTimeUs >= stopTimeUs)) {
1650                packetSource->queueAccessUnit(mSession->createFormatChangeBuffer());
1651                mStreamTypeMask = 0;
1652                mPacketSources.clear();
1653                return ERROR_OUT_OF_RANGE;
1654            }
1655        }
1656
1657        sp<ABuffer> unit = new ABuffer(aac_frame_length);
1658        memcpy(unit->data(), adtsHeader, aac_frame_length);
1659
1660        unit->meta()->setInt64("timeUs", unitTimeUs);
1661        setAccessUnitProperties(unit, packetSource);
1662        packetSource->queueAccessUnit(unit);
1663    }
1664
1665    return OK;
1666}
1667
1668void PlaylistFetcher::updateDuration() {
1669    int64_t durationUs = 0ll;
1670    for (size_t index = 0; index < mPlaylist->size(); ++index) {
1671        sp<AMessage> itemMeta;
1672        CHECK(mPlaylist->itemAt(
1673                    index, NULL /* uri */, &itemMeta));
1674
1675        int64_t itemDurationUs;
1676        CHECK(itemMeta->findInt64("durationUs", &itemDurationUs));
1677
1678        durationUs += itemDurationUs;
1679    }
1680
1681    sp<AMessage> msg = mNotify->dup();
1682    msg->setInt32("what", kWhatDurationUpdate);
1683    msg->setInt64("durationUs", durationUs);
1684    msg->post();
1685}
1686
1687int64_t PlaylistFetcher::resumeThreshold(const sp<AMessage> &msg) {
1688    int64_t durationUs, threshold;
1689    if (msg->findInt64("durationUs", &durationUs) && durationUs > 0) {
1690        return kNumSkipFrames * durationUs;
1691    }
1692
1693    sp<RefBase> obj;
1694    msg->findObject("format", &obj);
1695    MetaData *format = static_cast<MetaData *>(obj.get());
1696
1697    const char *mime;
1698    CHECK(format->findCString(kKeyMIMEType, &mime));
1699    bool audio = !strncasecmp(mime, "audio/", 6);
1700    if (audio) {
1701        // Assumes 1000 samples per frame.
1702        int32_t sampleRate;
1703        CHECK(format->findInt32(kKeySampleRate, &sampleRate));
1704        return kNumSkipFrames  /* frames */ * 1000 /* samples */
1705                * (1000000 / sampleRate) /* sample duration (us) */;
1706    } else {
1707        int32_t frameRate;
1708        if (format->findInt32(kKeyFrameRate, &frameRate) && frameRate > 0) {
1709            return kNumSkipFrames * (1000000 / frameRate);
1710        }
1711    }
1712
1713    return 500000ll;
1714}
1715
1716}  // namespace android
1717