1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "CubebUtils.h"
#include "GraphDriver.h"
#include "gmock/gmock.h"
#include "gtest/gtest-printers.h"
#include "gtest/gtest.h"
#include "MediaTrackGraphImpl.h"
#include "mozilla/Attributes.h"
#include "mozilla/UniquePtr.h"
#include "nsTArray.h"
#include "MockCubeb.h"
#include "WaitFor.h"
using namespace mozilla;
using IterationResult = GraphInterface::IterationResult;
using ::testing::NiceMock;
class MockGraphInterface : public GraphInterface {
NS_DECL_THREADSAFE_ISUPPORTS
explicit MockGraphInterface(TrackRate aSampleRate)
: mSampleRate(aSampleRate) {}
MOCK_METHOD4(NotifyOutputData,
void(AudioDataValue*, size_t, TrackRate, uint32_t));
MOCK_METHOD0(NotifyInputStopped, void());
MOCK_METHOD5(NotifyInputData, void(const AudioDataValue*, size_t, TrackRate,
uint32_t, uint32_t));
MOCK_METHOD0(DeviceChanged, void());
/* OneIteration cannot be mocked because IterationResult is non-memmovable and
* cannot be passed as a parameter, which GMock does internally. */
IterationResult OneIteration(GraphTime aStateComputedTime, GraphTime,
AudioMixer* aMixer) {
GraphDriver* driver = mCurrentDriver;
if (aMixer) {
aMixer->StartMixing();
aMixer->Mix(nullptr,
driver->AsAudioCallbackDriver()->OutputChannelCount(),
aStateComputedTime - mStateComputedTime, mSampleRate);
aMixer->FinishMixing();
}
if (aStateComputedTime != mStateComputedTime) {
mFramesIteratedEvent.Notify(aStateComputedTime - mStateComputedTime);
++mIterationCount;
}
mStateComputedTime = aStateComputedTime;
if (!mKeepProcessing) {
return IterationResult::CreateStop(
NS_NewRunnableFunction(__func__, [] {}));
}
GraphDriver* next = mNextDriver.exchange(nullptr);
if (next) {
return IterationResult::CreateSwitchDriver(
next, NS_NewRunnableFunction(__func__, [] {}));
}
if (mEnsureNextIteration) {
driver->EnsureNextIteration();
}
return IterationResult::CreateStillProcessing();
}
void SetEnsureNextIteration(bool aEnsure) { mEnsureNextIteration = aEnsure; }
#ifdef DEBUG
bool InDriverIteration(const GraphDriver* aDriver) const override {
return aDriver->OnThread();
}
#endif
size_t IterationCount() const { return mIterationCount; }
GraphTime StateComputedTime() const { return mStateComputedTime; }
void SetCurrentDriver(GraphDriver* aDriver) { mCurrentDriver = aDriver; }
void StopIterating() { mKeepProcessing = false; }
void SwitchTo(GraphDriver* aDriver) { mNextDriver = aDriver; }
const TrackRate mSampleRate;
MediaEventSource<uint32_t>& FramesIteratedEvent() {
return mFramesIteratedEvent;
}
protected:
Atomic<size_t> mIterationCount{0};
Atomic<GraphTime> mStateComputedTime{0};
Atomic<GraphDriver*> mCurrentDriver{nullptr};
Atomic<bool> mEnsureNextIteration{false};
Atomic<bool> mKeepProcessing{true};
Atomic<GraphDriver*> mNextDriver{nullptr};
MediaEventProducer<uint32_t> mFramesIteratedEvent;
virtual ~MockGraphInterface() = default;
};
NS_IMPL_ISUPPORTS0(MockGraphInterface)
TEST(TestAudioCallbackDriver, StartStop)
MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION {
const TrackRate rate = 44100;
MockCubeb* cubeb = new MockCubeb();
CubebUtils::ForceSetCubebContext(cubeb->AsCubebContext());
RefPtr<AudioCallbackDriver> driver;
auto graph = MakeRefPtr<NiceMock<MockGraphInterface>>(rate);
EXPECT_CALL(*graph, NotifyInputStopped).Times(0);
ON_CALL(*graph, NotifyOutputData)
.WillByDefault([&](AudioDataValue*, size_t, TrackRate, uint32_t) {});
driver = MakeRefPtr<AudioCallbackDriver>(graph, nullptr, rate, 2, 0, nullptr,
nullptr, AudioInputType::Unknown);
EXPECT_FALSE(driver->ThreadRunning()) << "Verify thread is not running";
EXPECT_FALSE(driver->IsStarted()) << "Verify thread is not started";
graph->SetCurrentDriver(driver);
driver->Start();
// Allow some time to "play" audio.
std::this_thread::sleep_for(std::chrono::milliseconds(200));
EXPECT_TRUE(driver->ThreadRunning()) << "Verify thread is running";
EXPECT_TRUE(driver->IsStarted()) << "Verify thread is started";
// This will block untill all events have been executed.
MOZ_KnownLive(driver)->Shutdown();
EXPECT_FALSE(driver->ThreadRunning()) << "Verify thread is not running";
EXPECT_FALSE(driver->IsStarted()) << "Verify thread is not started";
}
void TestSlowStart(const TrackRate aRate) MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION {
std::cerr << "TestSlowStart with rate " << aRate << std::endl;
MockCubeb* cubeb = new MockCubeb();
cubeb->SetStreamStartFreezeEnabled(true);
auto unforcer = WaitFor(cubeb->ForceAudioThread()).unwrap();
Unused << unforcer;
CubebUtils::ForceSetCubebContext(cubeb->AsCubebContext());
RefPtr<AudioCallbackDriver> driver;
auto graph = MakeRefPtr<NiceMock<MockGraphInterface>>(aRate);
EXPECT_CALL(*graph, NotifyInputStopped).Times(0);
Maybe<int64_t> audioStart;
Maybe<uint32_t> alreadyBuffered;
int64_t inputFrameCount = 0;
int64_t outputFrameCount = 0;
int64_t processedFrameCount = 0;
ON_CALL(*graph, NotifyInputData)
.WillByDefault([&](const AudioDataValue*, size_t aFrames, TrackRate,
uint32_t, uint32_t aAlreadyBuffered) {
if (!audioStart) {
audioStart = Some(graph->StateComputedTime());
alreadyBuffered = Some(aAlreadyBuffered);
}
EXPECT_NEAR(inputFrameCount,
static_cast<int64_t>(graph->StateComputedTime() -
*audioStart + *alreadyBuffered),
WEBAUDIO_BLOCK_SIZE)
<< "Input should be behind state time, due to the delayed start. "
"stateComputedTime="
<< graph->StateComputedTime() << ", audioStartTime=" << *audioStart
<< ", alreadyBuffered=" << *alreadyBuffered;
inputFrameCount += aFrames;
});
ON_CALL(*graph, NotifyOutputData)
.WillByDefault([&](AudioDataValue*, size_t aFrames, TrackRate aRate,
uint32_t) { outputFrameCount += aFrames; });
driver = MakeRefPtr<AudioCallbackDriver>(graph, nullptr, aRate, 2, 2, nullptr,
(void*)1, AudioInputType::Voice);
EXPECT_FALSE(driver->ThreadRunning()) << "Verify thread is not running";
EXPECT_FALSE(driver->IsStarted()) << "Verify thread is not started";
graph->SetCurrentDriver(driver);
graph->SetEnsureNextIteration(true);
driver->Start();
RefPtr<SmartMockCubebStream> stream = WaitFor(cubeb->StreamInitEvent());
cubeb->SetStreamStartFreezeEnabled(false);
const size_t fallbackIterations = 3;
WaitUntil(graph->FramesIteratedEvent(), [&](uint32_t aFrames) {
const GraphTime tenMillis = aRate / 100;
// An iteration is always rounded upwards to the next full block.
const GraphTime tenMillisIteration =
MediaTrackGraphImpl::RoundUpToEndOfAudioBlock(tenMillis);
// The iteration may be smaller because up to an extra block may have been
// processed and buffered.
const GraphTime tenMillisMinIteration =
tenMillisIteration - WEBAUDIO_BLOCK_SIZE;
// An iteration must be at least one audio block.
const GraphTime minIteration =
std::max<GraphTime>(WEBAUDIO_BLOCK_SIZE, tenMillisMinIteration);
EXPECT_GE(aFrames, minIteration)
<< "Fallback driver iteration >= 10ms, modulo an audio block";
EXPECT_LT(aFrames, static_cast<size_t>(aRate))
<< "Fallback driver iteration <1s (sanity)";
return graph->IterationCount() >= fallbackIterations;
});
stream->Thaw();
// Wait for at least 100ms of audio data.
WaitUntil(stream->FramesProcessedEvent(), [&](uint32_t aFrames) {
processedFrameCount += aFrames;
return processedFrameCount >= aRate / 10;
});
// This will block untill all events have been executed.
MOZ_KnownLive(driver)->Shutdown();
EXPECT_EQ(inputFrameCount, outputFrameCount);
EXPECT_NEAR(graph->StateComputedTime() - *audioStart,
inputFrameCount + *alreadyBuffered, WEBAUDIO_BLOCK_SIZE)
<< "Graph progresses while audio driver runs. stateComputedTime="
<< graph->StateComputedTime() << ", inputFrameCount=" << inputFrameCount;
}
TEST(TestAudioCallbackDriver, SlowStart)
MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION {
TestSlowStart(1000); // 10ms = 10 <<< 128 samples
TestSlowStart(8000); // 10ms = 80 < 128 samples
TestSlowStart(44100); // 10ms = 441 > 128 samples
}
|