andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [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 | |
stefan@webrtc.org | 9b53152 | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/remote_bitrate_estimator/include/rtp_to_ntp.h" |
| 12 | |
| 13 | #include "webrtc/system_wrappers/interface/clock.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 14 | |
| 15 | #include <assert.h> |
| 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | namespace synchronization { |
| 20 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 21 | RtcpMeasurement::RtcpMeasurement() |
| 22 | : ntp_secs(0), ntp_frac(0), rtp_timestamp(0) {} |
| 23 | |
| 24 | RtcpMeasurement::RtcpMeasurement(uint32_t ntp_secs, uint32_t ntp_frac, |
| 25 | uint32_t timestamp) |
| 26 | : ntp_secs(ntp_secs), ntp_frac(ntp_frac), rtp_timestamp(timestamp) {} |
| 27 | |
| 28 | // Calculates the RTP timestamp frequency from two pairs of NTP and RTP |
| 29 | // timestamps. |
| 30 | bool CalculateFrequency( |
| 31 | int64_t rtcp_ntp_ms1, |
| 32 | uint32_t rtp_timestamp1, |
| 33 | int64_t rtcp_ntp_ms2, |
| 34 | uint32_t rtp_timestamp2, |
| 35 | double* frequency_khz) { |
| 36 | if (rtcp_ntp_ms1 <= rtcp_ntp_ms2) { |
| 37 | return false; |
| 38 | } |
| 39 | *frequency_khz = static_cast<double>(rtp_timestamp1 - rtp_timestamp2) / |
| 40 | static_cast<double>(rtcp_ntp_ms1 - rtcp_ntp_ms2); |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | // Detects if there has been a wraparound between |old_timestamp| and |
| 45 | // |new_timestamp|, and compensates by adding 2^32 if that is the case. |
| 46 | bool CompensateForWrapAround(uint32_t new_timestamp, |
| 47 | uint32_t old_timestamp, |
| 48 | int64_t* compensated_timestamp) { |
| 49 | assert(compensated_timestamp); |
| 50 | int64_t wraps = synchronization::CheckForWrapArounds(new_timestamp, |
| 51 | old_timestamp); |
| 52 | if (wraps < 0) { |
| 53 | // Reordering, don't use this packet. |
| 54 | return false; |
| 55 | } |
| 56 | *compensated_timestamp = new_timestamp + (wraps << 32); |
| 57 | return true; |
| 58 | } |
| 59 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 60 | // Converts |rtp_timestamp| to the NTP time base using the NTP and RTP timestamp |
| 61 | // pairs in |rtcp|. The converted timestamp is returned in |
| 62 | // |rtp_timestamp_in_ms|. This function compensates for wrap arounds in RTP |
| 63 | // timestamps and returns false if it can't do the conversion due to reordering. |
| 64 | bool RtpToNtpMs(int64_t rtp_timestamp, |
| 65 | const synchronization::RtcpList& rtcp, |
| 66 | int64_t* rtp_timestamp_in_ms) { |
| 67 | assert(rtcp.size() == 2); |
stefan@webrtc.org | 9b53152 | 2013-04-12 11:56:23 +0000 | [diff] [blame] | 68 | int64_t rtcp_ntp_ms_new = Clock::NtpToMs(rtcp.front().ntp_secs, |
| 69 | rtcp.front().ntp_frac); |
| 70 | int64_t rtcp_ntp_ms_old = Clock::NtpToMs(rtcp.back().ntp_secs, |
| 71 | rtcp.back().ntp_frac); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 72 | int64_t rtcp_timestamp_new = rtcp.front().rtp_timestamp; |
| 73 | int64_t rtcp_timestamp_old = rtcp.back().rtp_timestamp; |
| 74 | if (!CompensateForWrapAround(rtcp_timestamp_new, |
| 75 | rtcp_timestamp_old, |
| 76 | &rtcp_timestamp_new)) { |
| 77 | return false; |
| 78 | } |
| 79 | double freq_khz; |
| 80 | if (!CalculateFrequency(rtcp_ntp_ms_new, |
| 81 | rtcp_timestamp_new, |
| 82 | rtcp_ntp_ms_old, |
| 83 | rtcp_timestamp_old, |
| 84 | &freq_khz)) { |
| 85 | return false; |
| 86 | } |
| 87 | double offset = rtcp_timestamp_new - freq_khz * rtcp_ntp_ms_new; |
| 88 | int64_t rtp_timestamp_unwrapped; |
| 89 | if (!CompensateForWrapAround(rtp_timestamp, rtcp_timestamp_old, |
| 90 | &rtp_timestamp_unwrapped)) { |
| 91 | return false; |
| 92 | } |
| 93 | double rtp_timestamp_ntp_ms = (static_cast<double>(rtp_timestamp_unwrapped) - |
| 94 | offset) / freq_khz + 0.5f; |
| 95 | if (rtp_timestamp_ntp_ms < 0) { |
| 96 | return false; |
| 97 | } |
| 98 | *rtp_timestamp_in_ms = rtp_timestamp_ntp_ms; |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | int CheckForWrapArounds(uint32_t new_timestamp, uint32_t old_timestamp) { |
| 103 | if (new_timestamp < old_timestamp) { |
| 104 | // This difference should be less than -2^31 if we have had a wrap around |
| 105 | // (e.g. |new_timestamp| = 1, |rtcp_rtp_timestamp| = 2^32 - 1). Since it is |
| 106 | // cast to a int32_t, it should be positive. |
| 107 | if (static_cast<int32_t>(new_timestamp - old_timestamp) > 0) { |
| 108 | // Forward wrap around. |
| 109 | return 1; |
| 110 | } |
| 111 | } else if (static_cast<int32_t>(old_timestamp - new_timestamp) > 0) { |
| 112 | // This difference should be less than -2^31 if we have had a backward wrap |
| 113 | // around. Since it is cast to a int32_t, it should be positive. |
| 114 | return -1; |
| 115 | } |
| 116 | return 0; |
| 117 | } |
| 118 | } // namespace synchronization |
| 119 | } // namespace webrtc |