Bug 883591 - Don't allow creating a MediaStreamDestinationNode on an OfflineAudioCont...
[gecko.git] / content / media / webaudio / AudioContext.h
blobf6095f8f8c0fce69192c3e74b4da21184634bfa3
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"
13 #include "nsCOMPtr.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!
26 #ifdef CurrentTime
27 #undef CurrentTime
28 #endif
30 struct JSContext;
31 class JSObject;
32 class nsPIDOMWindow;
34 namespace mozilla {
36 class ErrorResult;
37 struct WebAudioDecodeJob;
39 namespace dom {
41 class AnalyserNode;
42 class AudioBuffer;
43 class AudioBufferSourceNode;
44 class AudioDestinationNode;
45 class AudioListener;
46 class BiquadFilterNode;
47 class ChannelMergerNode;
48 class ChannelSplitterNode;
49 class ConvolverNode;
50 class DelayNode;
51 class DynamicsCompressorNode;
52 class GainNode;
53 class GlobalObject;
54 class MediaStreamAudioDestinationNode;
55 class OfflineRenderSuccessCallback;
56 class PannerNode;
57 class ScriptProcessorNode;
58 class WaveShaperNode;
59 class PeriodicWave;
61 class AudioContext MOZ_FINAL : public nsDOMEventTargetHelper,
62 public EnableWebAudioCheck
64 AudioContext(nsPIDOMWindow* aParentWindow,
65 bool aIsOffline,
66 uint32_t aNumberOfChannels = 0,
67 uint32_t aLength = 0,
68 float aSampleRate = 0.0f);
69 ~AudioContext();
71 public:
72 NS_DECL_ISUPPORTS_INHERITED
73 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioContext,
74 nsDOMEventTargetHelper)
76 nsPIDOMWindow* GetParentObject() const
78 return GetOwner();
81 void Shutdown();
82 void Suspend();
83 void Resume();
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,
98 uint32_t aLength,
99 float aSampleRate,
100 ErrorResult& aRv);
102 // AudioContext methods
104 AudioDestinationNode* Destination() const
106 return mDestination;
109 float SampleRate() const
111 return mSampleRate;
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,
123 ErrorResult& aRv);
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,
136 ErrorResult& aRv);
138 already_AddRefed<ScriptProcessorNode>
139 CreateJavaScriptNode(uint32_t aBufferSize,
140 uint32_t aNumberOfInputChannels,
141 uint32_t aNumberOfOutputChannels,
142 ErrorResult& aRv)
144 return CreateScriptProcessor(aBufferSize, aNumberOfInputChannels,
145 aNumberOfOutputChannels, aRv);
148 already_AddRefed<AnalyserNode>
149 CreateAnalyser();
151 already_AddRefed<GainNode>
152 CreateGain();
154 already_AddRefed<WaveShaperNode>
155 CreateWaveShaper();
157 already_AddRefed<GainNode>
158 CreateGainNode()
160 return CreateGain();
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>
173 CreatePanner();
175 already_AddRefed<ConvolverNode>
176 CreateConvolver();
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,
192 ErrorResult& aRv);
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;
213 void Mute() const;
214 void Unmute() const;
216 JSContext* GetJSContext() const;
218 private:
219 void RemoveFromDecodeQueue(WebAudioDecodeJob* aDecodeJob);
221 friend struct ::mozilla::WebAudioDecodeJob;
223 private:
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;
241 bool mIsOffline;
247 #endif