OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012, Google Inc. All rights reserved. | 2 * Copyright (C) 2012, Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include "modules/webaudio/AudioNodeOutput.h" | 29 #include "modules/webaudio/AudioNodeOutput.h" |
30 #include "modules/webaudio/BaseAudioContext.h" | 30 #include "modules/webaudio/BaseAudioContext.h" |
31 #include "modules/webaudio/MediaStreamAudioSourceOptions.h" | 31 #include "modules/webaudio/MediaStreamAudioSourceOptions.h" |
32 #include "wtf/Locker.h" | 32 #include "wtf/Locker.h" |
33 #include <memory> | 33 #include <memory> |
34 | 34 |
35 namespace blink { | 35 namespace blink { |
36 | 36 |
37 MediaStreamAudioSourceHandler::MediaStreamAudioSourceHandler( | 37 MediaStreamAudioSourceHandler::MediaStreamAudioSourceHandler( |
38 AudioNode& node, | 38 AudioNode& node, |
39 MediaStream& mediaStream, | |
40 MediaStreamTrack* audioTrack, | |
41 std::unique_ptr<AudioSourceProvider> audioSourceProvider) | 39 std::unique_ptr<AudioSourceProvider> audioSourceProvider) |
42 : AudioHandler(NodeTypeMediaStreamAudioSource, | 40 : AudioHandler(NodeTypeMediaStreamAudioSource, |
43 node, | 41 node, |
44 node.context()->sampleRate()), | 42 node.context()->sampleRate()), |
45 m_mediaStream(mediaStream), | |
46 m_audioTrack(audioTrack), | |
47 m_audioSourceProvider(std::move(audioSourceProvider)), | 43 m_audioSourceProvider(std::move(audioSourceProvider)), |
48 m_sourceNumberOfChannels(0) { | 44 m_sourceNumberOfChannels(0) { |
49 // Default to stereo. This could change depending on the format of the | 45 // Default to stereo. This could change depending on the format of the |
50 // MediaStream's audio track. | 46 // MediaStream's audio track. |
51 addOutput(2); | 47 addOutput(2); |
52 | 48 |
53 initialize(); | 49 initialize(); |
54 } | 50 } |
55 | 51 |
56 PassRefPtr<MediaStreamAudioSourceHandler> MediaStreamAudioSourceHandler::create( | 52 PassRefPtr<MediaStreamAudioSourceHandler> MediaStreamAudioSourceHandler::create( |
57 AudioNode& node, | 53 AudioNode& node, |
58 MediaStream& mediaStream, | |
59 MediaStreamTrack* audioTrack, | |
60 std::unique_ptr<AudioSourceProvider> audioSourceProvider) { | 54 std::unique_ptr<AudioSourceProvider> audioSourceProvider) { |
61 return adoptRef(new MediaStreamAudioSourceHandler( | 55 return adoptRef( |
62 node, mediaStream, audioTrack, std::move(audioSourceProvider))); | 56 new MediaStreamAudioSourceHandler(node, std::move(audioSourceProvider))); |
63 } | 57 } |
64 | 58 |
65 MediaStreamAudioSourceHandler::~MediaStreamAudioSourceHandler() { | 59 MediaStreamAudioSourceHandler::~MediaStreamAudioSourceHandler() { |
66 uninitialize(); | 60 uninitialize(); |
67 } | 61 } |
68 | 62 |
69 void MediaStreamAudioSourceHandler::setFormat(size_t numberOfChannels, | 63 void MediaStreamAudioSourceHandler::setFormat(size_t numberOfChannels, |
70 float sourceSampleRate) { | 64 float sourceSampleRate) { |
71 if (numberOfChannels != m_sourceNumberOfChannels || | 65 if (numberOfChannels != m_sourceNumberOfChannels || |
72 sourceSampleRate != context()->sampleRate()) { | 66 sourceSampleRate != context()->sampleRate()) { |
(...skipping 24 matching lines...) Expand all Loading... |
97 } | 91 } |
98 | 92 |
99 void MediaStreamAudioSourceHandler::process(size_t numberOfFrames) { | 93 void MediaStreamAudioSourceHandler::process(size_t numberOfFrames) { |
100 AudioBus* outputBus = output(0).bus(); | 94 AudioBus* outputBus = output(0).bus(); |
101 | 95 |
102 if (!getAudioSourceProvider()) { | 96 if (!getAudioSourceProvider()) { |
103 outputBus->zero(); | 97 outputBus->zero(); |
104 return; | 98 return; |
105 } | 99 } |
106 | 100 |
107 if (!getMediaStream() || | 101 if (m_sourceNumberOfChannels != outputBus->numberOfChannels()) { |
108 m_sourceNumberOfChannels != outputBus->numberOfChannels()) { | |
109 outputBus->zero(); | 102 outputBus->zero(); |
110 return; | 103 return; |
111 } | 104 } |
112 | 105 |
113 // Use a tryLock() to avoid contention in the real-time audio thread. | 106 // Use a tryLock() to avoid contention in the real-time audio thread. |
114 // If we fail to acquire the lock then the MediaStream must be in the middle | 107 // If we fail to acquire the lock then the MediaStream must be in the middle |
115 // of a format change, so we output silence in this case. | 108 // of a format change, so we output silence in this case. |
116 MutexTryLocker tryLocker(m_processLock); | 109 MutexTryLocker tryLocker(m_processLock); |
117 if (tryLocker.locked()) { | 110 if (tryLocker.locked()) { |
118 getAudioSourceProvider()->provideInput(outputBus, numberOfFrames); | 111 getAudioSourceProvider()->provideInput(outputBus, numberOfFrames); |
119 } else { | 112 } else { |
120 // We failed to acquire the lock. | 113 // We failed to acquire the lock. |
121 outputBus->zero(); | 114 outputBus->zero(); |
122 } | 115 } |
123 } | 116 } |
124 | 117 |
125 // ---------------------------------------------------------------- | 118 // ---------------------------------------------------------------- |
126 | 119 |
127 MediaStreamAudioSourceNode::MediaStreamAudioSourceNode( | 120 MediaStreamAudioSourceNode::MediaStreamAudioSourceNode( |
128 BaseAudioContext& context, | 121 BaseAudioContext& context, |
129 MediaStream& mediaStream, | 122 MediaStream& mediaStream, |
130 MediaStreamTrack* audioTrack, | |
131 std::unique_ptr<AudioSourceProvider> audioSourceProvider) | 123 std::unique_ptr<AudioSourceProvider> audioSourceProvider) |
132 : AudioNode(context) { | 124 : AudioNode(context), m_mediaStream(mediaStream) { |
133 setHandler(MediaStreamAudioSourceHandler::create( | 125 setHandler(MediaStreamAudioSourceHandler::create( |
134 *this, mediaStream, audioTrack, std::move(audioSourceProvider))); | 126 *this, std::move(audioSourceProvider))); |
135 } | 127 } |
136 | 128 |
137 MediaStreamAudioSourceNode* MediaStreamAudioSourceNode::create( | 129 MediaStreamAudioSourceNode* MediaStreamAudioSourceNode::create( |
138 BaseAudioContext& context, | 130 BaseAudioContext& context, |
139 MediaStream& mediaStream, | 131 MediaStream& mediaStream, |
140 ExceptionState& exceptionState) { | 132 ExceptionState& exceptionState) { |
141 DCHECK(isMainThread()); | 133 DCHECK(isMainThread()); |
142 | 134 |
143 if (context.isContextClosed()) { | 135 if (context.isContextClosed()) { |
144 context.throwExceptionForClosedState(exceptionState); | 136 context.throwExceptionForClosedState(exceptionState); |
145 return nullptr; | 137 return nullptr; |
146 } | 138 } |
147 | 139 |
148 MediaStreamTrackVector audioTracks = mediaStream.getAudioTracks(); | 140 MediaStreamTrackVector audioTracks = mediaStream.getAudioTracks(); |
149 if (audioTracks.isEmpty()) { | 141 if (audioTracks.isEmpty()) { |
150 exceptionState.throwDOMException(InvalidStateError, | 142 exceptionState.throwDOMException(InvalidStateError, |
151 "MediaStream has no audio track"); | 143 "MediaStream has no audio track"); |
152 return nullptr; | 144 return nullptr; |
153 } | 145 } |
154 | 146 |
155 // Use the first audio track in the media stream. | 147 // Use the first audio track in the media stream. |
156 MediaStreamTrack* audioTrack = audioTracks[0]; | 148 MediaStreamTrack* audioTrack = audioTracks[0]; |
157 std::unique_ptr<AudioSourceProvider> provider = | 149 std::unique_ptr<AudioSourceProvider> provider = |
158 audioTrack->createWebAudioSource(); | 150 audioTrack->createWebAudioSource(); |
159 | 151 |
160 MediaStreamAudioSourceNode* node = new MediaStreamAudioSourceNode( | 152 MediaStreamAudioSourceNode* node = |
161 context, mediaStream, audioTrack, std::move(provider)); | 153 new MediaStreamAudioSourceNode(context, mediaStream, std::move(provider)); |
162 | 154 |
163 if (!node) | 155 if (!node) |
164 return nullptr; | 156 return nullptr; |
165 | 157 |
166 // TODO(hongchan): Only stereo streams are supported right now. We should be | 158 // TODO(hongchan): Only stereo streams are supported right now. We should be |
167 // able to accept multi-channel streams. | 159 // able to accept multi-channel streams. |
168 node->setFormat(2, context.sampleRate()); | 160 node->setFormat(2, context.sampleRate()); |
169 // context keeps reference until node is disconnected | 161 // context keeps reference until node is disconnected |
170 context.notifySourceNodeStartedProcessing(node); | 162 context.notifySourceNodeStartedProcessing(node); |
171 | 163 |
172 return node; | 164 return node; |
173 } | 165 } |
174 | 166 |
175 MediaStreamAudioSourceNode* MediaStreamAudioSourceNode::create( | 167 MediaStreamAudioSourceNode* MediaStreamAudioSourceNode::create( |
176 BaseAudioContext* context, | 168 BaseAudioContext* context, |
177 const MediaStreamAudioSourceOptions& options, | 169 const MediaStreamAudioSourceOptions& options, |
178 ExceptionState& exceptionState) { | 170 ExceptionState& exceptionState) { |
179 return create(*context, *options.mediaStream(), exceptionState); | 171 return create(*context, *options.mediaStream(), exceptionState); |
180 } | 172 } |
181 | 173 |
182 DEFINE_TRACE(MediaStreamAudioSourceNode) { | 174 DEFINE_TRACE(MediaStreamAudioSourceNode) { |
| 175 visitor->trace(m_mediaStream); |
183 AudioSourceProviderClient::trace(visitor); | 176 AudioSourceProviderClient::trace(visitor); |
184 AudioNode::trace(visitor); | 177 AudioNode::trace(visitor); |
185 } | 178 } |
186 | 179 |
187 MediaStreamAudioSourceHandler& | 180 MediaStreamAudioSourceHandler& |
188 MediaStreamAudioSourceNode::mediaStreamAudioSourceHandler() const { | 181 MediaStreamAudioSourceNode::mediaStreamAudioSourceHandler() const { |
189 return static_cast<MediaStreamAudioSourceHandler&>(handler()); | 182 return static_cast<MediaStreamAudioSourceHandler&>(handler()); |
190 } | 183 } |
191 | 184 |
192 MediaStream* MediaStreamAudioSourceNode::getMediaStream() const { | 185 MediaStream* MediaStreamAudioSourceNode::getMediaStream() const { |
193 return mediaStreamAudioSourceHandler().getMediaStream(); | 186 return m_mediaStream; |
194 } | 187 } |
195 | 188 |
196 void MediaStreamAudioSourceNode::setFormat(size_t numberOfChannels, | 189 void MediaStreamAudioSourceNode::setFormat(size_t numberOfChannels, |
197 float sourceSampleRate) { | 190 float sourceSampleRate) { |
198 mediaStreamAudioSourceHandler().setFormat(numberOfChannels, sourceSampleRate); | 191 mediaStreamAudioSourceHandler().setFormat(numberOfChannels, sourceSampleRate); |
199 } | 192 } |
200 | 193 |
201 } // namespace blink | 194 } // namespace blink |
OLD | NEW |