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

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

Issue 1952793002: Move the exception logic to the AudioNode creator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 4 years, 7 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
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(
263 AbstractAudioContext& context,
264 ExceptionState& exceptionState)
263 { 265 {
266 DCHECK(isMainThread());
267
268 // Default buffer size is 0 (let WebAudio choose) with 2 inputs and 2
269 // outputs.
270 return create(context, 0, 2, 2, exceptionState);
271 }
272
273 ScriptProcessorNode* ScriptProcessorNode::create(
274 AbstractAudioContext& context,
275 size_t bufferSize,
276 ExceptionState& exceptionState)
277 {
278 DCHECK(isMainThread());
279
280 // Default is 2 inputs and 2 outputs.
281 return create(context, bufferSize, 2, 2, exceptionState);
282 }
283
284 ScriptProcessorNode* ScriptProcessorNode::create(
285 AbstractAudioContext& context,
286 size_t bufferSize,
287 unsigned numberOfInputChannels,
288 ExceptionState& exceptionState)
289 {
290 DCHECK(isMainThread());
291
292 // Default is 2 outputs.
293 return create(context, bufferSize, numberOfInputChannels, 2, exceptionState) ;
294 }
295
296 ScriptProcessorNode* ScriptProcessorNode::create(
297 AbstractAudioContext& context,
298 size_t bufferSize,
299 unsigned numberOfInputChannels,
300 unsigned numberOfOutputChannels,
301 ExceptionState& exceptionState)
302 {
303 DCHECK(isMainThread());
304
305 if (context.isContextClosed()) {
306 context.throwExceptionForClosedState(exceptionState);
307 return nullptr;
308 }
309
310 if (numberOfInputChannels == 0 && numberOfOutputChannels == 0) {
311 exceptionState.throwDOMException(
312 IndexSizeError,
313 "number of input channels and output channels cannot both be zero.") ;
314 return nullptr;
315 }
316
317 if (numberOfInputChannels > AbstractAudioContext::maxNumberOfChannels()) {
318 exceptionState.throwDOMException(
319 IndexSizeError,
320 "number of input channels (" + String::number(numberOfInputChannels)
321 + ") exceeds maximum ("
322 + String::number(AbstractAudioContext::maxNumberOfChannels()) + ")." );
323 return nullptr;
324 }
325
326 if (numberOfOutputChannels > AbstractAudioContext::maxNumberOfChannels()) {
327 exceptionState.throwDOMException(
328 IndexSizeError,
329 "number of output channels (" + String::number(numberOfInputChannels )
330 + ") exceeds maximum ("
331 + String::number(AbstractAudioContext::maxNumberOfChannels()) + ")." );
332 return nullptr;
333 }
334
264 // Check for valid buffer size. 335 // Check for valid buffer size.
265 switch (bufferSize) { 336 switch (bufferSize) {
266 case 0: 337 case 0:
267 bufferSize = chooseBufferSize(); 338 bufferSize = chooseBufferSize();
268 break; 339 break;
269 case 256: 340 case 256:
270 case 512: 341 case 512:
271 case 1024: 342 case 1024:
272 case 2048: 343 case 2048:
273 case 4096: 344 case 4096:
274 case 8192: 345 case 8192:
275 case 16384: 346 case 16384:
276 break; 347 break;
277 default: 348 default:
349 exceptionState.throwDOMException(
350 IndexSizeError,
351 "buffer size (" + String::number(bufferSize)
352 + ") must be 0 or a power of two between 256 and 16384.");
278 return nullptr; 353 return nullptr;
279 } 354 }
280 355
281 if (!numberOfInputChannels && !numberOfOutputChannels) 356 ScriptProcessorNode* node = new ScriptProcessorNode(context, context.sample Rate(), bufferSize, numberOfInputChannels, numberOfOutputChannels);
357
358 if (!node)
282 return nullptr; 359 return nullptr;
283 360
284 if (numberOfInputChannels > AbstractAudioContext::maxNumberOfChannels()) 361 // context keeps reference until we stop making javascript rendering callbac ks
285 return nullptr; 362 context.notifySourceNodeStartedProcessing(node);
286 363
287 if (numberOfOutputChannels > AbstractAudioContext::maxNumberOfChannels()) 364 return node;
288 return nullptr;
289
290 return new ScriptProcessorNode(context, sampleRate, bufferSize, numberOfInpu tChannels, numberOfOutputChannels);
291 } 365 }
292 366
293 size_t ScriptProcessorNode::bufferSize() const 367 size_t ScriptProcessorNode::bufferSize() const
294 { 368 {
295 return static_cast<ScriptProcessorHandler&>(handler()).bufferSize(); 369 return static_cast<ScriptProcessorHandler&>(handler()).bufferSize();
296 } 370 }
297 371
298 bool ScriptProcessorNode::hasPendingActivity() const 372 bool ScriptProcessorNode::hasPendingActivity() const
299 { 373 {
300 // To prevent the node from leaking after the context is closed. 374 // To prevent the node from leaking after the context is closed.
301 if (context()->isContextClosed()) 375 if (context()->isContextClosed())
302 return false; 376 return false;
303 377
304 // If |onaudioprocess| event handler is defined, the node should not be 378 // If |onaudioprocess| event handler is defined, the node should not be
305 // GCed even if it is out of scope. 379 // GCed even if it is out of scope.
306 if (hasEventListeners(EventTypeNames::audioprocess)) 380 if (hasEventListeners(EventTypeNames::audioprocess))
307 return true; 381 return true;
308 382
309 return false; 383 return false;
310 } 384 }
311 385
312 } // namespace blink 386 } // namespace blink
313 387
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698