PlaylistFetcher.h revision f580806d893c4631f5324ff0af5c2db68a40ef42
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#ifndef PLAYLIST_FETCHER_H_
18
19#define PLAYLIST_FETCHER_H_
20
21#include <media/stagefright/foundation/AHandler.h>
22
23#include "mpeg2ts/ATSParser.h"
24#include "LiveSession.h"
25
26namespace android {
27
28struct ABuffer;
29struct AnotherPacketSource;
30struct DataSource;
31struct HTTPBase;
32struct LiveDataSource;
33struct M3UParser;
34struct String8;
35
36struct PlaylistFetcher : public AHandler {
37    static const int64_t kMinBufferedDurationUs;
38    static const int32_t kDownloadBlockSize;
39
40    enum {
41        kWhatStarted,
42        kWhatPaused,
43        kWhatStopped,
44        kWhatError,
45        kWhatDurationUpdate,
46        kWhatTemporarilyDoneFetching,
47        kWhatPrepared,
48        kWhatPreparationFailed,
49        kWhatStartedAt,
50    };
51
52    PlaylistFetcher(
53            const sp<AMessage> &notify,
54            const sp<LiveSession> &session,
55            const char *uri,
56            int32_t subtitleGeneration);
57
58    sp<DataSource> getDataSource();
59
60    void startAsync(
61            const sp<AnotherPacketSource> &audioSource,
62            const sp<AnotherPacketSource> &videoSource,
63            const sp<AnotherPacketSource> &subtitleSource,
64            int64_t startTimeUs = -1ll,         // starting timestamps
65            int64_t segmentStartTimeUs = -1ll, // starting position within playlist
66            // startTimeUs!=segmentStartTimeUs only when playlist is live
67            int32_t startDiscontinuitySeq = 0,
68            bool adaptive = false);
69
70    void pauseAsync();
71
72    void stopAsync(bool clear = true);
73
74    void resumeUntilAsync(const sp<AMessage> &params);
75
76    uint32_t getStreamTypeMask() const {
77        return mStreamTypeMask;
78    }
79
80protected:
81    virtual ~PlaylistFetcher();
82    virtual void onMessageReceived(const sp<AMessage> &msg);
83
84private:
85    enum {
86        kMaxNumRetries         = 5,
87    };
88
89    enum {
90        kWhatStart          = 'strt',
91        kWhatPause          = 'paus',
92        kWhatStop           = 'stop',
93        kWhatMonitorQueue   = 'moni',
94        kWhatResumeUntil    = 'rsme',
95        kWhatDownloadNext   = 'dlnx',
96    };
97
98    static const int64_t kMaxMonitorDelayUs;
99    static const int32_t kNumSkipFrames;
100
101    static bool bufferStartsWithTsSyncByte(const sp<ABuffer>& buffer);
102    static bool bufferStartsWithWebVTTMagicSequence(const sp<ABuffer>& buffer);
103
104    // notifications to mSession
105    sp<AMessage> mNotify;
106    sp<AMessage> mStartTimeUsNotify;
107
108    sp<LiveSession> mSession;
109    AString mURI;
110
111    uint32_t mStreamTypeMask;
112    int64_t mStartTimeUs;
113
114    // Start time relative to the beginning of the first segment in the initial
115    // playlist. It's value is initialized to a non-negative value only when we are
116    // adapting or switching tracks.
117    int64_t mSegmentStartTimeUs;
118
119    ssize_t mDiscontinuitySeq;
120    bool mStartTimeUsRelative;
121    sp<AMessage> mStopParams; // message containing the latest timestamps we should fetch.
122
123    KeyedVector<LiveSession::StreamType, sp<AnotherPacketSource> >
124        mPacketSources;
125
126    KeyedVector<AString, sp<ABuffer> > mAESKeyForURI;
127
128    int64_t mLastPlaylistFetchTimeUs;
129    sp<M3UParser> mPlaylist;
130    int32_t mSeqNumber;
131    int32_t mNumRetries;
132    bool mStartup;
133    bool mAdaptive;
134    bool mPrepared;
135    int64_t mNextPTSTimeUs;
136
137    int32_t mMonitorQueueGeneration;
138    const int32_t mSubtitleGeneration;
139
140    enum RefreshState {
141        INITIAL_MINIMUM_RELOAD_DELAY,
142        FIRST_UNCHANGED_RELOAD_ATTEMPT,
143        SECOND_UNCHANGED_RELOAD_ATTEMPT,
144        THIRD_UNCHANGED_RELOAD_ATTEMPT
145    };
146    RefreshState mRefreshState;
147
148    uint8_t mPlaylistHash[16];
149
150    sp<ATSParser> mTSParser;
151
152    bool mFirstPTSValid;
153    uint64_t mFirstPTS;
154    int64_t mFirstTimeUs;
155    int64_t mAbsoluteTimeAnchorUs;
156    sp<AnotherPacketSource> mVideoBuffer;
157
158    // Stores the initialization vector to decrypt the next block of cipher text, which can
159    // either be derived from the sequence number, read from the manifest, or copied from
160    // the last block of cipher text (cipher-block chaining).
161    unsigned char mAESInitVec[16];
162
163    // Set first to true if decrypting the first segment of a playlist segment. When
164    // first is true, reset the initialization vector based on the available
165    // information in the manifest; otherwise, use the initialization vector as
166    // updated by the last call to AES_cbc_encrypt.
167    //
168    // For the input to decrypt correctly, decryptBuffer must be called on
169    // consecutive byte ranges on block boundaries, e.g. 0..15, 16..47, 48..63,
170    // and so on.
171    status_t decryptBuffer(
172            size_t playlistIndex, const sp<ABuffer> &buffer,
173            bool first = true);
174    status_t checkDecryptPadding(const sp<ABuffer> &buffer);
175
176    void postMonitorQueue(int64_t delayUs = 0, int64_t minDelayUs = 0);
177    void cancelMonitorQueue();
178
179    int64_t delayUsToRefreshPlaylist() const;
180    status_t refreshPlaylist();
181
182    // Returns the media time in us of the segment specified by seqNumber.
183    // This is computed by summing the durations of all segments before it.
184    int64_t getSegmentStartTimeUs(int32_t seqNumber) const;
185
186    status_t onStart(const sp<AMessage> &msg);
187    void onPause();
188    void onStop(const sp<AMessage> &msg);
189    void onMonitorQueue();
190    void onDownloadNext();
191
192    // Resume a fetcher to continue until the stopping point stored in msg.
193    status_t onResumeUntil(const sp<AMessage> &msg);
194
195    const sp<ABuffer> &setAccessUnitProperties(
196            const sp<ABuffer> &accessUnit,
197            const sp<AnotherPacketSource> &source,
198            bool discard = false);
199    status_t extractAndQueueAccessUnitsFromTs(const sp<ABuffer> &buffer);
200
201    status_t extractAndQueueAccessUnits(
202            const sp<ABuffer> &buffer, const sp<AMessage> &itemMeta);
203
204    void notifyError(status_t err);
205
206    void queueDiscontinuity(
207            ATSParser::DiscontinuityType type, const sp<AMessage> &extra);
208
209    int32_t getSeqNumberWithAnchorTime(int64_t anchorTimeUs) const;
210    int32_t getSeqNumberForDiscontinuity(size_t discontinuitySeq) const;
211    int32_t getSeqNumberForTime(int64_t timeUs) const;
212
213    void updateDuration();
214
215    // Before resuming a fetcher in onResume, check the remaining duration is longer than that
216    // returned by resumeThreshold.
217    int64_t resumeThreshold(const sp<AMessage> &msg);
218
219    DISALLOW_EVIL_CONSTRUCTORS(PlaylistFetcher);
220};
221
222}  // namespace android
223
224#endif  // PLAYLIST_FETCHER_H_
225
226