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 |
| 15 #include "webrtc/base/gunit.h" |
| 16 #include "webrtc/base/random.h" |
| 17 #include "webrtc/base/timestampaligner.h" |
| 18 |
| 19 namespace rtc { |
| 20 |
| 21 namespace { |
| 22 // 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 // 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 // translation. See the comment on TimestampAligner::UpdateOffset for more |
| 27 // context. |
| 28 double MeanTimeDifference(int nsamples, int window_size) { |
| 29 if (nsamples <= window_size) { |
| 30 // Plain averaging. |
| 31 return nsamples / 2.0; |
| 32 } else { |
| 33 // Exponential convergence towards |
| 34 // interval_error * (window_size - 1) |
| 35 double alpha = 1.0 - 1.0 / window_size; |
| 36 |
| 37 return ((window_size - 1) - |
| 38 (window_size / 2.0 - 1) * pow(alpha, nsamples - window_size)); |
| 39 } |
| 40 } |
| 41 |
| 42 } // Anonymous namespace |
| 43 |
| 44 class TimestampAlignerTest : public testing::Test { |
| 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); |
| 112 } |
| 113 |
| 114 // 100 ppm is a worst case for a reasonable crystal. |
| 115 TEST_F(TimestampAlignerTest, AttenuateTimestampJitterSmallPosDrift) { |
| 116 TestTimestampFilter(0.0001); |
| 117 } |
| 118 |
| 119 TEST_F(TimestampAlignerTest, AttenuateTimestampJitterSmallNegDrift) { |
| 120 TestTimestampFilter(-0.0001); |
| 121 } |
| 122 |
| 123 // 3000 ppm, 3 ms / s, is the worst observed drift, see |
| 124 // https://bugs.chromium.org/p/webrtc/issues/detail?id=5456 |
| 125 TEST_F(TimestampAlignerTest, AttenuateTimestampJitterLargePosDrift) { |
| 126 TestTimestampFilter(0.003); |
| 127 } |
| 128 |
| 129 TEST_F(TimestampAlignerTest, AttenuateTimestampJitterLargeNegDrift) { |
| 130 TestTimestampFilter(-0.003); |
| 131 } |
| 132 |
| 133 } // namespace rtc |
OLD | NEW |