2 * Copyright (C) 2011, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #if ENABLE(WEB_AUDIO) && ENABLE(VIDEO)
29 #include "MediaElementAudioSourceNode.h"
31 #include "AudioContext.h"
32 #include "AudioNodeOutput.h"
33 #include "MediaPlayer.h"
37 PassRefPtr<MediaElementAudioSourceNode> MediaElementAudioSourceNode::create(AudioContext* context, HTMLMediaElement* mediaElement)
39 return adoptRef(new MediaElementAudioSourceNode(context, mediaElement));
42 MediaElementAudioSourceNode::MediaElementAudioSourceNode(AudioContext* context, HTMLMediaElement* mediaElement)
43 : AudioSourceNode(context, context->sampleRate())
44 , m_mediaElement(mediaElement)
46 // Default to stereo. This could change depending on what the media element .src is set to.
47 addOutput(adoptPtr(new AudioNodeOutput(this, 2)));
49 setNodeType(NodeTypeMediaElementAudioSource);
54 MediaElementAudioSourceNode::~MediaElementAudioSourceNode()
56 m_mediaElement->setAudioSourceNode(0);
60 void MediaElementAudioSourceNode::setFormat(size_t, float)
62 // FIXME: setup a sample-rate converter if necessary to convert to the AudioContext sample-rate.
66 void MediaElementAudioSourceNode::process(size_t numberOfFrames)
68 AudioBus* outputBus = output(0)->bus();
70 if (!mediaElement()) {
75 // Use a tryLock() to avoid contention in the real-time audio thread.
76 // If we fail to acquire the lock then the HTMLMediaElement must be in the middle of
77 // reconfiguring its playback engine, so we output silence in this case.
78 if (m_processLock.tryLock()) {
79 if (AudioSourceProvider* provider = mediaElement()->audioSourceProvider())
80 provider->provideInput(outputBus, numberOfFrames);
83 m_processLock.unlock();
88 void MediaElementAudioSourceNode::reset()
92 void MediaElementAudioSourceNode::lock()
98 void MediaElementAudioSourceNode::unlock()
100 m_processLock.unlock();
104 } // namespace WebCore
106 #endif // ENABLE(WEB_AUDIO)