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