Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(398)

Side by Side Diff: third_party/WebKit/Source/modules/webaudio/OfflineAudioContext.cpp

Issue 1405413004: Implement suspend() and resume() for OfflineAudioContext (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Clarifying error messages in layout tests Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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"
35 #include "platform/audio/AudioUtilities.h" 37 #include "platform/audio/AudioUtilities.h"
36 38
37 namespace blink { 39 namespace blink {
38 40
39 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState) 41 OfflineAudioContext* OfflineAudioContext::create(ExecutionContext* context, unsi gned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState& exceptionState)
40 { 42 {
41 // FIXME: add support for workers. 43 // FIXME: add support for workers.
42 if (!context || !context->isDocument()) { 44 if (!context || !context->isDocument()) {
43 exceptionState.throwDOMException( 45 exceptionState.throwDOMException(
44 NotSupportedError, 46 NotSupportedError,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 + ", " + String::number(sampleRate) 88 + ", " + String::number(sampleRate)
87 + ")"); 89 + ")");
88 } 90 }
89 91
90 audioContext->suspendIfNeeded(); 92 audioContext->suspendIfNeeded();
91 return audioContext; 93 return audioContext;
92 } 94 }
93 95
94 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate) 96 OfflineAudioContext::OfflineAudioContext(Document* document, unsigned numberOfCh annels, size_t numberOfFrames, float sampleRate)
95 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat e) 97 : AbstractAudioContext(document, numberOfChannels, numberOfFrames, sampleRat e)
98 , m_isRenderingStarted(false)
99 , m_totalRenderFrames(numberOfFrames)
96 { 100 {
101 // Create a new destination for offline rendering.
102 m_renderTarget = AudioBuffer::create(numberOfChannels, numberOfFrames, sampl eRate);
103 if (m_renderTarget.get())
104 m_destinationNode = OfflineAudioDestinationNode::create(this, m_renderTa rget.get());
105
106 initialize();
Raymond Toy 2015/10/21 18:22:45 What happens if m_renderTarget is actually nullptr
hongchan 2015/10/22 18:23:48 That means the OAC object cannot be created, we sh
97 } 107 }
98 108
99 OfflineAudioContext::~OfflineAudioContext() 109 OfflineAudioContext::~OfflineAudioContext()
100 { 110 {
101 } 111 }
102 112
113 DEFINE_TRACE(OfflineAudioContext)
114 {
115 visitor->trace(m_renderTarget);
116 visitor->trace(m_completeResolver);
117 visitor->trace(m_scheduledSuspends);
118 AbstractAudioContext::trace(visitor);
119 }
120
103 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) 121 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e)
104 { 122 {
123 ASSERT(isMainThread());
124
105 // Calling close() on an OfflineAudioContext is not supported/allowed, 125 // Calling close() on an OfflineAudioContext is not supported/allowed,
106 // but it might well have been stopped by its execution context. 126 // but it might well have been closed by its execution context.
107 if (isContextClosed()) { 127 if (isContextClosed()) {
108 return ScriptPromise::rejectWithDOMException( 128 return ScriptPromise::rejectWithDOMException(
109 scriptState, 129 scriptState,
110 DOMException::create( 130 DOMException::create(
111 InvalidStateError, 131 InvalidStateError,
112 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); 132 "cannot call startRendering on an OfflineAudioContext in a close d state."));
113 } 133 }
114 134
115 if (m_offlineResolver) { 135 if (m_completeResolver) {
116 // Can't call startRendering more than once. Return a rejected promise now. 136 // Can't call startRendering more than once. Return a rejected promise now.
117 return ScriptPromise::rejectWithDOMException( 137 return ScriptPromise::rejectWithDOMException(
118 scriptState, 138 scriptState,
119 DOMException::create( 139 DOMException::create(
120 InvalidStateError, 140 InvalidStateError,
121 "cannot call startRendering more than once")); 141 "cannot call startRendering more than once"));
122 } 142 }
123 143
124 m_offlineResolver = ScriptPromiseResolver::create(scriptState); 144 // If the context is not in the suspended state, reject the promise.
125 startRendering(); 145 if (contextState() != AudioContextState::Suspended) {
Raymond Toy 2015/10/21 18:22:45 Maybe combine this with the case in 127 and use st
hongchan 2015/10/22 18:23:49 Done. Also moved to this check and the ASSERT for
126 return m_offlineResolver->promise(); 146 return ScriptPromise::rejectWithDOMException(
147 scriptState,
148 DOMException::create(
149 InvalidStateError,
150 "cannot startRendering when an OfflineAudioContext is not in a s uspended state"));
151 }
152
153 m_completeResolver = ScriptPromiseResolver::create(scriptState);
154
155 ASSERT(!m_isRenderingStarted);
156
157 // Start rendering and return the promise.
158 m_isRenderingStarted = true;
159 setContextState(Running);
160 destinationHandler().startRendering();
161
162 return m_completeResolver->promise();
127 } 163 }
128 164
129 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState) 165 ScriptPromise OfflineAudioContext::closeContext(ScriptState* scriptState)
130 { 166 {
131 return ScriptPromise::rejectWithDOMException( 167 return ScriptPromise::rejectWithDOMException(scriptState,
Raymond Toy 2015/10/21 18:22:45 Does the style checks enforce this? If not, don't
hongchan 2015/10/22 18:23:48 Acknowledged. I made it consistent with other part
132 scriptState,
133 DOMException::create(InvalidAccessError, "cannot close an OfflineAudioCo ntext.")); 168 DOMException::create(InvalidAccessError, "cannot close an OfflineAudioCo ntext."));
134 } 169 }
135 170
171 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState, doub le when)
172 {
173 ASSERT(isMainThread());
174
175 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
176 ScriptPromise promise = resolver->promise();
177
178 // The render thread does not exist; reject the promise.
179 if (!destinationHandler().offlineRenderThread()) {
180 resolver->reject(DOMException::create(InvalidStateError,
181 "the rendering is already finished"));
182 return promise;
183 }
184
185 // The specified suspend time is negative; reject the promise.
186 if (when < 0) {
187 resolver->reject(DOMException::create(InvalidStateError,
188 "negative suspend time (" + String::number(when) + ") is not allowed "));
189 return promise;
190 }
191
192 // Quantize (to the lower boundary) the suspend time by the render quantum.
193 size_t frame = when * sampleRate();
194 frame -= frame % destinationHandler().renderQuantumFrames();
195
196 // The suspend time should be earlier than the total render frame. If the
197 // requested suspension time is equal to the total render frame, the promise
198 // will be rejected.
199 if (m_totalRenderFrames <= frame) {
200 resolver->reject(DOMException::create(InvalidStateError,
201 "cannot schedule a suspend at frame " + String::number(frame) +
202 " (" + String::number(when) + " seconds) " +
203 "because it is greater than or equal to the total render duration of " +
204 String::number(m_totalRenderFrames) + " frames"));
205 return promise;
206 }
207
208 // The specified suspend time is in the past; reject the promise.
209 if (frame < currentSampleFrame()) {
210 resolver->reject(DOMException::create(InvalidStateError,
211 "cannot schedule a suspend at frame " +
212 String::number(frame) + " (" + String::number(when) +
213 " seconds) because it is earlier than the current frame of " +
214 String::number(currentSampleFrame()) + " (" +
215 String::number(currentTime()) + " seconds)"));
216 return promise;
217 }
218
219 // If there is a duplicate suspension at the same quantized frame,
220 // reject the promise.
221 if (m_scheduledSuspends.contains(frame)) {
222 resolver->reject(DOMException::create(InvalidStateError,
223 "cannot schedule more than one suspend at frame " +
224 String::number(frame) + " (" +
225 String::number(when) + " seconds)"));
226 return promise;
227 }
228
229 // Wait until the suspend map is available for the insertion.
230 AutoLocker lock(this);
231
232 m_scheduledSuspends.add(frame, resolver);
233
234 return promise;
235 }
236
237 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState)
238 {
239 ASSERT(isMainThread());
240
241 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
242 ScriptPromise promise = resolver->promise();
243
244 // If the rendering has not started, reject the promise.
245 if (!m_isRenderingStarted) {
246 resolver->reject(DOMException::create(InvalidStateError,
247 "cannot resume a context that has not started"));
Raymond Toy 2015/10/21 18:22:45 Maybe say "resume an offline context", just to mak
hongchan 2015/10/22 18:23:48 Done.
248 return promise;
249 }
250
251 // If the context is not in a suspended or closed state, reject the promise.
252 // TODO(hongchan): there is a conflict in the spec regarding resuming a
253 // running context. Per the current spec, the implementation here rejects
254 // the promise for resuming a running context.
255 if (contextState() != AudioContextState::Suspended) {
256 resolver->reject(DOMException::create(InvalidStateError,
257 "cannot resume a context that is " + state()));
258 return promise;
259 }
260
261 // If the context is suspended, resume rendering by setting the state to
262 // "Running." and calling startRendering(). Note that resuming is possible
263 // only after the rendering started.
264 setContextState(Running);
265 destinationHandler().startRendering();
266
267 // Resolve the promise immediately.
268 resolver->resolve();
269
270 return promise;
271 }
272
136 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState) 273 ScriptPromise OfflineAudioContext::suspendContext(ScriptState* scriptState)
137 { 274 {
275 // This CANNOT be called on OfflineAudioContext; this is only to implement
276 // the pure virtual interface from AbstractAudioContext.
277 RELEASE_ASSERT_NOT_REACHED();
278
138 return ScriptPromise::rejectWithDOMException( 279 return ScriptPromise::rejectWithDOMException(
139 scriptState, 280 scriptState,
140 DOMException::create( 281 DOMException::create(
141 InvalidAccessError, 282 InvalidStateError,
142 "cannot suspend an OfflineAudioContext")); 283 "cannot suspend offline audio context without the specified time.")) ;
143 } 284 }
144 285
145 ScriptPromise OfflineAudioContext::resumeContext(ScriptState* scriptState) 286 void OfflineAudioContext::fireCompletionEvent()
146 { 287 {
147 return ScriptPromise::rejectWithDOMException( 288 ASSERT(isMainThread());
148 scriptState, 289
149 DOMException::create( 290 // We set the state to closed here so that the oncomplete event handler sees
150 InvalidAccessError, 291 // that the context has been closed.
151 "cannot resume an OfflineAudioContext")); 292 setContextState(Closed);
293
294 AudioBuffer* renderedBuffer = renderTarget();
295
296 ASSERT(renderedBuffer);
297 if (!renderedBuffer)
298 return;
299
300 // Avoid firing the event if the document has already gone away.
301 if (executionContext()) {
302 // Call the offline rendering completion event listener and resolve the
303 // promise too.
304 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer));
305 m_completeResolver->resolve(renderedBuffer);
306 } else {
307 // The resolver should be rejected when the execution context is gone.
308 m_completeResolver->reject(DOMException::create(InvalidStateError,
309 "the execution context does not exist"));
310 }
311 }
312
313 bool OfflineAudioContext::handlePreOfflineRenderTasks()
314 {
315 ASSERT(isAudioThread());
316
317 // This is offline rendering, so it is safe to lock. The offline rendering
318 // is non-realtime process and blocking the non-realtime render is not
319 // problematic. Note that tryLock() cannot be used here because the timing
320 // of suspension should be accurate.
321 deferredTaskHandler().offlineLock();
322
323 deferredTaskHandler().handleDeferredTasks();
324 handleStoppableSourceNodes();
325 deferredTaskHandler().unlock();
326
327 return shouldSuspend();
328 }
329
330 void OfflineAudioContext::handlePostOfflineRenderTasks()
331 {
332 ASSERT(isAudioThread());
333
334 // This is offline rendering, so it is safe to lock with the same reason
335 // described in |handlePreOfflineRenderTasks|.
336 deferredTaskHandler().offlineLock();
337
338 deferredTaskHandler().breakConnections();
339 releaseFinishedSourceNodes();
340 deferredTaskHandler().handleDeferredTasks();
341 deferredTaskHandler().requestToDeleteHandlersOnMainThread();
342
343 deferredTaskHandler().unlock();
344 }
345
346
347 OfflineAudioDestinationHandler& OfflineAudioContext::destinationHandler()
348 {
349 return static_cast<OfflineAudioDestinationHandler&>(destination()->audioDest inationHandler());
350 }
351
352 void OfflineAudioContext::resolveSuspendOnMainThread(size_t frame)
353 {
354 ASSERT(isMainThread());
355
356 // Suspend the context first. This will fire onstatechange event.
357 setContextState(Suspended);
358
359 ASSERT(m_scheduledSuspends.contains(frame));
360
361 // Wait until the suspend map is available for the removal.
362 AutoLocker locker(this);
363
364 SuspendMap::iterator it = m_scheduledSuspends.find(frame);
365 it->value->resolve();
366
367 m_scheduledSuspends.remove(it);
368 }
369
370 bool OfflineAudioContext::shouldSuspend()
371 {
372 ASSERT(isAudioThread());
373
374 if (m_scheduledSuspends.contains(currentSampleFrame()))
375 return true;
376
377 return false;
152 } 378 }
153 379
154 } // namespace blink 380 } // namespace blink
155 381
156 #endif // ENABLE(WEB_AUDIO) 382 #endif // ENABLE(WEB_AUDIO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698