LiveSession.h revision 00598ec0b15426197494aaf9e5ec0bc88507c762
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 }; 112 113 struct BandwidthItem { 114 size_t mPlaylistIndex; 115 unsigned long mBandwidth; 116 }; 117 118 struct FetcherInfo { 119 sp<PlaylistFetcher> mFetcher; 120 int64_t mDurationUs; 121 bool mIsPrepared; 122 bool mToBeRemoved; 123 }; 124 125 struct StreamItem { 126 const char *mType; 127 AString mUri; 128 size_t mCurDiscontinuitySeq; 129 int64_t mLastDequeuedTimeUs; 130 int64_t mLastSampleDurationUs; 131 StreamItem() 132 : mType(""), 133 mCurDiscontinuitySeq(0), 134 mLastDequeuedTimeUs(0), 135 mLastSampleDurationUs(0) {} 136 StreamItem(const char *type) 137 : mType(type), 138 mCurDiscontinuitySeq(0), 139 mLastDequeuedTimeUs(0), 140 mLastSampleDurationUs(0) {} 141 AString uriKey() { 142 AString key(mType); 143 key.append("URI"); 144 return key; 145 } 146 }; 147 StreamItem mStreams[kMaxStreams]; 148 149 sp<AMessage> mNotify; 150 uint32_t mFlags; 151 sp<IMediaHTTPService> mHTTPService; 152 153 bool mInPreparationPhase; 154 155 sp<HTTPBase> mHTTPDataSource; 156 KeyedVector<String8, String8> mExtraHeaders; 157 158 AString mMasterURL; 159 160 Vector<BandwidthItem> mBandwidthItems; 161 ssize_t mCurBandwidthIndex; 162 163 sp<M3UParser> mPlaylist; 164 165 KeyedVector<AString, FetcherInfo> mFetcherInfos; 166 uint32_t mStreamMask; 167 168 // Masks used during reconfiguration: 169 // mNewStreamMask: streams in the variant playlist we're switching to; 170 // we don't want to immediately overwrite the original value. 171 uint32_t mNewStreamMask; 172 173 // mSwapMask: streams that have started to playback content in the new variant playlist; 174 // we use this to track reconfiguration progress. 175 uint32_t mSwapMask; 176 177 KeyedVector<StreamType, sp<AnotherPacketSource> > mDiscontinuities; 178 KeyedVector<StreamType, sp<AnotherPacketSource> > mPacketSources; 179 // A second set of packet sources that buffer content for the variant we're switching to. 180 KeyedVector<StreamType, sp<AnotherPacketSource> > mPacketSources2; 181 182 // A mutex used to serialize two sets of events: 183 // * the swapping of packet sources in dequeueAccessUnit on the player thread, AND 184 // * a forced bandwidth switch termination in cancelSwitch on the live looper. 185 Mutex mSwapMutex; 186 187 int32_t mCheckBandwidthGeneration; 188 int32_t mSwitchGeneration; 189 190 size_t mContinuationCounter; 191 sp<AMessage> mContinuation; 192 sp<AMessage> mSeekReply; 193 194 int64_t mLastDequeuedTimeUs; 195 int64_t mRealTimeBaseUs; 196 197 bool mReconfigurationInProgress; 198 bool mSwitchInProgress; 199 uint32_t mDisconnectReplyID; 200 uint32_t mSeekReplyID; 201 202 bool mFirstTimeUsValid; 203 int64_t mFirstTimeUs; 204 int64_t mLastSeekTimeUs; 205 KeyedVector<size_t, int64_t> mDiscontinuityAbsStartTimesUs; 206 KeyedVector<size_t, int64_t> mDiscontinuityOffsetTimesUs; 207 208 sp<PlaylistFetcher> addFetcher(const char *uri); 209 210 void onConnect(const sp<AMessage> &msg); 211 status_t onSeek(const sp<AMessage> &msg); 212 void onFinishDisconnect2(); 213 214 // If given a non-zero block_size (default 0), it is used to cap the number of 215 // bytes read in from the DataSource. If given a non-NULL buffer, new content 216 // is read into the end. 217 // 218 // The DataSource we read from is responsible for signaling error or EOF to help us 219 // break out of the read loop. The DataSource can be returned to the caller, so 220 // that the caller can reuse it for subsequent fetches (within the initially 221 // requested range). 222 // 223 // For reused HTTP sources, the caller must download a file sequentially without 224 // any overlaps or gaps to prevent reconnection. 225 ssize_t fetchFile( 226 const char *url, sp<ABuffer> *out, 227 /* request/open a file starting at range_offset for range_length bytes */ 228 int64_t range_offset = 0, int64_t range_length = -1, 229 /* download block size */ 230 uint32_t block_size = 0, 231 /* reuse DataSource if doing partial fetch */ 232 sp<DataSource> *source = NULL, 233 String8 *actualUrl = NULL); 234 235 sp<M3UParser> fetchPlaylist( 236 const char *url, uint8_t *curPlaylistHash, bool *unchanged); 237 238 size_t getBandwidthIndex(); 239 240 static int SortByBandwidth(const BandwidthItem *, const BandwidthItem *); 241 static StreamType indexToType(int idx); 242 243 void changeConfiguration( 244 int64_t timeUs, size_t bandwidthIndex, bool pickTrack = false); 245 void onChangeConfiguration(const sp<AMessage> &msg); 246 void onChangeConfiguration2(const sp<AMessage> &msg); 247 void onChangeConfiguration3(const sp<AMessage> &msg); 248 void onSwapped(const sp<AMessage> &msg); 249 void tryToFinishBandwidthSwitch(); 250 251 void scheduleCheckBandwidthEvent(); 252 void cancelCheckBandwidthEvent(); 253 254 // cancelBandwidthSwitch is atomic wrt swapPacketSource; call it to prevent packet sources 255 // from being swapped out on stale discontinuities while manipulating 256 // mPacketSources/mPacketSources2. 257 void cancelBandwidthSwitch(); 258 259 bool canSwitchBandwidthTo(size_t bandwidthIndex); 260 void onCheckBandwidth(const sp<AMessage> &msg); 261 262 void finishDisconnect(); 263 264 void postPrepared(status_t err); 265 266 void swapPacketSource(StreamType stream); 267 bool canSwitchUp(); 268 269 DISALLOW_EVIL_CONSTRUCTORS(LiveSession); 270}; 271 272} // namespace android 273 274#endif // LIVE_SESSION_H_ 275