Chromium Code Reviews| 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 14 matching lines...) Expand all Loading... | |
| 25 #include "config.h" | 25 #include "config.h" |
| 26 #if ENABLE(WEB_AUDIO) | 26 #if ENABLE(WEB_AUDIO) |
| 27 #include "modules/webaudio/OfflineAudioContext.h" | 27 #include "modules/webaudio/OfflineAudioContext.h" |
| 28 | 28 |
| 29 #include "bindings/core/v8/ExceptionMessages.h" | 29 #include "bindings/core/v8/ExceptionMessages.h" |
| 30 #include "bindings/core/v8/ExceptionState.h" | 30 #include "bindings/core/v8/ExceptionState.h" |
| 31 #include "bindings/core/v8/ScriptState.h" | 31 #include "bindings/core/v8/ScriptState.h" |
| 32 #include "core/dom/Document.h" | 32 #include "core/dom/Document.h" |
| 33 #include "core/dom/ExceptionCode.h" | 33 #include "core/dom/ExceptionCode.h" |
| 34 #include "core/dom/ExecutionContext.h" | 34 #include "core/dom/ExecutionContext.h" |
| 35 #include "modules/webaudio/DeferredTaskHandler.h" | |
| 36 #include "modules/webaudio/OfflineAudioCompletionEvent.h" | |
| 37 #include "modules/webaudio/OfflineAudioDestinationNode.h" | |
| 38 | |
| 35 #include "platform/audio/AudioUtilities.h" | 39 #include "platform/audio/AudioUtilities.h" |
| 36 | 40 |
| 37 namespace blink { | 41 namespace blink { |
| 38 | 42 |
| 39 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) | 43 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) |
| 40 { | 44 { |
| 41 // FIXME: add support for workers. | 45 // FIXME: add support for workers. |
| 42 if (!context || !context->isDocument()) { | 46 if (!context || !context->isDocument()) { |
| 43 exceptionState.throwDOMException( | 47 exceptionState.throwDOMException( |
| 44 NotSupportedError, | 48 NotSupportedError, |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 69 if (!AudioUtilities::isValidAudioBufferSampleRate(sampleRate)) { | 73 if (!AudioUtilities::isValidAudioBufferSampleRate(sampleRate)) { |
| 70 exceptionState.throwDOMException( | 74 exceptionState.throwDOMException( |
| 71 IndexSizeError, | 75 IndexSizeError, |
| 72 ExceptionMessages::indexOutsideRange( | 76 ExceptionMessages::indexOutsideRange( |
| 73 "sampleRate", sampleRate, | 77 "sampleRate", sampleRate, |
| 74 AudioUtilities::minAudioBufferSampleRate(), ExceptionMessages::I nclusiveBound, | 78 AudioUtilities::minAudioBufferSampleRate(), ExceptionMessages::I nclusiveBound, |
| 75 AudioUtilities::maxAudioBufferSampleRate(), ExceptionMessages::I nclusiveBound)); | 79 AudioUtilities::maxAudioBufferSampleRate(), ExceptionMessages::I nclusiveBound)); |
| 76 return nullptr; | 80 return nullptr; |
| 77 } | 81 } |
| 78 | 82 |
| 79 OfflineAudioContext* audioContext = new OfflineAudioContext(document, number OfChannels, numberOfFrames, sampleRate); | 83 OfflineAudioContext* audioContext = new OfflineAudioContext(document, number OfChannels, numberOfFrames, sampleRate, exceptionState); |
| 80 | 84 |
| 81 if (!audioContext->destination()) { | 85 if (!audioContext->destination()) { |
| 82 exceptionState.throwDOMException( | 86 exceptionState.throwDOMException( |
| 83 NotSupportedError, | 87 NotSupportedError, |
| 84 "OfflineAudioContext(" + String::number(numberOfChannels) | 88 "OfflineAudioContext(" + String::number(numberOfChannels) |
| 85 + ", " + String::number(numberOfFrames) | 89 + ", " + String::number(numberOfFrames) |
| 86 + ", " + String::number(sampleRate) | 90 + ", " + String::number(sampleRate) |
| 87 + ")"); | 91 + ")"); |
| 88 } | 92 } |
| 89 | 93 |
| 90 audioContext->suspendIfNeeded(); | 94 audioContext->suspendIfNeeded(); |
| 91 return audioContext; | 95 return audioContext; |
| 92 } | 96 } |
| 93 | 97 |
| 94 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) | 98 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) |
| 95 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat e) | 99 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat e) |
| 100 , m_isRenderingStarted(false) | |
| 101 , m_totalRenderFrames(numberOfFrames) | |
| 96 { | 102 { |
| 103 // Create a new destination for offline rendering. | |
| 104 m_renderTarget = AudioBuffer::create(numberOfChannels, numberOfFrames, sampl eRate); | |
| 105 | |
| 106 // Throw an exception if the render target is not ready. | |
| 107 if (!m_renderTarget) { | |
| 108 exceptionState.throwRangeError(ExceptionMessages::failedToConstruct( | |
| 109 "AudioBuffer", | |
| 110 "failed to create a target buffer for offline rendering" + | |
| 111 String::number(numberOfChannels) + ", " + | |
| 112 String::number(numberOfFrames) + ").")); | |
| 113 } | |
|
Raymond Toy
2015/10/22 18:49:47
I think the message you're producing is:
"failed
hongchan
2015/10/22 20:17:04
Oops. Sorry.
| |
| 114 | |
| 115 if (m_renderTarget.get()) | |
| 116 m_destinationNode = OfflineAudioDestinationNode::create(this, m_renderTa rget.get()); | |
| 117 | |
| 118 initialize(); | |
| 97 } | 119 } |
| 98 | 120 |
| 99 OfflineAudioContext::~OfflineAudioContext() | 121 OfflineAudioContext::~OfflineAudioContext() |
| 100 { | 122 { |
| 101 } | 123 } |
| 102 | 124 |
| 125 DEFINE_TRACE(OfflineAudioContext) | |
| 126 { | |
| 127 visitor->trace(m_renderTarget); | |
| 128 visitor->trace(m_completeResolver); | |
| 129 visitor->trace(m_scheduledSuspends); | |
| 130 AbstractAudioContext::trace(visitor); | |
| 131 } | |
| 132 | |
| 103 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) | 133 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) |
| 104 { | 134 { |
| 135 ASSERT(isMainThread()); | |
| 136 | |
| 105 // Calling close() on an OfflineAudioContext is not supported/allowed, | 137 // Calling close() on an OfflineAudioContext is not supported/allowed, |
| 106 // but it might well have been stopped by its execution context. | 138 // but it might well have been stopped by its execution context. |
| 139 // | |
| 140 // See: crbug.com/435867 | |
| 107 if (isContextClosed()) { | 141 if (isContextClosed()) { |
| 108 return ScriptPromise::rejectWithDOMException( | 142 return ScriptPromise::rejectWithDOMException( |
| 109 scriptState, | 143 scriptState, |
| 110 DOMException::create( | 144 DOMException::create( |
| 111 InvalidStateError, | 145 InvalidStateError, |
| 112 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); | 146 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); |
| 113 } | 147 } |
| 114 | 148 |
| 115 if (m_offlineResolver) { | 149 // If the context is not in the suspended state (i.e. running), reject the p romise. |
| 116 // Can't call startRendering more than once. Return a rejected promise now. | 150 if (contextState() != AudioContextState::Suspended) { |
| 117 return ScriptPromise::rejectWithDOMException( | 151 return ScriptPromise::rejectWithDOMException( |
| 118 scriptState, | 152 scriptState, |
| 119 DOMException::create( | 153 DOMException::create( |
| 154 InvalidStateError, | |
| 155 "cannot startRendering when an OfflineAudioContext is " + state( ))); | |
| 156 } | |
| 157 | |
| 158 // Can't call startRendering more than once. Return a rejected promise now. | |
| 159 if (m_isRenderingStarted) { | |
| 160 return ScriptPromise::rejectWithDOMException( | |
| 161 scriptState, | |
| 162 DOMException::create( | |
| 120 InvalidStateError, | 163 InvalidStateError, |
| 121 "cannot call startRendering more than once")); | 164 "cannot call startRendering more than once")); |
| 122 } | 165 } |
| 123 | 166 |
| 124 m_offlineResolver = ScriptPromiseResolver::create(scriptState); | 167 ASSERT(!m_isRenderingStarted); |
| 125 startRendering(); | 168 |
| 126 return m_offlineResolver->promise(); | 169 m_completeResolver = ScriptPromiseResolver::create(scriptState); |
| 170 | |
| 171 // Start rendering and return the promise. | |
| 172 m_isRenderingStarted = true; | |
| 173 setContextState(Running); | |
| 174 destinationHandler().startRendering(); | |
| 175 | |
| 176 return m_completeResolver->promise(); | |
| 127 } | 177 } |
| 128 | 178 |
| 129 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) | 179 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) |
| 130 { | 180 { |
| 131 return ScriptPromise::rejectWithDOMException( | 181 return ScriptPromise::rejectWithDOMException( |
| 132 scriptState, | 182 scriptState, |
| 133 DOMException::create(InvalidAccessError, "cannot close an OfflineAudioCo ntext.")); | |
| 134 } | |
| 135 | |
| 136 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) | |
| 137 { | |
| 138 return ScriptPromise::rejectWithDOMException( | |
| 139 scriptState, | |
| 140 DOMException::create( | 183 DOMException::create( |
| 141 InvalidAccessError, | 184 InvalidAccessError, |
| 142 "cannot suspend an OfflineAudioContext")); | 185 "cannot close an OfflineAudioContext.")); |
| 186 } | |
| 187 | |
| 188 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState, doub le when) | |
| 189 { | |
| 190 ASSERT(isMainThread()); | |
| 191 | |
| 192 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
| 193 ScriptPromise promise = resolver->promise(); | |
| 194 | |
| 195 // The render thread does not exist; reject the promise. | |
| 196 if (!destinationHandler().offlineRenderThread()) { | |
| 197 resolver->reject(DOMException::create(InvalidStateError, | |
| 198 "the rendering is already finished")); | |
| 199 return promise; | |
| 200 } | |
| 201 | |
| 202 // The specified suspend time is negative; reject the promise. | |
| 203 if (when < 0) { | |
| 204 resolver->reject(DOMException::create(InvalidStateError, | |
| 205 "negative suspend time (" + String::number(when) + ") is not allowed ")); | |
| 206 return promise; | |
| 207 } | |
| 208 | |
| 209 // Quantize (to the lower boundary) the suspend time by the render quantum. | |
| 210 size_t frame = when * sampleRate(); | |
| 211 frame -= frame % destinationHandler().renderQuantumFrames(); | |
| 212 | |
| 213 // The suspend time should be earlier than the total render frame. If the | |
| 214 // requested suspension time is equal to the total render frame, the promise | |
| 215 // will be rejected. | |
| 216 if (m_totalRenderFrames <= frame) { | |
| 217 resolver->reject(DOMException::create(InvalidStateError, | |
| 218 "cannot schedule a suspend at frame " + String::number(frame) + | |
| 219 " (" + String::number(when) + " seconds) " + | |
| 220 "because it is greater than or equal to the total render duration of " + | |
| 221 String::number(m_totalRenderFrames) + " frames")); | |
| 222 return promise; | |
| 223 } | |
| 224 | |
| 225 // The specified suspend time is in the past; reject the promise. | |
| 226 if (frame < currentSampleFrame()) { | |
| 227 resolver->reject(DOMException::create(InvalidStateError, | |
| 228 "cannot schedule a suspend at frame " + | |
| 229 String::number(frame) + " (" + String::number(when) + | |
| 230 " seconds) because it is earlier than the current frame of " + | |
| 231 String::number(currentSampleFrame()) + " (" + | |
| 232 String::number(currentTime()) + " seconds)")); | |
| 233 return promise; | |
| 234 } | |
| 235 | |
| 236 // If there is a duplicate suspension at the same quantized frame, | |
| 237 // reject the promise. | |
| 238 if (m_scheduledSuspends.contains(frame)) { | |
| 239 resolver->reject(DOMException::create(InvalidStateError, | |
| 240 "cannot schedule more than one suspend at frame " + | |
| 241 String::number(frame) + " (" + | |
| 242 String::number(when) + " seconds)")); | |
| 243 return promise; | |
| 244 } | |
| 245 | |
| 246 // Wait until the suspend map is available for the insertion. Here we should | |
| 247 // use AutoLocker because it locks the graph from the main thread. | |
| 248 AutoLocker locker(this); | |
| 249 | |
| 250 m_scheduledSuspends.add(frame, resolver); | |
| 251 | |
| 252 return promise; | |
| 143 } | 253 } |
| 144 | 254 |
| 145 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) | 255 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) |
| 146 { | 256 { |
| 257 ASSERT(isMainThread()); | |
| 258 | |
| 259 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; | |
| 260 ScriptPromise promise = resolver->promise(); | |
| 261 | |
| 262 // If the rendering has not started, reject the promise. | |
| 263 if (!m_isRenderingStarted) { | |
| 264 resolver->reject(DOMException::create(InvalidStateError, | |
| 265 "cannot resume an offline context that has not started")); | |
| 266 return promise; | |
| 267 } | |
| 268 | |
| 269 // If the context is not in a suspended or closed state, reject the promise. | |
| 270 // TODO(hongchan): there is a conflict in the spec regarding resuming a | |
| 271 // running context. Per the current spec, the implementation here rejects | |
| 272 // the promise for resuming a running context. See crbug.com/545686 | |
| 273 if (contextState() != AudioContextState::Suspended) { | |
| 274 resolver->reject(DOMException::create(InvalidStateError, | |
| 275 "cannot resume an offline context that is " + state())); | |
| 276 return promise; | |
| 277 } | |
| 278 | |
| 279 // If the context is suspended, resume rendering by setting the state to | |
| 280 // "Running." and calling startRendering(). Note that resuming is possible | |
| 281 // only after the rendering started. | |
| 282 setContextState(Running); | |
| 283 destinationHandler().startRendering(); | |
| 284 | |
| 285 // Resolve the promise immediately. | |
| 286 resolver->resolve(); | |
| 287 | |
| 288 return promise; | |
| 289 } | |
| 290 | |
| 291 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) | |
| 292 { | |
| 293 // This CANNOT be called on OfflineAudioContext; this is only to implement | |
| 294 // the pure virtual interface from AbstractAudioContext. | |
| 295 RELEASE_ASSERT_NOT_REACHED(); | |
| 296 | |
| 147 return ScriptPromise::rejectWithDOMException( | 297 return ScriptPromise::rejectWithDOMException( |
| 148 scriptState, | 298 scriptState, |
| 149 DOMException::create( | 299 DOMException::create( |
| 150 InvalidAccessError, | 300 InvalidStateError, |
| 151 "cannot resume an OfflineAudioContext")); | 301 "cannot suspend offline audio context without the specified time.")) ; |
| 302 } | |
| 303 | |
| 304 void OfflineAudioContext::fireCompletionEvent() | |
| 305 { | |
| 306 ASSERT(isMainThread()); | |
| 307 | |
| 308 // We set the state to closed here so that the oncomplete event handler sees | |
| 309 // that the context has been closed. | |
| 310 setContextState(Closed); | |
| 311 | |
| 312 AudioBuffer* renderedBuffer = renderTarget(); | |
| 313 | |
| 314 ASSERT(renderedBuffer); | |
| 315 if (!renderedBuffer) | |
| 316 return; | |
| 317 | |
| 318 // Avoid firing the event if the document has already gone away. | |
| 319 if (executionContext()) { | |
| 320 // Call the offline rendering completion event listener and resolve the | |
| 321 // promise too. | |
| 322 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); | |
| 323 m_completeResolver->resolve(renderedBuffer); | |
| 324 } else { | |
| 325 // The resolver should be rejected when the execution context is gone. | |
| 326 m_completeResolver->reject(DOMException::create(InvalidStateError, | |
| 327 "the execution context does not exist")); | |
| 328 } | |
| 329 } | |
| 330 | |
| 331 bool OfflineAudioContext::handlePreOfflineRenderTasks() | |
| 332 { | |
| 333 ASSERT(isAudioThread()); | |
| 334 | |
| 335 // OfflineGraphAutoLocker here locks the audio graph for this scope and it | |
| 336 // is safe to lock the offline rendering because it does not care about | |
| 337 // glitches as opposed to the real-time rendering. It is also necessary to | |
| 338 // avoid tryLock() inside of this auto locker because the timing of | |
| 339 // suspension MUST NOT be delayed. | |
| 340 OfflineGraphAutoLocker locker(this); | |
| 341 | |
| 342 deferredTaskHandler().handleDeferredTasks(); | |
| 343 handleStoppableSourceNodes(); | |
| 344 | |
| 345 return shouldSuspend(); | |
| 346 } | |
| 347 | |
| 348 void OfflineAudioContext::handlePostOfflineRenderTasks() | |
| 349 { | |
| 350 ASSERT(isAudioThread()); | |
| 351 | |
| 352 // OfflineGraphAutoLocker here locks the render thread for this scope and it | |
| 353 // is safe to lock the offline rendering because of the same reason | |
| 354 // described in |handlePreOfflineRenderTasks|. | |
| 355 OfflineGraphAutoLocker locker(this); | |
| 356 | |
| 357 deferredTaskHandler().breakConnections(); | |
| 358 releaseFinishedSourceNodes(); | |
| 359 deferredTaskHandler().handleDeferredTasks(); | |
| 360 deferredTaskHandler().requestToDeleteHandlersOnMainThread(); | |
| 361 } | |
| 362 | |
| 363 | |
| 364 OfflineAudioDestinationHandler& OfflineAudioContext::destinationHandler() | |
| 365 { | |
| 366 return static_cast<OfflineAudioDestinationHandler&>(destination()->audioDest inationHandler()); | |
| 367 } | |
| 368 | |
| 369 void OfflineAudioContext::resolveSuspendOnMainThread(size_t frame) | |
| 370 { | |
| 371 ASSERT(isMainThread()); | |
| 372 | |
| 373 // Suspend the context first. This will fire onstatechange event. | |
| 374 setContextState(Suspended); | |
| 375 | |
| 376 ASSERT(m_scheduledSuspends.contains(frame)); | |
| 377 | |
| 378 // Wait until the suspend map is available for the removal. | |
| 379 AutoLocker locker(this); | |
| 380 | |
| 381 SuspendMap::iterator it = m_scheduledSuspends.find(frame); | |
| 382 it->value->resolve(); | |
| 383 | |
| 384 m_scheduledSuspends.remove(it); | |
| 385 } | |
| 386 | |
| 387 bool OfflineAudioContext::shouldSuspend() | |
| 388 { | |
| 389 ASSERT(isAudioThread()); | |
| 390 | |
| 391 if (m_scheduledSuspends.contains(currentSampleFrame())) | |
| 392 return true; | |
| 393 | |
| 394 return false; | |
| 152 } | 395 } |
| 153 | 396 |
| 154 } // namespace blink | 397 } // namespace blink |
| 155 | 398 |
| 156 #endif // ENABLE(WEB_AUDIO) | 399 #endif // ENABLE(WEB_AUDIO) |
| OLD | NEW |