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

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: Removing ThreadSafeRefCounted from ScheduledSuspendContainer Created 5 years, 5 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_completeResolver);
113 AudioContext::trace(visitor);
114 }
115
116 bool OfflineAudioContext::shouldSuspendNow()
117 {
118 ASSERT(!isMainThread());
119
120 // If there is any scheduled suspend at |currentSampleFrame|, the context
121 // should be suspended.
122 if (!m_scheduledSuspends.contains(currentSampleFrame()))
123 return false;
124
125 return true;
126 }
127
128 void OfflineAudioContext::resolvePendingSuspendPromises()
129 {
130 ASSERT(!isMainThread());
131
132 // Find a suspend scheduled at |currentSampleFrame| and resolve the
133 // associated promise on the main thread.
134 SuspendContainerMap::iterator it = m_scheduledSuspends.find(currentSampleFra me());
135 if (it == m_scheduledSuspends.end())
136 return;
137
138 ScheduledSuspendContainer* suspendContainer = it->value;
139 m_scheduledSuspends.remove(it);
140 Platform::current()->mainThread()->postTask(FROM_HERE,
141 threadSafeBind(&OfflineAudioContext::resolveSuspendContainerOnMainThread , this, suspendContainer));
hongchan 2015/07/08 17:47:32 haraken@ Now |suspendContainer| being created on
hiroshige 2015/07/09 05:20:15 IIUC, - |suspendContainer| is created/managed/dest
hongchan 2015/07/09 17:08:37 Done.
142 }
143
144 void OfflineAudioContext::fireCompletionEvent()
145 {
146 ASSERT(isMainThread());
147
148 // We set the state to closed here so that the oncomplete event handler sees
149 // that the context has been closed.
150 setContextState(Closed);
151
152 AudioBuffer* renderedBuffer = renderTarget();
153
154 ASSERT(renderedBuffer);
155 if (!renderedBuffer)
156 return;
157
158 // Avoid firing the event if the document has already gone away.
159 if (executionContext()) {
160 // Call the offline rendering completion event listener and resolve the
161 // promise too.
162 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer));
163 m_completeResolver->resolve(renderedBuffer);
164 } else {
165 // The resolver should be rejected when the execution context is gone.
166 m_completeResolver->reject(DOMException::create(InvalidStateError,
167 "the execution context does not exist"));
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 closed 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 close d 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 ASSERT(!m_isRenderingStarted);
207 m_isRenderingStarted = true;
208 setContextState(Running);
209 destination()->audioDestinationHandler().startRendering();
210
211 return m_completeResolver->promise();
212 }
213
214 ScriptPromise OfflineAudioContext::suspendOfflineRendering(ScriptState* scriptSt ate, double when)
215 {
216 ASSERT(isMainThread());
217
218 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
219 ScriptPromise promise = resolver->promise();
220
221 // The render thread does not exist; reject the promise.
222 if (!destination()->audioDestinationHandler().offlineRenderThread()) {
223 resolver->reject(DOMException::create(InvalidStateError,
224 "the rendering is already finished"));
225 return promise;
226 }
227
228 // The specified suspend time is negative; reject the promise.
229 if (when < 0) {
230 resolver->reject(DOMException::create(InvalidStateError,
231 "negative suspend time (" + String::number(when) + ") is not allowed "));
232 return promise;
233 }
234
235 // Quantize the suspend time to the render quantum boundary.
236 size_t quantizedFrame = destination()->audioDestinationHandler().quantizeTim eToRenderQuantum(when);
237
238 // The suspend time should be earlier than the total render frame. If the
239 // requested suspension time is equal to the total render frame, the promise
240 // will be rejected.
241 if (m_totalRenderFrames <= quantizedFrame) {
242 resolver->reject(DOMException::create(InvalidStateError,
243 "cannot schedule a suspend at frame " + String::number(quantizedFram e) +
244 " (" + String::number(when) + " seconds) " +
245 "because it is greater than or equal to the total render duration of " +
246 String::number(m_totalRenderFrames) + " frames"));
247 return promise;
248 }
249
250 ScheduledSuspendContainer* suspendContainer = ScheduledSuspendContainer::cre ate(when, quantizedFrame, resolver);
251
252 // Validate the suspend and append if necessary on the render thread.
253 destination()->audioDestinationHandler().offlineRenderThread()->postTask(FRO M_HERE,
254 threadSafeBind(&OfflineAudioContext::validateSuspendContainerOnRenderThr ead, this, suspendContainer));
255
256 return promise;
257 }
258
259 ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptSta te)
260 {
261 ASSERT(isMainThread());
262
263 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
264 ScriptPromise promise = resolver->promise();
265
266 // If the context is not in a suspended state, reject the promise.
267 if (contextState() != AudioContextState::Suspended) {
268 resolver->reject(DOMException::create(InvalidStateError,
269 "cannot resume a context that is not suspended"));
270 return promise;
271 }
272
273 // If the rendering has not started, reject the promise.
274 if (!m_isRenderingStarted) {
275 resolver->reject(DOMException::create(InvalidStateError,
276 "cannot resume a context that has not started"));
277 return promise;
278 }
279
280 // If the context is suspended, resume rendering by setting the state to
281 // "Running." and calling startRendering(). Note that resuming is possible
282 // only after the rendering started.
283 setContextState(Running);
284 destination()->audioDestinationHandler().startRendering();
285
286 // Resolve the promise immediately.
287 resolver->resolve();
288
289 return promise;
290 }
291
292 void OfflineAudioContext::validateSuspendContainerOnRenderThread(ScheduledSuspen dContainer* suspendContainer)
293 {
294 ASSERT(!isMainThread());
295
296 bool shouldBeRejected = false;
297
298 // The specified suspend time is in the past; reject the promise. We cannot
299 // reject the promise in here, so set the error message before posting the
300 // rejection task to the main thread.
301 if (suspendContainer->suspendFrame() < currentSampleFrame()) {
302 shouldBeRejected = true;
303 suspendContainer->setErrorMessageForRejection(InvalidStateError,
304 "cannot schedule a suspend at frame " +
305 String::number(suspendContainer->suspendFrame()) + " (" +
306 String::number(suspendContainer->suspendTime()) +
307 " seconds) because it is earlier than the current frame of " +
308 String::number(currentSampleFrame()));
309 } else if (m_scheduledSuspends.contains(suspendContainer->suspendFrame())) {
310 // If there is a duplicate suspension at the same quantized frame,
311 // reject the promise. The rejection of promise should happen in the
312 // main thread, so we post a task to it. Here we simply set the error
313 // message and post a task to the main thread.
314 shouldBeRejected = true;
315 suspendContainer->setErrorMessageForRejection(InvalidStateError,
316 "cannot schedule more than one suspend at frame " +
317 String::number(suspendContainer->suspendFrame()) + " (" +
318 String::number(suspendContainer->suspendTime()) + " seconds)");
319 }
320
321 // Reject the promise if necessary on the main thread.
322 if (shouldBeRejected) {
323 Platform::current()->mainThread()->postTask(FROM_HERE,
324 threadSafeBind(&OfflineAudioContext::rejectSuspendContainerOnMainThr ead, this, suspendContainer));
325 return;
326 }
327
328 // If the validation check passes, we can add the container safely here in
329 // the render thread.
330 m_scheduledSuspends.add(suspendContainer->suspendFrame(), suspendContainer);
331 }
332
333 void OfflineAudioContext::rejectSuspendContainerOnMainThread(ScheduledSuspendCon tainer* suspendContainer)
334 {
335 ASSERT(isMainThread());
336
337 suspendContainer->rejectPromise();
338 }
339
340 void OfflineAudioContext::resolveSuspendContainerOnMainThread(ScheduledSuspendCo ntainer* suspendContainer)
341 {
342 ASSERT(isMainThread());
343
344 // Suspend the context first. This will fire onstatechange event.
345 setContextState(Suspended);
346
347 suspendContainer->resolvePromise();
348 }
349
350 ScheduledSuspendContainer::ScheduledSuspendContainer(double suspendTime, size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver)
351 : m_suspendTime(suspendTime)
352 , m_suspendFrame(suspendFrame)
353 , m_resolver(resolver)
354 {
355 ASSERT(isMainThread());
356 }
357
358 ScheduledSuspendContainer::~ScheduledSuspendContainer()
359 {
360 ASSERT(isMainThread());
361 }
362
363 ScheduledSuspendContainer* ScheduledSuspendContainer::create(double suspendTime, size_t suspendFrame, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver)
364 {
365 return new ScheduledSuspendContainer(suspendTime, suspendFrame, resolver);
366 }
367
368 void ScheduledSuspendContainer::setErrorMessageForRejection(ExceptionCode errorC ode, const String& errorMessage)
369 {
370 ASSERT(!isMainThread());
371
372 m_errorCode = errorCode;
373
374 // |errorMessage| is created on the render thread, but its actual usage is
375 // in the main thread. So we need to be thread-safe on this.
376 m_errorMessage = errorMessage.isolatedCopy();
377 }
378
379 void ScheduledSuspendContainer::resolvePromise()
380 {
381 ASSERT(isMainThread());
382
383 m_resolver->resolve();
384 }
385
386 void ScheduledSuspendContainer::rejectPromise()
387 {
388 ASSERT(isMainThread());
389 ASSERT(m_errorCode);
390 ASSERT(m_errorMessage);
391
392 m_resolver->reject(DOMException::create(m_errorCode, m_errorMessage));
126 } 393 }
127 394
128 } // namespace blink 395 } // namespace blink
129 396
130 #endif // ENABLE(WEB_AUDIO) 397 #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