• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "video/stream_synchronization.h"
12 
13 #include <algorithm>
14 
15 #include "system_wrappers/include/clock.h"
16 #include "system_wrappers/include/ntp_time.h"
17 #include "test/gtest.h"
18 
19 namespace webrtc {
20 namespace {
21 constexpr int kMaxChangeMs = 80;  // From stream_synchronization.cc
22 constexpr int kDefaultAudioFrequency = 8000;
23 constexpr int kDefaultVideoFrequency = 90000;
24 constexpr int kSmoothingFilter = 4 * 2;
25 }  // namespace
26 
27 class StreamSynchronizationTest : public ::testing::Test {
28  public:
StreamSynchronizationTest()29   StreamSynchronizationTest()
30       : sync_(0, 0), clock_sender_(98765000), clock_receiver_(43210000) {}
31 
32  protected:
33   // Generates the necessary RTCP measurements and RTP timestamps and computes
34   // the audio and video delays needed to get the two streams in sync.
35   // `audio_delay_ms` and `video_delay_ms` are the number of milliseconds after
36   // capture which the frames are received.
37   // `current_audio_delay_ms` is the number of milliseconds which audio is
38   // currently being delayed by the receiver.
DelayedStreams(int audio_delay_ms,int video_delay_ms,int current_audio_delay_ms,int * total_audio_delay_ms,int * total_video_delay_ms)39   bool DelayedStreams(int audio_delay_ms,
40                       int video_delay_ms,
41                       int current_audio_delay_ms,
42                       int* total_audio_delay_ms,
43                       int* total_video_delay_ms) {
44     int audio_frequency =
45         static_cast<int>(kDefaultAudioFrequency * audio_clock_drift_ + 0.5);
46     int video_frequency =
47         static_cast<int>(kDefaultVideoFrequency * video_clock_drift_ + 0.5);
48 
49     // Generate NTP/RTP timestamp pair for both streams corresponding to RTCP.
50     StreamSynchronization::Measurements audio;
51     StreamSynchronization::Measurements video;
52     NtpTime ntp_time = clock_sender_.CurrentNtpTime();
53     uint32_t rtp_timestamp =
54         clock_sender_.CurrentTime().ms() * audio_frequency / 1000;
55     EXPECT_EQ(audio.rtp_to_ntp.UpdateMeasurements(ntp_time, rtp_timestamp),
56               RtpToNtpEstimator::kNewMeasurement);
57     clock_sender_.AdvanceTimeMilliseconds(100);
58     clock_receiver_.AdvanceTimeMilliseconds(100);
59     ntp_time = clock_sender_.CurrentNtpTime();
60     rtp_timestamp = clock_sender_.CurrentTime().ms() * video_frequency / 1000;
61     EXPECT_EQ(video.rtp_to_ntp.UpdateMeasurements(ntp_time, rtp_timestamp),
62               RtpToNtpEstimator::kNewMeasurement);
63     clock_sender_.AdvanceTimeMilliseconds(900);
64     clock_receiver_.AdvanceTimeMilliseconds(900);
65     ntp_time = clock_sender_.CurrentNtpTime();
66     rtp_timestamp = clock_sender_.CurrentTime().ms() * audio_frequency / 1000;
67     EXPECT_EQ(audio.rtp_to_ntp.UpdateMeasurements(ntp_time, rtp_timestamp),
68               RtpToNtpEstimator::kNewMeasurement);
69     clock_sender_.AdvanceTimeMilliseconds(100);
70     clock_receiver_.AdvanceTimeMilliseconds(100);
71     ntp_time = clock_sender_.CurrentNtpTime();
72     rtp_timestamp = clock_sender_.CurrentTime().ms() * video_frequency / 1000;
73     EXPECT_EQ(video.rtp_to_ntp.UpdateMeasurements(ntp_time, rtp_timestamp),
74               RtpToNtpEstimator::kNewMeasurement);
75     clock_sender_.AdvanceTimeMilliseconds(900);
76     clock_receiver_.AdvanceTimeMilliseconds(900);
77 
78     // Capture an audio and a video frame at the same time.
79     audio.latest_timestamp =
80         clock_sender_.CurrentTime().ms() * audio_frequency / 1000;
81     video.latest_timestamp =
82         clock_sender_.CurrentTime().ms() * video_frequency / 1000;
83 
84     if (audio_delay_ms > video_delay_ms) {
85       // Audio later than video.
86       clock_receiver_.AdvanceTimeMilliseconds(video_delay_ms);
87       video.latest_receive_time_ms = clock_receiver_.CurrentTime().ms();
88       clock_receiver_.AdvanceTimeMilliseconds(audio_delay_ms - video_delay_ms);
89       audio.latest_receive_time_ms = clock_receiver_.CurrentTime().ms();
90     } else {
91       // Video later than audio.
92       clock_receiver_.AdvanceTimeMilliseconds(audio_delay_ms);
93       audio.latest_receive_time_ms = clock_receiver_.CurrentTime().ms();
94       clock_receiver_.AdvanceTimeMilliseconds(video_delay_ms - audio_delay_ms);
95       video.latest_receive_time_ms = clock_receiver_.CurrentTime().ms();
96     }
97 
98     int relative_delay_ms;
99     EXPECT_TRUE(StreamSynchronization::ComputeRelativeDelay(
100         audio, video, &relative_delay_ms));
101     EXPECT_EQ(video_delay_ms - audio_delay_ms, relative_delay_ms);
102 
103     return sync_.ComputeDelays(relative_delay_ms, current_audio_delay_ms,
104                                total_audio_delay_ms, total_video_delay_ms);
105   }
106 
107   // Simulate audio playback 300 ms after capture and video rendering 100 ms
108   // after capture. Verify that the correct extra delays are calculated for
109   // audio and video, and that they change correctly when we simulate that
110   // NetEQ or the VCM adds more delay to the streams.
BothDelayedAudioLaterTest(int base_target_delay_ms)111   void BothDelayedAudioLaterTest(int base_target_delay_ms) {
112     const int kAudioDelayMs = base_target_delay_ms + 300;
113     const int kVideoDelayMs = base_target_delay_ms + 100;
114     int current_audio_delay_ms = base_target_delay_ms;
115     int total_audio_delay_ms = 0;
116     int total_video_delay_ms = base_target_delay_ms;
117     int filtered_move = (kAudioDelayMs - kVideoDelayMs) / kSmoothingFilter;
118 
119     EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
120                                current_audio_delay_ms, &total_audio_delay_ms,
121                                &total_video_delay_ms));
122     EXPECT_EQ(base_target_delay_ms + filtered_move, total_video_delay_ms);
123     EXPECT_EQ(base_target_delay_ms, total_audio_delay_ms);
124 
125     // Set new current delay.
126     current_audio_delay_ms = total_audio_delay_ms;
127     clock_sender_.AdvanceTimeMilliseconds(1000);
128     clock_receiver_.AdvanceTimeMilliseconds(
129         1000 - std::max(kAudioDelayMs, kVideoDelayMs));
130     // Simulate base_target_delay_ms minimum delay in the VCM.
131     total_video_delay_ms = base_target_delay_ms;
132     EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
133                                current_audio_delay_ms, &total_audio_delay_ms,
134                                &total_video_delay_ms));
135     EXPECT_EQ(base_target_delay_ms + 2 * filtered_move, total_video_delay_ms);
136     EXPECT_EQ(base_target_delay_ms, total_audio_delay_ms);
137 
138     // Set new current delay.
139     current_audio_delay_ms = total_audio_delay_ms;
140     clock_sender_.AdvanceTimeMilliseconds(1000);
141     clock_receiver_.AdvanceTimeMilliseconds(
142         1000 - std::max(kAudioDelayMs, kVideoDelayMs));
143     // Simulate base_target_delay_ms minimum delay in the VCM.
144     total_video_delay_ms = base_target_delay_ms;
145     EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
146                                current_audio_delay_ms, &total_audio_delay_ms,
147                                &total_video_delay_ms));
148     EXPECT_EQ(base_target_delay_ms + 3 * filtered_move, total_video_delay_ms);
149     EXPECT_EQ(base_target_delay_ms, total_audio_delay_ms);
150 
151     // Simulate that NetEQ introduces some audio delay.
152     const int kNeteqDelayIncrease = 50;
153     current_audio_delay_ms = base_target_delay_ms + kNeteqDelayIncrease;
154     clock_sender_.AdvanceTimeMilliseconds(1000);
155     clock_receiver_.AdvanceTimeMilliseconds(
156         1000 - std::max(kAudioDelayMs, kVideoDelayMs));
157     // Simulate base_target_delay_ms minimum delay in the VCM.
158     total_video_delay_ms = base_target_delay_ms;
159     EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
160                                current_audio_delay_ms, &total_audio_delay_ms,
161                                &total_video_delay_ms));
162     filtered_move = 3 * filtered_move +
163                     (kNeteqDelayIncrease + kAudioDelayMs - kVideoDelayMs) /
164                         kSmoothingFilter;
165     EXPECT_EQ(base_target_delay_ms + filtered_move, total_video_delay_ms);
166     EXPECT_EQ(base_target_delay_ms, total_audio_delay_ms);
167 
168     // Simulate that NetEQ reduces its delay.
169     const int kNeteqDelayDecrease = 10;
170     current_audio_delay_ms = base_target_delay_ms + kNeteqDelayDecrease;
171     clock_sender_.AdvanceTimeMilliseconds(1000);
172     clock_receiver_.AdvanceTimeMilliseconds(
173         1000 - std::max(kAudioDelayMs, kVideoDelayMs));
174     // Simulate base_target_delay_ms minimum delay in the VCM.
175     total_video_delay_ms = base_target_delay_ms;
176     EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
177                                current_audio_delay_ms, &total_audio_delay_ms,
178                                &total_video_delay_ms));
179     filtered_move =
180         filtered_move + (kNeteqDelayDecrease + kAudioDelayMs - kVideoDelayMs) /
181                             kSmoothingFilter;
182     EXPECT_EQ(base_target_delay_ms + filtered_move, total_video_delay_ms);
183     EXPECT_EQ(base_target_delay_ms, total_audio_delay_ms);
184   }
185 
BothDelayedVideoLaterTest(int base_target_delay_ms)186   void BothDelayedVideoLaterTest(int base_target_delay_ms) {
187     const int kAudioDelayMs = base_target_delay_ms + 100;
188     const int kVideoDelayMs = base_target_delay_ms + 300;
189     int current_audio_delay_ms = base_target_delay_ms;
190     int total_audio_delay_ms = 0;
191     int total_video_delay_ms = base_target_delay_ms;
192 
193     EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
194                                current_audio_delay_ms, &total_audio_delay_ms,
195                                &total_video_delay_ms));
196     EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
197     // The audio delay is not allowed to change more than this.
198     EXPECT_GE(base_target_delay_ms + kMaxChangeMs, total_audio_delay_ms);
199     int last_total_audio_delay_ms = total_audio_delay_ms;
200 
201     // Set new current audio delay.
202     current_audio_delay_ms = total_audio_delay_ms;
203     clock_sender_.AdvanceTimeMilliseconds(1000);
204     clock_receiver_.AdvanceTimeMilliseconds(800);
205     EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
206                                current_audio_delay_ms, &total_audio_delay_ms,
207                                &total_video_delay_ms));
208     EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
209     EXPECT_EQ(last_total_audio_delay_ms +
210                   MaxAudioDelayChangeMs(
211                       current_audio_delay_ms,
212                       base_target_delay_ms + kVideoDelayMs - kAudioDelayMs),
213               total_audio_delay_ms);
214     last_total_audio_delay_ms = total_audio_delay_ms;
215 
216     // Set new current audio delay.
217     current_audio_delay_ms = total_audio_delay_ms;
218     clock_sender_.AdvanceTimeMilliseconds(1000);
219     clock_receiver_.AdvanceTimeMilliseconds(800);
220     EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
221                                current_audio_delay_ms, &total_audio_delay_ms,
222                                &total_video_delay_ms));
223     EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
224     EXPECT_EQ(last_total_audio_delay_ms +
225                   MaxAudioDelayChangeMs(
226                       current_audio_delay_ms,
227                       base_target_delay_ms + kVideoDelayMs - kAudioDelayMs),
228               total_audio_delay_ms);
229     last_total_audio_delay_ms = total_audio_delay_ms;
230 
231     // Simulate that NetEQ for some reason reduced the delay.
232     current_audio_delay_ms = base_target_delay_ms + 10;
233     clock_sender_.AdvanceTimeMilliseconds(1000);
234     clock_receiver_.AdvanceTimeMilliseconds(800);
235     EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
236                                current_audio_delay_ms, &total_audio_delay_ms,
237                                &total_video_delay_ms));
238     EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
239     EXPECT_EQ(last_total_audio_delay_ms +
240                   MaxAudioDelayChangeMs(
241                       current_audio_delay_ms,
242                       base_target_delay_ms + kVideoDelayMs - kAudioDelayMs),
243               total_audio_delay_ms);
244     last_total_audio_delay_ms = total_audio_delay_ms;
245 
246     // Simulate that NetEQ for some reason significantly increased the delay.
247     current_audio_delay_ms = base_target_delay_ms + 350;
248     clock_sender_.AdvanceTimeMilliseconds(1000);
249     clock_receiver_.AdvanceTimeMilliseconds(800);
250     EXPECT_TRUE(DelayedStreams(kAudioDelayMs, kVideoDelayMs,
251                                current_audio_delay_ms, &total_audio_delay_ms,
252                                &total_video_delay_ms));
253     EXPECT_EQ(base_target_delay_ms, total_video_delay_ms);
254     EXPECT_EQ(last_total_audio_delay_ms +
255                   MaxAudioDelayChangeMs(
256                       current_audio_delay_ms,
257                       base_target_delay_ms + kVideoDelayMs - kAudioDelayMs),
258               total_audio_delay_ms);
259   }
260 
MaxAudioDelayChangeMs(int current_audio_delay_ms,int delay_ms) const261   int MaxAudioDelayChangeMs(int current_audio_delay_ms, int delay_ms) const {
262     int diff_ms = (delay_ms - current_audio_delay_ms) / kSmoothingFilter;
263     diff_ms = std::min(diff_ms, kMaxChangeMs);
264     diff_ms = std::max(diff_ms, -kMaxChangeMs);
265     return diff_ms;
266   }
267 
268   StreamSynchronization sync_;
269   SimulatedClock clock_sender_;
270   SimulatedClock clock_receiver_;
271   double audio_clock_drift_ = 1.0;
272   double video_clock_drift_ = 1.0;
273 };
274 
TEST_F(StreamSynchronizationTest,NoDelay)275 TEST_F(StreamSynchronizationTest, NoDelay) {
276   int total_audio_delay_ms = 0;
277   int total_video_delay_ms = 0;
278 
279   EXPECT_FALSE(DelayedStreams(/*audio_delay_ms=*/0, /*video_delay_ms=*/0,
280                               /*current_audio_delay_ms=*/0,
281                               &total_audio_delay_ms, &total_video_delay_ms));
282   EXPECT_EQ(0, total_audio_delay_ms);
283   EXPECT_EQ(0, total_video_delay_ms);
284 }
285 
TEST_F(StreamSynchronizationTest,VideoDelayed)286 TEST_F(StreamSynchronizationTest, VideoDelayed) {
287   const int kAudioDelayMs = 200;
288   int total_audio_delay_ms = 0;
289   int total_video_delay_ms = 0;
290 
291   EXPECT_TRUE(DelayedStreams(kAudioDelayMs, /*video_delay_ms=*/0,
292                              /*current_audio_delay_ms=*/0,
293                              &total_audio_delay_ms, &total_video_delay_ms));
294   EXPECT_EQ(0, total_audio_delay_ms);
295   // The delay is not allowed to change more than this.
296   EXPECT_EQ(kAudioDelayMs / kSmoothingFilter, total_video_delay_ms);
297 
298   // Simulate 0 minimum delay in the VCM.
299   total_video_delay_ms = 0;
300   clock_sender_.AdvanceTimeMilliseconds(1000);
301   clock_receiver_.AdvanceTimeMilliseconds(800);
302   EXPECT_TRUE(DelayedStreams(kAudioDelayMs, /*video_delay_ms=*/0,
303                              /*current_audio_delay_ms=*/0,
304                              &total_audio_delay_ms, &total_video_delay_ms));
305   EXPECT_EQ(0, total_audio_delay_ms);
306   EXPECT_EQ(2 * kAudioDelayMs / kSmoothingFilter, total_video_delay_ms);
307 
308   // Simulate 0 minimum delay in the VCM.
309   total_video_delay_ms = 0;
310   clock_sender_.AdvanceTimeMilliseconds(1000);
311   clock_receiver_.AdvanceTimeMilliseconds(800);
312   EXPECT_TRUE(DelayedStreams(kAudioDelayMs, /*video_delay_ms=*/0,
313                              /*current_audio_delay_ms=*/0,
314                              &total_audio_delay_ms, &total_video_delay_ms));
315   EXPECT_EQ(0, total_audio_delay_ms);
316   EXPECT_EQ(3 * kAudioDelayMs / kSmoothingFilter, total_video_delay_ms);
317 }
318 
TEST_F(StreamSynchronizationTest,AudioDelayed)319 TEST_F(StreamSynchronizationTest, AudioDelayed) {
320   const int kVideoDelayMs = 200;
321   int current_audio_delay_ms = 0;
322   int total_audio_delay_ms = 0;
323   int total_video_delay_ms = 0;
324 
325   EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
326                              current_audio_delay_ms, &total_audio_delay_ms,
327                              &total_video_delay_ms));
328   EXPECT_EQ(0, total_video_delay_ms);
329   // The delay is not allowed to change more than this.
330   EXPECT_EQ(kVideoDelayMs / kSmoothingFilter, total_audio_delay_ms);
331   int last_total_audio_delay_ms = total_audio_delay_ms;
332 
333   // Set new current audio delay.
334   current_audio_delay_ms = total_audio_delay_ms;
335   clock_sender_.AdvanceTimeMilliseconds(1000);
336   clock_receiver_.AdvanceTimeMilliseconds(800);
337   EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
338                              current_audio_delay_ms, &total_audio_delay_ms,
339                              &total_video_delay_ms));
340   EXPECT_EQ(0, total_video_delay_ms);
341   EXPECT_EQ(last_total_audio_delay_ms +
342                 MaxAudioDelayChangeMs(current_audio_delay_ms, kVideoDelayMs),
343             total_audio_delay_ms);
344   last_total_audio_delay_ms = total_audio_delay_ms;
345 
346   // Set new current audio delay.
347   current_audio_delay_ms = total_audio_delay_ms;
348   clock_sender_.AdvanceTimeMilliseconds(1000);
349   clock_receiver_.AdvanceTimeMilliseconds(800);
350   EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
351                              current_audio_delay_ms, &total_audio_delay_ms,
352                              &total_video_delay_ms));
353   EXPECT_EQ(0, total_video_delay_ms);
354   EXPECT_EQ(last_total_audio_delay_ms +
355                 MaxAudioDelayChangeMs(current_audio_delay_ms, kVideoDelayMs),
356             total_audio_delay_ms);
357   last_total_audio_delay_ms = total_audio_delay_ms;
358 
359   // Simulate that NetEQ for some reason reduced the delay.
360   current_audio_delay_ms = 10;
361   clock_sender_.AdvanceTimeMilliseconds(1000);
362   clock_receiver_.AdvanceTimeMilliseconds(800);
363   EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
364                              current_audio_delay_ms, &total_audio_delay_ms,
365                              &total_video_delay_ms));
366   EXPECT_EQ(0, total_video_delay_ms);
367   EXPECT_EQ(last_total_audio_delay_ms +
368                 MaxAudioDelayChangeMs(current_audio_delay_ms, kVideoDelayMs),
369             total_audio_delay_ms);
370   last_total_audio_delay_ms = total_audio_delay_ms;
371 
372   // Simulate that NetEQ for some reason significantly increased the delay.
373   current_audio_delay_ms = 350;
374   clock_sender_.AdvanceTimeMilliseconds(1000);
375   clock_receiver_.AdvanceTimeMilliseconds(800);
376   EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
377                              current_audio_delay_ms, &total_audio_delay_ms,
378                              &total_video_delay_ms));
379   EXPECT_EQ(0, total_video_delay_ms);
380   EXPECT_EQ(last_total_audio_delay_ms +
381                 MaxAudioDelayChangeMs(current_audio_delay_ms, kVideoDelayMs),
382             total_audio_delay_ms);
383 }
384 
TEST_F(StreamSynchronizationTest,NoAudioIncomingUnboundedIncrease)385 TEST_F(StreamSynchronizationTest, NoAudioIncomingUnboundedIncrease) {
386   // Test how audio delay can grow unbounded when audio stops coming in.
387   // This is handled in caller of RtpStreamsSynchronizer, for example in
388   // RtpStreamsSynchronizer by not updating delays when audio samples stop
389   // coming in.
390   const int kVideoDelayMs = 300;
391   const int kAudioDelayMs = 100;
392   int current_audio_delay_ms = kAudioDelayMs;
393   int total_audio_delay_ms = 0;
394   int total_video_delay_ms = 0;
395 
396   EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
397                              current_audio_delay_ms, &total_audio_delay_ms,
398                              &total_video_delay_ms));
399   EXPECT_EQ(0, total_video_delay_ms);
400   // The delay is not allowed to change more than this.
401   EXPECT_EQ((kVideoDelayMs - kAudioDelayMs) / kSmoothingFilter,
402             total_audio_delay_ms);
403   int last_total_audio_delay_ms = total_audio_delay_ms;
404 
405   // Set new current audio delay: simulate audio samples are flowing in.
406   current_audio_delay_ms = total_audio_delay_ms;
407 
408   clock_sender_.AdvanceTimeMilliseconds(1000);
409   clock_receiver_.AdvanceTimeMilliseconds(1000);
410   EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
411                              current_audio_delay_ms, &total_audio_delay_ms,
412                              &total_video_delay_ms));
413   EXPECT_EQ(0, total_video_delay_ms);
414   EXPECT_EQ(last_total_audio_delay_ms +
415                 MaxAudioDelayChangeMs(current_audio_delay_ms, kVideoDelayMs),
416             total_audio_delay_ms);
417   last_total_audio_delay_ms = total_audio_delay_ms;
418 
419   // Simulate no incoming audio by not update audio delay.
420   const int kSimulationSecs = 300;     // 5min
421   const int kMaxDeltaDelayMs = 10000;  // max delay for audio in webrtc
422   for (auto time_secs = 0; time_secs < kSimulationSecs; time_secs++) {
423     clock_sender_.AdvanceTimeMilliseconds(1000);
424     clock_receiver_.AdvanceTimeMilliseconds(1000);
425     EXPECT_TRUE(DelayedStreams(/*audio_delay_ms=*/0, kVideoDelayMs,
426                                current_audio_delay_ms, &total_audio_delay_ms,
427                                &total_video_delay_ms));
428     EXPECT_EQ(0, total_video_delay_ms);
429 
430     // Audio delay does not go above kMaxDeltaDelayMs.
431     EXPECT_EQ(std::min(kMaxDeltaDelayMs,
432                        last_total_audio_delay_ms +
433                            MaxAudioDelayChangeMs(current_audio_delay_ms,
434                                                  kVideoDelayMs)),
435               total_audio_delay_ms);
436     last_total_audio_delay_ms = total_audio_delay_ms;
437   }
438   // By now the audio delay has grown unbounded to kMaxDeltaDelayMs.
439   EXPECT_EQ(kMaxDeltaDelayMs, last_total_audio_delay_ms);
440 }
441 
TEST_F(StreamSynchronizationTest,BothDelayedVideoLater)442 TEST_F(StreamSynchronizationTest, BothDelayedVideoLater) {
443   BothDelayedVideoLaterTest(0);
444 }
445 
TEST_F(StreamSynchronizationTest,BothDelayedVideoLaterAudioClockDrift)446 TEST_F(StreamSynchronizationTest, BothDelayedVideoLaterAudioClockDrift) {
447   audio_clock_drift_ = 1.05;
448   BothDelayedVideoLaterTest(0);
449 }
450 
TEST_F(StreamSynchronizationTest,BothDelayedVideoLaterVideoClockDrift)451 TEST_F(StreamSynchronizationTest, BothDelayedVideoLaterVideoClockDrift) {
452   video_clock_drift_ = 1.05;
453   BothDelayedVideoLaterTest(0);
454 }
455 
TEST_F(StreamSynchronizationTest,BothDelayedAudioLater)456 TEST_F(StreamSynchronizationTest, BothDelayedAudioLater) {
457   BothDelayedAudioLaterTest(0);
458 }
459 
TEST_F(StreamSynchronizationTest,BothDelayedAudioClockDrift)460 TEST_F(StreamSynchronizationTest, BothDelayedAudioClockDrift) {
461   audio_clock_drift_ = 1.05;
462   BothDelayedAudioLaterTest(0);
463 }
464 
TEST_F(StreamSynchronizationTest,BothDelayedVideoClockDrift)465 TEST_F(StreamSynchronizationTest, BothDelayedVideoClockDrift) {
466   video_clock_drift_ = 1.05;
467   BothDelayedAudioLaterTest(0);
468 }
469 
TEST_F(StreamSynchronizationTest,BothEquallyDelayed)470 TEST_F(StreamSynchronizationTest, BothEquallyDelayed) {
471   const int kDelayMs = 2000;
472   int current_audio_delay_ms = kDelayMs;
473   int total_audio_delay_ms = 0;
474   int total_video_delay_ms = kDelayMs;
475   // In sync, expect no change.
476   EXPECT_FALSE(DelayedStreams(kDelayMs, kDelayMs, current_audio_delay_ms,
477                               &total_audio_delay_ms, &total_video_delay_ms));
478   // Trigger another call with the same values, delay should not be modified.
479   total_video_delay_ms = kDelayMs;
480   EXPECT_FALSE(DelayedStreams(kDelayMs, kDelayMs, current_audio_delay_ms,
481                               &total_audio_delay_ms, &total_video_delay_ms));
482   // Change delay value, delay should not be modified.
483   const int kDelayMs2 = 5000;
484   current_audio_delay_ms = kDelayMs2;
485   total_video_delay_ms = kDelayMs2;
486   EXPECT_FALSE(DelayedStreams(kDelayMs2, kDelayMs2, current_audio_delay_ms,
487                               &total_audio_delay_ms, &total_video_delay_ms));
488 }
489 
TEST_F(StreamSynchronizationTest,BothDelayedAudioLaterWithBaseDelay)490 TEST_F(StreamSynchronizationTest, BothDelayedAudioLaterWithBaseDelay) {
491   const int kBaseTargetDelayMs = 3000;
492   sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
493   BothDelayedAudioLaterTest(kBaseTargetDelayMs);
494 }
495 
TEST_F(StreamSynchronizationTest,BothDelayedAudioClockDriftWithBaseDelay)496 TEST_F(StreamSynchronizationTest, BothDelayedAudioClockDriftWithBaseDelay) {
497   const int kBaseTargetDelayMs = 3000;
498   sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
499   audio_clock_drift_ = 1.05;
500   BothDelayedAudioLaterTest(kBaseTargetDelayMs);
501 }
502 
TEST_F(StreamSynchronizationTest,BothDelayedVideoClockDriftWithBaseDelay)503 TEST_F(StreamSynchronizationTest, BothDelayedVideoClockDriftWithBaseDelay) {
504   const int kBaseTargetDelayMs = 3000;
505   sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
506   video_clock_drift_ = 1.05;
507   BothDelayedAudioLaterTest(kBaseTargetDelayMs);
508 }
509 
TEST_F(StreamSynchronizationTest,BothDelayedVideoLaterWithBaseDelay)510 TEST_F(StreamSynchronizationTest, BothDelayedVideoLaterWithBaseDelay) {
511   const int kBaseTargetDelayMs = 2000;
512   sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
513   BothDelayedVideoLaterTest(kBaseTargetDelayMs);
514 }
515 
TEST_F(StreamSynchronizationTest,BothDelayedVideoLaterAudioClockDriftWithBaseDelay)516 TEST_F(StreamSynchronizationTest,
517        BothDelayedVideoLaterAudioClockDriftWithBaseDelay) {
518   const int kBaseTargetDelayMs = 2000;
519   audio_clock_drift_ = 1.05;
520   sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
521   BothDelayedVideoLaterTest(kBaseTargetDelayMs);
522 }
523 
TEST_F(StreamSynchronizationTest,BothDelayedVideoLaterVideoClockDriftWithBaseDelay)524 TEST_F(StreamSynchronizationTest,
525        BothDelayedVideoLaterVideoClockDriftWithBaseDelay) {
526   const int kBaseTargetDelayMs = 2000;
527   video_clock_drift_ = 1.05;
528   sync_.SetTargetBufferingDelay(kBaseTargetDelayMs);
529   BothDelayedVideoLaterTest(kBaseTargetDelayMs);
530 }
531 
532 }  // namespace webrtc
533