stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 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 <math.h> |
| 12 | #include <algorithm> |
| 13 | |
| 14 | #include "gtest/gtest.h" |
| 15 | #include "video_engine/stream_synchronization.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | // These correspond to the same constants defined in vie_sync_module.cc. |
| 20 | enum { kMaxVideoDiffMs = 80 }; |
| 21 | enum { kMaxAudioDiffMs = 80 }; |
| 22 | enum { kMaxDelay = 1500 }; |
| 23 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 24 | // Test constants. |
| 25 | enum { kDefaultAudioFrequency = 8000 }; |
| 26 | enum { kDefaultVideoFrequency = 90000 }; |
| 27 | const double kNtpFracPerMs = 4.294967296E6; |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 28 | static const int kSmoothingFilter = 4 * 2; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 29 | |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 30 | class Time { |
| 31 | public: |
| 32 | explicit Time(int64_t offset) |
| 33 | : kNtpJan1970(2208988800UL), |
| 34 | time_now_ms_(offset) {} |
| 35 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 36 | synchronization::RtcpMeasurement GenerateRtcp(int frequency, |
| 37 | uint32_t offset) const { |
| 38 | synchronization::RtcpMeasurement rtcp; |
| 39 | NowNtp(&rtcp.ntp_secs, &rtcp.ntp_frac); |
| 40 | rtcp.rtp_timestamp = NowRtp(frequency, offset); |
| 41 | return rtcp; |
| 42 | } |
| 43 | |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 44 | void NowNtp(uint32_t* ntp_secs, uint32_t* ntp_frac) const { |
| 45 | *ntp_secs = time_now_ms_ / 1000 + kNtpJan1970; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 46 | int64_t remainder_ms = time_now_ms_ % 1000; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 47 | *ntp_frac = static_cast<uint32_t>( |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 48 | static_cast<double>(remainder_ms) * kNtpFracPerMs + 0.5); |
| 49 | } |
| 50 | |
| 51 | uint32_t NowRtp(int frequency, uint32_t offset) const { |
| 52 | return frequency * time_now_ms_ / 1000 + offset; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void IncreaseTimeMs(int64_t inc) { |
| 56 | time_now_ms_ += inc; |
| 57 | } |
| 58 | |
| 59 | int64_t time_now_ms() const { |
| 60 | return time_now_ms_; |
| 61 | } |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 62 | |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 63 | private: |
| 64 | // January 1970, in NTP seconds. |
| 65 | const uint32_t kNtpJan1970; |
| 66 | int64_t time_now_ms_; |
| 67 | }; |
| 68 | |
| 69 | class StreamSynchronizationTest : public ::testing::Test { |
| 70 | protected: |
| 71 | virtual void SetUp() { |
| 72 | sync_ = new StreamSynchronization(0, 0); |
| 73 | send_time_ = new Time(kSendTimeOffsetMs); |
| 74 | receive_time_ = new Time(kReceiveTimeOffsetMs); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 75 | audio_clock_drift_ = 1.0; |
| 76 | video_clock_drift_ = 1.0; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | virtual void TearDown() { |
| 80 | delete sync_; |
| 81 | delete send_time_; |
| 82 | delete receive_time_; |
| 83 | } |
| 84 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 85 | // Generates the necessary RTCP measurements and RTP timestamps and computes |
| 86 | // the audio and video delays needed to get the two streams in sync. |
| 87 | // |audio_delay_ms| and |video_delay_ms| are the number of milliseconds after |
| 88 | // capture which the frames are rendered. |
| 89 | // |current_audio_delay_ms| is the number of milliseconds which audio is |
| 90 | // currently being delayed by the receiver. |
| 91 | bool DelayedStreams(int audio_delay_ms, |
| 92 | int video_delay_ms, |
| 93 | int current_audio_delay_ms, |
| 94 | int* extra_audio_delay_ms, |
| 95 | int* total_video_delay_ms) { |
| 96 | int audio_frequency = static_cast<int>(kDefaultAudioFrequency * |
| 97 | audio_clock_drift_ + 0.5); |
| 98 | int audio_offset = 0; |
| 99 | int video_frequency = static_cast<int>(kDefaultVideoFrequency * |
| 100 | video_clock_drift_ + 0.5); |
| 101 | int video_offset = 0; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 102 | StreamSynchronization::Measurements audio; |
| 103 | StreamSynchronization::Measurements video; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 104 | // Generate NTP/RTP timestamp pair for both streams corresponding to RTCP. |
| 105 | audio.rtcp.push_front(send_time_->GenerateRtcp(audio_frequency, |
| 106 | audio_offset)); |
| 107 | send_time_->IncreaseTimeMs(100); |
| 108 | receive_time_->IncreaseTimeMs(100); |
| 109 | video.rtcp.push_front(send_time_->GenerateRtcp(video_frequency, |
| 110 | video_offset)); |
| 111 | send_time_->IncreaseTimeMs(900); |
| 112 | receive_time_->IncreaseTimeMs(900); |
| 113 | audio.rtcp.push_front(send_time_->GenerateRtcp(audio_frequency, |
| 114 | audio_offset)); |
| 115 | send_time_->IncreaseTimeMs(100); |
| 116 | receive_time_->IncreaseTimeMs(100); |
| 117 | video.rtcp.push_front(send_time_->GenerateRtcp(video_frequency, |
| 118 | video_offset)); |
| 119 | send_time_->IncreaseTimeMs(900); |
| 120 | receive_time_->IncreaseTimeMs(900); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 121 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 122 | // Capture an audio and a video frame at the same time. |
| 123 | audio.latest_timestamp = send_time_->NowRtp(audio_frequency, |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 124 | audio_offset); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 125 | video.latest_timestamp = send_time_->NowRtp(video_frequency, |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 126 | video_offset); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 127 | |
| 128 | if (audio_delay_ms > video_delay_ms) { |
| 129 | // Audio later than video. |
| 130 | receive_time_->IncreaseTimeMs(video_delay_ms); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 131 | video.latest_receive_time_ms = receive_time_->time_now_ms(); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 132 | receive_time_->IncreaseTimeMs(audio_delay_ms - video_delay_ms); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 133 | audio.latest_receive_time_ms = receive_time_->time_now_ms(); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 134 | } else { |
| 135 | // Video later than audio. |
| 136 | receive_time_->IncreaseTimeMs(audio_delay_ms); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 137 | audio.latest_receive_time_ms = receive_time_->time_now_ms(); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 138 | receive_time_->IncreaseTimeMs(video_delay_ms - audio_delay_ms); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 139 | video.latest_receive_time_ms = receive_time_->time_now_ms(); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 140 | } |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 141 | int relative_delay_ms; |
| 142 | StreamSynchronization::ComputeRelativeDelay(audio, video, |
| 143 | &relative_delay_ms); |
| 144 | EXPECT_EQ(video_delay_ms - audio_delay_ms, relative_delay_ms); |
| 145 | return sync_->ComputeDelays(relative_delay_ms, |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 146 | current_audio_delay_ms, |
| 147 | extra_audio_delay_ms, |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 148 | total_video_delay_ms); |
| 149 | } |
| 150 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 151 | // Simulate audio playback 300 ms after capture and video rendering 100 ms |
| 152 | // after capture. Verify that the correct extra delays are calculated for |
| 153 | // audio and video, and that they change correctly when we simulate that |
| 154 | // NetEQ or the VCM adds more delay to the streams. |
| 155 | // TODO(holmer): This is currently wrong! We should simply change |
| 156 | // audio_delay_ms or video_delay_ms since those now include VCM and NetEQ |
| 157 | // delays. |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 158 | void BothDelayedAudioLaterTest(int base_target_delay) { |
| 159 | int current_audio_delay_ms = base_target_delay; |
| 160 | int audio_delay_ms = base_target_delay + 300; |
| 161 | int video_delay_ms = base_target_delay + 100; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 162 | int extra_audio_delay_ms = 0; |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 163 | int total_video_delay_ms = base_target_delay; |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 164 | int filtered_move = (audio_delay_ms - video_delay_ms) / kSmoothingFilter; |
pwestin@webrtc.org | 4e545b3 | 2013-04-26 15:23:34 +0000 | [diff] [blame^] | 165 | const int kNeteqDelayIncrease = 50; |
| 166 | const int kNeteqDelayDecrease = 10; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 167 | |
| 168 | EXPECT_TRUE(DelayedStreams(audio_delay_ms, |
| 169 | video_delay_ms, |
| 170 | current_audio_delay_ms, |
| 171 | &extra_audio_delay_ms, |
| 172 | &total_video_delay_ms)); |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 173 | EXPECT_EQ(base_target_delay + filtered_move, total_video_delay_ms); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 174 | EXPECT_EQ(base_target_delay, extra_audio_delay_ms); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 175 | current_audio_delay_ms = extra_audio_delay_ms; |
| 176 | |
| 177 | send_time_->IncreaseTimeMs(1000); |
| 178 | receive_time_->IncreaseTimeMs(1000 - std::max(audio_delay_ms, |
| 179 | video_delay_ms)); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 180 | // Simulate base_target_delay minimum delay in the VCM. |
| 181 | total_video_delay_ms = base_target_delay; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 182 | EXPECT_TRUE(DelayedStreams(audio_delay_ms, |
| 183 | video_delay_ms, |
| 184 | current_audio_delay_ms, |
| 185 | &extra_audio_delay_ms, |
| 186 | &total_video_delay_ms)); |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 187 | EXPECT_EQ(base_target_delay + 2 * filtered_move, total_video_delay_ms); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 188 | EXPECT_EQ(base_target_delay, extra_audio_delay_ms); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 189 | current_audio_delay_ms = extra_audio_delay_ms; |
| 190 | |
| 191 | send_time_->IncreaseTimeMs(1000); |
| 192 | receive_time_->IncreaseTimeMs(1000 - std::max(audio_delay_ms, |
| 193 | video_delay_ms)); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 194 | // Simulate base_target_delay minimum delay in the VCM. |
| 195 | total_video_delay_ms = base_target_delay; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 196 | EXPECT_TRUE(DelayedStreams(audio_delay_ms, |
| 197 | video_delay_ms, |
| 198 | current_audio_delay_ms, |
| 199 | &extra_audio_delay_ms, |
| 200 | &total_video_delay_ms)); |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 201 | EXPECT_EQ(base_target_delay + 3 * filtered_move, total_video_delay_ms); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 202 | EXPECT_EQ(base_target_delay, extra_audio_delay_ms); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 203 | |
| 204 | // Simulate that NetEQ introduces some audio delay. |
pwestin@webrtc.org | 4e545b3 | 2013-04-26 15:23:34 +0000 | [diff] [blame^] | 205 | current_audio_delay_ms = base_target_delay + kNeteqDelayIncrease; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 206 | send_time_->IncreaseTimeMs(1000); |
| 207 | receive_time_->IncreaseTimeMs(1000 - std::max(audio_delay_ms, |
| 208 | video_delay_ms)); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 209 | // Simulate base_target_delay minimum delay in the VCM. |
| 210 | total_video_delay_ms = base_target_delay; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 211 | EXPECT_TRUE(DelayedStreams(audio_delay_ms, |
| 212 | video_delay_ms, |
| 213 | current_audio_delay_ms, |
| 214 | &extra_audio_delay_ms, |
| 215 | &total_video_delay_ms)); |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 216 | filtered_move = 3 * filtered_move + |
pwestin@webrtc.org | 4e545b3 | 2013-04-26 15:23:34 +0000 | [diff] [blame^] | 217 | (kNeteqDelayIncrease + audio_delay_ms - video_delay_ms) / |
| 218 | kSmoothingFilter; |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 219 | EXPECT_EQ(base_target_delay + filtered_move, total_video_delay_ms); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 220 | EXPECT_EQ(base_target_delay, extra_audio_delay_ms); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 221 | |
| 222 | // Simulate that NetEQ reduces its delay. |
pwestin@webrtc.org | 4e545b3 | 2013-04-26 15:23:34 +0000 | [diff] [blame^] | 223 | current_audio_delay_ms = base_target_delay + kNeteqDelayDecrease; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 224 | send_time_->IncreaseTimeMs(1000); |
| 225 | receive_time_->IncreaseTimeMs(1000 - std::max(audio_delay_ms, |
| 226 | video_delay_ms)); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 227 | // Simulate base_target_delay minimum delay in the VCM. |
| 228 | total_video_delay_ms = base_target_delay; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 229 | EXPECT_TRUE(DelayedStreams(audio_delay_ms, |
| 230 | video_delay_ms, |
| 231 | current_audio_delay_ms, |
| 232 | &extra_audio_delay_ms, |
| 233 | &total_video_delay_ms)); |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 234 | |
| 235 | filtered_move = filtered_move + |
pwestin@webrtc.org | 4e545b3 | 2013-04-26 15:23:34 +0000 | [diff] [blame^] | 236 | (kNeteqDelayDecrease + audio_delay_ms - video_delay_ms) / |
| 237 | kSmoothingFilter; |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 238 | |
| 239 | EXPECT_EQ(base_target_delay + filtered_move, total_video_delay_ms); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 240 | EXPECT_EQ(base_target_delay, extra_audio_delay_ms); |
| 241 | } |
| 242 | |
| 243 | void BothDelayedVideoLaterTest(int base_target_delay) { |
| 244 | int current_audio_delay_ms = base_target_delay; |
| 245 | int audio_delay_ms = base_target_delay + 100; |
| 246 | int video_delay_ms = base_target_delay + 300; |
| 247 | int extra_audio_delay_ms = 0; |
| 248 | int total_video_delay_ms = base_target_delay; |
| 249 | |
| 250 | EXPECT_TRUE(DelayedStreams(audio_delay_ms, |
| 251 | video_delay_ms, |
| 252 | current_audio_delay_ms, |
| 253 | &extra_audio_delay_ms, |
| 254 | &total_video_delay_ms)); |
| 255 | EXPECT_EQ(base_target_delay, total_video_delay_ms); |
| 256 | // The audio delay is not allowed to change more than this in 1 second. |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 257 | EXPECT_GE(base_target_delay + kMaxAudioDiffMs, extra_audio_delay_ms); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 258 | current_audio_delay_ms = extra_audio_delay_ms; |
| 259 | int current_extra_delay_ms = extra_audio_delay_ms; |
| 260 | |
| 261 | send_time_->IncreaseTimeMs(1000); |
| 262 | receive_time_->IncreaseTimeMs(800); |
| 263 | EXPECT_TRUE(DelayedStreams(audio_delay_ms, |
| 264 | video_delay_ms, |
| 265 | current_audio_delay_ms, |
| 266 | &extra_audio_delay_ms, |
| 267 | &total_video_delay_ms)); |
| 268 | EXPECT_EQ(base_target_delay, total_video_delay_ms); |
| 269 | // The audio delay is not allowed to change more than the half of the |
| 270 | // required change in delay. |
| 271 | EXPECT_EQ(current_extra_delay_ms + MaxAudioDelayIncrease( |
| 272 | current_audio_delay_ms, |
| 273 | base_target_delay + video_delay_ms - audio_delay_ms), |
| 274 | extra_audio_delay_ms); |
| 275 | current_audio_delay_ms = extra_audio_delay_ms; |
| 276 | current_extra_delay_ms = extra_audio_delay_ms; |
| 277 | |
| 278 | send_time_->IncreaseTimeMs(1000); |
| 279 | receive_time_->IncreaseTimeMs(800); |
| 280 | EXPECT_TRUE(DelayedStreams(audio_delay_ms, |
| 281 | video_delay_ms, |
| 282 | current_audio_delay_ms, |
| 283 | &extra_audio_delay_ms, |
| 284 | &total_video_delay_ms)); |
| 285 | EXPECT_EQ(base_target_delay, total_video_delay_ms); |
| 286 | // The audio delay is not allowed to change more than the half of the |
| 287 | // required change in delay. |
| 288 | EXPECT_EQ(current_extra_delay_ms + MaxAudioDelayIncrease( |
| 289 | current_audio_delay_ms, |
| 290 | base_target_delay + video_delay_ms - audio_delay_ms), |
| 291 | extra_audio_delay_ms); |
| 292 | current_extra_delay_ms = extra_audio_delay_ms; |
| 293 | |
| 294 | // Simulate that NetEQ for some reason reduced the delay. |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 295 | current_audio_delay_ms = base_target_delay + 10; |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 296 | send_time_->IncreaseTimeMs(1000); |
| 297 | receive_time_->IncreaseTimeMs(800); |
| 298 | EXPECT_TRUE(DelayedStreams(audio_delay_ms, |
| 299 | video_delay_ms, |
| 300 | current_audio_delay_ms, |
| 301 | &extra_audio_delay_ms, |
| 302 | &total_video_delay_ms)); |
| 303 | EXPECT_EQ(base_target_delay, total_video_delay_ms); |
| 304 | // Since we only can ask NetEQ for a certain amount of extra delay, and |
| 305 | // we only measure the total NetEQ delay, we will ask for additional delay |
| 306 | // here to try to stay in sync. |
| 307 | EXPECT_EQ(current_extra_delay_ms + MaxAudioDelayIncrease( |
| 308 | current_audio_delay_ms, |
| 309 | base_target_delay + video_delay_ms - audio_delay_ms), |
| 310 | extra_audio_delay_ms); |
| 311 | current_extra_delay_ms = extra_audio_delay_ms; |
| 312 | |
| 313 | // Simulate that NetEQ for some reason significantly increased the delay. |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 314 | current_audio_delay_ms = base_target_delay + 350; |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 315 | send_time_->IncreaseTimeMs(1000); |
| 316 | receive_time_->IncreaseTimeMs(800); |
| 317 | EXPECT_TRUE(DelayedStreams(audio_delay_ms, |
| 318 | video_delay_ms, |
| 319 | current_audio_delay_ms, |
| 320 | &extra_audio_delay_ms, |
| 321 | &total_video_delay_ms)); |
| 322 | EXPECT_EQ(base_target_delay, total_video_delay_ms); |
| 323 | // The audio delay is not allowed to change more than the half of the |
| 324 | // required change in delay. |
| 325 | EXPECT_EQ(current_extra_delay_ms + MaxAudioDelayIncrease( |
| 326 | current_audio_delay_ms, |
| 327 | base_target_delay + video_delay_ms - audio_delay_ms), |
| 328 | extra_audio_delay_ms); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 329 | } |
| 330 | |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 331 | int MaxAudioDelayIncrease(int current_audio_delay_ms, int delay_ms) { |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 332 | return std::min((delay_ms - current_audio_delay_ms) / kSmoothingFilter, |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 333 | static_cast<int>(kMaxAudioDiffMs)); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | int MaxAudioDelayDecrease(int current_audio_delay_ms, int delay_ms) { |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 337 | return std::max((delay_ms - current_audio_delay_ms) / kSmoothingFilter, |
| 338 | -kMaxAudioDiffMs); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 339 | } |
| 340 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 341 | enum { kSendTimeOffsetMs = 98765 }; |
| 342 | enum { kReceiveTimeOffsetMs = 43210 }; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 343 | |
| 344 | StreamSynchronization* sync_; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 345 | Time* send_time_; // The simulated clock at the sender. |
| 346 | Time* receive_time_; // The simulated clock at the receiver. |
| 347 | double audio_clock_drift_; |
| 348 | double video_clock_drift_; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | TEST_F(StreamSynchronizationTest, NoDelay) { |
| 352 | uint32_t current_audio_delay_ms = 0; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 353 | int extra_audio_delay_ms = 0; |
| 354 | int total_video_delay_ms = 0; |
| 355 | |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 356 | EXPECT_FALSE(DelayedStreams(0, 0, current_audio_delay_ms, |
| 357 | &extra_audio_delay_ms, &total_video_delay_ms)); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 358 | EXPECT_EQ(0, extra_audio_delay_ms); |
| 359 | EXPECT_EQ(0, total_video_delay_ms); |
| 360 | } |
| 361 | |
| 362 | TEST_F(StreamSynchronizationTest, VideoDelay) { |
| 363 | uint32_t current_audio_delay_ms = 0; |
| 364 | int delay_ms = 200; |
| 365 | int extra_audio_delay_ms = 0; |
| 366 | int total_video_delay_ms = 0; |
| 367 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 368 | EXPECT_TRUE(DelayedStreams(delay_ms, 0, current_audio_delay_ms, |
| 369 | &extra_audio_delay_ms, &total_video_delay_ms)); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 370 | EXPECT_EQ(0, extra_audio_delay_ms); |
| 371 | // The video delay is not allowed to change more than this in 1 second. |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 372 | EXPECT_EQ(delay_ms / kSmoothingFilter, total_video_delay_ms); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 373 | |
| 374 | send_time_->IncreaseTimeMs(1000); |
| 375 | receive_time_->IncreaseTimeMs(800); |
| 376 | // Simulate 0 minimum delay in the VCM. |
| 377 | total_video_delay_ms = 0; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 378 | EXPECT_TRUE(DelayedStreams(delay_ms, 0, current_audio_delay_ms, |
| 379 | &extra_audio_delay_ms, &total_video_delay_ms)); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 380 | EXPECT_EQ(0, extra_audio_delay_ms); |
| 381 | // The video delay is not allowed to change more than this in 1 second. |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 382 | EXPECT_EQ(2 * delay_ms / kSmoothingFilter, total_video_delay_ms); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 383 | |
| 384 | send_time_->IncreaseTimeMs(1000); |
| 385 | receive_time_->IncreaseTimeMs(800); |
| 386 | // Simulate 0 minimum delay in the VCM. |
| 387 | total_video_delay_ms = 0; |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 388 | EXPECT_TRUE(DelayedStreams(delay_ms, 0, current_audio_delay_ms, |
| 389 | &extra_audio_delay_ms, &total_video_delay_ms)); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 390 | EXPECT_EQ(0, extra_audio_delay_ms); |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 391 | EXPECT_EQ(3 * delay_ms / kSmoothingFilter, total_video_delay_ms); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | TEST_F(StreamSynchronizationTest, AudioDelay) { |
| 395 | int current_audio_delay_ms = 0; |
| 396 | int delay_ms = 200; |
| 397 | int extra_audio_delay_ms = 0; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 398 | int total_video_delay_ms = 0; |
| 399 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 400 | EXPECT_TRUE(DelayedStreams(0, delay_ms, current_audio_delay_ms, |
| 401 | &extra_audio_delay_ms, &total_video_delay_ms)); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 402 | EXPECT_EQ(0, total_video_delay_ms); |
| 403 | // The audio delay is not allowed to change more than this in 1 second. |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 404 | EXPECT_EQ(delay_ms / kSmoothingFilter, extra_audio_delay_ms); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 405 | current_audio_delay_ms = extra_audio_delay_ms; |
andrew@webrtc.org | d7a71d0 | 2012-08-01 01:40:02 +0000 | [diff] [blame] | 406 | int current_extra_delay_ms = extra_audio_delay_ms; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 407 | |
| 408 | send_time_->IncreaseTimeMs(1000); |
| 409 | receive_time_->IncreaseTimeMs(800); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 410 | EXPECT_TRUE(DelayedStreams(0, delay_ms, current_audio_delay_ms, |
| 411 | &extra_audio_delay_ms, &total_video_delay_ms)); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 412 | EXPECT_EQ(0, total_video_delay_ms); |
| 413 | // The audio delay is not allowed to change more than the half of the required |
| 414 | // change in delay. |
| 415 | EXPECT_EQ(current_extra_delay_ms + |
| 416 | MaxAudioDelayIncrease(current_audio_delay_ms, delay_ms), |
| 417 | extra_audio_delay_ms); |
| 418 | current_audio_delay_ms = extra_audio_delay_ms; |
| 419 | current_extra_delay_ms = extra_audio_delay_ms; |
| 420 | |
| 421 | send_time_->IncreaseTimeMs(1000); |
| 422 | receive_time_->IncreaseTimeMs(800); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 423 | EXPECT_TRUE(DelayedStreams(0, delay_ms, current_audio_delay_ms, |
| 424 | &extra_audio_delay_ms, &total_video_delay_ms)); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 425 | EXPECT_EQ(0, total_video_delay_ms); |
| 426 | // The audio delay is not allowed to change more than the half of the required |
| 427 | // change in delay. |
| 428 | EXPECT_EQ(current_extra_delay_ms + |
| 429 | MaxAudioDelayIncrease(current_audio_delay_ms, delay_ms), |
| 430 | extra_audio_delay_ms); |
| 431 | current_extra_delay_ms = extra_audio_delay_ms; |
| 432 | |
| 433 | // Simulate that NetEQ for some reason reduced the delay. |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 434 | current_audio_delay_ms = 10; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 435 | send_time_->IncreaseTimeMs(1000); |
| 436 | receive_time_->IncreaseTimeMs(800); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 437 | EXPECT_TRUE(DelayedStreams(0, delay_ms, current_audio_delay_ms, |
| 438 | &extra_audio_delay_ms, &total_video_delay_ms)); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 439 | EXPECT_EQ(0, total_video_delay_ms); |
| 440 | // Since we only can ask NetEQ for a certain amount of extra delay, and |
| 441 | // we only measure the total NetEQ delay, we will ask for additional delay |
| 442 | // here to try to |
| 443 | EXPECT_EQ(current_extra_delay_ms + |
| 444 | MaxAudioDelayIncrease(current_audio_delay_ms, delay_ms), |
| 445 | extra_audio_delay_ms); |
| 446 | current_extra_delay_ms = extra_audio_delay_ms; |
| 447 | |
| 448 | // Simulate that NetEQ for some reason significantly increased the delay. |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 449 | current_audio_delay_ms = 350; |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 450 | send_time_->IncreaseTimeMs(1000); |
| 451 | receive_time_->IncreaseTimeMs(800); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 452 | EXPECT_TRUE(DelayedStreams(0, delay_ms, current_audio_delay_ms, |
| 453 | &extra_audio_delay_ms, &total_video_delay_ms)); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 454 | EXPECT_EQ(0, total_video_delay_ms); |
| 455 | // The audio delay is not allowed to change more than the half of the required |
| 456 | // change in delay. |
| 457 | EXPECT_EQ(current_extra_delay_ms + |
| 458 | MaxAudioDelayDecrease(current_audio_delay_ms, delay_ms), |
| 459 | extra_audio_delay_ms); |
| 460 | } |
| 461 | |
| 462 | TEST_F(StreamSynchronizationTest, BothDelayedVideoLater) { |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 463 | BothDelayedVideoLaterTest(0); |
| 464 | } |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 465 | |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 466 | TEST_F(StreamSynchronizationTest, BothDelayedVideoLaterAudioClockDrift) { |
| 467 | audio_clock_drift_ = 1.05; |
| 468 | BothDelayedVideoLaterTest(0); |
| 469 | } |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 470 | |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 471 | TEST_F(StreamSynchronizationTest, BothDelayedVideoLaterVideoClockDrift) { |
| 472 | video_clock_drift_ = 1.05; |
| 473 | BothDelayedVideoLaterTest(0); |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | TEST_F(StreamSynchronizationTest, BothDelayedAudioLater) { |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 477 | BothDelayedAudioLaterTest(0); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 478 | } |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 479 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 480 | TEST_F(StreamSynchronizationTest, BothDelayedAudioClockDrift) { |
| 481 | audio_clock_drift_ = 1.05; |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 482 | BothDelayedAudioLaterTest(0); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 483 | } |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 484 | |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 485 | TEST_F(StreamSynchronizationTest, BothDelayedVideoClockDrift) { |
| 486 | video_clock_drift_ = 1.05; |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 487 | BothDelayedAudioLaterTest(0); |
stefan@webrtc.org | 7c3523c | 2012-09-11 07:00:42 +0000 | [diff] [blame] | 488 | } |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 489 | |
| 490 | TEST_F(StreamSynchronizationTest, BaseDelay) { |
| 491 | int base_target_delay_ms = 2000; |
| 492 | int current_audio_delay_ms = 2000; |
| 493 | int extra_audio_delay_ms = 0; |
| 494 | int total_video_delay_ms = base_target_delay_ms; |
| 495 | sync_->SetTargetBufferingDelay(base_target_delay_ms); |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 496 | // We are in sync don't change. |
| 497 | EXPECT_FALSE(DelayedStreams(base_target_delay_ms, base_target_delay_ms, |
| 498 | current_audio_delay_ms, |
| 499 | &extra_audio_delay_ms, &total_video_delay_ms)); |
mikhal@webrtc.org | 0d8d010 | 2013-02-22 19:30:44 +0000 | [diff] [blame] | 500 | // Triggering another call with the same values. Delay should not be modified. |
| 501 | base_target_delay_ms = 2000; |
| 502 | current_audio_delay_ms = base_target_delay_ms; |
| 503 | total_video_delay_ms = base_target_delay_ms; |
| 504 | sync_->SetTargetBufferingDelay(base_target_delay_ms); |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 505 | // We are in sync don't change. |
| 506 | EXPECT_FALSE(DelayedStreams(base_target_delay_ms, base_target_delay_ms, |
| 507 | current_audio_delay_ms, |
| 508 | &extra_audio_delay_ms, &total_video_delay_ms)); |
mikhal@webrtc.org | 0d8d010 | 2013-02-22 19:30:44 +0000 | [diff] [blame] | 509 | // Changing delay value - intended to test this module only. In practice it |
| 510 | // would take VoE time to adapt. |
| 511 | base_target_delay_ms = 5000; |
| 512 | current_audio_delay_ms = base_target_delay_ms; |
| 513 | total_video_delay_ms = base_target_delay_ms; |
| 514 | sync_->SetTargetBufferingDelay(base_target_delay_ms); |
pwestin@webrtc.org | 6311733 | 2013-04-22 18:57:14 +0000 | [diff] [blame] | 515 | // We are in sync don't change. |
| 516 | EXPECT_FALSE(DelayedStreams(base_target_delay_ms, base_target_delay_ms, |
| 517 | current_audio_delay_ms, |
| 518 | &extra_audio_delay_ms, &total_video_delay_ms)); |
mikhal@webrtc.org | ef9f76a | 2013-02-15 23:22:18 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | TEST_F(StreamSynchronizationTest, BothDelayedAudioLaterWithBaseDelay) { |
| 522 | int base_target_delay_ms = 3000; |
| 523 | sync_->SetTargetBufferingDelay(base_target_delay_ms); |
| 524 | BothDelayedAudioLaterTest(base_target_delay_ms); |
| 525 | } |
| 526 | |
| 527 | TEST_F(StreamSynchronizationTest, BothDelayedAudioClockDriftWithBaseDelay) { |
| 528 | int base_target_delay_ms = 3000; |
| 529 | sync_->SetTargetBufferingDelay(base_target_delay_ms); |
| 530 | audio_clock_drift_ = 1.05; |
| 531 | BothDelayedAudioLaterTest(base_target_delay_ms); |
| 532 | } |
| 533 | |
| 534 | TEST_F(StreamSynchronizationTest, BothDelayedVideoClockDriftWithBaseDelay) { |
| 535 | int base_target_delay_ms = 3000; |
| 536 | sync_->SetTargetBufferingDelay(base_target_delay_ms); |
| 537 | video_clock_drift_ = 1.05; |
| 538 | BothDelayedAudioLaterTest(base_target_delay_ms); |
| 539 | } |
| 540 | |
| 541 | TEST_F(StreamSynchronizationTest, BothDelayedVideoLaterWithBaseDelay) { |
| 542 | int base_target_delay_ms = 2000; |
| 543 | sync_->SetTargetBufferingDelay(base_target_delay_ms); |
| 544 | BothDelayedVideoLaterTest(base_target_delay_ms); |
| 545 | } |
| 546 | |
| 547 | TEST_F(StreamSynchronizationTest, |
| 548 | BothDelayedVideoLaterAudioClockDriftWithBaseDelay) { |
| 549 | int base_target_delay_ms = 2000; |
| 550 | audio_clock_drift_ = 1.05; |
| 551 | sync_->SetTargetBufferingDelay(base_target_delay_ms); |
| 552 | BothDelayedVideoLaterTest(base_target_delay_ms); |
| 553 | } |
| 554 | |
| 555 | TEST_F(StreamSynchronizationTest, |
| 556 | BothDelayedVideoLaterVideoClockDriftWithBaseDelay) { |
| 557 | int base_target_delay_ms = 2000; |
| 558 | video_clock_drift_ = 1.05; |
| 559 | sync_->SetTargetBufferingDelay(base_target_delay_ms); |
| 560 | BothDelayedVideoLaterTest(base_target_delay_ms); |
| 561 | } |
| 562 | |
stefan@webrtc.org | 5f28498 | 2012-06-28 07:51:16 +0000 | [diff] [blame] | 563 | } // namespace webrtc |