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 26 namespace android { 27 28 struct ABuffer; 29 struct AnotherPacketSource; 30 struct DataSource; 31 struct HTTPBase; 32 struct LiveDataSource; 33 struct M3UParser; 34 struct String8; 35 36 struct 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, 61 int64_t minStartTimeUs = 0ll /* start after this timestamp */, 62 int32_t startSeqNumberHint = -1 /* try starting at this sequence number */); 63 64 void pauseAsync(); 65 66 void stopAsync(bool selfTriggered = false); 67 68 void resumeUntilAsync(const sp<AMessage> ¶ms); 69 70 protected: 71 virtual ~PlaylistFetcher(); 72 virtual void onMessageReceived(const sp<AMessage> &msg); 73 74 private: 75 enum { 76 kMaxNumRetries = 5, 77 }; 78 79 enum { 80 kWhatStart = 'strt', 81 kWhatPause = 'paus', 82 kWhatStop = 'stop', 83 kWhatMonitorQueue = 'moni', 84 kWhatResumeUntil = 'rsme', 85 kWhatDownloadNext = 'dlnx', 86 }; 87 88 static const int64_t kMinBufferedDurationUs; 89 static const int64_t kMaxMonitorDelayUs; 90 static const int32_t kDownloadBlockSize; 91 static const int32_t kNumSkipFrames; 92 93 static bool bufferStartsWithTsSyncByte(const sp<ABuffer>& buffer); 94 95 // notifications to mSession 96 sp<AMessage> mNotify; 97 sp<AMessage> mStartTimeUsNotify; 98 99 sp<LiveSession> mSession; 100 AString mURI; 101 102 uint32_t mStreamTypeMask; 103 int64_t mStartTimeUs; 104 int64_t mMinStartTimeUs; // start fetching no earlier than this value 105 sp<AMessage> mStopParams; // message containing the latest timestamps we should fetch. 106 107 KeyedVector<LiveSession::StreamType, sp<AnotherPacketSource> > 108 mPacketSources; 109 110 KeyedVector<AString, sp<ABuffer> > mAESKeyForURI; 111 112 int64_t mLastPlaylistFetchTimeUs; 113 sp<M3UParser> mPlaylist; 114 int32_t mSeqNumber; 115 int32_t mNumRetries; 116 bool mStartup; 117 bool mPrepared; 118 int64_t mNextPTSTimeUs; 119 120 int32_t mMonitorQueueGeneration; 121 122 enum RefreshState { 123 INITIAL_MINIMUM_RELOAD_DELAY, 124 FIRST_UNCHANGED_RELOAD_ATTEMPT, 125 SECOND_UNCHANGED_RELOAD_ATTEMPT, 126 THIRD_UNCHANGED_RELOAD_ATTEMPT 127 }; 128 RefreshState mRefreshState; 129 130 uint8_t mPlaylistHash[16]; 131 132 sp<ATSParser> mTSParser; 133 134 bool mFirstPTSValid; 135 uint64_t mFirstPTS; 136 int64_t mAbsoluteTimeAnchorUs; 137 138 // Stores the initialization vector to decrypt the next block of cipher text, which can 139 // either be derived from the sequence number, read from the manifest, or copied from 140 // the last block of cipher text (cipher-block chaining). 141 unsigned char mAESInitVec[16]; 142 143 // Set first to true if decrypting the first segment of a playlist segment. When 144 // first is true, reset the initialization vector based on the available 145 // information in the manifest; otherwise, use the initialization vector as 146 // updated by the last call to AES_cbc_encrypt. 147 // 148 // For the input to decrypt correctly, decryptBuffer must be called on 149 // consecutive byte ranges on block boundaries, e.g. 0..15, 16..47, 48..63, 150 // and so on. 151 status_t decryptBuffer( 152 size_t playlistIndex, const sp<ABuffer> &buffer, 153 bool first = true); 154 status_t checkDecryptPadding(const sp<ABuffer> &buffer); 155 156 void postMonitorQueue(int64_t delayUs = 0, int64_t minDelayUs = 0); 157 void cancelMonitorQueue(); 158 159 int64_t delayUsToRefreshPlaylist() const; 160 status_t refreshPlaylist(); 161 162 // Returns the media time in us of the segment specified by seqNumber. 163 // This is computed by summing the durations of all segments before it. 164 int64_t getSegmentStartTimeUs(int32_t seqNumber) const; 165 166 status_t onStart(const sp<AMessage> &msg); 167 void onPause(); 168 void onStop(const sp<AMessage> &msg); 169 void onMonitorQueue(); 170 void onDownloadNext(); 171 172 // Resume a fetcher to continue until the stopping point stored in msg. 173 status_t onResumeUntil(const sp<AMessage> &msg); 174 175 status_t extractAndQueueAccessUnitsFromTs(const sp<ABuffer> &buffer); 176 177 status_t extractAndQueueAccessUnits( 178 const sp<ABuffer> &buffer, const sp<AMessage> &itemMeta); 179 180 void notifyError(status_t err); 181 182 void queueDiscontinuity( 183 ATSParser::DiscontinuityType type, const sp<AMessage> &extra); 184 185 int32_t getSeqNumberForTime(int64_t timeUs) const; 186 187 void updateDuration(); 188 189 // Before resuming a fetcher in onResume, check the remaining duration is longer than that 190 // returned by resumeThreshold. 191 int64_t resumeThreshold(const sp<AMessage> &msg); 192 193 DISALLOW_EVIL_CONSTRUCTORS(PlaylistFetcher); 194 }; 195 196 } // namespace android 197 198 #endif // PLAYLIST_FETCHER_H_ 199 200