2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/system_wrappers/interface/rtp_to_ntp.h"
13 #include "webrtc/system_wrappers/interface/clock.h"
19 RtcpMeasurement::RtcpMeasurement()
20 : ntp_secs(0), ntp_frac(0), rtp_timestamp(0) {}
22 RtcpMeasurement::RtcpMeasurement(uint32_t ntp_secs, uint32_t ntp_frac,
24 : ntp_secs(ntp_secs), ntp_frac(ntp_frac), rtp_timestamp(timestamp) {}
26 // Calculates the RTP timestamp frequency from two pairs of NTP and RTP
28 bool CalculateFrequency(
30 uint32_t rtp_timestamp1,
32 uint32_t rtp_timestamp2,
33 double* frequency_khz) {
34 if (rtcp_ntp_ms1 <= rtcp_ntp_ms2) {
37 *frequency_khz = static_cast<double>(rtp_timestamp1 - rtp_timestamp2) /
38 static_cast<double>(rtcp_ntp_ms1 - rtcp_ntp_ms2);
42 // Detects if there has been a wraparound between |old_timestamp| and
43 // |new_timestamp|, and compensates by adding 2^32 if that is the case.
44 bool CompensateForWrapAround(uint32_t new_timestamp,
45 uint32_t old_timestamp,
46 int64_t* compensated_timestamp) {
47 assert(compensated_timestamp);
48 int64_t wraps = CheckForWrapArounds(new_timestamp, old_timestamp);
50 // Reordering, don't use this packet.
53 *compensated_timestamp = new_timestamp + (wraps << 32);
57 bool UpdateRtcpList(uint32_t ntp_secs,
59 uint32_t rtp_timestamp,
63 if (ntp_secs == 0 && ntp_frac == 0) {
67 RtcpMeasurement measurement;
68 measurement.ntp_secs = ntp_secs;
69 measurement.ntp_frac = ntp_frac;
70 measurement.rtp_timestamp = rtp_timestamp;
72 for (RtcpList::iterator it = rtcp_list->begin();
73 it != rtcp_list->end(); ++it) {
74 if (measurement.ntp_secs == (*it).ntp_secs &&
75 measurement.ntp_frac == (*it).ntp_frac) {
76 // This RTCP has already been added to the list.
81 // We need two RTCP SR reports to map between RTP and NTP. More than two will
82 // not improve the mapping.
83 if (rtcp_list->size() == 2) {
84 rtcp_list->pop_back();
86 rtcp_list->push_front(measurement);
91 // Converts |rtp_timestamp| to the NTP time base using the NTP and RTP timestamp
92 // pairs in |rtcp|. The converted timestamp is returned in
93 // |rtp_timestamp_in_ms|. This function compensates for wrap arounds in RTP
94 // timestamps and returns false if it can't do the conversion due to reordering.
95 bool RtpToNtpMs(int64_t rtp_timestamp,
97 int64_t* rtp_timestamp_in_ms) {
98 assert(rtcp.size() == 2);
99 int64_t rtcp_ntp_ms_new = Clock::NtpToMs(rtcp.front().ntp_secs,
100 rtcp.front().ntp_frac);
101 int64_t rtcp_ntp_ms_old = Clock::NtpToMs(rtcp.back().ntp_secs,
102 rtcp.back().ntp_frac);
103 int64_t rtcp_timestamp_new = rtcp.front().rtp_timestamp;
104 int64_t rtcp_timestamp_old = rtcp.back().rtp_timestamp;
105 if (!CompensateForWrapAround(rtcp_timestamp_new,
107 &rtcp_timestamp_new)) {
111 if (!CalculateFrequency(rtcp_ntp_ms_new,
118 double offset = rtcp_timestamp_new - freq_khz * rtcp_ntp_ms_new;
119 int64_t rtp_timestamp_unwrapped;
120 if (!CompensateForWrapAround(rtp_timestamp, rtcp_timestamp_old,
121 &rtp_timestamp_unwrapped)) {
124 double rtp_timestamp_ntp_ms = (static_cast<double>(rtp_timestamp_unwrapped) -
125 offset) / freq_khz + 0.5f;
126 if (rtp_timestamp_ntp_ms < 0) {
129 *rtp_timestamp_in_ms = rtp_timestamp_ntp_ms;
133 int CheckForWrapArounds(uint32_t new_timestamp, uint32_t old_timestamp) {
134 if (new_timestamp < old_timestamp) {
135 // This difference should be less than -2^31 if we have had a wrap around
136 // (e.g. |new_timestamp| = 1, |rtcp_rtp_timestamp| = 2^32 - 1). Since it is
137 // cast to a int32_t, it should be positive.
138 if (static_cast<int32_t>(new_timestamp - old_timestamp) > 0) {
139 // Forward wrap around.
142 } else if (static_cast<int32_t>(old_timestamp - new_timestamp) > 0) {
143 // This difference should be less than -2^31 if we have had a backward wrap
144 // around. Since it is cast to a int32_t, it should be positive.
150 } // namespace webrtc