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

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

Issue 1140723003: Implement suspend() and resume() for OfflineAudioContext (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Moving the suspend list manipulation to the render thread (WIP) Created 5 years, 6 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 13 matching lines...) Expand all
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
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_scheduledSuspends);
113 visitor->trace(m_completeResolver);
114 AudioContext::trace(visitor);
115 }
116
117 bool OfflineAudioContext::shouldSuspendNow()
118 {
119 ASSERT(!isMainThread());
120
121 // Suspend if necessary and mark the associated promise as pending. Marked
122 // promises will be resolved later. Note that duplicate entries in the
123 // suspend list are prohibited so it returns immediately when a valid
124 // suspend is found. This duplicate check is done by
125 // |suspendOfflineRendering|.
126 size_t nowFrame = currentSampleFrame();
127 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) {
128 if (m_scheduledSuspends.at(index)->shouldSuspendAt(nowFrame)) {
129 m_scheduledSuspends.at(index)->markAsPending();
130 return true;
131 }
132 }
133
134 return false;
135 }
136
137 void OfflineAudioContext::resolvePendingSuspendPromises()
138 {
139 ASSERT(!isMainThread());
140
141 // Resolve promises marked as 'pending'.
142 if (m_scheduledSuspends.size() > 0) {
143 Platform::current()->mainThread()->postTask(FROM_HERE,
144 threadSafeBind(&OfflineAudioContext::resolvePendingSuspendPromisesOn MainThread, this));
145 }
146 }
147
148 void OfflineAudioContext::fireCompletionEvent()
149 {
150 ASSERT(isMainThread());
151
152 // We set the state to closed here so that the oncomplete event handler sees
153 // that the context has been closed.
154 setContextState(Closed);
155
156 AudioBuffer* renderedBuffer = renderTarget();
157
158 ASSERT(renderedBuffer);
159 if (!renderedBuffer)
160 return;
161
162 // Avoid firing the event if the document has already gone away.
163 if (executionContext()) {
164 // Call the offline rendering completion event listener and resolve the
165 // promise too.
166 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer));
167 m_completeResolver->resolve(renderedBuffer);
168 }
169 }
170
102 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) 171 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e)
103 { 172 {
173 ASSERT(isMainThread());
174
104 // Calling close() on an OfflineAudioContext is not supported/allowed, 175 // Calling close() on an OfflineAudioContext is not supported/allowed,
105 // but it might well have been stopped by its execution context. 176 // but it might well have been stopped by its execution context.
106 if (isContextClosed()) { 177 if (isContextClosed()) {
107 return ScriptPromise::rejectWithDOMException( 178 return ScriptPromise::rejectWithDOMException(
108 scriptState, 179 scriptState,
109 DOMException::create( 180 DOMException::create(
110 InvalidStateError, 181 InvalidStateError,
111 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); 182 "cannot call startRendering on an OfflineAudioContext in a stopp ed state."));
112 } 183 }
113 184
114 if (m_offlineResolver) { 185 if (m_completeResolver) {
115 // Can't call startRendering more than once. Return a rejected promise now. 186 // Can't call startRendering more than once. Return a rejected promise now.
116 return ScriptPromise::rejectWithDOMException( 187 return ScriptPromise::rejectWithDOMException(
117 scriptState, 188 scriptState,
118 DOMException::create( 189 DOMException::create(
119 InvalidStateError, 190 InvalidStateError,
120 "cannot call startRendering more than once")); 191 "cannot call startRendering more than once"));
121 } 192 }
122 193
123 m_offlineResolver = ScriptPromiseResolver::create(scriptState); 194 // If the context is not in the suspended state, reject the promise.
124 startRendering(); 195 if (contextState() != AudioContextState::Suspended) {
125 return m_offlineResolver->promise(); 196 return ScriptPromise::rejectWithDOMException(
197 scriptState,
198 DOMException::create(
199 InvalidStateError,
200 "cannot startRendering when an OfflineAudioContext is not in a s uspended state"));
201 }
202
203 m_completeResolver = ScriptPromiseResolver::create(scriptState);
204
205 // Start rendering and return the promise.
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 // validateSuspend(when, resolver);
221
222 // Validate the suspend and append if necessary on the render thread.
223 // TODO: this does not compile. threadSafeBind throws an error here.
224 destination()->audioDestinationHandler().offlineRenderThread()->postTask(FRO M_HERE,
225 threadSafeBind(&OfflineAudioContext::validateSuspend, this, when, resolv er));
hongchan 2015/06/19 22:15:40 This doesn't compile with the following error mess
yhirano 2015/06/22 02:26:19 You cannot pass a ScriptPromiseResolver to another
hongchan 2015/06/22 18:23:59 Done. So, is ScheduledSuspendContainer a ThreadSa
226
227 return promise;
hongchan 2015/06/19 22:15:39 @yhirano We return the promise here, but the rej
yhirano 2015/06/22 02:26:19 No, resolution / rejection must be done on the ori
hongchan 2015/06/22 18:23:59 Okay. I moved the resolution/rejection to the main
228 }
229
230 ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptSta te)
231 {
232 ASSERT(isMainThread());
233
234 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
235 ScriptPromise promise = resolver->promise();
236
237 // If the context is not in a suspended state, reject the promise.
238 if (contextState() != AudioContextState::Suspended) {
239 resolver->reject(DOMException::create(InvalidStateError,
240 "cannot resume a context that is not suspended"));
241 return promise;
242 }
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"));
248 return promise;
249 }
250
251 // If the context is suspended, resume rendering by setting the state to
252 // "Running." and calling startRendering(). Note that resuming is possible
253 // only after the rendering started.
254 setContextState(Running);
255 destination()->audioDestinationHandler().startRendering();
256
257 // Resolve the promise immediately.
258 resolver->resolve();
259
260 return promise;
261 }
262
263 void OfflineAudioContext::validateSuspend(double when, PassRefPtrWillBeRawPtr<Sc riptPromiseResolver> resolver)
264 {
265 ASSERT(!isMainThread());
266
267 // The specified suspend time is negative, reject the promise.
268 if (when < 0) {
269 resolver->reject(DOMException::create(InvalidStateError,
270 "negative suspend time (" + String::number(when) + ") is not allowed "));
271 return;
272 }
273
274 // Quantize the suspend time to the rendering block boundary.
275 size_t quantizedFrame = destination()->audioDestinationHandler().quantizeTim eToRenderQuantum(when);
276
277 // The specified suspend time is in the past, reject the promise.
278 if (quantizedFrame < currentSampleFrame()) {
279 resolver->reject(DOMException::create(InvalidStateError,
280 "cannot schedule a suspend at frame " + String::number(quantizedFram e) +
281 " (" + String::number(when) + " seconds) because it is earlier than the current frame of " +
282 String::number(currentSampleFrame())));
283 return;
284 }
285
286 // The suspend time should be earlier than the total render frame. If the
287 // requested suspension time is equal to the total render frame, the promise
288 // will be rejected.
289 if (m_totalRenderFrames <= quantizedFrame) {
290 resolver->reject(DOMException::create(InvalidStateError,
291 "cannot schedule a suspend at frame " + String::number(quantizedFram e) +
292 " (" + String::number(when) + " seconds) because it is greater than or equal to the total render duration of " +
293 String::number(m_totalRenderFrames) + " frames"));
294 return;
295 }
296
297 // If there is a duplicate suspension at the same quantize frame, reject the
298 // promise.
299 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) {
300 if (m_scheduledSuspends.at(index)->shouldSuspendAt(quantizedFrame)) {
301 resolver->reject(DOMException::create(
302 InvalidStateError,
303 "cannot schedule more than one suspend at frame " + String::numb er(quantizedFrame) +
304 " (" + String::number(when) + " seconds)"));
305 return;
306 }
307 }
308
309 m_scheduledSuspends.append(ScheduledSuspendContainer::create(quantizedFrame, resolver));
310 }
311
312 void OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread()
hongchan 2015/06/19 22:15:40 @yhirano This method needs to be rewritten. I wil
313 {
314 ASSERT(isMainThread());
315 AutoLocker locker(this);
316
317 // Suspend the context first. This will fire onstatechange event.
318 setContextState(Suspended);
319
320 // TODO: is removing elements efficient? What if there are 10K suspends?
321 // Resolve any pending suspend and remove it from the list.
322 bool pendingPromiseResolved = false;
323 for (unsigned index = 0; index < m_scheduledSuspends.size();) {
324 if (m_scheduledSuspends.at(index)->isPending()) {
325
326 // We should never have more than one pending suspend at any time.
327 ASSERT(!pendingPromiseResolved);
328
329 m_scheduledSuspends.at(index)->resolver()->resolve();
330 m_scheduledSuspends.remove(index);
331
332 pendingPromiseResolved = true;
333 } else {
334 ++index;
335 }
336 }
337 }
338
339 ScheduledSuspendContainer::ScheduledSuspendContainer(
340 size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver)
341 : m_suspendFrame(suspendFrame)
342 , m_resolver(resolver)
343 , m_isPending(false)
344 {
345 }
346
347 ScheduledSuspendContainer::~ScheduledSuspendContainer()
348 {
349 }
350
351 DEFINE_TRACE(ScheduledSuspendContainer)
352 {
353 visitor->trace(m_resolver);
354 }
355
356 ScheduledSuspendContainer* ScheduledSuspendContainer::create(
357 size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver)
358 {
359 return new ScheduledSuspendContainer(suspendFrame, resolver);
360 }
361
362 bool ScheduledSuspendContainer::shouldSuspendAt(size_t whenFrame) const
363 {
364 if (m_suspendFrame != whenFrame)
365 return false;
366
367 return true;
368 }
369
370 bool ScheduledSuspendContainer::isPending() const
371 {
372 return m_isPending;
373 }
374
375 void ScheduledSuspendContainer::markAsPending()
376 {
377 m_isPending = true;
126 } 378 }
127 379
128 } // namespace blink 380 } // namespace blink
129 381
130 #endif // ENABLE(WEB_AUDIO) 382 #endif // ENABLE(WEB_AUDIO)
OLDNEW
« no previous file with comments | « Source/modules/webaudio/OfflineAudioContext.h ('k') | Source/modules/webaudio/OfflineAudioContext.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698