1 /* 2 * Copyright (C) 2010, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include "config.h" 26 27 #if ENABLE(WEB_AUDIO) 28 29 #include "modules/webaudio/AudioNodeOutput.h" 30 31 #include "modules/webaudio/AudioContext.h" 32 #include "modules/webaudio/AudioNodeInput.h" 33 #include "wtf/Threading.h" 34 35 namespace WebCore { 36 37 AudioNodeOutput::AudioNodeOutput(AudioNode* node, unsigned numberOfChannels) 38 : m_node(node) 39 , m_numberOfChannels(numberOfChannels) 40 , m_desiredNumberOfChannels(numberOfChannels) 41 , m_isInPlace(false) 42 , m_isEnabled(true) 43 , m_renderingFanOutCount(0) 44 , m_renderingParamFanOutCount(0) 45 { 46 ASSERT(numberOfChannels <= AudioContext::maxNumberOfChannels()); 47 48 m_internalBus = AudioBus::create(numberOfChannels, AudioNode::ProcessingSizeInFrames); 49 } 50 51 void AudioNodeOutput::setNumberOfChannels(unsigned numberOfChannels) 52 { 53 ASSERT(numberOfChannels <= AudioContext::maxNumberOfChannels()); 54 ASSERT(context()->isGraphOwner()); 55 56 m_desiredNumberOfChannels = numberOfChannels; 57 58 if (context()->isAudioThread()) { 59 // If we're in the audio thread then we can take care of it right away (we should be at the very start or end of a rendering quantum). 60 updateNumberOfChannels(); 61 } else { 62 // Let the context take care of it in the audio thread in the pre and post render tasks. 63 context()->markAudioNodeOutputDirty(this); 64 } 65 } 66 67 void AudioNodeOutput::updateInternalBus() 68 { 69 if (numberOfChannels() == m_internalBus->numberOfChannels()) 70 return; 71 72 m_internalBus = AudioBus::create(numberOfChannels(), AudioNode::ProcessingSizeInFrames); 73 } 74 75 void AudioNodeOutput::updateRenderingState() 76 { 77 updateNumberOfChannels(); 78 m_renderingFanOutCount = fanOutCount(); 79 m_renderingParamFanOutCount = paramFanOutCount(); 80 } 81 82 void AudioNodeOutput::updateNumberOfChannels() 83 { 84 ASSERT(context()->isAudioThread() && context()->isGraphOwner()); 85 86 if (m_numberOfChannels != m_desiredNumberOfChannels) { 87 m_numberOfChannels = m_desiredNumberOfChannels; 88 updateInternalBus(); 89 propagateChannelCount(); 90 } 91 } 92 93 void AudioNodeOutput::propagateChannelCount() 94 { 95 ASSERT(context()->isAudioThread() && context()->isGraphOwner()); 96 97 if (isChannelCountKnown()) { 98 // Announce to any nodes we're connected to that we changed our channel count for its input. 99 for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) { 100 AudioNodeInput* input = *i; 101 AudioNode* connectionNode = input->node(); 102 connectionNode->checkNumberOfChannelsForInput(input); 103 } 104 } 105 } 106 107 AudioBus* AudioNodeOutput::pull(AudioBus* inPlaceBus, size_t framesToProcess) 108 { 109 ASSERT(context()->isAudioThread()); 110 ASSERT(m_renderingFanOutCount > 0 || m_renderingParamFanOutCount > 0); 111 112 // Causes our AudioNode to process if it hasn't already for this render quantum. 113 // We try to do in-place processing (using inPlaceBus) if at all possible, 114 // but we can't process in-place if we're connected to more than one input (fan-out > 1). 115 // In this case pull() is called multiple times per rendering quantum, and the processIfNecessary() call below will 116 // cause our node to process() only the first time, caching the output in m_internalOutputBus for subsequent calls. 117 118 m_isInPlace = inPlaceBus && inPlaceBus->numberOfChannels() == numberOfChannels() && (m_renderingFanOutCount + m_renderingParamFanOutCount) == 1; 119 120 m_inPlaceBus = m_isInPlace ? inPlaceBus : 0; 121 122 node()->processIfNecessary(framesToProcess); 123 return bus(); 124 } 125 126 AudioBus* AudioNodeOutput::bus() const 127 { 128 ASSERT(const_cast<AudioNodeOutput*>(this)->context()->isAudioThread()); 129 return m_isInPlace ? m_inPlaceBus.get() : m_internalBus.get(); 130 } 131 132 unsigned AudioNodeOutput::fanOutCount() 133 { 134 ASSERT(context()->isGraphOwner()); 135 return m_inputs.size(); 136 } 137 138 unsigned AudioNodeOutput::paramFanOutCount() 139 { 140 ASSERT(context()->isGraphOwner()); 141 return m_params.size(); 142 } 143 144 unsigned AudioNodeOutput::renderingFanOutCount() const 145 { 146 return m_renderingFanOutCount; 147 } 148 149 unsigned AudioNodeOutput::renderingParamFanOutCount() const 150 { 151 return m_renderingParamFanOutCount; 152 } 153 154 void AudioNodeOutput::addInput(AudioNodeInput* input) 155 { 156 ASSERT(context()->isGraphOwner()); 157 158 ASSERT(input); 159 if (!input) 160 return; 161 162 m_inputs.add(input); 163 } 164 165 void AudioNodeOutput::removeInput(AudioNodeInput* input) 166 { 167 ASSERT(context()->isGraphOwner()); 168 169 ASSERT(input); 170 if (!input) 171 return; 172 173 m_inputs.remove(input); 174 } 175 176 void AudioNodeOutput::disconnectAllInputs() 177 { 178 ASSERT(context()->isGraphOwner()); 179 180 // AudioNodeInput::disconnect() changes m_inputs by calling removeInput(). 181 while (!m_inputs.isEmpty()) { 182 AudioNodeInput* input = *m_inputs.begin(); 183 input->disconnect(this); 184 } 185 } 186 187 void AudioNodeOutput::addParam(AudioParam* param) 188 { 189 ASSERT(context()->isGraphOwner()); 190 191 ASSERT(param); 192 if (!param) 193 return; 194 195 m_params.add(param); 196 } 197 198 void AudioNodeOutput::removeParam(AudioParam* param) 199 { 200 ASSERT(context()->isGraphOwner()); 201 202 ASSERT(param); 203 if (!param) 204 return; 205 206 m_params.remove(param); 207 } 208 209 void AudioNodeOutput::disconnectAllParams() 210 { 211 ASSERT(context()->isGraphOwner()); 212 213 // AudioParam::disconnect() changes m_params by calling removeParam(). 214 while (!m_params.isEmpty()) { 215 AudioParam* param = m_params.begin()->get(); 216 param->disconnect(this); 217 } 218 } 219 220 void AudioNodeOutput::disconnectAll() 221 { 222 disconnectAllInputs(); 223 disconnectAllParams(); 224 } 225 226 void AudioNodeOutput::disable() 227 { 228 ASSERT(context()->isGraphOwner()); 229 230 if (m_isEnabled) { 231 for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) { 232 AudioNodeInput* input = *i; 233 input->disable(this); 234 } 235 m_isEnabled = false; 236 } 237 } 238 239 void AudioNodeOutput::enable() 240 { 241 ASSERT(context()->isGraphOwner()); 242 243 if (!m_isEnabled) { 244 for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) { 245 AudioNodeInput* input = *i; 246 input->enable(this); 247 } 248 m_isEnabled = true; 249 } 250 } 251 252 } // namespace WebCore 253 254 #endif // ENABLE(WEB_AUDIO) 255