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