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

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: Added a layout test for synchronous graph manipulation 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/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
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
Raymond Toy 2015/06/16 17:57:09 Add note that pending promises will get resolved l
hongchan 2015/06/16 21:31:21 Done.
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
104 // Calling close() on an OfflineAudioContext is not supported/allowed, 216 // Calling close() on an OfflineAudioContext is not supported/allowed,
105 // but it might well have been stopped by its execution context. 217 // but it might well have been stopped by its execution context.
106 if (isContextClosed()) { 218 if (isContextClosed()) {
107 return ScriptPromise::rejectWithDOMException( 219 return ScriptPromise::rejectWithDOMException(
108 scriptState, 220 scriptState,
109 DOMException::create( 221 DOMException::create(
110 InvalidStateError, 222 InvalidStateError,
111 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); 223 "cannot call startRendering on an OfflineAudioContext in a stopp ed state."));
112 } 224 }
113 225
114 if (m_offlineResolver) { 226 if (m_completeResolver) {
115 // Can't call startRendering more than once. Return a rejected promise now. 227 // Can't call startRendering more than once. Return a rejected promise now.
116 return ScriptPromise::rejectWithDOMException( 228 return ScriptPromise::rejectWithDOMException(
117 scriptState, 229 scriptState,
118 DOMException::create( 230 DOMException::create(
119 InvalidStateError, 231 InvalidStateError,
120 "cannot call startRendering more than once")); 232 "cannot call startRendering more than once"));
121 } 233 }
122 234
123 m_offlineResolver = ScriptPromiseResolver::create(scriptState); 235 m_completeResolver = ScriptPromiseResolver::create(scriptState);
124 startRendering(); 236
125 return m_offlineResolver->promise(); 237 // If the context is not in the suspended state, reject the promise.
238 if (contextState() != AudioContextState::Suspended) {
239 return ScriptPromise::rejectWithDOMException(
240 scriptState,
241 DOMException::create(
242 InvalidStateError,
243 "cannot startRendering when an OfflineAudioContext is not in a s uspended state"));
244 }
245
246 // Start rendering and return the promise.
247 m_isRenderingStarted = true;
248 setContextState(Running);
249 destination()->audioDestinationHandler().startRendering();
250 return m_completeResolver->promise();
251 }
252
253 ScriptPromise OfflineAudioContext::suspendOfflineRendering(ScriptState* scriptSt ate, double when)
254 {
255 ASSERT(isMainThread());
256
257 // The specified suspend time is negative, reject the promise.
258 if (when < 0) {
259 return ScriptPromise::rejectWithDOMException(
260 scriptState,
261 DOMException::create(
262 InvalidStateError,
263 "negative suspend time is not allowed"));
264 }
265
266 // Quantize the suspend time to the rendering block boundary.
267 size_t quantizedFrame = destination()->audioDestinationHandler().quantizeTim eToRenderQuantum(when);
268
269 // The specified suspend time is in the past, reject the promise.
270 if (quantizedFrame < currentSampleFrame()) {
271 return ScriptPromise::rejectWithDOMException(
272 scriptState,
273 DOMException::create(
274 InvalidStateError,
275 "cannot schedule a suspend at frame " + String::number(quantizedFram e) +
276 " (" + String::number(when) + " seconds) because it is earlier than the current frame of " +
277 String::number(currentSampleFrame())));
278 }
279
280 // The suspend time should be earlier than the total render frame. If the
281 // requested suspension time is equal to the total render frame, the promise
282 // will be rejected.
283 if (m_totalRenderFrames <= quantizedFrame) {
284 return ScriptPromise::rejectWithDOMException(
285 scriptState,
286 DOMException::create(
287 InvalidStateError,
288 "cannot schedule a suspend at frame " + String::number(quantizedFram e) +
289 " (" + String::number(when) + " seconds) because it is greater than or equal to the total render duration of " +
290 String::number(m_totalRenderFrames) + " frames"));
291 }
292
293 // If there is a duplicate suspension at the same quantize frame, reject the
294 // promise.
295 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) {
296 if (m_scheduledSuspends.at(index)->shouldSuspendAt(quantizedFrame)) {
297 return ScriptPromise::rejectWithDOMException(
298 scriptState,
299 DOMException::create(
300 InvalidStateError,
301 "cannot schedule more than one suspend at frame " + String::numb er(quantizedFrame) +
302 " (" + String::number(when) + " seconds)"));
303 }
304 }
305
306 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
307 ScriptPromise promise = resolver->promise();
308 m_scheduledSuspends.append(ScheduledSuspendContainer::create(quantizedFrame, resolver));
309
310 return promise;
311 }
312
313 ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptSta te)
314 {
315 ASSERT(isMainThread());
316
317 AutoLocker locker(this);
318
319 // If the context is not in a suspended state, reject the promise.
320 if (contextState() != AudioContextState::Suspended) {
321 return ScriptPromise::rejectWithDOMException(
322 scriptState,
323 DOMException::create(
324 InvalidStateError,
325 "cannot resume a context that is not suspended"));
326 }
327
328 // If the rendering has not started, reject the promise.
329 if (!m_isRenderingStarted) {
330 return ScriptPromise::rejectWithDOMException(
331 scriptState,
332 DOMException::create(
333 InvalidStateError,
334 "cannot resume a context that has not started"));
335 }
336
337 // If the context is suspended, resume rendering by calling startRendering()
338 // and set the state to "Running." Note that resuming is possible only after
339 // the rendering started.
340 setContextState(Running);
341
342 destination()->audioDestinationHandler().startRendering();
343
344 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
345 ScriptPromise promise = resolver->promise();
346
347 // Resolve the promise immediately.
348 resolver->resolve();
349
350 return promise;
351 }
352
353 void OfflineAudioContext::resolvePendingSuspendPromisesOnMainThread()
354 {
355 ASSERT(isMainThread());
356 AutoLocker locker(this);
357
358 // Suspend the context first. This will fire onstatechange event.
359 setContextState(Suspended);
360
361 // FIXME: is removing elements efficient? What if there are 10K suspends?
362 // Resolve any pending suspend and remove it from the list.
Raymond Toy 2015/06/16 17:57:09 Since we should never have more than one suspend a
hongchan 2015/06/16 21:31:21 As a safety net I loop through the whole list here
363 for (unsigned index = 0; index < m_scheduledSuspends.size();) {
364 if (m_scheduledSuspends.at(index)->isPending()) {
365 m_scheduledSuspends.at(index)->resolver()->resolve();
366 m_scheduledSuspends.remove(index);
367 } else {
368 ++index;
369 }
370 }
126 } 371 }
127 372
128 } // namespace blink 373 } // namespace blink
129 374
130 #endif // ENABLE(WEB_AUDIO) 375 #endif // ENABLE(WEB_AUDIO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698