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