Shares hold up on bad networks. A slow or bloated link used to queue seconds of stale video (0.9 s median, 1.5 s p90, ~20 s of freezes per 40 s on a 3 Mbit bufferbloated link). Now:
Browse files at this commit3e9d442a0cb6d5dfd0178762e276cc357436ed60Parent: e5ae8bc- fix
- shares hold up on bad networks. A slow or bloated link used to queue seconds of stale video (0.9 s median, 1.5 s p90, ~20 s of freezes per 40 s on a 3 Mbit bufferbloated link). Now:
9 changed files +264 -67
- crates/client/src/gui/community_settings.rs +14 -11
- crates/client/src/gui/jump.rs +8 -12
- crates/client/src/gui/settings_page.rs +18 -12
- crates/client/src/lib.rs +1 -1
- crates/client/src/share/session.rs +124 -21
- crates/client/src/tray.rs +4 -2
- crates/common/src/datagram.rs +63 -3
- crates/common/src/transport.rs +1 -1
- crates/server/src/rooms.rs +31 -4
crates/client/src/gui/community_settings.rsmodified+14 -11
| @@ -187,17 +187,20 @@ impl Yumush { | |||
| 187 | )); | 187 | )); |
| 188 | } | 188 | } |
| 189 | if permission::can_delete_community(actor) { | 189 | if permission::can_delete_community(actor) { |
| 190 | - sections.push(danger("danger_zone", vec![ | 190 | + sections.push(danger( |
| 191 | - delete_control( | 191 | + "danger_zone", |
| 192 | - "delete_community_button", | 192 | + vec![ |
| 193 | - w().community_settings.delete_community, | 193 | + delete_control( |
| 194 | - self.armed(Armed::DeleteCommunity), | 194 | + "delete_community_button", |
| 195 | - entity, | 195 | + w().community_settings.delete_community, |
| 196 | - palette, | 196 | + self.armed(Armed::DeleteCommunity), |
| 197 | - Yumush::delete_community, | 197 | + entity, |
| 198 | - ) | 198 | + palette, |
| 199 | - .into_any_element(), | 199 | + Yumush::delete_community, |
| 200 | - ])); | 200 | + ) |
| 201 | + .into_any_element(), | ||
| 202 | + ], | ||
| 203 | + )); | ||
| 201 | } | 204 | } |
| 202 | if sections.is_empty() { | 205 | if sections.is_empty() { |
| 203 | sections.push(section( | 206 | sections.push(section( |
crates/client/src/gui/jump.rsmodified+8 -12
| @@ -180,18 +180,14 @@ impl Yumush { | |||
| 180 | ), | 180 | ), |
| 181 | ] | 181 | ] |
| 182 | .into_iter() | 182 | .into_iter() |
| 183 | - .chain( | 183 | + .chain(SettingsSection::ALL.into_iter().map(|section| { |
| 184 | - SettingsSection::ALL | 184 | + Row::new( |
| 185 | - .into_iter() | 185 | + Place::Settings(section), |
| 186 | - .map(|section| { | 186 | + section.label().to_string(), |
| 187 | - Row::new( | 187 | + Glyph::Icon(IconName::Settings2), |
| 188 | - Place::Settings(section), | 188 | + ) |
| 189 | - section.label().to_string(), | 189 | + .hinted(w().jump.hint_settings) |
| 190 | - Glyph::Icon(IconName::Settings2), | 190 | + })) |
| 191 | - ) | ||
| 192 | - .hinted(w().jump.hint_settings) | ||
| 193 | - }), | ||
| 194 | - ) | ||
| 195 | .chain( | 191 | .chain( |
| 196 | setting_rows() | 192 | setting_rows() |
| 197 | .into_iter() | 193 | .into_iter() |
crates/client/src/gui/settings_page.rsmodified+18 -12
| @@ -277,17 +277,20 @@ impl Yumush { | |||
| 277 | appearance, | 277 | appearance, |
| 278 | )); | 278 | )); |
| 279 | if self.user.is_some() { | 279 | if self.user.is_some() { |
| 280 | - sections.push(danger(SettingsSection::DangerZone.id(), vec![ | 280 | + sections.push(danger( |
| 281 | - delete_control( | 281 | + SettingsSection::DangerZone.id(), |
| 282 | - "delete_account_button", | 282 | + vec![ |
| 283 | - w().settings_page.delete_account, | 283 | + delete_control( |
| 284 | - self.armed(Armed::DeleteAccount), | 284 | + "delete_account_button", |
| 285 | - entity, | 285 | + w().settings_page.delete_account, |
| 286 | - palette, | 286 | + self.armed(Armed::DeleteAccount), |
| 287 | - Yumush::delete_account, | 287 | + entity, |
| 288 | - ) | 288 | + palette, |
| 289 | - .into_any_element(), | 289 | + Yumush::delete_account, |
| 290 | - ])); | 290 | + ) |
| 291 | + .into_any_element(), | ||
| 292 | + ], | ||
| 293 | + )); | ||
| 291 | } | 294 | } |
| 292 | 295 | ||
| 293 | page( | 296 | page( |
| @@ -678,7 +681,10 @@ pub(super) fn page( | |||
| 678 | .into_any_element() | 681 | .into_any_element() |
| 679 | }) | 682 | }) |
| 680 | .collect(); | 683 | .collect(); |
| 681 | - let Some(Section { title: name, body, .. }) = sections.into_iter().nth(selected) else { | 684 | + let Some(Section { |
| 685 | + title: name, body, .. | ||
| 686 | + }) = sections.into_iter().nth(selected) | ||
| 687 | + else { | ||
| 682 | return frame(id, entity, palette, title, index, Vec::new(), Vec::new()); | 688 | return frame(id, entity, palette, title, index, Vec::new(), Vec::new()); |
| 683 | }; | 689 | }; |
| 684 | 690 | ||
crates/client/src/lib.rsmodified+1 -1
| @@ -109,7 +109,7 @@ impl Server { | |||
| 109 | 109 | ||
| 110 | pub fn save(&self) { | 110 | pub fn save(&self) { |
| 111 | let flag = if self.any_cert { "any_cert" } else { "verify" }; | 111 | let flag = if self.any_cert { "any_cert" } else { "verify" }; |
| 112 | - write_config("server", &format!("{}\n{flag}\n", self.address)); | 112 | + write_config("server", format!("{}\n{flag}\n", self.address)); |
| 113 | } | 113 | } |
| 114 | 114 | ||
| 115 | pub fn host(&self) -> &str { | 115 | pub fn host(&self) -> &str { |
crates/client/src/share/session.rsmodified+124 -21
| @@ -15,7 +15,9 @@ use common::{ | |||
| 15 | SHARE_AUDIO_BITRATE, SHARE_AUDIO_CHANNELS, VOICE_FRAME_SAMPLES, VOICE_MAX_PAYLOAD, | 15 | SHARE_AUDIO_BITRATE, SHARE_AUDIO_CHANNELS, VOICE_FRAME_SAMPLES, VOICE_MAX_PAYLOAD, |
| 16 | VOICE_SAMPLE_RATE, | 16 | VOICE_SAMPLE_RATE, |
| 17 | }, | 17 | }, |
| 18 | - datagram::{self, FrameInfo, Packet, RELAY_PREFIX, fragments, split, warn_if_small}, | 18 | + datagram::{ |
| 19 | + self, FrameInfo, Gate, Link, Packet, RELAY_PREFIX, fragments, split, warn_if_small, | ||
| 20 | + }, | ||
| 19 | model::Id, | 21 | model::Id, |
| 20 | share::{Codec, Fps, Kind, Layer, Mode, Quality, Resolution, Source, clamp}, | 22 | share::{Codec, Fps, Kind, Layer, Mode, Quality, Resolution, Source, clamp}, |
| 21 | }; | 23 | }; |
| @@ -72,11 +74,18 @@ const LOSS_LIMIT: f32 = 0.05; | |||
| 72 | const CLEAN_LOSS: f32 = 0.01; | 74 | const CLEAN_LOSS: f32 = 0.01; |
| 73 | const KEYFRAME_INTERVAL: Duration = Duration::from_secs(1); | 75 | const KEYFRAME_INTERVAL: Duration = Duration::from_secs(1); |
| 74 | const CLEAN_SECONDS: u32 = 10; | 76 | const CLEAN_SECONDS: u32 = 10; |
| 77 | +const MAX_PATIENCE: u32 = 60; | ||
| 78 | +const PROBE_SECONDS: u32 = 10; | ||
| 79 | +const LOSS_TICKS: usize = 3; | ||
| 80 | +const FIT: f32 = 0.8; | ||
| 81 | +const GOODPUT_TICKS: usize = 3; | ||
| 82 | +const DELAY_CATCH_UP: i64 = 32; | ||
| 75 | const HOLD_SECONDS: u32 = 3; | 83 | const HOLD_SECONDS: u32 = 3; |
| 76 | const MIN_SHOWN_WIDTH: u32 = 320; | 84 | const MIN_SHOWN_WIDTH: u32 = 320; |
| 77 | const QUEUING_LIMIT_MS: i64 = 60; | 85 | const QUEUING_LIMIT_MS: i64 = 60; |
| 78 | const QUEUING_CLEAN_MS: i64 = 20; | 86 | const QUEUING_CLEAN_MS: i64 = 20; |
| 79 | const QUEUING_WINDOW_MS: i64 = 1000; | 87 | const QUEUING_WINDOW_MS: i64 = 1000; |
| 88 | +const QUEUING_FLOOR_SECONDS: i64 = 30; | ||
| 80 | const CHUNK_SAMPLES: usize = VOICE_FRAME_SAMPLES * SHARE_AUDIO_CHANNELS; | 89 | const CHUNK_SAMPLES: usize = VOICE_FRAME_SAMPLES * SHARE_AUDIO_CHANNELS; |
| 81 | const DECODE_SAMPLES: usize = VOICE_SAMPLE_RATE as usize * 120 / 1000 * SHARE_AUDIO_CHANNELS; | 90 | const DECODE_SAMPLES: usize = VOICE_SAMPLE_RATE as usize * 120 / 1000 * SHARE_AUDIO_CHANNELS; |
| 82 | const AUDIO_RING: usize = VOICE_SAMPLE_RATE as usize * SHARE_AUDIO_CHANNELS / 2; | 91 | const AUDIO_RING: usize = VOICE_SAMPLE_RATE as usize * SHARE_AUDIO_CHANNELS / 2; |
| @@ -463,10 +472,10 @@ fn send( | |||
| 463 | let lane = lanes.entry(item.layer).or_insert_with(|| { | 472 | let lane = lanes.entry(item.layer).or_insert_with(|| { |
| 464 | let (tx, rx) = mpsc::channel(); | 473 | let (tx, rx) = mpsc::channel(); |
| 465 | let bitrate = lock(&encoders).layer_bitrate(item.layer); | 474 | let bitrate = lock(&encoders).layer_bitrate(item.layer); |
| 466 | - let (connection, loss) = (connection.clone(), loss.clone()); | 475 | + let (connection, loss, encoders) = (connection.clone(), loss.clone(), encoders.clone()); |
| 467 | let _ = std::thread::Builder::new() | 476 | let _ = std::thread::Builder::new() |
| 468 | .name("share-send".to_string()) | 477 | .name("share-send".to_string()) |
| 469 | - .spawn(move || send_layer(kind, rx, &connection, &loss, bitrate, seed)); | 478 | + .spawn(move || send_layer(kind, rx, &connection, &encoders, &loss, bitrate, seed)); |
| 470 | tx | 479 | tx |
| 471 | }); | 480 | }); |
| 472 | let _ = lane.send(item); | 481 | let _ = lane.send(item); |
| @@ -477,13 +486,30 @@ fn send_layer( | |||
| 477 | kind: Kind, | 486 | kind: Kind, |
| 478 | rx: Receiver<Encoded>, | 487 | rx: Receiver<Encoded>, |
| 479 | connection: &Connection, | 488 | connection: &Connection, |
| 489 | + encoders: &Mutex<Encoders>, | ||
| 480 | loss: &Loss, | 490 | loss: &Loss, |
| 481 | bitrate: u32, | 491 | bitrate: u32, |
| 482 | seed: u32, | 492 | seed: u32, |
| 483 | ) { | 493 | ) { |
| 484 | let mut pacer = Pacer::default(); | 494 | let mut pacer = Pacer::default(); |
| 485 | let mut number = seed; | 495 | let mut number = seed; |
| 496 | + let mut link = Link::default(); | ||
| 497 | + let mut stalled = false; | ||
| 486 | for item in rx { | 498 | for item in rx { |
| 499 | + let gate = link.gate(connection, item.keyframe); | ||
| 500 | + match gate { | ||
| 501 | + Gate::Send => stalled &= !item.keyframe, | ||
| 502 | + Gate::Pause => { | ||
| 503 | + tracing::info!(?kind, "uplink congested, pausing until it drains"); | ||
| 504 | + stalled = true; | ||
| 505 | + } | ||
| 506 | + Gate::Drop => {} | ||
| 507 | + Gate::Resume => lock(encoders).keyframe(item.layer, false), | ||
| 508 | + } | ||
| 509 | + if stalled || gate != Gate::Send { | ||
| 510 | + number = number.wrapping_add(1); | ||
| 511 | + continue; | ||
| 512 | + } | ||
| 487 | let info = FrameInfo { | 513 | let info = FrameInfo { |
| 488 | kind, | 514 | kind, |
| 489 | layer: item.layer, | 515 | layer: item.layer, |
| @@ -578,6 +604,8 @@ impl Clock { | |||
| 578 | #[derive(Default)] | 604 | #[derive(Default)] |
| 579 | struct Playout { | 605 | struct Playout { |
| 580 | lateness: VecDeque<(i64, i64)>, | 606 | lateness: VecDeque<(i64, i64)>, |
| 607 | + transit: VecDeque<(i64, i64)>, | ||
| 608 | + floors: VecDeque<(i64, i64)>, | ||
| 581 | frames: u32, | 609 | frames: u32, |
| 582 | epoch: u32, | 610 | epoch: u32, |
| 583 | } | 611 | } |
| @@ -588,9 +616,12 @@ impl Playout { | |||
| 588 | if epoch != self.epoch { | 616 | if epoch != self.epoch { |
| 589 | self.epoch = epoch; | 617 | self.epoch = epoch; |
| 590 | self.lateness.clear(); | 618 | self.lateness.clear(); |
| 619 | + self.transit.clear(); | ||
| 620 | + self.floors.clear(); | ||
| 591 | } | 621 | } |
| 592 | let now = clock.now(); | 622 | let now = clock.now(); |
| 593 | let current = clock.delay.load(Ordering::Relaxed); | 623 | let current = clock.delay.load(Ordering::Relaxed); |
| 624 | + self.track(now, now - i64::from(sharer_ms)); | ||
| 594 | let next = self.settle(now, now - clock.origin(sharer_ms), current); | 625 | let next = self.settle(now, now - clock.origin(sharer_ms), current); |
| 595 | clock.delay.store(next, Ordering::Relaxed); | 626 | clock.delay.store(next, Ordering::Relaxed); |
| 596 | clock | 627 | clock |
| @@ -618,6 +649,8 @@ impl Playout { | |||
| 618 | target | 649 | target |
| 619 | } else if target > current { | 650 | } else if target > current { |
| 620 | current + 1 | 651 | current + 1 |
| 652 | + } else if target < current - DELAY_CATCH_UP { | ||
| 653 | + current - (current - target) / DELAY_CATCH_UP | ||
| 621 | } else if target < current && self.frames.is_multiple_of(DELAY_FALL_EVERY) { | 654 | } else if target < current && self.frames.is_multiple_of(DELAY_FALL_EVERY) { |
| 622 | current - 1 | 655 | current - 1 |
| 623 | } else { | 656 | } else { |
| @@ -626,21 +659,38 @@ impl Playout { | |||
| 626 | next.clamp(MIN_DELAY_MS, MAX_DELAY_MS) | 659 | next.clamp(MIN_DELAY_MS, MAX_DELAY_MS) |
| 627 | } | 660 | } |
| 628 | 661 | ||
| 662 | + fn track(&mut self, now: i64, transit: i64) { | ||
| 663 | + self.transit.push_back((now, transit)); | ||
| 664 | + while self | ||
| 665 | + .transit | ||
| 666 | + .front() | ||
| 667 | + .is_some_and(|(at, _)| now - at >= QUEUING_WINDOW_MS) | ||
| 668 | + { | ||
| 669 | + self.transit.pop_front(); | ||
| 670 | + } | ||
| 671 | + match self.floors.back_mut() { | ||
| 672 | + Some((second, floor)) if *second == now / 1000 => *floor = (*floor).min(transit), | ||
| 673 | + _ => self.floors.push_back((now / 1000, transit)), | ||
| 674 | + } | ||
| 675 | + while self | ||
| 676 | + .floors | ||
| 677 | + .front() | ||
| 678 | + .is_some_and(|(second, _)| now / 1000 - second >= QUEUING_FLOOR_SECONDS) | ||
| 679 | + { | ||
| 680 | + self.floors.pop_front(); | ||
| 681 | + } | ||
| 682 | + } | ||
| 683 | + | ||
| 629 | fn queuing(&self) -> i64 { | 684 | fn queuing(&self) -> i64 { |
| 630 | - let Some(&(newest, _)) = self.lateness.back() else { | ||
| 631 | - return 0; | ||
| 632 | - }; | ||
| 633 | let floor = self | 685 | let floor = self |
| 634 | - .lateness | 686 | + .floors |
| 635 | .iter() | 687 | .iter() |
| 636 | - .map(|(_, late)| *late) | 688 | + .map(|(_, floor)| *floor) |
| 637 | .min() | 689 | .min() |
| 638 | .unwrap_or(0); | 690 | .unwrap_or(0); |
| 639 | - self.lateness | 691 | + self.transit |
| 640 | .iter() | 692 | .iter() |
| 641 | - .rev() | 693 | + .map(|(_, transit)| *transit) |
| 642 | - .take_while(|(at, _)| newest - at < QUEUING_WINDOW_MS) | ||
| 643 | - .map(|(_, late)| *late) | ||
| 644 | .min() | 694 | .min() |
| 645 | .map_or(0, |best| best - floor) | 695 | .map_or(0, |best| best - floor) |
| 646 | } | 696 | } |
| @@ -694,7 +744,11 @@ pub struct Watching { | |||
| 694 | audio_tx: Option<Sender<Bytes>>, | 744 | audio_tx: Option<Sender<Bytes>>, |
| 695 | meter: Meter, | 745 | meter: Meter, |
| 696 | received: AtomicU64, | 746 | received: AtomicU64, |
| 747 | + goodput: AtomicU64, | ||
| 748 | + goodputs: VecDeque<f32>, | ||
| 749 | + kind: Kind, | ||
| 697 | since: Instant, | 750 | since: Instant, |
| 751 | + ticked: Instant, | ||
| 698 | _video: Worker, | 752 | _video: Worker, |
| 699 | _audio: Option<Worker>, | 753 | _audio: Option<Worker>, |
| 700 | _output: Option<Audio>, | 754 | _output: Option<Audio>, |
| @@ -704,6 +758,9 @@ pub struct Watching { | |||
| 704 | rung: usize, | 758 | rung: usize, |
| 705 | clean: u32, | 759 | clean: u32, |
| 706 | hold: u32, | 760 | hold: u32, |
| 761 | + losses: VecDeque<f32>, | ||
| 762 | + patience: u32, | ||
| 763 | + probed: u32, | ||
| 707 | requested: Instant, | 764 | requested: Instant, |
| 708 | } | 765 | } |
| 709 | 766 | ||
| @@ -768,7 +825,11 @@ impl Watching { | |||
| 768 | audio_tx, | 825 | audio_tx, |
| 769 | meter, | 826 | meter, |
| 770 | received: AtomicU64::new(0), | 827 | received: AtomicU64::new(0), |
| 828 | + goodput: AtomicU64::new(0), | ||
| 829 | + goodputs: VecDeque::new(), | ||
| 830 | + kind, | ||
| 771 | since: Instant::now(), | 831 | since: Instant::now(), |
| 832 | + ticked: Instant::now(), | ||
| 772 | _video: video_worker, | 833 | _video: video_worker, |
| 773 | link, | 834 | link, |
| 774 | _audio: audio_worker, | 835 | _audio: audio_worker, |
| @@ -779,6 +840,9 @@ impl Watching { | |||
| 779 | ladder, | 840 | ladder, |
| 780 | clean: 0, | 841 | clean: 0, |
| 781 | hold: 0, | 842 | hold: 0, |
| 843 | + losses: VecDeque::new(), | ||
| 844 | + patience: CLEAN_SECONDS, | ||
| 845 | + probed: u32::MAX, | ||
| 782 | requested: Instant::now(), | 846 | requested: Instant::now(), |
| 783 | }; | 847 | }; |
| 784 | watching.emitted = Some(watching.wanted()); | 848 | watching.emitted = Some(watching.wanted()); |
| @@ -802,6 +866,8 @@ impl Watching { | |||
| 802 | .fetch_add((datagram.len() - RELAY_PREFIX) as u64, Ordering::Relaxed); | 866 | .fetch_add((datagram.len() - RELAY_PREFIX) as u64, Ordering::Relaxed); |
| 803 | match packet { | 867 | match packet { |
| 804 | Packet::Video { .. } => { | 868 | Packet::Video { .. } => { |
| 869 | + self.goodput | ||
| 870 | + .fetch_add(datagram.len() as u64, Ordering::Relaxed); | ||
| 805 | let _ = self.video_tx.send((Instant::now(), datagram.clone())); | 871 | let _ = self.video_tx.send((Instant::now(), datagram.clone())); |
| 806 | } | 872 | } |
| 807 | _ => { | 873 | _ => { |
| @@ -928,8 +994,18 @@ impl Watching { | |||
| 928 | self.requested = Instant::now(); | 994 | self.requested = Instant::now(); |
| 929 | ticks.push(Tick::Keyframe { recover }); | 995 | ticks.push(Tick::Keyframe { recover }); |
| 930 | } | 996 | } |
| 997 | + let seconds = std::mem::replace(&mut self.ticked, Instant::now()) | ||
| 998 | + .elapsed() | ||
| 999 | + .as_secs_f32() | ||
| 1000 | + .max(f32::EPSILON); | ||
| 1001 | + if self.goodputs.len() >= GOODPUT_TICKS { | ||
| 1002 | + self.goodputs.pop_front(); | ||
| 1003 | + } | ||
| 1004 | + self.goodputs | ||
| 1005 | + .push_back(self.goodput.swap(0, Ordering::Relaxed) as f32 * 8. / seconds); | ||
| 1006 | + let goodput = self.goodputs.iter().copied().fold(0., f32::max); | ||
| 931 | if self.quality == WatchQuality::Auto { | 1007 | if self.quality == WatchQuality::Auto { |
| 932 | - self.adapt(loss, queuing_ms, served); | 1008 | + self.adapt(loss, queuing_ms, served, goodput); |
| 933 | } | 1009 | } |
| 934 | let wanted = self.wanted(); | 1010 | let wanted = self.wanted(); |
| 935 | if self.emitted != Some(wanted) { | 1011 | if self.emitted != Some(wanted) { |
| @@ -939,29 +1015,56 @@ impl Watching { | |||
| 939 | ticks | 1015 | ticks |
| 940 | } | 1016 | } |
| 941 | 1017 | ||
| 942 | - fn adapt(&mut self, loss: f32, queuing_ms: i64, served: Option<Layer>) { | 1018 | + fn adapt(&mut self, loss: f32, queuing_ms: i64, served: Option<Layer>, goodput: f32) { |
| 943 | let rung = self.rung; | 1019 | let rung = self.rung; |
| 944 | - if self.hold > 0 { | 1020 | + self.probed = self.probed.saturating_add(1); |
| 945 | - self.hold -= 1; | 1021 | + if self.losses.len() >= LOSS_TICKS { |
| 1022 | + self.losses.pop_front(); | ||
| 1023 | + } | ||
| 1024 | + self.losses.push_back(loss); | ||
| 1025 | + let lossy = self.losses.len() >= LOSS_TICKS | ||
| 1026 | + && self.losses.iter().sum::<f32>() / LOSS_TICKS as f32 > LOSS_LIMIT; | ||
| 1027 | + let switching = served.is_some_and(|layer| layer.quality != self.ladder[self.rung]); | ||
| 1028 | + if switching && self.hold < HOLD_SECONDS { | ||
| 1029 | + self.hold += 1; | ||
| 946 | return; | 1030 | return; |
| 947 | } | 1031 | } |
| 948 | - if loss > LOSS_LIMIT || queuing_ms > QUEUING_LIMIT_MS { | 1032 | + self.hold = 0; |
| 949 | - self.rung = (self.rung + 1).min(self.ladder.len() - 1); | 1033 | + if queuing_ms > QUEUING_LIMIT_MS || lossy { |
| 1034 | + let fit = served.map_or(self.rung + 1, |layer| { | ||
| 1035 | + (self.rung + 1..self.ladder.len()) | ||
| 1036 | + .find(|&index| { | ||
| 1037 | + let rung = Layer { | ||
| 1038 | + quality: self.ladder[index], | ||
| 1039 | + codec: layer.codec, | ||
| 1040 | + }; | ||
| 1041 | + rung.bitrate(3840, 2160) as f32 <= goodput * FIT | ||
| 1042 | + }) | ||
| 1043 | + .unwrap_or(self.ladder.len() - 1) | ||
| 1044 | + }); | ||
| 1045 | + self.rung = fit.max(self.rung + 1).min(self.ladder.len() - 1); | ||
| 1046 | + if self.probed <= PROBE_SECONDS { | ||
| 1047 | + self.patience = (self.patience * 2).min(MAX_PATIENCE); | ||
| 1048 | + self.probed = u32::MAX; | ||
| 1049 | + } | ||
| 950 | self.clean = 0; | 1050 | self.clean = 0; |
| 951 | } else if loss < CLEAN_LOSS && queuing_ms < QUEUING_CLEAN_MS { | 1051 | } else if loss < CLEAN_LOSS && queuing_ms < QUEUING_CLEAN_MS { |
| 952 | self.clean += 1; | 1052 | self.clean += 1; |
| 953 | - if self.clean >= CLEAN_SECONDS | 1053 | + if self.probed == PROBE_SECONDS + 1 { |
| 1054 | + self.patience = CLEAN_SECONDS; | ||
| 1055 | + } | ||
| 1056 | + if self.clean >= self.patience | ||
| 954 | && served.is_some_and(|layer| layer.quality == self.ladder[self.rung]) | 1057 | && served.is_some_and(|layer| layer.quality == self.ladder[self.rung]) |
| 955 | { | 1058 | { |
| 956 | self.rung = self.rung.saturating_sub(1); | 1059 | self.rung = self.rung.saturating_sub(1); |
| 957 | self.clean = 0; | 1060 | self.clean = 0; |
| 1061 | + self.probed = 0; | ||
| 958 | } | 1062 | } |
| 959 | } else { | 1063 | } else { |
| 960 | self.clean = 0; | 1064 | self.clean = 0; |
| 961 | } | 1065 | } |
| 962 | if self.rung != rung { | 1066 | if self.rung != rung { |
| 963 | - self.hold = HOLD_SECONDS; | 1067 | + tracing::info!(kind = ?self.kind, loss, queuing_ms, goodput, quality = ?self.ladder[self.rung], "share watch quality"); |
| 964 | - tracing::info!(loss, queuing_ms, quality = ?self.ladder[self.rung], "share watch quality"); | ||
| 965 | } | 1068 | } |
| 966 | } | 1069 | } |
| 967 | } | 1070 | } |
crates/client/src/tray.rsmodified+4 -2
| @@ -80,8 +80,10 @@ fn coverage(x: f32, y: f32, (cx, cy, radius): (f32, f32, f32)) -> f32 { | |||
| 80 | #[cfg(any(target_os = "linux", target_os = "windows"))] | 80 | #[cfg(any(target_os = "linux", target_os = "windows"))] |
| 81 | fn logo() -> Vec<([u8; 3], u8)> { | 81 | fn logo() -> Vec<([u8; 3], u8)> { |
| 82 | crate::logo::rgba(ICON_SIZE as u32) | 82 | crate::logo::rgba(ICON_SIZE as u32) |
| 83 | - .chunks_exact(4) | 83 | + .as_chunks::<4>() |
| 84 | - .map(|pixel| ([pixel[0], pixel[1], pixel[2]], pixel[3])) | 84 | + .0 |
| 85 | + .iter() | ||
| 86 | + .map(|&[r, g, b, a]| ([r, g, b], a)) | ||
| 85 | .collect() | 87 | .collect() |
| 86 | } | 88 | } |
| 87 | 89 | ||
crates/common/src/datagram.rsmodified+63 -3
| @@ -1,6 +1,9 @@ | |||
| 1 | -use std::sync::{ | 1 | +use std::{ |
| 2 | - LazyLock, | 2 | + sync::{ |
| 3 | - atomic::{AtomicBool, AtomicU64, Ordering}, | 3 | + LazyLock, |
| 4 | + atomic::{AtomicBool, AtomicU64, Ordering}, | ||
| 5 | + }, | ||
| 6 | + time::{Duration, Instant}, | ||
| 4 | }; | 7 | }; |
| 5 | 8 | ||
| 6 | use bytes::Bytes; | 9 | use bytes::Bytes; |
| @@ -10,6 +13,7 @@ use tracing::warn; | |||
| 10 | use crate::{ | 13 | use crate::{ |
| 11 | constant::{MAX_FRAME_BYTES, VIDEO_FRAGMENT_BYTES, VOICE_MAX_PAYLOAD}, | 14 | constant::{MAX_FRAME_BYTES, VIDEO_FRAGMENT_BYTES, VOICE_MAX_PAYLOAD}, |
| 12 | share::{Kind, Layer}, | 15 | share::{Kind, Layer}, |
| 16 | + transport::DATAGRAM_BUFFER_BYTES, | ||
| 13 | }; | 17 | }; |
| 14 | 18 | ||
| 15 | pub const RELAY_PREFIX: usize = 8; | 19 | pub const RELAY_PREFIX: usize = 8; |
| @@ -375,6 +379,62 @@ fn test_drop() -> bool { | |||
| 375 | x < *TEST_DROP | 379 | x < *TEST_DROP |
| 376 | } | 380 | } |
| 377 | 381 | ||
| 382 | +const BACKLOG_LIMIT: Duration = Duration::from_millis(250); | ||
| 383 | +const RTT_SLACK: Duration = Duration::from_millis(150); | ||
| 384 | +const PAUSE_MAX: Duration = Duration::from_secs(2); | ||
| 385 | + | ||
| 386 | +fn backlog(connection: &Connection) -> Duration { | ||
| 387 | + let queued = DATAGRAM_BUFFER_BYTES.saturating_sub(connection.datagram_send_buffer_space()); | ||
| 388 | + if queued == 0 { | ||
| 389 | + return Duration::ZERO; | ||
| 390 | + } | ||
| 391 | + let path = connection.stats().path; | ||
| 392 | + let bytes_per_second = path.cwnd as f64 / path.rtt.as_secs_f64().max(0.001); | ||
| 393 | + Duration::from_secs_f64(queued as f64 / bytes_per_second.max(1.0)) | ||
| 394 | +} | ||
| 395 | + | ||
| 396 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 397 | +pub enum Gate { | ||
| 398 | + Send, | ||
| 399 | + Pause, | ||
| 400 | + Drop, | ||
| 401 | + Resume, | ||
| 402 | +} | ||
| 403 | + | ||
| 404 | +#[derive(Default)] | ||
| 405 | +pub struct Link { | ||
| 406 | + base: Option<Duration>, | ||
| 407 | + paused: Option<Instant>, | ||
| 408 | +} | ||
| 409 | + | ||
| 410 | +impl Link { | ||
| 411 | + pub fn paused(&self) -> bool { | ||
| 412 | + self.paused.is_some() | ||
| 413 | + } | ||
| 414 | + | ||
| 415 | + pub fn gate(&mut self, connection: &Connection, keyframe: bool) -> Gate { | ||
| 416 | + let rtt = connection.rtt(); | ||
| 417 | + let base = self.base.map_or(rtt, |base| base.min(rtt)); | ||
| 418 | + self.base = Some(base); | ||
| 419 | + let congested = rtt > base + RTT_SLACK || backlog(connection) > BACKLOG_LIMIT; | ||
| 420 | + match self.paused { | ||
| 421 | + Some(since) if congested && since.elapsed() < PAUSE_MAX => Gate::Drop, | ||
| 422 | + Some(since) => { | ||
| 423 | + if since.elapsed() >= PAUSE_MAX { | ||
| 424 | + self.base = None; | ||
| 425 | + } | ||
| 426 | + self.paused = None; | ||
| 427 | + Gate::Resume | ||
| 428 | + } | ||
| 429 | + None if congested && !keyframe => { | ||
| 430 | + self.paused = Some(Instant::now()); | ||
| 431 | + Gate::Pause | ||
| 432 | + } | ||
| 433 | + None => Gate::Send, | ||
| 434 | + } | ||
| 435 | + } | ||
| 436 | +} | ||
| 437 | + | ||
| 378 | pub fn send(connection: &Connection, bytes: Bytes) { | 438 | pub fn send(connection: &Connection, bytes: Bytes) { |
| 379 | if test_drop() { | 439 | if test_drop() { |
| 380 | return; | 440 | return; |
crates/common/src/transport.rsmodified+1 -1
| @@ -8,7 +8,7 @@ pub const ALPN: &[u8] = b"yumush/3"; | |||
| 8 | const IDLE_TIMEOUT: Duration = Duration::from_secs(10); | 8 | const IDLE_TIMEOUT: Duration = Duration::from_secs(10); |
| 9 | pub const KEEP_ALIVE: Duration = Duration::from_secs(1); | 9 | pub const KEEP_ALIVE: Duration = Duration::from_secs(1); |
| 10 | const BIDI_STREAMS: u32 = 256; | 10 | const BIDI_STREAMS: u32 = 256; |
| 11 | -const DATAGRAM_BUFFER_BYTES: usize = 4 << 20; | 11 | +pub const DATAGRAM_BUFFER_BYTES: usize = 4 << 20; |
| 12 | const SOCKET_BUFFER_BYTES: usize = 8 << 20; | 12 | const SOCKET_BUFFER_BYTES: usize = 8 << 20; |
| 13 | 13 | ||
| 14 | pub fn transport() -> TransportConfig { | 14 | pub fn transport() -> TransportConfig { |
crates/server/src/rooms.rsmodified+31 -4
| @@ -6,7 +6,7 @@ use std::{ | |||
| 6 | 6 | ||
| 7 | use bytes::Bytes; | 7 | use bytes::Bytes; |
| 8 | use common::{ | 8 | use common::{ |
| 9 | - datagram::{self, Packet, RELAY_PREFIX, relayed}, | 9 | + datagram::{self, Gate, Link, Packet, RELAY_PREFIX, relayed}, |
| 10 | error::Error, | 10 | error::Error, |
| 11 | event::{Event, Media}, | 11 | event::{Event, Media}, |
| 12 | model::{CallLog, Id}, | 12 | model::{CallLog, Id}, |
| @@ -74,6 +74,7 @@ struct Viewer { | |||
| 74 | decode: Vec<Codec>, | 74 | decode: Vec<Codec>, |
| 75 | forward: Forward, | 75 | forward: Forward, |
| 76 | acked: Option<(Layer, u32)>, | 76 | acked: Option<(Layer, u32)>, |
| 77 | + link: Link, | ||
| 77 | } | 78 | } |
| 78 | 79 | ||
| 79 | const RECENT_PACKETS: usize = 4096; | 80 | const RECENT_PACKETS: usize = 4096; |
| @@ -642,6 +643,7 @@ impl Rooms { | |||
| 642 | decode: decode.to_vec(), | 643 | decode: decode.to_vec(), |
| 643 | forward, | 644 | forward, |
| 644 | acked: None, | 645 | acked: None, |
| 646 | + link: Link::default(), | ||
| 645 | }); | 647 | }); |
| 646 | let mut broadcasts = Vec::from_iter(layers_changed(sharer_conn, kind, sharing, before)); | 648 | let mut broadcasts = Vec::from_iter(layers_changed(sharer_conn, kind, sharing, before)); |
| 647 | broadcasts.push(Broadcast::media( | 649 | broadcasts.push(Broadcast::media( |
| @@ -877,14 +879,39 @@ fn relay_video( | |||
| 877 | sharing.recent.insert((layer, frame, index), bytes.clone()); | 879 | sharing.recent.insert((layer, frame, index), bytes.clone()); |
| 878 | } | 880 | } |
| 879 | let before = keyframe.then(|| sharing.layers()); | 881 | let before = keyframe.then(|| sharing.layers()); |
| 882 | + let mut stalled = None; | ||
| 880 | for viewer in &mut sharing.viewers { | 883 | for viewer in &mut sharing.viewers { |
| 884 | + if viewer.link.paused() { | ||
| 885 | + if viewer.link.gate(&viewer.connection, keyframe) == Gate::Resume { | ||
| 886 | + stalled = Some(viewer.forward.target()); | ||
| 887 | + } | ||
| 888 | + continue; | ||
| 889 | + } | ||
| 881 | if keyframe { | 890 | if keyframe { |
| 882 | viewer.forward = viewer.forward.on_keyframe(layer); | 891 | viewer.forward = viewer.forward.on_keyframe(layer); |
| 883 | } | 892 | } |
| 884 | - if viewer.forward.live() == Some(layer) { | 893 | + if viewer.forward.live() != Some(layer) { |
| 885 | - datagram::send(&viewer.connection, bytes.clone()); | 894 | + continue; |
| 895 | + } | ||
| 896 | + if viewer.link.gate(&viewer.connection, keyframe) == Gate::Pause { | ||
| 897 | + tracing::info!(?kind, "viewer link congested, pausing until it drains"); | ||
| 898 | + viewer.forward = Forward::Waiting(viewer.forward.target()); | ||
| 899 | + continue; | ||
| 886 | } | 900 | } |
| 901 | + datagram::send(&viewer.connection, bytes.clone()); | ||
| 887 | } | 902 | } |
| 888 | 903 | ||
| 889 | - Vec::from_iter(before.and_then(|before| layers_changed(id, kind, sharing, before))) | 904 | + let mut broadcasts = |
| 905 | + Vec::from_iter(before.and_then(|before| layers_changed(id, kind, sharing, before))); | ||
| 906 | + if let Some(layer) = stalled { | ||
| 907 | + broadcasts.push(Broadcast::media( | ||
| 908 | + id, | ||
| 909 | + Media::Keyframe { | ||
| 910 | + kind, | ||
| 911 | + layer, | ||
| 912 | + recover: false, | ||
| 913 | + }, | ||
| 914 | + )); | ||
| 915 | + } | ||
| 916 | + broadcasts | ||
| 890 | } | 917 | } |