forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopback_stream_unittest.cc
417 lines (348 loc) · 14.8 KB
/
loopback_stream_unittest.cc
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/audio/loopback_stream.h"
#include <cmath>
#include <cstdint>
#include <memory>
#include "base/bind.h"
#include "base/containers/unique_ptr_adapters.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/ranges/algorithm.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "media/base/audio_parameters.h"
#include "media/base/audio_timestamp_helper.h"
#include "media/base/channel_layout.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/audio/loopback_coordinator.h"
#include "services/audio/loopback_group_member.h"
#include "services/audio/test/fake_consumer.h"
#include "services/audio/test/fake_loopback_group_member.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::Mock;
using testing::NiceMock;
using testing::StrictMock;
namespace audio {
namespace {
// Volume settings for the FakeLoopbackGroupMember (source) and LoopbackStream.
constexpr double kSnoopVolume = 0.25;
constexpr double kLoopbackVolume = 0.5;
// Piano key frequencies.
constexpr double kMiddleAFreq = 440;
constexpr double kMiddleCFreq = 261.626;
// Audio buffer duration.
constexpr base::TimeDelta kBufferDuration = base::Milliseconds(10);
// Local audio output delay.
constexpr base::TimeDelta kDelayUntilOutput = base::Milliseconds(20);
// The amount of audio signal to record each time PumpAudioAndTakeNewRecording()
// is called.
constexpr base::TimeDelta kTestRecordingDuration = base::Milliseconds(250);
const media::AudioParameters& GetLoopbackStreamParams() {
// 48 kHz, 2-channel audio, with 10 ms buffers.
static const media::AudioParameters params(
media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::ChannelLayoutConfig::Stereo(), 48000, 480);
return params;
}
class MockClientAndObserver : public media::mojom::AudioInputStreamClient,
public media::mojom::AudioInputStreamObserver {
public:
MockClientAndObserver() = default;
~MockClientAndObserver() override = default;
void Bind(mojo::PendingReceiver<media::mojom::AudioInputStreamClient>
client_receiver,
mojo::PendingReceiver<media::mojom::AudioInputStreamObserver>
observer_receiver) {
client_receiver_.Bind(std::move(client_receiver));
observer_receiver_.Bind(std::move(observer_receiver));
}
void CloseClientBinding() { client_receiver_.reset(); }
void CloseObserverBinding() { observer_receiver_.reset(); }
MOCK_METHOD1(OnError, void(media::mojom::InputStreamErrorCode));
MOCK_METHOD0(DidStartRecording, void());
void OnMutedStateChanged(bool) override { NOTREACHED(); }
private:
mojo::Receiver<media::mojom::AudioInputStreamClient> client_receiver_{this};
mojo::Receiver<media::mojom::AudioInputStreamObserver> observer_receiver_{
this};
};
// Subclass of FakeConsumer that adapts the SyncWriter interface to allow the
// tests to record and analyze the audio data from the LoopbackStream.
class FakeSyncWriter : public FakeConsumer, public InputController::SyncWriter {
public:
FakeSyncWriter(int channels, int sample_rate)
: FakeConsumer(channels, sample_rate) {}
~FakeSyncWriter() override = default;
void Clear() {
FakeConsumer::Clear();
last_capture_time_ = base::TimeTicks();
}
// media::AudioInputController::SyncWriter implementation.
void Write(const media::AudioBus* data,
double volume,
bool key_pressed,
base::TimeTicks capture_time) final {
FakeConsumer::Consume(*data);
// Capture times should be monotonically increasing.
if (!last_capture_time_.is_null()) {
CHECK_LT(last_capture_time_, capture_time);
}
last_capture_time_ = capture_time;
}
void Close() final {}
base::TimeTicks last_capture_time_;
};
class LoopbackStreamTest : public testing::Test {
public:
LoopbackStreamTest() : group_id_(base::UnguessableToken::Create()) {}
LoopbackStreamTest(const LoopbackStreamTest&) = delete;
LoopbackStreamTest& operator=(const LoopbackStreamTest&) = delete;
~LoopbackStreamTest() override = default;
void TearDown() override {
stream_ = nullptr;
for (const auto& source : sources_) {
coordinator_.UnregisterMember(group_id_, source.get());
}
sources_.clear();
task_environment_.FastForwardUntilNoTasksRemain();
}
MockClientAndObserver* client() { return &client_; }
LoopbackStream* stream() { return stream_.get(); }
FakeSyncWriter* consumer() { return consumer_; }
void RunMojoTasks() { task_environment_.RunUntilIdle(); }
FakeLoopbackGroupMember* AddSource(int channels, int sample_rate) {
sources_.emplace_back(
std::make_unique<FakeLoopbackGroupMember>(media::AudioParameters(
media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::ChannelLayoutConfig::Guess(channels), sample_rate,
(sample_rate * kBufferDuration).InSeconds())));
coordinator_.RegisterMember(group_id_, sources_.back().get());
return sources_.back().get();
}
void RemoveSource(FakeLoopbackGroupMember* source) {
const auto it =
base::ranges::find_if(sources_, base::MatchesUniquePtr(source));
if (it != sources_.end()) {
coordinator_.UnregisterMember(group_id_, source);
sources_.erase(it);
}
}
void CreateLoopbackStream() {
CHECK(!stream_);
mojo::PendingRemote<media::mojom::AudioInputStreamClient> client;
mojo::PendingRemote<media::mojom::AudioInputStreamObserver> observer;
client_.Bind(client.InitWithNewPipeAndPassReceiver(),
observer.InitWithNewPipeAndPassReceiver());
stream_ = std::make_unique<LoopbackStream>(
base::BindOnce([](media::mojom::ReadOnlyAudioDataPipePtr pipe) {
EXPECT_TRUE(pipe->shared_memory.IsValid());
EXPECT_TRUE(pipe->socket.is_valid());
}),
base::BindOnce([](LoopbackStreamTest* self,
LoopbackStream* stream) { self->stream_ = nullptr; },
this),
task_environment_.GetMainThreadTaskRunner(),
remote_input_stream_.BindNewPipeAndPassReceiver(), std::move(client),
std::move(observer), GetLoopbackStreamParams(),
// The following argument is the |shared_memory_count|, which does not
// matter because the SyncWriter will be overridden with FakeSyncWriter
// below.
1, &coordinator_, group_id_);
// Override the clock used by the LoopbackStream so that everything is
// single-threaded and synchronized with the driving code in these tests.
stream_->set_clock_for_testing(task_environment_.GetMockTickClock());
// Redirect the output of the LoopbackStream to a FakeSyncWriter.
// LoopbackStream takes ownership of the FakeSyncWriter.
auto consumer = std::make_unique<FakeSyncWriter>(
GetLoopbackStreamParams().channels(),
GetLoopbackStreamParams().sample_rate());
CHECK(!consumer_);
consumer_ = consumer.get();
stream_->set_sync_writer_for_testing(std::move(consumer));
// Set the volume for the LoopbackStream.
remote_input_stream_->SetVolume(kLoopbackVolume);
// Allow all pending mojo tasks for all of the above to run and propagate
// state.
RunMojoTasks();
ASSERT_TRUE(remote_input_stream_);
}
void StartLoopbackRecording() {
ASSERT_EQ(0, consumer_->GetRecordedFrameCount());
remote_input_stream_->Record();
RunMojoTasks();
}
void SetLoopbackVolume(double volume) {
remote_input_stream_->SetVolume(volume);
RunMojoTasks();
}
void PumpAudioAndTakeNewRecording() {
consumer_->Clear();
const int min_frames_to_record = media::AudioTimestampHelper::TimeToFrames(
kTestRecordingDuration, GetLoopbackStreamParams().sample_rate());
do {
// Render audio meant for local output at some point in the near
// future.
const base::TimeTicks output_timestamp =
task_environment_.NowTicks() + kDelayUntilOutput;
for (const auto& source : sources_) {
source->RenderMoreAudio(output_timestamp);
}
// Move the task runner forward, which will cause the FlowNetwork's
// delayed tasks to run, which will generate output for the consumer.
task_environment_.FastForwardBy(kBufferDuration);
} while (consumer_->GetRecordedFrameCount() < min_frames_to_record);
}
void CloseInputStreamPtr() {
remote_input_stream_.reset();
RunMojoTasks();
}
private:
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
LoopbackCoordinator coordinator_;
const base::UnguessableToken group_id_;
std::vector<std::unique_ptr<FakeLoopbackGroupMember>> sources_;
NiceMock<MockClientAndObserver> client_;
std::unique_ptr<LoopbackStream> stream_;
raw_ptr<FakeSyncWriter> consumer_ = nullptr; // Owned by |stream_|.
mojo::Remote<media::mojom::AudioInputStream> remote_input_stream_;
};
TEST_F(LoopbackStreamTest, ShutsDownStreamWhenInterfacePtrIsClosed) {
CreateLoopbackStream();
EXPECT_CALL(*client(), DidStartRecording());
StartLoopbackRecording();
PumpAudioAndTakeNewRecording();
EXPECT_CALL(*client(), OnError(media::mojom::InputStreamErrorCode::kUnknown));
CloseInputStreamPtr();
EXPECT_FALSE(stream());
Mock::VerifyAndClearExpectations(client());
}
TEST_F(LoopbackStreamTest, ShutsDownStreamWhenClientBindingIsClosed) {
CreateLoopbackStream();
EXPECT_CALL(*client(), DidStartRecording());
StartLoopbackRecording();
PumpAudioAndTakeNewRecording();
// Note: Expect no call to client::OnError() because it is the client binding
// that is being closed and causing the error.
EXPECT_CALL(*client(), OnError(_)).Times(0);
client()->CloseClientBinding();
RunMojoTasks();
EXPECT_FALSE(stream());
Mock::VerifyAndClearExpectations(client());
}
TEST_F(LoopbackStreamTest, ShutsDownStreamWhenObserverBindingIsClosed) {
CreateLoopbackStream();
EXPECT_CALL(*client(), DidStartRecording());
StartLoopbackRecording();
PumpAudioAndTakeNewRecording();
EXPECT_CALL(*client(), OnError(media::mojom::InputStreamErrorCode::kUnknown));
client()->CloseObserverBinding();
RunMojoTasks();
EXPECT_FALSE(stream());
Mock::VerifyAndClearExpectations(client());
}
TEST_F(LoopbackStreamTest, ProducesSilenceWhenNoMembersArePresent) {
CreateLoopbackStream();
EXPECT_CALL(*client(), DidStartRecording());
StartLoopbackRecording();
PumpAudioAndTakeNewRecording();
for (int ch = 0; ch < GetLoopbackStreamParams().channels(); ++ch) {
SCOPED_TRACE(testing::Message() << "ch=" << ch);
EXPECT_TRUE(consumer()->IsSilent(ch));
}
}
// Syntatic sugar to confirm a tone exists and its amplitude matches
// expectations.
#define EXPECT_TONE(ch, frequency, expected_amplitude) \
{ \
SCOPED_TRACE(testing::Message() << "ch=" << ch); \
const double amplitude = consumer()->ComputeAmplitudeAt( \
ch, frequency, consumer()->GetRecordedFrameCount()); \
VLOG(1) << "For ch=" << ch << ", amplitude at frequency=" << frequency \
<< " is " << amplitude; \
EXPECT_NEAR(expected_amplitude, amplitude, 0.01); \
}
TEST_F(LoopbackStreamTest, ProducesAudioFromASingleSource) {
FakeLoopbackGroupMember* const source =
AddSource(1, 48000); // Monaural, 48 kHz.
source->SetChannelTone(0, kMiddleAFreq);
source->SetVolume(kSnoopVolume);
CreateLoopbackStream();
EXPECT_CALL(*client(), DidStartRecording());
StartLoopbackRecording();
PumpAudioAndTakeNewRecording();
// Expect to have recorded middle-A in all of the loopback stream's channels.
for (int ch = 0; ch < GetLoopbackStreamParams().channels(); ++ch) {
EXPECT_TONE(ch, kMiddleAFreq, kSnoopVolume * kLoopbackVolume);
}
}
TEST_F(LoopbackStreamTest, ProducesAudioFromTwoSources) {
// Start the first source (of a middle-A note) before creating the loopback
// stream.
const int channels = GetLoopbackStreamParams().channels();
FakeLoopbackGroupMember* const source1 = AddSource(channels, 48000);
source1->SetChannelTone(0, kMiddleAFreq);
source1->SetVolume(kSnoopVolume);
CreateLoopbackStream();
EXPECT_CALL(*client(), DidStartRecording());
StartLoopbackRecording();
PumpAudioAndTakeNewRecording();
// Start the second source (of a middle-C note) while the loopback stream is
// running. The second source has a different sample rate than the first.
FakeLoopbackGroupMember* const source2 = AddSource(channels, 44100);
source2->SetChannelTone(1, kMiddleCFreq);
source2->SetVolume(kSnoopVolume);
PumpAudioAndTakeNewRecording();
// Expect to have recorded both middle-A and middle-C in all of the loopback
// stream's channels.
EXPECT_TONE(0, kMiddleAFreq, kSnoopVolume * kLoopbackVolume);
EXPECT_TONE(1, kMiddleCFreq, kSnoopVolume * kLoopbackVolume);
// Switch the channels containig the tone in both sources, and expect to see
// the tones have switched channels in the loopback output.
source1->SetChannelTone(0, 0.0);
source1->SetChannelTone(1, kMiddleAFreq);
source2->SetChannelTone(0, kMiddleCFreq);
source2->SetChannelTone(1, 0.0);
PumpAudioAndTakeNewRecording();
EXPECT_TONE(1, kMiddleAFreq, kSnoopVolume * kLoopbackVolume);
EXPECT_TONE(0, kMiddleCFreq, kSnoopVolume * kLoopbackVolume);
}
TEST_F(LoopbackStreamTest, AudioChangesVolume) {
FakeLoopbackGroupMember* const source =
AddSource(1, 48000); // Monaural, 48 kHz.
source->SetChannelTone(0, kMiddleAFreq);
source->SetVolume(kSnoopVolume);
CreateLoopbackStream();
StartLoopbackRecording();
PumpAudioAndTakeNewRecording();
// Record and check the amplitude at the default volume settings.
double expected_amplitude = kSnoopVolume * kLoopbackVolume;
for (int ch = 0; ch < GetLoopbackStreamParams().channels(); ++ch) {
EXPECT_TONE(ch, kMiddleAFreq, expected_amplitude);
}
// Double the volume of the source and expect the output to have also doubled.
source->SetVolume(kSnoopVolume * 2);
PumpAudioAndTakeNewRecording();
expected_amplitude *= 2;
for (int ch = 0; ch < GetLoopbackStreamParams().channels(); ++ch) {
EXPECT_TONE(ch, kMiddleAFreq, expected_amplitude);
}
// Drop the LoopbackStream volume by 1/3 and expect the output to also have
// dropped by 1/3.
SetLoopbackVolume(kLoopbackVolume / 3);
PumpAudioAndTakeNewRecording();
expected_amplitude /= 3;
for (int ch = 0; ch < GetLoopbackStreamParams().channels(); ++ch) {
EXPECT_TONE(ch, kMiddleAFreq, expected_amplitude);
}
}
} // namespace
} // namespace audio