1 // Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <stdio.h>
6 #include <gtest/gtest.h>
7 
8 extern "C" {
9 #include "audio_thread_log.h"
10 #include "byte_buffer.h"
11 #include "cras_audio_area.h"
12 #include "cras_rstream.h"
13 #include "cras_shm.h"
14 #include "cras_types.h"
15 #include "dev_stream.h"
16 }
17 
18 namespace {
19 
20 extern "C" {
21 struct audio_thread_event_log *atlog;
22 unsigned int max_frames_for_conversion(
23     unsigned int stream_frames,
24     unsigned int stream_rate,
25     unsigned int device_rate);
26 };
27 
28 static struct timespec clock_gettime_retspec;
29 static struct timespec cb_ts;
30 
31 static const int kBufferFrames = 1024;
32 static const struct cras_audio_format fmt_s16le_44_1 = {
33   SND_PCM_FORMAT_S16_LE,
34   44100,
35   2,
36 };
37 static const struct cras_audio_format fmt_s16le_48 = {
38   SND_PCM_FORMAT_S16_LE,
39   48000,
40   2,
41 };
42 static const struct cras_audio_format fmt_s16le_48_mono = {
43   SND_PCM_FORMAT_S16_LE,
44   48000,
45   1,
46 };
47 static const struct cras_audio_format fmt_s16le_8 = {
48   SND_PCM_FORMAT_S16_LE,
49   8000,
50   2,
51 };
52 
53 struct cras_audio_area_copy_call {
54   const struct cras_audio_area *dst;
55   unsigned int dst_offset;
56   unsigned int dst_format_bytes;
57   const struct cras_audio_area *src;
58   unsigned int src_offset;
59   float software_gain_scaler;
60 };
61 
62 struct fmt_conv_call {
63   struct cras_fmt_conv *conv;
64   uint8_t *in_buf;
65   uint8_t *out_buf;
66   size_t in_frames;
67   size_t out_frames;
68 };
69 
70 struct mix_add_call {
71   int16_t *dst;
72   int16_t *src;
73   unsigned int count;
74   unsigned int index;
75   int mute;
76   float mix_vol;
77 };
78 
79 struct rstream_get_readable_call {
80   struct cras_rstream *rstream;
81   unsigned int offset;
82   unsigned int num_called;
83 };
84 
85 static int config_format_converter_called;
86 static const struct cras_audio_format *config_format_converter_from_fmt;
87 static int config_format_converter_frames;
88 static struct cras_fmt_conv *config_format_converter_conv;
89 static struct cras_audio_format in_fmt;
90 static struct cras_audio_format out_fmt;
91 static struct cras_audio_area_copy_call copy_area_call;
92 static struct fmt_conv_call conv_frames_call;
93 static int cras_audio_area_create_num_channels_val;
94 static int cras_fmt_conversion_needed_val;
95 static int cras_fmt_conv_set_linear_resample_rates_called;
96 static float cras_fmt_conv_set_linear_resample_rates_from;
97 static float cras_fmt_conv_set_linear_resample_rates_to;
98 
99 static unsigned int rstream_playable_frames_ret;
100 static struct mix_add_call mix_add_call;
101 static struct rstream_get_readable_call rstream_get_readable_call;
102 static unsigned int rstream_get_readable_num;
103 static uint8_t *rstream_get_readable_ptr;
104 
105 static struct cras_audio_format *cras_rstream_post_processing_format_val;
106 static int cras_rstream_audio_ready_called;
107 static int cras_rstream_audio_ready_count;
108 static int cras_rstream_is_pending_reply_ret;
109 static int cras_rstream_flush_old_audio_messages_called;
110 
111 class CreateSuite : public testing::Test{
112   protected:
SetUp()113     virtual void SetUp() {
114       in_fmt.format = SND_PCM_FORMAT_S16_LE;
115       out_fmt.format = SND_PCM_FORMAT_S16_LE;
116       in_fmt.num_channels = 2;
117       out_fmt.num_channels = 2;
118 
119       SetupShm(&rstream_.shm);
120 
121       rstream_.stream_id = 0x10001;
122       rstream_.buffer_frames = kBufferFrames;
123       rstream_.cb_threshold = kBufferFrames / 2;
124       rstream_.is_draining = 0;
125       rstream_.stream_type = CRAS_STREAM_TYPE_DEFAULT;
126       rstream_.direction = CRAS_STREAM_OUTPUT;
127       rstream_.format.format = SND_PCM_FORMAT_S16_LE;
128       rstream_.format.num_channels = 2;
129       rstream_.format = fmt_s16le_44_1;
130       rstream_.flags = 0;
131 
132       config_format_converter_from_fmt = NULL;
133       config_format_converter_called = 0;
134       cras_fmt_conversion_needed_val = 0;
135       cras_fmt_conv_set_linear_resample_rates_called = 0;
136 
137       cras_rstream_audio_ready_called = 0;
138       cras_rstream_audio_ready_count = 0;
139       cras_rstream_is_pending_reply_ret = 0;
140       cras_rstream_flush_old_audio_messages_called = 0;
141 
142       memset(&copy_area_call, 0xff, sizeof(copy_area_call));
143       memset(&conv_frames_call, 0xff, sizeof(conv_frames_call));
144 
145       atlog = audio_thread_event_log_init();
146 
147       devstr.stream = &rstream_;
148       devstr.conv = NULL;
149       devstr.conv_buffer = NULL;
150       devstr.conv_buffer_size_frames = 0;
151 
152       area = (struct cras_audio_area*)calloc(1,
153           sizeof(*area) + 2 * sizeof(struct cras_channel_area));
154       area->num_channels = 2;
155       channel_area_set_channel(&area->channels[0], CRAS_CH_FL);
156       channel_area_set_channel(&area->channels[1], CRAS_CH_FR);
157       area->channels[0].step_bytes = 4;
158       area->channels[0].buf = (uint8_t *)(cap_buf);
159       area->channels[1].step_bytes = 4;
160       area->channels[1].buf = (uint8_t *)(cap_buf + 1);
161       area->frames = kBufferFrames;
162 
163       stream_area = (struct cras_audio_area*)calloc(1,
164           sizeof(*area) + 2 * sizeof(struct cras_channel_area));
165       stream_area->num_channels = 2;
166       rstream_.audio_area = stream_area;
167       int16_t *shm_samples = (int16_t *)rstream_.shm.area->samples;
168       stream_area->channels[0].step_bytes = 4;
169       stream_area->channels[0].buf = (uint8_t *)(shm_samples);
170       stream_area->channels[1].step_bytes = 4;
171       stream_area->channels[1].buf = (uint8_t *)(shm_samples + 1);
172     }
173 
TearDown()174     virtual void TearDown() {
175       free(area);
176       free(stream_area);
177       free(rstream_.shm.area);
178       audio_thread_event_log_deinit(atlog);
179     }
180 
SetupShm(struct cras_audio_shm * shm)181     void SetupShm(struct cras_audio_shm *shm) {
182       int16_t *buf;
183 
184       shm->area = static_cast<struct cras_audio_shm_area *>(
185           calloc(1, kBufferFrames * 4 + sizeof(cras_audio_shm_area)));
186       cras_shm_set_frame_bytes(shm, 4);
187       cras_shm_set_used_size(shm,
188                              kBufferFrames * cras_shm_frame_bytes(shm));
189 
190       buf = (int16_t *)shm->area->samples;
191       for (size_t i = 0; i < kBufferFrames * 2; i++)
192         buf[i] = i;
193       cras_shm_set_mute(shm, 0);
194       cras_shm_set_volume_scaler(shm, 1.0);
195     }
196 
SetUpFmtConv(unsigned int in_rate,unsigned int out_rate,unsigned int conv_buf_size)197     void SetUpFmtConv(unsigned int in_rate, unsigned int out_rate,
198                       unsigned int conv_buf_size) {
199       in_fmt.frame_rate = in_rate;
200       out_fmt.frame_rate = out_rate;
201       cras_fmt_conversion_needed_val = 1;
202 
203       devstr.conv = (struct cras_fmt_conv *)0xdead;
204       devstr.conv_buffer =
205           (struct byte_buffer *)byte_buffer_create(conv_buf_size * 4);
206       devstr.conv_buffer_size_frames = kBufferFrames * 2;
207 
208       devstr.conv_area = (struct cras_audio_area*)calloc(1,
209           sizeof(*area) + 2 * sizeof(*area->channels));
210       devstr.conv_area->num_channels = 2;
211       devstr.conv_area->channels[0].step_bytes = 4;
212       devstr.conv_area->channels[0].buf = (uint8_t *)(devstr.conv_buffer->bytes);
213       devstr.conv_area->channels[1].step_bytes = 4;
214       devstr.conv_area->channels[1].buf =
215           (uint8_t *)(devstr.conv_buffer->bytes + 1);
216     }
217 
218   struct dev_stream devstr;
219   struct cras_audio_area *area;
220   struct cras_audio_area *stream_area;
221   int16_t cap_buf[kBufferFrames * 2];
222   struct cras_rstream rstream_;
223 };
224 
TEST_F(CreateSuite,CaptureNoSRC)225 TEST_F(CreateSuite, CaptureNoSRC) {
226   float software_gain_scaler = 10;
227 
228   dev_stream_capture(&devstr, area, 0, software_gain_scaler);
229 
230   EXPECT_EQ(stream_area, copy_area_call.dst);
231   EXPECT_EQ(0, copy_area_call.dst_offset);
232   EXPECT_EQ(4, copy_area_call.dst_format_bytes);
233   EXPECT_EQ(area, copy_area_call.src);
234   EXPECT_EQ(software_gain_scaler, copy_area_call.software_gain_scaler);
235 
236 }
237 
TEST_F(CreateSuite,CaptureSRCSmallConverterBuffer)238 TEST_F(CreateSuite, CaptureSRCSmallConverterBuffer) {
239   float software_gain_scaler = 10;
240   unsigned int conv_buf_avail_at_input_rate;
241   int nread;
242 
243   SetUpFmtConv(44100, 32000, kBufferFrames / 4);
244   nread = dev_stream_capture(&devstr, area, 0, software_gain_scaler);
245 
246   // |nread| is bound by small converter buffer size (kBufferFrames / 4)
247   conv_buf_avail_at_input_rate =
248       cras_frames_at_rate(out_fmt.frame_rate,
249                           (kBufferFrames / 4),
250                           in_fmt.frame_rate);
251 
252   EXPECT_EQ(conv_buf_avail_at_input_rate, nread);
253   EXPECT_EQ((struct cras_fmt_conv *)0xdead, conv_frames_call.conv);
254   EXPECT_EQ((uint8_t *)cap_buf, conv_frames_call.in_buf);
255   EXPECT_EQ(devstr.conv_buffer->bytes, conv_frames_call.out_buf);
256 
257   EXPECT_EQ(conv_buf_avail_at_input_rate, conv_frames_call.in_frames);
258 
259   // Expect number of output frames is limited by the size of converter buffer.
260   EXPECT_EQ(kBufferFrames / 4, conv_frames_call.out_frames);
261 
262   EXPECT_EQ(stream_area, copy_area_call.dst);
263   EXPECT_EQ(0, copy_area_call.dst_offset);
264   EXPECT_EQ(4, copy_area_call.dst_format_bytes);
265   EXPECT_EQ(devstr.conv_area, copy_area_call.src);
266   EXPECT_EQ(software_gain_scaler, copy_area_call.software_gain_scaler);
267 
268   free(devstr.conv_area);
269   byte_buffer_destroy(&devstr.conv_buffer);
270 }
271 
TEST_F(CreateSuite,CaptureSRCLargeConverterBuffer)272 TEST_F(CreateSuite, CaptureSRCLargeConverterBuffer) {
273   float software_gain_scaler = 10;
274   unsigned int stream_avail_at_input_rate;
275   int nread;
276 
277   SetUpFmtConv(44100, 32000, kBufferFrames * 2);
278   nread = dev_stream_capture(&devstr, area, 0, software_gain_scaler);
279 
280   // Available frames at stream side is bound by cb_threshold, which
281   // equals to kBufferFrames / 2.
282   stream_avail_at_input_rate =
283       cras_frames_at_rate(out_fmt.frame_rate,
284                           (kBufferFrames / 2),
285                           in_fmt.frame_rate);
286 
287   EXPECT_EQ(stream_avail_at_input_rate, nread);
288   EXPECT_EQ((struct cras_fmt_conv *)0xdead, conv_frames_call.conv);
289   EXPECT_EQ((uint8_t *)cap_buf, conv_frames_call.in_buf);
290   EXPECT_EQ(devstr.conv_buffer->bytes, conv_frames_call.out_buf);
291 
292   // Expect number of input frames is limited by |stream_avail_at_input_rate|
293   // at format conversion.
294   EXPECT_EQ(stream_avail_at_input_rate, conv_frames_call.in_frames);
295 
296   // Expect number of output frames is limited by the size of converter buffer.
297   EXPECT_EQ(kBufferFrames * 2, conv_frames_call.out_frames);
298 
299   EXPECT_EQ(stream_area, copy_area_call.dst);
300   EXPECT_EQ(0, copy_area_call.dst_offset);
301   EXPECT_EQ(4, copy_area_call.dst_format_bytes);
302   EXPECT_EQ(devstr.conv_area, copy_area_call.src);
303   EXPECT_EQ(software_gain_scaler, copy_area_call.software_gain_scaler);
304 
305   free(devstr.conv_area);
306   byte_buffer_destroy(&devstr.conv_buffer);
307 }
308 
TEST_F(CreateSuite,CreateSRC44to48)309 TEST_F(CreateSuite, CreateSRC44to48) {
310   struct dev_stream *dev_stream;
311 
312   rstream_.format = fmt_s16le_44_1;
313   in_fmt.frame_rate = 44100;  // Input to converter is stream rate.
314   out_fmt.frame_rate = 48000;  // Output from converter is device rate.
315   config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
316   dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_48, (void *)0x55,
317                                  &cb_ts);
318   EXPECT_EQ(1, config_format_converter_called);
319   EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
320   // Converter tmp and output buffers are large enough for device output.
321   unsigned int device_frames =
322       cras_frames_at_rate(in_fmt.frame_rate, kBufferFrames,
323                           out_fmt.frame_rate);
324   EXPECT_LE(kBufferFrames, device_frames); // Sanity check.
325   EXPECT_LE(device_frames, config_format_converter_frames);
326   EXPECT_LE(device_frames, dev_stream->conv_buffer_size_frames);
327   dev_stream_destroy(dev_stream);
328 }
329 
TEST_F(CreateSuite,CreateSRC44from48Input)330 TEST_F(CreateSuite, CreateSRC44from48Input) {
331   struct dev_stream *dev_stream;
332   struct cras_audio_format processed_fmt = fmt_s16le_48;
333 
334   processed_fmt.num_channels = 1;
335   rstream_.format = fmt_s16le_44_1;
336   rstream_.direction = CRAS_STREAM_INPUT;
337   in_fmt.frame_rate = 48000;  // Input to converter is device rate.
338   out_fmt.frame_rate = 44100;  // Output from converter is stream rate.
339   config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
340   cras_rstream_post_processing_format_val = &processed_fmt;
341   dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_48, (void *)0x55,
342                                  &cb_ts);
343   EXPECT_EQ(1, config_format_converter_called);
344   EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
345   // Converter tmp and output buffers are large enough for device input.
346   unsigned int device_frames =
347       cras_frames_at_rate(out_fmt.frame_rate, kBufferFrames,
348                           in_fmt.frame_rate);
349   EXPECT_LE(kBufferFrames, device_frames); // Sanity check.
350   EXPECT_LE(device_frames, config_format_converter_frames);
351   EXPECT_EQ(&processed_fmt, config_format_converter_from_fmt);
352   EXPECT_LE(device_frames, dev_stream->conv_buffer_size_frames);
353   dev_stream_destroy(dev_stream);
354 }
355 
TEST_F(CreateSuite,CreateSRC48to44)356 TEST_F(CreateSuite, CreateSRC48to44) {
357   struct dev_stream *dev_stream;
358 
359   rstream_.format = fmt_s16le_48;
360   in_fmt.frame_rate = 48000;  // Stream rate.
361   out_fmt.frame_rate = 44100;  // Device rate.
362   config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
363   dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_44_1, (void *)0x55,
364                                  &cb_ts);
365   EXPECT_EQ(1, config_format_converter_called);
366   EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
367   // Converter tmp and output buffers are large enough for stream input.
368   EXPECT_LE(kBufferFrames, config_format_converter_frames);
369   EXPECT_LE(kBufferFrames, dev_stream->conv_buffer_size_frames);
370   dev_stream_destroy(dev_stream);
371 }
372 
TEST_F(CreateSuite,CreateSRC48from44Input)373 TEST_F(CreateSuite, CreateSRC48from44Input) {
374   struct dev_stream *dev_stream;
375 
376   rstream_.format = fmt_s16le_48;
377   rstream_.direction = CRAS_STREAM_INPUT;
378   in_fmt.frame_rate = 44100;  // Device rate.
379   out_fmt.frame_rate = 48000;  // Stream rate.
380   config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
381   dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_44_1, (void *)0x55,
382                                  &cb_ts);
383   EXPECT_EQ(1, config_format_converter_called);
384   EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
385   // Converter tmp and output buffers are large enough for stream output.
386   EXPECT_LE(kBufferFrames, config_format_converter_frames);
387   EXPECT_LE(kBufferFrames, dev_stream->conv_buffer_size_frames);
388   dev_stream_destroy(dev_stream);
389 }
390 
TEST_F(CreateSuite,CreateSRC8to48)391 TEST_F(CreateSuite, CreateSRC8to48) {
392   struct dev_stream *dev_stream;
393 
394   rstream_.format = fmt_s16le_8;
395   in_fmt.frame_rate = 8000;  // Stream rate.
396   out_fmt.frame_rate = 48000;  // Device rate.
397   config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
398   dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_48, (void *)0x55,
399                                  &cb_ts);
400   EXPECT_EQ(1, config_format_converter_called);
401   EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
402   // Converter tmp and output buffers are large enough for device output.
403   unsigned int device_frames =
404       cras_frames_at_rate(in_fmt.frame_rate, kBufferFrames,
405                           out_fmt.frame_rate);
406   EXPECT_LE(kBufferFrames, device_frames); // Sanity check.
407   EXPECT_LE(device_frames, config_format_converter_frames);
408   EXPECT_LE(device_frames, dev_stream->conv_buffer_size_frames);
409   dev_stream_destroy(dev_stream);
410 }
411 
TEST_F(CreateSuite,CreateSRC8from48Input)412 TEST_F(CreateSuite, CreateSRC8from48Input) {
413   struct dev_stream *dev_stream;
414 
415   rstream_.format = fmt_s16le_8;
416   rstream_.direction = CRAS_STREAM_INPUT;
417   in_fmt.frame_rate = 48000;  // Device rate.
418   out_fmt.frame_rate = 8000;  // Stream rate.
419   config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
420   dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_48, (void *)0x55,
421                                  &cb_ts);
422   EXPECT_EQ(1, config_format_converter_called);
423   EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
424   // Converter tmp and output buffers are large enough for device input.
425   unsigned int device_frames =
426       cras_frames_at_rate(out_fmt.frame_rate, kBufferFrames,
427                           in_fmt.frame_rate);
428   EXPECT_LE(kBufferFrames, device_frames); // Sanity check.
429   EXPECT_LE(device_frames, config_format_converter_frames);
430   EXPECT_LE(device_frames, dev_stream->conv_buffer_size_frames);
431   dev_stream_destroy(dev_stream);
432 }
433 
TEST_F(CreateSuite,CreateSRC48to8)434 TEST_F(CreateSuite, CreateSRC48to8) {
435   struct dev_stream *dev_stream;
436 
437   rstream_.format = fmt_s16le_48;
438   in_fmt.frame_rate = 48000;  // Stream rate.
439   out_fmt.frame_rate = 8000;  // Device rate.
440   config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
441   dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_8, (void *)0x55,
442                                  &cb_ts);
443   EXPECT_EQ(1, config_format_converter_called);
444   EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
445   // Converter tmp and output buffers are large enough for stream input.
446   EXPECT_LE(kBufferFrames, config_format_converter_frames);
447   EXPECT_LE(kBufferFrames, dev_stream->conv_buffer_size_frames);
448   dev_stream_destroy(dev_stream);
449 }
450 
TEST_F(CreateSuite,CreateSRC48from8Input)451 TEST_F(CreateSuite, CreateSRC48from8Input) {
452   struct dev_stream *dev_stream;
453 
454   rstream_.format = fmt_s16le_48;
455   rstream_.direction = CRAS_STREAM_INPUT;
456   in_fmt.frame_rate = 8000;  // Device rate.
457   out_fmt.frame_rate = 48000;  // Stream rate.
458   config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
459   dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_8, (void *)0x55,
460                                  &cb_ts);
461   EXPECT_EQ(1, config_format_converter_called);
462   EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
463   // Converter tmp and output buffers are large enough for stream output.
464   EXPECT_LE(kBufferFrames, config_format_converter_frames);
465   EXPECT_LE(kBufferFrames, dev_stream->conv_buffer_size_frames);
466   dev_stream_destroy(dev_stream);
467 }
468 
TEST_F(CreateSuite,CreateSRC48MonoFrom44StereoInput)469 TEST_F(CreateSuite, CreateSRC48MonoFrom44StereoInput) {
470   struct dev_stream *dev_stream;
471 
472   rstream_.format = fmt_s16le_48_mono;
473   rstream_.direction = CRAS_STREAM_INPUT;
474   in_fmt.frame_rate = 44100;  // Device rate.
475   out_fmt.frame_rate = 48000;  // Stream rate.
476   config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
477   dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_44_1, (void *)0x55,
478                                  &cb_ts);
479   EXPECT_EQ(1, config_format_converter_called);
480   EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
481   // Converter tmp and output buffers are large enough for stream output.
482   EXPECT_LE(kBufferFrames, config_format_converter_frames);
483   EXPECT_LE(kBufferFrames, dev_stream->conv_buffer_size_frames);
484   EXPECT_EQ(dev_stream->conv_buffer_size_frames * 4,
485             dev_stream->conv_buffer->max_size);
486   EXPECT_EQ(2, cras_audio_area_create_num_channels_val);
487   dev_stream_destroy(dev_stream);
488 }
489 
TEST_F(CreateSuite,CaptureAvailConvBufHasSamples)490 TEST_F(CreateSuite, CaptureAvailConvBufHasSamples) {
491   struct dev_stream *dev_stream;
492   unsigned int avail;
493 
494   rstream_.format = fmt_s16le_48;
495   rstream_.direction = CRAS_STREAM_INPUT;
496   config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
497   dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_44_1, (void *)0x55,
498                                  &cb_ts);
499   EXPECT_EQ(1, config_format_converter_called);
500   EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
501   EXPECT_LE(cras_frames_at_rate(in_fmt.frame_rate, kBufferFrames,
502                                 out_fmt.frame_rate),
503             dev_stream->conv_buffer_size_frames);
504   EXPECT_EQ(dev_stream->conv_buffer_size_frames * 4,
505             dev_stream->conv_buffer->max_size);
506   EXPECT_EQ(2, cras_audio_area_create_num_channels_val);
507 
508   buf_increment_write(dev_stream->conv_buffer, 50 * 4);
509   avail = dev_stream_capture_avail(dev_stream);
510 
511   EXPECT_EQ(cras_frames_at_rate(48000, 512 - 50, 44100), avail);
512 
513   dev_stream_destroy(dev_stream);
514 }
515 
TEST_F(CreateSuite,SetDevRateNotMasterDev)516 TEST_F(CreateSuite, SetDevRateNotMasterDev) {
517   struct dev_stream *dev_stream;
518   unsigned int dev_id = 9;
519 
520   rstream_.format = fmt_s16le_48;
521   rstream_.direction = CRAS_STREAM_INPUT;
522   rstream_.master_dev.dev_id = 4;
523   config_format_converter_conv =
524       reinterpret_cast<struct cras_fmt_conv*>(0x33);
525   dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
526                                  (void *)0x55, &cb_ts);
527 
528   dev_stream_set_dev_rate(dev_stream, 44100, 1.01, 1.0, 0);
529   EXPECT_EQ(1, cras_fmt_conv_set_linear_resample_rates_called);
530   EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
531   EXPECT_EQ(44541, cras_fmt_conv_set_linear_resample_rates_to);
532 
533   dev_stream_set_dev_rate(dev_stream, 44100, 1.01, 1.0, 1);
534   EXPECT_EQ(2, cras_fmt_conv_set_linear_resample_rates_called);
535   EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
536   EXPECT_LE(44541, cras_fmt_conv_set_linear_resample_rates_to);
537 
538   dev_stream_set_dev_rate(dev_stream, 44100, 1.0, 1.01, -1);
539   EXPECT_EQ(3, cras_fmt_conv_set_linear_resample_rates_called);
540   EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
541   EXPECT_GE(43663, cras_fmt_conv_set_linear_resample_rates_to);
542   dev_stream_destroy(dev_stream);
543 }
544 
TEST_F(CreateSuite,SetDevRateMasterDev)545 TEST_F(CreateSuite, SetDevRateMasterDev) {
546   struct dev_stream *dev_stream;
547   unsigned int dev_id = 9;
548   unsigned int expected_ts_nsec;
549 
550   rstream_.format = fmt_s16le_48;
551   rstream_.direction = CRAS_STREAM_INPUT;
552   rstream_.master_dev.dev_id = dev_id;
553   config_format_converter_conv =
554       reinterpret_cast<struct cras_fmt_conv*>(0x33);
555   dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
556                                  (void *)0x55, &cb_ts);
557 
558   dev_stream_set_dev_rate(dev_stream, 44100, 1.01, 1.0, 0);
559   EXPECT_EQ(1, cras_fmt_conv_set_linear_resample_rates_called);
560   EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
561   EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_to);
562   expected_ts_nsec = 1000000000.0 * kBufferFrames / 2.0 / 48000.0 / 1.01;
563   EXPECT_EQ(0, rstream_.sleep_interval_ts.tv_sec);
564   EXPECT_EQ(expected_ts_nsec, rstream_.sleep_interval_ts.tv_nsec);
565 
566   dev_stream_set_dev_rate(dev_stream, 44100, 1.01, 1.0, 1);
567   EXPECT_EQ(2, cras_fmt_conv_set_linear_resample_rates_called);
568   EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
569   EXPECT_LE(44100, cras_fmt_conv_set_linear_resample_rates_to);
570   expected_ts_nsec = 1000000000.0 * kBufferFrames / 2.0 / 48000.0 / 1.01;
571   EXPECT_EQ(0, rstream_.sleep_interval_ts.tv_sec);
572   EXPECT_EQ(expected_ts_nsec, rstream_.sleep_interval_ts.tv_nsec);
573 
574   dev_stream_set_dev_rate(dev_stream, 44100, 1.0, 1.33, -1);
575   EXPECT_EQ(3, cras_fmt_conv_set_linear_resample_rates_called);
576   EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
577   EXPECT_GE(44100, cras_fmt_conv_set_linear_resample_rates_to);
578   expected_ts_nsec = 1000000000.0 * kBufferFrames / 2.0 / 48000.0;
579   EXPECT_EQ(0, rstream_.sleep_interval_ts.tv_sec);
580   EXPECT_EQ(expected_ts_nsec, rstream_.sleep_interval_ts.tv_nsec);
581   dev_stream_destroy(dev_stream);
582 }
583 
TEST_F(CreateSuite,StreamMixNoFrames)584 TEST_F(CreateSuite, StreamMixNoFrames) {
585   struct dev_stream dev_stream;
586   struct cras_audio_format fmt;
587 
588   dev_stream.conv = NULL;
589   rstream_playable_frames_ret = 0;
590   fmt.num_channels = 2;
591   fmt.format = SND_PCM_FORMAT_S16_LE;
592   EXPECT_EQ(0, dev_stream_mix(&dev_stream, &fmt, 0, 3));
593 }
594 
TEST_F(CreateSuite,StreamMixNoConv)595 TEST_F(CreateSuite, StreamMixNoConv) {
596   struct dev_stream dev_stream;
597   const unsigned int nfr = 100;
598   struct cras_audio_format fmt;
599 
600   dev_stream.conv = NULL;
601   dev_stream.stream = reinterpret_cast<cras_rstream*>(0x5446);
602   rstream_playable_frames_ret = nfr;
603   rstream_get_readable_num = nfr;
604   rstream_get_readable_ptr = reinterpret_cast<uint8_t*>(0x4000);
605   rstream_get_readable_call.num_called = 0;
606   fmt.num_channels = 2;
607   fmt.format = SND_PCM_FORMAT_S16_LE;
608   EXPECT_EQ(nfr, dev_stream_mix(&dev_stream, &fmt, (uint8_t*)0x5000, nfr));
609   EXPECT_EQ((int16_t*)0x5000, mix_add_call.dst);
610   EXPECT_EQ((int16_t*)0x4000, mix_add_call.src);
611   EXPECT_EQ(200, mix_add_call.count);
612   EXPECT_EQ(1, mix_add_call.index);
613   EXPECT_EQ(dev_stream.stream, rstream_get_readable_call.rstream);
614   EXPECT_EQ(0, rstream_get_readable_call.offset);
615   EXPECT_EQ(1, rstream_get_readable_call.num_called);
616 }
617 
TEST_F(CreateSuite,StreamMixNoConvTwoPass)618 TEST_F(CreateSuite, StreamMixNoConvTwoPass) {
619   struct dev_stream dev_stream;
620   const unsigned int nfr = 100;
621   const unsigned int bytes_per_sample = 2;
622   const unsigned int num_channels = 2;
623   const unsigned int bytes_per_frame = bytes_per_sample * num_channels;
624   struct cras_audio_format fmt;
625 
626   dev_stream.conv = NULL;
627   dev_stream.stream = reinterpret_cast<cras_rstream*>(0x5446);
628   rstream_playable_frames_ret = nfr;
629   rstream_get_readable_num = nfr / 2;
630   rstream_get_readable_ptr = reinterpret_cast<uint8_t*>(0x4000);
631   rstream_get_readable_call.num_called = 0;
632   fmt.num_channels = 2;
633   fmt.format = SND_PCM_FORMAT_S16_LE;
634   EXPECT_EQ(nfr, dev_stream_mix(&dev_stream, &fmt, (uint8_t*)0x5000, nfr));
635   const unsigned int half_offset = nfr / 2 * bytes_per_frame;
636   EXPECT_EQ((int16_t*)(0x5000 + half_offset), mix_add_call.dst);
637   EXPECT_EQ((int16_t*)0x4000, mix_add_call.src);
638   EXPECT_EQ(nfr / 2 * num_channels, mix_add_call.count);
639   EXPECT_EQ(1, mix_add_call.index);
640   EXPECT_EQ(dev_stream.stream, rstream_get_readable_call.rstream);
641   EXPECT_EQ(nfr/2, rstream_get_readable_call.offset);
642   EXPECT_EQ(2, rstream_get_readable_call.num_called);
643 }
644 
TEST_F(CreateSuite,DevStreamFlushAudioMessages)645 TEST_F(CreateSuite, DevStreamFlushAudioMessages) {
646   struct dev_stream *dev_stream;
647   unsigned int dev_id = 9;
648 
649   dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
650                                  (void *)0x55, &cb_ts);
651 
652   dev_stream_flush_old_audio_messages(dev_stream);
653   EXPECT_EQ(1, cras_rstream_flush_old_audio_messages_called);
654   dev_stream_destroy(dev_stream);
655 }
656 
TEST_F(CreateSuite,DevStreamIsPending)657 TEST_F(CreateSuite, DevStreamIsPending) {
658   struct dev_stream *dev_stream;
659   unsigned int dev_id = 9;
660 
661   dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
662                                  (void *)0x55, &cb_ts);
663 
664   // dev_stream_is_pending_reply is only a wrapper.
665   cras_rstream_is_pending_reply_ret = 0;
666   EXPECT_EQ(0, dev_stream_is_pending_reply(dev_stream));
667 
668   cras_rstream_is_pending_reply_ret = 1;
669   EXPECT_EQ(1, dev_stream_is_pending_reply(dev_stream));
670 
671   dev_stream_destroy(dev_stream);
672 }
673 
TEST_F(CreateSuite,StreamCanFetch)674 TEST_F(CreateSuite, StreamCanFetch) {
675   struct dev_stream *dev_stream;
676   unsigned int dev_id = 9;
677 
678   dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
679                                  (void *)0x55, &cb_ts);
680 
681   /* Verify stream cannot fetch when it's still pending. */
682   cras_rstream_is_pending_reply_ret = 1;
683   EXPECT_EQ(0, dev_stream_can_fetch(dev_stream));
684 
685   /* Verify stream can fetch when buffer available. */
686   cras_rstream_is_pending_reply_ret = 0;
687   rstream_.shm.area->write_offset[0] = 0;
688   rstream_.shm.area->write_offset[1] = 0;
689   EXPECT_EQ(1, dev_stream_can_fetch(dev_stream));
690 
691   /* Verify stream cannot fetch when there's still buffer. */
692   cras_rstream_is_pending_reply_ret = 0;
693   rstream_.shm.area->write_offset[0] = kBufferFrames;
694   rstream_.shm.area->write_offset[1] = kBufferFrames;
695   EXPECT_EQ(0, dev_stream_can_fetch(dev_stream));
696   dev_stream_destroy(dev_stream);
697 }
698 
TEST_F(CreateSuite,StreamCanSend)699 TEST_F(CreateSuite, StreamCanSend) {
700   struct dev_stream *dev_stream;
701   unsigned int dev_id = 9;
702   int written_frames;
703   int rc;
704   struct timespec expected_next_cb_ts;
705 
706   rstream_.direction = CRAS_STREAM_INPUT;
707   dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
708                                  (void *)0x55, &cb_ts);
709 
710   // Assume there is a next_cb_ts on rstream.
711   rstream_.next_cb_ts.tv_sec = 1;
712   rstream_.next_cb_ts.tv_nsec = 0;
713 
714   // Case 1: Not enough samples. Time is not late enough.
715   //         Stream can not send data to client.
716   clock_gettime_retspec.tv_sec = 0;
717   clock_gettime_retspec.tv_nsec = 0;
718   rc = dev_stream_capture_update_rstream(dev_stream);
719   EXPECT_EQ(0, cras_rstream_audio_ready_called);
720   EXPECT_EQ(0, rc);
721 
722   // Case 2: Not enough samples. Time is late enough.
723   //         Stream can not send data to client.
724 
725   // Assume time is greater than next_cb_ts.
726   clock_gettime_retspec.tv_sec = 1;
727   clock_gettime_retspec.tv_nsec = 500;
728   // However, written frames is less than cb_threshold.
729   // Stream still can not send samples to client.
730   rc = dev_stream_capture_update_rstream(dev_stream);
731   EXPECT_EQ(0, cras_rstream_audio_ready_called);
732   EXPECT_EQ(0, rc);
733 
734   // Case 3: Enough samples. Time is not late enough.
735   //         Stream can not send data to client.
736 
737   // Assume time is less than next_cb_ts.
738   clock_gettime_retspec.tv_sec = 0;
739   clock_gettime_retspec.tv_nsec = 0;
740   // Enough samples are written.
741   written_frames = rstream_.cb_threshold + 10;
742   cras_shm_buffer_written(&rstream_.shm, written_frames);
743   // Stream still can not send samples to client.
744   rc = dev_stream_capture_update_rstream(dev_stream);
745   EXPECT_EQ(0, cras_rstream_audio_ready_called);
746   EXPECT_EQ(0, rc);
747 
748   // Case 4: Enough samples. Time is late enough.
749   //         Stream should send one cb_threshold to client.
750   clock_gettime_retspec.tv_sec = 1;
751   clock_gettime_retspec.tv_nsec = 500;
752   rc = dev_stream_capture_update_rstream(dev_stream);
753   EXPECT_EQ(1, cras_rstream_audio_ready_called);
754   EXPECT_EQ(rstream_.cb_threshold, cras_rstream_audio_ready_count);
755   EXPECT_EQ(0, rc);
756 
757   // Check next_cb_ts is increased by one sleep interval.
758   expected_next_cb_ts.tv_sec = 1;
759   expected_next_cb_ts.tv_nsec = 0;
760   add_timespecs(&expected_next_cb_ts,
761                 &rstream_.sleep_interval_ts);
762   EXPECT_EQ(expected_next_cb_ts.tv_sec, rstream_.next_cb_ts.tv_sec);
763   EXPECT_EQ(expected_next_cb_ts.tv_nsec, rstream_.next_cb_ts.tv_nsec);
764 
765   // Reset stub data of interest.
766   cras_rstream_audio_ready_called = 0;
767   cras_rstream_audio_ready_count = 0;
768   rstream_.next_cb_ts.tv_sec = 1;
769   rstream_.next_cb_ts.tv_nsec = 0;
770 
771   // Case 5: Enough samples. Time is late enough and it is too late
772   //         such that a new next_cb_ts is in the past.
773   //         Stream should send one cb_threshold to client and reset schedule.
774   clock_gettime_retspec.tv_sec = 2;
775   clock_gettime_retspec.tv_nsec = 0;
776   rc = dev_stream_capture_update_rstream(dev_stream);
777   EXPECT_EQ(1, cras_rstream_audio_ready_called);
778   EXPECT_EQ(rstream_.cb_threshold, cras_rstream_audio_ready_count);
779   EXPECT_EQ(0, rc);
780 
781   // Check next_cb_ts is rest to be now plus one sleep interval.
782   expected_next_cb_ts.tv_sec = 2;
783   expected_next_cb_ts.tv_nsec = 0;
784   add_timespecs(&expected_next_cb_ts,
785                 &rstream_.sleep_interval_ts);
786   EXPECT_EQ(expected_next_cb_ts.tv_sec, rstream_.next_cb_ts.tv_sec);
787   EXPECT_EQ(expected_next_cb_ts.tv_nsec, rstream_.next_cb_ts.tv_nsec);
788 
789   dev_stream_destroy(dev_stream);
790 }
791 
TEST_F(CreateSuite,StreamCanSendBulkAudio)792 TEST_F(CreateSuite, StreamCanSendBulkAudio) {
793   struct dev_stream *dev_stream;
794   unsigned int dev_id = 9;
795   int written_frames;
796   int rc;
797   struct timespec expected_next_cb_ts;
798 
799   rstream_.direction = CRAS_STREAM_INPUT;
800   rstream_.flags |= BULK_AUDIO_OK;
801   dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
802                                  (void *)0x55, &cb_ts);
803 
804   // Assume there is a next_cb_ts on rstream.
805   rstream_.next_cb_ts.tv_sec = 1;
806   rstream_.next_cb_ts.tv_nsec = 0;
807 
808   // Case 1: Not enough samples. Time is not late enough.
809   //         Bulk audio stream can not send data to client.
810   clock_gettime_retspec.tv_sec = 0;
811   clock_gettime_retspec.tv_nsec = 0;
812   rc = dev_stream_capture_update_rstream(dev_stream);
813   EXPECT_EQ(0, cras_rstream_audio_ready_called);
814   EXPECT_EQ(0, rc);
815 
816   // Case 2: Not enough samples. Time is late enough.
817   //         Bulk audio stream can not send data to client.
818 
819   // Assume time is greater than next_cb_ts.
820   clock_gettime_retspec.tv_sec = 1;
821   clock_gettime_retspec.tv_nsec = 500;
822   // However, written frames is less than cb_threshold.
823   // Stream still can not send samples to client.
824   rc = dev_stream_capture_update_rstream(dev_stream);
825   EXPECT_EQ(0, cras_rstream_audio_ready_called);
826   EXPECT_EQ(0, rc);
827 
828   // Case 3: Enough samples. Time is not late enough.
829   //         Bulk audio stream CAN send data to client.
830 
831   // Assume time is less than next_cb_ts.
832   clock_gettime_retspec.tv_sec = 0;
833   clock_gettime_retspec.tv_nsec = 0;
834   // Enough samples are written.
835   written_frames = rstream_.cb_threshold + 10;
836   cras_shm_buffer_written(&rstream_.shm, written_frames);
837   // Bulk audio stream can send all written samples to client.
838   rc = dev_stream_capture_update_rstream(dev_stream);
839   EXPECT_EQ(1, cras_rstream_audio_ready_called);
840   EXPECT_EQ(written_frames, cras_rstream_audio_ready_count);
841   EXPECT_EQ(0, rc);
842 
843   // Case 4: Enough samples. Time is late enough.
844   //         Bulk audio stream can send all written samples to client.
845 
846   // Reset stub data of interest.
847   cras_rstream_audio_ready_called = 0;
848   cras_rstream_audio_ready_count = 0;
849   rstream_.next_cb_ts.tv_sec = 1;
850   rstream_.next_cb_ts.tv_nsec = 0;
851 
852   clock_gettime_retspec.tv_sec = 1;
853   clock_gettime_retspec.tv_nsec = 500;
854   rc = dev_stream_capture_update_rstream(dev_stream);
855   EXPECT_EQ(1, cras_rstream_audio_ready_called);
856   EXPECT_EQ(written_frames, cras_rstream_audio_ready_count);
857   EXPECT_EQ(0, rc);
858 
859   // Check next_cb_ts is increased by one sleep interval.
860   expected_next_cb_ts.tv_sec = 1;
861   expected_next_cb_ts.tv_nsec = 0;
862   add_timespecs(&expected_next_cb_ts,
863                 &rstream_.sleep_interval_ts);
864   EXPECT_EQ(expected_next_cb_ts.tv_sec, rstream_.next_cb_ts.tv_sec);
865   EXPECT_EQ(expected_next_cb_ts.tv_nsec, rstream_.next_cb_ts.tv_nsec);
866 
867   dev_stream_destroy(dev_stream);
868 }
869 
TEST_F(CreateSuite,TriggerOnlyStreamSendOnlyOnce)870 TEST_F(CreateSuite, TriggerOnlyStreamSendOnlyOnce) {
871   struct dev_stream *dev_stream;
872   unsigned int dev_id = 9;
873 
874   rstream_.direction = CRAS_STREAM_INPUT;
875   dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
876                                  (void *) 0x55, &cb_ts);
877   dev_stream->stream->flags = TRIGGER_ONLY;
878   dev_stream->stream->triggered = 0;
879 
880   // Check first trigger callback called.
881   cras_shm_buffer_written(&rstream_.shm, rstream_.cb_threshold);
882   clock_gettime_retspec.tv_sec = 1;
883   clock_gettime_retspec.tv_nsec = 0;
884   dev_stream_capture_update_rstream(dev_stream);
885   EXPECT_EQ(1, cras_rstream_audio_ready_called);
886   EXPECT_EQ(1, dev_stream->stream->triggered);
887 
888   // No future callback will be called for TRIGGER_ONLY streams.
889   cras_shm_buffer_written(&rstream_.shm, rstream_.cb_threshold);
890   clock_gettime_retspec.tv_sec = 2;
891   clock_gettime_retspec.tv_nsec = 0;
892   dev_stream_capture_update_rstream(dev_stream);
893   EXPECT_EQ(1, cras_rstream_audio_ready_called);
894   dev_stream_destroy(dev_stream);
895 }
896 
TEST_F(CreateSuite,InputDevStreamWakeTimeByNextCbTs)897 TEST_F(CreateSuite, InputDevStreamWakeTimeByNextCbTs) {
898   struct dev_stream *dev_stream;
899   unsigned int dev_id = 9;
900   int rc;
901   unsigned int curr_level = 0;
902   int written_frames;
903   struct timespec level_tstamp = {.tv_sec = 1, .tv_nsec = 0};
904   struct timespec wake_time_out = {.tv_sec = 0, .tv_nsec = 0};
905 
906   rstream_.direction = CRAS_STREAM_INPUT;
907   dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
908                                  (void *)0x55, &cb_ts);
909 
910   // Assume there is a next_cb_ts on rstream.
911   rstream_.next_cb_ts.tv_sec = 1;
912   rstream_.next_cb_ts.tv_nsec = 500000;
913 
914   // Assume there are enough samples for stream.
915   written_frames = rstream_.cb_threshold + 10;
916   cras_shm_buffer_written(&rstream_.shm, written_frames);
917 
918   rc = dev_stream_wake_time(dev_stream, curr_level,
919                             &level_tstamp, rstream_.cb_threshold, 0,
920                             &wake_time_out);
921 
922   // The next wake up time is determined by next_cb_ts on dev_stream.
923   EXPECT_EQ(rstream_.next_cb_ts.tv_sec, wake_time_out.tv_sec);
924   EXPECT_EQ(rstream_.next_cb_ts.tv_nsec, wake_time_out.tv_nsec);
925   EXPECT_EQ(0, rc);
926   dev_stream_destroy(dev_stream);
927 }
928 
TEST_F(CreateSuite,InputDevStreamWakeTimeByDevice)929 TEST_F(CreateSuite, InputDevStreamWakeTimeByDevice) {
930   struct dev_stream *dev_stream;
931   unsigned int dev_id = 9;
932   int rc;
933   unsigned int curr_level = 100;
934   int written_frames;
935   struct timespec level_tstamp = {.tv_sec = 1, .tv_nsec = 0};
936   struct timespec wake_time_out = {.tv_sec = 0, .tv_nsec = 0};
937   struct timespec expected_tstamp = {.tv_sec = 0, .tv_nsec = 0};
938   struct timespec needed_time_for_device = {.tv_sec = 0, .tv_nsec = 0};
939   int needed_frames_from_device = 0;
940 
941   rstream_.direction = CRAS_STREAM_INPUT;
942   dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_48,
943                                  (void *)0x55, &cb_ts);
944 
945   // Assume there is a next_cb_ts on rstream, that is, 1.005 seconds.
946   rstream_.next_cb_ts.tv_sec = 1;
947   rstream_.next_cb_ts.tv_nsec = 5000000; // 5ms
948 
949   // Assume there are not enough samples for stream.
950   written_frames = 123;
951   cras_shm_buffer_written(&rstream_.shm, written_frames);
952 
953   // Compute wake up time for device level to reach enough samples
954   // for one cb_threshold:
955   // Device has 100 samples (48K rate).
956   // Stream has 123 samples (44.1K rate)
957   // cb_threshold = 512 samples.
958   // Stream needs 512 - 123 = 389 samples.
959   // Converted to device rate => 389 * 48000.0 / 44100 = 423.4 samples
960   // => 424 samples.
961   // Device needs another 424 - 100 = 324 samples.
962   // Time for 252 samples = 324 / 48000 = 0.00675 sec.
963   // So expected wake up time for samples is at level_tstamp + 0.00675 sec =
964   // 1.00675 seconds.
965   needed_frames_from_device = cras_frames_at_rate(
966        44100, rstream_.cb_threshold - written_frames, 48000);
967   needed_frames_from_device -= curr_level;
968   cras_frames_to_time(needed_frames_from_device, 48000,
969                       &needed_time_for_device);
970 
971   expected_tstamp.tv_sec = level_tstamp.tv_sec;
972   expected_tstamp.tv_nsec = level_tstamp.tv_nsec;
973 
974   add_timespecs(&expected_tstamp, &needed_time_for_device);
975 
976   // Set the stub data for cras_fmt_conv_out_frames_to_in.
977   out_fmt.frame_rate = 44100;
978   in_fmt.frame_rate = 48000;
979 
980   rc = dev_stream_wake_time(dev_stream, curr_level,
981                             &level_tstamp, rstream_.cb_threshold, 0,
982                             &wake_time_out);
983 
984   // The next wake up time is determined by needed time for device level
985   // to reach enough samples for one cb_threshold.
986   EXPECT_EQ(expected_tstamp.tv_sec, wake_time_out.tv_sec);
987   EXPECT_EQ(expected_tstamp.tv_nsec, wake_time_out.tv_nsec);
988   EXPECT_EQ(0, rc);
989 
990   // Assume current level is larger than cb_threshold.
991   // The wake up time is determined by next_cb_ts.
992   curr_level += rstream_.cb_threshold;
993   rc = dev_stream_wake_time(dev_stream, curr_level,
994                             &level_tstamp, rstream_.cb_threshold, 0,
995                             &wake_time_out);
996   EXPECT_EQ(rstream_.next_cb_ts.tv_sec, wake_time_out.tv_sec);
997   EXPECT_EQ(rstream_.next_cb_ts.tv_nsec, wake_time_out.tv_nsec);
998   EXPECT_EQ(0, rc);
999   dev_stream_destroy(dev_stream);
1000 }
1001 
1002 //  Test set_playback_timestamp.
TEST(DevStreamTimimg,SetPlaybackTimeStampSimple)1003 TEST(DevStreamTimimg, SetPlaybackTimeStampSimple) {
1004   struct cras_timespec ts;
1005 
1006   clock_gettime_retspec.tv_sec = 1;
1007   clock_gettime_retspec.tv_nsec = 0;
1008   cras_set_playback_timestamp(48000, 24000, &ts);
1009   EXPECT_EQ(1, ts.tv_sec);
1010   EXPECT_GE(ts.tv_nsec, 499900000);
1011   EXPECT_LE(ts.tv_nsec, 500100000);
1012 }
1013 
TEST(DevStreamTimimg,SetPlaybackTimeStampWrap)1014 TEST(DevStreamTimimg, SetPlaybackTimeStampWrap) {
1015   struct cras_timespec ts;
1016 
1017   clock_gettime_retspec.tv_sec = 1;
1018   clock_gettime_retspec.tv_nsec = 750000000;
1019   cras_set_playback_timestamp(48000, 24000, &ts);
1020   EXPECT_EQ(2, ts.tv_sec);
1021   EXPECT_GE(ts.tv_nsec, 249900000);
1022   EXPECT_LE(ts.tv_nsec, 250100000);
1023 }
1024 
TEST(DevStreamTimimg,SetPlaybackTimeStampWrapTwice)1025 TEST(DevStreamTimimg, SetPlaybackTimeStampWrapTwice) {
1026   struct cras_timespec ts;
1027 
1028   clock_gettime_retspec.tv_sec = 1;
1029   clock_gettime_retspec.tv_nsec = 750000000;
1030   cras_set_playback_timestamp(48000, 72000, &ts);
1031   EXPECT_EQ(3, ts.tv_sec);
1032   EXPECT_GE(ts.tv_nsec, 249900000);
1033   EXPECT_LE(ts.tv_nsec, 250100000);
1034 }
1035 
1036 //  Test set_capture_timestamp.
TEST(DevStreamTimimg,SetCaptureTimeStampSimple)1037 TEST(DevStreamTimimg, SetCaptureTimeStampSimple) {
1038   struct cras_timespec ts;
1039 
1040   clock_gettime_retspec.tv_sec = 1;
1041   clock_gettime_retspec.tv_nsec = 750000000;
1042   cras_set_capture_timestamp(48000, 24000, &ts);
1043   EXPECT_EQ(1, ts.tv_sec);
1044   EXPECT_GE(ts.tv_nsec, 249900000);
1045   EXPECT_LE(ts.tv_nsec, 250100000);
1046 }
1047 
TEST(DevStreamTimimg,SetCaptureTimeStampWrap)1048 TEST(DevStreamTimimg, SetCaptureTimeStampWrap) {
1049   struct cras_timespec ts;
1050 
1051   clock_gettime_retspec.tv_sec = 1;
1052   clock_gettime_retspec.tv_nsec = 0;
1053   cras_set_capture_timestamp(48000, 24000, &ts);
1054   EXPECT_EQ(0, ts.tv_sec);
1055   EXPECT_GE(ts.tv_nsec, 499900000);
1056   EXPECT_LE(ts.tv_nsec, 500100000);
1057 }
1058 
TEST(DevStreamTimimg,SetCaptureTimeStampWrapPartial)1059 TEST(DevStreamTimimg, SetCaptureTimeStampWrapPartial) {
1060   struct cras_timespec ts;
1061 
1062   clock_gettime_retspec.tv_sec = 2;
1063   clock_gettime_retspec.tv_nsec = 750000000;
1064   cras_set_capture_timestamp(48000, 72000, &ts);
1065   EXPECT_EQ(1, ts.tv_sec);
1066   EXPECT_GE(ts.tv_nsec, 249900000);
1067   EXPECT_LE(ts.tv_nsec, 250100000);
1068 }
1069 
1070 TEST(MaxFramesForConverter, 8to48) {
1071   EXPECT_EQ(481, max_frames_for_conversion(80,  // Stream frames.
1072                                            8000,  // Stream rate.
1073                                            48000));  // Device rate.
1074 }
1075 
1076 TEST(MaxFramesForConverter, 48to8) {
1077   EXPECT_EQ(81, max_frames_for_conversion(80,  // Stream frames.
1078                                           48000,  // Stream rate.
1079                                           8000));  // Device rate.
1080 }
1081 
1082 /* Stubs */
1083 extern "C" {
1084 
cras_rstream_audio_ready(struct cras_rstream * stream,size_t count)1085 int cras_rstream_audio_ready(struct cras_rstream *stream, size_t count) {
1086   cras_rstream_audio_ready_count = count;
1087   cras_rstream_audio_ready_called++;
1088   return 0;
1089 }
1090 
cras_rstream_request_audio(struct cras_rstream * stream,const struct timespec * now)1091 int cras_rstream_request_audio(struct cras_rstream *stream,
1092     const struct timespec *now) {
1093   return 0;
1094 }
1095 
cras_rstream_record_fetch_interval(struct cras_rstream * rstream,const struct timespec * now)1096 void cras_rstream_record_fetch_interval(struct cras_rstream *rstream,
1097                                         const struct timespec *now) {
1098 }
1099 
cras_rstream_update_input_write_pointer(struct cras_rstream * rstream)1100 void cras_rstream_update_input_write_pointer(struct cras_rstream *rstream) {
1101 }
1102 
cras_rstream_update_output_read_pointer(struct cras_rstream * rstream)1103 void cras_rstream_update_output_read_pointer(struct cras_rstream *rstream) {
1104 }
1105 
cras_rstream_dev_offset_update(struct cras_rstream * rstream,unsigned int frames,unsigned int dev_id)1106 void cras_rstream_dev_offset_update(struct cras_rstream *rstream,
1107 					unsigned int frames,
1108 					unsigned int dev_id) {
1109 }
1110 
cras_rstream_dev_attach(struct cras_rstream * rstream,unsigned int dev_id,void * dev_ptr)1111 void cras_rstream_dev_attach(struct cras_rstream *rstream, unsigned int dev_id,
1112                              void *dev_ptr)
1113 {
1114 }
1115 
cras_rstream_dev_detach(struct cras_rstream * rstream,unsigned int dev_id)1116 void cras_rstream_dev_detach(struct cras_rstream *rstream, unsigned int dev_id)
1117 {
1118 }
1119 
cras_rstream_dev_offset(const struct cras_rstream * rstream,unsigned int dev_id)1120 unsigned int cras_rstream_dev_offset(const struct cras_rstream *rstream,
1121                                      unsigned int dev_id) {
1122   return 0;
1123 }
1124 
cras_rstream_playable_frames(struct cras_rstream * rstream,unsigned int dev_id)1125 unsigned int cras_rstream_playable_frames(struct cras_rstream *rstream,
1126 					  unsigned int dev_id) {
1127   return rstream_playable_frames_ret;
1128 }
1129 
cras_rstream_get_volume_scaler(struct cras_rstream * rstream)1130 float cras_rstream_get_volume_scaler(struct cras_rstream *rstream) {
1131   return 1.0;
1132 }
1133 
cras_rstream_get_readable_frames(struct cras_rstream * rstream,unsigned int offset,size_t * frames)1134 uint8_t *cras_rstream_get_readable_frames(struct cras_rstream *rstream,
1135                                           unsigned int offset,
1136 					  size_t *frames) {
1137   rstream_get_readable_call.rstream = rstream;
1138   rstream_get_readable_call.offset = offset;
1139   rstream_get_readable_call.num_called++;
1140   *frames = rstream_get_readable_num;
1141   return rstream_get_readable_ptr;
1142 }
1143 
cras_rstream_get_mute(const struct cras_rstream * rstream)1144 int cras_rstream_get_mute(const struct cras_rstream *rstream) {
1145   return 0;
1146 }
cras_rstream_update_queued_frames(struct cras_rstream * rstream)1147 void cras_rstream_update_queued_frames(struct cras_rstream *rstream)
1148 {
1149 }
1150 
cras_rstream_post_processing_format(const struct cras_rstream * stream,void * dev_ptr)1151 struct cras_audio_format *cras_rstream_post_processing_format(
1152     const struct cras_rstream *stream, void *dev_ptr)
1153 {
1154   return cras_rstream_post_processing_format_val;
1155 }
1156 
config_format_converter(struct cras_fmt_conv ** conv,enum CRAS_STREAM_DIRECTION dir,const struct cras_audio_format * from,const struct cras_audio_format * to,unsigned int frames)1157 int config_format_converter(struct cras_fmt_conv **conv,
1158 			    enum CRAS_STREAM_DIRECTION dir,
1159 			    const struct cras_audio_format *from,
1160 			    const struct cras_audio_format *to,
1161 			    unsigned int frames) {
1162   config_format_converter_called++;
1163   config_format_converter_from_fmt = from;
1164   config_format_converter_frames = frames;
1165   *conv = config_format_converter_conv;
1166   return 0;
1167 }
1168 
cras_fmt_conv_destroy(struct cras_fmt_conv * conv)1169 void cras_fmt_conv_destroy(struct cras_fmt_conv *conv) {
1170 }
1171 
cras_fmt_conv_convert_frames(struct cras_fmt_conv * conv,uint8_t * in_buf,uint8_t * out_buf,unsigned int * in_frames,unsigned int out_frames)1172 size_t cras_fmt_conv_convert_frames(struct cras_fmt_conv *conv,
1173 				    uint8_t *in_buf,
1174 				    uint8_t *out_buf,
1175 				    unsigned int *in_frames,
1176 				    unsigned int out_frames) {
1177   unsigned int ret;
1178   conv_frames_call.conv = conv;
1179   conv_frames_call.in_buf = in_buf;
1180   conv_frames_call.out_buf = out_buf;
1181   conv_frames_call.in_frames = *in_frames;
1182   ret = cras_frames_at_rate(in_fmt.frame_rate,
1183                             *in_frames,
1184                             out_fmt.frame_rate);
1185   conv_frames_call.out_frames = out_frames;
1186   if (ret > out_frames) {
1187     ret = out_frames;
1188     *in_frames = cras_frames_at_rate(
1189       out_fmt.frame_rate,
1190       ret, in_fmt.frame_rate);
1191   }
1192 
1193   return ret;
1194 }
1195 
cras_mix_add(snd_pcm_format_t fmt,uint8_t * dst,uint8_t * src,unsigned int count,unsigned int index,int mute,float mix_vol)1196 void cras_mix_add(snd_pcm_format_t fmt, uint8_t *dst, uint8_t *src,
1197                   unsigned int count, unsigned int index,
1198                   int mute, float mix_vol) {
1199   mix_add_call.dst = (int16_t *)dst;
1200   mix_add_call.src = (int16_t *)src;
1201   mix_add_call.count = count;
1202   mix_add_call.index = index;
1203   mix_add_call.mute = mute;
1204   mix_add_call.mix_vol = mix_vol;
1205 }
1206 
cras_audio_area_create(int num_channels)1207 struct cras_audio_area *cras_audio_area_create(int num_channels) {
1208   cras_audio_area_create_num_channels_val = num_channels;
1209   return NULL;
1210 }
1211 
cras_audio_area_destroy(struct cras_audio_area * area)1212 void cras_audio_area_destroy(struct cras_audio_area *area) {
1213 }
1214 
cras_audio_area_config_buf_pointers(struct cras_audio_area * area,const struct cras_audio_format * fmt,uint8_t * base_buffer)1215 void cras_audio_area_config_buf_pointers(struct cras_audio_area *area,
1216                                          const struct cras_audio_format *fmt,
1217                                          uint8_t *base_buffer) {
1218 }
1219 
cras_audio_area_config_channels(struct cras_audio_area * area,const struct cras_audio_format * fmt)1220 void cras_audio_area_config_channels(struct cras_audio_area *area,
1221 				     const struct cras_audio_format *fmt) {
1222 }
1223 
cras_audio_area_copy(const struct cras_audio_area * dst,unsigned int dst_offset,const struct cras_audio_format * dst_fmt,const struct cras_audio_area * src,unsigned int src_offset,float software_gain_scaler)1224 unsigned int cras_audio_area_copy(const struct cras_audio_area *dst,
1225                                   unsigned int dst_offset,
1226                                   const struct cras_audio_format *dst_fmt,
1227                                   const struct cras_audio_area *src,
1228                                   unsigned int src_offset,
1229                                   float software_gain_scaler) {
1230   copy_area_call.dst = dst;
1231   copy_area_call.dst_offset = dst_offset;
1232   copy_area_call.dst_format_bytes = cras_get_format_bytes(dst_fmt);
1233   copy_area_call.src = src;
1234   copy_area_call.src_offset = src_offset;
1235   copy_area_call.software_gain_scaler = software_gain_scaler;
1236   return src->frames;
1237 }
1238 
cras_fmt_conv_in_frames_to_out(struct cras_fmt_conv * conv,size_t in_frames)1239 size_t cras_fmt_conv_in_frames_to_out(struct cras_fmt_conv *conv,
1240 				      size_t in_frames)
1241 {
1242   return cras_frames_at_rate(in_fmt.frame_rate,
1243                              in_frames,
1244                              out_fmt.frame_rate);
1245 }
1246 
cras_fmt_conv_out_frames_to_in(struct cras_fmt_conv * conv,size_t out_frames)1247 size_t cras_fmt_conv_out_frames_to_in(struct cras_fmt_conv *conv,
1248                                       size_t out_frames) {
1249   return cras_frames_at_rate(out_fmt.frame_rate,
1250                              out_frames,
1251                              in_fmt.frame_rate);
1252 }
1253 
cras_fmt_conv_in_format(const struct cras_fmt_conv * conv)1254 const struct cras_audio_format *cras_fmt_conv_in_format(
1255     const struct cras_fmt_conv *conv) {
1256   return &in_fmt;
1257 }
1258 
cras_fmt_conv_out_format(const struct cras_fmt_conv * conv)1259 const struct cras_audio_format *cras_fmt_conv_out_format(
1260     const struct cras_fmt_conv *conv) {
1261   return &out_fmt;
1262 }
1263 
cras_fmt_conversion_needed(const struct cras_fmt_conv * conv)1264 int cras_fmt_conversion_needed(const struct cras_fmt_conv *conv)
1265 {
1266   return cras_fmt_conversion_needed_val;
1267 }
1268 
cras_fmt_conv_set_linear_resample_rates(struct cras_fmt_conv * conv,float from,float to)1269 void cras_fmt_conv_set_linear_resample_rates(struct cras_fmt_conv *conv,
1270                                              float from,
1271                                              float to)
1272 {
1273   cras_fmt_conv_set_linear_resample_rates_from = from;
1274   cras_fmt_conv_set_linear_resample_rates_to = to;
1275   cras_fmt_conv_set_linear_resample_rates_called++;
1276 }
1277 
cras_rstream_is_pending_reply(const struct cras_rstream * stream)1278 int cras_rstream_is_pending_reply(const struct cras_rstream *stream)
1279 {
1280   return cras_rstream_is_pending_reply_ret;
1281 }
1282 
cras_rstream_flush_old_audio_messages(struct cras_rstream * stream)1283 int cras_rstream_flush_old_audio_messages(struct  cras_rstream *stream)
1284 {
1285   cras_rstream_flush_old_audio_messages_called++;
1286   return 0;
1287 }
1288 
1289 //  From librt.
clock_gettime(clockid_t clk_id,struct timespec * tp)1290 int clock_gettime(clockid_t clk_id, struct timespec *tp) {
1291   tp->tv_sec = clock_gettime_retspec.tv_sec;
1292   tp->tv_nsec = clock_gettime_retspec.tv_nsec;
1293   return 0;
1294 }
1295 
1296 }  // extern "C"
1297 
1298 }  //  namespace
1299 
main(int argc,char ** argv)1300 int main(int argc, char **argv) {
1301   ::testing::InitGoogleTest(&argc, argv);
1302   return RUN_ALL_TESTS();
1303 }
1304