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