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/ThreadSafeFunctional.h" | |
| 34 #include "platform/audio/AudioUtilities.h" | 37 #include "platform/audio/AudioUtilities.h" |
| 38 #include "public/platform/Platform.h" | |
| 35 | 39 |
| 36 namespace blink { | 40 namespace blink { |
| 37 | 41 |
| 38 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) | 42 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) |
| 39 { | 43 { |
| 40 // FIXME: add support for workers. | 44 // FIXME: add support for workers. |
| 41 if (!context || !context->isDocument()) { | 45 if (!context || !context->isDocument()) { |
| 42 exceptionState.throwDOMException( | 46 exceptionState.throwDOMException( |
| 43 NotSupportedError, | 47 NotSupportedError, |
| 44 "Workers are not supported."); | 48 "Workers are not supported."); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 + ", " + String::number(sampleRate) | 89 + ", " + String::number(sampleRate) |
| 86 + ")"); | 90 + ")"); |
| 87 } | 91 } |
| 88 | 92 |
| 89 audioContext->suspendIfNeeded(); | 93 audioContext->suspendIfNeeded(); |
| 90 return audioContext; | 94 return audioContext; |
| 91 } | 95 } |
| 92 | 96 |
| 93 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) | 97 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) |
| 94 : AudioContext(document, numberOfChannels, numberOfFrames, sampleRate) | 98 : AudioContext(document, numberOfChannels, numberOfFrames, sampleRate) |
| 99 , m_isRenderingStarted(false) | |
| 100 , m_totalRenderFrames(numberOfFrames) | |
| 95 { | 101 { |
| 96 } | 102 } |
| 97 | 103 |
| 98 OfflineAudioContext::~OfflineAudioContext() | 104 OfflineAudioContext::~OfflineAudioContext() |
| 99 { | 105 { |
| 100 } | 106 } |
| 101 | 107 |
| 108 // FIXME: What should be done to trace members in OfflineAudioContext? | |
| 109 // DEFINE_TRACE(OfflineAudioContext) | |
| 110 // { | |
| 111 // visitor->trace(m_scheduledSuspends); | |
| 112 // visitor->trace(m_completeResolver); | |
| 113 // } | |
| 114 | |
| 115 OfflineAudioContext::ScheduledSuspendContainer::ScheduledSuspendContainer( | |
| 116 size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) | |
| 117 : m_suspendFrame(suspendFrame) | |
| 118 , m_resolver(resolver) | |
| 119 , m_isPending(false) | |
| 120 { | |
| 121 } | |
| 122 | |
| 123 OfflineAudioContext::ScheduledSuspendContainer::~ScheduledSuspendContainer() | |
| 124 { | |
| 125 } | |
| 126 | |
| 127 DEFINE_TRACE(OfflineAudioContext::ScheduledSuspendContainer) | |
| 128 { | |
| 129 visitor->trace(m_resolver); | |
| 130 } | |
| 131 | |
| 132 PassOwnPtr<OfflineAudioContext::ScheduledSuspendContainer> | |
| 133 OfflineAudioContext::ScheduledSuspendContainer::create( | |
| 134 size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) | |
| 135 { | |
| 136 return adoptPtr(new ScheduledSuspendContainer(suspendFrame, resolver)); | |
| 137 } | |
| 138 | |
| 139 bool OfflineAudioContext::ScheduledSuspendContainer::shouldSuspendAt(size_t when Frame) const | |
| 140 { | |
| 141 if (m_suspendFrame != whenFrame) | |
| 142 return false; | |
| 143 | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 bool OfflineAudioContext::ScheduledSuspendContainer::isPending() const | |
| 148 { | |
| 149 return m_isPending; | |
| 150 } | |
| 151 | |
| 152 void OfflineAudioContext::ScheduledSuspendContainer::markAsPending() | |
| 153 { | |
| 154 m_isPending = true; | |
| 155 } | |
| 156 | |
| 157 bool OfflineAudioContext::shouldSuspendNow() | |
| 158 { | |
| 159 ASSERT(!isMainThread()); | |
| 160 | |
| 161 // Suspend if necessary and mark the associated promise as pending. Note | |
| 162 // that duplicate entries in the suspend list are prohibited so it returns | |
| 163 // immediately when a valid suspend is found. This duplicate check is done | |
| 164 // by |suspendOfflineRendering|. | |
| 165 size_t nowFrame = currentSampleFrame(); | |
| 166 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) { | |
| 167 if (m_scheduledSuspends.at(index)->shouldSuspendAt(nowFrame)) { | |
| 168 m_scheduledSuspends.at(index)->markAsPending(); | |
| 169 return true; | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 return false; | |
| 174 } | |
| 175 | |
| 176 void OfflineAudioContext::resolvePendingSuspendPromises() | |
| 177 { | |
| 178 ASSERT(!isMainThread()); | |
| 179 | |
| 180 // Resolve promises marked as 'pending'. | |
| 181 if (m_scheduledSuspends.size() > 0) { | |
| 182 Platform::current()->mainThread()->postTask(FROM_HERE, | |
| 183 threadSafeBind(&OfflineAudioContext::resolvePendingSuspendPromisesOn MainThread, this)); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 void OfflineAudioContext::fireCompletionEvent() | |
| 188 { | |
| 189 ASSERT(isMainThread()); | |
| 190 if (!isMainThread()) | |
| 191 return; | |
| 192 | |
| 193 // We set the state to closed here so that the oncomplete event handler sees | |
| 194 // that the context has been closed. | |
| 195 setContextState(Closed); | |
| 196 | |
| 197 AudioBuffer* renderedBuffer = renderTarget().get(); | |
| 198 | |
| 199 ASSERT(renderedBuffer); | |
| 200 if (!renderedBuffer) | |
| 201 return; | |
| 202 | |
| 203 // Avoid firing the event if the document has already gone away. | |
| 204 if (executionContext()) { | |
| 205 // Call the offline rendering completion event listener and resolve the | |
| 206 // promise too. | |
| 207 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer)); | |
| 208 m_completeResolver->resolve(renderedBuffer); | |
| 209 } | |
| 210 } | |
| 211 | |
| 102 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) | 212 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) |
| 103 { | 213 { |
| 214 ASSERT(isMainThread()); | |
| 215 | |
| 216 // FIXME: This check causes crash on oac-detached-no-crash.html | |
| 217 // ASSERT(m_destinationNode); | |
|
Raymond Toy
2015/06/15 20:42:14
Does this still crash? If not, remove these lines.
| |
| 218 | |
| 104 // Calling close() on an OfflineAudioContext is not supported/allowed, | 219 // Calling close() on an OfflineAudioContext is not supported/allowed, |
| 105 // but it might well have been stopped by its execution context. | 220 // but it might well have been stopped by its execution context. |
| 106 if (isContextClosed()) { | 221 if (isContextClosed()) { |
| 107 return ScriptPromise::rejectWithDOMException( | 222 return ScriptPromise::rejectWithDOMException( |
| 108 scriptState, | 223 scriptState, |
| 109 DOMException::create( | 224 DOMException::create( |
| 110 InvalidStateError, | 225 InvalidStateError, |
| 111 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); | 226 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); |
| 112 } | 227 } |
| 113 | 228 |
| 114 if (m_offlineResolver) { | 229 if (m_completeResolver) { |
| 115 // Can't call startRendering more than once. Return a rejected promise now. | 230 // Can't call startRendering more than once. Return a rejected promise now. |
| 116 return ScriptPromise::rejectWithDOMException( | 231 return ScriptPromise::rejectWithDOMException( |
| 117 scriptState, | 232 scriptState, |
| 118 DOMException::create( | 233 DOMException::create( |
| 119 InvalidStateError, | 234 InvalidStateError, |
| 120 "cannot call startRendering more than once")); | 235 "cannot call startRendering more than once")); |
| 121 } | 236 } |
| 122 | 237 |
| 123 m_offlineResolver = ScriptPromiseResolver::create(scriptState); | 238 m_completeResolver = ScriptPromiseResolver::create(scriptState); |
| 124 startRendering(); | 239 |
| 125 return m_offlineResolver->promise(); | 240 // If the context is not in the suspended state, reject the promise. |
| 241 if (contextState() != AudioContextState::Suspended) { | |
| 242 return ScriptPromise::rejectWithDOMException( | |
| 243 scriptState, | |
| 244 DOMException::create( | |
| 245 InvalidStateError, | |
| 246 "cannot startRendering when an OfflineAudioContext is not in a s uspended state")); | |
| 247 } | |
| 248 | |
| 249 // Start rendering and return the promise. | |
| 250 m_isRenderingStarted = true; | |
| 251 setContextState(Running); | |
| 252 destination()->audioDestinationHandler().startRendering(); | |
| 253 return m_completeResolver->promise(); | |
| 254 } | |
| 255 | |
| 256 ScriptPromise OfflineAudioContext::suspendOfflineRendering(ScriptState* scriptSt ate, double when) | |
| 257 { | |
| 258 ASSERT(isMainThread()); | |
| 259 | |
| 260 // The specified suspend time is negative, reject the promise. | |
| 261 if (when < 0) { | |
| 262 return ScriptPromise::rejectWithDOMException( | |
| 263 scriptState, | |
| 264 DOMException::create( | |
| 265 InvalidStateError, | |
| 266 "negative suspend time is not allowed")); | |
| 267 } | |
| 268 | |
| 269 // Quantize the suspend time to the rendering block boundary. | |
| 270 size_t quantizedFrame = destination()->audioDestinationHandler().quantizeTim eToRenderQuantum(when); | |
| 271 | |
| 272 // The specified suspend time is in the past, reject the promise. | |
| 273 if (quantizedFrame < currentSampleFrame()) { | |
| 274 return ScriptPromise::rejectWithDOMException( | |
| 275 scriptState, | |
| 276 DOMException::create( | |
| 277 InvalidStateError, | |
| 278 "cannot schedule a suspend at " + String::number(when) + | |
| 279 " which is in the past")); | |
|
Raymond Toy
2015/06/15 20:42:13
Perhaps this is more helpful in debugging if we al
hongchan
2015/06/15 21:44:20
Done.
| |
| 280 } | |
| 281 | |
| 282 // The suspend time should be earlier than the total render frame. If the | |
| 283 // requested suspension time is equal to the total render frame, the promise | |
| 284 // will be rejected. | |
| 285 if (m_totalRenderFrames <= quantizedFrame) { | |
| 286 return ScriptPromise::rejectWithDOMException( | |
| 287 scriptState, | |
| 288 DOMException::create( | |
| 289 InvalidStateError, | |
| 290 "cannot schedule a suspend at " + String::number(when) + | |
| 291 " seconds which is greater than or equal to the total render duratio n of " + | |
| 292 String::number(m_totalRenderFrames / sampleRate()))); | |
|
Raymond Toy
2015/06/15 20:42:13
I wonder if this would be clearer if we used frame
hongchan
2015/06/15 21:44:20
Done.
| |
| 293 } | |
| 294 | |
| 295 // If there is a duplicate suspension at the same quantize frame, reject the | |
| 296 // promise. | |
| 297 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) { | |
| 298 if (m_scheduledSuspends.at(index)->shouldSuspendAt(quantizedFrame)) { | |
| 299 return ScriptPromise::rejectWithDOMException( | |
| 300 scriptState, | |
| 301 DOMException::create( | |
| 302 InvalidStateError, | |
| 303 "cannot schedule more than one suspend at " + String::number(whe n) + | |
| 304 " seconds whose quantized frame is " + String::number(quantizedF rame))); | |
| 305 } | |
| 306 } | |
| 307 | |
| 308 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | |
| 309 ScriptPromise promise = resolver->promise(); | |
| 310 m_scheduledSuspends.append(ScheduledSuspendContainer::create(quantizedFrame, resolver)); | |
| 311 | |
| 312 return promise; | |
| 313 } | |
| 314 | |
| 315 ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptSta te) | |
| 316 { | |
| 317 ASSERT(isMainThread()); | |
| 318 | |
| 319 AutoLocker locker(this); | |
| 320 | |
| 321 // If the context is not in a suspended state, reject the promise. | |
| 322 if (contextState() != AudioContextState::Suspended) { | |
| 323 return ScriptPromise::rejectWithDOMException( | |
| 324 scriptState, | |
| 325 DOMException::create( | |
| 326 InvalidStateError, | |
| 327 "cannot resume a context that is not suspended")); | |
| 328 } | |
| 329 | |
| 330 // If the rendering has not started, reject the promise. | |
| 331 if (!m_isRenderingStarted) { | |
| 332 return ScriptPromise::rejectWithDOMException( | |
| 333 scriptState, | |
| 334 DOMException::create( | |
| 335 InvalidStateError, | |
| 336 "cannot resume a context that has not started")); | |
| 337 } | |
| 338 | |
| 339 // If the context is suspended, resume rendering by calling startRendering() | |
| 340 // and set the state to "Running." Note that resuming is possible only after | |
| 341 // the rendering started. | |
| 342 setContextState(Running); | |
| 343 | |
| 344 destination()->audioDestinationHandler().startRendering(); | |
| 345 | |
| 346 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | |
| 347 ScriptPromise promise = resolver->promise(); | |
| 348 | |
| 349 // Resolve the promise immediately. | |
| 350 resolver->resolve(); | |
| 351 | |
| 352 return promise; | |
| 353 } | |
| 354 | |
| 355 void OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread() | |
| 356 { | |
| 357 ASSERT(isMainThread()); | |
| 358 AutoLocker locker(this); | |
| 359 | |
| 360 // Suspend the context first. This will fire onstatechange event. | |
| 361 setContextState(Suspended); | |
| 362 | |
| 363 // FIXME: is removing elements efficient? What if there are 10K suspends? | |
| 364 // Resolve any pending suspend and remove it from the list. | |
| 365 for (unsigned index = 0; index < m_scheduledSuspends.size();) { | |
| 366 if (m_scheduledSuspends.at(index)->isPending()) { | |
| 367 m_scheduledSuspends.at(index)->resolver()->resolve(); | |
| 368 m_scheduledSuspends.remove(index); | |
| 369 } else { | |
| 370 ++index; | |
| 371 } | |
| 372 } | |
| 126 } | 373 } |
| 127 | 374 |
| 128 } // namespace blink | 375 } // namespace blink |
| 129 | 376 |
| 130 #endif // ENABLE(WEB_AUDIO) | 377 #endif // ENABLE(WEB_AUDIO) |
| OLD | NEW |