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

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: Initial review + layout tests Created 5 years, 7 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)
95 { 100 {
101 m_totalRenderFrames = numberOfFrames;
Raymond Toy 2015/05/28 16:37:35 Why not move this to the initializer part?
hongchan 2015/06/09 20:49:59 Done.
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 when, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver)
Raymond Toy 2015/05/28 16:37:35 I think frame is better than when.
hongchan 2015/06/09 20:49:59 Done.
117 : m_when(when)
118 , m_resolver(resolver)
119 {
120 }
121
122 OfflineAudioContext::ScheduledSuspendContainer::~ScheduledSuspendContainer()
123 {
124 }
125
126 DEFINE_TRACE(OfflineAudioContext::ScheduledSuspendContainer)
127 {
128 visitor->trace(m_resolver);
129 }
130
131 PassOwnPtr<OfflineAudioContext::ScheduledSuspendContainer>
132 OfflineAudioContext::ScheduledSuspendContainer::create(
133 size_t when, PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver)
134 {
135 return adoptPtr(new ScheduledSuspendContainer(when, resolver));
136 }
137
138 bool OfflineAudioContext::ScheduledSuspendContainer::shouldSuspendAt(size_t when ) const
139 {
140 return m_when == when;
141 }
142
143 bool OfflineAudioContext::shouldSuspendNow()
144 {
145 ASSERT(!isMainThread());
146
147 // Suspend if necessary and resolve the associated promise.
148 size_t now = currentSampleFrame();
149 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) {
150 if (m_scheduledSuspends.at(index)->shouldSuspendAt(now)) {
151 // Resolve the scheduled suspend on the main thread. (async)
152 Platform::current()->mainThread()->postTask(FROM_HERE,
153 threadSafeBind(
154 &OfflineAudioContext::resolveSuspendOnMainThread,
155 this,
156 index));
Raymond Toy 2015/05/28 16:37:36 I think we have a problem here. If the main threa
hongchan 2015/06/09 20:49:59 Done. However, the post render task happens every
157 return true;
158 }
159 }
160
161 return false;
162 }
163
164 void OfflineAudioContext::fireCompletionEvent()
165 {
166 ASSERT(isMainThread());
167 if (!isMainThread())
168 return;
169
170 // We set the state to closed here so that the oncomplete event handler sees
171 // that the context has been closed.
172 setContextState(Closed);
173
174 AudioBuffer* renderedBuffer = m_renderTarget.get();
175
176 ASSERT(renderedBuffer);
177 if (!renderedBuffer)
178 return;
179
180 // Avoid firing the event if the document has already gone away.
181 if (executionContext()) {
182 // Call the offline rendering completion event listener and resolve the
183 // promise too.
184 dispatchEvent(OfflineAudioCompletionEvent::create(renderedBuffer));
185 m_completeResolver->resolve(renderedBuffer);
186 }
187 }
188
102 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e) 189 ScriptPromise OfflineAudioContext::startOfflineRendering(ScriptState* scriptStat e)
103 { 190 {
191 ASSERT(isMainThread());
192
193 // FIXME: This check causes crash on oac-detached-no-crash.html
194 // ASSERT(m_destinationNode);
Raymond Toy 2015/05/28 16:37:36 We'll have to fix this before landing this CL.
hongchan 2015/06/09 20:49:59 Acknowledged.
195
196 AutoLocker locker(this);
197
104 // Calling close() on an OfflineAudioContext is not supported/allowed, 198 // Calling close() on an OfflineAudioContext is not supported/allowed,
105 // but it might well have been stopped by its execution context. 199 // but it might well have been stopped by its execution context.
106 if (isContextClosed()) { 200 if (isContextClosed()) {
107 return ScriptPromise::rejectWithDOMException( 201 return ScriptPromise::rejectWithDOMException(
108 scriptState, 202 scriptState,
109 DOMException::create( 203 DOMException::create(
110 InvalidStateError, 204 InvalidStateError,
111 "cannot call startRendering on an OfflineAudioContext in a stopp ed state.")); 205 "cannot call startRendering on an OfflineAudioContext in a stopp ed state."));
112 } 206 }
113 207
114 if (m_offlineResolver) { 208 if (m_completeResolver) {
115 // Can't call startRendering more than once. Return a rejected promise now. 209 // Can't call startRendering more than once. Return a rejected promise now.
116 return ScriptPromise::rejectWithDOMException( 210 return ScriptPromise::rejectWithDOMException(
117 scriptState, 211 scriptState,
118 DOMException::create( 212 DOMException::create(
119 InvalidStateError, 213 InvalidStateError,
120 "cannot call startRendering more than once")); 214 "cannot call startRendering more than once"));
121 } 215 }
122 216
123 m_offlineResolver = ScriptPromiseResolver::create(scriptState); 217 m_completeResolver = ScriptPromiseResolver::create(scriptState);
124 startRendering(); 218
125 return m_offlineResolver->promise(); 219 if (contextState() == AudioContextState::Suspended) {
220 // If the context state is valid (Suspended), star rendering and return
Raymond Toy 2015/05/28 16:37:35 I think the code is easier to understand if you ha
hongchan 2015/06/09 20:49:59 Done.
221 // the promise.
222 destination()->audioDestinationHandler().startRendering();
223 m_isRenderingStarted = true;
224 setContextState(Running);
225 return m_completeResolver->promise();
226 }
227
228 // If the context is not in a valid state, reject promise.
229 return ScriptPromise::rejectWithDOMException(
230 scriptState,
231 DOMException::create(
232 InvalidStateError,
233 "invalid context state"));
Raymond Toy 2015/05/28 16:37:35 Use better error message saying why this is invali
hongchan 2015/06/09 20:49:59 Done.
234 }
235
236 ScriptPromise OfflineAudioContext::suspendOfflineRendering(ScriptState* scriptSt ate, double when)
237 {
238 ASSERT(isMainThread());
239
240 AutoLocker locker(this);
241
242 // Quantize the suspend time based on the rendering block boundary.
243 unsigned quantizedFrame = destination()->audioDestinationHandler().quantizeT ime(when);
244
245 if (isValidToScheduleAt(quantizedFrame)) {
246 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolv er::create(scriptState);
247 ScriptPromise promise = resolver->promise();
248 m_scheduledSuspends.append(ScheduledSuspendContainer::create(quantizedFr ame, resolver));
249
250 return promise;
251 }
252
253 // If suspendTime is already passed, reject the promise and throw an excepti on.
254 return ScriptPromise::rejectWithDOMException(
255 scriptState,
256 DOMException::create(
257 InvalidStateError,
258 "cannot schedule a suspend in the past, beyond the total render dura tion or at the duplicate position."));
Raymond Toy 2015/05/28 16:37:36 Can we provide more information here?
hongchan 2015/06/09 20:49:59 I broke this down into multiple checks and rejecti
259 }
260
261 ScriptPromise OfflineAudioContext::resumeOfflineRendering(ScriptState* scriptSta te)
262 {
263 ASSERT(isMainThread());
264
265 AutoLocker locker(this);
266
267 // If the context is suspended, resume rendering by calling startRendering()
268 // and set the state to "Running." Note that resuming is possible only after
269 // the rendering started.
270 if (contextState() == AudioContextState::Suspended && m_isRenderingStarted) {
Raymond Toy 2015/05/28 16:37:35 I think it's easier to understand if you reverse t
hongchan 2015/06/09 20:49:59 Done.
271 destination()->audioDestinationHandler().startRendering();
272 setContextState(Running);
273
274 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolv er::create(scriptState);
275 ScriptPromise promise = resolver->promise();
276
277 // Resolve the promise immediately.
278 resolver->resolve();
279
280 return promise;
281 }
282
283 // If the rendering is already running or not started, reject the promise.
284 return ScriptPromise::rejectWithDOMException(
285 scriptState,
286 DOMException::create(
287 InvalidStateError,
288 "cannot resume the context is already running or has not started"));
Raymond Toy 2015/05/28 16:37:36 Can we have a more specific message? We know exac
hongchan 2015/06/09 20:49:59 I separate this into two checks and rejections as
289 }
290
291 void OfflineAudioContext::resolveSuspendOnMainThread(unsigned index)
292 {
293 ASSERT(isMainThread());
294
295 // FIXME: This process (removing suspend from the list) should be
Raymond Toy 2015/05/28 16:37:35 Why does this need fixing?
hongchan 2015/06/09 20:49:59 First, the location of this comment is wrong, so I
296 // synchronous.
297
298 AutoLocker locker(this);
299
300 // Suspend the context first. This will fire onstatechange event.
301 setContextState(Suspended);
302
303 // Resolve the suspend and remove it from the list.
304 // FIXME: is removing an element efficient? What if there are 10K suspends?
305 m_scheduledSuspends.at(index)->resolver()->resolve();
306 m_scheduledSuspends.remove(index);
307 }
308
309 bool OfflineAudioContext::isValidToScheduleAt(size_t when)
310 {
311 // The specified suspend time should be between the current frame and the
312 // total render frame.
313 bool isInValidRange = currentSampleFrame() < when && when < m_totalRenderFra mes;
314
315 // Also there should be no duplicate in the existing scheduled suspends.
316 bool isDuplicate = false;
317 for (unsigned index = 0; index < m_scheduledSuspends.size(); ++index) {
318 if (m_scheduledSuspends.at(index)->shouldSuspendAt(when))
319 isDuplicate = true;
320 }
321
322 return isInValidRange && !isDuplicate;
126 } 323 }
127 324
128 } // namespace blink 325 } // namespace blink
129 326
130 #endif // ENABLE(WEB_AUDIO) 327 #endif // ENABLE(WEB_AUDIO)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698