1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef AudioContext_h_
8 #define AudioContext_h_
10 #include "nsDOMEventTargetHelper.h"
11 #include "nsCycleCollectionParticipant.h"
12 #include "mozilla/Attributes.h"
14 #include "EnableWebAudioCheck.h"
15 #include "nsAutoPtr.h"
16 #include "mozilla/dom/TypedArray.h"
17 #include "mozilla/dom/BindingUtils.h"
18 #include "mozilla/dom/AudioContextBinding.h"
19 #include "MediaBufferDecoder.h"
20 #include "StreamBuffer.h"
21 #include "MediaStreamGraph.h"
22 #include "nsTHashtable.h"
24 // X11 has a #define for CurrentTime. Unbelievable :-(.
25 // See content/media/DOMMediaStream.h for more fun!
37 struct WebAudioDecodeJob
;
43 class AudioBufferSourceNode
;
44 class AudioDestinationNode
;
46 class BiquadFilterNode
;
47 class ChannelMergerNode
;
48 class ChannelSplitterNode
;
51 class DynamicsCompressorNode
;
54 class MediaStreamAudioDestinationNode
;
55 class OfflineRenderSuccessCallback
;
57 class ScriptProcessorNode
;
61 class AudioContext MOZ_FINAL
: public nsDOMEventTargetHelper
,
62 public EnableWebAudioCheck
64 AudioContext(nsPIDOMWindow
* aParentWindow
,
66 uint32_t aNumberOfChannels
= 0,
68 float aSampleRate
= 0.0f
);
72 NS_DECL_ISUPPORTS_INHERITED
73 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioContext
,
74 nsDOMEventTargetHelper
)
76 nsPIDOMWindow
* GetParentObject() const
85 virtual JSObject
* WrapObject(JSContext
* aCx
,
86 JS::Handle
<JSObject
*> aScope
) MOZ_OVERRIDE
;
88 using nsDOMEventTargetHelper::DispatchTrustedEvent
;
90 // Constructor for regular AudioContext
91 static already_AddRefed
<AudioContext
>
92 Constructor(const GlobalObject
& aGlobal
, ErrorResult
& aRv
);
94 // Constructor for offline AudioContext
95 static already_AddRefed
<AudioContext
>
96 Constructor(const GlobalObject
& aGlobal
,
97 uint32_t aNumberOfChannels
,
102 // AudioContext methods
104 AudioDestinationNode
* Destination() const
109 float SampleRate() const
114 double CurrentTime() const;
116 AudioListener
* Listener();
118 already_AddRefed
<AudioBufferSourceNode
> CreateBufferSource();
120 already_AddRefed
<AudioBuffer
>
121 CreateBuffer(JSContext
* aJSContext
, uint32_t aNumberOfChannels
,
122 uint32_t aLength
, float aSampleRate
,
125 already_AddRefed
<AudioBuffer
>
126 CreateBuffer(JSContext
* aJSContext
, ArrayBuffer
& aBuffer
,
127 bool aMixToMono
, ErrorResult
& aRv
);
129 already_AddRefed
<MediaStreamAudioDestinationNode
>
130 CreateMediaStreamDestination(ErrorResult
& aRv
);
132 already_AddRefed
<ScriptProcessorNode
>
133 CreateScriptProcessor(uint32_t aBufferSize
,
134 uint32_t aNumberOfInputChannels
,
135 uint32_t aNumberOfOutputChannels
,
138 already_AddRefed
<ScriptProcessorNode
>
139 CreateJavaScriptNode(uint32_t aBufferSize
,
140 uint32_t aNumberOfInputChannels
,
141 uint32_t aNumberOfOutputChannels
,
144 return CreateScriptProcessor(aBufferSize
, aNumberOfInputChannels
,
145 aNumberOfOutputChannels
, aRv
);
148 already_AddRefed
<AnalyserNode
>
151 already_AddRefed
<GainNode
>
154 already_AddRefed
<WaveShaperNode
>
157 already_AddRefed
<GainNode
>
163 already_AddRefed
<DelayNode
>
164 CreateDelay(double aMaxDelayTime
, ErrorResult
& aRv
);
166 already_AddRefed
<DelayNode
>
167 CreateDelayNode(double aMaxDelayTime
, ErrorResult
& aRv
)
169 return CreateDelay(aMaxDelayTime
, aRv
);
172 already_AddRefed
<PannerNode
>
175 already_AddRefed
<ConvolverNode
>
178 already_AddRefed
<ChannelSplitterNode
>
179 CreateChannelSplitter(uint32_t aNumberOfOutputs
, ErrorResult
& aRv
);
181 already_AddRefed
<ChannelMergerNode
>
182 CreateChannelMerger(uint32_t aNumberOfInputs
, ErrorResult
& aRv
);
184 already_AddRefed
<DynamicsCompressorNode
>
185 CreateDynamicsCompressor();
187 already_AddRefed
<BiquadFilterNode
>
188 CreateBiquadFilter();
190 already_AddRefed
<PeriodicWave
>
191 CreatePeriodicWave(const Float32Array
& aRealData
, const Float32Array
& aImagData
,
194 void DecodeAudioData(const ArrayBuffer
& aBuffer
,
195 DecodeSuccessCallback
& aSuccessCallback
,
196 const Optional
<OwningNonNull
<DecodeErrorCallback
> >& aFailureCallback
);
198 // OfflineAudioContext methods
199 void StartRendering();
200 IMPL_EVENT_HANDLER(complete
)
202 bool IsOffline() const { return mIsOffline
; }
204 MediaStreamGraph
* Graph() const;
205 MediaStream
* DestinationStream() const;
206 void UnregisterAudioBufferSourceNode(AudioBufferSourceNode
* aNode
);
207 void UnregisterPannerNode(PannerNode
* aNode
);
208 void UnregisterScriptProcessorNode(ScriptProcessorNode
* aNode
);
209 void UpdatePannerSource();
211 uint32_t MaxChannelCount() const;
216 JSContext
* GetJSContext() const;
219 void RemoveFromDecodeQueue(WebAudioDecodeJob
* aDecodeJob
);
221 friend struct ::mozilla::WebAudioDecodeJob
;
224 // Note that it's important for mSampleRate to be initialized before
225 // mDestination, as mDestination's constructor needs to access it!
226 const float mSampleRate
;
227 nsRefPtr
<AudioDestinationNode
> mDestination
;
228 nsRefPtr
<AudioListener
> mListener
;
229 MediaBufferDecoder mDecoder
;
230 nsTArray
<nsRefPtr
<WebAudioDecodeJob
> > mDecodeJobs
;
231 // Two hashsets containing all the PannerNodes and AudioBufferSourceNodes,
232 // to compute the doppler shift, and also to stop AudioBufferSourceNodes.
233 // These are all weak pointers.
234 nsTHashtable
<nsPtrHashKey
<PannerNode
> > mPannerNodes
;
235 nsTHashtable
<nsPtrHashKey
<AudioBufferSourceNode
> > mAudioBufferSourceNodes
;
236 // Hashset containing all ScriptProcessorNodes in order to stop them.
237 // These are all weak pointers.
238 nsTHashtable
<nsPtrHashKey
<ScriptProcessorNode
> > mScriptProcessorNodes
;
239 // Number of channels passed in the OfflineAudioContext ctor.
240 uint32_t mNumberOfChannels
;