NuPlayerRenderer.h revision f0e83644637bd05852c244df481f21a0d435ff66
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 "NuPlayer.h" 22 23namespace android { 24 25struct ABuffer; 26struct VideoFrameScheduler; 27 28struct NuPlayer::Renderer : public AHandler { 29 enum Flags { 30 FLAG_REAL_TIME = 1, 31 FLAG_OFFLOAD_AUDIO = 2, 32 }; 33 Renderer(const sp<MediaPlayerBase::AudioSink> &sink, 34 const sp<AMessage> ¬ify, 35 uint32_t flags = 0); 36 37 static size_t AudioSinkCallback( 38 MediaPlayerBase::AudioSink *audioSink, 39 void *data, size_t size, void *me, 40 MediaPlayerBase::AudioSink::cb_event_t event); 41 42 void queueBuffer( 43 bool audio, 44 const sp<ABuffer> &buffer, 45 const sp<AMessage> ¬ifyConsumed); 46 47 void queueEOS(bool audio, status_t finalResult); 48 49 void flush(bool audio, bool notifyComplete); 50 51 void signalTimeDiscontinuity(); 52 53 void signalAudioSinkChanged(); 54 55 void signalDisableOffloadAudio(); 56 void signalEnableOffloadAudio(); 57 58 void pause(); 59 void resume(); 60 61 void setVideoFrameRate(float fps); 62 63 // Following setters and getters are protected by mTimeLock. 64 status_t getCurrentPosition(int64_t *mediaUs); 65 status_t getCurrentPosition( 66 int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo = false); 67 void setHasMedia(bool audio); 68 void setAudioFirstAnchorTime(int64_t mediaUs); 69 void setAudioFirstAnchorTimeIfNeeded(int64_t mediaUs); 70 void setAnchorTime( 71 int64_t mediaUs, int64_t realUs, int64_t numFramesWritten = -1, bool resume = false); 72 void setVideoLateByUs(int64_t lateUs); 73 int64_t getVideoLateByUs(); 74 void setPauseStartedTimeRealUs(int64_t realUs); 75 76 status_t openAudioSink( 77 const sp<AMessage> &format, 78 bool offloadOnly, 79 bool hasVideo, 80 uint32_t flags, 81 bool *isOffloaded); 82 void closeAudioSink(); 83 84 enum { 85 kWhatEOS = 'eos ', 86 kWhatFlushComplete = 'fluC', 87 kWhatPosition = 'posi', 88 kWhatVideoRenderingStart = 'vdrd', 89 kWhatMediaRenderingStart = 'mdrd', 90 kWhatAudioOffloadTearDown = 'aOTD', 91 kWhatAudioOffloadPauseTimeout = 'aOPT', 92 }; 93 94 enum AudioOffloadTearDownReason { 95 kDueToError = 0, 96 kDueToTimeout, 97 }; 98 99protected: 100 virtual ~Renderer(); 101 102 virtual void onMessageReceived(const sp<AMessage> &msg); 103 104private: 105 enum { 106 kWhatDrainAudioQueue = 'draA', 107 kWhatDrainVideoQueue = 'draV', 108 kWhatPostDrainVideoQueue = 'pDVQ', 109 kWhatQueueBuffer = 'queB', 110 kWhatQueueEOS = 'qEOS', 111 kWhatFlush = 'flus', 112 kWhatAudioSinkChanged = 'auSC', 113 kWhatPause = 'paus', 114 kWhatResume = 'resm', 115 kWhatOpenAudioSink = 'opnA', 116 kWhatCloseAudioSink = 'clsA', 117 kWhatStopAudioSink = 'stpA', 118 kWhatDisableOffloadAudio = 'noOA', 119 kWhatEnableOffloadAudio = 'enOA', 120 kWhatSetVideoFrameRate = 'sVFR', 121 }; 122 123 struct QueueEntry { 124 sp<ABuffer> mBuffer; 125 sp<AMessage> mNotifyConsumed; 126 size_t mOffset; 127 status_t mFinalResult; 128 int32_t mBufferOrdinal; 129 }; 130 131 static const int64_t kMinPositionUpdateDelayUs; 132 133 sp<MediaPlayerBase::AudioSink> mAudioSink; 134 sp<AMessage> mNotify; 135 Mutex mLock; 136 uint32_t mFlags; 137 List<QueueEntry> mAudioQueue; 138 List<QueueEntry> mVideoQueue; 139 uint32_t mNumFramesWritten; 140 sp<VideoFrameScheduler> mVideoScheduler; 141 142 bool mDrainAudioQueuePending; 143 bool mDrainVideoQueuePending; 144 int32_t mAudioQueueGeneration; 145 int32_t mVideoQueueGeneration; 146 147 Mutex mTimeLock; 148 // |mTimeLock| protects the following 7 member vars that are related to time. 149 // Note: those members are only written on Renderer thread, so reading on Renderer thread 150 // doesn't need to be protected. Otherwise accessing those members must be protected by 151 // |mTimeLock|. 152 // TODO: move those members to a seperated media clock class. 153 int64_t mAudioFirstAnchorTimeMediaUs; 154 int64_t mAnchorTimeMediaUs; 155 int64_t mAnchorTimeRealUs; 156 int64_t mAnchorNumFramesWritten; 157 int64_t mAnchorMaxMediaUs; 158 int64_t mVideoLateByUs; 159 bool mHasAudio; 160 bool mHasVideo; 161 int64_t mPauseStartedTimeRealUs; 162 163 Mutex mFlushLock; // protects the following 2 member vars. 164 bool mFlushingAudio; 165 bool mFlushingVideo; 166 bool mNotifyCompleteAudio; 167 bool mNotifyCompleteVideo; 168 169 bool mSyncQueues; 170 171 bool mPaused; 172 bool mVideoSampleReceived; 173 bool mVideoRenderingStarted; 174 int32_t mVideoRenderingStartGeneration; 175 int32_t mAudioRenderingStartGeneration; 176 177 int64_t mLastPositionUpdateUs; 178 179 int32_t mAudioOffloadPauseTimeoutGeneration; 180 bool mAudioOffloadTornDown; 181 audio_offload_info_t mCurrentOffloadInfo; 182 183 struct PcmInfo { 184 audio_channel_mask_t mChannelMask; 185 audio_output_flags_t mFlags; 186 audio_format_t mFormat; 187 int32_t mNumChannels; 188 int32_t mSampleRate; 189 }; 190 PcmInfo mCurrentPcmInfo; 191 static const PcmInfo AUDIO_PCMINFO_INITIALIZER; 192 193 int32_t mTotalBuffersQueued; 194 int32_t mLastAudioBufferDrained; 195 196 197 size_t fillAudioBuffer(void *buffer, size_t size); 198 199 bool onDrainAudioQueue(); 200 int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs); 201 int64_t getPlayedOutAudioDurationUs(int64_t nowUs); 202 void postDrainAudioQueue_l(int64_t delayUs = 0); 203 204 void onNewAudioMediaTime(int64_t mediaTimeUs); 205 int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs); 206 207 void onDrainVideoQueue(); 208 void postDrainVideoQueue(); 209 210 void prepareForMediaRenderingStart(); 211 void notifyIfMediaRenderingStarted(); 212 213 void onQueueBuffer(const sp<AMessage> &msg); 214 void onQueueEOS(const sp<AMessage> &msg); 215 void onFlush(const sp<AMessage> &msg); 216 void onAudioSinkChanged(); 217 void onDisableOffloadAudio(); 218 void onEnableOffloadAudio(); 219 void onPause(); 220 void onResume(); 221 void onSetVideoFrameRate(float fps); 222 void onAudioOffloadTearDown(AudioOffloadTearDownReason reason); 223 status_t onOpenAudioSink( 224 const sp<AMessage> &format, 225 bool offloadOnly, 226 bool hasVideo, 227 uint32_t flags); 228 void onCloseAudioSink(); 229 230 void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0); 231 void notifyFlushComplete(bool audio); 232 void notifyPosition(); 233 void notifyVideoLateBy(int64_t lateByUs); 234 void notifyVideoRenderingStart(); 235 void notifyAudioOffloadTearDown(); 236 237 void flushQueue(List<QueueEntry> *queue); 238 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg); 239 void syncQueuesDone_l(); 240 241 bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; } 242 243 void startAudioOffloadPauseTimeout(); 244 void cancelAudioOffloadPauseTimeout(); 245 246 DISALLOW_EVIL_CONSTRUCTORS(Renderer); 247}; 248 249} // namespace android 250 251#endif // NUPLAYER_RENDERER_H_ 252