mirror of
https://github.com/VallierDev/uu-remote-linux.git
synced 2026-11-16 16:50:41 +01:00
Video: encoder and codec selection settings (set-encoder / set-codec) (#92)
Step 1 of #85: unified encoder/codec selection (host_settings.json > UU_VIDEO_* > auto), set-encoder / set-codec subcommands with locked merge writes, per-connection selection snapshot, doctor shows sources. Co-authored-by: ferstar <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
14 files changed
+676
-121
No files matched your search
+41
-10
@@ -120,6 +120,7 @@ fn custom_bitrate_bps(value: u64) -> u32 {
|
||||
bps.min(u64::from(u32::MAX)) as u32
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32)) -> Option<Vec<u8>> {
|
||||
handle_rpc_with_codecs(rpc, settings, native, true, false)
|
||||
}
|
||||
@@ -128,17 +129,32 @@ pub fn handle_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, na
|
||||
/// lists H265 first in its offer) and a hardware encoder is selected. Software libx265 is about ten
|
||||
/// times slower than libx264 (measured 15 vs 155 fps at 1080p on a 4-core ARM host, #59), so CPU
|
||||
/// hosts default to H.264. UU_VIDEO_CODEC=h264 / h265 overrides.
|
||||
pub fn preferred_codec(h264: bool, h265: bool) -> VideoCodec {
|
||||
let forced = std::env::var("UU_VIDEO_CODEC").ok().map(|v| v.to_ascii_lowercase());
|
||||
pick_codec(h264, h265, forced.as_deref(), crate::video::Encoder::requested() != crate::video::Encoder::Cpu)
|
||||
#[cfg(test)]
|
||||
fn test_selection() -> crate::video_select::Selection {
|
||||
crate::video_select::resolve_pair(Some("auto"), None, Some("auto"), None, false)
|
||||
}
|
||||
fn pick_codec(h264: bool, h265: bool, forced: Option<&str>, hardware: bool) -> VideoCodec {
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn preferred_codec(h264: bool, h265: bool) -> VideoCodec {
|
||||
preferred_codec_for(h264, h265, test_selection())
|
||||
}
|
||||
|
||||
pub fn preferred_codec_for(h264: bool, h265: bool, selection: crate::video_select::Selection) -> VideoCodec {
|
||||
pick_codec(h264, h265, crate::video_select::codec_force(selection.codec_pref), crate::video_select::hardware(selection.encoder))
|
||||
}
|
||||
|
||||
pub(crate) fn pick_codec(h264: bool, h265: bool, forced: Option<&str>, hardware: bool) -> VideoCodec {
|
||||
let wants_h265 = match forced { Some("h264") => false, Some("h265" | "hevc") => true, _ => hardware };
|
||||
if h265 && (wants_h265 || !h264) { VideoCodec::H265 } else { VideoCodec::H264 }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn handle_rpc_with_codecs(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool) -> Option<Vec<u8>> {
|
||||
apply_rpc(rpc, settings, native, h264, h265, crate::video::full_chroma_available())
|
||||
handle_rpc_for(rpc, settings, native, h264, h265, test_selection())
|
||||
}
|
||||
|
||||
pub fn handle_rpc_for(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool, selection: crate::video_select::Selection) -> Option<Vec<u8>> {
|
||||
apply_rpc(rpc, settings, native, h264, h265, crate::video::full_chroma_for(selection.encoder), crate::video_select::codec_force(selection.codec_pref))
|
||||
}
|
||||
|
||||
/// The resolution a capture/resolution RPC asks for, parsed like apply_rpc: X11 hosts apply it
|
||||
@@ -156,7 +172,7 @@ pub fn requested_resolution(rpc: &[WireField]) -> Option<crate::desktop_mode::Re
|
||||
} else { None }
|
||||
}
|
||||
|
||||
fn apply_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), h264: bool, h265: bool, full_chroma: bool) -> Option<Vec<u8>> {
|
||||
fn apply_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native: (u32, u32), _h264: bool, h265: bool, full_chroma: bool, forced: Option<&str>) -> Option<Vec<u8>> {
|
||||
let header = message(rpc, 1).unwrap_or(&[]);
|
||||
let mut next = *settings.borrow();
|
||||
if let Some(request) = message(rpc, 2) {
|
||||
@@ -167,7 +183,9 @@ fn apply_rpc(rpc: &[WireField], settings: &watch::Sender<VideoSettings>, native:
|
||||
// codec_type 13 (1 H264, 2 H265): like the official host (negotiateCodec), the host picks the
|
||||
// codec from both capability lists; the Mac viewer sends its current codec (1 while receiving
|
||||
// H.264), so only an explicit H.265 request switches, never one absent from the m-line.
|
||||
if number(&fields, 13) == Some(2) && h265 { next.codec = VideoCodec::H265; }
|
||||
// A forced codec (host_settings video_codec or UU_VIDEO_CODEC) wins over that switch, same
|
||||
// rule as pick_codec at stream start: h264 stays H.264; auto/h265 still follow the request.
|
||||
if number(&fields, 13) == Some(2) && h265 && forced != Some("h264") { next.codec = VideoCodec::H265; }
|
||||
// Field 1 is the viewer's fps option index (1 = 30, 2 = 60, ...); 18 fpsCount is the frame rate.
|
||||
if let Some(fps) = number(&fields, 18).filter(|v| *v > 0).or_else(|| number(&fields, 1).and_then(fps_from_option)) { next.fps = fps as u32; }
|
||||
if let Some(quality) = number(&fields, 2).filter(|v| *v > 0) { next.quality = quality; }
|
||||
@@ -333,12 +351,12 @@ mod tests {
|
||||
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||
// Observed Mac viewer request with 真彩 on: field 7 = 3.
|
||||
let request = rpc(5, 2, &[8, 2, 16, 4, 56, 3, 144, 1, 60]);
|
||||
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, false).unwrap();
|
||||
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, false, None).unwrap();
|
||||
assert_eq!(rx.borrow().chroma, CHROMA_420);
|
||||
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, true).unwrap();
|
||||
apply_rpc(&decode_wire(&request).unwrap(), &tx, (1920, 1080), true, false, true, None).unwrap();
|
||||
assert_eq!(rx.borrow().chroma, CHROMA_444);
|
||||
assert_eq!(number(&decode_wire(&sync_payload(&rx.borrow(), (1920, 1080))).unwrap(), 3), Some(CHROMA_444));
|
||||
apply_rpc(&decode_wire(&rpc(6, 2, &[56, 1])).unwrap(), &tx, (1920, 1080), true, false, true).unwrap();
|
||||
apply_rpc(&decode_wire(&rpc(6, 2, &[56, 1])).unwrap(), &tx, (1920, 1080), true, false, true, None).unwrap();
|
||||
assert_eq!(rx.borrow().chroma, CHROMA_420);
|
||||
}
|
||||
|
||||
@@ -570,6 +588,19 @@ mod codec_tests {
|
||||
assert_eq!(pick_codec(false, true, None, false), VideoCodec::H265);
|
||||
assert_eq!(pick_codec(true, false, Some("h265"), true), VideoCodec::H264);
|
||||
}
|
||||
#[test]
|
||||
fn forced_h264_ignores_later_h265_capture_request() {
|
||||
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||
// Field 13 = 2 on a peer that negotiated both codecs.
|
||||
let request = decode_wire(&[18, 2, 104, 2]).unwrap();
|
||||
apply_rpc(&request, &tx, (1920, 1080), true, true, false, Some("h264")).unwrap();
|
||||
assert_eq!(rx.borrow().codec, VideoCodec::H264);
|
||||
apply_rpc(&request, &tx, (1920, 1080), true, true, false, None).unwrap();
|
||||
assert_eq!(rx.borrow().codec, VideoCodec::H265);
|
||||
let (tx, rx) = watch::channel(VideoSettings::default());
|
||||
apply_rpc(&request, &tx, (1920, 1080), true, true, false, Some("h265")).unwrap();
|
||||
assert_eq!(rx.borrow().codec, VideoCodec::H265);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -5,6 +5,13 @@
|
||||
use std::path::PathBuf;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
/// Tests that point UU_CONFIG at a temp file must take this, or they race.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn test_env_lock() -> &'static std::sync::Mutex<()> {
|
||||
static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
&LOCK
|
||||
}
|
||||
|
||||
pub fn path() -> PathBuf {
|
||||
if let Some(p) = std::env::var_os("UU_CONFIG") { return PathBuf::from(p); }
|
||||
let base = std::env::var_os("XDG_CONFIG_HOME").map(PathBuf::from)
|
||||
|
||||
+8
-3
@@ -92,9 +92,14 @@ pub async fn run() -> Result<(), String> {
|
||||
line("UU_INPUT_BACKEND", std::env::var("UU_INPUT_BACKEND").unwrap_or_else(|_| "auto".into()));
|
||||
|
||||
println!("-- video");
|
||||
let encoder = crate::video::Encoder::requested();
|
||||
line("encoder", format!("{encoder:?} (UU_VIDEO_ENCODER={})", std::env::var("UU_VIDEO_ENCODER").unwrap_or_else(|_| "auto".into())));
|
||||
line("default codec", format!("{:?}", crate::capture_setting::preferred_codec(true, true)));
|
||||
let selection = crate::video_select::selection();
|
||||
let env_enc = std::env::var("UU_VIDEO_ENCODER").unwrap_or_else(|_| "unset".into());
|
||||
let codec = match selection.codec_pref { crate::video_select::CodecPref::Auto => "auto", crate::video_select::CodecPref::H264 => "h264", crate::video_select::CodecPref::H265 => "h265" };
|
||||
line("encoder", format!("{:?} (source={}; UU_VIDEO_ENCODER={env_enc})", selection.encoder, crate::video_select::encoder_source_label(selection.encoder_source)));
|
||||
line("codec", format!("{codec} (source={})", crate::video_select::source_label(selection.codec_source)));
|
||||
// Both codecs offered: hardware prefers H.265, CPU stays H.264 unless the preference forces one.
|
||||
let resolved = crate::capture_setting::pick_codec(true, true, crate::video_select::codec_force(selection.codec_pref), crate::video_select::hardware(selection.encoder));
|
||||
line("resolved codec", format!("{resolved:?}"));
|
||||
let mut ffmpeg = crate::video::ffmpeg_command();
|
||||
ffmpeg.args(["-hide_banner", "-encoders"]).kill_on_drop(true);
|
||||
match tokio::time::timeout(std::time::Duration::from_secs(5), ffmpeg.output()).await {
|
||||
|
||||
+5
-5
@@ -25,11 +25,11 @@ async fn black_frame(codec: VideoCodec) -> Result<Bytes, String> {
|
||||
Ok(output.stdout.into())
|
||||
}
|
||||
|
||||
pub async fn run(pc: &RTCPeerConnection, track: &VideoTrack, desktop: &PortalDesktop) -> Result<(), String> {
|
||||
run_with_encoder(pc, track, desktop, black_frame).await
|
||||
pub async fn run(pc: &RTCPeerConnection, track: &VideoTrack, desktop: &PortalDesktop, selection: crate::video_select::Selection) -> Result<(), String> {
|
||||
run_with_encoder(pc, track, desktop, selection, black_frame).await
|
||||
}
|
||||
|
||||
async fn run_with_encoder<F, Fut>(pc: &RTCPeerConnection, track: &VideoTrack, desktop: &PortalDesktop, mut encode: F) -> Result<(), String>
|
||||
async fn run_with_encoder<F, Fut>(pc: &RTCPeerConnection, track: &VideoTrack, desktop: &PortalDesktop, selection: crate::video_select::Selection, mut encode: F) -> Result<(), String>
|
||||
where F: FnMut(VideoCodec) -> Fut, Fut: std::future::Future<Output = Result<Bytes, String>> {
|
||||
// #29: iPhone 4.42.0 cold ON/OFF/ON tests only sent action15 after black video.
|
||||
// Keep CONTROL permissions truthful and use the existing video track until real capture is ready.
|
||||
@@ -55,8 +55,8 @@ where F: FnMut(VideoCodec) -> Fut, Fut: std::future::Future<Output = Result<Byte
|
||||
if !updates.borrow().waiting || *locked.borrow() != Some(true)
|
||||
|| pc.connection_state() != RTCPeerConnectionState::Connected { continue; }
|
||||
if frame.is_none() {
|
||||
let codec = crate::capture_setting::preferred_codec(
|
||||
track.supports(VideoCodec::H264), track.supports(VideoCodec::H265));
|
||||
let codec = crate::capture_setting::preferred_codec_for(
|
||||
track.supports(VideoCodec::H264), track.supports(VideoCodec::H265), selection);
|
||||
let encoded = tokio::select! {
|
||||
biased;
|
||||
_ = updates.changed() => continue,
|
||||
|
||||
@@ -206,7 +206,7 @@ async fn exercise_locked_video(codec: VideoCodec) {
|
||||
let run_host = peers.host.clone();
|
||||
let run_track = peers.track.clone();
|
||||
let run_desktop = desktop.clone();
|
||||
let task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
|
||||
let task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop, crate::video_select::selection()).await });
|
||||
|
||||
assert_ne!(peers.host.connection_state(), RTCPeerConnectionState::Connected);
|
||||
assert_no_rtp(&mut peers.packets, Duration::from_millis(150)).await;
|
||||
@@ -273,7 +273,7 @@ async fn locked_video_rechecks_state_when_encoding_finishes() {
|
||||
let (encoded_tx, mut encoded_rx) = mpsc::unbounded_channel();
|
||||
let revision = peers.track.revision();
|
||||
let task = tokio::spawn(async move {
|
||||
run_with_encoder(&run_host, &run_track, &run_desktop, |_| {
|
||||
run_with_encoder(&run_host, &run_track, &run_desktop, crate::video_select::selection(), |_| {
|
||||
let desktop = run_desktop.clone();
|
||||
let lock_tx = lock_tx.clone();
|
||||
let encoded_tx = encoded_tx.clone();
|
||||
@@ -317,7 +317,7 @@ async fn locked_video_stops_on_capture_rejection_and_terminal_peer_close() {
|
||||
let run_host = peers.host.clone();
|
||||
let run_track = peers.track.clone();
|
||||
let run_desktop = rejected_desktop.clone();
|
||||
let rejection_task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
|
||||
let rejection_task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop, crate::video_select::selection()).await });
|
||||
rejected_desktop.publish_test_screen(
|
||||
Arc::new(ScreenCapture::unavailable("synthetic capture rejection".into())),
|
||||
);
|
||||
@@ -335,7 +335,7 @@ async fn locked_video_stops_on_capture_rejection_and_terminal_peer_close() {
|
||||
let run_host = peers.host.clone();
|
||||
let run_track = peers.track.clone();
|
||||
let run_desktop = pending_desktop;
|
||||
let close_task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop).await });
|
||||
let close_task = tokio::spawn(async move { run(&run_host, &run_track, &run_desktop, crate::video_select::selection()).await });
|
||||
peers.host.close().await.unwrap();
|
||||
tokio::time::timeout(Duration::from_secs(2), close_task).await
|
||||
.expect("locked video task did not stop after terminal peer close")
|
||||
|
||||
+6
-2
@@ -382,8 +382,11 @@ mod tests {
|
||||
assert_eq!(qr_ticket(&json!({"token": "old"})), Some("old"));
|
||||
assert_eq!(qr_ticket(&json!({"status_query_ticket": ""})), None);
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn service_waits_until_a_new_token_is_saved() {
|
||||
#[test]
|
||||
fn service_waits_until_a_new_token_is_saved() {
|
||||
let _env = crate::config::test_env_lock().lock().unwrap();
|
||||
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
|
||||
rt.block_on(async {
|
||||
let dir = std::env::temp_dir().join(format!("uu-login-wait-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("config.json");
|
||||
@@ -399,6 +402,7 @@ mod tests {
|
||||
let got = tokio::time::timeout(Duration::from_secs(10), wait_for_credentials(Some("old".into()))).await.unwrap();
|
||||
assert_eq!(got.token, "new"); // the expired token in the file is not accepted
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+79
-5
@@ -30,6 +30,7 @@ mod signaling;
|
||||
mod terminal;
|
||||
mod webrtc;
|
||||
mod video;
|
||||
mod video_select;
|
||||
mod video_track;
|
||||
mod locked_video;
|
||||
#[cfg(test)]
|
||||
@@ -127,6 +128,75 @@ fn systemd_unit() -> Option<String> {
|
||||
cgroup.lines().find_map(|l| l.rsplit('/').next().filter(|u| u.ends_with(".service")).map(str::to_owned))
|
||||
}
|
||||
fn display_present() -> bool { std::env::var_os("DISPLAY").is_some() || std::env::var_os("WAYLAND_DISPLAY").is_some() }
|
||||
|
||||
fn set_video_pref() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let which = std::env::args().nth(1).unwrap_or_default();
|
||||
let stored = std::env::args().nth(2).unwrap_or_default().trim().to_ascii_lowercase();
|
||||
let stored = if stored == "hevc" { "h265".to_owned() } else { stored };
|
||||
if let Err(e) = system_actions::merge_save(|settings| {
|
||||
if which == "set-encoder" { settings.video_encoder = Some(stored.clone()); }
|
||||
else { settings.video_codec = Some(stored.clone()); }
|
||||
}) {
|
||||
// Exit 2 is reserved for bad CLI input; a settings file that cannot be merged is exit 1.
|
||||
eprintln!("{which}: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
let selection = video_select::selection();
|
||||
let codec = match selection.codec_pref { video_select::CodecPref::Auto => "auto", video_select::CodecPref::H264 => "h264", video_select::CodecPref::H265 => "h265" };
|
||||
println!("encoder={:?} source={} codec_pref={codec} source={}", selection.encoder, video_select::encoder_source_label(selection.encoder_source), video_select::source_label(selection.codec_source));
|
||||
println!("takes effect the next time a desktop connection starts");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Explicit command words for set-encoder / set-codec. Empty and whitespace are illegal.
|
||||
pub(crate) fn cli_pref_ok(which: &str, value: &str) -> bool {
|
||||
let value = value.trim();
|
||||
if value.is_empty() { return false; }
|
||||
let value = value.to_ascii_lowercase();
|
||||
match which {
|
||||
"set-encoder" => matches!(value.as_str(), "auto" | "cpu" | "vaapi" | "nvenc"),
|
||||
"set-codec" => matches!(value.as_str(), "auto" | "h264" | "h265" | "hevc"),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
const SUBCOMMANDS: [&str; 17] = ["doctor", "show", "assist", "set-encoder", "set-codec", "desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||
"wayland-input-probe", "wayland-text-probe", "clipboard-acceptance", "portal-acceptance"];
|
||||
|
||||
/// Unknown arguments and illegal set-encoder / set-codec values exit 2 (never start a second host).
|
||||
/// help / -h / --help exit 0 so main prints usage instead of starting the host.
|
||||
pub(crate) fn reject_cli(arg: Option<&str>, argc: usize, value_ok: bool) -> Option<i32> {
|
||||
if matches!(arg, Some("help" | "-h" | "--help")) { return Some(0); }
|
||||
if arg.is_some_and(|a| !SUBCOMMANDS.contains(&a)) { return Some(2); }
|
||||
if matches!(arg, Some("set-encoder" | "set-codec")) && (argc != 3 || !value_ok) { return Some(2); }
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod cli_exit_tests {
|
||||
#[test]
|
||||
fn unknown_and_illegal_subcommand_exit_2() {
|
||||
assert_eq!(super::reject_cli(Some("not-a-command"), 2, true), Some(2));
|
||||
assert_eq!(super::reject_cli(Some("set-encoder"), 3, false), Some(2));
|
||||
assert_eq!(super::reject_cli(Some("set-encoder"), 2, true), Some(2));
|
||||
assert_eq!(super::reject_cli(Some("set-codec"), 4, true), Some(2));
|
||||
assert_eq!(super::reject_cli(Some("set-encoder"), 3, true), None);
|
||||
assert_eq!(super::reject_cli(Some("set-codec"), 3, true), None);
|
||||
assert_eq!(super::reject_cli(Some("help"), 2, true), Some(0));
|
||||
assert_eq!(super::reject_cli(Some("-h"), 2, true), Some(0));
|
||||
assert_eq!(super::reject_cli(Some("--help"), 2, true), Some(0));
|
||||
assert_eq!(super::reject_cli(None, 1, true), None);
|
||||
assert!(!super::cli_pref_ok("set-encoder", ""));
|
||||
assert!(!super::cli_pref_ok("set-encoder", " "));
|
||||
assert!(!super::cli_pref_ok("set-codec", ""));
|
||||
assert!(!super::cli_pref_ok("set-codec", "\t"));
|
||||
assert!(!super::cli_pref_ok("set-encoder", "bogus"));
|
||||
assert!(super::cli_pref_ok("set-encoder", " NVENC "));
|
||||
assert!(super::cli_pref_ok("set-codec", "hevc"));
|
||||
assert_eq!(super::reject_cli(Some("set-encoder"), 3, super::cli_pref_ok("set-encoder", "")), Some(2));
|
||||
assert_eq!(super::reject_cli(Some("set-codec"), 3, super::cli_pref_ok("set-codec", " ")), Some(2));
|
||||
}
|
||||
}
|
||||
/// SIGUSR1 (`uuremote-server show`) opens the host window.
|
||||
fn present_on_usr1(ui: std::sync::Arc<assist::LocalUi>) {
|
||||
// Install the handler before publishing the pid: SIGUSR1's default action terminates the process.
|
||||
@@ -226,16 +296,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.filter_module("uuremote_server", log::LevelFilter::Info).init();
|
||||
} else { env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); }
|
||||
// An unknown argument must never fall through to starting a second full host server.
|
||||
const SUBCOMMANDS: [&str; 15] = ["doctor", "show", "assist", "desktop-input-probe", "virtual-audio-probe", "login", "unlock-check", "desktop-probe", "desktop-video-probe", "desktop-audio-probe", "wayland-capture",
|
||||
"wayland-input-probe", "wayland-text-probe", "clipboard-acceptance", "portal-acceptance"];
|
||||
if let Some(arg) = std::env::args().nth(1).filter(|a| !SUBCOMMANDS.contains(&a.as_str())) {
|
||||
let arg1 = std::env::args().nth(1);
|
||||
let value_ok = match arg1.as_deref() {
|
||||
Some(which @ ("set-encoder" | "set-codec")) => cli_pref_ok(which, &std::env::args().nth(2).unwrap_or_default()),
|
||||
_ => true,
|
||||
};
|
||||
if let Some(code) = reject_cli(arg1.as_deref(), std::env::args().len(), value_ok) {
|
||||
let usage = format!("usage: uuremote-server [{}]\n (no argument: run the host)", SUBCOMMANDS.join(" | "));
|
||||
if matches!(arg.as_str(), "help" | "-h" | "--help") { println!("{usage}"); return Ok(()); }
|
||||
if code == 0 { println!("{usage}"); return Ok(()); }
|
||||
eprintln!("unknown argument\n{usage}");
|
||||
std::process::exit(2);
|
||||
std::process::exit(code);
|
||||
}
|
||||
|
||||
if std::env::args().nth(1).as_deref() == Some("show") { return show_running_host(); }
|
||||
if matches!(std::env::args().nth(1).as_deref(), Some("set-encoder") | Some("set-codec")) { return set_video_pref(); }
|
||||
if assist_enabled && !unified {
|
||||
if std::env::args().len() > 2 { return Err("assist accepts no additional arguments; configure it with a private UU_CONFIG".into()); }
|
||||
assist::configure()?;
|
||||
|
||||
+159
-12
@@ -25,11 +25,27 @@ pub const SET_AUTO_LOCK: u64 = 19;
|
||||
/// Host settings from the viewer's 安全 menu. The viewer sends them only when a switch is toggled
|
||||
/// and shows what SystemMetrics reports, so they are host-wide and persisted (like the official
|
||||
/// host), not per connection: a reconnect must still lock / mute.
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
struct Settings {
|
||||
#[derive(Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
pub(crate) struct Settings {
|
||||
#[serde(default)] auto_lock: bool,
|
||||
#[serde(default)] keep_mute: bool,
|
||||
#[serde(default)] mute_permanent: bool,
|
||||
/// "auto" or absent does not override the environment. A non-string value becomes None so a bad
|
||||
/// video field cannot drop auto_lock / mute when the document is loaded.
|
||||
#[serde(default, deserialize_with = "lenient_string")] pub(crate) video_encoder: Option<String>,
|
||||
#[serde(default, deserialize_with = "lenient_string")] pub(crate) video_codec: Option<String>,
|
||||
}
|
||||
|
||||
fn lenient_string<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<Option<String>, D::Error> {
|
||||
use serde::Deserialize;
|
||||
match Option::<serde_json::Value>::deserialize(deserializer)? {
|
||||
Some(serde_json::Value::String(s)) => Ok(Some(s)),
|
||||
Some(_) => {
|
||||
log::warn!("Ignoring non-string video_encoder/video_codec in host settings");
|
||||
Ok(None)
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
struct HostState {
|
||||
@@ -39,22 +55,83 @@ struct HostState {
|
||||
desktop_sessions: usize,
|
||||
}
|
||||
|
||||
fn settings_path() -> std::path::PathBuf { crate::config::path().with_file_name("host_settings.json") }
|
||||
pub(crate) fn settings_path() -> std::path::PathBuf { crate::config::path().with_file_name("host_settings.json") }
|
||||
|
||||
pub(crate) fn load_settings() -> Settings {
|
||||
std::fs::read(settings_path()).ok().and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Re-read, apply `patch`, write via a temp file and rename (mode 0600).
|
||||
pub(crate) fn merge_save(patch: impl FnOnce(&mut Settings)) -> Result<Settings, String> {
|
||||
merge_write(patch)?;
|
||||
let settings = load_settings();
|
||||
if let Ok(mut guard) = state().lock() { guard.settings = settings.clone(); }
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
fn state() -> &'static Mutex<HostState> {
|
||||
static STATE: std::sync::OnceLock<Mutex<HostState>> = std::sync::OnceLock::new();
|
||||
STATE.get_or_init(|| {
|
||||
let settings = std::fs::read(settings_path()).ok()
|
||||
.and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or_default();
|
||||
let settings = load_settings();
|
||||
Mutex::new(HostState { settings, restore: None, desktop_sessions: 0 })
|
||||
})
|
||||
}
|
||||
|
||||
fn save(settings: &Settings) {
|
||||
/// Re-read, copy only the security switches from `settings`, keep video_* from disk.
|
||||
fn save(settings: &Settings) -> Result<(), String> {
|
||||
merge_write(|disk| {
|
||||
disk.auto_lock = settings.auto_lock;
|
||||
disk.keep_mute = settings.keep_mute;
|
||||
disk.mute_permanent = settings.mute_permanent;
|
||||
})
|
||||
}
|
||||
|
||||
/// Held across read-patch-write-rename so set-* and a security-toggle save cannot clobber each other.
|
||||
struct SettingsLock { file: std::fs::File }
|
||||
impl SettingsLock {
|
||||
fn acquire(path: &std::path::Path) -> Result<Self, String> {
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
let lock_path = path.with_file_name("host_settings.json.lock");
|
||||
let file = std::fs::OpenOptions::new().read(true).write(true).create(true).mode(0o600).open(&lock_path).map_err(|e| e.kind().to_string())?;
|
||||
let rc = unsafe { libc::flock(std::os::unix::io::AsRawFd::as_raw_fd(&file), libc::LOCK_EX) };
|
||||
if rc != 0 { return Err(std::io::Error::last_os_error().kind().to_string()); }
|
||||
Ok(Self { file })
|
||||
}
|
||||
}
|
||||
impl Drop for SettingsLock {
|
||||
fn drop(&mut self) {
|
||||
unsafe { libc::flock(std::os::unix::io::AsRawFd::as_raw_fd(&self.file), libc::LOCK_UN); }
|
||||
}
|
||||
}
|
||||
|
||||
/// NotFound starts from defaults. Any other read or parse error leaves the file untouched:
|
||||
/// a bad document must not be rewritten with security switches forced off.
|
||||
fn read_for_merge(path: &std::path::Path) -> Result<Settings, String> {
|
||||
match std::fs::read(path) {
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Settings::default()),
|
||||
Err(e) => Err(format!("host settings unreadable: {}", e.kind())),
|
||||
// serde_json::Error's Display quotes the offending value; the journal and set-* must not.
|
||||
Ok(bytes) => serde_json::from_slice(&bytes).map_err(|e| match (e.line(), e.column()) {
|
||||
(0, 0) => "host_settings.json is not valid".to_owned(),
|
||||
(line, column) => format!("host_settings.json is not valid (line {line}, column {column})"),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_write(patch: impl FnOnce(&mut Settings)) -> Result<(), String> {
|
||||
use std::io::Write;
|
||||
use std::os::unix::fs::OpenOptionsExt;
|
||||
let path = settings_path();
|
||||
let result = serde_json::to_vec_pretty(settings).map_err(|e| e.to_string())
|
||||
.and_then(|json| std::fs::write(&path, json).map_err(|e| e.to_string()));
|
||||
if let Err(e) = result { log::warn!("Host settings not saved: {}", e); }
|
||||
if let Some(dir) = path.parent() { std::fs::create_dir_all(dir).map_err(|e| e.kind().to_string())?; }
|
||||
let _lock = SettingsLock::acquire(&path)?;
|
||||
let mut disk = read_for_merge(&path)?;
|
||||
patch(&mut disk);
|
||||
let json = serde_json::to_vec_pretty(&disk).map_err(|e| e.to_string())?;
|
||||
let tmp = path.with_extension(format!("json.{}.tmp", std::process::id()));
|
||||
let mut file = std::fs::OpenOptions::new().write(true).create(true).truncate(true).mode(0o600).open(&tmp).map_err(|e| e.kind().to_string())?;
|
||||
file.write_all(&json).map_err(|e| e.kind().to_string())?;
|
||||
std::fs::rename(&tmp, &path).map_err(|e| e.kind().to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Handle for one desktop connection (system actions are host-wide).
|
||||
@@ -142,7 +219,7 @@ impl PeerActions {
|
||||
_ => {}
|
||||
}
|
||||
let mut state = state().lock().unwrap();
|
||||
let before = state.settings;
|
||||
let before = state.settings.clone();
|
||||
match action {
|
||||
// 静音运行 on: the viewer sends MUTE_SYSTEM + MUTE_TEMPORARY (observed), off: MUTE_RESTORE.
|
||||
MUTE_SYSTEM | MUTE_TEMPORARY => {
|
||||
@@ -172,7 +249,7 @@ impl PeerActions {
|
||||
}
|
||||
_ => return false,
|
||||
}
|
||||
if state.settings != before { save(&state.settings); }
|
||||
if state.settings != before { if let Err(e) = save(&state.settings) { log::warn!("Host settings not saved: {e}"); } }
|
||||
true
|
||||
}
|
||||
|
||||
@@ -205,7 +282,7 @@ impl PeerActions {
|
||||
pub fn metrics_payload(&self, sound: bool, desktop: crate::system_metrics::Permission) -> Vec<u8> {
|
||||
use crate::system_metrics::payload_with_settings;
|
||||
let home = std::env::var("HOME").unwrap_or_else(|_| "/".into());
|
||||
let current = state().lock().unwrap().settings;
|
||||
let current = state().lock().unwrap().settings.clone();
|
||||
payload_with_settings(desktop, desktop, desktop, &home, &crate::system_metrics::Settings {
|
||||
sound, keep_mute: current.keep_mute, mute_permanent: current.mute_permanent, auto_lock: current.auto_lock,
|
||||
})
|
||||
@@ -274,6 +351,76 @@ mod tests {
|
||||
assert!(plugin_response(&decode_wire(&[10, 2, 8, 9]).unwrap()).is_none());
|
||||
}
|
||||
#[test]
|
||||
fn merge_save_keeps_security_fields_and_mode() {
|
||||
let _guard = crate::config::test_env_lock().lock().unwrap();
|
||||
let dir = std::env::temp_dir().join(format!("uu-settings-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let cfg = dir.join("config.json");
|
||||
std::env::set_var("UU_CONFIG", &cfg);
|
||||
let path = settings_path();
|
||||
std::fs::write(&path, br#"{"auto_lock":true,"keep_mute":true,"mute_permanent":false}"#).unwrap();
|
||||
let saved = merge_save(|s| s.video_encoder = Some("vaapi".into())).unwrap();
|
||||
assert!(saved.auto_lock && saved.keep_mute && !saved.mute_permanent);
|
||||
assert_eq!(saved.video_encoder.as_deref(), Some("vaapi"));
|
||||
let again = merge_save(|s| s.video_codec = Some("h264".into())).unwrap();
|
||||
assert_eq!(again.video_encoder.as_deref(), Some("vaapi"));
|
||||
assert_eq!(again.video_codec.as_deref(), Some("h264"));
|
||||
assert!(again.auto_lock);
|
||||
let mode = { use std::os::unix::fs::PermissionsExt; std::fs::metadata(&path).unwrap().permissions().mode() & 0o777 };
|
||||
assert_eq!(mode, 0o600);
|
||||
// A mute/lock save must not drop video_* written after the in-memory copy was taken.
|
||||
let mut memory = load_settings();
|
||||
std::fs::write(&path, br#"{"auto_lock":true,"keep_mute":true,"mute_permanent":false,"video_encoder":"nvenc","video_codec":"h265"}"#).unwrap();
|
||||
memory.keep_mute = false;
|
||||
save(&memory).unwrap();
|
||||
let disk: serde_json::Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
|
||||
assert_eq!(disk["video_encoder"], "nvenc");
|
||||
assert_eq!(disk["video_codec"], "h265");
|
||||
assert_eq!(disk["keep_mute"], false);
|
||||
assert_eq!(disk["auto_lock"], true);
|
||||
// A non-string video field must not fail the whole document (security switches survive).
|
||||
std::fs::write(&path, br#"{"auto_lock":true,"keep_mute":false,"mute_permanent":true,"video_encoder":1,"video_codec":{"x":1}}"#).unwrap();
|
||||
let loaded = load_settings();
|
||||
assert!(loaded.auto_lock && !loaded.keep_mute && loaded.mute_permanent);
|
||||
assert!(loaded.video_encoder.is_none() && loaded.video_codec.is_none());
|
||||
merge_save(|s| s.video_encoder = Some("cpu".into())).unwrap();
|
||||
let disk: serde_json::Value = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
|
||||
assert_eq!(disk["auto_lock"], true);
|
||||
assert_eq!(disk["mute_permanent"], true);
|
||||
assert_eq!(disk["video_encoder"], "cpu");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::env::remove_var("UU_CONFIG");
|
||||
}
|
||||
#[test]
|
||||
fn merge_write_refuses_malformed_or_unreadable_settings() {
|
||||
let _guard = crate::config::test_env_lock().lock().unwrap();
|
||||
let dir = std::env::temp_dir().join(format!("uu-settings-bad-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::env::set_var("UU_CONFIG", dir.join("config.json"));
|
||||
let path = settings_path();
|
||||
let malformed = br#"{"auto_lock":true, "keep_mute":"#;
|
||||
std::fs::write(&path, malformed).unwrap();
|
||||
let err = merge_write(|s| s.video_encoder = Some("cpu".into())).unwrap_err();
|
||||
assert!(err.contains("not valid"), "{err}");
|
||||
assert_eq!(std::fs::read(&path).unwrap(), malformed);
|
||||
let bad_type = br#"{"auto_lock":"yes","keep_mute":false,"mute_permanent":false}"#;
|
||||
std::fs::write(&path, bad_type).unwrap();
|
||||
let err = merge_write(|s| s.video_codec = Some("h265".into())).unwrap_err();
|
||||
assert!(err.contains("not valid"), "{err}");
|
||||
assert!(err.contains("line ") && err.contains("column "), "{err}");
|
||||
assert!(!err.contains("yes"), "{err}");
|
||||
assert_eq!(std::fs::read(&path).unwrap(), bad_type);
|
||||
std::fs::remove_file(&path).unwrap();
|
||||
std::fs::create_dir(&path).unwrap();
|
||||
let err = merge_write(|s| s.video_encoder = Some("cpu".into())).unwrap_err();
|
||||
assert!(err.contains("unreadable"), "{err}");
|
||||
assert!(path.is_dir());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::env::remove_var("UU_CONFIG");
|
||||
}
|
||||
#[test]
|
||||
fn args_shape_never_contains_values() {
|
||||
let shape = args_shape(br#"{"enable":true,"secret":"abc"}"#);
|
||||
assert!(shape.contains("enable") && !shape.contains("abc"));
|
||||
|
||||
+90
-62
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||
use std::ffi::OsString;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::process::Command;
|
||||
@@ -177,16 +177,9 @@ impl CompleteFrame {
|
||||
|
||||
const RTP_CLOCK: u64 = 90_000;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
|
||||
pub(crate) enum Encoder { Cpu, Nvenc, Vaapi }
|
||||
impl Encoder {
|
||||
pub(crate) fn requested() -> Self { Self::from_setting(std::env::var("UU_VIDEO_ENCODER").ok().as_deref(), nvidia_present()) }
|
||||
/// Unset/auto tries NVENC only with an NVIDIA driver loaded: `ffmpeg -encoders` lists h264_nvenc
|
||||
/// on any machine, so without this every stream start first ran a doomed NVENC process (#10).
|
||||
fn from_setting(value: Option<&str>, nvidia: bool) -> Self {
|
||||
match value { Some("cpu") => Self::Cpu, Some("vaapi") => Self::Vaapi, Some("nvenc") => Self::Nvenc,
|
||||
_ => if nvidia { Self::Nvenc } else { Self::Cpu } }
|
||||
}
|
||||
pub(crate) fn name_for(self, codec: VideoCodec) -> &'static str {
|
||||
match (self, codec) {
|
||||
(Self::Cpu, VideoCodec::H264) => "libx264", (Self::Cpu, VideoCodec::H265) => "libx265",
|
||||
@@ -195,14 +188,12 @@ impl Encoder {
|
||||
}
|
||||
}
|
||||
}
|
||||
fn nvidia_present() -> bool {
|
||||
pub(crate) fn nvidia_present() -> bool {
|
||||
std::path::Path::new("/proc/driver/nvidia/version").exists() || std::path::Path::new("/dev/nvidia0").exists()
|
||||
}
|
||||
/// 真彩 (4:4:4): NVENC (in-process or FFmpeg) and libx264; h264_vaapi is 4:2:0 only.
|
||||
/// A GPU without 4:4:4 NVENC falls back to libx264, which keeps 4:4:4.
|
||||
pub fn full_chroma_available() -> bool {
|
||||
Encoder::requested() != Encoder::Vaapi
|
||||
}
|
||||
pub(crate) fn full_chroma_for(encoder: Encoder) -> bool { encoder != Encoder::Vaapi }
|
||||
|
||||
/// (-profile:v, -pix_fmt) for NVENC / libx264 / libx265: 4:4:4 (真彩) needs a 4:4:4 profile.
|
||||
fn chroma_args(settings: &VideoSettings, encoder: Encoder) -> (&'static str, &'static str) {
|
||||
@@ -430,33 +421,50 @@ async fn ffmpeg_timing_mode(spec: &FfmpegSpec) -> FfmpegTimingMode {
|
||||
*cell.get_or_init(|| probe_timing_mode(spec)).await
|
||||
}
|
||||
|
||||
type HevcCache = Mutex<HashMap<(FfmpegSpec, Encoder), Arc<tokio::sync::OnceCell<bool>>>>;
|
||||
|
||||
fn hevc_cache() -> &'static HevcCache {
|
||||
static CACHE: OnceLock<HevcCache> = OnceLock::new();
|
||||
CACHE.get_or_init(|| Mutex::new(HashMap::new()))
|
||||
}
|
||||
|
||||
fn hevc_cell(cache: &HevcCache, spec: &FfmpegSpec, encoder: Encoder) -> Arc<tokio::sync::OnceCell<bool>> {
|
||||
let mut cache = cache.lock().expect("HEVC probe cache poisoned");
|
||||
cache.entry((spec.clone(), encoder)).or_insert_with(|| Arc::new(tokio::sync::OnceCell::const_new())).clone()
|
||||
}
|
||||
|
||||
/// A compiled-in encoder name does not prove that its driver can initialize.
|
||||
/// Probe one synthetic 4:2:0 frame once per process before advertising HEVC.
|
||||
pub async fn hevc_available() -> bool {
|
||||
static AVAILABLE: tokio::sync::OnceCell<bool> = tokio::sync::OnceCell::const_new();
|
||||
*AVAILABLE.get_or_init(|| async {
|
||||
let requested = Encoder::requested();
|
||||
let mut candidates = vec![requested];
|
||||
if requested != Encoder::Cpu { candidates.push(Encoder::Cpu); }
|
||||
for encoder in candidates {
|
||||
let settings = VideoSettings { codec: VideoCodec::H265, ..Default::default() };
|
||||
let mut command = ffmpeg_command();
|
||||
command.args(["-hide_banner", "-loglevel", "error", "-nostdin"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||
command.args(["-f", "lavfi", "-i", "color=black:size=256x256:rate=1", "-frames:v", "1", "-an"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vf", &vaapi_filter(None)]); }
|
||||
command.args(encoder_options(&settings, encoder, "30", None));
|
||||
command.args(["-f", "hevc", "pipe:1"]).kill_on_drop(true).stderr(std::process::Stdio::null());
|
||||
if let Ok(Ok(output)) = tokio::time::timeout(Duration::from_secs(5), command.output()).await {
|
||||
if output.status.success() && is_independent_frame_for(&output.stdout, VideoCodec::H265) {
|
||||
log::info!("HEVC encoder probe: {} ok", encoder.name_for(VideoCodec::H265));
|
||||
return true;
|
||||
}
|
||||
/// Probe one synthetic 4:2:0 frame per resolved encoder and FFmpeg spec. The caller
|
||||
/// passes the session's encoder so capability matches that snapshot. set-encoder
|
||||
/// changes the snapshot for the next session, so a process-wide bool would keep advertising
|
||||
/// the first session's result.
|
||||
pub async fn hevc_available(requested: Encoder) -> bool {
|
||||
let spec = ffmpeg_spec();
|
||||
let cell = hevc_cell(hevc_cache(), &spec, requested);
|
||||
*cell.get_or_init(|| probe_hevc(spec, requested)).await
|
||||
}
|
||||
|
||||
async fn probe_hevc(spec: FfmpegSpec, requested: Encoder) -> bool {
|
||||
let mut candidates = vec![requested];
|
||||
if requested != Encoder::Cpu { candidates.push(Encoder::Cpu); }
|
||||
for encoder in candidates {
|
||||
let settings = VideoSettings { codec: VideoCodec::H265, ..Default::default() };
|
||||
let mut command = spec.command();
|
||||
command.args(["-hide_banner", "-loglevel", "error", "-nostdin"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vaapi_device", &vaapi_device()]); }
|
||||
command.args(["-f", "lavfi", "-i", "color=black:size=256x256:rate=1", "-frames:v", "1", "-an"]);
|
||||
if encoder == Encoder::Vaapi { command.args(["-vf", &vaapi_filter(None)]); }
|
||||
command.args(encoder_options(&settings, encoder, "30", None));
|
||||
command.args(["-f", "hevc", "pipe:1"]).kill_on_drop(true).stderr(std::process::Stdio::null());
|
||||
if let Ok(Ok(output)) = tokio::time::timeout(Duration::from_secs(5), command.output()).await {
|
||||
if output.status.success() && is_independent_frame_for(&output.stdout, VideoCodec::H265) {
|
||||
log::info!("HEVC encoder probe: {} ok", encoder.name_for(VideoCodec::H265));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
log::info!("HEVC encoder probe: unavailable");
|
||||
false
|
||||
}).await
|
||||
}
|
||||
log::info!("HEVC encoder probe: unavailable");
|
||||
false
|
||||
}
|
||||
|
||||
/// Round UP (+1ns float guard) so webrtc's floor(duration*90000) yields exactly `ticks`.
|
||||
@@ -520,25 +528,27 @@ async fn read_encoder(
|
||||
size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
||||
latest: mpsc::Sender<Arc<CompleteFrame>>,
|
||||
settings: VideoSettings, sequence: Arc<std::sync::atomic::AtomicU64>, idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
|
||||
selection: crate::video_select::Selection,
|
||||
) -> Result<(), String> {
|
||||
// The sequence is shared so a CPU fallback or settings restart continues it (skip statistics stay correct).
|
||||
let encoder = Encoder::requested();
|
||||
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,true,sequence,settings,idr,budget).await; }
|
||||
// `selection` is fixed for this desktop connection (taken where the codec is negotiated).
|
||||
let encoder = selection.encoder;
|
||||
if encoder == Encoder::Cpu { return read_encoder_with(size,display,portal,latest,Encoder::Cpu,true,sequence,settings,idr,budget,selection).await; }
|
||||
#[cfg_attr(not(feature = "inproc-av"), allow(unused_mut))]
|
||||
let mut result = read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,true,sequence.clone(),settings,idr.clone(),budget.clone()).await;
|
||||
let mut result = read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,true,sequence.clone(),settings,idr.clone(),budget.clone(),selection).await;
|
||||
// A failed native NVENC session (old driver, unsupported mode) still leaves FFmpeg's NVENC process pipeline.
|
||||
#[cfg(feature = "inproc-av")]
|
||||
if let Err(error) = &result {
|
||||
if !latest.is_closed() && native_inproc(&display, portal.is_some(), encoder) && x11_resized(&display, portal.is_some(), size).is_none() {
|
||||
log::warn!("In-process {} stream unavailable; switching to the FFmpeg process pipeline: {}",encoder.name_for(settings.codec),error);
|
||||
result = read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,false,sequence.clone(),settings,idr.clone(),budget.clone()).await;
|
||||
result = read_encoder_with(size,display.clone(),portal.clone(),latest.clone(),encoder,false,sequence.clone(),settings,idr.clone(),budget.clone(),selection).await;
|
||||
}
|
||||
}
|
||||
match result {
|
||||
Ok(())=>Ok(()),
|
||||
Err(error) if !latest.is_closed() && x11_resized(&display, portal.is_some(), size).is_none()=>{
|
||||
log::warn!("{} stream unavailable; switching scoped encoder to CPU: {}",encoder.name_for(settings.codec),error);
|
||||
read_encoder_with(size,display,portal,latest,Encoder::Cpu,true,sequence,settings,idr,budget).await
|
||||
read_encoder_with(size,display,portal,latest,Encoder::Cpu,true,sequence,settings,idr,budget,selection).await
|
||||
},
|
||||
Err(error)=>Err(error),
|
||||
}
|
||||
@@ -624,6 +634,7 @@ async fn read_encoder_with(
|
||||
size: (u32,u32),display:String,portal:Option<Arc<crate::portal::PortalSession>>,latest:mpsc::Sender<Arc<CompleteFrame>>,encoder:Encoder,
|
||||
#[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] allow_inproc: bool,sequence:Arc<std::sync::atomic::AtomicU64>,
|
||||
settings: VideoSettings, #[cfg_attr(not(feature = "inproc-av"), allow(unused_variables))] idr: Arc<AtomicBool>, budget: Arc<Semaphore>,
|
||||
selection: crate::video_select::Selection,
|
||||
)->Result<(),String>{
|
||||
let selected_output=if display.starts_with("wayland:") {crate::wayland::source(settings.source_id)} else {None};
|
||||
let size=selected_output.map_or(size,|s|(s.width,s.height));
|
||||
@@ -645,8 +656,9 @@ async fn read_encoder_with(
|
||||
max_bps: max_kbps * 1000, buffer_bits: i64::from(buffer_for(&settings, encoder)) * 1000, codec: settings.codec, chroma: settings.chroma,
|
||||
vaapi: encoder == Encoder::Vaapi,
|
||||
};
|
||||
log::info!("Desktop encoder: display={} source={}x{} output={:?} fps={} cq={} maxrate={}k encoder={} pipeline=in-process",
|
||||
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name_for(settings.codec));
|
||||
log::info!("Desktop encoder: display={} source={}x{} output={:?} fps={} cq={} maxrate={}k encoder={} source={} pipeline=in-process",
|
||||
display,size.0,size.1,settings.output,settings.fps,config.cq,max_kbps,encoder.name_for(settings.codec),
|
||||
crate::video_select::encoder_source_label(selection.encoder_source));
|
||||
return read_inprocess(config, latest, sequence, idr, budget).await;
|
||||
}
|
||||
let source_size = size;
|
||||
@@ -658,7 +670,7 @@ async fn read_encoder_with(
|
||||
let cq = settings.cq().to_string();
|
||||
// x11grab delivers bgr0; the Wayland/portal/KMS pipe is bgra.
|
||||
let rgb = nvenc_rgb_input(&settings, encoder, if wayland { "bgra" } else { "bgr0" });
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} pix_fmt={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name_for(settings.codec),if encoder == Encoder::Vaapi { "nv12" } else { rgb.unwrap_or(chroma_args(&settings, encoder).1) });
|
||||
log::info!("Desktop encoder: display={} source={} output={:?} fps={} cq={} maxrate={} encoder={} source={} pix_fmt={} low_latency=true",display,size,settings.output,settings.fps,cq,maxrate,encoder.name_for(settings.codec),crate::video_select::encoder_source_label(selection.encoder_source),if encoder == Encoder::Vaapi { "nv12" } else { rgb.unwrap_or(chroma_args(&settings, encoder).1) });
|
||||
// A bundled FFmpeg prefix (config ffmpeg_root / UU_FFMPEG_ROOT) or `ffmpeg` on PATH.
|
||||
let ffmpeg = ffmpeg_spec();
|
||||
let mut command = ffmpeg.command();
|
||||
@@ -820,15 +832,15 @@ fn probe_settings() -> Result<VideoSettings,String> {
|
||||
};
|
||||
let mut settings=VideoSettings {fps,quality,codec:probe_codec(),..VideoSettings::default()};
|
||||
// UU_PROBE_CHROMA=444 requests 真彩 like the viewer does (only where full_chroma_available()).
|
||||
if std::env::var("UU_PROBE_CHROMA").as_deref()==Ok("444") && full_chroma_available() { settings.chroma=crate::capture_setting::CHROMA_444; }
|
||||
if std::env::var("UU_PROBE_CHROMA").as_deref()==Ok("444") && full_chroma_for(crate::video_select::selection().encoder) { settings.chroma=crate::capture_setting::CHROMA_444; }
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
/// The probe follows `UU_VIDEO_CODEC` so the HEVC path can be measured at all (H.264 otherwise),
|
||||
/// and its first access unit is then checked against that codec instead of always H.264.
|
||||
fn probe_codec() -> VideoCodec {
|
||||
match std::env::var("UU_VIDEO_CODEC").ok().as_deref().map(str::to_ascii_lowercase).as_deref() {
|
||||
Some("h265") | Some("hevc") => VideoCodec::H265,
|
||||
match crate::video_select::codec_force(crate::video_select::selection().codec_pref) {
|
||||
Some("h265") => VideoCodec::H265,
|
||||
_ => VideoCodec::H264,
|
||||
}
|
||||
}
|
||||
@@ -871,7 +883,7 @@ pub async fn probe_input(input:OsString,path:Option<String>)->Result<(),String>
|
||||
if wanted==0 { return Err("Fixed-input UU_PROBE_FRAMES must be positive".into()); }
|
||||
let spec=ffmpeg_spec();
|
||||
let size=probe_input_size(&spec,&input).await?;
|
||||
let encoder=Encoder::requested();
|
||||
let encoder = crate::video_select::selection().encoder;
|
||||
let (maxrate,_)=rate_args(&settings,encoder);
|
||||
let output=settings.output.unwrap_or(size);
|
||||
println!("Fixed input probe: source={}x{} output={}x{} fps={} cq={} maxrate={} encoder={}",
|
||||
@@ -923,7 +935,8 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
|
||||
let (tx,mut rx)=mpsc::channel(4);
|
||||
let sequence=Arc::new(std::sync::atomic::AtomicU64::new(0));
|
||||
let settings=probe_settings()?;
|
||||
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)),Arc::new(Semaphore::new(4)))));
|
||||
let selection = crate::video_select::selection();
|
||||
let _reader=EncoderTask(tokio::spawn(read_encoder(size,display,portal,tx,settings,sequence,Arc::new(AtomicBool::new(false)),Arc::new(Semaphore::new(4)),selection)));
|
||||
let mut count=0;
|
||||
let mut bytes=0;
|
||||
// UU_PROBE_FRAMES lengthens the sample for baseline measurements; default is 5 frames.
|
||||
@@ -942,7 +955,8 @@ pub async fn probe(size:(u32,u32),display:String,portal:Option<Arc<crate::portal
|
||||
}
|
||||
|
||||
pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_track::VideoTrack>, mut size: (u32, u32), display: String, portal:Option<Arc<crate::portal::PortalSession>>,
|
||||
mut settings: tokio::sync::watch::Receiver<VideoSettings>, applied: Arc<tokio::sync::watch::Sender<VideoSettings>>, idr: Arc<AtomicBool>) -> Result<(), String> {
|
||||
mut settings: tokio::sync::watch::Receiver<VideoSettings>, applied: Arc<tokio::sync::watch::Sender<VideoSettings>>, idr: Arc<AtomicBool>,
|
||||
selection: crate::video_select::Selection) -> Result<(), String> {
|
||||
let _desktop_mode = crate::desktop_mode::StreamGuard::new();
|
||||
loop {
|
||||
if transport_closed(&pc) { return Ok(()); }
|
||||
@@ -963,7 +977,8 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
applied.send_replace(current);
|
||||
settings.borrow_and_update();
|
||||
}
|
||||
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone())));
|
||||
// Reader restarts reuse this snapshot, so set-encoder waits for the next desktop connection.
|
||||
let mut reader = EncoderTask(tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone(), selection)));
|
||||
let mut previous = None;
|
||||
let mut timeline = RtpTimeline { fps: u64::from(current.fps), ..Default::default() };
|
||||
let mut settings_open = true;
|
||||
@@ -989,9 +1004,9 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
reader.0.abort();
|
||||
let _ = (&mut reader.0).await;
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone(), budget.clone()));
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone(), budget.clone(), selection));
|
||||
} else {
|
||||
reader.restart(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone(), budget.clone())).await;
|
||||
reader.restart(read_encoder(size, display.clone(), portal.clone(), latest.clone(), next, sequence.clone(), idr.clone(), budget.clone(), selection)).await;
|
||||
}
|
||||
send_interrupted=false;
|
||||
}
|
||||
@@ -1002,7 +1017,7 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
log::info!("Desktop geometry changed during the stream ({}x{} -> {}x{}); restarting capture", size.0, size.1, resized.0, resized.1);
|
||||
size = resized;
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone()));
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone(), selection));
|
||||
continue;
|
||||
}
|
||||
if let (Err(error), VideoCodec::H265, true) = (&result, current.codec, track.supports(VideoCodec::H264)) {
|
||||
@@ -1010,7 +1025,7 @@ pub async fn stream_desktop(pc: Arc<RTCPeerConnection>, track: Arc<crate::video_
|
||||
applied.send_modify(|value| value.codec = VideoCodec::H264);
|
||||
current = *settings.borrow_and_update();
|
||||
while receiver.try_recv().is_ok() {}
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone()));
|
||||
reader.0 = tokio::spawn(read_encoder(size, display.clone(), portal.clone(), latest.clone(), current, sequence.clone(), idr.clone(), budget.clone(), selection));
|
||||
continue;
|
||||
}
|
||||
if result.is_err() && !crate::wayland::outputs().is_empty() && !crate::wayland::input_ready() {
|
||||
@@ -1297,11 +1312,6 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn vaapi_is_explicit_and_upload_follows_scale() {
|
||||
assert_eq!(Encoder::from_setting(None, true), Encoder::Nvenc);
|
||||
assert_eq!(Encoder::from_setting(None, false), Encoder::Cpu);
|
||||
assert_eq!(Encoder::from_setting(Some("nvenc"), false), Encoder::Nvenc);
|
||||
assert_eq!(Encoder::from_setting(Some("cpu"), true), Encoder::Cpu);
|
||||
assert_eq!(Encoder::from_setting(Some("vaapi"), true), Encoder::Vaapi);
|
||||
assert_eq!(vaapi_filter(None), "format=nv12,hwupload");
|
||||
assert_eq!(vaapi_filter(Some((1280,720))), "scale=1280:720:flags=bilinear,format=nv12,hwupload");
|
||||
let options = vaapi_options(&VideoSettings::default(), "30");
|
||||
@@ -1431,7 +1441,25 @@ mod tests {
|
||||
#[tokio::test]
|
||||
#[ignore = "requires installed FFmpeg with a working HEVC encoder"]
|
||||
async fn hevc_capability_probe_initializes_real_encoder() {
|
||||
assert!(hevc_available().await);
|
||||
assert!(hevc_available(crate::video_select::selection().encoder).await);
|
||||
}
|
||||
#[tokio::test]
|
||||
async fn hevc_probe_cache_is_keyed_by_encoder_and_ffmpeg_spec() {
|
||||
let cache = Mutex::new(HashMap::new());
|
||||
let spec = FfmpegSpec { program: PathBuf::from("ffmpeg"), ld_library_path: None, path: None };
|
||||
let other = FfmpegSpec { program: PathBuf::from("/opt/ffmpeg/bin/ffmpeg"), ld_library_path: None, path: None };
|
||||
let calls = Arc::new(AtomicUsize::new(0));
|
||||
let lookup = |spec: &FfmpegSpec, encoder: Encoder, result: bool| {
|
||||
let cell = hevc_cell(&cache, spec, encoder);
|
||||
let calls = calls.clone();
|
||||
async move { *cell.get_or_init(|| async move { calls.fetch_add(1, Ordering::SeqCst); result }).await }
|
||||
};
|
||||
assert!(lookup(&spec, Encoder::Cpu, true).await);
|
||||
assert!(lookup(&spec, Encoder::Cpu, false).await);
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||
assert!(!lookup(&spec, Encoder::Nvenc, false).await);
|
||||
assert!(lookup(&other, Encoder::Cpu, true).await);
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 3);
|
||||
}
|
||||
#[test]
|
||||
fn hevc_annex_b_boundaries_and_irap_use_two_byte_headers() {
|
||||
|
||||
@@ -0,0 +1,244 @@
|
||||
//! Runtime encoder / codec preference. host_settings.json beats the environment,
|
||||
//! and "auto" or a missing field falls through. Illegal values are ignored.
|
||||
//! Auto keeps the v0.2.23 rule (NVENC when an NVIDIA driver is present, otherwise CPU).
|
||||
|
||||
use crate::video::Encoder;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Source {
|
||||
Setting,
|
||||
Env,
|
||||
Auto,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum CodecPref {
|
||||
Auto,
|
||||
H264,
|
||||
H265,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Selection {
|
||||
pub encoder: Encoder,
|
||||
pub encoder_source: Source,
|
||||
pub codec_pref: CodecPref,
|
||||
pub codec_source: Source,
|
||||
}
|
||||
|
||||
pub fn parse_encoder(value: Option<&str>) -> Option<Option<Encoder>> {
|
||||
match value
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| s.to_ascii_lowercase())
|
||||
{
|
||||
None => Some(None),
|
||||
Some(v) if v == "auto" => Some(None),
|
||||
Some(v) if v == "cpu" => Some(Some(Encoder::Cpu)),
|
||||
Some(v) if v == "vaapi" => Some(Some(Encoder::Vaapi)),
|
||||
Some(v) if v == "nvenc" => Some(Some(Encoder::Nvenc)),
|
||||
Some(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_codec(value: Option<&str>) -> Option<Option<CodecPref>> {
|
||||
match value
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|s| s.to_ascii_lowercase())
|
||||
{
|
||||
None => Some(None),
|
||||
Some(v) if v == "auto" => Some(None),
|
||||
Some(v) if v == "h264" => Some(Some(CodecPref::H264)),
|
||||
Some(v) if v == "h265" || v == "hevc" => Some(Some(CodecPref::H265)),
|
||||
Some(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn file_fields() -> (Option<String>, Option<String>) {
|
||||
let path = crate::system_actions::settings_path();
|
||||
let Ok(bytes) = std::fs::read(path) else { return (None, None) };
|
||||
let Ok(value) = serde_json::from_slice::<serde_json::Value>(&bytes) else { return (None, None) };
|
||||
let field = |name: &str| value.get(name).and_then(|v| v.as_str()).map(str::to_owned);
|
||||
(field("video_encoder"), field("video_codec"))
|
||||
}
|
||||
|
||||
/// Unset/auto tries NVENC only with an NVIDIA driver loaded: `ffmpeg -encoders` lists h264_nvenc
|
||||
/// on any machine, so without this every stream start first ran a doomed NVENC process (#10).
|
||||
fn legacy_auto(nvidia: bool) -> Encoder {
|
||||
if nvidia { Encoder::Nvenc } else { Encoder::Cpu }
|
||||
}
|
||||
|
||||
/// Setting (non-auto) beats the environment, which beats the NVIDIA/CPU default.
|
||||
pub fn selection() -> Selection {
|
||||
let (file_enc, file_codec) = file_fields();
|
||||
let env_enc = std::env::var("UU_VIDEO_ENCODER").ok();
|
||||
let env_codec = std::env::var("UU_VIDEO_CODEC").ok();
|
||||
resolve_pair(
|
||||
file_enc.as_deref(),
|
||||
env_enc.as_deref(),
|
||||
file_codec.as_deref(),
|
||||
env_codec.as_deref(),
|
||||
crate::video::nvidia_present(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn resolve_pair(
|
||||
enc_setting: Option<&str>,
|
||||
enc_env: Option<&str>,
|
||||
codec_setting: Option<&str>,
|
||||
codec_env: Option<&str>,
|
||||
nvidia: bool,
|
||||
) -> Selection {
|
||||
let (encoder, encoder_source) = match parse_encoder(enc_setting) {
|
||||
Some(Some(e)) => (e, Source::Setting),
|
||||
None => {
|
||||
log::warn!("Ignoring video_encoder value (not auto/cpu/vaapi/nvenc)");
|
||||
pick_encoder(enc_env, nvidia)
|
||||
}
|
||||
Some(None) => pick_encoder(enc_env, nvidia),
|
||||
};
|
||||
let (codec_pref, codec_source) = match parse_codec(codec_setting) {
|
||||
Some(Some(p)) => (p, Source::Setting),
|
||||
None => {
|
||||
log::warn!("Ignoring video_codec value (not auto/h264/h265)");
|
||||
pick_codec(codec_env)
|
||||
}
|
||||
Some(None) => pick_codec(codec_env),
|
||||
};
|
||||
Selection {
|
||||
encoder,
|
||||
encoder_source,
|
||||
codec_pref,
|
||||
codec_source,
|
||||
}
|
||||
}
|
||||
|
||||
fn pick_encoder(env: Option<&str>, nvidia: bool) -> (Encoder, Source) {
|
||||
match parse_encoder(env) {
|
||||
Some(Some(e)) => (e, Source::Env),
|
||||
None => {
|
||||
log::warn!("Ignoring UU_VIDEO_ENCODER value (not auto/cpu/vaapi/nvenc)");
|
||||
(legacy_auto(nvidia), Source::Auto)
|
||||
}
|
||||
Some(None) => (legacy_auto(nvidia), Source::Auto),
|
||||
}
|
||||
}
|
||||
|
||||
fn pick_codec(env: Option<&str>) -> (CodecPref, Source) {
|
||||
match parse_codec(env) {
|
||||
Some(Some(p)) => (p, Source::Env),
|
||||
None => {
|
||||
log::warn!("Ignoring UU_VIDEO_CODEC value (not auto/h264/h265)");
|
||||
(CodecPref::Auto, Source::Auto)
|
||||
}
|
||||
Some(None) => (CodecPref::Auto, Source::Auto),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn codec_force(pref: CodecPref) -> Option<&'static str> {
|
||||
match pref {
|
||||
CodecPref::H264 => Some("h264"),
|
||||
CodecPref::H265 => Some("h265"),
|
||||
CodecPref::Auto => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Hardware (VAAPI / NVENC) prefers H.265 unless the preference forces a codec.
|
||||
pub fn hardware(encoder: Encoder) -> bool {
|
||||
encoder != Encoder::Cpu
|
||||
}
|
||||
|
||||
pub fn source_label(source: Source) -> &'static str {
|
||||
match source {
|
||||
Source::Setting => "setting",
|
||||
Source::Env => "env",
|
||||
Source::Auto => "default",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encoder_source_label(source: Source) -> &'static str {
|
||||
match source {
|
||||
Source::Setting => "setting",
|
||||
Source::Env => "env",
|
||||
Source::Auto => "auto",
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn sel(
|
||||
es: Option<&str>,
|
||||
ee: Option<&str>,
|
||||
cs: Option<&str>,
|
||||
ce: Option<&str>,
|
||||
nvidia: bool,
|
||||
) -> Selection {
|
||||
resolve_pair(es, ee, cs, ce, nvidia)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn priority_matrix_and_illegal_values() {
|
||||
let encoders = [
|
||||
None,
|
||||
Some("auto"),
|
||||
Some("cpu"),
|
||||
Some("vaapi"),
|
||||
Some("nvenc"),
|
||||
Some("bogus"),
|
||||
];
|
||||
let codecs = [None, Some("auto"), Some("h264"), Some("h265"), Some("nope")];
|
||||
for setting in encoders {
|
||||
for env in encoders {
|
||||
let got = sel(setting, env, None, None, true);
|
||||
let expect_enc = match (parse_encoder(setting), parse_encoder(env)) {
|
||||
(Some(Some(e)), _) => (e, Source::Setting),
|
||||
(None, Some(Some(e))) | (Some(None), Some(Some(e))) => (e, Source::Env),
|
||||
_ => (Encoder::Nvenc, Source::Auto),
|
||||
};
|
||||
assert_eq!(
|
||||
(got.encoder, got.encoder_source),
|
||||
expect_enc,
|
||||
"{setting:?} {env:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
for setting in codecs {
|
||||
for env in codecs {
|
||||
let got = sel(None, None, setting, env, false);
|
||||
let expect = match (parse_codec(setting), parse_codec(env)) {
|
||||
(Some(Some(p)), _) => (p, Source::Setting),
|
||||
(None, Some(Some(p))) | (Some(None), Some(Some(p))) => (p, Source::Env),
|
||||
_ => (CodecPref::Auto, Source::Auto),
|
||||
};
|
||||
assert_eq!(
|
||||
(got.codec_pref, got.codec_source),
|
||||
expect,
|
||||
"{setting:?} {env:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
sel(Some("auto"), Some("cpu"), Some("auto"), Some("h264"), true).encoder,
|
||||
Encoder::Cpu
|
||||
);
|
||||
assert_eq!(
|
||||
sel(Some("auto"), None, None, None, false).encoder,
|
||||
Encoder::Cpu
|
||||
);
|
||||
assert_eq!(
|
||||
sel(None, Some("auto"), None, None, true).encoder,
|
||||
Encoder::Nvenc
|
||||
);
|
||||
assert_eq!(
|
||||
sel(Some(" NVENC "), None, Some(" HEVC "), None, false).encoder,
|
||||
Encoder::Nvenc
|
||||
);
|
||||
assert_eq!(
|
||||
sel(None, None, Some(" HEVC "), None, false).codec_pref,
|
||||
CodecPref::H265
|
||||
);
|
||||
}
|
||||
}
|
||||
+18
-9
@@ -63,7 +63,10 @@ impl WebRtcSession {
|
||||
m.register_default_codecs()
|
||||
.map_err(|e| format!("Register standard codecs failed: {}", e))?;
|
||||
|
||||
let hevc = desktop && crate::video::hevc_available().await;
|
||||
// Encoder, codec preference and 4:4:4 stay fixed for this peer (codec is negotiated at CONTROL open).
|
||||
// Read once before the HEVC probe so a concurrent set-encoder/set-codec cannot split capability from CONTROL.
|
||||
let video_selection = crate::video_select::selection();
|
||||
let hevc = desktop && crate::video::hevc_available(video_selection.encoder).await;
|
||||
if hevc { crate::video_track::register_hevc(&mut m).map_err(|e| format!("Register HEVC failed: {e}"))?; }
|
||||
|
||||
m.register_header_extension(
|
||||
@@ -162,11 +165,13 @@ impl WebRtcSession {
|
||||
let connection_input=input_session.clone();
|
||||
let connected_signal = signaling_client.clone();
|
||||
let notified = Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let connected_selection = video_selection;
|
||||
pc.on_peer_connection_state_change(Box::new(move |state| {
|
||||
let ctx = connected_context.clone();
|
||||
let input=connection_input.clone();
|
||||
let sig = connected_signal.clone();
|
||||
let notified = notified.clone();
|
||||
let video_selection = connected_selection;
|
||||
Box::pin(async move {
|
||||
log::info!("PeerConnection State changed: {:?}", state);
|
||||
if let Some(session)=input.as_ref() {
|
||||
@@ -177,10 +182,10 @@ impl WebRtcSession {
|
||||
tokio::spawn(async move {
|
||||
let ctx = ctx.lock().await.clone();
|
||||
if let (Some(client), Some(ice)) = (&ctx.client_id, ctx.ice_id.as_ref().and_then(|v| v.as_str())) {
|
||||
let encoder = crate::video::Encoder::requested();
|
||||
let encoder = video_selection.encoder;
|
||||
let codec_impl = if encoder == crate::video::Encoder::Nvenc { crate::capability::CODEC_IMPL_NVENC } else { 5 };
|
||||
let payload = crate::capability::device_capability(client, ice, 0, crate::video::display_fps() as i32, codec_impl,
|
||||
hevc, crate::video::full_chroma_available());
|
||||
hevc, crate::video::full_chroma_for(encoder));
|
||||
if let Err(e) = sig.emit_event(crate::capability::FORWARD_SETTING_EVENT, payload).await {
|
||||
log::error!("Device capability notification failed: {}", e);
|
||||
}
|
||||
@@ -299,6 +304,7 @@ impl WebRtcSession {
|
||||
}
|
||||
let portal=capture.portal.clone();
|
||||
let stream_desktop = portal_desktop.clone();
|
||||
let stream_selection = video_selection;
|
||||
// File/terminal/port-mapping connections carry no desktop video.
|
||||
let control_video_track = video_track.clone();
|
||||
let stream_settings = video_settings.clone();
|
||||
@@ -306,7 +312,7 @@ impl WebRtcSession {
|
||||
let state_pc = streaming_pc.clone();
|
||||
tokio::spawn(async move {
|
||||
let screen = if let Some(desktop) = stream_desktop {
|
||||
if let Err(error) = crate::locked_video::run(&streaming_pc, &video_track, &desktop).await {
|
||||
if let Err(error) = crate::locked_video::run(&streaming_pc, &video_track, &desktop, stream_selection).await {
|
||||
log::warn!("Locked desktop video unavailable: {error}");
|
||||
}
|
||||
let mut updates = desktop.subscribe();
|
||||
@@ -324,7 +330,7 @@ impl WebRtcSession {
|
||||
let (capture_size, capture_display, portal) = screen.as_ref().map_or(
|
||||
(capture_size, capture_display, portal),
|
||||
|screen| ((screen.width, screen.height), screen.display.clone(), screen.portal.clone()));
|
||||
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, stream_settings, idr_request).await {
|
||||
if let Err(e) = crate::video::stream_desktop(streaming_pc, video_track, capture_size, capture_display, portal, video_settings_rx, stream_settings, idr_request, stream_selection).await {
|
||||
// A send racing the viewer's close fails with a misleading track error.
|
||||
if matches!(state_pc.connection_state(), RTCPeerConnectionState::Closed | RTCPeerConnectionState::Failed) {
|
||||
log::info!("Live desktop stream ended: peer connection closed ({})", e);
|
||||
@@ -414,6 +420,7 @@ impl WebRtcSession {
|
||||
let peer_video_settings = video_settings.clone();
|
||||
let peer_video_track = control_video_track.clone();
|
||||
let peer_actions = peer_actions.clone();
|
||||
let peer_selection = video_selection;
|
||||
if label == "FILE_DATA_CHANNEL" { *file_channel.lock().unwrap() = Some(d.clone()); }
|
||||
|
||||
Box::pin(async move {
|
||||
@@ -458,6 +465,7 @@ impl WebRtcSession {
|
||||
let initial_screen = screen.clone();
|
||||
let switch_owner=channel_input_session.clone();
|
||||
let settings_serial=peer_settings_serial.clone();
|
||||
let capture_selection = peer_selection;
|
||||
tokio::spawn(async move {
|
||||
let mut health = tokio::time::interval(std::time::Duration::from_millis(100));
|
||||
loop {
|
||||
@@ -577,10 +585,10 @@ impl WebRtcSession {
|
||||
let before = *settings.borrow();
|
||||
let output_before = before.output;
|
||||
let revision = track.revision();
|
||||
let apply=|| crate::capture_setting::handle_rpc_with_codecs(
|
||||
let apply=|| crate::capture_setting::handle_rpc_for(
|
||||
&rpc_fields, &settings, input_size,
|
||||
track.supports(crate::capture_setting::VideoCodec::H264),
|
||||
track.supports(crate::capture_setting::VideoCodec::H265));
|
||||
track.supports(crate::capture_setting::VideoCodec::H265), capture_selection);
|
||||
let response=if let Some(owner)=&switch_owner {
|
||||
let mut response=None;owner.with_current(||response=apply());response
|
||||
} else {apply()};
|
||||
@@ -630,6 +638,7 @@ impl WebRtcSession {
|
||||
let open_desktop = portal_desktop.clone();
|
||||
let open_owner = channel_input_session.clone();
|
||||
let open_screen = screen.clone();
|
||||
let open_selection = peer_selection;
|
||||
d.on_open(Box::new(move || {
|
||||
let init_actions = open_actions.clone();
|
||||
let init_video = open_video.clone();
|
||||
@@ -826,8 +835,8 @@ impl WebRtcSession {
|
||||
(7, crate::capture_setting::screen_sources(next_seq(), timestamp, screen_size, init_video.borrow().output, "Linux desktop")),
|
||||
];
|
||||
if desktop && screen.init_error.is_none() {
|
||||
let codec = crate::capture_setting::preferred_codec(init_track.supports(crate::capture_setting::VideoCodec::H264),
|
||||
init_track.supports(crate::capture_setting::VideoCodec::H265));
|
||||
let codec = crate::capture_setting::preferred_codec_for(init_track.supports(crate::capture_setting::VideoCodec::H264),
|
||||
init_track.supports(crate::capture_setting::VideoCodec::H265), open_selection);
|
||||
init_video.send_if_modified(|current| if current.codec != codec {
|
||||
log::info!("Desktop codec selected: {:?}", codec); current.codec = codec; true } else { false });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user