asapersson | 31fd785 | 2016-12-22 15:53:51 | [diff] [blame] | 1 | /* |
| 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.lundin | b7cd5f9 | 2017-09-04 16:02:15 | [diff] [blame] | 13 | #include "webrtc/rtc_base/checks.h" |
Edward Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 14 | #include "webrtc/rtc_base/logging.h" |
asapersson | 31fd785 | 2016-12-22 15:53:51 | [diff] [blame] | 15 | #include "webrtc/system_wrappers/include/clock.h" |
| 16 | |
| 17 | namespace webrtc { |
| 18 | namespace { |
| 19 | // Number of RTCP SR reports to use to map between RTP and NTP. |
| 20 | const size_t kNumRtcpReportsToUse = 2; |
| 21 | |
| 22 | // Calculates the RTP timestamp frequency from two pairs of NTP/RTP timestamps. |
| 23 | bool 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. |
| 38 | bool 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 | |
| 50 | bool 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 | } |
asapersson | 31fd785 | 2016-12-22 15:53:51 | [diff] [blame] | 58 | } // namespace |
| 59 | |
| 60 | RtpToNtpEstimator::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 | |
| 65 | bool 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. |
stefan | 303eb7b | 2017-06-30 13:28:13 | [diff] [blame] | 73 | RtpToNtpEstimator::RtpToNtpEstimator() : consecutive_invalid_samples_(0) {} |
asapersson | 31fd785 | 2016-12-22 15:53:51 | [diff] [blame] | 74 | RtpToNtpEstimator::~RtpToNtpEstimator() {} |
| 75 | |
| 76 | void 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, ×tamp_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 | ¶ms_.frequency_khz)) { |
| 90 | return; |
| 91 | } |
| 92 | params_.offset_ms = timestamp_new - params_.frequency_khz * ntp_ms_new; |
| 93 | params_.calculated = true; |
| 94 | } |
| 95 | |
| 96 | bool 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 | |
stefan | 303eb7b | 2017-06-30 13:28:13 | [diff] [blame] | 102 | RtcpMeasurement new_measurement(ntp_secs, ntp_frac, rtp_timestamp); |
| 103 | if (Contains(measurements_, new_measurement)) { |
asapersson | 31fd785 | 2016-12-22 15:53:51 | [diff] [blame] | 104 | // RTCP SR report already added. |
| 105 | return true; |
| 106 | } |
stefan | 303eb7b | 2017-06-30 13:28:13 | [diff] [blame] | 107 | if (!new_measurement.ntp_time.Valid()) |
asapersson | 31fd785 | 2016-12-22 15:53:51 | [diff] [blame] | 108 | return false; |
stefan | 303eb7b | 2017-06-30 13:28:13 | [diff] [blame] | 109 | |
| 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 | ×tamp_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 | } |
asapersson | 31fd785 | 2016-12-22 15:53:51 | [diff] [blame] | 130 | } |
stefan | 303eb7b | 2017-06-30 13:28:13 | [diff] [blame] | 131 | 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; |
asapersson | 31fd785 | 2016-12-22 15:53:51 | [diff] [blame] | 141 | |
| 142 | // Insert new RTCP SR report. |
| 143 | if (measurements_.size() == kNumRtcpReportsToUse) |
| 144 | measurements_.pop_back(); |
| 145 | |
stefan | 303eb7b | 2017-06-30 13:28:13 | [diff] [blame] | 146 | measurements_.push_front(new_measurement); |
asapersson | 31fd785 | 2016-12-22 15:53:51 | [diff] [blame] | 147 | *new_rtcp_sr = true; |
| 148 | |
| 149 | // List updated, calculate new parameters. |
| 150 | UpdateParameters(); |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | bool 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.lundin | b7cd5f9 | 2017-09-04 16:02:15 | [diff] [blame] | 166 | // 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); |
asapersson | 31fd785 | 2016-12-22 15:53:51 | [diff] [blame] | 169 | 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 | |
| 181 | int 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 |