Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "AudioParam.h"
8 : #include "mozilla/dom/AudioParamBinding.h"
9 : #include "AudioNodeEngine.h"
10 : #include "AudioNodeStream.h"
11 : #include "AudioContext.h"
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 : NS_IMPL_CYCLE_COLLECTION_CLASS(AudioParam)
17 :
18 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AudioParam)
19 0 : tmp->DisconnectFromGraphAndDestroyStream();
20 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK(mNode)
21 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
22 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
23 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AudioParam)
24 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNode)
25 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
26 :
27 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(AudioParam)
28 :
29 0 : NS_IMPL_CYCLE_COLLECTING_NATIVE_ADDREF(AudioParam)
30 0 : NS_IMPL_CYCLE_COLLECTING_NATIVE_RELEASE(AudioParam)
31 :
32 0 : NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(AudioParam, AddRef)
33 0 : NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(AudioParam, Release)
34 :
35 0 : AudioParam::AudioParam(AudioNode* aNode,
36 : uint32_t aIndex,
37 : const char* aName,
38 : float aDefaultValue,
39 : float aMinValue,
40 0 : float aMaxValue)
41 : : AudioParamTimeline(aDefaultValue)
42 : , mNode(aNode)
43 : , mName(aName)
44 : , mIndex(aIndex)
45 : , mDefaultValue(aDefaultValue)
46 : , mMinValue(aMinValue)
47 0 : , mMaxValue(aMaxValue)
48 : {
49 0 : }
50 :
51 0 : AudioParam::~AudioParam()
52 : {
53 0 : DisconnectFromGraphAndDestroyStream();
54 0 : }
55 :
56 : JSObject*
57 0 : AudioParam::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
58 : {
59 0 : return AudioParamBinding::Wrap(aCx, this, aGivenProto);
60 : }
61 :
62 : void
63 0 : AudioParam::DisconnectFromGraphAndDestroyStream()
64 : {
65 0 : MOZ_ASSERT(mRefCnt.get() > mInputNodes.Length(),
66 : "Caller should be holding a reference or have called "
67 : "mRefCnt.stabilizeForDeletion()");
68 :
69 0 : while (!mInputNodes.IsEmpty()) {
70 0 : uint32_t i = mInputNodes.Length() - 1;
71 0 : RefPtr<AudioNode> input = mInputNodes[i].mInputNode;
72 0 : mInputNodes.RemoveElementAt(i);
73 0 : input->RemoveOutputParam(this);
74 : }
75 :
76 0 : if (mNodeStreamPort) {
77 0 : mNodeStreamPort->Destroy();
78 0 : mNodeStreamPort = nullptr;
79 : }
80 :
81 0 : if (mStream) {
82 0 : mStream->Destroy();
83 0 : mStream = nullptr;
84 : }
85 0 : }
86 :
87 : MediaStream*
88 0 : AudioParam::Stream()
89 : {
90 0 : if (mStream) {
91 0 : return mStream;
92 : }
93 :
94 0 : AudioNodeEngine* engine = new AudioNodeEngine(nullptr);
95 : RefPtr<AudioNodeStream> stream =
96 0 : AudioNodeStream::Create(mNode->Context(), engine,
97 : AudioNodeStream::NO_STREAM_FLAGS,
98 0 : mNode->Context()->Graph());
99 :
100 : // Force the input to have only one channel, and make it down-mix using
101 : // the speaker rules if needed.
102 0 : stream->SetChannelMixingParametersImpl(1, ChannelCountMode::Explicit, ChannelInterpretation::Speakers);
103 : // Mark as an AudioParam helper stream
104 0 : stream->SetAudioParamHelperStream();
105 :
106 0 : mStream = stream.forget();
107 :
108 : // Setup the AudioParam's stream as an input to the owner AudioNode's stream
109 0 : AudioNodeStream* nodeStream = mNode->GetStream();
110 0 : if (nodeStream) {
111 : mNodeStreamPort =
112 0 : nodeStream->AllocateInputPort(mStream, AudioNodeStream::AUDIO_TRACK);
113 : }
114 :
115 : // Send the stream to the timeline on the MSG side.
116 0 : AudioTimelineEvent event(mStream);
117 0 : SendEventToEngine(event);
118 :
119 0 : return mStream;
120 : }
121 :
122 : static const char*
123 0 : ToString(AudioTimelineEvent::Type aType)
124 : {
125 0 : switch (aType) {
126 : case AudioTimelineEvent::SetValue:
127 0 : return "SetValue";
128 : case AudioTimelineEvent::SetValueAtTime:
129 0 : return "SetValueAtTime";
130 : case AudioTimelineEvent::LinearRamp:
131 0 : return "LinearRamp";
132 : case AudioTimelineEvent::ExponentialRamp:
133 0 : return "ExponentialRamp";
134 : case AudioTimelineEvent::SetTarget:
135 0 : return "SetTarget";
136 : case AudioTimelineEvent::SetValueCurve:
137 0 : return "SetValueCurve";
138 : case AudioTimelineEvent::Stream:
139 0 : return "Stream";
140 : case AudioTimelineEvent::Cancel:
141 0 : return "Cancel";
142 : default:
143 0 : return "unknown AudioTimelineEvent";
144 : }
145 : }
146 :
147 : void
148 0 : AudioParam::SendEventToEngine(const AudioTimelineEvent& aEvent)
149 : {
150 0 : WEB_AUDIO_API_LOG("%f: %s for %u %s %s=%g time=%f %s=%g",
151 : GetParentObject()->CurrentTime(),
152 : mName, ParentNodeId(), ToString(aEvent.mType),
153 : aEvent.mType == AudioTimelineEvent::SetValueCurve ?
154 : "length" : "value",
155 : aEvent.mType == AudioTimelineEvent::SetValueCurve ?
156 : static_cast<double>(aEvent.mCurveLength) :
157 : static_cast<double>(aEvent.mValue),
158 : aEvent.Time<double>(),
159 : aEvent.mType == AudioTimelineEvent::SetValueCurve ?
160 : "duration" : "constant",
161 : aEvent.mType == AudioTimelineEvent::SetValueCurve ?
162 : aEvent.mDuration : aEvent.mTimeConstant);
163 :
164 0 : AudioNodeStream* stream = mNode->GetStream();
165 0 : if (stream) {
166 0 : stream->SendTimelineEvent(mIndex, aEvent);
167 : }
168 0 : }
169 :
170 : void
171 0 : AudioParam::CleanupOldEvents()
172 : {
173 0 : MOZ_ASSERT(NS_IsMainThread());
174 0 : double currentTime = mNode->Context()->CurrentTime();
175 :
176 0 : CleanupEventsOlderThan(currentTime);
177 0 : }
178 :
179 : float
180 0 : AudioParamTimeline::AudioNodeInputValue(size_t aCounter) const
181 : {
182 0 : MOZ_ASSERT(mStream);
183 :
184 : // If we have a chunk produced by the AudioNode inputs to the AudioParam,
185 : // get its value now. We use aCounter to tell us which frame of the last
186 : // AudioChunk to look at.
187 0 : float audioNodeInputValue = 0.0f;
188 : const AudioBlock& lastAudioNodeChunk =
189 0 : static_cast<AudioNodeStream*>(mStream.get())->LastChunks()[0];
190 0 : if (!lastAudioNodeChunk.IsNull()) {
191 0 : MOZ_ASSERT(lastAudioNodeChunk.GetDuration() == WEBAUDIO_BLOCK_SIZE);
192 0 : audioNodeInputValue =
193 0 : static_cast<const float*>(lastAudioNodeChunk.mChannelData[0])[aCounter];
194 0 : audioNodeInputValue *= lastAudioNodeChunk.mVolume;
195 : }
196 :
197 0 : return audioNodeInputValue;
198 : }
199 :
200 : } // namespace dom
201 : } // namespace mozilla
202 :
|