aboutsummaryrefslogtreecommitdiff
path: root/video/stream_synchronization_unittest.cc
blob: 3d6fdd82a7cb2f9d53517e59f492e4dfea8b435e (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
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "video/stream_synchronization.h"

#include <algorithm>

#include "system_wrappers/include/clock.h"
#include "system_wrappers/include/ntp_time.h"
#include "test/gtest.h"

namespace webrtc {
namespace {
constexpr int kMaxChangeMs = 80;  // From stream_synchronization.cc
constexpr int kDefaultAudioFrequency = 8000;
constexpr int kDefaultVideoFrequency = 90000;
constexpr int kSmoothingFilter = 4 * 2;
}  // namespace

class StreamSynchronizationTest : public ::testing::Test {
 public:
  StreamSynchronizationTest()
      : sync_(0, 0), clock_sender_(98765000), clock_receiver_(43210000) {}

 protected:
  // Generates the necessary RTCP measurements and RTP timestamps and computes
  // the audio and video delays needed to get the two streams in sync.
  // |audio_delay_ms| and |video_delay_ms| are the number of milliseconds after
  // capture which the frames are received.
  // |current_audio_delay_ms| is the number of milliseconds which audio is
  // currently being delayed by the receiver.
  bool DelayedStreams(int audio_delay_ms,
                      int video_delay_ms,
                      int current_audio_delay_ms,
                      int* total_audio_delay_ms,
                      int* total_video_delay_ms) {
    int audio_frequency =
        static_cast<int>(kDefaultAudioFrequency * audio_clock_drift_ + 0.5);
    int video_frequency =
        static_cast<int>(kDefaultVideoFrequency * video_clock_drift_ + 0.5);

    // Generate NTP/RTP timestamp pair for both streams corresponding to RTCP.
    bool new_sr;
    StreamSynchronization::Measurements audio;
    StreamSynchronization::Measurements video;
    NtpTime ntp_time = clock_sender_.CurrentNtpTime();
    uint32_t rtp_timestamp =
        clock_sender_.CurrentTime().ms() * audio_frequency / 1000;
    EXPECT_TRUE(audio.rtp_to_ntp.UpdateMeasurements(
        ntp_time.seconds(), ntp_time.fractions(), rtp_timestamp, &new_sr));
    clock_sender_.AdvanceTimeMilliseconds(100);
    clock_receiver_.AdvanceTimeMilliseconds(100);
    ntp_time = clock_sender_.CurrentNtpTime();
    rtp_timestamp = clock_sender_.CurrentTime().ms() * video_frequency / 1000;
    EXPECT_TRUE(video.rtp_to_ntp.UpdateMeasurements(
        ntp_time.seconds(), ntp_time.fractions(), rtp_timestamp, &new_sr));
    clock_sender_.AdvanceTimeMilliseconds(900);
    clock_receiver_.AdvanceTimeMilliseconds(900);
    ntp_time = clock_sender_.CurrentNtpTime();
    rtp_timestamp = clock_sender_.CurrentTime().ms() * audio_frequency / 1000;
    EXPECT_TRUE(audio.rtp_to_ntp.UpdateMeasurements(
        ntp_time.seconds(), ntp_time.fractions(), rtp_timestamp, &new_sr));
    clock_sender_.AdvanceTimeMilliseconds(100);
    clock_receiver_.AdvanceTimeMilliseconds(100);
    ntp_time = clock_sender_.CurrentNtpTime();
    rtp_timestamp = clock_sender_.CurrentTime().ms() * video_frequency / 1000;
    EXPECT_TRUE(video.rtp_to_ntp.UpdateMeasurements(
        ntp_time.seconds(), ntp_time.fractions(), rtp_timestamp, &new_sr));
    clock_sender_.AdvanceTimeMilliseconds(900);
    clock_receiver_.AdvanceTimeMilliseconds(900);

    // Capture an audio and a video frame at the same time.
    audio.latest_timestamp =
        clock_sender_.CurrentTime().ms() * audio_frequency / 1000;
    video.latest_timestamp =
        clock_sender_.CurrentTime().ms() * video_frequency / 1000;

    if (audio_delay_ms > video_delay_ms) {
      // Audio later than video.
      clock_receiver_.AdvanceTimeMilliseconds(video_delay_ms);
      video.latest_receive_time_ms = clock_receiver_.CurrentTime().ms();
      clock_receiver_.AdvanceTimeMilliseconds(audio_delay_ms - video_delay_ms);
      audio.latest_receive_time_ms = clock_receiver_.CurrentTime().ms();
    } else {
      // Video later than audio.
      clock_receiver_.AdvanceTimeMilliseconds(audio_delay_ms);
      audio.latest_receive_time_ms = clock_receiver_.CurrentTime().ms();
      clock_receiver_.AdvanceTimeMilliseconds(video_delay_ms - audio_delay_ms);
      video.latest_receive_time_ms = clock_receiver_.CurrentTime().ms();
    }

    int relative_delay_ms;
    EXPECT_TRUE(StreamSynchronization::ComputeRelativeDelay(
        audio, video, &relative_delay_ms));
    EXPECT_EQ(video_delay_ms - audio_delay_ms, relative_delay_ms);

    return sync_.ComputeDelays(relative_delay_ms, current_audio_delay_ms,
                               total_audio_delay_ms, total_video_delay_ms);
  }

  // Simulate audio playback 300 ms after capture and video rendering 100 ms
  // after capture. Verify that the correct extra delays are calculated for
  // audio and video, and that they change correctly when we simulate that
  // NetEQ or the VCM adds more delay to the streams.
  void BothDelayedAudioLaterTest(int base_target_delay_ms) {
    const int kAudioDelayMs = base_target_delay_ms + 300;
    const int kVideoDelayMs = base_target_delay_ms + 100;
    int current_audio_delay_ms = base_target_delay_ms;
    int total_audio_delay_ms = 0;
    int total_video_delay_ms = base_target_delay_ms;
    int filtered_move = (kAudioDelayMs - kVideoDelayMs) / kSmoothingFilter;

    EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    EXPECT_EQ(base_target_delay_ms + filtered_move, total_video_delay_ms);
    EXPECT_EQ(base_target_delay_ms, total_audio_delay_ms);

    // Set new current delay.
    current_audio_delay_ms = total_audio_delay_ms;
    clock_sender_.AdvanceTimeMilliseconds(1000);
    clock_receiver_.AdvanceTimeMilliseconds(
        1000 - std::max(kAudioDelayMs, kVideoDelayMs));
    // Simulate base_target_delay_ms minimum delay in the VCM.
    total_video_delay_ms = base_target_delay_ms;
    EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    EXPECT_EQ(base_target_delay_ms + 2 * filtered_move, total_video_delay_ms);
    EXPECT_EQ(base_target_delay_ms, total_audio_delay_ms);

    // Set new current delay.
    current_audio_delay_ms = total_audio_delay_ms;
    clock_sender_.AdvanceTimeMilliseconds(1000);
    clock_receiver_.AdvanceTimeMilliseconds(
        1000 - std::max(kAudioDelayMs, kVideoDelayMs));
    // Simulate base_target_delay_ms minimum delay in the VCM.
    total_video_delay_ms = base_target_delay_ms;
    EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    EXPECT_EQ(base_target_delay_ms + 3 * filtered_move, total_video_delay_ms);
    EXPECT_EQ(base_target_delay_ms, total_audio_delay_ms);

    // Simulate that NetEQ introduces some audio delay.
    const int kNeteqDelayIncrease = 50;
    current_audio_delay_ms = base_target_delay_ms + kNeteqDelayIncrease;
    clock_sender_.AdvanceTimeMilliseconds(1000);
    clock_receiver_.AdvanceTimeMilliseconds(
        1000 - std::max(kAudioDelayMs, kVideoDelayMs));
    // Simulate base_target_delay_ms minimum delay in the VCM.
    total_video_delay_ms = base_target_delay_ms;
    EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    filtered_move = 3 * filtered_move +
                    (kNeteqDelayIncrease + kAudioDelayMs - kVideoDelayMs) /
                        kSmoothingFilter;
    EXPECT_EQ(base_target_delay_ms + filtered_move, total_video_delay_ms);
    EXPECT_EQ(base_target_delay_ms, total_audio_delay_ms);

    // Simulate that NetEQ reduces its delay.
    const int kNeteqDelayDecrease = 10;
    current_audio_delay_ms = base_target_delay_ms + kNeteqDelayDecrease;
    clock_sender_.AdvanceTimeMilliseconds(1000);
    clock_receiver_.AdvanceTimeMilliseconds(
        1000 - std::max(kAudioDelayMs, kVideoDelayMs));
    // Simulate base_target_delay_ms minimum delay in the VCM.
    total_video_delay_ms = base_target_delay_ms;
    EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    filtered_move =
        filtered_move + (kNeteqDelayDecrease + kAudioDelayMs - kVideoDelayMs) /
                            kSmoothingFilter;
    EXPECT_EQ(base_target_delay_ms + filtered_move, total_video_delay_ms);
    EXPECT_EQ(base_target_delay_ms, total_audio_delay_ms);
  }

  void BothDelayedVideoLaterTest(int base_target_delay_ms) {
    const int kAudioDelayMs = base_target_delay_ms + 100;
    const int kVideoDelayMs = base_target_delay_ms + 300;
    int current_audio_delay_ms = base_target_delay_ms;
    int total_audio_delay_ms = 0;
    int total_video_delay_ms = base_target_delay_ms;

    EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
    // The audio delay is not allowed to change more than this.
    EXPECT_GE(base_target_delay_ms + kMaxChangeMs, total_audio_delay_ms);
    int last_total_audio_delay_ms = total_audio_delay_ms;

    // Set new current audio delay.
    current_audio_delay_ms = total_audio_delay_ms;
    clock_sender_.AdvanceTimeMilliseconds(1000);
    clock_receiver_.AdvanceTimeMilliseconds(800);
    EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
    EXPECT_EQ(last_total_audio_delay_ms +
                  MaxAudioDelayChangeMs(
                      current_audio_delay_ms,
                      base_target_delay_ms + kVideoDelayMs - kAudioDelayMs),
              total_audio_delay_ms);
    last_total_audio_delay_ms = total_audio_delay_ms;

    // Set new current audio delay.
    current_audio_delay_ms = total_audio_delay_ms;
    clock_sender_.AdvanceTimeMilliseconds(1000);
    clock_receiver_.AdvanceTimeMilliseconds(800);
    EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
    EXPECT_EQ(last_total_audio_delay_ms +
                  MaxAudioDelayChangeMs(
                      current_audio_delay_ms,
                      base_target_delay_ms + kVideoDelayMs - kAudioDelayMs),
              total_audio_delay_ms);
    last_total_audio_delay_ms = total_audio_delay_ms;

    // Simulate that NetEQ for some reason reduced the delay.
    current_audio_delay_ms = base_target_delay_ms + 10;
    clock_sender_.AdvanceTimeMilliseconds(1000);
    clock_receiver_.AdvanceTimeMilliseconds(800);
    EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
    EXPECT_EQ(last_total_audio_delay_ms +
                  MaxAudioDelayChangeMs(
                      current_audio_delay_ms,
                      base_target_delay_ms + kVideoDelayMs - kAudioDelayMs),
              total_audio_delay_ms);
    last_total_audio_delay_ms = total_audio_delay_ms;

    // Simulate that NetEQ for some reason significantly increased the delay.
    current_audio_delay_ms = base_target_delay_ms + 350;
    clock_sender_.AdvanceTimeMilliseconds(1000);
    clock_receiver_.AdvanceTimeMilliseconds(800);
    EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
    EXPECT_EQ(last_total_audio_delay_ms +
                  MaxAudioDelayChangeMs(
                      current_audio_delay_ms,
                      base_target_delay_ms + kVideoDelayMs - kAudioDelayMs),
              total_audio_delay_ms);
  }

  int MaxAudioDelayChangeMs(int current_audio_delay_ms, int delay_ms) const {
    int diff_ms = (delay_ms - current_audio_delay_ms) / kSmoothingFilter;
    diff_ms = std::min(diff_ms, kMaxChangeMs);
    diff_ms = std::max(diff_ms, -kMaxChangeMs);
    return diff_ms;
  }

  StreamSynchronization sync_;
  SimulatedClock clock_sender_;
  SimulatedClock clock_receiver_;
  double audio_clock_drift_ = 1.0;
  double video_clock_drift_ = 1.0;
};

TEST_F(StreamSynchronizationTest, NoDelay) {
  int total_audio_delay_ms = 0;
  int total_video_delay_ms = 0;

  EXPECT_FALSE(DelayedStreams(/*audio_delay_ms=*/0, /*video_delay_ms=*/0,
                              /*current_audio_delay_ms=*/0,
                              &total_audio_delay_ms, &total_video_delay_ms));
  EXPECT_EQ(0, total_audio_delay_ms);
  EXPECT_EQ(0, total_video_delay_ms);
}

TEST_F(StreamSynchronizationTest, VideoDelayed) {
  const int kAudioDelayMs = 200;
  int total_audio_delay_ms = 0;
  int total_video_delay_ms = 0;

  EXPECT_TRUE(DelayedStreams(kAudioDelayMs, /*video_delay_ms=*/0,
                             /*current_audio_delay_ms=*/0,
                             &total_audio_delay_ms, &total_video_delay_ms));
  EXPECT_EQ(0, total_audio_delay_ms);
  // The delay is not allowed to change more than this.
  EXPECT_EQ(kAudioDelayMs / kSmoothingFilter, total_video_delay_ms);

  // Simulate 0 minimum delay in the VCM.
  total_video_delay_ms = 0;
  clock_sender_.AdvanceTimeMilliseconds(1000);
  clock_receiver_.AdvanceTimeMilliseconds(800);
  EXPECT_TRUE(DelayedStreams(kAudioDelayMs, /*video_delay_ms=*/0,
                             /*current_audio_delay_ms=*/0,
                             &total_audio_delay_ms, &total_video_delay_ms));
  EXPECT_EQ(0, total_audio_delay_ms);
  EXPECT_EQ(2 * kAudioDelayMs / kSmoothingFilter, total_video_delay_ms);

  // Simulate 0 minimum delay in the VCM.
  total_video_delay_ms = 0;
  clock_sender_.AdvanceTimeMilliseconds(1000);
  clock_receiver_.AdvanceTimeMilliseconds(800);
  EXPECT_TRUE(DelayedStreams(kAudioDelayMs, /*video_delay_ms=*/0,
                             /*current_audio_delay_ms=*/0,
                             &total_audio_delay_ms, &total_video_delay_ms));
  EXPECT_EQ(0, total_audio_delay_ms);
  EXPECT_EQ(3 * kAudioDelayMs / kSmoothingFilter, total_video_delay_ms);
}

TEST_F(StreamSynchronizationTest, AudioDelayed) {
  const int kVideoDelayMs = 200;
  int current_audio_delay_ms = 0;
  int total_audio_delay_ms = 0;
  int total_video_delay_ms = 0;

  EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
                             current_audio_delay_ms, &total_audio_delay_ms,
                             &total_video_delay_ms));
  EXPECT_EQ(0, total_video_delay_ms);
  // The delay is not allowed to change more than this.
  EXPECT_EQ(kVideoDelayMs / kSmoothingFilter, total_audio_delay_ms);
  int last_total_audio_delay_ms = total_audio_delay_ms;

  // Set new current audio delay.
  current_audio_delay_ms = total_audio_delay_ms;
  clock_sender_.AdvanceTimeMilliseconds(1000);
  clock_receiver_.AdvanceTimeMilliseconds(800);
  EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
                             current_audio_delay_ms, &total_audio_delay_ms,
                             &total_video_delay_ms));
  EXPECT_EQ(0, total_video_delay_ms);
  EXPECT_EQ(last_total_audio_delay_ms +
                MaxAudioDelayChangeMs(current_audio_delay_ms, kVideoDelayMs),
            total_audio_delay_ms);
  last_total_audio_delay_ms = total_audio_delay_ms;

  // Set new current audio delay.
  current_audio_delay_ms = total_audio_delay_ms;
  clock_sender_.AdvanceTimeMilliseconds(1000);
  clock_receiver_.AdvanceTimeMilliseconds(800);
  EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
                             current_audio_delay_ms, &total_audio_delay_ms,
                             &total_video_delay_ms));
  EXPECT_EQ(0, total_video_delay_ms);
  EXPECT_EQ(last_total_audio_delay_ms +
                MaxAudioDelayChangeMs(current_audio_delay_ms, kVideoDelayMs),
            total_audio_delay_ms);
  last_total_audio_delay_ms = total_audio_delay_ms;

  // Simulate that NetEQ for some reason reduced the delay.
  current_audio_delay_ms = 10;
  clock_sender_.AdvanceTimeMilliseconds(1000);
  clock_receiver_.AdvanceTimeMilliseconds(800);
  EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
                             current_audio_delay_ms, &total_audio_delay_ms,
                             &total_video_delay_ms));
  EXPECT_EQ(0, total_video_delay_ms);
  EXPECT_EQ(last_total_audio_delay_ms +
                MaxAudioDelayChangeMs(current_audio_delay_ms, kVideoDelayMs),
            total_audio_delay_ms);
  last_total_audio_delay_ms = total_audio_delay_ms;

  // Simulate that NetEQ for some reason significantly increased the delay.
  current_audio_delay_ms = 350;
  clock_sender_.AdvanceTimeMilliseconds(1000);
  clock_receiver_.AdvanceTimeMilliseconds(800);
  EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
                             current_audio_delay_ms, &total_audio_delay_ms,
                             &total_video_delay_ms));
  EXPECT_EQ(0, total_video_delay_ms);
  EXPECT_EQ(last_total_audio_delay_ms +
                MaxAudioDelayChangeMs(current_audio_delay_ms, kVideoDelayMs),
            total_audio_delay_ms);
}

TEST_F(StreamSynchronizationTest, NoAudioIncomingUnboundedIncrease) {
  // Test how audio delay can grow unbounded when audio stops coming in.
  // This is handled in caller of RtpStreamsSynchronizer, for example in
  // RtpStreamsSynchronizer by not updating delays when audio samples stop
  // coming in.
  const int kVideoDelayMs = 300;
  const int kAudioDelayMs = 100;
  int current_audio_delay_ms = kAudioDelayMs;
  int total_audio_delay_ms = 0;
  int total_video_delay_ms = 0;

  EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
                             current_audio_delay_ms, &total_audio_delay_ms,
                             &total_video_delay_ms));
  EXPECT_EQ(0, total_video_delay_ms);
  // The delay is not allowed to change more than this.
  EXPECT_EQ((kVideoDelayMs - kAudioDelayMs) / kSmoothingFilter,
            total_audio_delay_ms);
  int last_total_audio_delay_ms = total_audio_delay_ms;

  // Set new current audio delay: simulate audio samples are flowing in.
  current_audio_delay_ms = total_audio_delay_ms;

  clock_sender_.AdvanceTimeMilliseconds(1000);
  clock_receiver_.AdvanceTimeMilliseconds(1000);
  EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
                             current_audio_delay_ms, &total_audio_delay_ms,
                             &total_video_delay_ms));
  EXPECT_EQ(0, total_video_delay_ms);
  EXPECT_EQ(last_total_audio_delay_ms +
                MaxAudioDelayChangeMs(current_audio_delay_ms, kVideoDelayMs),
            total_audio_delay_ms);
  last_total_audio_delay_ms = total_audio_delay_ms;

  // Simulate no incoming audio by not update audio delay.
  const int kSimulationSecs = 300;     // 5min
  const int kMaxDeltaDelayMs = 10000;  // max delay for audio in webrtc
  for (auto time_secs = 0; time_secs < kSimulationSecs; time_secs++) {
    clock_sender_.AdvanceTimeMilliseconds(1000);
    clock_receiver_.AdvanceTimeMilliseconds(1000);
    EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
                               current_audio_delay_ms, &total_audio_delay_ms,
                               &total_video_delay_ms));
    EXPECT_EQ(0, total_video_delay_ms);

    // Audio delay does not go above kMaxDeltaDelayMs.
    EXPECT_EQ(std::min(kMaxDeltaDelayMs,
                       last_total_audio_delay_ms +
                           MaxAudioDelayChangeMs(current_audio_delay_ms,
                                                 kVideoDelayMs)),
              total_audio_delay_ms);
    last_total_audio_delay_ms = total_audio_delay_ms;
  }
  // By now the audio delay has grown unbounded to kMaxDeltaDelayMs.
  EXPECT_EQ(kMaxDeltaDelayMs, last_total_audio_delay_ms);
}

TEST_F(StreamSynchronizationTest, BothDelayedVideoLater) {
  BothDelayedVideoLaterTest(0);
}

TEST_F(StreamSynchronizationTest, BothDelayedVideoLaterAudioClockDrift) {
  audio_clock_drift_ = 1.05;
  BothDelayedVideoLaterTest(0);
}

TEST_F(StreamSynchronizationTest, BothDelayedVideoLaterVideoClockDrift) {
  video_clock_drift_ = 1.05;
  BothDelayedVideoLaterTest(0);
}

TEST_F(StreamSynchronizationTest, BothDelayedAudioLater) {
  BothDelayedAudioLaterTest(0);
}

TEST_F(StreamSynchronizationTest, BothDelayedAudioClockDrift) {
  audio_clock_drift_ = 1.05;
  BothDelayedAudioLaterTest(0);
}

TEST_F(StreamSynchronizationTest, BothDelayedVideoClockDrift) {
  video_clock_drift_ = 1.05;
  BothDelayedAudioLaterTest(0);
}

TEST_F(StreamSynchronizationTest, BothEquallyDelayed) {
  const int kDelayMs = 2000;
  int current_audio_delay_ms = kDelayMs;
  int total_audio_delay_ms = 0;
  int total_video_delay_ms = kDelayMs;
  // In sync, expect no change.
  EXPECT_FALSE(DelayedStreams(kDelayMs, kDelayMs, current_audio_delay_ms,
                              &total_audio_delay_ms, &total_video_delay_ms));
  // Trigger another call with the same values, delay should not be modified.
  total_video_delay_ms = kDelayMs;
  EXPECT_FALSE(DelayedStreams(kDelayMs, kDelayMs, current_audio_delay_ms,
                              &total_audio_delay_ms, &total_video_delay_ms));
  // Change delay value, delay should not be modified.
  const int kDelayMs2 = 5000;
  current_audio_delay_ms = kDelayMs2;
  total_video_delay_ms = kDelayMs2;
  EXPECT_FALSE(DelayedStreams(kDelayMs2, kDelayMs2, current_audio_delay_ms,
                              &total_audio_delay_ms, &total_video_delay_ms));
}

TEST_F(StreamSynchronizationTest, BothDelayedAudioLaterWithBaseDelay) {
  const int kBaseTargetDelayMs = 3000;
  sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
  BothDelayedAudioLaterTest(kBaseTargetDelayMs);
}

TEST_F(StreamSynchronizationTest, BothDelayedAudioClockDriftWithBaseDelay) {
  const int kBaseTargetDelayMs = 3000;
  sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
  audio_clock_drift_ = 1.05;
  BothDelayedAudioLaterTest(kBaseTargetDelayMs);
}

TEST_F(StreamSynchronizationTest, BothDelayedVideoClockDriftWithBaseDelay) {
  const int kBaseTargetDelayMs = 3000;
  sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
  video_clock_drift_ = 1.05;
  BothDelayedAudioLaterTest(kBaseTargetDelayMs);
}

TEST_F(StreamSynchronizationTest, BothDelayedVideoLaterWithBaseDelay) {
  const int kBaseTargetDelayMs = 2000;
  sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
  BothDelayedVideoLaterTest(kBaseTargetDelayMs);
}

TEST_F(StreamSynchronizationTest,
       BothDelayedVideoLaterAudioClockDriftWithBaseDelay) {
  const int kBaseTargetDelayMs = 2000;
  audio_clock_drift_ = 1.05;
  sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
  BothDelayedVideoLaterTest(kBaseTargetDelayMs);
}

TEST_F(StreamSynchronizationTest,
       BothDelayedVideoLaterVideoClockDriftWithBaseDelay) {
  const int kBaseTargetDelayMs = 2000;
  video_clock_drift_ = 1.05;
  sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
  BothDelayedVideoLaterTest(kBaseTargetDelayMs);
}

}  // namespace webrtc