LiveSession.h revision 0ad776d2e4c6b4968d9dcd9bf34b962366b312a9
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> ¬ify, 44 uint32_t flags, 45 const sp<IMediaHTTPService> &httpService); 46 47 enum StreamIndex { 48 kAudioIndex = 0, 49 kVideoIndex = 1, 50 kSubtitleIndex = 2, 51 kMaxStreams = 3, 52 }; 53 54 enum StreamType { 55 STREAMTYPE_AUDIO = 1 << kAudioIndex, 56 STREAMTYPE_VIDEO = 1 << kVideoIndex, 57 STREAMTYPE_SUBTITLES = 1 << kSubtitleIndex, 58 }; 59 status_t dequeueAccessUnit(StreamType stream, sp<ABuffer> *accessUnit); 60 61 status_t getStreamFormat(StreamType stream, sp<AMessage> *format); 62 63 void connectAsync( 64 const char *url, 65 const KeyedVector<String8, String8> *headers = NULL); 66 67 status_t disconnect(); 68 69 // Blocks until seek is complete. 70 status_t seekTo(int64_t timeUs); 71 72 status_t getDuration(int64_t *durationUs) const; 73 size_t getTrackCount() const; 74 sp<AMessage> getTrackInfo(size_t trackIndex) const; 75 status_t selectTrack(size_t index, bool select); 76 77 bool isSeekable() const; 78 bool hasDynamicDuration() const; 79 80 enum { 81 kWhatStreamsChanged, 82 kWhatError, 83 kWhatPrepared, 84 kWhatPreparationFailed, 85 }; 86 87 // create a format-change discontinuity 88 // 89 // swap: 90 // whether is format-change discontinuity should trigger a buffer swap 91 sp<ABuffer> createFormatChangeBuffer(bool swap = true); 92protected: 93 virtual ~LiveSession(); 94 95 virtual void onMessageReceived(const sp<AMessage> &msg); 96 97private: 98 friend struct PlaylistFetcher; 99 100 enum { 101 kWhatConnect = 'conn', 102 kWhatDisconnect = 'disc', 103 kWhatSeek = 'seek', 104 kWhatFetcherNotify = 'notf', 105 kWhatCheckBandwidth = 'bndw', 106 kWhatChangeConfiguration = 'chC0', 107 kWhatChangeConfiguration2 = 'chC2', 108 kWhatChangeConfiguration3 = 'chC3', 109 kWhatFinishDisconnect2 = 'fin2', 110 kWhatSwapped = 'swap', 111 kWhatCheckSwitchDown = 'ckSD', 112 kWhatSwitchDown = 'sDwn', 113 }; 114 115 struct BandwidthItem { 116 size_t mPlaylistIndex; 117 unsigned long mBandwidth; 118 }; 119 120 struct FetcherInfo { 121 sp<PlaylistFetcher> mFetcher; 122 int64_t mDurationUs; 123 bool mIsPrepared; 124 bool mToBeRemoved; 125 }; 126 127 struct StreamItem { 128 const char *mType; 129 AString mUri; 130 size_t mCurDiscontinuitySeq; 131 int64_t mLastDequeuedTimeUs; 132 int64_t mLastSampleDurationUs; 133 StreamItem() 134 : mType(""), 135 mCurDiscontinuitySeq(0), 136 mLastDequeuedTimeUs(0), 137 mLastSampleDurationUs(0) {} 138 StreamItem(const char *type) 139 : mType(type), 140 mCurDiscontinuitySeq(0), 141 mLastDequeuedTimeUs(0), 142 mLastSampleDurationUs(0) {} 143 AString uriKey() { 144 AString key(mType); 145 key.append("URI"); 146 return key; 147 } 148 }; 149 StreamItem mStreams[kMaxStreams]; 150 151 sp<AMessage> mNotify; 152 uint32_t mFlags; 153 sp<IMediaHTTPService> mHTTPService; 154 155 bool mInPreparationPhase; 156 157 sp<HTTPBase> mHTTPDataSource; 158 KeyedVector<String8, String8> mExtraHeaders; 159 160 AString mMasterURL; 161 162 Vector<BandwidthItem> mBandwidthItems; 163 ssize_t mCurBandwidthIndex; 164 165 sp<M3UParser> mPlaylist; 166 167 KeyedVector<AString, FetcherInfo> mFetcherInfos; 168 uint32_t mStreamMask; 169 170 // Masks used during reconfiguration: 171 // mNewStreamMask: streams in the variant playlist we're switching to; 172 // we don't want to immediately overwrite the original value. 173 uint32_t mNewStreamMask; 174 175 // mSwapMask: streams that have started to playback content in the new variant playlist; 176 // we use this to track reconfiguration progress. 177 uint32_t mSwapMask; 178 179 KeyedVector<StreamType, sp<AnotherPacketSource> > mDiscontinuities; 180 KeyedVector<StreamType, sp<AnotherPacketSource> > mPacketSources; 181 // A second set of packet sources that buffer content for the variant we're switching to. 182 KeyedVector<StreamType, sp<AnotherPacketSource> > mPacketSources2; 183 184 // A mutex used to serialize two sets of events: 185 // * the swapping of packet sources in dequeueAccessUnit on the player thread, AND 186 // * a forced bandwidth switch termination in cancelSwitch on the live looper. 187 Mutex mSwapMutex; 188 189 int32_t mCheckBandwidthGeneration; 190 int32_t mSwitchGeneration; 191 192 size_t mContinuationCounter; 193 sp<AMessage> mContinuation; 194 sp<AMessage> mSeekReply; 195 196 int64_t mLastDequeuedTimeUs; 197 int64_t mRealTimeBaseUs; 198 199 bool mReconfigurationInProgress; 200 bool mSwitchInProgress; 201 uint32_t mDisconnectReplyID; 202 uint32_t mSeekReplyID; 203 204 bool mFirstTimeUsValid; 205 int64_t mFirstTimeUs; 206 int64_t mLastSeekTimeUs; 207 sp<AMessage> mSwitchDownMonitor; 208 KeyedVector<size_t, int64_t> mDiscontinuityAbsStartTimesUs; 209 KeyedVector<size_t, int64_t> mDiscontinuityOffsetTimesUs; 210 211 sp<PlaylistFetcher> addFetcher(const char *uri); 212 213 void onConnect(const sp<AMessage> &msg); 214 status_t onSeek(const sp<AMessage> &msg); 215 void onFinishDisconnect2(); 216 217 // If given a non-zero block_size (default 0), it is used to cap the number of 218 // bytes read in from the DataSource. If given a non-NULL buffer, new content 219 // is read into the end. 220 // 221 // The DataSource we read from is responsible for signaling error or EOF to help us 222 // break out of the read loop. The DataSource can be returned to the caller, so 223 // that the caller can reuse it for subsequent fetches (within the initially 224 // requested range). 225 // 226 // For reused HTTP sources, the caller must download a file sequentially without 227 // any overlaps or gaps to prevent reconnection. 228 ssize_t fetchFile( 229 const char *url, sp<ABuffer> *out, 230 /* request/open a file starting at range_offset for range_length bytes */ 231 int64_t range_offset = 0, int64_t range_length = -1, 232 /* download block size */ 233 uint32_t block_size = 0, 234 /* reuse DataSource if doing partial fetch */ 235 sp<DataSource> *source = NULL, 236 String8 *actualUrl = NULL); 237 238 sp<M3UParser> fetchPlaylist( 239 const char *url, uint8_t *curPlaylistHash, bool *unchanged); 240 241 size_t getBandwidthIndex(); 242 243 static int SortByBandwidth(const BandwidthItem *, const BandwidthItem *); 244 static StreamType indexToType(int idx); 245 246 void changeConfiguration( 247 int64_t timeUs, size_t bandwidthIndex, bool pickTrack = false); 248 void onChangeConfiguration(const sp<AMessage> &msg); 249 void onChangeConfiguration2(const sp<AMessage> &msg); 250 void onChangeConfiguration3(const sp<AMessage> &msg); 251 void onSwapped(const sp<AMessage> &msg); 252 void onCheckSwitchDown(); 253 void onSwitchDown(); 254 void tryToFinishBandwidthSwitch(); 255 256 void scheduleCheckBandwidthEvent(); 257 void cancelCheckBandwidthEvent(); 258 259 // cancelBandwidthSwitch is atomic wrt swapPacketSource; call it to prevent packet sources 260 // from being swapped out on stale discontinuities while manipulating 261 // mPacketSources/mPacketSources2. 262 void cancelBandwidthSwitch(); 263 264 bool canSwitchBandwidthTo(size_t bandwidthIndex); 265 void onCheckBandwidth(); 266 267 void finishDisconnect(); 268 269 void postPrepared(status_t err); 270 271 void swapPacketSource(StreamType stream); 272 bool canSwitchUp(); 273 274 DISALLOW_EVIL_CONSTRUCTORS(LiveSession); 275}; 276 277} // namespace android 278 279#endif // LIVE_SESSION_H_ 280