Skip to main content

moq_rtc/
session.rs

1//! str0m session driver shared by every HTTP role / media direction.
2//!
3//! str0m is sans-IO, so we drive the [`str0m::Rtc`] instance from a tokio
4//! task that owns a UDP socket. [`Session::run`] alternates between
5//! [`Rtc::poll_output`] (drain pending transmits / events) and
6//! [`Rtc::handle_input`] (feed UDP packets or timeouts).
7//!
8//! The session itself doesn't care whether the [`Rtc`] was populated by
9//! accepting an SDP offer (server side) or by minting one and posting it
10//! to a remote URL (client side), or whether the media flow is RTP-in
11//! ([`MediaSink`]) or RTP-out ([`crate::egress::EgressSource`]).
12
13use std::collections::HashMap;
14use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
15use std::sync::Arc;
16use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
17
18use str0m::{Candidate, Event, IceConnectionState, Input, Output, Rtc, net::Receive};
19use tokio::net::UdpSocket;
20use tokio::sync::mpsc;
21
22use crate::egress::{EgressClock, EgressSource, WriteRequest};
23use crate::{Error, Result, codec};
24
25/// One inbound UDP datagram plus its source address, the unit fed to a session.
26/// The [`server`](crate::server) paths get these from the shared-socket demux
27/// (`crate::server::mux`); the client paths get them from a 1:1 reader
28/// ([`spawn_socket_reader`]).
29pub(crate) type Packet = (Vec<u8>, SocketAddr);
30
31/// Bound on a session's inbound datagram queue, sized like a socket buffer:
32/// past this, datagrams are dropped rather than buffered (WebRTC tolerates loss
33/// and a stalled session must not grow memory without limit).
34pub(crate) const SESSION_INBOX: usize = 256;
35
36/// str0m's outbound video buffer depth (packets), which also backs NACK resends.
37/// Raised above the str0m default (1000) so a late-joining peer can recover a
38/// large keyframe and the rest of the current group via NACK; see
39/// [`rtc_config_with_codecs`].
40const EGRESS_SEND_BUFFER_VIDEO: usize = 3000;
41
42/// Backstop deadline for a session to reach a connected ICE state, covering the one
43/// case str0m's ICE agent deliberately never times out: a peer that answers the SDP
44/// but provides NO remote candidates and sends nothing (an abandoned WHIP/WHEP
45/// offer, or a probe that only exercises signalling). str0m DOES end a connection
46/// whose candidate pairs were tried and exhausted -- the agent goes to
47/// `IceConnectionState::Disconnected` (handled in `handle_event`) after
48/// ~`StunTiming::timeout()` (~21s at the defaults). But when `remote_candidates`
49/// stays empty the agent treats the session as "still possible" forever (trickle
50/// ICE: more candidates could arrive), so it sits in `Checking` indefinitely,
51/// pinning this task, its broadcast announcement, and its mux registration. Nothing
52/// upstream ends it, so we do. Set ABOVE str0m's ~21s pair-exhaustion so a
53/// connection that actually started checks is ended by str0m's native path (and a
54/// slow-but-real TURN/lossy peer isn't clipped); this only fires for the
55/// never-any-candidate case.
56const ICE_ESTABLISH_TIMEOUT: Duration = Duration::from_secs(30);
57
58/// Receives `MediaData` events from str0m and dispatches to the right codec
59/// [`Bridge`](codec::Bridge). Used as the per-session sink in [`Session::run`]
60/// for any flow where RTP arrives from the peer (`server publish` / WHIP
61/// server, `client subscribe` / WHEP client).
62pub trait MediaSink: Send {
63	/// Called once str0m has confirmed which codec is on which `mid`.
64	fn on_track(
65		&mut self,
66		mid: str0m::media::Mid,
67		kind: str0m::media::MediaKind,
68		codec: str0m::format::Codec,
69		audio_params: Option<(u32, u32)>,
70	) -> Result<()>;
71
72	/// Called on each [`MediaData`](str0m::media::MediaData) event. The session
73	/// loop has already converted the timestamp to microseconds.
74	fn on_frame(&mut self, mid: str0m::media::Mid, frame: codec::Frame) -> Result<()>;
75}
76
77/// What the session does with the negotiated media stream.
78#[non_exhaustive]
79pub enum MediaRole {
80	/// RTP-in: dispatch peer frames into a [`MediaSink`].
81	Ingest(Box<dyn MediaSink>),
82	/// RTP-out: pull frames from a [`crate::egress::EgressSource`] and forward to the peer.
83	Egress(Box<EgressSource>),
84}
85
86/// Drives a [`Rtc`] instance until it ends.
87///
88/// The caller pre-populates the `Rtc` with whatever SDP exchange they need.
89/// Sends go out the (possibly shared) `socket`; inbound datagrams arrive on
90/// `inbound` rather than being read off the socket directly, so several
91/// sessions can share one socket behind the `crate::server::mux`.
92pub struct Session {
93	rtc: Rtc,
94	/// Send side. Shared across sessions on the server (the mux socket); owned
95	/// 1:1 on the client. Receiving happens via `inbound`, not this socket.
96	socket: Arc<UdpSocket>,
97	/// The local ICE candidates we advertised. Each inbound datagram is tagged
98	/// (for str0m) with the candidate whose address family matches the packet's
99	/// source, so a dual-stack peer reaching us over IPv6 isn't told the packet
100	/// arrived on an IPv4 host candidate. MUST be the advertised candidates, not
101	/// the socket's bind address: str0m drops a STUN binding request whose
102	/// destination doesn't match a host candidate ("unknown interface"), and the
103	/// shared mux socket binds a wildcard (`0.0.0.0`) while advertising concrete
104	/// IPs. Never empty (falls back to the bound address).
105	locals: Vec<SocketAddr>,
106	/// Inbound datagrams routed to this session (demux on the server, a 1:1
107	/// reader on the client). `None` from `recv` means every sender dropped, so
108	/// the session is done.
109	inbound: mpsc::Receiver<Packet>,
110	role: MediaRole,
111	/// Egress write requests. `Some` only for [`MediaRole::Egress`]
112	/// sessions; pumps send frames here, the main loop forwards them into
113	/// str0m's [`Writer`](str0m::media::Writer).
114	writes_rx: Option<mpsc::Receiver<WriteRequest>>,
115	/// Rebases each ingested track's raw RTP timestamps onto one session
116	/// timeline so audio and video stay in sync. Unused by egress sessions.
117	ingest_clock: IngestClock,
118	/// Maps the shared MoQ presentation timeline to str0m's sender-report
119	/// wallclock. Unused by ingest sessions.
120	egress_clock: EgressClock,
121}
122
123impl Session {
124	/// Convenience for the ingest case (WHIP server, WHEP client). `locals` are the
125	/// advertised ICE candidates (see the field docs), not the socket bind.
126	pub fn ingest(
127		rtc: Rtc,
128		socket: Arc<UdpSocket>,
129		locals: Vec<SocketAddr>,
130		inbound: mpsc::Receiver<Packet>,
131		sink: Box<dyn MediaSink>,
132	) -> Self {
133		Self {
134			rtc,
135			socket,
136			locals,
137			inbound,
138			role: MediaRole::Ingest(sink),
139			writes_rx: None,
140			ingest_clock: IngestClock::default(),
141			egress_clock: EgressClock::default(),
142		}
143	}
144
145	/// Convenience for the egress case (WHEP server, WHIP client). `locals` are the
146	/// advertised ICE candidates (see the field docs), not the socket bind.
147	pub fn egress(
148		rtc: Rtc,
149		socket: Arc<UdpSocket>,
150		locals: Vec<SocketAddr>,
151		inbound: mpsc::Receiver<Packet>,
152		mut source: EgressSource,
153	) -> Self {
154		let writes_rx = source.take_writes();
155		Self {
156			rtc,
157			socket,
158			locals,
159			inbound,
160			role: MediaRole::Egress(Box::new(source)),
161			writes_rx: Some(writes_rx),
162			ingest_clock: IngestClock::default(),
163			egress_clock: EgressClock::default(),
164		}
165	}
166
167	pub async fn run(mut self) -> Result<()> {
168		let started = Instant::now();
169		let mut connected = false;
170		// str0m hands back the canonical destination we fed it, so a dual-stack
171		// socket needs IPv4 re-mapped before each send (see crate::net).
172		let socket_v6 = self.socket.local_addr().map_err(Error::Io)?.is_ipv6();
173		loop {
174			// A dead Rtc (DTLS/SDP failure, explicit disconnect) makes poll_output
175			// return a never-firing timeout instead of erroring, which would hang
176			// this task forever holding the broadcast announcement + mux
177			// registration. Bail so those release.
178			if !self.rtc.is_alive() {
179				return Err(Error::SessionClosed);
180			}
181
182			// Abort a session that never finishes connecting (see
183			// ICE_ESTABLISH_TIMEOUT); once connected, str0m's own timeouts take over.
184			if !connected && started.elapsed() >= ICE_ESTABLISH_TIMEOUT {
185				return Err(Error::IceTimeout);
186			}
187
188			let timeout = match self.rtc.poll_output().map_err(Error::Rtc)? {
189				Output::Timeout(t) => t,
190				Output::Transmit(t) => {
191					let dst = crate::net::to_family(t.destination, socket_v6);
192					if let Err(err) = self.socket.send_to(&t.contents, dst).await {
193						tracing::warn!(%err, %dst, "send failed");
194					}
195					continue;
196				}
197				Output::Event(event) => {
198					if let Event::IceConnectionStateChange(state) = &event {
199						connected |= state.is_connected();
200					}
201					self.handle_event(event)?;
202					continue;
203				}
204			};
205
206			let now = Instant::now();
207			let mut duration = timeout.saturating_duration_since(now);
208			// While still connecting, never sleep past the establishment deadline, so
209			// the check above fires on time even if str0m scheduled a far-off timeout.
210			if !connected {
211				duration = duration.min(ICE_ESTABLISH_TIMEOUT.saturating_sub(started.elapsed()));
212			}
213			if duration.is_zero() {
214				self.rtc.handle_input(Input::Timeout(now)).map_err(Error::Rtc)?;
215				continue;
216			}
217
218			// Wait for the earliest of: an inbound UDP packet, an egress
219			// write request (if egress), or the str0m-requested timeout.
220			tokio::select! {
221				biased;
222
223				// Egress writes get drained promptly. Without `biased` an
224				// idle socket select could starve them.
225				Some(req) = async {
226					match self.writes_rx.as_mut() {
227						Some(rx) => rx.recv().await,
228						None => std::future::pending::<Option<WriteRequest>>().await,
229					}
230				} => {
231					let now = Instant::now();
232					let wallclock = self.egress_clock.wallclock(req.time, now);
233					crate::egress::dispatch(&mut self.rtc, req, wallclock);
234				}
235
236				packet = self.inbound.recv() => {
237					match packet {
238						Some((data, src)) => {
239							let now = Instant::now();
240							// Tag the packet with the advertised candidate matching its
241							// address family, not the socket bind (see the `locals` docs).
242							let local = pick_local(&self.locals, src);
243							let recv = Receive::new(str0m::net::Protocol::Udp, src, local, &data)
244								.map_err(Error::RtcInput)?;
245							self.rtc.handle_input(Input::Receive(now, recv)).map_err(Error::Rtc)?;
246						}
247						// Every sender dropped: the demux unregistered us (or the
248						// 1:1 reader stopped). Nothing more will arrive, so end.
249						None => return Err(Error::SessionClosed),
250					}
251				}
252
253				_ = tokio::time::sleep(duration) => {
254					self.rtc
255						.handle_input(Input::Timeout(Instant::now()))
256						.map_err(Error::Rtc)?;
257				}
258			}
259		}
260	}
261
262	fn handle_event(&mut self, event: Event) -> Result<()> {
263		match event {
264			Event::IceConnectionStateChange(state) => {
265				tracing::debug!(?state, "ice state");
266				if state == IceConnectionState::Disconnected {
267					return Err(Error::SessionClosed);
268				}
269			}
270			Event::MediaAdded(added) => self.handle_media_added(added)?,
271			Event::MediaData(data) => {
272				// `ingest_clock` and `role` are disjoint fields, so the borrow checker lets
273				// us rebase the (random, per-track) RTP base and feed the sink in one
274				// block; egress sessions never get here so the clock stays untouched.
275				if let MediaRole::Ingest(sink) = &mut self.role {
276					let media_us = media_time_to_micros(&data.time);
277					let timestamp_us = self.ingest_clock.normalize(data.mid, data.network_time, media_us);
278					sink.on_frame(
279						data.mid,
280						codec::Frame {
281							timestamp_us,
282							payload: bytes::Bytes::from_owner(data.data),
283						},
284					)?;
285				}
286			}
287			Event::SenderFeedback(feedback) => {
288				if matches!(&self.role, MediaRole::Ingest(_)) {
289					self.ingest_clock.observe(feedback.mid, feedback.sender_info);
290				}
291			}
292			Event::KeyframeRequest(req) => {
293				// PLI / FIR from the egress peer. For v1 we just log and
294				// rely on the next natural keyframe from the MoQ source.
295				tracing::debug!(?req, "keyframe request from peer");
296			}
297			_ => {}
298		}
299		Ok(())
300	}
301
302	fn handle_media_added(&mut self, added: str0m::media::MediaAdded) -> Result<()> {
303		// str0m's CodecConfig is the negotiated set; pick the first
304		// codec advertised for this `mid`.
305		let pt = self.rtc.media(added.mid).and_then(|m| m.remote_pts().first().copied());
306		let params = pt.and_then(|pt| self.rtc.codec_config().params().iter().find(|p| p.pt() == pt).copied());
307		let params = match params {
308			Some(p) => p,
309			None => {
310				tracing::warn!(?added.mid, "no codec params for media; ignoring");
311				return Ok(());
312			}
313		};
314		let spec = params.spec();
315		let codec = spec.codec;
316
317		match &mut self.role {
318			MediaRole::Ingest(sink) => {
319				let audio_params = if codec.is_audio() {
320					Some((spec.clock_rate.get(), spec.channels.unwrap_or(1) as u32))
321				} else {
322					None
323				};
324				sink.on_track(added.mid, added.kind, codec, audio_params)?;
325			}
326			MediaRole::Egress(source) => {
327				source.on_track(added.mid, codec, params.pt(), spec.clock_rate)?;
328			}
329		}
330		Ok(())
331	}
332}
333
334/// Per-session clock that rebases each ingested track's raw RTP timestamps onto
335/// one timeline so audio and video stay in sync.
336///
337/// str0m hands us the RTP header timestamp verbatim
338/// ([`MediaData::time`](str0m::media::MediaData::time)). Per RFC 3550 that base
339/// is random and independent for each track, and str0m applies no RTCP
340/// sender-report correlation, so publishing the values as-is would desync audio
341/// from video (their bases differ by hours) and start the broadcast at an
342/// arbitrary offset. Until every track has an RTCP sender report, we anchor each
343/// track on its first frame's arrival. Once reports are available, their common
344/// NTP clock replaces arrival time as the cross-track reference. The NTP epoch
345/// is chosen so the transition can only move timestamps forward, never rewind a
346/// track that has already been published.
347#[derive(Default)]
348pub(crate) struct IngestClock {
349	/// Arrival time of the first frame seen in the session; the timeline origin.
350	arrival_epoch: Option<Instant>,
351	/// Remote NTP time corresponding to timestamp zero on the published timeline.
352	ntp_epoch_us: Option<i128>,
353	tracks: HashMap<str0m::media::Mid, IngestTrackClock>,
354}
355
356impl IngestClock {
357	/// Record the newest RTP-to-NTP correlation for a track.
358	fn observe(&mut self, mid: str0m::media::Mid, sender: str0m::rtp::rtcp::SenderInfo) {
359		self.tracks.entry(mid).or_default().sender = Some(SenderAnchor::new(sender));
360		self.establish_ntp_epoch();
361	}
362
363	/// Map a raw RTP-derived microsecond timestamp onto the session timeline.
364	/// `arrival` is the packet's network time
365	/// ([`MediaData::network_time`](str0m::media::MediaData::network_time)).
366	fn normalize(&mut self, mid: str0m::media::Mid, arrival: Instant, media_us: u64) -> u64 {
367		let epoch = *self.arrival_epoch.get_or_insert(arrival);
368		let track = self.tracks.entry(mid).or_default();
369		let offset = *track.arrival_offset_us.get_or_insert_with(|| {
370			// Signed wall delta from the epoch: a track whose first frame we dequeue
371			// after the epoch frame may have actually arrived *before* it, and that
372			// lead must pull its timeline earlier (not clamp to the epoch via an
373			// unsigned subtraction) so it stays in sync.
374			let wall_us = if arrival >= epoch {
375				arrival.duration_since(epoch).as_micros() as i64
376			} else {
377				-(epoch.duration_since(arrival).as_micros() as i64)
378			};
379			wall_us as i128 - media_us as i128
380		});
381		let fallback = to_u64(media_us as i128 + offset);
382		let previous = track.last_output_us;
383		track.last_media_us = Some(media_us);
384		track.last_output_us = Some(fallback);
385
386		self.establish_ntp_epoch();
387		let mapped = self
388			.ntp_epoch_us
389			.zip(self.tracks.get(&mid).and_then(|track| track.sender))
390			.map(|(epoch, sender)| to_u64(sender.capture_time_us(media_us) - epoch));
391		let output = match mapped {
392			// The epoch chosen during the transition makes `mapped >= fallback`.
393			// Later sender reports can make tiny clock corrections, so retain strict
394			// monotonicity if one would otherwise move this track backwards.
395			Some(mapped) => mapped.max(previous.map_or(fallback, |last| last.saturating_add(1))),
396			None => fallback,
397		};
398		self.tracks.get_mut(&mid).expect("track was inserted").last_output_us = Some(output);
399		output
400	}
401
402	/// Switch to the sender's common clock once every negotiated track has both
403	/// a media sample and an RTCP sender report.
404	fn establish_ntp_epoch(&mut self) {
405		// A single RTP clock needs rebasing but no cross-track synchronization.
406		// Waiting for two observed tracks also avoids a dormant negotiated m-line
407		// preventing active audio and video from ever switching to sender reports.
408		if self.ntp_epoch_us.is_some() || self.tracks.len() < 2 {
409			return;
410		}
411
412		let mut epoch = i128::MAX;
413		for track in self.tracks.values() {
414			let (Some(sender), Some(media_us), Some(output_us)) =
415				(track.sender, track.last_media_us, track.last_output_us)
416			else {
417				return;
418			};
419			// Choosing the minimum candidate makes every track's NTP-derived
420			// timestamp at least its last published timestamp. The common timeline
421			// may jump forward, but no individual track can rewind.
422			epoch = epoch.min(sender.capture_time_us(media_us) - output_us as i128);
423		}
424		self.ntp_epoch_us = Some(epoch);
425	}
426}
427
428#[derive(Default)]
429struct IngestTrackClock {
430	arrival_offset_us: Option<i128>,
431	sender: Option<SenderAnchor>,
432	last_media_us: Option<u64>,
433	last_output_us: Option<u64>,
434}
435
436#[derive(Clone, Copy)]
437struct SenderAnchor {
438	ntp_us: i128,
439	rtp_us: i128,
440}
441
442impl SenderAnchor {
443	fn new(sender: str0m::rtp::rtcp::SenderInfo) -> Self {
444		Self {
445			ntp_us: system_time_to_micros(sender.ntp_time),
446			rtp_us: media_time_to_micros(&sender.rtp_time) as i128,
447		}
448	}
449
450	fn capture_time_us(self, media_us: u64) -> i128 {
451		self.ntp_us + media_us as i128 - self.rtp_us
452	}
453}
454
455fn system_time_to_micros(time: SystemTime) -> i128 {
456	match time.duration_since(UNIX_EPOCH) {
457		Ok(duration) => duration.as_micros() as i128,
458		Err(err) => -(err.duration().as_micros() as i128),
459	}
460}
461
462fn to_u64(value: i128) -> u64 {
463	value.clamp(0, u64::MAX as i128) as u64
464}
465
466/// Log a finished session at the right level: an ordinary peer disconnect
467/// ([`Error::SessionClosed`]) is debug, a genuine failure is a warning. Keeps
468/// normal WebRTC churn out of the warning stream. `role` labels the path
469/// (e.g. `"whip server"`).
470pub(crate) fn log_session_end(role: &str, result: &Result<()>) {
471	match result {
472		Ok(()) | Err(Error::SessionClosed) => tracing::debug!(role, "session ended"),
473		// An abandoned offer (peer answered but never connected) is normal churn, not
474		// a failure: keep it out of the warning stream.
475		Err(Error::IceTimeout) => tracing::debug!(role, "session ended: ICE never connected"),
476		Err(err) => tracing::warn!(%err, role, "session ended"),
477	}
478}
479
480/// Pick the advertised local candidate to tag an inbound packet with: the first
481/// one whose address family matches `src`, falling back to the first candidate
482/// (the list is never empty). Keeps a dual-stack peer's packets tagged with a
483/// same-family host candidate so str0m's ICE pairing stays consistent.
484fn pick_local(locals: &[SocketAddr], src: SocketAddr) -> SocketAddr {
485	locals
486		.iter()
487		.find(|l| l.is_ipv4() == src.is_ipv4())
488		.copied()
489		.unwrap_or(locals[0])
490}
491
492/// Convert a str0m [`MediaTime`](str0m::media::MediaTime) to microseconds.
493fn media_time_to_micros(time: &str0m::media::MediaTime) -> u64 {
494	// MediaTime stores `numer / denom` seconds; cast through i128 so the
495	// product doesn't overflow at 90 kHz video timestamps.
496	let numer = time.numer() as i128;
497	let denom = time.denom() as i128;
498	if denom == 0 {
499		return 0;
500	}
501	let micros = (numer.saturating_mul(1_000_000)) / denom;
502	micros.max(0) as u64
503}
504
505/// Type-erased map of `Mid` -> codec bridge, populated as `MediaAdded`
506/// events arrive on the ingest side.
507pub(crate) struct Bridges {
508	inner: HashMap<str0m::media::Mid, Box<dyn codec::Bridge>>,
509}
510
511impl Bridges {
512	pub fn new() -> Self {
513		Self { inner: HashMap::new() }
514	}
515
516	pub fn insert(&mut self, mid: str0m::media::Mid, bridge: Box<dyn codec::Bridge>) {
517		self.inner.insert(mid, bridge);
518	}
519
520	pub fn push(&mut self, mid: str0m::media::Mid, frame: codec::Frame) -> Result<()> {
521		if let Some(bridge) = self.inner.get_mut(&mid) {
522			bridge.push(frame)?;
523		}
524		Ok(())
525	}
526}
527
528/// Build a [`Rtc`] with `CodecConfig` restricted to the supplied codecs.
529///
530/// Used by the two egress paths so we don't advertise codecs we have no
531/// source for in the catalog (WHIP client) or accept incoming codecs we
532/// can't fulfil (WHEP server). For both, the negotiated SDP intersects with
533/// what we can actually deliver, so `MediaAdded` only fires for codecs that
534/// [`crate::egress::EgressSource`] can match to a rendition.
535pub fn rtc_config_with_codecs(codecs: &[str0m::format::Codec]) -> str0m::RtcConfig {
536	use str0m::format::Codec;
537	// str0m fulfils NACK resends from the video send buffer (default 1000
538	// packets). MoQ has no PLI path back to the publisher, so a late joiner's
539	// recovery is whatever the peer can NACK out of this buffer while the current
540	// group is still in flight. Widen it so a large keyframe plus the rest of the
541	// group stays recoverable instead of aging out after ~1000 packets.
542	let mut config = str0m::RtcConfig::new()
543		.clear_codecs()
544		.set_send_buffer_video(EGRESS_SEND_BUFFER_VIDEO);
545	for c in codecs {
546		config = match c {
547			Codec::Opus => config.enable_opus(true),
548			Codec::H264 => config.enable_h264(true),
549			Codec::H265 => config.enable_h265(true),
550			Codec::Vp8 => config.enable_vp8(true),
551			Codec::Vp9 => config.enable_vp9(true),
552			Codec::Av1 => config.enable_av1(true),
553			// Any other codec str0m grows is one we have no egress source for.
554			_ => config,
555		};
556	}
557	config
558}
559
560/// Build a codec-restricted [`Rtc`] for the client egress path (which lets
561/// str0m mint its own ICE credentials). The server egress path uses
562/// [`rtc_config_with_codecs`] directly so it can inject the mux's known
563/// credentials before building.
564pub fn rtc_with_codecs(codecs: &[str0m::format::Codec]) -> Rtc {
565	rtc_config_with_codecs(codecs).build(std::time::Instant::now())
566}
567
568/// Bind an ephemeral UDP socket for a single client session and return it
569/// (shared with its [reader task](spawn_socket_reader)) plus the ICE candidates
570/// to advertise.
571///
572/// The client paths are 1:1 (one socket per dialed session, no demux); the
573/// server paths share one socket via `crate::server::mux` instead. `advertise`
574/// IPs are used verbatim (reusing the bound port); empty falls back to the
575/// bound address, substituting loopback when that address is unspecified.
576pub async fn bind_udp(advertise: &[SocketAddr]) -> Result<(Arc<UdpSocket>, Vec<SocketAddr>)> {
577	let socket = UdpSocket::bind(("0.0.0.0", 0)).await?;
578	let local = socket.local_addr()?;
579	let candidates = advertised_candidates(advertise, local)?;
580	Ok((Arc::new(socket), candidates))
581}
582
583/// Pair configured ICE candidates with the bound UDP port and validate them.
584pub(crate) fn advertised_candidates(advertise: &[SocketAddr], local: SocketAddr) -> Result<Vec<SocketAddr>> {
585	let port = local.port();
586	let candidates = if advertise.is_empty() {
587		let ip = match local.ip() {
588			IpAddr::V4(ip) if ip.is_unspecified() => IpAddr::V4(Ipv4Addr::LOCALHOST),
589			IpAddr::V6(ip) if ip.is_unspecified() => IpAddr::V6(Ipv6Addr::LOCALHOST),
590			ip => ip,
591		};
592
593		let candidate = SocketAddr::new(ip, port);
594		if candidate != local {
595			tracing::info!(bound = %local, advertised = %candidate, "webrtc udp bind is unspecified, advertising loopback ICE candidate");
596		}
597		vec![candidate]
598	} else {
599		// Reuse the bound port across each advertised IP, since str0m's ICE agent
600		// picks the destination port from the candidate it's pairing against.
601		advertise.iter().map(|addr| SocketAddr::new(addr.ip(), port)).collect()
602	};
603
604	for addr in &candidates {
605		Candidate::host(*addr, "udp").map_err(str0m::RtcError::from)?;
606	}
607	Ok(candidates)
608}
609
610/// Spawn a 1:1 reader pumping every datagram from `socket` into a channel, for
611/// the client paths (one socket per session, so no demux is needed). Mirrors the
612/// inbound side of `crate::server::mux` for a single session.
613pub fn spawn_socket_reader(socket: Arc<UdpSocket>) -> mpsc::Receiver<Packet> {
614	let (tx, rx) = mpsc::channel(SESSION_INBOX);
615	tokio::spawn(async move {
616		let mut buf = vec![0u8; 65_535];
617		loop {
618			match socket.recv_from(&mut buf).await {
619				// Bounded like a socket buffer: drop on full, stop once the
620				// session's receiver is gone.
621				Ok((len, src)) => {
622					let src = crate::net::canonical(src);
623					if let Err(mpsc::error::TrySendError::Closed(_)) = tx.try_send((buf[..len].to_vec(), src)) {
624						break;
625					}
626				}
627				Err(err) => {
628					tracing::warn!(%err, "webrtc client socket recv failed");
629					break;
630				}
631			}
632		}
633	});
634	rx
635}
636
637#[cfg(test)]
638mod tests {
639	use std::time::{Duration, UNIX_EPOCH};
640
641	use str0m::media::Mid;
642	use str0m::rtp::Ssrc;
643	use str0m::rtp::rtcp::SenderInfo;
644
645	use super::*;
646
647	#[test]
648	fn advertised_candidates_use_loopback_for_unspecified_ipv4() {
649		let local: SocketAddr = "0.0.0.0:4444".parse().unwrap();
650		let candidates = advertised_candidates(&[], local).unwrap();
651		assert_eq!(candidates, vec!["127.0.0.1:4444".parse().unwrap()]);
652	}
653
654	#[test]
655	fn advertised_candidates_use_loopback_for_unspecified_ipv6() {
656		let local: SocketAddr = "[::]:4444".parse().unwrap();
657		let candidates = advertised_candidates(&[], local).unwrap();
658		assert_eq!(candidates, vec!["[::1]:4444".parse().unwrap()]);
659	}
660
661	#[test]
662	fn advertised_candidates_keep_bound_address_when_specific() {
663		let local: SocketAddr = "127.0.0.1:4444".parse().unwrap();
664		assert_eq!(advertised_candidates(&[], local).unwrap(), vec![local]);
665	}
666
667	#[test]
668	fn advertised_candidates_reuse_bound_port_for_configured_addresses() {
669		let local: SocketAddr = "0.0.0.0:4444".parse().unwrap();
670		let advertised = vec!["127.0.0.1:1000".parse().unwrap(), "[::1]:2000".parse().unwrap()];
671
672		assert_eq!(
673			advertised_candidates(&advertised, local).unwrap(),
674			vec!["127.0.0.1:4444".parse().unwrap(), "[::1]:4444".parse().unwrap()]
675		);
676	}
677
678	#[test]
679	fn advertised_candidates_reject_configured_unspecified_addresses() {
680		let local: SocketAddr = "127.0.0.1:4444".parse().unwrap();
681		let advertised = vec!["0.0.0.0:1000".parse().unwrap()];
682		assert!(advertised_candidates(&advertised, local).is_err());
683	}
684
685	#[test]
686	fn pick_local_matches_address_family() {
687		let v4: SocketAddr = "1.2.3.4:5000".parse().unwrap();
688		let v6: SocketAddr = "[2001:db8::1]:5000".parse().unwrap();
689		let locals = vec![v4, v6];
690		let src_v4: SocketAddr = "9.9.9.9:1".parse().unwrap();
691		let src_v6: SocketAddr = "[2001:db8::2]:1".parse().unwrap();
692		assert_eq!(pick_local(&locals, src_v4), v4);
693		assert_eq!(pick_local(&locals, src_v6), v6);
694		// No same-family candidate falls back to the first.
695		assert_eq!(pick_local(&[v4], src_v6), v4);
696	}
697
698	#[test]
699	fn ingest_clock_rebases_first_frame_to_zero() {
700		let mut clock = IngestClock::default();
701		let mid = Mid::from("0");
702		let t0 = Instant::now();
703		// Raw RTP base is a large random value; the first frame must map to 0.
704		assert_eq!(clock.normalize(mid, t0, 5_000_000_000), 0);
705	}
706
707	#[test]
708	fn ingest_clock_tracks_rtp_delta_within_track() {
709		let mut clock = IngestClock::default();
710		let mid = Mid::from("0");
711		let t0 = Instant::now();
712		assert_eq!(clock.normalize(mid, t0, 5_000_000_000), 0);
713		// A later frame advances by the RTP delta, not by arrival jitter.
714		let arrival = t0 + Duration::from_millis(17); // jittered arrival, ignored after anchor
715		assert_eq!(clock.normalize(mid, arrival, 5_000_020_000), 20_000);
716	}
717
718	#[test]
719	fn ingest_clock_keeps_tracks_in_sync_via_arrival() {
720		let mut clock = IngestClock::default();
721		let audio = Mid::from("0");
722		let video = Mid::from("1");
723		let t0 = Instant::now();
724		// Audio anchors the session at 0 with its own random RTP base.
725		assert_eq!(clock.normalize(audio, t0, 1_000_000_000), 0);
726		// Video's first frame arrives 5 ms later with an unrelated RTP base; it
727		// must land at +5 ms on the shared timeline, not at video's raw base.
728		let video_arrival = t0 + Duration::from_millis(5);
729		assert_eq!(clock.normalize(video, video_arrival, 8_000_000_000), 5_000);
730		// And then track its own RTP delta.
731		assert_eq!(
732			clock.normalize(video, video_arrival + Duration::from_millis(33), 8_000_033_000),
733			38_000
734		);
735	}
736
737	#[test]
738	fn ingest_clock_handles_track_arriving_before_epoch() {
739		let mut clock = IngestClock::default();
740		let audio = Mid::from("0");
741		let video = Mid::from("1");
742		let t0 = Instant::now();
743		// Audio's MediaData is dequeued first and sets the epoch at t0.
744		assert_eq!(clock.normalize(audio, t0, 1_000_000), 0);
745		// Video's first frame actually arrived 5 ms *before* the epoch. Its lead
746		// pulls the start below zero (clamped to 0), and a frame 33 ms into video
747		// lands 28 ms onto the shared timeline (33 ms - the 5 ms head start).
748		let video_arrival = t0 - Duration::from_millis(5);
749		assert_eq!(clock.normalize(video, video_arrival, 8_000_000), 0);
750		assert_eq!(
751			clock.normalize(video, video_arrival + Duration::from_millis(33), 8_033_000),
752			28_000
753		);
754	}
755
756	#[test]
757	fn ingest_clock_replaces_arrival_jitter_with_sender_report_sync() {
758		let mut clock = IngestClock::default();
759		let audio = Mid::from("0");
760		let video = Mid::from("1");
761		let t0 = Instant::now();
762		let audio_base = 1_000_000_000;
763		let video_base = 8_000_000_000;
764
765		assert_eq!(clock.normalize(audio, t0, audio_base), 0);
766		assert_eq!(
767			clock.normalize(video, t0 + Duration::from_millis(50), video_base),
768			50_000
769		);
770		assert_eq!(
771			clock.normalize(audio, t0 + Duration::from_secs(1), audio_base + 1_000_000),
772			1_000_000
773		);
774		assert_eq!(
775			clock.normalize(video, t0 + Duration::from_millis(1_050), video_base + 1_000_000,),
776			1_050_000
777		);
778
779		// Both reports identify the same capture instant despite unrelated RTP
780		// bases. The 50 ms first-packet arrival skew must disappear permanently.
781		let report_time = UNIX_EPOCH + Duration::from_secs(1_700_000_001);
782		clock.observe(audio, sender_info(1, report_time, audio_base + 1_000_000));
783		clock.observe(video, sender_info(2, report_time, video_base + 1_000_000));
784
785		let audio_time = clock.normalize(audio, t0 + Duration::from_millis(1_020), audio_base + 1_020_000);
786		let video_time = clock.normalize(video, t0 + Duration::from_millis(1_070), video_base + 1_020_000);
787		assert_eq!(audio_time, video_time);
788		assert_eq!(audio_time, 1_070_000);
789	}
790
791	fn sender_info(ssrc: u32, ntp_time: SystemTime, rtp_us: u64) -> SenderInfo {
792		SenderInfo {
793			ssrc: Ssrc::from(ssrc),
794			ntp_time,
795			rtp_time: str0m::media::MediaTime::from_micros(rtp_us),
796			sender_packet_count: 0,
797			sender_octet_count: 0,
798		}
799	}
800}