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