blob: 422759939a2a00c7ef396ad8ac34f56e2d340532 [file] [log] [blame]
asapersson31fd7852016-12-22 15:53:511/*
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
11#include "webrtc/system_wrappers/include/rtp_to_ntp_estimator.h"
12
henrik.lundinb7cd5f92017-09-04 16:02:1513#include "webrtc/rtc_base/checks.h"
Edward Lemur76de83e2017-07-06 17:44:3414#include "webrtc/rtc_base/logging.h"
asapersson31fd7852016-12-22 15:53:5115#include "webrtc/system_wrappers/include/clock.h"
16
17namespace webrtc {
18namespace {
19// Number of RTCP SR reports to use to map between RTP and NTP.
20const size_t kNumRtcpReportsToUse = 2;
21
22// Calculates the RTP timestamp frequency from two pairs of NTP/RTP timestamps.
23bool CalculateFrequency(int64_t ntp_ms1,
24 uint32_t rtp_timestamp1,
25 int64_t ntp_ms2,
26 uint32_t rtp_timestamp2,
27 double* frequency_khz) {
28 if (ntp_ms1 <= ntp_ms2)
29 return false;
30
31 *frequency_khz = static_cast<double>(rtp_timestamp1 - rtp_timestamp2) /
32 static_cast<double>(ntp_ms1 - ntp_ms2);
33 return true;
34}
35
36// Detects if there has been a wraparound between |old_timestamp| and
37// |new_timestamp|, and compensates by adding 2^32 if that is the case.
38bool CompensateForWrapAround(uint32_t new_timestamp,
39 uint32_t old_timestamp,
40 int64_t* compensated_timestamp) {
41 int64_t wraps = CheckForWrapArounds(new_timestamp, old_timestamp);
42 if (wraps < 0) {
43 // Reordering, don't use this packet.
44 return false;
45 }
46 *compensated_timestamp = new_timestamp + (wraps << 32);
47 return true;
48}
49
50bool Contains(const std::list<RtpToNtpEstimator::RtcpMeasurement>& measurements,
51 const RtpToNtpEstimator::RtcpMeasurement& other) {
52 for (const auto& measurement : measurements) {
53 if (measurement.IsEqual(other))
54 return true;
55 }
56 return false;
57}
asapersson31fd7852016-12-22 15:53:5158} // namespace
59
60RtpToNtpEstimator::RtcpMeasurement::RtcpMeasurement(uint32_t ntp_secs,
61 uint32_t ntp_frac,
62 uint32_t timestamp)
63 : ntp_time(ntp_secs, ntp_frac), rtp_timestamp(timestamp) {}
64
65bool RtpToNtpEstimator::RtcpMeasurement::IsEqual(
66 const RtcpMeasurement& other) const {
67 // Use || since two equal timestamps will result in zero frequency and in
68 // RtpToNtpMs, |rtp_timestamp_ms| is estimated by dividing by the frequency.
69 return (ntp_time == other.ntp_time) || (rtp_timestamp == other.rtp_timestamp);
70}
71
72// Class for converting an RTP timestamp to the NTP domain.
stefan303eb7b2017-06-30 13:28:1373RtpToNtpEstimator::RtpToNtpEstimator() : consecutive_invalid_samples_(0) {}
asapersson31fd7852016-12-22 15:53:5174RtpToNtpEstimator::~RtpToNtpEstimator() {}
75
76void RtpToNtpEstimator::UpdateParameters() {
77 if (measurements_.size() != kNumRtcpReportsToUse)
78 return;
79
80 int64_t timestamp_new = measurements_.front().rtp_timestamp;
81 int64_t timestamp_old = measurements_.back().rtp_timestamp;
82 if (!CompensateForWrapAround(timestamp_new, timestamp_old, &timestamp_new))
83 return;
84
85 int64_t ntp_ms_new = measurements_.front().ntp_time.ToMs();
86 int64_t ntp_ms_old = measurements_.back().ntp_time.ToMs();
87
88 if (!CalculateFrequency(ntp_ms_new, timestamp_new, ntp_ms_old, timestamp_old,
89 &params_.frequency_khz)) {
90 return;
91 }
92 params_.offset_ms = timestamp_new - params_.frequency_khz * ntp_ms_new;
93 params_.calculated = true;
94}
95
96bool RtpToNtpEstimator::UpdateMeasurements(uint32_t ntp_secs,
97 uint32_t ntp_frac,
98 uint32_t rtp_timestamp,
99 bool* new_rtcp_sr) {
100 *new_rtcp_sr = false;
101
stefan303eb7b2017-06-30 13:28:13102 RtcpMeasurement new_measurement(ntp_secs, ntp_frac, rtp_timestamp);
103 if (Contains(measurements_, new_measurement)) {
asapersson31fd7852016-12-22 15:53:51104 // RTCP SR report already added.
105 return true;
106 }
stefan303eb7b2017-06-30 13:28:13107 if (!new_measurement.ntp_time.Valid())
asapersson31fd7852016-12-22 15:53:51108 return false;
stefan303eb7b2017-06-30 13:28:13109
110 int64_t ntp_ms_new = new_measurement.ntp_time.ToMs();
111 bool invalid_sample = false;
112 for (const auto& measurement : measurements_) {
113 if (ntp_ms_new <= measurement.ntp_time.ToMs()) {
114 // Old report.
115 invalid_sample = true;
116 break;
117 }
118 int64_t timestamp_new = new_measurement.rtp_timestamp;
119 if (!CompensateForWrapAround(timestamp_new, measurement.rtp_timestamp,
120 &timestamp_new)) {
121 invalid_sample = true;
122 break;
123 }
124 if (timestamp_new <= measurement.rtp_timestamp) {
125 LOG(LS_WARNING)
126 << "Newer RTCP SR report with older RTP timestamp, dropping";
127 invalid_sample = true;
128 break;
129 }
asapersson31fd7852016-12-22 15:53:51130 }
stefan303eb7b2017-06-30 13:28:13131 if (invalid_sample) {
132 ++consecutive_invalid_samples_;
133 if (consecutive_invalid_samples_ < kMaxInvalidSamples) {
134 return false;
135 }
136 LOG(LS_WARNING) << "Multiple consecutively invalid RTCP SR reports, "
137 "clearing measurements.";
138 measurements_.clear();
139 }
140 consecutive_invalid_samples_ = 0;
asapersson31fd7852016-12-22 15:53:51141
142 // Insert new RTCP SR report.
143 if (measurements_.size() == kNumRtcpReportsToUse)
144 measurements_.pop_back();
145
stefan303eb7b2017-06-30 13:28:13146 measurements_.push_front(new_measurement);
asapersson31fd7852016-12-22 15:53:51147 *new_rtcp_sr = true;
148
149 // List updated, calculate new parameters.
150 UpdateParameters();
151 return true;
152}
153
154bool RtpToNtpEstimator::Estimate(int64_t rtp_timestamp,
155 int64_t* rtp_timestamp_ms) const {
156 if (!params_.calculated || measurements_.empty())
157 return false;
158
159 uint32_t rtp_timestamp_old = measurements_.back().rtp_timestamp;
160 int64_t rtp_timestamp_unwrapped;
161 if (!CompensateForWrapAround(rtp_timestamp, rtp_timestamp_old,
162 &rtp_timestamp_unwrapped)) {
163 return false;
164 }
165
henrik.lundinb7cd5f92017-09-04 16:02:15166 // params_.calculated should not be true unless params_.frequency_khz has been
167 // set to something non-zero.
168 RTC_DCHECK_NE(params_.frequency_khz, 0.0);
asapersson31fd7852016-12-22 15:53:51169 double rtp_ms =
170 (static_cast<double>(rtp_timestamp_unwrapped) - params_.offset_ms) /
171 params_.frequency_khz +
172 0.5f;
173
174 if (rtp_ms < 0)
175 return false;
176
177 *rtp_timestamp_ms = rtp_ms;
178 return true;
179}
180
181int CheckForWrapArounds(uint32_t new_timestamp, uint32_t old_timestamp) {
182 if (new_timestamp < old_timestamp) {
183 // This difference should be less than -2^31 if we have had a wrap around
184 // (e.g. |new_timestamp| = 1, |rtcp_rtp_timestamp| = 2^32 - 1). Since it is
185 // cast to a int32_t, it should be positive.
186 if (static_cast<int32_t>(new_timestamp - old_timestamp) > 0) {
187 // Forward wrap around.
188 return 1;
189 }
190 } else if (static_cast<int32_t>(old_timestamp - new_timestamp) > 0) {
191 // This difference should be less than -2^31 if we have had a backward wrap
192 // around. Since it is cast to a int32_t, it should be positive.
193 return -1;
194 }
195 return 0;
196}
197
198} // namespace webrtc