summaryrefslogtreecommitdiff
path: root/cras/src/tests/iodev_unittest.cc
blob: c59627e1a78eba998b70693065e0004f1ff3da4c (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
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
// Copyright (c) 2012 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 <stdio.h>
#include <gtest/gtest.h>

extern "C" {
#include "cras_iodev.h"
#include "cras_rstream.h"
#include "dev_stream.h"
#include "utlist.h"
#include "cras_audio_area.h"
#include "audio_thread_log.h"

// Mock software volume scalers.
float softvol_scalers[101];
}

#define BUFFER_SIZE 8192

static int cras_iodev_list_disable_dev_called;
static int select_node_called;
static enum CRAS_STREAM_DIRECTION select_node_direction;
static cras_node_id_t select_node_id;
static struct cras_ionode *node_selected;
static size_t notify_nodes_changed_called;
static size_t notify_active_node_changed_called;
static size_t notify_node_volume_called;
static size_t notify_node_capture_gain_called;
static int dsp_context_new_sample_rate;
static const char *dsp_context_new_purpose;
static int dsp_context_free_called;
static int update_channel_layout_called;
static int update_channel_layout_return_val;
static int  set_swap_mode_for_node_called;
static int  set_swap_mode_for_node_enable;
static int notify_node_left_right_swapped_called;
static int cras_audio_format_set_channel_layout_called;
static unsigned int cras_system_get_volume_return;
static int cras_dsp_get_pipeline_called;
static int cras_dsp_get_pipeline_ret;
static int cras_dsp_put_pipeline_called;
static int cras_dsp_pipeline_get_source_buffer_called;
static int cras_dsp_pipeline_get_sink_buffer_called;
static float cras_dsp_pipeline_source_buffer[2][DSP_BUFFER_SIZE];
static float cras_dsp_pipeline_sink_buffer[2][DSP_BUFFER_SIZE];
static int cras_dsp_pipeline_get_delay_called;
static int cras_dsp_pipeline_apply_called;
static int cras_dsp_pipeline_apply_sample_count;
static unsigned int cras_mix_mute_count;
static unsigned int cras_dsp_num_input_channels_return;
static unsigned int cras_dsp_num_output_channels_return;
struct cras_dsp_context *cras_dsp_context_new_return;
static unsigned int rate_estimator_add_frames_num_frames;
static unsigned int rate_estimator_add_frames_called;
static int cras_system_get_mute_return;
static snd_pcm_format_t cras_scale_buffer_fmt;
static float cras_scale_buffer_scaler;
static unsigned int pre_dsp_hook_called;
static const uint8_t *pre_dsp_hook_frames;
static void *pre_dsp_hook_cb_data;
static unsigned int post_dsp_hook_called;
static const uint8_t *post_dsp_hook_frames;
static void *post_dsp_hook_cb_data;
static int iodev_buffer_size;
static long cras_system_get_capture_gain_ret_value;
static uint8_t audio_buffer[BUFFER_SIZE];
static struct cras_audio_area *audio_area;
static unsigned int put_buffer_nframes;
static int output_should_wake_ret;
static int no_stream_called;
static int no_stream_enable;
// This will be used extensively in cras_iodev.
struct audio_thread_event_log *atlog;
static unsigned int simple_no_stream_called;
static int simple_no_stream_enable;
static int dev_stream_playback_frames_ret;
static int get_num_underruns_ret;


// Iodev callback
int update_channel_layout(struct cras_iodev *iodev) {
  update_channel_layout_called = 1;
  return update_channel_layout_return_val;
}

// Iodev callback
int set_swap_mode_for_node(struct cras_iodev *iodev, struct cras_ionode *node,
                           int enable)
{
  set_swap_mode_for_node_called++;
  set_swap_mode_for_node_enable = enable;
  return 0;
}

void ResetStubData() {
  cras_iodev_list_disable_dev_called = 0;
  select_node_called = 0;
  notify_nodes_changed_called = 0;
  notify_active_node_changed_called = 0;
  notify_node_volume_called = 0;
  notify_node_capture_gain_called = 0;
  dsp_context_new_sample_rate = 0;
  dsp_context_new_purpose = NULL;
  dsp_context_free_called = 0;
  set_swap_mode_for_node_called = 0;
  set_swap_mode_for_node_enable = 0;
  notify_node_left_right_swapped_called = 0;
  cras_audio_format_set_channel_layout_called = 0;
  cras_dsp_get_pipeline_called = 0;
  cras_dsp_get_pipeline_ret = 0;
  cras_dsp_put_pipeline_called = 0;
  cras_dsp_pipeline_get_source_buffer_called = 0;
  cras_dsp_pipeline_get_sink_buffer_called = 0;
  memset(&cras_dsp_pipeline_source_buffer, 0,
         sizeof(cras_dsp_pipeline_source_buffer));
  memset(&cras_dsp_pipeline_sink_buffer, 0,
         sizeof(cras_dsp_pipeline_sink_buffer));
  cras_dsp_pipeline_get_delay_called = 0;
  cras_dsp_pipeline_apply_called = 0;
  cras_dsp_pipeline_apply_sample_count = 0;
  cras_dsp_num_input_channels_return = 2;
  cras_dsp_num_output_channels_return = 2;
  cras_dsp_context_new_return = NULL;
  rate_estimator_add_frames_num_frames = 0;
  rate_estimator_add_frames_called = 0;
  cras_system_get_mute_return = 0;
  cras_mix_mute_count = 0;
  pre_dsp_hook_called = 0;
  pre_dsp_hook_frames = NULL;
  post_dsp_hook_called = 0;
  post_dsp_hook_frames = NULL;
  iodev_buffer_size = 0;
  cras_system_get_capture_gain_ret_value = 0;
  // Assume there is some data in audio buffer.
  memset(audio_buffer, 0xff, sizeof(audio_buffer));
  if (audio_area) {
    free(audio_area);
    audio_area = NULL;
  }
  put_buffer_nframes = 0;
  output_should_wake_ret= 0;
  no_stream_called = 0;
  no_stream_enable = 0;
  simple_no_stream_called = 0;
  simple_no_stream_enable = 0;
  dev_stream_playback_frames_ret = 0;
  if (!atlog)
    atlog = audio_thread_event_log_init();
  get_num_underruns_ret = 0;
}

namespace {

//  Test fill_time_from_frames
TEST(IoDevTestSuite, FillTimeFromFramesNormal) {
  struct timespec ts;

  cras_iodev_fill_time_from_frames(12000, 48000, &ts);
  EXPECT_EQ(0, ts.tv_sec);
  EXPECT_GE(ts.tv_nsec, 249900000);
  EXPECT_LE(ts.tv_nsec, 250100000);
}

TEST(IoDevTestSuite, FillTimeFromFramesLong) {
  struct timespec ts;

  cras_iodev_fill_time_from_frames(120000 - 12000, 48000, &ts);
  EXPECT_EQ(2, ts.tv_sec);
  EXPECT_GE(ts.tv_nsec, 249900000);
  EXPECT_LE(ts.tv_nsec, 250100000);
}

TEST(IoDevTestSuite, FillTimeFromFramesShort) {
  struct timespec ts;

  cras_iodev_fill_time_from_frames(12000 - 12000, 48000, &ts);
  EXPECT_EQ(0, ts.tv_sec);
  EXPECT_EQ(0, ts.tv_nsec);
}

class IoDevSetFormatTestSuite : public testing::Test {
  protected:
    virtual void SetUp() {
      ResetStubData();
      sample_rates_[0] = 44100;
      sample_rates_[1] = 48000;
      sample_rates_[2] = 0;

      channel_counts_[0] = 2;
      channel_counts_[1] = 0;
      channel_counts_[2] = 0;

      pcm_formats_[0] = SND_PCM_FORMAT_S16_LE;
      pcm_formats_[1] = SND_PCM_FORMAT_S32_LE;
      pcm_formats_[2] = static_cast<snd_pcm_format_t>(0);

      update_channel_layout_called = 0;
      update_channel_layout_return_val = 0;

      memset(&iodev_, 0, sizeof(iodev_));
      iodev_.update_channel_layout = update_channel_layout;
      iodev_.supported_rates = sample_rates_;
      iodev_.supported_channel_counts = channel_counts_;
      iodev_.supported_formats = pcm_formats_;
      iodev_.dsp_context = NULL;

      cras_audio_format_set_channel_layout_called  = 0;
    }

    virtual void TearDown() {
      cras_iodev_free_format(&iodev_);
    }

    struct cras_iodev iodev_;
    size_t sample_rates_[3];
    size_t channel_counts_[3];
    snd_pcm_format_t pcm_formats_[3];
};

TEST_F(IoDevSetFormatTestSuite, SupportedFormatSecondary) {
  struct cras_audio_format fmt;
  int rc;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;
  iodev_.direction = CRAS_STREAM_OUTPUT;
  ResetStubData();
  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
  EXPECT_EQ(dsp_context_new_sample_rate, 48000);
  EXPECT_STREQ(dsp_context_new_purpose, "playback");
}

TEST_F(IoDevSetFormatTestSuite, SupportedFormat32bit) {
  struct cras_audio_format fmt;
  int rc;

  fmt.format = SND_PCM_FORMAT_S32_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;
  iodev_.direction = CRAS_STREAM_OUTPUT;
  ResetStubData();
  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S32_LE, iodev_.ext_format->format);
  EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
  EXPECT_EQ(dsp_context_new_sample_rate, 48000);
  EXPECT_STREQ(dsp_context_new_purpose, "playback");
}

TEST_F(IoDevSetFormatTestSuite, SupportedFormatPrimary) {
  struct cras_audio_format fmt;
  int rc;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 44100;
  fmt.num_channels = 2;
  iodev_.direction = CRAS_STREAM_INPUT;
  ResetStubData();
  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(44100, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
  EXPECT_EQ(dsp_context_new_sample_rate, 44100);
  EXPECT_STREQ(dsp_context_new_purpose, "capture");
}

TEST_F(IoDevSetFormatTestSuite, SupportedFormatDivisor) {
  struct cras_audio_format fmt;
  int rc;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 96000;
  fmt.num_channels = 2;
  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
}

TEST_F(IoDevSetFormatTestSuite, Supported96k) {
  struct cras_audio_format fmt;
  int rc;

  sample_rates_[0] = 48000;
  sample_rates_[1] = 96000;
  sample_rates_[2] = 0;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 96000;
  fmt.num_channels = 2;
  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(96000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
}

TEST_F(IoDevSetFormatTestSuite, LimitLowRate) {
  struct cras_audio_format fmt;
  int rc;

  sample_rates_[0] = 48000;
  sample_rates_[1] = 8000;
  sample_rates_[2] = 0;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 8000;
  fmt.num_channels = 2;
  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
}

TEST_F(IoDevSetFormatTestSuite, UnsupportedChannelCount) {
  struct cras_audio_format fmt;
  int rc;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 96000;
  fmt.num_channels = 1;
  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
}

TEST_F(IoDevSetFormatTestSuite, SupportedFormatFallbackDefault) {
  struct cras_audio_format fmt;
  int rc;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 96008;
  fmt.num_channels = 2;
  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(44100, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
}

TEST_F(IoDevSetFormatTestSuite, OutputDSPChannleReduction) {
  struct cras_audio_format fmt;
  int rc;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;

  iodev_.direction = CRAS_STREAM_OUTPUT;
  iodev_.supported_channel_counts[0] = 1;
  iodev_.supported_channel_counts[1] = 0;
  cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf00);
  cras_dsp_get_pipeline_ret =  0xf01;
  cras_dsp_num_input_channels_return = 2;
  cras_dsp_num_output_channels_return = 1;
  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
}

TEST_F(IoDevSetFormatTestSuite, InputDSPChannleReduction) {
  struct cras_audio_format fmt;
  int rc;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;

  iodev_.direction = CRAS_STREAM_INPUT;
  iodev_.supported_channel_counts[0] = 10;
  iodev_.supported_channel_counts[1] = 0;
  cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf00);
  cras_dsp_get_pipeline_ret =  0xf01;
  cras_dsp_num_input_channels_return = 10;
  cras_dsp_num_output_channels_return = 2;
  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
}

TEST_F(IoDevSetFormatTestSuite, UpdateChannelLayoutSuccess) {
  struct cras_audio_format fmt;
  int rc;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 6;

  iodev_.supported_channel_counts[0] = 6;
  iodev_.supported_channel_counts[1] = 2;

  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(6, iodev_.ext_format->num_channels);
}

TEST_F(IoDevSetFormatTestSuite, UpdateChannelLayoutFail) {
  static const int8_t stereo_layout[] =
      {0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
  struct cras_audio_format fmt;
  int rc, i;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;

  cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf0f);

  update_channel_layout_return_val = -1;
  iodev_.supported_channel_counts[0] = 6;
  iodev_.supported_channel_counts[1] = 2;

  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(2, iodev_.ext_format->num_channels);
  EXPECT_EQ(2, cras_audio_format_set_channel_layout_called);
  EXPECT_EQ(0, dsp_context_free_called);
  for (i = 0; i < CRAS_CH_MAX; i++)
    EXPECT_EQ(iodev_.format->channel_layout[i], stereo_layout[i]);
}

TEST_F(IoDevSetFormatTestSuite, UpdateChannelLayoutFail6ch) {
  static const int8_t default_6ch_layout[] =
      {0, 1, 2, 3, 4, 5, -1, -1, -1, -1, -1};
  struct cras_audio_format fmt;
  int rc, i;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 6;

  cras_dsp_context_new_return = reinterpret_cast<cras_dsp_context *>(0xf0f);

  update_channel_layout_return_val = -1;
  iodev_.supported_channel_counts[0] = 6;
  iodev_.supported_channel_counts[1] = 2;

  rc = cras_iodev_set_format(&iodev_, &fmt);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, iodev_.ext_format->format);
  EXPECT_EQ(48000, iodev_.ext_format->frame_rate);
  EXPECT_EQ(6, iodev_.ext_format->num_channels);
  EXPECT_EQ(2, cras_audio_format_set_channel_layout_called);
  EXPECT_EQ(0, dsp_context_free_called);
  for (i = 0; i < CRAS_CH_MAX; i++)
    EXPECT_EQ(iodev_.format->channel_layout[i], default_6ch_layout[i]);
}

// Put buffer tests

static int get_buffer(cras_iodev* iodev, struct cras_audio_area** area,
               unsigned int* num) {
  size_t sz = sizeof(*audio_area) + sizeof(struct cras_channel_area) * 2;

  audio_area = (cras_audio_area*)calloc(1, sz);
  audio_area->frames = *num;
  audio_area->num_channels = 2;
  audio_area->channels[0].buf = audio_buffer;
  channel_area_set_channel(&audio_area->channels[0], CRAS_CH_FL);
  audio_area->channels[0].step_bytes = 4;
  audio_area->channels[1].buf = audio_buffer + 2;
  channel_area_set_channel(&audio_area->channels[1], CRAS_CH_FR);
  audio_area->channels[1].step_bytes = 4;

  *area = audio_area;
  return 0;
}

static int put_buffer(struct cras_iodev *iodev, unsigned int nframes)
{
  put_buffer_nframes = nframes;
  if (audio_area) {
    free(audio_area);
    audio_area = NULL;
  }
  return 0;
}

static int no_stream(struct cras_iodev *odev, int enable)
{
  no_stream_called++;
  no_stream_enable = enable;
  // Use default no stream playback to test default behavior.
  return cras_iodev_default_no_stream_playback(odev, enable);
}

static int output_should_wake(const struct cras_iodev *odev)
{
  return output_should_wake_ret;
}

static int pre_dsp_hook(const uint8_t *frames, unsigned int nframes,
			const struct cras_audio_format *fmt, void *cb_data)
{
  pre_dsp_hook_called++;
  pre_dsp_hook_frames = frames;
  pre_dsp_hook_cb_data = cb_data;
  return 0;
}

static int post_dsp_hook(const uint8_t *frames, unsigned int nframes,
			 const struct cras_audio_format *fmt, void *cb_data)
{
  post_dsp_hook_called++;
  post_dsp_hook_frames = frames;
  post_dsp_hook_cb_data = cb_data;
  return 0;
}

TEST(IoDevPutOutputBuffer, SystemMuted) {
  struct cras_audio_format fmt;
  struct cras_iodev iodev;
  uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
  int rc;

  ResetStubData();
  memset(&iodev, 0, sizeof(iodev));
  cras_system_get_mute_return = 1;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;
  iodev.format = &fmt;
  iodev.put_buffer = put_buffer;

  rc = cras_iodev_put_output_buffer(&iodev, frames, 20);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(20, cras_mix_mute_count);
  EXPECT_EQ(20, put_buffer_nframes);
  EXPECT_EQ(20, rate_estimator_add_frames_num_frames);
}

TEST(IoDevPutOutputBuffer, NoDSP) {
  struct cras_audio_format fmt;
  struct cras_iodev iodev;
  uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
  int rc;

  ResetStubData();
  memset(&iodev, 0, sizeof(iodev));

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;
  iodev.format = &fmt;
  iodev.put_buffer = put_buffer;

  rc = cras_iodev_put_output_buffer(&iodev, frames, 22);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(0, cras_mix_mute_count);
  EXPECT_EQ(22, put_buffer_nframes);
  EXPECT_EQ(22, rate_estimator_add_frames_num_frames);
}

TEST(IoDevPutOutputBuffer, DSP) {
  struct cras_audio_format fmt;
  struct cras_iodev iodev;
  uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
  int rc;

  ResetStubData();
  memset(&iodev, 0, sizeof(iodev));
  iodev.dsp_context = reinterpret_cast<cras_dsp_context*>(0x15);
  cras_dsp_get_pipeline_ret = 0x25;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;
  iodev.format = &fmt;
  iodev.put_buffer = put_buffer;
  cras_iodev_register_pre_dsp_hook(&iodev, pre_dsp_hook, (void *)0x1234);
  cras_iodev_register_post_dsp_hook(&iodev, post_dsp_hook, (void *)0x5678);

  rc = cras_iodev_put_output_buffer(&iodev, frames, 32);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(0, cras_mix_mute_count);
  EXPECT_EQ(1, pre_dsp_hook_called);
  EXPECT_EQ(frames, pre_dsp_hook_frames);
  EXPECT_EQ((void *)0x1234, pre_dsp_hook_cb_data);
  EXPECT_EQ(1, post_dsp_hook_called);
  EXPECT_EQ((void *)0x5678, post_dsp_hook_cb_data);
  EXPECT_EQ(32, put_buffer_nframes);
  EXPECT_EQ(32, rate_estimator_add_frames_num_frames);
  EXPECT_EQ(32, cras_dsp_pipeline_apply_sample_count);
  EXPECT_EQ(cras_dsp_get_pipeline_called, cras_dsp_put_pipeline_called);
}

TEST(IoDevPutOutputBuffer, SoftVol) {
  struct cras_audio_format fmt;
  struct cras_iodev iodev;
  uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
  int rc;

  ResetStubData();
  memset(&iodev, 0, sizeof(iodev));
  iodev.software_volume_needed = 1;

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;
  iodev.format = &fmt;
  iodev.put_buffer = put_buffer;

  cras_system_get_volume_return = 13;
  softvol_scalers[13] = 0.435;

  rc = cras_iodev_put_output_buffer(&iodev, frames, 53);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(0, cras_mix_mute_count);
  EXPECT_EQ(53, put_buffer_nframes);
  EXPECT_EQ(53, rate_estimator_add_frames_num_frames);
  EXPECT_EQ(softvol_scalers[13], cras_scale_buffer_scaler);
  EXPECT_EQ(SND_PCM_FORMAT_S16_LE, cras_scale_buffer_fmt);
}

TEST(IoDevPutOutputBuffer, Scale32Bit) {
  struct cras_audio_format fmt;
  struct cras_iodev iodev;
  uint8_t *frames = reinterpret_cast<uint8_t*>(0x44);
  int rc;

  ResetStubData();
  memset(&iodev, 0, sizeof(iodev));
  iodev.software_volume_needed = 1;

  cras_system_get_volume_return = 13;
  softvol_scalers[13] = 0.435;

  fmt.format = SND_PCM_FORMAT_S32_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;
  iodev.format = &fmt;
  iodev.put_buffer = put_buffer;

  rc = cras_iodev_put_output_buffer(&iodev, frames, 53);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(0, cras_mix_mute_count);
  EXPECT_EQ(53, put_buffer_nframes);
  EXPECT_EQ(53, rate_estimator_add_frames_num_frames);
  EXPECT_EQ(SND_PCM_FORMAT_S32_LE, cras_scale_buffer_fmt);
}

// frames queued/avail tests

static unsigned fr_queued = 0;

static int frames_queued(const struct cras_iodev *iodev,
                         struct timespec *tstamp)
{
  clock_gettime(CLOCK_MONOTONIC_RAW, tstamp);
  return fr_queued;
}

TEST(IoDevQueuedBuffer, ZeroMinBufferLevel) {
  struct cras_iodev iodev;
  struct timespec tstamp;
  int rc;

  ResetStubData();
  memset(&iodev, 0, sizeof(iodev));
  iodev.direction = CRAS_STREAM_OUTPUT;
  iodev.frames_queued = frames_queued;
  iodev.min_buffer_level = 0;
  iodev.buffer_size = 200;
  fr_queued = 50;

  rc = cras_iodev_frames_queued(&iodev, &tstamp);
  EXPECT_EQ(50, rc);
  rc = cras_iodev_buffer_avail(&iodev, rc);
  EXPECT_EQ(150, rc);
}

TEST(IoDevQueuedBuffer, NonZeroMinBufferLevel) {
  struct cras_iodev iodev;
  struct timespec hw_tstamp;
  int rc;

  ResetStubData();
  memset(&iodev, 0, sizeof(iodev));
  iodev.direction = CRAS_STREAM_OUTPUT;
  iodev.frames_queued = frames_queued;
  iodev.min_buffer_level = 100;
  iodev.buffer_size = 200;
  fr_queued = 180;

  rc = cras_iodev_frames_queued(&iodev, &hw_tstamp);
  EXPECT_EQ(80, rc);
  rc = cras_iodev_buffer_avail(&iodev, rc);
  EXPECT_EQ(20, rc);

  /* When fr_queued < min_buffer_level*/
  fr_queued = 80;
  rc = cras_iodev_frames_queued(&iodev, &hw_tstamp);
  EXPECT_EQ(0, rc);
  rc = cras_iodev_buffer_avail(&iodev, rc);
  EXPECT_EQ(100, rc);
}

static void update_active_node(struct cras_iodev *iodev,
                               unsigned node_idx,
                               unsigned dev_enabled)
{
}

static void dev_set_volume(struct cras_iodev *iodev)
{
}

static void dev_set_capture_gain(struct cras_iodev *iodev)
{
}

TEST(IoNodePlug, PlugUnplugNode) {
  struct cras_iodev iodev;
  struct cras_ionode ionode, ionode2;

  memset(&iodev, 0, sizeof(iodev));
  memset(&ionode, 0, sizeof(ionode));
  memset(&ionode2, 0, sizeof(ionode2));
  iodev.direction = CRAS_STREAM_INPUT;
  iodev.update_active_node = update_active_node;
  ionode.dev = &iodev;
  cras_iodev_add_node(&iodev, &ionode);
  ionode2.dev = &iodev;
  cras_iodev_add_node(&iodev, &ionode2);
  cras_iodev_set_active_node(&iodev, &ionode);
  ResetStubData();
  cras_iodev_set_node_attr(&ionode, IONODE_ATTR_PLUGGED, 1);
  EXPECT_EQ(0, cras_iodev_list_disable_dev_called);
  cras_iodev_set_node_attr(&ionode, IONODE_ATTR_PLUGGED, 0);
  EXPECT_EQ(1, cras_iodev_list_disable_dev_called);

  /* Unplug non-active node shouldn't disable iodev. */
  cras_iodev_set_node_attr(&ionode2, IONODE_ATTR_PLUGGED, 1);
  EXPECT_EQ(1, cras_iodev_list_disable_dev_called);
  cras_iodev_set_node_attr(&ionode2, IONODE_ATTR_PLUGGED, 0);
  EXPECT_EQ(1, cras_iodev_list_disable_dev_called);
}

TEST(IoDev, AddRemoveNode) {
  struct cras_iodev iodev;
  struct cras_ionode ionode;

  memset(&iodev, 0, sizeof(iodev));
  memset(&ionode, 0, sizeof(ionode));
  ResetStubData();
  EXPECT_EQ(0, notify_nodes_changed_called);
  cras_iodev_add_node(&iodev, &ionode);
  EXPECT_EQ(1, notify_nodes_changed_called);
  cras_iodev_rm_node(&iodev, &ionode);
  EXPECT_EQ(2, notify_nodes_changed_called);
}

TEST(IoDev, SetActiveNode) {
  struct cras_iodev iodev;
  struct cras_ionode ionode;

  memset(&iodev, 0, sizeof(iodev));
  memset(&ionode, 0, sizeof(ionode));
  ResetStubData();
  EXPECT_EQ(0, notify_active_node_changed_called);
  cras_iodev_set_active_node(&iodev, &ionode);
  EXPECT_EQ(1, notify_active_node_changed_called);
}

TEST(IoDev, SetNodeVolume) {
  struct cras_iodev iodev;
  struct cras_ionode ionode;

  memset(&iodev, 0, sizeof(iodev));
  memset(&ionode, 0, sizeof(ionode));
  iodev.set_volume = dev_set_volume;
  iodev.set_capture_gain = dev_set_capture_gain;
  ionode.dev = &iodev;
  ResetStubData();
  cras_iodev_set_node_attr(&ionode, IONODE_ATTR_VOLUME, 10);
  EXPECT_EQ(1, notify_node_volume_called);
  iodev.direction = CRAS_STREAM_INPUT;
  cras_iodev_set_node_attr(&ionode, IONODE_ATTR_CAPTURE_GAIN, 10);
  EXPECT_EQ(1, notify_node_capture_gain_called);
}

TEST(IoDev, SetNodeSwapLeftRight) {
  struct cras_iodev iodev;
  struct cras_ionode ionode;

  memset(&iodev, 0, sizeof(iodev));
  memset(&ionode, 0, sizeof(ionode));
  iodev.set_swap_mode_for_node = set_swap_mode_for_node;
  ionode.dev = &iodev;
  ResetStubData();
  cras_iodev_set_node_attr(&ionode, IONODE_ATTR_SWAP_LEFT_RIGHT, 1);
  EXPECT_EQ(1, set_swap_mode_for_node_called);
  EXPECT_EQ(1, set_swap_mode_for_node_enable);
  EXPECT_EQ(1, ionode.left_right_swapped);
  EXPECT_EQ(1, notify_node_left_right_swapped_called);
  cras_iodev_set_node_attr(&ionode, IONODE_ATTR_SWAP_LEFT_RIGHT, 0);
  EXPECT_EQ(2, set_swap_mode_for_node_called);
  EXPECT_EQ(0, set_swap_mode_for_node_enable);
  EXPECT_EQ(0, ionode.left_right_swapped);
  EXPECT_EQ(2, notify_node_left_right_swapped_called);
}


// Test software volume changes for default output.
TEST(IoDev, SoftwareVolume) {
  struct cras_iodev iodev;
  struct cras_ionode ionode;

  memset(&iodev, 0, sizeof(iodev));
  memset(&ionode, 0, sizeof(ionode));
  ResetStubData();

  iodev.nodes = &ionode;
  iodev.active_node = &ionode;
  iodev.active_node->dev = &iodev;

  iodev.active_node->volume = 100;
  iodev.software_volume_needed = 0;


  softvol_scalers[80] = 0.5;
  softvol_scalers[70] = 0.3;

  // Check that system volume changes software volume if needed.
  cras_system_get_volume_return = 80;
  // system_volume - 100 + node_volume = 80 - 100 + 100 = 80
  EXPECT_FLOAT_EQ(0.5, cras_iodev_get_software_volume_scaler(&iodev));

  // Check that node volume changes software volume if needed.
  iodev.active_node->volume = 90;
  // system_volume - 100 + node_volume = 80 - 100 + 90 = 70
  EXPECT_FLOAT_EQ(0.3, cras_iodev_get_software_volume_scaler(&iodev));
}

// Test software gain scaler.
TEST(IoDev, SoftwareGain) {
  struct cras_iodev iodev;
  struct cras_ionode ionode;

  memset(&iodev, 0, sizeof(iodev));
  memset(&ionode, 0, sizeof(ionode));
  ResetStubData();

  iodev.nodes = &ionode;
  iodev.active_node = &ionode;
  iodev.active_node->dev = &iodev;

  ionode.capture_gain= 400;
  ionode.software_volume_needed = 1;
  ionode.max_software_gain = 3000;

  // Check that system volume changes software volume if needed.
  cras_system_get_capture_gain_ret_value = 2000;
  // system_gain + node_gain = 2000 + 400  = 2400
  // 2400 dBm is 15.848931
  EXPECT_FLOAT_EQ(15.848931, cras_iodev_get_software_gain_scaler(&iodev));
  EXPECT_FLOAT_EQ(3000, cras_iodev_maximum_software_gain(&iodev));

  // Software gain scaler should be 1.0 if software gain is not needed.
  ionode.software_volume_needed = 0;
  EXPECT_FLOAT_EQ(1.0, cras_iodev_get_software_gain_scaler(&iodev));
  EXPECT_FLOAT_EQ(0, cras_iodev_maximum_software_gain(&iodev));
}

// This get_buffer implementation set returned frames larger than requested
// frames.
static int bad_get_buffer(struct cras_iodev *iodev,
                          struct cras_audio_area **area,
                          unsigned *frames)
{
  *frames = *frames + 1;
  return 0;
}

// Check that if get_buffer implementation returns invalid frames,
// cras_iodev_get_output_buffer and cras_iodev_get_input_buffer can return
// error.
TEST(IoDev, GetBufferInvalidFrames) {
  struct cras_iodev iodev;
  struct cras_audio_area **area = NULL;
  unsigned int frames = 512;
  struct cras_audio_format fmt;

  // Format is used in cras_iodev_get_input_buffer;
  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;

  memset(&iodev, 0, sizeof(iodev));

  ResetStubData();

  iodev.format = &fmt;
  iodev.get_buffer = bad_get_buffer;

  EXPECT_EQ(-EINVAL, cras_iodev_get_output_buffer(&iodev, area, &frames));
  EXPECT_EQ(-EINVAL, cras_iodev_get_input_buffer(&iodev, area, &frames));
}

static int open_dev(struct cras_iodev *iodev) {
  iodev->buffer_size = iodev_buffer_size;
  return 0;
}

TEST(IoDev, OpenOutputDeviceNoStart) {
  struct cras_iodev iodev;

  memset(&iodev, 0, sizeof(iodev));
  iodev.open_dev = open_dev;
  iodev.direction = CRAS_STREAM_OUTPUT;
  ResetStubData();

  iodev.state = CRAS_IODEV_STATE_CLOSE;

  iodev_buffer_size = 1024;
  cras_iodev_open(&iodev, 240);
  EXPECT_EQ(0, iodev.max_cb_level);
  EXPECT_EQ(240, iodev.min_cb_level);

  // Test that state is no stream run when there is no start ops.
  EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state);
}

int fake_start(const struct cras_iodev *iodev) {
  return 0;
}

TEST(IoDev, OpenOutputDeviceWithStart) {
  struct cras_iodev iodev;

  memset(&iodev, 0, sizeof(iodev));
  iodev.open_dev = open_dev;
  iodev.direction = CRAS_STREAM_OUTPUT;
  ResetStubData();

  iodev.state = CRAS_IODEV_STATE_CLOSE;
  iodev.start = fake_start;

  iodev_buffer_size = 1024;
  cras_iodev_open(&iodev, 240);
  EXPECT_EQ(0, iodev.max_cb_level);
  EXPECT_EQ(240, iodev.min_cb_level);

  // Test that state is no stream run when there is start ops.
  EXPECT_EQ(CRAS_IODEV_STATE_OPEN, iodev.state);
}

TEST(IoDev, OpenInputDeviceNoStart) {
  struct cras_iodev iodev;

  memset(&iodev, 0, sizeof(iodev));
  iodev.open_dev = open_dev;
  iodev.direction = CRAS_STREAM_INPUT;
  ResetStubData();

  iodev.state = CRAS_IODEV_STATE_CLOSE;

  iodev_buffer_size = 1024;
  cras_iodev_open(&iodev, 240);
  EXPECT_EQ(0, iodev.max_cb_level);
  EXPECT_EQ(240, iodev.min_cb_level);

  // Test that state is normal run when there is start ops.
  EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state);
}

TEST(IoDev, OpenInputDeviceWithStart) {
  struct cras_iodev iodev;

  memset(&iodev, 0, sizeof(iodev));
  iodev.open_dev = open_dev;
  iodev.direction = CRAS_STREAM_INPUT;
  ResetStubData();

  iodev.state = CRAS_IODEV_STATE_CLOSE;
  iodev.start = fake_start;

  iodev_buffer_size = 1024;
  cras_iodev_open(&iodev, 240);
  EXPECT_EQ(0, iodev.max_cb_level);
  EXPECT_EQ(240, iodev.min_cb_level);

  // Test that state is normal run even if there is start ops.
  EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state);
}

static int simple_no_stream(struct cras_iodev *dev, int enable)
{
  simple_no_stream_enable = enable;
  simple_no_stream_called++;
  return 0;
}

TEST(IoDev, AddRmStream) {
  struct cras_iodev iodev;
  struct cras_rstream rstream1, rstream2;
  struct dev_stream stream1, stream2;

  memset(&iodev, 0, sizeof(iodev));
  iodev.open_dev = open_dev;
  iodev.no_stream = simple_no_stream;
  iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
  rstream1.cb_threshold = 800;
  stream1.stream = &rstream1;
  rstream2.cb_threshold = 400;
  stream2.stream = &rstream2;
  ResetStubData();

  iodev_buffer_size = 1024;
  cras_iodev_open(&iodev, rstream1.cb_threshold);
  EXPECT_EQ(0, iodev.max_cb_level);
  EXPECT_EQ(512, iodev.min_cb_level);

  /* min_cb_level should not exceed half the buffer size. */
  cras_iodev_add_stream(&iodev, &stream1);
  EXPECT_EQ(800, iodev.max_cb_level);
  EXPECT_EQ(512, iodev.min_cb_level);

  cras_iodev_add_stream(&iodev, &stream2);
  EXPECT_EQ(800, iodev.max_cb_level);
  EXPECT_EQ(400, iodev.min_cb_level);

  cras_iodev_rm_stream(&iodev, &rstream1);
  EXPECT_EQ(400, iodev.max_cb_level);
  EXPECT_EQ(400, iodev.min_cb_level);
  EXPECT_EQ(0, simple_no_stream_called);

  /* When all streams are removed, keep the last min_cb_level for draining. */
  cras_iodev_rm_stream(&iodev, &rstream2);
  EXPECT_EQ(0, iodev.max_cb_level);
  EXPECT_EQ(400, iodev.min_cb_level);
}

TEST(IoDev, FillZeros) {
  struct cras_iodev iodev;
  struct cras_audio_format fmt;
  unsigned int frames = 50;
  int16_t *zeros;
  int rc;

  ResetStubData();

  memset(&iodev, 0, sizeof(iodev));
  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;
  iodev.ext_format = &fmt;
  iodev.get_buffer = get_buffer;
  iodev.put_buffer = put_buffer;

  iodev.direction = CRAS_STREAM_INPUT;
  rc = cras_iodev_fill_odev_zeros(&iodev, frames);
  EXPECT_EQ(-EINVAL, rc);

  iodev.direction = CRAS_STREAM_OUTPUT;
  rc = cras_iodev_fill_odev_zeros(&iodev, frames);

  EXPECT_EQ(0, rc);
  EXPECT_EQ(frames, put_buffer_nframes);
  zeros = (int16_t *)calloc(frames * 2, sizeof(*zeros));
  rc = memcmp(audio_buffer, zeros, frames * 2 * 2);
  free(zeros);
  EXPECT_EQ(0, rc);
}

TEST(IoDev, DefaultNoStreamPlaybackRunning) {
  struct cras_iodev iodev;
  struct cras_audio_format fmt;
  unsigned int hw_level = 50;
  unsigned int min_cb_level = 240;
  unsigned int zeros_to_fill;
  int16_t *zeros;
  int rc;

  memset(&iodev, 0, sizeof(iodev));

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;
  iodev.ext_format = &fmt;
  iodev.min_cb_level = min_cb_level;
  iodev.get_buffer = get_buffer;
  iodev.put_buffer = put_buffer;
  iodev.frames_queued = frames_queued;
  iodev.min_buffer_level = 0;
  iodev.direction = CRAS_STREAM_OUTPUT;
  iodev.buffer_size = BUFFER_SIZE;
  iodev.no_stream = no_stream;

  ResetStubData();

  // Device is running. hw_level is less than target.
  // Need to fill to callback level * 2;
  iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
  fr_queued = hw_level;
  zeros_to_fill = min_cb_level * 2 - hw_level;

  rc = cras_iodev_default_no_stream_playback(&iodev, 1);

  EXPECT_EQ(0, rc);
  EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state);
  EXPECT_EQ(zeros_to_fill, put_buffer_nframes);
  zeros = (int16_t *)calloc(zeros_to_fill * 2, sizeof(*zeros));
  EXPECT_EQ(0, memcmp(audio_buffer, zeros, zeros_to_fill * 2 * 2));
  free(zeros);

  ResetStubData();

  // Device is running. hw_level is not less than target.
  // No need to fill zeros.
  iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
  hw_level = min_cb_level * 2;
  fr_queued = hw_level;
  zeros_to_fill = 0;

  rc = cras_iodev_default_no_stream_playback(&iodev, 1);
  EXPECT_EQ(0, rc);
  EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state);
  EXPECT_EQ(zeros_to_fill, put_buffer_nframes);
}

TEST(IoDev, PrepareOutputBeforeWriteSamples) {
  struct cras_iodev iodev;
  struct cras_audio_format fmt;
  unsigned int min_cb_level = 240;
  int rc;
  struct cras_rstream rstream1;
  struct dev_stream stream1;
  struct cras_iodev_info info;

  ResetStubData();

  rstream1.cb_threshold = min_cb_level;
  stream1.stream = &rstream1;

  memset(&iodev, 0, sizeof(iodev));

  fmt.format = SND_PCM_FORMAT_S16_LE;
  fmt.frame_rate = 48000;
  fmt.num_channels = 2;
  iodev.ext_format = &fmt;
  iodev.min_cb_level = min_cb_level;
  iodev.get_buffer = get_buffer;
  iodev.put_buffer = put_buffer;
  iodev.frames_queued = frames_queued;
  iodev.min_buffer_level = 0;
  iodev.direction = CRAS_STREAM_OUTPUT;
  iodev.buffer_size = BUFFER_SIZE;
  iodev.no_stream = no_stream;
  iodev.open_dev = open_dev;
  iodev.start = fake_start;
  iodev.info = info;
  iodev_buffer_size = BUFFER_SIZE;

  // Open device.
  cras_iodev_open(&iodev, rstream1.cb_threshold);

  // Add one stream to device.
  cras_iodev_add_stream(&iodev, &stream1);

  // Case 1: Assume device is not started yet.
  iodev.state = CRAS_IODEV_STATE_OPEN;
  // Assume sample is not ready yet.
  dev_stream_playback_frames_ret = 0;

  rc = cras_iodev_prepare_output_before_write_samples(&iodev);

  EXPECT_EQ(0, rc);
  // Device should remain in open state.
  EXPECT_EQ(CRAS_IODEV_STATE_OPEN, iodev.state);
  EXPECT_EQ(0, no_stream_called);

  // Assume now sample is ready.
  dev_stream_playback_frames_ret = 100;

  rc = cras_iodev_prepare_output_before_write_samples(&iodev);

  EXPECT_EQ(0, rc);
  // Device should enter normal run state.
  EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state);
  EXPECT_EQ(0, no_stream_called);
  // Need to fill 1 callback level of zeros;
  EXPECT_EQ(min_cb_level, put_buffer_nframes);

  ResetStubData();

  // Case 2: Assume device is started and is in no stream state.
  iodev.state = CRAS_IODEV_STATE_NO_STREAM_RUN;
  // Sample is not ready yet.
  dev_stream_playback_frames_ret = 0;

  rc = cras_iodev_prepare_output_before_write_samples(&iodev);

  EXPECT_EQ(0, rc);
  // Device should remain in no_stream state.
  EXPECT_EQ(CRAS_IODEV_STATE_NO_STREAM_RUN, iodev.state);
  // Device in no_stream state should call no_stream ops once.
  EXPECT_EQ(1, no_stream_called);
  EXPECT_EQ(1, no_stream_enable);

  // Assume now sample is ready.
  dev_stream_playback_frames_ret = 100;

  rc = cras_iodev_prepare_output_before_write_samples(&iodev);

  EXPECT_EQ(0, rc);
  // Device should enter normal run state.
  EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state);
  // Device should call no_stream ops with enable=0 to leave no stream state.
  EXPECT_EQ(2, no_stream_called);
  EXPECT_EQ(0, no_stream_enable);

  ResetStubData();

  // Case 3: Assume device is started and is in normal run state.
  iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;

  rc = cras_iodev_prepare_output_before_write_samples(&iodev);

  EXPECT_EQ(0, rc);
  // Device should remain in normal run state.
  EXPECT_EQ(CRAS_IODEV_STATE_NORMAL_RUN, iodev.state);
  // Device in no_stream state should call no_stream ops once.
  EXPECT_EQ(0, no_stream_called);

  ResetStubData();
}

TEST(IoDev, OutputDeviceShouldWake) {
  struct cras_iodev iodev;
  int rc;

  memset(&iodev, 0, sizeof(iodev));

  ResetStubData();

  // Device is not running. No need to wake for this device.
  iodev.state = CRAS_IODEV_STATE_OPEN;
  rc = cras_iodev_odev_should_wake(&iodev);
  EXPECT_EQ(0, rc);

  // Device is running. Need to wake for this device.
  iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
  rc = cras_iodev_odev_should_wake(&iodev);
  EXPECT_EQ(1, rc);

  // Device is running. Device has output_should_wake ops.
  iodev.output_should_wake = output_should_wake;
  output_should_wake_ret = 0;
  rc = cras_iodev_odev_should_wake(&iodev);
  EXPECT_EQ(0, rc);

  // Device is running. Device has output_should_wake ops.
  output_should_wake_ret = 1;
  rc = cras_iodev_odev_should_wake(&iodev);
  EXPECT_EQ(1, rc);

  // Ignore input device.
  iodev.direction = CRAS_STREAM_INPUT;
  rc = cras_iodev_odev_should_wake(&iodev);
  EXPECT_EQ(0, rc);
}

TEST(IoDev, FramesToPlayInSleep) {
  struct cras_iodev iodev;
  unsigned int min_cb_level = 240, hw_level;
  unsigned int got_hw_level, got_frames;
  struct timespec hw_tstamp;

  memset(&iodev, 0, sizeof(iodev));
  iodev.frames_queued = frames_queued;
  iodev.min_buffer_level = 0;
  iodev.direction = CRAS_STREAM_OUTPUT;
  iodev.buffer_size = BUFFER_SIZE;
  iodev.min_cb_level = min_cb_level;

  ResetStubData();

  // Device is running. There is at least one stream for this device.
  // hw_level is greater than min_cb_level.
  iodev.state = CRAS_IODEV_STATE_NORMAL_RUN;
  hw_level = min_cb_level + 50;
  fr_queued = hw_level;
  iodev.streams = reinterpret_cast<struct dev_stream *>(0x1);

  got_frames = cras_iodev_frames_to_play_in_sleep(
                   &iodev, &got_hw_level, &hw_tstamp);
  EXPECT_EQ(hw_level, got_hw_level);
  EXPECT_EQ(hw_level, got_frames);

  // Device is running. There is no stream for this device.
  // hw_level is greater than min_cb_level.
  iodev.streams = NULL;

  got_frames = cras_iodev_frames_to_play_in_sleep(
                   &iodev, &got_hw_level, &hw_tstamp);
  EXPECT_EQ(hw_level, got_hw_level);
  EXPECT_EQ(hw_level - min_cb_level, got_frames);

  // Device is running. There is no stream for this device.
  // hw_level is less than min_cb_level.
  iodev.streams = NULL;
  hw_level = min_cb_level - 50;
  fr_queued = hw_level;

  got_frames = cras_iodev_frames_to_play_in_sleep(
                   &iodev, &got_hw_level, &hw_tstamp);
  EXPECT_EQ(hw_level, got_hw_level);
  EXPECT_EQ(0, got_frames);
}

static unsigned int get_num_underruns(const struct cras_iodev *iodev) {
  return get_num_underruns_ret;
}

TEST(IoDev, GetNumUnderruns) {
  struct cras_iodev iodev;
  memset(&iodev, 0, sizeof(iodev));

  EXPECT_EQ(0, cras_iodev_get_num_underruns(&iodev));

  iodev.get_num_underruns = get_num_underruns;
  get_num_underruns_ret = 10;
  EXPECT_EQ(10, cras_iodev_get_num_underruns(&iodev));
}

extern "C" {

//  From libpthread.
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine)(void*), void *arg) {
  return 0;
}

int pthread_join(pthread_t thread, void **value_ptr) {
  return 0;
}

// From audio_thread
struct cras_fmt_conv *audio_thread_get_global_remix_converter()
{
  return NULL;
}

// Fromt fmt_conv
void cras_channel_remix_convert(struct cras_fmt_conv *conv,
    uint8_t *in_buf,
    size_t frames)
{
}

// From buffer_share
struct buffer_share *buffer_share_create(unsigned int buf_sz) {
  return NULL;
}

void buffer_share_destroy(struct buffer_share *mix)
{
}

int buffer_share_offset_update(struct buffer_share *mix, unsigned int id,
                               unsigned int frames) {
  return 0;
}

unsigned int buffer_share_get_new_write_point(struct buffer_share *mix) {
  return 0;
}

int buffer_share_add_id(struct buffer_share *mix, unsigned int id) {
  return 0;
}

int buffer_share_rm_id(struct buffer_share *mix, unsigned int id) {
  return 0;
}

unsigned int buffer_share_id_offset(const struct buffer_share *mix,
                                    unsigned int id)
{
  return 0;
}

// From cras_system_state.
void cras_system_state_stream_added(enum CRAS_STREAM_DIRECTION direction) {
}

void cras_system_state_stream_removed(enum CRAS_STREAM_DIRECTION direction) {
}

// From cras_dsp
struct cras_dsp_context *cras_dsp_context_new(int sample_rate,
                                              const char *purpose)
{
  dsp_context_new_sample_rate = sample_rate;
  dsp_context_new_purpose = purpose;
  return cras_dsp_context_new_return;
}

void cras_dsp_context_free(struct cras_dsp_context *ctx)
{
  dsp_context_free_called++;
}

void cras_dsp_load_pipeline(struct cras_dsp_context *ctx)
{
}

void cras_dsp_set_variable_string(struct cras_dsp_context *ctx, const char *key,
                                  const char *value)
{
}

struct pipeline *cras_dsp_get_pipeline(struct cras_dsp_context *ctx)
{
  cras_dsp_get_pipeline_called++;
  return reinterpret_cast<struct pipeline *>(cras_dsp_get_pipeline_ret);
}

void cras_dsp_put_pipeline(struct cras_dsp_context *ctx)
{
  cras_dsp_put_pipeline_called++;
}

float *cras_dsp_pipeline_get_source_buffer(struct pipeline *pipeline,
					   int index)
{
  cras_dsp_pipeline_get_source_buffer_called++;
  return cras_dsp_pipeline_source_buffer[index];
}

float *cras_dsp_pipeline_get_sink_buffer(struct pipeline *pipeline, int index)
{
  cras_dsp_pipeline_get_sink_buffer_called++;
  return cras_dsp_pipeline_sink_buffer[index];
}

int cras_dsp_pipeline_get_delay(struct pipeline *pipeline)
{
  cras_dsp_pipeline_get_delay_called++;
  return 0;
}

void cras_dsp_pipeline_apply(struct pipeline *pipeline,
			     uint8_t *buf, unsigned int frames)
{
  cras_dsp_pipeline_apply_called++;
  cras_dsp_pipeline_apply_sample_count = frames;
}

void cras_dsp_pipeline_add_statistic(struct pipeline *pipeline,
                                     const struct timespec *time_delta,
                                     int samples)
{
}

unsigned int cras_dsp_num_output_channels(const struct cras_dsp_context *ctx)
{
	return cras_dsp_num_output_channels_return;
}

unsigned int cras_dsp_num_input_channels(const struct cras_dsp_context *ctx)
{
	return cras_dsp_num_input_channels_return;
}

// From audio thread
int audio_thread_post_message(struct audio_thread *thread,
                              struct audio_thread_msg *msg) {
  return 0;
}

void cras_iodev_list_select_node(enum CRAS_STREAM_DIRECTION direction,
                                 cras_node_id_t node_id)
{
  select_node_called++;
  select_node_direction = direction;
  select_node_id = node_id;
}

int cras_iodev_list_node_selected(struct cras_ionode *node)
{
  return node == node_selected;
}

void cras_iodev_list_disable_dev(struct cras_iodev *dev)
{
  cras_iodev_list_disable_dev_called++;
}

void cras_iodev_list_notify_nodes_changed()
{
  notify_nodes_changed_called++;
}

void cras_iodev_list_notify_active_node_changed(
				enum CRAS_STREAM_DIRECTION direction)
{
  notify_active_node_changed_called++;
}

void cras_iodev_list_notify_node_volume(struct cras_ionode *node)
{
	notify_node_volume_called++;
}

void cras_iodev_list_notify_node_capture_gain(struct cras_ionode *node)
{
	notify_node_capture_gain_called++;
}

void cras_iodev_list_notify_node_left_right_swapped(struct cras_ionode *node)
{
  notify_node_left_right_swapped_called++;
}

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

void cras_audio_area_destroy(struct cras_audio_area *area) {
}

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

int cras_audio_format_set_channel_layout(struct cras_audio_format *format,
					 const int8_t layout[CRAS_CH_MAX])
{
  int i;
  cras_audio_format_set_channel_layout_called++;
  for (i = 0; i < CRAS_CH_MAX; i++)
    format->channel_layout[i] = layout[i];
  return 0;
}

float softvol_get_scaler(unsigned int volume_index)
{
	return softvol_scalers[volume_index];
}

size_t cras_system_get_volume() {
  return cras_system_get_volume_return;
}

long cras_system_get_capture_gain() {
  return cras_system_get_capture_gain_ret_value;
}

int cras_system_get_mute() {
  return cras_system_get_mute_return;
}

int cras_system_get_capture_mute() {
  return 0;
}

void cras_scale_buffer(snd_pcm_format_t fmt, uint8_t *buffer,
                       unsigned int count, float scaler) {
  cras_scale_buffer_fmt = fmt;
  cras_scale_buffer_scaler = scaler;
}

size_t cras_mix_mute_buffer(uint8_t *dst,
                            size_t frame_bytes,
                            size_t count) {
  cras_mix_mute_count = count;
  return count;
}

struct rate_estimator *rate_estimator_create(unsigned int rate,
                                             const struct timespec *window_size,
                                             double smooth_factor) {
  return NULL;
}

void rate_estimator_destroy(struct rate_estimator *re) {
}

void rate_estimator_add_frames(struct rate_estimator *re, int fr) {
  rate_estimator_add_frames_called++;
  rate_estimator_add_frames_num_frames = fr;
}

int rate_estimator_check(struct rate_estimator *re, int level,
                         struct timespec *now) {
  return 0;
}

void rate_estimator_reset_rate(struct rate_estimator *re, unsigned int rate) {
}

double rate_estimator_get_rate(struct rate_estimator *re) {
  return 0.0;
}

unsigned int dev_stream_cb_threshold(const struct dev_stream *dev_stream) {
  if (dev_stream->stream)
    return dev_stream->stream->cb_threshold;
  return 0;
}

int dev_stream_attached_devs(const struct dev_stream *dev_stream) {
  return 1;
}

void dev_stream_update_frames(const struct dev_stream *dev_stream) {
}

int dev_stream_playback_frames(const struct dev_stream *dev_stream) {
  return dev_stream_playback_frames_ret;
}

}  // extern "C"
}  //  namespace

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

  audio_thread_event_log_deinit(atlog);
  return rc;
}