Debian Codegraph @

src/proto/rist_time.c

raw symbols SCIP
1 #include "rist_time.h"
2
3 #include "endian-shim.h"
4 #include "rtp.h"
5 #include "time-shim.h"
6
7 #include <stdint.h>
8
9 uint64_t timestampNTP_u64(void) {
10 // We use clock_gettime instead of gettimeofday even though we only need
11 // microseconds because gettimeofday implementation under linux is dependent
12 // on the kernel clock and can produce duplicate times (too close to kernel
13 // timer)
14
15 // We use the NTP time standard: rfc5905
16 // (https://tools.ietf.org/html/rfc5905#section-6) The 64-bit timestamps used
17 // by NTP consist of a 32-bit part for seconds and a 32-bit part for
18 // fractional second, giving a time scale that rolls over every 232 seconds
19 // (136 years) and a theoretical resolution of 2−32 seconds (233 picoseconds).
20 // NTP uses an epoch of January 1, 1900. Therefore, the first rollover occurs
21 // on February 7, 2036.
22 // Zero-initialize so that, if clock_gettime fails (e.g. a sandbox blocks
23 // sysctl(KERN_BOOTTIME) in the macOS shim), we return a stable zero value
24 // instead of stack garbage that would break every time comparison downstream.
25 timespec_t ts = {0, 0};
26 #if defined(__APPLE__)
27 clock_gettime_osx(CLOCK_MONOTONIC_OSX, &ts);
28 #else
29 clock_gettime(CLOCK_MONOTONIC, &ts);
30 #endif
31 // Convert nanoseconds to 32-bits fraction (232 picosecond units)
32 uint64_t t = (uint64_t)(ts.tv_nsec) << 32;
33 t /= 1000000000;
34 // There is 70 years (incl. 17 leap ones) offset to the Unix Epoch.
35 // No leap seconds during that period since they were not invented yet.
36 t |= (uint64_t)((70LL * 365 + 17) * 24 * 60 * 60 + ts.tv_sec) << 32;
37 return t; // nanoseconds (technically, 232.831 picosecond units)
38 }
39
40 uint64_t timestampNTP_RTC_u64(void) {
41 timespec_t ts = {0, 0};
42 #if defined(__APPLE__)
43 clock_gettime_osx(CLOCK_REALTIME_OSX, &ts);
44 #else
45 clock_gettime(CLOCK_REALTIME, &ts);
46 #endif
47 // Convert nanoseconds to 32-bits fraction (232 picosecond units)
48 uint64_t t = (uint64_t)(ts.tv_nsec) << 32;
49 t /= 1000000000;
50 // There is 70 years (incl. 17 leap ones) offset to the Unix Epoch.
51 // No leap seconds during that period since they were not invented yet.
52 t |= (uint64_t)((70LL * 365 + 17) * 24 * 60 * 60 + ts.tv_sec) << 32;
53 return t;
54 }
55
56 uint32_t timestampRTP_u32(int advanced, uint64_t i_ntp) {
57 if (!advanced) {
58 i_ntp *= RTP_PTYPE_MPEGTS_CLOCKHZ;
59 i_ntp = i_ntp >> 32;
60 return (uint32_t)i_ntp;
61 } else {
62 // We just need the middle 32 bits, i.e. 65536Hz clock
63 i_ntp = i_ntp >> 16;
64 return (uint32_t)i_ntp;
65 }
66 }
67
68 uint64_t convertRTPtoNTP(uint8_t ptype, uint32_t time_extension,
69 uint32_t i_rtp) {
70 uint64_t i_ntp;
71 if (ptype == RTP_PTYPE_RIST) {
72 // Convert rtp to 64 bit and shift it 16 bits
73 uint64_t part2 = (uint64_t)i_rtp;
74 part2 = part2 << 16;
75 // rebuild source_time (lower and upper 16 bits)
76 uint64_t part3 = (uint64_t)(time_extension & 0xffff);
77 uint64_t part1 = ((uint64_t)(time_extension & 0xffff0000)) << 32;
78 i_ntp = part1 | part2 | part3;
79 // fprintf(stderr,"source time %"PRIu64", rtp time %"PRIu32"\n",
80 // source_time, rtp_time);
81 } else {
82 int32_t clock = get_rtp_ts_clock(ptype);
83 if (RIST_UNLIKELY(!clock)) {
84 clock = RTP_PTYPE_MPEGTS_CLOCKHZ;
85 // Insert a new timestamp (not ideal but better than failing)
86 i_rtp = htobe32(timestampRTP_u32(0, timestampNTP_u64()));
87 }
88 i_ntp = (uint64_t)i_rtp << 32;
89 i_ntp /= clock;
90 }
91 return i_ntp;
92 }
93
94 uint64_t calculate_rtt_delay(uint64_t request, uint64_t response,
95 uint32_t delay) {
96 /* both request and response are NTP timestamps, delay is in microseconds */
97 if (request > response)
98 return 0;
99 uint64_t rtt = response - request;
100 if (RIST_UNLIKELY(delay)) {
101 uint64_t delay_ticks = (((uint64_t)delay) << 32) / 1000000;
102 if (delay_ticks > rtt)
103 return 0;
104 rtt -= delay_ticks;
105 }
106 return rtt;
107 }