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 #include "config.h"
     26 
     27 #if ENABLE(WEB_AUDIO)
     28 
     29 #include "AudioNodeOutput.h"
     30 
     31 #include "AudioBus.h"
     32 #include "AudioContext.h"
     33 #include "AudioNodeInput.h"
     34 #include <wtf/Threading.h>
     35 
     36 namespace WebCore {
     37 
     38 AudioNodeOutput::AudioNodeOutput(AudioNode* node, unsigned numberOfChannels)
     39     : m_node(node)
     40     , m_numberOfChannels(numberOfChannels)
     41     , m_desiredNumberOfChannels(numberOfChannels)
     42     , m_internalOutputBus(0)
     43     , m_actualDestinationBus(0)
     44     , m_isEnabled(true)
     45     , m_renderingFanOutCount(0)
     46 {
     47     m_monoInternalBus = adoptPtr(new AudioBus(1, AudioNode::ProcessingSizeInFrames));
     48     m_stereoInternalBus = adoptPtr(new AudioBus(2, AudioNode::ProcessingSizeInFrames));
     49     setInternalBus();
     50 }
     51 
     52 void AudioNodeOutput::setNumberOfChannels(unsigned numberOfChannels)
     53 {
     54     ASSERT(context()->isGraphOwner());
     55 
     56     m_desiredNumberOfChannels = numberOfChannels;
     57 
     58     if (context()->isAudioThread()) {
     59         // If we're in the audio thread then we can take care of it right away (we should be at the very start or end of a rendering quantum).
     60         updateNumberOfChannels();
     61     } else {
     62         // Let the context take care of it in the audio thread in the pre and post render tasks.
     63         context()->markAudioNodeOutputDirty(this);
     64     }
     65 }
     66 
     67 void AudioNodeOutput::setInternalBus()
     68 {
     69     switch (m_numberOfChannels) {
     70     case 0:
     71     case 1:
     72         m_internalOutputBus = m_monoInternalBus.get();
     73         break;
     74     case 2:
     75         m_internalOutputBus = m_stereoInternalBus.get();
     76         break;
     77     default:
     78         // FIXME: later we can fully implement more than stereo, 5.1, etc.
     79         ASSERT_NOT_REACHED();
     80     }
     81 
     82     // This may later be changed in pull() to point to an in-place bus with the same number of channels.
     83     m_actualDestinationBus = m_internalOutputBus;
     84 }
     85 
     86 void AudioNodeOutput::updateRenderingState()
     87 {
     88     updateNumberOfChannels();
     89     m_renderingFanOutCount = fanOutCount();
     90 }
     91 
     92 void AudioNodeOutput::updateNumberOfChannels()
     93 {
     94     ASSERT(context()->isAudioThread() && context()->isGraphOwner());
     95 
     96     if (m_numberOfChannels != m_desiredNumberOfChannels) {
     97         m_numberOfChannels = m_desiredNumberOfChannels;
     98         setInternalBus();
     99         propagateChannelCount();
    100     }
    101 }
    102 
    103 void AudioNodeOutput::propagateChannelCount()
    104 {
    105     ASSERT(context()->isAudioThread() && context()->isGraphOwner());
    106 
    107     if (isChannelCountKnown()) {
    108         // Announce to any nodes we're connected to that we changed our channel count for its input.
    109         for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
    110             AudioNodeInput* input = *i;
    111             AudioNode* connectionNode = input->node();
    112             connectionNode->checkNumberOfChannelsForInput(input);
    113         }
    114     }
    115 }
    116 
    117 AudioBus* AudioNodeOutput::pull(AudioBus* inPlaceBus, size_t framesToProcess)
    118 {
    119     ASSERT(context()->isAudioThread());
    120     ASSERT(m_renderingFanOutCount > 0);
    121 
    122     // Causes our AudioNode to process if it hasn't already for this render quantum.
    123     // We try to do in-place processing (using inPlaceBus) if at all possible,
    124     // but we can't process in-place if we're connected to more than one input (fan-out > 1).
    125     // In this case pull() is called multiple times per rendering quantum, and the processIfNecessary() call below will
    126     // cause our node to process() only the first time, caching the output in m_internalOutputBus for subsequent calls.
    127 
    128     bool isInPlace = inPlaceBus && inPlaceBus->numberOfChannels() == numberOfChannels() && m_renderingFanOutCount == 1;
    129 
    130     // Setup the actual destination bus for processing when our node's process() method gets called in processIfNecessary() below.
    131     m_actualDestinationBus = isInPlace ? inPlaceBus : m_internalOutputBus;
    132 
    133     node()->processIfNecessary(framesToProcess);
    134     return m_actualDestinationBus;
    135 }
    136 
    137 AudioBus* AudioNodeOutput::bus() const
    138 {
    139     ASSERT(const_cast<AudioNodeOutput*>(this)->context()->isAudioThread());
    140     ASSERT(m_actualDestinationBus);
    141     return m_actualDestinationBus;
    142 }
    143 
    144 unsigned AudioNodeOutput::renderingFanOutCount() const
    145 {
    146     return m_renderingFanOutCount;
    147 }
    148 
    149 unsigned AudioNodeOutput::fanOutCount()
    150 {
    151     ASSERT(context()->isGraphOwner());
    152     return m_inputs.size();
    153 }
    154 
    155 void AudioNodeOutput::addInput(AudioNodeInput* input)
    156 {
    157     ASSERT(context()->isGraphOwner());
    158 
    159     ASSERT(input);
    160     if (!input)
    161         return;
    162 
    163     m_inputs.add(input);
    164 }
    165 
    166 void AudioNodeOutput::removeInput(AudioNodeInput* input)
    167 {
    168     ASSERT(context()->isGraphOwner());
    169 
    170     ASSERT(input);
    171     if (!input)
    172         return;
    173 
    174     m_inputs.remove(input);
    175 }
    176 
    177 void AudioNodeOutput::disconnectAllInputs()
    178 {
    179     ASSERT(context()->isGraphOwner());
    180 
    181     // AudioNodeInput::disconnect() changes m_inputs by calling removeInput().
    182     while (!m_inputs.isEmpty()) {
    183         AudioNodeInput* input = *m_inputs.begin();
    184         input->disconnect(this);
    185     }
    186 }
    187 
    188 void AudioNodeOutput::disable()
    189 {
    190     ASSERT(context()->isGraphOwner());
    191 
    192     if (m_isEnabled) {
    193         for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
    194             AudioNodeInput* input = *i;
    195             input->disable(this);
    196         }
    197         m_isEnabled = false;
    198     }
    199 }
    200 
    201 void AudioNodeOutput::enable()
    202 {
    203     ASSERT(context()->isGraphOwner());
    204 
    205     if (!m_isEnabled) {
    206         for (InputsIterator i = m_inputs.begin(); i != m_inputs.end(); ++i) {
    207             AudioNodeInput* input = *i;
    208             input->enable(this);
    209         }
    210         m_isEnabled = true;
    211     }
    212 }
    213 
    214 } // namespace WebCore
    215 
    216 #endif // ENABLE(WEB_AUDIO)
    217