iodev_unittest.cc revision 4f86aad321250a527075476dac94241c24817521
1// Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <stdio.h> 6#include <gtest/gtest.h> 7 8extern "C" { 9#include "cras_iodev.h" 10#include "cras_rstream.h" 11#include "dev_stream.h" 12#include "utlist.h" 13#include "cras_audio_area.h" 14#include "audio_thread_log.h" 15 16// Mock software volume scalers. 17float softvol_scalers[101]; 18} 19 20#define BUFFER_SIZE 8192 21 22static int cras_iodev_list_disable_dev_called; 23static int select_node_called; 24static enum CRAS_STREAM_DIRECTION select_node_direction; 25static cras_node_id_t select_node_id; 26static struct cras_ionode *node_selected; 27static size_t notify_nodes_changed_called; 28static size_t notify_active_node_changed_called; 29static size_t notify_node_volume_called; 30static size_t notify_node_capture_gain_called; 31static int dsp_context_new_sample_rate; 32static const char *dsp_context_new_purpose; 33static int dsp_context_free_called; 34static int update_channel_layout_called; 35static int update_channel_layout_return_val; 36static int set_swap_mode_for_node_called; 37static int set_swap_mode_for_node_enable; 38static int notify_node_left_right_swapped_called; 39static int cras_audio_format_set_channel_layout_called; 40static unsigned int cras_system_get_volume_return; 41static int cras_dsp_get_pipeline_called; 42static int cras_dsp_get_pipeline_ret; 43static int cras_dsp_put_pipeline_called; 44static int cras_dsp_pipeline_get_source_buffer_called; 45static int cras_dsp_pipeline_get_sink_buffer_called; 46static float cras_dsp_pipeline_source_buffer[2][DSP_BUFFER_SIZE]; 47static float cras_dsp_pipeline_sink_buffer[2][DSP_BUFFER_SIZE]; 48static int cras_dsp_pipeline_get_delay_called; 49static int cras_dsp_pipeline_apply_called; 50static int cras_dsp_pipeline_apply_sample_count; 51static unsigned int cras_mix_mute_count; 52static unsigned int cras_dsp_num_input_channels_return; 53static unsigned int cras_dsp_num_output_channels_return; 54struct cras_dsp_context *cras_dsp_context_new_return; 55static unsigned int rate_estimator_add_frames_num_frames; 56static unsigned int rate_estimator_add_frames_called; 57static int cras_system_get_mute_return; 58static snd_pcm_format_t cras_scale_buffer_fmt; 59static float cras_scale_buffer_scaler; 60static unsigned int pre_dsp_hook_called; 61static const uint8_t *pre_dsp_hook_frames; 62static void *pre_dsp_hook_cb_data; 63static unsigned int post_dsp_hook_called; 64static const uint8_t *post_dsp_hook_frames; 65static void *post_dsp_hook_cb_data; 66static int iodev_buffer_size; 67static long cras_system_get_capture_gain_ret_value; 68static uint8_t audio_buffer[BUFFER_SIZE]; 69static struct cras_audio_area *audio_area; 70static unsigned int put_buffer_nframes; 71static int output_should_wake_ret; 72static int no_stream_called; 73static int no_stream_enable; 74// This will be used extensively in cras_iodev. 75struct audio_thread_event_log *atlog; 76static unsigned int simple_no_stream_called; 77static int simple_no_stream_enable; 78static int dev_stream_playback_frames_ret; 79static int get_num_underruns_ret; 80 81 82// Iodev callback 83int update_channel_layout(struct cras_iodev *iodev) { 84 update_channel_layout_called = 1; 85 return update_channel_layout_return_val; 86} 87 88// Iodev callback 89int set_swap_mode_for_node(struct cras_iodev *iodev, struct cras_ionode *node, 90 int enable) 91{ 92 set_swap_mode_for_node_called++; 93 set_swap_mode_for_node_enable = enable; 94 return 0; 95} 96 97void ResetStubData() { 98 cras_iodev_list_disable_dev_called = 0; 99 select_node_called = 0; 100 notify_nodes_changed_called = 0; 101 notify_active_node_changed_called = 0; 102 notify_node_volume_called = 0; 103 notify_node_capture_gain_called = 0; 104 dsp_context_new_sample_rate = 0; 105 dsp_context_new_purpose = NULL; 106 dsp_context_free_called = 0; 107 set_swap_mode_for_node_called = 0; 108 set_swap_mode_for_node_enable = 0; 109 notify_node_left_right_swapped_called = 0; 110 cras_audio_format_set_channel_layout_called = 0; 111 cras_dsp_get_pipeline_called = 0; 112 cras_dsp_get_pipeline_ret = 0; 113 cras_dsp_put_pipeline_called = 0; 114 cras_dsp_pipeline_get_source_buffer_called = 0; 115 cras_dsp_pipeline_get_sink_buffer_called = 0; 116 memset(&cras_dsp_pipeline_source_buffer, 0, 117 sizeof(cras_dsp_pipeline_source_buffer)); 118 memset(&cras_dsp_pipeline_sink_buffer, 0, 119 sizeof(cras_dsp_pipeline_sink_buffer)); 120 cras_dsp_pipeline_get_delay_called = 0; 121 cras_dsp_pipeline_apply_called = 0; 122 cras_dsp_pipeline_apply_sample_count = 0; 123 cras_dsp_num_input_channels_return = 2; 124 cras_dsp_num_output_channels_return = 2; 125 cras_dsp_context_new_return = NULL; 126 rate_estimator_add_frames_num_frames = 0; 127 rate_estimator_add_frames_called = 0; 128 cras_system_get_mute_return = 0; 129 cras_mix_mute_count = 0; 130 pre_dsp_hook_called = 0; 131 pre_dsp_hook_frames = NULL; 132 post_dsp_hook_called = 0; 133 post_dsp_hook_frames = NULL; 134 iodev_buffer_size = 0; 135 cras_system_get_capture_gain_ret_value = 0; 136 // Assume there is some data in audio buffer. 137 memset(audio_buffer, 0xff, sizeof(audio_buffer)); 138 if (audio_area) { 139 free(audio_area); 140 audio_area = NULL; 141 } 142 put_buffer_nframes = 0; 143 output_should_wake_ret= 0; 144 no_stream_called = 0; 145 no_stream_enable = 0; 146 simple_no_stream_called = 0; 147 simple_no_stream_enable = 0; 148 dev_stream_playback_frames_ret = 0; 149 if (!atlog) 150 atlog = audio_thread_event_log_init(); 151 get_num_underruns_ret = 0; 152} 153 154namespace { 155 156// Test fill_time_from_frames 157TEST(IoDevTestSuite, FillTimeFromFramesNormal) { 158 struct timespec ts; 159 160 cras_iodev_fill_time_from_frames(12000, 48000, &ts); 161 EXPECT_EQ(0, ts.tv_sec); 162 EXPECT_GE(ts.tv_nsec, 249900000); 163 EXPECT_LE(ts.tv_nsec, 250100000); 164} 165 166TEST(IoDevTestSuite, FillTimeFromFramesLong) { 167 struct timespec ts; 168 169 cras_iodev_fill_time_from_frames(120000 - 12000, 48000, &ts); 170 EXPECT_EQ(2, ts.tv_sec); 171 EXPECT_GE(ts.tv_nsec, 249900000); 172 EXPECT_LE(ts.tv_nsec, 250100000); 173} 174 175TEST(IoDevTestSuite, FillTimeFromFramesShort) { 176 struct timespec ts; 177 178 cras_iodev_fill_time_from_frames(12000 - 12000, 48000, &ts); 179 EXPECT_EQ(0, ts.tv_sec); 180 EXPECT_EQ(0, ts.tv_nsec); 181} 182 183class IoDevSetFormatTestSuite : public testing::Test { 184 protected: 185 virtual void SetUp() { 186 ResetStubData(); 187 sample_rates_[0] = 44100; 188 sample_rates_[1] = 48000; 189 sample_rates_[2] = 0; 190 191 channel_counts_[0] = 2; 192 channel_counts_[1] = 0; 193 channel_counts_[2] = 0; 194 195 pcm_formats_[0] = SND_PCM_FORMAT_S16_LE; 196 pcm_formats_[1] = SND_PCM_FORMAT_S32_LE; 197 pcm_formats_[2] = static_cast<snd_pcm_format_t>(0); 198 199 update_channel_layout_called = 0; 200 update_channel_layout_return_val = 0; 201 202 memset(&iodev_, 0, sizeof(iodev_)); 203 iodev_.update_channel_layout = update_channel_layout; 204 iodev_.supported_rates = sample_rates_; 205 iodev_.supported_channel_counts = channel_counts_; 206 iodev_.supported_formats = pcm_formats_; 207 iodev_.dsp_context = NULL; 208 209 cras_audio_format_set_channel_layout_called = 0; 210 } 211 212 virtual void TearDown() { 213 cras_iodev_free_format(&iodev_); 214 } 215 216 struct cras_iodev iodev_; 217 size_t sample_rates_[3]; 218 size_t channel_counts_[3]; 219 snd_pcm_format_t pcm_formats_[3]; 220}; 221 222TEST_F(IoDevSetFormatTestSuite, SupportedFormatSecondary) { 223 struct cras_audio_format fmt; 224 int rc; 225 226 fmt.format = SND_PCM_FORMAT_S16_LE; 227 fmt.frame_rate = 48000; 228 fmt.num_channels = 2; 229 iodev_.direction = CRAS_STREAM_OUTPUT; 230 ResetStubData(); 231 rc = cras_iodev_set_format(&iodev_, &fmt); 232 EXPECT_EQ(0, rc); 233 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 234 EXPECT_EQ(48000, iodev_.ext_format->frame_rate); 235 EXPECT_EQ(2, iodev_.ext_format->num_channels); 236 EXPECT_EQ(dsp_context_new_sample_rate, 48000); 237 EXPECT_STREQ(dsp_context_new_purpose, "playback"); 238} 239 240TEST_F(IoDevSetFormatTestSuite, SupportedFormat32bit) { 241 struct cras_audio_format fmt; 242 int rc; 243 244 fmt.format = SND_PCM_FORMAT_S32_LE; 245 fmt.frame_rate = 48000; 246 fmt.num_channels = 2; 247 iodev_.direction = CRAS_STREAM_OUTPUT; 248 ResetStubData(); 249 rc = cras_iodev_set_format(&iodev_, &fmt); 250 EXPECT_EQ(0, rc); 251 EXPECT_EQ(SND_PCM_FORMAT_S32_LE, iodev_.ext_format->format); 252 EXPECT_EQ(48000, iodev_.ext_format->frame_rate); 253 EXPECT_EQ(2, iodev_.ext_format->num_channels); 254 EXPECT_EQ(dsp_context_new_sample_rate, 48000); 255 EXPECT_STREQ(dsp_context_new_purpose, "playback"); 256} 257 258TEST_F(IoDevSetFormatTestSuite, SupportedFormatPrimary) { 259 struct cras_audio_format fmt; 260 int rc; 261 262 fmt.format = SND_PCM_FORMAT_S16_LE; 263 fmt.frame_rate = 44100; 264 fmt.num_channels = 2; 265 iodev_.direction = CRAS_STREAM_INPUT; 266 ResetStubData(); 267 rc = cras_iodev_set_format(&iodev_, &fmt); 268 EXPECT_EQ(0, rc); 269 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 270 EXPECT_EQ(44100, iodev_.ext_format->frame_rate); 271 EXPECT_EQ(2, iodev_.ext_format->num_channels); 272 EXPECT_EQ(dsp_context_new_sample_rate, 44100); 273 EXPECT_STREQ(dsp_context_new_purpose, "capture"); 274} 275 276TEST_F(IoDevSetFormatTestSuite, SupportedFormatDivisor) { 277 struct cras_audio_format fmt; 278 int rc; 279 280 fmt.format = SND_PCM_FORMAT_S16_LE; 281 fmt.frame_rate = 96000; 282 fmt.num_channels = 2; 283 rc = cras_iodev_set_format(&iodev_, &fmt); 284 EXPECT_EQ(0, rc); 285 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 286 EXPECT_EQ(48000, iodev_.ext_format->frame_rate); 287 EXPECT_EQ(2, iodev_.ext_format->num_channels); 288} 289 290TEST_F(IoDevSetFormatTestSuite, Supported96k) { 291 struct cras_audio_format fmt; 292 int rc; 293 294 sample_rates_[0] = 48000; 295 sample_rates_[1] = 96000; 296 sample_rates_[2] = 0; 297 298 fmt.format = SND_PCM_FORMAT_S16_LE; 299 fmt.frame_rate = 96000; 300 fmt.num_channels = 2; 301 rc = cras_iodev_set_format(&iodev_, &fmt); 302 EXPECT_EQ(0, rc); 303 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 304 EXPECT_EQ(96000, iodev_.ext_format->frame_rate); 305 EXPECT_EQ(2, iodev_.ext_format->num_channels); 306} 307 308TEST_F(IoDevSetFormatTestSuite, LimitLowRate) { 309 struct cras_audio_format fmt; 310 int rc; 311 312 sample_rates_[0] = 48000; 313 sample_rates_[1] = 8000; 314 sample_rates_[2] = 0; 315 316 fmt.format = SND_PCM_FORMAT_S16_LE; 317 fmt.frame_rate = 8000; 318 fmt.num_channels = 2; 319 rc = cras_iodev_set_format(&iodev_, &fmt); 320 EXPECT_EQ(0, rc); 321 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 322 EXPECT_EQ(48000, iodev_.ext_format->frame_rate); 323 EXPECT_EQ(2, iodev_.ext_format->num_channels); 324} 325 326TEST_F(IoDevSetFormatTestSuite, UnsupportedChannelCount) { 327 struct cras_audio_format fmt; 328 int rc; 329 330 fmt.format = SND_PCM_FORMAT_S16_LE; 331 fmt.frame_rate = 96000; 332 fmt.num_channels = 1; 333 rc = cras_iodev_set_format(&iodev_, &fmt); 334 EXPECT_EQ(0, rc); 335 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 336 EXPECT_EQ(48000, iodev_.ext_format->frame_rate); 337 EXPECT_EQ(2, iodev_.ext_format->num_channels); 338} 339 340TEST_F(IoDevSetFormatTestSuite, SupportedFormatFallbackDefault) { 341 struct cras_audio_format fmt; 342 int rc; 343 344 fmt.format = SND_PCM_FORMAT_S16_LE; 345 fmt.frame_rate = 96008; 346 fmt.num_channels = 2; 347 rc = cras_iodev_set_format(&iodev_, &fmt); 348 EXPECT_EQ(0, rc); 349 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 350 EXPECT_EQ(44100, iodev_.ext_format->frame_rate); 351 EXPECT_EQ(2, iodev_.ext_format->num_channels); 352} 353 354TEST_F(IoDevSetFormatTestSuite, OutputDSPChannleReduction) { 355 struct cras_audio_format fmt; 356 int rc; 357 358 fmt.format = SND_PCM_FORMAT_S16_LE; 359 fmt.frame_rate = 48000; 360 fmt.num_channels = 2; 361 362 iodev_.direction = CRAS_STREAM_OUTPUT; 363 iodev_.supported_channel_counts[0] = 1; 364 iodev_.supported_channel_counts[1] = 0; 365 cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf00); 366 cras_dsp_get_pipeline_ret = 0xf01; 367 cras_dsp_num_input_channels_return = 2; 368 cras_dsp_num_output_channels_return = 1; 369 rc = cras_iodev_set_format(&iodev_, &fmt); 370 EXPECT_EQ(0, rc); 371 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 372 EXPECT_EQ(48000, iodev_.ext_format->frame_rate); 373 EXPECT_EQ(2, iodev_.ext_format->num_channels); 374} 375 376TEST_F(IoDevSetFormatTestSuite, InputDSPChannleReduction) { 377 struct cras_audio_format fmt; 378 int rc; 379 380 fmt.format = SND_PCM_FORMAT_S16_LE; 381 fmt.frame_rate = 48000; 382 fmt.num_channels = 2; 383 384 iodev_.direction = CRAS_STREAM_INPUT; 385 iodev_.supported_channel_counts[0] = 10; 386 iodev_.supported_channel_counts[1] = 0; 387 cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf00); 388 cras_dsp_get_pipeline_ret = 0xf01; 389 cras_dsp_num_input_channels_return = 10; 390 cras_dsp_num_output_channels_return = 2; 391 rc = cras_iodev_set_format(&iodev_, &fmt); 392 EXPECT_EQ(0, rc); 393 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 394 EXPECT_EQ(48000, iodev_.ext_format->frame_rate); 395 EXPECT_EQ(2, iodev_.ext_format->num_channels); 396} 397 398TEST_F(IoDevSetFormatTestSuite, UpdateChannelLayoutSuccess) { 399 struct cras_audio_format fmt; 400 int rc; 401 402 fmt.format = SND_PCM_FORMAT_S16_LE; 403 fmt.frame_rate = 48000; 404 fmt.num_channels = 6; 405 406 iodev_.supported_channel_counts[0] = 6; 407 iodev_.supported_channel_counts[1] = 2; 408 409 rc = cras_iodev_set_format(&iodev_, &fmt); 410 EXPECT_EQ(0, rc); 411 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 412 EXPECT_EQ(48000, iodev_.ext_format->frame_rate); 413 EXPECT_EQ(6, iodev_.ext_format->num_channels); 414} 415 416TEST_F(IoDevSetFormatTestSuite, UpdateChannelLayoutFail) { 417 static const int8_t stereo_layout[] = 418 {0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; 419 struct cras_audio_format fmt; 420 int rc, i; 421 422 fmt.format = SND_PCM_FORMAT_S16_LE; 423 fmt.frame_rate = 48000; 424 fmt.num_channels = 2; 425 426 cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf0f); 427 428 update_channel_layout_return_val = -1; 429 iodev_.supported_channel_counts[0] = 6; 430 iodev_.supported_channel_counts[1] = 2; 431 432 rc = cras_iodev_set_format(&iodev_, &fmt); 433 EXPECT_EQ(0, rc); 434 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 435 EXPECT_EQ(48000, iodev_.ext_format->frame_rate); 436 EXPECT_EQ(2, iodev_.ext_format->num_channels); 437 EXPECT_EQ(2, cras_audio_format_set_channel_layout_called); 438 EXPECT_EQ(0, dsp_context_free_called); 439 for (i = 0; i < CRAS_CH_MAX; i++) 440 EXPECT_EQ(iodev_.format->channel_layout[i], stereo_layout[i]); 441} 442 443TEST_F(IoDevSetFormatTestSuite, UpdateChannelLayoutFail6ch) { 444 static const int8_t default_6ch_layout[] = 445 {0, 1, 2, 3, 4, 5, -1, -1, -1, -1, -1}; 446 struct cras_audio_format fmt; 447 int rc, i; 448 449 fmt.format = SND_PCM_FORMAT_S16_LE; 450 fmt.frame_rate = 48000; 451 fmt.num_channels = 6; 452 453 cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf0f); 454 455 update_channel_layout_return_val = -1; 456 iodev_.supported_channel_counts[0] = 6; 457 iodev_.supported_channel_counts[1] = 2; 458 459 rc = cras_iodev_set_format(&iodev_, &fmt); 460 EXPECT_EQ(0, rc); 461 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format); 462 EXPECT_EQ(48000, iodev_.ext_format->frame_rate); 463 EXPECT_EQ(6, iodev_.ext_format->num_channels); 464 EXPECT_EQ(2, cras_audio_format_set_channel_layout_called); 465 EXPECT_EQ(0, dsp_context_free_called); 466 for (i = 0; i < CRAS_CH_MAX; i++) 467 EXPECT_EQ(iodev_.format->channel_layout[i], default_6ch_layout[i]); 468} 469 470// Put buffer tests 471 472static int get_buffer(cras_iodev* iodev, struct cras_audio_area** area, 473 unsigned int* num) { 474 size_t sz = sizeof(*audio_area) + sizeof(struct cras_channel_area) * 2; 475 476 audio_area = (cras_audio_area*)calloc(1, sz); 477 audio_area->frames = *num; 478 audio_area->num_channels = 2; 479 audio_area->channels[0].buf = audio_buffer; 480 channel_area_set_channel(&audio_area->channels[0], CRAS_CH_FL); 481 audio_area->channels[0].step_bytes = 4; 482 audio_area->channels[1].buf = audio_buffer + 2; 483 channel_area_set_channel(&audio_area->channels[1], CRAS_CH_FR); 484 audio_area->channels[1].step_bytes = 4; 485 486 *area = audio_area; 487 return 0; 488} 489 490static int put_buffer(struct cras_iodev *iodev, unsigned int nframes) 491{ 492 put_buffer_nframes = nframes; 493 if (audio_area) { 494 free(audio_area); 495 audio_area = NULL; 496 } 497 return 0; 498} 499 500static int no_stream(struct cras_iodev *odev, int enable) 501{ 502 no_stream_called++; 503 no_stream_enable = enable; 504 // Use default no stream playback to test default behavior. 505 return cras_iodev_default_no_stream_playback(odev, enable); 506} 507 508static int output_should_wake(const struct cras_iodev *odev) 509{ 510 return output_should_wake_ret; 511} 512 513static int pre_dsp_hook(const uint8_t *frames, unsigned int nframes, 514 const struct cras_audio_format *fmt, void *cb_data) 515{ 516 pre_dsp_hook_called++; 517 pre_dsp_hook_frames = frames; 518 pre_dsp_hook_cb_data = cb_data; 519 return 0; 520} 521 522static int post_dsp_hook(const uint8_t *frames, unsigned int nframes, 523 const struct cras_audio_format *fmt, void *cb_data) 524{ 525 post_dsp_hook_called++; 526 post_dsp_hook_frames = frames; 527 post_dsp_hook_cb_data = cb_data; 528 return 0; 529} 530 531TEST(IoDevPutOutputBuffer, SystemMuted) { 532 struct cras_audio_format fmt; 533 struct cras_iodev iodev; 534 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44); 535 int rc; 536 537 ResetStubData(); 538 memset(&iodev, 0, sizeof(iodev)); 539 cras_system_get_mute_return = 1; 540 541 fmt.format = SND_PCM_FORMAT_S16_LE; 542 fmt.frame_rate = 48000; 543 fmt.num_channels = 2; 544 iodev.format = &fmt; 545 iodev.put_buffer = put_buffer; 546 547 rc = cras_iodev_put_output_buffer(&iodev, frames, 20); 548 EXPECT_EQ(0, rc); 549 EXPECT_EQ(20, cras_mix_mute_count); 550 EXPECT_EQ(20, put_buffer_nframes); 551 EXPECT_EQ(20, rate_estimator_add_frames_num_frames); 552} 553 554TEST(IoDevPutOutputBuffer, NoDSP) { 555 struct cras_audio_format fmt; 556 struct cras_iodev iodev; 557 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44); 558 int rc; 559 560 ResetStubData(); 561 memset(&iodev, 0, sizeof(iodev)); 562 563 fmt.format = SND_PCM_FORMAT_S16_LE; 564 fmt.frame_rate = 48000; 565 fmt.num_channels = 2; 566 iodev.format = &fmt; 567 iodev.put_buffer = put_buffer; 568 569 rc = cras_iodev_put_output_buffer(&iodev, frames, 22); 570 EXPECT_EQ(0, rc); 571 EXPECT_EQ(0, cras_mix_mute_count); 572 EXPECT_EQ(22, put_buffer_nframes); 573 EXPECT_EQ(22, rate_estimator_add_frames_num_frames); 574} 575 576TEST(IoDevPutOutputBuffer, DSP) { 577 struct cras_audio_format fmt; 578 struct cras_iodev iodev; 579 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44); 580 int rc; 581 582 ResetStubData(); 583 memset(&iodev, 0, sizeof(iodev)); 584 iodev.dsp_context = reinterpret_cast<cras_dsp_context*>(0x15); 585 cras_dsp_get_pipeline_ret = 0x25; 586 587 fmt.format = SND_PCM_FORMAT_S16_LE; 588 fmt.frame_rate = 48000; 589 fmt.num_channels = 2; 590 iodev.format = &fmt; 591 iodev.put_buffer = put_buffer; 592 cras_iodev_register_pre_dsp_hook(&iodev, pre_dsp_hook, (void *)0x1234); 593 cras_iodev_register_post_dsp_hook(&iodev, post_dsp_hook, (void *)0x5678); 594 595 rc = cras_iodev_put_output_buffer(&iodev, frames, 32); 596 EXPECT_EQ(0, rc); 597 EXPECT_EQ(0, cras_mix_mute_count); 598 EXPECT_EQ(1, pre_dsp_hook_called); 599 EXPECT_EQ(frames, pre_dsp_hook_frames); 600 EXPECT_EQ((void *)0x1234, pre_dsp_hook_cb_data); 601 EXPECT_EQ(1, post_dsp_hook_called); 602 EXPECT_EQ((void *)0x5678, post_dsp_hook_cb_data); 603 EXPECT_EQ(32, put_buffer_nframes); 604 EXPECT_EQ(32, rate_estimator_add_frames_num_frames); 605 EXPECT_EQ(32, cras_dsp_pipeline_apply_sample_count); 606 EXPECT_EQ(cras_dsp_get_pipeline_called, cras_dsp_put_pipeline_called); 607} 608 609TEST(IoDevPutOutputBuffer, SoftVol) { 610 struct cras_audio_format fmt; 611 struct cras_iodev iodev; 612 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44); 613 int rc; 614 615 ResetStubData(); 616 memset(&iodev, 0, sizeof(iodev)); 617 iodev.software_volume_needed = 1; 618 619 fmt.format = SND_PCM_FORMAT_S16_LE; 620 fmt.frame_rate = 48000; 621 fmt.num_channels = 2; 622 iodev.format = &fmt; 623 iodev.put_buffer = put_buffer; 624 625 cras_system_get_volume_return = 13; 626 softvol_scalers[13] = 0.435; 627 628 rc = cras_iodev_put_output_buffer(&iodev, frames, 53); 629 EXPECT_EQ(0, rc); 630 EXPECT_EQ(0, cras_mix_mute_count); 631 EXPECT_EQ(53, put_buffer_nframes); 632 EXPECT_EQ(53, rate_estimator_add_frames_num_frames); 633 EXPECT_EQ(softvol_scalers[13], cras_scale_buffer_scaler); 634 EXPECT_EQ(SND_PCM_FORMAT_S16_LE, cras_scale_buffer_fmt); 635} 636 637TEST(IoDevPutOutputBuffer, Scale32Bit) { 638 struct cras_audio_format fmt; 639 struct cras_iodev iodev; 640 uint8_t *frames = reinterpret_cast<uint8_t*>(0x44); 641 int rc; 642 643 ResetStubData(); 644 memset(&iodev, 0, sizeof(iodev)); 645 iodev.software_volume_needed = 1; 646 647 cras_system_get_volume_return = 13; 648 softvol_scalers[13] = 0.435; 649 650 fmt.format = SND_PCM_FORMAT_S32_LE; 651 fmt.frame_rate = 48000; 652 fmt.num_channels = 2; 653 iodev.format = &fmt; 654 iodev.put_buffer = put_buffer; 655 656 rc = cras_iodev_put_output_buffer(&iodev, frames, 53); 657 EXPECT_EQ(0, rc); 658 EXPECT_EQ(0, cras_mix_mute_count); 659 EXPECT_EQ(53, put_buffer_nframes); 660 EXPECT_EQ(53, rate_estimator_add_frames_num_frames); 661 EXPECT_EQ(SND_PCM_FORMAT_S32_LE, cras_scale_buffer_fmt); 662} 663 664// frames queued/avail tests 665 666static unsigned fr_queued = 0; 667 668static int frames_queued(const struct cras_iodev *iodev, 669 struct timespec *tstamp) 670{ 671 clock_gettime(CLOCK_MONOTONIC_RAW, tstamp); 672 return fr_queued; 673} 674 675TEST(IoDevQueuedBuffer, ZeroMinBufferLevel) { 676 struct cras_iodev iodev; 677 struct timespec tstamp; 678 int rc; 679 680 ResetStubData(); 681 memset(&iodev, 0, sizeof(iodev)); 682 iodev.direction = CRAS_STREAM_OUTPUT; 683 iodev.frames_queued = frames_queued; 684 iodev.min_buffer_level = 0; 685 iodev.buffer_size = 200; 686 fr_queued = 50; 687 688 rc = cras_iodev_frames_queued(&iodev, &tstamp); 689 EXPECT_EQ(50, rc); 690 rc = cras_iodev_buffer_avail(&iodev, rc); 691 EXPECT_EQ(150, rc); 692} 693 694TEST(IoDevQueuedBuffer, NonZeroMinBufferLevel) { 695 struct cras_iodev iodev; 696 struct timespec hw_tstamp; 697 int rc; 698 699 ResetStubData(); 700 memset(&iodev, 0, sizeof(iodev)); 701 iodev.direction = CRAS_STREAM_OUTPUT; 702 iodev.frames_queued = frames_queued; 703 iodev.min_buffer_level = 100; 704 iodev.buffer_size = 200; 705 fr_queued = 180; 706 707 rc = cras_iodev_frames_queued(&iodev, &hw_tstamp); 708 EXPECT_EQ(80, rc); 709 rc = cras_iodev_buffer_avail(&iodev, rc); 710 EXPECT_EQ(20, rc); 711 712 /* When fr_queued < min_buffer_level*/ 713 fr_queued = 80; 714 rc = cras_iodev_frames_queued(&iodev, &hw_tstamp); 715 EXPECT_EQ(0, rc); 716 rc = cras_iodev_buffer_avail(&iodev, rc); 717 EXPECT_EQ(100, rc); 718} 719 720static void update_active_node(struct cras_iodev *iodev, 721 unsigned node_idx, 722 unsigned dev_enabled) 723{ 724} 725 726static void dev_set_volume(struct cras_iodev *iodev) 727{ 728} 729 730static void dev_set_capture_gain(struct cras_iodev *iodev) 731{ 732} 733 734TEST(IoNodePlug, PlugUnplugNode) { 735 struct cras_iodev iodev; 736 struct cras_ionode ionode, ionode2; 737 738 memset(&iodev, 0, sizeof(iodev)); 739 memset(&ionode, 0, sizeof(ionode)); 740 memset(&ionode2, 0, sizeof(ionode2)); 741 iodev.direction = CRAS_STREAM_INPUT; 742 iodev.update_active_node = update_active_node; 743 ionode.dev = &iodev; 744 cras_iodev_add_node(&iodev, &ionode); 745 ionode2.dev = &iodev; 746 cras_iodev_add_node(&iodev, &ionode2); 747 cras_iodev_set_active_node(&iodev, &ionode); 748 ResetStubData(); 749 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_PLUGGED, 1); 750 EXPECT_EQ(0, cras_iodev_list_disable_dev_called); 751 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_PLUGGED, 0); 752 EXPECT_EQ(1, cras_iodev_list_disable_dev_called); 753 754 /* Unplug non-active node shouldn't disable iodev. */ 755 cras_iodev_set_node_attr(&ionode2, IONODE_ATTR_PLUGGED, 1); 756 EXPECT_EQ(1, cras_iodev_list_disable_dev_called); 757 cras_iodev_set_node_attr(&ionode2, IONODE_ATTR_PLUGGED, 0); 758 EXPECT_EQ(1, cras_iodev_list_disable_dev_called); 759} 760 761TEST(IoDev, AddRemoveNode) { 762 struct cras_iodev iodev; 763 struct cras_ionode ionode; 764 765 memset(&iodev, 0, sizeof(iodev)); 766 memset(&ionode, 0, sizeof(ionode)); 767 ResetStubData(); 768 EXPECT_EQ(0, notify_nodes_changed_called); 769 cras_iodev_add_node(&iodev, &ionode); 770 EXPECT_EQ(1, notify_nodes_changed_called); 771 cras_iodev_rm_node(&iodev, &ionode); 772 EXPECT_EQ(2, notify_nodes_changed_called); 773} 774 775TEST(IoDev, SetActiveNode) { 776 struct cras_iodev iodev; 777 struct cras_ionode ionode; 778 779 memset(&iodev, 0, sizeof(iodev)); 780 memset(&ionode, 0, sizeof(ionode)); 781 ResetStubData(); 782 EXPECT_EQ(0, notify_active_node_changed_called); 783 cras_iodev_set_active_node(&iodev, &ionode); 784 EXPECT_EQ(1, notify_active_node_changed_called); 785} 786 787TEST(IoDev, SetNodeVolume) { 788 struct cras_iodev iodev; 789 struct cras_ionode ionode; 790 791 memset(&iodev, 0, sizeof(iodev)); 792 memset(&ionode, 0, sizeof(ionode)); 793 iodev.set_volume = dev_set_volume; 794 iodev.set_capture_gain = dev_set_capture_gain; 795 ionode.dev = &iodev; 796 ResetStubData(); 797 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_VOLUME, 10); 798 EXPECT_EQ(1, notify_node_volume_called); 799 iodev.direction = CRAS_STREAM_INPUT; 800 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_CAPTURE_GAIN, 10); 801 EXPECT_EQ(1, notify_node_capture_gain_called); 802} 803 804TEST(IoDev, SetNodeSwapLeftRight) { 805 struct cras_iodev iodev; 806 struct cras_ionode ionode; 807 808 memset(&iodev, 0, sizeof(iodev)); 809 memset(&ionode, 0, sizeof(ionode)); 810 iodev.set_swap_mode_for_node = set_swap_mode_for_node; 811 ionode.dev = &iodev; 812 ResetStubData(); 813 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_SWAP_LEFT_RIGHT, 1); 814 EXPECT_EQ(1, set_swap_mode_for_node_called); 815 EXPECT_EQ(1, set_swap_mode_for_node_enable); 816 EXPECT_EQ(1, ionode.left_right_swapped); 817 EXPECT_EQ(1, notify_node_left_right_swapped_called); 818 cras_iodev_set_node_attr(&ionode, IONODE_ATTR_SWAP_LEFT_RIGHT, 0); 819 EXPECT_EQ(2, set_swap_mode_for_node_called); 820 EXPECT_EQ(0, set_swap_mode_for_node_enable); 821 EXPECT_EQ(0, ionode.left_right_swapped); 822 EXPECT_EQ(2, notify_node_left_right_swapped_called); 823} 824 825 826// Test software volume changes for default output. 827TEST(IoDev, SoftwareVolume) { 828 struct cras_iodev iodev; 829 struct cras_ionode ionode; 830 831 memset(&iodev, 0, sizeof(iodev)); 832 memset(&ionode, 0, sizeof(ionode)); 833 ResetStubData(); 834 835 iodev.nodes = &ionode; 836 iodev.active_node = &ionode; 837 iodev.active_node->dev = &iodev; 838 839 iodev.active_node->volume = 100; 840 iodev.software_volume_needed = 0; 841 842 843 softvol_scalers[80] = 0.5; 844 softvol_scalers[70] = 0.3; 845 846 // Check that system volume changes software volume if needed. 847 cras_system_get_volume_return = 80; 848 // system_volume - 100 + node_volume = 80 - 100 + 100 = 80 849 EXPECT_FLOAT_EQ(0.5, cras_iodev_get_software_volume_scaler(&iodev)); 850 851 // Check that node volume changes software volume if needed. 852 iodev.active_node->volume = 90; 853 // system_volume - 100 + node_volume = 80 - 100 + 90 = 70 854 EXPECT_FLOAT_EQ(0.3, cras_iodev_get_software_volume_scaler(&iodev)); 855} 856 857// Test software gain scaler. 858TEST(IoDev, SoftwareGain) { 859 struct cras_iodev iodev; 860 struct cras_ionode ionode; 861 862 memset(&iodev, 0, sizeof(iodev)); 863 memset(&ionode, 0, sizeof(ionode)); 864 ResetStubData(); 865 866 iodev.nodes = &ionode; 867 iodev.active_node = &ionode; 868 iodev.active_node->dev = &iodev; 869 870 ionode.capture_gain= 400; 871 ionode.software_volume_needed = 1; 872 ionode.max_software_gain = 3000; 873 874 // Check that system volume changes software volume if needed. 875 cras_system_get_capture_gain_ret_value = 2000; 876 // system_gain + node_gain = 2000 + 400 = 2400 877 // 2400 dBm is 15.848931 878 EXPECT_FLOAT_EQ(15.848931, cras_iodev_get_software_gain_scaler(&iodev)); 879 EXPECT_FLOAT_EQ(3000, cras_iodev_maximum_software_gain(&iodev)); 880 881 // Software gain scaler should be 1.0 if software gain is not needed. 882 ionode.software_volume_needed = 0; 883 EXPECT_FLOAT_EQ(1.0, cras_iodev_get_software_gain_scaler(&iodev)); 884 EXPECT_FLOAT_EQ(0, cras_iodev_maximum_software_gain(&iodev)); 885} 886 887// This get_buffer implementation set returned frames larger than requested 888// frames. 889static int bad_get_buffer(struct cras_iodev *iodev, 890 struct cras_audio_area **area, 891 unsigned *frames) 892{ 893 *frames = *frames + 1; 894 return 0; 895} 896 897// Check that if get_buffer implementation returns invalid frames, 898// cras_iodev_get_output_buffer and cras_iodev_get_input_buffer can return 899// error. 900TEST(IoDev, GetBufferInvalidFrames) { 901 struct cras_iodev iodev; 902 struct cras_audio_area **area = NULL; 903 unsigned int frames = 512; 904 struct cras_audio_format fmt; 905 906 // Format is used in cras_iodev_get_input_buffer; 907 fmt.format = SND_PCM_FORMAT_S16_LE; 908 fmt.frame_rate = 48000; 909 fmt.num_channels = 2; 910 911 memset(&iodev, 0, sizeof(iodev)); 912 913 ResetStubData(); 914 915 iodev.format = &fmt; 916 iodev.get_buffer = bad_get_buffer; 917 918 EXPECT_EQ(-EINVAL, cras_iodev_get_output_buffer(&iodev, area, &frames)); 919 EXPECT_EQ(-EINVAL, cras_iodev_get_input_buffer(&iodev, area, &frames)); 920} 921 922static int open_dev(struct cras_iodev *iodev) { 923 iodev->buffer_size = iodev_buffer_size; 924 return 0; 925} 926 927TEST(IoDev, OpenOutputDeviceNoStart) { 928 struct cras_iodev iodev; 929 930 memset(&iodev, 0, sizeof(iodev)); 931 iodev.open_dev = open_dev; 932 iodev.direction = CRAS_STREAM_OUTPUT; 933 ResetStubData(); 934 935 iodev.state = CRAS_IODEV_STATE_CLOSE; 936 937 iodev_buffer_size = 1024; 938 cras_iodev_open(&iodev, 240); 939 EXPECT_EQ(0, iodev.max_cb_level); 940 EXPECT_EQ(240, iodev.min_cb_level); 941 942 // Test that state is no stream run when there is no start ops. 943 EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state); 944} 945 946int fake_start(const struct cras_iodev *iodev) { 947 return 0; 948} 949 950TEST(IoDev, OpenOutputDeviceWithStart) { 951 struct cras_iodev iodev; 952 953 memset(&iodev, 0, sizeof(iodev)); 954 iodev.open_dev = open_dev; 955 iodev.direction = CRAS_STREAM_OUTPUT; 956 ResetStubData(); 957 958 iodev.state = CRAS_IODEV_STATE_CLOSE; 959 iodev.start = fake_start; 960 961 iodev_buffer_size = 1024; 962 cras_iodev_open(&iodev, 240); 963 EXPECT_EQ(0, iodev.max_cb_level); 964 EXPECT_EQ(240, iodev.min_cb_level); 965 966 // Test that state is no stream run when there is start ops. 967 EXPECT_EQ(CRAS_IODEV_STATE_OPEN, iodev.state); 968} 969 970TEST(IoDev, OpenInputDeviceNoStart) { 971 struct cras_iodev iodev; 972 973 memset(&iodev, 0, sizeof(iodev)); 974 iodev.open_dev = open_dev; 975 iodev.direction = CRAS_STREAM_INPUT; 976 ResetStubData(); 977 978 iodev.state = CRAS_IODEV_STATE_CLOSE; 979 980 iodev_buffer_size = 1024; 981 cras_iodev_open(&iodev, 240); 982 EXPECT_EQ(0, iodev.max_cb_level); 983 EXPECT_EQ(240, iodev.min_cb_level); 984 985 // Test that state is normal run when there is start ops. 986 EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state); 987} 988 989TEST(IoDev, OpenInputDeviceWithStart) { 990 struct cras_iodev iodev; 991 992 memset(&iodev, 0, sizeof(iodev)); 993 iodev.open_dev = open_dev; 994 iodev.direction = CRAS_STREAM_INPUT; 995 ResetStubData(); 996 997 iodev.state = CRAS_IODEV_STATE_CLOSE; 998 iodev.start = fake_start; 999 1000 iodev_buffer_size = 1024; 1001 cras_iodev_open(&iodev, 240); 1002 EXPECT_EQ(0, iodev.max_cb_level); 1003 EXPECT_EQ(240, iodev.min_cb_level); 1004 1005 // Test that state is normal run even if there is start ops. 1006 EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state); 1007} 1008 1009static int simple_no_stream(struct cras_iodev *dev, int enable) 1010{ 1011 simple_no_stream_enable = enable; 1012 simple_no_stream_called++; 1013 return 0; 1014} 1015 1016TEST(IoDev, AddRmStream) { 1017 struct cras_iodev iodev; 1018 struct cras_rstream rstream1, rstream2; 1019 struct dev_stream stream1, stream2; 1020 1021 memset(&iodev, 0, sizeof(iodev)); 1022 iodev.open_dev = open_dev; 1023 iodev.no_stream = simple_no_stream; 1024 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN; 1025 rstream1.cb_threshold = 800; 1026 stream1.stream = &rstream1; 1027 rstream2.cb_threshold = 400; 1028 stream2.stream = &rstream2; 1029 ResetStubData(); 1030 1031 iodev_buffer_size = 1024; 1032 cras_iodev_open(&iodev, rstream1.cb_threshold); 1033 EXPECT_EQ(0, iodev.max_cb_level); 1034 EXPECT_EQ(512, iodev.min_cb_level); 1035 1036 /* min_cb_level should not exceed half the buffer size. */ 1037 cras_iodev_add_stream(&iodev, &stream1); 1038 EXPECT_EQ(800, iodev.max_cb_level); 1039 EXPECT_EQ(512, iodev.min_cb_level); 1040 1041 cras_iodev_add_stream(&iodev, &stream2); 1042 EXPECT_EQ(800, iodev.max_cb_level); 1043 EXPECT_EQ(400, iodev.min_cb_level); 1044 1045 cras_iodev_rm_stream(&iodev, &rstream1); 1046 EXPECT_EQ(400, iodev.max_cb_level); 1047 EXPECT_EQ(400, iodev.min_cb_level); 1048 EXPECT_EQ(0, simple_no_stream_called); 1049 1050 /* When all streams are removed, keep the last min_cb_level for draining. */ 1051 cras_iodev_rm_stream(&iodev, &rstream2); 1052 EXPECT_EQ(0, iodev.max_cb_level); 1053 EXPECT_EQ(400, iodev.min_cb_level); 1054} 1055 1056TEST(IoDev, FillZeros) { 1057 struct cras_iodev iodev; 1058 struct cras_audio_format fmt; 1059 unsigned int frames = 50; 1060 int16_t *zeros; 1061 int rc; 1062 1063 ResetStubData(); 1064 1065 memset(&iodev, 0, sizeof(iodev)); 1066 fmt.format = SND_PCM_FORMAT_S16_LE; 1067 fmt.frame_rate = 48000; 1068 fmt.num_channels = 2; 1069 iodev.ext_format = &fmt; 1070 iodev.get_buffer = get_buffer; 1071 iodev.put_buffer = put_buffer; 1072 1073 iodev.direction = CRAS_STREAM_INPUT; 1074 rc = cras_iodev_fill_odev_zeros(&iodev, frames); 1075 EXPECT_EQ(-EINVAL, rc); 1076 1077 iodev.direction = CRAS_STREAM_OUTPUT; 1078 rc = cras_iodev_fill_odev_zeros(&iodev, frames); 1079 1080 EXPECT_EQ(0, rc); 1081 EXPECT_EQ(frames, put_buffer_nframes); 1082 zeros = (int16_t *)calloc(frames * 2, sizeof(*zeros)); 1083 rc = memcmp(audio_buffer, zeros, frames * 2 * 2); 1084 free(zeros); 1085 EXPECT_EQ(0, rc); 1086} 1087 1088TEST(IoDev, DefaultNoStreamPlaybackRunning) { 1089 struct cras_iodev iodev; 1090 struct cras_audio_format fmt; 1091 unsigned int hw_level = 50; 1092 unsigned int min_cb_level = 240; 1093 unsigned int zeros_to_fill; 1094 int16_t *zeros; 1095 int rc; 1096 1097 memset(&iodev, 0, sizeof(iodev)); 1098 1099 fmt.format = SND_PCM_FORMAT_S16_LE; 1100 fmt.frame_rate = 48000; 1101 fmt.num_channels = 2; 1102 iodev.ext_format = &fmt; 1103 iodev.min_cb_level = min_cb_level; 1104 iodev.get_buffer = get_buffer; 1105 iodev.put_buffer = put_buffer; 1106 iodev.frames_queued = frames_queued; 1107 iodev.min_buffer_level = 0; 1108 iodev.direction = CRAS_STREAM_OUTPUT; 1109 iodev.buffer_size = BUFFER_SIZE; 1110 iodev.no_stream = no_stream; 1111 1112 ResetStubData(); 1113 1114 // Device is running. hw_level is less than target. 1115 // Need to fill to callback level * 2; 1116 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN; 1117 fr_queued = hw_level; 1118 zeros_to_fill = min_cb_level * 2 - hw_level; 1119 1120 rc = cras_iodev_default_no_stream_playback(&iodev, 1); 1121 1122 EXPECT_EQ(0, rc); 1123 EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state); 1124 EXPECT_EQ(zeros_to_fill, put_buffer_nframes); 1125 zeros = (int16_t *)calloc(zeros_to_fill * 2, sizeof(*zeros)); 1126 EXPECT_EQ(0, memcmp(audio_buffer, zeros, zeros_to_fill * 2 * 2)); 1127 free(zeros); 1128 1129 ResetStubData(); 1130 1131 // Device is running. hw_level is not less than target. 1132 // No need to fill zeros. 1133 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN; 1134 hw_level = min_cb_level * 2; 1135 fr_queued = hw_level; 1136 zeros_to_fill = 0; 1137 1138 rc = cras_iodev_default_no_stream_playback(&iodev, 1); 1139 EXPECT_EQ(0, rc); 1140 EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state); 1141 EXPECT_EQ(zeros_to_fill, put_buffer_nframes); 1142} 1143 1144TEST(IoDev, PrepareOutputBeforeWriteSamples) { 1145 struct cras_iodev iodev; 1146 struct cras_audio_format fmt; 1147 unsigned int min_cb_level = 240; 1148 int rc; 1149 struct cras_rstream rstream1; 1150 struct dev_stream stream1; 1151 struct cras_iodev_info info; 1152 1153 ResetStubData(); 1154 1155 rstream1.cb_threshold = min_cb_level; 1156 stream1.stream = &rstream1; 1157 1158 memset(&iodev, 0, sizeof(iodev)); 1159 1160 fmt.format = SND_PCM_FORMAT_S16_LE; 1161 fmt.frame_rate = 48000; 1162 fmt.num_channels = 2; 1163 iodev.ext_format = &fmt; 1164 iodev.min_cb_level = min_cb_level; 1165 iodev.get_buffer = get_buffer; 1166 iodev.put_buffer = put_buffer; 1167 iodev.frames_queued = frames_queued; 1168 iodev.min_buffer_level = 0; 1169 iodev.direction = CRAS_STREAM_OUTPUT; 1170 iodev.buffer_size = BUFFER_SIZE; 1171 iodev.no_stream = no_stream; 1172 iodev.open_dev = open_dev; 1173 iodev.start = fake_start; 1174 iodev.info = info; 1175 iodev_buffer_size = BUFFER_SIZE; 1176 1177 // Open device. 1178 cras_iodev_open(&iodev, rstream1.cb_threshold); 1179 1180 // Add one stream to device. 1181 cras_iodev_add_stream(&iodev, &stream1); 1182 1183 // Case 1: Assume device is not started yet. 1184 iodev.state = CRAS_IODEV_STATE_OPEN; 1185 // Assume sample is not ready yet. 1186 dev_stream_playback_frames_ret = 0; 1187 1188 rc = cras_iodev_prepare_output_before_write_samples(&iodev); 1189 1190 EXPECT_EQ(0, rc); 1191 // Device should remain in open state. 1192 EXPECT_EQ(CRAS_IODEV_STATE_OPEN, iodev.state); 1193 EXPECT_EQ(0, no_stream_called); 1194 1195 // Assume now sample is ready. 1196 dev_stream_playback_frames_ret = 100; 1197 1198 rc = cras_iodev_prepare_output_before_write_samples(&iodev); 1199 1200 EXPECT_EQ(0, rc); 1201 // Device should enter normal run state. 1202 EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state); 1203 EXPECT_EQ(0, no_stream_called); 1204 // Need to fill 1 callback level of zeros; 1205 EXPECT_EQ(min_cb_level, put_buffer_nframes); 1206 1207 ResetStubData(); 1208 1209 // Case 2: Assume device is started and is in no stream state. 1210 iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN; 1211 // Sample is not ready yet. 1212 dev_stream_playback_frames_ret = 0; 1213 1214 rc = cras_iodev_prepare_output_before_write_samples(&iodev); 1215 1216 EXPECT_EQ(0, rc); 1217 // Device should remain in no_stream state. 1218 EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state); 1219 // Device in no_stream state should call no_stream ops once. 1220 EXPECT_EQ(1, no_stream_called); 1221 EXPECT_EQ(1, no_stream_enable); 1222 1223 // Assume now sample is ready. 1224 dev_stream_playback_frames_ret = 100; 1225 1226 rc = cras_iodev_prepare_output_before_write_samples(&iodev); 1227 1228 EXPECT_EQ(0, rc); 1229 // Device should enter normal run state. 1230 EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state); 1231 // Device should call no_stream ops with enable=0 to leave no stream state. 1232 EXPECT_EQ(2, no_stream_called); 1233 EXPECT_EQ(0, no_stream_enable); 1234 1235 ResetStubData(); 1236 1237 // Case 3: Assume device is started and is in normal run state. 1238 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN; 1239 1240 rc = cras_iodev_prepare_output_before_write_samples(&iodev); 1241 1242 EXPECT_EQ(0, rc); 1243 // Device should remain in normal run state. 1244 EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state); 1245 // Device in no_stream state should call no_stream ops once. 1246 EXPECT_EQ(0, no_stream_called); 1247 1248 ResetStubData(); 1249} 1250 1251TEST(IoDev, OutputDeviceShouldWake) { 1252 struct cras_iodev iodev; 1253 int rc; 1254 1255 memset(&iodev, 0, sizeof(iodev)); 1256 1257 ResetStubData(); 1258 1259 // Device is not running. No need to wake for this device. 1260 iodev.state = CRAS_IODEV_STATE_OPEN; 1261 rc = cras_iodev_odev_should_wake(&iodev); 1262 EXPECT_EQ(0, rc); 1263 1264 // Device is running. Need to wake for this device. 1265 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN; 1266 rc = cras_iodev_odev_should_wake(&iodev); 1267 EXPECT_EQ(1, rc); 1268 1269 // Device is running. Device has output_should_wake ops. 1270 iodev.output_should_wake = output_should_wake; 1271 output_should_wake_ret = 0; 1272 rc = cras_iodev_odev_should_wake(&iodev); 1273 EXPECT_EQ(0, rc); 1274 1275 // Device is running. Device has output_should_wake ops. 1276 output_should_wake_ret = 1; 1277 rc = cras_iodev_odev_should_wake(&iodev); 1278 EXPECT_EQ(1, rc); 1279 1280 // Ignore input device. 1281 iodev.direction = CRAS_STREAM_INPUT; 1282 rc = cras_iodev_odev_should_wake(&iodev); 1283 EXPECT_EQ(0, rc); 1284} 1285 1286TEST(IoDev, FramesToPlayInSleep) { 1287 struct cras_iodev iodev; 1288 unsigned int min_cb_level = 240, hw_level; 1289 unsigned int got_hw_level, got_frames; 1290 struct timespec hw_tstamp; 1291 1292 memset(&iodev, 0, sizeof(iodev)); 1293 iodev.frames_queued = frames_queued; 1294 iodev.min_buffer_level = 0; 1295 iodev.direction = CRAS_STREAM_OUTPUT; 1296 iodev.buffer_size = BUFFER_SIZE; 1297 iodev.min_cb_level = min_cb_level; 1298 1299 ResetStubData(); 1300 1301 // Device is running. There is at least one stream for this device. 1302 // hw_level is greater than min_cb_level. 1303 iodev.state = CRAS_IODEV_STATE_NORMAL_RUN; 1304 hw_level = min_cb_level + 50; 1305 fr_queued = hw_level; 1306 iodev.streams = reinterpret_cast<struct dev_stream *>(0x1); 1307 1308 got_frames = cras_iodev_frames_to_play_in_sleep( 1309 &iodev, &got_hw_level, &hw_tstamp); 1310 EXPECT_EQ(hw_level, got_hw_level); 1311 EXPECT_EQ(hw_level, got_frames); 1312 1313 // Device is running. There is no stream for this device. 1314 // hw_level is greater than min_cb_level. 1315 iodev.streams = NULL; 1316 1317 got_frames = cras_iodev_frames_to_play_in_sleep( 1318 &iodev, &got_hw_level, &hw_tstamp); 1319 EXPECT_EQ(hw_level, got_hw_level); 1320 EXPECT_EQ(hw_level - min_cb_level, got_frames); 1321 1322 // Device is running. There is no stream for this device. 1323 // hw_level is less than min_cb_level. 1324 iodev.streams = NULL; 1325 hw_level = min_cb_level - 50; 1326 fr_queued = hw_level; 1327 1328 got_frames = cras_iodev_frames_to_play_in_sleep( 1329 &iodev, &got_hw_level, &hw_tstamp); 1330 EXPECT_EQ(hw_level, got_hw_level); 1331 EXPECT_EQ(0, got_frames); 1332} 1333 1334static unsigned int get_num_underruns(const struct cras_iodev *iodev) { 1335 return get_num_underruns_ret; 1336} 1337 1338TEST(IoDev, GetNumUnderruns) { 1339 struct cras_iodev iodev; 1340 memset(&iodev, 0, sizeof(iodev)); 1341 1342 EXPECT_EQ(0, cras_iodev_get_num_underruns(&iodev)); 1343 1344 iodev.get_num_underruns = get_num_underruns; 1345 get_num_underruns_ret = 10; 1346 EXPECT_EQ(10, cras_iodev_get_num_underruns(&iodev)); 1347} 1348 1349extern "C" { 1350 1351// From libpthread. 1352int pthread_create(pthread_t *thread, const pthread_attr_t *attr, 1353 void *(*start_routine)(void*), void *arg) { 1354 return 0; 1355} 1356 1357int pthread_join(pthread_t thread, void **value_ptr) { 1358 return 0; 1359} 1360 1361// From audio_thread 1362struct cras_fmt_conv *audio_thread_get_global_remix_converter() 1363{ 1364 return NULL; 1365} 1366 1367// Fromt fmt_conv 1368void cras_channel_remix_convert(struct cras_fmt_conv *conv, 1369 uint8_t *in_buf, 1370 size_t frames) 1371{ 1372} 1373 1374// From buffer_share 1375struct buffer_share *buffer_share_create(unsigned int buf_sz) { 1376 return NULL; 1377} 1378 1379void buffer_share_destroy(struct buffer_share *mix) 1380{ 1381} 1382 1383int buffer_share_offset_update(struct buffer_share *mix, unsigned int id, 1384 unsigned int frames) { 1385 return 0; 1386} 1387 1388unsigned int buffer_share_get_new_write_point(struct buffer_share *mix) { 1389 return 0; 1390} 1391 1392int buffer_share_add_id(struct buffer_share *mix, unsigned int id) { 1393 return 0; 1394} 1395 1396int buffer_share_rm_id(struct buffer_share *mix, unsigned int id) { 1397 return 0; 1398} 1399 1400unsigned int buffer_share_id_offset(const struct buffer_share *mix, 1401 unsigned int id) 1402{ 1403 return 0; 1404} 1405 1406// From cras_system_state. 1407void cras_system_state_stream_added(enum CRAS_STREAM_DIRECTION direction) { 1408} 1409 1410void cras_system_state_stream_removed(enum CRAS_STREAM_DIRECTION direction) { 1411} 1412 1413// From cras_dsp 1414struct cras_dsp_context *cras_dsp_context_new(int sample_rate, 1415 const char *purpose) 1416{ 1417 dsp_context_new_sample_rate = sample_rate; 1418 dsp_context_new_purpose = purpose; 1419 return cras_dsp_context_new_return; 1420} 1421 1422void cras_dsp_context_free(struct cras_dsp_context *ctx) 1423{ 1424 dsp_context_free_called++; 1425} 1426 1427void cras_dsp_load_pipeline(struct cras_dsp_context *ctx) 1428{ 1429} 1430 1431void cras_dsp_set_variable_string(struct cras_dsp_context *ctx, const char *key, 1432 const char *value) 1433{ 1434} 1435 1436struct pipeline *cras_dsp_get_pipeline(struct cras_dsp_context *ctx) 1437{ 1438 cras_dsp_get_pipeline_called++; 1439 return reinterpret_cast<struct pipeline *>(cras_dsp_get_pipeline_ret); 1440} 1441 1442void cras_dsp_put_pipeline(struct cras_dsp_context *ctx) 1443{ 1444 cras_dsp_put_pipeline_called++; 1445} 1446 1447float *cras_dsp_pipeline_get_source_buffer(struct pipeline *pipeline, 1448 int index) 1449{ 1450 cras_dsp_pipeline_get_source_buffer_called++; 1451 return cras_dsp_pipeline_source_buffer[index]; 1452} 1453 1454float *cras_dsp_pipeline_get_sink_buffer(struct pipeline *pipeline, int index) 1455{ 1456 cras_dsp_pipeline_get_sink_buffer_called++; 1457 return cras_dsp_pipeline_sink_buffer[index]; 1458} 1459 1460int cras_dsp_pipeline_get_delay(struct pipeline *pipeline) 1461{ 1462 cras_dsp_pipeline_get_delay_called++; 1463 return 0; 1464} 1465 1466void cras_dsp_pipeline_apply(struct pipeline *pipeline, 1467 uint8_t *buf, unsigned int frames) 1468{ 1469 cras_dsp_pipeline_apply_called++; 1470 cras_dsp_pipeline_apply_sample_count = frames; 1471} 1472 1473void cras_dsp_pipeline_add_statistic(struct pipeline *pipeline, 1474 const struct timespec *time_delta, 1475 int samples) 1476{ 1477} 1478 1479unsigned int cras_dsp_num_output_channels(const struct cras_dsp_context *ctx) 1480{ 1481 return cras_dsp_num_output_channels_return; 1482} 1483 1484unsigned int cras_dsp_num_input_channels(const struct cras_dsp_context *ctx) 1485{ 1486 return cras_dsp_num_input_channels_return; 1487} 1488 1489// From audio thread 1490int audio_thread_post_message(struct audio_thread *thread, 1491 struct audio_thread_msg *msg) { 1492 return 0; 1493} 1494 1495void cras_iodev_list_select_node(enum CRAS_STREAM_DIRECTION direction, 1496 cras_node_id_t node_id) 1497{ 1498 select_node_called++; 1499 select_node_direction = direction; 1500 select_node_id = node_id; 1501} 1502 1503int cras_iodev_list_node_selected(struct cras_ionode *node) 1504{ 1505 return node == node_selected; 1506} 1507 1508void cras_iodev_list_disable_dev(struct cras_iodev *dev) 1509{ 1510 cras_iodev_list_disable_dev_called++; 1511} 1512 1513void cras_iodev_list_notify_nodes_changed() 1514{ 1515 notify_nodes_changed_called++; 1516} 1517 1518void cras_iodev_list_notify_active_node_changed( 1519 enum CRAS_STREAM_DIRECTION direction) 1520{ 1521 notify_active_node_changed_called++; 1522} 1523 1524void cras_iodev_list_notify_node_volume(struct cras_ionode *node) 1525{ 1526 notify_node_volume_called++; 1527} 1528 1529void cras_iodev_list_notify_node_capture_gain(struct cras_ionode *node) 1530{ 1531 notify_node_capture_gain_called++; 1532} 1533 1534void cras_iodev_list_notify_node_left_right_swapped(struct cras_ionode *node) 1535{ 1536 notify_node_left_right_swapped_called++; 1537} 1538 1539struct cras_audio_area *cras_audio_area_create(int num_channels) { 1540 return NULL; 1541} 1542 1543void cras_audio_area_destroy(struct cras_audio_area *area) { 1544} 1545 1546void cras_audio_area_config_channels(struct cras_audio_area *area, 1547 const struct cras_audio_format *fmt) { 1548} 1549 1550int cras_audio_format_set_channel_layout(struct cras_audio_format *format, 1551 const int8_t layout[CRAS_CH_MAX]) 1552{ 1553 int i; 1554 cras_audio_format_set_channel_layout_called++; 1555 for (i = 0; i < CRAS_CH_MAX; i++) 1556 format->channel_layout[i] = layout[i]; 1557 return 0; 1558} 1559 1560float softvol_get_scaler(unsigned int volume_index) 1561{ 1562 return softvol_scalers[volume_index]; 1563} 1564 1565size_t cras_system_get_volume() { 1566 return cras_system_get_volume_return; 1567} 1568 1569long cras_system_get_capture_gain() { 1570 return cras_system_get_capture_gain_ret_value; 1571} 1572 1573int cras_system_get_mute() { 1574 return cras_system_get_mute_return; 1575} 1576 1577int cras_system_get_capture_mute() { 1578 return 0; 1579} 1580 1581void cras_scale_buffer(snd_pcm_format_t fmt, uint8_t *buffer, 1582 unsigned int count, float scaler) { 1583 cras_scale_buffer_fmt = fmt; 1584 cras_scale_buffer_scaler = scaler; 1585} 1586 1587size_t cras_mix_mute_buffer(uint8_t *dst, 1588 size_t frame_bytes, 1589 size_t count) { 1590 cras_mix_mute_count = count; 1591 return count; 1592} 1593 1594struct rate_estimator *rate_estimator_create(unsigned int rate, 1595 const struct timespec *window_size, 1596 double smooth_factor) { 1597 return NULL; 1598} 1599 1600void rate_estimator_destroy(struct rate_estimator *re) { 1601} 1602 1603void rate_estimator_add_frames(struct rate_estimator *re, int fr) { 1604 rate_estimator_add_frames_called++; 1605 rate_estimator_add_frames_num_frames = fr; 1606} 1607 1608int rate_estimator_check(struct rate_estimator *re, int level, 1609 struct timespec *now) { 1610 return 0; 1611} 1612 1613void rate_estimator_reset_rate(struct rate_estimator *re, unsigned int rate) { 1614} 1615 1616double rate_estimator_get_rate(struct rate_estimator *re) { 1617 return 0.0; 1618} 1619 1620unsigned int dev_stream_cb_threshold(const struct dev_stream *dev_stream) { 1621 if (dev_stream->stream) 1622 return dev_stream->stream->cb_threshold; 1623 return 0; 1624} 1625 1626int dev_stream_attached_devs(const struct dev_stream *dev_stream) { 1627 return 1; 1628} 1629 1630void dev_stream_update_frames(const struct dev_stream *dev_stream) { 1631} 1632 1633int dev_stream_playback_frames(const struct dev_stream *dev_stream) { 1634 return dev_stream_playback_frames_ret; 1635} 1636 1637} // extern "C" 1638} // namespace 1639 1640int main(int argc, char **argv) { 1641 ::testing::InitGoogleTest(&argc, argv); 1642 int rc = RUN_ALL_TESTS(); 1643 1644 audio_thread_event_log_deinit(atlog); 1645 return rc; 1646} 1647