michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "AudioNode.h" michael@0: #include "mozilla/ErrorResult.h" michael@0: #include "AudioNodeStream.h" michael@0: #include "AudioNodeEngine.h" michael@0: #include "mozilla/dom/AudioParam.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: static const uint32_t INVALID_PORT = 0xffffffff; michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(AudioNode) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(AudioNode, DOMEventTargetHelper) michael@0: tmp->DisconnectFromGraph(); michael@0: if (tmp->mContext) { michael@0: tmp->mContext->UpdateNodeCount(-1); michael@0: } michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK(mContext) michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK(mOutputNodes) michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK(mOutputParams) michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AudioNode, michael@0: DOMEventTargetHelper) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContext) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOutputNodes) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOutputParams) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(AudioNode, DOMEventTargetHelper) michael@0: michael@0: NS_IMETHODIMP_(MozExternalRefCountType) michael@0: AudioNode::Release() michael@0: { michael@0: if (mRefCnt.get() == 1) { michael@0: // We are about to be deleted, disconnect the object from the graph before michael@0: // the derived type is destroyed. michael@0: DisconnectFromGraph(); michael@0: } michael@0: nsrefcnt r = DOMEventTargetHelper::Release(); michael@0: NS_LOG_RELEASE(this, r, "AudioNode"); michael@0: return r; michael@0: } michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(AudioNode) michael@0: NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) michael@0: michael@0: AudioNode::AudioNode(AudioContext* aContext, michael@0: uint32_t aChannelCount, michael@0: ChannelCountMode aChannelCountMode, michael@0: ChannelInterpretation aChannelInterpretation) michael@0: : DOMEventTargetHelper(aContext->GetParentObject()) michael@0: , mContext(aContext) michael@0: , mChannelCount(aChannelCount) michael@0: , mChannelCountMode(aChannelCountMode) michael@0: , mChannelInterpretation(aChannelInterpretation) michael@0: { michael@0: MOZ_ASSERT(aContext); michael@0: DOMEventTargetHelper::BindToOwner(aContext->GetParentObject()); michael@0: SetIsDOMBinding(); michael@0: aContext->UpdateNodeCount(1); michael@0: } michael@0: michael@0: AudioNode::~AudioNode() michael@0: { michael@0: MOZ_ASSERT(mInputNodes.IsEmpty()); michael@0: MOZ_ASSERT(mOutputNodes.IsEmpty()); michael@0: MOZ_ASSERT(mOutputParams.IsEmpty()); michael@0: if (mContext) { michael@0: mContext->UpdateNodeCount(-1); michael@0: } michael@0: } michael@0: michael@0: size_t michael@0: AudioNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: // Not owned: michael@0: // - mContext michael@0: // - mStream michael@0: size_t amount = 0; michael@0: michael@0: amount += mInputNodes.SizeOfExcludingThis(aMallocSizeOf); michael@0: for (size_t i = 0; i < mInputNodes.Length(); i++) { michael@0: amount += mInputNodes[i].SizeOfExcludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: // Just measure the array. The entire audio node graph is measured via the michael@0: // MediaStreamGraph's streams, so we don't want to double-count the elements. michael@0: amount += mOutputNodes.SizeOfExcludingThis(aMallocSizeOf); michael@0: michael@0: amount += mOutputParams.SizeOfExcludingThis(aMallocSizeOf); michael@0: for (size_t i = 0; i < mOutputParams.Length(); i++) { michael@0: amount += mOutputParams[i]->SizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: return amount; michael@0: } michael@0: michael@0: size_t michael@0: AudioNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: template michael@0: static uint32_t michael@0: FindIndexOfNode(const nsTArray& aInputNodes, const AudioNode* aNode) michael@0: { michael@0: for (uint32_t i = 0; i < aInputNodes.Length(); ++i) { michael@0: if (aInputNodes[i].mInputNode == aNode) { michael@0: return i; michael@0: } michael@0: } michael@0: return nsTArray::NoIndex; michael@0: } michael@0: michael@0: template michael@0: static uint32_t michael@0: FindIndexOfNodeWithPorts(const nsTArray& aInputNodes, const AudioNode* aNode, michael@0: uint32_t aInputPort, uint32_t aOutputPort) michael@0: { michael@0: for (uint32_t i = 0; i < aInputNodes.Length(); ++i) { michael@0: if (aInputNodes[i].mInputNode == aNode && michael@0: aInputNodes[i].mInputPort == aInputPort && michael@0: aInputNodes[i].mOutputPort == aOutputPort) { michael@0: return i; michael@0: } michael@0: } michael@0: return nsTArray::NoIndex; michael@0: } michael@0: michael@0: void michael@0: AudioNode::DisconnectFromGraph() michael@0: { michael@0: // Addref this temporarily so the refcount bumping below doesn't destroy us michael@0: // prematurely michael@0: nsRefPtr kungFuDeathGrip = this; michael@0: michael@0: // The idea here is that we remove connections one by one, and at each step michael@0: // the graph is in a valid state. michael@0: michael@0: // Disconnect inputs. We don't need them anymore. michael@0: while (!mInputNodes.IsEmpty()) { michael@0: uint32_t i = mInputNodes.Length() - 1; michael@0: nsRefPtr input = mInputNodes[i].mInputNode; michael@0: mInputNodes.RemoveElementAt(i); michael@0: input->mOutputNodes.RemoveElement(this); michael@0: } michael@0: michael@0: while (!mOutputNodes.IsEmpty()) { michael@0: uint32_t i = mOutputNodes.Length() - 1; michael@0: nsRefPtr output = mOutputNodes[i].forget(); michael@0: mOutputNodes.RemoveElementAt(i); michael@0: uint32_t inputIndex = FindIndexOfNode(output->mInputNodes, this); michael@0: // It doesn't matter which one we remove, since we're going to remove all michael@0: // entries for this node anyway. michael@0: output->mInputNodes.RemoveElementAt(inputIndex); michael@0: } michael@0: michael@0: while (!mOutputParams.IsEmpty()) { michael@0: uint32_t i = mOutputParams.Length() - 1; michael@0: nsRefPtr output = mOutputParams[i].forget(); michael@0: mOutputParams.RemoveElementAt(i); michael@0: uint32_t inputIndex = FindIndexOfNode(output->InputNodes(), this); michael@0: // It doesn't matter which one we remove, since we're going to remove all michael@0: // entries for this node anyway. michael@0: output->RemoveInputNode(inputIndex); michael@0: } michael@0: michael@0: DestroyMediaStream(); michael@0: } michael@0: michael@0: void michael@0: AudioNode::Connect(AudioNode& aDestination, uint32_t aOutput, michael@0: uint32_t aInput, ErrorResult& aRv) michael@0: { michael@0: if (aOutput >= NumberOfOutputs() || michael@0: aInput >= aDestination.NumberOfInputs()) { michael@0: aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); michael@0: return; michael@0: } michael@0: michael@0: if (Context() != aDestination.Context()) { michael@0: aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); michael@0: return; michael@0: } michael@0: michael@0: if (FindIndexOfNodeWithPorts(aDestination.mInputNodes, this, aInput, aOutput) != michael@0: nsTArray::NoIndex) { michael@0: // connection already exists. michael@0: return; michael@0: } michael@0: michael@0: // The MediaStreamGraph will handle cycle detection. We don't need to do it michael@0: // here. michael@0: michael@0: mOutputNodes.AppendElement(&aDestination); michael@0: InputNode* input = aDestination.mInputNodes.AppendElement(); michael@0: input->mInputNode = this; michael@0: input->mInputPort = aInput; michael@0: input->mOutputPort = aOutput; michael@0: if (aDestination.mStream) { michael@0: // Connect streams in the MediaStreamGraph michael@0: MOZ_ASSERT(aDestination.mStream->AsProcessedStream()); michael@0: ProcessedMediaStream* ps = michael@0: static_cast(aDestination.mStream.get()); michael@0: MOZ_ASSERT(aInput <= UINT16_MAX, "Unexpected large input port number"); michael@0: MOZ_ASSERT(aOutput <= UINT16_MAX, "Unexpected large output port number"); michael@0: input->mStreamPort = michael@0: ps->AllocateInputPort(mStream, MediaInputPort::FLAG_BLOCK_INPUT, michael@0: static_cast(aInput), michael@0: static_cast(aOutput)); michael@0: } michael@0: michael@0: // This connection may have connected a panner and a source. michael@0: Context()->UpdatePannerSource(); michael@0: } michael@0: michael@0: void michael@0: AudioNode::Connect(AudioParam& aDestination, uint32_t aOutput, michael@0: ErrorResult& aRv) michael@0: { michael@0: if (aOutput >= NumberOfOutputs()) { michael@0: aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); michael@0: return; michael@0: } michael@0: michael@0: if (Context() != aDestination.GetParentObject()) { michael@0: aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); michael@0: return; michael@0: } michael@0: michael@0: if (FindIndexOfNodeWithPorts(aDestination.InputNodes(), this, INVALID_PORT, aOutput) != michael@0: nsTArray::NoIndex) { michael@0: // connection already exists. michael@0: return; michael@0: } michael@0: michael@0: mOutputParams.AppendElement(&aDestination); michael@0: InputNode* input = aDestination.AppendInputNode(); michael@0: input->mInputNode = this; michael@0: input->mInputPort = INVALID_PORT; michael@0: input->mOutputPort = aOutput; michael@0: michael@0: MediaStream* stream = aDestination.Stream(); michael@0: MOZ_ASSERT(stream->AsProcessedStream()); michael@0: ProcessedMediaStream* ps = static_cast(stream); michael@0: michael@0: // Setup our stream as an input to the AudioParam's stream michael@0: MOZ_ASSERT(aOutput <= UINT16_MAX, "Unexpected large output port number"); michael@0: input->mStreamPort = ps->AllocateInputPort(mStream, MediaInputPort::FLAG_BLOCK_INPUT, michael@0: 0, static_cast(aOutput)); michael@0: } michael@0: michael@0: void michael@0: AudioNode::SendDoubleParameterToStream(uint32_t aIndex, double aValue) michael@0: { michael@0: AudioNodeStream* ns = static_cast(mStream.get()); michael@0: MOZ_ASSERT(ns, "How come we don't have a stream here?"); michael@0: ns->SetDoubleParameter(aIndex, aValue); michael@0: } michael@0: michael@0: void michael@0: AudioNode::SendInt32ParameterToStream(uint32_t aIndex, int32_t aValue) michael@0: { michael@0: AudioNodeStream* ns = static_cast(mStream.get()); michael@0: MOZ_ASSERT(ns, "How come we don't have a stream here?"); michael@0: ns->SetInt32Parameter(aIndex, aValue); michael@0: } michael@0: michael@0: void michael@0: AudioNode::SendThreeDPointParameterToStream(uint32_t aIndex, const ThreeDPoint& aValue) michael@0: { michael@0: AudioNodeStream* ns = static_cast(mStream.get()); michael@0: MOZ_ASSERT(ns, "How come we don't have a stream here?"); michael@0: ns->SetThreeDPointParameter(aIndex, aValue); michael@0: } michael@0: michael@0: void michael@0: AudioNode::SendChannelMixingParametersToStream() michael@0: { michael@0: AudioNodeStream* ns = static_cast(mStream.get()); michael@0: MOZ_ASSERT(ns, "How come we don't have a stream here?"); michael@0: ns->SetChannelMixingParameters(mChannelCount, mChannelCountMode, michael@0: mChannelInterpretation); michael@0: } michael@0: michael@0: void michael@0: AudioNode::SendTimelineParameterToStream(AudioNode* aNode, uint32_t aIndex, michael@0: const AudioParamTimeline& aValue) michael@0: { michael@0: AudioNodeStream* ns = static_cast(aNode->mStream.get()); michael@0: MOZ_ASSERT(ns, "How come we don't have a stream here?"); michael@0: ns->SetTimelineParameter(aIndex, aValue); michael@0: } michael@0: michael@0: void michael@0: AudioNode::Disconnect(uint32_t aOutput, ErrorResult& aRv) michael@0: { michael@0: if (aOutput >= NumberOfOutputs()) { michael@0: aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); michael@0: return; michael@0: } michael@0: michael@0: // An upstream node may be starting to play on the graph thread, and the michael@0: // engine for a downstream node may be sending a PlayingRefChangeHandler michael@0: // ADDREF message to this (main) thread. Wait for a round trip before michael@0: // releasing nodes, to give engines receiving sound now time to keep their michael@0: // nodes alive. michael@0: class RunnableRelease : public nsRunnable { michael@0: public: michael@0: explicit RunnableRelease(already_AddRefed aNode) michael@0: : mNode(aNode) {} michael@0: michael@0: NS_IMETHODIMP Run() MOZ_OVERRIDE michael@0: { michael@0: mNode = nullptr; michael@0: return NS_OK; michael@0: } michael@0: private: michael@0: nsRefPtr mNode; michael@0: }; michael@0: michael@0: for (int32_t i = mOutputNodes.Length() - 1; i >= 0; --i) { michael@0: AudioNode* dest = mOutputNodes[i]; michael@0: for (int32_t j = dest->mInputNodes.Length() - 1; j >= 0; --j) { michael@0: InputNode& input = dest->mInputNodes[j]; michael@0: if (input.mInputNode == this && input.mOutputPort == aOutput) { michael@0: // Destroying the InputNode here sends a message to the graph thread michael@0: // to disconnect the streams, which should be sent before the michael@0: // RunAfterPendingUpdates() call below. michael@0: dest->mInputNodes.RemoveElementAt(j); michael@0: // Remove one instance of 'dest' from mOutputNodes. There could be michael@0: // others, and it's not correct to remove them all since some of them michael@0: // could be for different output ports. michael@0: nsRefPtr runnable = michael@0: new RunnableRelease(mOutputNodes[i].forget()); michael@0: mOutputNodes.RemoveElementAt(i); michael@0: mStream->RunAfterPendingUpdates(runnable.forget()); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: for (int32_t i = mOutputParams.Length() - 1; i >= 0; --i) { michael@0: AudioParam* dest = mOutputParams[i]; michael@0: for (int32_t j = dest->InputNodes().Length() - 1; j >= 0; --j) { michael@0: const InputNode& input = dest->InputNodes()[j]; michael@0: if (input.mInputNode == this && input.mOutputPort == aOutput) { michael@0: dest->RemoveInputNode(j); michael@0: // Remove one instance of 'dest' from mOutputParams. There could be michael@0: // others, and it's not correct to remove them all since some of them michael@0: // could be for different output ports. michael@0: mOutputParams.RemoveElementAt(i); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // This disconnection may have disconnected a panner and a source. michael@0: Context()->UpdatePannerSource(); michael@0: } michael@0: michael@0: void michael@0: AudioNode::DestroyMediaStream() michael@0: { michael@0: if (mStream) { michael@0: { michael@0: // Remove the node reference on the engine, and take care to not michael@0: // hold the lock when the stream gets destroyed, because that will michael@0: // cause the engine to be destroyed as well, and we don't want to michael@0: // be holding the lock as we're trying to destroy it! michael@0: AudioNodeStream* ns = static_cast(mStream.get()); michael@0: MutexAutoLock lock(ns->Engine()->NodeMutex()); michael@0: MOZ_ASSERT(ns, "How come we don't have a stream here?"); michael@0: MOZ_ASSERT(ns->Engine()->Node() == this, "Invalid node reference"); michael@0: ns->Engine()->ClearNode(); michael@0: } michael@0: michael@0: mStream->Destroy(); michael@0: mStream = nullptr; michael@0: } michael@0: } michael@0: michael@0: void michael@0: AudioNode::RemoveOutputParam(AudioParam* aParam) michael@0: { michael@0: mOutputParams.RemoveElement(aParam); michael@0: } michael@0: michael@0: } michael@0: }