blob: 1e01c94c2dcb54fe24cef293b8b791fb7af9c684 [file] [log] [blame]
danilchap1227e8b2015-12-21 11:06:50 -08001/*
2 * Copyright (c) 2015 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_RTP_RTCP_SOURCE_TIME_UTIL_H_
12#define MODULES_RTP_RTCP_SOURCE_TIME_UTIL_H_
danilchap1227e8b2015-12-21 11:06:50 -080013
pbosc7c26a02017-01-02 08:42:32 -080014#include <stdint.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "system_wrappers/include/ntp_time.h"
danilchap1227e8b2015-12-21 11:06:50 -080017
18namespace webrtc {
19
Danil Chapovalov24929842017-11-28 10:26:54 +010020// Converts time obtained using rtc::TimeMicros to ntp format.
21// TimeMicrosToNtp guarantees difference of the returned values matches
22// difference of the passed values.
Danil Chapovalov89c79382018-02-22 16:34:50 +010023// As a result TimeMicrosToNtp(rtc::TimeMicros()) doesn't guarantee to match
24// system time.
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020025// However, TimeMicrosToNtp Guarantees that returned NtpTime will be offsetted
26// from rtc::TimeMicros() by integral number of milliseconds.
27// Use NtpOffsetMs() to get that offset value.
Danil Chapovalov24929842017-11-28 10:26:54 +010028NtpTime TimeMicrosToNtp(int64_t time_us);
29
Ilya Nikolaevskiy88c2c502018-10-26 16:00:08 +020030// Difference between Ntp time and local relative time returned by
31// rtc::TimeMicros()
32int64_t NtpOffsetMs();
33
danilchap1227e8b2015-12-21 11:06:50 -080034// Converts NTP timestamp to RTP timestamp.
35inline uint32_t NtpToRtp(NtpTime ntp, uint32_t freq) {
36 uint32_t tmp = (static_cast<uint64_t>(ntp.fractions()) * freq) >> 32;
37 return ntp.seconds() * freq + tmp;
38}
danilchap1227e8b2015-12-21 11:06:50 -080039
40// Helper function for compact ntp representation:
41// RFC 3550, Section 4. Time Format.
42// Wallclock time is represented using the timestamp format of
43// the Network Time Protocol (NTP).
44// ...
45// In some fields where a more compact representation is
46// appropriate, only the middle 32 bits are used; that is, the low 16
47// bits of the integer part and the high 16 bits of the fractional part.
48inline uint32_t CompactNtp(NtpTime ntp) {
49 return (ntp.seconds() << 16) | (ntp.fractions() >> 16);
50}
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010051
52// Converts interval in microseconds to compact ntp (1/2^16 seconds) resolution.
53// Negative values converted to 0, Overlarge values converted to max uint32_t.
54uint32_t SaturatedUsToCompactNtp(int64_t us);
55
danilchap1227e8b2015-12-21 11:06:50 -080056// Converts interval between compact ntp timestamps to milliseconds.
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010057// This interval can be up to ~9.1 hours (2^15 seconds).
58// Values close to 2^16 seconds consider negative and result in minimum rtt = 1.
59int64_t CompactNtpRttToMs(uint32_t compact_ntp_interval);
danilchap1227e8b2015-12-21 11:06:50 -080060
61} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020062#endif // MODULES_RTP_RTCP_SOURCE_TIME_UTIL_H_