Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/webaudio/ChannelSplitterNode.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/ChannelSplitterNode.h"
8
#include "mozilla/dom/ChannelSplitterNodeBinding.h"
9
#include "AudioNodeEngine.h"
10
#include "AudioNodeStream.h"
11
12
namespace mozilla {
13
namespace dom {
14
15
class ChannelSplitterNodeEngine final : public AudioNodeEngine
16
{
17
public:
18
  explicit ChannelSplitterNodeEngine(ChannelSplitterNode* 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 only have one input port");
30
0
31
0
    aOutput.SetLength(OutputCount());
32
0
    for (uint16_t i = 0; i < OutputCount(); ++i) {
33
0
      if (i < aInput[0].ChannelCount()) {
34
0
        // Split out existing channels
35
0
        aOutput[i].AllocateChannels(1);
36
0
        AudioBlockCopyChannelWithScale(
37
0
            static_cast<const float*>(aInput[0].mChannelData[i]),
38
0
            aInput[0].mVolume,
39
0
            aOutput[i].ChannelFloatsForWrite(0));
40
0
      } else {
41
0
        // Pad with silent channels if needed
42
0
        aOutput[i].SetNull(WEBAUDIO_BLOCK_SIZE);
43
0
      }
44
0
    }
45
0
  }
46
47
  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
48
0
  {
49
0
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
50
0
  }
51
};
52
53
ChannelSplitterNode::ChannelSplitterNode(AudioContext* aContext,
54
                                         uint16_t aOutputCount)
55
  : AudioNode(aContext,
56
              aOutputCount,
57
              ChannelCountMode::Explicit,
58
              ChannelInterpretation::Discrete)
59
  , mOutputCount(aOutputCount)
60
0
{
61
0
  mStream = AudioNodeStream::Create(aContext,
62
0
                                    new ChannelSplitterNodeEngine(this),
63
0
                                    AudioNodeStream::NO_STREAM_FLAGS,
64
0
                                    aContext->Graph());
65
0
}
66
67
/* static */ already_AddRefed<ChannelSplitterNode>
68
ChannelSplitterNode::Create(AudioContext& aAudioContext,
69
                            const ChannelSplitterOptions& aOptions,
70
                            ErrorResult& aRv)
71
0
{
72
0
  if (aAudioContext.CheckClosed(aRv)) {
73
0
    return nullptr;
74
0
  }
75
0
76
0
  if (aOptions.mNumberOfOutputs == 0 ||
77
0
      aOptions.mNumberOfOutputs > WebAudioUtils::MaxChannelCount) {
78
0
    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
79
0
    return nullptr;
80
0
  }
81
0
82
0
  RefPtr<ChannelSplitterNode> audioNode =
83
0
    new ChannelSplitterNode(&aAudioContext, aOptions.mNumberOfOutputs);
84
0
85
0
  // Manually check that the other options are valid, this node has channelCount,
86
0
  // channelCountMode and channelInterpretation constraints: they cannot be
87
0
  // changed from the default.
88
0
  if (aOptions.mChannelCount.WasPassed() &&
89
0
      aOptions.mChannelCount.Value() != audioNode->ChannelCount()) {
90
0
    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
91
0
    return nullptr;
92
0
  }
93
0
  if (aOptions.mChannelInterpretation.WasPassed() &&
94
0
      aOptions.mChannelInterpretation.Value() != audioNode->ChannelInterpretationValue()) {
95
0
    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
96
0
    return nullptr;
97
0
  }
98
0
  if (aOptions.mChannelCountMode.WasPassed() &&
99
0
      aOptions.mChannelCountMode.Value() != audioNode->ChannelCountModeValue()) {
100
0
    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
101
0
    return nullptr;
102
0
  }
103
0
104
0
  return audioNode.forget();
105
0
}
106
107
JSObject*
108
ChannelSplitterNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
109
0
{
110
0
  return ChannelSplitterNode_Binding::Wrap(aCx, this, aGivenProto);
111
0
}
112
113
} // namespace dom
114
} // namespace mozilla