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