1/*
2 * Copyright (C) 2012, Intel Corporation. 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/AudioBasicInspectorNode.h"
30
31#include "modules/webaudio/AudioContext.h"
32#include "modules/webaudio/AudioNodeInput.h"
33#include "modules/webaudio/AudioNodeOutput.h"
34
35namespace blink {
36
37AudioBasicInspectorNode::AudioBasicInspectorNode(AudioContext* context, float sampleRate, unsigned outputChannelCount)
38    : AudioNode(context, sampleRate)
39    , m_needAutomaticPull(false)
40{
41    addInput();
42    addOutput(AudioNodeOutput::create(this, outputChannelCount));
43}
44
45// We override pullInputs() as an optimization allowing this node to take advantage of in-place processing,
46// where the input is simply passed through unprocessed to the output.
47// Note: this only applies if the input and output channel counts match.
48void AudioBasicInspectorNode::pullInputs(size_t framesToProcess)
49{
50    // Render input stream - try to render directly into output bus for pass-through processing where process() doesn't need to do anything...
51    input(0)->pull(output(0)->bus(), framesToProcess);
52}
53
54void AudioBasicInspectorNode::connect(AudioNode* destination, unsigned outputIndex, unsigned inputIndex, ExceptionState& exceptionState)
55{
56    ASSERT(isMainThread());
57
58    AudioContext::AutoLocker locker(context());
59
60    AudioNode::connect(destination, outputIndex, inputIndex, exceptionState);
61    updatePullStatus();
62}
63
64void AudioBasicInspectorNode::disconnect(unsigned outputIndex, ExceptionState& exceptionState)
65{
66    ASSERT(isMainThread());
67
68    AudioContext::AutoLocker locker(context());
69
70    AudioNode::disconnect(outputIndex, exceptionState);
71    updatePullStatus();
72}
73
74void AudioBasicInspectorNode::checkNumberOfChannelsForInput(AudioNodeInput* input)
75{
76    ASSERT(context()->isAudioThread() && context()->isGraphOwner());
77
78    ASSERT(input == this->input(0));
79    if (input != this->input(0))
80        return;
81
82    unsigned numberOfChannels = input->numberOfChannels();
83
84    if (numberOfChannels != output(0)->numberOfChannels()) {
85        // This will propagate the channel count to any nodes connected further downstream in the graph.
86        output(0)->setNumberOfChannels(numberOfChannels);
87    }
88
89    AudioNode::checkNumberOfChannelsForInput(input);
90
91    updatePullStatus();
92}
93
94void AudioBasicInspectorNode::updatePullStatus()
95{
96    ASSERT(context()->isGraphOwner());
97
98    if (output(0)->isConnected()) {
99        // When an AudioBasicInspectorNode is connected to a downstream node, it will get pulled by the
100        // downstream node, thus remove it from the context's automatic pull list.
101        if (m_needAutomaticPull) {
102            context()->removeAutomaticPullNode(this);
103            m_needAutomaticPull = false;
104        }
105    } else {
106        unsigned numberOfInputConnections = input(0)->numberOfRenderingConnections();
107        if (numberOfInputConnections && !m_needAutomaticPull) {
108            // When an AudioBasicInspectorNode is not connected to any downstream node while still connected from
109            // upstream node(s), add it to the context's automatic pull list.
110            context()->addAutomaticPullNode(this);
111            m_needAutomaticPull = true;
112        } else if (!numberOfInputConnections && m_needAutomaticPull) {
113            // The AudioBasicInspectorNode is connected to nothing, remove it from the context's automatic pull list.
114            context()->removeAutomaticPullNode(this);
115            m_needAutomaticPull = false;
116        }
117    }
118}
119
120} // namespace blink
121
122#endif // ENABLE(WEB_AUDIO)
123