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