PlaylistFetcher.h revision 6708a45ba1b5eacf583d5e80b2075b68940fe0ab
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    };
47
48    PlaylistFetcher(
49            const sp<AMessage> &notify,
50            const sp<LiveSession> &session,
51            const char *uri);
52
53    sp<DataSource> getDataSource();
54
55    void startAsync(
56            const sp<AnotherPacketSource> &audioSource,
57            const sp<AnotherPacketSource> &videoSource,
58            const sp<AnotherPacketSource> &subtitleSource,
59            int64_t startTimeUs = -1ll);
60
61    void pauseAsync();
62
63    void stopAsync();
64
65protected:
66    virtual ~PlaylistFetcher();
67    virtual void onMessageReceived(const sp<AMessage> &msg);
68
69private:
70    enum {
71        kMaxNumRetries         = 5,
72    };
73
74    enum {
75        kWhatStart          = 'strt',
76        kWhatPause          = 'paus',
77        kWhatStop           = 'stop',
78        kWhatMonitorQueue   = 'moni',
79    };
80
81    static const int64_t kMinBufferedDurationUs;
82    static const int64_t kMaxMonitorDelayUs;
83
84    sp<AMessage> mNotify;
85    sp<LiveSession> mSession;
86    AString mURI;
87
88    uint32_t mStreamTypeMask;
89    int64_t mStartTimeUs;
90
91    KeyedVector<LiveSession::StreamType, sp<AnotherPacketSource> >
92        mPacketSources;
93
94    KeyedVector<AString, sp<ABuffer> > mAESKeyForURI;
95
96    int64_t mLastPlaylistFetchTimeUs;
97    sp<M3UParser> mPlaylist;
98    int32_t mSeqNumber;
99    int32_t mNumRetries;
100    bool mStartup;
101    bool mPrepared;
102    int64_t mNextPTSTimeUs;
103
104    int32_t mMonitorQueueGeneration;
105
106    enum RefreshState {
107        INITIAL_MINIMUM_RELOAD_DELAY,
108        FIRST_UNCHANGED_RELOAD_ATTEMPT,
109        SECOND_UNCHANGED_RELOAD_ATTEMPT,
110        THIRD_UNCHANGED_RELOAD_ATTEMPT
111    };
112    RefreshState mRefreshState;
113
114    uint8_t mPlaylistHash[16];
115
116    sp<ATSParser> mTSParser;
117
118    bool mFirstPTSValid;
119    uint64_t mFirstPTS;
120    int64_t mAbsoluteTimeAnchorUs;
121
122    status_t decryptBuffer(
123            size_t playlistIndex, const sp<ABuffer> &buffer);
124
125    void postMonitorQueue(int64_t delayUs = 0, int64_t minDelayUs = 0);
126    void cancelMonitorQueue();
127
128    int64_t delayUsToRefreshPlaylist() const;
129    status_t refreshPlaylist();
130
131    // Returns the media time in us of the segment specified by seqNumber.
132    // This is computed by summing the durations of all segments before it.
133    int64_t getSegmentStartTimeUs(int32_t seqNumber) const;
134
135    status_t onStart(const sp<AMessage> &msg);
136    void onPause();
137    void onStop();
138    void onMonitorQueue();
139    void onDownloadNext();
140
141    status_t extractAndQueueAccessUnits(
142            const sp<ABuffer> &buffer, const sp<AMessage> &itemMeta);
143
144    void notifyError(status_t err);
145
146    void queueDiscontinuity(
147            ATSParser::DiscontinuityType type, const sp<AMessage> &extra);
148
149    int32_t getSeqNumberForTime(int64_t timeUs) const;
150
151    void updateDuration();
152
153    DISALLOW_EVIL_CONSTRUCTORS(PlaylistFetcher);
154};
155
156}  // namespace android
157
158#endif  // PLAYLIST_FETCHER_H_
159
160