Debian Codegraph @

src/proto/rist_time.h

raw symbols SCIP
1 #ifndef RIST_TIME_H
2 #define RIST_TIME_H
3
4 #include "common/attributes.h"
5 #include "proto/rtp.h"
6
7 #include <stdint.h>
8 /* Time conversion */
9 // this value is UINT32_MAX 4294967.296
10 #define RIST_CLOCK (4294967LL)
11 #define ONE_SECOND (1000 * RIST_CLOCK)
12 #define RIST_LOG_QUIESCE_TIMER ONE_SECOND
13 #define SEVENTY_YEARS_OFFSET (2208988800ULL)
14
15 RIST_PRIV uint64_t timestampNTP_u64(void);
16 RIST_PRIV uint64_t timestampNTP_RTC_u64(void);
17 RIST_PRIV uint64_t convertRTPtoNTP(uint8_t ptype, uint32_t time_extension, uint32_t i_rtp);
18 RIST_PRIV uint64_t calculate_rtt_delay(uint64_t request, uint64_t response, uint32_t delay);
19
20 /* Convert a 64-bit NTP timestamp (1 s = 2^32) to a 32-bit RTP timestamp.
21 * advanced picks the clock: the Advanced Profile 1 MHz clock (TR-06-3
22 * Section 5.2.1; RIST_ADV_CLOCK_HZ) or the MPEG-TS 90 kHz clock. Scaling by
23 * the clock rate and shifting down 32 bits gives the tick count; the uint32
24 * cast wraps as RTP timestamps do. Header-inline so it can be unit-tested
25 * without dragging in the clock_gettime plumbing of this module. */
26 static inline uint32_t timestampRTP_u32(int advanced, uint64_t i_ntp)
27 {
28 uint64_t hz = advanced ? 1000000ULL : (uint64_t)RTP_PTYPE_MPEGTS_CLOCKHZ;
29 return (uint32_t)((i_ntp * hz) >> 32);
30 }
31
32 /* Convert the 64-bit NTP form (1 s = 2^32) to nanoseconds or microseconds. The
33 * halves are converted separately because the 1900 epoch offset in the upper 32
34 * bits makes (i_ntp * 1000000000) >> 32 overflow a uint64. */
35 static inline uint64_t timestampNTP_to_ns(uint64_t i_ntp)
36 {
37 return (i_ntp >> 32) * 1000000000ULL +
38 (((i_ntp & 0xFFFFFFFFULL) * 1000000000ULL) >> 32);
39 }
40
41 static inline uint64_t timestampNTP_to_us(uint64_t i_ntp)
42 {
43 return (i_ntp >> 32) * 1000000ULL +
44 (((i_ntp & 0xFFFFFFFFULL) * 1000000ULL) >> 32);
45 }
46
47 #endif /* RIST_TIME_H */