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