2 * Copyright (C) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #include "modules/webaudio/AudioNode.h"
31 #include "bindings/v8/ExceptionMessages.h"
32 #include "bindings/v8/ExceptionState.h"
33 #include "core/dom/ExceptionCode.h"
34 #include "modules/webaudio/AudioContext.h"
35 #include "modules/webaudio/AudioNodeInput.h"
36 #include "modules/webaudio/AudioNodeOutput.h"
37 #include "modules/webaudio/AudioParam.h"
38 #include "wtf/Atomics.h"
39 #include "wtf/MainThread.h"
41 #if DEBUG_AUDIONODE_REFERENCES
47 AudioNode::AudioNode(AudioContext* context, float sampleRate)
48 : m_isInitialized(false)
49 , m_nodeType(NodeTypeUnknown)
51 , m_sampleRate(sampleRate)
52 , m_lastProcessingTime(-1)
53 , m_lastNonSilentTime(-1)
54 , m_normalRefCount(1) // start out with normal refCount == 1 (like WTF::RefCounted class)
55 , m_connectionRefCount(0)
56 , m_isMarkedForDeletion(false)
59 , m_channelCountMode(Max)
60 , m_channelInterpretation(AudioBus::Speakers)
62 ScriptWrappable::init(this);
63 #if DEBUG_AUDIONODE_REFERENCES
64 if (!s_isNodeCountInitialized) {
65 s_isNodeCountInitialized = true;
66 atexit(AudioNode::printNodeCounts);
71 AudioNode::~AudioNode()
73 #if DEBUG_AUDIONODE_REFERENCES
74 --s_nodeCount[nodeType()];
75 fprintf(stderr, "%p: %d: AudioNode::~AudioNode() %d %d\n", this, nodeType(), m_normalRefCount, m_connectionRefCount);
79 void AudioNode::initialize()
81 m_isInitialized = true;
84 void AudioNode::uninitialize()
86 m_isInitialized = false;
89 String AudioNode::nodeTypeName() const
92 case NodeTypeDestination:
93 return "AudioDestinationNode";
94 case NodeTypeOscillator:
95 return "OscillatorNode";
96 case NodeTypeAudioBufferSource:
97 return "AudioBufferSourceNode";
98 case NodeTypeMediaElementAudioSource:
99 return "MediaElementAudioSourceNode";
100 case NodeTypeMediaStreamAudioDestination:
101 return "MediaStreamAudioDestinationNode";
102 case NodeTypeMediaStreamAudioSource:
103 return "MediaStreamAudioSourceNode";
104 case NodeTypeJavaScript:
105 return "ScriptProcessorNode";
106 case NodeTypeBiquadFilter:
107 return "BiquadFilterNode";
110 case NodeTypeConvolver:
111 return "ConvolverNode";
116 case NodeTypeChannelSplitter:
117 return "ChannelSplitterNode";
118 case NodeTypeChannelMerger:
119 return "ChannelMergerNode";
120 case NodeTypeAnalyser:
121 return "AnalyserNode";
122 case NodeTypeDynamicsCompressor:
123 return "DynamicsCompressorNode";
124 case NodeTypeWaveShaper:
125 return "WaveShaperNode";
126 case NodeTypeUnknown:
129 ASSERT_NOT_REACHED();
130 return "UnknownNode";
134 void AudioNode::setNodeType(NodeType type)
138 #if DEBUG_AUDIONODE_REFERENCES
143 void AudioNode::lazyInitialize()
145 if (!isInitialized())
149 void AudioNode::addInput(PassOwnPtr<AudioNodeInput> input)
151 m_inputs.append(input);
154 void AudioNode::addOutput(PassOwnPtr<AudioNodeOutput> output)
156 m_outputs.append(output);
159 AudioNodeInput* AudioNode::input(unsigned i)
161 if (i < m_inputs.size())
162 return m_inputs[i].get();
166 AudioNodeOutput* AudioNode::output(unsigned i)
168 if (i < m_outputs.size())
169 return m_outputs[i].get();
173 void AudioNode::connect(AudioNode* destination, unsigned outputIndex, unsigned inputIndex, ExceptionState& es)
175 ASSERT(isMainThread());
176 AudioContext::AutoLocker locker(context());
179 es.throwDOMException(
181 ExceptionMessages::failedToExecute(
184 "invalid destination node."));
188 // Sanity check input and output indices.
189 if (outputIndex >= numberOfOutputs()) {
190 es.throwDOMException(
192 ExceptionMessages::failedToExecute(
195 "output index (" + String::number(outputIndex) + ") exceeds number of outputs (" + String::number(numberOfOutputs()) + ")."));
199 if (destination && inputIndex >= destination->numberOfInputs()) {
200 es.throwDOMException(
202 ExceptionMessages::failedToExecute(
205 "input index (" + String::number(inputIndex) + ") exceeds number of inputs (" + String::number(destination->numberOfInputs()) + ")."));
209 if (context() != destination->context()) {
210 es.throwDOMException(
212 ExceptionMessages::failedToExecute(
215 "cannot connect to a destination belonging to a different audio context."));
219 AudioNodeInput* input = destination->input(inputIndex);
220 AudioNodeOutput* output = this->output(outputIndex);
221 input->connect(output);
223 // Let context know that a connection has been made.
224 context()->incrementConnectionCount();
227 void AudioNode::connect(AudioParam* param, unsigned outputIndex, ExceptionState& es)
229 ASSERT(isMainThread());
230 AudioContext::AutoLocker locker(context());
233 es.throwDOMException(
235 ExceptionMessages::failedToExecute(
238 "invalid AudioParam."));
242 if (outputIndex >= numberOfOutputs()) {
243 es.throwDOMException(
245 ExceptionMessages::failedToExecute(
248 "output index (" + String::number(outputIndex) + ") exceeds number of outputs (" + String::number(numberOfOutputs()) + ")."));
252 if (context() != param->context()) {
253 es.throwDOMException(
255 ExceptionMessages::failedToExecute(
258 "cannot connect to an AudioParam belonging to a different audio context."));
262 AudioNodeOutput* output = this->output(outputIndex);
263 param->connect(output);
266 void AudioNode::disconnect(unsigned outputIndex, ExceptionState& es)
268 ASSERT(isMainThread());
269 AudioContext::AutoLocker locker(context());
271 // Sanity check input and output indices.
272 if (outputIndex >= numberOfOutputs()) {
273 es.throwDOMException(
275 ExceptionMessages::failedToExecute(
278 "output index (" + String::number(outputIndex) + ") exceeds number of outputs (" + String::number(numberOfOutputs()) + ")."));
282 AudioNodeOutput* output = this->output(outputIndex);
283 output->disconnectAll();
286 unsigned long AudioNode::channelCount()
288 return m_channelCount;
291 void AudioNode::setChannelCount(unsigned long channelCount, ExceptionState& es)
293 ASSERT(isMainThread());
294 AudioContext::AutoLocker locker(context());
296 if (channelCount > 0 && channelCount <= AudioContext::maxNumberOfChannels()) {
297 if (m_channelCount != channelCount) {
298 m_channelCount = channelCount;
299 if (m_channelCountMode != Max)
300 updateChannelsForInputs();
303 es.throwDOMException(
305 ExceptionMessages::failedToSet(
308 "channel count (" + String::number(channelCount)
309 + ") must be between 1 and "
310 + String::number(AudioContext::maxNumberOfChannels()) + "."));
314 String AudioNode::channelCountMode()
316 switch (m_channelCountMode) {
320 return "clamped-max";
324 ASSERT_NOT_REACHED();
328 void AudioNode::setChannelCountMode(const String& mode, ExceptionState& es)
330 ASSERT(isMainThread());
331 AudioContext::AutoLocker locker(context());
333 ChannelCountMode oldMode = m_channelCountMode;
336 m_channelCountMode = Max;
337 } else if (mode == "clamped-max") {
338 m_channelCountMode = ClampedMax;
339 } else if (mode == "explicit") {
340 m_channelCountMode = Explicit;
342 es.throwDOMException(
344 ExceptionMessages::failedToSet(
347 "invalid mode '" + mode + "'; must be 'max', 'clamped-max', or 'explicit'."));
350 if (m_channelCountMode != oldMode)
351 updateChannelsForInputs();
354 String AudioNode::channelInterpretation()
356 switch (m_channelInterpretation) {
357 case AudioBus::Speakers:
359 case AudioBus::Discrete:
362 ASSERT_NOT_REACHED();
366 void AudioNode::setChannelInterpretation(const String& interpretation, ExceptionState& es)
368 ASSERT(isMainThread());
369 AudioContext::AutoLocker locker(context());
371 if (interpretation == "speakers") {
372 m_channelInterpretation = AudioBus::Speakers;
373 } else if (interpretation == "discrete") {
374 m_channelInterpretation = AudioBus::Discrete;
376 es.throwDOMException(
378 ExceptionMessages::failedToSet(
379 "channelInterpretation",
381 "invalid interpretation '" + interpretation + "'; must be 'speakers' or 'discrete'."));
385 void AudioNode::updateChannelsForInputs()
387 for (unsigned i = 0; i < m_inputs.size(); ++i)
388 input(i)->changedOutputs();
391 const AtomicString& AudioNode::interfaceName() const
393 return EventTargetNames::AudioNode;
396 ExecutionContext* AudioNode::executionContext() const
398 return const_cast<AudioNode*>(this)->context()->executionContext();
401 void AudioNode::processIfNecessary(size_t framesToProcess)
403 ASSERT(context()->isAudioThread());
405 if (!isInitialized())
408 // Ensure that we only process once per rendering quantum.
409 // This handles the "fanout" problem where an output is connected to multiple inputs.
410 // The first time we're called during this time slice we process, but after that we don't want to re-process,
411 // instead our output(s) will already have the results cached in their bus;
412 double currentTime = context()->currentTime();
413 if (m_lastProcessingTime != currentTime) {
414 m_lastProcessingTime = currentTime; // important to first update this time because of feedback loops in the rendering graph
416 pullInputs(framesToProcess);
418 bool silentInputs = inputsAreSilent();
420 m_lastNonSilentTime = (context()->currentSampleFrame() + framesToProcess) / static_cast<double>(m_sampleRate);
422 if (silentInputs && propagatesSilence())
425 process(framesToProcess);
431 void AudioNode::checkNumberOfChannelsForInput(AudioNodeInput* input)
433 ASSERT(context()->isAudioThread() && context()->isGraphOwner());
435 ASSERT(m_inputs.contains(input));
436 if (!m_inputs.contains(input))
439 input->updateInternalBus();
442 bool AudioNode::propagatesSilence() const
444 return m_lastNonSilentTime + latencyTime() + tailTime() < context()->currentTime();
447 void AudioNode::pullInputs(size_t framesToProcess)
449 ASSERT(context()->isAudioThread());
451 // Process all of the AudioNodes connected to our inputs.
452 for (unsigned i = 0; i < m_inputs.size(); ++i)
453 input(i)->pull(0, framesToProcess);
456 bool AudioNode::inputsAreSilent()
458 for (unsigned i = 0; i < m_inputs.size(); ++i) {
459 if (!input(i)->bus()->isSilent())
465 void AudioNode::silenceOutputs()
467 for (unsigned i = 0; i < m_outputs.size(); ++i)
468 output(i)->bus()->zero();
471 void AudioNode::unsilenceOutputs()
473 for (unsigned i = 0; i < m_outputs.size(); ++i)
474 output(i)->bus()->clearSilentFlag();
477 void AudioNode::enableOutputsIfNecessary()
479 if (m_isDisabled && m_connectionRefCount > 0) {
480 ASSERT(isMainThread());
481 AudioContext::AutoLocker locker(context());
483 m_isDisabled = false;
484 for (unsigned i = 0; i < m_outputs.size(); ++i)
489 void AudioNode::disableOutputsIfNecessary()
491 // Disable outputs if appropriate. We do this if the number of connections is 0 or 1. The case
492 // of 0 is from finishDeref() where there are no connections left. The case of 1 is from
493 // AudioNodeInput::disable() where we want to disable outputs when there's only one connection
494 // left because we're ready to go away, but can't quite yet.
495 if (m_connectionRefCount <= 1 && !m_isDisabled) {
496 // Still may have JavaScript references, but no more "active" connection references, so put all of our outputs in a "dormant" disabled state.
497 // Garbage collection may take a very long time after this time, so the "dormant" disabled nodes should not bog down the rendering...
499 // As far as JavaScript is concerned, our outputs must still appear to be connected.
500 // But internally our outputs should be disabled from the inputs they're connected to.
501 // disable() can recursively deref connections (and call disable()) down a whole chain of connected nodes.
503 // FIXME: we special case the convolver and delay since they have a significant tail-time and shouldn't be disconnected simply
504 // because they no longer have any input connections. This needs to be handled more generally where AudioNodes have
505 // a tailTime attribute. Then the AudioNode only needs to remain "active" for tailTime seconds after there are no
506 // longer any active connections.
507 if (nodeType() != NodeTypeConvolver && nodeType() != NodeTypeDelay) {
509 for (unsigned i = 0; i < m_outputs.size(); ++i)
510 output(i)->disable();
515 void AudioNode::ref(RefType refType)
519 atomicIncrement(&m_normalRefCount);
521 case RefTypeConnection:
522 atomicIncrement(&m_connectionRefCount);
525 ASSERT_NOT_REACHED();
528 #if DEBUG_AUDIONODE_REFERENCES
529 fprintf(stderr, "%p: %d: AudioNode::ref(%d) %d %d\n", this, nodeType(), refType, m_normalRefCount, m_connectionRefCount);
532 // See the disabling code in finishDeref() below. This handles the case where a node
533 // is being re-connected after being used at least once and disconnected.
534 // In this case, we need to re-enable.
535 if (refType == RefTypeConnection)
536 enableOutputsIfNecessary();
539 void AudioNode::deref(RefType refType)
541 // The actually work for deref happens completely within the audio context's graph lock.
542 // In the case of the audio thread, we must use a tryLock to avoid glitches.
543 bool hasLock = false;
544 bool mustReleaseLock = false;
546 if (context()->isAudioThread()) {
547 // Real-time audio thread must not contend lock (to avoid glitches).
548 hasLock = context()->tryLock(mustReleaseLock);
550 context()->lock(mustReleaseLock);
555 // This is where the real deref work happens.
556 finishDeref(refType);
561 // We were unable to get the lock, so put this in a list to finish up later.
562 ASSERT(context()->isAudioThread());
563 ASSERT(refType == RefTypeConnection);
564 context()->addDeferredFinishDeref(this);
567 // Once AudioContext::uninitialize() is called there's no more chances for deleteMarkedNodes() to get called, so we call here.
568 // We can't call in AudioContext::~AudioContext() since it will never be called as long as any AudioNode is alive
569 // because AudioNodes keep a reference to the context.
570 if (context()->isAudioThreadFinished())
571 context()->deleteMarkedNodes();
574 void AudioNode::finishDeref(RefType refType)
576 ASSERT(context()->isGraphOwner());
580 ASSERT(m_normalRefCount > 0);
581 atomicDecrement(&m_normalRefCount);
583 case RefTypeConnection:
584 ASSERT(m_connectionRefCount > 0);
585 atomicDecrement(&m_connectionRefCount);
588 ASSERT_NOT_REACHED();
591 #if DEBUG_AUDIONODE_REFERENCES
592 fprintf(stderr, "%p: %d: AudioNode::deref(%d) %d %d\n", this, nodeType(), refType, m_normalRefCount, m_connectionRefCount);
595 if (!m_connectionRefCount) {
596 if (!m_normalRefCount) {
597 if (!m_isMarkedForDeletion) {
598 // All references are gone - we need to go away.
599 for (unsigned i = 0; i < m_outputs.size(); ++i)
600 output(i)->disconnectAll(); // This will deref() nodes we're connected to.
602 // Mark for deletion at end of each render quantum or when context shuts down.
603 context()->markForDeletion(this);
604 m_isMarkedForDeletion = true;
606 } else if (refType == RefTypeConnection)
607 disableOutputsIfNecessary();
611 #if DEBUG_AUDIONODE_REFERENCES
613 bool AudioNode::s_isNodeCountInitialized = false;
614 int AudioNode::s_nodeCount[NodeTypeEnd];
616 void AudioNode::printNodeCounts()
618 fprintf(stderr, "\n\n");
619 fprintf(stderr, "===========================\n");
620 fprintf(stderr, "AudioNode: reference counts\n");
621 fprintf(stderr, "===========================\n");
623 for (unsigned i = 0; i < NodeTypeEnd; ++i)
624 fprintf(stderr, "%d: %d\n", i, s_nodeCount[i]);
626 fprintf(stderr, "===========================\n\n\n");
629 #endif // DEBUG_AUDIONODE_REFERENCES
631 } // namespace WebCore
633 #endif // ENABLE(WEB_AUDIO)