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"
|
|
|
|
#include "nsIDOMWindow.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"
|
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-02-01 14:13:23 -08:00
|
|
|
#include "nsNetUtil.h"
|
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;
|
|
|
|
|
2012-08-31 13:59:37 -07:00
|
|
|
AudioContext::AudioContext(nsIDOMWindow* aWindow)
|
|
|
|
: 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);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-11-19 12:52:29 -08:00
|
|
|
if (aMaxDelayTime > 0. && aMaxDelayTime < 3.) {
|
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);
|
|
|
|
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-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-03-21 19:59:33 -07:00
|
|
|
void
|
|
|
|
AudioContext::Suspend()
|
|
|
|
{
|
|
|
|
DestinationStream()->ChangeExplicitBlockerCount(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioContext::Resume()
|
|
|
|
{
|
|
|
|
DestinationStream()->ChangeExplicitBlockerCount(-1);
|
|
|
|
}
|
|
|
|
|
2013-02-04 15:07:25 -08:00
|
|
|
}
|
|
|
|
}
|