LiveSession.h revision 6c8495c8f1ccc35db972ee7ac0dbb8baf5843548
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 LiveDataSource; 32struct M3UParser; 33struct PlaylistFetcher; 34struct Parcel; 35 36struct LiveSession : public AHandler { 37 enum Flags { 38 // Don't log any URLs. 39 kFlagIncognito = 1, 40 }; 41 LiveSession( 42 const sp<AMessage> ¬ify, 43 uint32_t flags = 0, bool uidValid = false, uid_t uid = 0); 44 45 enum StreamIndex { 46 kAudioIndex = 0, 47 kVideoIndex = 1, 48 kSubtitleIndex = 2, 49 kMaxStreams = 3, 50 }; 51 52 enum StreamType { 53 STREAMTYPE_AUDIO = 1 << kAudioIndex, 54 STREAMTYPE_VIDEO = 1 << kVideoIndex, 55 STREAMTYPE_SUBTITLES = 1 << kSubtitleIndex, 56 }; 57 status_t dequeueAccessUnit(StreamType stream, sp<ABuffer> *accessUnit); 58 59 status_t getStreamFormat(StreamType stream, sp<AMessage> *format); 60 61 void connectAsync( 62 const char *url, 63 const KeyedVector<String8, String8> *headers = NULL); 64 65 status_t disconnect(); 66 67 // Blocks until seek is complete. 68 status_t seekTo(int64_t timeUs); 69 70 status_t getDuration(int64_t *durationUs) const; 71 status_t getTrackInfo(Parcel *reply) const; 72 status_t selectTrack(size_t index, bool select); 73 74 bool isSeekable() const; 75 bool hasDynamicDuration() const; 76 77 enum { 78 kWhatStreamsChanged, 79 kWhatError, 80 kWhatPrepared, 81 kWhatPreparationFailed, 82 }; 83 84protected: 85 virtual ~LiveSession(); 86 87 virtual void onMessageReceived(const sp<AMessage> &msg); 88 89private: 90 friend struct PlaylistFetcher; 91 92 enum { 93 kWhatConnect = 'conn', 94 kWhatDisconnect = 'disc', 95 kWhatSeek = 'seek', 96 kWhatFetcherNotify = 'notf', 97 kWhatCheckBandwidth = 'bndw', 98 kWhatChangeConfiguration = 'chC0', 99 kWhatChangeConfiguration2 = 'chC2', 100 kWhatChangeConfiguration3 = 'chC3', 101 kWhatFinishDisconnect2 = 'fin2', 102 }; 103 104 struct BandwidthItem { 105 size_t mPlaylistIndex; 106 unsigned long mBandwidth; 107 }; 108 109 struct FetcherInfo { 110 sp<PlaylistFetcher> mFetcher; 111 int64_t mDurationUs; 112 bool mIsPrepared; 113 }; 114 115 struct StreamItem { 116 const char *mType; 117 AString mUri; 118 StreamItem() : mType("") {} 119 StreamItem(const char *type) : mType(type) {} 120 AString uriKey() { 121 AString key(mType); 122 key.append("URI"); 123 return key; 124 } 125 }; 126 StreamItem mStreams[kMaxStreams]; 127 128 sp<AMessage> mNotify; 129 uint32_t mFlags; 130 bool mUIDValid; 131 uid_t mUID; 132 133 bool mInPreparationPhase; 134 135 sp<HTTPBase> mHTTPDataSource; 136 KeyedVector<String8, String8> mExtraHeaders; 137 138 AString mMasterURL; 139 140 Vector<BandwidthItem> mBandwidthItems; 141 ssize_t mPrevBandwidthIndex; 142 143 sp<M3UParser> mPlaylist; 144 145 KeyedVector<AString, FetcherInfo> mFetcherInfos; 146 uint32_t mStreamMask; 147 148 KeyedVector<StreamType, sp<AnotherPacketSource> > mPacketSources; 149 150 int32_t mCheckBandwidthGeneration; 151 152 size_t mContinuationCounter; 153 sp<AMessage> mContinuation; 154 155 int64_t mLastDequeuedTimeUs; 156 int64_t mRealTimeBaseUs; 157 158 bool mReconfigurationInProgress; 159 uint32_t mDisconnectReplyID; 160 161 sp<PlaylistFetcher> addFetcher(const char *uri); 162 163 void onConnect(const sp<AMessage> &msg); 164 status_t onSeek(const sp<AMessage> &msg); 165 void onFinishDisconnect2(); 166 167 // If given a non-zero block_size (default 0), it is used to cap the number of 168 // bytes read in from the DataSource. If given a non-NULL buffer, new content 169 // is read into the end. 170 // 171 // The DataSource we read from is responsible for signaling error or EOF to help us 172 // break out of the read loop. The DataSource can be returned to the caller, so 173 // that the caller can reuse it for subsequent fetches (within the initially 174 // requested range). 175 // 176 // For reused HTTP sources, the caller must download a file sequentially without 177 // any overlaps or gaps to prevent reconnection. 178 status_t fetchFile( 179 const char *url, sp<ABuffer> *out, 180 /* request/open a file starting at range_offset for range_length bytes */ 181 int64_t range_offset = 0, int64_t range_length = -1, 182 /* download block size */ 183 uint32_t block_size = 0, 184 /* reuse DataSource if doing partial fetch */ 185 sp<DataSource> *source = NULL); 186 187 sp<M3UParser> fetchPlaylist( 188 const char *url, uint8_t *curPlaylistHash, bool *unchanged); 189 190 size_t getBandwidthIndex(); 191 192 static int SortByBandwidth(const BandwidthItem *, const BandwidthItem *); 193 static StreamType indexToType(int idx); 194 195 void changeConfiguration( 196 int64_t timeUs, size_t bandwidthIndex, bool pickTrack = false); 197 void onChangeConfiguration(const sp<AMessage> &msg); 198 void onChangeConfiguration2(const sp<AMessage> &msg); 199 void onChangeConfiguration3(const sp<AMessage> &msg); 200 201 void scheduleCheckBandwidthEvent(); 202 void cancelCheckBandwidthEvent(); 203 204 void onCheckBandwidth(); 205 206 void finishDisconnect(); 207 208 void postPrepared(status_t err); 209 210 DISALLOW_EVIL_CONSTRUCTORS(LiveSession); 211}; 212 213} // namespace android 214 215#endif // LIVE_SESSION_H_ 216