1 /* 2 * Copyright (C) 2010, 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/AudioNode.h" 30 31 #include "bindings/v8/ExceptionState.h" 32 #include "core/dom/ExceptionCode.h" 33 #include "modules/webaudio/AudioContext.h" 34 #include "modules/webaudio/AudioNodeInput.h" 35 #include "modules/webaudio/AudioNodeOutput.h" 36 #include "modules/webaudio/AudioParam.h" 37 #include "wtf/Atomics.h" 38 #include "wtf/MainThread.h" 39 40 #if DEBUG_AUDIONODE_REFERENCES 41 #include <stdio.h> 42 #endif 43 44 namespace WebCore { 45 46 AudioNode::AudioNode(AudioContext* context, float sampleRate) 47 : m_isInitialized(false) 48 , m_nodeType(NodeTypeUnknown) 49 , m_context(context) 50 , m_sampleRate(sampleRate) 51 , m_lastProcessingTime(-1) 52 , m_lastNonSilentTime(-1) 53 , m_normalRefCount(1) // start out with normal refCount == 1 (like WTF::RefCounted class) 54 , m_connectionRefCount(0) 55 , m_isMarkedForDeletion(false) 56 , m_isDisabled(false) 57 , m_channelCount(2) 58 , m_channelCountMode(Max) 59 , m_channelInterpretation(AudioBus::Speakers) 60 { 61 ScriptWrappable::init(this); 62 #if DEBUG_AUDIONODE_REFERENCES 63 if (!s_isNodeCountInitialized) { 64 s_isNodeCountInitialized = true; 65 atexit(AudioNode::printNodeCounts); 66 } 67 #endif 68 } 69 70 AudioNode::~AudioNode() 71 { 72 #if DEBUG_AUDIONODE_REFERENCES 73 --s_nodeCount[nodeType()]; 74 fprintf(stderr, "%p: %d: AudioNode::~AudioNode() %d %d\n", this, nodeType(), m_normalRefCount, m_connectionRefCount); 75 #endif 76 } 77 78 void AudioNode::initialize() 79 { 80 m_isInitialized = true; 81 } 82 83 void AudioNode::uninitialize() 84 { 85 m_isInitialized = false; 86 } 87 88 void AudioNode::setNodeType(NodeType type) 89 { 90 m_nodeType = type; 91 92 #if DEBUG_AUDIONODE_REFERENCES 93 ++s_nodeCount[type]; 94 #endif 95 } 96 97 void AudioNode::lazyInitialize() 98 { 99 if (!isInitialized()) 100 initialize(); 101 } 102 103 void AudioNode::addInput(PassOwnPtr<AudioNodeInput> input) 104 { 105 m_inputs.append(input); 106 } 107 108 void AudioNode::addOutput(PassOwnPtr<AudioNodeOutput> output) 109 { 110 m_outputs.append(output); 111 } 112 113 AudioNodeInput* AudioNode::input(unsigned i) 114 { 115 if (i < m_inputs.size()) 116 return m_inputs[i].get(); 117 return 0; 118 } 119 120 AudioNodeOutput* AudioNode::output(unsigned i) 121 { 122 if (i < m_outputs.size()) 123 return m_outputs[i].get(); 124 return 0; 125 } 126 127 void AudioNode::connect(AudioNode* destination, unsigned outputIndex, unsigned inputIndex, ExceptionState& es) 128 { 129 ASSERT(isMainThread()); 130 AudioContext::AutoLocker locker(context()); 131 132 if (!destination) { 133 es.throwDOMException(SyntaxError); 134 return; 135 } 136 137 // Sanity check input and output indices. 138 if (outputIndex >= numberOfOutputs()) { 139 es.throwDOMException(IndexSizeError); 140 return; 141 } 142 143 if (destination && inputIndex >= destination->numberOfInputs()) { 144 es.throwDOMException(IndexSizeError); 145 return; 146 } 147 148 if (context() != destination->context()) { 149 es.throwDOMException(SyntaxError); 150 return; 151 } 152 153 AudioNodeInput* input = destination->input(inputIndex); 154 AudioNodeOutput* output = this->output(outputIndex); 155 input->connect(output); 156 157 // Let context know that a connection has been made. 158 context()->incrementConnectionCount(); 159 } 160 161 void AudioNode::connect(AudioParam* param, unsigned outputIndex, ExceptionState& es) 162 { 163 ASSERT(isMainThread()); 164 AudioContext::AutoLocker locker(context()); 165 166 if (!param) { 167 es.throwDOMException(SyntaxError); 168 return; 169 } 170 171 if (outputIndex >= numberOfOutputs()) { 172 es.throwDOMException(IndexSizeError); 173 return; 174 } 175 176 if (context() != param->context()) { 177 es.throwDOMException(SyntaxError); 178 return; 179 } 180 181 AudioNodeOutput* output = this->output(outputIndex); 182 param->connect(output); 183 } 184 185 void AudioNode::disconnect(unsigned outputIndex, ExceptionState& es) 186 { 187 ASSERT(isMainThread()); 188 AudioContext::AutoLocker locker(context()); 189 190 // Sanity check input and output indices. 191 if (outputIndex >= numberOfOutputs()) { 192 es.throwDOMException(IndexSizeError); 193 return; 194 } 195 196 AudioNodeOutput* output = this->output(outputIndex); 197 output->disconnectAll(); 198 } 199 200 unsigned long AudioNode::channelCount() 201 { 202 return m_channelCount; 203 } 204 205 void AudioNode::setChannelCount(unsigned long channelCount, ExceptionState& es) 206 { 207 ASSERT(isMainThread()); 208 AudioContext::AutoLocker locker(context()); 209 210 if (channelCount > 0 && channelCount <= AudioContext::maxNumberOfChannels()) { 211 if (m_channelCount != channelCount) { 212 m_channelCount = channelCount; 213 if (m_channelCountMode != Max) 214 updateChannelsForInputs(); 215 } 216 } else { 217 es.throwDOMException(InvalidStateError); 218 } 219 } 220 221 String AudioNode::channelCountMode() 222 { 223 switch (m_channelCountMode) { 224 case Max: 225 return "max"; 226 case ClampedMax: 227 return "clamped-max"; 228 case Explicit: 229 return "explicit"; 230 } 231 ASSERT_NOT_REACHED(); 232 return ""; 233 } 234 235 void AudioNode::setChannelCountMode(const String& mode, ExceptionState& es) 236 { 237 ASSERT(isMainThread()); 238 AudioContext::AutoLocker locker(context()); 239 240 ChannelCountMode oldMode = m_channelCountMode; 241 242 if (mode == "max") 243 m_channelCountMode = Max; 244 else if (mode == "clamped-max") 245 m_channelCountMode = ClampedMax; 246 else if (mode == "explicit") 247 m_channelCountMode = Explicit; 248 else 249 es.throwDOMException(InvalidStateError); 250 251 if (m_channelCountMode != oldMode) 252 updateChannelsForInputs(); 253 } 254 255 String AudioNode::channelInterpretation() 256 { 257 switch (m_channelInterpretation) { 258 case AudioBus::Speakers: 259 return "speakers"; 260 case AudioBus::Discrete: 261 return "discrete"; 262 } 263 ASSERT_NOT_REACHED(); 264 return ""; 265 } 266 267 void AudioNode::setChannelInterpretation(const String& interpretation, ExceptionState& es) 268 { 269 ASSERT(isMainThread()); 270 AudioContext::AutoLocker locker(context()); 271 272 if (interpretation == "speakers") 273 m_channelInterpretation = AudioBus::Speakers; 274 else if (interpretation == "discrete") 275 m_channelInterpretation = AudioBus::Discrete; 276 else 277 es.throwDOMException(InvalidStateError); 278 } 279 280 void AudioNode::updateChannelsForInputs() 281 { 282 for (unsigned i = 0; i < m_inputs.size(); ++i) 283 input(i)->changedOutputs(); 284 } 285 286 const AtomicString& AudioNode::interfaceName() const 287 { 288 return eventNames().interfaceForAudioNode; 289 } 290 291 ScriptExecutionContext* AudioNode::scriptExecutionContext() const 292 { 293 return const_cast<AudioNode*>(this)->context()->scriptExecutionContext(); 294 } 295 296 void AudioNode::processIfNecessary(size_t framesToProcess) 297 { 298 ASSERT(context()->isAudioThread()); 299 300 if (!isInitialized()) 301 return; 302 303 // Ensure that we only process once per rendering quantum. 304 // This handles the "fanout" problem where an output is connected to multiple inputs. 305 // The first time we're called during this time slice we process, but after that we don't want to re-process, 306 // instead our output(s) will already have the results cached in their bus; 307 double currentTime = context()->currentTime(); 308 if (m_lastProcessingTime != currentTime) { 309 m_lastProcessingTime = currentTime; // important to first update this time because of feedback loops in the rendering graph 310 311 pullInputs(framesToProcess); 312 313 bool silentInputs = inputsAreSilent(); 314 if (!silentInputs) 315 m_lastNonSilentTime = (context()->currentSampleFrame() + framesToProcess) / static_cast<double>(m_sampleRate); 316 317 if (silentInputs && propagatesSilence()) 318 silenceOutputs(); 319 else { 320 process(framesToProcess); 321 unsilenceOutputs(); 322 } 323 } 324 } 325 326 void AudioNode::checkNumberOfChannelsForInput(AudioNodeInput* input) 327 { 328 ASSERT(context()->isAudioThread() && context()->isGraphOwner()); 329 330 ASSERT(m_inputs.contains(input)); 331 if (!m_inputs.contains(input)) 332 return; 333 334 input->updateInternalBus(); 335 } 336 337 bool AudioNode::propagatesSilence() const 338 { 339 return m_lastNonSilentTime + latencyTime() + tailTime() < context()->currentTime(); 340 } 341 342 void AudioNode::pullInputs(size_t framesToProcess) 343 { 344 ASSERT(context()->isAudioThread()); 345 346 // Process all of the AudioNodes connected to our inputs. 347 for (unsigned i = 0; i < m_inputs.size(); ++i) 348 input(i)->pull(0, framesToProcess); 349 } 350 351 bool AudioNode::inputsAreSilent() 352 { 353 for (unsigned i = 0; i < m_inputs.size(); ++i) { 354 if (!input(i)->bus()->isSilent()) 355 return false; 356 } 357 return true; 358 } 359 360 void AudioNode::silenceOutputs() 361 { 362 for (unsigned i = 0; i < m_outputs.size(); ++i) 363 output(i)->bus()->zero(); 364 } 365 366 void AudioNode::unsilenceOutputs() 367 { 368 for (unsigned i = 0; i < m_outputs.size(); ++i) 369 output(i)->bus()->clearSilentFlag(); 370 } 371 372 void AudioNode::enableOutputsIfNecessary() 373 { 374 if (m_isDisabled && m_connectionRefCount > 0) { 375 ASSERT(isMainThread()); 376 AudioContext::AutoLocker locker(context()); 377 378 m_isDisabled = false; 379 for (unsigned i = 0; i < m_outputs.size(); ++i) 380 output(i)->enable(); 381 } 382 } 383 384 void AudioNode::disableOutputsIfNecessary() 385 { 386 // Disable outputs if appropriate. We do this if the number of connections is 0 or 1. The case 387 // of 0 is from finishDeref() where there are no connections left. The case of 1 is from 388 // AudioNodeInput::disable() where we want to disable outputs when there's only one connection 389 // left because we're ready to go away, but can't quite yet. 390 if (m_connectionRefCount <= 1 && !m_isDisabled) { 391 // Still may have JavaScript references, but no more "active" connection references, so put all of our outputs in a "dormant" disabled state. 392 // Garbage collection may take a very long time after this time, so the "dormant" disabled nodes should not bog down the rendering... 393 394 // As far as JavaScript is concerned, our outputs must still appear to be connected. 395 // But internally our outputs should be disabled from the inputs they're connected to. 396 // disable() can recursively deref connections (and call disable()) down a whole chain of connected nodes. 397 398 // FIXME: we special case the convolver and delay since they have a significant tail-time and shouldn't be disconnected simply 399 // because they no longer have any input connections. This needs to be handled more generally where AudioNodes have 400 // a tailTime attribute. Then the AudioNode only needs to remain "active" for tailTime seconds after there are no 401 // longer any active connections. 402 if (nodeType() != NodeTypeConvolver && nodeType() != NodeTypeDelay) { 403 m_isDisabled = true; 404 for (unsigned i = 0; i < m_outputs.size(); ++i) 405 output(i)->disable(); 406 } 407 } 408 } 409 410 void AudioNode::ref(RefType refType) 411 { 412 switch (refType) { 413 case RefTypeNormal: 414 atomicIncrement(&m_normalRefCount); 415 break; 416 case RefTypeConnection: 417 atomicIncrement(&m_connectionRefCount); 418 break; 419 default: 420 ASSERT_NOT_REACHED(); 421 } 422 423 #if DEBUG_AUDIONODE_REFERENCES 424 fprintf(stderr, "%p: %d: AudioNode::ref(%d) %d %d\n", this, nodeType(), refType, m_normalRefCount, m_connectionRefCount); 425 #endif 426 427 // See the disabling code in finishDeref() below. This handles the case where a node 428 // is being re-connected after being used at least once and disconnected. 429 // In this case, we need to re-enable. 430 if (refType == RefTypeConnection) 431 enableOutputsIfNecessary(); 432 } 433 434 void AudioNode::deref(RefType refType) 435 { 436 // The actually work for deref happens completely within the audio context's graph lock. 437 // In the case of the audio thread, we must use a tryLock to avoid glitches. 438 bool hasLock = false; 439 bool mustReleaseLock = false; 440 441 if (context()->isAudioThread()) { 442 // Real-time audio thread must not contend lock (to avoid glitches). 443 hasLock = context()->tryLock(mustReleaseLock); 444 } else { 445 context()->lock(mustReleaseLock); 446 hasLock = true; 447 } 448 449 if (hasLock) { 450 // This is where the real deref work happens. 451 finishDeref(refType); 452 453 if (mustReleaseLock) 454 context()->unlock(); 455 } else { 456 // We were unable to get the lock, so put this in a list to finish up later. 457 ASSERT(context()->isAudioThread()); 458 ASSERT(refType == RefTypeConnection); 459 context()->addDeferredFinishDeref(this); 460 } 461 462 // Once AudioContext::uninitialize() is called there's no more chances for deleteMarkedNodes() to get called, so we call here. 463 // We can't call in AudioContext::~AudioContext() since it will never be called as long as any AudioNode is alive 464 // because AudioNodes keep a reference to the context. 465 if (context()->isAudioThreadFinished()) 466 context()->deleteMarkedNodes(); 467 } 468 469 void AudioNode::finishDeref(RefType refType) 470 { 471 ASSERT(context()->isGraphOwner()); 472 473 switch (refType) { 474 case RefTypeNormal: 475 ASSERT(m_normalRefCount > 0); 476 atomicDecrement(&m_normalRefCount); 477 break; 478 case RefTypeConnection: 479 ASSERT(m_connectionRefCount > 0); 480 atomicDecrement(&m_connectionRefCount); 481 break; 482 default: 483 ASSERT_NOT_REACHED(); 484 } 485 486 #if DEBUG_AUDIONODE_REFERENCES 487 fprintf(stderr, "%p: %d: AudioNode::deref(%d) %d %d\n", this, nodeType(), refType, m_normalRefCount, m_connectionRefCount); 488 #endif 489 490 if (!m_connectionRefCount) { 491 if (!m_normalRefCount) { 492 if (!m_isMarkedForDeletion) { 493 // All references are gone - we need to go away. 494 for (unsigned i = 0; i < m_outputs.size(); ++i) 495 output(i)->disconnectAll(); // This will deref() nodes we're connected to. 496 497 // Mark for deletion at end of each render quantum or when context shuts down. 498 context()->markForDeletion(this); 499 m_isMarkedForDeletion = true; 500 } 501 } else if (refType == RefTypeConnection) 502 disableOutputsIfNecessary(); 503 } 504 } 505 506 #if DEBUG_AUDIONODE_REFERENCES 507 508 bool AudioNode::s_isNodeCountInitialized = false; 509 int AudioNode::s_nodeCount[NodeTypeEnd]; 510 511 void AudioNode::printNodeCounts() 512 { 513 fprintf(stderr, "\n\n"); 514 fprintf(stderr, "===========================\n"); 515 fprintf(stderr, "AudioNode: reference counts\n"); 516 fprintf(stderr, "===========================\n"); 517 518 for (unsigned i = 0; i < NodeTypeEnd; ++i) 519 fprintf(stderr, "%d: %d\n", i, s_nodeCount[i]); 520 521 fprintf(stderr, "===========================\n\n\n"); 522 } 523 524 #endif // DEBUG_AUDIONODE_REFERENCES 525 526 } // namespace WebCore 527 528 #endif // ENABLE(WEB_AUDIO) 529