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