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