[fix](trx-server): pause audio capture when no clients are connected
Instead of running the cpal input stream continuously, start it paused and only activate when broadcast subscribers are present. When the last client disconnects, pause the stream and sleep at 100ms intervals polling for new receivers. This eliminates idle CPU usage from continuous CoreAudio callbacks, channel allocations, and sample processing when nobody is listening. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Stanislaw Grams <stanislawgrams@gmail.com>
This commit is contained in:
@@ -97,13 +97,34 @@ fn run_capture(
|
||||
None,
|
||||
)?;
|
||||
|
||||
stream.play()?;
|
||||
info!("Audio capture: started ({}Hz, {} ch, {}ms frames)", sample_rate, channels, frame_duration_ms);
|
||||
// Start paused — only capture when clients are connected
|
||||
info!("Audio capture: ready ({}Hz, {} ch, {}ms frames)", sample_rate, channels, frame_duration_ms);
|
||||
|
||||
let mut pcm_buf: Vec<f32> = Vec::with_capacity(frame_samples * 2);
|
||||
let mut opus_buf = vec![0u8; 4096];
|
||||
let mut capturing = false;
|
||||
|
||||
loop {
|
||||
let has_receivers = tx.receiver_count() > 0;
|
||||
|
||||
if has_receivers && !capturing {
|
||||
let _ = stream.play();
|
||||
capturing = true;
|
||||
info!("Audio capture: started");
|
||||
} else if !has_receivers && capturing {
|
||||
let _ = stream.pause();
|
||||
capturing = false;
|
||||
pcm_buf.clear();
|
||||
// Drain any buffered samples
|
||||
while sample_rx.try_recv().is_ok() {}
|
||||
info!("Audio capture: paused (no listeners)");
|
||||
}
|
||||
|
||||
if !capturing {
|
||||
std::thread::sleep(std::time::Duration::from_millis(100));
|
||||
continue;
|
||||
}
|
||||
|
||||
match sample_rx.recv() {
|
||||
Ok(samples) => {
|
||||
pcm_buf.extend_from_slice(&samples);
|
||||
|
||||
Reference in New Issue
Block a user