[feat](trx-rs): persistent multi-channel virtual channels with OOB eviction

Allow users to allocate multiple virtual channels independently of
browser tab count. Channels survive SDR center-frequency retuning as
long as they stay within the capture bandwidth; channels that fall
outside the SDR span are automatically destroyed.

Changes:
- trx-core: add AUDIO_MSG_VCHAN_DESTROYED (0x12) wire constant;
  add default subscribe_destroyed() to VirtualChannelManager trait
- trx-backend-soapysdr: update_center_hz() detects OOB channels,
  removes them, fires destroyed_tx broadcast; add destroyed_sender()
  and subscribe_destroyed() override
- trx-server/audio: recv_destroyed() helper avoids select! busy-loop
  for non-SDR backends; send AUDIO_MSG_VCHAN_DESTROYED to client when
  a channel is evicted server-side
- trx-client/audio_client: persist active_subs across TCP reconnects,
  re-subscribe on reconnect; handle AUDIO_MSG_VCHAN_DESTROYED by
  pruning vchan_audio map and forwarding UUID via vchan_destroyed_tx
- trx-frontend/lib: add vchan_destroyed broadcast field to
  FrontendRuntimeContext
- trx-client/main: wire vchan_destroyed_tx into audio client and
  frontend runtime context
- trx-frontend-http/vchan: remove per-session one-channel limit in
  allocate(); replace auto-evict in release_session_on_rig() with
  subscriber-count-only update; add remove_by_uuid() for server-
  triggered OOB destruction (skips redundant VChanAudioCmd::Remove)
- trx-frontend-http/server: spawn background task that forwards
  vchan_destroyed broadcast to ClientChannelManager.remove_by_uuid()

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Stan Grams <sjg@haxx.space>
This commit is contained in:
2026-03-11 20:50:49 +01:00
parent 4e93dcc82a
commit 60267d450b
9 changed files with 215 additions and 45 deletions
@@ -87,6 +87,8 @@ pub struct SdrVirtualChannelManager {
/// Maximum total channels including the primary (enforced on `add_channel`).
max_total: usize,
channels: RwLock<Vec<ManagedChannel>>,
/// Fires whenever a channel is destroyed (e.g. went out of SDR bandwidth).
destroyed_tx: broadcast::Sender<Uuid>,
}
impl SdrVirtualChannelManager {
@@ -124,15 +126,22 @@ impl SdrVirtualChannelManager {
permanent: true,
};
let (destroyed_tx, _) = broadcast::channel::<Uuid>(16);
Self {
center_hz: pipeline.shared_center_hz.clone(),
pipeline,
fixed_slot_count,
max_total: max_total.max(1),
channels: RwLock::new(vec![primary]),
destroyed_tx,
}
}
pub fn destroyed_sender(&self) -> broadcast::Sender<Uuid> {
self.destroyed_tx.clone()
}
fn half_span_hz(&self) -> i64 {
i64::from(self.pipeline.sdr_sample_rate) / 2
}
@@ -141,16 +150,28 @@ impl SdrVirtualChannelManager {
/// Recomputes the IF offset for every virtual channel.
pub fn update_center_hz(&self, new_center_hz: i64) {
self.center_hz.store(new_center_hz, Ordering::Relaxed);
let channels = self.channels.read().unwrap();
let dsps = self.pipeline.channel_dsps.read().unwrap();
for ch in channels.iter().filter(|c| !c.permanent) {
let new_if_hz = ch.freq_hz as i64 - new_center_hz;
if let Some(dsp_arc) = dsps.get(ch.pipeline_slot) {
dsp_arc
.lock()
.unwrap()
.set_channel_if_hz(new_if_hz as f64);
let half_span = self.half_span_hz();
// Single pass under read lock: update in-band IF offsets and collect OOB IDs.
let oob_ids: Vec<Uuid> = {
let channels = self.channels.read().unwrap();
let dsps = self.pipeline.channel_dsps.read().unwrap();
let mut oob = Vec::new();
for ch in channels.iter().filter(|c| !c.permanent) {
let new_if_hz = ch.freq_hz as i64 - new_center_hz;
if new_if_hz.unsigned_abs() as i64 > half_span {
oob.push(ch.id);
} else if let Some(dsp_arc) = dsps.get(ch.pipeline_slot) {
dsp_arc.lock().unwrap().set_channel_if_hz(new_if_hz as f64);
}
}
oob
}; // read locks released here
// Destroy OOB channels and notify subscribers.
for id in oob_ids {
let _ = self.remove_channel(id); // acquires write lock internally
let _ = self.destroyed_tx.send(id);
}
}
@@ -301,6 +322,10 @@ impl VirtualChannelManager for SdrVirtualChannelManager {
self.max_total
}
fn subscribe_destroyed(&self) -> broadcast::Receiver<Uuid> {
self.destroyed_tx.subscribe()
}
fn ensure_channel_pcm(
&self,
id: Uuid,