diff --git a/src/video.rs b/src/video.rs index 604c1b5..1cb9e1d 100644 --- a/src/video.rs +++ b/src/video.rs @@ -12,19 +12,19 @@ use crate::capture_setting::VideoSettings; /// Default frame rate; the viewer's 帧率 setting overrides it per connection. pub const FPS: u64 = 60; -/// IDR every 2s. All-intra 60fps measured 30 Mbit/s on the real desktop (saturating -/// the viewer's Wi-Fi: 3-6 fps received). On a static desktop with NVENC CQ18 a 0.5s GOP -/// measured 7.7 Mbit/s and a 2s GOP 1.9 Mbit/s. A broken prediction chain freezes until the -/// next IDR or a PLI, so a 2s GOP relies on PLI/FIR recovery being prompt. -const GOP: u64 = 120; +/// Keyframe interval. All-intra 60fps measured 30 Mbit/s on the real desktop (saturating the +/// viewer's Wi-Fi: 3-6 fps received). On a static desktop with NVENC CQ18 a 0.5s GOP measured +/// 7.7 Mbit/s and a 2s GOP 1.9 Mbit/s. A broken prediction chain freezes until the next IDR or +/// a PLI. Only the in-process encoder answers PLI/FIR with an IDR, so the child-process path +/// keeps the short 0.5s GOP and the longer 2s GOP is used only where an on-demand IDR exists. +const GOP_SECONDS_PROCESS: f64 = 0.5; +const GOP_SECONDS_IN_PROCESS: f64 = 2.0; -/// Keyframe interval in frames at the nominal rate. `UU_VIDEO_GOP_SECONDS` overrides the default for tuning. -fn gop_frames(fps: u32) -> u64 { - let seconds = std::env::var("UU_VIDEO_GOP_SECONDS").ok().and_then(|v| v.parse::().ok()); - match seconds { - Some(s) if s > 0.0 => ((f64::from(fps) * s).round() as u64).max(1), - _ => (u64::from(fps) * GOP / FPS).max(1), - } +/// Keyframe interval in frames at the nominal rate. `UU_VIDEO_GOP_SECONDS` overrides the default for both paths. +fn gop_frames(fps: u32, in_process: bool) -> u64 { + let default = if in_process { GOP_SECONDS_IN_PROCESS } else { GOP_SECONDS_PROCESS }; + let seconds = std::env::var("UU_VIDEO_GOP_SECONDS").ok().and_then(|v| v.parse::().ok()).unwrap_or(default); + ((f64::from(fps) * seconds).round() as u64).max(1) } const MAX_ACCESS_UNIT: usize = 8 * 1024 * 1024; @@ -176,7 +176,7 @@ async fn read_encoder_with( let max_kbps = settings.max_kbps().unwrap_or(30_000) as i64; let config = crate::inproc_encoder::Config { display: display.clone(), input: size, output: settings.output, fps: u32::from(settings.fps), - gop: gop_frames(settings.fps) as u32, cq: settings.cq() as u32, + gop: gop_frames(settings.fps, true) as u32, cq: settings.cq() as u32, target_bps: settings.max_kbps().map_or(16_000, |k| i64::from(k) * 3 / 4) * 1000, max_bps: max_kbps * 1000, buffer_bits: (max_kbps / 8).clamp(100, 500) * 1000, gpu, }; @@ -189,7 +189,7 @@ async fn read_encoder_with( if wayland && portal.is_none() { settings.fps = FPS as u32; } let fps = settings.fps.to_string(); // Keep the IDR interval at 2 s whatever the frame rate. - let gop = gop_frames(settings.fps).to_string(); + let gop = gop_frames(settings.fps, false).to_string(); let (maxrate, bufsize) = rate_args(&settings); let cq = settings.cq().to_string(); let target = settings.max_kbps().map_or("16M".to_string(), |k| format!("{}k", k * 3 / 4));