PlaylistFetcher.h revision 309aa8bf5e4cd66fe988adf2654cac3fadc2a1c3
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; 34struct String8; 35 36struct PlaylistFetcher : public AHandler { 37 enum { 38 kWhatStarted, 39 kWhatPaused, 40 kWhatStopped, 41 kWhatError, 42 kWhatDurationUpdate, 43 kWhatTemporarilyDoneFetching, 44 kWhatPrepared, 45 kWhatPreparationFailed, 46 kWhatStartedAt, 47 }; 48 49 PlaylistFetcher( 50 const sp<AMessage> ¬ify, 51 const sp<LiveSession> &session, 52 const char *uri); 53 54 sp<DataSource> getDataSource(); 55 56 void startAsync( 57 const sp<AnotherPacketSource> &audioSource, 58 const sp<AnotherPacketSource> &videoSource, 59 const sp<AnotherPacketSource> &subtitleSource, 60 int64_t startTimeUs = -1ll, // starting timestamps 61 int64_t segmentStartTimeUs = -1ll, // starting position within playlist 62 // startTimeUs!=segmentStartTimeUs only when playlist is live 63 int32_t startDiscontinuitySeq = 0, 64 bool adaptive = false); 65 66 void pauseAsync(); 67 68 void stopAsync(bool clear = true); 69 70 void resumeUntilAsync(const sp<AMessage> ¶ms); 71 72protected: 73 virtual ~PlaylistFetcher(); 74 virtual void onMessageReceived(const sp<AMessage> &msg); 75 76private: 77 enum { 78 kMaxNumRetries = 5, 79 }; 80 81 enum { 82 kWhatStart = 'strt', 83 kWhatPause = 'paus', 84 kWhatStop = 'stop', 85 kWhatMonitorQueue = 'moni', 86 kWhatResumeUntil = 'rsme', 87 kWhatDownloadNext = 'dlnx', 88 }; 89 90 static const int64_t kMinBufferedDurationUs; 91 static const int64_t kMaxMonitorDelayUs; 92 static const int32_t kDownloadBlockSize; 93 static const int32_t kNumSkipFrames; 94 95 static bool bufferStartsWithTsSyncByte(const sp<ABuffer>& buffer); 96 static bool bufferStartsWithWebVTTMagicSequence(const sp<ABuffer>& buffer); 97 98 // notifications to mSession 99 sp<AMessage> mNotify; 100 sp<AMessage> mStartTimeUsNotify; 101 102 sp<LiveSession> mSession; 103 AString mURI; 104 105 uint32_t mStreamTypeMask; 106 int64_t mStartTimeUs; 107 int64_t mSegmentStartTimeUs; 108 ssize_t mDiscontinuitySeq; 109 bool mStartTimeUsRelative; 110 sp<AMessage> mStopParams; // message containing the latest timestamps we should fetch. 111 112 KeyedVector<LiveSession::StreamType, sp<AnotherPacketSource> > 113 mPacketSources; 114 115 KeyedVector<AString, sp<ABuffer> > mAESKeyForURI; 116 117 int64_t mLastPlaylistFetchTimeUs; 118 sp<M3UParser> mPlaylist; 119 int32_t mSeqNumber; 120 int32_t mNumRetries; 121 bool mStartup; 122 bool mAdaptive; 123 bool mPrepared; 124 int64_t mNextPTSTimeUs; 125 126 int32_t mMonitorQueueGeneration; 127 128 enum RefreshState { 129 INITIAL_MINIMUM_RELOAD_DELAY, 130 FIRST_UNCHANGED_RELOAD_ATTEMPT, 131 SECOND_UNCHANGED_RELOAD_ATTEMPT, 132 THIRD_UNCHANGED_RELOAD_ATTEMPT 133 }; 134 RefreshState mRefreshState; 135 136 uint8_t mPlaylistHash[16]; 137 138 sp<ATSParser> mTSParser; 139 140 bool mFirstPTSValid; 141 uint64_t mFirstPTS; 142 int64_t mFirstTimeUs; 143 int64_t mAbsoluteTimeAnchorUs; 144 sp<AnotherPacketSource> mVideoBuffer; 145 146 // Stores the initialization vector to decrypt the next block of cipher text, which can 147 // either be derived from the sequence number, read from the manifest, or copied from 148 // the last block of cipher text (cipher-block chaining). 149 unsigned char mAESInitVec[16]; 150 151 // Set first to true if decrypting the first segment of a playlist segment. When 152 // first is true, reset the initialization vector based on the available 153 // information in the manifest; otherwise, use the initialization vector as 154 // updated by the last call to AES_cbc_encrypt. 155 // 156 // For the input to decrypt correctly, decryptBuffer must be called on 157 // consecutive byte ranges on block boundaries, e.g. 0..15, 16..47, 48..63, 158 // and so on. 159 status_t decryptBuffer( 160 size_t playlistIndex, const sp<ABuffer> &buffer, 161 bool first = true); 162 status_t checkDecryptPadding(const sp<ABuffer> &buffer); 163 164 void postMonitorQueue(int64_t delayUs = 0, int64_t minDelayUs = 0); 165 void cancelMonitorQueue(); 166 167 int64_t delayUsToRefreshPlaylist() const; 168 status_t refreshPlaylist(); 169 170 // Returns the media time in us of the segment specified by seqNumber. 171 // This is computed by summing the durations of all segments before it. 172 int64_t getSegmentStartTimeUs(int32_t seqNumber) const; 173 174 status_t onStart(const sp<AMessage> &msg); 175 void onPause(); 176 void onStop(const sp<AMessage> &msg); 177 void onMonitorQueue(); 178 void onDownloadNext(); 179 180 // Resume a fetcher to continue until the stopping point stored in msg. 181 status_t onResumeUntil(const sp<AMessage> &msg); 182 183 const sp<ABuffer> &setAccessUnitProperties( 184 const sp<ABuffer> &accessUnit, 185 const sp<AnotherPacketSource> &source, 186 bool discard = false); 187 status_t extractAndQueueAccessUnitsFromTs(const sp<ABuffer> &buffer); 188 189 status_t extractAndQueueAccessUnits( 190 const sp<ABuffer> &buffer, const sp<AMessage> &itemMeta); 191 192 void notifyError(status_t err); 193 194 void queueDiscontinuity( 195 ATSParser::DiscontinuityType type, const sp<AMessage> &extra); 196 197 int32_t getSeqNumberWithAnchorTime(int64_t anchorTimeUs) const; 198 int32_t getSeqNumberForDiscontinuity(size_t discontinuitySeq) const; 199 int32_t getSeqNumberForTime(int64_t timeUs) const; 200 201 void updateDuration(); 202 203 // Before resuming a fetcher in onResume, check the remaining duration is longer than that 204 // returned by resumeThreshold. 205 int64_t resumeThreshold(const sp<AMessage> &msg); 206 207 DISALLOW_EVIL_CONSTRUCTORS(PlaylistFetcher); 208}; 209 210} // namespace android 211 212#endif // PLAYLIST_FETCHER_H_ 213 214