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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
media / mojo / clients / mojo_audio_encoder_unittest.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "media/mojo/clients/mojo_audio_encoder.h"
#include <memory>
#include "base/containers/heap_array.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "media/base/audio_encoder.h"
#include "media/base/decoder_buffer.h"
#include "media/base/media_util.h"
#include "media/base/mock_filters.h"
#include "media/base/test_helpers.h"
#include "media/base/waiting.h"
#include "media/mojo/mojom/audio_encoder.mojom.h"
#include "media/mojo/services/mojo_audio_encoder_service.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::base::test::RunOnceCallback;
using ::testing::_;
using ::testing::DoAll;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::Return;
using ::testing::StrictMock;
namespace media {
// Tests MojoAudioEncoder (client) and MojoAudioEncoderService (service).
class MojoAudioEncoderTest : public ::testing::Test {
public:
MojoAudioEncoderTest() : service_thread_("Service Thread") {}
void SetUp() override {
service_thread_.Start();
callback_runner_ = task_environment_.GetMainThreadTaskRunner();
service_task_runner_ = service_thread_.task_runner();
std::unique_ptr<StrictMock<MockAudioEncoder>> mock_audio_encoder(
new StrictMock<MockAudioEncoder>());
mock_audio_encoder_ = mock_audio_encoder.get();
EXPECT_CALL(*mock_audio_encoder_, OnDestruct());
// Setup the mojo connection.
mojo::PendingRemote<mojom::AudioEncoder> remote_audio_encoder;
audio_encoder_service_ = std::make_unique<MojoAudioEncoderService>(
std::move(mock_audio_encoder));
service_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&MojoAudioEncoderTest::SetupMojoService,
base::Unretained(this),
remote_audio_encoder.InitWithNewPipeAndPassReceiver()));
mojo_audio_encoder_ =
std::make_unique<MojoAudioEncoder>(std::move(remote_audio_encoder));
}
void TearDown() override {
mojo_audio_encoder_.reset();
service_task_runner_->DeleteSoon(FROM_HERE, std::move(receiver_));
service_task_runner_->DeleteSoon(FROM_HERE,
std::move(audio_encoder_service_));
service_thread_.Stop();
}
void SetupMojoService(mojo::PendingReceiver<mojom::AudioEncoder> receiver) {
receiver_ = std::make_unique<mojo::Receiver<mojom::AudioEncoder>>(
audio_encoder_service_.get(), std::move(receiver));
}
std::unique_ptr<AudioBus> MakeInput(float seed, int channels, int frames) {
CHECK_EQ(std::clamp(seed, -1.0f, 1.0f), seed)
<< "Some AudioBuffer <-> AudioBus conversions involve clipping to the "
"range [-1.0, 1.0], so test results will be unreliable";
auto result = AudioBus::Create(channels, frames);
for (int channel = 0; channel < channels; channel++) {
for (int i = 0; i < frames; i++)
result->channel(channel)[i] = seed;
}
return result;
}
AudioEncoder::Options MakeOptions() {
AudioEncoder::Options options;
options.codec = AudioCodec::kOpus;
options.bitrate = 128000;
options.channels = 2;
options.sample_rate = 44000;
return options;
}
base::TimeTicks FromMilliseconds(int ms) {
return base::TimeTicks() + base::Milliseconds(ms);
}
int64_t ToMilliseconds(base::TimeTicks ticks) {
return (ticks - base::TimeTicks()).InMilliseconds();
}
AudioEncoder::EncoderStatusCB ValidatingStatusCB(
base::Location loc = FROM_HERE) {
struct CallEnforcer {
bool called = false;
std::string location;
~CallEnforcer() {
EXPECT_TRUE(called) << "Callback created: " << location;
}
};
auto enforcer = std::make_unique<CallEnforcer>();
enforcer->location = loc.ToString();
return base::BindLambdaForTesting(
[this, enforcer{std::move(enforcer)}](EncoderStatus s) {
EXPECT_TRUE(callback_runner_->RunsTasksInCurrentSequence());
EXPECT_TRUE(s.is_ok()) << " Callback created: " << enforcer->location
<< " Error: " << s.message();
enforcer->called = true;
});
}
base::test::SingleThreadTaskEnvironment task_environment_;
std::unique_ptr<base::RunLoop> run_loop_;
// The thread where the service runs. This provides test coverage in an
// environment similar to what we use in production.
base::Thread service_thread_;
scoped_refptr<base::SequencedTaskRunner> service_task_runner_;
scoped_refptr<base::SequencedTaskRunner> callback_runner_;
// The MojoAudioEncoder that we are testing.
std::unique_ptr<MojoAudioEncoder> mojo_audio_encoder_;
// Mojo server-side
std::unique_ptr<mojo::Receiver<mojom::AudioEncoder>> receiver_;
std::unique_ptr<MojoAudioEncoderService> audio_encoder_service_;
raw_ptr<StrictMock<MockAudioEncoder>, AcrossTasksDanglingUntriaged>
mock_audio_encoder_ = nullptr;
};
TEST_F(MojoAudioEncoderTest, Initialize_Success) {
base::RunLoop run_loop;
AudioEncoder::Options options = MakeOptions();
EXPECT_CALL(*mock_audio_encoder_, Initialize(_, _, _))
.WillOnce(Invoke([this](const AudioEncoder::Options& options,
AudioEncoder::OutputCB output_cb,
AudioEncoder::EncoderStatusCB done_cb) {
EXPECT_TRUE(service_task_runner_->RunsTasksInCurrentSequence());
std::move(done_cb).Run(EncoderStatus::Codes::kOk);
}));
AudioEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](EncodedAudioBuffer output,
std::optional<AudioEncoder::CodecDescription>) { FAIL(); });
auto done_cb = base::BindLambdaForTesting([&, this](EncoderStatus s) {
EXPECT_TRUE(callback_runner_->RunsTasksInCurrentSequence());
EXPECT_TRUE(s.is_ok());
run_loop.QuitWhenIdle();
});
mojo_audio_encoder_->Initialize(options, std::move(output_cb),
std::move(done_cb));
run_loop.Run();
}
TEST_F(MojoAudioEncoderTest, Initialize_Fail) {
base::RunLoop run_loop;
AudioEncoder::Options options = MakeOptions();
EXPECT_CALL(*mock_audio_encoder_, Initialize(_, _, _))
.WillOnce(Invoke([](const AudioEncoder::Options& options,
AudioEncoder::OutputCB output_cb,
AudioEncoder::EncoderStatusCB done_cb) {
std::move(done_cb).Run(
EncoderStatus::Codes::kEncoderInitializationError);
}));
AudioEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](EncodedAudioBuffer output,
std::optional<AudioEncoder::CodecDescription>) { FAIL(); });
auto done_cb = base::BindLambdaForTesting([&, this](EncoderStatus s) {
EXPECT_TRUE(callback_runner_->RunsTasksInCurrentSequence());
EXPECT_EQ(s.code(), EncoderStatus::Codes::kEncoderInitializationError);
run_loop.QuitWhenIdle();
});
mojo_audio_encoder_->Initialize(options, std::move(output_cb),
std::move(done_cb));
run_loop.Run();
}
TEST_F(MojoAudioEncoderTest, Initialize_Twice) {
base::RunLoop good_init_run_loop;
base::RunLoop failed_initi_run_loop;
AudioEncoder::Options options = MakeOptions();
EXPECT_CALL(*mock_audio_encoder_, Initialize(_, _, _))
.WillRepeatedly(Invoke([](const AudioEncoder::Options& options,
AudioEncoder::OutputCB output_cb,
AudioEncoder::EncoderStatusCB done_cb) {
std::move(done_cb).Run(EncoderStatus::Codes::kOk);
}));
auto expect_ok = base::BindLambdaForTesting([&](EncoderStatus s) {
EXPECT_TRUE(s.is_ok());
good_init_run_loop.QuitWhenIdle();
});
auto expect_error = base::BindLambdaForTesting([&](EncoderStatus s) {
EXPECT_EQ(s.code(), EncoderStatus::Codes::kEncoderInitializeTwice);
failed_initi_run_loop.QuitWhenIdle();
});
mojo_audio_encoder_->Initialize(options, base::DoNothing(),
std::move(expect_ok));
good_init_run_loop.Run();
mojo_audio_encoder_->Initialize(options, base::DoNothing(),
std::move(expect_error));
failed_initi_run_loop.Run();
}
TEST_F(MojoAudioEncoderTest, Encode) {
base::RunLoop run_loop;
AudioEncoder::Options options = MakeOptions();
constexpr int kInputCount = 20;
constexpr size_t kFrameCount = 1024;
int output_count = 0;
AudioEncoder::OutputCB service_output_cb;
EXPECT_CALL(*mock_audio_encoder_, Initialize(_, _, _))
.WillOnce(Invoke([&, this](const AudioEncoder::Options& options,
AudioEncoder::OutputCB output_cb,
AudioEncoder::EncoderStatusCB done_cb) {
EXPECT_TRUE(service_task_runner_->RunsTasksInCurrentSequence());
service_output_cb = std::move(output_cb);
std::move(done_cb).Run(EncoderStatus::Codes::kOk);
}));
EXPECT_CALL(*mock_audio_encoder_, Encode(_, _, _))
.WillRepeatedly(Invoke([&, this](std::unique_ptr<AudioBus> audio_bus,
base::TimeTicks capture_time,
AudioEncoder::EncoderStatusCB done_cb) {
EXPECT_TRUE(service_task_runner_->RunsTasksInCurrentSequence());
std::move(done_cb).Run(EncoderStatus::Codes::kOk);
int64_t input_number = ToMilliseconds(capture_time);
EXPECT_LE(input_number, kInputCount);
AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
{CHANNEL_LAYOUT_DISCRETE, audio_bus->channels()},
options.sample_rate, audio_bus->frames());
const auto channel_data =
base::span(reinterpret_cast<const uint8_t*>(audio_bus->channel(0)),
base::checked_cast<size_t>(AudioBus::CalculateMemorySize(
/*channels=*/1, audio_bus->frames())));
auto encoded_data = base::HeapArray<uint8_t>::CopiedFrom(channel_data);
EncodedAudioBuffer output(params, std::move(encoded_data),
capture_time);
std::optional<AudioEncoder::CodecDescription> desc;
if (input_number > 0)
desc.emplace(AudioEncoder::CodecDescription{
static_cast<uint8_t>(input_number)});
service_output_cb.Run(std::move(output), desc);
}));
AudioEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](EncodedAudioBuffer output,
std::optional<AudioEncoder::CodecDescription> desc) {
const int64_t output_number = ToMilliseconds(output.timestamp);
EXPECT_EQ(output_number, output_count);
EXPECT_EQ(output.params.channels(), options.channels);
EXPECT_EQ(output.params.sample_rate(), options.sample_rate);
ASSERT_EQ(output.encoded_data.size(),
base::checked_cast<size_t>(AudioBus::CalculateMemorySize(
/*channels=*/1, kFrameCount)));
const float seed = 1.0 / kInputCount * output_number;
auto* const encoded_data =
reinterpret_cast<const float*>(output.encoded_data.data());
for (size_t i = 0; i < kFrameCount; i++) {
EXPECT_EQ(encoded_data[i], seed)
<< " output_number: " << output_number << " i: " << i;
}
if (output_number == 0)
EXPECT_FALSE(desc.has_value());
else
EXPECT_EQ(output_number, desc.value()[0]);
output_count++;
if (output_count == kInputCount) {
run_loop.QuitWhenIdle();
}
});
mojo_audio_encoder_->Initialize(options, std::move(output_cb),
ValidatingStatusCB());
for (int i = 0; i < kInputCount; i++) {
auto ts = FromMilliseconds(i);
mojo_audio_encoder_->Encode(
MakeInput(1.0 / kInputCount * i, options.channels, kFrameCount), ts,
ValidatingStatusCB());
}
run_loop.Run();
} // namespace media
TEST_F(MojoAudioEncoderTest, EncodeWithEmptyResult) {
base::RunLoop run_loop;
AudioEncoder::Options options = MakeOptions();
AudioEncoder::OutputCB service_output_cb;
EXPECT_CALL(*mock_audio_encoder_, Initialize(_, _, _))
.WillOnce(Invoke([&](const AudioEncoder::Options& options,
AudioEncoder::OutputCB output_cb,
AudioEncoder::EncoderStatusCB done_cb) {
service_output_cb = std::move(output_cb);
std::move(done_cb).Run(EncoderStatus::Codes::kOk);
}));
EXPECT_CALL(*mock_audio_encoder_, Encode(_, _, _))
.WillRepeatedly(Invoke([&, this](std::unique_ptr<AudioBus> audio_bus,
base::TimeTicks capture_time,
AudioEncoder::EncoderStatusCB done_cb) {
EXPECT_TRUE(service_task_runner_->RunsTasksInCurrentSequence());
std::move(done_cb).Run(EncoderStatus::Codes::kOk);
AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
{CHANNEL_LAYOUT_DISCRETE, 1}, 8000, 1);
EncodedAudioBuffer output(params, base::HeapArray<uint8_t>(),
capture_time);
service_output_cb.Run(std::move(output), {});
}));
AudioEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](EncodedAudioBuffer output,
std::optional<AudioEncoder::CodecDescription> desc) {
EXPECT_TRUE(output.encoded_data.empty());
run_loop.QuitWhenIdle();
});
mojo_audio_encoder_->Initialize(options, std::move(output_cb),
ValidatingStatusCB());
auto ts = FromMilliseconds(1);
mojo_audio_encoder_->Encode(
MakeInput(1, options.channels, options.sample_rate), ts,
ValidatingStatusCB());
run_loop.Run();
} // namespace media
TEST_F(MojoAudioEncoderTest, Flush) {
base::RunLoop run_loop;
AudioEncoder::Options options = MakeOptions();
const int input_count = 5;
int output_count = 0;
AudioEncoder::OutputCB service_output_cb;
EXPECT_CALL(*mock_audio_encoder_, Initialize(_, _, _))
.WillOnce(Invoke([&](const AudioEncoder::Options& options,
AudioEncoder::OutputCB output_cb,
AudioEncoder::EncoderStatusCB done_cb) {
service_output_cb = std::move(output_cb);
std::move(done_cb).Run(EncoderStatus::Codes::kOk);
}));
EXPECT_CALL(*mock_audio_encoder_, Encode(_, _, _))
.WillRepeatedly(Invoke([&](std::unique_ptr<AudioBus> audio_bus,
base::TimeTicks capture_time,
AudioEncoder::EncoderStatusCB done_cb) {
std::move(done_cb).Run(EncoderStatus::Codes::kOk);
AudioParameters params(AudioParameters::AUDIO_PCM_LOW_LATENCY,
{CHANNEL_LAYOUT_DISCRETE, audio_bus->channels()},
options.sample_rate, audio_bus->frames());
EncodedAudioBuffer output(params, base::HeapArray<uint8_t>(),
capture_time);
service_output_cb.Run(std::move(output), {});
}));
EXPECT_CALL(*mock_audio_encoder_, Flush(_))
.WillRepeatedly(Invoke([&, this](AudioEncoder::EncoderStatusCB done_cb) {
EXPECT_TRUE(service_task_runner_->RunsTasksInCurrentSequence());
std::move(done_cb).Run(EncoderStatus::Codes::kOk);
}));
AudioEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](EncodedAudioBuffer output,
std::optional<AudioEncoder::CodecDescription>) { output_count++; });
mojo_audio_encoder_->Initialize(options, std::move(output_cb),
ValidatingStatusCB());
for (int i = 0; i < input_count; i++) {
auto ts = FromMilliseconds(i);
mojo_audio_encoder_->Encode(
MakeInput(1.0 / input_count * i, options.channels, options.sample_rate),
ts, ValidatingStatusCB());
}
auto flush_cb = base::BindLambdaForTesting([&](EncoderStatus s) {
EXPECT_TRUE(callback_runner_->RunsTasksInCurrentSequence());
EXPECT_TRUE(s.is_ok());
EXPECT_EQ(output_count, input_count);
run_loop.QuitWhenIdle();
});
mojo_audio_encoder_->Flush(std::move(flush_cb));
run_loop.Run();
}
// Test that in case of an mojo error all status callbacks report correct
// error status.
TEST_F(MojoAudioEncoderTest, MojoErrorCallsAllDoneCallbacks) {
base::RunLoop run_loop;
AudioEncoder::Options options = MakeOptions();
std::vector<AudioEncoder::EncoderStatusCB> done_callbacks;
const int input_count = 5;
int error_count = 0;
EXPECT_CALL(*mock_audio_encoder_, Initialize(_, _, _))
.WillOnce(Invoke([&](const AudioEncoder::Options& options,
AudioEncoder::OutputCB output_cb,
AudioEncoder::EncoderStatusCB done_cb) {
std::move(done_cb).Run(EncoderStatus::Codes::kOk);
}));
EXPECT_CALL(*mock_audio_encoder_, Encode(_, _, _))
.WillRepeatedly(Invoke([&](std::unique_ptr<AudioBus> audio_bus,
base::TimeTicks capture_time,
AudioEncoder::EncoderStatusCB done_cb) {
done_callbacks.push_back(std::move(done_cb));
}));
EXPECT_CALL(*mock_audio_encoder_, Flush(_))
.WillOnce(Invoke([&](AudioEncoder::EncoderStatusCB done_cb) {
done_callbacks.push_back(std::move(done_cb));
service_task_runner_->DeleteSoon(FROM_HERE, std::move(receiver_));
}));
mojo_audio_encoder_->Initialize(options, base::DoNothing(),
ValidatingStatusCB());
for (int i = 0; i < input_count; i++) {
auto ts = FromMilliseconds(i);
auto done_cb = base::BindLambdaForTesting([&](EncoderStatus s) {
EXPECT_EQ(s.code(), EncoderStatus::Codes::kEncoderMojoConnectionError);
error_count++;
});
mojo_audio_encoder_->Encode(
MakeInput(1.0 / input_count * i, options.channels, options.sample_rate),
ts, std::move(done_cb));
}
auto flush_cb = base::BindLambdaForTesting([&](EncoderStatus s) {
EXPECT_EQ(s.code(), EncoderStatus::Codes::kEncoderMojoConnectionError);
run_loop.QuitWhenIdle();
});
mojo_audio_encoder_->Flush(std::move(flush_cb));
run_loop.Run();
EXPECT_EQ(error_count, input_count);
}
} // namespace media