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