forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput_sync_writer_unittest.cc
333 lines (269 loc) · 11.6 KB
/
input_sync_writer_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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/audio/input_sync_writer.h"
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <string>
#include <utility>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/sync_socket.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_parameters.h"
#include "media/base/channel_layout.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
namespace audio {
namespace {
// Number of audio buffers in the faked ring buffer.
const int kSegments = 10;
} // namespace
// Mocked out sockets used for Send/ReceiveWithTimeout. Counts the number of
// outstanding reads, i.e. the diff between send and receive calls.
class MockCancelableSyncSocket : public base::CancelableSyncSocket {
public:
explicit MockCancelableSyncSocket(int buffer_size)
: in_failure_mode_(false),
writes_(0),
reads_(0),
receives_(0),
buffer_size_(buffer_size),
read_buffer_index_(0) {}
size_t Send(const void* buffer, size_t length) override {
EXPECT_EQ(length, sizeof(uint32_t));
++writes_;
EXPECT_LE(NumberOfBuffersFilled(), buffer_size_);
return length;
}
size_t Receive(void* buffer, size_t length) override {
EXPECT_EQ(0u, length % sizeof(uint32_t));
if (in_failure_mode_)
return 0;
if (receives_ == reads_)
return 0;
uint32_t* ptr = static_cast<uint32_t*>(buffer);
size_t received = 0;
for (; received < length / sizeof(uint32_t) && receives_ < reads_;
++received, ++ptr) {
++receives_;
EXPECT_LE(receives_, reads_);
*ptr = ++read_buffer_index_;
}
return received * sizeof(uint32_t);
}
size_t Peek() override { return (reads_ - receives_) * sizeof(uint32_t); }
// Simluates reading |buffers| number of buffers from the ring buffer.
void Read(int buffers) {
reads_ += buffers;
EXPECT_LE(reads_, writes_);
}
// When |in_failure_mode_| == true, the socket fails to receive.
void SetFailureMode(bool in_failure_mode) {
in_failure_mode_ = in_failure_mode;
}
int NumberOfBuffersFilled() { return writes_ - reads_; }
private:
bool in_failure_mode_;
int writes_;
int reads_;
int receives_;
int buffer_size_;
uint32_t read_buffer_index_;
DISALLOW_COPY_AND_ASSIGN(MockCancelableSyncSocket);
};
class InputSyncWriterTest : public testing::Test {
public:
InputSyncWriterTest() {
const int sampling_frequency_hz = 16000;
const int frames = sampling_frequency_hz / 100; // 10 ms
const media::AudioParameters audio_params(
media::AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_MONO,
sampling_frequency_hz, frames);
const uint32_t data_size =
ComputeAudioInputBufferSize(audio_params, kSegments);
auto shared_memory = base::ReadOnlySharedMemoryRegion::Create(data_size);
EXPECT_TRUE(shared_memory.IsValid());
auto socket = std::make_unique<MockCancelableSyncSocket>(kSegments);
socket_ = socket.get();
writer_ = std::make_unique<InputSyncWriter>(
mock_logger_.Get(), std::move(shared_memory), std::move(socket),
kSegments, audio_params);
audio_bus_ = media::AudioBus::Create(audio_params);
}
~InputSyncWriterTest() override {}
// Get total number of expected log calls. On non-Android we expect one log
// call at first Write() call, zero on Android. We also expect all call in the
// with a glitch summary from the destructor. Besides that only for errors
// and fifo info.
int GetTotalNumberOfExpectedLogCalls(int expected_calls_due_to_error) {
#if defined(OS_ANDROID)
return expected_calls_due_to_error + 1;
#else
return expected_calls_due_to_error + 2;
#endif
}
// Tests expected numbers which are given as arguments.
bool TestSocketAndFifoExpectations(int number_of_buffers_in_socket,
size_t number_of_verifications_in_socket,
size_t number_of_buffers_in_fifo) {
EXPECT_EQ(number_of_buffers_in_socket, socket_->NumberOfBuffersFilled());
EXPECT_EQ(number_of_verifications_in_socket, socket_->Peek());
EXPECT_EQ(number_of_buffers_in_fifo, writer_->overflow_data_.size());
return number_of_buffers_in_socket == socket_->NumberOfBuffersFilled() &&
number_of_verifications_in_socket == socket_->Peek() &&
number_of_buffers_in_fifo == writer_->overflow_data_.size();
}
protected:
using MockLogger =
base::MockCallback<base::RepeatingCallback<void(const std::string&)>>;
base::test::TaskEnvironment env_;
MockLogger mock_logger_;
std::unique_ptr<InputSyncWriter> writer_;
MockCancelableSyncSocket* socket_;
std::unique_ptr<media::AudioBus> audio_bus_;
private:
DISALLOW_COPY_AND_ASSIGN(InputSyncWriterTest);
};
TEST_F(InputSyncWriterTest, SingleWriteAndRead) {
EXPECT_CALL(mock_logger_, Run(_)).Times(GetTotalNumberOfExpectedLogCalls(0));
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(1, 0, 0));
socket_->Read(1);
EXPECT_TRUE(TestSocketAndFifoExpectations(0, 1 * sizeof(uint32_t), 0));
}
TEST_F(InputSyncWriterTest, MultipleWritesAndReads) {
EXPECT_CALL(mock_logger_, Run(_)).Times(GetTotalNumberOfExpectedLogCalls(0));
for (int i = 1; i <= 2 * kSegments; ++i) {
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(1, 0, 0));
socket_->Read(1);
EXPECT_TRUE(TestSocketAndFifoExpectations(0, 1 * sizeof(uint32_t), 0));
}
}
TEST_F(InputSyncWriterTest, MultipleWritesNoReads) {
EXPECT_CALL(mock_logger_, Run(_)).Times(GetTotalNumberOfExpectedLogCalls(1));
// Fill the ring buffer.
for (int i = 1; i <= kSegments; ++i) {
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(i, 0, 0));
}
// Now the ring buffer is full, do more writes. We should start filling the
// fifo and should get one extra log call for that.
for (size_t i = 1; i <= kSegments; ++i) {
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, i));
}
}
TEST_F(InputSyncWriterTest, FillAndEmptyRingBuffer) {
EXPECT_CALL(mock_logger_, Run(_)).Times(GetTotalNumberOfExpectedLogCalls(2));
// Fill the ring buffer.
for (int i = 1; i <= kSegments; ++i) {
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
}
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, 0));
// Empty half of the ring buffer.
const int buffers_to_read = kSegments / 2;
socket_->Read(buffers_to_read);
EXPECT_TRUE(TestSocketAndFifoExpectations(
kSegments - buffers_to_read, buffers_to_read * sizeof(uint32_t), 0));
// Fill up again. The first write should do receive until that queue is
// empty.
for (int i = kSegments - buffers_to_read + 1; i <= kSegments; ++i) {
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(i, 0, 0));
}
// Another write, should put the data in the fifo, and render an extra log
// call.
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, 1));
// Empty the ring buffer.
socket_->Read(kSegments);
EXPECT_TRUE(
TestSocketAndFifoExpectations(0, kSegments * sizeof(uint32_t), 1));
// Another write, should do receive until that queue is empty and write both
// the data in the fifo and the new data, and render a log call.
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(2, 0, 0));
// Read the two data blocks.
socket_->Read(2);
EXPECT_TRUE(TestSocketAndFifoExpectations(0, 2 * sizeof(uint32_t), 0));
}
TEST_F(InputSyncWriterTest, FillRingBufferAndFifo) {
EXPECT_CALL(mock_logger_, Run(_)).Times(GetTotalNumberOfExpectedLogCalls(2));
// Fill the ring buffer.
for (int i = 1; i <= kSegments; ++i) {
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
}
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, 0));
// Fill the fifo. Should render one log call for starting filling it.
const size_t max_fifo_size = InputSyncWriter::kMaxOverflowBusesSize;
for (size_t i = 1; i <= max_fifo_size; ++i) {
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
}
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, max_fifo_size));
// Another write, data should be dropped and render one log call.
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, max_fifo_size));
}
TEST_F(InputSyncWriterTest, MultipleFillAndEmptyRingBufferAndPartOfFifo) {
EXPECT_CALL(mock_logger_, Run(_)).Times(GetTotalNumberOfExpectedLogCalls(4));
// Fill the ring buffer.
for (int i = 1; i <= kSegments; ++i) {
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
}
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, 0));
// Write more data, should be put in the fifo and render one log call for
// starting filling it.
for (size_t i = 1; i <= 2 * kSegments; ++i) {
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
}
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, 2 * kSegments));
// Empty the ring buffer.
socket_->Read(kSegments);
EXPECT_TRUE(TestSocketAndFifoExpectations(0, kSegments * sizeof(uint32_t),
2 * kSegments));
// Another write should fill up the ring buffer with data from the fifo and
// put this data into the fifo.
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, kSegments + 1));
// Empty the ring buffer again.
socket_->Read(kSegments);
EXPECT_TRUE(TestSocketAndFifoExpectations(0, kSegments * sizeof(uint32_t),
kSegments + 1));
// Another write should fill up the ring buffer with data from the fifo and
// put this data into the fifo.
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, 2));
// Empty the ring buffer again.
socket_->Read(kSegments);
EXPECT_TRUE(
TestSocketAndFifoExpectations(0, kSegments * sizeof(uint32_t), 2));
// Another write should put the remaining data in the fifo in the ring buffer
// together with this data. Should render a log call for emptying the fifo.
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
EXPECT_TRUE(TestSocketAndFifoExpectations(3, 0, 0));
// Read the remaining data.
socket_->Read(3);
EXPECT_TRUE(TestSocketAndFifoExpectations(0, 3 * sizeof(uint32_t), 0));
// Fill the ring buffer and part of the fifo. Should render one log call for
// starting filling it.
for (int i = 1; i <= kSegments + 2; ++i) {
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
}
EXPECT_TRUE(TestSocketAndFifoExpectations(kSegments, 0, 2));
// Empty both. Should render a log call for emptying the fifo.
socket_->Read(kSegments);
writer_->Write(audio_bus_.get(), 0, false, base::TimeTicks::Now());
socket_->Read(3);
EXPECT_TRUE(TestSocketAndFifoExpectations(0, 3 * sizeof(uint32_t), 0));
}
} // namespace audio