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 m_destinationNode = OfflineAudioDestinationNode::create(this, m_renderTa
rget.get()); |
| 109 } else { |
| 110 exceptionState.throwRangeError(ExceptionMessages::failedToConstruct( |
| 111 "OfflineAudioContext", |
| 112 "failed to create OfflineAudioContext(" + |
| 113 String::number(numberOfChannels) + ", " + |
| 114 String::number(numberOfFrames) + ", " + |
| 115 String::number(sampleRate) + ")")); |
| 116 } |
| 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) |
| 189 { |
| 190 // This CANNOT be called on OfflineAudioContext; this is only to implement |
| 191 // the pure virtual interface from AbstractAudioContext. |
| 192 RELEASE_ASSERT_NOT_REACHED(); |
| 193 |
| 194 return ScriptPromise(); |
| 195 } |
| 196 |
| 197 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState, doub
le when) |
| 198 { |
| 199 ASSERT(isMainThread()); |
| 200 |
| 201 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 202 ScriptPromise promise = resolver->promise(); |
| 203 |
| 204 // The render thread does not exist; reject the promise. |
| 205 if (!destinationHandler().offlineRenderThread()) { |
| 206 resolver->reject(DOMException::create(InvalidStateError, |
| 207 "the rendering is already finished")); |
| 208 return promise; |
| 209 } |
| 210 |
| 211 // The specified suspend time is negative; reject the promise. |
| 212 if (when < 0) { |
| 213 resolver->reject(DOMException::create(InvalidStateError, |
| 214 "negative suspend time (" + String::number(when) + ") is not allowed
")); |
| 215 return promise; |
| 216 } |
| 217 |
| 218 // Quantize (to the lower boundary) the suspend time by the render quantum. |
| 219 size_t frame = when * sampleRate(); |
| 220 frame -= frame % destinationHandler().renderQuantumFrames(); |
| 221 |
| 222 // The suspend time should be earlier than the total render frame. If the |
| 223 // requested suspension time is equal to the total render frame, the promise |
| 224 // will be rejected. |
| 225 if (m_totalRenderFrames <= frame) { |
| 226 resolver->reject(DOMException::create(InvalidStateError, |
| 227 "cannot schedule a suspend at frame " + String::number(frame) + |
| 228 " (" + String::number(when) + " seconds) " + |
| 229 "because it is greater than or equal to the total render duration of
" + |
| 230 String::number(m_totalRenderFrames) + " frames")); |
| 231 return promise; |
| 232 } |
| 233 |
| 234 // The specified suspend time is in the past; reject the promise. |
| 235 if (frame < currentSampleFrame()) { |
| 236 resolver->reject(DOMException::create(InvalidStateError, |
| 237 "cannot schedule a suspend at frame " + |
| 238 String::number(frame) + " (" + String::number(when) + |
| 239 " seconds) because it is earlier than the current frame of " + |
| 240 String::number(currentSampleFrame()) + " (" + |
| 241 String::number(currentTime()) + " seconds)")); |
| 242 return promise; |
| 243 } |
| 244 |
| 245 // Wait until the suspend map is available for the insertion. Here we should |
| 246 // use AutoLocker because it locks the graph from the main thread. |
| 247 AutoLocker locker(this); |
| 248 |
| 249 // If there is a duplicate suspension at the same quantized frame, |
| 250 // reject the promise. |
| 251 if (m_scheduledSuspends.contains(frame)) { |
| 252 resolver->reject(DOMException::create(InvalidStateError, |
| 253 "cannot schedule more than one suspend at frame " + |
| 254 String::number(frame) + " (" + |
| 255 String::number(when) + " seconds)")); |
| 256 return promise; |
| 257 } |
| 258 |
| 259 m_scheduledSuspends.add(frame, resolver); |
| 260 |
| 261 return promise; |
143 } | 262 } |
144 | 263 |
145 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) | 264 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) |
146 { | 265 { |
147 return ScriptPromise::rejectWithDOMException( | 266 ASSERT(isMainThread()); |
148 scriptState, | 267 |
149 DOMException::create( | 268 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
150 InvalidAccessError, | 269 ScriptPromise promise = resolver->promise(); |
151 "cannot resume an OfflineAudioContext")); | 270 |
| 271 // If the rendering has not started, reject the promise. |
| 272 if (!m_isRenderingStarted) { |
| 273 resolver->reject(DOMException::create(InvalidStateError, |
| 274 "cannot resume an offline context that has not started")); |
| 275 return promise; |
| 276 } |
| 277 |
| 278 // If the context is in a closed state, reject the promise. |
| 279 if (contextState() == AudioContextState::Closed) { |
| 280 resolver->reject(DOMException::create(InvalidStateError, |
| 281 "cannot resume a closed offline context")); |
| 282 return promise; |
| 283 } |
| 284 |
| 285 // If the context is already running, resolve the promise without altering |
| 286 // the current state or starting the rendering loop. |
| 287 if (contextState() == AudioContextState::Running) { |
| 288 resolver->resolve(); |
| 289 return promise; |
| 290 } |
| 291 |
| 292 ASSERT(contextState() == AudioContextState::Suspended); |
| 293 |
| 294 // If the context is suspended, resume rendering by setting the state to |
| 295 // "Running". and calling startRendering(). Note that resuming is possible |
| 296 // only after the rendering started. |
| 297 setContextState(Running); |
| 298 destinationHandler().startRendering(); |
| 299 |
| 300 // Resolve the promise immediately. |
| 301 resolver->resolve(); |
| 302 |
| 303 return promise; |
| 304 } |
| 305 |
| 306 void OfflineAudioContext::fireCompletionEvent() |
| 307 { |
| 308 ASSERT(isMainThread()); |
| 309 |
| 310 // We set the state to closed here so that the oncomplete event handler sees |
| 311 // that the context has been closed. |
| 312 setContextState(Closed); |
| 313 |
| 314 AudioBuffer* renderedBuffer = renderTarget(); |
| 315 |
| 316 ASSERT(renderedBuffer); |
| 317 if (!renderedBuffer) |
| 318 return; |
| 319 |
| 320 // Avoid firing the event if the document has already gone away. |
| 321 if (executionContext()) { |
| 322 // Call the offline rendering completion event listener and resolve the |
| 323 // promise too. |
| 324 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); |
| 325 m_completeResolver->resolve(renderedBuffer); |
| 326 } else { |
| 327 // The resolver should be rejected when the execution context is gone. |
| 328 m_completeResolver->reject(DOMException::create(InvalidStateError, |
| 329 "the execution context does not exist")); |
| 330 } |
| 331 } |
| 332 |
| 333 bool OfflineAudioContext::handlePreOfflineRenderTasks() |
| 334 { |
| 335 ASSERT(isAudioThread()); |
| 336 |
| 337 // OfflineGraphAutoLocker here locks the audio graph for this scope. Note |
| 338 // that this locker does not use tryLock() inside 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 audio graph for the same reason |
| 353 // above in |handlePreOfflineRenderTasks|. |
| 354 OfflineGraphAutoLocker locker(this); |
| 355 |
| 356 deferredTaskHandler().breakConnections(); |
| 357 releaseFinishedSourceNodes(); |
| 358 deferredTaskHandler().handleDeferredTasks(); |
| 359 deferredTaskHandler().requestToDeleteHandlersOnMainThread(); |
| 360 } |
| 361 |
| 362 |
| 363 OfflineAudioDestinationHandler& OfflineAudioContext::destinationHandler() |
| 364 { |
| 365 return static_cast<OfflineAudioDestinationHandler&>(destination()->audioDest
inationHandler()); |
| 366 } |
| 367 |
| 368 void OfflineAudioContext::resolveSuspendOnMainThread(size_t frame) |
| 369 { |
| 370 ASSERT(isMainThread()); |
| 371 |
| 372 // Suspend the context first. This will fire onstatechange event. |
| 373 setContextState(Suspended); |
| 374 |
| 375 // Wait until the suspend map is available for the removal. |
| 376 AutoLocker locker(this); |
| 377 |
| 378 ASSERT(m_scheduledSuspends.contains(frame)); |
| 379 |
| 380 SuspendMap::iterator it = m_scheduledSuspends.find(frame); |
| 381 it->value->resolve(); |
| 382 |
| 383 m_scheduledSuspends.remove(it); |
| 384 } |
| 385 |
| 386 bool OfflineAudioContext::shouldSuspend() |
| 387 { |
| 388 ASSERT(isAudioThread()); |
| 389 |
| 390 // Note that the GraphLock is required before this check. Since this needs |
| 391 // to run on the audio thread, OfflineGraphAutoLocker must be used. |
| 392 if (m_scheduledSuspends.contains(currentSampleFrame())) |
| 393 return true; |
| 394 |
| 395 return false; |
152 } | 396 } |
153 | 397 |
154 } // namespace blink | 398 } // namespace blink |
155 | 399 |
156 #endif // ENABLE(WEB_AUDIO) | 400 #endif // ENABLE(WEB_AUDIO) |
OLD | NEW |