1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 extern "C" { 6 #include "audio_thread.c" 7 } 8 9 #include <stdio.h> 10 #include <sys/select.h> 11 #include <gtest/gtest.h> 12 13 extern "C" { 14 15 struct dev_stream_capture_call { 16 struct dev_stream *dev_stream; 17 const struct cras_audio_area *area; 18 unsigned int dev_index; 19 unsigned int num_called; 20 }; 21 22 struct cap_sleep_frames_call { 23 struct dev_stream *dev_stream; 24 unsigned int written; 25 unsigned int num_called; 26 }; 27 28 static int dev_stream_mix_dont_fill_next; 29 static unsigned int dev_stream_mix_count; 30 static unsigned int cras_mix_mute_count; 31 static unsigned int dev_stream_request_playback_samples_called; 32 static unsigned int cras_rstream_destroy_called; 33 static unsigned int cras_metrics_log_histogram_called; 34 static const char *cras_metrics_log_histogram_name; 35 static unsigned int cras_metrics_log_histogram_sample; 36 static unsigned int cras_metrics_log_event_called; 37 38 static void (*cras_system_add_select_fd_callback)(void *data); 39 static void *cras_system_add_select_fd_callback_data; 40 41 static int select_return_value; 42 static struct timeval select_timeval; 43 static int select_max_fd; 44 static fd_set select_in_fds; 45 static fd_set select_out_fds; 46 static uint32_t *select_write_ptr; 47 static uint32_t select_write_value; 48 static unsigned int cras_iodev_set_format_called; 49 static unsigned int dev_stream_set_delay_called; 50 static unsigned int cras_system_get_volume_return; 51 static unsigned int dev_stream_mix_called; 52 53 static struct timespec time_now; 54 static int cras_fmt_conversion_needed_return_val; 55 static struct cras_audio_area *dummy_audio_area1; 56 static struct cras_audio_area *dummy_audio_area2; 57 static struct cras_audio_format cras_iodev_set_format_val; 58 59 static struct dev_stream_capture_call dev_stream_capture_call; 60 static struct cap_sleep_frames_call cap_sleep_frames_call; 61 } 62 63 // Number of frames past target that will be added to sleep times to insure that 64 // all frames are ready. 65 static const int CAP_EXTRA_SLEEP_FRAMES = 16; 66 67 // Test the audio capture path. 68 class ReadStreamSuite : public testing::Test { 69 protected: 70 virtual void SetUp() { 71 memset(&cras_iodev_set_format_val, 0, sizeof(cras_iodev_set_format_val)); 72 cras_iodev_set_format_val.frame_rate = 44100; 73 cras_iodev_set_format_val.num_channels = 2; 74 cras_iodev_set_format_val.format = SND_PCM_FORMAT_S16_LE; 75 76 memset(&iodev_, 0, sizeof(iodev_)); 77 iodev_.buffer_size = 16384; 78 cb_threshold_ = 480; 79 iodev_.direction = CRAS_STREAM_INPUT; 80 81 iodev_.frames_queued = frames_queued; 82 iodev_.delay_frames = delay_frames; 83 iodev_.get_buffer = get_buffer; 84 iodev_.put_buffer = put_buffer; 85 iodev_.is_open = is_open; 86 iodev_.open_dev = open_dev; 87 iodev_.close_dev = close_dev; 88 iodev_.dev_running = dev_running; 89 90 memcpy(&output_dev_, &iodev_, sizeof(output_dev_)); 91 output_dev_.direction = CRAS_STREAM_OUTPUT; 92 93 SetupRstream(&rstream_, 1); 94 shm_ = cras_rstream_input_shm(rstream_); 95 SetupRstream(&rstream2_, 2); 96 shm2_ = cras_rstream_input_shm(rstream2_); 97 98 dummy_audio_area1 = (cras_audio_area*)calloc(1, 99 sizeof(cras_audio_area) + 2 * sizeof(cras_channel_area)); 100 dummy_audio_area1->num_channels = 2; 101 channel_area_set_channel(&dummy_audio_area1->channels[0], CRAS_CH_FL); 102 channel_area_set_channel(&dummy_audio_area1->channels[1], CRAS_CH_FR); 103 rstream_->input_audio_area = dummy_audio_area1; 104 dummy_audio_area2 = (cras_audio_area*)calloc(1, 105 sizeof(cras_audio_area) + 2 * sizeof(cras_channel_area)); 106 dummy_audio_area2->num_channels = 2; 107 channel_area_set_channel(&dummy_audio_area2->channels[0], CRAS_CH_FL); 108 channel_area_set_channel(&dummy_audio_area2->channels[1], CRAS_CH_FR); 109 rstream2_->input_audio_area = dummy_audio_area2; 110 111 dev_stream_mix_dont_fill_next = 0; 112 dev_stream_mix_count = 0; 113 dev_running_called_ = 0; 114 is_open_ = 0; 115 close_dev_called_ = 0; 116 117 cras_iodev_set_format_called = 0; 118 dev_stream_set_delay_called = 0; 119 } 120 121 virtual void TearDown() { 122 free(shm_->area); 123 free(rstream_); 124 free(shm2_->area); 125 free(rstream2_); 126 free(dummy_audio_area1); 127 free(dummy_audio_area2); 128 } 129 130 void SetupRstream(struct cras_rstream **rstream, int fd) { 131 struct cras_audio_shm *shm; 132 133 *rstream = (struct cras_rstream *)calloc(1, sizeof(**rstream)); 134 memcpy(&(*rstream)->format, &cras_iodev_set_format_val, 135 sizeof(cras_iodev_set_format_val)); 136 (*rstream)->direction = CRAS_STREAM_INPUT; 137 (*rstream)->cb_threshold = cb_threshold_; 138 (*rstream)->client = (struct cras_rclient *)this; 139 140 shm = cras_rstream_input_shm(*rstream); 141 shm->area = (struct cras_audio_shm_area *)calloc(1, 142 sizeof(*shm->area) + cb_threshold_ * 8); 143 cras_shm_set_frame_bytes(shm, 4); 144 cras_shm_set_used_size( 145 shm, cb_threshold_ * cras_shm_frame_bytes(shm)); 146 } 147 148 unsigned int GetCaptureSleepFrames() { 149 // Account for padding the sleep interval to ensure the wake up happens 150 // after the last desired frame is received. 151 return cb_threshold_ + 16; 152 } 153 154 // Stub functions for the iodev structure. 155 static int frames_queued(const cras_iodev* iodev) { 156 return frames_queued_; 157 } 158 159 static int delay_frames(const cras_iodev* iodev) { 160 return delay_frames_; 161 } 162 163 static int get_buffer(cras_iodev* iodev, 164 struct cras_audio_area** area, 165 unsigned int* num) { 166 size_t sz = sizeof(*area_) + sizeof(struct cras_channel_area) * 2; 167 168 if (audio_buffer_size_ < *num) 169 *num = audio_buffer_size_; 170 171 area_ = (cras_audio_area*)calloc(1, sz); 172 area_->frames = *num; 173 area_->num_channels = 2; 174 area_->channels[0].buf = audio_buffer_; 175 channel_area_set_channel(&area_->channels[0], CRAS_CH_FL); 176 area_->channels[0].step_bytes = 4; 177 area_->channels[1].buf = audio_buffer_ + 2; 178 channel_area_set_channel(&area_->channels[1], CRAS_CH_FR); 179 area_->channels[1].step_bytes = 4; 180 181 *area = area_; 182 return 0; 183 } 184 185 static int put_buffer(cras_iodev* iodev, 186 unsigned int num) { 187 free(area_); 188 return 0; 189 } 190 191 static int is_open(const cras_iodev* iodev) { 192 return is_open_; 193 } 194 195 static int open_dev(cras_iodev* iodev) { 196 return 0; 197 } 198 199 static int close_dev(cras_iodev* iodev) { 200 close_dev_called_++; 201 return 0; 202 } 203 204 static int dev_running(const cras_iodev* iodev) { 205 dev_running_called_++; 206 return 1; 207 } 208 209 210 struct cras_iodev iodev_; 211 struct cras_iodev output_dev_; 212 static int is_open_; 213 static int frames_queued_; 214 static int delay_frames_; 215 static unsigned int cb_threshold_; 216 static uint8_t audio_buffer_[8192]; 217 static struct cras_audio_area *area_; 218 static unsigned int audio_buffer_size_; 219 static unsigned int dev_running_called_; 220 static unsigned int close_dev_called_; 221 struct cras_rstream *rstream_; 222 struct cras_rstream *rstream2_; 223 struct cras_audio_shm *shm_; 224 struct cras_audio_shm *shm2_; 225 }; 226 227 int ReadStreamSuite::is_open_ = 0; 228 int ReadStreamSuite::frames_queued_ = 0; 229 int ReadStreamSuite::delay_frames_ = 0; 230 unsigned int ReadStreamSuite::close_dev_called_ = 0; 231 uint8_t ReadStreamSuite::audio_buffer_[8192]; 232 unsigned int ReadStreamSuite::audio_buffer_size_ = 0; 233 unsigned int ReadStreamSuite::dev_running_called_ = 0; 234 unsigned int ReadStreamSuite::cb_threshold_ = 0; 235 struct cras_audio_area *ReadStreamSuite::area_; 236 237 TEST_F(ReadStreamSuite, PossiblyReadGetAvailError) { 238 struct timespec ts; 239 int rc; 240 struct audio_thread *thread; 241 242 thread = audio_thread_create(); 243 ASSERT_TRUE(thread); 244 thread_set_active_dev(thread, &iodev_); 245 246 thread_add_stream(thread, rstream_); 247 EXPECT_EQ(1, cras_iodev_set_format_called); 248 249 frames_queued_ = -4; 250 is_open_ = 1; 251 rc = unified_io(thread, &ts); 252 EXPECT_EQ(-4, rc); 253 EXPECT_EQ(0, ts.tv_sec); 254 EXPECT_EQ(0, ts.tv_nsec); 255 EXPECT_EQ(0, dev_stream_set_delay_called); 256 EXPECT_EQ(1, close_dev_called_); 257 258 audio_thread_destroy(thread); 259 } 260 261 TEST_F(ReadStreamSuite, PossiblyReadEmpty) { 262 struct timespec ts; 263 int rc; 264 uint64_t nsec_expected; 265 struct audio_thread *thread; 266 267 thread = audio_thread_create(); 268 ASSERT_TRUE(thread); 269 thread_set_active_dev(thread, &iodev_); 270 271 thread_add_stream(thread, rstream_); 272 EXPECT_EQ(1, cras_iodev_set_format_called); 273 274 // If no samples are present, it should sleep for cb_threshold frames. 275 frames_queued_ = 0; 276 is_open_ = 1; 277 nsec_expected = (GetCaptureSleepFrames()) * 1000000000ULL / 278 (uint64_t)cras_iodev_set_format_val.frame_rate; 279 rc = unified_io(thread, &ts); 280 EXPECT_EQ(0, rc); 281 EXPECT_EQ(0, ts.tv_sec); 282 EXPECT_EQ(0, shm_->area->write_offset[0]); 283 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 284 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 285 EXPECT_EQ(1, dev_running_called_); 286 EXPECT_EQ(1, dev_stream_set_delay_called); 287 288 audio_thread_destroy(thread); 289 } 290 291 TEST_F(ReadStreamSuite, PossiblyReadTooLittleData) { 292 struct timespec ts; 293 int rc; 294 uint64_t nsec_expected; 295 static const uint64_t num_frames_short = 40; 296 struct audio_thread *thread; 297 298 thread = audio_thread_create(); 299 ASSERT_TRUE(thread); 300 thread_set_active_dev(thread, &iodev_); 301 302 thread_add_stream(thread, rstream_); 303 304 frames_queued_ = cb_threshold_ - num_frames_short; 305 is_open_ = 1; 306 audio_buffer_size_ = frames_queued_; 307 nsec_expected = ((uint64_t)num_frames_short + CAP_EXTRA_SLEEP_FRAMES) * 308 1000000000ULL / 309 (uint64_t)cras_iodev_set_format_val.frame_rate; 310 311 rc = unified_io(thread, &ts); 312 EXPECT_EQ(0, rc); 313 /* As much data as can be, should be read. */ 314 EXPECT_EQ(&audio_buffer_[0], dev_stream_capture_call.area->channels[0].buf); 315 EXPECT_EQ(rstream_, dev_stream_capture_call.dev_stream->stream); 316 EXPECT_EQ(cb_threshold_ - num_frames_short, cap_sleep_frames_call.written); 317 EXPECT_EQ(0, ts.tv_sec); 318 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 319 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 320 321 audio_thread_destroy(thread); 322 } 323 324 TEST_F(ReadStreamSuite, PossiblyReadHasDataWriteStream) { 325 struct timespec ts; 326 int rc; 327 uint64_t nsec_expected; 328 struct audio_thread *thread; 329 330 thread = audio_thread_create(); 331 ASSERT_TRUE(thread); 332 thread_set_active_dev(thread, &iodev_); 333 334 thread_add_stream(thread, rstream_); 335 336 // A full block plus 4 frames. 337 frames_queued_ = cb_threshold_ + 4; 338 audio_buffer_size_ = frames_queued_; 339 340 for (unsigned int i = 0; i < sizeof(audio_buffer_); i++) 341 audio_buffer_[i] = i; 342 343 uint64_t sleep_frames = GetCaptureSleepFrames() - 4; 344 nsec_expected = (uint64_t)sleep_frames * 1000000000ULL / 345 (uint64_t)cras_iodev_set_format_val.frame_rate; 346 is_open_ = 1; 347 // Give it some samples to copy. 348 rc = unified_io(thread, &ts); 349 EXPECT_EQ(0, rc); 350 EXPECT_EQ(0, ts.tv_sec); 351 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 352 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 353 EXPECT_EQ(1, dev_stream_set_delay_called); 354 EXPECT_EQ(&audio_buffer_[0], dev_stream_capture_call.area->channels[0].buf); 355 EXPECT_EQ(rstream_, dev_stream_capture_call.dev_stream->stream); 356 EXPECT_EQ(cb_threshold_, cap_sleep_frames_call.written); 357 EXPECT_EQ(rstream_, cap_sleep_frames_call.dev_stream->stream); 358 359 audio_thread_destroy(thread); 360 } 361 362 TEST_F(ReadStreamSuite, PossiblyReadHasDataWriteTwoStreams) { 363 struct timespec ts; 364 int rc; 365 uint64_t nsec_expected; 366 struct audio_thread *thread; 367 368 dev_stream_capture_call.num_called = 0; 369 cap_sleep_frames_call.num_called = 0; 370 371 thread = audio_thread_create(); 372 ASSERT_TRUE(thread); 373 thread_set_active_dev(thread, &iodev_); 374 375 rc = thread_add_stream(thread, rstream_); 376 EXPECT_EQ(0, rc); 377 rc = thread_add_stream(thread, rstream2_); 378 EXPECT_EQ(0, rc); 379 380 // A full block plus 4 frames. 381 frames_queued_ = cb_threshold_ + 4; 382 audio_buffer_size_ = frames_queued_; 383 384 for (unsigned int i = 0; i < sizeof(audio_buffer_); i++) 385 audio_buffer_[i] = i; 386 387 uint64_t sleep_frames = GetCaptureSleepFrames() - 4; 388 nsec_expected = (uint64_t)sleep_frames * 1000000000ULL / 389 (uint64_t)cras_iodev_set_format_val.frame_rate; 390 is_open_ = 1; 391 // Give it some samples to copy. 392 rc = unified_io(thread, &ts); 393 EXPECT_EQ(0, rc); 394 EXPECT_EQ(0, ts.tv_sec); 395 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 396 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 397 EXPECT_EQ(2, dev_stream_capture_call.num_called); 398 EXPECT_EQ(2, cap_sleep_frames_call.num_called); 399 400 audio_thread_destroy(thread); 401 } 402 403 TEST_F(ReadStreamSuite, PossiblyReadHasDataWriteTwoDifferentStreams) { 404 struct timespec ts; 405 int rc; 406 uint64_t nsec_expected; 407 struct audio_thread *thread; 408 409 thread = audio_thread_create(); 410 ASSERT_TRUE(thread); 411 thread_set_active_dev(thread, &iodev_); 412 413 cb_threshold_ /= 2; 414 rstream_->cb_threshold = cb_threshold_; 415 416 rc = thread_add_stream(thread, rstream_); 417 EXPECT_EQ(0, rc); 418 rc = thread_add_stream(thread, rstream2_); 419 EXPECT_EQ(0, rc); 420 421 // A full block plus 4 frames. 422 frames_queued_ = cb_threshold_ + 4; 423 audio_buffer_size_ = frames_queued_; 424 425 uint64_t sleep_frames = GetCaptureSleepFrames() - 4; 426 nsec_expected = (uint64_t)sleep_frames * 1000000000ULL / 427 (uint64_t)cras_iodev_set_format_val.frame_rate; 428 is_open_ = 1; 429 // Give it some samples to copy. 430 rc = unified_io(thread, &ts); 431 EXPECT_EQ(0, rc); 432 EXPECT_EQ(0, ts.tv_sec); 433 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 434 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 435 436 frames_queued_ = cb_threshold_ + 5; 437 sleep_frames = GetCaptureSleepFrames() - 5; 438 nsec_expected = (uint64_t)sleep_frames * 1000000000ULL / 439 (uint64_t)cras_iodev_set_format_val.frame_rate; 440 audio_buffer_size_ = frames_queued_; 441 is_open_ = 1; 442 // Give it some samples to copy. 443 rc = unified_io(thread, &ts); 444 EXPECT_EQ(0, rc); 445 EXPECT_EQ(0, ts.tv_sec); 446 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 447 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 448 449 audio_thread_destroy(thread); 450 } 451 452 TEST_F(ReadStreamSuite, PossiblyReadWriteThreeBuffers) { 453 struct timespec ts; 454 int rc; 455 struct audio_thread *thread; 456 457 thread = audio_thread_create(); 458 ASSERT_TRUE(thread); 459 thread_set_active_dev(thread, &iodev_); 460 461 thread_add_stream(thread, rstream_); 462 463 // A full block plus 4 frames. 464 frames_queued_ = cb_threshold_ + 4; 465 audio_buffer_size_ = frames_queued_; 466 is_open_ = 1; 467 468 // Give it some samples to copy. 469 rc = unified_io(thread, &ts); 470 EXPECT_EQ(0, rc); 471 EXPECT_EQ(0, cras_shm_num_overruns(shm_)); 472 EXPECT_EQ(&audio_buffer_[0], dev_stream_capture_call.area->channels[0].buf); 473 EXPECT_EQ(rstream_, dev_stream_capture_call.dev_stream->stream); 474 EXPECT_EQ(cb_threshold_, cap_sleep_frames_call.written); 475 EXPECT_EQ(rstream_, cap_sleep_frames_call.dev_stream->stream); 476 477 is_open_ = 1; 478 rc = unified_io(thread, &ts); 479 EXPECT_EQ(0, rc); 480 EXPECT_EQ(0, cras_shm_num_overruns(shm_)); 481 EXPECT_EQ(&audio_buffer_[0], dev_stream_capture_call.area->channels[0].buf); 482 EXPECT_EQ(rstream_, dev_stream_capture_call.dev_stream->stream); 483 EXPECT_EQ(cb_threshold_, cap_sleep_frames_call.written); 484 EXPECT_EQ(rstream_, cap_sleep_frames_call.dev_stream->stream); 485 486 is_open_ = 1; 487 rc = unified_io(thread, &ts); 488 EXPECT_EQ(0, rc); 489 EXPECT_EQ(&audio_buffer_[0], dev_stream_capture_call.area->channels[0].buf); 490 EXPECT_EQ(rstream_, dev_stream_capture_call.dev_stream->stream); 491 EXPECT_EQ(cb_threshold_, cap_sleep_frames_call.written); 492 EXPECT_EQ(rstream_, cap_sleep_frames_call.dev_stream->stream); 493 494 audio_thread_destroy(thread); 495 } 496 497 // Test the audio playback path. 498 class WriteStreamSuite : public testing::Test { 499 protected: 500 virtual void SetUp() { 501 memset(&fmt_, 0, sizeof(fmt_)); 502 fmt_.frame_rate = 44100; 503 fmt_.num_channels = 2; 504 fmt_.format = SND_PCM_FORMAT_S16_LE; 505 506 memset(&iodev_, 0, sizeof(iodev_)); 507 iodev_.format = &fmt_; 508 iodev_.buffer_size = 16384; 509 iodev_.direction = CRAS_STREAM_OUTPUT; 510 511 iodev_.frames_queued = frames_queued; 512 iodev_.delay_frames = delay_frames; 513 iodev_.get_buffer = get_buffer; 514 iodev_.put_buffer = put_buffer; 515 iodev_.dev_running = dev_running; 516 iodev_.is_open = is_open; 517 iodev_.open_dev = open_dev; 518 iodev_.close_dev = close_dev; 519 iodev_.buffer_size = 480; 520 521 buffer_frames_ = iodev_.buffer_size; 522 cb_threshold_ = 96; 523 SetupRstream(&rstream_, 1); 524 shm_ = cras_rstream_output_shm(rstream_); 525 SetupRstream(&rstream2_, 2); 526 shm2_ = cras_rstream_output_shm(rstream2_); 527 528 thread_ = audio_thread_create(); 529 ASSERT_TRUE(thread_); 530 thread_set_active_dev(thread_, &iodev_); 531 532 dev_stream_mix_dont_fill_next = 0; 533 dev_stream_mix_count = 0; 534 select_max_fd = -1; 535 select_write_ptr = NULL; 536 cras_metrics_log_event_called = 0; 537 dev_stream_request_playback_samples_called = 0; 538 cras_rstream_destroy_called = 0; 539 dev_stream_mix_called = 0; 540 is_open_ = 0; 541 close_dev_called_ = 0; 542 543 dev_running_called_ = 0; 544 545 audio_buffer_size_ = 8196; 546 thread_add_stream(thread_, rstream_); 547 frames_written_ = 0; 548 } 549 550 virtual void TearDown() { 551 free(shm_->area); 552 free(rstream_); 553 free(shm2_->area); 554 free(rstream2_); 555 audio_thread_destroy(thread_); 556 } 557 558 void SetupRstream(struct cras_rstream **rstream, int fd) { 559 struct cras_audio_shm *shm; 560 561 *rstream = (struct cras_rstream *)calloc(1, sizeof(**rstream)); 562 memcpy(&(*rstream)->format, &fmt_, sizeof(fmt_)); 563 (*rstream)->fd = fd; 564 (*rstream)->buffer_frames = buffer_frames_; 565 (*rstream)->cb_threshold = cb_threshold_; 566 (*rstream)->client = (struct cras_rclient *)this; 567 568 shm = cras_rstream_output_shm(*rstream); 569 shm->area = (struct cras_audio_shm_area *)calloc(1, 570 sizeof(*shm->area) + cb_threshold_ * 8); 571 cras_shm_set_frame_bytes(shm, 4); 572 cras_shm_set_used_size( 573 shm, buffer_frames_ * cras_shm_frame_bytes(shm)); 574 } 575 576 uint64_t GetCaptureSleepFrames() { 577 // Account for padding the sleep interval to ensure the wake up happens 578 // after the last desired frame is received. 579 return cb_threshold_ + CAP_EXTRA_SLEEP_FRAMES; 580 } 581 582 // Stub functions for the iodev structure. 583 static int frames_queued(const cras_iodev* iodev) { 584 return frames_queued_ + frames_written_; 585 } 586 587 static int delay_frames(const cras_iodev* iodev) { 588 return delay_frames_; 589 } 590 591 static int get_buffer(cras_iodev* iodev, 592 struct cras_audio_area** area, 593 unsigned int* num) { 594 size_t sz = sizeof(*area_) + sizeof(struct cras_channel_area) * 2; 595 596 if (audio_buffer_size_ < *num) 597 *num = audio_buffer_size_; 598 599 area_ = (cras_audio_area*)calloc(1, sz); 600 area_->frames = *num; 601 area_->num_channels = 2; 602 area_->channels[0].buf = audio_buffer_; 603 channel_area_set_channel(&area_->channels[0], CRAS_CH_FL); 604 area_->channels[0].step_bytes = 4; 605 area_->channels[1].buf = audio_buffer_ + 2; 606 channel_area_set_channel(&area_->channels[1], CRAS_CH_FR); 607 area_->channels[1].step_bytes = 4; 608 609 *area = area_; 610 return 0; 611 } 612 613 static int put_buffer(cras_iodev* iodev, 614 unsigned int num) { 615 free(area_); 616 frames_written_ += num; 617 return 0; 618 } 619 620 static int dev_running(const cras_iodev* iodev) { 621 dev_running_called_++; 622 return dev_running_; 623 } 624 625 static int is_open(const cras_iodev* iodev) { 626 return is_open_; 627 } 628 629 static int open_dev(cras_iodev* iodev) { 630 is_open_ = 1; 631 open_dev_called_++; 632 return 0; 633 } 634 635 static int close_dev(cras_iodev* iodev) { 636 close_dev_called_++; 637 is_open_ = 0; 638 return 0; 639 } 640 641 struct cras_iodev iodev_; 642 static int is_open_; 643 static int frames_queued_; 644 static int frames_written_; 645 static int delay_frames_; 646 static unsigned int cb_threshold_; 647 static unsigned int buffer_frames_; 648 static uint8_t audio_buffer_[8192]; 649 static unsigned int audio_buffer_size_; 650 static int dev_running_; 651 static unsigned int dev_running_called_; 652 static unsigned int close_dev_called_; 653 static unsigned int open_dev_called_; 654 static struct cras_audio_area *area_; 655 struct cras_audio_format fmt_; 656 struct cras_rstream* rstream_; 657 struct cras_rstream* rstream2_; 658 struct cras_audio_shm* shm_; 659 struct cras_audio_shm* shm2_; 660 struct audio_thread *thread_; 661 }; 662 663 int WriteStreamSuite::is_open_ = 0; 664 int WriteStreamSuite::frames_queued_ = 0; 665 int WriteStreamSuite::frames_written_ = 0; 666 int WriteStreamSuite::delay_frames_ = 0; 667 unsigned int WriteStreamSuite::cb_threshold_ = 0; 668 unsigned int WriteStreamSuite::buffer_frames_ = 0; 669 uint8_t WriteStreamSuite::audio_buffer_[8192]; 670 unsigned int WriteStreamSuite::audio_buffer_size_ = 0; 671 int WriteStreamSuite::dev_running_ = 1; 672 unsigned int WriteStreamSuite::dev_running_called_ = 0; 673 unsigned int WriteStreamSuite::close_dev_called_ = 0; 674 unsigned int WriteStreamSuite::open_dev_called_ = 0; 675 struct cras_audio_area *WriteStreamSuite::area_; 676 677 TEST_F(WriteStreamSuite, PossiblyFillGetAvailError) { 678 struct timespec ts; 679 int rc; 680 681 frames_queued_ = -4; 682 is_open_ = 1; 683 rc = unified_io(thread_, &ts); 684 EXPECT_EQ(-4, rc); 685 EXPECT_EQ(0, ts.tv_sec); 686 EXPECT_EQ(0, ts.tv_nsec); 687 EXPECT_EQ(1, close_dev_called_); 688 } 689 690 TEST_F(WriteStreamSuite, PossiblyFillEarlyWake) { 691 struct timespec ts; 692 int rc; 693 694 // If woken and still have tons of data to play, go back to sleep. 695 frames_queued_ = cb_threshold_ * 2; 696 audio_buffer_size_ = buffer_frames_ - frames_queued_; 697 698 iodev_.direction = CRAS_STREAM_OUTPUT; 699 is_open_ = 1; 700 701 rc = unified_io(thread_, &ts); 702 EXPECT_EQ(0, rc); 703 EXPECT_EQ(0, ts.tv_sec); 704 } 705 706 TEST_F(WriteStreamSuite, PossiblyFillGetFromStreamFull) { 707 struct timespec ts; 708 int rc; 709 uint64_t nsec_expected; 710 711 // Have cb_threshold samples left. 712 frames_queued_ = cb_threshold_; 713 audio_buffer_size_ = buffer_frames_ - frames_queued_; 714 nsec_expected = (uint64_t)cb_threshold_ * 715 1000000000ULL / (uint64_t)fmt_.frame_rate; 716 717 // shm has plenty of data in it. 718 shm_->area->write_offset[0] = cb_threshold_ * 4; 719 720 FD_ZERO(&select_out_fds); 721 FD_SET(rstream_->fd, &select_out_fds); 722 select_return_value = 1; 723 is_open_ = 1; 724 725 rc = unified_io(thread_, &ts); 726 EXPECT_EQ(0, rc); 727 EXPECT_EQ(0, ts.tv_sec); 728 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 729 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 730 EXPECT_EQ(cb_threshold_, dev_stream_mix_count); 731 EXPECT_EQ(0, dev_stream_request_playback_samples_called); 732 EXPECT_EQ(-1, select_max_fd); 733 } 734 735 TEST_F(WriteStreamSuite, PossiblyFillGetFromStreamMinSet) { 736 struct timespec ts; 737 int rc; 738 uint64_t nsec_expected; 739 740 // Have cb_threshold samples left. 741 frames_queued_ = cb_threshold_ * 2; 742 audio_buffer_size_ = buffer_frames_ - frames_queued_; 743 // Setting the min_buffer_level should shorten the sleep time. 744 iodev_.min_buffer_level = cb_threshold_; 745 746 // shm has is empty. 747 shm_->area->write_offset[0] = 0; 748 749 FD_ZERO(&select_out_fds); 750 FD_SET(rstream_->fd, &select_out_fds); 751 select_return_value = 1; 752 is_open_ = 1; 753 // Set write offset after call to select. 754 select_write_ptr = &shm_->area->write_offset[0]; 755 select_write_value = cb_threshold_ * 4; 756 757 // After the callback there will be cb_thresh of data in the buffer and 758 // cb_thresh x 2 data in the hardware (frames_queued_) = 3 cb_thresh total. 759 // It should sleep until there is a total of cb_threshold + min_buffer_level 760 // left, or 3 - 2 = 1 cb_thresh worth of delay. 761 nsec_expected = (uint64_t)cb_threshold_ * 762 1000000000ULL / (uint64_t)fmt_.frame_rate; 763 764 rc = unified_io(thread_, &ts); 765 EXPECT_EQ(0, rc); 766 EXPECT_EQ(0, ts.tv_sec); 767 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 768 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 769 EXPECT_EQ(cb_threshold_, dev_stream_mix_count); 770 EXPECT_EQ(1, dev_stream_request_playback_samples_called); 771 } 772 773 TEST_F(WriteStreamSuite, PossiblyFillFramesQueued) { 774 struct timespec ts; 775 int rc; 776 777 // Have cb_threshold samples left. 778 frames_queued_ = cb_threshold_; 779 audio_buffer_size_ = buffer_frames_ - frames_queued_; 780 781 // shm has plenty of data in it. 782 shm_->area->write_offset[0] = cras_shm_used_size(shm_); 783 784 is_open_ = 1; 785 rc = unified_io(thread_, &ts); 786 EXPECT_EQ(0, rc); 787 EXPECT_EQ(1, dev_running_called_); 788 } 789 790 TEST_F(WriteStreamSuite, PossiblyFillGetFromStreamOneEmpty) { 791 struct timespec ts; 792 int rc; 793 794 // Have cb_threshold samples left. 795 frames_queued_ = cb_threshold_; 796 audio_buffer_size_ = buffer_frames_ - frames_queued_; 797 798 // shm has plenty of data in it. 799 shm_->area->write_offset[0] = cras_shm_used_size(shm_); 800 801 // Test that nothing breaks if there is an empty stream. 802 dev_stream_mix_dont_fill_next = 1; 803 804 FD_ZERO(&select_out_fds); 805 FD_SET(rstream_->fd, &select_out_fds); 806 select_return_value = 1; 807 808 is_open_ = 1; 809 rc = unified_io(thread_, &ts); 810 EXPECT_EQ(0, rc); 811 EXPECT_EQ(0, dev_stream_request_playback_samples_called); 812 EXPECT_EQ(-1, select_max_fd); 813 EXPECT_EQ(0, shm_->area->read_offset[0]); 814 EXPECT_EQ(0, shm_->area->read_offset[1]); 815 EXPECT_EQ(cras_shm_used_size(shm_), shm_->area->write_offset[0]); 816 EXPECT_EQ(0, shm_->area->write_offset[1]); 817 } 818 819 TEST_F(WriteStreamSuite, PossiblyFillGetFromStreamNeedFill) { 820 struct timespec ts; 821 uint64_t nsec_expected; 822 int rc; 823 824 // Have cb_threshold samples left. 825 frames_queued_ = cb_threshold_; 826 audio_buffer_size_ = buffer_frames_ - frames_queued_; 827 828 // shm is out of data. 829 shm_->area->write_offset[0] = 0; 830 831 FD_ZERO(&select_out_fds); 832 FD_SET(rstream_->fd, &select_out_fds); 833 select_return_value = 1; 834 // Set write offset after call to select. 835 select_write_ptr = &shm_->area->write_offset[0]; 836 select_write_value = (buffer_frames_ - cb_threshold_) * 4; 837 838 nsec_expected = (buffer_frames_ - cb_threshold_) * 839 1000000000ULL / (uint64_t)fmt_.frame_rate; 840 841 is_open_ = 1; 842 rc = unified_io(thread_, &ts); 843 EXPECT_EQ(0, rc); 844 EXPECT_EQ(0, ts.tv_sec); 845 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 846 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 847 EXPECT_EQ(buffer_frames_ - cb_threshold_, dev_stream_mix_count); 848 EXPECT_EQ(1, dev_stream_request_playback_samples_called); 849 EXPECT_NE(-1, select_max_fd); 850 EXPECT_EQ(0, memcmp(&select_out_fds, &select_in_fds, sizeof(select_in_fds))); 851 EXPECT_EQ(0, shm_->area->read_offset[0]); 852 } 853 854 TEST_F(WriteStreamSuite, PossiblyFillGetFromTwoStreamsFull) { 855 struct timespec ts; 856 int rc; 857 uint64_t nsec_expected; 858 859 // Have cb_threshold samples left. 860 frames_queued_ = cras_rstream_get_cb_threshold(rstream_); 861 audio_buffer_size_ = buffer_frames_ - frames_queued_; 862 nsec_expected = (uint64_t)cras_rstream_get_cb_threshold(rstream_) * 863 1000000000ULL / (uint64_t)fmt_.frame_rate; 864 865 // shm has plenty of data in it. 866 shm_->area->write_offset[0] = cras_rstream_get_cb_threshold(rstream_) * 4; 867 shm2_->area->write_offset[0] = cras_rstream_get_cb_threshold(rstream2_) * 4; 868 869 thread_add_stream(thread_, rstream2_); 870 871 is_open_ = 1; 872 rc = unified_io(thread_, &ts); 873 EXPECT_EQ(0, rc); 874 EXPECT_EQ(2, dev_stream_mix_called); 875 EXPECT_EQ(0, ts.tv_sec); 876 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 877 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 878 EXPECT_EQ(cras_rstream_get_cb_threshold(rstream_), 879 dev_stream_mix_count); 880 EXPECT_EQ(0, dev_stream_request_playback_samples_called); 881 EXPECT_EQ(-1, select_max_fd); 882 } 883 884 TEST_F(WriteStreamSuite, PossiblyFillGetFromTwoOneEmptySmallerCbThreshold) { 885 struct timespec ts; 886 int rc; 887 uint64_t nsec_expected; 888 889 // Have cb_threshold samples left. 890 frames_queued_ = cb_threshold_; 891 audio_buffer_size_ = buffer_frames_ - frames_queued_; 892 893 // First stream is empty and with a smaller cb_threshold. This is to test 894 // the case that when buffer level reaches the cb_threshold of one stream 895 // but not yet the other stream of smaller cb_threshold. 896 rstream_->cb_threshold -= 20; 897 nsec_expected = 20 * 1000000000ULL / (uint64_t)fmt_.frame_rate; 898 shm_->area->write_offset[0] = 0; 899 shm2_->area->write_offset[0] = cras_shm_used_size(shm2_); 900 901 thread_add_stream(thread_, rstream2_); 902 is_open_ = 1; 903 rc = unified_io(thread_, &ts); 904 905 // In this case, assert (1) we didn't request the empty stream since buffer 906 // level is larger then its cb_threshold, (2) still mix both streams so 907 // dev_stream_mix_count is zero, and (3) the resulting sleep frames 908 // equals the cb_threshold difference. 909 EXPECT_EQ(0, rc); 910 EXPECT_EQ(2, dev_stream_mix_called); 911 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 912 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 913 EXPECT_EQ(0, dev_stream_mix_count); 914 EXPECT_EQ(0, dev_stream_request_playback_samples_called); 915 } 916 917 TEST_F(WriteStreamSuite, PossiblyFillGetFromTwoOneEmptyAfterFetch) { 918 struct timespec ts; 919 int rc; 920 921 // Have cb_threshold samples left. 922 frames_queued_ = cb_threshold_; 923 audio_buffer_size_ = buffer_frames_ - frames_queued_; 924 925 // First stream empty while the second stream full. 926 shm_->area->write_offset[0] = 0; 927 shm2_->area->write_offset[0] = cras_shm_used_size(shm2_); 928 929 thread_add_stream(thread_, rstream2_); 930 931 FD_ZERO(&select_out_fds); 932 FD_SET(rstream_->fd, &select_out_fds); 933 select_return_value = 1; 934 935 is_open_ = 1; 936 rc = unified_io(thread_, &ts); 937 938 // Assert that the empty stream is skipped, only one stream mixed. 939 EXPECT_EQ(0, rc); 940 EXPECT_EQ(1, dev_stream_mix_called); 941 EXPECT_EQ(buffer_frames_ - cb_threshold_, dev_stream_mix_count); 942 EXPECT_EQ(1, dev_stream_request_playback_samples_called); 943 EXPECT_NE(-1, select_max_fd); 944 EXPECT_EQ(0, memcmp(&select_out_fds, &select_in_fds, sizeof(select_in_fds))); 945 } 946 947 TEST_F(WriteStreamSuite, PossiblyFillGetFromTwoStreamsFullOneMixes) { 948 struct timespec ts; 949 int rc; 950 size_t written_expected; 951 952 // Have cb_threshold samples left. 953 frames_queued_ = cb_threshold_; 954 audio_buffer_size_ = buffer_frames_ - frames_queued_; 955 written_expected = buffer_frames_ - cb_threshold_; 956 957 // shm has plenty of data in it. 958 shm_->area->write_offset[0] = cras_shm_used_size(shm_); 959 shm2_->area->write_offset[0] = cras_shm_used_size(shm2_); 960 961 thread_add_stream(thread_, rstream2_); 962 963 // Test that nothing breaks if one stream doesn't fill. 964 dev_stream_mix_dont_fill_next = 1; 965 966 is_open_ = 1; 967 rc = unified_io(thread_, &ts); 968 EXPECT_EQ(0, rc); 969 EXPECT_EQ(0, dev_stream_request_playback_samples_called); 970 EXPECT_EQ(0, shm_->area->read_offset[0]); // No write from first stream. 971 EXPECT_EQ(written_expected * 4, shm2_->area->read_offset[0]); 972 } 973 974 TEST_F(WriteStreamSuite, PossiblyFillGetFromTwoStreamsOneLimited) { 975 struct timespec ts; 976 int rc; 977 uint64_t nsec_expected; 978 static const unsigned int smaller_frames = 10; 979 980 // Have cb_threshold samples left. 981 frames_queued_ = cb_threshold_; 982 audio_buffer_size_ = buffer_frames_ - frames_queued_; 983 nsec_expected = (uint64_t)smaller_frames * 984 (1000000000ULL / (uint64_t)fmt_.frame_rate); 985 986 // One has too little the other is full. 987 shm_->area->write_offset[0] = smaller_frames * 4; 988 shm_->area->write_buf_idx = 1; 989 shm2_->area->write_offset[0] = cras_shm_used_size(shm2_); 990 shm2_->area->write_buf_idx = 1; 991 992 thread_add_stream(thread_, rstream2_); 993 994 FD_ZERO(&select_out_fds); 995 FD_SET(rstream_->fd, &select_out_fds); 996 select_return_value = 1; 997 998 is_open_ = 1; 999 rc = unified_io(thread_, &ts); 1000 EXPECT_EQ(0, rc); 1001 EXPECT_EQ(0, ts.tv_sec); 1002 EXPECT_GE(ts.tv_nsec, nsec_expected - 1000); 1003 EXPECT_LE(ts.tv_nsec, nsec_expected + 1000); 1004 EXPECT_EQ(smaller_frames, dev_stream_mix_count); 1005 EXPECT_EQ(1, dev_stream_request_playback_samples_called); 1006 EXPECT_NE(-1, select_max_fd); 1007 } 1008 1009 TEST_F(WriteStreamSuite, DrainOutputBufferCompelete) { 1010 frames_queued_ = 3 * cb_threshold_; 1011 close_dev_called_ = 0; 1012 // All the audio in hw buffer are extra silent frames. 1013 iodev_.extra_silent_frames = frames_queued_ + 1; 1014 drain_output_buffer(thread_, &iodev_); 1015 EXPECT_EQ(1, close_dev_called_); 1016 } 1017 1018 1019 TEST_F(WriteStreamSuite, DrainOutputBufferWaitForPlayback) { 1020 // Hardware buffer is full. 1021 frames_queued_ = buffer_frames_; 1022 iodev_.extra_silent_frames = 0; 1023 close_dev_called_ = 0; 1024 drain_output_buffer(thread_, &iodev_); 1025 EXPECT_EQ(0, close_dev_called_); 1026 } 1027 1028 TEST_F(WriteStreamSuite, DrainOutputBufferWaitForAudio) { 1029 // Hardware buffer is almost empty 1030 frames_queued_ = 30; 1031 iodev_.extra_silent_frames = 0; 1032 close_dev_called_ = 0; 1033 drain_output_buffer(thread_, &iodev_); 1034 EXPECT_LT(cb_threshold_ - frames_queued_, frames_written_); 1035 EXPECT_EQ(0, close_dev_called_); 1036 } 1037 1038 TEST_F(WriteStreamSuite, DrainOutputStream) { 1039 struct timespec ts; 1040 int rc; 1041 1042 // Have 3 * cb_threshold samples in the hw buffer. 1043 // Have 4 * cb_threshold samples in the first stream's shm 1044 // Note: used_size = 5 * cb_threshold. 1045 frames_queued_ = 3 * cb_threshold_; 1046 audio_buffer_size_ = buffer_frames_ - frames_queued_; 1047 shm_->area->write_offset[0] = 4 * cb_threshold_ * 4; 1048 1049 is_open_ = 1; 1050 close_dev_called_ = 0; 1051 open_dev_called_ = 0; 1052 1053 thread_disconnect_stream(thread_, rstream_); 1054 1055 // We should be draining the audio. 1056 EXPECT_EQ(0, close_dev_called_); 1057 EXPECT_EQ(0, open_dev_called_); 1058 1059 rc = unified_io(thread_, &ts); 1060 1061 EXPECT_EQ(0, rc); 1062 EXPECT_EQ(2 * cb_threshold_, frames_written_); 1063 EXPECT_EQ(0, open_dev_called_); 1064 EXPECT_EQ(0, close_dev_called_); 1065 1066 // Clear the hardware buffer 1067 frames_queued_ = 0; 1068 frames_written_ = 0; 1069 audio_buffer_size_ = buffer_frames_ - frames_queued_; 1070 1071 rc = unified_io(thread_, &ts); 1072 1073 // Verified that all data in stream1 is written. 1074 // The device is not closed before we have played all the content. 1075 EXPECT_EQ(0, rc); 1076 EXPECT_EQ(2 * cb_threshold_, frames_written_); 1077 EXPECT_EQ(0, close_dev_called_); 1078 1079 // Clear the hardware buffer again. 1080 frames_queued_ = 0; 1081 frames_written_ = 0; 1082 audio_buffer_size_ = buffer_frames_ - frames_queued_; 1083 rc = unified_io(thread_, &ts); 1084 1085 EXPECT_EQ(1, cras_rstream_destroy_called); 1086 EXPECT_EQ(1, iodev_.is_draining); 1087 EXPECT_EQ(0, rc); 1088 EXPECT_EQ(480, thread_->buffer_frames[CRAS_STREAM_OUTPUT]); 1089 EXPECT_EQ(96, thread_->cb_threshold[CRAS_STREAM_OUTPUT]); 1090 1091 // Clear the hardware buffer again. 1092 frames_queued_ = 0; 1093 frames_written_ = 0; 1094 audio_buffer_size_ = buffer_frames_ - frames_queued_; 1095 rc = unified_io(thread_, &ts); 1096 1097 EXPECT_EQ(1, close_dev_called_); 1098 EXPECT_EQ(0, thread_->buffer_frames[CRAS_STREAM_OUTPUT]); 1099 EXPECT_EQ(0, thread_->cb_threshold[CRAS_STREAM_OUTPUT]); 1100 } 1101 1102 // Test adding and removing streams. 1103 class AddStreamSuite : public testing::Test { 1104 protected: 1105 virtual void SetUp() { 1106 memset(&cras_iodev_set_format_val, 0, sizeof(cras_iodev_set_format_val)); 1107 cras_iodev_set_format_val.frame_rate = 44100; 1108 cras_iodev_set_format_val.num_channels = 2; 1109 cras_iodev_set_format_val.format = SND_PCM_FORMAT_S16_LE; 1110 1111 memset(&iodev_, 0, sizeof(iodev_)); 1112 iodev_.buffer_size = 16384; 1113 used_size_ = 480; 1114 cb_threshold_ = 96; 1115 iodev_.direction = CRAS_STREAM_OUTPUT; 1116 1117 iodev_.is_open = is_open; 1118 iodev_.open_dev = open_dev; 1119 iodev_.close_dev = close_dev; 1120 iodev_.get_buffer = get_buffer; 1121 iodev_.put_buffer = put_buffer; 1122 1123 is_open_ = 0; 1124 is_open_called_ = 0; 1125 open_dev_called_ = 0; 1126 close_dev_called_ = 0; 1127 open_dev_return_val_ = 0; 1128 1129 cras_iodev_set_format_called = 0; 1130 cras_rstream_destroy_called = 0; 1131 cras_metrics_log_histogram_called = 0; 1132 cras_metrics_log_histogram_name = NULL; 1133 cras_metrics_log_histogram_sample = 0; 1134 1135 audio_buffer_size_ = 8196; 1136 } 1137 1138 virtual void TearDown() { 1139 } 1140 1141 unsigned int GetCaptureSleepFrames() { 1142 // Account for padding the sleep interval to ensure the wake up happens 1143 // after the last desired frame is received. 1144 return cb_threshold_ + 16; 1145 } 1146 1147 // Stub functions for the iodev structure. 1148 static int get_buffer(cras_iodev* iodev, 1149 struct cras_audio_area** area, 1150 unsigned int* num) { 1151 size_t sz = sizeof(*area_) + sizeof(struct cras_channel_area) * 2; 1152 1153 if (audio_buffer_size_ < *num) 1154 *num = audio_buffer_size_; 1155 1156 area_ = (cras_audio_area*)calloc(1, sz); 1157 area_->frames = *num; 1158 area_->num_channels = 2; 1159 area_->channels[0].buf = audio_buffer_; 1160 channel_area_set_channel(&area_->channels[0], CRAS_CH_FL); 1161 area_->channels[0].step_bytes = 4; 1162 area_->channels[1].buf = audio_buffer_ + 2; 1163 channel_area_set_channel(&area_->channels[1], CRAS_CH_FR); 1164 area_->channels[1].step_bytes = 4; 1165 1166 *area = area_; 1167 return 0; 1168 } 1169 1170 static int put_buffer(cras_iodev* iodev, 1171 unsigned int num) { 1172 free(area_); 1173 return 0; 1174 } 1175 1176 static int is_open(const cras_iodev* iodev) { 1177 is_open_called_++; 1178 return is_open_; 1179 } 1180 1181 static int open_dev(cras_iodev* iodev) { 1182 open_dev_called_++; 1183 is_open_ = true; 1184 return open_dev_return_val_; 1185 } 1186 1187 static int close_dev(cras_iodev* iodev) { 1188 close_dev_called_++; 1189 is_open_ = false; 1190 return 0; 1191 } 1192 1193 void add_rm_two_streams(CRAS_STREAM_DIRECTION direction) { 1194 int rc; 1195 struct cras_rstream *new_stream, *second_stream; 1196 cras_audio_shm *shm; 1197 struct cras_audio_format *fmt; 1198 struct audio_thread *thread; 1199 1200 thread = audio_thread_create(); 1201 1202 fmt = (struct cras_audio_format *)malloc(sizeof(*fmt)); 1203 memcpy(fmt, &cras_iodev_set_format_val, sizeof(*fmt)); 1204 iodev_.direction = direction; 1205 new_stream = (struct cras_rstream *)calloc(1, sizeof(*new_stream)); 1206 new_stream->fd = 55; 1207 new_stream->buffer_frames = 65; 1208 new_stream->cb_threshold = 80; 1209 new_stream->direction = direction; 1210 memcpy(&new_stream->format, fmt, sizeof(*fmt)); 1211 shm = cras_rstream_output_shm(new_stream); 1212 shm->area = (struct cras_audio_shm_area *)calloc(1, sizeof(*shm->area)); 1213 1214 if (direction == CRAS_STREAM_INPUT) 1215 thread_set_active_dev(thread, &iodev_); 1216 else 1217 thread_set_active_dev(thread, &iodev_); 1218 1219 thread_add_stream(thread, new_stream); 1220 EXPECT_EQ(1, thread->devs_open[direction]); 1221 EXPECT_EQ(1, open_dev_called_); 1222 EXPECT_EQ(65, thread->buffer_frames[direction]); 1223 if (direction == CRAS_STREAM_OUTPUT) 1224 EXPECT_EQ(32, thread->cb_threshold[direction]); 1225 else 1226 EXPECT_EQ(80, thread->cb_threshold[direction]); 1227 1228 is_open_ = 1; 1229 1230 second_stream = (struct cras_rstream *)calloc(1, sizeof(*second_stream)); 1231 second_stream->fd = 56; 1232 second_stream->buffer_frames = 25; 1233 second_stream->cb_threshold = 12; 1234 second_stream->direction = direction; 1235 memcpy(&second_stream->format, fmt, sizeof(*fmt)); 1236 shm = cras_rstream_output_shm(second_stream); 1237 shm->area = (struct cras_audio_shm_area *)calloc(1, sizeof(*shm->area)); 1238 1239 is_open_called_ = 0; 1240 thread_add_stream(thread, second_stream); 1241 EXPECT_EQ(1, thread->devs_open[direction]); 1242 EXPECT_EQ(1, open_dev_called_); 1243 EXPECT_EQ(25, thread->buffer_frames[direction]); 1244 EXPECT_EQ(12, thread->cb_threshold[direction]); 1245 1246 // Remove the streams. 1247 rc = thread_remove_stream(thread, second_stream); 1248 EXPECT_EQ(1, rc); 1249 EXPECT_EQ(0, close_dev_called_); 1250 if (direction == CRAS_STREAM_OUTPUT) 1251 EXPECT_EQ(32, thread->cb_threshold[direction]); 1252 else 1253 EXPECT_EQ(80, thread->cb_threshold[direction]); 1254 1255 rc = thread_remove_stream(thread, new_stream); 1256 EXPECT_EQ(0, rc); 1257 1258 // For output stream, we enter the draining mode; 1259 // for input stream, we close the device directly. 1260 if (direction == CRAS_STREAM_INPUT) { 1261 EXPECT_EQ(0, thread->devs_open[direction]); 1262 EXPECT_EQ(0, thread->buffer_frames[direction]); 1263 EXPECT_EQ(0, thread->cb_threshold[direction]); 1264 } else { 1265 EXPECT_EQ(1, iodev_.is_draining); 1266 } 1267 1268 free(fmt); 1269 shm = cras_rstream_output_shm(new_stream); 1270 audio_thread_destroy(thread); 1271 free(shm->area); 1272 free(new_stream); 1273 shm = cras_rstream_output_shm(second_stream); 1274 free(shm->area); 1275 free(second_stream); 1276 } 1277 1278 struct cras_iodev iodev_; 1279 static int is_open_; 1280 static int is_open_called_; 1281 static int open_dev_called_; 1282 static int open_dev_return_val_; 1283 static int close_dev_called_; 1284 static int used_size_; 1285 static int cb_threshold_; 1286 struct cras_audio_format fmt_; 1287 static struct cras_audio_area *area_; 1288 static uint8_t audio_buffer_[8192]; 1289 static unsigned int audio_buffer_size_; 1290 }; 1291 1292 int AddStreamSuite::is_open_ = 0; 1293 int AddStreamSuite::is_open_called_ = 0; 1294 int AddStreamSuite::open_dev_called_ = 0; 1295 int AddStreamSuite::open_dev_return_val_ = 0; 1296 int AddStreamSuite::close_dev_called_ = 0; 1297 int AddStreamSuite::used_size_ = 0; 1298 int AddStreamSuite::cb_threshold_ = 0; 1299 struct cras_audio_area *AddStreamSuite::area_; 1300 uint8_t AddStreamSuite::audio_buffer_[8192]; 1301 unsigned int AddStreamSuite::audio_buffer_size_ = 0; 1302 1303 TEST_F(AddStreamSuite, SimpleAddOutputStream) { 1304 int rc; 1305 cras_rstream* new_stream; 1306 cras_audio_shm *shm; 1307 struct audio_thread *thread; 1308 1309 thread = audio_thread_create(); 1310 1311 new_stream = (struct cras_rstream *)calloc(1, sizeof(*new_stream)); 1312 new_stream->client = (struct cras_rclient* )this; 1313 new_stream->fd = 55; 1314 new_stream->buffer_frames = 65; 1315 new_stream->cb_threshold = 80; 1316 memcpy(&new_stream->format, &cras_iodev_set_format_val, 1317 sizeof(cras_iodev_set_format_val)); 1318 1319 shm = cras_rstream_output_shm(new_stream); 1320 shm->area = (struct cras_audio_shm_area *)calloc(1, sizeof(*shm->area)); 1321 1322 thread_set_active_dev(thread, &iodev_); 1323 1324 rc = thread_add_stream(thread, new_stream); 1325 ASSERT_EQ(0, rc); 1326 EXPECT_EQ(1, thread->devs_open[CRAS_STREAM_OUTPUT]); 1327 EXPECT_EQ(1, open_dev_called_); 1328 EXPECT_EQ(65, thread->buffer_frames[CRAS_STREAM_OUTPUT]); 1329 EXPECT_EQ(32, thread->cb_threshold[CRAS_STREAM_OUTPUT]); 1330 1331 is_open_ = 1; 1332 1333 // remove the stream. 1334 rc = thread_remove_stream(thread, new_stream); 1335 EXPECT_EQ(0, rc); 1336 EXPECT_EQ(1, iodev_.is_draining); 1337 EXPECT_EQ(0, cras_metrics_log_histogram_called); 1338 EXPECT_EQ(0, cras_rstream_destroy_called); 1339 1340 rc = thread_disconnect_stream(thread, new_stream); 1341 EXPECT_EQ(1, cras_rstream_destroy_called); 1342 1343 free(shm->area); 1344 audio_thread_destroy(thread); 1345 free(new_stream); 1346 } 1347 1348 1349 TEST_F(AddStreamSuite, AddStreamOpenFail) { 1350 struct audio_thread *thread; 1351 cras_rstream new_stream; 1352 cras_audio_shm *shm; 1353 1354 thread = audio_thread_create(); 1355 ASSERT_TRUE(thread); 1356 thread_set_active_dev(thread, &iodev_); 1357 printf("1\n"); 1358 1359 shm = cras_rstream_output_shm(&new_stream); 1360 shm->area = (struct cras_audio_shm_area *)calloc(1, sizeof(*shm->area)); 1361 1362 open_dev_return_val_ = -1; 1363 new_stream.direction = CRAS_STREAM_OUTPUT; 1364 EXPECT_EQ(AUDIO_THREAD_OUTPUT_DEV_ERROR, 1365 thread_add_stream(thread, &new_stream)); 1366 printf("2\n"); 1367 EXPECT_EQ(1, open_dev_called_); 1368 EXPECT_EQ(1, cras_iodev_set_format_called); 1369 audio_thread_destroy(thread); 1370 printf("3\n"); 1371 free(shm->area); 1372 } 1373 1374 TEST_F(AddStreamSuite, AddRmTwoOutputStreams) { 1375 add_rm_two_streams(CRAS_STREAM_OUTPUT); 1376 } 1377 1378 TEST_F(AddStreamSuite, AddRmTwoInputStreams) { 1379 add_rm_two_streams(CRAS_STREAM_INPUT); 1380 } 1381 1382 TEST_F(AddStreamSuite, RmStreamLogLongestTimeout) { 1383 int rc; 1384 cras_rstream* new_stream; 1385 cras_audio_shm *shm; 1386 struct audio_thread *thread; 1387 1388 thread = audio_thread_create(); 1389 1390 new_stream = (struct cras_rstream *)calloc(1, sizeof(*new_stream)); 1391 new_stream->fd = 55; 1392 new_stream->buffer_frames = 65; 1393 new_stream->cb_threshold = 80; 1394 memcpy(&new_stream->format, &cras_iodev_set_format_val, 1395 sizeof(cras_iodev_set_format_val)); 1396 1397 shm = cras_rstream_output_shm(new_stream); 1398 shm->area = (struct cras_audio_shm_area *)calloc(1, sizeof(*shm->area)); 1399 1400 thread_set_active_dev(thread, &iodev_); 1401 rc = thread_add_stream(thread, new_stream); 1402 ASSERT_EQ(0, rc); 1403 EXPECT_EQ(1, thread->devs_open[CRAS_STREAM_OUTPUT]); 1404 EXPECT_EQ(1, open_dev_called_); 1405 EXPECT_EQ(65, thread->buffer_frames[CRAS_STREAM_OUTPUT]); 1406 EXPECT_EQ(32, thread->cb_threshold[CRAS_STREAM_OUTPUT]); 1407 1408 is_open_ = 1; 1409 cras_shm_set_longest_timeout(shm, 90); 1410 1411 // remove the stream. 1412 rc = thread_remove_stream(thread, new_stream); 1413 EXPECT_EQ(0, rc); 1414 EXPECT_EQ(1, iodev_.is_draining); 1415 1416 cras_system_add_select_fd_callback(cras_system_add_select_fd_callback_data); 1417 1418 EXPECT_EQ(1, cras_metrics_log_histogram_called); 1419 EXPECT_STREQ(kStreamTimeoutMilliSeconds, cras_metrics_log_histogram_name); 1420 EXPECT_EQ(90, cras_metrics_log_histogram_sample); 1421 1422 free(shm->area); 1423 free(new_stream); 1424 audio_thread_destroy(thread); 1425 } 1426 1427 class ActiveDevicesSuite : public testing::Test { 1428 protected: 1429 virtual void SetUp() { 1430 memset(&cras_iodev_set_format_val, 0, sizeof(cras_iodev_set_format_val)); 1431 cras_iodev_set_format_val.frame_rate = 44100; 1432 cras_iodev_set_format_val.num_channels = 2; 1433 cras_iodev_set_format_val.format = SND_PCM_FORMAT_S16_LE; 1434 1435 memset(&iodev_, 0, sizeof(iodev_)); 1436 memset(&iodev2_, 0, sizeof(iodev2_)); 1437 iodev_.close_dev = close_dev; 1438 iodev_.is_open = is_open; 1439 iodev_.open_dev = open_dev; 1440 iodev_.delay_frames = delay_frames; 1441 iodev_.get_buffer = get_buffer; 1442 iodev_.put_buffer = put_buffer; 1443 iodev_.frames_queued = frames_queued; 1444 iodev_.delay_frames = delay_frames; 1445 iodev_.dev_running = dev_running; 1446 iodev_.buffer_size = 2048; 1447 iodev2_.close_dev = close_dev; 1448 iodev2_.is_open = is_open; 1449 iodev2_.open_dev = open_dev; 1450 iodev2_.delay_frames = delay_frames; 1451 iodev2_.get_buffer = get_buffer; 1452 iodev2_.put_buffer = put_buffer; 1453 iodev2_.frames_queued = frames_queued; 1454 iodev2_.delay_frames = delay_frames; 1455 iodev2_.dev_running = dev_running; 1456 iodev2_.buffer_size = 2048; 1457 thread_ = audio_thread_create(); 1458 ASSERT_TRUE(thread_); 1459 1460 buffer_frames_ = 500; 1461 cb_threshold_ = 250; 1462 SetupRstream(&rstream_); 1463 SetupRstream(&rstream2_); 1464 rstream2_->buffer_frames -= 50; 1465 rstream2_->cb_threshold -= 50; 1466 1467 dummy_audio_area1 = (cras_audio_area*)calloc(1, 1468 sizeof(cras_audio_area) + 2 * sizeof(cras_channel_area)); 1469 dummy_audio_area1->num_channels = 2; 1470 channel_area_set_channel(&dummy_audio_area1->channels[0], CRAS_CH_FL); 1471 channel_area_set_channel(&dummy_audio_area1->channels[1], CRAS_CH_FR); 1472 rstream_->input_audio_area = dummy_audio_area1; 1473 dummy_audio_area2 = (cras_audio_area*)calloc(1, 1474 sizeof(cras_audio_area) + 2 * sizeof(cras_channel_area)); 1475 dummy_audio_area2->num_channels = 2; 1476 channel_area_set_channel(&dummy_audio_area2->channels[0], CRAS_CH_FL); 1477 channel_area_set_channel(&dummy_audio_area2->channels[1], CRAS_CH_FR); 1478 rstream2_->input_audio_area = dummy_audio_area2; 1479 1480 cras_iodev_set_format_called = 0; 1481 close_dev_called_ = 0; 1482 is_open_ = 0; 1483 cras_fmt_conversion_needed_return_val = 0; 1484 open_dev_val_idx_ = 0; 1485 delay_frames_val_idx_ = 0; 1486 frames_queued_val_idx_ = 0; 1487 frames_queued_[0] = 250; 1488 frames_queued_[1] = 250; 1489 get_buffer_val_idx_ = 0; 1490 put_buffer_val_idx_ = 0; 1491 for (int i = 0; i < 8; i++) { 1492 open_dev_val_[i] = 0; 1493 delay_frames_[i] = 0; 1494 audio_buffer_size_[i] = 250; 1495 get_buffer_rc_[i] = 0; 1496 put_buffer_rc_[i] = 0; 1497 } 1498 } 1499 1500 virtual void TearDown() { 1501 struct cras_audio_shm *shm; 1502 audio_thread_destroy(thread_); 1503 shm = cras_rstream_output_shm(rstream_); 1504 free(shm->area); 1505 free(rstream_); 1506 free(dummy_audio_area1); 1507 free(dummy_audio_area2); 1508 } 1509 1510 void SetupRstream(struct cras_rstream **rstream) { 1511 struct cras_audio_shm *shm; 1512 *rstream = (struct cras_rstream *)calloc(1, sizeof(**rstream)); 1513 memcpy(&(*rstream)->format, &cras_iodev_set_format_val, 1514 sizeof(cras_iodev_set_format_val)); 1515 (*rstream)->direction = CRAS_STREAM_OUTPUT; 1516 (*rstream)->buffer_frames = buffer_frames_; 1517 (*rstream)->cb_threshold = cb_threshold_; 1518 shm = cras_rstream_output_shm(*rstream); 1519 shm->area = (struct cras_audio_shm_area *)calloc(1, 1520 sizeof(*shm->area) + cb_threshold_ * 8); 1521 cras_shm_set_frame_bytes(shm, 4); 1522 cras_shm_set_used_size( 1523 shm, buffer_frames_ * cras_shm_frame_bytes(shm)); 1524 shm = cras_rstream_input_shm(*rstream); 1525 shm->area = (struct cras_audio_shm_area *)calloc(1, 1526 sizeof(*shm->area) + buffer_frames_ * 8); 1527 cras_shm_set_frame_bytes(shm, 4); 1528 cras_shm_set_used_size( 1529 shm, cb_threshold_* cras_shm_frame_bytes(shm)); 1530 } 1531 1532 static int close_dev(struct cras_iodev *iodev) { 1533 close_dev_called_++; 1534 return 0; 1535 } 1536 1537 static int is_open(const cras_iodev *iodev) { 1538 return is_open_; 1539 } 1540 1541 static int open_dev(struct cras_iodev *iodev) { 1542 open_dev_val_idx_ %= 8; 1543 is_open_ = 1; 1544 return open_dev_val_[open_dev_val_idx_++]; 1545 } 1546 1547 static int delay_frames(const cras_iodev* iodev) { 1548 delay_frames_val_idx_ %= 8; 1549 return delay_frames_[delay_frames_val_idx_++]; 1550 } 1551 1552 static int dev_running(const cras_iodev* iodev) { 1553 return 1; 1554 } 1555 1556 static int frames_queued(const cras_iodev* iodev) { 1557 frames_queued_val_idx_ %= 2; 1558 return frames_queued_[frames_queued_val_idx_++]; 1559 } 1560 1561 static int get_buffer(cras_iodev* iodev, 1562 struct cras_audio_area** area, 1563 unsigned int* num) { 1564 size_t sz = sizeof(*area_) + sizeof(struct cras_channel_area) * 2; 1565 1566 get_buffer_val_idx_ %= 8; 1567 if (audio_buffer_size_[get_buffer_val_idx_] < *num) 1568 *num = audio_buffer_size_[get_buffer_val_idx_]; 1569 1570 area_ = (cras_audio_area*)calloc(1, sz); 1571 area_->frames = *num; 1572 area_->num_channels = 2; 1573 area_->channels[0].buf = audio_buffer_[get_buffer_val_idx_]; 1574 channel_area_set_channel(&area_->channels[0], CRAS_CH_FL); 1575 area_->channels[0].step_bytes = 4; 1576 area_->channels[1].buf = audio_buffer_[get_buffer_val_idx_] + 2; 1577 channel_area_set_channel(&area_->channels[1], CRAS_CH_FR); 1578 area_->channels[1].step_bytes = 4; 1579 1580 *area = area_; 1581 1582 get_buffer_val_idx_++; 1583 return 0; 1584 } 1585 1586 static int put_buffer(cras_iodev* iodev, 1587 unsigned int num) { 1588 free(area_); 1589 put_buffer_val_idx_ %= 8; 1590 return put_buffer_rc_[put_buffer_val_idx_++]; 1591 } 1592 1593 static int is_open_; 1594 static int open_dev_val_[8]; 1595 static int open_dev_val_idx_; 1596 static int close_dev_called_; 1597 static int buffer_frames_; 1598 static int cb_threshold_; 1599 static int frames_queued_[2]; 1600 static int frames_queued_val_idx_; 1601 static uint8_t audio_buffer_[8][8192]; 1602 static unsigned int audio_buffer_size_[8]; 1603 static int get_buffer_rc_[8]; 1604 static int put_buffer_rc_[8]; 1605 static int get_buffer_val_idx_; 1606 static int put_buffer_val_idx_; 1607 struct cras_iodev iodev_; 1608 struct cras_iodev iodev2_; 1609 struct cras_rstream *rstream_; 1610 struct cras_rstream *rstream2_; 1611 struct audio_thread *thread_; 1612 static struct cras_audio_area *area_; 1613 static int delay_frames_val_idx_; 1614 static int delay_frames_[8]; 1615 }; 1616 1617 int ActiveDevicesSuite::is_open_ = 0; 1618 int ActiveDevicesSuite::buffer_frames_ = 0; 1619 int ActiveDevicesSuite::cb_threshold_ = 0; 1620 int ActiveDevicesSuite::frames_queued_[2]; 1621 int ActiveDevicesSuite::frames_queued_val_idx_; 1622 int ActiveDevicesSuite::close_dev_called_ = 0; 1623 int ActiveDevicesSuite::open_dev_val_[8]; 1624 int ActiveDevicesSuite::open_dev_val_idx_ = 0; 1625 int ActiveDevicesSuite::delay_frames_val_idx_ = 0; 1626 int ActiveDevicesSuite::delay_frames_[8]; 1627 uint8_t ActiveDevicesSuite::audio_buffer_[8][8192]; 1628 unsigned int ActiveDevicesSuite::audio_buffer_size_[8]; 1629 int ActiveDevicesSuite::get_buffer_val_idx_ = 0; 1630 int ActiveDevicesSuite::put_buffer_val_idx_ = 0; 1631 int ActiveDevicesSuite::get_buffer_rc_[8]; 1632 int ActiveDevicesSuite::put_buffer_rc_[8]; 1633 struct cras_audio_area *ActiveDevicesSuite::area_; 1634 1635 TEST_F(ActiveDevicesSuite, SetActiveDevRemoveOld) { 1636 struct active_dev *adevs; 1637 struct cras_iodev iodev3_; 1638 1639 iodev_.direction = CRAS_STREAM_INPUT; 1640 iodev2_.direction = CRAS_STREAM_INPUT; 1641 iodev3_.direction = CRAS_STREAM_INPUT; 1642 1643 thread_set_active_dev(thread_, &iodev_); 1644 adevs = thread_->active_devs[CRAS_STREAM_INPUT]; 1645 EXPECT_NE((void *)NULL, adevs); 1646 EXPECT_EQ(adevs->dev, &iodev_); 1647 EXPECT_EQ(1, iodev_.is_active); 1648 1649 /* Assert the first active dev is still iodev. */ 1650 thread_add_active_dev(thread_, &iodev2_); 1651 adevs = thread_->active_devs[CRAS_STREAM_INPUT]; 1652 EXPECT_EQ(adevs->dev, &iodev_); 1653 1654 thread_set_active_dev(thread_, &iodev3_); 1655 adevs = thread_->active_devs[CRAS_STREAM_INPUT]; 1656 EXPECT_EQ(adevs->dev, &iodev3_); 1657 EXPECT_EQ(iodev3_.is_active, 1); 1658 EXPECT_EQ(iodev_.is_active, 0); 1659 } 1660 1661 TEST_F(ActiveDevicesSuite, SetActiveDevAlreadyInList) { 1662 struct active_dev *adevs; 1663 iodev_.direction = CRAS_STREAM_INPUT; 1664 iodev2_.direction = CRAS_STREAM_INPUT; 1665 1666 thread_set_active_dev(thread_, &iodev_); 1667 thread_add_active_dev(thread_, &iodev2_); 1668 1669 adevs = thread_->active_devs[CRAS_STREAM_INPUT]; 1670 EXPECT_EQ(adevs->dev, &iodev_); 1671 EXPECT_EQ(iodev_.is_active, 1); 1672 1673 thread_set_active_dev(thread_, &iodev2_); 1674 adevs = thread_->active_devs[CRAS_STREAM_INPUT]; 1675 EXPECT_EQ(adevs->dev, &iodev2_); 1676 EXPECT_EQ(iodev2_.is_active, 1); 1677 EXPECT_EQ(iodev_.is_active, 0); 1678 } 1679 1680 TEST_F(ActiveDevicesSuite, AddRemoveActiveDevice) { 1681 struct active_dev *adevs; 1682 iodev_.direction = CRAS_STREAM_INPUT; 1683 iodev2_.direction = CRAS_STREAM_INPUT; 1684 1685 thread_set_active_dev(thread_, &iodev_); 1686 adevs = thread_->active_devs[CRAS_STREAM_INPUT]; 1687 EXPECT_NE((void *)NULL, adevs); 1688 EXPECT_EQ(adevs->dev, &iodev_); 1689 EXPECT_EQ(1, iodev_.is_active); 1690 1691 thread_add_active_dev(thread_, &iodev2_); 1692 EXPECT_NE((void *)NULL, adevs->next); 1693 EXPECT_EQ(adevs->next->dev, &iodev2_); 1694 EXPECT_EQ(1, iodev2_.is_active); 1695 1696 thread_rm_active_dev(thread_, &iodev_); 1697 adevs = thread_->active_devs[CRAS_STREAM_INPUT]; 1698 EXPECT_EQ((void *)NULL, adevs->next); 1699 EXPECT_EQ(adevs->dev, &iodev2_); 1700 EXPECT_EQ(0, iodev_.is_active); 1701 1702 iodev_.direction = CRAS_STREAM_POST_MIX_PRE_DSP; 1703 thread_add_active_dev(thread_, &iodev_); 1704 EXPECT_NE((void *)NULL, thread_->active_devs[CRAS_STREAM_POST_MIX_PRE_DSP]); 1705 EXPECT_EQ(1, iodev_.is_active); 1706 } 1707 1708 TEST_F(ActiveDevicesSuite, ClearActiveDevices) { 1709 iodev_.direction = CRAS_STREAM_OUTPUT; 1710 iodev2_.direction = CRAS_STREAM_OUTPUT; 1711 1712 thread_set_active_dev(thread_, &iodev_); 1713 thread_add_active_dev(thread_, &iodev2_); 1714 EXPECT_NE((void *)NULL, thread_->active_devs[CRAS_STREAM_OUTPUT]); 1715 EXPECT_EQ(1, iodev_.is_active); 1716 EXPECT_EQ(1, iodev2_.is_active); 1717 1718 thread_clear_active_devs(thread_, CRAS_STREAM_OUTPUT); 1719 EXPECT_EQ((void *)NULL, thread_->active_devs[CRAS_STREAM_OUTPUT]); 1720 EXPECT_EQ(0, iodev_.is_active); 1721 EXPECT_EQ(0, iodev2_.is_active); 1722 } 1723 1724 TEST_F(ActiveDevicesSuite, OpenActiveDevices) { 1725 iodev_.direction = CRAS_STREAM_OUTPUT; 1726 iodev2_.direction = CRAS_STREAM_OUTPUT; 1727 1728 thread_set_active_dev(thread_, &iodev_); 1729 thread_add_active_dev(thread_, &iodev2_); 1730 thread_add_stream(thread_, rstream_); 1731 1732 EXPECT_EQ(2, cras_iodev_set_format_called); 1733 EXPECT_EQ(500, thread_->buffer_frames[CRAS_STREAM_OUTPUT]); 1734 EXPECT_EQ(250, thread_->cb_threshold[CRAS_STREAM_OUTPUT]); 1735 } 1736 1737 TEST_F(ActiveDevicesSuite, OpenFirstActiveDeviceFail) { 1738 int rc; 1739 iodev_.direction = CRAS_STREAM_OUTPUT; 1740 iodev2_.direction = CRAS_STREAM_OUTPUT; 1741 1742 thread_set_active_dev(thread_, &iodev_); 1743 thread_add_active_dev(thread_, &iodev2_); 1744 1745 open_dev_val_[0] = -1; 1746 rc = thread_add_stream(thread_, rstream_); 1747 EXPECT_EQ(rc, AUDIO_THREAD_OUTPUT_DEV_ERROR); 1748 EXPECT_EQ(2, cras_iodev_set_format_called); 1749 EXPECT_EQ(0, thread_->buffer_frames[CRAS_STREAM_OUTPUT]); 1750 EXPECT_EQ(0, thread_->cb_threshold[CRAS_STREAM_OUTPUT]); 1751 } 1752 1753 TEST_F(ActiveDevicesSuite, OpenSecondActiveDeviceFail) { 1754 int rc; 1755 iodev_.direction = CRAS_STREAM_OUTPUT; 1756 iodev2_.direction = CRAS_STREAM_OUTPUT; 1757 1758 thread_set_active_dev(thread_, &iodev_); 1759 thread_add_active_dev(thread_, &iodev2_); 1760 1761 open_dev_val_[1] = -1; 1762 rc = thread_add_stream(thread_, rstream_); 1763 EXPECT_EQ(0, rc); 1764 EXPECT_EQ(2, cras_iodev_set_format_called); 1765 EXPECT_EQ(500, thread_->buffer_frames[CRAS_STREAM_OUTPUT]); 1766 EXPECT_EQ(250, thread_->cb_threshold[CRAS_STREAM_OUTPUT]); 1767 EXPECT_EQ(0, close_dev_called_); 1768 EXPECT_EQ((void *)NULL, thread_->active_devs[CRAS_STREAM_OUTPUT]->next); 1769 } 1770 1771 TEST_F(ActiveDevicesSuite, OpenSecondActiveDeviceFormatIncompatible) { 1772 int rc; 1773 iodev_.direction = CRAS_STREAM_OUTPUT; 1774 iodev2_.direction = CRAS_STREAM_OUTPUT; 1775 1776 thread_set_active_dev(thread_, &iodev_); 1777 thread_add_active_dev(thread_, &iodev2_); 1778 1779 cras_fmt_conversion_needed_return_val = 1; 1780 rc = thread_add_stream(thread_, rstream_); 1781 EXPECT_EQ(0, rc); 1782 EXPECT_EQ(2, cras_iodev_set_format_called); 1783 EXPECT_EQ(500, thread_->buffer_frames[CRAS_STREAM_OUTPUT]); 1784 EXPECT_EQ(250, thread_->cb_threshold[CRAS_STREAM_OUTPUT]); 1785 EXPECT_EQ(1, close_dev_called_); 1786 EXPECT_EQ((void *)NULL, thread_->active_devs[CRAS_STREAM_OUTPUT]->next); 1787 } 1788 1789 TEST_F(ActiveDevicesSuite, CloseActiveDevices) { 1790 iodev_.direction = CRAS_STREAM_OUTPUT; 1791 iodev2_.direction = CRAS_STREAM_OUTPUT; 1792 1793 thread_set_active_dev(thread_, &iodev_); 1794 thread_add_active_dev(thread_, &iodev2_); 1795 1796 thread_add_stream(thread_, rstream_); 1797 EXPECT_EQ(1, thread_->devs_open[CRAS_STREAM_OUTPUT]); 1798 EXPECT_EQ(2, cras_iodev_set_format_called); 1799 1800 thread_add_stream(thread_, rstream2_); 1801 EXPECT_EQ(4, cras_iodev_set_format_called); 1802 1803 thread_remove_stream(thread_, rstream2_); 1804 1805 thread_remove_stream(thread_, rstream_); 1806 EXPECT_EQ(1, iodev_.is_draining); 1807 EXPECT_EQ(1, iodev2_.is_draining); 1808 } 1809 1810 TEST_F(ActiveDevicesSuite, InputDelayFrames) { 1811 int fr; 1812 iodev_.direction = CRAS_STREAM_INPUT; 1813 iodev2_.direction = CRAS_STREAM_INPUT; 1814 rstream_->direction = CRAS_STREAM_INPUT; 1815 1816 thread_set_active_dev(thread_, &iodev_); 1817 thread_add_active_dev(thread_, &iodev2_); 1818 1819 thread_add_stream(thread_, rstream_); 1820 delay_frames_[0] = 3; 1821 delay_frames_[1] = 33; 1822 fr = input_delay_frames(thread_->active_devs[CRAS_STREAM_INPUT]); 1823 EXPECT_EQ(33, fr); 1824 1825 delay_frames_val_idx_ = 0; 1826 delay_frames_[1] = -1; 1827 fr = input_delay_frames(thread_->active_devs[CRAS_STREAM_INPUT]); 1828 EXPECT_EQ(-1, fr); 1829 } 1830 1831 TEST_F(ActiveDevicesSuite, InputFramesQueued) { 1832 int fr; 1833 iodev_.direction = CRAS_STREAM_INPUT; 1834 iodev2_.direction = CRAS_STREAM_INPUT; 1835 thread_set_active_dev(thread_, &iodev_); 1836 thread_add_active_dev(thread_, &iodev2_); 1837 frames_queued_val_idx_ = 0; 1838 frames_queued_[0] = 195; 1839 frames_queued_[1] = 190; 1840 fr = input_min_frames_queued(thread_->active_devs[CRAS_STREAM_INPUT]); 1841 EXPECT_EQ(190, fr); 1842 1843 /* Test error path. */ 1844 frames_queued_val_idx_ = 0; 1845 frames_queued_[0] = -1; 1846 frames_queued_[1] = 190; 1847 fr = input_min_frames_queued(thread_->active_devs[CRAS_STREAM_INPUT]); 1848 EXPECT_EQ(-1, fr); 1849 } 1850 1851 TEST_F(ActiveDevicesSuite, MixMultipleInputs) { 1852 struct timespec ts; 1853 1854 iodev_.direction = CRAS_STREAM_INPUT; 1855 iodev2_.direction = CRAS_STREAM_INPUT; 1856 rstream_->direction = CRAS_STREAM_INPUT; 1857 rstream2_->direction = CRAS_STREAM_INPUT; 1858 1859 for (unsigned int dev = 0; dev < 8; dev++) { 1860 int16_t *buff = (int16_t*)audio_buffer_[dev]; 1861 for (unsigned int i = 0; i < 250; i++) 1862 buff[i] = i; 1863 } 1864 thread_set_active_dev(thread_, &iodev_); 1865 thread_add_active_dev(thread_, &iodev2_); 1866 1867 /* Assert shm from rstream_ is used. */ 1868 thread_add_stream(thread_, rstream_); 1869 unified_io(thread_, &ts); 1870 EXPECT_EQ(rstream_, cap_sleep_frames_call.dev_stream->stream); 1871 1872 thread_add_stream(thread_, rstream2_); 1873 unified_io(thread_, &ts); 1874 EXPECT_EQ(rstream2_, cap_sleep_frames_call.dev_stream->stream); 1875 } 1876 1877 extern "C" { 1878 1879 const char kNoCodecsFoundMetric[] = "Cras.NoCodecsFoundAtBoot"; 1880 const char kStreamTimeoutMilliSeconds[] = "Cras.StreamTimeoutMilliSeconds"; 1881 1882 int cras_iodev_get_thread_poll_fd(const struct cras_iodev *iodev) { 1883 return 0; 1884 } 1885 1886 int cras_iodev_read_thread_command(struct cras_iodev *iodev, 1887 uint8_t *buf, 1888 size_t max_len) { 1889 return 0; 1890 } 1891 1892 int cras_iodev_send_command_response(struct cras_iodev *iodev, int rc) { 1893 return 0; 1894 } 1895 1896 void cras_iodev_fill_time_from_frames(size_t frames, 1897 size_t frame_rate, 1898 struct timespec *ts) { 1899 uint64_t to_play_usec; 1900 1901 ts->tv_sec = 0; 1902 /* adjust sleep time to target our callback threshold */ 1903 to_play_usec = (uint64_t)frames * 1000000L / (uint64_t)frame_rate; 1904 1905 while (to_play_usec > 1000000) { 1906 ts->tv_sec++; 1907 to_play_usec -= 1000000; 1908 } 1909 ts->tv_nsec = to_play_usec * 1000; 1910 } 1911 1912 void dev_stream_set_delay(const struct dev_stream *dev_stream, 1913 unsigned int delay_frames) { 1914 dev_stream_set_delay_called++; 1915 } 1916 1917 void cras_set_capture_timestamp(size_t frame_rate, 1918 size_t frames, 1919 struct cras_timespec *ts) { 1920 } 1921 1922 int cras_iodev_set_format(struct cras_iodev *iodev, 1923 struct cras_audio_format *fmt) 1924 { 1925 cras_iodev_set_format_called++; 1926 iodev->format = &cras_iodev_set_format_val; 1927 return 0; 1928 } 1929 1930 // From mixer. 1931 unsigned int dev_stream_mix(struct dev_stream *dev_stream, 1932 size_t num_channels, 1933 uint8_t *dst, 1934 size_t *count, 1935 size_t *index) { 1936 int16_t *src; 1937 int16_t *target = (int16_t *)dst; 1938 size_t fr_written, fr_in_buf; 1939 size_t num_samples; 1940 size_t frames = 0; 1941 struct cras_audio_shm *shm; 1942 1943 if (dev_stream->stream->direction == CRAS_STREAM_OUTPUT) { 1944 shm = &dev_stream->stream->output_shm; 1945 } else { 1946 shm = &dev_stream->stream->input_shm; 1947 } 1948 1949 dev_stream_mix_called++; 1950 1951 if (dev_stream_mix_dont_fill_next) { 1952 dev_stream_mix_dont_fill_next = 0; 1953 return 0; 1954 } 1955 dev_stream_mix_count = *count; 1956 1957 /* We only copy the data from shm to dst, not actually mix them. */ 1958 fr_in_buf = cras_shm_get_frames(shm); 1959 if (fr_in_buf == 0) 1960 return 0; 1961 if (fr_in_buf < *count) 1962 *count = fr_in_buf; 1963 1964 fr_written = 0; 1965 while (fr_written < *count) { 1966 src = cras_shm_get_readable_frames(shm, 1967 fr_written, &frames); 1968 if (frames > *count - fr_written) 1969 frames = *count - fr_written; 1970 num_samples = frames * num_channels; 1971 memcpy(target, src, num_samples * 2); 1972 fr_written += frames; 1973 target += num_samples; 1974 } 1975 1976 *index = *index + 1; 1977 cras_shm_buffer_read(shm, fr_written); 1978 return *count; 1979 } 1980 1981 void cras_scale_buffer(int16_t *buffer, unsigned int count, float scaler) { 1982 } 1983 1984 size_t cras_mix_mute_buffer(uint8_t *dst, 1985 size_t frame_bytes, 1986 size_t count) { 1987 cras_mix_mute_count = count; 1988 return count; 1989 } 1990 1991 void cras_mix_add_clip(int16_t *dst, const int16_t *src, size_t count) { 1992 int32_t sum; 1993 unsigned int i; 1994 1995 for (i = 0; i < count; i++) { 1996 sum = dst[i] + src[i]; 1997 if (sum > 0x7fff) 1998 sum = 0x7fff; 1999 else if (sum < -0x8000) 2000 sum = -0x8000; 2001 dst[i] = sum; 2002 } 2003 } 2004 2005 // From cras_metrics.c 2006 void cras_metrics_log_event(const char *event) 2007 { 2008 cras_metrics_log_event_called++; 2009 } 2010 2011 void cras_metrics_log_histogram(const char *name, int sample, int min, 2012 int max, int nbuckets) 2013 { 2014 cras_metrics_log_histogram_called++; 2015 cras_metrics_log_histogram_name = name; 2016 cras_metrics_log_histogram_sample = sample; 2017 } 2018 2019 // From util. 2020 int cras_set_rt_scheduling(int rt_lim) { 2021 return 0; 2022 } 2023 2024 int cras_set_thread_priority(int priority) { 2025 return 0; 2026 } 2027 2028 // From rstream. 2029 int cras_rstream_get_audio_request_reply(const struct cras_rstream *stream) { 2030 return 0; 2031 } 2032 2033 void cras_rstream_log_overrun(const struct cras_rstream *stream) { 2034 } 2035 2036 int cras_system_add_select_fd(int fd, 2037 void (*callback)(void *data), 2038 void *callback_data) 2039 { 2040 cras_system_add_select_fd_callback = callback; 2041 cras_system_add_select_fd_callback_data = callback_data; 2042 return 0; 2043 } 2044 2045 void cras_system_rm_select_fd(int fd) 2046 { 2047 } 2048 2049 size_t cras_system_get_volume() { 2050 return cras_system_get_volume_return; 2051 } 2052 2053 int cras_system_get_mute() { 2054 return 0; 2055 } 2056 2057 int cras_system_get_capture_mute() { 2058 return 0; 2059 } 2060 2061 void cras_rstream_destroy(struct cras_rstream *stream) { 2062 cras_rstream_destroy_called++; 2063 } 2064 2065 void loopback_iodev_set_format(struct cras_iodev *loopback_dev, 2066 const struct cras_audio_format *fmt) { 2067 } 2068 2069 int loopback_iodev_add_audio(struct cras_iodev *loopback_dev, 2070 const uint8_t *audio, 2071 unsigned int count) { 2072 return 0; 2073 } 2074 2075 int loopback_iodev_add_zeros(struct cras_iodev *dev, 2076 unsigned int count) { 2077 return 0; 2078 } 2079 2080 int cras_fmt_conversion_needed(const struct cras_audio_format *a, 2081 const struct cras_audio_format *b) 2082 { 2083 return cras_fmt_conversion_needed_return_val; 2084 } 2085 2086 void cras_audio_area_config_buf_pointers(struct cras_audio_area *area, 2087 const struct cras_audio_format *fmt, 2088 uint8_t *base_buffer) { 2089 unsigned int i; 2090 const int sample_size = snd_pcm_format_physical_width(fmt->format) / 8; 2091 2092 /* TODO(dgreid) - assuming interleaved audio here for now. */ 2093 for (i = 0 ; i < area->num_channels; i++) { 2094 area->channels[i].step_bytes = cras_get_format_bytes(fmt); 2095 area->channels[i].buf = base_buffer + i * sample_size; 2096 } 2097 } 2098 2099 void cras_audio_area_copy(const struct cras_audio_area *dst, 2100 unsigned int dst_offset, unsigned int dst_format_bytes, 2101 const struct cras_audio_area *src, unsigned int src_index) 2102 { 2103 unsigned count, i; 2104 int16_t *dchan, *schan; 2105 2106 if (src_index == 0) 2107 memset(dst->channels[0].buf, 0, src->frames * dst_format_bytes); 2108 2109 dchan = (int16_t *)(dst->channels[0].buf + 2110 dst_offset * dst->channels[0].step_bytes); 2111 schan = (int16_t *)src->channels[0].buf; 2112 count = src->frames * src->num_channels; 2113 for (i = 0; i < count; i++) { 2114 int32_t sum; 2115 sum = *dchan + *schan; 2116 if (sum > 0x7fff) 2117 sum = 0x7fff; 2118 else if (sum < -0x8000) 2119 sum = -0x8000; 2120 *dchan = sum; 2121 dchan++; 2122 schan++; 2123 } 2124 } 2125 2126 // Override select so it can be stubbed. 2127 int select(int nfds, 2128 fd_set *readfds, 2129 fd_set *writefds, 2130 fd_set *exceptfds, 2131 struct timeval *timeout) { 2132 select_max_fd = nfds; 2133 select_timeval.tv_sec = timeout->tv_sec; 2134 select_timeval.tv_usec = timeout->tv_usec; 2135 select_in_fds = *readfds; 2136 *readfds = select_out_fds; 2137 if (select_write_ptr) 2138 *select_write_ptr = select_write_value; 2139 return select_return_value; 2140 } 2141 2142 int clock_gettime(clockid_t clk_id, struct timespec *tp) { 2143 *tp = time_now; 2144 return 0; 2145 } 2146 2147 struct dev_stream *dev_stream_create(struct cras_rstream *stream, 2148 const struct cras_audio_format *fmt) { 2149 struct dev_stream *out = static_cast<dev_stream*>(calloc(1, sizeof(*out))); 2150 out->stream = stream; 2151 2152 return out; 2153 } 2154 2155 void dev_stream_destroy(struct dev_stream *dev_stream) { 2156 free(dev_stream); 2157 } 2158 2159 void dev_stream_capture(struct dev_stream *dev_stream, 2160 const struct cras_audio_area *area, 2161 unsigned int dev_index) 2162 { 2163 dev_stream_capture_call.dev_stream = dev_stream; 2164 dev_stream_capture_call.area = area; 2165 dev_stream_capture_call.dev_index = dev_index; 2166 dev_stream_capture_call.num_called++; 2167 } 2168 2169 int dev_stream_playback_frames(const struct dev_stream *dev_stream) { 2170 struct cras_audio_shm *shm; 2171 int frames; 2172 2173 shm = cras_rstream_output_shm(dev_stream->stream); 2174 2175 frames = cras_shm_get_frames(shm); 2176 if (frames < 0) 2177 return frames; 2178 2179 if (!dev_stream->conv) 2180 return frames; 2181 2182 return cras_fmt_conv_in_frames_to_out(dev_stream->conv, frames); 2183 } 2184 2185 unsigned int dev_stream_capture_avail(const struct dev_stream *dev_stream) 2186 { 2187 struct cras_audio_shm *shm; 2188 struct cras_rstream *rstream = dev_stream->stream; 2189 unsigned int cb_threshold = cras_rstream_get_cb_threshold(rstream); 2190 unsigned int frames_avail; 2191 2192 shm = cras_rstream_input_shm(rstream); 2193 2194 cras_shm_get_writeable_frames(shm, cb_threshold, &frames_avail); 2195 2196 return frames_avail; 2197 } 2198 2199 int dev_stream_capture_sleep_frames(struct dev_stream *dev_stream, 2200 unsigned int written) { 2201 cap_sleep_frames_call.dev_stream = dev_stream; 2202 cap_sleep_frames_call.written = written; 2203 cap_sleep_frames_call.num_called++; 2204 return 0; 2205 } 2206 2207 int dev_stream_request_playback_samples(struct dev_stream *dev_stream) 2208 { 2209 struct cras_rstream *rstream = dev_stream->stream; 2210 2211 dev_stream_request_playback_samples_called++; 2212 2213 cras_shm_set_callback_pending(cras_rstream_output_shm(rstream), 1); 2214 return 0; 2215 } 2216 2217 size_t cras_fmt_conv_in_frames_to_out(struct cras_fmt_conv *conv, 2218 size_t in_frames) 2219 { 2220 return in_frames; 2221 } 2222 2223 size_t cras_fmt_conv_out_frames_to_in(struct cras_fmt_conv *conv, 2224 size_t out_frames) 2225 { 2226 return out_frames; 2227 } 2228 2229 } // extern "C" 2230 2231 int main(int argc, char **argv) { 2232 ::testing::InitGoogleTest(&argc, argv); 2233 return RUN_ALL_TESTS(); 2234 } 2235