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