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 "AudioNodeOutput.h" |
30 | |
31 | #include "AudioBus.h" |
32 | #include "AudioContext.h" |
33 | #include "AudioNodeInput.h" |
34 | #include "AudioParam.h" |
35 | #include <wtf/Threading.h> |
36 | |
37 | namespace WebCore { |
38 | |
39 | AudioNodeOutput::AudioNodeOutput(AudioNode* node, unsigned numberOfChannels) |
40 | : m_node(node) |
41 | , m_numberOfChannels(numberOfChannels) |
42 | , m_desiredNumberOfChannels(numberOfChannels) |
43 | , m_isInPlace(false) |
44 | , m_isEnabled(true) |
45 | , m_renderingFanOutCount(0) |
46 | , m_renderingParamFanOutCount(0) |
47 | { |
48 | ASSERT(numberOfChannels <= AudioContext::maxNumberOfChannels()); |
49 | |
50 | m_internalBus = AudioBus::create(numberOfChannels, AudioNode::ProcessingSizeInFrames); |
51 | } |
52 | |
53 | void AudioNodeOutput::setNumberOfChannels(unsigned numberOfChannels) |
54 | { |
55 | ASSERT(numberOfChannels <= AudioContext::maxNumberOfChannels()); |
56 | ASSERT(context().isGraphOwner()); |
57 | |
58 | m_desiredNumberOfChannels = numberOfChannels; |
59 | |
60 | if (context().isAudioThread()) { |
61 | // 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). |
62 | updateNumberOfChannels(); |
63 | } else { |
64 | // Let the context take care of it in the audio thread in the pre and post render tasks. |
65 | context().markAudioNodeOutputDirty(this); |
66 | } |
67 | } |
68 | |
69 | void AudioNodeOutput::updateInternalBus() |
70 | { |
71 | if (numberOfChannels() == m_internalBus->numberOfChannels()) |
72 | return; |
73 | |
74 | m_internalBus = AudioBus::create(numberOfChannels(), AudioNode::ProcessingSizeInFrames); |
75 | } |
76 | |
77 | void AudioNodeOutput::updateRenderingState() |
78 | { |
79 | updateNumberOfChannels(); |
80 | m_renderingFanOutCount = fanOutCount(); |
81 | m_renderingParamFanOutCount = paramFanOutCount(); |
82 | } |
83 | |
84 | void AudioNodeOutput::updateNumberOfChannels() |
85 | { |
86 | ASSERT(context().isAudioThread() && context().isGraphOwner()); |
87 | |
88 | if (m_numberOfChannels != m_desiredNumberOfChannels) { |
89 | m_numberOfChannels = m_desiredNumberOfChannels; |
90 | updateInternalBus(); |
91 | propagateChannelCount(); |
92 | } |
93 | } |
94 | |
95 | void AudioNodeOutput::propagateChannelCount() |
96 | { |
97 | ASSERT(context().isAudioThread() && context().isGraphOwner()); |
98 | |
99 | if (isChannelCountKnown()) { |
100 | // Announce to any nodes we're connected to that we changed our channel count for its input. |
101 | for (auto& input : m_inputs) { |
102 | AudioNode* connectionNode = input->node(); |
103 | connectionNode->checkNumberOfChannelsForInput(input); |
104 | } |
105 | } |
106 | } |
107 | |
108 | AudioBus* AudioNodeOutput::pull(AudioBus* inPlaceBus, size_t framesToProcess) |
109 | { |
110 | ASSERT(context().isAudioThread()); |
111 | ASSERT(m_renderingFanOutCount > 0 || m_renderingParamFanOutCount > 0); |
112 | |
113 | // Causes our AudioNode to process if it hasn't already for this render quantum. |
114 | // We try to do in-place processing (using inPlaceBus) if at all possible, |
115 | // but we can't process in-place if we're connected to more than one input (fan-out > 1). |
116 | // In this case pull() is called multiple times per rendering quantum, and the processIfNecessary() call below will |
117 | // cause our node to process() only the first time, caching the output in m_internalOutputBus for subsequent calls. |
118 | |
119 | m_isInPlace = inPlaceBus && inPlaceBus->numberOfChannels() == numberOfChannels() && (m_renderingFanOutCount + m_renderingParamFanOutCount) == 1; |
120 | |
121 | m_inPlaceBus = m_isInPlace ? inPlaceBus : 0; |
122 | |
123 | node()->processIfNecessary(framesToProcess); |
124 | return bus(); |
125 | } |
126 | |
127 | AudioBus* AudioNodeOutput::bus() const |
128 | { |
129 | ASSERT(const_cast<AudioNodeOutput*>(this)->context().isAudioThread()); |
130 | return m_isInPlace ? m_inPlaceBus.get() : m_internalBus.get(); |
131 | } |
132 | |
133 | unsigned AudioNodeOutput::fanOutCount() |
134 | { |
135 | ASSERT(context().isGraphOwner()); |
136 | return m_inputs.size(); |
137 | } |
138 | |
139 | unsigned AudioNodeOutput::paramFanOutCount() |
140 | { |
141 | ASSERT(context().isGraphOwner()); |
142 | return m_params.size(); |
143 | } |
144 | |
145 | unsigned AudioNodeOutput::renderingFanOutCount() const |
146 | { |
147 | return m_renderingFanOutCount; |
148 | } |
149 | |
150 | unsigned AudioNodeOutput::renderingParamFanOutCount() const |
151 | { |
152 | return m_renderingParamFanOutCount; |
153 | } |
154 | |
155 | void AudioNodeOutput::addInput(AudioNodeInput* input) |
156 | { |
157 | ASSERT(context().isGraphOwner()); |
158 | |
159 | ASSERT(input); |
160 | if (!input) |
161 | return; |
162 | |
163 | m_inputs.add(input); |
164 | } |
165 | |
166 | void AudioNodeOutput::removeInput(AudioNodeInput* input) |
167 | { |
168 | ASSERT(context().isGraphOwner()); |
169 | |
170 | ASSERT(input); |
171 | if (!input) |
172 | return; |
173 | |
174 | m_inputs.remove(input); |
175 | } |
176 | |
177 | void AudioNodeOutput::disconnectAllInputs() |
178 | { |
179 | ASSERT(context().isGraphOwner()); |
180 | |
181 | // AudioNodeInput::disconnect() changes m_inputs by calling removeInput(). |
182 | while (!m_inputs.isEmpty()) { |
183 | AudioNodeInput* input = *m_inputs.begin(); |
184 | input->disconnect(this); |
185 | } |
186 | } |
187 | |
188 | void AudioNodeOutput::addParam(AudioParam* param) |
189 | { |
190 | ASSERT(context().isGraphOwner()); |
191 | |
192 | ASSERT(param); |
193 | if (!param) |
194 | return; |
195 | |
196 | m_params.add(param); |
197 | } |
198 | |
199 | void AudioNodeOutput::removeParam(AudioParam* param) |
200 | { |
201 | ASSERT(context().isGraphOwner()); |
202 | |
203 | ASSERT(param); |
204 | if (!param) |
205 | return; |
206 | |
207 | m_params.remove(param); |
208 | } |
209 | |
210 | void AudioNodeOutput::disconnectAllParams() |
211 | { |
212 | ASSERT(context().isGraphOwner()); |
213 | |
214 | // AudioParam::disconnect() changes m_params by calling removeParam(). |
215 | while (!m_params.isEmpty()) { |
216 | AudioParam* param = m_params.begin()->get(); |
217 | param->disconnect(this); |
218 | } |
219 | } |
220 | |
221 | void AudioNodeOutput::disconnectAll() |
222 | { |
223 | disconnectAllInputs(); |
224 | disconnectAllParams(); |
225 | } |
226 | |
227 | void AudioNodeOutput::disable() |
228 | { |
229 | ASSERT(context().isGraphOwner()); |
230 | |
231 | if (m_isEnabled) { |
232 | for (auto& input : m_inputs) |
233 | input->disable(this); |
234 | m_isEnabled = false; |
235 | } |
236 | } |
237 | |
238 | void AudioNodeOutput::enable() |
239 | { |
240 | ASSERT(context().isGraphOwner()); |
241 | |
242 | if (!m_isEnabled) { |
243 | for (auto& input : m_inputs) |
244 | input->enable(this); |
245 | m_isEnabled = true; |
246 | } |
247 | } |
248 | |
249 | } // namespace WebCore |
250 | |
251 | #endif // ENABLE(WEB_AUDIO) |
252 | |