NuPlayerRenderer.h revision 0852917279f79a94907e9906d0533ae409a30f6a
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 enum { 63 kWhatEOS = 'eos ', 64 kWhatFlushComplete = 'fluC', 65 kWhatPosition = 'posi', 66 kWhatVideoRenderingStart = 'vdrd', 67 kWhatMediaRenderingStart = 'mdrd', 68 kWhatAudioOffloadTearDown = 'aOTD', 69 kWhatAudioOffloadPauseTimeout = 'aOPT', 70 }; 71 72 enum AudioOffloadTearDownReason { 73 kDueToError = 0, 74 kDueToTimeout, 75 }; 76 77protected: 78 virtual ~Renderer(); 79 80 virtual void onMessageReceived(const sp<AMessage> &msg); 81 82private: 83 enum { 84 kWhatDrainAudioQueue = 'draA', 85 kWhatDrainVideoQueue = 'draV', 86 kWhatQueueBuffer = 'queB', 87 kWhatQueueEOS = 'qEOS', 88 kWhatFlush = 'flus', 89 kWhatAudioSinkChanged = 'auSC', 90 kWhatPause = 'paus', 91 kWhatResume = 'resm', 92 kWhatStopAudioSink = 'stpA', 93 kWhatDisableOffloadAudio = 'noOA', 94 kWhatSetVideoFrameRate = 'sVFR', 95 }; 96 97 struct QueueEntry { 98 sp<ABuffer> mBuffer; 99 sp<AMessage> mNotifyConsumed; 100 size_t mOffset; 101 status_t mFinalResult; 102 }; 103 104 static const int64_t kMinPositionUpdateDelayUs; 105 106 sp<MediaPlayerBase::AudioSink> mAudioSink; 107 sp<AMessage> mNotify; 108 Mutex mLock; 109 uint32_t mFlags; 110 List<QueueEntry> mAudioQueue; 111 List<QueueEntry> mVideoQueue; 112 uint32_t mNumFramesWritten; 113 sp<VideoFrameScheduler> mVideoScheduler; 114 115 bool mDrainAudioQueuePending; 116 bool mDrainVideoQueuePending; 117 int32_t mAudioQueueGeneration; 118 int32_t mVideoQueueGeneration; 119 120 int64_t mFirstAnchorTimeMediaUs; 121 int64_t mAnchorTimeMediaUs; 122 int64_t mAnchorTimeRealUs; 123 124 Mutex mFlushLock; // protects the following 2 member vars. 125 bool mFlushingAudio; 126 bool mFlushingVideo; 127 128 bool mHasAudio; 129 bool mHasVideo; 130 bool mSyncQueues; 131 132 bool mPaused; 133 bool mVideoSampleReceived; 134 bool mVideoRenderingStarted; 135 int32_t mVideoRenderingStartGeneration; 136 int32_t mAudioRenderingStartGeneration; 137 138 int64_t mLastPositionUpdateUs; 139 int64_t mVideoLateByUs; 140 141 int32_t mAudioOffloadPauseTimeoutGeneration; 142 bool mAudioOffloadTornDown; 143 144 size_t fillAudioBuffer(void *buffer, size_t size); 145 146 bool onDrainAudioQueue(); 147 int64_t getPendingAudioPlayoutDurationUs(int64_t nowUs); 148 int64_t getPlayedOutAudioDurationUs(int64_t nowUs); 149 void postDrainAudioQueue_l(int64_t delayUs = 0); 150 151 void onDrainVideoQueue(); 152 void postDrainVideoQueue(); 153 154 void prepareForMediaRenderingStart(); 155 void notifyIfMediaRenderingStarted(); 156 157 void onQueueBuffer(const sp<AMessage> &msg); 158 void onQueueEOS(const sp<AMessage> &msg); 159 void onFlush(const sp<AMessage> &msg); 160 void onAudioSinkChanged(); 161 void onDisableOffloadAudio(); 162 void onPause(); 163 void onResume(); 164 void onSetVideoFrameRate(float fps); 165 void onAudioOffloadTearDown(AudioOffloadTearDownReason reason); 166 167 void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0); 168 void notifyFlushComplete(bool audio); 169 void notifyPosition(); 170 void notifyVideoLateBy(int64_t lateByUs); 171 void notifyVideoRenderingStart(); 172 void notifyAudioOffloadTearDown(); 173 174 void flushQueue(List<QueueEntry> *queue); 175 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg); 176 void syncQueuesDone_l(); 177 178 bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; } 179 180 void startAudioOffloadPauseTimeout(); 181 void cancelAudioOffloadPauseTimeout(); 182 183 DISALLOW_EVIL_CONSTRUCTORS(Renderer); 184}; 185 186} // namespace android 187 188#endif // NUPLAYER_RENDERER_H_ 189