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.

189 lines
6.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2016 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 "rtc_base/timestamp_aligner.h"
  11. #include <math.h>
  12. #include <algorithm>
  13. #include <limits>
  14. #include "rtc_base/random.h"
  15. #include "rtc_base/time_utils.h"
  16. #include "test/gtest.h"
  17. namespace rtc {
  18. namespace {
  19. // Computes the difference x_k - mean(x), when x_k is the linear sequence x_k =
  20. // k, and the "mean" is plain mean for the first |window_size| samples, followed
  21. // by exponential averaging with weight 1 / |window_size| for each new sample.
  22. // This is needed to predict the effect of camera clock drift on the timestamp
  23. // translation. See the comment on TimestampAligner::UpdateOffset for more
  24. // context.
  25. double MeanTimeDifference(int nsamples, int window_size) {
  26. if (nsamples <= window_size) {
  27. // Plain averaging.
  28. return nsamples / 2.0;
  29. } else {
  30. // Exponential convergence towards
  31. // interval_error * (window_size - 1)
  32. double alpha = 1.0 - 1.0 / window_size;
  33. return ((window_size - 1) -
  34. (window_size / 2.0 - 1) * pow(alpha, nsamples - window_size));
  35. }
  36. }
  37. class TimestampAlignerForTest : public TimestampAligner {
  38. // Make internal methods accessible to testing.
  39. public:
  40. using TimestampAligner::ClipTimestamp;
  41. using TimestampAligner::UpdateOffset;
  42. };
  43. void TestTimestampFilter(double rel_freq_error) {
  44. TimestampAlignerForTest timestamp_aligner_for_test;
  45. TimestampAligner timestamp_aligner;
  46. const int64_t kEpoch = 10000;
  47. const int64_t kJitterUs = 5000;
  48. const int64_t kIntervalUs = 33333; // 30 FPS
  49. const int kWindowSize = 100;
  50. const int kNumFrames = 3 * kWindowSize;
  51. int64_t interval_error_us = kIntervalUs * rel_freq_error;
  52. int64_t system_start_us = rtc::TimeMicros();
  53. webrtc::Random random(17);
  54. int64_t prev_translated_time_us = system_start_us;
  55. for (int i = 0; i < kNumFrames; i++) {
  56. // Camera time subject to drift.
  57. int64_t camera_time_us = kEpoch + i * (kIntervalUs + interval_error_us);
  58. int64_t system_time_us = system_start_us + i * kIntervalUs;
  59. // And system time readings are subject to jitter.
  60. int64_t system_measured_us = system_time_us + random.Rand(kJitterUs);
  61. int64_t offset_us = timestamp_aligner_for_test.UpdateOffset(
  62. camera_time_us, system_measured_us);
  63. int64_t filtered_time_us = camera_time_us + offset_us;
  64. int64_t translated_time_us = timestamp_aligner_for_test.ClipTimestamp(
  65. filtered_time_us, system_measured_us);
  66. // Check that we get identical result from the all-in-one helper method.
  67. ASSERT_EQ(translated_time_us, timestamp_aligner.TranslateTimestamp(
  68. camera_time_us, system_measured_us));
  69. EXPECT_LE(translated_time_us, system_measured_us);
  70. EXPECT_GE(translated_time_us,
  71. prev_translated_time_us + rtc::kNumMicrosecsPerMillisec);
  72. // The relative frequency error contributes to the expected error
  73. // by a factor which is the difference between the current time
  74. // and the average of earlier sample times.
  75. int64_t expected_error_us =
  76. kJitterUs / 2 +
  77. rel_freq_error * kIntervalUs * MeanTimeDifference(i, kWindowSize);
  78. int64_t bias_us = filtered_time_us - translated_time_us;
  79. EXPECT_GE(bias_us, 0);
  80. if (i == 0) {
  81. EXPECT_EQ(translated_time_us, system_measured_us);
  82. } else {
  83. EXPECT_NEAR(filtered_time_us, system_time_us + expected_error_us,
  84. 2.0 * kJitterUs / sqrt(std::max(i, kWindowSize)));
  85. }
  86. // If the camera clock runs too fast (rel_freq_error > 0.0), The
  87. // bias is expected to roughly cancel the expected error from the
  88. // clock drift, as this grows. Otherwise, it reflects the
  89. // measurement noise. The tolerances here were selected after some
  90. // trial and error.
  91. if (i < 10 || rel_freq_error <= 0.0) {
  92. EXPECT_LE(bias_us, 3000);
  93. } else {
  94. EXPECT_NEAR(bias_us, expected_error_us, 1500);
  95. }
  96. prev_translated_time_us = translated_time_us;
  97. }
  98. }
  99. } // Anonymous namespace
  100. TEST(TimestampAlignerTest, AttenuateTimestampJitterNoDrift) {
  101. TestTimestampFilter(0.0);
  102. }
  103. // 100 ppm is a worst case for a reasonable crystal.
  104. TEST(TimestampAlignerTest, AttenuateTimestampJitterSmallPosDrift) {
  105. TestTimestampFilter(0.0001);
  106. }
  107. TEST(TimestampAlignerTest, AttenuateTimestampJitterSmallNegDrift) {
  108. TestTimestampFilter(-0.0001);
  109. }
  110. // 3000 ppm, 3 ms / s, is the worst observed drift, see
  111. // https://bugs.chromium.org/p/webrtc/issues/detail?id=5456
  112. TEST(TimestampAlignerTest, AttenuateTimestampJitterLargePosDrift) {
  113. TestTimestampFilter(0.003);
  114. }
  115. TEST(TimestampAlignerTest, AttenuateTimestampJitterLargeNegDrift) {
  116. TestTimestampFilter(-0.003);
  117. }
  118. // Exhibits a mostly hypothetical problem, where certain inputs to the
  119. // TimestampAligner.UpdateOffset filter result in non-monotonous
  120. // translated timestamps. This test verifies that the ClipTimestamp
  121. // logic handles this case correctly.
  122. TEST(TimestampAlignerTest, ClipToMonotonous) {
  123. TimestampAlignerForTest timestamp_aligner;
  124. // For system time stamps { 0, s1, s1 + s2 }, and camera timestamps
  125. // {0, c1, c1 + c2}, we exhibit non-monotonous behaviour if and only
  126. // if c1 > s1 + 2 s2 + 4 c2.
  127. const int kNumSamples = 3;
  128. const int64_t camera_time_us[kNumSamples] = {0, 80000, 90001};
  129. const int64_t system_time_us[kNumSamples] = {0, 10000, 20000};
  130. const int64_t expected_offset_us[kNumSamples] = {0, -35000, -46667};
  131. // Non-monotonic translated timestamps can happen when only for
  132. // translated timestamps in the future. Which is tolerated if
  133. // |timestamp_aligner.clip_bias_us| is large enough. Instead of
  134. // changing that private member for this test, just add the bias to
  135. // |system_time_us| when calling ClipTimestamp.
  136. const int64_t kClipBiasUs = 100000;
  137. bool did_clip = false;
  138. int64_t prev_timestamp_us = std::numeric_limits<int64_t>::min();
  139. for (int i = 0; i < kNumSamples; i++) {
  140. int64_t offset_us =
  141. timestamp_aligner.UpdateOffset(camera_time_us[i], system_time_us[i]);
  142. EXPECT_EQ(offset_us, expected_offset_us[i]);
  143. int64_t translated_timestamp_us = camera_time_us[i] + offset_us;
  144. int64_t clip_timestamp_us = timestamp_aligner.ClipTimestamp(
  145. translated_timestamp_us, system_time_us[i] + kClipBiasUs);
  146. if (translated_timestamp_us <= prev_timestamp_us) {
  147. did_clip = true;
  148. EXPECT_EQ(clip_timestamp_us,
  149. prev_timestamp_us + rtc::kNumMicrosecsPerMillisec);
  150. } else {
  151. // No change from clipping.
  152. EXPECT_EQ(clip_timestamp_us, translated_timestamp_us);
  153. }
  154. prev_timestamp_us = clip_timestamp_us;
  155. }
  156. EXPECT_TRUE(did_clip);
  157. }
  158. } // namespace rtc