PlaylistFetcher.h revision a48d372833ccec13c96ece9efcc226e8beac7f59
1/* 2 * Copyright (C) 2012 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 PLAYLIST_FETCHER_H_ 18 19#define PLAYLIST_FETCHER_H_ 20 21#include <media/stagefright/foundation/AHandler.h> 22 23#include "mpeg2ts/ATSParser.h" 24#include "LiveSession.h" 25 26namespace android { 27 28struct ABuffer; 29struct AnotherPacketSource; 30struct DataSource; 31struct HTTPBase; 32struct LiveDataSource; 33struct M3UParser; 34class String8; 35 36struct PlaylistFetcher : public AHandler { 37 static const int64_t kMinBufferedDurationUs; 38 static const int32_t kDownloadBlockSize; 39 static const int64_t kFetcherResumeThreshold; 40 41 enum { 42 kWhatStarted, 43 kWhatPaused, 44 kWhatStopped, 45 kWhatError, 46 kWhatDurationUpdate, 47 kWhatPrepared, 48 kWhatPreparationFailed, 49 kWhatStartedAt, 50 }; 51 52 PlaylistFetcher( 53 const sp<AMessage> ¬ify, 54 const sp<LiveSession> &session, 55 const char *uri, 56 int32_t subtitleGeneration); 57 58 sp<DataSource> getDataSource(); 59 60 void startAsync( 61 const sp<AnotherPacketSource> &audioSource, 62 const sp<AnotherPacketSource> &videoSource, 63 const sp<AnotherPacketSource> &subtitleSource, 64 int64_t startTimeUs = -1ll, // starting timestamps 65 int64_t segmentStartTimeUs = -1ll, // starting position within playlist 66 // startTimeUs!=segmentStartTimeUs only when playlist is live 67 int32_t startDiscontinuitySeq = 0, 68 LiveSession::SeekMode seekMode = LiveSession::kSeekModeExactPosition); 69 70 void pauseAsync(float thresholdRatio); 71 72 void stopAsync(bool clear = true); 73 74 void resumeUntilAsync(const sp<AMessage> ¶ms); 75 76 uint32_t getStreamTypeMask() const { 77 return mStreamTypeMask; 78 } 79 80protected: 81 virtual ~PlaylistFetcher(); 82 virtual void onMessageReceived(const sp<AMessage> &msg); 83 84private: 85 enum { 86 kMaxNumRetries = 5, 87 }; 88 89 enum { 90 kWhatStart = 'strt', 91 kWhatPause = 'paus', 92 kWhatStop = 'stop', 93 kWhatMonitorQueue = 'moni', 94 kWhatResumeUntil = 'rsme', 95 kWhatDownloadNext = 'dlnx', 96 }; 97 98 struct DownloadState; 99 100 static const int64_t kMaxMonitorDelayUs; 101 static const int32_t kNumSkipFrames; 102 103 static bool bufferStartsWithTsSyncByte(const sp<ABuffer>& buffer); 104 static bool bufferStartsWithWebVTTMagicSequence(const sp<ABuffer>& buffer); 105 106 // notifications to mSession 107 sp<AMessage> mNotify; 108 sp<AMessage> mStartTimeUsNotify; 109 110 sp<HTTPBase> mHTTPDataSource; 111 sp<LiveSession> mSession; 112 AString mURI; 113 114 uint32_t mStreamTypeMask; 115 int64_t mStartTimeUs; 116 117 // Start time relative to the beginning of the first segment in the initial 118 // playlist. It's value is initialized to a non-negative value only when we are 119 // adapting or switching tracks. 120 int64_t mSegmentStartTimeUs; 121 122 int32_t mDiscontinuitySeq; 123 bool mStartTimeUsRelative; 124 sp<AMessage> mStopParams; // message containing the latest timestamps we should fetch. 125 126 KeyedVector<LiveSession::StreamType, sp<AnotherPacketSource> > 127 mPacketSources; 128 129 KeyedVector<AString, sp<ABuffer> > mAESKeyForURI; 130 131 int64_t mLastPlaylistFetchTimeUs; 132 sp<M3UParser> mPlaylist; 133 int32_t mSeqNumber; 134 int32_t mNumRetries; 135 bool mStartup; 136 bool mIDRFound; 137 int32_t mSeekMode; 138 bool mPrepared; 139 bool mTimeChangeSignaled; 140 int64_t mNextPTSTimeUs; 141 142 int32_t mMonitorQueueGeneration; 143 const int32_t mSubtitleGeneration; 144 145 int32_t mLastDiscontinuitySeq; 146 147 enum RefreshState { 148 INITIAL_MINIMUM_RELOAD_DELAY, 149 FIRST_UNCHANGED_RELOAD_ATTEMPT, 150 SECOND_UNCHANGED_RELOAD_ATTEMPT, 151 THIRD_UNCHANGED_RELOAD_ATTEMPT 152 }; 153 RefreshState mRefreshState; 154 155 uint8_t mPlaylistHash[16]; 156 157 sp<ATSParser> mTSParser; 158 159 bool mFirstPTSValid; 160 int64_t mFirstTimeUs; 161 int64_t mSegmentFirstPTS; 162 sp<AnotherPacketSource> mVideoBuffer; 163 164 // Stores the initialization vector to decrypt the next block of cipher text, which can 165 // either be derived from the sequence number, read from the manifest, or copied from 166 // the last block of cipher text (cipher-block chaining). 167 unsigned char mAESInitVec[16]; 168 169 Mutex mThresholdLock; 170 float mThresholdRatio; 171 172 sp<DownloadState> mDownloadState; 173 174 // Set first to true if decrypting the first segment of a playlist segment. When 175 // first is true, reset the initialization vector based on the available 176 // information in the manifest; otherwise, use the initialization vector as 177 // updated by the last call to AES_cbc_encrypt. 178 // 179 // For the input to decrypt correctly, decryptBuffer must be called on 180 // consecutive byte ranges on block boundaries, e.g. 0..15, 16..47, 48..63, 181 // and so on. 182 status_t decryptBuffer( 183 size_t playlistIndex, const sp<ABuffer> &buffer, 184 bool first = true); 185 status_t checkDecryptPadding(const sp<ABuffer> &buffer); 186 187 void postMonitorQueue(int64_t delayUs = 0, int64_t minDelayUs = 0); 188 void cancelMonitorQueue(); 189 void setStoppingThreshold(float thresholdRatio); 190 bool shouldPauseDownload(bool startFound); 191 192 int64_t delayUsToRefreshPlaylist() const; 193 status_t refreshPlaylist(); 194 195 // Returns the media time in us of the segment specified by seqNumber. 196 // This is computed by summing the durations of all segments before it. 197 int64_t getSegmentStartTimeUs(int32_t seqNumber) const; 198 199 status_t onStart(const sp<AMessage> &msg); 200 void onPause(); 201 void onStop(const sp<AMessage> &msg); 202 void onMonitorQueue(); 203 void onDownloadNext(); 204 bool initDownloadState( 205 AString &uri, 206 sp<AMessage> &itemMeta, 207 int32_t &firstSeqNumberInPlaylist, 208 int32_t &lastSeqNumberInPlaylist); 209 210 // Resume a fetcher to continue until the stopping point stored in msg. 211 status_t onResumeUntil(const sp<AMessage> &msg); 212 213 const sp<ABuffer> &setAccessUnitProperties( 214 const sp<ABuffer> &accessUnit, 215 const sp<AnotherPacketSource> &source, 216 bool discard = false); 217 status_t extractAndQueueAccessUnitsFromTs(const sp<ABuffer> &buffer); 218 219 status_t extractAndQueueAccessUnits( 220 const sp<ABuffer> &buffer, const sp<AMessage> &itemMeta); 221 222 void notifyError(status_t err); 223 224 void queueDiscontinuity( 225 ATSParser::DiscontinuityType type, const sp<AMessage> &extra); 226 227 int32_t getSeqNumberWithAnchorTime( 228 int64_t anchorTimeUs, int64_t targetDurationUs) const; 229 int32_t getSeqNumberForDiscontinuity(size_t discontinuitySeq) const; 230 int32_t getSeqNumberForTime(int64_t timeUs) const; 231 232 void updateDuration(); 233 234 DISALLOW_EVIL_CONSTRUCTORS(PlaylistFetcher); 235}; 236 237} // namespace android 238 239#endif // PLAYLIST_FETCHER_H_ 240 241