OLD | NEW |
| (Empty) |
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 | |
11 #include <math.h> | |
12 | |
13 #include <algorithm> | |
14 #include <limits> | |
15 | |
16 #include "webrtc/base/gunit.h" | |
17 #include "webrtc/base/random.h" | |
18 #include "webrtc/base/timestampaligner.h" | |
19 | |
20 namespace rtc { | |
21 | |
22 namespace { | |
23 // Computes the difference x_k - mean(x), when x_k is the linear sequence x_k = | |
24 // k, and the "mean" is plain mean for the first |window_size| samples, followed | |
25 // by exponential averaging with weight 1 / |window_size| for each new sample. | |
26 // This is needed to predict the effect of camera clock drift on the timestamp | |
27 // translation. See the comment on TimestampAligner::UpdateOffset for more | |
28 // context. | |
29 double MeanTimeDifference(int nsamples, int window_size) { | |
30 if (nsamples <= window_size) { | |
31 // Plain averaging. | |
32 return nsamples / 2.0; | |
33 } else { | |
34 // Exponential convergence towards | |
35 // interval_error * (window_size - 1) | |
36 double alpha = 1.0 - 1.0 / window_size; | |
37 | |
38 return ((window_size - 1) - | |
39 (window_size / 2.0 - 1) * pow(alpha, nsamples - window_size)); | |
40 } | |
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 | |
117 } // Anonymous namespace | |
118 | |
119 TEST(TimestampAlignerTest, AttenuateTimestampJitterNoDrift) { | |
120 TestTimestampFilter(0.0); | |
121 } | |
122 | |
123 // 100 ppm is a worst case for a reasonable crystal. | |
124 TEST(TimestampAlignerTest, AttenuateTimestampJitterSmallPosDrift) { | |
125 TestTimestampFilter(0.0001); | |
126 } | |
127 | |
128 TEST(TimestampAlignerTest, AttenuateTimestampJitterSmallNegDrift) { | |
129 TestTimestampFilter(-0.0001); | |
130 } | |
131 | |
132 // 3000 ppm, 3 ms / s, is the worst observed drift, see | |
133 // https://bugs.chromium.org/p/webrtc/issues/detail?id=5456 | |
134 TEST(TimestampAlignerTest, AttenuateTimestampJitterLargePosDrift) { | |
135 TestTimestampFilter(0.003); | |
136 } | |
137 | |
138 TEST(TimestampAlignerTest, AttenuateTimestampJitterLargeNegDrift) { | |
139 TestTimestampFilter(-0.003); | |
140 } | |
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 | |
187 } // namespace rtc | |
OLD | NEW |