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

Side by Side Diff: Source/modules/webaudio/AudioContext.h

Issue 625363004: Implement suspend/resume for AudioContext (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix build error. Created 6 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010, Google Inc. All rights reserved. 2 * Copyright (C) 2010, 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
11 * documentation and/or other materials provided with the distribution. 11 * documentation and/or other materials provided with the distribution.
12 * 12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN Y
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y 16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN Y
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O N
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */ 23 */
24 24
25 #ifndef AudioContext_h 25 #ifndef AudioContext_h
26 #define AudioContext_h 26 #define AudioContext_h
27 27
28 #include "bindings/core/v8/ScriptPromise.h"
29 #include "bindings/core/v8/ScriptPromiseResolver.h"
28 #include "core/dom/ActiveDOMObject.h" 30 #include "core/dom/ActiveDOMObject.h"
29 #include "core/events/EventListener.h" 31 #include "core/events/EventListener.h"
30 #include "modules/EventTargetModules.h" 32 #include "modules/EventTargetModules.h"
31 #include "modules/webaudio/AsyncAudioDecoder.h" 33 #include "modules/webaudio/AsyncAudioDecoder.h"
32 #include "modules/webaudio/AudioDestinationNode.h" 34 #include "modules/webaudio/AudioDestinationNode.h"
33 #include "platform/audio/AudioBus.h" 35 #include "platform/audio/AudioBus.h"
34 #include "platform/heap/Handle.h" 36 #include "platform/heap/Handle.h"
35 #include "wtf/HashSet.h" 37 #include "wtf/HashSet.h"
36 #include "wtf/MainThread.h" 38 #include "wtf/MainThread.h"
37 #include "wtf/OwnPtr.h" 39 #include "wtf/OwnPtr.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 class WaveShaperNode; 72 class WaveShaperNode;
71 73
72 // AudioContext is the cornerstone of the web audio API and all AudioNodes are c reated from it. 74 // AudioContext is the cornerstone of the web audio API and all AudioNodes are c reated from it.
73 // For thread safety between the audio thread and the main thread, it has a rend ering graph locking mechanism. 75 // For thread safety between the audio thread and the main thread, it has a rend ering graph locking mechanism.
74 76
75 class AudioContext : public RefCountedGarbageCollectedWillBeGarbageCollectedFina lized<AudioContext>, public ActiveDOMObject, public EventTargetWithInlineData { 77 class AudioContext : public RefCountedGarbageCollectedWillBeGarbageCollectedFina lized<AudioContext>, public ActiveDOMObject, public EventTargetWithInlineData {
76 DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<A udioContext>); 78 DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<A udioContext>);
77 DEFINE_WRAPPERTYPEINFO(); 79 DEFINE_WRAPPERTYPEINFO();
78 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(AudioContext); 80 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(AudioContext);
79 public: 81 public:
82 // The state of an audio context. On creation, the state is Paused. The sta te is Running if
83 // audio is being processed (audio graph is being pulled for data). The stat e is Released if the
84 // audio context has been released. The valid transitions are from Paused t o either Running or
85 // Released; Running to Paused or Released. Once Released, there are no vali d transitions.
86 enum AudioContextState {
87 Paused,
88 Running,
89 Released
90 };
91
80 // Create an AudioContext for rendering to the audio hardware. 92 // Create an AudioContext for rendering to the audio hardware.
81 static AudioContext* create(Document&, ExceptionState&); 93 static AudioContext* create(Document&, ExceptionState&);
82 94
83 virtual ~AudioContext(); 95 virtual ~AudioContext();
84 96
85 virtual void trace(Visitor*) override; 97 virtual void trace(Visitor*) override;
86 98
87 bool isInitialized() const { return m_isInitialized; } 99 bool isInitialized() const { return m_isInitialized; }
88 bool isOfflineContext() { return m_isOfflineContext; } 100 bool isOfflineContext() { return m_isOfflineContext; }
89 101
90 // Document notification 102 // Document notification
91 virtual void stop() override final; 103 virtual void stop() override final;
92 virtual bool hasPendingActivity() const override; 104 virtual bool hasPendingActivity() const override;
93 105
94 AudioDestinationNode* destination() { return m_destinationNode.get(); } 106 AudioDestinationNode* destination() { return m_destinationNode.get(); }
95 size_t currentSampleFrame() const { return m_destinationNode->currentSampleF rame(); } 107 size_t currentSampleFrame() const { return m_destinationNode->currentSampleF rame(); }
96 double currentTime() const { return m_destinationNode->currentTime(); } 108 double currentTime() const { return m_destinationNode->currentTime(); }
97 float sampleRate() const { return m_destinationNode->sampleRate(); } 109 float sampleRate() const { return m_destinationNode->sampleRate(); }
110 String state() const;
98 111
99 AudioBuffer* createBuffer(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState&); 112 AudioBuffer* createBuffer(unsigned numberOfChannels, size_t numberOfFrames, float sampleRate, ExceptionState&);
100 113
101 // Asynchronous audio file data decoding. 114 // Asynchronous audio file data decoding.
102 void decodeAudioData(ArrayBuffer*, AudioBufferCallback*, AudioBufferCallback *, ExceptionState&); 115 void decodeAudioData(ArrayBuffer*, AudioBufferCallback*, AudioBufferCallback *, ExceptionState&);
103 116
104 AudioListener* listener() { return m_listener.get(); } 117 AudioListener* listener() { return m_listener.get(); }
105 118
106 // The AudioNode create methods are called on the main thread (from JavaScri pt). 119 // The AudioNode create methods are called on the main thread (from JavaScri pt).
107 AudioBufferSourceNode* createBufferSource(); 120 AudioBufferSourceNode* createBufferSource();
(...skipping 13 matching lines...) Expand all
121 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, ExceptionState &); 134 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, ExceptionState &);
122 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, size_t numberO fInputChannels, ExceptionState&); 135 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, size_t numberO fInputChannels, ExceptionState&);
123 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, size_t numberO fInputChannels, size_t numberOfOutputChannels, ExceptionState&); 136 ScriptProcessorNode* createScriptProcessor(size_t bufferSize, size_t numberO fInputChannels, size_t numberOfOutputChannels, ExceptionState&);
124 ChannelSplitterNode* createChannelSplitter(ExceptionState&); 137 ChannelSplitterNode* createChannelSplitter(ExceptionState&);
125 ChannelSplitterNode* createChannelSplitter(size_t numberOfOutputs, Exception State&); 138 ChannelSplitterNode* createChannelSplitter(size_t numberOfOutputs, Exception State&);
126 ChannelMergerNode* createChannelMerger(ExceptionState&); 139 ChannelMergerNode* createChannelMerger(ExceptionState&);
127 ChannelMergerNode* createChannelMerger(size_t numberOfInputs, ExceptionState &); 140 ChannelMergerNode* createChannelMerger(size_t numberOfInputs, ExceptionState &);
128 OscillatorNode* createOscillator(); 141 OscillatorNode* createOscillator();
129 PeriodicWave* createPeriodicWave(Float32Array* real, Float32Array* imag, Exc eptionState&); 142 PeriodicWave* createPeriodicWave(Float32Array* real, Float32Array* imag, Exc eptionState&);
130 143
144 // Pause/Resume
145 void suspendContext(ExceptionState&);
146 ScriptPromise resumeContext(ScriptState*);
147
131 // When a source node has no more processing to do (has finished playing), t hen it tells the context to dereference it. 148 // When a source node has no more processing to do (has finished playing), t hen it tells the context to dereference it.
132 void notifyNodeFinishedProcessing(AudioNode*); 149 void notifyNodeFinishedProcessing(AudioNode*);
133 150
134 // Called at the start of each render quantum. 151 // Called at the start of each render quantum.
135 void handlePreRenderTasks(); 152 void handlePreRenderTasks();
136 153
137 // Called at the end of each render quantum. 154 // Called at the end of each render quantum.
138 void handlePostRenderTasks(); 155 void handlePostRenderTasks();
139 156
140 // Called periodically at the end of each render quantum to dereference fini shed source nodes. 157 // Called periodically at the end of each render quantum to dereference fini shed source nodes.
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 GC_PLUGIN_IGNORE("http://crbug.com/404527") 284 GC_PLUGIN_IGNORE("http://crbug.com/404527")
268 Vector<AudioNode*> m_finishedNodes; 285 Vector<AudioNode*> m_finishedNodes;
269 286
270 // List of source nodes. This is either accessed when the graph lock is 287 // List of source nodes. This is either accessed when the graph lock is
271 // held, or on the main thread when the audio thread has finished. 288 // held, or on the main thread when the audio thread has finished.
272 // Oilpan: This Vector holds connection references. We must call 289 // Oilpan: This Vector holds connection references. We must call
273 // AudioNode::makeConnection when we add an AudioNode to this, and must call 290 // AudioNode::makeConnection when we add an AudioNode to this, and must call
274 // AudioNode::breakConnection() when we remove an AudioNode from this. 291 // AudioNode::breakConnection() when we remove an AudioNode from this.
275 HeapVector<Member<AudioNode> > m_referencedNodes; 292 HeapVector<Member<AudioNode> > m_referencedNodes;
276 293
294 // Stop rendering the audio graph.
295 void stopRendering();
296
297 // Handle Promises for resume().
298 void resolvePromisesForResume();
299 void resolvePromisesForResumeOnMainThread();
300
301 // Vector of promises created by resume(). It takes time to handle them, so we collect all of
302 // the promises here until they can be resolved or rejected.
303 Vector<RefPtr<ScriptPromiseResolver> > m_resumePromises;
304
305 // True if we're in the process of resolving promises for resume(). Resolvi ng can take some
306 // time and the audio context process loop is very fast, so we don't want to call resolve an
307 // excessive number of times.
308 bool m_isResolvingResumePromises;
309
277 class AudioNodeDisposer { 310 class AudioNodeDisposer {
278 public: 311 public:
279 explicit AudioNodeDisposer(AudioNode& node) : m_node(node) { } 312 explicit AudioNodeDisposer(AudioNode& node) : m_node(node) { }
280 ~AudioNodeDisposer(); 313 ~AudioNodeDisposer();
281 314
282 private: 315 private:
283 AudioNode& m_node; 316 AudioNode& m_node;
284 }; 317 };
285 HeapHashMap<WeakMember<AudioNode>, OwnPtr<AudioNodeDisposer> > m_liveNodes; 318 HeapHashMap<WeakMember<AudioNode>, OwnPtr<AudioNodeDisposer> > m_liveNodes;
286 319
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 // Only accessed in the audio thread. 364 // Only accessed in the audio thread.
332 // Oilpan: Since items are added to these vectors by the audio thread (not r egistered to Oilpan), 365 // Oilpan: Since items are added to these vectors by the audio thread (not r egistered to Oilpan),
333 // we cannot use HeapVectors. 366 // we cannot use HeapVectors.
334 GC_PLUGIN_IGNORE("http://crbug.com/404527") 367 GC_PLUGIN_IGNORE("http://crbug.com/404527")
335 Vector<AudioNode*> m_deferredBreakConnectionList; 368 Vector<AudioNode*> m_deferredBreakConnectionList;
336 369
337 Member<AudioBuffer> m_renderTarget; 370 Member<AudioBuffer> m_renderTarget;
338 371
339 bool m_isOfflineContext; 372 bool m_isOfflineContext;
340 373
374 AudioContextState m_contextState;
375 void setContextState(AudioContextState);
376
341 AsyncAudioDecoder m_audioDecoder; 377 AsyncAudioDecoder m_audioDecoder;
342 378
343 // Collection of nodes where the channel count mode has changed. We want the channel count mode 379 // Collection of nodes where the channel count mode has changed. We want the channel count mode
344 // to change in the pre- or post-rendering phase so as not to disturb the ru nning audio thread. 380 // to change in the pre- or post-rendering phase so as not to disturb the ru nning audio thread.
345 GC_PLUGIN_IGNORE("http://crbug.com/404527") 381 GC_PLUGIN_IGNORE("http://crbug.com/404527")
346 HashSet<AudioNode*> m_deferredCountModeChange; 382 HashSet<AudioNode*> m_deferredCountModeChange;
347 383
348 // This is considering 32 is large enough for multiple channels audio. 384 // This is considering 32 is large enough for multiple channels audio.
349 // It is somewhat arbitrary and could be increased if necessary. 385 // It is somewhat arbitrary and could be increased if necessary.
350 enum { MaxNumberOfChannels = 32 }; 386 enum { MaxNumberOfChannels = 32 };
351 }; 387 };
352 388
353 } // namespace blink 389 } // namespace blink
354 390
355 #endif // AudioContext_h 391 #endif // AudioContext_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698