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 "AudioNode.h"
8 #include "mozilla/ErrorResult.h"
9 #include "AudioNodeTrack.h"
10 #include "AudioNodeEngine.h"
11 #include "mozilla/dom/AudioParam.h"
12 #include "mozilla/Services.h"
13 #include "nsIObserverService.h"
14
15 namespace mozilla {
16 namespace dom {
17
18 static const uint32_t INVALID_PORT = 0xffffffff;
19 static uint32_t gId = 0;
20
21 NS_IMPL_CYCLE_COLLECTION_CLASS(AudioNode)
22
23 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(AudioNode, DOMEventTargetHelper)
24 tmp->DisconnectFromGraph();
25 if (tmp->mContext) {
26 tmp->mContext->UnregisterNode(tmp);
27 }
28 NS_IMPL_CYCLE_COLLECTION_UNLINK(mContext)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mParams)29 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParams)
30 NS_IMPL_CYCLE_COLLECTION_UNLINK(mOutputNodes)
31 NS_IMPL_CYCLE_COLLECTION_UNLINK(mOutputParams)
32 NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
33 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
34 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(AudioNode,
35 DOMEventTargetHelper)
36 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContext)
37 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParams)
38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOutputNodes)
39 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOutputParams)
40 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
41
42 NS_IMPL_ADDREF_INHERITED(AudioNode, DOMEventTargetHelper)
43 NS_IMPL_RELEASE_INHERITED(AudioNode, DOMEventTargetHelper)
44
45 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AudioNode)
46 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
47 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
48
49 AudioNode::AudioNode(AudioContext* aContext, uint32_t aChannelCount,
50 ChannelCountMode aChannelCountMode,
51 ChannelInterpretation aChannelInterpretation)
52 : DOMEventTargetHelper(aContext->GetParentObject()),
53 mContext(aContext),
54 mChannelCount(aChannelCount),
55 mChannelCountMode(aChannelCountMode),
56 mChannelInterpretation(aChannelInterpretation),
57 mId(gId++),
58 mPassThrough(false),
59 mAbstractMainThread(
60 aContext->GetOwnerGlobal()
61 ? aContext->GetOwnerGlobal()->AbstractMainThreadFor(
62 TaskCategory::Other)
63 : nullptr) {
64 MOZ_ASSERT(aContext);
65 aContext->RegisterNode(this);
66 }
67
~AudioNode()68 AudioNode::~AudioNode() {
69 MOZ_ASSERT(mInputNodes.IsEmpty());
70 MOZ_ASSERT(mOutputNodes.IsEmpty());
71 MOZ_ASSERT(mOutputParams.IsEmpty());
72 MOZ_ASSERT(!mTrack,
73 "The webaudio-node-demise notification must have been sent");
74 if (mContext) {
75 mContext->UnregisterNode(this);
76 }
77 }
78
Initialize(const AudioNodeOptions & aOptions,ErrorResult & aRv)79 void AudioNode::Initialize(const AudioNodeOptions& aOptions, ErrorResult& aRv) {
80 if (aOptions.mChannelCount.WasPassed()) {
81 SetChannelCount(aOptions.mChannelCount.Value(), aRv);
82 if (NS_WARN_IF(aRv.Failed())) {
83 return;
84 }
85 }
86
87 if (aOptions.mChannelCountMode.WasPassed()) {
88 SetChannelCountModeValue(aOptions.mChannelCountMode.Value(), aRv);
89 if (NS_WARN_IF(aRv.Failed())) {
90 return;
91 }
92 }
93
94 if (aOptions.mChannelInterpretation.WasPassed()) {
95 SetChannelInterpretationValue(aOptions.mChannelInterpretation.Value(), aRv);
96 if (NS_WARN_IF(aRv.Failed())) {
97 return;
98 }
99 }
100 }
101
SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const102 size_t AudioNode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
103 // Not owned:
104 // - mContext
105 // - mTrack
106 size_t amount = 0;
107
108 amount += mInputNodes.ShallowSizeOfExcludingThis(aMallocSizeOf);
109 for (size_t i = 0; i < mInputNodes.Length(); i++) {
110 amount += mInputNodes[i].SizeOfExcludingThis(aMallocSizeOf);
111 }
112
113 // Just measure the array. The entire audio node graph is measured via the
114 // MediaTrackGraph's tracks, so we don't want to double-count the elements.
115 amount += mOutputNodes.ShallowSizeOfExcludingThis(aMallocSizeOf);
116
117 amount += mOutputParams.ShallowSizeOfExcludingThis(aMallocSizeOf);
118 for (size_t i = 0; i < mOutputParams.Length(); i++) {
119 amount += mOutputParams[i]->SizeOfIncludingThis(aMallocSizeOf);
120 }
121
122 return amount;
123 }
124
SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const125 size_t AudioNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
126 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
127 }
128
129 template <class InputNode>
FindIndexOfNode(const nsTArray<InputNode> & aInputNodes,const AudioNode * aNode)130 static size_t FindIndexOfNode(const nsTArray<InputNode>& aInputNodes,
131 const AudioNode* aNode) {
132 for (size_t i = 0; i < aInputNodes.Length(); ++i) {
133 if (aInputNodes[i].mInputNode == aNode) {
134 return i;
135 }
136 }
137 return nsTArray<InputNode>::NoIndex;
138 }
139
140 template <class InputNode>
FindIndexOfNodeWithPorts(const nsTArray<InputNode> & aInputNodes,const AudioNode * aNode,uint32_t aInputPort,uint32_t aOutputPort)141 static size_t FindIndexOfNodeWithPorts(const nsTArray<InputNode>& aInputNodes,
142 const AudioNode* aNode,
143 uint32_t aInputPort,
144 uint32_t aOutputPort) {
145 for (size_t i = 0; i < aInputNodes.Length(); ++i) {
146 if (aInputNodes[i].mInputNode == aNode &&
147 aInputNodes[i].mInputPort == aInputPort &&
148 aInputNodes[i].mOutputPort == aOutputPort) {
149 return i;
150 }
151 }
152 return nsTArray<InputNode>::NoIndex;
153 }
154
DisconnectFromGraph()155 void AudioNode::DisconnectFromGraph() {
156 MOZ_ASSERT(mRefCnt.get() > mInputNodes.Length(),
157 "Caller should be holding a reference");
158
159 // The idea here is that we remove connections one by one, and at each step
160 // the graph is in a valid state.
161
162 // Disconnect inputs. We don't need them anymore.
163 while (!mInputNodes.IsEmpty()) {
164 size_t i = mInputNodes.Length() - 1;
165 RefPtr<AudioNode> input = mInputNodes[i].mInputNode;
166 mInputNodes.RemoveElementAt(i);
167 input->mOutputNodes.RemoveElement(this);
168 }
169
170 while (!mOutputNodes.IsEmpty()) {
171 size_t i = mOutputNodes.Length() - 1;
172 RefPtr<AudioNode> output = std::move(mOutputNodes[i]);
173 mOutputNodes.RemoveElementAt(i);
174 size_t inputIndex = FindIndexOfNode(output->mInputNodes, this);
175 // It doesn't matter which one we remove, since we're going to remove all
176 // entries for this node anyway.
177 output->mInputNodes.RemoveElementAt(inputIndex);
178 // This effects of this connection will remain.
179 output->NotifyHasPhantomInput();
180 }
181
182 while (!mOutputParams.IsEmpty()) {
183 size_t i = mOutputParams.Length() - 1;
184 RefPtr<AudioParam> output = std::move(mOutputParams[i]);
185 mOutputParams.RemoveElementAt(i);
186 size_t inputIndex = FindIndexOfNode(output->InputNodes(), this);
187 // It doesn't matter which one we remove, since we're going to remove all
188 // entries for this node anyway.
189 output->RemoveInputNode(inputIndex);
190 }
191
192 DestroyMediaTrack();
193 }
194
Connect(AudioNode & aDestination,uint32_t aOutput,uint32_t aInput,ErrorResult & aRv)195 AudioNode* AudioNode::Connect(AudioNode& aDestination, uint32_t aOutput,
196 uint32_t aInput, ErrorResult& aRv) {
197 if (aOutput >= NumberOfOutputs()) {
198 aRv.ThrowIndexSizeError(
199 nsPrintfCString("Output index %u is out of bounds", aOutput));
200 return nullptr;
201 }
202
203 if (aInput >= aDestination.NumberOfInputs()) {
204 aRv.ThrowIndexSizeError(
205 nsPrintfCString("Input index %u is out of bounds", aInput));
206 return nullptr;
207 }
208
209 if (Context() != aDestination.Context()) {
210 aRv.ThrowInvalidAccessError(
211 "Can't connect nodes from different AudioContexts");
212 return nullptr;
213 }
214
215 if (FindIndexOfNodeWithPorts(aDestination.mInputNodes, this, aInput,
216 aOutput) !=
217 nsTArray<AudioNode::InputNode>::NoIndex) {
218 // connection already exists.
219 return &aDestination;
220 }
221
222 WEB_AUDIO_API_LOG("%f: %s %u Connect() to %s %u", Context()->CurrentTime(),
223 NodeType(), Id(), aDestination.NodeType(),
224 aDestination.Id());
225
226 // The MediaTrackGraph will handle cycle detection. We don't need to do it
227 // here.
228
229 mOutputNodes.AppendElement(&aDestination);
230 InputNode* input = aDestination.mInputNodes.AppendElement();
231 input->mInputNode = this;
232 input->mInputPort = aInput;
233 input->mOutputPort = aOutput;
234 AudioNodeTrack* destinationTrack = aDestination.mTrack;
235 if (mTrack && destinationTrack) {
236 // Connect tracks in the MediaTrackGraph
237 MOZ_ASSERT(aInput <= UINT16_MAX, "Unexpected large input port number");
238 MOZ_ASSERT(aOutput <= UINT16_MAX, "Unexpected large output port number");
239 input->mTrackPort = destinationTrack->AllocateInputPort(
240 mTrack, static_cast<uint16_t>(aInput), static_cast<uint16_t>(aOutput));
241 }
242 aDestination.NotifyInputsChanged();
243
244 return &aDestination;
245 }
246
Connect(AudioParam & aDestination,uint32_t aOutput,ErrorResult & aRv)247 void AudioNode::Connect(AudioParam& aDestination, uint32_t aOutput,
248 ErrorResult& aRv) {
249 if (aOutput >= NumberOfOutputs()) {
250 aRv.ThrowIndexSizeError(
251 nsPrintfCString("Output index %u is out of bounds", aOutput));
252 return;
253 }
254
255 if (Context() != aDestination.GetParentObject()) {
256 aRv.ThrowInvalidAccessError(
257 "Can't connect a node to an AudioParam from a different AudioContext");
258 return;
259 }
260
261 if (FindIndexOfNodeWithPorts(aDestination.InputNodes(), this, INVALID_PORT,
262 aOutput) !=
263 nsTArray<AudioNode::InputNode>::NoIndex) {
264 // connection already exists.
265 return;
266 }
267
268 mOutputParams.AppendElement(&aDestination);
269 InputNode* input = aDestination.AppendInputNode();
270 input->mInputNode = this;
271 input->mInputPort = INVALID_PORT;
272 input->mOutputPort = aOutput;
273
274 mozilla::MediaTrack* track = aDestination.Track();
275 MOZ_ASSERT(track->AsProcessedTrack());
276 ProcessedMediaTrack* ps = static_cast<ProcessedMediaTrack*>(track);
277 if (mTrack) {
278 // Setup our track as an input to the AudioParam's track
279 MOZ_ASSERT(aOutput <= UINT16_MAX, "Unexpected large output port number");
280 input->mTrackPort =
281 ps->AllocateInputPort(mTrack, 0, static_cast<uint16_t>(aOutput));
282 }
283 }
284
SendDoubleParameterToTrack(uint32_t aIndex,double aValue)285 void AudioNode::SendDoubleParameterToTrack(uint32_t aIndex, double aValue) {
286 MOZ_ASSERT(mTrack, "How come we don't have a track here?");
287 mTrack->SetDoubleParameter(aIndex, aValue);
288 }
289
SendInt32ParameterToTrack(uint32_t aIndex,int32_t aValue)290 void AudioNode::SendInt32ParameterToTrack(uint32_t aIndex, int32_t aValue) {
291 MOZ_ASSERT(mTrack, "How come we don't have a track here?");
292 mTrack->SetInt32Parameter(aIndex, aValue);
293 }
294
SendChannelMixingParametersToTrack()295 void AudioNode::SendChannelMixingParametersToTrack() {
296 if (mTrack) {
297 mTrack->SetChannelMixingParameters(mChannelCount, mChannelCountMode,
298 mChannelInterpretation);
299 }
300 }
301
302 template <>
DisconnectFromOutputIfConnected(uint32_t aOutputNodeIndex,uint32_t aInputIndex)303 bool AudioNode::DisconnectFromOutputIfConnected<AudioNode>(
304 uint32_t aOutputNodeIndex, uint32_t aInputIndex) {
305 WEB_AUDIO_API_LOG("%f: %s %u Disconnect()", Context()->CurrentTime(),
306 NodeType(), Id());
307
308 AudioNode* destination = mOutputNodes[aOutputNodeIndex];
309
310 MOZ_ASSERT(aOutputNodeIndex < mOutputNodes.Length());
311 MOZ_ASSERT(aInputIndex < destination->InputNodes().Length());
312
313 // An upstream node may be starting to play on the graph thread, and the
314 // engine for a downstream node may be sending a PlayingRefChangeHandler
315 // ADDREF message to this (main) thread. Wait for a round trip before
316 // releasing nodes, to give engines receiving sound now time to keep their
317 // nodes alive.
318 class RunnableRelease final : public Runnable {
319 public:
320 explicit RunnableRelease(already_AddRefed<AudioNode> aNode)
321 : mozilla::Runnable("RunnableRelease"), mNode(aNode) {}
322
323 NS_IMETHOD Run() override {
324 mNode = nullptr;
325 return NS_OK;
326 }
327
328 private:
329 RefPtr<AudioNode> mNode;
330 };
331
332 InputNode& input = destination->mInputNodes[aInputIndex];
333 if (input.mInputNode != this) {
334 return false;
335 }
336
337 // Remove one instance of 'dest' from mOutputNodes. There could be
338 // others, and it's not correct to remove them all since some of them
339 // could be for different output ports.
340 RefPtr<AudioNode> output = std::move(mOutputNodes[aOutputNodeIndex]);
341 mOutputNodes.RemoveElementAt(aOutputNodeIndex);
342 // Destroying the InputNode here sends a message to the graph thread
343 // to disconnect the tracks, which should be sent before the
344 // RunAfterPendingUpdates() call below.
345 destination->mInputNodes.RemoveElementAt(aInputIndex);
346 output->NotifyInputsChanged();
347 if (mTrack) {
348 nsCOMPtr<nsIRunnable> runnable = new RunnableRelease(output.forget());
349 mTrack->RunAfterPendingUpdates(runnable.forget());
350 }
351 return true;
352 }
353
354 template <>
DisconnectFromOutputIfConnected(uint32_t aOutputParamIndex,uint32_t aInputIndex)355 bool AudioNode::DisconnectFromOutputIfConnected<AudioParam>(
356 uint32_t aOutputParamIndex, uint32_t aInputIndex) {
357 MOZ_ASSERT(aOutputParamIndex < mOutputParams.Length());
358
359 AudioParam* destination = mOutputParams[aOutputParamIndex];
360
361 MOZ_ASSERT(aInputIndex < destination->InputNodes().Length());
362
363 const InputNode& input = destination->InputNodes()[aInputIndex];
364 if (input.mInputNode != this) {
365 return false;
366 }
367 destination->RemoveInputNode(aInputIndex);
368 // Remove one instance of 'dest' from mOutputParams. There could be
369 // others, and it's not correct to remove them all since some of them
370 // could be for different output ports.
371 mOutputParams.RemoveElementAt(aOutputParamIndex);
372 return true;
373 }
374
375 template <>
376 const nsTArray<AudioNode::InputNode>&
InputsForDestination(uint32_t aOutputNodeIndex) const377 AudioNode::InputsForDestination<AudioNode>(uint32_t aOutputNodeIndex) const {
378 return mOutputNodes[aOutputNodeIndex]->InputNodes();
379 }
380
381 template <>
382 const nsTArray<AudioNode::InputNode>&
InputsForDestination(uint32_t aOutputNodeIndex) const383 AudioNode::InputsForDestination<AudioParam>(uint32_t aOutputNodeIndex) const {
384 return mOutputParams[aOutputNodeIndex]->InputNodes();
385 }
386
387 template <typename DestinationType, typename Predicate>
DisconnectMatchingDestinationInputs(uint32_t aDestinationIndex,Predicate aPredicate)388 bool AudioNode::DisconnectMatchingDestinationInputs(uint32_t aDestinationIndex,
389 Predicate aPredicate) {
390 bool wasConnected = false;
391 uint32_t inputCount =
392 InputsForDestination<DestinationType>(aDestinationIndex).Length();
393
394 for (int32_t inputIndex = inputCount - 1; inputIndex >= 0; --inputIndex) {
395 const InputNode& input =
396 InputsForDestination<DestinationType>(aDestinationIndex)[inputIndex];
397 if (aPredicate(input)) {
398 if (DisconnectFromOutputIfConnected<DestinationType>(aDestinationIndex,
399 inputIndex)) {
400 wasConnected = true;
401 break;
402 }
403 }
404 }
405 return wasConnected;
406 }
407
Disconnect(ErrorResult & aRv)408 void AudioNode::Disconnect(ErrorResult& aRv) {
409 for (int32_t outputIndex = mOutputNodes.Length() - 1; outputIndex >= 0;
410 --outputIndex) {
411 DisconnectMatchingDestinationInputs<AudioNode>(
412 outputIndex, [](const InputNode&) { return true; });
413 }
414
415 for (int32_t outputIndex = mOutputParams.Length() - 1; outputIndex >= 0;
416 --outputIndex) {
417 DisconnectMatchingDestinationInputs<AudioParam>(
418 outputIndex, [](const InputNode&) { return true; });
419 }
420 }
421
Disconnect(uint32_t aOutput,ErrorResult & aRv)422 void AudioNode::Disconnect(uint32_t aOutput, ErrorResult& aRv) {
423 if (aOutput >= NumberOfOutputs()) {
424 aRv.ThrowIndexSizeError(
425 nsPrintfCString("Output index %u is out of bounds", aOutput));
426 return;
427 }
428
429 for (int32_t outputIndex = mOutputNodes.Length() - 1; outputIndex >= 0;
430 --outputIndex) {
431 DisconnectMatchingDestinationInputs<AudioNode>(
432 outputIndex, [aOutput](const InputNode& aInputNode) {
433 return aInputNode.mOutputPort == aOutput;
434 });
435 }
436
437 for (int32_t outputIndex = mOutputParams.Length() - 1; outputIndex >= 0;
438 --outputIndex) {
439 DisconnectMatchingDestinationInputs<AudioParam>(
440 outputIndex, [aOutput](const InputNode& aInputNode) {
441 return aInputNode.mOutputPort == aOutput;
442 });
443 }
444 }
445
Disconnect(AudioNode & aDestination,ErrorResult & aRv)446 void AudioNode::Disconnect(AudioNode& aDestination, ErrorResult& aRv) {
447 bool wasConnected = false;
448
449 for (int32_t outputIndex = mOutputNodes.Length() - 1; outputIndex >= 0;
450 --outputIndex) {
451 if (mOutputNodes[outputIndex] != &aDestination) {
452 continue;
453 }
454 wasConnected |= DisconnectMatchingDestinationInputs<AudioNode>(
455 outputIndex, [](const InputNode&) { return true; });
456 }
457
458 if (!wasConnected) {
459 aRv.ThrowInvalidAccessError(
460 "Trying to disconnect from a node we're not connected to");
461 return;
462 }
463 }
464
Disconnect(AudioNode & aDestination,uint32_t aOutput,ErrorResult & aRv)465 void AudioNode::Disconnect(AudioNode& aDestination, uint32_t aOutput,
466 ErrorResult& aRv) {
467 if (aOutput >= NumberOfOutputs()) {
468 aRv.ThrowIndexSizeError(
469 nsPrintfCString("Output index %u is out of bounds", aOutput));
470 return;
471 }
472
473 bool wasConnected = false;
474
475 for (int32_t outputIndex = mOutputNodes.Length() - 1; outputIndex >= 0;
476 --outputIndex) {
477 if (mOutputNodes[outputIndex] != &aDestination) {
478 continue;
479 }
480 wasConnected |= DisconnectMatchingDestinationInputs<AudioNode>(
481 outputIndex, [aOutput](const InputNode& aInputNode) {
482 return aInputNode.mOutputPort == aOutput;
483 });
484 }
485
486 if (!wasConnected) {
487 aRv.ThrowInvalidAccessError(
488 "Trying to disconnect from a node we're not connected to");
489 return;
490 }
491 }
492
Disconnect(AudioNode & aDestination,uint32_t aOutput,uint32_t aInput,ErrorResult & aRv)493 void AudioNode::Disconnect(AudioNode& aDestination, uint32_t aOutput,
494 uint32_t aInput, ErrorResult& aRv) {
495 if (aOutput >= NumberOfOutputs()) {
496 aRv.ThrowIndexSizeError(
497 nsPrintfCString("Output index %u is out of bounds", aOutput));
498 return;
499 }
500
501 if (aInput >= aDestination.NumberOfInputs()) {
502 aRv.ThrowIndexSizeError(
503 nsPrintfCString("Input index %u is out of bounds", aInput));
504 return;
505 }
506
507 bool wasConnected = false;
508
509 for (int32_t outputIndex = mOutputNodes.Length() - 1; outputIndex >= 0;
510 --outputIndex) {
511 if (mOutputNodes[outputIndex] != &aDestination) {
512 continue;
513 }
514 wasConnected |= DisconnectMatchingDestinationInputs<AudioNode>(
515 outputIndex, [aOutput, aInput](const InputNode& aInputNode) {
516 return aInputNode.mOutputPort == aOutput &&
517 aInputNode.mInputPort == aInput;
518 });
519 }
520
521 if (!wasConnected) {
522 aRv.ThrowInvalidAccessError(
523 "Trying to disconnect from a node we're not connected to");
524 return;
525 }
526 }
527
Disconnect(AudioParam & aDestination,ErrorResult & aRv)528 void AudioNode::Disconnect(AudioParam& aDestination, ErrorResult& aRv) {
529 bool wasConnected = false;
530
531 for (int32_t outputIndex = mOutputParams.Length() - 1; outputIndex >= 0;
532 --outputIndex) {
533 if (mOutputParams[outputIndex] != &aDestination) {
534 continue;
535 }
536 wasConnected |= DisconnectMatchingDestinationInputs<AudioParam>(
537 outputIndex, [](const InputNode&) { return true; });
538 }
539
540 if (!wasConnected) {
541 aRv.ThrowInvalidAccessError(
542 "Trying to disconnect from an AudioParam we're not connected to");
543 return;
544 }
545 }
546
Disconnect(AudioParam & aDestination,uint32_t aOutput,ErrorResult & aRv)547 void AudioNode::Disconnect(AudioParam& aDestination, uint32_t aOutput,
548 ErrorResult& aRv) {
549 if (aOutput >= NumberOfOutputs()) {
550 aRv.ThrowIndexSizeError(
551 nsPrintfCString("Output index %u is out of bounds", aOutput));
552 return;
553 }
554
555 bool wasConnected = false;
556
557 for (int32_t outputIndex = mOutputParams.Length() - 1; outputIndex >= 0;
558 --outputIndex) {
559 if (mOutputParams[outputIndex] != &aDestination) {
560 continue;
561 }
562 wasConnected |= DisconnectMatchingDestinationInputs<AudioParam>(
563 outputIndex, [aOutput](const InputNode& aInputNode) {
564 return aInputNode.mOutputPort == aOutput;
565 });
566 }
567
568 if (!wasConnected) {
569 aRv.ThrowInvalidAccessError(
570 "Trying to disconnect from an AudioParam we're not connected to");
571 return;
572 }
573 }
574
DestroyMediaTrack()575 void AudioNode::DestroyMediaTrack() {
576 if (mTrack) {
577 // Remove the node pointer on the engine.
578 AudioNodeTrack* ns = mTrack;
579 MOZ_ASSERT(ns, "How come we don't have a track here?");
580 MOZ_ASSERT(ns->Engine()->NodeMainThread() == this,
581 "Invalid node reference");
582 ns->Engine()->ClearNode();
583
584 mTrack->Destroy();
585 mTrack = nullptr;
586
587 nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
588 if (obs) {
589 nsAutoString id;
590 id.AppendPrintf("%u", mId);
591 obs->NotifyObservers(nullptr, "webaudio-node-demise", id.get());
592 }
593 }
594 }
595
RemoveOutputParam(AudioParam * aParam)596 void AudioNode::RemoveOutputParam(AudioParam* aParam) {
597 mOutputParams.RemoveElement(aParam);
598 }
599
PassThrough() const600 bool AudioNode::PassThrough() const {
601 MOZ_ASSERT(NumberOfInputs() <= 1 && NumberOfOutputs() == 1);
602 return mPassThrough;
603 }
604
SetPassThrough(bool aPassThrough)605 void AudioNode::SetPassThrough(bool aPassThrough) {
606 MOZ_ASSERT(NumberOfInputs() <= 1 && NumberOfOutputs() == 1);
607 mPassThrough = aPassThrough;
608 if (mTrack) {
609 mTrack->SetPassThrough(mPassThrough);
610 }
611 }
612
CreateAudioParam(RefPtr<AudioParam> & aParam,uint32_t aIndex,const char16_t * aName,float aDefaultValue,float aMinValue,float aMaxValue)613 void AudioNode::CreateAudioParam(RefPtr<AudioParam>& aParam, uint32_t aIndex,
614 const char16_t* aName, float aDefaultValue,
615 float aMinValue, float aMaxValue) {
616 aParam =
617 new AudioParam(this, aIndex, aName, aDefaultValue, aMinValue, aMaxValue);
618 mParams.AppendElement(aParam);
619 }
620
621 } // namespace dom
622 } // namespace mozilla
623