Home | History | Annotate | Download | only in webaudio
      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 
     96     // setInternalBus() sets m_internalOutputBus appropriately for the number of channels.
     97     // It is called in the constructor or in the audio thread with the context's graph lock.
     98     void setInternalBus();
     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 
    113     // m_internalOutputBus will point to either m_monoInternalBus or m_stereoInternalBus.
    114     // It must only be changed in the audio thread (or constructor).
    115     AudioBus* m_internalOutputBus;
    116     OwnPtr<AudioBus> m_monoInternalBus;
    117     OwnPtr<AudioBus> m_stereoInternalBus;
    118 
    119     // m_actualDestinationBus is set in pull() and will either point to one of our internal busses or to the in-place bus.
    120     // It must only be changed in the audio thread (or constructor).
    121     AudioBus* m_actualDestinationBus;
    122 
    123     HashSet<AudioNodeInput*> m_inputs;
    124     typedef HashSet<AudioNodeInput*>::iterator InputsIterator;
    125     bool m_isEnabled;
    126 
    127     // For the purposes of rendering, keeps track of the number of inputs we're connected to.
    128     // This value should only be changed at the very start or end of the rendering quantum.
    129     unsigned m_renderingFanOutCount;
    130 };
    131 
    132 } // namespace WebCore
    133 
    134 #endif // AudioNodeOutput_h
    135