PlaylistFetcher.h revision 2cd94583868b775a548233a4f7cd1d988fc6344f
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 }; 47 48 PlaylistFetcher( 49 const sp<AMessage> ¬ify, 50 const sp<LiveSession> &session, 51 const char *uri); 52 53 sp<DataSource> getDataSource(); 54 55 void startAsync( 56 const sp<AnotherPacketSource> &audioSource, 57 const sp<AnotherPacketSource> &videoSource, 58 const sp<AnotherPacketSource> &subtitleSource, 59 int64_t startTimeUs = -1ll); 60 61 void pauseAsync(); 62 63 void stopAsync(); 64 65protected: 66 virtual ~PlaylistFetcher(); 67 virtual void onMessageReceived(const sp<AMessage> &msg); 68 69private: 70 enum { 71 kMaxNumRetries = 5, 72 }; 73 74 enum { 75 kWhatStart = 'strt', 76 kWhatPause = 'paus', 77 kWhatStop = 'stop', 78 kWhatMonitorQueue = 'moni', 79 }; 80 81 static const int64_t kMinBufferedDurationUs; 82 static const int64_t kMaxMonitorDelayUs; 83 84 sp<AMessage> mNotify; 85 sp<LiveSession> mSession; 86 AString mURI; 87 88 uint32_t mStreamTypeMask; 89 int64_t mStartTimeUs; 90 91 KeyedVector<LiveSession::StreamType, sp<AnotherPacketSource> > 92 mPacketSources; 93 94 KeyedVector<AString, sp<ABuffer> > mAESKeyForURI; 95 96 int64_t mLastPlaylistFetchTimeUs; 97 sp<M3UParser> mPlaylist; 98 int32_t mSeqNumber; 99 int32_t mNumRetries; 100 bool mStartup; 101 bool mPrepared; 102 int64_t mNextPTSTimeUs; 103 104 int32_t mMonitorQueueGeneration; 105 106 enum RefreshState { 107 INITIAL_MINIMUM_RELOAD_DELAY, 108 FIRST_UNCHANGED_RELOAD_ATTEMPT, 109 SECOND_UNCHANGED_RELOAD_ATTEMPT, 110 THIRD_UNCHANGED_RELOAD_ATTEMPT 111 }; 112 RefreshState mRefreshState; 113 114 uint8_t mPlaylistHash[16]; 115 116 sp<ATSParser> mTSParser; 117 118 bool mFirstPTSValid; 119 uint64_t mFirstPTS; 120 int64_t mAbsoluteTimeAnchorUs; 121 122 // Stores the initialization vector to decrypt the next block of cipher text, which can 123 // either be derived from the sequence number, read from the manifest, or copied from 124 // the last block of cipher text (cipher-block chaining). 125 unsigned char mAESInitVec[16]; 126 127 // Set first to true if decrypting the first segment of a playlist segment. When 128 // first is true, reset the initialization vector based on the available 129 // information in the manifest; otherwise, use the initialization vector as 130 // updated by the last call to AES_cbc_encrypt. 131 // 132 // For the input to decrypt correctly, decryptBuffer must be called on 133 // consecutive byte ranges on block boundaries, e.g. 0..15, 16..47, 48..63, 134 // and so on. 135 status_t decryptBuffer( 136 size_t playlistIndex, const sp<ABuffer> &buffer, 137 bool first = true); 138 status_t checkDecryptPadding(const sp<ABuffer> &buffer); 139 140 void postMonitorQueue(int64_t delayUs = 0, int64_t minDelayUs = 0); 141 void cancelMonitorQueue(); 142 143 int64_t delayUsToRefreshPlaylist() const; 144 status_t refreshPlaylist(); 145 146 // Returns the media time in us of the segment specified by seqNumber. 147 // This is computed by summing the durations of all segments before it. 148 int64_t getSegmentStartTimeUs(int32_t seqNumber) const; 149 150 status_t onStart(const sp<AMessage> &msg); 151 void onPause(); 152 void onStop(); 153 void onMonitorQueue(); 154 void onDownloadNext(); 155 156 status_t extractAndQueueAccessUnits( 157 const sp<ABuffer> &buffer, const sp<AMessage> &itemMeta); 158 159 void notifyError(status_t err); 160 161 void queueDiscontinuity( 162 ATSParser::DiscontinuityType type, const sp<AMessage> &extra); 163 164 int32_t getSeqNumberForTime(int64_t timeUs) const; 165 166 void updateDuration(); 167 168 DISALLOW_EVIL_CONSTRUCTORS(PlaylistFetcher); 169}; 170 171} // namespace android 172 173#endif // PLAYLIST_FETCHER_H_ 174 175