Chromium Code Reviews| 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 "webrtc/base/gunit.h" | |
| 12 #include "webrtc/base/random.h" | |
| 13 #include "webrtc/base/timestampaligner.h" | |
| 14 | |
| 15 namespace rtc { | |
| 16 | |
| 17 namespace { | |
| 18 // Computes the difference x_k - mean(x), when x_k is the linear sequence x_k = | |
| 19 // k, and the "mean" is plain mean for the first |window_size| samples, followed | |
| 20 // by exponential averaging with weight 1/|window_size| for each new sample. | |
|
pbos-webrtc
2016/06/21 14:49:50
" / "
nisse-webrtc
2016/06/22 07:11:15
Done. Also updated the invalid "VideoCapturer" ref
| |
| 21 // This is needed to predict the effect of camera clock drift on the timestamp | |
| 22 // translation. See the comment on VideoCapturer::UpdateOffset for more context. | |
| 23 double MeanTimeDifference(int nsamples, int window_size) { | |
| 24 if (nsamples <= window_size) { | |
| 25 // Plain averaging. | |
| 26 return nsamples / 2.0; | |
| 27 } else { | |
| 28 // Exponential convergence towards | |
| 29 // interval_error * (window_size - 1) | |
| 30 double alpha = 1.0 - 1.0 / window_size; | |
| 31 | |
| 32 return ((window_size - 1) - | |
| 33 (window_size / 2.0 - 1) * pow(alpha, nsamples - window_size)); | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 } // Anonymous namespace | |
| 38 | |
| 39 class TimestampAlignerTest : public testing::Test { | |
| 40 protected: | |
| 41 void TestTimestampFilter(double rel_freq_error) { | |
| 42 const int64_t kEpoch = 10000; | |
| 43 const int64_t kJitterUs = 5000; | |
| 44 const int64_t kIntervalUs = 33333; // 30 FPS | |
| 45 const int kWindowSize = 100; | |
| 46 const int kNumFrames = 3 * kWindowSize; | |
| 47 | |
| 48 int64_t interval_error_us = kIntervalUs * rel_freq_error; | |
| 49 int64_t system_start_us = rtc::TimeMicros(); | |
| 50 webrtc::Random random(17); | |
| 51 | |
| 52 int64_t prev_translated_time_us = system_start_us; | |
| 53 | |
| 54 for (int i = 0; i < kNumFrames; i++) { | |
| 55 // Camera time subject to drift. | |
| 56 int64_t camera_time_us = kEpoch + i * (kIntervalUs + interval_error_us); | |
| 57 int64_t system_time_us = system_start_us + i * kIntervalUs; | |
| 58 // And system time readings are subject to jitter. | |
| 59 int64_t system_measured_us = system_time_us + random.Rand(kJitterUs); | |
| 60 | |
| 61 int64_t offset_us = | |
| 62 timestamp_aligner_.UpdateOffset(camera_time_us, system_measured_us); | |
| 63 | |
| 64 int64_t filtered_time_us = camera_time_us + offset_us; | |
| 65 int64_t translated_time_us = timestamp_aligner_.ClipTimestamp( | |
| 66 filtered_time_us, system_measured_us); | |
| 67 | |
| 68 EXPECT_LE(translated_time_us, system_measured_us); | |
| 69 EXPECT_GE(translated_time_us, prev_translated_time_us); | |
| 70 | |
| 71 // The relative frequency error contributes to the expected error | |
| 72 // by a factor which is the difference between the current time | |
| 73 // and the average of earlier sample times. | |
| 74 int64_t expected_error_us = | |
| 75 kJitterUs / 2 + | |
| 76 rel_freq_error * kIntervalUs * MeanTimeDifference(i, kWindowSize); | |
| 77 | |
| 78 int64_t bias_us = filtered_time_us - translated_time_us; | |
| 79 EXPECT_GE(bias_us, 0); | |
| 80 | |
| 81 if (i == 0) { | |
| 82 EXPECT_EQ(translated_time_us, system_measured_us); | |
| 83 } else { | |
| 84 EXPECT_NEAR(filtered_time_us, system_time_us + expected_error_us, | |
| 85 2.0 * kJitterUs / sqrt(std::max(i, kWindowSize))); | |
| 86 } | |
| 87 // If the camera clock runs too fast (rel_freq_error > 0.0), The | |
| 88 // bias is expected to roughly cancel the expected error from the | |
| 89 // clock drift, as this grows. Otherwise, it reflects the | |
| 90 // measurement noise. The tolerances here were selected after some | |
| 91 // trial and error. | |
| 92 if (i < 10 || rel_freq_error <= 0.0) { | |
| 93 EXPECT_LE(bias_us, 3000); | |
| 94 } else { | |
| 95 EXPECT_NEAR(bias_us, expected_error_us, 1500); | |
| 96 } | |
| 97 prev_translated_time_us = translated_time_us; | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 private: | |
| 102 TimestampAligner timestamp_aligner_; | |
| 103 }; | |
| 104 | |
| 105 TEST_F(TimestampAlignerTest, AttenuateTimestampJitterNoDrift) { | |
| 106 TestTimestampFilter(0.0); | |
| 107 } | |
| 108 | |
| 109 // 100 ppm is a worst case for a reasonable crystal. | |
| 110 TEST_F(TimestampAlignerTest, AttenuateTimestampJitterSmallPosDrift) { | |
| 111 TestTimestampFilter(0.0001); | |
| 112 } | |
| 113 | |
| 114 TEST_F(TimestampAlignerTest, AttenuateTimestampJitterSmallNegDrift) { | |
| 115 TestTimestampFilter(-0.0001); | |
| 116 } | |
| 117 | |
| 118 // 3000 ppm, 3 ms / s, is the worst observed drift, see | |
| 119 // https://bugs.chromium.org/p/webrtc/issues/detail?id=5456 | |
| 120 TEST_F(TimestampAlignerTest, AttenuateTimestampJitterLargePosDrift) { | |
| 121 TestTimestampFilter(0.003); | |
| 122 } | |
| 123 | |
| 124 TEST_F(TimestampAlignerTest, AttenuateTimestampJitterLargeNegDrift) { | |
| 125 TestTimestampFilter(-0.003); | |
| 126 } | |
| 127 | |
| 128 } // namespce rtc | |
| OLD | NEW |