2012-08-31 20:59:37 +00: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/. */
|
|
|
|
|
|
|
|
#include "AudioContext.h"
|
2013-09-10 07:03:37 +00:00
|
|
|
|
2013-03-17 07:55:15 +00:00
|
|
|
#include "nsPIDOMWindow.h"
|
2012-08-31 20:59:37 +00:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-07-25 03:01:49 +00:00
|
|
|
#include "mozilla/dom/AnalyserNode.h"
|
2013-05-16 23:30:41 +00:00
|
|
|
#include "mozilla/dom/AudioContextBinding.h"
|
2013-07-25 03:01:49 +00:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2013-05-16 23:30:41 +00:00
|
|
|
#include "mozilla/dom/OfflineAudioContextBinding.h"
|
2013-09-10 07:03:37 +00:00
|
|
|
#include "mozilla/dom/OwningNonNull.h"
|
2013-02-04 23:07:25 +00:00
|
|
|
#include "MediaStreamGraph.h"
|
2014-06-13 06:06:14 +00:00
|
|
|
#include "AudioChannelService.h"
|
2012-09-18 23:07:33 +00:00
|
|
|
#include "AudioDestinationNode.h"
|
|
|
|
#include "AudioBufferSourceNode.h"
|
2012-09-21 22:42:14 +00:00
|
|
|
#include "AudioBuffer.h"
|
2012-10-31 19:09:32 +00:00
|
|
|
#include "GainNode.h"
|
2013-07-25 03:01:49 +00:00
|
|
|
#include "MediaElementAudioSourceNode.h"
|
2013-07-24 11:29:39 +00:00
|
|
|
#include "MediaStreamAudioSourceNode.h"
|
2012-11-01 00:26:03 +00:00
|
|
|
#include "DelayNode.h"
|
2012-11-06 02:14:13 +00:00
|
|
|
#include "PannerNode.h"
|
2012-11-06 00:26:03 +00:00
|
|
|
#include "AudioListener.h"
|
2012-11-07 01:01:11 +00:00
|
|
|
#include "DynamicsCompressorNode.h"
|
2012-11-08 01:59:14 +00:00
|
|
|
#include "BiquadFilterNode.h"
|
2013-04-14 01:37:04 +00:00
|
|
|
#include "ScriptProcessorNode.h"
|
2013-05-05 15:49:37 +00:00
|
|
|
#include "ChannelMergerNode.h"
|
2013-05-05 15:49:13 +00:00
|
|
|
#include "ChannelSplitterNode.h"
|
2013-05-21 19:17:47 +00:00
|
|
|
#include "MediaStreamAudioDestinationNode.h"
|
2013-05-14 04:12:30 +00:00
|
|
|
#include "WaveShaperNode.h"
|
2013-06-19 22:24:26 +00:00
|
|
|
#include "PeriodicWave.h"
|
2013-06-10 20:07:55 +00:00
|
|
|
#include "ConvolverNode.h"
|
2013-08-19 18:53:00 +00:00
|
|
|
#include "OscillatorNode.h"
|
2013-02-01 22:13:23 +00:00
|
|
|
#include "nsNetUtil.h"
|
2013-12-09 19:54:49 +00:00
|
|
|
#include "AudioStream.h"
|
2012-08-31 20:59:37 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-09-07 22:13:26 +00:00
|
|
|
namespace dom {
|
2012-08-31 20:59:37 +00:00
|
|
|
|
2013-09-24 01:47:30 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AudioContext)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioContext)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDestination)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mListener)
|
|
|
|
if (!tmp->mIsStarted) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mActiveNodes)
|
|
|
|
}
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(DOMEventTargetHelper)
|
2013-09-24 01:47:30 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AudioContext,
|
|
|
|
DOMEventTargetHelper)
|
2013-09-24 01:47:30 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDestination)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mListener)
|
|
|
|
if (!tmp->mIsStarted) {
|
|
|
|
MOZ_ASSERT(tmp->mIsOffline,
|
|
|
|
"Online AudioContexts should always be started");
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mActiveNodes)
|
|
|
|
}
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2012-11-27 23:08:22 +00:00
|
|
|
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(AudioContext, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(AudioContext, DOMEventTargetHelper)
|
2013-04-25 04:28:39 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(AudioContext)
|
2014-04-01 06:13:50 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2012-08-31 20:59:37 +00:00
|
|
|
|
2014-02-03 04:40:24 +00:00
|
|
|
static float GetSampleRateForAudioContext(bool aIsOffline, float aSampleRate)
|
2013-12-09 19:54:49 +00:00
|
|
|
{
|
|
|
|
if (aIsOffline) {
|
|
|
|
return aSampleRate;
|
|
|
|
} else {
|
|
|
|
AudioStream::InitPreferredSampleRate();
|
|
|
|
return static_cast<float>(AudioStream::PreferredSampleRate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 23:30:42 +00:00
|
|
|
AudioContext::AudioContext(nsPIDOMWindow* aWindow,
|
|
|
|
bool aIsOffline,
|
2014-04-18 09:23:36 +00:00
|
|
|
AudioChannel aChannel,
|
2013-05-16 23:30:42 +00:00
|
|
|
uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength,
|
|
|
|
float aSampleRate)
|
2014-04-01 06:13:50 +00:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
2014-01-07 02:53:23 +00:00
|
|
|
, mSampleRate(GetSampleRateForAudioContext(aIsOffline, aSampleRate))
|
2013-06-10 17:32:28 +00:00
|
|
|
, mNumberOfChannels(aNumberOfChannels)
|
2014-01-15 11:08:20 +00:00
|
|
|
, mNodeCount(0)
|
2013-05-16 23:30:41 +00:00
|
|
|
, mIsOffline(aIsOffline)
|
2013-09-16 05:14:45 +00:00
|
|
|
, mIsStarted(!aIsOffline)
|
2013-09-18 01:10:30 +00:00
|
|
|
, mIsShutDown(false)
|
2012-08-31 20:59:37 +00:00
|
|
|
{
|
2013-09-12 04:16:53 +00:00
|
|
|
aWindow->AddAudioContext(this);
|
2013-10-03 22:40:20 +00:00
|
|
|
|
|
|
|
// Note: AudioDestinationNode needs an AudioContext that must already be
|
|
|
|
// bound to the window.
|
2014-04-18 09:23:36 +00:00
|
|
|
mDestination = new AudioDestinationNode(this, aIsOffline, aChannel,
|
|
|
|
aNumberOfChannels, aLength, aSampleRate);
|
2014-07-29 15:30:40 +00:00
|
|
|
// We skip calling SetIsOnlyNodeForContext and the creation of the
|
|
|
|
// audioChannelAgent during mDestination's constructor, because we can only
|
|
|
|
// call them after mDestination has been set up.
|
|
|
|
mDestination->CreateAudioChannelAgent();
|
2014-01-15 11:08:20 +00:00
|
|
|
mDestination->SetIsOnlyNodeForContext(true);
|
2012-08-31 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AudioContext::~AudioContext()
|
|
|
|
{
|
2013-09-12 04:16:53 +00:00
|
|
|
nsPIDOMWindow* window = GetOwner();
|
|
|
|
if (window) {
|
|
|
|
window->RemoveAudioContext(this);
|
|
|
|
}
|
2014-01-04 18:15:41 +00:00
|
|
|
|
|
|
|
UnregisterWeakMemoryReporter(this);
|
2012-08-31 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-08 22:27:18 +00:00
|
|
|
AudioContext::WrapObject(JSContext* aCx)
|
2012-08-31 20:59:37 +00:00
|
|
|
{
|
2013-05-16 23:30:41 +00:00
|
|
|
if (mIsOffline) {
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return OfflineAudioContextBinding::Wrap(aCx, this);
|
2013-05-16 23:30:41 +00:00
|
|
|
} else {
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return AudioContextBinding::Wrap(aCx, this);
|
2013-05-16 23:30:41 +00:00
|
|
|
}
|
2012-08-31 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<AudioContext>
|
2013-08-23 05:17:08 +00:00
|
|
|
AudioContext::Constructor(const GlobalObject& aGlobal,
|
|
|
|
ErrorResult& aRv)
|
2012-08-31 20:59:37 +00:00
|
|
|
{
|
2013-08-23 05:17:08 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2012-08-31 20:59:37 +00:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-13 06:06:14 +00:00
|
|
|
nsRefPtr<AudioContext> object =
|
|
|
|
new AudioContext(window, false,
|
|
|
|
AudioChannelService::GetDefaultAudioChannel());
|
2014-01-04 18:15:41 +00:00
|
|
|
|
|
|
|
RegisterWeakMemoryReporter(object);
|
|
|
|
|
2013-05-16 23:30:41 +00:00
|
|
|
return object.forget();
|
|
|
|
}
|
|
|
|
|
2014-04-18 09:23:36 +00:00
|
|
|
/* static */ already_AddRefed<AudioContext>
|
|
|
|
AudioContext::Constructor(const GlobalObject& aGlobal,
|
|
|
|
AudioChannel aChannel,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<AudioContext> object = new AudioContext(window, false, aChannel);
|
|
|
|
|
|
|
|
RegisterWeakMemoryReporter(object);
|
|
|
|
|
|
|
|
return object.forget();
|
|
|
|
}
|
|
|
|
|
2013-05-16 23:30:41 +00:00
|
|
|
/* static */ already_AddRefed<AudioContext>
|
|
|
|
AudioContext::Constructor(const GlobalObject& aGlobal,
|
|
|
|
uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength,
|
|
|
|
float aSampleRate,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2013-08-23 05:17:08 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-05-16 23:30:41 +00:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-29 23:35:30 +00:00
|
|
|
if (aNumberOfChannels == 0 ||
|
|
|
|
aNumberOfChannels > WebAudioUtils::MaxChannelCount ||
|
|
|
|
aLength == 0 ||
|
2014-05-15 20:44:18 +00:00
|
|
|
aSampleRate < WebAudioUtils::MinSampleRate ||
|
|
|
|
aSampleRate > WebAudioUtils::MaxSampleRate) {
|
2013-05-24 17:11:32 +00:00
|
|
|
// The DOM binding protects us against infinity and NaN
|
2013-05-29 23:35:30 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
2013-05-16 23:30:41 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-16 23:30:42 +00:00
|
|
|
nsRefPtr<AudioContext> object = new AudioContext(window,
|
|
|
|
true,
|
2014-04-18 09:23:36 +00:00
|
|
|
AudioChannel::Normal,
|
2013-05-16 23:30:42 +00:00
|
|
|
aNumberOfChannels,
|
|
|
|
aLength,
|
|
|
|
aSampleRate);
|
2014-01-04 18:15:41 +00:00
|
|
|
|
|
|
|
RegisterWeakMemoryReporter(object);
|
|
|
|
|
2013-03-17 07:55:15 +00:00
|
|
|
return object.forget();
|
2012-08-31 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
2012-09-18 23:07:33 +00:00
|
|
|
already_AddRefed<AudioBufferSourceNode>
|
|
|
|
AudioContext::CreateBufferSource()
|
|
|
|
{
|
|
|
|
nsRefPtr<AudioBufferSourceNode> bufferNode =
|
|
|
|
new AudioBufferSourceNode(this);
|
|
|
|
return bufferNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-09-21 22:42:14 +00:00
|
|
|
already_AddRefed<AudioBuffer>
|
|
|
|
AudioContext::CreateBuffer(JSContext* aJSContext, uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength, float aSampleRate,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2014-05-15 21:23:27 +00:00
|
|
|
if (!aNumberOfChannels) {
|
2013-12-13 16:58:00 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
2013-03-08 17:29:00 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-15 21:23:27 +00:00
|
|
|
return AudioBuffer::Create(this, aNumberOfChannels, aLength,
|
|
|
|
aSampleRate, aJSContext, aRv);
|
2012-09-21 22:42:14 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 01:37:04 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool IsValidBufferSize(uint32_t aBufferSize) {
|
|
|
|
switch (aBufferSize) {
|
|
|
|
case 0: // let the implementation choose the buffer size
|
|
|
|
case 256:
|
|
|
|
case 512:
|
|
|
|
case 1024:
|
|
|
|
case 2048:
|
|
|
|
case 4096:
|
|
|
|
case 8192:
|
|
|
|
case 16384:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:17:47 +00:00
|
|
|
already_AddRefed<MediaStreamAudioDestinationNode>
|
2013-07-18 09:57:38 +00:00
|
|
|
AudioContext::CreateMediaStreamDestination(ErrorResult& aRv)
|
2013-05-21 19:17:47 +00:00
|
|
|
{
|
2013-07-18 09:57:38 +00:00
|
|
|
if (mIsOffline) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-21 19:17:47 +00:00
|
|
|
nsRefPtr<MediaStreamAudioDestinationNode> node =
|
|
|
|
new MediaStreamAudioDestinationNode(this);
|
|
|
|
return node.forget();
|
|
|
|
}
|
|
|
|
|
2013-04-14 01:37:04 +00:00
|
|
|
already_AddRefed<ScriptProcessorNode>
|
|
|
|
AudioContext::CreateScriptProcessor(uint32_t aBufferSize,
|
|
|
|
uint32_t aNumberOfInputChannels,
|
|
|
|
uint32_t aNumberOfOutputChannels,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2013-05-23 12:26:21 +00:00
|
|
|
if ((aNumberOfInputChannels == 0 && aNumberOfOutputChannels == 0) ||
|
2013-05-29 11:36:37 +00:00
|
|
|
aNumberOfInputChannels > WebAudioUtils::MaxChannelCount ||
|
|
|
|
aNumberOfOutputChannels > WebAudioUtils::MaxChannelCount ||
|
2013-04-14 01:37:04 +00:00
|
|
|
!IsValidBufferSize(aBufferSize)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ScriptProcessorNode> scriptProcessor =
|
|
|
|
new ScriptProcessorNode(this, aBufferSize, aNumberOfInputChannels,
|
|
|
|
aNumberOfOutputChannels);
|
|
|
|
return scriptProcessor.forget();
|
|
|
|
}
|
|
|
|
|
2013-04-01 03:41:14 +00:00
|
|
|
already_AddRefed<AnalyserNode>
|
|
|
|
AudioContext::CreateAnalyser()
|
|
|
|
{
|
|
|
|
nsRefPtr<AnalyserNode> analyserNode = new AnalyserNode(this);
|
|
|
|
return analyserNode.forget();
|
|
|
|
}
|
|
|
|
|
2013-07-25 03:01:49 +00:00
|
|
|
already_AddRefed<MediaElementAudioSourceNode>
|
|
|
|
AudioContext::CreateMediaElementSource(HTMLMediaElement& aMediaElement,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mIsOffline) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsRefPtr<DOMMediaStream> stream = aMediaElement.MozCaptureStream(aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsRefPtr<MediaElementAudioSourceNode> mediaElementAudioSourceNode =
|
|
|
|
new MediaElementAudioSourceNode(this, stream);
|
|
|
|
return mediaElementAudioSourceNode.forget();
|
|
|
|
}
|
|
|
|
|
2013-07-24 11:29:39 +00:00
|
|
|
already_AddRefed<MediaStreamAudioSourceNode>
|
2013-07-25 02:07:34 +00:00
|
|
|
AudioContext::CreateMediaStreamSource(DOMMediaStream& aMediaStream,
|
2013-07-24 11:29:39 +00:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mIsOffline) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-07-25 03:01:49 +00:00
|
|
|
nsRefPtr<MediaStreamAudioSourceNode> mediaStreamAudioSourceNode =
|
|
|
|
new MediaStreamAudioSourceNode(this, &aMediaStream);
|
2013-07-24 11:29:39 +00:00
|
|
|
return mediaStreamAudioSourceNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-10-31 19:09:32 +00:00
|
|
|
already_AddRefed<GainNode>
|
|
|
|
AudioContext::CreateGain()
|
|
|
|
{
|
|
|
|
nsRefPtr<GainNode> gainNode = new GainNode(this);
|
|
|
|
return gainNode.forget();
|
|
|
|
}
|
|
|
|
|
2013-05-14 04:12:30 +00:00
|
|
|
already_AddRefed<WaveShaperNode>
|
|
|
|
AudioContext::CreateWaveShaper()
|
|
|
|
{
|
|
|
|
nsRefPtr<WaveShaperNode> waveShaperNode = new WaveShaperNode(this);
|
|
|
|
return waveShaperNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-01 00:26:03 +00:00
|
|
|
already_AddRefed<DelayNode>
|
2012-11-19 20:52:29 +00:00
|
|
|
AudioContext::CreateDelay(double aMaxDelayTime, ErrorResult& aRv)
|
2012-11-01 00:26:03 +00:00
|
|
|
{
|
2013-03-25 19:44:14 +00:00
|
|
|
if (aMaxDelayTime > 0. && aMaxDelayTime < 180.) {
|
2012-11-16 01:48:04 +00:00
|
|
|
nsRefPtr<DelayNode> delayNode = new DelayNode(this, aMaxDelayTime);
|
|
|
|
return delayNode.forget();
|
2012-11-08 18:17:22 +00:00
|
|
|
}
|
2012-11-16 01:48:04 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
2012-11-01 00:26:03 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 02:14:13 +00:00
|
|
|
already_AddRefed<PannerNode>
|
|
|
|
AudioContext::CreatePanner()
|
|
|
|
{
|
|
|
|
nsRefPtr<PannerNode> pannerNode = new PannerNode(this);
|
2013-04-25 03:32:41 +00:00
|
|
|
mPannerNodes.PutEntry(pannerNode);
|
2012-11-06 02:14:13 +00:00
|
|
|
return pannerNode.forget();
|
|
|
|
}
|
|
|
|
|
2013-06-10 20:07:55 +00:00
|
|
|
already_AddRefed<ConvolverNode>
|
|
|
|
AudioContext::CreateConvolver()
|
|
|
|
{
|
|
|
|
nsRefPtr<ConvolverNode> convolverNode = new ConvolverNode(this);
|
|
|
|
return convolverNode.forget();
|
|
|
|
}
|
|
|
|
|
2013-05-05 15:49:13 +00:00
|
|
|
already_AddRefed<ChannelSplitterNode>
|
|
|
|
AudioContext::CreateChannelSplitter(uint32_t aNumberOfOutputs, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aNumberOfOutputs == 0 ||
|
2013-05-29 11:36:37 +00:00
|
|
|
aNumberOfOutputs > WebAudioUtils::MaxChannelCount) {
|
2013-05-05 15:49:13 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ChannelSplitterNode> splitterNode =
|
|
|
|
new ChannelSplitterNode(this, aNumberOfOutputs);
|
|
|
|
return splitterNode.forget();
|
|
|
|
}
|
|
|
|
|
2013-05-05 15:49:37 +00:00
|
|
|
already_AddRefed<ChannelMergerNode>
|
|
|
|
AudioContext::CreateChannelMerger(uint32_t aNumberOfInputs, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aNumberOfInputs == 0 ||
|
2013-05-29 11:36:37 +00:00
|
|
|
aNumberOfInputs > WebAudioUtils::MaxChannelCount) {
|
2013-05-05 15:49:37 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ChannelMergerNode> mergerNode =
|
|
|
|
new ChannelMergerNode(this, aNumberOfInputs);
|
|
|
|
return mergerNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-07 01:01:11 +00:00
|
|
|
already_AddRefed<DynamicsCompressorNode>
|
|
|
|
AudioContext::CreateDynamicsCompressor()
|
|
|
|
{
|
|
|
|
nsRefPtr<DynamicsCompressorNode> compressorNode =
|
|
|
|
new DynamicsCompressorNode(this);
|
|
|
|
return compressorNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-08 01:59:14 +00:00
|
|
|
already_AddRefed<BiquadFilterNode>
|
|
|
|
AudioContext::CreateBiquadFilter()
|
|
|
|
{
|
|
|
|
nsRefPtr<BiquadFilterNode> filterNode =
|
|
|
|
new BiquadFilterNode(this);
|
|
|
|
return filterNode.forget();
|
|
|
|
}
|
|
|
|
|
2013-08-19 18:53:00 +00:00
|
|
|
already_AddRefed<OscillatorNode>
|
|
|
|
AudioContext::CreateOscillator()
|
|
|
|
{
|
|
|
|
nsRefPtr<OscillatorNode> oscillatorNode =
|
|
|
|
new OscillatorNode(this);
|
|
|
|
return oscillatorNode.forget();
|
|
|
|
}
|
|
|
|
|
2013-06-19 22:24:26 +00:00
|
|
|
already_AddRefed<PeriodicWave>
|
|
|
|
AudioContext::CreatePeriodicWave(const Float32Array& aRealData,
|
|
|
|
const Float32Array& aImagData,
|
|
|
|
ErrorResult& aRv)
|
2013-05-28 11:19:07 +00:00
|
|
|
{
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 21:32:41 +00:00
|
|
|
aRealData.ComputeLengthAndData();
|
|
|
|
aImagData.ComputeLengthAndData();
|
|
|
|
|
2013-05-28 11:19:07 +00:00
|
|
|
if (aRealData.Length() != aImagData.Length() ||
|
|
|
|
aRealData.Length() == 0 ||
|
|
|
|
aRealData.Length() > 4096) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-06-19 22:24:26 +00:00
|
|
|
nsRefPtr<PeriodicWave> periodicWave =
|
2013-08-28 22:39:26 +00:00
|
|
|
new PeriodicWave(this, aRealData.Data(), aImagData.Data(),
|
|
|
|
aImagData.Length(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-06-19 22:24:26 +00:00
|
|
|
return periodicWave.forget();
|
2013-05-28 11:19:07 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 00:26:03 +00:00
|
|
|
AudioListener*
|
|
|
|
AudioContext::Listener()
|
|
|
|
{
|
|
|
|
if (!mListener) {
|
|
|
|
mListener = new AudioListener(this);
|
|
|
|
}
|
|
|
|
return mListener;
|
|
|
|
}
|
|
|
|
|
2013-02-01 22:13:23 +00:00
|
|
|
void
|
|
|
|
AudioContext::DecodeAudioData(const ArrayBuffer& aBuffer,
|
|
|
|
DecodeSuccessCallback& aSuccessCallback,
|
|
|
|
const Optional<OwningNonNull<DecodeErrorCallback> >& aFailureCallback)
|
|
|
|
{
|
2014-05-13 12:09:44 +00:00
|
|
|
AutoJSAPI jsapi;
|
2014-06-19 07:21:14 +00:00
|
|
|
jsapi.Init();
|
2014-05-13 12:09:44 +00:00
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JSAutoCompartment ac(cx, aBuffer.Obj());
|
|
|
|
|
Bug 999651, bug 995679, bug 1009952, bug 1011007, bug 991981. r=sfink, r=shu, r=jandem, r=jdm, r=luke, r=bbouvier, r=nmatsakis, r=bz, r=ehsan, r=jgilbert, r=smaug, r=sicking, r=terrence, r=bholley, r=bent, r=efaust, r=jorendorff
2014-05-27 21:32:41 +00:00
|
|
|
aBuffer.ComputeLengthAndData();
|
|
|
|
|
2014-05-13 12:09:44 +00:00
|
|
|
// Neuter the array buffer
|
|
|
|
size_t length = aBuffer.Length();
|
|
|
|
JS::RootedObject obj(cx, aBuffer.Obj());
|
|
|
|
|
|
|
|
uint8_t* data = static_cast<uint8_t*>(JS_StealArrayBufferContents(cx, obj));
|
|
|
|
|
2013-02-01 22:13:23 +00:00
|
|
|
// Sniff the content of the media.
|
|
|
|
// Failed type sniffing will be handled by AsyncDecodeMedia.
|
|
|
|
nsAutoCString contentType;
|
2014-05-13 12:09:44 +00:00
|
|
|
NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, nullptr, data, length, contentType);
|
2013-02-01 22:13:23 +00:00
|
|
|
|
2014-03-18 00:23:03 +00:00
|
|
|
nsRefPtr<DecodeErrorCallback> failureCallback;
|
2013-02-01 22:13:23 +00:00
|
|
|
if (aFailureCallback.WasPassed()) {
|
2013-06-19 18:48:43 +00:00
|
|
|
failureCallback = &aFailureCallback.Value();
|
2013-02-01 22:13:23 +00:00
|
|
|
}
|
2013-07-16 09:00:36 +00:00
|
|
|
nsRefPtr<WebAudioDecodeJob> job(
|
2014-05-13 12:09:44 +00:00
|
|
|
new WebAudioDecodeJob(contentType, this,
|
2013-02-01 22:13:23 +00:00
|
|
|
&aSuccessCallback, failureCallback));
|
2014-05-13 12:09:44 +00:00
|
|
|
mDecoder.AsyncDecodeMedia(contentType.get(), data, length, *job);
|
2013-02-01 22:13:23 +00:00
|
|
|
// Transfer the ownership to mDecodeJobs
|
2014-03-15 19:00:17 +00:00
|
|
|
mDecodeJobs.AppendElement(job);
|
2013-02-01 22:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::RemoveFromDecodeQueue(WebAudioDecodeJob* aDecodeJob)
|
|
|
|
{
|
|
|
|
mDecodeJobs.RemoveElement(aDecodeJob);
|
|
|
|
}
|
|
|
|
|
2013-09-16 23:53:40 +00:00
|
|
|
void
|
|
|
|
AudioContext::RegisterActiveNode(AudioNode* aNode)
|
|
|
|
{
|
2013-09-18 01:10:30 +00:00
|
|
|
if (!mIsShutDown) {
|
|
|
|
mActiveNodes.PutEntry(aNode);
|
|
|
|
}
|
2013-09-16 23:53:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::UnregisterActiveNode(AudioNode* aNode)
|
|
|
|
{
|
|
|
|
mActiveNodes.RemoveEntry(aNode);
|
|
|
|
}
|
|
|
|
|
2013-04-11 12:47:57 +00:00
|
|
|
void
|
|
|
|
AudioContext::UnregisterAudioBufferSourceNode(AudioBufferSourceNode* aNode)
|
|
|
|
{
|
2013-04-30 23:20:55 +00:00
|
|
|
UpdatePannerSource();
|
2013-04-11 12:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::UnregisterPannerNode(PannerNode* aNode)
|
|
|
|
{
|
2013-04-25 03:32:41 +00:00
|
|
|
mPannerNodes.RemoveEntry(aNode);
|
2013-07-16 12:45:41 +00:00
|
|
|
if (mListener) {
|
|
|
|
mListener->UnregisterPannerNode(aNode);
|
|
|
|
}
|
2013-04-11 12:47:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-25 03:32:41 +00:00
|
|
|
static PLDHashOperator
|
|
|
|
FindConnectedSourcesOn(nsPtrHashKey<PannerNode>* aEntry, void* aData)
|
|
|
|
{
|
|
|
|
aEntry->GetKey()->FindConnectedSources();
|
|
|
|
return PL_DHASH_NEXT;
|
2013-04-23 01:23:54 +00:00
|
|
|
}
|
|
|
|
|
2013-04-11 12:47:57 +00:00
|
|
|
void
|
|
|
|
AudioContext::UpdatePannerSource()
|
|
|
|
{
|
2013-04-25 03:32:41 +00:00
|
|
|
mPannerNodes.EnumerateEntries(FindConnectedSourcesOn, nullptr);
|
2013-04-11 12:47:57 +00:00
|
|
|
}
|
|
|
|
|
2013-06-10 17:32:28 +00:00
|
|
|
uint32_t
|
|
|
|
AudioContext::MaxChannelCount() const
|
|
|
|
{
|
|
|
|
return mIsOffline ? mNumberOfChannels : AudioStream::MaxNumberOfChannels();
|
|
|
|
}
|
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
MediaStreamGraph*
|
|
|
|
AudioContext::Graph() const
|
|
|
|
{
|
|
|
|
return Destination()->Stream()->Graph();
|
2012-08-31 20:59:37 +00:00
|
|
|
}
|
2013-02-04 23:07:25 +00:00
|
|
|
|
|
|
|
MediaStream*
|
|
|
|
AudioContext::DestinationStream() const
|
|
|
|
{
|
2013-09-17 13:23:52 +00:00
|
|
|
if (Destination()) {
|
|
|
|
return Destination()->Stream();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2012-09-07 22:13:26 +00:00
|
|
|
}
|
2012-08-31 20:59:37 +00:00
|
|
|
|
2013-03-15 01:01:02 +00:00
|
|
|
double
|
|
|
|
AudioContext::CurrentTime() const
|
|
|
|
{
|
2014-06-12 04:44:58 +00:00
|
|
|
MediaStream* stream = Destination()->Stream();
|
|
|
|
return stream->StreamTimeToSeconds(stream->GetCurrentTime()) +
|
2014-01-15 11:08:20 +00:00
|
|
|
ExtraCurrentTime();
|
2013-03-15 01:01:02 +00:00
|
|
|
}
|
|
|
|
|
2013-04-22 15:45:34 +00:00
|
|
|
void
|
|
|
|
AudioContext::Shutdown()
|
|
|
|
{
|
2013-09-18 01:10:30 +00:00
|
|
|
mIsShutDown = true;
|
|
|
|
|
2013-12-17 15:15:07 +00:00
|
|
|
// We mute rather than suspending, because the delay between the ::Shutdown
|
|
|
|
// call and the CC would make us overbuffer in the MediaStreamGraph.
|
|
|
|
// See bug 936784 for details.
|
2013-12-18 15:07:37 +00:00
|
|
|
if (!mIsOffline) {
|
|
|
|
Mute();
|
|
|
|
}
|
2013-08-02 16:07:17 +00:00
|
|
|
|
2014-06-18 05:24:05 +00:00
|
|
|
mDecoder.Shutdown();
|
|
|
|
|
2013-09-16 23:53:40 +00:00
|
|
|
// Release references to active nodes.
|
|
|
|
// Active AudioNodes don't unregister in destructors, at which point the
|
|
|
|
// Node is already unregistered.
|
|
|
|
mActiveNodes.Clear();
|
|
|
|
|
2013-05-16 23:31:08 +00:00
|
|
|
// For offline contexts, we can destroy the MediaStreamGraph at this point.
|
2013-09-19 00:04:13 +00:00
|
|
|
if (mIsOffline && mDestination) {
|
2013-09-10 05:10:53 +00:00
|
|
|
mDestination->OfflineShutdown();
|
2013-05-16 23:31:08 +00:00
|
|
|
}
|
2013-04-22 15:45:34 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 02:59:33 +00:00
|
|
|
void
|
|
|
|
AudioContext::Suspend()
|
|
|
|
{
|
2013-04-15 01:52:55 +00:00
|
|
|
MediaStream* ds = DestinationStream();
|
|
|
|
if (ds) {
|
|
|
|
ds->ChangeExplicitBlockerCount(1);
|
|
|
|
}
|
2013-03-22 02:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::Resume()
|
|
|
|
{
|
2013-04-15 01:52:55 +00:00
|
|
|
MediaStream* ds = DestinationStream();
|
|
|
|
if (ds) {
|
|
|
|
ds->ChangeExplicitBlockerCount(-1);
|
|
|
|
}
|
2013-03-22 02:59:33 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 11:08:20 +00:00
|
|
|
void
|
|
|
|
AudioContext::UpdateNodeCount(int32_t aDelta)
|
|
|
|
{
|
|
|
|
bool firstNode = mNodeCount == 0;
|
|
|
|
mNodeCount += aDelta;
|
|
|
|
MOZ_ASSERT(mNodeCount >= 0);
|
|
|
|
// mDestinationNode may be null when we're destroying nodes unlinked by CC
|
|
|
|
if (!firstNode && mDestination) {
|
|
|
|
mDestination->SetIsOnlyNodeForContext(mNodeCount == 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 18:33:17 +00:00
|
|
|
JSObject*
|
|
|
|
AudioContext::GetGlobalJSObject() const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> parentObject = do_QueryInterface(GetParentObject());
|
|
|
|
if (!parentObject) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This can also return null.
|
|
|
|
return parentObject->GetGlobalJSObject();
|
|
|
|
}
|
|
|
|
|
2013-05-16 23:30:42 +00:00
|
|
|
void
|
2013-09-16 05:14:45 +00:00
|
|
|
AudioContext::StartRendering(ErrorResult& aRv)
|
2013-05-16 23:30:42 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIsOffline, "This should only be called on OfflineAudioContext");
|
2013-09-16 05:14:45 +00:00
|
|
|
if (mIsStarted) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2013-05-16 23:30:42 +00:00
|
|
|
|
2013-09-16 05:14:45 +00:00
|
|
|
mIsStarted = true;
|
2013-05-16 23:30:42 +00:00
|
|
|
mDestination->StartRendering();
|
|
|
|
}
|
|
|
|
|
2013-07-04 00:44:32 +00:00
|
|
|
void
|
|
|
|
AudioContext::Mute() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsOffline);
|
2014-01-07 10:17:28 +00:00
|
|
|
if (mDestination) {
|
|
|
|
mDestination->Mute();
|
|
|
|
}
|
2013-07-04 00:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::Unmute() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsOffline);
|
2014-01-07 10:17:28 +00:00
|
|
|
if (mDestination) {
|
|
|
|
mDestination->Unmute();
|
|
|
|
}
|
2013-07-04 00:44:32 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 11:55:47 +00:00
|
|
|
AudioChannel
|
|
|
|
AudioContext::MozAudioChannelType() const
|
|
|
|
{
|
|
|
|
return mDestination->MozAudioChannelType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::SetMozAudioChannelType(AudioChannel aValue, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mDestination->SetMozAudioChannelType(aValue, aRv);
|
|
|
|
}
|
|
|
|
|
2014-01-04 18:15:41 +00:00
|
|
|
size_t
|
|
|
|
AudioContext::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
// AudioNodes are tracked separately because we do not want the AudioContext
|
|
|
|
// to track all of the AudioNodes it creates, so we wouldn't be able to
|
|
|
|
// traverse them from here.
|
|
|
|
|
|
|
|
size_t amount = aMallocSizeOf(this);
|
|
|
|
if (mListener) {
|
|
|
|
amount += mListener->SizeOfIncludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
amount += mDecoder.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mDecodeJobs.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
for (uint32_t i = 0; i < mDecodeJobs.Length(); ++i) {
|
2014-05-01 17:37:54 +00:00
|
|
|
amount += mDecodeJobs[i]->SizeOfIncludingThis(aMallocSizeOf);
|
2014-01-04 18:15:41 +00:00
|
|
|
}
|
|
|
|
amount += mActiveNodes.SizeOfExcludingThis(nullptr, aMallocSizeOf);
|
|
|
|
amount += mPannerNodes.SizeOfExcludingThis(nullptr, aMallocSizeOf);
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioContext::CollectReports(nsIHandleReportCallback* aHandleReport,
|
2014-05-21 06:06:54 +00:00
|
|
|
nsISupports* aData, bool aAnonymize)
|
2014-01-04 18:15:41 +00:00
|
|
|
{
|
|
|
|
int64_t amount = SizeOfIncludingThis(MallocSizeOf);
|
|
|
|
return MOZ_COLLECT_REPORT("explicit/webaudio/audiocontext", KIND_HEAP, UNITS_BYTES,
|
|
|
|
amount, "Memory used by AudioContext objects (Web Audio).");
|
|
|
|
}
|
|
|
|
|
2014-01-15 11:08:20 +00:00
|
|
|
double
|
|
|
|
AudioContext::ExtraCurrentTime() const
|
|
|
|
{
|
|
|
|
return mDestination->ExtraCurrentTime();
|
|
|
|
}
|
|
|
|
|
2013-02-04 23:07:25 +00:00
|
|
|
}
|
|
|
|
}
|