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