NuPlayerRenderer.h revision fbe8bef8bcf7aed97f0332908a817b0e6d91b9ba
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; 26 27struct NuPlayer::Renderer : public AHandler { 28 enum Flags { 29 FLAG_REAL_TIME = 1, 30 FLAG_OFFLOAD_AUDIO = 2, 31 }; 32 Renderer(const sp<MediaPlayerBase::AudioSink> &sink, 33 const sp<AMessage> ¬ify, 34 uint32_t flags = 0); 35 36 static size_t AudioSinkCallback( 37 MediaPlayerBase::AudioSink *audioSink, 38 void *data, size_t size, void *me, 39 MediaPlayerBase::AudioSink::cb_event_t event); 40 41 void queueBuffer( 42 bool audio, 43 const sp<ABuffer> &buffer, 44 const sp<AMessage> ¬ifyConsumed); 45 46 void queueEOS(bool audio, status_t finalResult); 47 48 void flush(bool audio); 49 50 void signalTimeDiscontinuity(); 51 52 void signalAudioSinkChanged(); 53 54 void signalDisableOffloadAudio(); 55 56 void pause(); 57 void resume(); 58 59 enum { 60 kWhatEOS = 'eos ', 61 kWhatFlushComplete = 'fluC', 62 kWhatPosition = 'posi', 63 kWhatVideoRenderingStart = 'vdrd', 64 kWhatMediaRenderingStart = 'mdrd', 65 kWhatAudioOffloadTearDown = 'aOTD', 66 }; 67 68protected: 69 virtual ~Renderer(); 70 71 virtual void onMessageReceived(const sp<AMessage> &msg); 72 73private: 74 enum { 75 kWhatDrainAudioQueue = 'draA', 76 kWhatDrainVideoQueue = 'draV', 77 kWhatQueueBuffer = 'queB', 78 kWhatQueueEOS = 'qEOS', 79 kWhatFlush = 'flus', 80 kWhatAudioSinkChanged = 'auSC', 81 kWhatPause = 'paus', 82 kWhatResume = 'resm', 83 kWhatStopAudioSink = 'stpA', 84 kWhatDisableOffloadAudio = 'noOA', 85 }; 86 87 struct QueueEntry { 88 sp<ABuffer> mBuffer; 89 sp<AMessage> mNotifyConsumed; 90 size_t mOffset; 91 status_t mFinalResult; 92 }; 93 94 static const int64_t kMinPositionUpdateDelayUs; 95 96 sp<MediaPlayerBase::AudioSink> mAudioSink; 97 sp<AMessage> mNotify; 98 Mutex mLock; 99 uint32_t mFlags; 100 List<QueueEntry> mAudioQueue; 101 List<QueueEntry> mVideoQueue; 102 uint32_t mNumFramesWritten; 103 104 bool mDrainAudioQueuePending; 105 bool mDrainVideoQueuePending; 106 int32_t mAudioQueueGeneration; 107 int32_t mVideoQueueGeneration; 108 109 int64_t mFirstAudioTimeUs; 110 int64_t mAnchorTimeMediaUs; 111 int64_t mAnchorTimeRealUs; 112 113 Mutex mFlushLock; // protects the following 2 member vars. 114 bool mFlushingAudio; 115 bool mFlushingVideo; 116 117 bool mHasAudio; 118 bool mHasVideo; 119 bool mSyncQueues; 120 121 bool mPaused; 122 bool mVideoSampleReceived; 123 bool mVideoRenderingStarted; 124 int32_t mVideoRenderingStartGeneration; 125 int32_t mAudioRenderingStartGeneration; 126 127 int64_t mLastPositionUpdateUs; 128 int64_t mVideoLateByUs; 129 130 size_t fillAudioBuffer(void *buffer, size_t size); 131 132 bool onDrainAudioQueue(); 133 int64_t getAudioPendingPlayoutUs(); 134 void postDrainAudioQueue_l(int64_t delayUs = 0); 135 136 void onDrainVideoQueue(); 137 void postDrainVideoQueue(); 138 139 void prepareForMediaRenderingStart(); 140 void notifyIfMediaRenderingStarted(); 141 142 void onQueueBuffer(const sp<AMessage> &msg); 143 void onQueueEOS(const sp<AMessage> &msg); 144 void onFlush(const sp<AMessage> &msg); 145 void onAudioSinkChanged(); 146 void onDisableOffloadAudio(); 147 void onPause(); 148 void onResume(); 149 void onAudioOffloadTearDown(); 150 151 void notifyEOS(bool audio, status_t finalResult, int64_t delayUs = 0); 152 void notifyFlushComplete(bool audio); 153 void notifyPosition(); 154 void notifyVideoLateBy(int64_t lateByUs); 155 void notifyVideoRenderingStart(); 156 void notifyAudioOffloadTearDown(); 157 158 void flushQueue(List<QueueEntry> *queue); 159 bool dropBufferWhileFlushing(bool audio, const sp<AMessage> &msg); 160 void syncQueuesDone_l(); 161 162 bool offloadingAudio() const { return (mFlags & FLAG_OFFLOAD_AUDIO) != 0; } 163 164 DISALLOW_EVIL_CONSTRUCTORS(Renderer); 165}; 166 167} // namespace android 168 169#endif // NUPLAYER_RENDERER_H_ 170