Skip to main content

gap/
client.rs

1//! Stateful GAP client.
2
3use crate::GapPayload;
4use gbp::CodecError;
5use gbp_core::{GbpFlags, MemberId, PayloadCodec, StreamType, timeouts};
6use gbp_node::{GroupNode, NodeError, OutboundFrame, Sealer};
7use std::collections::HashMap;
8use std::time::Duration;
9#[cfg(not(target_arch = "wasm32"))]
10use std::time::Instant;
11#[cfg(target_arch = "wasm32")]
12use web_time::Instant;
13
14/// Errors returned by [`GapClient`].
15#[derive(Debug, thiserror::Error)]
16pub enum GapError {
17    /// Failed to decode the CBOR payload.
18    #[error("decode: {0}")]
19    Decode(#[from] CodecError),
20    /// `key_phase` does not match the current group epoch (GAP §10).
21    #[error("epoch stale: kp={kp}, expected={expected}")]
22    EpochStale {
23        /// Reported `key_phase`.
24        kp: u32,
25        /// Expected `key_phase` (current epoch).
26        expected: u32,
27    },
28    /// `rtp_sequence` was already seen for the same `media_source_id`.
29    #[error("rtp replay: src={src}, seq={seq}, hw={hw}")]
30    RtpReplay {
31        /// `media_source_id`.
32        src: u32,
33        /// Reported `rtp_sequence`.
34        seq: u32,
35        /// Replay-window high-water mark.
36        hw: u32,
37    },
38    /// Underlying GBP node error during send.
39    #[error("node: {0}")]
40    Node(#[from] NodeError),
41}
42
43/// Outcome of accepting a GAP payload.
44#[derive(Debug)]
45pub enum GapAccept {
46    /// New audio frame.
47    New(GapPayload),
48    /// Late audio frame (`rtp_sequence` <= last seen). MAY be dropped per
49    /// GAP §7.
50    Late(GapPayload),
51}
52
53/// A snapshot of one old epoch's replay window, kept for `T_GAP_KEY_OVERLAP_MS`
54/// so that late in-flight audio frames from that epoch are still accepted.
55struct OldEpochWindow {
56    epoch: u64,
57    in_hw: HashMap<u32, u32>,
58    expires: Instant,
59}
60
61/// Stateful GAP client.
62///
63/// Maintains an outbound `rtp_sequence` counter and an inbound replay window,
64/// both keyed by `media_source_id`.
65///
66/// The client observes the current group epoch on every [`GapClient::send`]
67/// or [`GapClient::accept`] call and automatically clears its replay window
68/// when the epoch advances. Callers may also drive a reset explicitly via
69/// [`GapClient::reset`].
70///
71/// Old-epoch windows are retained for [`timeouts::T_GAP_KEY_OVERLAP_MS`]
72/// (default 10 s) so that in-flight audio frames from the previous epoch can
73/// still be accepted after an epoch transition (gap_rfc §4).
74#[derive(Default)]
75pub struct GapClient {
76    out_rtp_seq: HashMap<u32, u32>,
77    in_hw: HashMap<u32, u32>,
78    current_epoch: Option<u64>,
79    /// Old replay windows from previous epochs, retained until T_overlap expires.
80    old_windows: Vec<OldEpochWindow>,
81}
82
83impl GapClient {
84    /// Creates an empty client.
85    pub fn new() -> Self {
86        Self::default()
87    }
88
89    /// Sends an Opus frame. `key_phase` is taken from `node.current_epoch`.
90    /// Uses the `O` profile (no `R` / `A` — voice is not reliable, GAP §7).
91    ///
92    /// The wire `rtp_sequence` is clamped to the 16-bit RTP range; on
93    /// overflow it wraps from `0xFFFF` back to `0x0000`.
94    /// `codec` controls how the payload is encoded; use [`PayloadCodec::Cbor`]
95    /// for maximum compatibility or [`PayloadCodec::FlatBuffers`] for
96    /// lowest decode latency.
97    // One parameter per RTP/GAP field passed through to `node.send_payload`
98    // below; a builder would just shuffle the same arguments.
99    #[allow(clippy::too_many_arguments)]
100    pub fn send<S: Sealer>(
101        &mut self,
102        node: &mut GroupNode,
103        seal: &mut S,
104        target: MemberId,
105        media_source_id: u32,
106        rtp_timestamp: u64,
107        opus: Vec<u8>,
108        codec: PayloadCodec,
109    ) -> Result<OutboundFrame, GapError> {
110        self.sync_epoch(node.current_epoch);
111        let seq = self.out_rtp_seq.entry(media_source_id).or_insert(0);
112        // RTP `sequence_number` is 16 bits — clamp every increment.
113        *seq = seq.wrapping_add(1) & 0xFFFF;
114        let payload = GapPayload {
115            media_source_id,
116            rtp_sequence: *seq,
117            rtp_timestamp,
118            key_phase: node.current_epoch as u32,
119            opus_frame: serde_bytes::ByteBuf::from(opus),
120        };
121        let stream_id = node.member_stream_id(2);
122        Ok(node.send_payload(
123            seal,
124            target,
125            StreamType::Audio,
126            stream_id,
127            GbpFlags::ordered_only(),
128            &payload.to_bytes(codec),
129            codec,
130        )?)
131    }
132
133    /// Accepts a plaintext payload delivered by the GBP layer.
134    ///
135    /// Returns [`GapAccept::New`] for fresh frames, [`GapAccept::Late`] for
136    /// replays that the spec allows to drop. Returns [`GapError::EpochStale`]
137    /// only when `key_phase` refers to an epoch that has already expired its
138    /// T_overlap window; frames from epochs still within T_overlap are
139    /// accepted normally (gap_rfc §4).
140    /// `codec` must match [`DeliveredPayload::codec`].
141    pub fn accept(
142        &mut self,
143        plaintext: &[u8],
144        current_epoch: u64,
145        codec: PayloadCodec,
146    ) -> Result<GapAccept, GapError> {
147        self.sync_epoch(current_epoch);
148        let p = GapPayload::from_bytes(plaintext, codec)?;
149        if p.key_phase == current_epoch as u32 {
150            // Fast path: current epoch.
151            let hw = self.in_hw.get(&p.media_source_id).copied().unwrap_or(0);
152            if p.rtp_sequence <= hw && hw.wrapping_sub(p.rtp_sequence) <= 0x7FFF {
153                return Ok(GapAccept::Late(p));
154            }
155            self.in_hw.insert(p.media_source_id, p.rtp_sequence);
156            return Ok(GapAccept::New(p));
157        }
158        // Slow path: frame from an older epoch — check the overlap buffer.
159        let now = Instant::now();
160        if let Some(old) = self
161            .old_windows
162            .iter_mut()
163            .find(|w| w.epoch == p.key_phase as u64 && w.expires > now)
164        {
165            let hw = old.in_hw.get(&p.media_source_id).copied().unwrap_or(0);
166            if p.rtp_sequence <= hw && hw.wrapping_sub(p.rtp_sequence) <= 0x7FFF {
167                return Ok(GapAccept::Late(p));
168            }
169            old.in_hw.insert(p.media_source_id, p.rtp_sequence);
170            return Ok(GapAccept::New(p));
171        }
172        Err(GapError::EpochStale {
173            kp: p.key_phase,
174            expected: current_epoch as u32,
175        })
176    }
177
178    /// Synchronises the client's view of the group epoch.
179    ///
180    /// When the epoch advances, the current replay window is moved to the
181    /// overlap buffer (retained for `T_GAP_KEY_OVERLAP_MS`) instead of being
182    /// discarded, so late in-flight frames from the previous epoch are still
183    /// accepted (gap_rfc §4). Expired entries are pruned on each call.
184    /// Called automatically by [`GapClient::send`] and [`GapClient::accept`].
185    pub fn sync_epoch(&mut self, epoch: u64) {
186        // Prune expired old windows.
187        let now = Instant::now();
188        self.old_windows.retain(|w| w.expires > now);
189
190        if Some(epoch) != self.current_epoch {
191            // Save current window to overlap buffer before resetting.
192            if let Some(old_epoch) = self.current_epoch
193                && !self.in_hw.is_empty()
194            {
195                self.old_windows.push(OldEpochWindow {
196                    epoch: old_epoch,
197                    in_hw: std::mem::take(&mut self.in_hw),
198                    expires: now + Duration::from_millis(timeouts::T_GAP_KEY_OVERLAP_MS),
199                });
200            }
201            self.out_rtp_seq.clear();
202            self.in_hw.clear();
203            self.current_epoch = Some(epoch);
204        }
205    }
206
207    /// Clears the outbound counters, the replay window, and the overlap buffer
208    /// unconditionally.
209    pub fn reset(&mut self) {
210        self.out_rtp_seq.clear();
211        self.in_hw.clear();
212        self.old_windows.clear();
213        self.current_epoch = None;
214    }
215}
216
217#[cfg(test)]
218mod tests {
219    use super::*;
220
221    fn make_payload(seq: u32, key_phase: u32) -> Vec<u8> {
222        crate::GapPayload {
223            media_source_id: 1,
224            rtp_sequence: seq,
225            rtp_timestamp: 960,
226            key_phase,
227            opus_frame: serde_bytes::ByteBuf::from(b"opus-data".to_vec()),
228        }
229        .to_bytes(PayloadCodec::Cbor)
230    }
231
232    #[test]
233    fn wraparound_after_ffff_is_accepted() {
234        let mut client = GapClient::new();
235        // Prime the high-water mark near wraparound point.
236        let _ = client
237            .accept(&make_payload(0xFFFE, 1), 1, PayloadCodec::Cbor)
238            .unwrap();
239        let _ = client
240            .accept(&make_payload(0xFFFF, 1), 1, PayloadCodec::Cbor)
241            .unwrap();
242        // After wraparound, seq=0 should be accepted as New, not Late.
243        let result = client
244            .accept(&make_payload(0x0000, 1), 1, PayloadCodec::Cbor)
245            .unwrap();
246        assert!(
247            matches!(result, GapAccept::New(_)),
248            "seq=0 after 0xFFFF must be New"
249        );
250    }
251
252    #[test]
253    fn strict_replay_within_window_is_late() {
254        let mut client = GapClient::new();
255        let _ = client
256            .accept(&make_payload(100, 1), 1, PayloadCodec::Cbor)
257            .unwrap();
258        let result = client
259            .accept(&make_payload(100, 1), 1, PayloadCodec::Cbor)
260            .unwrap();
261        assert!(
262            matches!(result, GapAccept::Late(_)),
263            "exact dup must be Late"
264        );
265    }
266
267    #[test]
268    fn epoch_change_clears_window() {
269        let mut client = GapClient::new();
270        let _ = client
271            .accept(&make_payload(1, 1), 1, PayloadCodec::Cbor)
272            .unwrap();
273        // Epoch change: seq 1 was seen in epoch 1, but in epoch 2 it's new again.
274        let result = client
275            .accept(&make_payload(1, 2), 2, PayloadCodec::Cbor)
276            .unwrap();
277        assert!(
278            matches!(result, GapAccept::New(_)),
279            "new epoch resets window"
280        );
281    }
282
283    // ---- T_overlap buffer (gap_rfc §4) --------------------------------------
284
285    #[test]
286    fn old_epoch_frame_accepted_within_overlap() {
287        let mut client = GapClient::new();
288        // Establish seq 5 in epoch 1.
289        let _ = client
290            .accept(&make_payload(5, 1), 1, PayloadCodec::Cbor)
291            .unwrap();
292        // Advance to epoch 2 — old window is buffered.
293        let _ = client
294            .accept(&make_payload(1, 2), 2, PayloadCodec::Cbor)
295            .unwrap();
296        // A late frame from epoch 1 (seq 6, not seen yet) arrives before T_overlap expires.
297        let result = client
298            .accept(&make_payload(6, 1), 2, PayloadCodec::Cbor)
299            .unwrap();
300        assert!(
301            matches!(result, GapAccept::New(_)),
302            "late epoch-1 frame accepted within T_overlap"
303        );
304    }
305
306    #[test]
307    fn old_epoch_replay_is_late_within_overlap() {
308        let mut client = GapClient::new();
309        let _ = client
310            .accept(&make_payload(5, 1), 1, PayloadCodec::Cbor)
311            .unwrap();
312        // Advance epoch.
313        let _ = client
314            .accept(&make_payload(1, 2), 2, PayloadCodec::Cbor)
315            .unwrap();
316        // Same seq from epoch 1 arrives again — replay → Late.
317        let result = client
318            .accept(&make_payload(5, 1), 2, PayloadCodec::Cbor)
319            .unwrap();
320        assert!(
321            matches!(result, GapAccept::Late(_)),
322            "duplicate from old epoch is Late"
323        );
324    }
325
326    #[test]
327    fn expired_old_epoch_frame_is_stale() {
328        let mut client = GapClient::new();
329        let _ = client
330            .accept(&make_payload(5, 1), 1, PayloadCodec::Cbor)
331            .unwrap();
332        // Advance epoch.
333        let _ = client
334            .accept(&make_payload(1, 2), 2, PayloadCodec::Cbor)
335            .unwrap();
336        // Manually expire the overlap window.
337        for w in &mut client.old_windows {
338            w.expires = Instant::now() - Duration::from_millis(1);
339        }
340        // Now a late epoch-1 frame should be rejected.
341        let result = client.accept(&make_payload(6, 1), 2, PayloadCodec::Cbor);
342        assert!(
343            matches!(result, Err(GapError::EpochStale { .. })),
344            "expired epoch is Stale"
345        );
346    }
347
348    #[test]
349    fn reset_clears_overlap_buffer() {
350        let mut client = GapClient::new();
351        let _ = client
352            .accept(&make_payload(1, 1), 1, PayloadCodec::Cbor)
353            .unwrap();
354        let _ = client
355            .accept(&make_payload(1, 2), 2, PayloadCodec::Cbor)
356            .unwrap();
357        assert!(!client.old_windows.is_empty(), "overlap buffer populated");
358        client.reset();
359        assert!(
360            client.old_windows.is_empty(),
361            "overlap buffer cleared after reset"
362        );
363    }
364}