blob: 6504737fd54bb32d84125bba2f8e4159a7005006 [file] [log] [blame]
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +00001/*
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
Henrik Kjellander98f53512015-10-28 18:17:40 +010011#include "webrtc/system_wrappers/include/rtp_to_ntp.h"
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +000012
Henrik Kjellander98f53512015-10-28 18:17:40 +010013#include "webrtc/system_wrappers/include/clock.h"
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +000014
15#include <assert.h>
16
17namespace webrtc {
18
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +000019RtcpMeasurement::RtcpMeasurement()
20 : ntp_secs(0), ntp_frac(0), rtp_timestamp(0) {}
21
22RtcpMeasurement::RtcpMeasurement(uint32_t ntp_secs, uint32_t ntp_frac,
23 uint32_t timestamp)
24 : ntp_secs(ntp_secs), ntp_frac(ntp_frac), rtp_timestamp(timestamp) {}
25
26// Calculates the RTP timestamp frequency from two pairs of NTP and RTP
27// timestamps.
28bool CalculateFrequency(
29 int64_t rtcp_ntp_ms1,
30 uint32_t rtp_timestamp1,
31 int64_t rtcp_ntp_ms2,
32 uint32_t rtp_timestamp2,
33 double* frequency_khz) {
34 if (rtcp_ntp_ms1 <= rtcp_ntp_ms2) {
35 return false;
36 }
37 *frequency_khz = static_cast<double>(rtp_timestamp1 - rtp_timestamp2) /
38 static_cast<double>(rtcp_ntp_ms1 - rtcp_ntp_ms2);
39 return true;
40}
41
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.
44bool CompensateForWrapAround(uint32_t new_timestamp,
45 uint32_t old_timestamp,
46 int64_t* compensated_timestamp) {
47 assert(compensated_timestamp);
wu@webrtc.org66773a02014-05-07 17:09:44 +000048 int64_t wraps = CheckForWrapArounds(new_timestamp, old_timestamp);
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +000049 if (wraps < 0) {
50 // Reordering, don't use this packet.
51 return false;
52 }
53 *compensated_timestamp = new_timestamp + (wraps << 32);
54 return true;
55}
56
wu@webrtc.orgcd701192014-04-24 22:10:24 +000057bool UpdateRtcpList(uint32_t ntp_secs,
58 uint32_t ntp_frac,
59 uint32_t rtp_timestamp,
60 RtcpList* rtcp_list,
61 bool* new_rtcp_sr) {
62 *new_rtcp_sr = false;
63 if (ntp_secs == 0 && ntp_frac == 0) {
64 return false;
65 }
66
67 RtcpMeasurement measurement;
68 measurement.ntp_secs = ntp_secs;
69 measurement.ntp_frac = ntp_frac;
70 measurement.rtp_timestamp = rtp_timestamp;
71
72 for (RtcpList::iterator it = rtcp_list->begin();
73 it != rtcp_list->end(); ++it) {
asaperssonfdca6692016-04-19 07:04:47 -070074 if ((measurement.ntp_secs == (*it).ntp_secs &&
75 measurement.ntp_frac == (*it).ntp_frac) ||
76 (measurement.rtp_timestamp == (*it).rtp_timestamp)) {
wu@webrtc.orgcd701192014-04-24 22:10:24 +000077 // This RTCP has already been added to the list.
78 return true;
79 }
80 }
81
82 // We need two RTCP SR reports to map between RTP and NTP. More than two will
83 // not improve the mapping.
84 if (rtcp_list->size() == 2) {
85 rtcp_list->pop_back();
86 }
87 rtcp_list->push_front(measurement);
88 *new_rtcp_sr = true;
89 return true;
90}
91
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +000092// Converts |rtp_timestamp| to the NTP time base using the NTP and RTP timestamp
93// pairs in |rtcp|. The converted timestamp is returned in
94// |rtp_timestamp_in_ms|. This function compensates for wrap arounds in RTP
95// timestamps and returns false if it can't do the conversion due to reordering.
96bool RtpToNtpMs(int64_t rtp_timestamp,
wu@webrtc.org66773a02014-05-07 17:09:44 +000097 const RtcpList& rtcp,
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +000098 int64_t* rtp_timestamp_in_ms) {
asaperssonf8cdd182016-03-15 01:00:47 -070099 if (rtcp.size() != 2)
100 return false;
101
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000102 int64_t rtcp_ntp_ms_new = Clock::NtpToMs(rtcp.front().ntp_secs,
103 rtcp.front().ntp_frac);
104 int64_t rtcp_ntp_ms_old = Clock::NtpToMs(rtcp.back().ntp_secs,
105 rtcp.back().ntp_frac);
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +0000106 int64_t rtcp_timestamp_new = rtcp.front().rtp_timestamp;
107 int64_t rtcp_timestamp_old = rtcp.back().rtp_timestamp;
108 if (!CompensateForWrapAround(rtcp_timestamp_new,
109 rtcp_timestamp_old,
110 &rtcp_timestamp_new)) {
111 return false;
112 }
113 double freq_khz;
114 if (!CalculateFrequency(rtcp_ntp_ms_new,
115 rtcp_timestamp_new,
116 rtcp_ntp_ms_old,
117 rtcp_timestamp_old,
118 &freq_khz)) {
119 return false;
120 }
121 double offset = rtcp_timestamp_new - freq_khz * rtcp_ntp_ms_new;
122 int64_t rtp_timestamp_unwrapped;
123 if (!CompensateForWrapAround(rtp_timestamp, rtcp_timestamp_old,
124 &rtp_timestamp_unwrapped)) {
125 return false;
126 }
127 double rtp_timestamp_ntp_ms = (static_cast<double>(rtp_timestamp_unwrapped) -
128 offset) / freq_khz + 0.5f;
129 if (rtp_timestamp_ntp_ms < 0) {
130 return false;
131 }
132 *rtp_timestamp_in_ms = rtp_timestamp_ntp_ms;
133 return true;
134}
135
136int CheckForWrapArounds(uint32_t new_timestamp, uint32_t old_timestamp) {
137 if (new_timestamp < old_timestamp) {
138 // This difference should be less than -2^31 if we have had a wrap around
139 // (e.g. |new_timestamp| = 1, |rtcp_rtp_timestamp| = 2^32 - 1). Since it is
140 // cast to a int32_t, it should be positive.
141 if (static_cast<int32_t>(new_timestamp - old_timestamp) > 0) {
142 // Forward wrap around.
143 return 1;
144 }
145 } else if (static_cast<int32_t>(old_timestamp - new_timestamp) > 0) {
146 // This difference should be less than -2^31 if we have had a backward wrap
147 // around. Since it is cast to a int32_t, it should be positive.
148 return -1;
149 }
150 return 0;
151}
wu@webrtc.org66773a02014-05-07 17:09:44 +0000152
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +0000153} // namespace webrtc