Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/webaudio/ChannelMergerNode.cpp
Line
Count
Source (jump to first uncovered line)
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 "mozilla/dom/ChannelMergerNode.h"
8
#include "mozilla/dom/ChannelMergerNodeBinding.h"
9
#include "AudioNodeEngine.h"
10
#include "AudioNodeStream.h"
11
12
namespace mozilla {
13
namespace dom {
14
15
class ChannelMergerNodeEngine final : public AudioNodeEngine
16
{
17
public:
18
  explicit ChannelMergerNodeEngine(ChannelMergerNode* aNode)
19
    : AudioNodeEngine(aNode)
20
0
  {
21
0
    MOZ_ASSERT(NS_IsMainThread());
22
0
  }
23
24
  void ProcessBlocksOnPorts(AudioNodeStream* aStream,
25
                            const OutputChunks& aInput,
26
                            OutputChunks& aOutput,
27
                            bool* aFinished) override
28
0
  {
29
0
    MOZ_ASSERT(aInput.Length() >= 1, "Should have one or more input ports");
30
0
31
0
    // Get the number of output channels, and allocate it
32
0
    size_t channelCount = InputCount();
33
0
    bool allNull = true;
34
0
    for (size_t i = 0; i < channelCount; ++i) {
35
0
      allNull &= aInput[i].IsNull();
36
0
    }
37
0
    if (allNull) {
38
0
      aOutput[0].SetNull(WEBAUDIO_BLOCK_SIZE);
39
0
      return;
40
0
    }
41
0
42
0
    aOutput[0].AllocateChannels(channelCount);
43
0
44
0
    for (size_t i = 0; i < channelCount; ++i) {
45
0
      float* output = aOutput[0].ChannelFloatsForWrite(i);
46
0
      if (aInput[i].IsNull()) {
47
0
        PodZero(output, WEBAUDIO_BLOCK_SIZE);
48
0
      } else {
49
0
        AudioBlockCopyChannelWithScale(
50
0
          static_cast<const float*>(aInput[i].mChannelData[0]),
51
0
          aInput[i].mVolume, output);
52
0
      }
53
0
    }
54
0
  }
55
56
  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
57
0
  {
58
0
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
59
0
  }
60
};
61
62
ChannelMergerNode::ChannelMergerNode(AudioContext* aContext,
63
                                     uint16_t aInputCount)
64
  : AudioNode(aContext,
65
              1,
66
              ChannelCountMode::Explicit,
67
              ChannelInterpretation::Speakers)
68
  , mInputCount(aInputCount)
69
0
{
70
0
  mStream = AudioNodeStream::Create(aContext,
71
0
                                    new ChannelMergerNodeEngine(this),
72
0
                                    AudioNodeStream::NO_STREAM_FLAGS,
73
0
                                    aContext->Graph());
74
0
}
75
76
/* static */ already_AddRefed<ChannelMergerNode>
77
ChannelMergerNode::Create(AudioContext& aAudioContext,
78
                          const ChannelMergerOptions& aOptions,
79
                          ErrorResult& aRv)
80
0
{
81
0
  if (aAudioContext.CheckClosed(aRv)) {
82
0
    return nullptr;
83
0
  }
84
0
85
0
  if (aOptions.mNumberOfInputs == 0 ||
86
0
      aOptions.mNumberOfInputs > WebAudioUtils::MaxChannelCount) {
87
0
    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
88
0
    return nullptr;
89
0
  }
90
0
91
0
  RefPtr<ChannelMergerNode> audioNode =
92
0
    new ChannelMergerNode(&aAudioContext, aOptions.mNumberOfInputs);
93
0
94
0
  audioNode->Initialize(aOptions, aRv);
95
0
  if (NS_WARN_IF(aRv.Failed())) {
96
0
    return nullptr;
97
0
  }
98
0
99
0
  return audioNode.forget();
100
0
}
101
102
JSObject*
103
ChannelMergerNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
104
0
{
105
0
  return ChannelMergerNode_Binding::Wrap(aCx, this, aGivenProto);
106
0
}
107
108
} // namespace dom
109
} // namespace mozilla