| 1 | /* librist. SPDX-License-Identifier: BSD-2-Clause |
| 2 | * |
| 3 | * Advanced Profile (TR-06-3) receive-side source-clock reconstruction. |
| 4 | * |
| 5 | * The Advanced Profile carries a 32-bit RTP timestamp on a 1 MHz clock |
| 6 | * (Section 5.2.1), i.e. microseconds that wrap every ~71.6 min. To dejitter |
| 7 | * off the sender's timeline (instead of local arrival), the receiver rebuilds |
| 8 | * a monotonic 64-bit NTP source clock from that field: accumulate wrap-safe |
| 9 | * signed deltas (good for reorders/gaps up to +-35 min) scaled us -> NTP |
| 10 | * ticks (x 2^32/1e6, exact as x2^26/15625, overflow-safe). |
| 11 | * |
| 12 | * By default the receiver assumes a conformant clock and dejitters off it |
| 13 | * immediately, exactly like the Main profile. The catch is our own history: |
| 14 | * librist releases before this one emitted this timestamp on a broken clock |
| 15 | * (65536x too fast; see the sender fix), so a receiver may be fed by a sender |
| 16 | * still running that code. There is no negotiated signal for it, so over the |
| 17 | * first window the receiver also checks that the rebuilt clock advances at |
| 18 | * real-time rate. A conformant clock matches arrival elapsed within jitter; a |
| 19 | * broken one is off by orders of magnitude, and the peer then falls back to |
| 20 | * arrival timing. The fallback is re-tried every window, not latched for the |
| 21 | * session: a sender that recovers (or an attack burst that stops) costs one |
| 22 | * window of dejitter, not the rest of the session. */ |
| 23 | |
| 24 | #ifndef RIST_ADV_TS_H |
| 25 | #define RIST_ADV_TS_H |
| 26 | |
| 27 | #include <stdbool.h> |
| 28 | #include <stdint.h> |
| 29 | |
| 30 | struct rist_adv_ts_state { |
| 31 | bool seeded; /* first live packet has anchored the clock */ |
| 32 | bool calibrating; /* still verifying the source clock rate */ |
| 33 | bool use_source; /* post-verdict: source clock kept, else arrival */ |
| 34 | uint32_t last; /* last wire 1 MHz timestamp accumulated */ |
| 35 | uint64_t accum; /* reconstructed source time, NTP ticks (1 s = 2^32) */ |
| 36 | uint64_t seed_now; /* arrival at the window anchor */ |
| 37 | uint64_t deadline; /* seed_now + window: when the verdict is (re)taken */ |
| 38 | uint64_t recon_anchor; /* accum at the window anchor (fallback re-verdict) */ |
| 39 | }; |
| 40 | |
| 41 | /* Reconstruct the source time (NTP ticks) for one Advanced data packet. |
| 42 | * wire_ts : the packet's 32-bit 1 MHz RTP timestamp |
| 43 | * now : local arrival time (NTP ticks), also the fallback value |
| 44 | * window : the source clock is used immediately; over this span its rate |
| 45 | * is verified and, if it fails, the peer falls back to arrival |
| 46 | * (re-verified every window thereafter) |
| 47 | * advance : true for an in-order live packet (advances the clock); |
| 48 | * false for a retransmit (position only, no state change) |
| 49 | * Returns the source time to stamp the packet with. */ |
| 50 | static inline uint64_t |
| 51 | rist_adv_ts_reconstruct(struct rist_adv_ts_state *st, uint32_t wire_ts, |
| 52 | uint64_t now, uint64_t window, bool advance) |
| 53 | { |
| 54 | /* Only a live packet can anchor the clock; until then there is no |
| 55 | * reference, so stamp arrival. */ |
| 56 | if (!st->seeded) { |
| 57 | if (!advance) |
| 58 | return now; |
| 59 | st->seeded = true; |
| 60 | st->calibrating = true; |
| 61 | st->last = wire_ts; |
| 62 | st->accum = now; |
| 63 | st->seed_now = now; |
| 64 | st->deadline = now + window; |
| 65 | st->recon_anchor = st->accum; |
| 66 | return now; |
| 67 | } |
| 68 | |
| 69 | /* Advance the rebuilt clock by the wire delta (wrap-safe signed 32-bit), |
| 70 | * scaled us -> NTP ticks. */ |
| 71 | int64_t d = (int32_t)(wire_ts - st->last); |
| 72 | uint64_t recon = st->accum + (uint64_t)((d * 67108864LL) / 15625LL); |
| 73 | |
| 74 | if (st->calibrating) { |
| 75 | /* Assume a conformant clock and dejitter off it from the start, |
| 76 | * like the Main profile. A retransmit only positions. Do not |
| 77 | * re-anchor here: a broken clock must be allowed to diverge so the |
| 78 | * verdict can see it. */ |
| 79 | if (!advance) |
| 80 | return recon; |
| 81 | st->accum = recon; |
| 82 | st->last = wire_ts; |
| 83 | if (now < st->deadline) |
| 84 | return recon; |
| 85 | /* Verdict: over a full window a conformant clock has advanced at |
| 86 | * real-time rate, so reconstructed elapsed matches arrival elapsed |
| 87 | * within jitter. A broken clock (the 65536x-fast one an older |
| 88 | * release emitted) misses by far more; fall back to arrival and |
| 89 | * keep re-trying the verdict every window. */ |
| 90 | st->calibrating = false; |
| 91 | uint64_t src_elapsed = st->accum - st->seed_now; |
| 92 | uint64_t arr_elapsed = now - st->seed_now; |
| 93 | uint64_t drift = src_elapsed > arr_elapsed |
| 94 | ? src_elapsed - arr_elapsed |
| 95 | : arr_elapsed - src_elapsed; |
| 96 | st->use_source = drift <= (arr_elapsed >> 1); |
| 97 | st->recon_anchor = st->accum; |
| 98 | st->seed_now = now; |
| 99 | st->deadline = now + window; |
| 100 | return st->use_source ? recon : now; |
| 101 | } |
| 102 | |
| 103 | if (!st->use_source) { |
| 104 | /* Arrival fallback. Keep tracking the wire clock and re-run the |
| 105 | * verdict each window; if the source clock advances at real-time |
| 106 | * rate again, resume dejittering off it. A retransmit only |
| 107 | * positions (stamped arrival anyway). */ |
| 108 | if (!advance) |
| 109 | return now; |
| 110 | st->accum = recon; |
| 111 | st->last = wire_ts; |
| 112 | if (now >= st->deadline) { |
| 113 | uint64_t src_elapsed = st->accum - st->recon_anchor; |
| 114 | uint64_t arr_elapsed = now - st->seed_now; |
| 115 | uint64_t drift = src_elapsed > arr_elapsed |
| 116 | ? src_elapsed - arr_elapsed |
| 117 | : arr_elapsed - src_elapsed; |
| 118 | st->use_source = drift <= (arr_elapsed >> 1); |
| 119 | st->recon_anchor = st->accum; |
| 120 | st->seed_now = now; |
| 121 | st->deadline = now + window; |
| 122 | } |
| 123 | return now; |
| 124 | } |
| 125 | |
| 126 | if (!advance) |
| 127 | return recon; /* retransmit: position only, no state change */ |
| 128 | |
| 129 | /* Verified: trust the source clock like the Main profile. A sender clock |
| 130 | * discontinuity is followed, not snapped to arrival (snapping would inject |
| 131 | * worse jitter than the jump itself). */ |
| 132 | st->accum = recon; |
| 133 | st->last = wire_ts; |
| 134 | return recon; |
| 135 | } |
| 136 | |
| 137 | #endif /* RIST_ADV_TS_H */ |