From 73ca79626e57d34fe4732a695ca5cd45098ee378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 25 Nov 2026 14:54:27 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20back=20off=20share=20bitrat?= =?UTF-8?q?e=20when=20the=20sharer=20uplink=20congests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/client/Cargo.toml | 32 ++++----- crates/client/src/share/congestion.rs | 93 +++++++++++++++++++++++++++ crates/client/src/share/encode.rs | 31 ++++++++- crates/client/src/share/mod.rs | 1 + crates/client/src/share/session.rs | 24 +++++++ 5 files changed, 163 insertions(+), 18 deletions(-) create mode 100644 crates/client/src/share/congestion.rs diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 1285c07..6f21b48 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -11,22 +11,22 @@ tokio.workspace = true quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] } gpui-kit = { version = "0.11.0", features = ["syntect"] } -ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } -ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } +ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } +ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } ureq = "3.5.2" bytes = "1.12.2" tracing = "0.1.44" diff --git a/crates/client/src/share/congestion.rs b/crates/client/src/share/congestion.rs new file mode 100644 index 0000000..b915c8e --- /dev/null +++ b/crates/client/src/share/congestion.rs @@ -0,0 +1,93 @@ +use std::time::Duration; + +use quinn::Connection; + +pub const PERIOD: Duration = Duration::from_millis(500); +const MIN_SCALE: f32 = 0.25; +const DECREASE: f32 = 0.75; +const INCREASE: f32 = 0.05; +const LOSS_LIMIT: f32 = 0.02; +const QUEUE_DELAY: Duration = Duration::from_millis(60); +const BUFFER_LOW: usize = 256 * 1024; + +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct Uplink { + pub rtt: Duration, + pub sent: u64, + pub lost: u64, + pub congestion_events: u64, + pub buffer_low: bool, +} + +impl Uplink { + pub fn read(connection: &Connection) -> Self { + let path = connection.stats().path; + Self { + rtt: connection.rtt(), + sent: path.sent_packets, + lost: path.lost_packets, + congestion_events: path.congestion_events, + buffer_low: connection.datagram_send_buffer_space() < BUFFER_LOW, + } + } +} + +pub struct Controller { + scale: f32, + base_rtt: Option, + last: Uplink, +} + +impl Default for Controller { + fn default() -> Self { + Self { + scale: 1.0, + base_rtt: None, + last: Uplink::default(), + } + } +} + +impl Controller { + pub fn observe(&mut self, now: Uplink) -> f32 { + let base = self.base_rtt.map_or(now.rtt, |base| base.min(now.rtt)); + self.base_rtt = Some(base); + let sent = now.sent.saturating_sub(self.last.sent); + let lost = now.lost.saturating_sub(self.last.lost); + let events = now + .congestion_events + .saturating_sub(self.last.congestion_events); + self.last = now; + let congested = events > 0 + || now.buffer_low + || lost as f32 > sent.max(1) as f32 * LOSS_LIMIT + || now.rtt > base + QUEUE_DELAY; + self.scale = match congested { + true => (self.scale * DECREASE).max(MIN_SCALE), + false => (self.scale + INCREASE).min(1.0), + }; + self.scale + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn backs_off_then_recovers() { + let mut controller = Controller::default(); + let mut uplink = Uplink { + rtt: Duration::from_millis(20), + ..Uplink::default() + }; + assert_eq!(controller.observe(uplink), 1.0); + uplink.rtt = Duration::from_millis(200); + uplink.sent = 100; + uplink.lost = 10; + assert_eq!(controller.observe(uplink), DECREASE); + uplink.rtt = Duration::from_millis(20); + uplink.sent = 200; + assert!(controller.observe(uplink) > DECREASE); + } +} diff --git a/crates/client/src/share/encode.rs b/crates/client/src/share/encode.rs index 4a3f6c3..cb2b367 100644 --- a/crates/client/src/share/encode.rs +++ b/crates/client/src/share/encode.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::mpsc::{Receiver, Sender, SyncSender, sync_channel}; use std::sync::{Arc, OnceLock}; use std::thread; @@ -99,6 +99,12 @@ impl Backend { } } + fn tune(&mut self, bitrate: u32, fps: u32) { + if let Self::Hardware(encoder) = self { + encoder.tune(bitrate, fps); + } + } + fn engine(&self) -> Engine { match self { Self::Hardware(_) => Engine::Hardware, @@ -145,6 +151,13 @@ mod hardware { pub struct Encoder(ec_hw::HwEncoder); impl Encoder { + pub fn tune(&mut self, bitrate: u32, fps: u32) { + report(self.0.tune(ec_hw::Tunings { + bitrate, + framerate: (fps, 1), + })); + } + pub fn encode( &mut self, frame: &Frame, @@ -228,11 +241,13 @@ fn run( settings: Settings, input: Receiver>, request: Arc, + scale: Arc, output: Sender, ) { let Some(mut backend) = Backend::open(&settings) else { return; }; + let mut applied = 1.0; let interval = 1000 / settings.fps; let mut last: Option = None; for frame in input { @@ -240,6 +255,11 @@ fn run( continue; } last = Some(frame.timestamp_ms); + let wanted = f32::from_bits(scale.load(Ordering::Relaxed)); + if wanted != applied { + applied = wanted; + backend.tune((settings.bitrate as f32 * wanted) as u32, settings.fps); + } let force = request.swap(false, Ordering::Relaxed); let Some((data, keyframe)) = backend.encode(&frame, &settings, force) else { continue; @@ -267,6 +287,7 @@ pub struct Encoders { source_height: u32, output: Sender, lanes: HashMap, + scale: Arc, } impl Encoders { @@ -276,6 +297,7 @@ impl Encoders { source_height, output, lanes: HashMap::new(), + scale: Arc::new(AtomicU32::new(1f32.to_bits())), } } @@ -295,6 +317,10 @@ impl Encoders { } } + pub fn set_scale(&self, scale: f32) { + self.scale.store(scale.to_bits(), Ordering::Relaxed); + } + pub fn push(&self, frame: Arc) { for lane in self.lanes.values() { let _ = lane.input.try_send(Arc::clone(&frame)); @@ -319,9 +345,10 @@ impl Encoders { let request = Arc::new(AtomicBool::new(false)); let thread_request = Arc::clone(&request); let output = self.output.clone(); + let scale = Arc::clone(&self.scale); let spawned = thread::Builder::new() .name("share-encode".into()) - .spawn(move || run(layer, settings, receiver, thread_request, output)); + .spawn(move || run(layer, settings, receiver, thread_request, scale, output)); if let Err(error) = spawned { tracing::warn!(?error, "share encode thread failed to start"); } diff --git a/crates/client/src/share/mod.rs b/crates/client/src/share/mod.rs index 6310801..bb04c08 100644 --- a/crates/client/src/share/mod.rs +++ b/crates/client/src/share/mod.rs @@ -9,6 +9,7 @@ pub mod capture; #[cfg(target_os = "windows")] #[path = "capture_windows.rs"] pub mod capture; +pub mod congestion; pub mod convert; pub mod encode; #[cfg(any(target_os = "linux", target_os = "windows"))] diff --git a/crates/client/src/share/session.rs b/crates/client/src/share/session.rs index 3b31732..b565a96 100644 --- a/crates/client/src/share/session.rs +++ b/crates/client/src/share/session.rs @@ -27,6 +27,7 @@ use super::{ audio::AudioMode, audio_capture::{self, AudioCapture}, capture::{self, Capture}, + congestion::{self, Controller, Uplink}, encode::{self, Encoders}, frame::{Encoded, Frame}, reassembly::{Assembled, Reassembler}, @@ -160,6 +161,7 @@ pub struct Sharing { _audio_encode: Option, meter: Meter, _sender: Worker, + _congestion: Worker, sent: Arc>, } @@ -257,6 +259,12 @@ impl Sharing { since: Instant::now(), tallies: HashMap::new(), })); + let congestion = { + let (connection, encoders) = (connection.clone(), encoders.clone()); + Worker::spawn("share-congestion", move |stop| { + control(&connection, &encoders, stop) + }) + }; let sender = { let sent = sent.clone(); Worker::spawn("share-send", move |stop| { @@ -277,6 +285,7 @@ impl Sharing { _audio_encode: audio_encode, meter, _sender: sender, + _congestion: congestion, sent, }, source, @@ -350,6 +359,21 @@ fn feed( } } +fn control(connection: &Connection, encoders: &Mutex, stop: &AtomicBool) { + let mut controller = Controller::default(); + let mut scale = 1.0; + while wait(stop, congestion::PERIOD) { + let next = controller.observe(Uplink::read(connection)); + if next < scale { + tracing::info!(scale = next, "share uplink congested"); + } + if next != scale { + scale = next; + lock(encoders).set_scale(scale); + } + } +} + fn send(rx: Receiver, connection: Connection, sent: Arc>, stop: &AtomicBool) { let mut counters: HashMap = HashMap::new(); while let Some(item) = next(&rx, stop) {