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 "AudioParam.h" michael@0: #include "mozilla/dom/AudioParamBinding.h" michael@0: #include "AudioNodeEngine.h" michael@0: #include "AudioNodeStream.h" michael@0: #include "AudioContext.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(AudioParam) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioParam) michael@0: tmp->DisconnectFromGraphAndDestroyStream(); michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK(mNode) michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_END michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AudioParam) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNode) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(AudioParam) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(AudioParam) michael@0: michael@0: NS_IMETHODIMP_(MozExternalRefCountType) michael@0: AudioParam::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: DisconnectFromGraphAndDestroyStream(); michael@0: } michael@0: NS_IMPL_CC_NATIVE_RELEASE_BODY(AudioParam) michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AudioParam, AddRef) michael@0: NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AudioParam, Release) michael@0: michael@0: AudioParam::AudioParam(AudioNode* aNode, michael@0: AudioParam::CallbackType aCallback, michael@0: float aDefaultValue) michael@0: : AudioParamTimeline(aDefaultValue) michael@0: , mNode(aNode) michael@0: , mCallback(aCallback) michael@0: , mDefaultValue(aDefaultValue) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: AudioParam::~AudioParam() michael@0: { michael@0: MOZ_ASSERT(mInputNodes.IsEmpty()); michael@0: } michael@0: michael@0: JSObject* michael@0: AudioParam::WrapObject(JSContext* aCx) michael@0: { michael@0: return AudioParamBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: void michael@0: AudioParam::DisconnectFromGraphAndDestroyStream() 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: 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->RemoveOutputParam(this); michael@0: } michael@0: michael@0: if (mNodeStreamPort) { michael@0: mNodeStreamPort->Destroy(); michael@0: mNodeStreamPort = nullptr; michael@0: } michael@0: michael@0: if (mStream) { michael@0: mStream->Destroy(); michael@0: mStream = nullptr; michael@0: } michael@0: } michael@0: michael@0: MediaStream* michael@0: AudioParam::Stream() michael@0: { michael@0: if (mStream) { michael@0: return mStream; michael@0: } michael@0: michael@0: AudioNodeEngine* engine = new AudioNodeEngine(nullptr); michael@0: nsRefPtr stream = michael@0: mNode->Context()->Graph()->CreateAudioNodeStream(engine, michael@0: MediaStreamGraph::INTERNAL_STREAM, michael@0: Node()->Context()->SampleRate()); michael@0: michael@0: // Force the input to have only one channel, and make it down-mix using michael@0: // the speaker rules if needed. michael@0: stream->SetChannelMixingParametersImpl(1, ChannelCountMode::Explicit, ChannelInterpretation::Speakers); michael@0: // Mark as an AudioParam helper stream michael@0: stream->SetAudioParamHelperStream(); michael@0: michael@0: mStream = stream.forget(); michael@0: michael@0: // Setup the AudioParam's stream as an input to the owner AudioNode's stream michael@0: MediaStream* nodeStream = mNode->Stream(); michael@0: MOZ_ASSERT(nodeStream->AsProcessedStream()); michael@0: ProcessedMediaStream* ps = static_cast(nodeStream); michael@0: mNodeStreamPort = ps->AllocateInputPort(mStream, MediaInputPort::FLAG_BLOCK_INPUT); michael@0: michael@0: // Let the MSG's copy of AudioParamTimeline know about the change in the stream michael@0: mCallback(mNode); michael@0: michael@0: return mStream; michael@0: } michael@0: michael@0: float michael@0: AudioParamTimeline::AudioNodeInputValue(size_t aCounter) const michael@0: { michael@0: MOZ_ASSERT(mStream); michael@0: michael@0: // If we have a chunk produced by the AudioNode inputs to the AudioParam, michael@0: // get its value now. We use aCounter to tell us which frame of the last michael@0: // AudioChunk to look at. michael@0: float audioNodeInputValue = 0.0f; michael@0: const AudioChunk& lastAudioNodeChunk = michael@0: static_cast(mStream.get())->LastChunks()[0]; michael@0: if (!lastAudioNodeChunk.IsNull()) { michael@0: MOZ_ASSERT(lastAudioNodeChunk.GetDuration() == WEBAUDIO_BLOCK_SIZE); michael@0: audioNodeInputValue = michael@0: static_cast(lastAudioNodeChunk.mChannelData[0])[aCounter]; michael@0: audioNodeInputValue *= lastAudioNodeChunk.mVolume; michael@0: } michael@0: michael@0: return audioNodeInputValue; michael@0: } michael@0: michael@0: } michael@0: } michael@0: