2012-08-31 13:59:37 -07: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"
|
|
|
|
#include "nsContentUtils.h"
|
2013-03-17 00:55:15 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
2012-08-31 13:59:37 -07:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2013-02-04 15:07:25 -08:00
|
|
|
#include "MediaStreamGraph.h"
|
2013-03-31 20:41:14 -07:00
|
|
|
#include "mozilla/dom/AnalyserNode.h"
|
2012-09-18 16:07:33 -07:00
|
|
|
#include "AudioDestinationNode.h"
|
|
|
|
#include "AudioBufferSourceNode.h"
|
2012-09-21 15:42:14 -07:00
|
|
|
#include "AudioBuffer.h"
|
2012-10-31 12:09:32 -07:00
|
|
|
#include "GainNode.h"
|
2012-10-31 17:26:03 -07:00
|
|
|
#include "DelayNode.h"
|
2012-11-05 18:14:13 -08:00
|
|
|
#include "PannerNode.h"
|
2012-11-05 16:26:03 -08:00
|
|
|
#include "AudioListener.h"
|
2012-11-06 17:01:11 -08:00
|
|
|
#include "DynamicsCompressorNode.h"
|
2012-11-07 17:59:14 -08:00
|
|
|
#include "BiquadFilterNode.h"
|
2013-04-13 18:37:04 -07:00
|
|
|
#include "ScriptProcessorNode.h"
|
2013-02-01 14:13:23 -08:00
|
|
|
#include "nsNetUtil.h"
|
2012-08-31 13:59:37 -07:00
|
|
|
|
2013-04-13 18:37:04 -07:00
|
|
|
// Note that this number is an arbitrary large value to protect against OOM
|
|
|
|
// attacks.
|
|
|
|
const unsigned MAX_SCRIPT_PROCESSOR_CHANNELS = 10000;
|
|
|
|
|
2012-08-31 13:59:37 -07:00
|
|
|
namespace mozilla {
|
2012-09-07 15:13:26 -07:00
|
|
|
namespace dom {
|
2012-08-31 13:59:37 -07:00
|
|
|
|
2012-11-27 15:08:22 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_3(AudioContext,
|
|
|
|
mWindow, mDestination, mListener)
|
|
|
|
|
2012-10-09 11:50:26 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AudioContext, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AudioContext, Release)
|
2012-08-31 13:59:37 -07:00
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
static uint8_t gWebAudioOutputKey;
|
|
|
|
|
2013-04-12 08:28:33 -07:00
|
|
|
AudioContext::AudioContext(nsPIDOMWindow* aWindow)
|
2012-08-31 13:59:37 -07:00
|
|
|
: mWindow(aWindow)
|
2013-02-04 15:07:25 -08:00
|
|
|
, mDestination(new AudioDestinationNode(this, MediaStreamGraph::GetInstance()))
|
2012-08-31 13:59:37 -07:00
|
|
|
{
|
2013-02-04 15:07:25 -08:00
|
|
|
// Actually play audio
|
|
|
|
mDestination->Stream()->AddAudioOutput(&gWebAudioOutputKey);
|
2012-08-31 13:59:37 -07:00
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioContext::~AudioContext()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-03-11 16:03:47 -07:00
|
|
|
AudioContext::WrapObject(JSContext* aCx, JSObject* aScope)
|
2012-08-31 13:59:37 -07:00
|
|
|
{
|
2013-03-11 16:03:47 -07:00
|
|
|
return AudioContextBinding::Wrap(aCx, aScope, this);
|
2012-08-31 13:59:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<AudioContext>
|
2012-12-03 08:07:49 -08:00
|
|
|
AudioContext::Constructor(const GlobalObject& aGlobal, ErrorResult& aRv)
|
2012-08-31 13:59:37 -07:00
|
|
|
{
|
2012-12-03 08:07:49 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.Get());
|
2012-08-31 13:59:37 -07:00
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-03-17 00:55:15 -07:00
|
|
|
nsRefPtr<AudioContext> object = new AudioContext(window);
|
2012-11-23 15:33:41 -08:00
|
|
|
window->AddAudioContext(object);
|
2013-03-17 00:55:15 -07:00
|
|
|
return object.forget();
|
2012-08-31 13:59:37 -07:00
|
|
|
}
|
|
|
|
|
2012-09-18 16:07:33 -07:00
|
|
|
already_AddRefed<AudioBufferSourceNode>
|
|
|
|
AudioContext::CreateBufferSource()
|
|
|
|
{
|
|
|
|
nsRefPtr<AudioBufferSourceNode> bufferNode =
|
|
|
|
new AudioBufferSourceNode(this);
|
2013-04-11 05:47:57 -07:00
|
|
|
mAudioBufferSourceNodes.AppendElement(bufferNode);
|
2012-09-18 16:07:33 -07:00
|
|
|
return bufferNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-09-21 15:42:14 -07:00
|
|
|
already_AddRefed<AudioBuffer>
|
|
|
|
AudioContext::CreateBuffer(JSContext* aJSContext, uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength, float aSampleRate,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2013-03-08 09:29:00 -08:00
|
|
|
if (aSampleRate < 8000 || aSampleRate > 96000) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
if (aLength > INT32_MAX) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<AudioBuffer> buffer =
|
|
|
|
new AudioBuffer(this, int32_t(aLength), aSampleRate);
|
2012-09-21 15:42:14 -07:00
|
|
|
if (!buffer->InitializeBuffers(aNumberOfChannels, aJSContext)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
|
2012-09-21 15:42:14 -07:00
|
|
|
return buffer.forget();
|
|
|
|
}
|
|
|
|
|
2013-04-13 18:37:04 -07: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<ScriptProcessorNode>
|
|
|
|
AudioContext::CreateScriptProcessor(uint32_t aBufferSize,
|
|
|
|
uint32_t aNumberOfInputChannels,
|
|
|
|
uint32_t aNumberOfOutputChannels,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aNumberOfInputChannels == 0 || aNumberOfOutputChannels == 0 ||
|
|
|
|
aNumberOfInputChannels > MAX_SCRIPT_PROCESSOR_CHANNELS ||
|
|
|
|
aNumberOfOutputChannels > MAX_SCRIPT_PROCESSOR_CHANNELS ||
|
|
|
|
!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-03-31 20:41:14 -07:00
|
|
|
already_AddRefed<AnalyserNode>
|
|
|
|
AudioContext::CreateAnalyser()
|
|
|
|
{
|
|
|
|
nsRefPtr<AnalyserNode> analyserNode = new AnalyserNode(this);
|
|
|
|
return analyserNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-10-31 12:09:32 -07:00
|
|
|
already_AddRefed<GainNode>
|
|
|
|
AudioContext::CreateGain()
|
|
|
|
{
|
|
|
|
nsRefPtr<GainNode> gainNode = new GainNode(this);
|
|
|
|
return gainNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-10-31 17:26:03 -07:00
|
|
|
already_AddRefed<DelayNode>
|
2012-11-19 12:52:29 -08:00
|
|
|
AudioContext::CreateDelay(double aMaxDelayTime, ErrorResult& aRv)
|
2012-10-31 17:26:03 -07:00
|
|
|
{
|
2013-03-25 12:44:14 -07:00
|
|
|
if (aMaxDelayTime > 0. && aMaxDelayTime < 180.) {
|
2012-11-15 17:48:04 -08:00
|
|
|
nsRefPtr<DelayNode> delayNode = new DelayNode(this, aMaxDelayTime);
|
|
|
|
return delayNode.forget();
|
2012-11-08 10:17:22 -08:00
|
|
|
}
|
2012-11-15 17:48:04 -08:00
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
2012-10-31 17:26:03 -07:00
|
|
|
}
|
|
|
|
|
2012-11-05 18:14:13 -08:00
|
|
|
already_AddRefed<PannerNode>
|
|
|
|
AudioContext::CreatePanner()
|
|
|
|
{
|
|
|
|
nsRefPtr<PannerNode> pannerNode = new PannerNode(this);
|
2013-04-11 05:47:57 -07:00
|
|
|
mPannerNodes.AppendElement(pannerNode);
|
2012-11-05 18:14:13 -08:00
|
|
|
return pannerNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-06 17:01:11 -08:00
|
|
|
already_AddRefed<DynamicsCompressorNode>
|
|
|
|
AudioContext::CreateDynamicsCompressor()
|
|
|
|
{
|
|
|
|
nsRefPtr<DynamicsCompressorNode> compressorNode =
|
|
|
|
new DynamicsCompressorNode(this);
|
|
|
|
return compressorNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-07 17:59:14 -08:00
|
|
|
already_AddRefed<BiquadFilterNode>
|
|
|
|
AudioContext::CreateBiquadFilter()
|
|
|
|
{
|
|
|
|
nsRefPtr<BiquadFilterNode> filterNode =
|
|
|
|
new BiquadFilterNode(this);
|
|
|
|
return filterNode.forget();
|
|
|
|
}
|
|
|
|
|
2012-11-05 16:26:03 -08:00
|
|
|
AudioListener*
|
|
|
|
AudioContext::Listener()
|
|
|
|
{
|
|
|
|
if (!mListener) {
|
|
|
|
mListener = new AudioListener(this);
|
|
|
|
}
|
|
|
|
return mListener;
|
|
|
|
}
|
|
|
|
|
2013-02-01 14:13:23 -08:00
|
|
|
void
|
|
|
|
AudioContext::DecodeAudioData(const ArrayBuffer& aBuffer,
|
|
|
|
DecodeSuccessCallback& aSuccessCallback,
|
|
|
|
const Optional<OwningNonNull<DecodeErrorCallback> >& aFailureCallback)
|
|
|
|
{
|
|
|
|
// Sniff the content of the media.
|
|
|
|
// Failed type sniffing will be handled by AsyncDecodeMedia.
|
|
|
|
nsAutoCString contentType;
|
|
|
|
NS_SniffContent(NS_DATA_SNIFFER_CATEGORY, nullptr,
|
|
|
|
aBuffer.Data(), aBuffer.Length(),
|
|
|
|
contentType);
|
|
|
|
|
|
|
|
nsCOMPtr<DecodeErrorCallback> failureCallback;
|
|
|
|
if (aFailureCallback.WasPassed()) {
|
|
|
|
failureCallback = aFailureCallback.Value().get();
|
|
|
|
}
|
|
|
|
nsAutoPtr<WebAudioDecodeJob> job(
|
|
|
|
new WebAudioDecodeJob(contentType, aBuffer, this,
|
|
|
|
&aSuccessCallback, failureCallback));
|
|
|
|
mDecoder.AsyncDecodeMedia(contentType.get(),
|
|
|
|
job->mBuffer, job->mLength, *job);
|
|
|
|
// Transfer the ownership to mDecodeJobs
|
|
|
|
mDecodeJobs.AppendElement(job.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::RemoveFromDecodeQueue(WebAudioDecodeJob* aDecodeJob)
|
|
|
|
{
|
|
|
|
mDecodeJobs.RemoveElement(aDecodeJob);
|
|
|
|
}
|
|
|
|
|
2013-04-11 05:47:57 -07:00
|
|
|
void
|
|
|
|
AudioContext::UnregisterAudioBufferSourceNode(AudioBufferSourceNode* aNode)
|
|
|
|
{
|
|
|
|
mAudioBufferSourceNodes.RemoveElement(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::UnregisterPannerNode(PannerNode* aNode)
|
|
|
|
{
|
|
|
|
mPannerNodes.RemoveElement(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::UpdatePannerSource()
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < mAudioBufferSourceNodes.Length(); i++) {
|
|
|
|
mAudioBufferSourceNodes[i]->UnregisterPannerNode();
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < mPannerNodes.Length(); i++) {
|
|
|
|
mPannerNodes[i]->FindConnectedSources();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
MediaStreamGraph*
|
|
|
|
AudioContext::Graph() const
|
|
|
|
{
|
|
|
|
return Destination()->Stream()->Graph();
|
2012-08-31 13:59:37 -07:00
|
|
|
}
|
2013-02-04 15:07:25 -08:00
|
|
|
|
|
|
|
MediaStream*
|
|
|
|
AudioContext::DestinationStream() const
|
|
|
|
{
|
|
|
|
return Destination()->Stream();
|
2012-09-07 15:13:26 -07:00
|
|
|
}
|
2012-08-31 13:59:37 -07:00
|
|
|
|
2013-03-14 18:01:02 -07:00
|
|
|
double
|
|
|
|
AudioContext::CurrentTime() const
|
|
|
|
{
|
|
|
|
return MediaTimeToSeconds(Destination()->Stream()->GetCurrentTime());
|
|
|
|
}
|
|
|
|
|
2013-04-22 08:45:34 -07:00
|
|
|
void
|
|
|
|
AudioContext::Shutdown()
|
|
|
|
{
|
|
|
|
Suspend();
|
|
|
|
mDecoder.Shutdown();
|
|
|
|
|
|
|
|
// Stop all audio buffer source nodes, to make sure that they release
|
|
|
|
// their self-references.
|
|
|
|
for (uint32_t i = 0; i < mAudioBufferSourceNodes.Length(); ++i) {
|
|
|
|
ErrorResult rv;
|
|
|
|
mAudioBufferSourceNodes[i]->Stop(0.0, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 19:59:33 -07:00
|
|
|
void
|
|
|
|
AudioContext::Suspend()
|
|
|
|
{
|
2013-04-14 18:52:55 -07:00
|
|
|
MediaStream* ds = DestinationStream();
|
|
|
|
if (ds) {
|
|
|
|
ds->ChangeExplicitBlockerCount(1);
|
|
|
|
}
|
2013-03-21 19:59:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::Resume()
|
|
|
|
{
|
2013-04-14 18:52:55 -07:00
|
|
|
MediaStream* ds = DestinationStream();
|
|
|
|
if (ds) {
|
|
|
|
ds->ChangeExplicitBlockerCount(-1);
|
|
|
|
}
|
2013-03-21 19:59:33 -07:00
|
|
|
}
|
|
|
|
|
2013-04-14 11:18:43 -07:00
|
|
|
JSContext*
|
|
|
|
AudioContext::GetJSContext() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> scriptGlobal =
|
|
|
|
do_QueryInterface(GetParentObject());
|
|
|
|
nsIScriptContext* scriptContext = scriptGlobal->GetContext();
|
|
|
|
if (!scriptContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return scriptContext->GetNativeContext();
|
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
|
|
|
}
|