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