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