LiveSession.h revision 1b86fe063badb5f28c467ade39be0f4008688947
1/*
2 * Copyright (C) 2010 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 LIVE_SESSION_H_
18
19#define LIVE_SESSION_H_
20
21#include <media/stagefright/foundation/AHandler.h>
22
23#include <utils/String8.h>
24
25namespace android {
26
27struct ABuffer;
28struct AnotherPacketSource;
29struct DataSource;
30struct HTTPBase;
31struct IMediaHTTPService;
32struct LiveDataSource;
33struct M3UParser;
34struct PlaylistFetcher;
35struct Parcel;
36
37struct LiveSession : public AHandler {
38    enum Flags {
39        // Don't log any URLs.
40        kFlagIncognito = 1,
41    };
42    LiveSession(
43            const sp<AMessage> &notify,
44            uint32_t flags,
45            const sp<IMediaHTTPService> &httpService,
46            bool uidValid = false,
47            uid_t uid = 0);
48
49    enum StreamType {
50        STREAMTYPE_AUDIO        = 1,
51        STREAMTYPE_VIDEO        = 2,
52        STREAMTYPE_SUBTITLES    = 4,
53    };
54    status_t dequeueAccessUnit(StreamType stream, sp<ABuffer> *accessUnit);
55
56    status_t getStreamFormat(StreamType stream, sp<AMessage> *format);
57
58    void connectAsync(
59            const char *url,
60            const KeyedVector<String8, String8> *headers = NULL);
61
62    status_t disconnect();
63
64    // Blocks until seek is complete.
65    status_t seekTo(int64_t timeUs);
66
67    status_t getDuration(int64_t *durationUs) const;
68    status_t getTrackInfo(Parcel *reply) const;
69    status_t selectTrack(size_t index, bool select);
70
71    bool isSeekable() const;
72    bool hasDynamicDuration() const;
73
74    enum {
75        kWhatStreamsChanged,
76        kWhatError,
77        kWhatPrepared,
78        kWhatPreparationFailed,
79    };
80
81protected:
82    virtual ~LiveSession();
83
84    virtual void onMessageReceived(const sp<AMessage> &msg);
85
86private:
87    friend struct PlaylistFetcher;
88
89    enum {
90        kWhatConnect                    = 'conn',
91        kWhatDisconnect                 = 'disc',
92        kWhatSeek                       = 'seek',
93        kWhatFetcherNotify              = 'notf',
94        kWhatCheckBandwidth             = 'bndw',
95        kWhatChangeConfiguration        = 'chC0',
96        kWhatChangeConfiguration2       = 'chC2',
97        kWhatChangeConfiguration3       = 'chC3',
98        kWhatFinishDisconnect2          = 'fin2',
99    };
100
101    struct BandwidthItem {
102        size_t mPlaylistIndex;
103        unsigned long mBandwidth;
104    };
105
106    struct FetcherInfo {
107        sp<PlaylistFetcher> mFetcher;
108        int64_t mDurationUs;
109        bool mIsPrepared;
110    };
111
112    sp<AMessage> mNotify;
113    uint32_t mFlags;
114    sp<IMediaHTTPService> mHTTPService;
115    bool mUIDValid;
116    uid_t mUID;
117
118    bool mInPreparationPhase;
119
120    sp<HTTPBase> mHTTPDataSource;
121    KeyedVector<String8, String8> mExtraHeaders;
122
123    AString mMasterURL;
124
125    Vector<BandwidthItem> mBandwidthItems;
126    ssize_t mPrevBandwidthIndex;
127
128    sp<M3UParser> mPlaylist;
129
130    KeyedVector<AString, FetcherInfo> mFetcherInfos;
131    AString mAudioURI, mVideoURI, mSubtitleURI;
132    uint32_t mStreamMask;
133
134    KeyedVector<StreamType, sp<AnotherPacketSource> > mPacketSources;
135
136    int32_t mCheckBandwidthGeneration;
137
138    size_t mContinuationCounter;
139    sp<AMessage> mContinuation;
140
141    int64_t mLastDequeuedTimeUs;
142    int64_t mRealTimeBaseUs;
143
144    bool mReconfigurationInProgress;
145    uint32_t mDisconnectReplyID;
146
147    sp<PlaylistFetcher> addFetcher(const char *uri);
148
149    void onConnect(const sp<AMessage> &msg);
150    status_t onSeek(const sp<AMessage> &msg);
151    void onFinishDisconnect2();
152
153    status_t fetchFile(
154            const char *url, sp<ABuffer> *out,
155            int64_t range_offset = 0, int64_t range_length = -1);
156
157    sp<M3UParser> fetchPlaylist(
158            const char *url, uint8_t *curPlaylistHash, bool *unchanged);
159
160    size_t getBandwidthIndex();
161
162    static int SortByBandwidth(const BandwidthItem *, const BandwidthItem *);
163
164    void changeConfiguration(
165            int64_t timeUs, size_t bandwidthIndex, bool pickTrack = false);
166    void onChangeConfiguration(const sp<AMessage> &msg);
167    void onChangeConfiguration2(const sp<AMessage> &msg);
168    void onChangeConfiguration3(const sp<AMessage> &msg);
169
170    void scheduleCheckBandwidthEvent();
171    void cancelCheckBandwidthEvent();
172
173    void onCheckBandwidth();
174
175    void finishDisconnect();
176
177    void postPrepared(status_t err);
178
179    DISALLOW_EVIL_CONSTRUCTORS(LiveSession);
180};
181
182}  // namespace android
183
184#endif  // LIVE_SESSION_H_
185