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