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#ifndef AudioNodeInput_h
26#define AudioNodeInput_h
27
28#include "AudioBus.h"
29#include "AudioNode.h"
30#include "AudioSummingJunction.h"
31#include <wtf/HashSet.h>
32#include <wtf/Vector.h>
33
34namespace WebCore {
35
36class AudioNode;
37class AudioNodeOutput;
38
39// An AudioNodeInput represents an input to an AudioNode and can be connected from one or more AudioNodeOutputs.
40// In the case of multiple connections, the input will act as a unity-gain summing junction, mixing all the outputs.
41// The number of channels of the input's bus is the maximum of the number of channels of all its connections.
42
43class AudioNodeInput : public AudioSummingJunction {
44public:
45    explicit AudioNodeInput(AudioNode*);
46
47    // AudioSummingJunction
48    virtual bool canUpdateState() override { return !node()->isMarkedForDeletion(); }
49    virtual void didUpdate() override;
50
51    // Can be called from any thread.
52    AudioNode* node() const { return m_node; }
53
54    // Must be called with the context's graph lock.
55    void connect(AudioNodeOutput*);
56    void disconnect(AudioNodeOutput*);
57
58    // disable() will take the output out of the active connections list and set aside in a disabled list.
59    // enable() will put the output back into the active connections list.
60    // Must be called with the context's graph lock.
61    void enable(AudioNodeOutput*);
62    void disable(AudioNodeOutput*);
63
64    // pull() processes all of the AudioNodes connected to us.
65    // In the case of multiple connections it sums the result into an internal summing bus.
66    // In the single connection case, it allows in-place processing where possible using inPlaceBus.
67    // It returns the bus which it rendered into, returning inPlaceBus if in-place processing was performed.
68    // Called from context's audio thread.
69    AudioBus* pull(AudioBus* inPlaceBus, size_t framesToProcess);
70
71    // bus() contains the rendered audio after pull() has been called for each time quantum.
72    // Called from context's audio thread.
73    AudioBus* bus();
74
75    // updateInternalBus() updates m_internalSummingBus appropriately for the number of channels.
76    // This must be called when we own the context's graph lock in the audio thread at the very start or end of the render quantum.
77    void updateInternalBus();
78
79    // The number of channels of the connection with the largest number of channels.
80    unsigned numberOfChannels() const;
81
82private:
83    AudioNode* m_node;
84
85    // m_disabledOutputs contains the AudioNodeOutputs which are disabled (will not be processed) by the audio graph rendering.
86    // But, from JavaScript's perspective, these outputs are still connected to us.
87    // Generally, these represent disabled connections from "notes" which have finished playing but are not yet garbage collected.
88    HashSet<AudioNodeOutput*> m_disabledOutputs;
89
90    // Called from context's audio thread.
91    AudioBus* internalSummingBus();
92    void sumAllConnections(AudioBus* summingBus, size_t framesToProcess);
93
94    RefPtr<AudioBus> m_internalSummingBus;
95};
96
97} // namespace WebCore
98
99#endif // AudioNodeInput_h
100