2012-10-17 05:19:06 +04: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 "AudioParam.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/dom/AudioParamBinding.h"
|
2013-05-02 05:02:31 +04:00
|
|
|
#include "AudioNodeEngine.h"
|
|
|
|
#include "AudioNodeStream.h"
|
2012-10-17 05:19:06 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AudioParam)
|
|
|
|
|
2013-05-02 02:59:02 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioParam)
|
2013-05-02 05:02:31 +04:00
|
|
|
tmp->DisconnectFromGraphAndDestroyStream();
|
2013-05-02 02:59:02 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AudioParam)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(AudioParam)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(AudioParam)
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
AudioParam::Release()
|
|
|
|
{
|
|
|
|
if (mRefCnt.get() == 1) {
|
|
|
|
// We are about to be deleted, disconnect the object from the graph before
|
|
|
|
// the derived type is destroyed.
|
2013-05-02 05:02:31 +04:00
|
|
|
DisconnectFromGraphAndDestroyStream();
|
2013-05-02 02:59:02 +04:00
|
|
|
}
|
|
|
|
NS_IMPL_CC_NATIVE_RELEASE_BODY(AudioParam)
|
|
|
|
}
|
2012-11-28 03:08:22 +04:00
|
|
|
|
2012-10-17 05:19:06 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AudioParam, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AudioParam, Release)
|
|
|
|
|
2013-01-29 03:59:29 +04:00
|
|
|
AudioParam::AudioParam(AudioNode* aNode,
|
|
|
|
AudioParam::CallbackType aCallback,
|
2013-04-09 05:57:54 +04:00
|
|
|
float aDefaultValue)
|
2013-01-25 23:26:31 +04:00
|
|
|
: AudioParamTimeline(aDefaultValue)
|
2013-01-29 03:59:29 +04:00
|
|
|
, mNode(aNode)
|
|
|
|
, mCallback(aCallback)
|
2013-01-25 23:26:31 +04:00
|
|
|
, mDefaultValue(aDefaultValue)
|
2012-10-17 05:19:06 +04:00
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioParam::~AudioParam()
|
|
|
|
{
|
2013-05-02 02:59:02 +04:00
|
|
|
MOZ_ASSERT(mInputNodes.IsEmpty());
|
2012-10-17 05:19:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-04-25 20:29:54 +04:00
|
|
|
AudioParam::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2012-10-17 05:19:06 +04:00
|
|
|
{
|
2013-03-12 03:03:47 +04:00
|
|
|
return AudioParamBinding::Wrap(aCx, aScope, this);
|
2012-10-17 05:19:06 +04:00
|
|
|
}
|
|
|
|
|
2013-05-02 02:59:02 +04:00
|
|
|
void
|
2013-05-02 05:02:31 +04:00
|
|
|
AudioParam::DisconnectFromGraphAndDestroyStream()
|
2013-05-02 02:59:02 +04:00
|
|
|
{
|
|
|
|
// Addref this temporarily so the refcount bumping below doesn't destroy us
|
|
|
|
// prematurely
|
|
|
|
nsRefPtr<AudioParam> kungFuDeathGrip = this;
|
|
|
|
|
|
|
|
while (!mInputNodes.IsEmpty()) {
|
|
|
|
uint32_t i = mInputNodes.Length() - 1;
|
|
|
|
nsRefPtr<AudioNode> input = mInputNodes[i].mInputNode;
|
|
|
|
mInputNodes.RemoveElementAt(i);
|
|
|
|
input->RemoveOutputParam(this);
|
|
|
|
}
|
2013-05-02 05:02:31 +04:00
|
|
|
|
|
|
|
if (mNodeStreamPort) {
|
|
|
|
mNodeStreamPort->Destroy();
|
|
|
|
mNodeStreamPort = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStream) {
|
|
|
|
mStream->Destroy();
|
|
|
|
mStream = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaStream*
|
|
|
|
AudioParam::Stream()
|
|
|
|
{
|
|
|
|
if (mStream) {
|
|
|
|
return mStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioNodeEngine* engine = new AudioNodeEngine(nullptr);
|
2013-05-24 21:09:29 +04:00
|
|
|
nsRefPtr<AudioNodeStream> stream =
|
|
|
|
mNode->Context()->Graph()->CreateAudioNodeStream(engine,
|
|
|
|
MediaStreamGraph::INTERNAL_STREAM,
|
|
|
|
Node()->Context()->SampleRate());
|
2013-05-02 05:02:31 +04:00
|
|
|
|
|
|
|
// Force the input to have only one channel, and make it down-mix using
|
|
|
|
// the speaker rules if needed.
|
|
|
|
stream->SetChannelMixingParametersImpl(1, ChannelCountMode::Explicit, ChannelInterpretation::Speakers);
|
|
|
|
// Mark as an AudioParam helper stream
|
|
|
|
stream->SetAudioParamHelperStream();
|
|
|
|
|
|
|
|
mStream = stream.forget();
|
|
|
|
|
|
|
|
// Setup the AudioParam's stream as an input to the owner AudioNode's stream
|
|
|
|
MediaStream* nodeStream = mNode->Stream();
|
|
|
|
MOZ_ASSERT(nodeStream->AsProcessedStream());
|
|
|
|
ProcessedMediaStream* ps = static_cast<ProcessedMediaStream*>(nodeStream);
|
|
|
|
mNodeStreamPort = ps->AllocateInputPort(mStream, MediaInputPort::FLAG_BLOCK_INPUT);
|
|
|
|
|
|
|
|
// Let the MSG's copy of AudioParamTimeline know about the change in the stream
|
|
|
|
mCallback(mNode);
|
|
|
|
|
|
|
|
return mStream;
|
2013-05-02 02:59:02 +04:00
|
|
|
}
|
|
|
|
|
2013-05-02 07:12:59 +04:00
|
|
|
float
|
|
|
|
AudioParamTimeline::AudioNodeInputValue(size_t aCounter) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mStream);
|
|
|
|
|
|
|
|
// If we have a chunk produced by the AudioNode inputs to the AudioParam,
|
|
|
|
// get its value now. We use aCounter to tell us which frame of the last
|
|
|
|
// AudioChunk to look at.
|
|
|
|
float audioNodeInputValue = 0.0f;
|
|
|
|
const AudioChunk& lastAudioNodeChunk =
|
2013-05-05 19:48:45 +04:00
|
|
|
static_cast<AudioNodeStream*>(mStream.get())->LastChunks()[0];
|
2013-05-02 07:12:59 +04:00
|
|
|
if (!lastAudioNodeChunk.IsNull()) {
|
|
|
|
MOZ_ASSERT(lastAudioNodeChunk.GetDuration() == WEBAUDIO_BLOCK_SIZE);
|
|
|
|
audioNodeInputValue =
|
|
|
|
static_cast<const float*>(lastAudioNodeChunk.mChannelData[0])[aCounter];
|
|
|
|
}
|
|
|
|
|
|
|
|
return audioNodeInputValue;
|
|
|
|
}
|
|
|
|
|
2012-10-17 05:19:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|