summaryrefslogtreecommitdiff
path: root/cras/src/tests/timing_unittest.cc
blob: 8a2de65f05302a8cefe7daa210cb8618b8823f11 (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
// Copyright 2017 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 <stdint.h>
#include <stdio.h>
#include <syslog.h>
#include <time.h>

#include <memory>

extern "C" {
#include "cras_iodev.h"    // stubbed
#include "cras_rstream.h"  // stubbed
#include "cras_shm.h"
#include "cras_types.h"
#include "dev_io.h"      // tested
#include "dev_stream.h"  // tested
#include "utlist.h"

struct audio_thread_event_log* atlog;
}

#include "dev_io_stubs.h"
#include "iodev_stub.h"
#include "metrics_stub.h"
#include "rstream_stub.h"

#define FAKE_POLL_FD 33

namespace {

class TimingSuite : public testing::Test {
 protected:
  virtual void SetUp() {
    atlog = static_cast<audio_thread_event_log*>(calloc(1, sizeof(*atlog)));
    iodev_stub_reset();
    rstream_stub_reset();
  }

  virtual void TearDown() { free(atlog); }

  timespec SingleInputDevNextWake(
      size_t dev_cb_threshold,
      size_t dev_level,
      const timespec* level_timestamp,
      cras_audio_format* dev_format,
      const std::vector<StreamPtr>& streams,
      CRAS_NODE_TYPE active_node_type = CRAS_NODE_TYPE_MIC) {
    struct open_dev* dev_list_ = NULL;

    DevicePtr dev = create_device(CRAS_STREAM_INPUT, dev_cb_threshold,
                                  dev_format, active_node_type);
    dev->dev->input_streaming = true;
    DL_APPEND(dev_list_, dev->odev.get());

    for (auto const& stream : streams) {
      add_stream_to_dev(dev->dev, stream);
    }

    // Set response for frames_queued.
    iodev_stub_frames_queued(dev->dev.get(), dev_level, *level_timestamp);

    dev_io_send_captured_samples(dev_list_);

    struct timespec dev_time;
    dev_time.tv_sec = level_timestamp->tv_sec + 500;  // Far in the future.
    dev_io_next_input_wake(&dev_list_, &dev_time);
    return dev_time;
  }

  timespec SingleOutputDevNextWake(
      size_t dev_cb_threshold,
      size_t dev_level,
      const timespec* level_timestamp,
      cras_audio_format* dev_format,
      const std::vector<StreamPtr>& streams,
      const timespec* dev_wake_ts,
      CRAS_NODE_TYPE active_node_type = CRAS_NODE_TYPE_HEADPHONE) {
    struct open_dev* dev_list_ = NULL;

    DevicePtr dev = create_device(CRAS_STREAM_OUTPUT, dev_cb_threshold,
                                  dev_format, active_node_type);
    DL_APPEND(dev_list_, dev->odev.get());

    for (auto const& stream : streams) {
      add_stream_to_dev(dev->dev, stream);
    }

    dev->odev->wake_ts = *dev_wake_ts;

    // Set response for frames_queued.
    iodev_stub_frames_queued(dev->dev.get(), dev_level, *level_timestamp);

    struct timespec dev_time;
    dev_time.tv_sec = level_timestamp->tv_sec + 500;  // Far in the future.
    dev_io_next_output_wake(&dev_list_, &dev_time);
    return dev_time;
  }
};

extern "C" {
//  From librt. Fix now at this time.
int clock_gettime(clockid_t clk_id, struct timespec* tp) {
  tp->tv_sec = 12345;
  tp->tv_nsec = 987654321;
  return 0;
}
};

// Add a new input stream, make sure the initial next_cb_ts is 0.
TEST_F(TimingSuite, NewInputStreamInit) {
  struct open_dev* dev_list_ = NULL;

  cras_audio_format format;
  fill_audio_format(&format, 48000);
  DevicePtr dev =
      create_device(CRAS_STREAM_INPUT, 1024, &format, CRAS_NODE_TYPE_MIC);
  DL_APPEND(dev_list_, dev->odev.get());
  struct cras_iodev* iodev = dev->odev->dev;

  ShmPtr shm = create_shm(480);
  RstreamPtr rstream =
      create_rstream(1, CRAS_STREAM_INPUT, 480, &format, shm.get());

  dev_io_append_stream(&dev_list_, rstream.get(), &iodev, 1);

  EXPECT_EQ(0, rstream->next_cb_ts.tv_sec);
  EXPECT_EQ(0, rstream->next_cb_ts.tv_nsec);

  dev_stream_destroy(iodev->streams);
}

// There is the pseudo code about wake up time for an input device.
//
// function set_input_dev_wake_ts(dev):
//   wake_ts = now + 20s #rule_1
//
//   cap_limit = MIN(dev_stream_capture_avail(stream)) for stream on dev
//
//   for stream in dev:
//   wake_ts = MIN(get_input_wake_time(stream, cap_limit), wake_ts)
//             for stream on dev #rule_2
//   if cap_limit:
//     wake_ts = MIN(get_input_dev_max_wake_ts(dev), wake_ts) #rule_3
//
//   device.wake_ts = wake_ts
//
// function get_input_wake_time(stream, cap_limit):
//   needed_frames_from_device = dev_stream_capture_avail(stream)
//
//   if needed_frames_from_device > cap_limit: #rule_4
//     return None
//
//   if stream is USE_DEV_TIMING and stream is pending reply: #rule_5
//     return None
//
//   time_for_sample = The time when device gets enough samples #rule_6
//
//   wake_time_out = MAX(stream.next_cb_ts, time_for_sample) #rule_7
//
//   if stream is USE_DEV_TIMING:
//     wake_time_out =  time_for_sample #rule_8
//
//   return wake_time_out
//
// function get_input_dev_max_wake_ts(dev):
//   return MAX(5ms, The time when hw_level = buffer_size / 2) #rule_9
//
//
// dev_stream_capture_avail: The number of frames free to be written to in a
//                           capture stream.
//
// The following unittests will check these logics.

// Test rule_1.
// The device wake up time should be 20s from now.
TEST_F(TimingSuite, InputWakeTimeNoStreamWithBigBufferDevice) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  std::vector<StreamPtr> streams;
  timespec dev_time =
      SingleInputDevNextWake(4800000, 0, &start, &format, streams);

  const timespec add_millis = {20, 0};
  add_timespecs(&start, &add_millis);
  EXPECT_EQ(start.tv_sec, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_2, rule_4(Stream 1), rule_7(Stream 2)
// Stream 1: next_cb_ts = now, cb_threshold = 480, dev_offset = 0
// Stream 2: next_cb_ts = now + 5s, cb_threshold = 480, dev_offset = 200
// Stream 1 need 480 frames and Stream 2 need 240 frames. So 240 will be the
// cap_limit and Stream 1 will be ignored. The next wake up time should be
// the next_cb_ts of stream2.
TEST_F(TimingSuite, InputWakeTimeTwoStreamsWithFramesInside) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream1 = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &format);
  stream1->rstream->next_cb_ts = start;

  StreamPtr stream2 = create_stream(1, 2, CRAS_STREAM_INPUT, 480, &format);
  stream2->rstream->next_cb_ts = start;
  stream2->rstream->next_cb_ts.tv_sec += 5;
  rstream_stub_dev_offset(stream2->rstream.get(), 1, 200);

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream1));
  streams.emplace_back(std::move(stream2));
  timespec dev_time =
      SingleInputDevNextWake(480000, 0, &start, &format, streams);

  EXPECT_EQ(start.tv_sec + 5, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_2, rule_7
// Stream 1: next_cb_ts = now + 2s, cb_threshold = 480, dev_offset = 0
// Stream 2: next_cb_ts = now + 5s, cb_threshold = 480, dev_offset = 0
// The audio thread will choose the earliest next_cb_ts because the they have
// the same value of needed_frames_from_device. The next wake up time should
// be the next_cb_ts of stream1.
TEST_F(TimingSuite, InputWakeTimeTwoEmptyStreams) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream1 = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &format);
  stream1->rstream->next_cb_ts = start;
  stream1->rstream->next_cb_ts.tv_sec += 2;

  StreamPtr stream2 = create_stream(1, 2, CRAS_STREAM_INPUT, 480, &format);
  stream2->rstream->next_cb_ts = start;
  stream2->rstream->next_cb_ts.tv_sec += 5;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream1));
  streams.emplace_back(std::move(stream2));
  timespec dev_time =
      SingleInputDevNextWake(480000, 0, &start, &format, streams);

  EXPECT_EQ(start.tv_sec + 2, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_3.
// If cap_limit is zero from stream, input_dev_max_wake_ts should not
// be taken into account.
TEST_F(TimingSuite, InputWakeTimeOneFullStreamWithDeviceWakeUp) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &format);

  struct timespec start, stream_wake;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  // Set the next stream wake to be 10ms from now.
  const timespec ten_millis = {0, 10 * 1000 * 1000};
  stream_wake = start;
  add_timespecs(&stream_wake, &ten_millis);
  stream->rstream->next_cb_ts = stream_wake;

  // Add fake data so the stream has no room for more data.
  AddFakeDataToStream(stream.get(), 480);

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec wake_time = SingleInputDevNextWake(240, 0, &start, &format, streams);

  // Input device would wake at 5ms from now, but since stream cap_limit == 0
  // the final wake_time is determined by stream.
  EXPECT_EQ(stream_wake.tv_sec, wake_time.tv_sec);
  EXPECT_EQ(stream_wake.tv_nsec, wake_time.tv_nsec);
}

// Test rule_3 and rule_9.
// One empty stream with small device buffer. It should wake up when there are
// buffer_size / 2 frames in device buffer.
TEST_F(TimingSuite, InputWakeTimeOneStreamWithDeviceWakeUp) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &format);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  // The next callback of the new stream is 0.
  stream->rstream->next_cb_ts.tv_sec = 0;
  stream->rstream->next_cb_ts.tv_nsec = 0;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time = SingleInputDevNextWake(240, 0, &start, &format, streams);
  // The device wake up time should be 5ms from now. At that time there are
  // 240 frames in the device.
  const timespec add_millis = {0, 5 * 1000 * 1000};
  add_timespecs(&start, &add_millis);
  EXPECT_EQ(start.tv_sec, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_5.
// The stream with USE_DEV_TIMING flag will be ignore if it is pending reply.
TEST_F(TimingSuite, InputWakeTimeOneStreamUsingDevTimingWithPendingReply) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &format);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  // The next callback should be ignored.
  stream->rstream->next_cb_ts = start;
  stream->rstream->next_cb_ts.tv_sec += 10;
  stream->rstream->flags = USE_DEV_TIMING;
  rstream_stub_pending_reply(stream->rstream.get(), 1);

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time = SingleInputDevNextWake(4800, 0, &start, &format, streams);

  // The device wake up time should be 100ms from now. At that time the hw_level
  // is buffer_size / 2.
  const timespec add_millis = {0, 100 * 1000 * 1000};
  add_timespecs(&start, &add_millis);
  EXPECT_EQ(start.tv_sec, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_6.
// Add a new stream, the wake up time is the time when it has enough data to
// post.
TEST_F(TimingSuite, InputWakeTimeOneStreamWithEmptyDevice) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &format);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  // The next callback of the new stream is 0.
  stream->rstream->next_cb_ts.tv_sec = 0;
  stream->rstream->next_cb_ts.tv_nsec = 0;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time = SingleInputDevNextWake(600, 0, &start, &format, streams);

  // The device wake up time should be 10ms from now. At that time the
  // stream will have 480 samples to post.
  const timespec ten_millis = {0, 10 * 1000 * 1000};
  add_timespecs(&start, &ten_millis);
  EXPECT_EQ(0, streams[0]->rstream->next_cb_ts.tv_sec);
  EXPECT_EQ(0, streams[0]->rstream->next_cb_ts.tv_nsec);
  EXPECT_EQ(start.tv_sec, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_6.
// Add a new stream with enough frames in device, check the wake up time is
// right now.
TEST_F(TimingSuite, InputWakeTimeOneStreamWithFullDevice) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &format);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  // The next callback of the new stream is 0.
  stream->rstream->next_cb_ts.tv_sec = 0;
  stream->rstream->next_cb_ts.tv_nsec = 0;

  // If there are enough frames in the device, we should wake up immediately.
  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time =
      SingleInputDevNextWake(480, 480, &start, &format, streams);
  EXPECT_EQ(0, streams[0]->rstream->next_cb_ts.tv_sec);
  EXPECT_EQ(0, streams[0]->rstream->next_cb_ts.tv_nsec);
  EXPECT_EQ(start.tv_sec, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_8.
// The stream with USE_DEV_TIMING flag should wake up when it has enough frames
// to post.
TEST_F(TimingSuite, InputWakeTimeOneStreamUsingDevTiming) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &format);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  // The next callback should be ignored.
  stream->rstream->next_cb_ts = start;
  stream->rstream->next_cb_ts.tv_sec += 10;
  stream->rstream->flags = USE_DEV_TIMING;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time = SingleInputDevNextWake(600, 0, &start, &format, streams);

  // The device wake up time should be 10ms from now. At that time the
  // stream will have 480 samples to post.
  const timespec add_millis = {0, 10 * 1000 * 1000};
  add_timespecs(&start, &add_millis);
  EXPECT_EQ(start.tv_sec, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_9.
// The device wake up time should be 10ms from now. At that time the hw_level
// is buffer_size / 2.
TEST_F(TimingSuite, InputWakeTimeNoStreamSmallBufferDevice) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  std::vector<StreamPtr> streams;
  timespec dev_time = SingleInputDevNextWake(480, 0, &start, &format, streams);

  const timespec add_millis = {0, 10 * 1000 * 1000};
  add_timespecs(&start, &add_millis);
  EXPECT_EQ(start.tv_sec, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_9.
// There are more than buffer_size / 2 frames in the device. The device needs
// to sleep at least 5ms.
TEST_F(TimingSuite, InputWakeTimeOneStreamWithEnoughFramesInDevice) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &format);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  // Make next_cb_ts far from now.
  stream->rstream->next_cb_ts = start;
  stream->rstream->next_cb_ts.tv_sec += 10;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time =
      SingleInputDevNextWake(480, 480, &start, &format, streams);

  const timespec add_millis = {0, 5 * 1000 * 1000};
  add_timespecs(&start, &add_millis);
  EXPECT_EQ(start.tv_sec, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// One device, one stream, write a callback of data and check the sleep time is
// one more wakeup interval.
TEST_F(TimingSuite, WaitAfterFill) {
  const size_t cb_threshold = 480;

  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream =
      create_stream(1, 1, CRAS_STREAM_INPUT, cb_threshold, &format);
  // rstream's next callback is now and there is enough data to fill.
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  stream->rstream->next_cb_ts = start;
  AddFakeDataToStream(stream.get(), 480);

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time =
      SingleInputDevNextWake(cb_threshold, 0, &start, &format, streams);

  // The next callback should be scheduled 10ms in the future.
  // And the next wake up should reflect the only attached stream.
  const timespec ten_millis = {0, 10 * 1000 * 1000};
  add_timespecs(&start, &ten_millis);
  EXPECT_EQ(start.tv_sec, streams[0]->rstream->next_cb_ts.tv_sec);
  EXPECT_EQ(start.tv_nsec, streams[0]->rstream->next_cb_ts.tv_nsec);
  EXPECT_EQ(dev_time.tv_sec, streams[0]->rstream->next_cb_ts.tv_sec);
  EXPECT_EQ(dev_time.tv_nsec, streams[0]->rstream->next_cb_ts.tv_nsec);
}

// One device with one stream which has block_size larger than the device buffer
// level. If the device buffer level = 0, the input device wake time should be
// set to (buffer_size / 2) / device_rate secs.
TEST_F(TimingSuite, LargeCallbackStreamWithEmptyBuffer) {
  const size_t cb_threshold = 3000;
  const size_t dev_cb_threshold = 1200;
  const size_t dev_level = 0;

  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream =
      create_stream(1, 1, CRAS_STREAM_INPUT, cb_threshold, &format);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  stream->rstream->next_cb_ts = start;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time = SingleInputDevNextWake(dev_cb_threshold, dev_level,
                                             &start, &format, streams);

  struct timespec delta;
  subtract_timespecs(&dev_time, &start, &delta);
  // The next dev wake ts should be 25ms since the buffer level is empty and
  // 1200 / 48000 = 0.025.
  EXPECT_EQ(delta.tv_sec, 0);
  EXPECT_LT(delta.tv_nsec, 25000000 + 5000 * 1000);
  EXPECT_GT(delta.tv_nsec, 25000000 - 5000 * 1000);
}

// One device with one stream which has block_size larger than the device buffer
// level. If the device buffer level = buffer_size / 2, the input device wake
// time should be set to max(0, 5ms) = 5ms to prevent busy loop occurs.
TEST_F(TimingSuite, LargeCallbackStreamWithHalfFullBuffer) {
  const size_t cb_threshold = 3000;
  const size_t dev_cb_threshold = 1200;
  const size_t dev_level = 1200;

  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream =
      create_stream(1, 1, CRAS_STREAM_INPUT, cb_threshold, &format);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  stream->rstream->next_cb_ts = start;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time = SingleInputDevNextWake(dev_cb_threshold, dev_level,
                                             &start, &format, streams);

  struct timespec delta;
  subtract_timespecs(&dev_time, &start, &delta);
  // The next dev wake ts should be 5ms since the buffer level is half full.
  EXPECT_EQ(delta.tv_sec, 0);
  EXPECT_LT(delta.tv_nsec, 5000000 + 5000 * 1000);
  EXPECT_GT(delta.tv_nsec, 5000000 - 5000 * 1000);
}

// One device(48k), one stream(44.1k), write a callback of data and check that
// the sleep time is correct when doing SRC.
TEST_F(TimingSuite, WaitAfterFillSRC) {
  cras_audio_format dev_format;
  fill_audio_format(&dev_format, 48000);
  cras_audio_format stream_format;
  fill_audio_format(&stream_format, 44100);

  StreamPtr stream =
      create_stream(1, 1, CRAS_STREAM_INPUT, 441, &stream_format);
  // rstream's next callback is now and there is enough data to fill.
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  stream->rstream->next_cb_ts = start;
  AddFakeDataToStream(stream.get(), 441);

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time =
      SingleInputDevNextWake(480, 0, &start, &dev_format, streams);

  // The next callback should be scheduled 10ms in the future.
  struct timespec delta;
  subtract_timespecs(&dev_time, &start, &delta);
  EXPECT_LT(9900 * 1000, delta.tv_nsec);
  EXPECT_GT(10100 * 1000, delta.tv_nsec);
}

// One device, two streams. One stream is ready the other still needs data.
// Checks that the sleep interval is based on the time the device will take to
// supply the needed samples for stream2.
TEST_F(TimingSuite, WaitTwoStreamsSameFormat) {
  const size_t cb_threshold = 480;

  cras_audio_format format;
  fill_audio_format(&format, 48000);

  // stream1's next callback is now and there is enough data to fill.
  StreamPtr stream1 =
      create_stream(1, 1, CRAS_STREAM_INPUT, cb_threshold, &format);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  stream1->rstream->next_cb_ts = start;
  AddFakeDataToStream(stream1.get(), cb_threshold);

  // stream2 is only half full.
  StreamPtr stream2 =
      create_stream(1, 1, CRAS_STREAM_INPUT, cb_threshold, &format);
  stream2->rstream->next_cb_ts = start;
  AddFakeDataToStream(stream2.get(), 240);

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream1));
  streams.emplace_back(std::move(stream2));
  timespec dev_time =
      SingleInputDevNextWake(cb_threshold, 0, &start, &format, streams);

  // Should wait for approximately 5 milliseconds for 240 samples at 48k.
  struct timespec delta2;
  subtract_timespecs(&dev_time, &start, &delta2);
  EXPECT_LT(4900 * 1000, delta2.tv_nsec);
  EXPECT_GT(5100 * 1000, delta2.tv_nsec);
}

// One device(44.1), two streams(44.1, 48). One stream is ready the other still
// needs data. Checks that the sleep interval is based on the time the device
// will take to supply the needed samples for stream2, stream2 is sample rate
// converted from the 44.1k device to the 48k stream.
TEST_F(TimingSuite, WaitTwoStreamsDifferentRates) {
  cras_audio_format s1_format, s2_format;
  fill_audio_format(&s1_format, 44100);
  fill_audio_format(&s2_format, 48000);

  // stream1's next callback is now and there is enough data to fill.
  StreamPtr stream1 = create_stream(1, 1, CRAS_STREAM_INPUT, 441, &s1_format);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  stream1->rstream->next_cb_ts = start;
  AddFakeDataToStream(stream1.get(), 441);
  // stream2's next callback is now but there is only half a callback of data.
  StreamPtr stream2 = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &s2_format);
  stream2->rstream->next_cb_ts = start;
  AddFakeDataToStream(stream2.get(), 240);

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream1));
  streams.emplace_back(std::move(stream2));
  timespec dev_time =
      SingleInputDevNextWake(441, 0, &start, &s1_format, streams);

  // Should wait for approximately 5 milliseconds for 240 48k samples from the
  // 44.1k device.
  struct timespec delta2;
  subtract_timespecs(&dev_time, &start, &delta2);
  EXPECT_LT(4900 * 1000, delta2.tv_nsec);
  EXPECT_GT(5100 * 1000, delta2.tv_nsec);
}

// One device, two streams. Both streams get a full callback of data and the
// device has enough samples for the next callback already. Checks that the
// shorter of the two streams times is used for the next sleep interval.
TEST_F(TimingSuite, WaitTwoStreamsDifferentWakeupTimes) {
  cras_audio_format s1_format, s2_format;
  fill_audio_format(&s1_format, 44100);
  fill_audio_format(&s2_format, 48000);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  // stream1's next callback is in 3ms.
  StreamPtr stream1 = create_stream(1, 1, CRAS_STREAM_INPUT, 441, &s1_format);
  stream1->rstream->next_cb_ts = start;
  const timespec three_millis = {0, 3 * 1000 * 1000};
  add_timespecs(&stream1->rstream->next_cb_ts, &three_millis);
  AddFakeDataToStream(stream1.get(), 441);
  // stream2 is also ready next cb in 5ms..
  StreamPtr stream2 = create_stream(1, 1, CRAS_STREAM_INPUT, 480, &s2_format);
  stream2->rstream->next_cb_ts = start;
  const timespec five_millis = {0, 5 * 1000 * 1000};
  add_timespecs(&stream2->rstream->next_cb_ts, &five_millis);
  AddFakeDataToStream(stream1.get(), 480);

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream1));
  streams.emplace_back(std::move(stream2));
  timespec dev_time =
      SingleInputDevNextWake(441, 441, &start, &s1_format, streams);

  // Should wait for approximately 3 milliseconds for stream 1 first.
  struct timespec delta2;
  subtract_timespecs(&dev_time, &start, &delta2);
  EXPECT_LT(2900 * 1000, delta2.tv_nsec);
  EXPECT_GT(3100 * 1000, delta2.tv_nsec);
}

// One hotword stream attaches to hotword device. Input data has copied from
// device to stream but total number is less than cb_threshold. Hotword stream
// should be scheduled wake base on the samples needed to fill full shm.
TEST_F(TimingSuite, HotwordStreamUseDevTiming) {
  cras_audio_format fmt;
  fill_audio_format(&fmt, 48000);

  struct timespec start, delay;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_INPUT, 240, &fmt);
  stream->rstream->flags = HOTWORD_STREAM;
  stream->rstream->next_cb_ts = start;
  delay.tv_sec = 0;
  delay.tv_nsec = 3 * 1000 * 1000;
  add_timespecs(&stream->rstream->next_cb_ts, &delay);

  // Add fake data to stream and device so its slightly less than cb_threshold.
  // Expect to wait for samples to fill the full buffer (480 - 192) frames
  // instead of using the next_cb_ts.
  AddFakeDataToStream(stream.get(), 192);
  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  timespec dev_time = SingleInputDevNextWake(4096, 0, &start, &fmt, streams);
  struct timespec delta;
  subtract_timespecs(&dev_time, &start, &delta);
  // 288 frames worth of time = 6 ms.
  EXPECT_EQ(6 * 1000 * 1000, delta.tv_nsec);
}

// One hotword stream attaches to hotword device. Input data burst to a number
// larger than cb_threshold. Also, stream is pending client reply.
// In this case stream fd is used to poll for next wake.
// And the dev wake time is unchanged from the default 20 seconds limit.
TEST_F(TimingSuite, HotwordStreamBulkDataIsPending) {
  int poll_fd = 0;
  cras_audio_format fmt;
  fill_audio_format(&fmt, 48000);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_INPUT, 240, &fmt);
  stream->rstream->flags = HOTWORD_STREAM;
  stream->rstream->next_cb_ts = start;

  AddFakeDataToStream(stream.get(), 480);
  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  // Stream is pending the reply from client.
  rstream_stub_pending_reply(streams[0]->rstream.get(), 1);

  // There is more than 1 cb_threshold of data in device.
  timespec dev_time = SingleInputDevNextWake(4096, 7000, &start, &fmt, streams,
                                             CRAS_NODE_TYPE_HOTWORD);

  // Need to wait for stream fd in the next ppoll.
  poll_fd = dev_stream_poll_stream_fd(streams[0]->dstream.get());
  EXPECT_EQ(FAKE_POLL_FD, poll_fd);

  struct timespec delta;
  subtract_timespecs(&dev_time, &start, &delta);
  // Wake up time should be default 20 seconds because audio thread
  // depends on reply from client to wake it up.
  EXPECT_LT(19, delta.tv_sec);
  EXPECT_GT(21, delta.tv_sec);
}

// One hotword stream attaches to hotword device. Input data burst to a number
// larger than cb_threshold. However, stream is not pending client reply.
// This happens if there was no data during capture_to_stream.
// In this case stream fd is NOT used to poll for next wake.
// And the dev wake time is changed to a 0 instead of default 20 seconds.
TEST_F(TimingSuite, HotwordStreamBulkDataIsNotPending) {
  int poll_fd = 0;
  cras_audio_format fmt;
  fill_audio_format(&fmt, 48000);

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_INPUT, 240, &fmt);
  stream->rstream->flags = HOTWORD_STREAM;
  stream->rstream->next_cb_ts = start;

  AddFakeDataToStream(stream.get(), 480);
  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));
  // Stream is not pending the reply from client.
  rstream_stub_pending_reply(streams[0]->rstream.get(), 0);

  // There is more than 1 cb_threshold of data in device.
  timespec dev_time = SingleInputDevNextWake(4096, 7000, &start, &fmt, streams);

  // Does not need to wait for stream fd in the next ppoll.
  poll_fd = dev_stream_poll_stream_fd(streams[0]->dstream.get());
  EXPECT_EQ(-1, poll_fd);

  struct timespec delta;
  subtract_timespecs(&dev_time, &start, &delta);
  // Wake up time should be very small because there is enough
  // data to be send to client.
  EXPECT_LT(delta.tv_sec, 0.1);
}

// When a new output stream is added, there are two rules to determine the
// initial next_cb_ts.
// 1. If the device already has streams, the next_cb_ts will be the earliest
// next callback time from these streams.
// 2. If there are no other streams, the next_cb_ts will be set to the time
// when the valid frames in device is lower than cb_threshold. (If it is
// already lower than cb_threshold, set next_cb_ts to now.)

// Test rule 1.
// The device already has streams, the next_cb_ts will be the earliest
// next_cb_ts from these streams.
TEST_F(TimingSuite, NewOutputStreamInitStreamInDevice) {
  struct open_dev* dev_list_ = NULL;

  cras_audio_format format;
  fill_audio_format(&format, 48000);
  DevicePtr dev = create_device(CRAS_STREAM_OUTPUT, 1024, &format,
                                CRAS_NODE_TYPE_HEADPHONE);
  DL_APPEND(dev_list_, dev->odev.get());
  struct cras_iodev* iodev = dev->odev->dev;

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_OUTPUT, 480, &format);
  add_stream_to_dev(dev->dev, stream);
  stream->rstream->next_cb_ts.tv_sec = 54321;
  stream->rstream->next_cb_ts.tv_nsec = 12345;

  ShmPtr shm = create_shm(480);
  RstreamPtr rstream =
      create_rstream(1, CRAS_STREAM_OUTPUT, 480, &format, shm.get());

  dev_io_append_stream(&dev_list_, rstream.get(), &iodev, 1);

  EXPECT_EQ(stream->rstream->next_cb_ts.tv_sec, rstream->next_cb_ts.tv_sec);
  EXPECT_EQ(stream->rstream->next_cb_ts.tv_nsec, rstream->next_cb_ts.tv_nsec);

  dev_stream_destroy(iodev->streams->next);
}

// Test rule 2.
// The there are no streams and no frames in device buffer. The next_cb_ts
// will be set to now.
TEST_F(TimingSuite, NewOutputStreamInitNoStreamNoFramesInDevice) {
  struct open_dev* dev_list_ = NULL;

  cras_audio_format format;
  fill_audio_format(&format, 48000);
  DevicePtr dev = create_device(CRAS_STREAM_OUTPUT, 1024, &format,
                                CRAS_NODE_TYPE_HEADPHONE);
  DL_APPEND(dev_list_, dev->odev.get());
  struct cras_iodev* iodev = dev->odev->dev;

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  ShmPtr shm = create_shm(480);
  RstreamPtr rstream =
      create_rstream(1, CRAS_STREAM_OUTPUT, 480, &format, shm.get());

  dev_io_append_stream(&dev_list_, rstream.get(), &iodev, 1);

  EXPECT_EQ(start.tv_sec, rstream->next_cb_ts.tv_sec);
  EXPECT_EQ(start.tv_nsec, rstream->next_cb_ts.tv_nsec);

  dev_stream_destroy(iodev->streams);
}

// Test rule 2.
// The there are no streams and some valid frames in device buffer. The
// next_cb_ts will be set to the time that valid frames in device is lower
// than cb_threshold.
TEST_F(TimingSuite, NewOutputStreamInitNoStreamSomeFramesInDevice) {
  struct open_dev* dev_list_ = NULL;

  cras_audio_format format;
  fill_audio_format(&format, 48000);
  DevicePtr dev = create_device(CRAS_STREAM_OUTPUT, 1024, &format,
                                CRAS_NODE_TYPE_HEADPHONE);
  DL_APPEND(dev_list_, dev->odev.get());
  struct cras_iodev* iodev = dev->odev->dev;

  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  iodev_stub_valid_frames(iodev, 960, start);

  ShmPtr shm = create_shm(480);
  RstreamPtr rstream =
      create_rstream(1, CRAS_STREAM_OUTPUT, 480, &format, shm.get());

  dev_io_append_stream(&dev_list_, rstream.get(), &iodev, 1);

  // The next_cb_ts should be 10ms from now. At that time there are
  // only 480 valid frames in the device.
  const timespec add_millis = {0, 10 * 1000 * 1000};
  add_timespecs(&start, &add_millis);
  EXPECT_EQ(start.tv_sec, rstream->next_cb_ts.tv_sec);
  EXPECT_EQ(start.tv_nsec, rstream->next_cb_ts.tv_nsec);

  dev_stream_destroy(iodev->streams);
}

// There is the pseudo code about wake up time for a output device.
//
// function dev_io_next_output_wake(dev):
//   wake_ts = get_next_stream_wake_from_list(dev.streams)
//   if cras_iodev_odev_should_wake(dev):
//     wake_ts = MIN(wake_ts, dev.wake_ts) # rule_1
//
// function get_next_stream_wake_from_list(streams):
//   for stream in streams:
//     if stream is draining: # rule_2
//     	 continue
//     if stream is pending reply: # rule_3
//       continue
//     if stream is USE_DEV_TIMING: # rule_4
//       continue
//     min_ts = MIN(min_ts, stream.next_cb_ts) # rule_5
//   return min_ts
//
// # This function is in iodev so we don't test its logic here.
// function cras_iodev_odev_should_wake(dev):
//   if dev.is_free_running:
//     return False
//   if dev.state == NORMAL_RUN or dev.state == NO_STREAM_RUN:
//     return True
//   return False

// Test rule_1.
// The wake up time should be the earlier time amoung streams and devices.
TEST_F(TimingSuite, OutputWakeTimeOneStreamWithEarlierStreamWakeTime) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_OUTPUT, 480, &format);
  stream->rstream->next_cb_ts = start;
  stream->rstream->next_cb_ts.tv_sec += 1;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));

  struct timespec dev_wake_ts = start;
  dev_wake_ts.tv_sec += 2;

  timespec dev_time =
      SingleOutputDevNextWake(48000, 0, &start, &format, streams, &dev_wake_ts);

  EXPECT_EQ(start.tv_sec + 1, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_1.
// The wake up time should be the earlier time amoung streams and devices.
TEST_F(TimingSuite, OutputWakeTimeOneStreamWithEarlierDeviceWakeTime) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_OUTPUT, 480, &format);
  stream->rstream->next_cb_ts = start;
  stream->rstream->next_cb_ts.tv_sec += 2;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream));

  struct timespec dev_wake_ts = start;
  dev_wake_ts.tv_sec += 1;

  timespec dev_time =
      SingleOutputDevNextWake(48000, 0, &start, &format, streams, &dev_wake_ts);

  EXPECT_EQ(start.tv_sec + 1, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_2.
// The stream 1 is draining so it will be ignored. The wake up time should be
// the next_cb_ts of stream 2.
TEST_F(TimingSuite, OutputWakeTimeTwoStreamsWithOneIsDraining) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream1 = create_stream(1, 1, CRAS_STREAM_OUTPUT, 480, &format);
  stream1->rstream->next_cb_ts = start;
  stream1->rstream->next_cb_ts.tv_sec += 2;
  stream1->rstream->is_draining = 1;
  stream1->rstream->queued_frames = 480;

  StreamPtr stream2 = create_stream(1, 2, CRAS_STREAM_OUTPUT, 480, &format);
  stream2->rstream->next_cb_ts = start;
  stream2->rstream->next_cb_ts.tv_sec += 5;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream1));
  streams.emplace_back(std::move(stream2));

  struct timespec dev_wake_ts = start;
  dev_wake_ts.tv_sec += 10;

  timespec dev_time =
      SingleOutputDevNextWake(48000, 0, &start, &format, streams, &dev_wake_ts);

  EXPECT_EQ(start.tv_sec + 5, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_3.
// The stream 1 is pending reply so it will be ignored. The wake up time should
// be the next_cb_ts of stream 2.
TEST_F(TimingSuite, OutputWakeTimeTwoStreamsWithOneIsPendingReply) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream1 = create_stream(1, 1, CRAS_STREAM_OUTPUT, 480, &format);
  stream1->rstream->next_cb_ts = start;
  stream1->rstream->next_cb_ts.tv_sec += 2;
  rstream_stub_pending_reply(stream1->rstream.get(), 1);

  StreamPtr stream2 = create_stream(1, 2, CRAS_STREAM_OUTPUT, 480, &format);
  stream2->rstream->next_cb_ts = start;
  stream2->rstream->next_cb_ts.tv_sec += 5;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream1));
  streams.emplace_back(std::move(stream2));

  struct timespec dev_wake_ts = start;
  dev_wake_ts.tv_sec += 10;

  timespec dev_time =
      SingleOutputDevNextWake(48000, 0, &start, &format, streams, &dev_wake_ts);

  EXPECT_EQ(start.tv_sec + 5, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_4.
// The stream 1 is uning device timing so it will be ignored. The wake up time
// should be the next_cb_ts of stream 2.
TEST_F(TimingSuite, OutputWakeTimeTwoStreamsWithOneIsUsingDevTiming) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream1 = create_stream(1, 1, CRAS_STREAM_OUTPUT, 480, &format);
  stream1->rstream->next_cb_ts = start;
  stream1->rstream->next_cb_ts.tv_sec += 2;
  stream1->rstream->flags = USE_DEV_TIMING;

  StreamPtr stream2 = create_stream(1, 2, CRAS_STREAM_OUTPUT, 480, &format);
  stream2->rstream->next_cb_ts = start;
  stream2->rstream->next_cb_ts.tv_sec += 5;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream1));
  streams.emplace_back(std::move(stream2));

  struct timespec dev_wake_ts = start;
  dev_wake_ts.tv_sec += 10;

  timespec dev_time =
      SingleOutputDevNextWake(48000, 0, &start, &format, streams, &dev_wake_ts);

  EXPECT_EQ(start.tv_sec + 5, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// Test rule_5.
// The wake up time should be the next_cb_ts of streams.
TEST_F(TimingSuite, OutputWakeTimeTwoStreams) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);

  StreamPtr stream1 = create_stream(1, 1, CRAS_STREAM_OUTPUT, 480, &format);
  stream1->rstream->next_cb_ts = start;
  stream1->rstream->next_cb_ts.tv_sec += 2;

  StreamPtr stream2 = create_stream(1, 2, CRAS_STREAM_OUTPUT, 480, &format);
  stream2->rstream->next_cb_ts = start;
  stream2->rstream->next_cb_ts.tv_sec += 5;

  std::vector<StreamPtr> streams;
  streams.emplace_back(std::move(stream1));
  streams.emplace_back(std::move(stream2));

  struct timespec dev_wake_ts = start;
  dev_wake_ts.tv_sec += 10;

  timespec dev_time =
      SingleOutputDevNextWake(48000, 0, &start, &format, streams, &dev_wake_ts);

  EXPECT_EQ(start.tv_sec + 2, dev_time.tv_sec);
  EXPECT_EQ(start.tv_nsec, dev_time.tv_nsec);
}

// One device, one stream, fetch stream and check the sleep time is one more
// wakeup interval.
TEST_F(TimingSuite, OutputStreamsUpdateAfterFetching) {
  cras_audio_format format;
  fill_audio_format(&format, 48000);

  StreamPtr stream = create_stream(1, 1, CRAS_STREAM_OUTPUT, 480, &format);

  // rstream's next callback is now.
  struct timespec start;
  clock_gettime(CLOCK_MONOTONIC_RAW, &start);
  stream->rstream->next_cb_ts = start;

  struct open_dev* dev_list_ = NULL;

  DevicePtr dev = create_device(CRAS_STREAM_OUTPUT, 1024, &format,
                                CRAS_NODE_TYPE_HEADPHONE);
  DL_APPEND(dev_list_, dev->odev.get());

  add_stream_to_dev(dev->dev, stream);

  dev_io_playback_fetch(dev_list_);

  // The next callback should be scheduled 10ms in the future.
  const timespec ten_millis = {0, 10 * 1000 * 1000};
  add_timespecs(&start, &ten_millis);
  EXPECT_EQ(start.tv_sec, stream->rstream->next_cb_ts.tv_sec);
  EXPECT_EQ(start.tv_nsec, stream->rstream->next_cb_ts.tv_nsec);
}

// TODO(yuhsuan): There are some time scheduling rules in cras_iodev. Maybe we
// can move them into dev_io so that all timing related codes are in the same
// file or leave them in iodev_unittest like now.
// 1. Device's wake_ts update: cras_iodev_frames_to_play_in_sleep.
// 2. wake_ts update when removing stream: cras_iodev_rm_stream.

/* Stubs */
extern "C" {

int input_data_get_for_stream(struct input_data* data,
                              struct cras_rstream* stream,
                              struct buffer_share* offsets,
                              struct cras_audio_area** area,
                              unsigned int* offset) {
  return 0;
}

int input_data_put_for_stream(struct input_data* data,
                              struct cras_rstream* stream,
                              struct buffer_share* offsets,
                              unsigned int frames) {
  return 0;
}

float input_data_get_software_gain_scaler(struct input_data* data,
                                          float idev_sw_gain_scaler,
                                          struct cras_rstream* stream) {
  return 1.0;
}

struct cras_audio_format* cras_rstream_post_processing_format(
    const struct cras_rstream* stream,
    void* dev_ptr) {
  return NULL;
}

int cras_audio_thread_event_drop_samples() {
  return 0;
}

int cras_audio_thread_event_severe_underrun() {
  return 0;
}

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){};
}  // extern "C"

}  //  namespace

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