Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/webaudio/AudioParamTimeline.h
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
#ifndef AudioParamTimeline_h_
8
#define AudioParamTimeline_h_
9
10
#include "AudioEventTimeline.h"
11
#include "mozilla/ErrorResult.h"
12
#include "MediaStreamGraph.h"
13
#include "AudioSegment.h"
14
15
namespace mozilla {
16
17
namespace dom {
18
19
// This helper class is used to represent the part of the AudioParam
20
// class that gets sent to AudioNodeEngine instances.  In addition to
21
// AudioEventTimeline methods, it holds a pointer to an optional
22
// MediaStream which represents the AudioNode inputs to the AudioParam.
23
// This MediaStream is managed by the AudioParam subclass on the main
24
// thread, and can only be obtained from the AudioNodeEngine instances
25
// consuming this class.
26
class AudioParamTimeline : public AudioEventTimeline
27
{
28
  typedef AudioEventTimeline BaseClass;
29
30
public:
31
  explicit AudioParamTimeline(float aDefaultValue)
32
    : BaseClass(aDefaultValue)
33
0
  {
34
0
  }
35
36
  MediaStream* Stream() const
37
0
  {
38
0
    return mStream;
39
0
  }
40
41
  bool HasSimpleValue() const
42
0
  {
43
0
    return BaseClass::HasSimpleValue() && !mStream;
44
0
  }
45
46
  template<class TimeType>
47
  float GetValueAtTime(TimeType aTime)
48
0
  {
49
0
    return GetValueAtTime(aTime, 0);
50
0
  }
Unexecuted instantiation: float mozilla::dom::AudioParamTimeline::GetValueAtTime<long>(long)
Unexecuted instantiation: float mozilla::dom::AudioParamTimeline::GetValueAtTime<double>(double)
51
52
  template<typename TimeType>
53
  void InsertEvent(const AudioTimelineEvent& aEvent)
54
0
  {
55
0
    if (aEvent.mType == AudioTimelineEvent::Cancel) {
56
0
      CancelScheduledValues(aEvent.Time<TimeType>());
57
0
      return;
58
0
    }
59
0
    if (aEvent.mType == AudioTimelineEvent::Stream) {
60
0
      mStream = aEvent.mStream;
61
0
      return;
62
0
    }
63
0
    if (aEvent.mType == AudioTimelineEvent::SetValue) {
64
0
      AudioEventTimeline::SetValue(aEvent.mValue);
65
0
      return;
66
0
    }
67
0
    AudioEventTimeline::InsertEvent<TimeType>(aEvent);
68
0
  }
69
70
  // Get the value of the AudioParam at time aTime + aCounter.
71
  // aCounter here is an offset to aTime if we try to get the value in ticks,
72
  // otherwise it should always be zero.  aCounter is meant to be used when
73
  template<class TimeType>
74
  float GetValueAtTime(TimeType aTime, size_t aCounter);
75
76
  // Get the values of the AudioParam at time aTime + (0 to aSize).
77
  // aBuffer must have the correct aSize.
78
  // aSize here is an offset to aTime if we try to get the value in ticks,
79
  // otherwise it should always be zero.  aSize is meant to be used when
80
  // getting the value of an a-rate AudioParam for each tick inside an
81
  // AudioNodeEngine implementation.
82
  template<class TimeType>
83
  void GetValuesAtTime(TimeType aTime, float* aBuffer, const size_t aSize);
84
85
  virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
86
0
  {
87
0
    return mStream ? mStream->SizeOfIncludingThis(aMallocSizeOf) : 0;
88
0
  }
89
90
  virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
91
0
  {
92
0
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
93
0
  }
94
95
96
private:
97
  float AudioNodeInputValue(size_t aCounter) const;
98
99
protected:
100
  // This is created lazily when needed.
101
  RefPtr<MediaStream> mStream;
102
};
103
104
template<> inline float
105
AudioParamTimeline::GetValueAtTime(double aTime, size_t aCounter)
106
0
{
107
0
  MOZ_ASSERT(!aCounter);
108
0
109
0
  // Getting an AudioParam value on an AudioNode does not consider input from
110
0
  // other AudioNodes, which is managed only on the graph thread.
111
0
  return BaseClass::GetValueAtTime(aTime);
112
0
}
113
114
template<> inline float
115
AudioParamTimeline::GetValueAtTime(int64_t aTime, size_t aCounter)
116
0
{
117
0
  MOZ_ASSERT(aCounter < WEBAUDIO_BLOCK_SIZE);
118
0
  MOZ_ASSERT(!aCounter || !HasSimpleValue());
119
0
120
0
  // Mix the value of the AudioParam itself with that of the AudioNode inputs.
121
0
  return BaseClass::GetValueAtTime(static_cast<int64_t>(aTime + aCounter)) +
122
0
    (mStream ? AudioNodeInputValue(aCounter) : 0.0f);
123
0
}
124
125
template<> inline void
126
AudioParamTimeline::GetValuesAtTime(double aTime, float* aBuffer,
127
                                    const size_t aSize)
128
0
{
129
0
  MOZ_ASSERT(aBuffer);
130
0
  MOZ_ASSERT(aSize == 1);
131
0
132
0
  // Getting an AudioParam value on an AudioNode does not consider input from
133
0
  // other AudioNodes, which is managed only on the graph thread.
134
0
  *aBuffer = BaseClass::GetValueAtTime(aTime);
135
0
}
136
137
template<> inline void
138
AudioParamTimeline::GetValuesAtTime(int64_t aTime, float* aBuffer,
139
                                    const size_t aSize)
140
0
{
141
0
  MOZ_ASSERT(aBuffer);
142
0
  MOZ_ASSERT(aSize <= WEBAUDIO_BLOCK_SIZE);
143
0
  MOZ_ASSERT(aSize == 1 || !HasSimpleValue());
144
0
145
0
  // Mix the value of the AudioParam itself with that of the AudioNode inputs.
146
0
  BaseClass::GetValuesAtTime(aTime, aBuffer, aSize);
147
0
  if (mStream) {
148
0
    uint32_t blockOffset = aTime % WEBAUDIO_BLOCK_SIZE;
149
0
    MOZ_ASSERT(blockOffset + aSize <= WEBAUDIO_BLOCK_SIZE);
150
0
    for (size_t i = 0; i < aSize; ++i) {
151
0
      aBuffer[i] += AudioNodeInputValue(blockOffset + i);
152
0
    }
153
0
  }
154
0
}
155
156
} // namespace dom
157
} // namespace mozilla
158
159
#endif