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 28 namespace android { 29 30 class DecryptHandle; 31 struct AnotherPacketSource; 32 struct ARTSPController; 33 class DataSource; 34 class IDataSource; 35 struct IMediaHTTPService; 36 struct MediaSource; 37 class MediaBuffer; 38 struct NuCachedSource2; 39 40 struct 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 97 protected: 98 virtual ~GenericSource(); 99 100 virtual void onMessageReceived(const sp<AMessage> &msg); 101 102 virtual sp<MetaData> getFormatMeta(bool audio); 103 104 private: 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 // Secure codec is required. 226 bool mIsSecure; 227 bool mIsStreaming; 228 bool mUIDValid; 229 uid_t mUID; 230 sp<IMediaHTTPService> mHTTPService; 231 AString mUri; 232 KeyedVector<String8, String8> mUriHeaders; 233 int mFd; 234 int64_t mOffset; 235 int64_t mLength; 236 237 sp<DataSource> mDataSource; 238 sp<NuCachedSource2> mCachedSource; 239 sp<DataSource> mHttpSource; 240 sp<MetaData> mFileMeta; 241 bool mStarted; 242 bool mStopRead; 243 int64_t mBitrate; 244 sp<BufferingMonitor> mBufferingMonitor; 245 uint32_t mPendingReadBufferTypes; 246 sp<ABuffer> mGlobalTimedText; 247 248 mutable Mutex mReadBufferLock; 249 mutable Mutex mDisconnectLock; 250 251 sp<ALooper> mLooper; 252 sp<ALooper> mBufferingMonitorLooper; 253 254 void resetDataSource(); 255 256 status_t initFromDataSource(); 257 int64_t getLastReadPosition(); 258 259 void notifyPreparedAndCleanup(status_t err); 260 void onSecureDecodersInstantiated(status_t err); 261 void finishPrepareAsync(); 262 status_t startSources(); 263 264 void onGetFormatMeta(const sp<AMessage>& msg) const; 265 sp<MetaData> doGetFormatMeta(bool audio) const; 266 267 void onGetTrackInfo(const sp<AMessage>& msg) const; 268 sp<AMessage> doGetTrackInfo(size_t trackIndex) const; 269 270 void onGetSelectedTrack(const sp<AMessage>& msg) const; 271 ssize_t doGetSelectedTrack(media_track_type type) const; 272 273 void onSelectTrack(const sp<AMessage>& msg); 274 status_t doSelectTrack(size_t trackIndex, bool select, int64_t timeUs); 275 276 void onSeek(const sp<AMessage>& msg); 277 status_t doSeek(int64_t seekTimeUs, MediaPlayerSeekMode mode); 278 279 void onPrepareAsync(); 280 281 void fetchTextData( 282 uint32_t what, media_track_type type, 283 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg); 284 285 void sendGlobalTextData( 286 uint32_t what, 287 int32_t curGen, sp<AMessage> msg); 288 289 void sendTextData( 290 uint32_t what, media_track_type type, 291 int32_t curGen, const sp<AnotherPacketSource>& packets, const sp<AMessage>& msg); 292 293 sp<ABuffer> mediaBufferToABuffer( 294 MediaBuffer *mbuf, 295 media_track_type trackType); 296 297 void postReadBuffer(media_track_type trackType); 298 void onReadBuffer(const sp<AMessage>& msg); 299 // When |mode| is MediaPlayerSeekMode::SEEK_CLOSEST, the buffer read shall 300 // include an item indicating skipping rendering all buffers with timestamp 301 // earlier than |seekTimeUs|. 302 // For other modes, the buffer read will not include the item as above in order 303 // to facilitate fast seek operation. 304 void readBuffer( 305 media_track_type trackType, 306 int64_t seekTimeUs = -1ll, 307 MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC, 308 int64_t *actualTimeUs = NULL, bool formatChange = false); 309 310 void queueDiscontinuityIfNeeded( 311 bool seeking, bool formatChange, media_track_type trackType, Track *track); 312 313 // Modular DRM 314 // The source is DRM protected and is prepared for DRM. 315 bool mIsDrmProtected; 316 // releaseDrm has been processed. 317 bool mIsDrmReleased; 318 Vector<String8> mMimes; 319 320 status_t checkDrmInfo(); 321 status_t onPrepareDrm(const sp<AMessage> &msg); 322 status_t onReleaseDrm(); 323 324 DISALLOW_EVIL_CONSTRUCTORS(GenericSource); 325 }; 326 327 } // namespace android 328 329 #endif // GENERIC_SOURCE_H_ 330