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