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