1 // Copyright 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 extern "C" {
6 #include "audio_thread.c"
7 #include "cras_audio_area.h"
8 }
9
10 #include <gtest/gtest.h>
11 #include <map>
12
13 #define MAX_CALLS 10
14 #define BUFFER_SIZE 8192
15 #define FIRST_CB_LEVEL 480
16
17 static int cras_audio_thread_busyloop_called;
18 static unsigned int cras_rstream_dev_offset_called;
19 static unsigned int cras_rstream_dev_offset_ret[MAX_CALLS];
20 static const struct cras_rstream *cras_rstream_dev_offset_rstream_val[MAX_CALLS];
21 static unsigned int cras_rstream_dev_offset_dev_id_val[MAX_CALLS];
22 static unsigned int cras_rstream_dev_offset_update_called;
23 static const struct cras_rstream *cras_rstream_dev_offset_update_rstream_val[MAX_CALLS];
24 static unsigned int cras_rstream_dev_offset_update_frames_val[MAX_CALLS];
25 static unsigned int cras_rstream_dev_offset_update_dev_id_val[MAX_CALLS];
26 static int cras_iodev_all_streams_written_ret;
27 static struct cras_audio_area *cras_iodev_get_output_buffer_area;
28 static int cras_iodev_put_output_buffer_called;
29 static unsigned int cras_iodev_put_output_buffer_nframes;
30 static unsigned int cras_iodev_fill_odev_zeros_frames;
31 static int dev_stream_playback_frames_ret;
32 static unsigned int cras_iodev_prepare_output_before_write_samples_called;
33 static enum CRAS_IODEV_STATE cras_iodev_prepare_output_before_write_samples_state;
34 static unsigned int cras_iodev_get_output_buffer_called;
35 static unsigned int cras_iodev_frames_to_play_in_sleep_called;
36 static int cras_iodev_prepare_output_before_write_samples_ret;
37 static int cras_iodev_reset_request_called;
38 static struct cras_iodev *cras_iodev_reset_request_iodev;
39 static int cras_iodev_output_underrun_called;
40 static int cras_device_monitor_reset_device_called;
41 static struct cras_iodev *cras_device_monitor_reset_device_iodev;
42 static struct cras_iodev *cras_iodev_start_ramp_odev;
43 static enum CRAS_IODEV_RAMP_REQUEST cras_iodev_start_ramp_request;
44 static std::map<const struct dev_stream*, struct timespec> dev_stream_wake_time_val;
45
ResetGlobalStubData()46 void ResetGlobalStubData() {
47 cras_rstream_dev_offset_called = 0;
48 cras_rstream_dev_offset_update_called = 0;
49 for (int i = 0; i < MAX_CALLS; i++) {
50 cras_rstream_dev_offset_ret[i] = 0;
51 cras_rstream_dev_offset_rstream_val[i] = NULL;
52 cras_rstream_dev_offset_dev_id_val[i] = 0;
53 cras_rstream_dev_offset_update_rstream_val[i] = NULL;
54 cras_rstream_dev_offset_update_frames_val[i] = 0;
55 cras_rstream_dev_offset_update_dev_id_val[i] = 0;
56 }
57 cras_iodev_all_streams_written_ret = 0;
58 if (cras_iodev_get_output_buffer_area) {
59 free(cras_iodev_get_output_buffer_area->channels[0].buf);
60 free(cras_iodev_get_output_buffer_area);
61 cras_iodev_get_output_buffer_area = NULL;
62 }
63 cras_iodev_put_output_buffer_called = 0;
64 cras_iodev_put_output_buffer_nframes = 0;
65 cras_iodev_fill_odev_zeros_frames = 0;
66 cras_iodev_frames_to_play_in_sleep_called = 0;
67 dev_stream_playback_frames_ret = 0;
68 cras_iodev_prepare_output_before_write_samples_called = 0;
69 cras_iodev_prepare_output_before_write_samples_state = CRAS_IODEV_STATE_OPEN;
70 cras_iodev_get_output_buffer_called = 0;
71 cras_iodev_prepare_output_before_write_samples_ret = 0;
72 cras_iodev_reset_request_called = 0;
73 cras_iodev_reset_request_iodev = NULL;
74 cras_iodev_output_underrun_called = 0;
75 cras_device_monitor_reset_device_called = 0;
76 cras_device_monitor_reset_device_iodev = NULL;
77 cras_iodev_start_ramp_odev = NULL;
78 cras_iodev_start_ramp_request = CRAS_IODEV_RAMP_REQUEST_UP_START_PLAYBACK;
79 dev_stream_wake_time_val.clear();
80 }
81
82 // Test streams and devices manipulation.
83 class StreamDeviceSuite : public testing::Test {
84 protected:
SetUp()85 virtual void SetUp() {
86 thread_ = audio_thread_create();
87 ResetStubData();
88 }
89
TearDown()90 virtual void TearDown() {
91 audio_thread_destroy(thread_);
92 ResetGlobalStubData();
93 }
94
SetupDevice(cras_iodev * iodev,enum CRAS_STREAM_DIRECTION direction)95 virtual void SetupDevice(cras_iodev *iodev,
96 enum CRAS_STREAM_DIRECTION direction) {
97 memset(iodev, 0, sizeof(*iodev));
98 iodev->info.idx = ++device_id_;
99 iodev->direction = direction;
100 iodev->configure_dev = configure_dev;
101 iodev->close_dev = close_dev;
102 iodev->frames_queued = frames_queued;
103 iodev->delay_frames = delay_frames;
104 iodev->get_buffer = get_buffer;
105 iodev->put_buffer = put_buffer;
106 iodev->flush_buffer = flush_buffer;
107 iodev->ext_format = &format_;
108 iodev->buffer_size = BUFFER_SIZE;
109 iodev->min_cb_level = FIRST_CB_LEVEL;
110 }
111
ResetStubData()112 void ResetStubData() {
113 device_id_ = 0;
114 open_dev_called_ = 0;
115 close_dev_called_ = 0;
116 frames_queued_ = 0;
117 delay_frames_ = 0;
118 audio_buffer_size_ = 0;
119 }
120
SetupRstream(struct cras_rstream * rstream,enum CRAS_STREAM_DIRECTION direction)121 void SetupRstream(struct cras_rstream *rstream,
122 enum CRAS_STREAM_DIRECTION direction) {
123 memset(rstream, 0, sizeof(*rstream));
124 rstream->direction = direction;
125 rstream->cb_threshold = 480;
126 rstream->shm.area = static_cast<cras_audio_shm_area*>(
127 calloc(1, sizeof(*rstream->shm.area)));
128 }
129
TearDownRstream(struct cras_rstream * rstream)130 void TearDownRstream(struct cras_rstream *rstream) {
131 free(rstream->shm.area);
132 }
133
SetupPinnedStream(struct cras_rstream * rstream,enum CRAS_STREAM_DIRECTION direction,cras_iodev * pin_to_dev)134 void SetupPinnedStream(struct cras_rstream *rstream,
135 enum CRAS_STREAM_DIRECTION direction,
136 cras_iodev* pin_to_dev) {
137 SetupRstream(rstream, direction);
138 rstream->is_pinned = 1;
139 rstream->pinned_dev_idx = pin_to_dev->info.idx;
140 }
141
configure_dev(cras_iodev * iodev)142 static int configure_dev(cras_iodev* iodev) {
143 open_dev_called_++;
144 return 0;
145 }
146
close_dev(cras_iodev * iodev)147 static int close_dev(cras_iodev* iodev) {
148 close_dev_called_++;
149 return 0;
150 }
151
frames_queued(const cras_iodev * iodev,struct timespec * tstamp)152 static int frames_queued(const cras_iodev* iodev, struct timespec* tstamp) {
153 clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
154 return frames_queued_;
155 }
156
delay_frames(const cras_iodev * iodev)157 static int delay_frames(const cras_iodev* iodev) {
158 return delay_frames_;
159 }
160
get_buffer(cras_iodev * iodev,struct cras_audio_area ** area,unsigned int * num)161 static int get_buffer(cras_iodev* iodev,
162 struct cras_audio_area** area,
163 unsigned int* num) {
164 size_t sz = sizeof(*area_) + sizeof(struct cras_channel_area) * 2;
165
166 if (audio_buffer_size_ < *num)
167 *num = audio_buffer_size_;
168
169 area_ = (cras_audio_area*)calloc(1, sz);
170 area_->frames = *num;
171 area_->num_channels = 2;
172 area_->channels[0].buf = audio_buffer_;
173 channel_area_set_channel(&area_->channels[0], CRAS_CH_FL);
174 area_->channels[0].step_bytes = 4;
175 area_->channels[1].buf = audio_buffer_ + 2;
176 channel_area_set_channel(&area_->channels[1], CRAS_CH_FR);
177 area_->channels[1].step_bytes = 4;
178
179 *area = area_;
180 return 0;
181 }
182
put_buffer(cras_iodev * iodev,unsigned int num)183 static int put_buffer(cras_iodev* iodev, unsigned int num) {
184 free(area_);
185 return 0;
186 }
187
flush_buffer(cras_iodev * iodev)188 static int flush_buffer(cras_iodev *iodev) {
189 return 0;
190 }
191
192 int device_id_;
193 struct audio_thread *thread_;
194
195 static int open_dev_called_;
196 static int close_dev_called_;
197 static int frames_queued_;
198 static int delay_frames_;
199 static struct cras_audio_format format_;
200 static struct cras_audio_area *area_;
201 static uint8_t audio_buffer_[BUFFER_SIZE];
202 static unsigned int audio_buffer_size_;
203 };
204
205 int StreamDeviceSuite::open_dev_called_;
206 int StreamDeviceSuite::close_dev_called_;
207 int StreamDeviceSuite::frames_queued_;
208 int StreamDeviceSuite::delay_frames_;
209 struct cras_audio_format StreamDeviceSuite::format_;
210 struct cras_audio_area *StreamDeviceSuite::area_;
211 uint8_t StreamDeviceSuite::audio_buffer_[8192];
212 unsigned int StreamDeviceSuite::audio_buffer_size_;
213
TEST_F(StreamDeviceSuite,AddRemoveOpenOutputDevice)214 TEST_F(StreamDeviceSuite, AddRemoveOpenOutputDevice) {
215 struct cras_iodev iodev;
216 struct open_dev *adev;
217
218 SetupDevice(&iodev, CRAS_STREAM_OUTPUT);
219
220 // Check the newly added device is open.
221 thread_add_open_dev(thread_, &iodev);
222 adev = thread_->open_devs[CRAS_STREAM_OUTPUT];
223 EXPECT_EQ(adev->dev, &iodev);
224
225 thread_rm_open_dev(thread_, &iodev);
226 adev = thread_->open_devs[CRAS_STREAM_OUTPUT];
227 EXPECT_EQ(NULL, adev);
228 }
229
TEST_F(StreamDeviceSuite,StartRamp)230 TEST_F(StreamDeviceSuite, StartRamp) {
231 struct cras_iodev iodev;
232 struct open_dev *adev;
233 int rc;
234 enum CRAS_IODEV_RAMP_REQUEST req;
235
236 SetupDevice(&iodev, CRAS_STREAM_OUTPUT);
237
238 // Check the newly added device is open.
239 thread_add_open_dev(thread_, &iodev);
240 adev = thread_->open_devs[CRAS_STREAM_OUTPUT];
241 EXPECT_EQ(adev->dev, &iodev);
242
243 // Ramp up for unmute.
244 req = CRAS_IODEV_RAMP_REQUEST_UP_UNMUTE;
245 rc = thread_dev_start_ramp(thread_, &iodev, req);
246
247 EXPECT_EQ(0, rc);
248 EXPECT_EQ(&iodev, cras_iodev_start_ramp_odev);
249 EXPECT_EQ(req, cras_iodev_start_ramp_request);
250
251 // Ramp down for mute.
252 ResetStubData();
253 req = CRAS_IODEV_RAMP_REQUEST_DOWN_MUTE;
254
255 rc = thread_dev_start_ramp(thread_, &iodev, req);
256
257 EXPECT_EQ(0, rc);
258 EXPECT_EQ(&iodev, cras_iodev_start_ramp_odev);
259 EXPECT_EQ(req, cras_iodev_start_ramp_request);
260 thread_rm_open_dev(thread_, &iodev);
261 }
262
TEST_F(StreamDeviceSuite,AddRemoveOpenInputDevice)263 TEST_F(StreamDeviceSuite, AddRemoveOpenInputDevice) {
264 struct cras_iodev iodev;
265 struct open_dev *adev;
266
267 SetupDevice(&iodev, CRAS_STREAM_INPUT);
268
269 // Check the newly added device is open.
270 thread_add_open_dev(thread_, &iodev);
271 adev = thread_->open_devs[CRAS_STREAM_INPUT];
272 EXPECT_EQ(adev->dev, &iodev);
273
274 thread_rm_open_dev(thread_, &iodev);
275 adev = thread_->open_devs[CRAS_STREAM_INPUT];
276 EXPECT_EQ(NULL, adev);
277 }
278
TEST_F(StreamDeviceSuite,AddRemoveMultipleOpenDevices)279 TEST_F(StreamDeviceSuite, AddRemoveMultipleOpenDevices) {
280 struct cras_iodev odev;
281 struct cras_iodev odev2;
282 struct cras_iodev odev3;
283 struct cras_iodev idev;
284 struct cras_iodev idev2;
285 struct cras_iodev idev3;
286 struct open_dev *adev;
287
288 SetupDevice(&odev, CRAS_STREAM_OUTPUT);
289 SetupDevice(&odev2, CRAS_STREAM_OUTPUT);
290 SetupDevice(&odev3, CRAS_STREAM_OUTPUT);
291 SetupDevice(&idev, CRAS_STREAM_INPUT);
292 SetupDevice(&idev2, CRAS_STREAM_INPUT);
293 SetupDevice(&idev3, CRAS_STREAM_INPUT);
294
295 // Add 2 open devices and check both are open.
296 thread_add_open_dev(thread_, &odev);
297 thread_add_open_dev(thread_, &odev2);
298 adev = thread_->open_devs[CRAS_STREAM_OUTPUT];
299 EXPECT_EQ(adev->dev, &odev);
300 EXPECT_EQ(adev->next->dev, &odev2);
301
302 // Remove first open device and check the second one is still open.
303 thread_rm_open_dev(thread_, &odev);
304 adev = thread_->open_devs[CRAS_STREAM_OUTPUT];
305 EXPECT_EQ(adev->dev, &odev2);
306
307 // Add another open device and check both are open.
308 thread_add_open_dev(thread_, &odev3);
309 adev = thread_->open_devs[CRAS_STREAM_OUTPUT];
310 EXPECT_EQ(adev->dev, &odev2);
311 EXPECT_EQ(adev->next->dev, &odev3);
312
313 // Add 2 open devices and check both are open.
314 thread_add_open_dev(thread_, &idev);
315 thread_add_open_dev(thread_, &idev2);
316 adev = thread_->open_devs[CRAS_STREAM_INPUT];
317 EXPECT_EQ(adev->dev, &idev);
318 EXPECT_EQ(adev->next->dev, &idev2);
319
320 // Remove first open device and check the second one is still open.
321 thread_rm_open_dev(thread_, &idev);
322 adev = thread_->open_devs[CRAS_STREAM_INPUT];
323 EXPECT_EQ(adev->dev, &idev2);
324
325 // Add and remove another open device and check still open.
326 thread_add_open_dev(thread_, &idev3);
327 thread_rm_open_dev(thread_, &idev3);
328 adev = thread_->open_devs[CRAS_STREAM_INPUT];
329 EXPECT_EQ(adev->dev, &idev2);
330 thread_rm_open_dev(thread_, &idev2);
331 thread_rm_open_dev(thread_, &odev2);
332 thread_rm_open_dev(thread_, &odev3);
333 }
334
TEST_F(StreamDeviceSuite,MultipleInputStreamsCopyFirstStreamOffset)335 TEST_F(StreamDeviceSuite, MultipleInputStreamsCopyFirstStreamOffset) {
336 struct cras_iodev iodev;
337 struct cras_iodev iodev2;
338 struct cras_iodev *iodevs[] = {&iodev, &iodev2};
339 struct cras_rstream rstream;
340 struct cras_rstream rstream2;
341 struct cras_rstream rstream3;
342
343 SetupDevice(&iodev, CRAS_STREAM_INPUT);
344 SetupDevice(&iodev2, CRAS_STREAM_INPUT);
345 SetupRstream(&rstream, CRAS_STREAM_INPUT);
346 SetupRstream(&rstream2, CRAS_STREAM_INPUT);
347 SetupRstream(&rstream3, CRAS_STREAM_INPUT);
348
349 thread_add_open_dev(thread_, &iodev);
350 thread_add_open_dev(thread_, &iodev2);
351
352 thread_add_stream(thread_, &rstream, iodevs, 2);
353 EXPECT_NE((void *)NULL, iodev.streams);
354 EXPECT_NE((void *)NULL, iodev2.streams);
355
356 EXPECT_EQ(0, cras_rstream_dev_offset_called);
357 EXPECT_EQ(0, cras_rstream_dev_offset_update_called);
358
359 // Fake offset for rstream
360 cras_rstream_dev_offset_ret[0] = 30;
361 cras_rstream_dev_offset_ret[1] = 0;
362
363 thread_add_stream(thread_, &rstream2, iodevs, 2);
364 EXPECT_EQ(2, cras_rstream_dev_offset_called);
365 EXPECT_EQ(&rstream, cras_rstream_dev_offset_rstream_val[0]);
366 EXPECT_EQ(iodev.info.idx, cras_rstream_dev_offset_dev_id_val[0]);
367 EXPECT_EQ(&rstream, cras_rstream_dev_offset_rstream_val[1]);
368 EXPECT_EQ(iodev2.info.idx, cras_rstream_dev_offset_dev_id_val[1]);
369
370 EXPECT_EQ(2, cras_rstream_dev_offset_update_called);
371 EXPECT_EQ(&rstream2, cras_rstream_dev_offset_update_rstream_val[0]);
372 EXPECT_EQ(30, cras_rstream_dev_offset_update_frames_val[0]);
373 EXPECT_EQ(&rstream2, cras_rstream_dev_offset_update_rstream_val[1]);
374 EXPECT_EQ(0, cras_rstream_dev_offset_update_frames_val[1]);
375
376 thread_rm_open_dev(thread_, &iodev);
377 thread_rm_open_dev(thread_, &iodev2);
378 TearDownRstream(&rstream);
379 TearDownRstream(&rstream2);
380 TearDownRstream(&rstream3);
381 }
382
TEST_F(StreamDeviceSuite,InputStreamsSetInputDeviceWakeTime)383 TEST_F(StreamDeviceSuite, InputStreamsSetInputDeviceWakeTime) {
384 struct cras_iodev iodev;
385 struct cras_iodev *iodevs[] = {&iodev};
386 struct cras_rstream rstream1, rstream2;
387 struct timespec ts_wake_1 = {.tv_sec = 1, .tv_nsec = 500};
388 struct timespec ts_wake_2 = {.tv_sec = 1, .tv_nsec = 1000};
389 struct open_dev *adev;
390
391 SetupDevice(&iodev, CRAS_STREAM_INPUT);
392 SetupRstream(&rstream1, CRAS_STREAM_INPUT);
393 SetupRstream(&rstream2, CRAS_STREAM_INPUT);
394
395 thread_add_open_dev(thread_, &iodev);
396 thread_add_stream(thread_, &rstream1, iodevs, 1);
397 thread_add_stream(thread_, &rstream2, iodevs, 1);
398 EXPECT_NE((void *)NULL, iodev.streams);
399
400 // Assume device is running.
401 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
402
403 // Set stub data for dev_stream_wake_time.
404 dev_stream_wake_time_val[iodev.streams] = ts_wake_1;
405 dev_stream_wake_time_val[iodev.streams->next] = ts_wake_2;
406
407 // Send captured samples to client.
408 // This will also update wake time for this device based on
409 // dev_stream_wake_time of each stream of this device.
410 dev_io_send_captured_samples(thread_->open_devs[CRAS_STREAM_INPUT]);
411
412 // wake_ts is maintained in open_dev.
413 adev = thread_->open_devs[CRAS_STREAM_INPUT];
414
415 // The wake up time for this device is the minimum of
416 // ts_wake_1 and ts_wake_2.
417 EXPECT_EQ(ts_wake_1.tv_sec, adev->wake_ts.tv_sec);
418 EXPECT_EQ(ts_wake_1.tv_nsec, adev->wake_ts.tv_nsec);
419
420 thread_rm_open_dev(thread_, &iodev);
421 TearDownRstream(&rstream1);
422 TearDownRstream(&rstream2);
423 }
424
TEST_F(StreamDeviceSuite,AddRemoveMultipleStreamsOnMultipleDevices)425 TEST_F(StreamDeviceSuite, AddRemoveMultipleStreamsOnMultipleDevices) {
426 struct cras_iodev iodev, *piodev = &iodev;
427 struct cras_iodev iodev2, *piodev2 = &iodev2;
428 struct cras_rstream rstream;
429 struct cras_rstream rstream2;
430 struct cras_rstream rstream3;
431 struct dev_stream *dev_stream;
432
433 SetupDevice(&iodev, CRAS_STREAM_OUTPUT);
434 SetupDevice(&iodev2, CRAS_STREAM_OUTPUT);
435 SetupRstream(&rstream, CRAS_STREAM_OUTPUT);
436 SetupRstream(&rstream2, CRAS_STREAM_OUTPUT);
437 SetupRstream(&rstream3, CRAS_STREAM_OUTPUT);
438
439 // Add first device as open and check 2 streams can be added.
440 thread_add_open_dev(thread_, &iodev);
441 thread_add_stream(thread_, &rstream, &piodev, 1);
442 dev_stream = iodev.streams;
443 EXPECT_EQ(dev_stream->stream, &rstream);
444 thread_add_stream(thread_, &rstream2, &piodev, 1);
445 EXPECT_EQ(dev_stream->next->stream, &rstream2);
446
447 // Add second device as open and check no streams are copied over.
448 thread_add_open_dev(thread_, &iodev2);
449 dev_stream = iodev2.streams;
450 EXPECT_EQ(NULL, dev_stream);
451 // Also check the 2 streams on first device remain intact.
452 dev_stream = iodev.streams;
453 EXPECT_EQ(dev_stream->stream, &rstream);
454 EXPECT_EQ(dev_stream->next->stream, &rstream2);
455
456 // Add a stream to the second dev and check it isn't also added to the first.
457 thread_add_stream(thread_, &rstream3, &piodev2, 1);
458 dev_stream = iodev.streams;
459 EXPECT_EQ(dev_stream->stream, &rstream);
460 EXPECT_EQ(dev_stream->next->stream, &rstream2);
461 EXPECT_EQ(NULL, dev_stream->next->next);
462 dev_stream = iodev2.streams;
463 EXPECT_EQ(&rstream3, dev_stream->stream);
464 EXPECT_EQ(NULL, dev_stream->next);
465
466 // Remove first device from open and streams on second device remain
467 // intact.
468 thread_rm_open_dev(thread_, &iodev);
469 dev_stream = iodev2.streams;
470 EXPECT_EQ(&rstream3, dev_stream->stream);
471 EXPECT_EQ(NULL, dev_stream->next);
472
473 // Remove 2 streams, check the streams are removed from both open devices.
474 dev_io_remove_stream(&thread_->open_devs[rstream.direction],
475 &rstream, &iodev);
476 dev_io_remove_stream(&thread_->open_devs[rstream3.direction],
477 &rstream3, &iodev2);
478 dev_stream = iodev2.streams;
479 EXPECT_EQ(NULL, dev_stream);
480
481 // Remove open devices and check stream is on fallback device.
482 thread_rm_open_dev(thread_, &iodev2);
483
484 // Add open device, again check it is empty of streams.
485 thread_add_open_dev(thread_, &iodev);
486 dev_stream = iodev.streams;
487 EXPECT_EQ(NULL, dev_stream);
488
489 thread_rm_open_dev(thread_, &iodev);
490 TearDownRstream(&rstream);
491 TearDownRstream(&rstream2);
492 TearDownRstream(&rstream3);
493 }
494
TEST_F(StreamDeviceSuite,WriteOutputSamplesPrepareOutputFailed)495 TEST_F(StreamDeviceSuite, WriteOutputSamplesPrepareOutputFailed) {
496 struct cras_iodev iodev;
497 struct open_dev *adev;
498
499 ResetGlobalStubData();
500
501 SetupDevice(&iodev, CRAS_STREAM_OUTPUT);
502
503 // Add the device.
504 thread_add_open_dev(thread_, &iodev);
505 adev = thread_->open_devs[CRAS_STREAM_OUTPUT];
506
507 // Assume device is started.
508 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
509 // Assume device remains in no stream state;
510 cras_iodev_prepare_output_before_write_samples_state = \
511 CRAS_IODEV_STATE_NO_STREAM_RUN;
512
513 // Assume there is an error in prepare_output.
514 cras_iodev_prepare_output_before_write_samples_ret = -EINVAL;
515
516 // cras_iodev should handle no stream playback.
517 EXPECT_EQ(-EINVAL,
518 write_output_samples(&thread_->open_devs[CRAS_STREAM_OUTPUT],
519 adev, nullptr));
520
521 // cras_iodev_get_output_buffer in audio_thread write_output_samples is not
522 // called.
523 EXPECT_EQ(0, cras_iodev_get_output_buffer_called);
524
525 thread_rm_open_dev(thread_, &iodev);
526 }
527
TEST_F(StreamDeviceSuite,WriteOutputSamplesNoStream)528 TEST_F(StreamDeviceSuite, WriteOutputSamplesNoStream) {
529 struct cras_iodev iodev;
530 struct open_dev *adev;
531
532 ResetGlobalStubData();
533
534 SetupDevice(&iodev, CRAS_STREAM_OUTPUT);
535
536 // Add the device.
537 thread_add_open_dev(thread_, &iodev);
538 adev = thread_->open_devs[CRAS_STREAM_OUTPUT];
539
540 // Assume device is started.
541 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
542 // Assume device remains in no stream state;
543 cras_iodev_prepare_output_before_write_samples_state = \
544 CRAS_IODEV_STATE_NO_STREAM_RUN;
545
546 // cras_iodev should handle no stream playback.
547 write_output_samples(&thread_->open_devs[CRAS_STREAM_OUTPUT], adev, nullptr);
548 EXPECT_EQ(1, cras_iodev_prepare_output_before_write_samples_called);
549 // cras_iodev_get_output_buffer in audio_thread write_output_samples is not
550 // called.
551 EXPECT_EQ(0, cras_iodev_get_output_buffer_called);
552
553 thread_rm_open_dev(thread_, &iodev);
554 }
555
TEST_F(StreamDeviceSuite,WriteOutputSamplesLeaveNoStream)556 TEST_F(StreamDeviceSuite, WriteOutputSamplesLeaveNoStream) {
557 struct cras_iodev iodev;
558 struct open_dev *adev;
559
560 ResetGlobalStubData();
561
562 SetupDevice(&iodev, CRAS_STREAM_OUTPUT);
563
564 // Setup the output buffer for device.
565 cras_iodev_get_output_buffer_area = cras_audio_area_create(2);
566
567 // Add the device.
568 thread_add_open_dev(thread_, &iodev);
569 adev = thread_->open_devs[CRAS_STREAM_OUTPUT];
570
571 // Assume device in no stream state.
572 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
573
574 // Assume device remains in no stream state;
575 cras_iodev_prepare_output_before_write_samples_state = \
576 CRAS_IODEV_STATE_NO_STREAM_RUN;
577
578 // cras_iodev should NOT leave no stream state;
579 write_output_samples(&thread_->open_devs[CRAS_STREAM_OUTPUT], adev, nullptr);
580 EXPECT_EQ(1, cras_iodev_prepare_output_before_write_samples_called);
581 // cras_iodev_get_output_buffer in audio_thread write_output_samples is not
582 // called.
583 EXPECT_EQ(0, cras_iodev_get_output_buffer_called);
584
585 // Assume device leaves no stream state;
586 cras_iodev_prepare_output_before_write_samples_state = \
587 CRAS_IODEV_STATE_NORMAL_RUN;
588
589 // cras_iodev should write samples from streams.
590 write_output_samples(&thread_->open_devs[CRAS_STREAM_OUTPUT], adev, nullptr);
591 EXPECT_EQ(2, cras_iodev_prepare_output_before_write_samples_called);
592 EXPECT_EQ(1, cras_iodev_get_output_buffer_called);
593
594 thread_rm_open_dev(thread_, &iodev);
595 }
596
TEST_F(StreamDeviceSuite,DoPlaybackNoStream)597 TEST_F(StreamDeviceSuite, DoPlaybackNoStream) {
598 struct cras_iodev iodev;
599
600 ResetGlobalStubData();
601
602 SetupDevice(&iodev, CRAS_STREAM_OUTPUT);
603
604 // Add the device.
605 thread_add_open_dev(thread_, &iodev);
606
607 // Assume device is started.
608 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
609 // Assume device remains in no stream state;
610 cras_iodev_prepare_output_before_write_samples_state = \
611 CRAS_IODEV_STATE_NO_STREAM_RUN;
612 // Add 10 frames in queue to prevent underrun
613 frames_queued_ = 10;
614
615 // cras_iodev should handle no stream playback.
616 dev_io_playback_write(&thread_->open_devs[CRAS_STREAM_OUTPUT], nullptr);
617 EXPECT_EQ(1, cras_iodev_prepare_output_before_write_samples_called);
618 // cras_iodev_get_output_buffer in audio_thread write_output_samples is not
619 // called.
620 EXPECT_EQ(0, cras_iodev_get_output_buffer_called);
621
622 EXPECT_EQ(0, cras_iodev_output_underrun_called);
623 // cras_iodev_frames_to_play_in_sleep should be called from
624 // update_dev_wakeup_time.
625 EXPECT_EQ(1, cras_iodev_frames_to_play_in_sleep_called);
626
627 thread_rm_open_dev(thread_, &iodev);
628 }
629
TEST_F(StreamDeviceSuite,DoPlaybackUnderrun)630 TEST_F(StreamDeviceSuite, DoPlaybackUnderrun) {
631 struct cras_iodev iodev, *piodev = &iodev;
632 struct cras_rstream rstream;
633
634 ResetGlobalStubData();
635
636 SetupDevice(&iodev, CRAS_STREAM_OUTPUT);
637 SetupRstream(&rstream, CRAS_STREAM_OUTPUT);
638
639 // Setup the output buffer for device.
640 cras_iodev_get_output_buffer_area = cras_audio_area_create(2);
641
642 // Add the device and add the stream.
643 thread_add_open_dev(thread_, &iodev);
644 thread_add_stream(thread_, &rstream, &piodev, 1);
645
646 // Assume device is running and there is an underrun.
647 // It wrote 11 frames into device but new hw_level is only 10.
648 // It means underrun may happened because 10 - 11 < 0.
649 // Audio thread should ask iodev to handle output underrun.
650 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
651 frames_queued_ = 10;
652 cras_iodev_all_streams_written_ret = 11;
653
654 // Assume device in normal run stream state;
655 cras_iodev_prepare_output_before_write_samples_state = \
656 CRAS_IODEV_STATE_NORMAL_RUN;
657
658 EXPECT_EQ(0, cras_iodev_output_underrun_called);
659 dev_io_playback_write(&thread_->open_devs[CRAS_STREAM_OUTPUT], nullptr);
660 EXPECT_EQ(1, cras_iodev_output_underrun_called);
661
662 thread_rm_open_dev(thread_, &iodev);
663 TearDownRstream(&rstream);
664 }
665
TEST_F(StreamDeviceSuite,DoPlaybackSevereUnderrun)666 TEST_F(StreamDeviceSuite, DoPlaybackSevereUnderrun) {
667 struct cras_iodev iodev, *piodev = &iodev;
668 struct cras_rstream rstream;
669
670 ResetGlobalStubData();
671
672 SetupDevice(&iodev, CRAS_STREAM_OUTPUT);
673 SetupRstream(&rstream, CRAS_STREAM_OUTPUT);
674
675 // Setup the output buffer for device.
676 cras_iodev_get_output_buffer_area = cras_audio_area_create(2);
677
678 // Add the device and add the stream.
679 thread_add_open_dev(thread_, &iodev);
680 thread_add_stream(thread_, &rstream, &piodev, 1);
681
682 // Assume device is running and there is a severe underrun.
683 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
684 frames_queued_ = -EPIPE;
685
686 // Assume device in normal run stream state;
687 cras_iodev_prepare_output_before_write_samples_state = \
688 CRAS_IODEV_STATE_NORMAL_RUN;
689
690 dev_io_playback_write(&thread_->open_devs[CRAS_STREAM_OUTPUT], nullptr);
691
692 // Audio thread should ask main thread to reset device.
693 EXPECT_EQ(1, cras_iodev_reset_request_called);
694 EXPECT_EQ(&iodev, cras_iodev_reset_request_iodev);
695
696 thread_rm_open_dev(thread_, &iodev);
697 TearDownRstream(&rstream);
698 }
699
TEST(AUdioThreadStreams,DrainStream)700 TEST(AUdioThreadStreams, DrainStream) {
701 struct cras_rstream rstream;
702 struct cras_audio_shm_area shm_area;
703 struct audio_thread thread;
704
705 memset(&rstream, 0, sizeof(rstream));
706 memset(&shm_area, 0, sizeof(shm_area));
707 rstream.shm.config.frame_bytes = 4;
708 shm_area.config.frame_bytes = 4;
709 shm_area.config.used_size = 4096 * 4;
710 rstream.shm.config.used_size = 4096 * 4;
711 rstream.shm.area = &shm_area;
712 rstream.format.frame_rate = 48000;
713 rstream.direction = CRAS_STREAM_OUTPUT;
714
715 shm_area.write_offset[0] = 1 * 4;
716 EXPECT_EQ(1, thread_drain_stream_ms_remaining(&thread, &rstream));
717
718 shm_area.write_offset[0] = 479 * 4;
719 EXPECT_EQ(10, thread_drain_stream_ms_remaining(&thread, &rstream));
720
721 shm_area.write_offset[0] = 0;
722 EXPECT_EQ(0, thread_drain_stream_ms_remaining(&thread, &rstream));
723
724 rstream.direction = CRAS_STREAM_INPUT;
725 shm_area.write_offset[0] = 479 * 4;
726 EXPECT_EQ(0, thread_drain_stream_ms_remaining(&thread, &rstream));
727 }
728
TEST(BusyloopDetectSuite,CheckerTest)729 TEST(BusyloopDetectSuite, CheckerTest) {
730 continuous_zero_sleep_count = 0;
731 cras_audio_thread_busyloop_called = 0;
732 timespec wait_ts;
733 wait_ts.tv_sec = 0;
734 wait_ts.tv_nsec = 0;
735
736 check_busyloop(&wait_ts);
737 EXPECT_EQ(continuous_zero_sleep_count, 1);
738 EXPECT_EQ(cras_audio_thread_busyloop_called, 0);
739 check_busyloop(&wait_ts);
740 EXPECT_EQ(continuous_zero_sleep_count, 2);
741 EXPECT_EQ(cras_audio_thread_busyloop_called, 1);
742 check_busyloop(&wait_ts);
743 EXPECT_EQ(continuous_zero_sleep_count, 3);
744 EXPECT_EQ(cras_audio_thread_busyloop_called, 1);
745
746 wait_ts.tv_sec = 1;
747 check_busyloop(&wait_ts);
748 EXPECT_EQ(continuous_zero_sleep_count, 0);
749 EXPECT_EQ(cras_audio_thread_busyloop_called, 1);
750 }
751
752 extern "C" {
753
cras_iodev_add_stream(struct cras_iodev * iodev,struct dev_stream * stream)754 int cras_iodev_add_stream(struct cras_iodev *iodev, struct dev_stream *stream)
755 {
756 DL_APPEND(iodev->streams, stream);
757 return 0;
758 }
759
cras_iodev_all_streams_written(struct cras_iodev * iodev)760 unsigned int cras_iodev_all_streams_written(struct cras_iodev *iodev)
761 {
762 return cras_iodev_all_streams_written_ret;
763 }
764
cras_iodev_close(struct cras_iodev * iodev)765 int cras_iodev_close(struct cras_iodev *iodev)
766 {
767 return 0;
768 }
769
cras_iodev_free_format(struct cras_iodev * iodev)770 void cras_iodev_free_format(struct cras_iodev *iodev)
771 {
772 return;
773 }
774
cras_iodev_get_est_rate_ratio(const struct cras_iodev * iodev)775 double cras_iodev_get_est_rate_ratio(const struct cras_iodev *iodev)
776 {
777 return 1.0;
778 }
779
cras_iodev_max_stream_offset(const struct cras_iodev * iodev)780 unsigned int cras_iodev_max_stream_offset(const struct cras_iodev *iodev)
781 {
782 return 0;
783 }
784
cras_iodev_open(struct cras_iodev * iodev,unsigned int cb_level,const struct cras_audio_format * fmt)785 int cras_iodev_open(struct cras_iodev *iodev, unsigned int cb_level,
786 const struct cras_audio_format *fmt)
787 {
788 return 0;
789 }
790
cras_iodev_put_buffer(struct cras_iodev * iodev,unsigned int nframes)791 int cras_iodev_put_buffer(struct cras_iodev *iodev, unsigned int nframes)
792 {
793 return 0;
794 }
795
cras_iodev_rm_stream(struct cras_iodev * iodev,const struct cras_rstream * stream)796 struct dev_stream *cras_iodev_rm_stream(struct cras_iodev *iodev,
797 const struct cras_rstream *stream)
798 {
799 struct dev_stream *out;
800 DL_FOREACH(iodev->streams, out) {
801 if (out->stream == stream) {
802 DL_DELETE(iodev->streams, out);
803 return out;
804 }
805 }
806 return NULL;
807 }
808
cras_iodev_set_format(struct cras_iodev * iodev,const struct cras_audio_format * fmt)809 int cras_iodev_set_format(struct cras_iodev *iodev,
810 const struct cras_audio_format *fmt)
811 {
812 return 0;
813 }
814
cras_iodev_stream_offset(struct cras_iodev * iodev,struct dev_stream * stream)815 unsigned int cras_iodev_stream_offset(struct cras_iodev *iodev,
816 struct dev_stream *stream)
817 {
818 return 0;
819 }
820
dev_stream_attached_devs(const struct dev_stream * dev_stream)821 int dev_stream_attached_devs(const struct dev_stream *dev_stream)
822 {
823 return 1;
824 }
825
cras_iodev_stream_written(struct cras_iodev * iodev,struct dev_stream * stream,unsigned int nwritten)826 void cras_iodev_stream_written(struct cras_iodev *iodev,
827 struct dev_stream *stream,
828 unsigned int nwritten)
829 {
830 }
831
cras_iodev_update_rate(struct cras_iodev * iodev,unsigned int level,struct timespec * level_tstamp)832 int cras_iodev_update_rate(struct cras_iodev *iodev, unsigned int level,
833 struct timespec *level_tstamp)
834 {
835 return 0;
836 }
837
cras_iodev_put_input_buffer(struct cras_iodev * iodev)838 int cras_iodev_put_input_buffer(struct cras_iodev *iodev)
839 {
840 return 0;
841 }
842
cras_iodev_put_output_buffer(struct cras_iodev * iodev,uint8_t * frames,unsigned int nframes,int * non_empty,struct cras_fmt_conv * output_converter)843 int cras_iodev_put_output_buffer(struct cras_iodev *iodev, uint8_t *frames,
844 unsigned int nframes, int* non_empty,
845 struct cras_fmt_conv *output_converter) {
846 cras_iodev_put_output_buffer_called++;
847 cras_iodev_put_output_buffer_nframes = nframes;
848 return 0;
849 }
850
cras_iodev_get_input_buffer(struct cras_iodev * iodev,unsigned * frames)851 int cras_iodev_get_input_buffer(struct cras_iodev *iodev,
852 unsigned *frames)
853 {
854 return 0;
855 }
856
cras_iodev_get_output_buffer(struct cras_iodev * iodev,struct cras_audio_area ** area,unsigned * frames)857 int cras_iodev_get_output_buffer(struct cras_iodev *iodev,
858 struct cras_audio_area **area,
859 unsigned *frames)
860 {
861 cras_iodev_get_output_buffer_called++;
862 *area = cras_iodev_get_output_buffer_area;
863 return 0;
864 }
865
cras_iodev_get_dsp_delay(const struct cras_iodev * iodev)866 int cras_iodev_get_dsp_delay(const struct cras_iodev *iodev)
867 {
868 return 0;
869 }
870
cras_fmt_conv_destroy(struct cras_fmt_conv ** conv)871 void cras_fmt_conv_destroy(struct cras_fmt_conv **conv)
872 {
873 }
874
cras_channel_remix_conv_create(unsigned int num_channels,const float * coefficient)875 struct cras_fmt_conv *cras_channel_remix_conv_create(
876 unsigned int num_channels,
877 const float *coefficient)
878 {
879 return NULL;
880 }
881
cras_rstream_dev_attach(struct cras_rstream * rstream,unsigned int dev_id,void * dev_ptr)882 void cras_rstream_dev_attach(struct cras_rstream *rstream,
883 unsigned int dev_id,
884 void *dev_ptr)
885 {
886 }
887
cras_rstream_dev_detach(struct cras_rstream * rstream,unsigned int dev_id)888 void cras_rstream_dev_detach(struct cras_rstream *rstream, unsigned int dev_id)
889 {
890 }
891
cras_rstream_destroy(struct cras_rstream * stream)892 void cras_rstream_destroy(struct cras_rstream *stream)
893 {
894 }
895
cras_rstream_dev_offset_update(struct cras_rstream * rstream,unsigned int frames,unsigned int dev_id)896 void cras_rstream_dev_offset_update(struct cras_rstream *rstream,
897 unsigned int frames,
898 unsigned int dev_id)
899 {
900 int i = cras_rstream_dev_offset_update_called;
901 if (i < MAX_CALLS) {
902 cras_rstream_dev_offset_update_rstream_val[i] = rstream;
903 cras_rstream_dev_offset_update_frames_val[i] = frames;
904 cras_rstream_dev_offset_update_dev_id_val[i] = dev_id;
905 cras_rstream_dev_offset_update_called++;
906 }
907 }
908
cras_rstream_dev_offset(const struct cras_rstream * rstream,unsigned int dev_id)909 unsigned int cras_rstream_dev_offset(const struct cras_rstream *rstream,
910 unsigned int dev_id)
911 {
912 int i = cras_rstream_dev_offset_called;
913 if (i < MAX_CALLS) {
914 cras_rstream_dev_offset_rstream_val[i] = rstream;
915 cras_rstream_dev_offset_dev_id_val[i] = dev_id;
916 cras_rstream_dev_offset_called++;
917 return cras_rstream_dev_offset_ret[i];
918 }
919 return 0;
920 }
921
cras_rstream_record_fetch_interval(struct cras_rstream * rstream,const struct timespec * now)922 void cras_rstream_record_fetch_interval(struct cras_rstream *rstream,
923 const struct timespec *now)
924 {
925 }
926
cras_set_rt_scheduling(int rt_lim)927 int cras_set_rt_scheduling(int rt_lim)
928 {
929 return 0;
930 }
931
cras_set_thread_priority(int priority)932 int cras_set_thread_priority(int priority)
933 {
934 return 0;
935 }
936
cras_system_rm_select_fd(int fd)937 void cras_system_rm_select_fd(int fd)
938 {
939 }
940
dev_stream_capture(struct dev_stream * dev_stream,const struct cras_audio_area * area,unsigned int area_offset,float software_gain_scaler)941 unsigned int dev_stream_capture(struct dev_stream *dev_stream,
942 const struct cras_audio_area *area,
943 unsigned int area_offset,
944 float software_gain_scaler)
945 {
946 return 0;
947 }
948
dev_stream_capture_avail(const struct dev_stream * dev_stream)949 unsigned int dev_stream_capture_avail(const struct dev_stream *dev_stream)
950 {
951 return 0;
952
953 }
dev_stream_cb_threshold(const struct dev_stream * dev_stream)954 unsigned int dev_stream_cb_threshold(const struct dev_stream *dev_stream)
955 {
956 return 0;
957 }
958
dev_stream_capture_update_rstream(struct dev_stream * dev_stream)959 int dev_stream_capture_update_rstream(struct dev_stream *dev_stream)
960 {
961 return 0;
962 }
963
dev_stream_create(struct cras_rstream * stream,unsigned int dev_id,const struct cras_audio_format * dev_fmt,void * dev_ptr,struct timespec * cb_ts)964 struct dev_stream *dev_stream_create(struct cras_rstream *stream,
965 unsigned int dev_id,
966 const struct cras_audio_format *dev_fmt,
967 void *dev_ptr, struct timespec *cb_ts)
968 {
969 struct dev_stream *out = static_cast<dev_stream*>(calloc(1, sizeof(*out)));
970 out->stream = stream;
971 return out;
972 }
973
dev_stream_destroy(struct dev_stream * dev_stream)974 void dev_stream_destroy(struct dev_stream *dev_stream)
975 {
976 free(dev_stream);
977 }
978
dev_stream_mix(struct dev_stream * dev_stream,const struct cras_audio_format * fmt,uint8_t * dst,unsigned int num_to_write)979 int dev_stream_mix(struct dev_stream *dev_stream,
980 const struct cras_audio_format *fmt,
981 uint8_t *dst,
982 unsigned int num_to_write)
983 {
984 return num_to_write;
985 }
986
dev_stream_playback_frames(const struct dev_stream * dev_stream)987 int dev_stream_playback_frames(const struct dev_stream *dev_stream)
988 {
989 return dev_stream_playback_frames_ret;
990 }
991
dev_stream_playback_update_rstream(struct dev_stream * dev_stream)992 int dev_stream_playback_update_rstream(struct dev_stream *dev_stream)
993 {
994 return 0;
995 }
996
dev_stream_poll_stream_fd(const struct dev_stream * dev_stream)997 int dev_stream_poll_stream_fd(const struct dev_stream *dev_stream)
998 {
999 return dev_stream->stream->fd;
1000 }
1001
dev_stream_can_fetch(struct dev_stream * dev_stream)1002 int dev_stream_can_fetch(struct dev_stream *dev_stream)
1003 {
1004 return 1;
1005 }
1006
dev_stream_request_playback_samples(struct dev_stream * dev_stream,const struct timespec * now)1007 int dev_stream_request_playback_samples(struct dev_stream *dev_stream,
1008 const struct timespec *now)
1009 {
1010 return 0;
1011 }
1012
dev_stream_set_delay(const struct dev_stream * dev_stream,unsigned int delay_frames)1013 void dev_stream_set_delay(const struct dev_stream *dev_stream,
1014 unsigned int delay_frames)
1015 {
1016 }
1017
dev_stream_set_dev_rate(struct dev_stream * dev_stream,unsigned int dev_rate,double dev_rate_ratio,double master_rate_ratio,int coarse_rate_adjust)1018 void dev_stream_set_dev_rate(struct dev_stream *dev_stream,
1019 unsigned int dev_rate,
1020 double dev_rate_ratio,
1021 double master_rate_ratio,
1022 int coarse_rate_adjust)
1023 {
1024 }
1025
dev_stream_update_frames(const struct dev_stream * dev_stream)1026 void dev_stream_update_frames(const struct dev_stream *dev_stream)
1027 {
1028 }
1029
dev_stream_wake_time(struct dev_stream * dev_stream,unsigned int curr_level,struct timespec * level_tstamp,unsigned int cap_limit,int is_cap_limit_stream,struct timespec * wake_time)1030 int dev_stream_wake_time(struct dev_stream *dev_stream,
1031 unsigned int curr_level,
1032 struct timespec *level_tstamp,
1033 unsigned int cap_limit,
1034 int is_cap_limit_stream,
1035 struct timespec *wake_time)
1036 {
1037 if (dev_stream_wake_time_val.find(dev_stream) !=
1038 dev_stream_wake_time_val.end()) {
1039 wake_time->tv_sec = dev_stream_wake_time_val[dev_stream].tv_sec;
1040 wake_time->tv_nsec = dev_stream_wake_time_val[dev_stream].tv_nsec;
1041 }
1042 return 0;
1043 }
1044
dev_stream_is_pending_reply(const struct dev_stream * dev_stream)1045 int dev_stream_is_pending_reply(const struct dev_stream *dev_stream)
1046 {
1047 return 0;
1048 }
1049
dev_stream_flush_old_audio_messages(struct dev_stream * dev_stream)1050 int dev_stream_flush_old_audio_messages(struct dev_stream *dev_stream)
1051 {
1052 return 0;
1053 }
1054
cras_iodev_frames_queued(struct cras_iodev * iodev,struct timespec * tstamp)1055 int cras_iodev_frames_queued(struct cras_iodev *iodev, struct timespec *tstamp)
1056 {
1057 return iodev->frames_queued(iodev, tstamp);
1058 }
1059
cras_iodev_buffer_avail(struct cras_iodev * iodev,unsigned hw_level)1060 int cras_iodev_buffer_avail(struct cras_iodev *iodev, unsigned hw_level)
1061 {
1062 struct timespec tstamp;
1063 return iodev->buffer_size - iodev->frames_queued(iodev, &tstamp);
1064 }
1065
cras_iodev_fill_odev_zeros(struct cras_iodev * odev,unsigned int frames)1066 int cras_iodev_fill_odev_zeros(struct cras_iodev *odev, unsigned int frames)
1067 {
1068 cras_iodev_fill_odev_zeros_frames = frames;
1069 return 0;
1070 }
1071
cras_iodev_output_underrun(struct cras_iodev * odev)1072 int cras_iodev_output_underrun(struct cras_iodev *odev)
1073 {
1074 cras_iodev_output_underrun_called++;
1075 return 0;
1076 }
1077
cras_iodev_prepare_output_before_write_samples(struct cras_iodev * odev)1078 int cras_iodev_prepare_output_before_write_samples(struct cras_iodev *odev)
1079 {
1080 cras_iodev_prepare_output_before_write_samples_called++;
1081 odev->state = cras_iodev_prepare_output_before_write_samples_state;
1082 return cras_iodev_prepare_output_before_write_samples_ret;
1083 }
1084
cras_server_metrics_highest_hw_level(unsigned hw_level,enum CRAS_STREAM_DIRECTION direction)1085 int cras_server_metrics_highest_hw_level(unsigned hw_level,
1086 enum CRAS_STREAM_DIRECTION direction)
1087 {
1088 return 0;
1089 }
1090
cras_server_metrics_longest_fetch_delay(int delay_msec)1091 int cras_server_metrics_longest_fetch_delay(int delay_msec)
1092 {
1093 return 0;
1094 }
1095
cras_server_metrics_num_underruns(unsigned num_underruns)1096 int cras_server_metrics_num_underruns(unsigned num_underruns)
1097 {
1098 return 0;
1099 }
1100
cras_iodev_get_software_gain_scaler(const struct cras_iodev * iodev)1101 float cras_iodev_get_software_gain_scaler(const struct cras_iodev *iodev)
1102 {
1103 return 1.0f;
1104 }
1105
cras_iodev_frames_to_play_in_sleep(struct cras_iodev * odev,unsigned int * hw_level,struct timespec * hw_tstamp)1106 unsigned int cras_iodev_frames_to_play_in_sleep(struct cras_iodev *odev,
1107 unsigned int *hw_level,
1108 struct timespec *hw_tstamp)
1109 {
1110 *hw_level = cras_iodev_frames_queued(odev, hw_tstamp);
1111 cras_iodev_frames_to_play_in_sleep_called++;
1112 return 0;
1113 }
1114
cras_iodev_odev_should_wake(const struct cras_iodev * odev)1115 int cras_iodev_odev_should_wake(const struct cras_iodev *odev)
1116 {
1117 return 1;
1118 }
1119
cras_audio_area_create(int num_channels)1120 struct cras_audio_area *cras_audio_area_create(int num_channels)
1121 {
1122 struct cras_audio_area *area;
1123 size_t sz;
1124
1125 sz = sizeof(*area) + num_channels * sizeof(struct cras_channel_area);
1126 area = (cras_audio_area *)calloc(1, sz);
1127 area->num_channels = num_channels;
1128 area->channels[0].buf = (uint8_t*)calloc(1, BUFFER_SIZE * 2 * num_channels);
1129
1130 return area;
1131 }
1132
cras_iodev_state(const struct cras_iodev * iodev)1133 enum CRAS_IODEV_STATE cras_iodev_state(const struct cras_iodev *iodev)
1134 {
1135 return iodev->state;
1136 }
1137
cras_iodev_get_num_underruns(const struct cras_iodev * iodev)1138 unsigned int cras_iodev_get_num_underruns(const struct cras_iodev *iodev)
1139 {
1140 return 0;
1141 }
1142
cras_iodev_reset_request(struct cras_iodev * iodev)1143 int cras_iodev_reset_request(struct cras_iodev *iodev)
1144 {
1145 cras_iodev_reset_request_called++;
1146 cras_iodev_reset_request_iodev = iodev;
1147 return 0;
1148 }
1149
cras_iodev_get_num_severe_underruns(const struct cras_iodev * iodev)1150 unsigned int cras_iodev_get_num_severe_underruns(const struct cras_iodev *iodev)
1151 {
1152 return 0;
1153 }
1154
cras_iodev_update_highest_hw_level(struct cras_iodev * iodev,unsigned int hw_level)1155 void cras_iodev_update_highest_hw_level(struct cras_iodev *iodev,
1156 unsigned int hw_level)
1157 {
1158 }
1159
cras_iodev_start_ramp(struct cras_iodev * odev,enum CRAS_IODEV_RAMP_REQUEST request)1160 int cras_iodev_start_ramp(struct cras_iodev *odev,
1161 enum CRAS_IODEV_RAMP_REQUEST request)
1162 {
1163 cras_iodev_start_ramp_odev = odev;
1164 cras_iodev_start_ramp_request = request;
1165 return 0;
1166 }
1167
input_data_get_for_stream(struct input_data * data,struct cras_rstream * stream,struct buffer_share * offsets,struct cras_audio_area ** area,unsigned int * offset)1168 int input_data_get_for_stream(
1169 struct input_data *data,
1170 struct cras_rstream *stream,
1171 struct buffer_share *offsets,
1172 struct cras_audio_area **area,
1173 unsigned int *offset)
1174 {
1175 return 0;
1176 }
1177
input_data_put_for_stream(struct input_data * data,struct cras_rstream * stream,struct buffer_share * offsets,unsigned int frames)1178 int input_data_put_for_stream(struct input_data *data,
1179 struct cras_rstream *stream,
1180 struct buffer_share *offsets,
1181 unsigned int frames)
1182 {
1183 return 0;
1184 }
1185
1186 #ifdef HAVE_WEBRTC_APM
1187
cras_apm_list_get_effects(struct cras_apm_list * list)1188 uint64_t cras_apm_list_get_effects(struct cras_apm_list *list)
1189 {
1190 return 0;
1191 }
1192
cras_apm_list_set_debug_recording(struct cras_apm * apm,unsigned int stream_id,int start,const char * file_name_base)1193 void cras_apm_list_set_debug_recording(struct cras_apm *apm,
1194 unsigned int stream_id, int start, const char *file_name_base)
1195 {
1196 }
cras_apm_list_set_aec_dump(struct cras_apm_list * list,void * dev_ptr,int start,int fd)1197 void cras_apm_list_set_aec_dump(struct cras_apm_list *list,
1198 void *dev_ptr,
1199 int start,
1200 int fd)
1201 {
1202 }
1203
1204 #endif
1205
cras_audio_thread_busyloop()1206 int cras_audio_thread_busyloop()
1207 {
1208 cras_audio_thread_busyloop_called ++;
1209 return 0;
1210 }
1211
1212 } // extern "C"
1213
main(int argc,char ** argv)1214 int main(int argc, char **argv) {
1215 ::testing::InitGoogleTest(&argc, argv);
1216 return RUN_ALL_TESTS();
1217 }
1218