blob: 94b914310ccf20dcab6ca60448b29a9ae1f39243 [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// Helper function for compact ntp representation:
35// RFC 3550, Section 4. Time Format.
36// Wallclock time is represented using the timestamp format of
37// the Network Time Protocol (NTP).
38// ...
39// In some fields where a more compact representation is
40// appropriate, only the middle 32 bits are used; that is, the low 16
41// bits of the integer part and the high 16 bits of the fractional part.
42inline uint32_t CompactNtp(NtpTime ntp) {
43 return (ntp.seconds() << 16) | (ntp.fractions() >> 16);
44}
Danil Chapovalovd4fdc272017-11-09 11:34:32 +010045
46// Converts interval in microseconds to compact ntp (1/2^16 seconds) resolution.
47// Negative values converted to 0, Overlarge values converted to max uint32_t.
48uint32_t SaturatedUsToCompactNtp(int64_t us);
49
danilchap1227e8b2015-12-21 11:06:50 -080050// Converts interval between compact ntp timestamps to milliseconds.
Danil Chapovalovc1e55c72016-03-09 15:14:35 +010051// This interval can be up to ~9.1 hours (2^15 seconds).
52// Values close to 2^16 seconds consider negative and result in minimum rtt = 1.
53int64_t CompactNtpRttToMs(uint32_t compact_ntp_interval);
danilchap1227e8b2015-12-21 11:06:50 -080054
55} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020056#endif // MODULES_RTP_RTCP_SOURCE_TIME_UTIL_H_