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