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 13 matching lines...) Expand all Loading... | |
| 24 | 24 |
| 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 "core/dom/Document.h" | 31 #include "core/dom/Document.h" |
| 32 #include "core/dom/ExceptionCode.h" | 32 #include "core/dom/ExceptionCode.h" |
| 33 #include "core/dom/ExecutionContext.h" | 33 #include "core/dom/ExecutionContext.h" |
| 34 #include "modules/webaudio/OfflineAudioCompletionEvent.h" | |
| 35 #include "modules/webaudio/OfflineAudioDestinationNode.h" | |
| 36 #include "platform/Task.h" | |
| 37 #include "platform/ThreadSafeFunctional.h" | |
| 34 #include "platform/audio/AudioUtilities.h" | 38 #include "platform/audio/AudioUtilities.h" |
| 39 #include "public/platform/Platform.h" | |
| 40 | |
| 35 | 41 |
| 36 namespace blink { | 42 namespace blink { |
| 37 | 43 |
| 38 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) | 44 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) |
| 39 { | 45 { |
| 40 // FIXME: add support for workers. | 46 // FIXME: add support for workers. |
| 41 if (!context || !context->isDocument()) { | 47 if (!context || !context->isDocument()) { |
| 42 exceptionState.throwDOMException( | 48 exceptionState.throwDOMException( |
| 43 NotSupportedError, | 49 NotSupportedError, |
| 44 "Workers are not supported."); | 50 "Workers are not supported."); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 + ", " + String::number(sampleRate) | 91 + ", " + String::number(sampleRate) |
| 86 + ")"); | 92 + ")"); |
| 87 } | 93 } |
| 88 | 94 |
| 89 audioContext->suspendIfNeeded(); | 95 audioContext->suspendIfNeeded(); |
| 90 return audioContext; | 96 return audioContext; |
| 91 } | 97 } |
| 92 | 98 |
| 93 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) | 99 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) |
| 94 : AudioContext(document, numberOfChannels, numberOfFrames, sampleRate) | 100 : AudioContext(document, numberOfChannels, numberOfFrames, sampleRate) |
| 101 , m_isRenderingStarted(false) | |
| 102 , m_totalRenderFrames(numberOfFrames) | |
| 95 { | 103 { |
| 96 } | 104 } |
| 97 | 105 |
| 98 OfflineAudioContext::~OfflineAudioContext() | 106 OfflineAudioContext::~OfflineAudioContext() |
| 99 { | 107 { |
| 100 } | 108 } |
| 101 | 109 |
| 110 DEFINE_TRACE(OfflineAudioContext) | |
| 111 { | |
| 112 visitor->trace(m_completeResolver); | |
| 113 AudioContext::trace(visitor); | |
| 114 } | |
| 115 | |
| 116 bool OfflineAudioContext::shouldSuspendNow() | |
| 117 { | |
| 118 ASSERT(!isMainThread()); | |
| 119 | |
| 120 // If there is any scheduled suspend at |currentSampleFrame|, the context | |
| 121 // should be suspended. | |
| 122 if (m_scheduledSuspends.isEmpty() || !m_scheduledSuspends.contains(currentSa mpleFrame())) | |
|
haraken
2015/07/08 04:05:41
You won't need the m_scheduledSuspends.isEmpty() c
hongchan
2015/07/08 17:47:31
Done.
| |
| 123 return false; | |
| 124 | |
| 125 return true; | |
| 126 } | |
| 127 | |
| 128 void OfflineAudioContext::resolvePendingSuspendPromises() | |
| 129 { | |
| 130 ASSERT(!isMainThread()); | |
| 131 | |
| 132 // Find a suspend scheduled at |currentSampleFrame| and resolve the | |
| 133 // associated promise on the main thread. | |
| 134 SuspendContainerMap::iterator it = m_scheduledSuspends.find(currentSampleFra me()); | |
| 135 if (m_scheduledSuspends.isEmpty() || it == m_scheduledSuspends.end()) | |
|
haraken
2015/07/08 04:05:41
You won't need the m_scheduledSuspends.isEmpty() c
hongchan
2015/07/08 17:47:31
Done.
| |
| 136 return; | |
| 137 | |
| 138 RefPtr<ScheduledSuspendContainer> suspendContainer = it->value; | |
| 139 m_scheduledSuspends.remove(it); | |
| 140 Platform::current()->mainThread()->postTask(FROM_HERE, | |
| 141 threadSafeBind(&OfflineAudioContext::resolveSuspendContainerOnMainThread , this, suspendContainer.release())); | |
| 142 } | |
| 143 | |
| 144 void OfflineAudioContext::fireCompletionEvent() | |
| 145 { | |
| 146 ASSERT(isMainThread()); | |
| 147 | |
| 148 // We set the state to closed here so that the oncomplete event handler sees | |
| 149 // that the context has been closed. | |
| 150 setContextState(Closed); | |
| 151 | |
| 152 AudioBuffer* renderedBuffer = renderTarget(); | |
| 153 | |
| 154 ASSERT(renderedBuffer); | |
| 155 if (!renderedBuffer) | |
| 156 return; | |
| 157 | |
| 158 // Avoid firing the event if the document has already gone away. | |
| 159 if (executionContext()) { | |
| 160 // Call the offline rendering completion event listener and resolve the | |
| 161 // promise too. | |
| 162 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); | |
| 163 m_completeResolver->resolve(renderedBuffer); | |
| 164 } else { | |
| 165 // The resolver should be rejected when the execution context is gone. | |
| 166 m_completeResolver->reject(); | |
|
haraken
2015/07/08 04:05:41
Do we want to specify some DOM exception when reje
hongchan
2015/07/08 17:47:31
I am not completely sure about the meaning of thro
| |
| 167 } | |
| 168 } | |
| 169 | |
| 102 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) | 170 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) |
| 103 { | 171 { |
| 172 ASSERT(isMainThread()); | |
| 173 | |
| 104 // Calling close() on an OfflineAudioContext is not supported/allowed, | 174 // Calling close() on an OfflineAudioContext is not supported/allowed, |
| 105 // but it might well have been stopped by its execution context. | 175 // but it might well have been closed by its execution context. |
| 106 if (isContextClosed()) { | 176 if (isContextClosed()) { |
| 107 return ScriptPromise::rejectWithDOMException( | 177 return ScriptPromise::rejectWithDOMException( |
| 108 scriptState, | 178 scriptState, |
| 109 DOMException::create( | 179 DOMException::create( |
| 110 InvalidStateError, | 180 InvalidStateError, |
| 111 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); | 181 "cannot call startRendering on an OfflineAudioContext in a close d state.")); |
| 112 } | 182 } |
| 113 | 183 |
| 114 if (m_offlineResolver) { | 184 if (m_completeResolver) { |
| 115 // Can't call startRendering more than once. Return a rejected promise now. | 185 // Can't call startRendering more than once. Return a rejected promise now. |
| 116 return ScriptPromise::rejectWithDOMException( | 186 return ScriptPromise::rejectWithDOMException( |
| 117 scriptState, | 187 scriptState, |
| 118 DOMException::create( | 188 DOMException::create( |
| 119 InvalidStateError, | 189 InvalidStateError, |
| 120 "cannot call startRendering more than once")); | 190 "cannot call startRendering more than once")); |
| 121 } | 191 } |
| 122 | 192 |
| 123 m_offlineResolver = ScriptPromiseResolver::create(scriptState); | 193 // If the context is not in the suspended state, reject the promise. |
| 124 startRendering(); | 194 if (contextState() != AudioContextState::Suspended) { |
| 125 return m_offlineResolver->promise(); | 195 return ScriptPromise::rejectWithDOMException( |
| 196 scriptState, | |
| 197 DOMException::create( | |
| 198 InvalidStateError, | |
| 199 "cannot startRendering when an OfflineAudioContext is not in a s uspended state")); | |
| 200 } | |
| 201 | |
| 202 m_completeResolver = ScriptPromiseResolver::create(scriptState); | |
| 203 | |
| 204 // Start rendering and return the promise. | |
| 205 ASSERT(!m_isRenderingStarted); | |
| 206 m_isRenderingStarted = true; | |
| 207 setContextState(Running); | |
| 208 destination()->audioDestinationHandler().startRendering(); | |
| 209 | |
| 210 return m_completeResolver->promise(); | |
| 211 } | |
| 212 | |
| 213 ScriptPromise OfflineAudioContext::suspendOfflineRendering(ScriptState* scriptSt ate, double when) | |
| 214 { | |
| 215 ASSERT(isMainThread()); | |
| 216 | |
| 217 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | |
| 218 ScriptPromise promise = resolver->promise(); | |
| 219 | |
| 220 // The render thread does not exist; reject the promise. | |
|
haraken
2015/07/08 04:05:41
Just to confirm: If the render thread exists but d
hongchan
2015/07/08 17:47:32
Good question.
The answer is no. "If the render t
| |
| 221 if (!destination()->audioDestinationHandler().offlineRenderThread()) { | |
| 222 resolver->reject(DOMException::create(InvalidStateError, | |
| 223 "the rendering is already finished")); | |
| 224 return promise; | |
| 225 } | |
| 226 | |
| 227 // The specified suspend time is negative; reject the promise. | |
| 228 if (when < 0) { | |
| 229 resolver->reject(DOMException::create(InvalidStateError, | |
| 230 "negative suspend time (" + String::number(when) + ") is not allowed ")); | |
| 231 return promise; | |
| 232 } | |
| 233 | |
| 234 // Quantize the suspend time to the render quantum boundary. | |
| 235 size_t quantizedFrame = destination()->audioDestinationHandler().quantizeTim eToRenderQuantum(when); | |
| 236 | |
| 237 // The suspend time should be earlier than the total render frame. If the | |
| 238 // requested suspension time is equal to the total render frame, the promise | |
| 239 // will be rejected. | |
| 240 if (m_totalRenderFrames <= quantizedFrame) { | |
| 241 resolver->reject(DOMException::create(InvalidStateError, | |
| 242 "cannot schedule a suspend at frame " + String::number(quantizedFram e) + | |
| 243 " (" + String::number(when) + " seconds) " + | |
| 244 "because it is greater than or equal to the total render duration of " + | |
| 245 String::number(m_totalRenderFrames) + " frames")); | |
| 246 return promise; | |
| 247 } | |
| 248 | |
| 249 RefPtr<ScheduledSuspendContainer> suspendContainer = ScheduledSuspendContain er::create(when, quantizedFrame, resolver); | |
| 250 | |
| 251 // Validate the suspend and append if necessary on the render thread. | |
| 252 destination()->audioDestinationHandler().offlineRenderThread()->postTask(FRO M_HERE, | |
| 253 threadSafeBind(&OfflineAudioContext::validateSuspendContainerOnRenderThr ead, this, suspendContainer)); | |
| 254 | |
| 255 return promise; | |
| 256 } | |
| 257 | |
| 258 ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptSta te) | |
| 259 { | |
| 260 ASSERT(isMainThread()); | |
| 261 | |
| 262 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | |
| 263 ScriptPromise promise = resolver->promise(); | |
| 264 | |
| 265 // If the context is not in a suspended state, reject the promise. | |
| 266 if (contextState() != AudioContextState::Suspended) { | |
| 267 resolver->reject(DOMException::create(InvalidStateError, | |
| 268 "cannot resume a context that is not suspended")); | |
| 269 return promise; | |
| 270 } | |
| 271 | |
| 272 // If the rendering has not started, reject the promise. | |
| 273 if (!m_isRenderingStarted) { | |
| 274 resolver->reject(DOMException::create(InvalidStateError, | |
| 275 "cannot resume a context that has not started")); | |
| 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 destination()->audioDestinationHandler().startRendering(); | |
| 284 | |
| 285 // Resolve the promise immediately. | |
| 286 resolver->resolve(); | |
| 287 | |
| 288 return promise; | |
| 289 } | |
| 290 | |
| 291 void OfflineAudioContext::validateSuspendContainerOnRenderThread(PassRefPtr<Sche duledSuspendContainer> suspendContainer) | |
| 292 { | |
| 293 ASSERT(!isMainThread()); | |
| 294 | |
| 295 bool shouldBeRejected = false; | |
| 296 | |
| 297 // The specified suspend time is in the past; reject the promise. We cannot | |
| 298 // reject the promise in here, so set the error message before posting the | |
| 299 // rejection task to the main thread. | |
| 300 if (suspendContainer->suspendFrame() < currentSampleFrame()) { | |
| 301 shouldBeRejected = true; | |
| 302 suspendContainer->setErrorMessageForRejection(InvalidStateError, | |
| 303 "cannot schedule a suspend at frame " + | |
| 304 String::number(suspendContainer->suspendFrame()) + " (" + | |
| 305 String::number(suspendContainer->suspendTime()) + | |
| 306 " seconds) because it is earlier than the current frame of " + | |
| 307 String::number(currentSampleFrame())); | |
| 308 } else if (m_scheduledSuspends.contains(suspendContainer->suspendFrame())) { | |
| 309 // If there is a duplicate suspension at the same quantized frame, | |
| 310 // reject the promise. The rejection of promise should happen in the | |
| 311 // main thread, so we post a task to it. Here we simply set the error | |
| 312 // message and post a task to the main thread. | |
| 313 shouldBeRejected = true; | |
| 314 suspendContainer->setErrorMessageForRejection(InvalidStateError, | |
| 315 "cannot schedule more than one suspend at frame " + | |
| 316 String::number(suspendContainer->suspendFrame()) + " (" + | |
| 317 String::number(suspendContainer->suspendTime()) + " seconds)"); | |
| 318 } | |
| 319 | |
| 320 // Reject the promise if necessary on the main thread. | |
| 321 if (shouldBeRejected) { | |
| 322 Platform::current()->mainThread()->postTask(FROM_HERE, | |
| 323 threadSafeBind(&OfflineAudioContext::rejectSuspendContainerOnMainThr ead, this, suspendContainer)); | |
| 324 return; | |
| 325 } | |
| 326 | |
| 327 // If the validation check passes, we can add the container safely here in | |
| 328 // the render thread. | |
| 329 m_scheduledSuspends.add(suspendContainer->suspendFrame(), suspendContainer); | |
| 330 } | |
| 331 | |
| 332 void OfflineAudioContext::rejectSuspendContainerOnMainThread(PassRefPtr<Schedule dSuspendContainer> suspendContainer) | |
| 333 { | |
| 334 ASSERT(isMainThread()); | |
| 335 | |
| 336 suspendContainer->rejectPromise(); | |
| 337 } | |
| 338 | |
| 339 void OfflineAudioContext::resolveSuspendContainerOnMainThread(PassRefPtr<Schedul edSuspendContainer> suspendContainer) | |
| 340 { | |
| 341 ASSERT(isMainThread()); | |
| 342 | |
| 343 // Suspend the context first. This will fire onstatechange event. | |
| 344 setContextState(Suspended); | |
| 345 | |
| 346 suspendContainer->resolvePromise(); | |
| 347 } | |
| 348 | |
| 349 ScheduledSuspendContainer::ScheduledSuspendContainer(double suspendTime, size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) | |
| 350 : m_suspendTime(suspendTime) | |
| 351 , m_suspendFrame(suspendFrame) | |
| 352 , m_resolver(resolver) | |
| 353 { | |
| 354 ASSERT(isMainThread()); | |
| 355 } | |
| 356 | |
| 357 ScheduledSuspendContainer::~ScheduledSuspendContainer() | |
| 358 { | |
| 359 ASSERT(isMainThread()); | |
| 360 } | |
| 361 | |
| 362 PassRefPtr<ScheduledSuspendContainer> ScheduledSuspendContainer::create(double s uspendTime, size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> r esolver) | |
| 363 { | |
| 364 return adoptRef(new ScheduledSuspendContainer(suspendTime, suspendFrame, res olver)); | |
| 365 } | |
| 366 | |
| 367 bool ScheduledSuspendContainer::shouldSuspendAtFrame(size_t whenFrame) const | |
|
haraken
2015/07/08 04:05:41
Who calls this? It seems this method is unused.
hongchan
2015/07/08 17:47:31
Oops. This should have been removed with the intro
| |
| 368 { | |
| 369 ASSERT(whenFrame <= m_suspendFrame); | |
| 370 | |
| 371 if (m_suspendFrame != whenFrame) | |
| 372 return false; | |
| 373 | |
| 374 return true; | |
| 375 } | |
| 376 | |
| 377 void ScheduledSuspendContainer::setErrorMessageForRejection(ExceptionCode errorC ode, const String& errorMessage) | |
| 378 { | |
|
haraken
2015/07/08 04:05:41
Add ASSERT(!isMainThread()).
hongchan
2015/07/08 17:47:31
Done.
| |
| 379 m_errorCode = errorCode; | |
| 380 | |
| 381 // |errorMessage| is created on the render thread, but its actual usage is | |
| 382 // in the main thread. So we need to be thread-safe on this. | |
| 383 m_errorMessage = errorMessage.isolatedCopy(); | |
| 384 } | |
| 385 | |
| 386 void ScheduledSuspendContainer::resolvePromise() | |
| 387 { | |
| 388 ASSERT(isMainThread()); | |
| 389 | |
| 390 m_resolver->resolve(); | |
| 391 } | |
| 392 | |
| 393 void ScheduledSuspendContainer::rejectPromise() | |
| 394 { | |
| 395 ASSERT(isMainThread()); | |
| 396 ASSERT(m_errorCode); | |
| 397 ASSERT(m_errorMessage); | |
| 398 | |
| 399 m_resolver->reject(DOMException::create(m_errorCode, m_errorMessage)); | |
| 126 } | 400 } |
| 127 | 401 |
| 128 } // namespace blink | 402 } // namespace blink |
| 129 | 403 |
| 130 #endif // ENABLE(WEB_AUDIO) | 404 #endif // ENABLE(WEB_AUDIO) |
| OLD | NEW |