2012-09-18 16:07:33 -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 "AudioDestinationNode.h"
|
|
|
|
#include "mozilla/dom/AudioDestinationNodeBinding.h"
|
2014-05-09 11:33:17 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2013-10-08 11:20:33 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-04-29 10:27:26 -07:00
|
|
|
#include "mozilla/Services.h"
|
2013-10-08 11:20:33 -07:00
|
|
|
#include "AudioChannelAgent.h"
|
2014-03-20 03:45:55 -07:00
|
|
|
#include "AudioChannelService.h"
|
2013-02-04 15:07:25 -08:00
|
|
|
#include "AudioNodeEngine.h"
|
|
|
|
#include "AudioNodeStream.h"
|
|
|
|
#include "MediaStreamGraph.h"
|
2013-05-16 16:30:57 -07:00
|
|
|
#include "OfflineAudioCompletionEvent.h"
|
2013-10-08 11:20:33 -07:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIDocShell.h"
|
2013-10-11 04:55:47 -07:00
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2014-01-15 03:08:20 -08:00
|
|
|
#include "nsIAppShell.h"
|
|
|
|
#include "nsWidgetsCID.h"
|
2012-09-18 16:07:33 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-03-11 03:46:55 -07:00
|
|
|
static uint8_t gWebAudioOutputKey;
|
|
|
|
|
2013-05-16 16:30:42 -07:00
|
|
|
class OfflineDestinationNodeEngine : public AudioNodeEngine
|
|
|
|
{
|
|
|
|
public:
|
2013-05-16 16:30:57 -07:00
|
|
|
typedef AutoFallibleTArray<nsAutoArrayPtr<float>, 2> InputChannels;
|
|
|
|
|
2013-05-16 16:30:42 -07:00
|
|
|
OfflineDestinationNodeEngine(AudioDestinationNode* aNode,
|
|
|
|
uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength,
|
|
|
|
float aSampleRate)
|
|
|
|
: AudioNodeEngine(aNode)
|
2013-05-16 16:30:57 -07:00
|
|
|
, mWriteIndex(0)
|
2013-05-16 16:30:42 -07:00
|
|
|
, mLength(aLength)
|
|
|
|
, mSampleRate(aSampleRate)
|
|
|
|
{
|
2013-05-16 16:30:57 -07:00
|
|
|
// These allocations might fail if content provides a huge number of
|
|
|
|
// channels or size, but it's OK since we'll deal with the failure
|
|
|
|
// gracefully.
|
|
|
|
if (mInputChannels.SetLength(aNumberOfChannels)) {
|
|
|
|
static const fallible_t fallible = fallible_t();
|
|
|
|
for (uint32_t i = 0; i < aNumberOfChannels; ++i) {
|
|
|
|
mInputChannels[i] = new(fallible) float[aLength];
|
|
|
|
if (!mInputChannels[i]) {
|
|
|
|
mInputChannels.Clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-16 16:30:42 -07:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:09:49 -08:00
|
|
|
virtual void ProcessBlock(AudioNodeStream* aStream,
|
|
|
|
const AudioChunk& aInput,
|
|
|
|
AudioChunk* aOutput,
|
|
|
|
bool* aFinished) MOZ_OVERRIDE
|
2013-05-16 16:30:42 -07:00
|
|
|
{
|
2013-05-16 16:30:57 -07:00
|
|
|
// Do this just for the sake of political correctness; this output
|
|
|
|
// will not go anywhere.
|
2013-05-16 16:30:42 -07:00
|
|
|
*aOutput = aInput;
|
2013-05-16 16:30:57 -07:00
|
|
|
|
|
|
|
// Handle the case of allocation failure in the input buffer
|
|
|
|
if (mInputChannels.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:08:02 -08:00
|
|
|
if (mWriteIndex >= mLength) {
|
|
|
|
NS_ASSERTION(mWriteIndex == mLength, "Overshot length");
|
|
|
|
// Don't record any more.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-16 16:30:57 -07:00
|
|
|
// Record our input buffer
|
|
|
|
MOZ_ASSERT(mWriteIndex < mLength, "How did this happen?");
|
|
|
|
const uint32_t duration = std::min(WEBAUDIO_BLOCK_SIZE, mLength - mWriteIndex);
|
2013-05-29 16:33:14 -07:00
|
|
|
const uint32_t commonChannelCount = std::min(mInputChannels.Length(),
|
|
|
|
aInput.mChannelData.Length());
|
|
|
|
// First, copy as many channels in the input as we have
|
|
|
|
for (uint32_t i = 0; i < commonChannelCount; ++i) {
|
2013-05-16 16:30:57 -07:00
|
|
|
if (aInput.IsNull()) {
|
|
|
|
PodZero(mInputChannels[i] + mWriteIndex, duration);
|
|
|
|
} else {
|
|
|
|
const float* inputBuffer = static_cast<const float*>(aInput.mChannelData[i]);
|
|
|
|
if (duration == WEBAUDIO_BLOCK_SIZE) {
|
|
|
|
// Use the optimized version of the copy with scale operation
|
|
|
|
AudioBlockCopyChannelWithScale(inputBuffer, aInput.mVolume,
|
|
|
|
mInputChannels[i] + mWriteIndex);
|
|
|
|
} else {
|
|
|
|
if (aInput.mVolume == 1.0f) {
|
|
|
|
PodCopy(mInputChannels[i] + mWriteIndex, inputBuffer, duration);
|
|
|
|
} else {
|
|
|
|
for (uint32_t j = 0; j < duration; ++j) {
|
|
|
|
mInputChannels[i][mWriteIndex + j] = aInput.mVolume * inputBuffer[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-29 16:33:14 -07:00
|
|
|
// Then, silence all of the remaining channels
|
|
|
|
for (uint32_t i = commonChannelCount; i < mInputChannels.Length(); ++i) {
|
|
|
|
PodZero(mInputChannels[i] + mWriteIndex, duration);
|
|
|
|
}
|
2013-05-16 16:30:57 -07:00
|
|
|
mWriteIndex += duration;
|
|
|
|
|
2013-12-08 21:08:02 -08:00
|
|
|
if (mWriteIndex >= mLength) {
|
|
|
|
NS_ASSERTION(mWriteIndex == mLength, "Overshot length");
|
|
|
|
// Go to finished state. When the graph's current time eventually reaches
|
|
|
|
// the end of the stream, then the main thread will be notified and we'll
|
|
|
|
// shut down the AudioContext.
|
2013-05-16 16:30:57 -07:00
|
|
|
*aFinished = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:08:02 -08:00
|
|
|
void FireOfflineCompletionEvent(AudioDestinationNode* aNode)
|
2013-05-16 16:30:57 -07:00
|
|
|
{
|
2013-12-08 21:08:02 -08:00
|
|
|
AudioContext* context = aNode->Context();
|
|
|
|
context->Shutdown();
|
|
|
|
// Shutdown drops self reference, but the context is still referenced by aNode,
|
|
|
|
// which is strongly referenced by the runnable that called
|
|
|
|
// AudioDestinationNode::FireOfflineCompletionEvent.
|
|
|
|
|
2014-06-19 00:21:14 -07:00
|
|
|
AutoJSAPI jsapi;
|
2014-06-25 03:17:17 -07:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(aNode->GetOwner()))) {
|
2013-12-08 21:08:02 -08:00
|
|
|
return;
|
|
|
|
}
|
2014-05-09 11:33:17 -07:00
|
|
|
JSContext* cx = jsapi.cx();
|
2013-05-16 16:30:57 -07:00
|
|
|
|
2013-12-08 21:08:02 -08:00
|
|
|
// Create the input buffer
|
2014-05-15 14:23:27 -07:00
|
|
|
ErrorResult rv;
|
|
|
|
nsRefPtr<AudioBuffer> renderedBuffer =
|
|
|
|
AudioBuffer::Create(context, mInputChannels.Length(),
|
|
|
|
mLength, mSampleRate, cx, rv);
|
|
|
|
if (rv.Failed()) {
|
2013-12-08 21:08:02 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < mInputChannels.Length(); ++i) {
|
2014-05-09 02:16:03 -07:00
|
|
|
renderedBuffer->SetRawChannelContents(i, mInputChannels[i]);
|
2013-12-08 21:08:02 -08:00
|
|
|
}
|
2013-05-16 16:30:57 -07:00
|
|
|
|
2013-12-08 21:08:02 -08:00
|
|
|
nsRefPtr<OfflineAudioCompletionEvent> event =
|
|
|
|
new OfflineAudioCompletionEvent(context, nullptr, nullptr);
|
|
|
|
event->InitEvent(renderedBuffer);
|
|
|
|
context->DispatchTrustedEvent(event);
|
2013-05-16 16:30:42 -07:00
|
|
|
}
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
size_t amount = AudioNodeEngine::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
amount += mInputChannels.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2013-05-16 16:30:57 -07:00
|
|
|
private:
|
|
|
|
// The input to the destination node is recorded in the mInputChannels buffer.
|
|
|
|
// When this buffer fills up with mLength frames, the buffered input is sent
|
|
|
|
// to the main thread in order to dispatch OfflineAudioCompletionEvent.
|
|
|
|
InputChannels mInputChannels;
|
|
|
|
// An index representing the next offset in mInputChannels to be written to.
|
|
|
|
uint32_t mWriteIndex;
|
|
|
|
// How many frames the OfflineAudioContext intends to produce.
|
2013-05-16 16:30:42 -07:00
|
|
|
uint32_t mLength;
|
|
|
|
float mSampleRate;
|
|
|
|
};
|
|
|
|
|
2014-06-24 22:15:36 -07:00
|
|
|
class InputMutedRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InputMutedRunnable(AudioNodeStream* aStream,
|
|
|
|
bool aInputMuted)
|
|
|
|
: mStream(aStream)
|
|
|
|
, mInputMuted(aInputMuted)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsRefPtr<AudioNode> node = mStream->Engine()->NodeMainThread();
|
|
|
|
|
|
|
|
if (node) {
|
|
|
|
nsRefPtr<AudioDestinationNode> destinationNode =
|
|
|
|
static_cast<AudioDestinationNode*>(node.get());
|
|
|
|
destinationNode->InputMuted(mInputMuted);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<AudioNodeStream> mStream;
|
|
|
|
bool mInputMuted;
|
|
|
|
};
|
|
|
|
|
2013-07-03 17:44:32 -07:00
|
|
|
class DestinationNodeEngine : public AudioNodeEngine
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit DestinationNodeEngine(AudioDestinationNode* aNode)
|
|
|
|
: AudioNodeEngine(aNode)
|
|
|
|
, mVolume(1.0f)
|
2014-10-19 01:23:20 -07:00
|
|
|
, mLastInputMuted(false)
|
2013-07-03 17:44:32 -07:00
|
|
|
{
|
2014-06-24 22:15:36 -07:00
|
|
|
MOZ_ASSERT(aNode);
|
2013-07-03 17:44:32 -07:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:09:49 -08:00
|
|
|
virtual void ProcessBlock(AudioNodeStream* aStream,
|
|
|
|
const AudioChunk& aInput,
|
|
|
|
AudioChunk* aOutput,
|
|
|
|
bool* aFinished) MOZ_OVERRIDE
|
2013-07-03 17:44:32 -07:00
|
|
|
{
|
|
|
|
*aOutput = aInput;
|
|
|
|
aOutput->mVolume *= mVolume;
|
2014-06-24 22:15:36 -07:00
|
|
|
|
|
|
|
bool newInputMuted = aInput.IsNull() || aInput.IsMuted();
|
|
|
|
if (newInputMuted != mLastInputMuted) {
|
|
|
|
mLastInputMuted = newInputMuted;
|
|
|
|
|
|
|
|
nsRefPtr<InputMutedRunnable> runnable =
|
|
|
|
new InputMutedRunnable(aStream, newInputMuted);
|
|
|
|
aStream->Graph()->
|
|
|
|
DispatchToMainThreadAfterStreamStateUpdate(runnable.forget());
|
|
|
|
}
|
2013-07-03 17:44:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void SetDoubleParameter(uint32_t aIndex, double aParam) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
if (aIndex == VOLUME) {
|
|
|
|
mVolume = aParam;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Parameters {
|
|
|
|
VOLUME,
|
|
|
|
};
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2013-07-03 17:44:32 -07:00
|
|
|
private:
|
|
|
|
float mVolume;
|
2014-06-24 22:15:36 -07:00
|
|
|
bool mLastInputMuted;
|
2013-07-03 17:44:32 -07:00
|
|
|
};
|
|
|
|
|
2013-10-08 11:20:33 -07:00
|
|
|
static bool UseAudioChannelService()
|
|
|
|
{
|
|
|
|
return Preferences::GetBool("media.useAudioChannelService");
|
|
|
|
}
|
|
|
|
|
2014-07-29 08:30:40 -07:00
|
|
|
class EventProxyHandler MOZ_FINAL : public nsIDOMEventListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
explicit EventProxyHandler(nsIDOMEventListener* aNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
mWeakNode = do_GetWeakReference(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIDOMEventListener
|
|
|
|
NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEventListener> listener = do_QueryReferent(mWeakNode);
|
|
|
|
if (!listener) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto node = static_cast<AudioDestinationNode*>(listener.get());
|
|
|
|
return node->HandleEvent(aEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~EventProxyHandler()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
nsWeakPtr mWeakNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(EventProxyHandler, nsIDOMEventListener)
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(AudioDestinationNode, AudioNode,
|
2014-07-29 08:30:40 -07:00
|
|
|
mAudioChannelAgent, mEventProxyHelper)
|
2013-10-08 11:20:33 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(AudioDestinationNode)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIAudioChannelAgentCallback)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(AudioNode)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(AudioDestinationNode, AudioNode)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(AudioDestinationNode, AudioNode)
|
2013-02-12 08:42:59 -08:00
|
|
|
|
2013-05-16 16:30:42 -07:00
|
|
|
AudioDestinationNode::AudioDestinationNode(AudioContext* aContext,
|
|
|
|
bool aIsOffline,
|
2014-04-18 02:23:36 -07:00
|
|
|
AudioChannel aChannel,
|
2013-05-16 16:30:42 -07:00
|
|
|
uint32_t aNumberOfChannels,
|
|
|
|
uint32_t aLength,
|
|
|
|
float aSampleRate)
|
2013-04-27 15:44:50 -07:00
|
|
|
: AudioNode(aContext,
|
2013-05-16 16:30:42 -07:00
|
|
|
aIsOffline ? aNumberOfChannels : 2,
|
2013-04-27 15:44:50 -07:00
|
|
|
ChannelCountMode::Explicit,
|
|
|
|
ChannelInterpretation::Speakers)
|
2013-05-16 16:30:42 -07:00
|
|
|
, mFramesToProduce(aLength)
|
2013-10-11 04:55:47 -07:00
|
|
|
, mAudioChannel(AudioChannel::Normal)
|
2013-12-08 21:08:02 -08:00
|
|
|
, mIsOffline(aIsOffline)
|
|
|
|
, mHasFinished(false)
|
2014-06-06 00:06:18 -07:00
|
|
|
, mAudioChannelAgentPlaying(false)
|
2014-01-15 03:08:20 -08:00
|
|
|
, mExtraCurrentTime(0)
|
|
|
|
, mExtraCurrentTimeSinceLastStartedBlocking(0)
|
|
|
|
, mExtraCurrentTimeUpdatedSinceLastStableState(false)
|
2012-09-18 16:07:33 -07:00
|
|
|
{
|
2013-05-16 16:30:42 -07:00
|
|
|
MediaStreamGraph* graph = aIsOffline ?
|
2014-04-23 02:20:56 -07:00
|
|
|
MediaStreamGraph::CreateNonRealtimeInstance(aSampleRate) :
|
2014-08-26 08:02:08 -07:00
|
|
|
MediaStreamGraph::GetInstance(DOMMediaStream::HINT_CONTENTS_AUDIO, aChannel);
|
2013-05-16 16:30:42 -07:00
|
|
|
AudioNodeEngine* engine = aIsOffline ?
|
|
|
|
new OfflineDestinationNodeEngine(this, aNumberOfChannels,
|
|
|
|
aLength, aSampleRate) :
|
2013-07-03 17:44:32 -07:00
|
|
|
static_cast<AudioNodeEngine*>(new DestinationNodeEngine(this));
|
|
|
|
|
2013-05-16 16:30:42 -07:00
|
|
|
mStream = graph->CreateAudioNodeStream(engine, MediaStreamGraph::EXTERNAL_STREAM);
|
2013-12-08 21:08:02 -08:00
|
|
|
mStream->AddMainThreadListener(this);
|
2014-03-11 03:46:55 -07:00
|
|
|
mStream->AddAudioOutput(&gWebAudioOutputKey);
|
2013-10-08 11:20:33 -07:00
|
|
|
|
2014-04-18 02:23:36 -07:00
|
|
|
if (aChannel != AudioChannel::Normal) {
|
2014-03-20 03:45:55 -07:00
|
|
|
ErrorResult rv;
|
2014-04-18 02:23:36 -07:00
|
|
|
SetMozAudioChannelType(aChannel, rv);
|
2014-03-20 03:45:55 -07:00
|
|
|
}
|
2012-09-18 16:07:33 -07:00
|
|
|
}
|
|
|
|
|
2014-07-08 14:23:17 -07:00
|
|
|
AudioDestinationNode::~AudioDestinationNode()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-13 11:08:10 -07:00
|
|
|
size_t
|
|
|
|
AudioDestinationNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
size_t amount = AudioNode::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
// Might be useful in the future:
|
|
|
|
// - mAudioChannelAgent
|
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
AudioDestinationNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2013-09-09 22:05:22 -07:00
|
|
|
void
|
|
|
|
AudioDestinationNode::DestroyMediaStream()
|
|
|
|
{
|
2013-10-08 11:20:33 -07:00
|
|
|
if (mAudioChannelAgent && !Context()->IsOffline()) {
|
|
|
|
mAudioChannelAgent->StopPlaying();
|
|
|
|
mAudioChannelAgent = nullptr;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(GetOwner());
|
|
|
|
NS_ENSURE_TRUE_VOID(target);
|
|
|
|
|
2014-07-29 08:30:40 -07:00
|
|
|
target->RemoveSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
|
|
|
|
mEventProxyHelper,
|
2013-10-08 11:20:33 -07:00
|
|
|
/* useCapture = */ true);
|
|
|
|
}
|
|
|
|
|
2013-09-09 22:05:22 -07:00
|
|
|
if (!mStream)
|
|
|
|
return;
|
|
|
|
|
2013-12-08 21:08:02 -08:00
|
|
|
mStream->RemoveMainThreadListener(this);
|
2013-09-09 22:05:22 -07:00
|
|
|
MediaStreamGraph* graph = mStream->Graph();
|
|
|
|
if (graph->IsNonRealtime()) {
|
|
|
|
MediaStreamGraph::DestroyNonRealtimeInstance(graph);
|
|
|
|
}
|
|
|
|
AudioNode::DestroyMediaStream();
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:08:02 -08:00
|
|
|
void
|
|
|
|
AudioDestinationNode::NotifyMainThreadStateChanged()
|
|
|
|
{
|
|
|
|
if (mStream->IsFinished() && !mHasFinished) {
|
|
|
|
mHasFinished = true;
|
|
|
|
if (mIsOffline) {
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethod(this, &AudioDestinationNode::FireOfflineCompletionEvent);
|
|
|
|
NS_DispatchToCurrentThread(runnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioDestinationNode::FireOfflineCompletionEvent()
|
|
|
|
{
|
|
|
|
AudioNodeStream* stream = static_cast<AudioNodeStream*>(Stream());
|
|
|
|
OfflineDestinationNodeEngine* engine =
|
|
|
|
static_cast<OfflineDestinationNodeEngine*>(stream->Engine());
|
|
|
|
engine->FireOfflineCompletionEvent(this);
|
|
|
|
}
|
|
|
|
|
2013-06-10 10:32:28 -07:00
|
|
|
uint32_t
|
|
|
|
AudioDestinationNode::MaxChannelCount() const
|
|
|
|
{
|
|
|
|
return Context()->MaxChannelCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioDestinationNode::SetChannelCount(uint32_t aChannelCount, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aChannelCount > MaxChannelCount()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioNode::SetChannelCount(aChannelCount, aRv);
|
|
|
|
}
|
|
|
|
|
2013-07-03 17:44:32 -07:00
|
|
|
void
|
|
|
|
AudioDestinationNode::Mute()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Context() && !Context()->IsOffline());
|
|
|
|
SendDoubleParameterToStream(DestinationNodeEngine::VOLUME, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioDestinationNode::Unmute()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Context() && !Context()->IsOffline());
|
|
|
|
SendDoubleParameterToStream(DestinationNodeEngine::VOLUME, 1.0f);
|
|
|
|
}
|
|
|
|
|
2013-05-16 16:31:08 -07:00
|
|
|
void
|
2013-09-09 22:10:53 -07:00
|
|
|
AudioDestinationNode::OfflineShutdown()
|
2013-05-16 16:31:08 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(Context() && Context()->IsOffline(),
|
|
|
|
"Should only be called on a valid OfflineAudioContext");
|
2013-09-09 22:10:53 -07:00
|
|
|
|
2013-05-16 16:31:08 -07:00
|
|
|
MediaStreamGraph::DestroyNonRealtimeInstance(mStream->Graph());
|
2013-09-09 22:10:53 -07:00
|
|
|
mOfflineRenderingRef.Drop(this);
|
2013-05-16 16:31:08 -07:00
|
|
|
}
|
|
|
|
|
2012-09-18 16:07:33 -07:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
AudioDestinationNode::WrapObject(JSContext* aCx)
|
2012-09-18 16:07:33 -07:00
|
|
|
{
|
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 15:27:17 -07:00
|
|
|
return AudioDestinationNodeBinding::Wrap(aCx, this);
|
2012-09-18 16:07:33 -07:00
|
|
|
}
|
|
|
|
|
2013-05-16 16:30:42 -07:00
|
|
|
void
|
|
|
|
AudioDestinationNode::StartRendering()
|
|
|
|
{
|
2013-09-09 22:10:53 -07:00
|
|
|
mOfflineRenderingRef.Take(this);
|
2013-12-08 21:08:02 -08:00
|
|
|
mStream->Graph()->StartNonRealtimeProcessing(TrackRate(Context()->SampleRate()), mFramesToProduce);
|
2013-05-16 16:30:42 -07:00
|
|
|
}
|
|
|
|
|
2013-10-08 11:20:33 -07:00
|
|
|
void
|
|
|
|
AudioDestinationNode::SetCanPlay(bool aCanPlay)
|
|
|
|
{
|
|
|
|
mStream->SetTrackEnabled(AudioNodeStream::AUDIO_TRACK, aCanPlay);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioDestinationNode::HandleEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
nsAutoString type;
|
|
|
|
aEvent->GetType(type);
|
|
|
|
|
|
|
|
if (!type.EqualsLiteral("visibilitychange")) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> docshell = do_GetInterface(GetOwner());
|
|
|
|
NS_ENSURE_TRUE(docshell, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
bool isActive = false;
|
|
|
|
docshell->GetIsActive(&isActive);
|
|
|
|
|
|
|
|
mAudioChannelAgent->SetVisibilityState(isActive);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioDestinationNode::CanPlayChanged(int32_t aCanPlay)
|
|
|
|
{
|
2014-06-06 00:06:18 -07:00
|
|
|
bool playing = aCanPlay == AudioChannelState::AUDIO_CHANNEL_STATE_NORMAL;
|
|
|
|
if (playing == mAudioChannelAgentPlaying) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAudioChannelAgentPlaying = playing;
|
|
|
|
SetCanPlay(playing);
|
|
|
|
|
|
|
|
Context()->DispatchTrustedEvent(
|
|
|
|
playing ? NS_LITERAL_STRING("mozinterruptend")
|
|
|
|
: NS_LITERAL_STRING("mozinterruptbegin"));
|
|
|
|
|
2013-10-08 11:20:33 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-11 03:46:55 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
AudioDestinationNode::WindowVolumeChanged()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mAudioChannelAgent);
|
|
|
|
|
|
|
|
if (!mStream) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
float volume;
|
|
|
|
nsresult rv = mAudioChannelAgent->GetWindowVolume(&volume);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mStream->SetAudioOutputVolume(&gWebAudioOutputKey, volume);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-11 04:55:47 -07:00
|
|
|
AudioChannel
|
|
|
|
AudioDestinationNode::MozAudioChannelType() const
|
|
|
|
{
|
|
|
|
return mAudioChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioDestinationNode::SetMozAudioChannelType(AudioChannel aValue, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (Context()->IsOffline()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aValue != mAudioChannel &&
|
|
|
|
CheckAudioChannelPermissions(aValue)) {
|
|
|
|
mAudioChannel = aValue;
|
|
|
|
|
2014-07-30 04:26:12 -07:00
|
|
|
if (mStream) {
|
|
|
|
mStream->SetAudioChannelType(mAudioChannel);
|
|
|
|
}
|
|
|
|
|
2013-10-11 04:55:47 -07:00
|
|
|
if (mAudioChannelAgent) {
|
|
|
|
CreateAudioChannelAgent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AudioDestinationNode::CheckAudioChannelPermissions(AudioChannel aValue)
|
|
|
|
{
|
|
|
|
if (!Preferences::GetBool("media.useAudioChannelService")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only normal channel doesn't need permission.
|
|
|
|
if (aValue == AudioChannel::Normal) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-20 03:45:55 -07:00
|
|
|
// Maybe this audio channel is equal to the default one.
|
|
|
|
if (aValue == AudioChannelService::GetDefaultAudioChannel()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-11 04:55:47 -07:00
|
|
|
nsCOMPtr<nsIPermissionManager> permissionManager =
|
2014-04-29 10:27:26 -07:00
|
|
|
services::GetPermissionManager();
|
2013-10-11 04:55:47 -07:00
|
|
|
if (!permissionManager) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(GetOwner());
|
|
|
|
NS_ASSERTION(sop, "Window didn't QI to nsIScriptObjectPrincipal!");
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
|
|
|
|
|
|
|
|
uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
|
|
|
|
nsCString channel;
|
|
|
|
channel.AssignASCII(AudioChannelValues::strings[uint32_t(aValue)].value,
|
|
|
|
AudioChannelValues::strings[uint32_t(aValue)].length);
|
|
|
|
permissionManager->TestExactPermissionFromPrincipal(principal,
|
|
|
|
nsCString(NS_LITERAL_CSTRING("audio-channel-") + channel).get(),
|
|
|
|
&perm);
|
|
|
|
|
|
|
|
return perm == nsIPermissionManager::ALLOW_ACTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioDestinationNode::CreateAudioChannelAgent()
|
|
|
|
{
|
2014-07-29 08:30:40 -07:00
|
|
|
if (mIsOffline || !UseAudioChannelService()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mEventProxyHelper) {
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(GetOwner());
|
|
|
|
if (target) {
|
|
|
|
// We use a proxy because otherwise the event listerner would hold a
|
|
|
|
// reference of the destination node, and by extension, everything
|
|
|
|
// connected to it.
|
|
|
|
mEventProxyHelper = new EventProxyHandler(this);
|
|
|
|
target->AddSystemEventListener(NS_LITERAL_STRING("visibilitychange"),
|
|
|
|
mEventProxyHelper,
|
|
|
|
/* useCapture = */ true,
|
|
|
|
/* wantsUntrusted = */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 04:55:47 -07:00
|
|
|
if (mAudioChannelAgent) {
|
|
|
|
mAudioChannelAgent->StopPlaying();
|
|
|
|
}
|
|
|
|
|
|
|
|
mAudioChannelAgent = new AudioChannelAgent();
|
2014-04-10 10:39:20 -07:00
|
|
|
mAudioChannelAgent->InitWithWeakCallback(GetOwner(),
|
|
|
|
static_cast<int32_t>(mAudioChannel),
|
|
|
|
this);
|
2013-10-11 04:55:47 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> docshell = do_GetInterface(GetOwner());
|
|
|
|
if (docshell) {
|
|
|
|
bool isActive = false;
|
|
|
|
docshell->GetIsActive(&isActive);
|
|
|
|
mAudioChannelAgent->SetVisibilityState(isActive);
|
2014-10-19 01:23:20 -07:00
|
|
|
|
|
|
|
// The AudioChannelAgent must start playing immediately in order to avoid
|
|
|
|
// race conditions with mozinterruptbegin/end events.
|
|
|
|
InputMuted(false);
|
2013-10-11 04:55:47 -07:00
|
|
|
}
|
|
|
|
}
|
2014-01-15 03:08:20 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
AudioDestinationNode::NotifyStableState()
|
|
|
|
{
|
|
|
|
mExtraCurrentTimeUpdatedSinceLastStableState = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioDestinationNode::ScheduleStableStateNotification()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAppShell> appShell = do_GetService(kAppShellCID);
|
|
|
|
if (appShell) {
|
|
|
|
nsCOMPtr<nsIRunnable> event =
|
|
|
|
NS_NewRunnableMethod(this, &AudioDestinationNode::NotifyStableState);
|
|
|
|
appShell->RunInStableState(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
AudioDestinationNode::ExtraCurrentTime()
|
|
|
|
{
|
|
|
|
if (!mStartedBlockingDueToBeingOnlyNode.IsNull() &&
|
|
|
|
!mExtraCurrentTimeUpdatedSinceLastStableState) {
|
|
|
|
mExtraCurrentTimeUpdatedSinceLastStableState = true;
|
|
|
|
mExtraCurrentTimeSinceLastStartedBlocking =
|
|
|
|
(TimeStamp::Now() - mStartedBlockingDueToBeingOnlyNode).ToSeconds();
|
|
|
|
ScheduleStableStateNotification();
|
|
|
|
}
|
|
|
|
return mExtraCurrentTime + mExtraCurrentTimeSinceLastStartedBlocking;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AudioDestinationNode::SetIsOnlyNodeForContext(bool aIsOnlyNode)
|
|
|
|
{
|
|
|
|
if (!mStartedBlockingDueToBeingOnlyNode.IsNull() == aIsOnlyNode) {
|
|
|
|
// Nothing changed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mStream) {
|
|
|
|
// DestroyMediaStream has been called, presumably during CC Unlink().
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mIsOffline) {
|
|
|
|
// Don't block the destination stream for offline AudioContexts, since
|
|
|
|
// we expect the zero data produced when there are no other nodes to
|
|
|
|
// show up in its result buffer. Also, we would get confused by adding
|
|
|
|
// ExtraCurrentTime before StartRendering has even been called.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aIsOnlyNode) {
|
|
|
|
mStream->ChangeExplicitBlockerCount(1);
|
|
|
|
mStartedBlockingDueToBeingOnlyNode = TimeStamp::Now();
|
|
|
|
// Don't do an update of mExtraCurrentTimeSinceLastStartedBlocking until the next stable state.
|
|
|
|
mExtraCurrentTimeUpdatedSinceLastStableState = true;
|
|
|
|
ScheduleStableStateNotification();
|
|
|
|
} else {
|
|
|
|
// Force update of mExtraCurrentTimeSinceLastStartedBlocking if necessary
|
|
|
|
ExtraCurrentTime();
|
|
|
|
mExtraCurrentTime += mExtraCurrentTimeSinceLastStartedBlocking;
|
2014-04-07 04:20:57 -07:00
|
|
|
mExtraCurrentTimeSinceLastStartedBlocking = 0;
|
2014-01-15 03:08:20 -08:00
|
|
|
mStream->ChangeExplicitBlockerCount(-1);
|
|
|
|
mStartedBlockingDueToBeingOnlyNode = TimeStamp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-24 22:15:36 -07:00
|
|
|
void
|
|
|
|
AudioDestinationNode::InputMuted(bool aMuted)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(Context() && !Context()->IsOffline());
|
|
|
|
|
|
|
|
if (!mAudioChannelAgent) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-11 04:55:47 -07:00
|
|
|
|
2014-06-24 22:15:36 -07:00
|
|
|
if (aMuted) {
|
|
|
|
mAudioChannelAgent->StopPlaying();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t state = 0;
|
2014-10-19 01:23:20 -07:00
|
|
|
nsresult rv = mAudioChannelAgent->StartPlaying(&state);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CanPlayChanged(state);
|
2012-09-18 16:07:33 -07:00
|
|
|
}
|
2014-06-24 22:15:36 -07:00
|
|
|
|
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|