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 "AudioNode.h"
30
31#include "AudioContext.h"
32#include "AudioNodeInput.h"
33#include "AudioNodeOutput.h"
34#include <wtf/Atomics.h>
35
36namespace WebCore {
37
38AudioNode::AudioNode(AudioContext* context, double sampleRate)
39    : m_isInitialized(false)
40    , m_type(NodeTypeUnknown)
41    , m_context(context)
42    , m_sampleRate(sampleRate)
43    , m_lastProcessingTime(-1.0)
44    , m_normalRefCount(1) // start out with normal refCount == 1 (like WTF::RefCounted class)
45    , m_connectionRefCount(0)
46    , m_disabledRefCount(0)
47    , m_isMarkedForDeletion(false)
48    , m_isDisabled(false)
49{
50#if DEBUG_AUDIONODE_REFERENCES
51    if (!s_isNodeCountInitialized) {
52        s_isNodeCountInitialized = true;
53        atexit(AudioNode::printNodeCounts);
54    }
55#endif
56}
57
58AudioNode::~AudioNode()
59{
60#if DEBUG_AUDIONODE_REFERENCES
61    --s_nodeCount[type()];
62    printf("%p: %d: AudioNode::~AudioNode() %d %d %d\n", this, type(), m_normalRefCount, m_connectionRefCount, m_disabledRefCount);
63#endif
64}
65
66void AudioNode::initialize()
67{
68    m_isInitialized = true;
69}
70
71void AudioNode::uninitialize()
72{
73    m_isInitialized = false;
74}
75
76void AudioNode::setType(NodeType type)
77{
78    m_type = type;
79
80#if DEBUG_AUDIONODE_REFERENCES
81    ++s_nodeCount[type];
82#endif
83}
84
85void AudioNode::lazyInitialize()
86{
87    if (!isInitialized())
88        initialize();
89}
90
91void AudioNode::addInput(PassOwnPtr<AudioNodeInput> input)
92{
93    m_inputs.append(input);
94}
95
96void AudioNode::addOutput(PassOwnPtr<AudioNodeOutput> output)
97{
98    m_outputs.append(output);
99}
100
101AudioNodeInput* AudioNode::input(unsigned i)
102{
103    return m_inputs[i].get();
104}
105
106AudioNodeOutput* AudioNode::output(unsigned i)
107{
108    return m_outputs[i].get();
109}
110
111bool AudioNode::connect(AudioNode* destination, unsigned outputIndex, unsigned inputIndex)
112{
113    ASSERT(isMainThread());
114    AudioContext::AutoLocker locker(context());
115
116    // Sanity check input and output indices.
117    if (outputIndex >= numberOfOutputs())
118        return false;
119    if (destination && inputIndex >= destination->numberOfInputs())
120        return false;
121
122    AudioNodeOutput* output = this->output(outputIndex);
123    if (!destination) {
124        // Disconnect output from any inputs it may be currently connected to.
125        output->disconnectAllInputs();
126        return true;
127    }
128
129    AudioNodeInput* input = destination->input(inputIndex);
130    input->connect(output);
131
132    // Let context know that a connection has been made.
133    context()->incrementConnectionCount();
134
135    return true;
136}
137
138bool AudioNode::disconnect(unsigned outputIndex)
139{
140    ASSERT(isMainThread());
141    AudioContext::AutoLocker locker(context());
142
143    return connect(0, outputIndex);
144}
145
146void AudioNode::processIfNecessary(size_t framesToProcess)
147{
148    ASSERT(context()->isAudioThread());
149
150    if (!isInitialized())
151        return;
152
153    // Ensure that we only process once per rendering quantum.
154    // This handles the "fanout" problem where an output is connected to multiple inputs.
155    // The first time we're called during this time slice we process, but after that we don't want to re-process,
156    // instead our output(s) will already have the results cached in their bus;
157    double currentTime = context()->currentTime();
158    if (m_lastProcessingTime != currentTime) {
159        m_lastProcessingTime = currentTime; // important to first update this time because of feedback loops in the rendering graph
160        pullInputs(framesToProcess);
161        process(framesToProcess);
162    }
163}
164
165void AudioNode::pullInputs(size_t framesToProcess)
166{
167    ASSERT(context()->isAudioThread());
168
169    // Process all of the AudioNodes connected to our inputs.
170    for (unsigned i = 0; i < m_inputs.size(); ++i)
171        input(i)->pull(0, framesToProcess);
172}
173
174void AudioNode::ref(RefType refType)
175{
176    switch (refType) {
177    case RefTypeNormal:
178        atomicIncrement(&m_normalRefCount);
179        break;
180    case RefTypeConnection:
181        atomicIncrement(&m_connectionRefCount);
182        break;
183    case RefTypeDisabled:
184        atomicIncrement(&m_disabledRefCount);
185        break;
186    default:
187        ASSERT_NOT_REACHED();
188    }
189
190#if DEBUG_AUDIONODE_REFERENCES
191    printf("%p: %d: AudioNode::ref(%d) %d %d %d\n", this, type(), refType, m_normalRefCount, m_connectionRefCount, m_disabledRefCount);
192#endif
193
194    if (m_connectionRefCount == 1 && refType == RefTypeConnection) {
195        // FIXME: implement wake-up - this is an advanced feature and is not necessary in a simple implementation.
196        // We should not be "actively" connected to anything, but now we're "waking up"
197        // For example, a note which has finished playing, but is now being played again.
198        // Note that if this is considered a worthwhile feature to add, then an evaluation of the locking considerations must be made.
199    }
200}
201
202void AudioNode::deref(RefType refType)
203{
204    // The actually work for deref happens completely within the audio context's graph lock.
205    // In the case of the audio thread, we must use a tryLock to avoid glitches.
206    bool hasLock = false;
207    bool mustReleaseLock = false;
208
209    if (context()->isAudioThread()) {
210        // Real-time audio thread must not contend lock (to avoid glitches).
211        hasLock = context()->tryLock(mustReleaseLock);
212    } else {
213        context()->lock(mustReleaseLock);
214        hasLock = true;
215    }
216
217    if (hasLock) {
218        // This is where the real deref work happens.
219        finishDeref(refType);
220
221        if (mustReleaseLock)
222            context()->unlock();
223    } else {
224        // We were unable to get the lock, so put this in a list to finish up later.
225        ASSERT(context()->isAudioThread());
226        context()->addDeferredFinishDeref(this, refType);
227    }
228
229    // Once AudioContext::uninitialize() is called there's no more chances for deleteMarkedNodes() to get called, so we call here.
230    // We can't call in AudioContext::~AudioContext() since it will never be called as long as any AudioNode is alive
231    // because AudioNodes keep a reference to the context.
232    if (context()->isAudioThreadFinished())
233        context()->deleteMarkedNodes();
234}
235
236void AudioNode::finishDeref(RefType refType)
237{
238    ASSERT(context()->isGraphOwner());
239
240    switch (refType) {
241    case RefTypeNormal:
242        ASSERT(m_normalRefCount > 0);
243        atomicDecrement(&m_normalRefCount);
244        break;
245    case RefTypeConnection:
246        ASSERT(m_connectionRefCount > 0);
247        atomicDecrement(&m_connectionRefCount);
248        break;
249    case RefTypeDisabled:
250        ASSERT(m_disabledRefCount > 0);
251        atomicDecrement(&m_disabledRefCount);
252        break;
253    default:
254        ASSERT_NOT_REACHED();
255    }
256
257#if DEBUG_AUDIONODE_REFERENCES
258    printf("%p: %d: AudioNode::deref(%d) %d %d %d\n", this, type(), refType, m_normalRefCount, m_connectionRefCount, m_disabledRefCount);
259#endif
260
261    if (!m_connectionRefCount) {
262        if (!m_normalRefCount && !m_disabledRefCount) {
263            if (!m_isMarkedForDeletion) {
264                // All references are gone - we need to go away.
265                for (unsigned i = 0; i < m_outputs.size(); ++i)
266                    output(i)->disconnectAllInputs(); // this will deref() nodes we're connected to...
267
268                // Mark for deletion at end of each render quantum or when context shuts down.
269                context()->markForDeletion(this);
270                m_isMarkedForDeletion = true;
271            }
272        } else if (refType == RefTypeConnection) {
273            if (!m_isDisabled) {
274                // Still may have JavaScript references, but no more "active" connection references, so put all of our outputs in a "dormant" disabled state.
275                // Garbage collection may take a very long time after this time, so the "dormant" disabled nodes should not bog down the rendering...
276
277                // As far as JavaScript is concerned, our outputs must still appear to be connected.
278                // But internally our outputs should be disabled from the inputs they're connected to.
279                // disable() can recursively deref connections (and call disable()) down a whole chain of connected nodes.
280
281                // FIXME: we special case the convolver and delay since they have a significant tail-time and shouldn't be disconnected simply
282                // because they no longer have any input connections.  This needs to be handled more generally where AudioNodes have
283                // a tailTime attribute.  Then the AudioNode only needs to remain "active" for tailTime seconds after there are no
284                // longer any active connections.
285                if (type() != NodeTypeConvolver && type() != NodeTypeDelay) {
286                    m_isDisabled = true;
287                    for (unsigned i = 0; i < m_outputs.size(); ++i)
288                        output(i)->disable();
289                }
290            }
291        }
292    }
293}
294
295#if DEBUG_AUDIONODE_REFERENCES
296
297bool AudioNode::s_isNodeCountInitialized = false;
298int AudioNode::s_nodeCount[NodeTypeEnd];
299
300void AudioNode::printNodeCounts()
301{
302    printf("\n\n");
303    printf("===========================\n");
304    printf("AudioNode: reference counts\n");
305    printf("===========================\n");
306
307    for (unsigned i = 0; i < NodeTypeEnd; ++i)
308        printf("%d: %d\n", i, s_nodeCount[i]);
309
310    printf("===========================\n\n\n");
311}
312
313#endif // DEBUG_AUDIONODE_REFERENCES
314
315} // namespace WebCore
316
317#endif // ENABLE(WEB_AUDIO)
318