blob: 706d861b55c82b0457516131c87b2412e87c16ca [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) {
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.
77 return true;
78 }
79 }
80
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();
85 }
86 rtcp_list->push_front(measurement);
87 *new_rtcp_sr = true;
88 return true;
89}
90
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +000091// 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.
95bool RtpToNtpMs(int64_t rtp_timestamp,
wu@webrtc.org66773a02014-05-07 17:09:44 +000096 const RtcpList& rtcp,
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +000097 int64_t* rtp_timestamp_in_ms) {
asaperssonf8cdd182016-03-15 01:00:47 -070098 if (rtcp.size() != 2)
99 return false;
100
stefan@webrtc.orgb8e7f4c2013-04-12 11:56:23 +0000101 int64_t rtcp_ntp_ms_new = Clock::NtpToMs(rtcp.front().ntp_secs,
102 rtcp.front().ntp_frac);
103 int64_t rtcp_ntp_ms_old = Clock::NtpToMs(rtcp.back().ntp_secs,
104 rtcp.back().ntp_frac);
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +0000105 int64_t rtcp_timestamp_new = rtcp.front().rtp_timestamp;
106 int64_t rtcp_timestamp_old = rtcp.back().rtp_timestamp;
107 if (!CompensateForWrapAround(rtcp_timestamp_new,
108 rtcp_timestamp_old,
109 &rtcp_timestamp_new)) {
110 return false;
111 }
112 double freq_khz;
113 if (!CalculateFrequency(rtcp_ntp_ms_new,
114 rtcp_timestamp_new,
115 rtcp_ntp_ms_old,
116 rtcp_timestamp_old,
117 &freq_khz)) {
118 return false;
119 }
120 double offset = rtcp_timestamp_new - freq_khz * rtcp_ntp_ms_new;
121 int64_t rtp_timestamp_unwrapped;
122 if (!CompensateForWrapAround(rtp_timestamp, rtcp_timestamp_old,
123 &rtp_timestamp_unwrapped)) {
124 return false;
125 }
126 double rtp_timestamp_ntp_ms = (static_cast<double>(rtp_timestamp_unwrapped) -
127 offset) / freq_khz + 0.5f;
128 if (rtp_timestamp_ntp_ms < 0) {
129 return false;
130 }
131 *rtp_timestamp_in_ms = rtp_timestamp_ntp_ms;
132 return true;
133}
134
135int CheckForWrapArounds(uint32_t new_timestamp, uint32_t old_timestamp) {
136 if (new_timestamp < old_timestamp) {
137 // This difference should be less than -2^31 if we have had a wrap around
138 // (e.g. |new_timestamp| = 1, |rtcp_rtp_timestamp| = 2^32 - 1). Since it is
139 // cast to a int32_t, it should be positive.
140 if (static_cast<int32_t>(new_timestamp - old_timestamp) > 0) {
141 // Forward wrap around.
142 return 1;
143 }
144 } else if (static_cast<int32_t>(old_timestamp - new_timestamp) > 0) {
145 // This difference should be less than -2^31 if we have had a backward wrap
146 // around. Since it is cast to a int32_t, it should be positive.
147 return -1;
148 }
149 return 0;
150}
wu@webrtc.org66773a02014-05-07 17:09:44 +0000151
stefan@webrtc.org64d9dec2012-09-26 16:47:40 +0000152} // namespace webrtc