GenericSource.h revision 29b7dcf6d3cdb97103467dc8106151c6260c239a
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 GENERIC_SOURCE_H_ 18 19#define GENERIC_SOURCE_H_ 20 21#include "NuPlayer.h" 22#include "NuPlayerSource.h" 23 24#include "ATSParser.h" 25 26#include <media/mediaplayer.h> 27 28namespace android { 29 30class DecryptHandle; 31class DrmManagerClient; 32struct AnotherPacketSource; 33struct ARTSPController; 34class DataSource; 35class IDataSource; 36struct IMediaHTTPService; 37struct MediaSource; 38class MediaBuffer; 39struct NuCachedSource2; 40 41struct NuPlayer::GenericSource : public NuPlayer::Source { 42 GenericSource(const sp<AMessage> ¬ify, bool uidValid, uid_t uid); 43 44 status_t setDataSource( 45 const sp<IMediaHTTPService> &httpService, 46 const char *url, 47 const KeyedVector<String8, String8> *headers); 48 49 status_t setDataSource(int fd, int64_t offset, int64_t length); 50 51 status_t setDataSource(const sp<DataSource>& dataSource); 52 53 virtual void prepareAsync(); 54 55 virtual void start(); 56 virtual void stop(); 57 virtual void pause(); 58 virtual void resume(); 59 60 virtual void disconnect(); 61 62 virtual status_t feedMoreTSData(); 63 64 virtual sp<MetaData> getFileFormatMeta() const; 65 66 virtual status_t dequeueAccessUnit(bool audio, sp<ABuffer> *accessUnit); 67 68 virtual status_t getDuration(int64_t *durationUs); 69 virtual size_t getTrackCount() const; 70 virtual sp<AMessage> getTrackInfo(size_t trackIndex) const; 71 virtual ssize_t getSelectedTrack(media_track_type type) const; 72 virtual status_t selectTrack(size_t trackIndex, bool select, int64_t timeUs); 73 virtual status_t seekTo(int64_t seekTimeUs, bool precise = false) override; 74 75 virtual status_t setBuffers(bool audio, Vector<MediaBuffer *> &buffers); 76 77 virtual bool isStreaming() const; 78 79 virtual void setOffloadAudio(bool offload); 80 81protected: 82 virtual ~GenericSource(); 83 84 virtual void onMessageReceived(const sp<AMessage> &msg); 85 86 virtual sp<MetaData> getFormatMeta(bool audio); 87 88private: 89 enum { 90 kWhatPrepareAsync, 91 kWhatFetchSubtitleData, 92 kWhatFetchTimedTextData, 93 kWhatSendSubtitleData, 94 kWhatSendGlobalTimedTextData, 95 kWhatSendTimedTextData, 96 kWhatChangeAVSource, 97 kWhatPollBuffering, 98 kWhatGetFormat, 99 kWhatGetSelectedTrack, 100 kWhatSelectTrack, 101 kWhatSeek, 102 kWhatReadBuffer, 103 kWhatStart, 104 kWhatResume, 105 kWhatSecureDecodersInstantiated, 106 }; 107 108 struct Track { 109 size_t mIndex; 110 sp<IMediaSource> mSource; 111 sp<AnotherPacketSource> mPackets; 112 }; 113 114 // Helper to monitor buffering status. The polling happens every second. 115 // When necessary, it will send out buffering events to the player. 116 struct BufferingMonitor : public AHandler { 117 public: 118 explicit BufferingMonitor(const sp<AMessage> ¬ify); 119 120 // Set up state. 121 void prepare(const sp<NuCachedSource2> &cachedSource, 122 int64_t durationUs, 123 int64_t bitrate, 124 bool isStreaming); 125 // Stop and reset buffering monitor. 126 void stop(); 127 // Cancel the current monitor task. 128 void cancelPollBuffering(); 129 // Restart the monitor task. 130 void restartPollBuffering(); 131 // Stop buffering task and send out corresponding events. 132 void stopBufferingIfNecessary(); 133 // Make sure data source is getting data. 134 void ensureCacheIsFetching(); 135 // Update media time of just extracted buffer from data source. 136 void updateQueuedTime(bool isAudio, int64_t timeUs); 137 138 // Set the offload mode. 139 void setOffloadAudio(bool offload); 140 // Update media time of last dequeued buffer which is sent to the decoder. 141 void updateDequeuedBufferTime(int64_t mediaUs); 142 143 protected: 144 virtual ~BufferingMonitor(); 145 virtual void onMessageReceived(const sp<AMessage> &msg); 146 147 private: 148 enum { 149 kWhatPollBuffering, 150 }; 151 152 sp<AMessage> mNotify; 153 154 sp<NuCachedSource2> mCachedSource; 155 int64_t mDurationUs; 156 int64_t mBitrate; 157 bool mIsStreaming; 158 159 int64_t mAudioTimeUs; 160 int64_t mVideoTimeUs; 161 int32_t mPollBufferingGeneration; 162 bool mPrepareBuffering; 163 bool mBuffering; 164 int32_t mPrevBufferPercentage; 165 166 mutable Mutex mLock; 167 168 bool mOffloadAudio; 169 int64_t mFirstDequeuedBufferRealUs; 170 int64_t mFirstDequeuedBufferMediaUs; 171 int64_t mlastDequeuedBufferMediaUs; 172 173 void prepare_l(const sp<NuCachedSource2> &cachedSource, 174 int64_t durationUs, 175 int64_t bitrate, 176 bool isStreaming); 177 void cancelPollBuffering_l(); 178 void notifyBufferingUpdate_l(int32_t percentage); 179 void startBufferingIfNecessary_l(); 180 void stopBufferingIfNecessary_l(); 181 void sendCacheStats_l(); 182 void ensureCacheIsFetching_l(); 183 int64_t getLastReadPosition_l(); 184 void onPollBuffering_l(); 185 void schedulePollBuffering_l(); 186 }; 187 188 Vector<sp<IMediaSource> > mSources; 189 Track mAudioTrack; 190 int64_t mAudioTimeUs; 191 int64_t mAudioLastDequeueTimeUs; 192 Track mVideoTrack; 193 int64_t mVideoTimeUs; 194 int64_t mVideoLastDequeueTimeUs; 195 Track mSubtitleTrack; 196 Track mTimedTextTrack; 197 198 int32_t mFetchSubtitleDataGeneration; 199 int32_t mFetchTimedTextDataGeneration; 200 int64_t mDurationUs; 201 bool mAudioIsVorbis; 202 bool mIsSecure; 203 bool mIsStreaming; 204 bool mUIDValid; 205 uid_t mUID; 206 sp<IMediaHTTPService> mHTTPService; 207 AString mUri; 208 KeyedVector<String8, String8> mUriHeaders; 209 int mFd; 210 int64_t mOffset; 211 int64_t mLength; 212 213 sp<DataSource> mDataSource; 214 sp<NuCachedSource2> mCachedSource; 215 sp<DataSource> mHttpSource; 216 sp<MetaData> mFileMeta; 217 DrmManagerClient *mDrmManagerClient; 218 sp<DecryptHandle> mDecryptHandle; 219 bool mStarted; 220 bool mStopRead; 221 int64_t mBitrate; 222 sp<BufferingMonitor> mBufferingMonitor; 223 uint32_t mPendingReadBufferTypes; 224 sp<ABuffer> mGlobalTimedText; 225 226 mutable Mutex mReadBufferLock; 227 mutable Mutex mDisconnectLock; 228 229 sp<ALooper> mLooper; 230 sp<ALooper> mBufferingMonitorLooper; 231 232 void resetDataSource(); 233 234 status_t initFromDataSource(); 235 void checkDrmStatus(const sp<DataSource>& dataSource); 236 int64_t getLastReadPosition(); 237 void setDrmPlaybackStatusIfNeeded(int playbackStatus, int64_t position); 238 239 void notifyPreparedAndCleanup(status_t err); 240 void onSecureDecodersInstantiated(status_t err); 241 void finishPrepareAsync(); 242 status_t startSources(); 243 244 void onGetFormatMeta(const sp<AMessage>& msg) const; 245 sp<MetaData> doGetFormatMeta(bool audio) const; 246 247 void onGetSelectedTrack(const sp<AMessage>& msg) const; 248 ssize_t doGetSelectedTrack(media_track_type type) const; 249 250 void onSelectTrack(const sp<AMessage>& msg); 251 status_t doSelectTrack(size_t trackIndex, bool select, int64_t timeUs); 252 253 void onSeek(const sp<AMessage>& msg); 254 status_t doSeek(int64_t seekTimeUs, bool precise); 255 256 void onPrepareAsync(); 257 258 void fetchTextData( 259 uint32_t what, media_track_type type, 260 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg); 261 262 void sendGlobalTextData( 263 uint32_t what, 264 int32_t curGen, sp<AMessage> msg); 265 266 void sendTextData( 267 uint32_t what, media_track_type type, 268 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg); 269 270 sp<ABuffer> mediaBufferToABuffer( 271 MediaBuffer *mbuf, 272 media_track_type trackType); 273 274 void postReadBuffer(media_track_type trackType); 275 void onReadBuffer(const sp<AMessage>& msg); 276 // |precise| is a modifier of |seekTimeUs|. 277 // When |precise| is true, the buffer read shall include an item indicating skipping 278 // rendering all buffers with timestamp earlier than |seekTimeUs|. 279 // When |precise| is false, the buffer read will not include the item as above in order 280 // to facilitate fast seek operation. 281 void readBuffer( 282 media_track_type trackType, 283 int64_t seekTimeUs = -1ll, bool precise = false, 284 int64_t *actualTimeUs = NULL, bool formatChange = false); 285 286 void queueDiscontinuityIfNeeded( 287 bool seeking, bool formatChange, media_track_type trackType, Track *track); 288 289 DISALLOW_EVIL_CONSTRUCTORS(GenericSource); 290}; 291 292} // namespace android 293 294#endif // GENERIC_SOURCE_H_ 295