crogers@google.com | fe109dd | 2010-10-27 02:31:35 +0000 | [diff] [blame] | 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 AudioNodeOutput_h |
| 26 | #define AudioNodeOutput_h |
| 27 | |
| 28 | #include "AudioBus.h" |
| 29 | #include "AudioNode.h" |
| 30 | #include <wtf/HashSet.h> |
| 31 | #include <wtf/OwnPtr.h> |
| 32 | #include <wtf/Vector.h> |
| 33 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | class AudioContext; |
| 37 | class AudioNodeInput; |
| 38 | |
| 39 | // AudioNodeOutput represents a single output for an AudioNode. |
| 40 | // It may be connected to one or more AudioNodeInputs. |
| 41 | |
| 42 | class AudioNodeOutput { |
| 43 | public: |
| 44 | // It's OK to pass 0 for numberOfChannels in which case setNumberOfChannels() must be called later on. |
| 45 | AudioNodeOutput(AudioNode*, unsigned numberOfChannels); |
| 46 | |
| 47 | // Can be called from any thread. |
| 48 | AudioNode* node() const { return m_node; } |
| 49 | AudioContext* context() { return m_node->context(); } |
| 50 | |
| 51 | // Causes our AudioNode to process if it hasn't already for this render quantum. |
| 52 | // It returns the bus containing the processed audio for this output, returning inPlaceBus if in-place processing was possible. |
| 53 | // Called from context's audio thread. |
| 54 | AudioBus* pull(AudioBus* inPlaceBus, size_t framesToProcess); |
| 55 | |
| 56 | // bus() will contain the rendered audio after pull() is called for each rendering time quantum. |
| 57 | // Called from context's audio thread. |
| 58 | AudioBus* bus() const; |
| 59 | |
| 60 | // fanOutCount() is the number of AudioNodeInputs that we're connected to. |
| 61 | // This function should not be called in audio thread rendering code, instead renderingFanOutCount() should be used. |
| 62 | // It must be called with the context's graph lock. |
| 63 | unsigned fanOutCount(); |
| 64 | |
| 65 | // renderingFanOutCount() is the number of AudioNodeInputs that we're connected to during rendering. |
| 66 | // Unlike fanOutCount() it will not change during the course of a render quantum. |
| 67 | unsigned renderingFanOutCount() const; |
| 68 | |
| 69 | // It must be called with the context's graph lock. |
| 70 | void disconnectAllInputs(); |
| 71 | |
| 72 | void setNumberOfChannels(unsigned); |
| 73 | unsigned numberOfChannels() const { return m_numberOfChannels; } |
| 74 | bool isChannelCountKnown() const { return numberOfChannels() > 0; } |
| 75 | |
| 76 | // Disable/Enable happens when there are still JavaScript references to a node, but it has otherwise "finished" its work. |
| 77 | // For example, when a note has finished playing. It is kept around, because it may be played again at a later time. |
| 78 | // They must be called with the context's graph lock. |
| 79 | void disable(); |
| 80 | void enable(); |
| 81 | |
| 82 | // updateRenderingState() is called in the audio thread at the start or end of the render quantum to handle any recent changes to the graph state. |
| 83 | // It must be called with the context's graph lock. |
| 84 | void updateRenderingState(); |
| 85 | |
| 86 | private: |
| 87 | AudioNode* m_node; |
| 88 | |
| 89 | friend class AudioNodeInput; |
| 90 | |
| 91 | // These are called from AudioNodeInput. |
| 92 | // They must be called with the context's graph lock. |
| 93 | void addInput(AudioNodeInput*); |
| 94 | void removeInput(AudioNodeInput*); |
| 95 | |
commit-queue@webkit.org | 694eac3 | 2012-01-19 03:22:57 +0000 | [diff] [blame] | 96 | // updateInternalBus() updates m_internalBus appropriately for the number of channels. |
crogers@google.com | fe109dd | 2010-10-27 02:31:35 +0000 | [diff] [blame] | 97 | // It is called in the constructor or in the audio thread with the context's graph lock. |
commit-queue@webkit.org | 694eac3 | 2012-01-19 03:22:57 +0000 | [diff] [blame] | 98 | void updateInternalBus(); |
crogers@google.com | fe109dd | 2010-10-27 02:31:35 +0000 | [diff] [blame] | 99 | |
| 100 | // Announce to any nodes we're connected to that we changed our channel count for its input. |
| 101 | // It must be called in the audio thread with the context's graph lock. |
| 102 | void propagateChannelCount(); |
| 103 | |
| 104 | // updateNumberOfChannels() is called in the audio thread at the start or end of the render quantum to pick up channel changes. |
| 105 | // It must be called with the context's graph lock. |
| 106 | void updateNumberOfChannels(); |
| 107 | |
| 108 | // m_numberOfChannels will only be changed in the audio thread. |
| 109 | // The main thread sets m_desiredNumberOfChannels which will later get picked up in the audio thread in updateNumberOfChannels(). |
| 110 | unsigned m_numberOfChannels; |
| 111 | unsigned m_desiredNumberOfChannels; |
| 112 | |
commit-queue@webkit.org | 694eac3 | 2012-01-19 03:22:57 +0000 | [diff] [blame] | 113 | // m_internalBus must only be changed in the audio thread with the context's graph lock (or constructor). |
| 114 | OwnPtr<AudioBus> m_internalBus; |
crogers@google.com | fe109dd | 2010-10-27 02:31:35 +0000 | [diff] [blame] | 115 | |
| 116 | // m_actualDestinationBus is set in pull() and will either point to one of our internal busses or to the in-place bus. |
| 117 | // It must only be changed in the audio thread (or constructor). |
| 118 | AudioBus* m_actualDestinationBus; |
| 119 | |
| 120 | HashSet<AudioNodeInput*> m_inputs; |
| 121 | typedef HashSet<AudioNodeInput*>::iterator InputsIterator; |
| 122 | bool m_isEnabled; |
| 123 | |
| 124 | // For the purposes of rendering, keeps track of the number of inputs we're connected to. |
| 125 | // This value should only be changed at the very start or end of the rendering quantum. |
| 126 | unsigned m_renderingFanOutCount; |
| 127 | }; |
| 128 | |
| 129 | } // namespace WebCore |
| 130 | |
| 131 | #endif // AudioNodeOutput_h |