Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/ScriptProcessorNode.cpp

Issue 1865583002: Implement BaseAudioContext (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, 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
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 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 AN Y 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 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 O N 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 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. 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */ 23 */
24 24
25 #include "modules/webaudio/ScriptProcessorNode.h" 25 #include "modules/webaudio/ScriptProcessorNode.h"
26 #include "bindings/core/v8/ExceptionState.h" 26 #include "bindings/core/v8/ExceptionState.h"
27 #include "core/dom/CrossThreadTask.h" 27 #include "core/dom/CrossThreadTask.h"
28 #include "core/dom/ExceptionCode.h" 28 #include "core/dom/ExceptionCode.h"
29 #include "core/dom/ExecutionContext.h" 29 #include "core/dom/ExecutionContext.h"
30 #include "modules/webaudio/AbstractAudioContext.h"
31 #include "modules/webaudio/AudioBuffer.h" 30 #include "modules/webaudio/AudioBuffer.h"
32 #include "modules/webaudio/AudioNodeInput.h" 31 #include "modules/webaudio/AudioNodeInput.h"
33 #include "modules/webaudio/AudioNodeOutput.h" 32 #include "modules/webaudio/AudioNodeOutput.h"
34 #include "modules/webaudio/AudioProcessingEvent.h" 33 #include "modules/webaudio/AudioProcessingEvent.h"
34 #include "modules/webaudio/BaseAudioContext.h"
35 #include "public/platform/Platform.h" 35 #include "public/platform/Platform.h"
36 36
37 namespace blink { 37 namespace blink {
38 38
39 ScriptProcessorHandler::ScriptProcessorHandler(AudioNode& node, float sampleRate , size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChan nels) 39 ScriptProcessorHandler::ScriptProcessorHandler(AudioNode& node, float sampleRate , size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutputChan nels)
40 : AudioHandler(NodeTypeJavaScript, node, sampleRate) 40 : AudioHandler(NodeTypeJavaScript, node, sampleRate)
41 , m_doubleBufferIndex(0) 41 , m_doubleBufferIndex(0)
42 , m_bufferSize(bufferSize) 42 , m_bufferSize(bufferSize)
43 , m_bufferReadWriteIndex(0) 43 , m_bufferReadWriteIndex(0)
44 , m_numberOfInputChannels(numberOfInputChannels) 44 , m_numberOfInputChannels(numberOfInputChannels)
45 , m_numberOfOutputChannels(numberOfOutputChannels) 45 , m_numberOfOutputChannels(numberOfOutputChannels)
46 , m_internalInputBus(AudioBus::create(numberOfInputChannels, ProcessingSizeI nFrames, false)) 46 , m_internalInputBus(AudioBus::create(numberOfInputChannels, ProcessingSizeI nFrames, false))
47 { 47 {
48 // Regardless of the allowed buffer sizes, we still need to process at the g ranularity of the AudioNode. 48 // Regardless of the allowed buffer sizes, we still need to process at the g ranularity of the AudioNode.
49 if (m_bufferSize < ProcessingSizeInFrames) 49 if (m_bufferSize < ProcessingSizeInFrames)
50 m_bufferSize = ProcessingSizeInFrames; 50 m_bufferSize = ProcessingSizeInFrames;
51 51
52 ASSERT(numberOfInputChannels <= AbstractAudioContext::maxNumberOfChannels()) ; 52 ASSERT(numberOfInputChannels <= BaseAudioContext::maxNumberOfChannels());
53 53
54 addInput(); 54 addInput();
55 addOutput(numberOfOutputChannels); 55 addOutput(numberOfOutputChannels);
56 56
57 m_channelCount = numberOfInputChannels; 57 m_channelCount = numberOfInputChannels;
58 m_channelCountMode = Explicit; 58 m_channelCountMode = Explicit;
59 59
60 initialize(); 60 initialize();
61 } 61 }
62 62
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 } 206 }
207 207
208 double ScriptProcessorHandler::latencyTime() const 208 double ScriptProcessorHandler::latencyTime() const
209 { 209 {
210 return std::numeric_limits<double>::infinity(); 210 return std::numeric_limits<double>::infinity();
211 } 211 }
212 212
213 void ScriptProcessorHandler::setChannelCount(unsigned long channelCount, Excepti onState& exceptionState) 213 void ScriptProcessorHandler::setChannelCount(unsigned long channelCount, Excepti onState& exceptionState)
214 { 214 {
215 ASSERT(isMainThread()); 215 ASSERT(isMainThread());
216 AbstractAudioContext::AutoLocker locker(context()); 216 BaseAudioContext::AutoLocker locker(context());
217 217
218 if (channelCount != m_channelCount) { 218 if (channelCount != m_channelCount) {
219 exceptionState.throwDOMException( 219 exceptionState.throwDOMException(
220 NotSupportedError, 220 NotSupportedError,
221 "channelCount cannot be changed from " + String::number(m_channelCou nt) + " to " + String::number(channelCount)); 221 "channelCount cannot be changed from " + String::number(m_channelCou nt) + " to " + String::number(channelCount));
222 } 222 }
223 } 223 }
224 224
225 void ScriptProcessorHandler::setChannelCountMode(const String& mode, ExceptionSt ate& exceptionState) 225 void ScriptProcessorHandler::setChannelCountMode(const String& mode, ExceptionSt ate& exceptionState)
226 { 226 {
227 ASSERT(isMainThread()); 227 ASSERT(isMainThread());
228 AbstractAudioContext::AutoLocker locker(context()); 228 BaseAudioContext::AutoLocker locker(context());
229 229
230 if ((mode == "max") || (mode == "clamped-max")) { 230 if ((mode == "max") || (mode == "clamped-max")) {
231 exceptionState.throwDOMException( 231 exceptionState.throwDOMException(
232 NotSupportedError, 232 NotSupportedError,
233 "channelCountMode cannot be changed from 'explicit' to '" + mode + " '"); 233 "channelCountMode cannot be changed from 'explicit' to '" + mode + " '");
234 } 234 }
235 } 235 }
236 236
237 // ---------------------------------------------------------------- 237 // ----------------------------------------------------------------
238 238
239 ScriptProcessorNode::ScriptProcessorNode(AbstractAudioContext& context, float sa mpleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOu tputChannels) 239 ScriptProcessorNode::ScriptProcessorNode(BaseAudioContext& context, float sample Rate, size_t bufferSize, unsigned numberOfInputChannels, unsigned numberOfOutput Channels)
240 : AudioNode(context) 240 : AudioNode(context)
241 , ActiveScriptWrappable(this) 241 , ActiveScriptWrappable(this)
242 { 242 {
243 setHandler(ScriptProcessorHandler::create(*this, sampleRate, bufferSize, num berOfInputChannels, numberOfOutputChannels)); 243 setHandler(ScriptProcessorHandler::create(*this, sampleRate, bufferSize, num berOfInputChannels, numberOfOutputChannels));
244 } 244 }
245 245
246 static size_t chooseBufferSize() 246 static size_t chooseBufferSize()
247 { 247 {
248 // Choose a buffer size based on the audio hardware buffer size. Arbitarily make it a power of 248 // Choose a buffer size based on the audio hardware buffer size. Arbitarily make it a power of
249 // two that is 4 times greater than the hardware buffer size. 249 // two that is 4 times greater than the hardware buffer size.
250 // FIXME: What is the best way to choose this? 250 // FIXME: What is the best way to choose this?
251 size_t hardwareBufferSize = Platform::current()->audioHardwareBufferSize(); 251 size_t hardwareBufferSize = Platform::current()->audioHardwareBufferSize();
252 size_t bufferSize = 1 << static_cast<unsigned>(log2(4 * hardwareBufferSize) + 0.5); 252 size_t bufferSize = 1 << static_cast<unsigned>(log2(4 * hardwareBufferSize) + 0.5);
253 253
254 if (bufferSize < 256) 254 if (bufferSize < 256)
255 return 256; 255 return 256;
256 if (bufferSize > 16384) 256 if (bufferSize > 16384)
257 return 16384; 257 return 16384;
258 258
259 return bufferSize; 259 return bufferSize;
260 } 260 }
261 261
262 ScriptProcessorNode* ScriptProcessorNode::create(AbstractAudioContext& context, float sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned nu mberOfOutputChannels) 262 ScriptProcessorNode* ScriptProcessorNode::create(BaseAudioContext& context, floa t sampleRate, size_t bufferSize, unsigned numberOfInputChannels, unsigned number OfOutputChannels)
263 { 263 {
264 // Check for valid buffer size. 264 // Check for valid buffer size.
265 switch (bufferSize) { 265 switch (bufferSize) {
266 case 0: 266 case 0:
267 bufferSize = chooseBufferSize(); 267 bufferSize = chooseBufferSize();
268 break; 268 break;
269 case 256: 269 case 256:
270 case 512: 270 case 512:
271 case 1024: 271 case 1024:
272 case 2048: 272 case 2048:
273 case 4096: 273 case 4096:
274 case 8192: 274 case 8192:
275 case 16384: 275 case 16384:
276 break; 276 break;
277 default: 277 default:
278 return nullptr; 278 return nullptr;
279 } 279 }
280 280
281 if (!numberOfInputChannels && !numberOfOutputChannels) 281 if (!numberOfInputChannels && !numberOfOutputChannels)
282 return nullptr; 282 return nullptr;
283 283
284 if (numberOfInputChannels > AbstractAudioContext::maxNumberOfChannels()) 284 if (numberOfInputChannels > BaseAudioContext::maxNumberOfChannels())
285 return nullptr; 285 return nullptr;
286 286
287 if (numberOfOutputChannels > AbstractAudioContext::maxNumberOfChannels()) 287 if (numberOfOutputChannels > BaseAudioContext::maxNumberOfChannels())
288 return nullptr; 288 return nullptr;
289 289
290 return new ScriptProcessorNode(context, sampleRate, bufferSize, numberOfInpu tChannels, numberOfOutputChannels); 290 return new ScriptProcessorNode(context, sampleRate, bufferSize, numberOfInpu tChannels, numberOfOutputChannels);
291 } 291 }
292 292
293 size_t ScriptProcessorNode::bufferSize() const 293 size_t ScriptProcessorNode::bufferSize() const
294 { 294 {
295 return static_cast<ScriptProcessorHandler&>(handler()).bufferSize(); 295 return static_cast<ScriptProcessorHandler&>(handler()).bufferSize();
296 } 296 }
297 297
298 bool ScriptProcessorNode::hasPendingActivity() const 298 bool ScriptProcessorNode::hasPendingActivity() const
299 { 299 {
300 // To prevent the node from leaking after the context is closed. 300 // To prevent the node from leaking after the context is closed.
301 if (context()->isContextClosed()) 301 if (context()->isContextClosed())
302 return false; 302 return false;
303 303
304 // If |onaudioprocess| event handler is defined, the node should not be 304 // If |onaudioprocess| event handler is defined, the node should not be
305 // GCed even if it is out of scope. 305 // GCed even if it is out of scope.
306 if (hasEventListeners(EventTypeNames::audioprocess)) 306 if (hasEventListeners(EventTypeNames::audioprocess))
307 return true; 307 return true;
308 308
309 return false; 309 return false;
310 } 310 }
311 311
312 } // namespace blink 312 } // namespace blink
313 313
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698