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