Back off share bitrate when the sharer uplink congests
Browse files at this commit73ca79626e57d34fe4732a695ca5cd45098ee378Parent: 5fe7a34- feat
- back off share bitrate when the sharer uplink congests
5 changed files +163 -18
- crates/client/Cargo.toml +16 -16
- crates/client/src/share/congestion.rs +93 -0
- crates/client/src/share/encode.rs +29 -2
- crates/client/src/share/mod.rs +1 -0
- crates/client/src/share/session.rs +24 -0
crates/client/Cargo.tomlmodified+16 -16
| @@ -11,22 +11,22 @@ tokio.workspace = true | |||
| 11 | quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] } | 11 | quinn = { workspace = true, features = ["rustls-ring", "platform-verifier"] } |
| 12 | 12 | ||
| 13 | gpui-kit = { version = "0.11.0", features = ["syntect"] } | 13 | gpui-kit = { version = "0.11.0", features = ["syntect"] } |
| 14 | -ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 14 | +ec-opus = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 15 | -ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 15 | +ec-core = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 16 | -ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 16 | +ec-probe = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 17 | -ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 17 | +ec-mp4 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 18 | -ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 18 | +ec-matroska = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 19 | -ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 19 | +ec-ogg = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 20 | -ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 20 | +ec-h264 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 21 | -ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 21 | +ec-vp8 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 22 | -ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 22 | +ec-aac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 23 | -ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 23 | +ec-mp3 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 24 | -ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 24 | +ec-vorbis = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 25 | -ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 25 | +ec-flac = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 26 | -ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 26 | +ec-h265 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 27 | -ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 27 | +ec-hw = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 28 | -ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 28 | +ec-vp9 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 29 | -ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "e5afa4e865ec6b0eaac27ac9972c9f6247d76b24" } | 29 | +ec-av1 = { git = "https://git.tahinli.com/Tahinli/edith_codecs.git", rev = "8352fa1d201b85fa8ede43cc6268ec5163ed00b3" } |
| 30 | ureq = "3.5.2" | 30 | ureq = "3.5.2" |
| 31 | bytes = "1.12.2" | 31 | bytes = "1.12.2" |
| 32 | tracing = "0.1.44" | 32 | tracing = "0.1.44" |
crates/client/src/share/congestion.rsadded+93 -0
| @@ -0,0 +1,93 @@ | |||
| 1 | +use std::time::Duration; | ||
| 2 | + | ||
| 3 | +use quinn::Connection; | ||
| 4 | + | ||
| 5 | +pub const PERIOD: Duration = Duration::from_millis(500); | ||
| 6 | +const MIN_SCALE: f32 = 0.25; | ||
| 7 | +const DECREASE: f32 = 0.75; | ||
| 8 | +const INCREASE: f32 = 0.05; | ||
| 9 | +const LOSS_LIMIT: f32 = 0.02; | ||
| 10 | +const QUEUE_DELAY: Duration = Duration::from_millis(60); | ||
| 11 | +const BUFFER_LOW: usize = 256 * 1024; | ||
| 12 | + | ||
| 13 | +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] | ||
| 14 | +pub struct Uplink { | ||
| 15 | + pub rtt: Duration, | ||
| 16 | + pub sent: u64, | ||
| 17 | + pub lost: u64, | ||
| 18 | + pub congestion_events: u64, | ||
| 19 | + pub buffer_low: bool, | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +impl Uplink { | ||
| 23 | + pub fn read(connection: &Connection) -> Self { | ||
| 24 | + let path = connection.stats().path; | ||
| 25 | + Self { | ||
| 26 | + rtt: connection.rtt(), | ||
| 27 | + sent: path.sent_packets, | ||
| 28 | + lost: path.lost_packets, | ||
| 29 | + congestion_events: path.congestion_events, | ||
| 30 | + buffer_low: connection.datagram_send_buffer_space() < BUFFER_LOW, | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +pub struct Controller { | ||
| 36 | + scale: f32, | ||
| 37 | + base_rtt: Option<Duration>, | ||
| 38 | + last: Uplink, | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +impl Default for Controller { | ||
| 42 | + fn default() -> Self { | ||
| 43 | + Self { | ||
| 44 | + scale: 1.0, | ||
| 45 | + base_rtt: None, | ||
| 46 | + last: Uplink::default(), | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +impl Controller { | ||
| 52 | + pub fn observe(&mut self, now: Uplink) -> f32 { | ||
| 53 | + let base = self.base_rtt.map_or(now.rtt, |base| base.min(now.rtt)); | ||
| 54 | + self.base_rtt = Some(base); | ||
| 55 | + let sent = now.sent.saturating_sub(self.last.sent); | ||
| 56 | + let lost = now.lost.saturating_sub(self.last.lost); | ||
| 57 | + let events = now | ||
| 58 | + .congestion_events | ||
| 59 | + .saturating_sub(self.last.congestion_events); | ||
| 60 | + self.last = now; | ||
| 61 | + let congested = events > 0 | ||
| 62 | + || now.buffer_low | ||
| 63 | + || lost as f32 > sent.max(1) as f32 * LOSS_LIMIT | ||
| 64 | + || now.rtt > base + QUEUE_DELAY; | ||
| 65 | + self.scale = match congested { | ||
| 66 | + true => (self.scale * DECREASE).max(MIN_SCALE), | ||
| 67 | + false => (self.scale + INCREASE).min(1.0), | ||
| 68 | + }; | ||
| 69 | + self.scale | ||
| 70 | + } | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +#[cfg(test)] | ||
| 74 | +mod tests { | ||
| 75 | + use super::*; | ||
| 76 | + | ||
| 77 | + #[test] | ||
| 78 | + fn backs_off_then_recovers() { | ||
| 79 | + let mut controller = Controller::default(); | ||
| 80 | + let mut uplink = Uplink { | ||
| 81 | + rtt: Duration::from_millis(20), | ||
| 82 | + ..Uplink::default() | ||
| 83 | + }; | ||
| 84 | + assert_eq!(controller.observe(uplink), 1.0); | ||
| 85 | + uplink.rtt = Duration::from_millis(200); | ||
| 86 | + uplink.sent = 100; | ||
| 87 | + uplink.lost = 10; | ||
| 88 | + assert_eq!(controller.observe(uplink), DECREASE); | ||
| 89 | + uplink.rtt = Duration::from_millis(20); | ||
| 90 | + uplink.sent = 200; | ||
| 91 | + assert!(controller.observe(uplink) > DECREASE); | ||
| 92 | + } | ||
| 93 | +} | ||
crates/client/src/share/encode.rsmodified+29 -2
| @@ -1,5 +1,5 @@ | |||
| 1 | use std::collections::HashMap; | 1 | use std::collections::HashMap; |
| 2 | -use std::sync::atomic::{AtomicBool, Ordering}; | 2 | +use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; |
| 3 | use std::sync::mpsc::{Receiver, Sender, SyncSender, sync_channel}; | 3 | use std::sync::mpsc::{Receiver, Sender, SyncSender, sync_channel}; |
| 4 | use std::sync::{Arc, OnceLock}; | 4 | use std::sync::{Arc, OnceLock}; |
| 5 | use std::thread; | 5 | use std::thread; |
| @@ -99,6 +99,12 @@ impl Backend { | |||
| 99 | } | 99 | } |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | + fn tune(&mut self, bitrate: u32, fps: u32) { | ||
| 103 | + if let Self::Hardware(encoder) = self { | ||
| 104 | + encoder.tune(bitrate, fps); | ||
| 105 | + } | ||
| 106 | + } | ||
| 107 | + | ||
| 102 | fn engine(&self) -> Engine { | 108 | fn engine(&self) -> Engine { |
| 103 | match self { | 109 | match self { |
| 104 | Self::Hardware(_) => Engine::Hardware, | 110 | Self::Hardware(_) => Engine::Hardware, |
| @@ -145,6 +151,13 @@ mod hardware { | |||
| 145 | pub struct Encoder(ec_hw::HwEncoder); | 151 | pub struct Encoder(ec_hw::HwEncoder); |
| 146 | 152 | ||
| 147 | impl Encoder { | 153 | impl Encoder { |
| 154 | + pub fn tune(&mut self, bitrate: u32, fps: u32) { | ||
| 155 | + report(self.0.tune(ec_hw::Tunings { | ||
| 156 | + bitrate, | ||
| 157 | + framerate: (fps, 1), | ||
| 158 | + })); | ||
| 159 | + } | ||
| 160 | + | ||
| 148 | pub fn encode( | 161 | pub fn encode( |
| 149 | &mut self, | 162 | &mut self, |
| 150 | frame: &Frame, | 163 | frame: &Frame, |
| @@ -228,11 +241,13 @@ fn run( | |||
| 228 | settings: Settings, | 241 | settings: Settings, |
| 229 | input: Receiver<Arc<Frame>>, | 242 | input: Receiver<Arc<Frame>>, |
| 230 | request: Arc<AtomicBool>, | 243 | request: Arc<AtomicBool>, |
| 244 | + scale: Arc<AtomicU32>, | ||
| 231 | output: Sender<Encoded>, | 245 | output: Sender<Encoded>, |
| 232 | ) { | 246 | ) { |
| 233 | let Some(mut backend) = Backend::open(&settings) else { | 247 | let Some(mut backend) = Backend::open(&settings) else { |
| 234 | return; | 248 | return; |
| 235 | }; | 249 | }; |
| 250 | + let mut applied = 1.0; | ||
| 236 | let interval = 1000 / settings.fps; | 251 | let interval = 1000 / settings.fps; |
| 237 | let mut last: Option<u32> = None; | 252 | let mut last: Option<u32> = None; |
| 238 | for frame in input { | 253 | for frame in input { |
| @@ -240,6 +255,11 @@ fn run( | |||
| 240 | continue; | 255 | continue; |
| 241 | } | 256 | } |
| 242 | last = Some(frame.timestamp_ms); | 257 | last = Some(frame.timestamp_ms); |
| 258 | + let wanted = f32::from_bits(scale.load(Ordering::Relaxed)); | ||
| 259 | + if wanted != applied { | ||
| 260 | + applied = wanted; | ||
| 261 | + backend.tune((settings.bitrate as f32 * wanted) as u32, settings.fps); | ||
| 262 | + } | ||
| 243 | let force = request.swap(false, Ordering::Relaxed); | 263 | let force = request.swap(false, Ordering::Relaxed); |
| 244 | let Some((data, keyframe)) = backend.encode(&frame, &settings, force) else { | 264 | let Some((data, keyframe)) = backend.encode(&frame, &settings, force) else { |
| 245 | continue; | 265 | continue; |
| @@ -267,6 +287,7 @@ pub struct Encoders { | |||
| 267 | source_height: u32, | 287 | source_height: u32, |
| 268 | output: Sender<Encoded>, | 288 | output: Sender<Encoded>, |
| 269 | lanes: HashMap<Layer, Lane>, | 289 | lanes: HashMap<Layer, Lane>, |
| 290 | + scale: Arc<AtomicU32>, | ||
| 270 | } | 291 | } |
| 271 | 292 | ||
| 272 | impl Encoders { | 293 | impl Encoders { |
| @@ -276,6 +297,7 @@ impl Encoders { | |||
| 276 | source_height, | 297 | source_height, |
| 277 | output, | 298 | output, |
| 278 | lanes: HashMap::new(), | 299 | lanes: HashMap::new(), |
| 300 | + scale: Arc::new(AtomicU32::new(1f32.to_bits())), | ||
| 279 | } | 301 | } |
| 280 | } | 302 | } |
| 281 | 303 | ||
| @@ -295,6 +317,10 @@ impl Encoders { | |||
| 295 | } | 317 | } |
| 296 | } | 318 | } |
| 297 | 319 | ||
| 320 | + pub fn set_scale(&self, scale: f32) { | ||
| 321 | + self.scale.store(scale.to_bits(), Ordering::Relaxed); | ||
| 322 | + } | ||
| 323 | + | ||
| 298 | pub fn push(&self, frame: Arc<Frame>) { | 324 | pub fn push(&self, frame: Arc<Frame>) { |
| 299 | for lane in self.lanes.values() { | 325 | for lane in self.lanes.values() { |
| 300 | let _ = lane.input.try_send(Arc::clone(&frame)); | 326 | let _ = lane.input.try_send(Arc::clone(&frame)); |
| @@ -319,9 +345,10 @@ impl Encoders { | |||
| 319 | let request = Arc::new(AtomicBool::new(false)); | 345 | let request = Arc::new(AtomicBool::new(false)); |
| 320 | let thread_request = Arc::clone(&request); | 346 | let thread_request = Arc::clone(&request); |
| 321 | let output = self.output.clone(); | 347 | let output = self.output.clone(); |
| 348 | + let scale = Arc::clone(&self.scale); | ||
| 322 | let spawned = thread::Builder::new() | 349 | let spawned = thread::Builder::new() |
| 323 | .name("share-encode".into()) | 350 | .name("share-encode".into()) |
| 324 | - .spawn(move || run(layer, settings, receiver, thread_request, output)); | 351 | + .spawn(move || run(layer, settings, receiver, thread_request, scale, output)); |
| 325 | if let Err(error) = spawned { | 352 | if let Err(error) = spawned { |
| 326 | tracing::warn!(?error, "share encode thread failed to start"); | 353 | tracing::warn!(?error, "share encode thread failed to start"); |
| 327 | } | 354 | } |
crates/client/src/share/mod.rsmodified+1 -0
| @@ -9,6 +9,7 @@ pub mod capture; | |||
| 9 | #[cfg(target_os = "windows")] | 9 | #[cfg(target_os = "windows")] |
| 10 | #[path = "capture_windows.rs"] | 10 | #[path = "capture_windows.rs"] |
| 11 | pub mod capture; | 11 | pub mod capture; |
| 12 | +pub mod congestion; | ||
| 12 | pub mod convert; | 13 | pub mod convert; |
| 13 | pub mod encode; | 14 | pub mod encode; |
| 14 | #[cfg(any(target_os = "linux", target_os = "windows"))] | 15 | #[cfg(any(target_os = "linux", target_os = "windows"))] |
crates/client/src/share/session.rsmodified+24 -0
| @@ -27,6 +27,7 @@ use super::{ | |||
| 27 | audio::AudioMode, | 27 | audio::AudioMode, |
| 28 | audio_capture::{self, AudioCapture}, | 28 | audio_capture::{self, AudioCapture}, |
| 29 | capture::{self, Capture}, | 29 | capture::{self, Capture}, |
| 30 | + congestion::{self, Controller, Uplink}, | ||
| 30 | encode::{self, Encoders}, | 31 | encode::{self, Encoders}, |
| 31 | frame::{Encoded, Frame}, | 32 | frame::{Encoded, Frame}, |
| 32 | reassembly::{Assembled, Reassembler}, | 33 | reassembly::{Assembled, Reassembler}, |
| @@ -160,6 +161,7 @@ pub struct Sharing { | |||
| 160 | _audio_encode: Option<Worker>, | 161 | _audio_encode: Option<Worker>, |
| 161 | meter: Meter, | 162 | meter: Meter, |
| 162 | _sender: Worker, | 163 | _sender: Worker, |
| 164 | + _congestion: Worker, | ||
| 163 | sent: Arc<Mutex<Sent>>, | 165 | sent: Arc<Mutex<Sent>>, |
| 164 | } | 166 | } |
| 165 | 167 | ||
| @@ -257,6 +259,12 @@ impl Sharing { | |||
| 257 | since: Instant::now(), | 259 | since: Instant::now(), |
| 258 | tallies: HashMap::new(), | 260 | tallies: HashMap::new(), |
| 259 | })); | 261 | })); |
| 262 | + let congestion = { | ||
| 263 | + let (connection, encoders) = (connection.clone(), encoders.clone()); | ||
| 264 | + Worker::spawn("share-congestion", move |stop| { | ||
| 265 | + control(&connection, &encoders, stop) | ||
| 266 | + }) | ||
| 267 | + }; | ||
| 260 | let sender = { | 268 | let sender = { |
| 261 | let sent = sent.clone(); | 269 | let sent = sent.clone(); |
| 262 | Worker::spawn("share-send", move |stop| { | 270 | Worker::spawn("share-send", move |stop| { |
| @@ -277,6 +285,7 @@ impl Sharing { | |||
| 277 | _audio_encode: audio_encode, | 285 | _audio_encode: audio_encode, |
| 278 | meter, | 286 | meter, |
| 279 | _sender: sender, | 287 | _sender: sender, |
| 288 | + _congestion: congestion, | ||
| 280 | sent, | 289 | sent, |
| 281 | }, | 290 | }, |
| 282 | source, | 291 | source, |
| @@ -350,6 +359,21 @@ fn feed( | |||
| 350 | } | 359 | } |
| 351 | } | 360 | } |
| 352 | 361 | ||
| 362 | +fn control(connection: &Connection, encoders: &Mutex<Encoders>, stop: &AtomicBool) { | ||
| 363 | + let mut controller = Controller::default(); | ||
| 364 | + let mut scale = 1.0; | ||
| 365 | + while wait(stop, congestion::PERIOD) { | ||
| 366 | + let next = controller.observe(Uplink::read(connection)); | ||
| 367 | + if next < scale { | ||
| 368 | + tracing::info!(scale = next, "share uplink congested"); | ||
| 369 | + } | ||
| 370 | + if next != scale { | ||
| 371 | + scale = next; | ||
| 372 | + lock(encoders).set_scale(scale); | ||
| 373 | + } | ||
| 374 | + } | ||
| 375 | +} | ||
| 376 | + | ||
| 353 | fn send(rx: Receiver<Encoded>, connection: Connection, sent: Arc<Mutex<Sent>>, stop: &AtomicBool) { | 377 | fn send(rx: Receiver<Encoded>, connection: Connection, sent: Arc<Mutex<Sent>>, stop: &AtomicBool) { |
| 354 | let mut counters: HashMap<Layer, u32> = HashMap::new(); | 378 | let mut counters: HashMap<Layer, u32> = HashMap::new(); |
| 355 | while let Some(item) = next(&rx, stop) { | 379 | while let Some(item) = next(&rx, stop) { |