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
|
// Copyright (c) 2012 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 <cmath>
#include "base/memory/scoped_ptr.h"
#include "media/audio/audio_util.h"
#include "media/base/audio_renderer_mixer.h"
#include "media/base/audio_renderer_mixer_input.h"
#include "media/base/fake_audio_render_callback.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
// Parameters which control the many input case tests.
static const int kMixerInputs = 64;
static const int kMixerCycles = 32;
static const int kBitsPerChannel = 16;
static const int kSampleRate = 48000;
static const ChannelLayout kChannelLayout = CHANNEL_LAYOUT_STEREO;
// Multiple rounds of addition result in precision loss with float values, so we
// need an epsilon such that if for all x f(x) = sum(x, m) and g(x) = m * x then
// fabs(f - g) < kEpsilon. The kEpsilon below has been tested with m < 128.
static const float kEpsilon = 0.00015f;
class MockAudioRendererSink : public AudioRendererSink {
public:
MOCK_METHOD0(Start, void());
MOCK_METHOD0(Stop, void());
MOCK_METHOD1(Pause, void(bool flush));
MOCK_METHOD0(Play, void());
MOCK_METHOD1(SetPlaybackRate, void(float rate));
MOCK_METHOD1(SetVolume, bool(double volume));
MOCK_METHOD1(GetVolume, void(double* volume));
void Initialize(const media::AudioParameters& params,
AudioRendererSink::RenderCallback* renderer) OVERRIDE {
// TODO(dalecurtis): Once we have resampling we need to ensure we are given
// an AudioParameters which reflects the hardware settings.
callback_ = renderer;
};
AudioRendererSink::RenderCallback* callback() {
return callback_;
}
void SimulateRenderError() {
callback_->OnRenderError();
}
protected:
virtual ~MockAudioRendererSink() {}
AudioRendererSink::RenderCallback* callback_;
};
class AudioRendererMixerTest : public ::testing::Test {
public:
AudioRendererMixerTest() {
audio_parameters_ = AudioParameters(
AudioParameters::AUDIO_PCM_LINEAR, kChannelLayout, kSampleRate,
kBitsPerChannel, GetHighLatencyOutputBufferSize(kSampleRate));
sink_ = new MockAudioRendererSink();
EXPECT_CALL(*sink_, Start());
EXPECT_CALL(*sink_, Stop());
mixer_ = new AudioRendererMixer(audio_parameters_, sink_);
mixer_callback_ = sink_->callback();
// TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to
// allocate these on 32-byte boundaries and ensure they're sized % 32 bytes.
audio_data_.reserve(audio_parameters_.channels());
for (int i = 0; i < audio_parameters_.channels(); ++i)
audio_data_.push_back(new float[audio_parameters_.frames_per_buffer()]);
fake_callback_.reset(new FakeAudioRenderCallback(audio_parameters_));
}
void InitializeInputs(int count) {
for (int i = 0; i < count; ++i) {
scoped_refptr<AudioRendererMixerInput> mixer_input(
new AudioRendererMixerInput(mixer_));
mixer_input->Initialize(audio_parameters_, fake_callback_.get());
mixer_input->SetVolume(1.0f);
mixer_inputs_.push_back(mixer_input);
}
}
bool ValidateAudioData(int start_index, int frames, float check_value) {
for (size_t i = 0; i < audio_data_.size(); ++i) {
for (int j = start_index; j < frames; j++) {
if (fabs(audio_data_[i][j] - check_value) > kEpsilon) {
EXPECT_NEAR(check_value, audio_data_[i][j], kEpsilon)
<< " i=" << i << ", j=" << j;
return false;
}
}
}
return true;
}
// Render audio_parameters_.frames_per_buffer() frames into |audio_data_| and
// verify the result against |check_value|.
bool RenderAndValidateAudioData(float check_value) {
int frames = mixer_callback_->Render(
audio_data_, audio_parameters_.frames_per_buffer(), 0);
return frames == audio_parameters_.frames_per_buffer() && ValidateAudioData(
0, audio_parameters_.frames_per_buffer(), check_value);
}
// Fill |audio_data_| fully with |value|.
void FillAudioData(float value) {
for (size_t i = 0; i < audio_data_.size(); ++i)
std::fill(audio_data_[i],
audio_data_[i] + audio_parameters_.frames_per_buffer(), value);
}
// Verify silence when mixer inputs are in pre-Start() and post-Start().
void StartTest(int inputs) {
InitializeInputs(inputs);
// Verify silence before any inputs have been started. Fill the buffer
// before hand with non-zero data to ensure we get zeros back.
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
// Start() all even numbered mixer inputs and ensure we still get silence.
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Start();
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
// Start() all mixer inputs and ensure we still get silence.
for (size_t i = 1; i < mixer_inputs_.size(); i += 2)
mixer_inputs_[i]->Start();
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
// Verify output when mixer inputs are in post-Play() state.
void PlayTest(int inputs) {
InitializeInputs(inputs);
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Start();
// Play() all even numbered mixer inputs and ensure we get the right value.
for (size_t i = 0; i < mixer_inputs_.size(); i += 2)
mixer_inputs_[i]->Play();
for (int i = 0; i < kMixerCycles; ++i) {
fake_callback_->NextFillValue();
ASSERT_TRUE(RenderAndValidateAudioData(
fake_callback_->fill_value() * std::max(
mixer_inputs_.size() / 2, static_cast<size_t>(1))));
}
// Play() all mixer inputs and ensure we still get the right values.
for (size_t i = 1; i < mixer_inputs_.size(); i += 2)
mixer_inputs_[i]->Play();
for (int i = 0; i < kMixerCycles; ++i) {
fake_callback_->NextFillValue();
ASSERT_TRUE(RenderAndValidateAudioData(
fake_callback_->fill_value() * mixer_inputs_.size()));
}
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
// Verify volume adjusted output when mixer inputs are in post-Play() state.
void PlayVolumeAdjustedTest(int inputs) {
InitializeInputs(inputs);
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
mixer_inputs_[i]->Start();
mixer_inputs_[i]->Play();
}
// Set a different volume for each mixer input and verify the results.
float total_scale = 0;
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
float volume = static_cast<float>(i) / mixer_inputs_.size();
total_scale += volume;
EXPECT_TRUE(mixer_inputs_[i]->SetVolume(volume));
}
for (int i = 0; i < kMixerCycles; ++i) {
fake_callback_->NextFillValue();
ASSERT_TRUE(RenderAndValidateAudioData(
fake_callback_->fill_value() * total_scale));
}
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
// Verify output when mixer inputs can only partially fulfill a Render().
void PlayPartialRenderTest(int inputs) {
InitializeInputs(inputs);
int frames = audio_parameters_.frames_per_buffer();
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
mixer_inputs_[i]->Start();
mixer_inputs_[i]->Play();
}
// Verify a properly filled buffer when half filled (remainder zeroed).
fake_callback_->set_half_fill(true);
for (int i = 0; i < kMixerCycles; ++i) {
fake_callback_->NextFillValue();
ASSERT_EQ(mixer_callback_->Render(audio_data_, frames, 0), frames);
ASSERT_TRUE(ValidateAudioData(
0, frames / 2, fake_callback_->fill_value() * mixer_inputs_.size()));
ASSERT_TRUE(ValidateAudioData(
frames / 2, frames, 0.0f));
}
fake_callback_->set_half_fill(false);
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
// Verify output when mixer inputs are in Pause() state.
void PauseTest(int inputs) {
InitializeInputs(inputs);
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
mixer_inputs_[i]->Start();
mixer_inputs_[i]->Play();
}
// Pause() all even numbered mixer inputs and ensure we get the right value.
for (size_t i = 0; i < mixer_inputs_.size(); i += 2)
mixer_inputs_[i]->Pause(false);
for (int i = 0; i < kMixerCycles; ++i) {
fake_callback_->NextFillValue();
ASSERT_TRUE(RenderAndValidateAudioData(
fake_callback_->fill_value() * (mixer_inputs_.size() / 2)));
}
// Pause() all the inputs and verify we get silence back.
for (size_t i = 1; i < mixer_inputs_.size(); i += 2)
mixer_inputs_[i]->Pause(false);
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Stop();
}
// Verify output when mixer inputs are in post-Stop() state.
void StopTest(int inputs) {
InitializeInputs(inputs);
// Start() and Stop() all inputs.
for (size_t i = 0; i < mixer_inputs_.size(); ++i) {
mixer_inputs_[i]->Start();
mixer_inputs_[i]->Stop();
}
// Verify we get silence back; fill |audio_data_| before hand to be sure.
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
}
protected:
virtual ~AudioRendererMixerTest() {
for (size_t i = 0; i < audio_data_.size(); ++i)
delete [] audio_data_[i];
}
scoped_refptr<MockAudioRendererSink> sink_;
scoped_refptr<AudioRendererMixer> mixer_;
AudioRendererSink::RenderCallback* mixer_callback_;
scoped_ptr<FakeAudioRenderCallback> fake_callback_;
AudioParameters audio_parameters_;
std::vector<float*> audio_data_;
std::vector< scoped_refptr<AudioRendererMixerInput> > mixer_inputs_;
DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerTest);
};
// Verify a mixer with no inputs returns silence for all requested frames.
TEST_F(AudioRendererMixerTest, NoInputs) {
FillAudioData(1.0f);
EXPECT_TRUE(RenderAndValidateAudioData(0.0f));
}
// Test mixer output with one input in the pre-Start() and post-Start() state.
TEST_F(AudioRendererMixerTest, OneInputStart) {
StartTest(1);
}
// Test mixer output with many inputs in the pre-Start() and post-Start() state.
TEST_F(AudioRendererMixerTest, ManyInputStart) {
StartTest(kMixerInputs);
}
// Test mixer output with one input in the post-Play() state.
TEST_F(AudioRendererMixerTest, OneInputPlay) {
PlayTest(1);
}
// Test mixer output with many inputs in the post-Play() state.
TEST_F(AudioRendererMixerTest, ManyInputPlay) {
PlayTest(kMixerInputs);
}
// Test volume adjusted mixer output with one input in the post-Play() state.
TEST_F(AudioRendererMixerTest, OneInputPlayVolumeAdjusted) {
PlayVolumeAdjustedTest(1);
}
// Test volume adjusted mixer output with many inputs in the post-Play() state.
TEST_F(AudioRendererMixerTest, ManyInputPlayVolumeAdjusted) {
PlayVolumeAdjustedTest(kMixerInputs);
}
// Test mixer output with one input and partial Render() in post-Play() state.
TEST_F(AudioRendererMixerTest, OneInputPlayPartialRender) {
PlayPartialRenderTest(1);
}
// Test mixer output with many inputs and partial Render() in post-Play() state.
TEST_F(AudioRendererMixerTest, ManyInputPlayPartialRender) {
PlayPartialRenderTest(kMixerInputs);
}
// Test mixer output with one input in the post-Pause() state.
TEST_F(AudioRendererMixerTest, OneInputPause) {
PauseTest(1);
}
// Test mixer output with many inputs in the post-Pause() state.
TEST_F(AudioRendererMixerTest, ManyInputPause) {
PauseTest(kMixerInputs);
}
// Test mixer output with one input in the post-Stop() state.
TEST_F(AudioRendererMixerTest, OneInputStop) {
StopTest(1);
}
// Test mixer output with many inputs in the post-Stop() state.
TEST_F(AudioRendererMixerTest, ManyInputStop) {
StopTest(kMixerInputs);
}
// Test mixer with many inputs in mixed post-Stop() and post-Play() states.
TEST_F(AudioRendererMixerTest, ManyInputMixedStopPlay) {
InitializeInputs(kMixerInputs);
// Start() all inputs.
for (size_t i = 0; i < mixer_inputs_.size(); ++i)
mixer_inputs_[i]->Start();
// Stop() all even numbered mixer inputs and Play() all odd numbered inputs
// and ensure we get the right value.
for (size_t i = 1; i < mixer_inputs_.size(); i += 2) {
mixer_inputs_[i - 1]->Stop();
mixer_inputs_[i]->Play();
}
for (int i = 0; i < kMixerCycles; ++i) {
fake_callback_->NextFillValue();
ASSERT_TRUE(RenderAndValidateAudioData(
fake_callback_->fill_value() * std::max(
mixer_inputs_.size() / 2, static_cast<size_t>(1))));
}
for (size_t i = 1; i < mixer_inputs_.size(); i += 2)
mixer_inputs_[i]->Stop();
}
TEST_F(AudioRendererMixerTest, OnRenderError) {
std::vector< scoped_refptr<AudioRendererMixerInput> > mixer_inputs;
for (int i = 0; i < kMixerInputs; ++i) {
scoped_refptr<AudioRendererMixerInput> mixer_input(
new AudioRendererMixerInput(mixer_));
mixer_input->Initialize(audio_parameters_, fake_callback_.get());
mixer_input->SetVolume(1.0f);
mixer_input->Start();
mixer_inputs_.push_back(mixer_input);
}
EXPECT_CALL(*fake_callback_, OnRenderError()).Times(kMixerInputs);
sink_->SimulateRenderError();
for (int i = 0; i < kMixerInputs; ++i)
mixer_inputs_[i]->Stop();
}
} // namespace media
|