1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6 #include <gtest/gtest.h>
7
8 extern "C" {
9 #include "cras_iodev.h"
10 #include "cras_ramp.h"
11 #include "cras_rstream.h"
12 #include "dev_stream.h"
13 #include "utlist.h"
14 #include "cras_audio_area.h"
15 #include "audio_thread_log.h"
16 #include "input_data.h"
17
18 // Mock software volume scalers.
19 float softvol_scalers[101];
20 }
21
22 #define BUFFER_SIZE 8192
23
24 static const float RAMP_UNMUTE_DURATION_SECS = 0.5;
25 static const float RAMP_NEW_STREAM_DURATION_SECS = 0.01;
26 static const float RAMP_MUTE_DURATION_SECS = 0.1;
27
28 static int cras_iodev_list_disable_dev_called;
29 static int select_node_called;
30 static enum CRAS_STREAM_DIRECTION select_node_direction;
31 static cras_node_id_t select_node_id;
32 static struct cras_ionode *node_selected;
33 static size_t notify_nodes_changed_called;
34 static size_t notify_active_node_changed_called;
35 static size_t notify_node_volume_called;
36 static size_t notify_node_capture_gain_called;
37 static int dsp_context_new_sample_rate;
38 static const char *dsp_context_new_purpose;
39 static int dsp_context_free_called;
40 static int update_channel_layout_called;
41 static int update_channel_layout_return_val;
42 static int set_swap_mode_for_node_called;
43 static int set_swap_mode_for_node_enable;
44 static int notify_node_left_right_swapped_called;
45 static int cras_audio_format_set_channel_layout_called;
46 static unsigned int cras_system_get_volume_return;
47 static int cras_dsp_get_pipeline_called;
48 static int cras_dsp_get_pipeline_ret;
49 static int cras_dsp_put_pipeline_called;
50 static int cras_dsp_pipeline_get_source_buffer_called;
51 static int cras_dsp_pipeline_get_sink_buffer_called;
52 static float cras_dsp_pipeline_source_buffer[2][DSP_BUFFER_SIZE];
53 static float cras_dsp_pipeline_sink_buffer[2][DSP_BUFFER_SIZE];
54 static int cras_dsp_pipeline_get_delay_called;
55 static int cras_dsp_pipeline_apply_called;
56 static int cras_dsp_pipeline_set_sink_ext_module_called;
57 static int cras_dsp_pipeline_apply_sample_count;
58 static unsigned int cras_mix_mute_count;
59 static unsigned int cras_dsp_num_input_channels_return;
60 static unsigned int cras_dsp_num_output_channels_return;
61 struct cras_dsp_context *cras_dsp_context_new_return;
62 static unsigned int cras_dsp_load_dummy_pipeline_called;
63 static unsigned int rate_estimator_add_frames_num_frames;
64 static unsigned int rate_estimator_add_frames_called;
65 static int cras_system_get_mute_return;
66 static snd_pcm_format_t cras_scale_buffer_fmt;
67 static float cras_scale_buffer_scaler;
68 static int cras_scale_buffer_called;
69 static unsigned int pre_dsp_hook_called;
70 static const uint8_t *pre_dsp_hook_frames;
71
72 static void *pre_dsp_hook_cb_data;
73 static unsigned int post_dsp_hook_called;
74 static const uint8_t *post_dsp_hook_frames;
75 static void *post_dsp_hook_cb_data;
76 static int iodev_buffer_size;
77 static long cras_system_get_capture_gain_ret_value;
78 static uint8_t audio_buffer[BUFFER_SIZE];
79 static struct cras_audio_area *audio_area;
80 static unsigned int put_buffer_nframes;
81 static int output_should_wake_ret;
82 static int no_stream_called;
83 static int no_stream_enable;
84 // This will be used extensively in cras_iodev.
85 struct audio_thread_event_log *atlog;
86 static unsigned int simple_no_stream_called;
87 static int simple_no_stream_enable;
88 static int dev_stream_playback_frames_ret;
89 static int get_num_underruns_ret;
90 static int device_monitor_reset_device_called;
91 static int output_underrun_called;
92 static int set_mute_called;
93 static int cras_ramp_start_is_up;
94 static int cras_ramp_start_duration_frames;
95 static int cras_ramp_start_is_called;
96 static int cras_ramp_reset_is_called;
97 static struct cras_ramp_action cras_ramp_get_current_action_ret;
98 static int cras_ramp_update_ramped_frames_num_frames;
99 static cras_ramp_cb cras_ramp_start_cb;
100 static void* cras_ramp_start_cb_data;
101 static int cras_device_monitor_set_device_mute_state_called;
102 static struct cras_iodev* cras_device_monitor_set_device_mute_state_dev;
103 static snd_pcm_format_t cras_scale_buffer_increment_fmt;
104 static uint8_t *cras_scale_buffer_increment_buff;
105 static unsigned int cras_scale_buffer_increment_frame;
106 static float cras_scale_buffer_increment_scaler;
107 static float cras_scale_buffer_increment_increment;
108 static int cras_scale_buffer_increment_channel;
109 static struct cras_audio_format audio_fmt;
110 static int buffer_share_add_id_called;
111 static int buffer_share_get_new_write_point_ret;
112 static int ext_mod_configure_called;
113 static struct input_data *input_data_create_ret;
114
115 // Iodev callback
update_channel_layout(struct cras_iodev * iodev)116 int update_channel_layout(struct cras_iodev *iodev) {
117 update_channel_layout_called = 1;
118 return update_channel_layout_return_val;
119 }
120
121 // Iodev callback
set_swap_mode_for_node(struct cras_iodev * iodev,struct cras_ionode * node,int enable)122 int set_swap_mode_for_node(struct cras_iodev *iodev, struct cras_ionode *node,
123 int enable)
124 {
125 set_swap_mode_for_node_called++;
126 set_swap_mode_for_node_enable = enable;
127 return 0;
128 }
129
ResetStubData()130 void ResetStubData() {
131 cras_iodev_list_disable_dev_called = 0;
132 select_node_called = 0;
133 notify_nodes_changed_called = 0;
134 notify_active_node_changed_called = 0;
135 notify_node_volume_called = 0;
136 notify_node_capture_gain_called = 0;
137 dsp_context_new_sample_rate = 0;
138 dsp_context_new_purpose = NULL;
139 dsp_context_free_called = 0;
140 set_swap_mode_for_node_called = 0;
141 set_swap_mode_for_node_enable = 0;
142 notify_node_left_right_swapped_called = 0;
143 cras_audio_format_set_channel_layout_called = 0;
144 cras_dsp_get_pipeline_called = 0;
145 cras_dsp_get_pipeline_ret = 0;
146 cras_dsp_put_pipeline_called = 0;
147 cras_dsp_pipeline_get_source_buffer_called = 0;
148 cras_dsp_pipeline_get_sink_buffer_called = 0;
149 memset(&cras_dsp_pipeline_source_buffer, 0,
150 sizeof(cras_dsp_pipeline_source_buffer));
151 memset(&cras_dsp_pipeline_sink_buffer, 0,
152 sizeof(cras_dsp_pipeline_sink_buffer));
153 cras_dsp_pipeline_get_delay_called = 0;
154 cras_dsp_pipeline_apply_called = 0;
155 cras_dsp_pipeline_set_sink_ext_module_called = 0;
156 cras_dsp_pipeline_apply_sample_count = 0;
157 cras_dsp_num_input_channels_return = 2;
158 cras_dsp_num_output_channels_return = 2;
159 cras_dsp_context_new_return = NULL;
160 cras_dsp_load_dummy_pipeline_called = 0;
161 rate_estimator_add_frames_num_frames = 0;
162 rate_estimator_add_frames_called = 0;
163 cras_system_get_mute_return = 0;
164 cras_system_get_volume_return = 100;
165 cras_mix_mute_count = 0;
166 pre_dsp_hook_called = 0;
167 pre_dsp_hook_frames = NULL;
168 post_dsp_hook_called = 0;
169 post_dsp_hook_frames = NULL; iodev_buffer_size = 0;
170 cras_system_get_capture_gain_ret_value = 0;
171 // Assume there is some data in audio buffer.
172 memset(audio_buffer, 0xff, sizeof(audio_buffer));
173 if (audio_area) {
174 free(audio_area);
175 audio_area = NULL;
176 }
177 put_buffer_nframes = 0;
178 output_should_wake_ret= 0;
179 no_stream_called = 0;
180 no_stream_enable = 0;
181 simple_no_stream_called = 0;
182 simple_no_stream_enable = 0;
183 dev_stream_playback_frames_ret = 0;
184 if (!atlog)
185 atlog = audio_thread_event_log_init();
186 get_num_underruns_ret = 0;
187 device_monitor_reset_device_called = 0;
188 output_underrun_called = 0;
189 set_mute_called = 0;
190 cras_ramp_start_is_up = 0;
191 cras_ramp_start_duration_frames = 0;
192 cras_ramp_start_cb = NULL;
193 cras_ramp_start_cb_data = NULL;
194 cras_ramp_start_is_called = 0;
195 cras_ramp_reset_is_called = 0;
196 cras_ramp_get_current_action_ret.type = CRAS_RAMP_ACTION_NONE;
197 cras_ramp_update_ramped_frames_num_frames = 0;
198 cras_device_monitor_set_device_mute_state_called = 0;
199 cras_device_monitor_set_device_mute_state_dev = NULL;
200 cras_scale_buffer_called = 0;
201 cras_scale_buffer_increment_fmt = SND_PCM_FORMAT_UNKNOWN;
202 cras_scale_buffer_increment_buff = NULL;
203 cras_scale_buffer_increment_frame = 0;
204 cras_scale_buffer_increment_scaler = 0;
205 cras_scale_buffer_increment_increment = 0;
206 cras_scale_buffer_increment_channel = 0;
207 audio_fmt.format = SND_PCM_FORMAT_S16_LE;
208 audio_fmt.frame_rate = 48000;
209 audio_fmt.num_channels = 2;
210 buffer_share_add_id_called = 0;
211 ext_mod_configure_called = 0;
212 }
213
214 namespace {
215
216 // Test fill_time_from_frames
TEST(IoDevTestSuite,FillTimeFromFramesNormal)217 TEST(IoDevTestSuite, FillTimeFromFramesNormal) {
218 struct timespec ts;
219
220 cras_iodev_fill_time_from_frames(12000, 48000, &ts);
221 EXPECT_EQ(0, ts.tv_sec);
222 EXPECT_GE(ts.tv_nsec, 249900000);
223 EXPECT_LE(ts.tv_nsec, 250100000);
224 }
225
TEST(IoDevTestSuite,FillTimeFromFramesLong)226 TEST(IoDevTestSuite, FillTimeFromFramesLong) {
227 struct timespec ts;
228
229 cras_iodev_fill_time_from_frames(120000 - 12000, 48000, &ts);
230 EXPECT_EQ(2, ts.tv_sec);
231 EXPECT_GE(ts.tv_nsec, 249900000);
232 EXPECT_LE(ts.tv_nsec, 250100000);
233 }
234
TEST(IoDevTestSuite,FillTimeFromFramesShort)235 TEST(IoDevTestSuite, FillTimeFromFramesShort) {
236 struct timespec ts;
237
238 cras_iodev_fill_time_from_frames(12000 - 12000, 48000, &ts);
239 EXPECT_EQ(0, ts.tv_sec);
240 EXPECT_EQ(0, ts.tv_nsec);
241 }
242
243 class IoDevSetFormatTestSuite : public testing::Test {
244 protected:
SetUp()245 virtual void SetUp() {
246 ResetStubData();
247 sample_rates_[0] = 44100;
248 sample_rates_[1] = 48000;
249 sample_rates_[2] = 0;
250
251 channel_counts_[0] = 2;
252 channel_counts_[1] = 0;
253 channel_counts_[2] = 0;
254
255 pcm_formats_[0] = SND_PCM_FORMAT_S16_LE;
256 pcm_formats_[1] = SND_PCM_FORMAT_S32_LE;
257 pcm_formats_[2] = static_cast<snd_pcm_format_t>(0);
258
259 update_channel_layout_called = 0;
260 update_channel_layout_return_val = 0;
261
262 memset(&iodev_, 0, sizeof(iodev_));
263 iodev_.update_channel_layout = update_channel_layout;
264 iodev_.supported_rates = sample_rates_;
265 iodev_.supported_channel_counts = channel_counts_;
266 iodev_.supported_formats = pcm_formats_;
267 iodev_.dsp_context = NULL;
268
269 cras_audio_format_set_channel_layout_called = 0;
270 }
271
TearDown()272 virtual void TearDown() {
273 cras_iodev_free_format(&iodev_);
274 }
275
276 struct cras_iodev iodev_;
277 size_t sample_rates_[3];
278 size_t channel_counts_[3];
279 snd_pcm_format_t pcm_formats_[3];
280 };
281
TEST_F(IoDevSetFormatTestSuite,SupportedFormatSecondary)282 TEST_F(IoDevSetFormatTestSuite, SupportedFormatSecondary) {
283 struct cras_audio_format fmt;
284 int rc;
285
286 fmt.format = SND_PCM_FORMAT_S16_LE;
287 fmt.frame_rate = 48000;
288 fmt.num_channels = 2;
289 iodev_.direction = CRAS_STREAM_OUTPUT;
290 ResetStubData();
291 rc = cras_iodev_set_format(&iodev_, &fmt);
292 EXPECT_EQ(0, rc);
293 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
294 EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
295 EXPECT_EQ(2, iodev_.ext_format->num_channels);
296 EXPECT_EQ(dsp_context_new_sample_rate, 48000);
297 EXPECT_STREQ(dsp_context_new_purpose, "playback");
298 }
299
TEST_F(IoDevSetFormatTestSuite,SupportedFormat32bit)300 TEST_F(IoDevSetFormatTestSuite, SupportedFormat32bit) {
301 struct cras_audio_format fmt;
302 int rc;
303
304 fmt.format = SND_PCM_FORMAT_S32_LE;
305 fmt.frame_rate = 48000;
306 fmt.num_channels = 2;
307 iodev_.direction = CRAS_STREAM_OUTPUT;
308 ResetStubData();
309 rc = cras_iodev_set_format(&iodev_, &fmt);
310 EXPECT_EQ(0, rc);
311 EXPECT_EQ(SND_PCM_FORMAT_S32_LE, iodev_.ext_format->format);
312 EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
313 EXPECT_EQ(2, iodev_.ext_format->num_channels);
314 EXPECT_EQ(dsp_context_new_sample_rate, 48000);
315 EXPECT_STREQ(dsp_context_new_purpose, "playback");
316 }
317
TEST_F(IoDevSetFormatTestSuite,SupportedFormatPrimary)318 TEST_F(IoDevSetFormatTestSuite, SupportedFormatPrimary) {
319 struct cras_audio_format fmt;
320 int rc;
321
322 fmt.format = SND_PCM_FORMAT_S16_LE;
323 fmt.frame_rate = 44100;
324 fmt.num_channels = 2;
325 iodev_.direction = CRAS_STREAM_INPUT;
326 ResetStubData();
327 rc = cras_iodev_set_format(&iodev_, &fmt);
328 EXPECT_EQ(0, rc);
329 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
330 EXPECT_EQ(44100, iodev_.ext_format->frame_rate);
331 EXPECT_EQ(2, iodev_.ext_format->num_channels);
332 EXPECT_EQ(dsp_context_new_sample_rate, 44100);
333 EXPECT_STREQ(dsp_context_new_purpose, "capture");
334 }
335
TEST_F(IoDevSetFormatTestSuite,SupportedFormatDivisor)336 TEST_F(IoDevSetFormatTestSuite, SupportedFormatDivisor) {
337 struct cras_audio_format fmt;
338 int rc;
339
340 fmt.format = SND_PCM_FORMAT_S16_LE;
341 fmt.frame_rate = 96000;
342 fmt.num_channels = 2;
343 rc = cras_iodev_set_format(&iodev_, &fmt);
344 EXPECT_EQ(0, rc);
345 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
346 EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
347 EXPECT_EQ(2, iodev_.ext_format->num_channels);
348 }
349
TEST_F(IoDevSetFormatTestSuite,Supported96k)350 TEST_F(IoDevSetFormatTestSuite, Supported96k) {
351 struct cras_audio_format fmt;
352 int rc;
353
354 sample_rates_[0] = 48000;
355 sample_rates_[1] = 96000;
356 sample_rates_[2] = 0;
357
358 fmt.format = SND_PCM_FORMAT_S16_LE;
359 fmt.frame_rate = 96000;
360 fmt.num_channels = 2;
361 rc = cras_iodev_set_format(&iodev_, &fmt);
362 EXPECT_EQ(0, rc);
363 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
364 EXPECT_EQ(96000, iodev_.ext_format->frame_rate);
365 EXPECT_EQ(2, iodev_.ext_format->num_channels);
366 }
367
TEST_F(IoDevSetFormatTestSuite,LimitLowRate)368 TEST_F(IoDevSetFormatTestSuite, LimitLowRate) {
369 struct cras_audio_format fmt;
370 int rc;
371
372 sample_rates_[0] = 48000;
373 sample_rates_[1] = 8000;
374 sample_rates_[2] = 0;
375
376 fmt.format = SND_PCM_FORMAT_S16_LE;
377 fmt.frame_rate = 8000;
378 fmt.num_channels = 2;
379 rc = cras_iodev_set_format(&iodev_, &fmt);
380 EXPECT_EQ(0, rc);
381 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
382 EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
383 EXPECT_EQ(2, iodev_.ext_format->num_channels);
384 }
385
TEST_F(IoDevSetFormatTestSuite,UnsupportedChannelCount)386 TEST_F(IoDevSetFormatTestSuite, UnsupportedChannelCount) {
387 struct cras_audio_format fmt;
388 int rc;
389
390 fmt.format = SND_PCM_FORMAT_S16_LE;
391 fmt.frame_rate = 96000;
392 fmt.num_channels = 1;
393 rc = cras_iodev_set_format(&iodev_, &fmt);
394 EXPECT_EQ(0, rc);
395 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
396 EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
397 EXPECT_EQ(2, iodev_.ext_format->num_channels);
398 }
399
TEST_F(IoDevSetFormatTestSuite,SupportedFormatFallbackDefault)400 TEST_F(IoDevSetFormatTestSuite, SupportedFormatFallbackDefault) {
401 struct cras_audio_format fmt;
402 int rc;
403
404 fmt.format = SND_PCM_FORMAT_S16_LE;
405 fmt.frame_rate = 96008;
406 fmt.num_channels = 2;
407 rc = cras_iodev_set_format(&iodev_, &fmt);
408 EXPECT_EQ(0, rc);
409 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
410 EXPECT_EQ(44100, iodev_.ext_format->frame_rate);
411 EXPECT_EQ(2, iodev_.ext_format->num_channels);
412 }
413
TEST_F(IoDevSetFormatTestSuite,OutputDSPChannleReduction)414 TEST_F(IoDevSetFormatTestSuite, OutputDSPChannleReduction) {
415 struct cras_audio_format fmt;
416 int rc;
417
418 fmt.format = SND_PCM_FORMAT_S16_LE;
419 fmt.frame_rate = 48000;
420 fmt.num_channels = 2;
421
422 iodev_.direction = CRAS_STREAM_OUTPUT;
423 iodev_.supported_channel_counts[0] = 1;
424 iodev_.supported_channel_counts[1] = 0;
425 cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf00);
426 cras_dsp_get_pipeline_ret = 0xf01;
427 cras_dsp_num_input_channels_return = 2;
428 cras_dsp_num_output_channels_return = 1;
429 rc = cras_iodev_set_format(&iodev_, &fmt);
430 EXPECT_EQ(0, rc);
431 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
432 EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
433 EXPECT_EQ(2, iodev_.ext_format->num_channels);
434 }
435
TEST_F(IoDevSetFormatTestSuite,InputDSPChannleReduction)436 TEST_F(IoDevSetFormatTestSuite, InputDSPChannleReduction) {
437 struct cras_audio_format fmt;
438 int rc;
439
440 fmt.format = SND_PCM_FORMAT_S16_LE;
441 fmt.frame_rate = 48000;
442 fmt.num_channels = 2;
443
444 iodev_.direction = CRAS_STREAM_INPUT;
445 iodev_.supported_channel_counts[0] = 10;
446 iodev_.supported_channel_counts[1] = 0;
447 cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf00);
448 cras_dsp_get_pipeline_ret = 0xf01;
449 cras_dsp_num_input_channels_return = 10;
450 cras_dsp_num_output_channels_return = 2;
451 rc = cras_iodev_set_format(&iodev_, &fmt);
452 EXPECT_EQ(0, rc);
453 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
454 EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
455 EXPECT_EQ(2, iodev_.ext_format->num_channels);
456 }
457
TEST_F(IoDevSetFormatTestSuite,UpdateChannelLayoutSuccess)458 TEST_F(IoDevSetFormatTestSuite, UpdateChannelLayoutSuccess) {
459 struct cras_audio_format fmt;
460 int rc;
461
462 fmt.format = SND_PCM_FORMAT_S16_LE;
463 fmt.frame_rate = 48000;
464 fmt.num_channels = 6;
465
466 iodev_.supported_channel_counts[0] = 6;
467 iodev_.supported_channel_counts[1] = 2;
468
469 rc = cras_iodev_set_format(&iodev_, &fmt);
470 EXPECT_EQ(0, rc);
471 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
472 EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
473 EXPECT_EQ(6, iodev_.ext_format->num_channels);
474 }
475
TEST_F(IoDevSetFormatTestSuite,UpdateChannelLayoutFail)476 TEST_F(IoDevSetFormatTestSuite, UpdateChannelLayoutFail) {
477 static const int8_t stereo_layout[] =
478 {0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
479 struct cras_audio_format fmt;
480 int rc, i;
481
482 fmt.format = SND_PCM_FORMAT_S16_LE;
483 fmt.frame_rate = 48000;
484 fmt.num_channels = 2;
485
486 cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf0f);
487
488 update_channel_layout_return_val = -1;
489 iodev_.supported_channel_counts[0] = 6;
490 iodev_.supported_channel_counts[1] = 2;
491
492 rc = cras_iodev_set_format(&iodev_, &fmt);
493 EXPECT_EQ(0, rc);
494 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
495 EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
496 EXPECT_EQ(2, iodev_.ext_format->num_channels);
497 EXPECT_EQ(0, dsp_context_free_called);
498 for (i = 0; i < CRAS_CH_MAX; i++) {
499 EXPECT_EQ(iodev_.format->channel_layout[i], stereo_layout[i]);
500 EXPECT_EQ(iodev_.ext_format->channel_layout[i], stereo_layout[i]);
501 }
502 }
503
TEST_F(IoDevSetFormatTestSuite,UpdateChannelLayoutFail6ch)504 TEST_F(IoDevSetFormatTestSuite, UpdateChannelLayoutFail6ch) {
505 static const int8_t default_6ch_layout[] =
506 {0, 1, 2, 3, 4, 5, -1, -1, -1, -1, -1};
507 struct cras_audio_format fmt;
508 int rc, i;
509
510 fmt.format = SND_PCM_FORMAT_S16_LE;
511 fmt.frame_rate = 48000;
512 fmt.num_channels = 6;
513
514 cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf0f);
515
516 update_channel_layout_return_val = -1;
517 iodev_.supported_channel_counts[0] = 6;
518 iodev_.supported_channel_counts[1] = 2;
519
520 rc = cras_iodev_set_format(&iodev_, &fmt);
521 EXPECT_EQ(0, rc);
522 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
523 EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
524 EXPECT_EQ(6, iodev_.ext_format->num_channels);
525 EXPECT_EQ(0, dsp_context_free_called);
526 for (i = 0; i < CRAS_CH_MAX; i++) {
527 EXPECT_EQ(iodev_.format->channel_layout[i], default_6ch_layout[i]);
528 EXPECT_EQ(iodev_.ext_format->channel_layout[i], default_6ch_layout[i]);
529 }
530 }
531
532 // Put buffer tests
533
get_buffer(cras_iodev * iodev,struct cras_audio_area ** area,unsigned int * num)534 static int get_buffer(cras_iodev* iodev, struct cras_audio_area** area,
535 unsigned int* num) {
536 size_t sz = sizeof(*audio_area) + sizeof(struct cras_channel_area) * 2;
537
538 audio_area = (cras_audio_area*)calloc(1, sz);
539 audio_area->frames = *num;
540 audio_area->num_channels = 2;
541 audio_area->channels[0].buf = audio_buffer;
542 channel_area_set_channel(&audio_area->channels[0], CRAS_CH_FL);
543 audio_area->channels[0].step_bytes = 4;
544 audio_area->channels[1].buf = audio_buffer + 2;
545 channel_area_set_channel(&audio_area->channels[1], CRAS_CH_FR);
546 audio_area->channels[1].step_bytes = 4;
547
548 *area = audio_area;
549 return 0;
550 }
551
put_buffer(struct cras_iodev * iodev,unsigned int nframes)552 static int put_buffer(struct cras_iodev *iodev, unsigned int nframes)
553 {
554 put_buffer_nframes = nframes;
555 if (audio_area) {
556 free(audio_area);
557 audio_area = NULL;
558 }
559 return 0;
560 }
561
no_stream(struct cras_iodev * odev,int enable)562 static int no_stream(struct cras_iodev *odev, int enable)
563 {
564 no_stream_called++;
565 no_stream_enable = enable;
566 // Use default no stream playback to test default behavior.
567 return cras_iodev_default_no_stream_playback(odev, enable);
568 }
569
output_should_wake(const struct cras_iodev * odev)570 static int output_should_wake(const struct cras_iodev *odev)
571 {
572 return output_should_wake_ret;
573 }
574
pre_dsp_hook(const uint8_t * frames,unsigned int nframes,const struct cras_audio_format * fmt,void * cb_data)575 static int pre_dsp_hook(const uint8_t *frames, unsigned int nframes,
576 const struct cras_audio_format *fmt, void *cb_data)
577 {
578 pre_dsp_hook_called++;
579 pre_dsp_hook_frames = frames;
580 pre_dsp_hook_cb_data = cb_data;
581 return 0;
582 }
583
post_dsp_hook(const uint8_t * frames,unsigned int nframes,const struct cras_audio_format * fmt,void * cb_data)584 static int post_dsp_hook(const uint8_t *frames, unsigned int nframes,
585 const struct cras_audio_format *fmt, void *cb_data)
586 {
587 post_dsp_hook_called++;
588 post_dsp_hook_frames = frames;
589 post_dsp_hook_cb_data = cb_data;
590 return 0;
591 }
592
TEST(IoDevPutOutputBuffer,SystemMuted)593 TEST(IoDevPutOutputBuffer, SystemMuted) {
594 struct cras_audio_format fmt;
595 struct cras_iodev iodev;
596 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
597 int rc;
598
599 ResetStubData();
600 memset(&iodev, 0, sizeof(iodev));
601 cras_system_get_mute_return = 1;
602
603 fmt.format = SND_PCM_FORMAT_S16_LE;
604 fmt.frame_rate = 48000;
605 fmt.num_channels = 2;
606 iodev.format = &fmt;
607 iodev.put_buffer = put_buffer;
608
609 rc = cras_iodev_put_output_buffer(&iodev, frames, 20, NULL, nullptr);
610 EXPECT_EQ(0, rc);
611 EXPECT_EQ(20, cras_mix_mute_count);
612 EXPECT_EQ(20, put_buffer_nframes);
613 EXPECT_EQ(20, rate_estimator_add_frames_num_frames);
614 }
615
TEST(IoDevPutOutputBuffer,MuteForVolume)616 TEST(IoDevPutOutputBuffer, MuteForVolume) {
617 struct cras_iodev iodev;
618 struct cras_ionode ionode;
619
620 ResetStubData();
621 memset(&iodev, 0, sizeof(iodev));
622 memset(&ionode, 0, sizeof(ionode));
623
624 iodev.nodes = &ionode;
625 iodev.active_node = &ionode;
626 iodev.active_node->dev = &iodev;
627
628 // Case: System volume 100; Node volume 0. => Mute
629 cras_system_get_volume_return = 100;
630 iodev.active_node->volume = 0;
631 EXPECT_EQ(1, cras_iodev_is_zero_volume(&iodev));
632
633 // Case: System volume 100; Node volume 50. => Not mute
634 cras_system_get_volume_return = 100;
635 iodev.active_node->volume = 50;
636 EXPECT_EQ(0, cras_iodev_is_zero_volume(&iodev));
637
638 // Case: System volume 0; Node volume 50. => Mute
639 cras_system_get_volume_return = 0;
640 iodev.active_node->volume = 50;
641 EXPECT_EQ(1, cras_iodev_is_zero_volume(&iodev));
642
643 // Case: System volume 50; Node volume 50. => Mute
644 cras_system_get_volume_return = 50;
645 iodev.active_node->volume = 50;
646 EXPECT_EQ(1, cras_iodev_is_zero_volume(&iodev));
647 }
648
TEST(IoDevPutOutputBuffer,NodeVolumeZeroShouldMute)649 TEST(IoDevPutOutputBuffer, NodeVolumeZeroShouldMute) {
650 struct cras_audio_format fmt;
651 struct cras_iodev iodev;
652 struct cras_ionode ionode;
653 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
654 int rc;
655
656 ResetStubData();
657 memset(&iodev, 0, sizeof(iodev));
658 memset(&ionode, 0, sizeof(ionode));
659
660 iodev.nodes = &ionode;
661 iodev.active_node = &ionode;
662 iodev.active_node->dev = &iodev;
663 iodev.active_node->volume = 0;
664
665 fmt.format = SND_PCM_FORMAT_S16_LE;
666 fmt.frame_rate = 48000;
667 fmt.num_channels = 2;
668 iodev.format = &fmt;
669 iodev.put_buffer = put_buffer;
670
671 rc = cras_iodev_put_output_buffer(&iodev, frames, 20, NULL, nullptr);
672 EXPECT_EQ(0, rc);
673 EXPECT_EQ(20, cras_mix_mute_count);
674 EXPECT_EQ(20, put_buffer_nframes);
675 EXPECT_EQ(20, rate_estimator_add_frames_num_frames);
676 }
677
TEST(IoDevPutOutputBuffer,SystemMutedWithRamp)678 TEST(IoDevPutOutputBuffer, SystemMutedWithRamp) {
679 struct cras_audio_format fmt;
680 struct cras_iodev iodev;
681 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
682 int rc;
683
684 ResetStubData();
685 memset(&iodev, 0, sizeof(iodev));
686 cras_system_get_mute_return = 1;
687
688 fmt.format = SND_PCM_FORMAT_S16_LE;
689 fmt.frame_rate = 48000;
690 fmt.num_channels = 2;
691 iodev.format = &fmt;
692 iodev.put_buffer = put_buffer;
693
694 // Assume device has ramp member.
695 iodev.ramp = reinterpret_cast<struct cras_ramp*>(0x1);
696
697 // Assume ramping is done.
698 cras_ramp_get_current_action_ret.type = CRAS_RAMP_ACTION_NONE;
699
700 rc = cras_iodev_put_output_buffer(&iodev, frames, 20, NULL, nullptr);
701 // Output should be muted.
702 EXPECT_EQ(0, rc);
703 EXPECT_EQ(20, cras_mix_mute_count);
704 EXPECT_EQ(20, put_buffer_nframes);
705 EXPECT_EQ(20, rate_estimator_add_frames_num_frames);
706
707 // Test for the case where ramping is not done yet.
708 ResetStubData();
709 cras_ramp_get_current_action_ret.type = CRAS_RAMP_ACTION_PARTIAL;
710 rc = cras_iodev_put_output_buffer(&iodev, frames, 20, NULL, nullptr);
711
712 // Output should not be muted.
713 EXPECT_EQ(0, rc);
714 EXPECT_EQ(0, cras_mix_mute_count);
715 // Ramped frames should be increased by 20.
716 EXPECT_EQ(20, cras_ramp_update_ramped_frames_num_frames);
717 EXPECT_EQ(20, put_buffer_nframes);
718 EXPECT_EQ(20, rate_estimator_add_frames_num_frames);
719 }
720
TEST(IoDevPutOutputBuffer,NodeVolumeZeroShouldMuteWithRamp)721 TEST(IoDevPutOutputBuffer, NodeVolumeZeroShouldMuteWithRamp) {
722 struct cras_audio_format fmt;
723 struct cras_iodev iodev;
724 struct cras_ionode ionode;
725 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
726 int rc;
727
728 ResetStubData();
729 memset(&iodev, 0, sizeof(iodev));
730 memset(&ionode, 0, sizeof(ionode));
731
732 iodev.nodes = &ionode;
733 iodev.active_node = &ionode;
734 iodev.active_node->dev = &iodev;
735 iodev.active_node->volume = 0;
736
737 fmt.format = SND_PCM_FORMAT_S16_LE;
738 fmt.frame_rate = 48000;
739 fmt.num_channels = 2;
740 iodev.format = &fmt;
741 iodev.put_buffer = put_buffer;
742
743 // Assume device has ramp member.
744 iodev.ramp = reinterpret_cast<struct cras_ramp*>(0x1);
745
746 // Assume ramping is done.
747 cras_ramp_get_current_action_ret.type = CRAS_RAMP_ACTION_NONE;
748
749 rc = cras_iodev_put_output_buffer(&iodev, frames, 20, NULL, nullptr);
750 EXPECT_EQ(0, rc);
751 EXPECT_EQ(20, cras_mix_mute_count);
752 EXPECT_EQ(20, put_buffer_nframes);
753 EXPECT_EQ(20, rate_estimator_add_frames_num_frames);
754
755 // Test for the case where ramping is not done yet.
756 ResetStubData();
757 cras_ramp_get_current_action_ret.type = CRAS_RAMP_ACTION_PARTIAL;
758 rc = cras_iodev_put_output_buffer(&iodev, frames, 20, NULL, nullptr);
759
760 // Output should not be muted.
761 EXPECT_EQ(0, rc);
762 EXPECT_EQ(0, cras_mix_mute_count);
763 // Ramped frames should be increased by 20.
764 EXPECT_EQ(20, cras_ramp_update_ramped_frames_num_frames);
765 EXPECT_EQ(20, put_buffer_nframes);
766 EXPECT_EQ(20, rate_estimator_add_frames_num_frames);
767 }
TEST(IoDevPutOutputBuffer,NoDSP)768 TEST(IoDevPutOutputBuffer, NoDSP) {
769 struct cras_audio_format fmt;
770 struct cras_iodev iodev;
771 struct cras_ionode ionode;
772 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
773 int rc;
774
775 ResetStubData();
776 memset(&iodev, 0, sizeof(iodev));
777 memset(&ionode, 0, sizeof(ionode));
778
779 iodev.nodes = &ionode;
780 iodev.active_node = &ionode;
781 iodev.active_node->dev = &iodev;
782 iodev.active_node->volume = 100;
783
784 fmt.format = SND_PCM_FORMAT_S16_LE;
785 fmt.frame_rate = 48000;
786 fmt.num_channels = 2;
787 iodev.format = &fmt;
788 iodev.put_buffer = put_buffer;
789
790 rc = cras_iodev_put_output_buffer(&iodev, frames, 22, NULL, nullptr);
791 EXPECT_EQ(0, rc);
792 EXPECT_EQ(0, cras_mix_mute_count);
793 EXPECT_EQ(22, put_buffer_nframes);
794 EXPECT_EQ(22, rate_estimator_add_frames_num_frames);
795 }
796
TEST(IoDevPutOutputBuffer,DSP)797 TEST(IoDevPutOutputBuffer, DSP) {
798 struct cras_audio_format fmt;
799 struct cras_iodev iodev;
800 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
801 int rc;
802
803 ResetStubData();
804 memset(&iodev, 0, sizeof(iodev));
805 iodev.dsp_context = reinterpret_cast<cras_dsp_context*>(0x15);
806 cras_dsp_get_pipeline_ret = 0x25;
807
808 fmt.format = SND_PCM_FORMAT_S16_LE;
809 fmt.frame_rate = 48000;
810 fmt.num_channels = 2;
811 iodev.format = &fmt;
812 iodev.put_buffer = put_buffer;
813 cras_iodev_register_pre_dsp_hook(&iodev, pre_dsp_hook, (void *)0x1234);
814 cras_iodev_register_post_dsp_hook(&iodev, post_dsp_hook, (void *)0x5678);
815
816 rc = cras_iodev_put_output_buffer(&iodev, frames, 32, NULL, nullptr);
817 EXPECT_EQ(0, rc);
818 EXPECT_EQ(0, cras_mix_mute_count);
819 EXPECT_EQ(1, pre_dsp_hook_called);
820 EXPECT_EQ(frames, pre_dsp_hook_frames);
821 EXPECT_EQ((void *)0x1234, pre_dsp_hook_cb_data);
822 EXPECT_EQ(1, post_dsp_hook_called);
823 EXPECT_EQ((void *)0x5678, post_dsp_hook_cb_data);
824 EXPECT_EQ(32, put_buffer_nframes);
825 EXPECT_EQ(32, rate_estimator_add_frames_num_frames);
826 EXPECT_EQ(32, cras_dsp_pipeline_apply_sample_count);
827 EXPECT_EQ(cras_dsp_get_pipeline_called, cras_dsp_put_pipeline_called);
828 }
829
TEST(IoDevPutOutputBuffer,SoftVol)830 TEST(IoDevPutOutputBuffer, SoftVol) {
831 struct cras_audio_format fmt;
832 struct cras_iodev iodev;
833 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
834 int rc;
835
836 ResetStubData();
837 memset(&iodev, 0, sizeof(iodev));
838 iodev.software_volume_needed = 1;
839
840 fmt.format = SND_PCM_FORMAT_S16_LE;
841 fmt.frame_rate = 48000;
842 fmt.num_channels = 2;
843 iodev.format = &fmt;
844 iodev.put_buffer = put_buffer;
845
846 cras_system_get_volume_return = 13;
847 softvol_scalers[13] = 0.435;
848
849 rc = cras_iodev_put_output_buffer(&iodev, frames, 53, NULL, nullptr);
850 EXPECT_EQ(0, rc);
851 EXPECT_EQ(0, cras_mix_mute_count);
852 EXPECT_EQ(53, put_buffer_nframes);
853 EXPECT_EQ(53, rate_estimator_add_frames_num_frames);
854 EXPECT_EQ(softvol_scalers[13], cras_scale_buffer_scaler);
855 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, cras_scale_buffer_fmt);
856 }
857
TEST(IoDevPutOutputBuffer,SoftVolWithRamp)858 TEST(IoDevPutOutputBuffer, SoftVolWithRamp) {
859 struct cras_audio_format fmt;
860 struct cras_iodev iodev;
861 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
862 int rc;
863 int n_frames = 53;
864 float ramp_scaler = 0.2;
865 float increment = 0.001;
866 int volume = 13;
867 float volume_scaler = 0.435;
868
869 ResetStubData();
870 memset(&iodev, 0, sizeof(iodev));
871 iodev.software_volume_needed = 1;
872
873 fmt.format = SND_PCM_FORMAT_S16_LE;
874 fmt.frame_rate = 48000;
875 fmt.num_channels = 2;
876 iodev.format = &fmt;
877 iodev.put_buffer = put_buffer;
878 // Assume device has ramp member.
879 iodev.ramp = reinterpret_cast<struct cras_ramp*>(0x1);
880
881 // Assume ramping is done.
882 cras_ramp_get_current_action_ret.type = CRAS_RAMP_ACTION_NONE;
883
884 cras_system_get_volume_return = volume;
885 softvol_scalers[volume] = volume_scaler;
886
887 rc = cras_iodev_put_output_buffer(&iodev, frames, n_frames, NULL, nullptr);
888 EXPECT_EQ(0, rc);
889 EXPECT_EQ(0, cras_mix_mute_count);
890 EXPECT_EQ(n_frames, put_buffer_nframes);
891 EXPECT_EQ(n_frames, rate_estimator_add_frames_num_frames);
892 EXPECT_EQ(softvol_scalers[volume], cras_scale_buffer_scaler);
893 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, cras_scale_buffer_fmt);
894
895 ResetStubData();
896 // Assume ramping is not done.
897 cras_ramp_get_current_action_ret.type = CRAS_RAMP_ACTION_PARTIAL;
898 cras_ramp_get_current_action_ret.scaler = ramp_scaler;
899 cras_ramp_get_current_action_ret.increment = increment;
900
901 cras_system_get_volume_return = volume;
902 softvol_scalers[volume] = volume_scaler;
903
904 rc = cras_iodev_put_output_buffer(&iodev, frames, n_frames, NULL, nullptr);
905 EXPECT_EQ(0, rc);
906 EXPECT_EQ(0, cras_mix_mute_count);
907 // cras_scale_buffer is not called.
908 EXPECT_EQ(0, cras_scale_buffer_called);
909
910 // Verify the arguments passed to cras_scale_buffer_increment.
911 EXPECT_EQ(fmt.format, cras_scale_buffer_increment_fmt);
912 EXPECT_EQ(frames, cras_scale_buffer_increment_buff);
913 EXPECT_EQ(n_frames, cras_scale_buffer_increment_frame);
914 // Initial scaler will be product of software volume scaler and
915 // ramp scaler.
916 EXPECT_FLOAT_EQ(softvol_scalers[volume] * ramp_scaler,
917 cras_scale_buffer_increment_scaler);
918 // Increment scaler will be product of software volume scaler and
919 // ramp increment.
920 EXPECT_FLOAT_EQ(softvol_scalers[volume] * increment,
921 cras_scale_buffer_increment_increment);
922 EXPECT_EQ(fmt.num_channels, cras_scale_buffer_increment_channel);
923
924 EXPECT_EQ(n_frames, put_buffer_nframes);
925 EXPECT_EQ(n_frames, rate_estimator_add_frames_num_frames);
926 }
927
TEST(IoDevPutOutputBuffer,NoSoftVolWithRamp)928 TEST(IoDevPutOutputBuffer, NoSoftVolWithRamp) {
929 struct cras_audio_format fmt;
930 struct cras_iodev iodev;
931 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
932 int rc;
933 int n_frames = 53;
934 float ramp_scaler = 0.2;
935 float increment = 0.001;
936
937 ResetStubData();
938 memset(&iodev, 0, sizeof(iodev));
939 iodev.software_volume_needed = 0;
940
941 fmt.format = SND_PCM_FORMAT_S16_LE;
942 fmt.frame_rate = 48000;
943 fmt.num_channels = 2;
944 iodev.format = &fmt;
945 iodev.put_buffer = put_buffer;
946 // Assume device has ramp member.
947 iodev.ramp = reinterpret_cast<struct cras_ramp*>(0x1);
948
949 // Assume ramping is done.
950 cras_ramp_get_current_action_ret.type = CRAS_RAMP_ACTION_NONE;
951
952 rc = cras_iodev_put_output_buffer(&iodev, frames, n_frames, NULL, nullptr);
953 EXPECT_EQ(0, rc);
954 EXPECT_EQ(0, cras_mix_mute_count);
955 // cras_scale_buffer is not called.
956 EXPECT_EQ(0, cras_scale_buffer_called);
957 EXPECT_EQ(n_frames, put_buffer_nframes);
958 EXPECT_EQ(n_frames, rate_estimator_add_frames_num_frames);
959
960 ResetStubData();
961 // Assume ramping is not done.
962 cras_ramp_get_current_action_ret.type = CRAS_RAMP_ACTION_PARTIAL;
963 cras_ramp_get_current_action_ret.scaler = ramp_scaler;
964 cras_ramp_get_current_action_ret.increment = increment;
965
966 rc = cras_iodev_put_output_buffer(&iodev, frames, n_frames, NULL, nullptr);
967 EXPECT_EQ(0, rc);
968 EXPECT_EQ(0, cras_mix_mute_count);
969 // cras_scale_buffer is not called.
970 EXPECT_EQ(0, cras_scale_buffer_called);
971
972 // Verify the arguments passed to cras_scale_buffer_increment.
973 EXPECT_EQ(fmt.format, cras_scale_buffer_increment_fmt);
974 EXPECT_EQ(frames, cras_scale_buffer_increment_buff);
975 EXPECT_EQ(n_frames, cras_scale_buffer_increment_frame);
976 EXPECT_FLOAT_EQ(ramp_scaler, cras_scale_buffer_increment_scaler);
977 EXPECT_FLOAT_EQ(increment, cras_scale_buffer_increment_increment);
978 EXPECT_EQ(fmt.num_channels, cras_scale_buffer_increment_channel);
979
980 EXPECT_EQ(n_frames, put_buffer_nframes);
981 EXPECT_EQ(n_frames, rate_estimator_add_frames_num_frames);
982 }
983
TEST(IoDevPutOutputBuffer,Scale32Bit)984 TEST(IoDevPutOutputBuffer, Scale32Bit) {
985 struct cras_audio_format fmt;
986 struct cras_iodev iodev;
987 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
988 int rc;
989
990 ResetStubData();
991 memset(&iodev, 0, sizeof(iodev));
992 iodev.software_volume_needed = 1;
993
994 cras_system_get_volume_return = 13;
995 softvol_scalers[13] = 0.435;
996
997 fmt.format = SND_PCM_FORMAT_S32_LE;
998 fmt.frame_rate = 48000;
999 fmt.num_channels = 2;
1000 iodev.format = &fmt;
1001 iodev.put_buffer = put_buffer;
1002
1003 rc = cras_iodev_put_output_buffer(&iodev, frames, 53, NULL, nullptr);
1004 EXPECT_EQ(0, rc);
1005 EXPECT_EQ(0, cras_mix_mute_count);
1006 EXPECT_EQ(53, put_buffer_nframes);
1007 EXPECT_EQ(53, rate_estimator_add_frames_num_frames);
1008 EXPECT_EQ(SND_PCM_FORMAT_S32_LE, cras_scale_buffer_fmt);
1009 }
1010
1011 // frames queued/avail tests
1012
1013 static unsigned fr_queued = 0;
1014
frames_queued(const struct cras_iodev * iodev,struct timespec * tstamp)1015 static int frames_queued(const struct cras_iodev *iodev,
1016 struct timespec *tstamp)
1017 {
1018 clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
1019 return fr_queued;
1020 }
1021
TEST(IoDevQueuedBuffer,ZeroMinBufferLevel)1022 TEST(IoDevQueuedBuffer, ZeroMinBufferLevel) {
1023 struct cras_iodev iodev;
1024 struct timespec tstamp;
1025 int rc;
1026
1027 ResetStubData();
1028 memset(&iodev, 0, sizeof(iodev));
1029 iodev.direction = CRAS_STREAM_OUTPUT;
1030 iodev.frames_queued = frames_queued;
1031 iodev.min_buffer_level = 0;
1032 iodev.buffer_size = 200;
1033 fr_queued = 50;
1034
1035 rc = cras_iodev_frames_queued(&iodev, &tstamp);
1036 EXPECT_EQ(50, rc);
1037 rc = cras_iodev_buffer_avail(&iodev, rc);
1038 EXPECT_EQ(150, rc);
1039 }
1040
TEST(IoDevQueuedBuffer,NonZeroMinBufferLevel)1041 TEST(IoDevQueuedBuffer, NonZeroMinBufferLevel) {
1042 struct cras_iodev iodev;
1043 struct timespec hw_tstamp;
1044 int rc;
1045
1046 ResetStubData();
1047 memset(&iodev, 0, sizeof(iodev));
1048 iodev.direction = CRAS_STREAM_OUTPUT;
1049 iodev.frames_queued = frames_queued;
1050 iodev.min_buffer_level = 100;
1051 iodev.buffer_size = 200;
1052 fr_queued = 180;
1053
1054 rc = cras_iodev_frames_queued(&iodev, &hw_tstamp);
1055 EXPECT_EQ(80, rc);
1056 rc = cras_iodev_buffer_avail(&iodev, rc);
1057 EXPECT_EQ(20, rc);
1058
1059 /* When fr_queued < min_buffer_level*/
1060 fr_queued = 80;
1061 rc = cras_iodev_frames_queued(&iodev, &hw_tstamp);
1062 EXPECT_EQ(0, rc);
1063 rc = cras_iodev_buffer_avail(&iodev, rc);
1064 EXPECT_EQ(100, rc);
1065 }
1066
update_active_node(struct cras_iodev * iodev,unsigned node_idx,unsigned dev_enabled)1067 static void update_active_node(struct cras_iodev *iodev,
1068 unsigned node_idx,
1069 unsigned dev_enabled)
1070 {
1071 }
1072
dev_set_volume(struct cras_iodev * iodev)1073 static void dev_set_volume(struct cras_iodev *iodev)
1074 {
1075 }
1076
dev_set_capture_gain(struct cras_iodev * iodev)1077 static void dev_set_capture_gain(struct cras_iodev *iodev)
1078 {
1079 }
1080
dev_set_mute(struct cras_iodev * iodev)1081 static void dev_set_mute(struct cras_iodev *iodev)
1082 {
1083 set_mute_called++;
1084 }
1085
TEST(IoNodePlug,PlugUnplugNode)1086 TEST(IoNodePlug, PlugUnplugNode) {
1087 struct cras_iodev iodev;
1088 struct cras_ionode ionode, ionode2;
1089
1090 memset(&iodev, 0, sizeof(iodev));
1091 memset(&ionode, 0, sizeof(ionode));
1092 memset(&ionode2, 0, sizeof(ionode2));
1093 iodev.direction = CRAS_STREAM_INPUT;
1094 iodev.update_active_node = update_active_node;
1095 ionode.dev = &iodev;
1096 cras_iodev_add_node(&iodev, &ionode);
1097 ionode2.dev = &iodev;
1098 cras_iodev_add_node(&iodev, &ionode2);
1099 cras_iodev_set_active_node(&iodev, &ionode);
1100 ResetStubData();
1101 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_PLUGGED, 1);
1102 EXPECT_EQ(0, cras_iodev_list_disable_dev_called);
1103 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_PLUGGED, 0);
1104 EXPECT_EQ(1, cras_iodev_list_disable_dev_called);
1105
1106 /* Unplug non-active node shouldn't disable iodev. */
1107 cras_iodev_set_node_attr(&ionode2, IONODE_ATTR_PLUGGED, 1);
1108 EXPECT_EQ(1, cras_iodev_list_disable_dev_called);
1109 cras_iodev_set_node_attr(&ionode2, IONODE_ATTR_PLUGGED, 0);
1110 EXPECT_EQ(1, cras_iodev_list_disable_dev_called);
1111 }
1112
TEST(IoDev,AddRemoveNode)1113 TEST(IoDev, AddRemoveNode) {
1114 struct cras_iodev iodev;
1115 struct cras_ionode ionode;
1116
1117 memset(&iodev, 0, sizeof(iodev));
1118 memset(&ionode, 0, sizeof(ionode));
1119 ResetStubData();
1120 EXPECT_EQ(0, notify_nodes_changed_called);
1121 cras_iodev_add_node(&iodev, &ionode);
1122 EXPECT_EQ(1, notify_nodes_changed_called);
1123 cras_iodev_rm_node(&iodev, &ionode);
1124 EXPECT_EQ(2, notify_nodes_changed_called);
1125 }
1126
TEST(IoDev,SetActiveNode)1127 TEST(IoDev, SetActiveNode) {
1128 struct cras_iodev iodev;
1129 struct cras_ionode ionode;
1130
1131 memset(&iodev, 0, sizeof(iodev));
1132 memset(&ionode, 0, sizeof(ionode));
1133 ResetStubData();
1134 EXPECT_EQ(0, notify_active_node_changed_called);
1135 cras_iodev_set_active_node(&iodev, &ionode);
1136 EXPECT_EQ(1, notify_active_node_changed_called);
1137 }
1138
TEST(IoDev,SetNodeVolume)1139 TEST(IoDev, SetNodeVolume) {
1140 struct cras_iodev iodev;
1141 struct cras_ionode ionode;
1142
1143 memset(&iodev, 0, sizeof(iodev));
1144 memset(&ionode, 0, sizeof(ionode));
1145 iodev.set_volume = dev_set_volume;
1146 iodev.set_capture_gain = dev_set_capture_gain;
1147 ionode.dev = &iodev;
1148 ResetStubData();
1149 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_VOLUME, 10);
1150 EXPECT_EQ(1, notify_node_volume_called);
1151 iodev.direction = CRAS_STREAM_INPUT;
1152 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_CAPTURE_GAIN, 10);
1153 EXPECT_EQ(1, notify_node_capture_gain_called);
1154 }
1155
TEST(IoDev,SetNodeSwapLeftRight)1156 TEST(IoDev, SetNodeSwapLeftRight) {
1157 struct cras_iodev iodev;
1158 struct cras_ionode ionode;
1159
1160 memset(&iodev, 0, sizeof(iodev));
1161 memset(&ionode, 0, sizeof(ionode));
1162 iodev.set_swap_mode_for_node = set_swap_mode_for_node;
1163 ionode.dev = &iodev;
1164 ResetStubData();
1165 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_SWAP_LEFT_RIGHT, 1);
1166 EXPECT_EQ(1, set_swap_mode_for_node_called);
1167 EXPECT_EQ(1, set_swap_mode_for_node_enable);
1168 EXPECT_EQ(1, ionode.left_right_swapped);
1169 EXPECT_EQ(1, notify_node_left_right_swapped_called);
1170 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_SWAP_LEFT_RIGHT, 0);
1171 EXPECT_EQ(2, set_swap_mode_for_node_called);
1172 EXPECT_EQ(0, set_swap_mode_for_node_enable);
1173 EXPECT_EQ(0, ionode.left_right_swapped);
1174 EXPECT_EQ(2, notify_node_left_right_swapped_called);
1175 }
1176
TEST(IoDev,SetMute)1177 TEST(IoDev, SetMute) {
1178 struct cras_iodev iodev;
1179 int rc;
1180
1181 memset(&iodev, 0, sizeof(iodev));
1182 iodev.set_mute = dev_set_mute;
1183 iodev.state = CRAS_IODEV_STATE_CLOSE;
1184
1185 ResetStubData();
1186 rc = cras_iodev_set_mute(&iodev);
1187 EXPECT_EQ(0, rc);
1188 EXPECT_EQ(0, set_mute_called);
1189
1190 iodev.state = CRAS_IODEV_STATE_OPEN;
1191 rc = cras_iodev_set_mute(&iodev);
1192 EXPECT_EQ(0, rc);
1193 EXPECT_EQ(1, set_mute_called);
1194 }
1195
1196 // Test software volume changes for default output.
TEST(IoDev,SoftwareVolume)1197 TEST(IoDev, SoftwareVolume) {
1198 struct cras_iodev iodev;
1199 struct cras_ionode ionode;
1200
1201 memset(&iodev, 0, sizeof(iodev));
1202 memset(&ionode, 0, sizeof(ionode));
1203 ResetStubData();
1204
1205 iodev.nodes = &ionode;
1206 iodev.active_node = &ionode;
1207 iodev.active_node->dev = &iodev;
1208
1209 iodev.active_node->volume = 100;
1210 iodev.software_volume_needed = 0;
1211
1212
1213 softvol_scalers[80] = 0.5;
1214 softvol_scalers[70] = 0.3;
1215
1216 // Check that system volume changes software volume if needed.
1217 cras_system_get_volume_return = 80;
1218 // system_volume - 100 + node_volume = 80 - 100 + 100 = 80
1219 EXPECT_FLOAT_EQ(0.5, cras_iodev_get_software_volume_scaler(&iodev));
1220
1221 // Check that node volume changes software volume if needed.
1222 iodev.active_node->volume = 90;
1223 // system_volume - 100 + node_volume = 80 - 100 + 90 = 70
1224 EXPECT_FLOAT_EQ(0.3, cras_iodev_get_software_volume_scaler(&iodev));
1225 }
1226
1227 // Test software gain scaler.
TEST(IoDev,SoftwareGain)1228 TEST(IoDev, SoftwareGain) {
1229 struct cras_iodev iodev;
1230 struct cras_ionode ionode;
1231
1232 memset(&iodev, 0, sizeof(iodev));
1233 memset(&ionode, 0, sizeof(ionode));
1234 ResetStubData();
1235
1236 iodev.nodes = &ionode;
1237 iodev.active_node = &ionode;
1238 iodev.active_node->dev = &iodev;
1239
1240 ionode.capture_gain= 400;
1241 ionode.software_volume_needed = 1;
1242 ionode.max_software_gain = 3000;
1243
1244 // Check that system volume changes software volume if needed.
1245 cras_system_get_capture_gain_ret_value = 2000;
1246 // system_gain + node_gain = 2000 + 400 = 2400
1247 // 2400 * 0.01 dB is 15.848931
1248 EXPECT_FLOAT_EQ(15.848931, cras_iodev_get_software_gain_scaler(&iodev));
1249 EXPECT_FLOAT_EQ(3000, cras_iodev_maximum_software_gain(&iodev));
1250
1251 // Software gain scaler should be 1.0 if software gain is not needed.
1252 ionode.software_volume_needed = 0;
1253 EXPECT_FLOAT_EQ(1.0, cras_iodev_get_software_gain_scaler(&iodev));
1254 EXPECT_FLOAT_EQ(0, cras_iodev_maximum_software_gain(&iodev));
1255 }
1256
1257 // This get_buffer implementation set returned frames larger than requested
1258 // frames.
bad_get_buffer(struct cras_iodev * iodev,struct cras_audio_area ** area,unsigned * frames)1259 static int bad_get_buffer(struct cras_iodev *iodev,
1260 struct cras_audio_area **area,
1261 unsigned *frames)
1262 {
1263 *frames = *frames + 1;
1264 return 0;
1265 }
1266
1267 // Check that if get_buffer implementation returns invalid frames,
1268 // cras_iodev_get_output_buffer and cras_iodev_get_input_buffer can return
1269 // error.
TEST(IoDev,GetBufferInvalidFrames)1270 TEST(IoDev, GetBufferInvalidFrames) {
1271 struct cras_iodev iodev;
1272 struct cras_audio_area **area = NULL;
1273 unsigned int frames = 512;
1274 struct cras_audio_format fmt;
1275
1276 // Format is used in cras_iodev_get_input_buffer;
1277 fmt.format = SND_PCM_FORMAT_S16_LE;
1278 fmt.frame_rate = 48000;
1279 fmt.num_channels = 2;
1280
1281 memset(&iodev, 0, sizeof(iodev));
1282
1283 ResetStubData();
1284
1285 iodev.format = &fmt;
1286 iodev.get_buffer = bad_get_buffer;
1287
1288 EXPECT_EQ(-EINVAL, cras_iodev_get_output_buffer(&iodev, area, &frames));
1289 EXPECT_EQ(-EINVAL, cras_iodev_get_input_buffer(&iodev, &frames));
1290 }
1291
configure_dev(struct cras_iodev * iodev)1292 static int configure_dev(struct cras_iodev *iodev) {
1293 iodev->buffer_size = iodev_buffer_size;
1294 return 0;
1295 }
1296
TEST(IoDev,OpenOutputDeviceNoStart)1297 TEST(IoDev, OpenOutputDeviceNoStart) {
1298 struct cras_iodev iodev;
1299
1300 memset(&iodev, 0, sizeof(iodev));
1301 iodev.configure_dev = configure_dev;
1302 iodev.direction = CRAS_STREAM_OUTPUT;
1303 iodev.ext_format = &audio_fmt;
1304 ResetStubData();
1305
1306 iodev.state = CRAS_IODEV_STATE_CLOSE;
1307
1308 iodev_buffer_size = 1024;
1309 cras_iodev_open(&iodev, 240, &audio_fmt);
1310 EXPECT_EQ(0, iodev.max_cb_level);
1311 EXPECT_EQ(240, iodev.min_cb_level);
1312
1313 // Test that state is no stream run when there is no start ops.
1314 EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state);
1315 }
1316
TEST(IoDev,OpenOutputDeviceWithLowRateFmt)1317 TEST(IoDev, OpenOutputDeviceWithLowRateFmt) {
1318 struct cras_iodev iodev;
1319
1320 memset(&iodev, 0, sizeof(iodev));
1321 iodev.configure_dev = configure_dev;
1322 iodev.direction = CRAS_STREAM_OUTPUT;
1323 iodev.ext_format = &audio_fmt;
1324 ResetStubData();
1325
1326 cras_audio_format low_rate_fmt = audio_fmt;
1327 low_rate_fmt.frame_rate = 8000;
1328 iodev.state = CRAS_IODEV_STATE_CLOSE;
1329
1330 iodev_buffer_size = 1024;
1331 cras_iodev_open(&iodev, 40, &low_rate_fmt);
1332 EXPECT_EQ(0, iodev.max_cb_level);
1333
1334 // Test that iodev min_cb_level should be set to
1335 // 40 * 48000 / 8000 = 240
1336 EXPECT_EQ(240, iodev.min_cb_level);
1337 }
1338
fake_start(const struct cras_iodev * iodev)1339 int fake_start(const struct cras_iodev *iodev) {
1340 return 0;
1341 }
1342
TEST(IoDev,OpenOutputDeviceWithStart)1343 TEST(IoDev, OpenOutputDeviceWithStart) {
1344 struct cras_iodev iodev;
1345
1346 memset(&iodev, 0, sizeof(iodev));
1347 iodev.configure_dev = configure_dev;
1348 iodev.direction = CRAS_STREAM_OUTPUT;
1349 iodev.ext_format = &audio_fmt;
1350 ResetStubData();
1351
1352 iodev.state = CRAS_IODEV_STATE_CLOSE;
1353 iodev.start = fake_start;
1354
1355 iodev_buffer_size = 1024;
1356 cras_iodev_open(&iodev, 240, &audio_fmt);
1357 EXPECT_EQ(0, iodev.max_cb_level);
1358 EXPECT_EQ(240, iodev.min_cb_level);
1359
1360 // Test that state is no stream run when there is start ops.
1361 EXPECT_EQ(CRAS_IODEV_STATE_OPEN, iodev.state);
1362 }
1363
TEST(IoDev,OpenInputDeviceNoStart)1364 TEST(IoDev, OpenInputDeviceNoStart) {
1365 struct cras_iodev iodev;
1366
1367 memset(&iodev, 0, sizeof(iodev));
1368 iodev.configure_dev = configure_dev;
1369 iodev.direction = CRAS_STREAM_INPUT;
1370 iodev.ext_format = &audio_fmt;
1371 ResetStubData();
1372
1373 iodev.state = CRAS_IODEV_STATE_CLOSE;
1374
1375 iodev_buffer_size = 1024;
1376 cras_iodev_open(&iodev, 240, &audio_fmt);
1377 EXPECT_EQ(0, iodev.max_cb_level);
1378 EXPECT_EQ(240, iodev.min_cb_level);
1379
1380 // Test that state is normal run when there is start ops.
1381 EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state);
1382 }
1383
TEST(IoDev,OpenInputDeviceWithStart)1384 TEST(IoDev, OpenInputDeviceWithStart) {
1385 struct cras_iodev iodev;
1386
1387 memset(&iodev, 0, sizeof(iodev));
1388 iodev.configure_dev = configure_dev;
1389 iodev.direction = CRAS_STREAM_INPUT;
1390 iodev.ext_format = &audio_fmt;
1391 ResetStubData();
1392
1393 iodev.state = CRAS_IODEV_STATE_CLOSE;
1394 iodev.start = fake_start;
1395
1396 iodev_buffer_size = 1024;
1397 cras_iodev_open(&iodev, 240, &audio_fmt);
1398 EXPECT_EQ(0, iodev.max_cb_level);
1399 EXPECT_EQ(240, iodev.min_cb_level);
1400
1401 // Test that state is normal run even if there is start ops.
1402 EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state);
1403 }
1404
TEST(IoDev,OpenInputDeviceWithLowRateFmt)1405 TEST(IoDev, OpenInputDeviceWithLowRateFmt) {
1406 struct cras_iodev iodev;
1407
1408 memset(&iodev, 0, sizeof(iodev));
1409 iodev.configure_dev = configure_dev;
1410 iodev.direction = CRAS_STREAM_INPUT;
1411 iodev.ext_format = &audio_fmt;
1412 ResetStubData();
1413
1414 cras_audio_format low_rate_fmt = audio_fmt;
1415 low_rate_fmt.frame_rate = 8000;
1416 iodev.state = CRAS_IODEV_STATE_CLOSE;
1417
1418 iodev_buffer_size = 1024;
1419 cras_iodev_open(&iodev, 40, &low_rate_fmt);
1420 EXPECT_EQ(0, iodev.max_cb_level);
1421
1422 // Test that iodev min_cb_level should be set to
1423 // 40 * 48000 / 8000 = 240
1424 EXPECT_EQ(240, iodev.min_cb_level);
1425 }
1426
simple_no_stream(struct cras_iodev * dev,int enable)1427 static int simple_no_stream(struct cras_iodev *dev, int enable)
1428 {
1429 simple_no_stream_enable = enable;
1430 simple_no_stream_called++;
1431 return 0;
1432 }
1433
TEST(IoDev,AddRmStream)1434 TEST(IoDev, AddRmStream) {
1435 struct cras_iodev iodev;
1436 struct cras_rstream rstream1, rstream2;
1437 struct dev_stream stream1, stream2;
1438
1439 memset(&iodev, 0, sizeof(iodev));
1440 memset(&rstream1, 0, sizeof(rstream1));
1441 memset(&rstream2, 0, sizeof(rstream2));
1442 iodev.configure_dev = configure_dev;
1443 iodev.no_stream = simple_no_stream;
1444 iodev.ext_format = &audio_fmt;
1445 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
1446 rstream1.cb_threshold = 800;
1447 stream1.stream = &rstream1;
1448 rstream2.cb_threshold = 400;
1449 stream2.stream = &rstream2;
1450 ResetStubData();
1451
1452 iodev_buffer_size = 1024;
1453 cras_iodev_open(&iodev, rstream1.cb_threshold, &audio_fmt);
1454 EXPECT_EQ(0, iodev.max_cb_level);
1455 EXPECT_EQ(512, iodev.min_cb_level);
1456
1457 /* min_cb_level should not exceed half the buffer size. */
1458 cras_iodev_add_stream(&iodev, &stream1);
1459 EXPECT_EQ(800, iodev.max_cb_level);
1460 EXPECT_EQ(512, iodev.min_cb_level);
1461 EXPECT_EQ(1, buffer_share_add_id_called);
1462
1463 cras_iodev_add_stream(&iodev, &stream2);
1464 EXPECT_EQ(800, iodev.max_cb_level);
1465 EXPECT_EQ(400, iodev.min_cb_level);
1466 EXPECT_EQ(2, buffer_share_add_id_called);
1467
1468 cras_iodev_rm_stream(&iodev, &rstream1);
1469 EXPECT_EQ(400, iodev.max_cb_level);
1470 EXPECT_EQ(400, iodev.min_cb_level);
1471 EXPECT_EQ(0, simple_no_stream_called);
1472
1473 /* When all streams are removed, keep the last min_cb_level for draining. */
1474 cras_iodev_rm_stream(&iodev, &rstream2);
1475 EXPECT_EQ(0, iodev.max_cb_level);
1476 EXPECT_EQ(512, iodev.min_cb_level);
1477 }
1478
TEST(IoDev,TriggerOnlyStreamNoBufferShare)1479 TEST(IoDev, TriggerOnlyStreamNoBufferShare) {
1480 struct cras_iodev iodev;
1481 struct cras_rstream rstream;
1482 struct dev_stream stream;
1483
1484 memset(&iodev, 0, sizeof(iodev));
1485 memset(&rstream, 0, sizeof(rstream));
1486 iodev.configure_dev = configure_dev;
1487 iodev.ext_format = &audio_fmt;
1488 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
1489 rstream.cb_threshold = 800;
1490 rstream.flags = TRIGGER_ONLY;
1491 stream.stream = &rstream;
1492 ResetStubData();
1493
1494 cras_iodev_open(&iodev, rstream.cb_threshold, &audio_fmt);
1495 /* TRIGGER_ONLY streams shall not be added to buffer_share. */
1496 cras_iodev_add_stream(&iodev, &stream);
1497 EXPECT_EQ(0, buffer_share_add_id_called);
1498 }
1499
TEST(IoDev,FillZeros)1500 TEST(IoDev, FillZeros) {
1501 struct cras_iodev iodev;
1502 struct cras_audio_format fmt;
1503 unsigned int frames = 50;
1504 int16_t *zeros;
1505 int rc;
1506
1507 ResetStubData();
1508
1509 memset(&iodev, 0, sizeof(iodev));
1510 fmt.format = SND_PCM_FORMAT_S16_LE;
1511 fmt.frame_rate = 48000;
1512 fmt.num_channels = 2;
1513 iodev.ext_format = &fmt;
1514 iodev.get_buffer = get_buffer;
1515 iodev.put_buffer = put_buffer;
1516
1517 iodev.direction = CRAS_STREAM_INPUT;
1518 rc = cras_iodev_fill_odev_zeros(&iodev, frames);
1519 EXPECT_EQ(-EINVAL, rc);
1520
1521 iodev.direction = CRAS_STREAM_OUTPUT;
1522 rc = cras_iodev_fill_odev_zeros(&iodev, frames);
1523
1524 EXPECT_EQ(0, rc);
1525 EXPECT_EQ(frames, put_buffer_nframes);
1526 zeros = (int16_t *)calloc(frames * 2, sizeof(*zeros));
1527 rc = memcmp(audio_buffer, zeros, frames * 2 * 2);
1528 free(zeros);
1529 EXPECT_EQ(0, rc);
1530 }
1531
TEST(IoDev,DefaultNoStreamPlaybackRunning)1532 TEST(IoDev, DefaultNoStreamPlaybackRunning) {
1533 struct cras_iodev iodev;
1534 struct cras_audio_format fmt;
1535 unsigned int hw_level = 50;
1536 unsigned int min_cb_level = 240;
1537 unsigned int zeros_to_fill;
1538 int16_t *zeros;
1539 int rc;
1540
1541 memset(&iodev, 0, sizeof(iodev));
1542
1543 fmt.format = SND_PCM_FORMAT_S16_LE;
1544 fmt.frame_rate = 48000;
1545 fmt.num_channels = 2;
1546 iodev.ext_format = &fmt;
1547 iodev.min_cb_level = min_cb_level;
1548 iodev.get_buffer = get_buffer;
1549 iodev.put_buffer = put_buffer;
1550 iodev.frames_queued = frames_queued;
1551 iodev.min_buffer_level = 0;
1552 iodev.direction = CRAS_STREAM_OUTPUT;
1553 iodev.buffer_size = BUFFER_SIZE;
1554 iodev.no_stream = no_stream;
1555
1556 ResetStubData();
1557
1558 // Device is running. hw_level is less than target.
1559 // Need to fill to callback level * 2;
1560 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
1561 fr_queued = hw_level;
1562 zeros_to_fill = min_cb_level * 2 - hw_level;
1563
1564 rc = cras_iodev_default_no_stream_playback(&iodev, 1);
1565
1566 EXPECT_EQ(0, rc);
1567 EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state);
1568 EXPECT_EQ(zeros_to_fill, put_buffer_nframes);
1569 zeros = (int16_t *)calloc(zeros_to_fill * 2, sizeof(*zeros));
1570 EXPECT_EQ(0, memcmp(audio_buffer, zeros, zeros_to_fill * 2 * 2));
1571 free(zeros);
1572
1573 ResetStubData();
1574
1575 // Device is running. hw_level is not less than target.
1576 // No need to fill zeros.
1577 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
1578 hw_level = min_cb_level * 2;
1579 fr_queued = hw_level;
1580 zeros_to_fill = 0;
1581
1582 rc = cras_iodev_default_no_stream_playback(&iodev, 1);
1583 EXPECT_EQ(0, rc);
1584 EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state);
1585 EXPECT_EQ(zeros_to_fill, put_buffer_nframes);
1586 }
1587
TEST(IoDev,PrepareOutputBeforeWriteSamples)1588 TEST(IoDev, PrepareOutputBeforeWriteSamples) {
1589 struct cras_iodev iodev;
1590 struct cras_audio_format fmt;
1591 unsigned int min_cb_level = 240;
1592 int rc;
1593 struct cras_rstream rstream1;
1594 struct dev_stream stream1;
1595 struct cras_iodev_info info;
1596
1597 memset(&info, 0, sizeof(info));
1598
1599 ResetStubData();
1600
1601 rstream1.cb_threshold = min_cb_level;
1602 stream1.stream = &rstream1;
1603
1604 memset(&iodev, 0, sizeof(iodev));
1605
1606 fmt.format = SND_PCM_FORMAT_S16_LE;
1607 fmt.frame_rate = 48000;
1608 fmt.num_channels = 2;
1609 iodev.ext_format = &fmt;
1610 iodev.format = &fmt;
1611 iodev.min_cb_level = min_cb_level;
1612 iodev.get_buffer = get_buffer;
1613 iodev.put_buffer = put_buffer;
1614 iodev.frames_queued = frames_queued;
1615 iodev.min_buffer_level = 0;
1616 iodev.direction = CRAS_STREAM_OUTPUT;
1617 iodev.buffer_size = BUFFER_SIZE;
1618 iodev.no_stream = no_stream;
1619 iodev.configure_dev = configure_dev;
1620 iodev.start = fake_start;
1621 iodev.info = info;
1622 iodev_buffer_size = BUFFER_SIZE;
1623
1624 // Open device.
1625 cras_iodev_open(&iodev, rstream1.cb_threshold, &fmt);
1626
1627 // Add one stream to device.
1628 cras_iodev_add_stream(&iodev, &stream1);
1629
1630 // Case 1: Assume device is not started yet.
1631 iodev.state = CRAS_IODEV_STATE_OPEN;
1632 // Assume sample is not ready yet.
1633 dev_stream_playback_frames_ret = 0;
1634
1635 rc = cras_iodev_prepare_output_before_write_samples(&iodev);
1636
1637 EXPECT_EQ(0, rc);
1638 // Device should remain in open state.
1639 EXPECT_EQ(CRAS_IODEV_STATE_OPEN, iodev.state);
1640 EXPECT_EQ(0, no_stream_called);
1641
1642 // Assume now sample is ready.
1643 dev_stream_playback_frames_ret = 100;
1644
1645 rc = cras_iodev_prepare_output_before_write_samples(&iodev);
1646
1647 EXPECT_EQ(0, rc);
1648 // Device should enter normal run state.
1649 EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state);
1650 EXPECT_EQ(0, no_stream_called);
1651 // Need to fill 1 callback level of zeros;
1652 EXPECT_EQ(min_cb_level, put_buffer_nframes);
1653
1654 ResetStubData();
1655
1656 // Case 2: Assume device is started and is in no stream state.
1657 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
1658 // Sample is not ready yet.
1659 dev_stream_playback_frames_ret = 0;
1660
1661 rc = cras_iodev_prepare_output_before_write_samples(&iodev);
1662
1663 EXPECT_EQ(0, rc);
1664 // Device should remain in no_stream state.
1665 EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state);
1666 // Device in no_stream state should call no_stream ops once.
1667 EXPECT_EQ(1, no_stream_called);
1668 EXPECT_EQ(1, no_stream_enable);
1669
1670 // Assume now sample is ready.
1671 dev_stream_playback_frames_ret = 100;
1672
1673 rc = cras_iodev_prepare_output_before_write_samples(&iodev);
1674
1675 EXPECT_EQ(0, rc);
1676 // Device should enter normal run state.
1677 EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state);
1678 // Device should call no_stream ops with enable=0 to leave no stream state.
1679 EXPECT_EQ(2, no_stream_called);
1680 EXPECT_EQ(0, no_stream_enable);
1681
1682 ResetStubData();
1683
1684 // Case 3: Assume device is started and is in normal run state.
1685 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
1686
1687 rc = cras_iodev_prepare_output_before_write_samples(&iodev);
1688
1689 EXPECT_EQ(0, rc);
1690 // Device should remain in normal run state.
1691 EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state);
1692 // Device in no_stream state should call no_stream ops once.
1693 EXPECT_EQ(0, no_stream_called);
1694
1695 ResetStubData();
1696
1697 // Test for device with ramp. Device should start ramping
1698 // when sample is ready.
1699
1700 // Assume device has ramp member.
1701 iodev.ramp = reinterpret_cast<struct cras_ramp*>(0x1);
1702
1703 // Case 4.1: Assume device with ramp is started and is in no stream state.
1704 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
1705 // Assume sample is ready.
1706 dev_stream_playback_frames_ret = 100;
1707
1708 rc = cras_iodev_prepare_output_before_write_samples(&iodev);
1709
1710 // Device should start ramping up without setting mute callback.
1711 EXPECT_EQ(0, rc);
1712 EXPECT_EQ(1, cras_ramp_start_is_called);
1713 EXPECT_EQ(1, cras_ramp_start_is_up);
1714 EXPECT_EQ(fmt.frame_rate * RAMP_NEW_STREAM_DURATION_SECS,
1715 cras_ramp_start_duration_frames);
1716 EXPECT_EQ(NULL, cras_ramp_start_cb);
1717 EXPECT_EQ(NULL, cras_ramp_start_cb_data);
1718
1719 ResetStubData();
1720
1721 // Case 4.2: Assume device with ramp is started and is in no stream state.
1722 // But system is muted.
1723 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
1724 // Assume system is muted.
1725 cras_system_get_mute_return = 1;
1726 // Assume sample is ready.
1727 dev_stream_playback_frames_ret = 100;
1728
1729 rc = cras_iodev_prepare_output_before_write_samples(&iodev);
1730
1731 // Device should not start ramping up because system is muted.
1732 EXPECT_EQ(0, rc);
1733 EXPECT_EQ(0, cras_ramp_start_is_called);
1734
1735 ResetStubData();
1736
1737 // Case 5.1: Assume device with ramp is in open state.
1738 iodev.state = CRAS_IODEV_STATE_OPEN;
1739 // Assume sample is ready.
1740 dev_stream_playback_frames_ret = 100;
1741
1742 rc = cras_iodev_prepare_output_before_write_samples(&iodev);
1743
1744 // Device should start ramping up without setting mute callback.
1745 EXPECT_EQ(0, rc);
1746 EXPECT_EQ(1, cras_ramp_start_is_called);
1747 EXPECT_EQ(1, cras_ramp_start_is_up);
1748 EXPECT_EQ(fmt.frame_rate * RAMP_NEW_STREAM_DURATION_SECS,
1749 cras_ramp_start_duration_frames);
1750 EXPECT_EQ(NULL, cras_ramp_start_cb);
1751 EXPECT_EQ(NULL, cras_ramp_start_cb_data);
1752
1753 ResetStubData();
1754
1755 // Case 5.2: Assume device with ramp is in open state. But system is muted.
1756 iodev.state = CRAS_IODEV_STATE_OPEN;
1757 // Assume system is muted.
1758 cras_system_get_mute_return = 1;
1759 // Assume sample is ready.
1760 dev_stream_playback_frames_ret = 100;
1761
1762 rc = cras_iodev_prepare_output_before_write_samples(&iodev);
1763
1764 // Device should not start ramping up because system is muted.
1765 EXPECT_EQ(0, rc);
1766 EXPECT_EQ(0, cras_ramp_start_is_called);
1767 }
1768
TEST(IoDev,StartRampUp)1769 TEST(IoDev, StartRampUp) {
1770 struct cras_iodev iodev;
1771 int rc;
1772 struct cras_audio_format fmt;
1773 enum CRAS_IODEV_RAMP_REQUEST req;
1774 memset(&iodev, 0, sizeof(iodev));
1775
1776 // Format will be used in cras_iodev_start_ramp to determine ramp duration.
1777 fmt.format = SND_PCM_FORMAT_S16_LE;
1778 fmt.frame_rate = 48000;
1779 fmt.num_channels = 2;
1780 iodev.format = &fmt;
1781
1782 // Assume device has ramp member.
1783 iodev.ramp = reinterpret_cast<struct cras_ramp*>(0x1);
1784
1785 // Case 1: Device is not opened yet.
1786 ResetStubData();
1787 iodev.state = CRAS_IODEV_STATE_CLOSE;
1788 req = CRAS_IODEV_RAMP_REQUEST_UP_UNMUTE;
1789
1790 rc = cras_iodev_start_ramp(&iodev, req);
1791
1792 // Ramp request is ignored.
1793 EXPECT_EQ(0, rc);
1794 EXPECT_EQ(0, cras_ramp_start_is_called);
1795
1796 // Case 2: Ramp up without mute.
1797 ResetStubData();
1798 iodev.state = CRAS_IODEV_STATE_OPEN;
1799 req = CRAS_IODEV_RAMP_REQUEST_UP_START_PLAYBACK;
1800
1801 rc = cras_iodev_start_ramp(&iodev, req);
1802
1803 // Device should start ramping up without setting mute callback.
1804 EXPECT_EQ(0, rc);
1805 EXPECT_EQ(1, cras_ramp_start_is_called);
1806 EXPECT_EQ(1, cras_ramp_start_is_up);
1807 EXPECT_EQ(fmt.frame_rate * RAMP_NEW_STREAM_DURATION_SECS,
1808 cras_ramp_start_duration_frames);
1809 EXPECT_EQ(NULL, cras_ramp_start_cb);
1810 EXPECT_EQ(NULL, cras_ramp_start_cb_data);
1811
1812 // Case 3: Ramp up for unmute.
1813 ResetStubData();
1814 iodev.state = CRAS_IODEV_STATE_OPEN;
1815 req = CRAS_IODEV_RAMP_REQUEST_UP_UNMUTE;
1816
1817 rc = cras_iodev_start_ramp(&iodev, req);
1818
1819 // Device should start ramping up.
1820 EXPECT_EQ(0, rc);
1821 EXPECT_EQ(1, cras_ramp_start_is_called);
1822 EXPECT_EQ(1, cras_ramp_start_is_up);
1823 EXPECT_EQ(fmt.frame_rate * RAMP_UNMUTE_DURATION_SECS,
1824 cras_ramp_start_duration_frames);
1825 // Callback for unmute is not used.
1826 EXPECT_EQ(NULL, cras_ramp_start_cb);
1827 // Device mute state is set after ramping starts.
1828 EXPECT_EQ(1, cras_device_monitor_set_device_mute_state_called);
1829 EXPECT_EQ(&iodev, cras_device_monitor_set_device_mute_state_dev);
1830 }
1831
TEST(IoDev,StartRampDown)1832 TEST(IoDev, StartRampDown) {
1833 struct cras_iodev iodev;
1834 int rc;
1835 struct cras_audio_format fmt;
1836 enum CRAS_IODEV_RAMP_REQUEST req;
1837 memset(&iodev, 0, sizeof(iodev));
1838
1839 // Format will be used in cras_iodev_start_ramp to determine ramp duration.
1840 fmt.format = SND_PCM_FORMAT_S16_LE;
1841 fmt.frame_rate = 48000;
1842 fmt.num_channels = 2;
1843 iodev.format = &fmt;
1844
1845 // Assume device has ramp member.
1846 iodev.ramp = reinterpret_cast<struct cras_ramp*>(0x1);
1847
1848 // Case 1: Device is not opened yet.
1849 ResetStubData();
1850 iodev.state = CRAS_IODEV_STATE_CLOSE;
1851 req = CRAS_IODEV_RAMP_REQUEST_DOWN_MUTE;
1852
1853 rc = cras_iodev_start_ramp(&iodev, req);
1854
1855 // Ramp request is ignored.
1856 EXPECT_EQ(0, rc);
1857 EXPECT_EQ(0, cras_ramp_start_is_called);
1858
1859 // Case 2: Ramp down for mute.
1860 ResetStubData();
1861 iodev.state = CRAS_IODEV_STATE_OPEN;
1862 req = CRAS_IODEV_RAMP_REQUEST_DOWN_MUTE;
1863
1864 rc = cras_iodev_start_ramp(&iodev, req);
1865
1866 // Device should start ramping down with mute callback.
1867 EXPECT_EQ(0, rc);
1868 EXPECT_EQ(1, cras_ramp_start_is_called);
1869 EXPECT_EQ(0, cras_ramp_start_is_up);
1870 EXPECT_EQ(fmt.frame_rate * RAMP_MUTE_DURATION_SECS,
1871 cras_ramp_start_duration_frames);
1872
1873 // Device mute state is not set yet. It should wait for ramp to finish.
1874 EXPECT_EQ(0, cras_device_monitor_set_device_mute_state_called);
1875 EXPECT_EQ(NULL, cras_device_monitor_set_device_mute_state_dev);
1876
1877 // Assume the callback is set, and it is later called after ramp is done.
1878 // It should trigger cras_device_monitor_set_device_mute_state.
1879 cras_ramp_start_cb(cras_ramp_start_cb_data);
1880 EXPECT_EQ(1, cras_device_monitor_set_device_mute_state_called);
1881 EXPECT_EQ(&iodev, cras_device_monitor_set_device_mute_state_dev);
1882 }
1883
TEST(IoDev,OutputDeviceShouldWake)1884 TEST(IoDev, OutputDeviceShouldWake) {
1885 struct cras_iodev iodev;
1886 int rc;
1887
1888 memset(&iodev, 0, sizeof(iodev));
1889
1890 ResetStubData();
1891
1892 // Device is not running. No need to wake for this device.
1893 iodev.state = CRAS_IODEV_STATE_OPEN;
1894 rc = cras_iodev_odev_should_wake(&iodev);
1895 EXPECT_EQ(0, rc);
1896
1897 // Device is running. Need to wake for this device.
1898 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
1899 rc = cras_iodev_odev_should_wake(&iodev);
1900 EXPECT_EQ(1, rc);
1901
1902 // Device is running. Device has output_should_wake ops.
1903 iodev.output_should_wake = output_should_wake;
1904 output_should_wake_ret = 0;
1905 rc = cras_iodev_odev_should_wake(&iodev);
1906 EXPECT_EQ(0, rc);
1907
1908 // Device is running. Device has output_should_wake ops.
1909 output_should_wake_ret = 1;
1910 rc = cras_iodev_odev_should_wake(&iodev);
1911 EXPECT_EQ(1, rc);
1912
1913 // Ignore input device.
1914 iodev.direction = CRAS_STREAM_INPUT;
1915 rc = cras_iodev_odev_should_wake(&iodev);
1916 EXPECT_EQ(0, rc);
1917 }
1918
TEST(IoDev,FramesToPlayInSleep)1919 TEST(IoDev, FramesToPlayInSleep) {
1920 struct cras_iodev iodev;
1921 struct cras_audio_format fmt;
1922 unsigned int min_cb_level = 512, hw_level;
1923 unsigned int got_hw_level, got_frames;
1924 struct timespec hw_tstamp;
1925
1926 memset(&iodev, 0, sizeof(iodev));
1927 memset(&fmt, 0, sizeof(fmt));
1928 iodev.frames_queued = frames_queued;
1929 iodev.min_buffer_level = 0;
1930 iodev.direction = CRAS_STREAM_OUTPUT;
1931 iodev.buffer_size = BUFFER_SIZE;
1932 iodev.min_cb_level = min_cb_level;
1933 iodev.streams = reinterpret_cast<struct dev_stream *>(0x1);
1934 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
1935 iodev.format = &fmt;
1936 fmt.frame_rate = 48000;
1937
1938 ResetStubData();
1939
1940 // Device is running. There is at least one stream for this device.
1941 // hw_level is greater than min_cb_level.
1942 hw_level = min_cb_level + 50;
1943 fr_queued = hw_level;
1944 got_frames = cras_iodev_frames_to_play_in_sleep(
1945 &iodev, &got_hw_level, &hw_tstamp);
1946 EXPECT_EQ(got_hw_level, hw_level);
1947 EXPECT_EQ(got_frames, 514);
1948
1949 // Device is running. There is at least one stream for this device.
1950 // hw_level is 2x greater than min_cb_level.
1951 hw_level = 2 * min_cb_level + 50;
1952 fr_queued = hw_level;
1953 got_frames = cras_iodev_frames_to_play_in_sleep(
1954 &iodev, &got_hw_level, &hw_tstamp);
1955 EXPECT_EQ(got_hw_level, hw_level);
1956 EXPECT_EQ(got_frames, 1026);
1957
1958 // Device is running. There is at least one stream for this device.
1959 // hw_level is less than min_cb_level.
1960 hw_level = min_cb_level / 2;
1961 fr_queued = hw_level;
1962 got_frames = cras_iodev_frames_to_play_in_sleep(
1963 &iodev, &got_hw_level, &hw_tstamp);
1964 EXPECT_EQ(got_hw_level, hw_level);
1965 EXPECT_EQ(got_frames, 208);
1966
1967 // Device is running. There is no stream for this device.
1968 // hw_level is greater than min_cb_level.
1969 iodev.streams = NULL;
1970
1971 got_frames = cras_iodev_frames_to_play_in_sleep(
1972 &iodev, &got_hw_level, &hw_tstamp);
1973 EXPECT_EQ(got_hw_level, hw_level);
1974 EXPECT_EQ(got_frames, 0);
1975
1976 // Device is running. There is no stream for this device.
1977 // hw_level is less than min_cb_level.
1978 iodev.streams = NULL;
1979 hw_level = min_cb_level - 50;
1980 fr_queued = hw_level;
1981
1982 got_frames = cras_iodev_frames_to_play_in_sleep(
1983 &iodev, &got_hw_level, &hw_tstamp);
1984 EXPECT_EQ(got_hw_level, hw_level);
1985 EXPECT_EQ(got_frames, 0);
1986 }
1987
get_num_underruns(const struct cras_iodev * iodev)1988 static unsigned int get_num_underruns(const struct cras_iodev *iodev) {
1989 return get_num_underruns_ret;
1990 }
1991
TEST(IoDev,GetNumUnderruns)1992 TEST(IoDev, GetNumUnderruns) {
1993 struct cras_iodev iodev;
1994 memset(&iodev, 0, sizeof(iodev));
1995
1996 EXPECT_EQ(0, cras_iodev_get_num_underruns(&iodev));
1997
1998 iodev.get_num_underruns = get_num_underruns;
1999 get_num_underruns_ret = 10;
2000 EXPECT_EQ(10, cras_iodev_get_num_underruns(&iodev));
2001 }
2002
TEST(IoDev,RequestReset)2003 TEST(IoDev, RequestReset) {
2004 struct cras_iodev iodev;
2005 memset(&iodev, 0, sizeof(iodev));
2006
2007 ResetStubData();
2008
2009 iodev.configure_dev = configure_dev;
2010 iodev.direction = CRAS_STREAM_OUTPUT;
2011 iodev.ext_format = &audio_fmt;
2012
2013 iodev.state = CRAS_IODEV_STATE_CLOSE;
2014 iodev_buffer_size = 1024;
2015
2016 // Open device.
2017 cras_iodev_open(&iodev, 240, &audio_fmt);
2018
2019 // The first reset request works.
2020 EXPECT_EQ(0, cras_iodev_reset_request(&iodev));
2021 EXPECT_EQ(1, device_monitor_reset_device_called);
2022
2023 // The second reset request will do nothing.
2024 EXPECT_EQ(0, cras_iodev_reset_request(&iodev));
2025 EXPECT_EQ(1, device_monitor_reset_device_called);
2026
2027 // Assume device is opened again.
2028 cras_iodev_open(&iodev, 240, &audio_fmt);
2029
2030 // The reset request works.
2031 EXPECT_EQ(0, cras_iodev_reset_request(&iodev));
2032 EXPECT_EQ(2, device_monitor_reset_device_called);
2033 }
2034
output_underrun(struct cras_iodev * iodev)2035 static int output_underrun(struct cras_iodev *iodev) {
2036 output_underrun_called++;
2037 return 0;
2038 }
2039
2040
TEST(IoDev,HandleOutputUnderrun)2041 TEST(IoDev, HandleOutputUnderrun) {
2042 struct cras_iodev iodev;
2043 struct cras_audio_format fmt;
2044 unsigned int frames = 240;
2045 int16_t *zeros;
2046 int rc;
2047
2048 ResetStubData();
2049
2050 memset(&iodev, 0, sizeof(iodev));
2051 fmt.format = SND_PCM_FORMAT_S16_LE;
2052 fmt.frame_rate = 48000;
2053 fmt.num_channels = 2;
2054 iodev.ext_format = &fmt;
2055 iodev.get_buffer = get_buffer;
2056 iodev.put_buffer = put_buffer;
2057 iodev.direction = CRAS_STREAM_OUTPUT;
2058 iodev.min_cb_level = frames;
2059
2060 // Default case, fill one block of zeros.
2061 EXPECT_EQ(0, cras_iodev_output_underrun(&iodev));
2062
2063 EXPECT_EQ(frames, put_buffer_nframes);
2064 zeros = (int16_t *)calloc(frames * 2, sizeof(*zeros));
2065 rc = memcmp(audio_buffer, zeros, frames * 2 * 2);
2066 free(zeros);
2067 EXPECT_EQ(0, rc);
2068
2069 // Test iodev has output_underrun ops.
2070 iodev.output_underrun = output_underrun;
2071 EXPECT_EQ(0, cras_iodev_output_underrun(&iodev));
2072 EXPECT_EQ(1, output_underrun_called);
2073 }
2074
ext_mod_configure(struct ext_dsp_module * ext,unsigned int buffer_size,unsigned int num_channels,unsigned int rate)2075 static void ext_mod_configure(
2076 struct ext_dsp_module *ext,
2077 unsigned int buffer_size,
2078 unsigned int num_channels,
2079 unsigned int rate)
2080 {
2081 ext_mod_configure_called++;
2082 }
2083
TEST(IoDev,SetExtDspMod)2084 TEST(IoDev, SetExtDspMod) {
2085 struct cras_iodev iodev;
2086 struct cras_audio_format fmt;
2087 struct ext_dsp_module ext;
2088
2089 ResetStubData();
2090
2091 memset(&iodev, 0, sizeof(iodev));
2092 fmt.format = SND_PCM_FORMAT_S16_LE;
2093 fmt.frame_rate = 48000;
2094 fmt.num_channels = 2;
2095 iodev.configure_dev = configure_dev;
2096 iodev.ext_format = &fmt;
2097 iodev.format = &fmt;
2098 iodev.state = CRAS_IODEV_STATE_CLOSE;
2099 ext.configure = ext_mod_configure;
2100
2101 iodev.dsp_context = reinterpret_cast<cras_dsp_context *>(0xf0f);
2102 cras_dsp_get_pipeline_ret = 0x25;
2103
2104 cras_iodev_set_ext_dsp_module(&iodev, &ext);
2105 EXPECT_EQ(0, ext_mod_configure_called);
2106
2107 cras_iodev_open(&iodev, 240, &fmt);
2108 EXPECT_EQ(1, ext_mod_configure_called);
2109 EXPECT_EQ(1, cras_dsp_get_pipeline_called);
2110 EXPECT_EQ(1, cras_dsp_pipeline_set_sink_ext_module_called);
2111
2112 cras_iodev_set_ext_dsp_module(&iodev, NULL);
2113 EXPECT_EQ(1, ext_mod_configure_called);
2114
2115 cras_iodev_set_ext_dsp_module(&iodev, &ext);
2116 EXPECT_EQ(2, ext_mod_configure_called);
2117 EXPECT_EQ(2, cras_dsp_get_pipeline_called);
2118 EXPECT_EQ(2, cras_dsp_pipeline_set_sink_ext_module_called);
2119
2120 /* If pipeline doesn't exist, dummy pipeline should be loaded. */
2121 cras_dsp_get_pipeline_ret = 0x0;
2122 cras_iodev_set_ext_dsp_module(&iodev, &ext);
2123 EXPECT_EQ(3, ext_mod_configure_called);
2124 EXPECT_EQ(4, cras_dsp_get_pipeline_called);
2125 EXPECT_EQ(1, cras_dsp_load_dummy_pipeline_called);
2126 EXPECT_EQ(3, cras_dsp_pipeline_set_sink_ext_module_called);
2127 }
2128
TEST(IoDev,InputDspOffset)2129 TEST(IoDev, InputDspOffset) {
2130 struct cras_iodev iodev;
2131 struct cras_audio_format fmt;
2132 struct cras_rstream rstream1;
2133 struct dev_stream stream1;
2134 struct input_data data;
2135 unsigned int frames = 240;
2136
2137 ResetStubData();
2138
2139 rstream1.cb_threshold = 240;
2140 rstream1.stream_id = 123;
2141 stream1.stream = &rstream1;
2142
2143 memset(&iodev, 0, sizeof(iodev));
2144 fmt.format = SND_PCM_FORMAT_S16_LE;
2145 fmt.frame_rate = 48000;
2146 fmt.num_channels = 2;
2147 iodev.configure_dev = configure_dev;
2148 iodev.ext_format = &fmt;
2149 iodev.format = &fmt;
2150 iodev.state = CRAS_IODEV_STATE_CLOSE;
2151 iodev.get_buffer = get_buffer;
2152 iodev.put_buffer = put_buffer;
2153 iodev.direction = CRAS_STREAM_INPUT;
2154 iodev.buffer_size = 480;
2155
2156 iodev.dsp_context = reinterpret_cast<cras_dsp_context *>(0xf0f);
2157 cras_dsp_get_pipeline_ret = 0x25;
2158 input_data_create_ret = &data;
2159
2160 cras_iodev_open(&iodev, 240, &fmt);
2161
2162 cras_iodev_add_stream(&iodev, &stream1);
2163 cras_iodev_get_input_buffer(&iodev, &frames);
2164
2165 buffer_share_get_new_write_point_ret = 100;
2166 cras_iodev_put_input_buffer(&iodev);
2167 EXPECT_EQ(140, iodev.input_dsp_offset);
2168
2169 frames = 130;
2170 cras_iodev_get_input_buffer(&iodev, &frames);
2171 EXPECT_EQ(130, iodev.input_frames_read);
2172
2173 buffer_share_get_new_write_point_ret = 80;
2174 cras_iodev_put_input_buffer(&iodev);
2175 EXPECT_EQ(60, iodev.input_dsp_offset);
2176 }
2177
2178 extern "C" {
2179
2180 // From libpthread.
pthread_create(pthread_t * thread,const pthread_attr_t * attr,void * (* start_routine)(void *),void * arg)2181 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
2182 void *(*start_routine)(void*), void *arg) {
2183 return 0;
2184 }
2185
pthread_join(pthread_t thread,void ** value_ptr)2186 int pthread_join(pthread_t thread, void **value_ptr) {
2187 return 0;
2188 }
2189
2190 // Fromt fmt_conv
cras_channel_remix_convert(struct cras_fmt_conv * conv,uint8_t * in_buf,size_t frames)2191 void cras_channel_remix_convert(struct cras_fmt_conv *conv,
2192 uint8_t *in_buf,
2193 size_t frames)
2194 {
2195 }
2196
cras_fmt_conv_in_frames_to_out(struct cras_fmt_conv * conv,size_t in_frames)2197 size_t cras_fmt_conv_in_frames_to_out(struct cras_fmt_conv *conv,
2198 size_t in_frames)
2199 {
2200 return in_frames;
2201 }
2202
2203 // From buffer_share
buffer_share_create(unsigned int buf_sz)2204 struct buffer_share *buffer_share_create(unsigned int buf_sz) {
2205 return NULL;
2206 }
2207
buffer_share_destroy(struct buffer_share * mix)2208 void buffer_share_destroy(struct buffer_share *mix)
2209 {
2210 }
2211
buffer_share_offset_update(struct buffer_share * mix,unsigned int id,unsigned int frames)2212 int buffer_share_offset_update(struct buffer_share *mix, unsigned int id,
2213 unsigned int frames) {
2214 return 0;
2215 }
2216
buffer_share_get_new_write_point(struct buffer_share * mix)2217 unsigned int buffer_share_get_new_write_point(struct buffer_share *mix) {
2218 return buffer_share_get_new_write_point_ret;
2219 }
2220
buffer_share_add_id(struct buffer_share * mix,unsigned int id)2221 int buffer_share_add_id(struct buffer_share *mix, unsigned int id) {
2222 buffer_share_add_id_called++;
2223 return 0;
2224 }
2225
buffer_share_rm_id(struct buffer_share * mix,unsigned int id)2226 int buffer_share_rm_id(struct buffer_share *mix, unsigned int id) {
2227 return 0;
2228 }
2229
buffer_share_id_offset(const struct buffer_share * mix,unsigned int id)2230 unsigned int buffer_share_id_offset(const struct buffer_share *mix,
2231 unsigned int id)
2232 {
2233 return 0;
2234 }
2235
2236 // From cras_system_state.
cras_system_state_stream_added(enum CRAS_STREAM_DIRECTION direction)2237 void cras_system_state_stream_added(enum CRAS_STREAM_DIRECTION direction) {
2238 }
2239
cras_system_state_stream_removed(enum CRAS_STREAM_DIRECTION direction)2240 void cras_system_state_stream_removed(enum CRAS_STREAM_DIRECTION direction) {
2241 }
2242
2243 // From cras_dsp
cras_dsp_context_new(int sample_rate,const char * purpose)2244 struct cras_dsp_context *cras_dsp_context_new(int sample_rate,
2245 const char *purpose)
2246 {
2247 dsp_context_new_sample_rate = sample_rate;
2248 dsp_context_new_purpose = purpose;
2249 return cras_dsp_context_new_return;
2250 }
2251
cras_dsp_context_free(struct cras_dsp_context * ctx)2252 void cras_dsp_context_free(struct cras_dsp_context *ctx)
2253 {
2254 dsp_context_free_called++;
2255 }
2256
cras_dsp_load_pipeline(struct cras_dsp_context * ctx)2257 void cras_dsp_load_pipeline(struct cras_dsp_context *ctx)
2258 {
2259 }
cras_dsp_load_dummy_pipeline(struct cras_dsp_context * ctx,unsigned int num_channels)2260 void cras_dsp_load_dummy_pipeline(struct cras_dsp_context *ctx,
2261 unsigned int num_channels)
2262 {
2263 cras_dsp_load_dummy_pipeline_called++;
2264 }
2265
cras_dsp_set_variable_string(struct cras_dsp_context * ctx,const char * key,const char * value)2266 void cras_dsp_set_variable_string(struct cras_dsp_context *ctx, const char *key,
2267 const char *value)
2268 {
2269 }
2270
cras_dsp_set_variable_boolean(struct cras_dsp_context * ctx,const char * key,char value)2271 void cras_dsp_set_variable_boolean(struct cras_dsp_context *ctx,
2272 const char *key,
2273 char value)
2274 {
2275 }
2276
cras_dsp_get_pipeline(struct cras_dsp_context * ctx)2277 struct pipeline *cras_dsp_get_pipeline(struct cras_dsp_context *ctx)
2278 {
2279 cras_dsp_get_pipeline_called++;
2280 return reinterpret_cast<struct pipeline *>(cras_dsp_get_pipeline_ret);
2281 }
2282
cras_dsp_put_pipeline(struct cras_dsp_context * ctx)2283 void cras_dsp_put_pipeline(struct cras_dsp_context *ctx)
2284 {
2285 cras_dsp_put_pipeline_called++;
2286 }
2287
cras_dsp_pipeline_get_source_buffer(struct pipeline * pipeline,int index)2288 float *cras_dsp_pipeline_get_source_buffer(struct pipeline *pipeline,
2289 int index)
2290 {
2291 cras_dsp_pipeline_get_source_buffer_called++;
2292 return cras_dsp_pipeline_source_buffer[index];
2293 }
2294
cras_dsp_pipeline_get_sink_buffer(struct pipeline * pipeline,int index)2295 float *cras_dsp_pipeline_get_sink_buffer(struct pipeline *pipeline, int index)
2296 {
2297 cras_dsp_pipeline_get_sink_buffer_called++;
2298 return cras_dsp_pipeline_sink_buffer[index];
2299 }
2300
cras_dsp_pipeline_get_delay(struct pipeline * pipeline)2301 int cras_dsp_pipeline_get_delay(struct pipeline *pipeline)
2302 {
2303 cras_dsp_pipeline_get_delay_called++;
2304 return 0;
2305 }
2306
cras_dsp_pipeline_apply(struct pipeline * pipeline,uint8_t * buf,snd_pcm_format_t format,unsigned int frames)2307 int cras_dsp_pipeline_apply(struct pipeline *pipeline,
2308 uint8_t *buf, snd_pcm_format_t format,
2309 unsigned int frames)
2310 {
2311 cras_dsp_pipeline_apply_called++;
2312 cras_dsp_pipeline_apply_sample_count = frames;
2313 return 0;
2314 }
2315
cras_dsp_pipeline_add_statistic(struct pipeline * pipeline,const struct timespec * time_delta,int samples)2316 void cras_dsp_pipeline_add_statistic(struct pipeline *pipeline,
2317 const struct timespec *time_delta,
2318 int samples)
2319 {
2320 }
cras_dsp_pipeline_set_sink_ext_module(struct pipeline * pipeline,struct ext_dsp_module * ext_module)2321 void cras_dsp_pipeline_set_sink_ext_module(struct pipeline *pipeline,
2322 struct ext_dsp_module *ext_module)
2323 {
2324 cras_dsp_pipeline_set_sink_ext_module_called++;
2325 }
2326
cras_dsp_num_output_channels(const struct cras_dsp_context * ctx)2327 unsigned int cras_dsp_num_output_channels(const struct cras_dsp_context *ctx)
2328 {
2329 return cras_dsp_num_output_channels_return;
2330 }
2331
cras_dsp_num_input_channels(const struct cras_dsp_context * ctx)2332 unsigned int cras_dsp_num_input_channels(const struct cras_dsp_context *ctx)
2333 {
2334 return cras_dsp_num_input_channels_return;
2335 }
2336
2337 // From audio thread
audio_thread_post_message(struct audio_thread * thread,struct audio_thread_msg * msg)2338 int audio_thread_post_message(struct audio_thread *thread,
2339 struct audio_thread_msg *msg) {
2340 return 0;
2341 }
2342
cras_iodev_list_select_node(enum CRAS_STREAM_DIRECTION direction,cras_node_id_t node_id)2343 void cras_iodev_list_select_node(enum CRAS_STREAM_DIRECTION direction,
2344 cras_node_id_t node_id)
2345 {
2346 select_node_called++;
2347 select_node_direction = direction;
2348 select_node_id = node_id;
2349 }
2350
cras_iodev_list_node_selected(struct cras_ionode * node)2351 int cras_iodev_list_node_selected(struct cras_ionode *node)
2352 {
2353 return node == node_selected;
2354 }
2355
cras_iodev_list_disable_dev(struct cras_iodev * dev)2356 void cras_iodev_list_disable_dev(struct cras_iodev *dev)
2357 {
2358 cras_iodev_list_disable_dev_called++;
2359 }
2360
cras_iodev_list_notify_nodes_changed()2361 void cras_iodev_list_notify_nodes_changed()
2362 {
2363 notify_nodes_changed_called++;
2364 }
2365
cras_iodev_list_notify_active_node_changed(enum CRAS_STREAM_DIRECTION direction)2366 void cras_iodev_list_notify_active_node_changed(
2367 enum CRAS_STREAM_DIRECTION direction)
2368 {
2369 notify_active_node_changed_called++;
2370 }
2371
cras_iodev_list_notify_node_volume(struct cras_ionode * node)2372 void cras_iodev_list_notify_node_volume(struct cras_ionode *node)
2373 {
2374 notify_node_volume_called++;
2375 }
2376
cras_iodev_list_notify_node_capture_gain(struct cras_ionode * node)2377 void cras_iodev_list_notify_node_capture_gain(struct cras_ionode *node)
2378 {
2379 notify_node_capture_gain_called++;
2380 }
2381
cras_iodev_list_notify_node_left_right_swapped(struct cras_ionode * node)2382 void cras_iodev_list_notify_node_left_right_swapped(struct cras_ionode *node)
2383 {
2384 notify_node_left_right_swapped_called++;
2385 }
2386
cras_audio_area_create(int num_channels)2387 struct cras_audio_area *cras_audio_area_create(int num_channels) {
2388 return NULL;
2389 }
2390
cras_audio_area_destroy(struct cras_audio_area * area)2391 void cras_audio_area_destroy(struct cras_audio_area *area) {
2392 }
2393
cras_audio_area_config_channels(struct cras_audio_area * area,const struct cras_audio_format * fmt)2394 void cras_audio_area_config_channels(struct cras_audio_area *area,
2395 const struct cras_audio_format *fmt) {
2396 }
2397
cras_audio_format_set_channel_layout(struct cras_audio_format * format,const int8_t layout[CRAS_CH_MAX])2398 int cras_audio_format_set_channel_layout(struct cras_audio_format *format,
2399 const int8_t layout[CRAS_CH_MAX])
2400 {
2401 int i;
2402 cras_audio_format_set_channel_layout_called++;
2403 for (i = 0; i < CRAS_CH_MAX; i++)
2404 format->channel_layout[i] = layout[i];
2405 return 0;
2406 }
2407
softvol_get_scaler(unsigned int volume_index)2408 float softvol_get_scaler(unsigned int volume_index)
2409 {
2410 return softvol_scalers[volume_index];
2411 }
2412
cras_system_get_volume()2413 size_t cras_system_get_volume() {
2414 return cras_system_get_volume_return;
2415 }
2416
cras_system_get_capture_gain()2417 long cras_system_get_capture_gain() {
2418 return cras_system_get_capture_gain_ret_value;
2419 }
2420
cras_system_get_mute()2421 int cras_system_get_mute() {
2422 return cras_system_get_mute_return;
2423 }
2424
cras_system_get_capture_mute()2425 int cras_system_get_capture_mute() {
2426 return 0;
2427 }
2428
cras_scale_buffer(snd_pcm_format_t fmt,uint8_t * buffer,unsigned int count,float scaler)2429 void cras_scale_buffer(snd_pcm_format_t fmt, uint8_t *buffer,
2430 unsigned int count, float scaler) {
2431 cras_scale_buffer_called++;
2432 cras_scale_buffer_fmt = fmt;
2433 cras_scale_buffer_scaler = scaler;
2434 }
2435
cras_scale_buffer_increment(snd_pcm_format_t fmt,uint8_t * buff,unsigned int frame,float scaler,float increment,int channel)2436 void cras_scale_buffer_increment(snd_pcm_format_t fmt, uint8_t *buff,
2437 unsigned int frame, float scaler,
2438 float increment, int channel)
2439 {
2440 cras_scale_buffer_increment_fmt = fmt;
2441 cras_scale_buffer_increment_buff = buff;
2442 cras_scale_buffer_increment_frame = frame;
2443 cras_scale_buffer_increment_scaler = scaler;
2444 cras_scale_buffer_increment_increment = increment;
2445 cras_scale_buffer_increment_channel = channel;
2446 }
2447
cras_mix_mute_buffer(uint8_t * dst,size_t frame_bytes,size_t count)2448 size_t cras_mix_mute_buffer(uint8_t *dst,
2449 size_t frame_bytes,
2450 size_t count) {
2451 cras_mix_mute_count = count;
2452 return count;
2453 }
2454
rate_estimator_create(unsigned int rate,const struct timespec * window_size,double smooth_factor)2455 struct rate_estimator *rate_estimator_create(unsigned int rate,
2456 const struct timespec *window_size,
2457 double smooth_factor) {
2458 return NULL;
2459 }
2460
rate_estimator_destroy(struct rate_estimator * re)2461 void rate_estimator_destroy(struct rate_estimator *re) {
2462 }
2463
rate_estimator_add_frames(struct rate_estimator * re,int fr)2464 void rate_estimator_add_frames(struct rate_estimator *re, int fr) {
2465 rate_estimator_add_frames_called++;
2466 rate_estimator_add_frames_num_frames = fr;
2467 }
2468
rate_estimator_check(struct rate_estimator * re,int level,struct timespec * now)2469 int rate_estimator_check(struct rate_estimator *re, int level,
2470 struct timespec *now) {
2471 return 0;
2472 }
2473
rate_estimator_reset_rate(struct rate_estimator * re,unsigned int rate)2474 void rate_estimator_reset_rate(struct rate_estimator *re, unsigned int rate) {
2475 }
2476
rate_estimator_get_rate(struct rate_estimator * re)2477 double rate_estimator_get_rate(struct rate_estimator *re) {
2478 return 0.0;
2479 }
2480
dev_stream_cb_threshold(const struct dev_stream * dev_stream)2481 unsigned int dev_stream_cb_threshold(const struct dev_stream *dev_stream) {
2482 if (dev_stream->stream)
2483 return dev_stream->stream->cb_threshold;
2484 return 0;
2485 }
2486
dev_stream_attached_devs(const struct dev_stream * dev_stream)2487 int dev_stream_attached_devs(const struct dev_stream *dev_stream) {
2488 return 1;
2489 }
2490
dev_stream_update_frames(const struct dev_stream * dev_stream)2491 void dev_stream_update_frames(const struct dev_stream *dev_stream) {
2492 }
2493
dev_stream_playback_frames(const struct dev_stream * dev_stream)2494 int dev_stream_playback_frames(const struct dev_stream *dev_stream) {
2495 return dev_stream_playback_frames_ret;
2496 }
2497
cras_device_monitor_reset_device(struct cras_iodev * iodev)2498 int cras_device_monitor_reset_device(struct cras_iodev *iodev) {
2499 device_monitor_reset_device_called++;
2500 return 0;
2501 }
2502
cras_ramp_destroy(struct cras_ramp * ramp)2503 void cras_ramp_destroy(struct cras_ramp* ramp) {
2504 return;
2505 }
2506
cras_ramp_start(struct cras_ramp * ramp,int is_up,int duration_frames,cras_ramp_cb cb,void * cb_data)2507 int cras_ramp_start(struct cras_ramp *ramp, int is_up, int duration_frames,
2508 cras_ramp_cb cb, void *cb_data)
2509 {
2510 cras_ramp_start_is_called++;
2511 cras_ramp_start_is_up = is_up;
2512 cras_ramp_start_duration_frames = duration_frames;
2513 cras_ramp_start_cb = cb;
2514 cras_ramp_start_cb_data = cb_data;
2515 return 0;
2516 }
2517
cras_ramp_reset(struct cras_ramp * ramp)2518 int cras_ramp_reset(struct cras_ramp *ramp) {
2519 cras_ramp_reset_is_called++;
2520 return 0;
2521 }
2522
cras_ramp_get_current_action(const struct cras_ramp * ramp)2523 struct cras_ramp_action cras_ramp_get_current_action(
2524 const struct cras_ramp *ramp) {
2525 return cras_ramp_get_current_action_ret;
2526 }
2527
cras_ramp_update_ramped_frames(struct cras_ramp * ramp,int num_frames)2528 int cras_ramp_update_ramped_frames(
2529 struct cras_ramp *ramp, int num_frames) {
2530 cras_ramp_update_ramped_frames_num_frames = num_frames;
2531 return 0;
2532 }
2533
cras_device_monitor_set_device_mute_state(struct cras_iodev * iodev)2534 int cras_device_monitor_set_device_mute_state(struct cras_iodev *iodev)
2535 {
2536 cras_device_monitor_set_device_mute_state_called++;
2537 cras_device_monitor_set_device_mute_state_dev = iodev;
2538 return 0;
2539 }
2540
mod_run(struct ext_dsp_module * ext,unsigned int nframes)2541 static void mod_run(struct ext_dsp_module *ext,
2542 unsigned int nframes)
2543 {
2544 }
2545
mod_configure(struct ext_dsp_module * ext,unsigned int buffer_size,unsigned int num_channels,unsigned int rate)2546 static void mod_configure(struct ext_dsp_module *ext,
2547 unsigned int buffer_size,
2548 unsigned int num_channels,
2549 unsigned int rate)
2550 {
2551 }
2552
input_data_create(void * dev_ptr)2553 struct input_data *input_data_create(void *dev_ptr)
2554 {
2555 if (input_data_create_ret) {
2556 input_data_create_ret->ext.run = mod_run;
2557 input_data_create_ret->ext.configure = mod_configure;
2558 }
2559 return input_data_create_ret;
2560 }
2561
input_data_destroy(struct input_data ** data)2562 void input_data_destroy(struct input_data **data)
2563 {
2564 }
input_data_set_all_streams_read(struct input_data * data,unsigned int nframes)2565 void input_data_set_all_streams_read(struct input_data *data,
2566 unsigned int nframes)
2567 {
2568 }
2569
cras_audio_thread_severe_underrun()2570 int cras_audio_thread_severe_underrun()
2571 {
2572 return 0;
2573 }
2574
cras_audio_thread_underrun()2575 int cras_audio_thread_underrun()
2576 {
2577 return 0;
2578 }
2579
2580 } // extern "C"
2581 } // namespace
2582
main(int argc,char ** argv)2583 int main(int argc, char **argv) {
2584 ::testing::InitGoogleTest(&argc, argv);
2585 int rc = RUN_ALL_TESTS();
2586
2587 audio_thread_event_log_deinit(atlog);
2588 return rc;
2589 }
2590