You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

545 lines
23 KiB

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