2012-09-01 00:59:37 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-10-31 01:39:38 +04:00
|
|
|
#ifndef AudioContext_h_
|
|
|
|
#define AudioContext_h_
|
2012-09-19 03:07:33 +04:00
|
|
|
|
2013-10-29 04:08:14 +04:00
|
|
|
#include "mozilla/dom/AudioChannelBinding.h"
|
2013-02-02 02:13:23 +04:00
|
|
|
#include "MediaBufferDecoder.h"
|
2013-07-24 11:31:06 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-04-01 10:13:50 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-07-24 11:31:06 +04:00
|
|
|
#include "mozilla/dom/TypedArray.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsHashKeys.h"
|
2013-04-25 07:32:41 +04:00
|
|
|
#include "nsTHashtable.h"
|
2013-08-28 06:59:14 +04:00
|
|
|
#include "js/TypeDecls.h"
|
2014-01-04 22:15:41 +04:00
|
|
|
#include "nsIMemoryReporter.h"
|
2012-09-01 00:59:37 +04:00
|
|
|
|
2013-03-15 05:01:02 +04:00
|
|
|
// X11 has a #define for CurrentTime. Unbelievable :-(.
|
2014-10-25 21:24:36 +04:00
|
|
|
// See dom/media/DOMMediaStream.h for more fun!
|
2013-03-15 05:01:02 +04:00
|
|
|
#ifdef CurrentTime
|
|
|
|
#undef CurrentTime
|
|
|
|
#endif
|
|
|
|
|
2013-04-12 19:28:33 +04:00
|
|
|
class nsPIDOMWindow;
|
2012-09-01 00:59:37 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2013-08-15 23:44:14 +04:00
|
|
|
class DOMMediaStream;
|
2012-09-01 00:59:37 +04:00
|
|
|
class ErrorResult;
|
2013-08-15 23:44:14 +04:00
|
|
|
class MediaStream;
|
|
|
|
class MediaStreamGraph;
|
2012-09-01 00:59:37 +04:00
|
|
|
|
2012-09-08 02:13:26 +04:00
|
|
|
namespace dom {
|
|
|
|
|
2013-04-01 07:41:14 +04:00
|
|
|
class AnalyserNode;
|
2012-09-22 02:42:14 +04:00
|
|
|
class AudioBuffer;
|
2012-11-06 04:26:03 +04:00
|
|
|
class AudioBufferSourceNode;
|
|
|
|
class AudioDestinationNode;
|
|
|
|
class AudioListener;
|
2013-09-17 03:53:40 +04:00
|
|
|
class AudioNode;
|
2012-11-08 05:59:14 +04:00
|
|
|
class BiquadFilterNode;
|
2013-05-05 19:49:37 +04:00
|
|
|
class ChannelMergerNode;
|
2013-05-05 19:49:13 +04:00
|
|
|
class ChannelSplitterNode;
|
2013-06-11 00:07:55 +04:00
|
|
|
class ConvolverNode;
|
2012-11-01 04:26:03 +04:00
|
|
|
class DelayNode;
|
2012-11-07 05:01:11 +04:00
|
|
|
class DynamicsCompressorNode;
|
2012-11-06 04:26:03 +04:00
|
|
|
class GainNode;
|
2013-07-25 07:01:49 +04:00
|
|
|
class HTMLMediaElement;
|
|
|
|
class MediaElementAudioSourceNode;
|
2013-08-23 09:17:08 +04:00
|
|
|
class GlobalObject;
|
2013-05-21 23:17:47 +04:00
|
|
|
class MediaStreamAudioDestinationNode;
|
2013-07-24 15:29:39 +04:00
|
|
|
class MediaStreamAudioSourceNode;
|
2013-08-19 22:53:00 +04:00
|
|
|
class OscillatorNode;
|
2012-11-06 06:14:13 +04:00
|
|
|
class PannerNode;
|
2013-04-14 05:37:04 +04:00
|
|
|
class ScriptProcessorNode;
|
2014-11-19 20:15:13 +03:00
|
|
|
class StereoPannerNode;
|
2013-05-14 08:12:30 +04:00
|
|
|
class WaveShaperNode;
|
2013-06-20 02:24:26 +04:00
|
|
|
class PeriodicWave;
|
2014-10-23 14:07:48 +04:00
|
|
|
class Promise;
|
2012-09-19 03:07:33 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class AudioContext final : public DOMEventTargetHelper,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsIMemoryReporter
|
2012-09-01 00:59:37 +04:00
|
|
|
{
|
2013-05-17 03:30:42 +04:00
|
|
|
AudioContext(nsPIDOMWindow* aParentWindow,
|
|
|
|
bool aIsOffline,
|
2014-06-13 10:06:14 +04:00
|
|
|
AudioChannel aChannel,
|
2013-05-17 03:30:42 +04:00
|
|
|
uint32_t aNumberOfChannels = 0,
|
|
|
|
uint32_t aLength = 0,
|
|
|
|
float aSampleRate = 0.0f);
|
|
|
|
~AudioContext();
|
2012-09-01 00:59:37 +04:00
|
|
|
|
|
|
|
public:
|
2013-04-25 08:28:39 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(AudioContext,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2014-01-04 22:15:41 +04:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(MallocSizeOf)
|
2012-09-01 00:59:37 +04:00
|
|
|
|
2013-04-12 19:28:33 +04:00
|
|
|
nsPIDOMWindow* GetParentObject() const
|
2012-09-01 00:59:37 +04:00
|
|
|
{
|
2013-04-25 08:28:39 +04:00
|
|
|
return GetOwner();
|
2012-09-01 00:59:37 +04:00
|
|
|
}
|
|
|
|
|
2013-09-10 09:05:22 +04:00
|
|
|
void Shutdown(); // idempotent
|
2015-04-09 14:44:27 +03:00
|
|
|
void Suspend();
|
|
|
|
void Resume();
|
2013-03-22 06:59:33 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2012-09-01 00:59:37 +04:00
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
using DOMEventTargetHelper::DispatchTrustedEvent;
|
2013-05-17 03:30:57 +04:00
|
|
|
|
2013-05-17 03:30:41 +04:00
|
|
|
// Constructor for regular AudioContext
|
2012-09-01 00:59:37 +04:00
|
|
|
static already_AddRefed<AudioContext>
|
2012-12-03 20:07:49 +04:00
|
|
|
Constructor(const GlobalObject& aGlobal, ErrorResult& aRv);
|
2012-09-01 00:59:37 +04:00
|
|
|
|
2014-04-18 13:23:36 +04:00
|
|
|
// Constructor for regular AudioContext. A default audio channel is needed.
|
|
|
|
static already_AddRefed<AudioContext>
|
|
|
|
Constructor(const GlobalObject& aGlobal,
|
|
|
|
AudioChannel aChannel,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-05-17 03:30:41 +04:00
|
|
|
// Constructor for offline AudioContext
|
|
|
|
static already_AddRefed<AudioContext>
|
|
|
|
Constructor(const GlobalObject& aGlobal,
|
|
|
|
uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength,
|
|
|
|
float aSampleRate,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
|
|
|
// AudioContext methods
|
|
|
|
|
2012-09-19 03:07:33 +04:00
|
|
|
AudioDestinationNode* Destination() const
|
|
|
|
{
|
|
|
|
return mDestination;
|
|
|
|
}
|
|
|
|
|
2012-11-30 07:31:39 +04:00
|
|
|
float SampleRate() const
|
|
|
|
{
|
2013-05-24 21:09:29 +04:00
|
|
|
return mSampleRate;
|
2012-11-30 07:31:39 +04:00
|
|
|
}
|
|
|
|
|
2013-03-15 05:01:02 +04:00
|
|
|
double CurrentTime() const;
|
|
|
|
|
2012-11-06 04:26:03 +04:00
|
|
|
AudioListener* Listener();
|
|
|
|
|
2015-04-09 14:44:27 +03:00
|
|
|
already_AddRefed<AudioBufferSourceNode> CreateBufferSource();
|
2012-09-19 03:07:33 +04:00
|
|
|
|
2012-09-22 02:42:14 +04:00
|
|
|
already_AddRefed<AudioBuffer>
|
|
|
|
CreateBuffer(JSContext* aJSContext, uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength, float aSampleRate,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2013-05-21 23:17:47 +04:00
|
|
|
already_AddRefed<MediaStreamAudioDestinationNode>
|
2013-07-18 13:57:38 +04:00
|
|
|
CreateMediaStreamDestination(ErrorResult& aRv);
|
2013-05-21 23:17:47 +04:00
|
|
|
|
2013-04-14 05:37:04 +04:00
|
|
|
already_AddRefed<ScriptProcessorNode>
|
|
|
|
CreateScriptProcessor(uint32_t aBufferSize,
|
|
|
|
uint32_t aNumberOfInputChannels,
|
|
|
|
uint32_t aNumberOfOutputChannels,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2014-11-19 20:15:13 +03:00
|
|
|
already_AddRefed<StereoPannerNode>
|
2015-04-09 14:44:27 +03:00
|
|
|
CreateStereoPanner();
|
2014-11-19 20:15:13 +03:00
|
|
|
|
2013-04-01 07:41:14 +04:00
|
|
|
already_AddRefed<AnalyserNode>
|
2015-04-09 14:44:27 +03:00
|
|
|
CreateAnalyser();
|
2013-04-01 07:41:14 +04:00
|
|
|
|
2012-10-31 23:09:32 +04:00
|
|
|
already_AddRefed<GainNode>
|
2015-04-09 14:44:27 +03:00
|
|
|
CreateGain();
|
2012-10-31 23:09:32 +04:00
|
|
|
|
2013-05-14 08:12:30 +04:00
|
|
|
already_AddRefed<WaveShaperNode>
|
2015-04-09 14:44:27 +03:00
|
|
|
CreateWaveShaper();
|
2013-05-14 08:12:30 +04:00
|
|
|
|
2013-07-25 07:01:49 +04:00
|
|
|
already_AddRefed<MediaElementAudioSourceNode>
|
|
|
|
CreateMediaElementSource(HTMLMediaElement& aMediaElement, ErrorResult& aRv);
|
2013-07-24 15:29:39 +04:00
|
|
|
already_AddRefed<MediaStreamAudioSourceNode>
|
2013-07-25 06:07:34 +04:00
|
|
|
CreateMediaStreamSource(DOMMediaStream& aMediaStream, ErrorResult& aRv);
|
2013-07-24 15:29:39 +04:00
|
|
|
|
2012-11-01 04:26:03 +04:00
|
|
|
already_AddRefed<DelayNode>
|
2012-11-20 00:52:29 +04:00
|
|
|
CreateDelay(double aMaxDelayTime, ErrorResult& aRv);
|
2012-11-01 04:26:03 +04:00
|
|
|
|
2012-11-06 06:14:13 +04:00
|
|
|
already_AddRefed<PannerNode>
|
2015-04-09 14:44:27 +03:00
|
|
|
CreatePanner();
|
2012-11-06 06:14:13 +04:00
|
|
|
|
2013-06-11 00:07:55 +04:00
|
|
|
already_AddRefed<ConvolverNode>
|
2015-04-09 14:44:27 +03:00
|
|
|
CreateConvolver();
|
2013-06-11 00:07:55 +04:00
|
|
|
|
2013-05-05 19:49:13 +04:00
|
|
|
already_AddRefed<ChannelSplitterNode>
|
|
|
|
CreateChannelSplitter(uint32_t aNumberOfOutputs, ErrorResult& aRv);
|
|
|
|
|
2013-05-05 19:49:37 +04:00
|
|
|
already_AddRefed<ChannelMergerNode>
|
|
|
|
CreateChannelMerger(uint32_t aNumberOfInputs, ErrorResult& aRv);
|
|
|
|
|
2012-11-07 05:01:11 +04:00
|
|
|
already_AddRefed<DynamicsCompressorNode>
|
2015-04-09 14:44:27 +03:00
|
|
|
CreateDynamicsCompressor();
|
2012-11-07 05:01:11 +04:00
|
|
|
|
2012-11-08 05:59:14 +04:00
|
|
|
already_AddRefed<BiquadFilterNode>
|
2015-04-09 14:44:27 +03:00
|
|
|
CreateBiquadFilter();
|
2012-11-08 05:59:14 +04:00
|
|
|
|
2013-08-19 22:53:00 +04:00
|
|
|
already_AddRefed<OscillatorNode>
|
2015-04-09 14:44:27 +03:00
|
|
|
CreateOscillator();
|
2013-08-19 22:53:00 +04:00
|
|
|
|
2013-06-20 02:24:26 +04:00
|
|
|
already_AddRefed<PeriodicWave>
|
|
|
|
CreatePeriodicWave(const Float32Array& aRealData, const Float32Array& aImagData,
|
|
|
|
ErrorResult& aRv);
|
2013-05-28 15:19:07 +04:00
|
|
|
|
2014-10-23 14:07:48 +04:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
DecodeAudioData(const ArrayBuffer& aBuffer,
|
|
|
|
const Optional<OwningNonNull<DecodeSuccessCallback> >& aSuccessCallback,
|
2015-01-05 15:43:00 +03:00
|
|
|
const Optional<OwningNonNull<DecodeErrorCallback> >& aFailureCallback,
|
|
|
|
ErrorResult& aRv);
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2013-05-17 03:30:41 +04:00
|
|
|
// OfflineAudioContext methods
|
2014-11-19 13:38:39 +03:00
|
|
|
already_AddRefed<Promise> StartRendering(ErrorResult& aRv);
|
2013-05-17 03:30:41 +04:00
|
|
|
IMPL_EVENT_HANDLER(complete)
|
|
|
|
|
2013-05-17 03:31:08 +04:00
|
|
|
bool IsOffline() const { return mIsOffline; }
|
2013-02-05 03:07:25 +04:00
|
|
|
|
|
|
|
MediaStreamGraph* Graph() const;
|
|
|
|
MediaStream* DestinationStream() const;
|
2013-09-17 03:53:40 +04:00
|
|
|
|
|
|
|
// Nodes register here if they will produce sound even if they have silent
|
|
|
|
// or no input connections. The AudioContext will keep registered nodes
|
|
|
|
// alive until the context is collected. This takes care of "playing"
|
|
|
|
// references and "tail-time" references.
|
|
|
|
void RegisterActiveNode(AudioNode* aNode);
|
|
|
|
// Nodes unregister when they have finished producing sound for the
|
|
|
|
// foreseeable future.
|
|
|
|
// Do NOT call UnregisterActiveNode from an AudioNode destructor.
|
|
|
|
// If the destructor is called, then the Node has already been unregistered.
|
|
|
|
// The destructor may be called during hashtable enumeration, during which
|
|
|
|
// unregistering would not be safe.
|
|
|
|
void UnregisterActiveNode(AudioNode* aNode);
|
|
|
|
|
2013-04-11 16:47:57 +04:00
|
|
|
void UnregisterAudioBufferSourceNode(AudioBufferSourceNode* aNode);
|
|
|
|
void UnregisterPannerNode(PannerNode* aNode);
|
|
|
|
void UpdatePannerSource();
|
2013-02-05 03:07:25 +04:00
|
|
|
|
2013-06-10 21:32:28 +04:00
|
|
|
uint32_t MaxChannelCount() const;
|
|
|
|
|
2013-07-04 04:44:32 +04:00
|
|
|
void Mute() const;
|
|
|
|
void Unmute() const;
|
|
|
|
|
2014-05-09 22:33:17 +04:00
|
|
|
JSObject* GetGlobalJSObject() const;
|
|
|
|
|
2013-10-11 15:55:47 +04:00
|
|
|
AudioChannel MozAudioChannelType() const;
|
|
|
|
|
2014-08-03 17:46:17 +04:00
|
|
|
AudioChannel TestAudioChannelInAudioNodeStream();
|
|
|
|
|
2014-01-15 15:08:20 +04:00
|
|
|
void UpdateNodeCount(int32_t aDelta);
|
|
|
|
|
|
|
|
double DOMTimeToStreamTime(double aTime) const
|
|
|
|
{
|
|
|
|
return aTime - ExtraCurrentTime();
|
|
|
|
}
|
|
|
|
|
2014-08-13 03:05:09 +04:00
|
|
|
double StreamTimeToDOMTime(double aTime) const
|
|
|
|
{
|
|
|
|
return aTime + ExtraCurrentTime();
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:06:14 +04:00
|
|
|
IMPL_EVENT_HANDLER(mozinterruptbegin)
|
|
|
|
IMPL_EVENT_HANDLER(mozinterruptend)
|
|
|
|
|
2013-02-02 02:13:23 +04:00
|
|
|
private:
|
2014-01-15 15:08:20 +04:00
|
|
|
/**
|
|
|
|
* Returns the amount of extra time added to the current time of the
|
|
|
|
* AudioDestinationNode's MediaStream to get this AudioContext's currentTime.
|
|
|
|
* Must be subtracted from all DOM API parameter times that are on the same
|
|
|
|
* timeline as AudioContext's currentTime to get times we can pass to the
|
|
|
|
* MediaStreamGraph.
|
|
|
|
*/
|
|
|
|
double ExtraCurrentTime() const;
|
|
|
|
|
2013-02-02 02:13:23 +04:00
|
|
|
void RemoveFromDecodeQueue(WebAudioDecodeJob* aDecodeJob);
|
2013-08-02 20:07:17 +04:00
|
|
|
void ShutdownDecoder();
|
2013-02-02 02:13:23 +04:00
|
|
|
|
2014-01-04 22:15:41 +04:00
|
|
|
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsISupports* aData, bool aAnonymize) override;
|
2014-01-04 22:15:41 +04:00
|
|
|
|
2013-02-02 02:13:23 +04:00
|
|
|
friend struct ::mozilla::WebAudioDecodeJob;
|
|
|
|
|
2012-09-01 00:59:37 +04:00
|
|
|
private:
|
2013-05-24 21:09:29 +04:00
|
|
|
// Note that it's important for mSampleRate to be initialized before
|
|
|
|
// mDestination, as mDestination's constructor needs to access it!
|
|
|
|
const float mSampleRate;
|
2012-09-19 03:07:33 +04:00
|
|
|
nsRefPtr<AudioDestinationNode> mDestination;
|
2012-11-06 04:26:03 +04:00
|
|
|
nsRefPtr<AudioListener> mListener;
|
2013-07-16 13:00:36 +04:00
|
|
|
nsTArray<nsRefPtr<WebAudioDecodeJob> > mDecodeJobs;
|
2013-09-17 03:53:40 +04:00
|
|
|
// See RegisterActiveNode. These will keep the AudioContext alive while it
|
|
|
|
// is rendering and the window remains alive.
|
|
|
|
nsTHashtable<nsRefPtrHashKey<AudioNode> > mActiveNodes;
|
2013-09-24 05:47:00 +04:00
|
|
|
// Hashsets containing all the PannerNodes, to compute the doppler shift.
|
|
|
|
// These are weak pointers.
|
2013-04-25 07:32:41 +04:00
|
|
|
nsTHashtable<nsPtrHashKey<PannerNode> > mPannerNodes;
|
2013-06-10 21:32:28 +04:00
|
|
|
// Number of channels passed in the OfflineAudioContext ctor.
|
|
|
|
uint32_t mNumberOfChannels;
|
2014-01-15 15:08:20 +04:00
|
|
|
// Number of nodes that currently exist for this AudioContext
|
|
|
|
int32_t mNodeCount;
|
2013-05-17 03:30:41 +04:00
|
|
|
bool mIsOffline;
|
2013-09-16 09:14:45 +04:00
|
|
|
bool mIsStarted;
|
2013-09-18 05:10:30 +04:00
|
|
|
bool mIsShutDown;
|
2012-09-01 00:59:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2012-09-08 02:13:26 +04:00
|
|
|
}
|
2012-09-01 00:59:37 +04:00
|
|
|
|
2012-10-31 01:39:38 +04:00
|
|
|
#endif
|
|
|
|
|