1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//! Test-only affordances for `Registry`.
//!
//! Gated with `cfg(any(test, feature = "test-utils"))` so the release build is
//! lean and these observer APIs cannot be abused in production code.
#[cfg(feature = "active-speaker")]
use std::time::Instant;
use super::Registry;
use crate::fanout::fanout;
use crate::ids::SfuRid;
use crate::propagate::Propagated;
impl Registry {
/// Run `fanout` against the registry's own client list.
///
/// Useful when clients are already inserted (cross-advertisement in effect)
/// and you only want to observe fanout behaviour from that point.
#[doc(hidden)]
pub fn fanout_for_tests(&mut self, p: &Propagated) {
fanout(p, &mut self.clients);
}
/// Read a client's delivered-media counter by index.
#[doc(hidden)]
pub fn delivered_media_count(&self, idx: usize) -> u64 {
self.clients[idx].delivered_media_count()
}
/// Read a client's delivered-active-speaker counter by index.
///
/// Used to verify skip-self semantics on `ActiveSpeakerChanged` fanout.
#[doc(hidden)]
#[cfg(feature = "active-speaker")]
pub fn delivered_active_speaker_count(&self, idx: usize) -> u64 {
self.clients[idx].delivered_active_speaker_count()
}
/// Flip a client's desired simulcast layer by index.
#[doc(hidden)]
pub fn set_desired_layer_for_tests(&mut self, idx: usize, rid: SfuRid) {
self.clients[idx].set_desired_layer(rid);
}
/// Inject an audio level into the dominant-speaker detector, bypassing
/// wire-level RFC 6464 parsing. Delegates to the public `record_level` API.
#[doc(hidden)]
#[cfg(feature = "active-speaker")]
pub fn inject_audio_level_for_tests(&mut self, peer_id: u64, level: u8, now: Instant) {
// Mirror the production guard: relay clients are excluded from the detector.
if self
.clients
.iter()
.any(|c| *c.id == peer_id && c.is_relay())
{
return;
}
let now_ms = now
.saturating_duration_since(self.detector_epoch)
.as_millis() as u64;
self.detector.record_level(peer_id, level, now_ms);
}
/// Force an ASO tick and drain any fanout the detector queued.
///
/// Returns the peer id if dominance changed on this tick.
#[doc(hidden)]
#[cfg(feature = "active-speaker")]
pub fn force_active_speaker_tick_for_tests(&mut self, now: Instant) -> Option<u64> {
let now_ms = now
.saturating_duration_since(self.detector_epoch)
.as_millis() as u64;
let changed = self.detector.tick(now_ms);
if let Some(ref change) = changed {
self.metrics.inc_dominant_speaker_changes();
self.to_propagate
.push_back(Propagated::ActiveSpeakerChanged {
peer_id: change.peer_id,
confidence: change.c2_margin,
});
}
self.fanout_pending();
changed.map(|c| c.peer_id)
}
/// Read the detector's current dominant peer.
#[doc(hidden)]
#[cfg(feature = "active-speaker")]
pub fn current_active_speaker(&self) -> Option<u64> {
self.detector.current_dominant().copied()
}
/// Force-disconnect a client by id so the next `reap_dead` pass drops it.
#[doc(hidden)]
pub fn disconnect_client_for_tests(&mut self, id: crate::propagate::ClientId) {
if let Some(client) = self.clients.iter_mut().find(|c| c.id == id) {
client.disconnect_for_tests();
}
}
/// Invoke `reap_dead` out-of-band.
#[doc(hidden)]
pub fn reap_dead_for_tests(&mut self) {
self.reap_dead();
}
/// Reap per-peer metric label series for a given peer id, simulating a
/// disconnect without running the full lifecycle reap.
#[doc(hidden)]
pub fn reap_dead_peer_for_tests(&mut self, peer_id: crate::propagate::ClientId) {
self.metrics.reap_dead_peer(*peer_id);
}
/// Wire subscriber at `sub_idx` to publisher at `pub_idx` for the track tagged
/// with `mid_tag` - forcing the track_out into Open state.
///
/// Must be called after both clients are inserted.
#[doc(hidden)]
pub fn wire_track_for_tests(&mut self, sub_idx: usize, pub_idx: usize, mid_tag: u8) {
use str0m::media::Mid;
let mid = Mid::from(&*format!("m{mid_tag}"));
let track_arc = self.clients[pub_idx]
.tracks_in
.iter()
.find(|e| e.id.mid == mid)
.map(|e| e.id.clone())
.expect("publisher track not found");
self.clients[sub_idx].handle_track_open(std::sync::Arc::downgrade(&track_arc));
for track_out in self.clients[sub_idx].tracks_out.iter_mut() {
if track_out.track_in.upgrade().as_deref().map(|t| t.mid) == Some(mid) {
track_out.state = crate::client::tracks::TrackOutState::Open(mid);
return;
}
}
}
/// Drain the registry's propagation queue and return all events.
///
/// Used to inspect what `emit_publisher_layer_hints` produces without
/// running `fanout_pending`.
#[doc(hidden)]
pub fn drain_propagated_for_tests(&mut self) -> Vec<crate::propagate::Propagated> {
self.to_propagate.drain(..).collect()
}
/// Mutable access to the clients slice — for tests that need to call
/// per-client methods (like `incoming_keyframe_req_for_tests`) without
/// running the full poll loop.
#[doc(hidden)]
pub fn clients_mut_for_tests(&mut self) -> &mut [crate::client::Client] {
&mut self.clients
}
/// Drive a subscriber's pacer directly --- for tests that cannot simulate TWCC.
#[cfg(all(any(test, feature = "test-utils"), feature = "pacer"))]
#[doc(hidden)]
pub fn drive_pacer_for_tests(&mut self, peer_id: crate::propagate::ClientId, bps: u64) {
use crate::bwe::PacerAction;
if let Some(client) = self.clients.iter_mut().find(|c| c.id == peer_id) {
match client.drive_pacer(bps) {
PacerAction::GoAudioOnly => {
self.to_propagate.push_back(Propagated::AudioOnlyMode {
peer_id,
audio_only: true,
});
}
PacerAction::RestoreVideo => {
self.to_propagate.push_back(Propagated::AudioOnlyMode {
peer_id,
audio_only: false,
});
}
PacerAction::SuspendVideo => {
client.set_suspended(true);
self.metrics.inc_suspend_video("enter");
self.to_propagate.push_back(Propagated::SuspendVideo {
peer_id,
suspended: true,
});
}
PacerAction::RestoreAudio => {
client.set_suspended(false);
self.metrics.inc_suspend_video("exit");
self.to_propagate.push_back(Propagated::SuspendVideo {
peer_id,
suspended: false,
});
}
PacerAction::ChangeLayer(_) | PacerAction::NoChange => {}
}
}
}
/// Encode the registry's Prometheus metrics as text format 0.0.4.
///
/// Useful for integration tests that assert counter values without running
/// a real HTTP server. Mirrors the `bind_metrics_server` + `scrape` pattern
/// from `tests/metrics_integration.rs` but without the network round-trip.
#[cfg(all(any(test, feature = "test-utils"), feature = "metrics-prometheus"))]
#[doc(hidden)]
pub fn scrape_metrics_for_tests(&self) -> String {
self.metrics
.encode_text()
.expect("scrape_metrics_for_tests: encode failed")
}
/// Mutable access to the `BandwidthEstimator` — for tests that need to inject
/// native estimates directly without simulating TWCC packets.
#[cfg(all(any(test, feature = "test-utils"), feature = "kalman-bwe"))]
#[doc(hidden)]
pub fn bandwidth_mut_for_tests(&mut self) -> &mut crate::bwe::estimator::BandwidthEstimator {
&mut self.bandwidth
}
}