NuPlayerRenderer.h revision f06060f9544c71ebdc1e0b1b8d73f6cb275e6311
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); 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(int64_t *mediaUs, int64_t nowUs); 66 void setHasMedia(bool audio); 67 void setAudioFirstAnchorTime(int64_t mediaUs); 68 void setAudioFirstAnchorTimeIfNeeded(int64_t mediaUs); 69 void setAnchorTime(int64_t mediaUs, int64_t realUs, bool resume = false); 70 void setVideoLateByUs(int64_t lateUs); 71 int64_t getVideoLateByUs(); 72 void setPauseStartedTimeRealUs(int64_t realUs); 73 74 bool openAudioSink( 75 const sp<AMessage> &format, 76 bool offloadOnly, 77 bool hasVideo, 78 uint32_t flags); 79 void closeAudioSink(); 80 81 enum { 82 kWhatEOS = 'eos ', 83 kWhatFlushComplete = 'fluC', 84 kWhatPosition = 'posi', 85 kWhatVideoRenderingStart = 'vdrd', 86 kWhatMediaRenderingStart = 'mdrd', 87 kWhatAudioOffloadTearDown = 'aOTD', 88 kWhatAudioOffloadPauseTimeout = 'aOPT', 89 }; 90 91 enum AudioOffloadTearDownReason { 92 kDueToError = 0, 93 kDueToTimeout, 94 }; 95 96protected: 97 virtual ~Renderer(); 98 99 virtual void onMessageReceived(const sp<AMessage> &msg); 100 101private: 102 enum { 103 kWhatDrainAudioQueue = 'draA', 104 kWhatDrainVideoQueue = 'draV', 105 kWhatPostDrainVideoQueue = 'pDVQ', 106 kWhatQueueBuffer = 'queB', 107 kWhatQueueEOS = 'qEOS', 108 kWhatFlush = 'flus', 109 kWhatAudioSinkChanged = 'auSC', 110 kWhatPause = 'paus', 111 kWhatResume = 'resm', 112 kWhatOpenAudioSink = 'opnA', 113 kWhatCloseAudioSink = 'clsA', 114 kWhatStopAudioSink = 'stpA', 115 kWhatDisableOffloadAudio = 'noOA', 116 kWhatEnableOffloadAudio = 'enOA', 117 kWhatSetVideoFrameRate = 'sVFR', 118 }; 119 120 struct QueueEntry { 121 sp<ABuffer> mBuffer; 122 sp<AMessage> mNotifyConsumed; 123 size_t mOffset; 124 status_t mFinalResult; 125 int32_t mBufferOrdinal; 126 }; 127 128 static const int64_t kMinPositionUpdateDelayUs; 129 130 sp<MediaPlayerBase::AudioSink> mAudioSink; 131 sp<AMessage> mNotify; 132 Mutex mLock; 133 uint32_t mFlags; 134 List<QueueEntry> mAudioQueue; 135 List<QueueEntry> mVideoQueue; 136 uint32_t mNumFramesWritten; 137 sp<VideoFrameScheduler> mVideoScheduler; 138 139 bool mDrainAudioQueuePending; 140 bool mDrainVideoQueuePending; 141 int32_t mAudioQueueGeneration; 142 int32_t mVideoQueueGeneration; 143 144 Mutex mTimeLock; 145 // |mTimeLock| protects the following 7 member vars that are related to time. 146 // Note: those members are only written on Renderer thread, so reading on Renderer thread 147 // doesn't need to be protected. Otherwise accessing those members must be protected by 148 // |mTimeLock|. 149 // TODO: move those members to a seperated media clock class. 150 int64_t mAudioFirstAnchorTimeMediaUs; 151 int64_t mAnchorTimeMediaUs; 152 int64_t mAnchorTimeRealUs; 153 int64_t mVideoLateByUs; 154 bool mHasAudio; 155 bool mHasVideo; 156 int64_t mPauseStartedTimeRealUs; 157 158 Mutex mFlushLock; // protects the following 2 member vars. 159 bool mFlushingAudio; 160 bool mFlushingVideo; 161 162 bool mSyncQueues; 163 164 bool mPaused; 165 bool mVideoSampleReceived; 166 bool mVideoRenderingStarted; 167 int32_t mVideoRenderingStartGeneration; 168 int32_t mAudioRenderingStartGeneration; 169 170 int64_t mLastPositionUpdateUs; 171 172 int32_t mAudioOffloadPauseTimeoutGeneration; 173 bool mAudioOffloadTornDown; 174 audio_offload_info_t mCurrentOffloadInfo; 175 176 int32_t mTotalBuffersQueued; 177 int32_t mLastAudioBufferDrained; 178 179 size_t fillAudioBuffer(void *buffer, size_t size); 180 181 bool onDrainAudioQueue(); 182 int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs); 183 int64_t getPlayedOutAudioDurationUs(int64_t nowUs); 184 void postDrainAudioQueue_l(int64_t delayUs = 0); 185 186 void onNewAudioMediaTime(int64_t mediaTimeUs); 187 int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs); 188 189 void onDrainVideoQueue(); 190 void postDrainVideoQueue(); 191 192 void prepareForMediaRenderingStart(); 193 void notifyIfMediaRenderingStarted(); 194 195 void onQueueBuffer(const sp<AMessage> &msg); 196 void onQueueEOS(const sp<AMessage> &msg); 197 void onFlush(const sp<AMessage> &msg); 198 void onAudioSinkChanged(); 199 void onDisableOffloadAudio(); 200 void onEnableOffloadAudio(); 201 void onPause(); 202 void onResume(); 203 void onSetVideoFrameRate(float fps); 204 void onAudioOffloadTearDown(AudioOffloadTearDownReason reason); 205 bool onOpenAudioSink( 206 const sp<AMessage> &format, 207 bool offloadOnly, 208 bool hasVideo, 209 uint32_t flags); 210 void onCloseAudioSink(); 211 212 void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0); 213 void notifyFlushComplete(bool audio); 214 void notifyPosition(); 215 void notifyVideoLateBy(int64_t lateByUs); 216 void notifyVideoRenderingStart(); 217 void notifyAudioOffloadTearDown(); 218 219 void flushQueue(List<QueueEntry> *queue); 220 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg); 221 void syncQueuesDone_l(); 222 223 bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; } 224 225 void startAudioOffloadPauseTimeout(); 226 void cancelAudioOffloadPauseTimeout(); 227 228 DISALLOW_EVIL_CONSTRUCTORS(Renderer); 229}; 230 231} // namespace android 232 233#endif // NUPLAYER_RENDERER_H_ 234