summaryrefslogtreecommitdiff
path: root/cras/src/tests/dev_stream_unittest.cc
blob: 700376fb6c47facd43947fef1f55b68a038e1c91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
// Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <gtest/gtest.h>
#include <stdio.h>

extern "C" {
#include "audio_thread_log.h"
#include "byte_buffer.h"
#include "cras_audio_area.h"
#include "cras_rstream.h"
#include "cras_shm.h"
#include "cras_types.h"
#include "dev_stream.h"
}

namespace {

extern "C" {
struct audio_thread_event_log* atlog;
// For audio_thread_log.h use.
int atlog_rw_shm_fd;
int atlog_ro_shm_fd;
unsigned int max_frames_for_conversion(unsigned int stream_frames,
                                       unsigned int stream_rate,
                                       unsigned int device_rate);
};

static struct timespec clock_gettime_retspec;
static struct timespec cb_ts;

static const int kBufferFrames = 1024;
static const struct cras_audio_format fmt_s16le_44_1 = {
    SND_PCM_FORMAT_S16_LE,
    44100,
    2,
};
static const struct cras_audio_format fmt_s16le_48 = {
    SND_PCM_FORMAT_S16_LE,
    48000,
    2,
};
static const struct cras_audio_format fmt_s16le_48_mono = {
    SND_PCM_FORMAT_S16_LE,
    48000,
    1,
};
static const struct cras_audio_format fmt_s16le_8 = {
    SND_PCM_FORMAT_S16_LE,
    8000,
    2,
};

struct cras_audio_area_copy_call {
  const struct cras_audio_area* dst;
  unsigned int dst_offset;
  unsigned int dst_format_bytes;
  const struct cras_audio_area* src;
  unsigned int src_offset;
  float software_gain_scaler;
};

struct fmt_conv_call {
  struct cras_fmt_conv* conv;
  uint8_t* in_buf;
  uint8_t* out_buf;
  size_t in_frames;
  size_t out_frames;
};

struct mix_add_call {
  int16_t* dst;
  int16_t* src;
  unsigned int count;
  unsigned int index;
  int mute;
  float mix_vol;
};

struct rstream_get_readable_call {
  struct cras_rstream* rstream;
  unsigned int offset;
  unsigned int num_called;
};

static int config_format_converter_called;
static const struct cras_audio_format* config_format_converter_from_fmt;
static int config_format_converter_frames;
static struct cras_fmt_conv* config_format_converter_conv;
static struct cras_audio_format in_fmt;
static struct cras_audio_format out_fmt;
static struct cras_audio_area_copy_call copy_area_call;
static struct fmt_conv_call conv_frames_call;
static int cras_audio_area_create_num_channels_val;
static int cras_fmt_conversion_needed_val;
static int cras_fmt_conv_set_linear_resample_rates_called;
static float cras_fmt_conv_set_linear_resample_rates_from;
static float cras_fmt_conv_set_linear_resample_rates_to;

static unsigned int rstream_playable_frames_ret;
static struct mix_add_call mix_add_call;
static struct rstream_get_readable_call rstream_get_readable_call;
static unsigned int rstream_get_readable_num;
static uint8_t* rstream_get_readable_ptr;

static struct cras_audio_format* cras_rstream_post_processing_format_val;
static int cras_rstream_audio_ready_called;
static int cras_rstream_audio_ready_count;
static int cras_rstream_is_pending_reply_ret;
static int cras_rstream_flush_old_audio_messages_called;
static int cras_server_metrics_missed_cb_event_called;

static char* atlog_name;

class CreateSuite : public testing::Test {
 protected:
  virtual void SetUp() {
    in_fmt.format = SND_PCM_FORMAT_S16_LE;
    out_fmt.format = SND_PCM_FORMAT_S16_LE;
    in_fmt.num_channels = 2;
    out_fmt.num_channels = 2;

    SetupShm(&rstream_.shm);

    rstream_.stream_id = 0x10001;
    rstream_.buffer_frames = kBufferFrames;
    rstream_.cb_threshold = kBufferFrames / 2;
    rstream_.is_draining = 0;
    rstream_.stream_type = CRAS_STREAM_TYPE_DEFAULT;
    rstream_.direction = CRAS_STREAM_OUTPUT;
    rstream_.format.format = SND_PCM_FORMAT_S16_LE;
    rstream_.format.num_channels = 2;
    rstream_.format = fmt_s16le_44_1;
    rstream_.flags = 0;
    rstream_.num_missed_cb = 0;

    config_format_converter_from_fmt = NULL;
    config_format_converter_called = 0;
    cras_fmt_conversion_needed_val = 0;
    cras_fmt_conv_set_linear_resample_rates_called = 0;

    cras_rstream_audio_ready_called = 0;
    cras_rstream_audio_ready_count = 0;
    cras_rstream_is_pending_reply_ret = 0;
    cras_rstream_flush_old_audio_messages_called = 0;
    cras_server_metrics_missed_cb_event_called = 0;

    memset(&copy_area_call, 0xff, sizeof(copy_area_call));
    memset(&conv_frames_call, 0xff, sizeof(conv_frames_call));

    ASSERT_FALSE(asprintf(&atlog_name, "/ATlog-%d", getpid()) < 0);
    /* To avoid un-used variable warning. */
    atlog_rw_shm_fd = atlog_ro_shm_fd = -1;
    atlog = audio_thread_event_log_init(atlog_name);

    devstr.stream = &rstream_;
    devstr.conv = NULL;
    devstr.conv_buffer = NULL;
    devstr.conv_buffer_size_frames = 0;

    area = (struct cras_audio_area*)calloc(
        1, sizeof(*area) + 2 * sizeof(struct cras_channel_area));
    area->num_channels = 2;
    channel_area_set_channel(&area->channels[0], CRAS_CH_FL);
    channel_area_set_channel(&area->channels[1], CRAS_CH_FR);
    area->channels[0].step_bytes = 4;
    area->channels[0].buf = (uint8_t*)(cap_buf);
    area->channels[1].step_bytes = 4;
    area->channels[1].buf = (uint8_t*)(cap_buf + 1);
    area->frames = kBufferFrames;

    stream_area = (struct cras_audio_area*)calloc(
        1, sizeof(*area) + 2 * sizeof(struct cras_channel_area));
    stream_area->num_channels = 2;
    rstream_.audio_area = stream_area;
    int16_t* shm_samples = (int16_t*)rstream_.shm->samples;
    stream_area->channels[0].step_bytes = 4;
    stream_area->channels[0].buf = (uint8_t*)(shm_samples);
    stream_area->channels[1].step_bytes = 4;
    stream_area->channels[1].buf = (uint8_t*)(shm_samples + 1);
  }

  virtual void TearDown() {
    free(area);
    free(stream_area);
    free(rstream_.shm->header);
    free(rstream_.shm->samples);
    free(rstream_.shm);
    audio_thread_event_log_deinit(atlog, atlog_name);
    free(atlog_name);
  }

  void SetupShm(struct cras_audio_shm** shm_out) {
    int16_t* buf;
    struct cras_audio_shm* shm;
    uint32_t used_size;

    shm = static_cast<struct cras_audio_shm*>(
        calloc(1, sizeof(struct cras_audio_shm)));

    shm->header = static_cast<struct cras_audio_shm_header*>(
        calloc(1, sizeof(struct cras_audio_shm_header)));
    cras_shm_set_frame_bytes(shm, 4);
    used_size = kBufferFrames * cras_shm_frame_bytes(shm);
    cras_shm_set_used_size(shm, used_size);

    shm->samples = static_cast<uint8_t*>(
        calloc(1, cras_shm_calculate_samples_size(used_size)));
    shm->samples_info.length = cras_shm_calculate_samples_size(used_size);

    buf = (int16_t*)shm->samples;
    for (size_t i = 0; i < kBufferFrames * 2; i++)
      buf[i] = i;
    cras_shm_set_mute(shm, 0);
    cras_shm_set_volume_scaler(shm, 1.0);

    *shm_out = shm;
  }

  void SetUpFmtConv(unsigned int in_rate,
                    unsigned int out_rate,
                    unsigned int conv_buf_size) {
    in_fmt.frame_rate = in_rate;
    out_fmt.frame_rate = out_rate;
    cras_fmt_conversion_needed_val = 1;

    devstr.conv = (struct cras_fmt_conv*)0xdead;
    devstr.conv_buffer =
        (struct byte_buffer*)byte_buffer_create(conv_buf_size * 4);
    devstr.conv_buffer_size_frames = kBufferFrames * 2;

    devstr.conv_area = (struct cras_audio_area*)calloc(
        1, sizeof(*area) + 2 * sizeof(*area->channels));
    devstr.conv_area->num_channels = 2;
    devstr.conv_area->channels[0].step_bytes = 4;
    devstr.conv_area->channels[0].buf = (uint8_t*)(devstr.conv_buffer->bytes);
    devstr.conv_area->channels[1].step_bytes = 4;
    devstr.conv_area->channels[1].buf =
        (uint8_t*)(devstr.conv_buffer->bytes + 1);
  }

  struct dev_stream devstr;
  struct cras_audio_area* area;
  struct cras_audio_area* stream_area;
  int16_t cap_buf[kBufferFrames * 2];
  struct cras_rstream rstream_;
};

TEST_F(CreateSuite, CaptureNoSRC) {
  float software_gain_scaler = 10;

  dev_stream_capture(&devstr, area, 0, software_gain_scaler);

  EXPECT_EQ(stream_area, copy_area_call.dst);
  EXPECT_EQ(0, copy_area_call.dst_offset);
  EXPECT_EQ(4, copy_area_call.dst_format_bytes);
  EXPECT_EQ(area, copy_area_call.src);
  EXPECT_EQ(software_gain_scaler, copy_area_call.software_gain_scaler);
}

TEST_F(CreateSuite, CaptureSRCSmallConverterBuffer) {
  float software_gain_scaler = 10;
  unsigned int conv_buf_avail_at_input_rate;
  int nread;

  SetUpFmtConv(44100, 32000, kBufferFrames / 4);
  nread = dev_stream_capture(&devstr, area, 0, software_gain_scaler);

  // |nread| is bound by small converter buffer size (kBufferFrames / 4)
  conv_buf_avail_at_input_rate = cras_frames_at_rate(
      out_fmt.frame_rate, (kBufferFrames / 4), in_fmt.frame_rate);

  EXPECT_EQ(conv_buf_avail_at_input_rate, nread);
  EXPECT_EQ((struct cras_fmt_conv*)0xdead, conv_frames_call.conv);
  EXPECT_EQ((uint8_t*)cap_buf, conv_frames_call.in_buf);
  EXPECT_EQ(devstr.conv_buffer->bytes, conv_frames_call.out_buf);

  EXPECT_EQ(conv_buf_avail_at_input_rate, conv_frames_call.in_frames);

  // Expect number of output frames is limited by the size of converter buffer.
  EXPECT_EQ(kBufferFrames / 4, conv_frames_call.out_frames);

  EXPECT_EQ(stream_area, copy_area_call.dst);
  EXPECT_EQ(0, copy_area_call.dst_offset);
  EXPECT_EQ(4, copy_area_call.dst_format_bytes);
  EXPECT_EQ(devstr.conv_area, copy_area_call.src);
  EXPECT_EQ(software_gain_scaler, copy_area_call.software_gain_scaler);

  free(devstr.conv_area);
  byte_buffer_destroy(&devstr.conv_buffer);
}

TEST_F(CreateSuite, CaptureSRCLargeConverterBuffer) {
  float software_gain_scaler = 10;
  unsigned int stream_avail_at_input_rate;
  int nread;

  SetUpFmtConv(44100, 32000, kBufferFrames * 2);
  nread = dev_stream_capture(&devstr, area, 0, software_gain_scaler);

  // Available frames at stream side is bound by cb_threshold, which
  // equals to kBufferFrames / 2.
  stream_avail_at_input_rate = cras_frames_at_rate(
      out_fmt.frame_rate, (kBufferFrames / 2), in_fmt.frame_rate);

  EXPECT_EQ(stream_avail_at_input_rate, nread);
  EXPECT_EQ((struct cras_fmt_conv*)0xdead, conv_frames_call.conv);
  EXPECT_EQ((uint8_t*)cap_buf, conv_frames_call.in_buf);
  EXPECT_EQ(devstr.conv_buffer->bytes, conv_frames_call.out_buf);

  // Expect number of input frames is limited by |stream_avail_at_input_rate|
  // at format conversion.
  EXPECT_EQ(stream_avail_at_input_rate, conv_frames_call.in_frames);

  // Expect number of output frames is limited by the size of converter buffer.
  EXPECT_EQ(kBufferFrames * 2, conv_frames_call.out_frames);

  EXPECT_EQ(stream_area, copy_area_call.dst);
  EXPECT_EQ(0, copy_area_call.dst_offset);
  EXPECT_EQ(4, copy_area_call.dst_format_bytes);
  EXPECT_EQ(devstr.conv_area, copy_area_call.src);
  EXPECT_EQ(software_gain_scaler, copy_area_call.software_gain_scaler);

  free(devstr.conv_area);
  byte_buffer_destroy(&devstr.conv_buffer);
}

TEST_F(CreateSuite, CreateSRC44to48) {
  struct dev_stream* dev_stream;

  rstream_.format = fmt_s16le_44_1;
  in_fmt.frame_rate = 44100;   // Input to converter is stream rate.
  out_fmt.frame_rate = 48000;  // Output from converter is device rate.
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream =
      dev_stream_create(&rstream_, 0, &fmt_s16le_48, (void*)0x55, &cb_ts, NULL);
  EXPECT_EQ(1, config_format_converter_called);
  EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
  // Converter tmp and output buffers are large enough for device output.
  unsigned int device_frames =
      cras_frames_at_rate(in_fmt.frame_rate, kBufferFrames, out_fmt.frame_rate);
  EXPECT_LE(kBufferFrames, device_frames);  // Soundness check.
  EXPECT_LE(device_frames, config_format_converter_frames);
  EXPECT_LE(device_frames, dev_stream->conv_buffer_size_frames);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, CreateOutputWithSchedule) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;
  // init_cb_ts and non-null init_sleep_ts will be used.
  struct timespec init_cb_ts = {1, 2};
  struct timespec init_sleep_ts = {3, 4};

  rstream_.direction = CRAS_STREAM_OUTPUT;
  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_48, (void*)0x55,
                                 &init_cb_ts, &init_sleep_ts);

  EXPECT_EQ(init_cb_ts.tv_sec, rstream_.next_cb_ts.tv_sec);
  EXPECT_EQ(init_cb_ts.tv_nsec, rstream_.next_cb_ts.tv_nsec);
  EXPECT_EQ(init_sleep_ts.tv_sec, rstream_.sleep_interval_ts.tv_sec);
  EXPECT_EQ(init_sleep_ts.tv_nsec, rstream_.sleep_interval_ts.tv_nsec);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, CreateSRC44from48Input) {
  struct dev_stream* dev_stream;
  struct cras_audio_format processed_fmt = fmt_s16le_48;

  processed_fmt.num_channels = 1;
  rstream_.format = fmt_s16le_44_1;
  rstream_.direction = CRAS_STREAM_INPUT;
  in_fmt.frame_rate = 48000;   // Input to converter is device rate.
  out_fmt.frame_rate = 44100;  // Output from converter is stream rate.
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  cras_rstream_post_processing_format_val = &processed_fmt;
  dev_stream =
      dev_stream_create(&rstream_, 0, &fmt_s16le_48, (void*)0x55, &cb_ts, NULL);
  EXPECT_EQ(1, config_format_converter_called);
  EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
  // Converter tmp and output buffers are large enough for device input.
  unsigned int device_frames =
      cras_frames_at_rate(out_fmt.frame_rate, kBufferFrames, in_fmt.frame_rate);
  EXPECT_LE(kBufferFrames, device_frames);  // Soundness check.
  EXPECT_LE(device_frames, config_format_converter_frames);
  EXPECT_EQ(&processed_fmt, config_format_converter_from_fmt);
  EXPECT_LE(device_frames, dev_stream->conv_buffer_size_frames);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, CreateSRC48to44) {
  struct dev_stream* dev_stream;

  rstream_.format = fmt_s16le_48;
  in_fmt.frame_rate = 48000;   // Stream rate.
  out_fmt.frame_rate = 44100;  // Device rate.
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_44_1, (void*)0x55,
                                 &cb_ts, NULL);
  EXPECT_EQ(1, config_format_converter_called);
  EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
  // Converter tmp and output buffers are large enough for stream input.
  EXPECT_LE(kBufferFrames, config_format_converter_frames);
  EXPECT_LE(kBufferFrames, dev_stream->conv_buffer_size_frames);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, CreateSRC48from44Input) {
  struct dev_stream* dev_stream;

  rstream_.format = fmt_s16le_48;
  rstream_.direction = CRAS_STREAM_INPUT;
  in_fmt.frame_rate = 44100;   // Device rate.
  out_fmt.frame_rate = 48000;  // Stream rate.
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_44_1, (void*)0x55,
                                 &cb_ts, NULL);
  EXPECT_EQ(1, config_format_converter_called);
  EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
  // Converter tmp and output buffers are large enough for stream output.
  EXPECT_LE(kBufferFrames, config_format_converter_frames);
  EXPECT_LE(kBufferFrames, dev_stream->conv_buffer_size_frames);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, CreateSRC8to48) {
  struct dev_stream* dev_stream;

  rstream_.format = fmt_s16le_8;
  in_fmt.frame_rate = 8000;    // Stream rate.
  out_fmt.frame_rate = 48000;  // Device rate.
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream =
      dev_stream_create(&rstream_, 0, &fmt_s16le_48, (void*)0x55, &cb_ts, NULL);
  EXPECT_EQ(1, config_format_converter_called);
  EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
  // Converter tmp and output buffers are large enough for device output.
  unsigned int device_frames =
      cras_frames_at_rate(in_fmt.frame_rate, kBufferFrames, out_fmt.frame_rate);
  EXPECT_LE(kBufferFrames, device_frames);  // Soundness check.
  EXPECT_LE(device_frames, config_format_converter_frames);
  EXPECT_LE(device_frames, dev_stream->conv_buffer_size_frames);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, CreateSRC8from48Input) {
  struct dev_stream* dev_stream;

  rstream_.format = fmt_s16le_8;
  rstream_.direction = CRAS_STREAM_INPUT;
  in_fmt.frame_rate = 48000;  // Device rate.
  out_fmt.frame_rate = 8000;  // Stream rate.
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream =
      dev_stream_create(&rstream_, 0, &fmt_s16le_48, (void*)0x55, &cb_ts, NULL);
  EXPECT_EQ(1, config_format_converter_called);
  EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
  // Converter tmp and output buffers are large enough for device input.
  unsigned int device_frames =
      cras_frames_at_rate(out_fmt.frame_rate, kBufferFrames, in_fmt.frame_rate);
  EXPECT_LE(kBufferFrames, device_frames);  // Soundness check.
  EXPECT_LE(device_frames, config_format_converter_frames);
  EXPECT_LE(device_frames, dev_stream->conv_buffer_size_frames);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, CreateSRC48to8) {
  struct dev_stream* dev_stream;

  rstream_.format = fmt_s16le_48;
  in_fmt.frame_rate = 48000;  // Stream rate.
  out_fmt.frame_rate = 8000;  // Device rate.
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream =
      dev_stream_create(&rstream_, 0, &fmt_s16le_8, (void*)0x55, &cb_ts, NULL);
  EXPECT_EQ(1, config_format_converter_called);
  EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
  // Converter tmp and output buffers are large enough for stream input.
  EXPECT_LE(kBufferFrames, config_format_converter_frames);
  EXPECT_LE(kBufferFrames, dev_stream->conv_buffer_size_frames);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, CreateSRC48from8Input) {
  struct dev_stream* dev_stream;

  rstream_.format = fmt_s16le_48;
  rstream_.direction = CRAS_STREAM_INPUT;
  in_fmt.frame_rate = 8000;    // Device rate.
  out_fmt.frame_rate = 48000;  // Stream rate.
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream =
      dev_stream_create(&rstream_, 0, &fmt_s16le_8, (void*)0x55, &cb_ts, NULL);
  EXPECT_EQ(1, config_format_converter_called);
  EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
  // Converter tmp and output buffers are large enough for stream output.
  EXPECT_LE(kBufferFrames, config_format_converter_frames);
  EXPECT_LE(kBufferFrames, dev_stream->conv_buffer_size_frames);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, CreateSRC48MonoFrom44StereoInput) {
  struct dev_stream* dev_stream;

  rstream_.format = fmt_s16le_48_mono;
  rstream_.direction = CRAS_STREAM_INPUT;
  in_fmt.frame_rate = 44100;   // Device rate.
  out_fmt.frame_rate = 48000;  // Stream rate.
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_44_1, (void*)0x55,
                                 &cb_ts, NULL);
  EXPECT_EQ(1, config_format_converter_called);
  EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
  // Converter tmp and output buffers are large enough for stream output.
  EXPECT_LE(kBufferFrames, config_format_converter_frames);
  EXPECT_LE(kBufferFrames, dev_stream->conv_buffer_size_frames);
  EXPECT_EQ(dev_stream->conv_buffer_size_frames * 4,
            dev_stream->conv_buffer->max_size);
  EXPECT_EQ(2, cras_audio_area_create_num_channels_val);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, CaptureAvailConvBufHasSamples) {
  struct dev_stream* dev_stream;
  unsigned int avail;

  rstream_.format = fmt_s16le_48;
  rstream_.direction = CRAS_STREAM_INPUT;
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream = dev_stream_create(&rstream_, 0, &fmt_s16le_44_1, (void*)0x55,
                                 &cb_ts, NULL);
  EXPECT_EQ(1, config_format_converter_called);
  EXPECT_NE(static_cast<byte_buffer*>(NULL), dev_stream->conv_buffer);
  EXPECT_LE(
      cras_frames_at_rate(in_fmt.frame_rate, kBufferFrames, out_fmt.frame_rate),
      dev_stream->conv_buffer_size_frames);
  EXPECT_EQ(dev_stream->conv_buffer_size_frames * 4,
            dev_stream->conv_buffer->max_size);
  EXPECT_EQ(2, cras_audio_area_create_num_channels_val);

  buf_increment_write(dev_stream->conv_buffer, 50 * 4);
  avail = dev_stream_capture_avail(dev_stream);

  EXPECT_EQ(cras_frames_at_rate(48000, 512 - 50, 44100), avail);

  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, SetDevRateNotMainDev) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;

  rstream_.format = fmt_s16le_48;
  rstream_.direction = CRAS_STREAM_INPUT;
  rstream_.main_dev.dev_id = 4;
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
                                 (void*)0x55, &cb_ts, NULL);

  dev_stream_set_dev_rate(dev_stream, 44100, 1.01, 1.0, 0);
  EXPECT_EQ(1, cras_fmt_conv_set_linear_resample_rates_called);
  EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
  EXPECT_EQ(44541, cras_fmt_conv_set_linear_resample_rates_to);

  dev_stream_set_dev_rate(dev_stream, 44100, 1.01, 1.0, 1);
  EXPECT_EQ(2, cras_fmt_conv_set_linear_resample_rates_called);
  EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
  EXPECT_LE(44541, cras_fmt_conv_set_linear_resample_rates_to);

  dev_stream_set_dev_rate(dev_stream, 44100, 1.0, 1.01, -1);
  EXPECT_EQ(3, cras_fmt_conv_set_linear_resample_rates_called);
  EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
  EXPECT_GE(43663, cras_fmt_conv_set_linear_resample_rates_to);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, SetDevRateMainDev) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;
  unsigned int expected_ts_nsec;

  rstream_.format = fmt_s16le_48;
  rstream_.direction = CRAS_STREAM_INPUT;
  rstream_.main_dev.dev_id = dev_id;
  config_format_converter_conv = reinterpret_cast<struct cras_fmt_conv*>(0x33);
  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
                                 (void*)0x55, &cb_ts, NULL);

  dev_stream_set_dev_rate(dev_stream, 44100, 1.01, 1.0, 0);
  EXPECT_EQ(1, cras_fmt_conv_set_linear_resample_rates_called);
  EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
  EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_to);
  expected_ts_nsec = 1000000000.0 * kBufferFrames / 2.0 / 48000.0 / 1.01;
  EXPECT_EQ(0, rstream_.sleep_interval_ts.tv_sec);
  EXPECT_EQ(expected_ts_nsec, rstream_.sleep_interval_ts.tv_nsec);

  dev_stream_set_dev_rate(dev_stream, 44100, 1.01, 1.0, 1);
  EXPECT_EQ(2, cras_fmt_conv_set_linear_resample_rates_called);
  EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
  EXPECT_LE(44100, cras_fmt_conv_set_linear_resample_rates_to);
  expected_ts_nsec = 1000000000.0 * kBufferFrames / 2.0 / 48000.0 / 1.01;
  EXPECT_EQ(0, rstream_.sleep_interval_ts.tv_sec);
  EXPECT_EQ(expected_ts_nsec, rstream_.sleep_interval_ts.tv_nsec);

  dev_stream_set_dev_rate(dev_stream, 44100, 1.0, 1.33, -1);
  EXPECT_EQ(3, cras_fmt_conv_set_linear_resample_rates_called);
  EXPECT_EQ(44100, cras_fmt_conv_set_linear_resample_rates_from);
  EXPECT_GE(44100, cras_fmt_conv_set_linear_resample_rates_to);
  expected_ts_nsec = 1000000000.0 * kBufferFrames / 2.0 / 48000.0;
  EXPECT_EQ(0, rstream_.sleep_interval_ts.tv_sec);
  EXPECT_EQ(expected_ts_nsec, rstream_.sleep_interval_ts.tv_nsec);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, StreamMixNoFrames) {
  struct dev_stream dev_stream;
  struct cras_audio_format fmt;

  dev_stream.conv = NULL;
  rstream_playable_frames_ret = 0;
  fmt.num_channels = 2;
  fmt.format = SND_PCM_FORMAT_S16_LE;
  EXPECT_EQ(0, dev_stream_mix(&dev_stream, &fmt, 0, 3));
}

TEST_F(CreateSuite, StreamMixNoConv) {
  struct dev_stream dev_stream;
  const unsigned int nfr = 100;
  struct cras_audio_format fmt;

  dev_stream.conv = NULL;
  dev_stream.stream = reinterpret_cast<cras_rstream*>(0x5446);
  rstream_playable_frames_ret = nfr;
  rstream_get_readable_num = nfr;
  rstream_get_readable_ptr = reinterpret_cast<uint8_t*>(0x4000);
  rstream_get_readable_call.num_called = 0;
  fmt.num_channels = 2;
  fmt.format = SND_PCM_FORMAT_S16_LE;
  EXPECT_EQ(nfr, dev_stream_mix(&dev_stream, &fmt, (uint8_t*)0x5000, nfr));
  EXPECT_EQ((int16_t*)0x5000, mix_add_call.dst);
  EXPECT_EQ((int16_t*)0x4000, mix_add_call.src);
  EXPECT_EQ(200, mix_add_call.count);
  EXPECT_EQ(1, mix_add_call.index);
  EXPECT_EQ(dev_stream.stream, rstream_get_readable_call.rstream);
  EXPECT_EQ(0, rstream_get_readable_call.offset);
  EXPECT_EQ(1, rstream_get_readable_call.num_called);
}

TEST_F(CreateSuite, StreamMixNoConvTwoPass) {
  struct dev_stream dev_stream;
  const unsigned int nfr = 100;
  const unsigned int bytes_per_sample = 2;
  const unsigned int num_channels = 2;
  const unsigned int bytes_per_frame = bytes_per_sample * num_channels;
  struct cras_audio_format fmt;

  dev_stream.conv = NULL;
  dev_stream.stream = reinterpret_cast<cras_rstream*>(0x5446);
  rstream_playable_frames_ret = nfr;
  rstream_get_readable_num = nfr / 2;
  rstream_get_readable_ptr = reinterpret_cast<uint8_t*>(0x4000);
  rstream_get_readable_call.num_called = 0;
  fmt.num_channels = 2;
  fmt.format = SND_PCM_FORMAT_S16_LE;
  EXPECT_EQ(nfr, dev_stream_mix(&dev_stream, &fmt, (uint8_t*)0x5000, nfr));
  const unsigned int half_offset = nfr / 2 * bytes_per_frame;
  EXPECT_EQ((int16_t*)(0x5000 + half_offset), mix_add_call.dst);
  EXPECT_EQ((int16_t*)0x4000, mix_add_call.src);
  EXPECT_EQ(nfr / 2 * num_channels, mix_add_call.count);
  EXPECT_EQ(1, mix_add_call.index);
  EXPECT_EQ(dev_stream.stream, rstream_get_readable_call.rstream);
  EXPECT_EQ(nfr / 2, rstream_get_readable_call.offset);
  EXPECT_EQ(2, rstream_get_readable_call.num_called);
}

TEST_F(CreateSuite, DevStreamFlushAudioMessages) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;

  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
                                 (void*)0x55, &cb_ts, NULL);

  dev_stream_flush_old_audio_messages(dev_stream);
  EXPECT_EQ(1, cras_rstream_flush_old_audio_messages_called);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, DevStreamIsPending) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;

  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
                                 (void*)0x55, &cb_ts, NULL);

  // dev_stream_is_pending_reply is only a wrapper.
  cras_rstream_is_pending_reply_ret = 0;
  EXPECT_EQ(0, dev_stream_is_pending_reply(dev_stream));

  cras_rstream_is_pending_reply_ret = 1;
  EXPECT_EQ(1, dev_stream_is_pending_reply(dev_stream));

  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, StreamCanSend) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;
  int written_frames;
  int rc;
  struct timespec expected_next_cb_ts;

  rstream_.direction = CRAS_STREAM_INPUT;
  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
                                 (void*)0x55, &cb_ts, NULL);

  // Assume there is a next_cb_ts on rstream.
  rstream_.next_cb_ts.tv_sec = 1;
  rstream_.next_cb_ts.tv_nsec = 0;

  // Case 1: Not enough samples. Time is not late enough.
  //         Stream can not send data to client.
  clock_gettime_retspec.tv_sec = 0;
  clock_gettime_retspec.tv_nsec = 0;
  rc = dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(0, cras_rstream_audio_ready_called);
  EXPECT_EQ(0, cras_server_metrics_missed_cb_event_called);
  EXPECT_EQ(0, rc);

  // Case 2: Not enough samples. Time is late enough.
  //         Stream can not send data to client.

  // Assume time is greater than next_cb_ts.
  clock_gettime_retspec.tv_sec = 1;
  clock_gettime_retspec.tv_nsec = 500;
  // However, written frames is less than cb_threshold.
  // Stream still can not send samples to client.
  rc = dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(0, cras_rstream_audio_ready_called);
  EXPECT_EQ(0, cras_server_metrics_missed_cb_event_called);
  EXPECT_EQ(0, rc);

  // Case 3: Enough samples. Time is not late enough.
  //         Stream can not send data to client.

  // Assume time is less than next_cb_ts.
  clock_gettime_retspec.tv_sec = 0;
  clock_gettime_retspec.tv_nsec = 0;
  // Enough samples are written.
  written_frames = rstream_.cb_threshold + 10;
  cras_shm_buffer_written(rstream_.shm, written_frames);
  // Stream still can not send samples to client.
  rc = dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(0, cras_rstream_audio_ready_called);
  EXPECT_EQ(0, cras_server_metrics_missed_cb_event_called);
  EXPECT_EQ(0, rc);

  // Case 4: Enough samples. Time is late enough.
  //         Stream should send one cb_threshold to client.
  clock_gettime_retspec.tv_sec = 1;
  clock_gettime_retspec.tv_nsec = 500;
  rc = dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(1, cras_rstream_audio_ready_called);
  EXPECT_EQ(rstream_.cb_threshold, cras_rstream_audio_ready_count);
  EXPECT_EQ(0, cras_server_metrics_missed_cb_event_called);
  EXPECT_EQ(0, rc);

  // Check next_cb_ts is increased by one sleep interval.
  expected_next_cb_ts.tv_sec = 1;
  expected_next_cb_ts.tv_nsec = 0;
  add_timespecs(&expected_next_cb_ts, &rstream_.sleep_interval_ts);
  EXPECT_EQ(expected_next_cb_ts.tv_sec, rstream_.next_cb_ts.tv_sec);
  EXPECT_EQ(expected_next_cb_ts.tv_nsec, rstream_.next_cb_ts.tv_nsec);

  // Reset stub data of interest.
  cras_rstream_audio_ready_called = 0;
  cras_rstream_audio_ready_count = 0;
  rstream_.next_cb_ts.tv_sec = 1;
  rstream_.next_cb_ts.tv_nsec = 0;

  // Case 5: Enough samples. Time is late enough and it is too late
  //         such that a new next_cb_ts is in the past.
  //         Stream should send one cb_threshold to client and reset schedule.
  clock_gettime_retspec.tv_sec = 2;
  clock_gettime_retspec.tv_nsec = 0;
  rc = dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(1, cras_rstream_audio_ready_called);
  EXPECT_EQ(rstream_.cb_threshold, cras_rstream_audio_ready_count);
  EXPECT_EQ(1, cras_server_metrics_missed_cb_event_called);
  EXPECT_EQ(0, rc);

  // Check next_cb_ts is rest to be now plus one sleep interval.
  expected_next_cb_ts.tv_sec = 2;
  expected_next_cb_ts.tv_nsec = 0;
  add_timespecs(&expected_next_cb_ts, &rstream_.sleep_interval_ts);
  EXPECT_EQ(expected_next_cb_ts.tv_sec, rstream_.next_cb_ts.tv_sec);
  EXPECT_EQ(expected_next_cb_ts.tv_nsec, rstream_.next_cb_ts.tv_nsec);

  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, StreamCanSendBulkAudio) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;
  int written_frames;
  int rc;
  struct timespec expected_next_cb_ts;

  rstream_.direction = CRAS_STREAM_INPUT;
  rstream_.flags |= BULK_AUDIO_OK;
  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
                                 (void*)0x55, &cb_ts, NULL);

  // Assume there is a next_cb_ts on rstream.
  rstream_.next_cb_ts.tv_sec = 1;
  rstream_.next_cb_ts.tv_nsec = 0;

  // Case 1: Not enough samples. Time is not late enough.
  //         Bulk audio stream can not send data to client.
  clock_gettime_retspec.tv_sec = 0;
  clock_gettime_retspec.tv_nsec = 0;
  rc = dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(0, cras_rstream_audio_ready_called);
  EXPECT_EQ(0, rc);

  // Case 2: Not enough samples. Time is late enough.
  //         Bulk audio stream can not send data to client.

  // Assume time is greater than next_cb_ts.
  clock_gettime_retspec.tv_sec = 1;
  clock_gettime_retspec.tv_nsec = 500;
  // However, written frames is less than cb_threshold.
  // Stream still can not send samples to client.
  rc = dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(0, cras_rstream_audio_ready_called);
  EXPECT_EQ(0, rc);

  // Case 3: Enough samples. Time is not late enough.
  //         Bulk audio stream CAN send data to client.

  // Assume time is less than next_cb_ts.
  clock_gettime_retspec.tv_sec = 0;
  clock_gettime_retspec.tv_nsec = 0;
  // Enough samples are written.
  written_frames = rstream_.cb_threshold + 10;
  cras_shm_buffer_written(rstream_.shm, written_frames);
  // Bulk audio stream can send all written samples to client.
  rc = dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(1, cras_rstream_audio_ready_called);
  EXPECT_EQ(written_frames, cras_rstream_audio_ready_count);
  EXPECT_EQ(0, rc);

  // Case 4: Enough samples. Time is late enough.
  //         Bulk audio stream can send all written samples to client.

  // Reset stub data of interest.
  cras_rstream_audio_ready_called = 0;
  cras_rstream_audio_ready_count = 0;
  rstream_.next_cb_ts.tv_sec = 1;
  rstream_.next_cb_ts.tv_nsec = 0;

  clock_gettime_retspec.tv_sec = 1;
  clock_gettime_retspec.tv_nsec = 500;
  rc = dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(1, cras_rstream_audio_ready_called);
  EXPECT_EQ(written_frames, cras_rstream_audio_ready_count);
  EXPECT_EQ(0, rc);

  // Check next_cb_ts is increased by one sleep interval.
  expected_next_cb_ts.tv_sec = 1;
  expected_next_cb_ts.tv_nsec = 0;
  add_timespecs(&expected_next_cb_ts, &rstream_.sleep_interval_ts);
  EXPECT_EQ(expected_next_cb_ts.tv_sec, rstream_.next_cb_ts.tv_sec);
  EXPECT_EQ(expected_next_cb_ts.tv_nsec, rstream_.next_cb_ts.tv_nsec);

  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, TriggerOnlyStreamSendOnlyOnce) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;

  rstream_.direction = CRAS_STREAM_INPUT;
  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
                                 (void*)0x55, &cb_ts, NULL);
  dev_stream->stream->flags = TRIGGER_ONLY;
  dev_stream->stream->triggered = 0;

  // Check first trigger callback called.
  cras_shm_buffer_written(rstream_.shm, rstream_.cb_threshold);
  clock_gettime_retspec.tv_sec = 1;
  clock_gettime_retspec.tv_nsec = 0;
  dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(1, cras_rstream_audio_ready_called);
  EXPECT_EQ(1, dev_stream->stream->triggered);

  // No future callback will be called for TRIGGER_ONLY streams.
  cras_shm_buffer_written(rstream_.shm, rstream_.cb_threshold);
  clock_gettime_retspec.tv_sec = 2;
  clock_gettime_retspec.tv_nsec = 0;
  dev_stream_capture_update_rstream(dev_stream);
  EXPECT_EQ(1, cras_rstream_audio_ready_called);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, InputDevStreamWakeTimeByNextCbTs) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;
  int rc;
  unsigned int curr_level = 0;
  int written_frames;
  struct timespec level_tstamp = {.tv_sec = 1, .tv_nsec = 0};
  struct timespec wake_time_out = {.tv_sec = 0, .tv_nsec = 0};

  rstream_.direction = CRAS_STREAM_INPUT;
  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
                                 (void*)0x55, &cb_ts, NULL);

  // Assume there is a next_cb_ts on rstream.
  rstream_.next_cb_ts.tv_sec = 1;
  rstream_.next_cb_ts.tv_nsec = 500000;

  // Assume there are enough samples for stream.
  written_frames = rstream_.cb_threshold + 10;
  cras_shm_buffer_written(rstream_.shm, written_frames);

  rc = dev_stream_wake_time(dev_stream, curr_level, &level_tstamp,
                            rstream_.cb_threshold, 0, &wake_time_out);

  // The next wake up time is determined by next_cb_ts on dev_stream.
  EXPECT_EQ(rstream_.next_cb_ts.tv_sec, wake_time_out.tv_sec);
  EXPECT_EQ(rstream_.next_cb_ts.tv_nsec, wake_time_out.tv_nsec);
  EXPECT_EQ(0, rc);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, InputDevStreamWakeTimeByDevice) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;
  int rc;
  unsigned int curr_level = 100;
  int written_frames;
  struct timespec level_tstamp = {.tv_sec = 1, .tv_nsec = 0};
  struct timespec wake_time_out = {.tv_sec = 0, .tv_nsec = 0};
  struct timespec expected_tstamp = {.tv_sec = 0, .tv_nsec = 0};
  struct timespec needed_time_for_device = {.tv_sec = 0, .tv_nsec = 0};
  int needed_frames_from_device = 0;

  rstream_.direction = CRAS_STREAM_INPUT;
  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_48, (void*)0x55,
                                 &cb_ts, NULL);

  // Assume there is a next_cb_ts on rstream, that is, 1.005 seconds.
  rstream_.next_cb_ts.tv_sec = 1;
  rstream_.next_cb_ts.tv_nsec = 5000000;  // 5ms

  // Assume there are not enough samples for stream.
  written_frames = 123;
  cras_shm_buffer_written(rstream_.shm, written_frames);

  // Compute wake up time for device level to reach enough samples
  // for one cb_threshold:
  // Device has 100 samples (48K rate).
  // Stream has 123 samples (44.1K rate)
  // cb_threshold = 512 samples.
  // Stream needs 512 - 123 = 389 samples.
  // Converted to device rate => 389 * 48000.0 / 44100 = 423.4 samples
  // => 424 samples.
  // Device needs another 424 - 100 = 324 samples.
  // Time for 252 samples = 324 / 48000 = 0.00675 sec.
  // So expected wake up time for samples is at level_tstamp + 0.00675 sec =
  // 1.00675 seconds.
  needed_frames_from_device =
      cras_frames_at_rate(44100, rstream_.cb_threshold - written_frames, 48000);
  needed_frames_from_device -= curr_level;
  cras_frames_to_time(needed_frames_from_device, 48000,
                      &needed_time_for_device);

  expected_tstamp.tv_sec = level_tstamp.tv_sec;
  expected_tstamp.tv_nsec = level_tstamp.tv_nsec;

  add_timespecs(&expected_tstamp, &needed_time_for_device);

  // Set the stub data for cras_fmt_conv_out_frames_to_in.
  out_fmt.frame_rate = 44100;
  in_fmt.frame_rate = 48000;

  rc = dev_stream_wake_time(dev_stream, curr_level, &level_tstamp,
                            rstream_.cb_threshold, 0, &wake_time_out);

  // The next wake up time is determined by needed time for device level
  // to reach enough samples for one cb_threshold.
  EXPECT_EQ(expected_tstamp.tv_sec, wake_time_out.tv_sec);
  EXPECT_EQ(expected_tstamp.tv_nsec, wake_time_out.tv_nsec);
  EXPECT_EQ(0, rc);

  // Assume current level is larger than cb_threshold.
  // The wake up time is determined by next_cb_ts.
  curr_level += rstream_.cb_threshold;
  rc = dev_stream_wake_time(dev_stream, curr_level, &level_tstamp,
                            rstream_.cb_threshold, 0, &wake_time_out);
  EXPECT_EQ(rstream_.next_cb_ts.tv_sec, wake_time_out.tv_sec);
  EXPECT_EQ(rstream_.next_cb_ts.tv_nsec, wake_time_out.tv_nsec);
  EXPECT_EQ(0, rc);
  dev_stream_destroy(dev_stream);
}

TEST_F(CreateSuite, UpdateNextWakeTime) {
  struct dev_stream* dev_stream;
  unsigned int dev_id = 9;
  struct timespec expected_next_cb_ts;

  rstream_.direction = CRAS_STREAM_OUTPUT;
  dev_stream = dev_stream_create(&rstream_, dev_id, &fmt_s16le_44_1,
                                 (void*)0x55, &cb_ts, NULL);

  // Case 1: The new next_cb_ts is greater than now. Do not need to reschedule.
  rstream_.next_cb_ts.tv_sec = 2;
  rstream_.next_cb_ts.tv_nsec = 0;
  clock_gettime_retspec.tv_sec = 2;
  clock_gettime_retspec.tv_nsec = 500;
  expected_next_cb_ts = rstream_.next_cb_ts;

  dev_stream_update_next_wake_time(dev_stream);
  EXPECT_EQ(0, cras_server_metrics_missed_cb_event_called);
  add_timespecs(&expected_next_cb_ts, &rstream_.sleep_interval_ts);
  EXPECT_EQ(expected_next_cb_ts.tv_sec, rstream_.next_cb_ts.tv_sec);
  EXPECT_EQ(expected_next_cb_ts.tv_nsec, rstream_.next_cb_ts.tv_nsec);

  // Case 2: The new next_cb_ts is less than now. Need to reset schedule.
  rstream_.next_cb_ts.tv_sec = 2;
  rstream_.next_cb_ts.tv_nsec = 0;
  clock_gettime_retspec.tv_sec = 3;
  clock_gettime_retspec.tv_nsec = 0;
  expected_next_cb_ts = clock_gettime_retspec;

  dev_stream_update_next_wake_time(dev_stream);
  EXPECT_EQ(1, cras_server_metrics_missed_cb_event_called);
  add_timespecs(&expected_next_cb_ts, &rstream_.sleep_interval_ts);
  EXPECT_EQ(expected_next_cb_ts.tv_sec, rstream_.next_cb_ts.tv_sec);
  EXPECT_EQ(expected_next_cb_ts.tv_nsec, rstream_.next_cb_ts.tv_nsec);
  dev_stream_destroy(dev_stream);
}

//  Test set_playback_timestamp.
TEST(DevStreamTimimg, SetPlaybackTimeStampSimple) {
  struct cras_timespec ts;

  clock_gettime_retspec.tv_sec = 1;
  clock_gettime_retspec.tv_nsec = 0;
  cras_set_playback_timestamp(48000, 24000, &ts);
  EXPECT_EQ(1, ts.tv_sec);
  EXPECT_GE(ts.tv_nsec, 499900000);
  EXPECT_LE(ts.tv_nsec, 500100000);
}

TEST(DevStreamTimimg, SetPlaybackTimeStampWrap) {
  struct cras_timespec ts;

  clock_gettime_retspec.tv_sec = 1;
  clock_gettime_retspec.tv_nsec = 750000000;
  cras_set_playback_timestamp(48000, 24000, &ts);
  EXPECT_EQ(2, ts.tv_sec);
  EXPECT_GE(ts.tv_nsec, 249900000);
  EXPECT_LE(ts.tv_nsec, 250100000);
}

TEST(DevStreamTimimg, SetPlaybackTimeStampWrapTwice) {
  struct cras_timespec ts;

  clock_gettime_retspec.tv_sec = 1;
  clock_gettime_retspec.tv_nsec = 750000000;
  cras_set_playback_timestamp(48000, 72000, &ts);
  EXPECT_EQ(3, ts.tv_sec);
  EXPECT_GE(ts.tv_nsec, 249900000);
  EXPECT_LE(ts.tv_nsec, 250100000);
}

//  Test set_capture_timestamp.
TEST(DevStreamTimimg, SetCaptureTimeStampSimple) {
  struct cras_timespec ts;

  clock_gettime_retspec.tv_sec = 1;
  clock_gettime_retspec.tv_nsec = 750000000;
  cras_set_capture_timestamp(48000, 24000, &ts);
  EXPECT_EQ(1, ts.tv_sec);
  EXPECT_GE(ts.tv_nsec, 249900000);
  EXPECT_LE(ts.tv_nsec, 250100000);
}

TEST(DevStreamTimimg, SetCaptureTimeStampWrap) {
  struct cras_timespec ts;

  clock_gettime_retspec.tv_sec = 1;
  clock_gettime_retspec.tv_nsec = 0;
  cras_set_capture_timestamp(48000, 24000, &ts);
  EXPECT_EQ(0, ts.tv_sec);
  EXPECT_GE(ts.tv_nsec, 499900000);
  EXPECT_LE(ts.tv_nsec, 500100000);
}

TEST(DevStreamTimimg, SetCaptureTimeStampWrapPartial) {
  struct cras_timespec ts;

  clock_gettime_retspec.tv_sec = 2;
  clock_gettime_retspec.tv_nsec = 750000000;
  cras_set_capture_timestamp(48000, 72000, &ts);
  EXPECT_EQ(1, ts.tv_sec);
  EXPECT_GE(ts.tv_nsec, 249900000);
  EXPECT_LE(ts.tv_nsec, 250100000);
}

TEST(MaxFramesForConverter, 8to48) {
  EXPECT_EQ(481, max_frames_for_conversion(80,       // Stream frames.
                                           8000,     // Stream rate.
                                           48000));  // Device rate.
}

TEST(MaxFramesForConverter, 48to8) {
  EXPECT_EQ(81, max_frames_for_conversion(80,      // Stream frames.
                                          48000,   // Stream rate.
                                          8000));  // Device rate.
}

/* Stubs */
extern "C" {

int cras_rstream_audio_ready(struct cras_rstream* stream, size_t count) {
  cras_rstream_audio_ready_count = count;
  cras_rstream_audio_ready_called++;
  return 0;
}

int cras_rstream_request_audio(struct cras_rstream* stream,
                               const struct timespec* now) {
  return 0;
}

void cras_rstream_record_fetch_interval(struct cras_rstream* rstream,
                                        const struct timespec* now) {}

void cras_rstream_update_input_write_pointer(struct cras_rstream* rstream) {}

void cras_rstream_update_output_read_pointer(struct cras_rstream* rstream) {}

void cras_rstream_dev_offset_update(struct cras_rstream* rstream,
                                    unsigned int frames,
                                    unsigned int dev_id) {}

void cras_rstream_dev_attach(struct cras_rstream* rstream,
                             unsigned int dev_id,
                             void* dev_ptr) {}

void cras_rstream_dev_detach(struct cras_rstream* rstream,
                             unsigned int dev_id) {}

unsigned int cras_rstream_dev_offset(const struct cras_rstream* rstream,
                                     unsigned int dev_id) {
  return 0;
}

unsigned int cras_rstream_playable_frames(struct cras_rstream* rstream,
                                          unsigned int dev_id) {
  return rstream_playable_frames_ret;
}

float cras_rstream_get_volume_scaler(struct cras_rstream* rstream) {
  return 1.0;
}

uint8_t* cras_rstream_get_readable_frames(struct cras_rstream* rstream,
                                          unsigned int offset,
                                          size_t* frames) {
  rstream_get_readable_call.rstream = rstream;
  rstream_get_readable_call.offset = offset;
  rstream_get_readable_call.num_called++;
  *frames = rstream_get_readable_num;
  return rstream_get_readable_ptr;
}

int cras_rstream_get_mute(const struct cras_rstream* rstream) {
  return 0;
}
void cras_rstream_update_queued_frames(struct cras_rstream* rstream) {}

struct cras_audio_format* cras_rstream_post_processing_format(
    const struct cras_rstream* stream,
    void* dev_ptr) {
  return cras_rstream_post_processing_format_val;
}
void* buffer_share_get_data(const struct buffer_share* mix, unsigned int id) {
  return NULL;
};
void cras_apm_list_start_apm(struct cras_apm_list* list, void* dev_ptr){};
void cras_apm_list_stop_apm(struct cras_apm_list* list, void* dev_ptr){};

int config_format_converter(struct cras_fmt_conv** conv,
                            enum CRAS_STREAM_DIRECTION dir,
                            const struct cras_audio_format* from,
                            const struct cras_audio_format* to,
                            unsigned int frames) {
  config_format_converter_called++;
  config_format_converter_from_fmt = from;
  config_format_converter_frames = frames;
  *conv = config_format_converter_conv;
  return 0;
}

void cras_fmt_conv_destroy(struct cras_fmt_conv* conv) {}

size_t cras_fmt_conv_convert_frames(struct cras_fmt_conv* conv,
                                    uint8_t* in_buf,
                                    uint8_t* out_buf,
                                    unsigned int* in_frames,
                                    unsigned int out_frames) {
  unsigned int ret;
  conv_frames_call.conv = conv;
  conv_frames_call.in_buf = in_buf;
  conv_frames_call.out_buf = out_buf;
  conv_frames_call.in_frames = *in_frames;
  ret = cras_frames_at_rate(in_fmt.frame_rate, *in_frames, out_fmt.frame_rate);
  conv_frames_call.out_frames = out_frames;
  if (ret > out_frames) {
    ret = out_frames;
    *in_frames =
        cras_frames_at_rate(out_fmt.frame_rate, ret, in_fmt.frame_rate);
  }

  return ret;
}

void cras_mix_add(snd_pcm_format_t fmt,
                  uint8_t* dst,
                  uint8_t* src,
                  unsigned int count,
                  unsigned int index,
                  int mute,
                  float mix_vol) {
  mix_add_call.dst = (int16_t*)dst;
  mix_add_call.src = (int16_t*)src;
  mix_add_call.count = count;
  mix_add_call.index = index;
  mix_add_call.mute = mute;
  mix_add_call.mix_vol = mix_vol;
}

struct cras_audio_area* cras_audio_area_create(int num_channels) {
  cras_audio_area_create_num_channels_val = num_channels;
  return NULL;
}

void cras_audio_area_destroy(struct cras_audio_area* area) {}

void cras_audio_area_config_buf_pointers(struct cras_audio_area* area,
                                         const struct cras_audio_format* fmt,
                                         uint8_t* base_buffer) {}

void cras_audio_area_config_channels(struct cras_audio_area* area,
                                     const struct cras_audio_format* fmt) {}

unsigned int cras_audio_area_copy(const struct cras_audio_area* dst,
                                  unsigned int dst_offset,
                                  const struct cras_audio_format* dst_fmt,
                                  const struct cras_audio_area* src,
                                  unsigned int src_offset,
                                  float software_gain_scaler) {
  copy_area_call.dst = dst;
  copy_area_call.dst_offset = dst_offset;
  copy_area_call.dst_format_bytes = cras_get_format_bytes(dst_fmt);
  copy_area_call.src = src;
  copy_area_call.src_offset = src_offset;
  copy_area_call.software_gain_scaler = software_gain_scaler;
  return src->frames;
}

size_t cras_fmt_conv_in_frames_to_out(struct cras_fmt_conv* conv,
                                      size_t in_frames) {
  return cras_frames_at_rate(in_fmt.frame_rate, in_frames, out_fmt.frame_rate);
}

size_t cras_fmt_conv_out_frames_to_in(struct cras_fmt_conv* conv,
                                      size_t out_frames) {
  return cras_frames_at_rate(out_fmt.frame_rate, out_frames, in_fmt.frame_rate);
}

const struct cras_audio_format* cras_fmt_conv_in_format(
    const struct cras_fmt_conv* conv) {
  return &in_fmt;
}

const struct cras_audio_format* cras_fmt_conv_out_format(
    const struct cras_fmt_conv* conv) {
  return &out_fmt;
}

int cras_fmt_conversion_needed(const struct cras_fmt_conv* conv) {
  return cras_fmt_conversion_needed_val;
}

void cras_fmt_conv_set_linear_resample_rates(struct cras_fmt_conv* conv,
                                             float from,
                                             float to) {
  cras_fmt_conv_set_linear_resample_rates_from = from;
  cras_fmt_conv_set_linear_resample_rates_to = to;
  cras_fmt_conv_set_linear_resample_rates_called++;
}

int cras_rstream_is_pending_reply(const struct cras_rstream* stream) {
  return cras_rstream_is_pending_reply_ret;
}

int cras_rstream_flush_old_audio_messages(struct cras_rstream* stream) {
  cras_rstream_flush_old_audio_messages_called++;
  return 0;
}

int cras_server_metrics_missed_cb_event(struct cras_rstream* stream) {
  cras_server_metrics_missed_cb_event_called++;
  return 0;
}

//  From librt.
int clock_gettime(clockid_t clk_id, struct timespec* tp) {
  tp->tv_sec = clock_gettime_retspec.tv_sec;
  tp->tv_nsec = clock_gettime_retspec.tv_nsec;
  return 0;
}

}  // extern "C"

}  //  namespace

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}