Home | History | Annotate | Download | only in webaudio
      1 /*
      2  * Copyright (C) 2012, 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 "modules/webaudio/MediaStreamAudioSourceNode.h"
     30 
     31 #include "core/platform/Logging.h"
     32 #include "modules/webaudio/AudioContext.h"
     33 #include "modules/webaudio/AudioNodeOutput.h"
     34 #include "wtf/Locker.h"
     35 
     36 namespace WebCore {
     37 
     38 PassRefPtr<MediaStreamAudioSourceNode> MediaStreamAudioSourceNode::create(AudioContext* context, MediaStream* mediaStream, AudioSourceProvider* audioSourceProvider)
     39 {
     40     return adoptRef(new MediaStreamAudioSourceNode(context, mediaStream, audioSourceProvider));
     41 }
     42 
     43 MediaStreamAudioSourceNode::MediaStreamAudioSourceNode(AudioContext* context, MediaStream* mediaStream, AudioSourceProvider* audioSourceProvider)
     44     : AudioSourceNode(context, context->sampleRate())
     45     , m_mediaStream(mediaStream)
     46     , m_audioSourceProvider(audioSourceProvider)
     47     , m_sourceNumberOfChannels(0)
     48 {
     49     ScriptWrappable::init(this);
     50     // Default to stereo. This could change depending on the format of the MediaStream's audio track.
     51     addOutput(adoptPtr(new AudioNodeOutput(this, 2)));
     52 
     53     setNodeType(NodeTypeMediaStreamAudioSource);
     54 
     55     initialize();
     56 }
     57 
     58 MediaStreamAudioSourceNode::~MediaStreamAudioSourceNode()
     59 {
     60     uninitialize();
     61 }
     62 
     63 void MediaStreamAudioSourceNode::setFormat(size_t numberOfChannels, float sourceSampleRate)
     64 {
     65     if (numberOfChannels != m_sourceNumberOfChannels || sourceSampleRate != sampleRate()) {
     66         // The sample-rate must be equal to the context's sample-rate.
     67         if (!numberOfChannels || numberOfChannels > AudioContext::maxNumberOfChannels() || sourceSampleRate != sampleRate()) {
     68             // process() will generate silence for these uninitialized values.
     69             LOG(Media, "MediaStreamAudioSourceNode::setFormat(%u, %f) - unhandled format change", static_cast<unsigned>(numberOfChannels), sourceSampleRate);
     70             m_sourceNumberOfChannels = 0;
     71             return;
     72         }
     73 
     74         // Synchronize with process().
     75         MutexLocker locker(m_processLock);
     76 
     77         m_sourceNumberOfChannels = numberOfChannels;
     78 
     79         {
     80             // The context must be locked when changing the number of output channels.
     81             AudioContext::AutoLocker contextLocker(context());
     82 
     83             // Do any necesssary re-configuration to the output's number of channels.
     84             output(0)->setNumberOfChannels(numberOfChannels);
     85         }
     86     }
     87 }
     88 
     89 void MediaStreamAudioSourceNode::process(size_t numberOfFrames)
     90 {
     91     AudioBus* outputBus = output(0)->bus();
     92 
     93     if (!audioSourceProvider()) {
     94         outputBus->zero();
     95         return;
     96     }
     97 
     98     if (!mediaStream() || m_sourceNumberOfChannels != outputBus->numberOfChannels()) {
     99         outputBus->zero();
    100         return;
    101     }
    102 
    103     // Use a tryLock() to avoid contention in the real-time audio thread.
    104     // If we fail to acquire the lock then the MediaStream must be in the middle of
    105     // a format change, so we output silence in this case.
    106     MutexTryLocker tryLocker(m_processLock);
    107     if (tryLocker.locked())
    108         audioSourceProvider()->provideInput(outputBus, numberOfFrames);
    109     else {
    110         // We failed to acquire the lock.
    111         outputBus->zero();
    112     }
    113 }
    114 
    115 void MediaStreamAudioSourceNode::reset()
    116 {
    117 }
    118 
    119 } // namespace WebCore
    120 
    121 #endif // ENABLE(WEB_AUDIO)
    122