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/interface/rtp_to_ntp.h"
12
13#include "webrtc/system_wrappers/interface/clock.h"
14
15#include <assert.h>
16
17namespace webrtc {
18
19RtcpMeasurement::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);
48  int64_t wraps = CheckForWrapArounds(new_timestamp, old_timestamp);
49  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
57bool 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
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.
95bool RtpToNtpMs(int64_t rtp_timestamp,
96                const RtcpList& rtcp,
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,
106                               rtcp_timestamp_old,
107                               &rtcp_timestamp_new)) {
108    return false;
109  }
110  double freq_khz;
111  if (!CalculateFrequency(rtcp_ntp_ms_new,
112                          rtcp_timestamp_new,
113                          rtcp_ntp_ms_old,
114                          rtcp_timestamp_old,
115                          &freq_khz)) {
116    return false;
117  }
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)) {
122    return false;
123  }
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) {
127    return false;
128  }
129  *rtp_timestamp_in_ms = rtp_timestamp_ntp_ms;
130  return true;
131}
132
133int 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.
140      return 1;
141    }
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.
145    return -1;
146  }
147  return 0;
148}
149
150}  // namespace webrtc
151