gecko/content/media/webaudio/AudioNode.cpp
Robert O'Callahan fc44cd635c Bug 804837. Part 0: Rework the connection and input/output port logic for Web Audio nodes; r=ehsan
Here's what this patch does:
-- Makes AudioNodes mostly not use nsWrapperCache. AudioDestinationNode
still does.
-- Rename MaxNumberOfInputs/Outputs to NumberOfInputs/Outputs, and have them
default to 1 in AudioNode.
-- Allow any number of nodes to be connected to any given input/output port.
2013-01-23 19:50:18 -05:00

132 lines
3.9 KiB
C++

/* -*- 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 "AudioNode.h"
#include "AudioContext.h"
#include "nsContentUtils.h"
#include "mozilla/ErrorResult.h"
namespace mozilla {
namespace dom {
inline void
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
AudioNode::InputNode& aField,
const char* aName,
unsigned aFlags)
{
CycleCollectionNoteChild(aCallback, aField.mInputNode.get(), aName, aFlags);
}
inline void
ImplCycleCollectionUnlink(nsCycleCollectionTraversalCallback& aCallback,
AudioNode::InputNode& aField,
const char* aName,
unsigned aFlags)
{
aField.mInputNode = nullptr;
}
NS_IMPL_CYCLE_COLLECTION_3(AudioNode, mContext, mInputNodes, mOutputNodes)
NS_IMPL_CYCLE_COLLECTING_ADDREF(AudioNode)
NS_IMPL_CYCLE_COLLECTING_RELEASE(AudioNode)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AudioNode)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
AudioNode::AudioNode(AudioContext* aContext)
: mContext(aContext)
{
MOZ_ASSERT(aContext);
}
AudioNode::~AudioNode()
{
MOZ_ASSERT(mInputNodes.IsEmpty());
MOZ_ASSERT(mOutputNodes.IsEmpty());
}
static uint32_t
FindIndexOfNodeWithPorts(const nsTArray<AudioNode::InputNode>& aInputNodes, const AudioNode* aNode,
uint32_t aInputPort, uint32_t aOutputPort)
{
for (uint32_t i = 0; i < aInputNodes.Length(); ++i) {
if (aInputNodes[i].mInputNode == aNode &&
aInputNodes[i].mInputPort == aInputPort &&
aInputNodes[i].mOutputPort == aOutputPort) {
return i;
}
}
return nsTArray<AudioNode::InputNode>::NoIndex;
}
void
AudioNode::Connect(AudioNode& aDestination, uint32_t aOutput,
uint32_t aInput, ErrorResult& aRv)
{
if (aOutput >= NumberOfOutputs() ||
aInput >= aDestination.NumberOfInputs()) {
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
return;
}
if (Context() != aDestination.Context()) {
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
return;
}
if (FindIndexOfNodeWithPorts(aDestination.mInputNodes, this, aInput, aOutput) !=
nsTArray<AudioNode::InputNode>::NoIndex) {
// connection already exists.
return;
}
// The MediaStreamGraph will handle cycle detection. We don't need to do it
// here.
// Addref this temporarily so the refcount bumping below doesn't destroy us
nsRefPtr<AudioNode> kungFuDeathGrip = this;
mOutputNodes.AppendElement(&aDestination);
InputNode* input = aDestination.mInputNodes.AppendElement();
input->mInputNode = this;
input->mInputPort = aInput;
input->mOutputPort = aOutput;
}
void
AudioNode::Disconnect(uint32_t aOutput, ErrorResult& aRv)
{
if (aOutput >= NumberOfOutputs()) {
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
return;
}
// Disconnect everything connected to this output. First find the
// corresponding inputs and remove them.
nsAutoTArray<nsRefPtr<AudioNode>,4> outputsToUpdate;
for (int32_t i = mOutputNodes.Length() - 1; i >= 0; --i) {
AudioNode* dest = mOutputNodes[i];
for (int32_t j = dest->mInputNodes.Length() - 1; j >= 0; --j) {
InputNode& input = dest->mInputNodes[j];
if (input.mInputNode == this && input.mOutputPort == aOutput) {
dest->mInputNodes.RemoveElementAt(j);
// Remove one instance of 'dest' from mOutputNodes. There could be
// others, and it's not correct to remove them all since some of them
// could be for different output ports.
*outputsToUpdate.AppendElement() = mOutputNodes[i].forget();
mOutputNodes.RemoveElementAt(i);
break;
}
}
}
}
}
}