1 /* 2 * Copyright (C) 2010 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 NUPLAYER_RENDERER_H_ 18 19 #define NUPLAYER_RENDERER_H_ 20 21 #include <atomic> 22 23 #include <media/AudioResamplerPublic.h> 24 #include <media/AVSyncSettings.h> 25 26 #include "NuPlayer.h" 27 28 namespace android { 29 30 class AWakeLock; 31 struct MediaClock; 32 class MediaCodecBuffer; 33 struct VideoFrameSchedulerBase; 34 35 struct NuPlayer::Renderer : public AHandler { 36 enum Flags { 37 FLAG_REAL_TIME = 1, 38 FLAG_OFFLOAD_AUDIO = 2, 39 }; 40 Renderer(const sp<MediaPlayerBase::AudioSink> &sink, 41 const sp<MediaClock> &mediaClock, 42 const sp<AMessage> ¬ify, 43 uint32_t flags = 0); 44 45 static size_t AudioSinkCallback( 46 MediaPlayerBase::AudioSink *audioSink, 47 void *data, size_t size, void *me, 48 MediaPlayerBase::AudioSink::cb_event_t event); 49 50 void queueBuffer( 51 bool audio, 52 const sp<MediaCodecBuffer> &buffer, 53 const sp<AMessage> ¬ifyConsumed); 54 55 void queueEOS(bool audio, status_t finalResult); 56 57 status_t setPlaybackSettings(const AudioPlaybackRate &rate /* sanitized */); 58 status_t getPlaybackSettings(AudioPlaybackRate *rate /* nonnull */); 59 status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint); 60 status_t getSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */); 61 62 void flush(bool audio, bool notifyComplete); 63 64 void signalTimeDiscontinuity(); 65 66 void signalDisableOffloadAudio(); 67 void signalEnableOffloadAudio(); 68 69 void pause(); 70 void resume(); 71 72 void setVideoFrameRate(float fps); 73 74 status_t getCurrentPosition(int64_t *mediaUs); 75 int64_t getVideoLateByUs(); 76 77 status_t openAudioSink( 78 const sp<AMessage> &format, 79 bool offloadOnly, 80 bool hasVideo, 81 uint32_t flags, 82 bool *isOffloaded, 83 bool isStreaming); 84 void closeAudioSink(); 85 86 // re-open audio sink after all pending audio buffers played. 87 void changeAudioFormat( 88 const sp<AMessage> &format, 89 bool offloadOnly, 90 bool hasVideo, 91 uint32_t flags, 92 bool isStreaming, 93 const sp<AMessage> ¬ify); 94 95 enum { 96 kWhatEOS = 'eos ', 97 kWhatFlushComplete = 'fluC', 98 kWhatPosition = 'posi', 99 kWhatVideoRenderingStart = 'vdrd', 100 kWhatMediaRenderingStart = 'mdrd', 101 kWhatAudioTearDown = 'adTD', 102 kWhatAudioOffloadPauseTimeout = 'aOPT', 103 }; 104 105 enum AudioTearDownReason { 106 kDueToError = 0, // Could restart with either offload or non-offload. 107 kDueToTimeout, 108 kForceNonOffload, // Restart only with non-offload. 109 }; 110 111 protected: 112 virtual ~Renderer(); 113 114 virtual void onMessageReceived(const sp<AMessage> &msg); 115 116 private: 117 enum { 118 kWhatDrainAudioQueue = 'draA', 119 kWhatDrainVideoQueue = 'draV', 120 kWhatPostDrainVideoQueue = 'pDVQ', 121 kWhatQueueBuffer = 'queB', 122 kWhatQueueEOS = 'qEOS', 123 kWhatConfigPlayback = 'cfPB', 124 kWhatConfigSync = 'cfSy', 125 kWhatGetPlaybackSettings = 'gPbS', 126 kWhatGetSyncSettings = 'gSyS', 127 kWhatFlush = 'flus', 128 kWhatPause = 'paus', 129 kWhatResume = 'resm', 130 kWhatOpenAudioSink = 'opnA', 131 kWhatCloseAudioSink = 'clsA', 132 kWhatChangeAudioFormat = 'chgA', 133 kWhatStopAudioSink = 'stpA', 134 kWhatDisableOffloadAudio = 'noOA', 135 kWhatEnableOffloadAudio = 'enOA', 136 kWhatSetVideoFrameRate = 'sVFR', 137 }; 138 139 // if mBuffer != nullptr, it's a buffer containing real data. 140 // else if mNotifyConsumed == nullptr, it's EOS. 141 // else it's a tag for re-opening audio sink in different format. 142 struct QueueEntry { 143 sp<MediaCodecBuffer> mBuffer; 144 sp<AMessage> mMeta; 145 sp<AMessage> mNotifyConsumed; 146 size_t mOffset; 147 status_t mFinalResult; 148 int32_t mBufferOrdinal; 149 }; 150 151 static const int64_t kMinPositionUpdateDelayUs; 152 153 sp<MediaPlayerBase::AudioSink> mAudioSink; 154 bool mUseVirtualAudioSink; 155 sp<AMessage> mNotify; 156 Mutex mLock; 157 uint32_t mFlags; 158 List<QueueEntry> mAudioQueue; 159 List<QueueEntry> mVideoQueue; 160 uint32_t mNumFramesWritten; 161 sp<VideoFrameSchedulerBase> mVideoScheduler; 162 163 bool mDrainAudioQueuePending; 164 bool mDrainVideoQueuePending; 165 int32_t mAudioQueueGeneration; 166 int32_t mVideoQueueGeneration; 167 int32_t mAudioDrainGeneration; 168 int32_t mVideoDrainGeneration; 169 int32_t mAudioEOSGeneration; 170 171 const sp<MediaClock> mMediaClock; 172 float mPlaybackRate; // audio track rate 173 174 AudioPlaybackRate mPlaybackSettings; 175 AVSyncSettings mSyncSettings; 176 float mVideoFpsHint; 177 178 int64_t mAudioFirstAnchorTimeMediaUs; 179 int64_t mAnchorTimeMediaUs; 180 int64_t mAnchorNumFramesWritten; 181 int64_t mVideoLateByUs; 182 int64_t mNextVideoTimeMediaUs; 183 bool mHasAudio; 184 bool mHasVideo; 185 186 bool mNotifyCompleteAudio; 187 bool mNotifyCompleteVideo; 188 189 bool mSyncQueues; 190 191 // modified on only renderer's thread. 192 bool mPaused; 193 int64_t mPauseDrainAudioAllowedUs; // time when we can drain/deliver audio in pause mode. 194 195 bool mVideoSampleReceived; 196 bool mVideoRenderingStarted; 197 int32_t mVideoRenderingStartGeneration; 198 int32_t mAudioRenderingStartGeneration; 199 bool mRenderingDataDelivered; 200 201 int64_t mNextAudioClockUpdateTimeUs; 202 // the media timestamp of last audio sample right before EOS. 203 int64_t mLastAudioMediaTimeUs; 204 205 int32_t mAudioOffloadPauseTimeoutGeneration; 206 bool mAudioTornDown; 207 audio_offload_info_t mCurrentOffloadInfo; 208 209 struct PcmInfo { 210 audio_channel_mask_t mChannelMask; 211 audio_output_flags_t mFlags; 212 audio_format_t mFormat; 213 int32_t mNumChannels; 214 int32_t mSampleRate; 215 }; 216 PcmInfo mCurrentPcmInfo; 217 static const PcmInfo AUDIO_PCMINFO_INITIALIZER; 218 219 int32_t mTotalBuffersQueued; 220 int32_t mLastAudioBufferDrained; 221 bool mUseAudioCallback; 222 223 sp<AWakeLock> mWakeLock; 224 225 std::atomic_flag mSyncFlag = ATOMIC_FLAG_INIT; 226 Mutex mSyncLock; 227 Condition mSyncCondition; 228 int64_t mSyncCount{0}; 229 230 status_t getCurrentPositionOnLooper(int64_t *mediaUs); 231 status_t getCurrentPositionOnLooper( 232 int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false); 233 bool getCurrentPositionIfPaused_l(int64_t *mediaUs); 234 status_t getCurrentPositionFromAnchor( 235 int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false); 236 237 void notifyEOSCallback(); 238 size_t fillAudioBuffer(void *buffer, size_t size); 239 240 bool onDrainAudioQueue(); 241 void drainAudioQueueUntilLastEOS(); 242 int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs); 243 void postDrainAudioQueue_l(int64_t delayUs = 0); 244 245 void clearAnchorTime(); 246 void clearAudioFirstAnchorTime_l(); 247 void setAudioFirstAnchorTimeIfNeeded_l(int64_t mediaUs); 248 void setVideoLateByUs(int64_t lateUs); 249 250 void onNewAudioMediaTime(int64_t mediaTimeUs); 251 int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs); 252 253 void onDrainVideoQueue(); 254 void postDrainVideoQueue(); 255 256 void prepareForMediaRenderingStart_l(); 257 void notifyIfMediaRenderingStarted_l(); 258 259 void onQueueBuffer(const sp<AMessage> &msg); 260 void onQueueEOS(const sp<AMessage> &msg); 261 void onFlush(const sp<AMessage> &msg); 262 void onAudioSinkChanged(); 263 void onDisableOffloadAudio(); 264 void onEnableOffloadAudio(); 265 status_t onConfigPlayback(const AudioPlaybackRate &rate /* sanitized */); 266 status_t onGetPlaybackSettings(AudioPlaybackRate *rate /* nonnull */); 267 status_t onConfigSync(const AVSyncSettings &sync, float videoFpsHint); 268 status_t onGetSyncSettings(AVSyncSettings *sync /* nonnull */, float *videoFps /* nonnull */); 269 270 void onPause(); 271 void onResume(); 272 void onSetVideoFrameRate(float fps); 273 int32_t getQueueGeneration(bool audio); 274 int32_t getDrainGeneration(bool audio); 275 bool getSyncQueues(); 276 void onAudioTearDown(AudioTearDownReason reason); 277 status_t onOpenAudioSink( 278 const sp<AMessage> &format, 279 bool offloadOnly, 280 bool hasVideo, 281 uint32_t flags, 282 bool isStreaming); 283 void onCloseAudioSink(); 284 void onChangeAudioFormat(const sp<AMessage> &meta, const sp<AMessage> ¬ify); 285 286 void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0); 287 void notifyEOS_l(bool audio, status_t finalResult, int64_t delayUs = 0); 288 void notifyFlushComplete(bool audio); 289 void notifyPosition(); 290 void notifyVideoLateBy(int64_t lateByUs); 291 void notifyVideoRenderingStart(); 292 void notifyAudioTearDown(AudioTearDownReason reason); 293 294 void flushQueue(List<QueueEntry> *queue); 295 bool dropBufferIfStale(bool audio, const sp<AMessage> &msg); 296 void syncQueuesDone_l(); 297 offloadingAudioRenderer298 bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; } 299 300 void startAudioOffloadPauseTimeout(); 301 void cancelAudioOffloadPauseTimeout(); 302 303 int64_t getDurationUsIfPlayedAtSampleRate(uint32_t numFrames); 304 305 DISALLOW_EVIL_CONSTRUCTORS(Renderer); 306 }; 307 308 } // namespace android 309 310 #endif // NUPLAYER_RENDERER_H_ 311