OLD | NEW |
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 AbstractAudioContext_h | 25 #ifndef BaseAudioContext_h |
26 #define AbstractAudioContext_h | 26 #define BaseAudioContext_h |
27 | 27 |
28 #include "bindings/core/v8/ActiveScriptWrappable.h" | 28 #include "bindings/core/v8/ActiveScriptWrappable.h" |
29 #include "bindings/core/v8/ScriptPromise.h" | 29 #include "bindings/core/v8/ScriptPromise.h" |
30 #include "bindings/core/v8/ScriptPromiseResolver.h" | 30 #include "bindings/core/v8/ScriptPromiseResolver.h" |
31 #include "core/dom/ActiveDOMObject.h" | 31 #include "core/dom/ActiveDOMObject.h" |
32 #include "core/dom/DOMTypedArray.h" | 32 #include "core/dom/DOMTypedArray.h" |
33 #include "core/events/EventListener.h" | 33 #include "core/events/EventListener.h" |
34 #include "modules/EventTargetModules.h" | 34 #include "modules/EventTargetModules.h" |
35 #include "modules/ModulesExport.h" | 35 #include "modules/ModulesExport.h" |
36 #include "modules/webaudio/AsyncAudioDecoder.h" | 36 #include "modules/webaudio/AsyncAudioDecoder.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 class OscillatorNode; | 71 class OscillatorNode; |
72 class PannerNode; | 72 class PannerNode; |
73 class PeriodicWave; | 73 class PeriodicWave; |
74 class ScriptProcessorNode; | 74 class ScriptProcessorNode; |
75 class ScriptPromiseResolver; | 75 class ScriptPromiseResolver; |
76 class ScriptState; | 76 class ScriptState; |
77 class SecurityOrigin; | 77 class SecurityOrigin; |
78 class StereoPannerNode; | 78 class StereoPannerNode; |
79 class WaveShaperNode; | 79 class WaveShaperNode; |
80 | 80 |
81 // AbstractAudioContext is the cornerstone of the web audio API and all AudioNod
es are created from it. | 81 // BaseAudioContext is the cornerstone of the web audio API and all AudioNodes a
re created from it. |
82 // For thread safety between the audio thread and the main thread, it has a rend
ering graph locking mechanism. | 82 // For thread safety between the audio thread and the main thread, it has a rend
ering graph locking mechanism. |
83 | 83 |
84 class MODULES_EXPORT AbstractAudioContext : public RefCountedGarbageCollectedEve
ntTargetWithInlineData<AbstractAudioContext>, public ActiveScriptWrappable, publ
ic ActiveDOMObject { | 84 class MODULES_EXPORT BaseAudioContext : public RefCountedGarbageCollectedEventTa
rgetWithInlineData<BaseAudioContext>, public ActiveScriptWrappable, public Activ
eDOMObject { |
85 REFCOUNTED_GARBAGE_COLLECTED_EVENT_TARGET(AbstractAudioContext); | 85 REFCOUNTED_GARBAGE_COLLECTED_EVENT_TARGET(BaseAudioContext); |
86 USING_GARBAGE_COLLECTED_MIXIN(AbstractAudioContext); | 86 USING_GARBAGE_COLLECTED_MIXIN(BaseAudioContext); |
87 DEFINE_WRAPPERTYPEINFO(); | 87 DEFINE_WRAPPERTYPEINFO(); |
88 public: | 88 public: |
89 // The state of an audio context. On creation, the state is Suspended. The
state is Running if | 89 // The state of an audio context. On creation, the state is Suspended. The
state is Running if |
90 // audio is being processed (audio graph is being pulled for data). The stat
e is Closed if the | 90 // audio is being processed (audio graph is being pulled for data). The stat
e is Closed if the |
91 // audio context has been closed. The valid transitions are from Suspended
to either Running or | 91 // audio context has been closed. The valid transitions are from Suspended
to either Running or |
92 // Closed; Running to Suspended or Closed. Once Closed, there are no valid t
ransitions. | 92 // Closed; Running to Suspended or Closed. Once Closed, there are no valid t
ransitions. |
93 enum AudioContextState { | 93 enum AudioContextState { |
94 Suspended, | 94 Suspended, |
95 Running, | 95 Running, |
96 Closed | 96 Closed |
97 }; | 97 }; |
98 | 98 |
99 // Create an AudioContext for rendering to the audio hardware. | 99 // Create an AudioContext for rendering to the audio hardware. |
100 static AbstractAudioContext* create(Document&, ExceptionState&); | 100 static BaseAudioContext* create(Document&, ExceptionState&); |
101 | 101 |
102 ~AbstractAudioContext() override; | 102 ~BaseAudioContext() override; |
103 | 103 |
104 DECLARE_VIRTUAL_TRACE(); | 104 DECLARE_VIRTUAL_TRACE(); |
105 | 105 |
106 // Is the destination node initialized and ready to handle audio? | 106 // Is the destination node initialized and ready to handle audio? |
107 bool isDestinationInitialized() const | 107 bool isDestinationInitialized() const |
108 { | 108 { |
109 AudioDestinationNode* dest = destination(); | 109 AudioDestinationNode* dest = destination(); |
110 return dest ? dest->audioDestinationHandler().isInitialized() : false; | 110 return dest ? dest->audioDestinationHandler().isInitialized() : false; |
111 } | 111 } |
112 | 112 |
(...skipping 25 matching lines...) Expand all Loading... |
138 | 138 |
139 // Handles the promise and callbacks when |decodeAudioData| is finished deco
ding. | 139 // Handles the promise and callbacks when |decodeAudioData| is finished deco
ding. |
140 void handleDecodeAudioData(AudioBuffer*, ScriptPromiseResolver*, AudioBuffer
Callback* successCallback, AudioBufferCallback* errorCallback); | 140 void handleDecodeAudioData(AudioBuffer*, ScriptPromiseResolver*, AudioBuffer
Callback* successCallback, AudioBufferCallback* errorCallback); |
141 | 141 |
142 AudioListener* listener() { return m_listener.get(); } | 142 AudioListener* listener() { return m_listener.get(); } |
143 | 143 |
144 virtual bool hasRealtimeConstraint() = 0; | 144 virtual bool hasRealtimeConstraint() = 0; |
145 | 145 |
146 // The AudioNode create methods are called on the main thread (from JavaScri
pt). | 146 // The AudioNode create methods are called on the main thread (from JavaScri
pt). |
147 AudioBufferSourceNode* createBufferSource(ExceptionState&); | 147 AudioBufferSourceNode* createBufferSource(ExceptionState&); |
148 MediaElementAudioSourceNode* createMediaElementSource(HTMLMediaElement*, Exc
eptionState&); | |
149 MediaStreamAudioSourceNode* createMediaStreamSource(MediaStream*, ExceptionS
tate&); | |
150 MediaStreamAudioDestinationNode* createMediaStreamDestination(ExceptionState
&); | |
151 GainNode* createGain(ExceptionState&); | 148 GainNode* createGain(ExceptionState&); |
152 BiquadFilterNode* createBiquadFilter(ExceptionState&); | 149 BiquadFilterNode* createBiquadFilter(ExceptionState&); |
153 WaveShaperNode* createWaveShaper(ExceptionState&); | 150 WaveShaperNode* createWaveShaper(ExceptionState&); |
154 DelayNode* createDelay(ExceptionState&); | 151 DelayNode* createDelay(ExceptionState&); |
155 DelayNode* createDelay(double maxDelayTime, ExceptionState&); | 152 DelayNode* createDelay(double maxDelayTime, ExceptionState&); |
156 PannerNode* createPanner(ExceptionState&); | 153 PannerNode* createPanner(ExceptionState&); |
157 ConvolverNode* createConvolver(ExceptionState&); | 154 ConvolverNode* createConvolver(ExceptionState&); |
158 DynamicsCompressorNode* createDynamicsCompressor(ExceptionState&); | 155 DynamicsCompressorNode* createDynamicsCompressor(ExceptionState&); |
159 AnalyserNode* createAnalyser(ExceptionState&); | 156 AnalyserNode* createAnalyser(ExceptionState&); |
160 ScriptProcessorNode* createScriptProcessor(ExceptionState&); | 157 ScriptProcessorNode* createScriptProcessor(ExceptionState&); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 // - it has been stopped by its execution context. | 246 // - it has been stopped by its execution context. |
250 virtual bool isContextClosed() const { return m_isCleared; } | 247 virtual bool isContextClosed() const { return m_isCleared; } |
251 | 248 |
252 // Get the security origin for this audio context. | 249 // Get the security origin for this audio context. |
253 SecurityOrigin* getSecurityOrigin() const; | 250 SecurityOrigin* getSecurityOrigin() const; |
254 | 251 |
255 // Get the PeriodicWave for the specified oscillator type. The table is ini
tialized internally | 252 // Get the PeriodicWave for the specified oscillator type. The table is ini
tialized internally |
256 // if necessary. | 253 // if necessary. |
257 PeriodicWave* periodicWave(int type); | 254 PeriodicWave* periodicWave(int type); |
258 protected: | 255 protected: |
259 explicit AbstractAudioContext(Document*); | 256 explicit BaseAudioContext(Document*); |
260 AbstractAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFr
ames, float sampleRate); | 257 BaseAudioContext(Document*, unsigned numberOfChannels, size_t numberOfFrames
, float sampleRate); |
261 | 258 |
262 void initialize(); | 259 void initialize(); |
263 void uninitialize(); | 260 void uninitialize(); |
264 | 261 |
265 void setContextState(AudioContextState); | 262 void setContextState(AudioContextState); |
266 | 263 |
267 virtual void didClose() {} | 264 virtual void didClose() {} |
268 | 265 |
269 // Tries to handle AudioBufferSourceNodes that were started but became disco
nnected or was never | 266 // Tries to handle AudioBufferSourceNodes that were started but became disco
nnected or was never |
270 // connected. Because these never get pulled anymore, they will stay around
forever. So if we | 267 // connected. Because these never get pulled anymore, they will stay around
forever. So if we |
271 // can, try to stop them so they can be collected. | 268 // can, try to stop them so they can be collected. |
272 void handleStoppableSourceNodes(); | 269 void handleStoppableSourceNodes(); |
273 | 270 |
274 Member<AudioDestinationNode> m_destinationNode; | 271 Member<AudioDestinationNode> m_destinationNode; |
275 | 272 |
276 // FIXME(dominicc): Move m_resumeResolvers to AudioContext, because only | 273 // FIXME(dominicc): Move m_resumeResolvers to AudioContext, because only |
277 // it creates these Promises. | 274 // it creates these Promises. |
278 // Vector of promises created by resume(). It takes time to handle them, so
we collect all of | 275 // Vector of promises created by resume(). It takes time to handle them, so
we collect all of |
279 // the promises here until they can be resolved or rejected. | 276 // the promises here until they can be resolved or rejected. |
280 HeapVector<Member<ScriptPromiseResolver>> m_resumeResolvers; | 277 HeapVector<Member<ScriptPromiseResolver>> m_resumeResolvers; |
281 | 278 |
282 void setClosedContextSampleRate(float newSampleRate) { m_closedContextSample
Rate = newSampleRate; } | 279 void setClosedContextSampleRate(float newSampleRate) { m_closedContextSample
Rate = newSampleRate; } |
283 float closedContextSampleRate() const { return m_closedContextSampleRate; } | 280 float closedContextSampleRate() const { return m_closedContextSampleRate; } |
| 281 void throwExceptionForClosedState(ExceptionState&); |
| 282 |
284 private: | 283 private: |
285 bool m_isCleared; | 284 bool m_isCleared; |
286 void clear(); | 285 void clear(); |
287 | 286 |
288 void throwExceptionForClosedState(ExceptionState&); | |
289 | |
290 // When the context goes away, there might still be some sources which | 287 // When the context goes away, there might still be some sources which |
291 // haven't finished playing. Make sure to release them here. | 288 // haven't finished playing. Make sure to release them here. |
292 void releaseActiveSourceNodes(); | 289 void releaseActiveSourceNodes(); |
293 | 290 |
294 Member<AudioListener> m_listener; | 291 Member<AudioListener> m_listener; |
295 | 292 |
296 // Only accessed in the audio thread. | 293 // Only accessed in the audio thread. |
297 // These raw pointers are safe because AudioSourceNodes in | 294 // These raw pointers are safe because AudioSourceNodes in |
298 // m_activeSourceNodes own them. | 295 // m_activeSourceNodes own them. |
299 Vector<AudioHandler*> m_finishedSourceHandlers; | 296 Vector<AudioHandler*> m_finishedSourceHandlers; |
(...skipping 18 matching lines...) Expand all Loading... |
318 // time and the audio context process loop is very fast, so we don't want to
call resolve an | 315 // time and the audio context process loop is very fast, so we don't want to
call resolve an |
319 // excessive number of times. | 316 // excessive number of times. |
320 bool m_isResolvingResumePromises; | 317 bool m_isResolvingResumePromises; |
321 | 318 |
322 unsigned m_connectionCount; | 319 unsigned m_connectionCount; |
323 | 320 |
324 // Graph locking. | 321 // Graph locking. |
325 bool m_didInitializeContextGraphMutex; | 322 bool m_didInitializeContextGraphMutex; |
326 RefPtr<DeferredTaskHandler> m_deferredTaskHandler; | 323 RefPtr<DeferredTaskHandler> m_deferredTaskHandler; |
327 | 324 |
328 // The state of the AbstractAudioContext. | 325 // The state of the BaseAudioContext. |
329 AudioContextState m_contextState; | 326 AudioContextState m_contextState; |
330 | 327 |
331 AsyncAudioDecoder m_audioDecoder; | 328 AsyncAudioDecoder m_audioDecoder; |
332 | 329 |
333 // When a context is closed, the sample rate is cleared. But decodeAudioDat
a can be called | 330 // When a context is closed, the sample rate is cleared. But decodeAudioDat
a can be called |
334 // after the context has been closed and it needs the sample rate. When the
context is closed, | 331 // after the context has been closed and it needs the sample rate. When the
context is closed, |
335 // the sample rate is saved here. | 332 // the sample rate is saved here. |
336 float m_closedContextSampleRate; | 333 float m_closedContextSampleRate; |
337 | 334 |
338 // Vector of promises created by decodeAudioData. This keeps the resolvers
alive until | 335 // Vector of promises created by decodeAudioData. This keeps the resolvers
alive until |
339 // decodeAudioData finishes decoding and can tell the main thread to resolve
them. | 336 // decodeAudioData finishes decoding and can tell the main thread to resolve
them. |
340 HeapHashSet<Member<ScriptPromiseResolver>> m_decodeAudioResolvers; | 337 HeapHashSet<Member<ScriptPromiseResolver>> m_decodeAudioResolvers; |
341 | 338 |
342 // PeriodicWave's for the builtin oscillator types. These only depend on th
e sample rate. so | 339 // PeriodicWave's for the builtin oscillator types. These only depend on th
e sample rate. so |
343 // they can be shared with all OscillatorNodes in the context. To conserve
memory, these are | 340 // they can be shared with all OscillatorNodes in the context. To conserve
memory, these are |
344 // lazily initiialized on first use. | 341 // lazily initiialized on first use. |
345 Member<PeriodicWave> m_periodicWaveSine; | 342 Member<PeriodicWave> m_periodicWaveSine; |
346 Member<PeriodicWave> m_periodicWaveSquare; | 343 Member<PeriodicWave> m_periodicWaveSquare; |
347 Member<PeriodicWave> m_periodicWaveSawtooth; | 344 Member<PeriodicWave> m_periodicWaveSawtooth; |
348 Member<PeriodicWave> m_periodicWaveTriangle; | 345 Member<PeriodicWave> m_periodicWaveTriangle; |
349 | 346 |
350 // This is considering 32 is large enough for multiple channels audio. | 347 // This is considering 32 is large enough for multiple channels audio. |
351 // It is somewhat arbitrary and could be increased if necessary. | 348 // It is somewhat arbitrary and could be increased if necessary. |
352 enum { MaxNumberOfChannels = 32 }; | 349 enum { MaxNumberOfChannels = 32 }; |
353 }; | 350 }; |
354 | 351 |
355 } // namespace blink | 352 } // namespace blink |
356 | 353 |
357 #endif // AbstractAudioContext_h | 354 #endif // BaseAudioContext_h |
OLD | NEW |