Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webaudio / AudioNodeOutput.cpp
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 void AudioNodeOutput::addInput(AudioNodeInput* input)
150 {
151     ASSERT(context()->isGraphOwner());
152
153     ASSERT(input);
154     if (!input)
155         return;
156
157     m_inputs.add(input);
158 }
159
160 void AudioNodeOutput::removeInput(AudioNodeInput* input)
161 {
162     ASSERT(context()->isGraphOwner());
163
164     ASSERT(input);
165     if (!input)
166         return;
167
168     m_inputs.remove(input);
169 }
170
171 void AudioNodeOutput::disconnectAllInputs()
172 {
173     ASSERT(context()->isGraphOwner());
174
175     // AudioNodeInput::disconnect() changes m_inputs by calling removeInput().
176     while (!m_inputs.isEmpty()) {
177         AudioNodeInput* input = *m_inputs.begin();
178         input->disconnect(this);
179     }
180 }
181
182 void AudioNodeOutput::addParam(AudioParam* param)
183 {
184     ASSERT(context()->isGraphOwner());
185
186     ASSERT(param);
187     if (!param)
188         return;
189
190     m_params.add(param);
191 }
192
193 void AudioNodeOutput::removeParam(AudioParam* param)
194 {
195     ASSERT(context()->isGraphOwner());
196
197     ASSERT(param);
198     if (!param)
199         return;
200
201     m_params.remove(param);
202 }
203
204 void AudioNodeOutput::disconnectAllParams()
205 {
206     ASSERT(context()->isGraphOwner());
207
208     // AudioParam::disconnect() changes m_params by calling removeParam().
209     while (!m_params.isEmpty()) {
210         AudioParam* param = m_params.begin()->get();
211         param->disconnect(this);
212     }
213 }
214
215 void AudioNodeOutput::disconnectAll()
216 {
217     disconnectAllInputs();
218     disconnectAllParams();
219 }
220
221 void AudioNodeOutput::disable()
222 {
223     ASSERT(context()->isGraphOwner());
224
225     if (m_isEnabled) {
226         for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
227             AudioNodeInput* input = *i;
228             input->disable(this);
229         }
230         m_isEnabled = false;
231     }
232 }
233
234 void AudioNodeOutput::enable()
235 {
236     ASSERT(context()->isGraphOwner());
237
238     if (!m_isEnabled) {
239         for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
240             AudioNodeInput* input = *i;
241             input->enable(this);
242         }
243         m_isEnabled = true;
244     }
245 }
246
247 } // namespace WebCore
248
249 #endif // ENABLE(WEB_AUDIO)