blob: b26115f703dc6763273616addafebaf990252f99 [file] [log] [blame]
asaperssonfe50b4d2016-12-22 07:53:51 -08001/*
2 * Copyright (c) 2012 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "system_wrappers/include/rtp_to_ntp_estimator.h"
asaperssonfe50b4d2016-12-22 07:53:51 -080012
Yves Gerey988cc082018-10-23 12:03:01 +020013#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +010015#include <cmath>
16#include <vector>
Yves Gerey988cc082018-10-23 12:03:01 +020017
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +010018#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/checks.h"
20#include "rtc_base/logging.h"
asaperssonfe50b4d2016-12-22 07:53:51 -080021
22namespace webrtc {
23namespace {
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +010024// Maximum number of RTCP SR reports to use to map between RTP and NTP.
25const size_t kNumRtcpReportsToUse = 20;
Ilya Nikolaevskiyee45f902018-11-09 10:52:39 +010026// Don't allow NTP timestamps to jump more than 1 hour. Chosen arbitrary as big
27// enough to not affect normal use-cases. Yet it is smaller than RTP wrap-around
28// half-period (90khz RTP clock wrap-arounds every 13.25 hours). After half of
29// wrap-around period it is impossible to unwrap RTP timestamps correctly.
30const int kMaxAllowedRtcpNtpIntervalMs = 60 * 60 * 1000;
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +000031
asaperssonfe50b4d2016-12-22 07:53:51 -080032bool Contains(const std::list<RtpToNtpEstimator::RtcpMeasurement>& measurements,
33 const RtpToNtpEstimator::RtcpMeasurement& other) {
34 for (const auto& measurement : measurements) {
35 if (measurement.IsEqual(other))
36 return true;
37 }
38 return false;
39}
asaperssonfe50b4d2016-12-22 07:53:51 -080040
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +010041// Given x[] and y[] writes out such k and b that line y=k*x+b approximates
42// given points in the best way (Least Squares Method).
43bool LinearRegression(rtc::ArrayView<const double> x,
44 rtc::ArrayView<const double> y,
45 double* k,
46 double* b) {
47 size_t n = x.size();
48 if (n < 2)
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +000049 return false;
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +010050
51 if (y.size() != n)
52 return false;
53
54 double avg_x = 0;
55 double avg_y = 0;
56 for (size_t i = 0; i < n; ++i) {
57 avg_x += x[i];
58 avg_y += y[i];
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +000059 }
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +010060 avg_x /= n;
61 avg_y /= n;
62
63 double variance_x = 0;
64 double covariance_xy = 0;
65 for (size_t i = 0; i < n; ++i) {
66 double normalized_x = x[i] - avg_x;
67 double normalized_y = y[i] - avg_y;
68 variance_x += normalized_x * normalized_x;
69 covariance_xy += normalized_x * normalized_y;
70 }
71
72 if (std::fabs(variance_x) < 1e-8)
73 return false;
74
75 *k = static_cast<double>(covariance_xy / variance_x);
76 *b = static_cast<double>(avg_y - (*k) * avg_x);
77 return true;
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +000078}
79
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +010080} // namespace
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +000081
asaperssonfe50b4d2016-12-22 07:53:51 -080082RtpToNtpEstimator::RtcpMeasurement::RtcpMeasurement(uint32_t ntp_secs,
83 uint32_t ntp_frac,
Ilya Nikolaevskiy558cabf2017-11-14 10:32:15 +010084 int64_t unwrapped_timestamp)
85 : ntp_time(ntp_secs, ntp_frac),
86 unwrapped_rtp_timestamp(unwrapped_timestamp) {}
asaperssonfe50b4d2016-12-22 07:53:51 -080087
88bool RtpToNtpEstimator::RtcpMeasurement::IsEqual(
89 const RtcpMeasurement& other) const {
90 // Use || since two equal timestamps will result in zero frequency and in
91 // RtpToNtpMs, |rtp_timestamp_ms| is estimated by dividing by the frequency.
Ilya Nikolaevskiy558cabf2017-11-14 10:32:15 +010092 return (ntp_time == other.ntp_time) ||
93 (unwrapped_rtp_timestamp == other.unwrapped_rtp_timestamp);
asaperssonfe50b4d2016-12-22 07:53:51 -080094}
95
96// Class for converting an RTP timestamp to the NTP domain.
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +010097RtpToNtpEstimator::RtpToNtpEstimator() : consecutive_invalid_samples_(0) {}
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +000098
asaperssonfe50b4d2016-12-22 07:53:51 -080099RtpToNtpEstimator::~RtpToNtpEstimator() {}
100
101void RtpToNtpEstimator::UpdateParameters() {
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +0100102 if (measurements_.size() < 2)
asaperssonfe50b4d2016-12-22 07:53:51 -0800103 return;
104
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +0100105 std::vector<double> x;
106 std::vector<double> y;
107 x.reserve(measurements_.size());
108 y.reserve(measurements_.size());
109 for (auto it = measurements_.begin(); it != measurements_.end(); ++it) {
110 x.push_back(it->unwrapped_rtp_timestamp);
111 y.push_back(it->ntp_time.ToMs());
112 }
113 double slope, offset;
asaperssonfe50b4d2016-12-22 07:53:51 -0800114
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +0100115 if (!LinearRegression(x, y, &slope, &offset)) {
asaperssonfe50b4d2016-12-22 07:53:51 -0800116 return;
117 }
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +0100118
119 params_.emplace(1 / slope, offset);
asaperssonfe50b4d2016-12-22 07:53:51 -0800120}
121
122bool RtpToNtpEstimator::UpdateMeasurements(uint32_t ntp_secs,
123 uint32_t ntp_frac,
124 uint32_t rtp_timestamp,
125 bool* new_rtcp_sr) {
126 *new_rtcp_sr = false;
127
Ilya Nikolaevskiy558cabf2017-11-14 10:32:15 +0100128 int64_t unwrapped_rtp_timestamp = unwrapper_.Unwrap(rtp_timestamp);
129
130 RtcpMeasurement new_measurement(ntp_secs, ntp_frac, unwrapped_rtp_timestamp);
131
stefan1009cfc2017-06-30 06:28:13 -0700132 if (Contains(measurements_, new_measurement)) {
asaperssonfe50b4d2016-12-22 07:53:51 -0800133 // RTCP SR report already added.
134 return true;
135 }
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +0000136
stefan1009cfc2017-06-30 06:28:13 -0700137 if (!new_measurement.ntp_time.Valid())
asaperssonfe50b4d2016-12-22 07:53:51 -0800138 return false;
stefan1009cfc2017-06-30 06:28:13 -0700139
140 int64_t ntp_ms_new = new_measurement.ntp_time.ToMs();
141 bool invalid_sample = false;
Ilya Nikolaevskiy558cabf2017-11-14 10:32:15 +0100142 if (!measurements_.empty()) {
143 int64_t old_rtp_timestamp = measurements_.front().unwrapped_rtp_timestamp;
144 int64_t old_ntp_ms = measurements_.front().ntp_time.ToMs();
Ilya Nikolaevskiyee45f902018-11-09 10:52:39 +0100145 if (ntp_ms_new <= old_ntp_ms ||
146 ntp_ms_new > old_ntp_ms + kMaxAllowedRtcpNtpIntervalMs) {
stefan1009cfc2017-06-30 06:28:13 -0700147 invalid_sample = true;
Ilya Nikolaevskiy558cabf2017-11-14 10:32:15 +0100148 } else if (unwrapped_rtp_timestamp <= old_rtp_timestamp) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100149 RTC_LOG(LS_WARNING)
stefan1009cfc2017-06-30 06:28:13 -0700150 << "Newer RTCP SR report with older RTP timestamp, dropping";
151 invalid_sample = true;
Ilya Nikolaevskiy558cabf2017-11-14 10:32:15 +0100152 } else if (unwrapped_rtp_timestamp - old_rtp_timestamp > (1 << 25)) {
153 // Sanity check. No jumps too far into the future in rtp.
154 invalid_sample = true;
stefan1009cfc2017-06-30 06:28:13 -0700155 }
asaperssonfe50b4d2016-12-22 07:53:51 -0800156 }
Ilya Nikolaevskiy558cabf2017-11-14 10:32:15 +0100157
stefan1009cfc2017-06-30 06:28:13 -0700158 if (invalid_sample) {
159 ++consecutive_invalid_samples_;
160 if (consecutive_invalid_samples_ < kMaxInvalidSamples) {
161 return false;
162 }
Mirko Bonadei675513b2017-11-09 11:09:25 +0100163 RTC_LOG(LS_WARNING) << "Multiple consecutively invalid RTCP SR reports, "
164 "clearing measurements.";
stefan1009cfc2017-06-30 06:28:13 -0700165 measurements_.clear();
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +0100166 params_ = absl::nullopt;
stefan1009cfc2017-06-30 06:28:13 -0700167 }
168 consecutive_invalid_samples_ = 0;
asaperssonfe50b4d2016-12-22 07:53:51 -0800169
170 // Insert new RTCP SR report.
171 if (measurements_.size() == kNumRtcpReportsToUse)
172 measurements_.pop_back();
173
stefan1009cfc2017-06-30 06:28:13 -0700174 measurements_.push_front(new_measurement);
asaperssonfe50b4d2016-12-22 07:53:51 -0800175 *new_rtcp_sr = true;
176
177 // List updated, calculate new parameters.
178 UpdateParameters();
179 return true;
180}
181
182bool RtpToNtpEstimator::Estimate(int64_t rtp_timestamp,
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +0100183 int64_t* ntp_timestamp_ms) const {
184 if (!params_)
asaperssonfe50b4d2016-12-22 07:53:51 -0800185 return false;
186
Ilya Nikolaevskiy558cabf2017-11-14 10:32:15 +0100187 int64_t rtp_timestamp_unwrapped = unwrapper_.Unwrap(rtp_timestamp);
asaperssonfe50b4d2016-12-22 07:53:51 -0800188
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +0000189 // params_calculated_ should not be true unless ms params.frequency_khz has
Ilya Nikolaevskiy558cabf2017-11-14 10:32:15 +0100190 // been calculated to something non zero.
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +0100191 RTC_DCHECK_NE(params_->frequency_khz, 0.0);
asaperssonfe50b4d2016-12-22 07:53:51 -0800192 double rtp_ms =
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +0100193 static_cast<double>(rtp_timestamp_unwrapped) / params_->frequency_khz +
194 params_->offset_ms + 0.5f;
asaperssonfe50b4d2016-12-22 07:53:51 -0800195
196 if (rtp_ms < 0)
197 return false;
198
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +0100199 *ntp_timestamp_ms = rtp_ms;
200
asaperssonfe50b4d2016-12-22 07:53:51 -0800201 return true;
202}
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +0000203
Danil Chapovalov196100e2018-06-21 10:17:24 +0200204const absl::optional<RtpToNtpEstimator::Parameters> RtpToNtpEstimator::params()
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +0000205 const {
Ilya Nikolaevskiyf1cc3a22018-11-12 15:03:51 +0100206 return params_;
Ilya Nikolaevskiy8b64fd82017-11-15 16:48:04 +0000207}
asaperssonfe50b4d2016-12-22 07:53:51 -0800208} // namespace webrtc