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