NuPlayerRenderer.h revision 3b9eb1f8629c6264d924ab7043f80d824cdd39e2
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 setVideoAnchorTime(int64_t mediaUs, int64_t realUs); 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 kWhatQueueBuffer = 'queB', 105 kWhatQueueEOS = 'qEOS', 106 kWhatFlush = 'flus', 107 kWhatAudioSinkChanged = 'auSC', 108 kWhatPause = 'paus', 109 kWhatResume = 'resm', 110 kWhatOpenAudioSink = 'opnA', 111 kWhatCloseAudioSink = 'clsA', 112 kWhatStopAudioSink = 'stpA', 113 kWhatDisableOffloadAudio = 'noOA', 114 kWhatSetVideoFrameRate = 'sVFR', 115 }; 116 117 struct QueueEntry { 118 sp<ABuffer> mBuffer; 119 sp<AMessage> mNotifyConsumed; 120 size_t mOffset; 121 status_t mFinalResult; 122 }; 123 124 static const int64_t kMinPositionUpdateDelayUs; 125 126 sp<MediaPlayerBase::AudioSink> mAudioSink; 127 sp<AMessage> mNotify; 128 Mutex mLock; 129 uint32_t mFlags; 130 List<QueueEntry> mAudioQueue; 131 List<QueueEntry> mVideoQueue; 132 uint32_t mNumFramesWritten; 133 sp<VideoFrameScheduler> mVideoScheduler; 134 135 bool mDrainAudioQueuePending; 136 bool mDrainVideoQueuePending; 137 int32_t mAudioQueueGeneration; 138 int32_t mVideoQueueGeneration; 139 140 Mutex mTimeLock; 141 // |mTimeLock| protects the following 7 member vars that are related to time. 142 // Note: those members are only written on Renderer thread, so reading on Renderer thread 143 // doesn't need to be protected. Otherwise accessing those members must be protected by 144 // |mTimeLock|. 145 // TODO: move those members to a seperated media clock class. 146 int64_t mAudioFirstAnchorTimeMediaUs; 147 int64_t mVideoAnchorTimeMediaUs; 148 int64_t mVideoAnchorTimeRealUs; 149 int64_t mVideoLateByUs; 150 bool mHasAudio; 151 bool mHasVideo; 152 int64_t mPauseStartedTimeRealUs; 153 154 Mutex mFlushLock; // protects the following 2 member vars. 155 bool mFlushingAudio; 156 bool mFlushingVideo; 157 158 bool mSyncQueues; 159 160 bool mPaused; 161 bool mVideoSampleReceived; 162 bool mVideoRenderingStarted; 163 int32_t mVideoRenderingStartGeneration; 164 int32_t mAudioRenderingStartGeneration; 165 166 int64_t mLastPositionUpdateUs; 167 168 int32_t mAudioOffloadPauseTimeoutGeneration; 169 bool mAudioOffloadTornDown; 170 audio_offload_info_t mCurrentOffloadInfo; 171 172 size_t fillAudioBuffer(void *buffer, size_t size); 173 174 bool onDrainAudioQueue(); 175 int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs); 176 int64_t getPlayedOutAudioDurationUs(int64_t nowUs); 177 void postDrainAudioQueue_l(int64_t delayUs = 0); 178 179 int64_t getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs); 180 181 void onDrainVideoQueue(); 182 void postDrainVideoQueue(); 183 184 void prepareForMediaRenderingStart(); 185 void notifyIfMediaRenderingStarted(); 186 187 void onQueueBuffer(const sp<AMessage> &msg); 188 void onQueueEOS(const sp<AMessage> &msg); 189 void onFlush(const sp<AMessage> &msg); 190 void onAudioSinkChanged(); 191 void onDisableOffloadAudio(); 192 void onPause(); 193 void onResume(); 194 void onSetVideoFrameRate(float fps); 195 void onAudioOffloadTearDown(AudioOffloadTearDownReason reason); 196 bool onOpenAudioSink( 197 const sp<AMessage> &format, 198 bool offloadOnly, 199 bool hasVideo, 200 uint32_t flags); 201 void onCloseAudioSink(); 202 203 void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0); 204 void notifyFlushComplete(bool audio); 205 void notifyPosition(); 206 void notifyVideoLateBy(int64_t lateByUs); 207 void notifyVideoRenderingStart(); 208 void notifyAudioOffloadTearDown(); 209 210 void flushQueue(List<QueueEntry> *queue); 211 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg); 212 void syncQueuesDone_l(); 213 214 bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; } 215 216 void startAudioOffloadPauseTimeout(); 217 void cancelAudioOffloadPauseTimeout(); 218 219 DISALLOW_EVIL_CONSTRUCTORS(Renderer); 220}; 221 222} // namespace android 223 224#endif // NUPLAYER_RENDERER_H_ 225