diff --git a/AGENTS.md b/AGENTS.md index 24b58f7..68cc726 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -145,7 +145,7 @@ every 60 s. | `src/assist.rs`, `c_engine/assist_window.c` | remote assistance (guest + account) and the unified GTK main window (assist + account/QR card) | Diagnostic subcommands: `login [--no-gui]`, `unlock-check` (password on stdin, prints only accepted/rejected), -`desktop-probe [new.png]`, `desktop-video-probe` (`UU_PROBE_FPS`, `UU_PROBE_FRAMES`, `UU_PROBE_QUALITY` 1-5 = viewer bitrate preset, 5 auto), `wayland-input-probe`, `clipboard-acceptance`, +`desktop-probe [new.png]`, `desktop-video-probe` (`UU_PROBE_FPS`, `UU_PROBE_FRAMES`, `UU_PROBE_QUALITY` 1-5 = viewer bitrate preset, 5 auto; `UU_PROBE_INPUT` fixed video/image sequence, `UU_PROBE_LOOP=1` repeats), `wayland-input-probe`, `clipboard-acceptance`, `portal-acceptance`, `help`. Unknown arguments exit with code 2. Builds up to v0.1.0 did **not** reject them and started a second full host server instead, so check that a subcommand exists in the binary you run. diff --git a/README.en.md b/README.en.md index dfa59ba..7dab2df 100644 --- a/README.en.md +++ b/README.en.md @@ -272,6 +272,8 @@ If you use Claude Code, Codex, Cursor or a similar coding agent, have it read [A It is written for language models: build and install steps, a code map, protocol notes and development rules, enough to take over installation or development. +For local encoding diagnostics, `desktop-video-probe [output.h264]` accepts `UU_PROBE_FPS`, `UU_PROBE_FRAMES` (default 5), and `UU_PROBE_QUALITY` (1–5); set `UU_PROBE_INPUT=clip.y4m` (also `.mp4` or `frames/%05d.png`) to bypass desktop capture and measure fixed input with the same production FFmpeg encoding options, printing dimensions and encoding statistics, and optionally `UU_PROBE_LOOP=1` to repeat until the requested frame count (unreadable or short input exits nonzero; fixed input always uses the external FFmpeg pipeline). + ## How it works ```mermaid diff --git a/README.md b/README.md index 37458c3..53ea121 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,8 @@ systemctl --user restart uuremote-native.service 选择码率档(原画 / 超清 / 高清 / 自定义)时,外部 FFmpeg 的 CPU 编码以平均码率(ABR)为目标,避免 4K 低码率下关键帧发灰;「自动」档仍使用恒定质量(CRF)。 +本地编码诊断 `desktop-video-probe [output.h264]` 可设置 `UU_PROBE_FPS`、`UU_PROBE_FRAMES`(默认 5)和 `UU_PROBE_QUALITY`(1–5);设置 `UU_PROBE_INPUT=clip.y4m`(也支持 `.mp4`、`frames/%05d.png`)可绕过桌面采集,以相同的生产 FFmpeg 编码参数测量固定输入并打印分辨率和编码统计,`UU_PROBE_LOOP=1` 循环到指定帧数,输入无法打开或帧数不足时以非零状态退出(固定输入始终使用外部 FFmpeg 管线)。 + ## 让 AI 帮你安装或开发 如果你用 Claude Code、Codex、Cursor 等 AI 编程工具,让它先阅读 [AGENTS.md](AGENTS.md)。 diff --git a/src/main.rs b/src/main.rs index 30f1a5f..1852470 100644 --- a/src/main.rs +++ b/src/main.rs @@ -318,6 +318,9 @@ async fn main() -> Result<(), Box> { return Ok(()); } if std::env::args().nth(1).as_deref() == Some("desktop-video-probe") { + if let Some(input)=std::env::var_os("UU_PROBE_INPUT") { + return video::probe_input(input,std::env::args().nth(2)).await.map_err(|e|e.into()); + } let screen=ScreenCapture::new()?; capture::keep_display_awake()?; return video::probe((screen.width,screen.height),screen.display.clone(),screen.portal.clone(),std::env::args().nth(2)).await.map_err(|e|e.into()); diff --git a/src/video.rs b/src/video.rs index 27291af..dbd819f 100644 --- a/src/video.rs +++ b/src/video.rs @@ -238,6 +238,11 @@ fn encoder_options(settings: &VideoSettings, encoder: Encoder, gop: &str, rgb: O options.into_iter().map(str::to_owned).collect() } +fn scale_options(settings: &VideoSettings, encoder: Encoder) -> Vec { + if encoder == Encoder::Vaapi { return vec!["-vf".into(), vaapi_filter(settings.output)]; } + settings.output.map_or_else(Vec::new, |(w,h)| vec!["-vf".into(), format!("scale={w}:{h}:flags=bilinear")]) +} + #[derive(Clone, Debug, Hash, PartialEq, Eq)] struct FfmpegSpec { program: PathBuf, @@ -538,11 +543,7 @@ async fn read_encoder_with( "-video_size",&size,"-i",&display,"-an"]); } // 其他分辨率: scale the captured desktop (the host display mode is left untouched). - if encoder == Encoder::Vaapi { - command.args(["-vf", &vaapi_filter(settings.output)]); - } else if let Some((width, height)) = settings.output { - command.args(["-vf", &format!("scale={width}:{height}:flags=bilinear")]); - } + command.args(scale_options(&settings, encoder)); command.args(encoder_options(&settings, encoder, &gop, rgb)); // x11grab paces against an absolute schedule; after a capture stall it grabs // back-to-back to "catch up" (measured 69 frames in 200ms). vfr on a 1/FPS @@ -677,6 +678,97 @@ fn transport_closed(pc: &RTCPeerConnection) -> bool { || pc.sctp().state() == webrtc::sctp_transport::sctp_transport_state::RTCSctpTransportState::Closed } +fn probe_settings() -> Result { + let fps=crate::wayland::capture_fps(std::env::var("UU_PROBE_FPS").ok().as_deref())?; + // UU_PROBE_QUALITY selects a viewer preset (1 fast 1M, 2 general 2M, 3 HD 8M, 4 bluray 20M, + // 5 auto); bitrate presets exercise the capped/ABR encoder path instead of CRF. + let quality=match std::env::var("UU_PROBE_QUALITY").ok().as_deref() { + None=>crate::capture_setting::QUALITY_AUTO, + Some(v)=>v.parse::().ok().filter(|q|(1..=5).contains(q)).ok_or("UU_PROBE_QUALITY must be 1-5")?, + }; + Ok(VideoSettings {fps,quality,..VideoSettings::default()}) +} + +fn probe_input_command(spec:&FfmpegSpec,input:&std::ffi::OsStr,loop_input:bool,settings:&VideoSettings, + encoder:Encoder,wanted:u64,timing:FfmpegTimingMode)->Command { + let mut command=spec.command(); + command.args(["-hide_banner","-loglevel","error","-nostdin"]); + if encoder==Encoder::Vaapi { command.args(["-vaapi_device",&vaapi_device()]); } + if loop_input { command.args(["-stream_loop","-1"]); } + // Override source timestamps so clips and image sequences use the same nominal probe rate. + command.args(["-r",&settings.fps.to_string(),"-i"]).arg(input).args(["-map","0:v:0","-an"]); + command.args(scale_options(settings,encoder)); + command.args(encoder_options(settings,encoder,&gop_frames(settings.fps,false).to_string(),None)); + command.args(timing_args(timing,settings.fps)); + command.args(["-frames:v",&wanted.to_string(),"-f","h264","pipe:1"]); + command +} + +async fn probe_input_size(spec:&FfmpegSpec,input:&std::ffi::OsStr)->Result<(u32,u32),String> { + // Read only the PNG header; never log decoded pixels, input paths or FFmpeg diagnostics. + let mut child=spec.command().args(["-hide_banner","-loglevel","error","-nostdin","-i"]).arg(input) + .args(["-map","0:v:0","-frames:v","1","-c:v","png","-f","image2pipe","pipe:1"]) + .stdin(std::process::Stdio::null()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null()) + .kill_on_drop(true).spawn().map_err(|e|format!("Start fixed-input probe: {e}"))?; + let mut header=[0u8;24]; + let result=tokio::time::timeout(Duration::from_secs(15),child.stdout.as_mut().unwrap().read_exact(&mut header)).await; + let _=child.kill().await; + if !matches!(result,Ok(Ok(_))) || &header[..8]!=b"\x89PNG\r\n\x1a\n" { + return Err("Cannot open/decode UU_PROBE_INPUT video input".into()); + } + Ok((u32::from_be_bytes(header[16..20].try_into().unwrap()),u32::from_be_bytes(header[20..24].try_into().unwrap()))) +} + +/// Fixed input bypasses desktop initialization and always uses the FFmpeg process encoder. +pub async fn probe_input(input:OsString,path:Option)->Result<(),String> { + use std::io::Write; + let settings=probe_settings()?; + let wanted:u64=std::env::var("UU_PROBE_FRAMES").ok().and_then(|v|v.parse().ok()).unwrap_or(5); + 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 (maxrate,_)=rate_args(&settings,encoder); + let output=settings.output.unwrap_or(size); + println!("Fixed input probe: source={}x{} output={}x{} fps={} cq={} maxrate={} encoder={}", + size.0,size.1,output.0,output.1,settings.fps,settings.cq(),maxrate,encoder.name_for(settings.codec)); + let mut file=match path { + Some(path)=>Some(std::fs::OpenOptions::new().write(true).create_new(true).open(path).map_err(|e|format!("Video probe file: {e}"))?), + None=>None, + }; + let mut command=probe_input_command(&spec,&input,std::env::var("UU_PROBE_LOOP").as_deref()==Ok("1"), + &settings,encoder,wanted,ffmpeg_timing_mode(&spec).await); + let mut child=command.stdin(std::process::Stdio::null()).stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::null()).kill_on_drop(true).spawn().map_err(|e|format!("Start fixed-input encoder: {e}"))?; + let mut stdout=child.stdout.take().ok_or("Encoder stdout missing")?; + let (mut count,mut bytes)=(0u64,0usize); + tokio::time::timeout(Duration::from_secs(15+wanted/30),async { + let mut pending=Vec::new(); + let mut chunk=[0u8;16384]; + loop { + let n=stdout.read(&mut chunk).await.map_err(|e|format!("Encoder read: {e}"))?; + pending.extend_from_slice(&chunk[..n]); + loop { + let aud=aud_offsets_for(&pending,settings.codec); + let end=if aud.len()>1 { aud[1] } else if n==0 && !pending.is_empty() { pending.len() } else { break; }; + if end>MAX_ACCESS_UNIT { return Err("Encoder access unit exceeds limit".into()); } + if count==0 && !is_independent_frame(&pending[..end]) { return Err("Video probe did not begin with IDR".into()); } + if let Some(file)=file.as_mut() { file.write_all(&pending[..end]).map_err(|e|e.to_string())?; } + bytes+=end; count+=1; + pending.drain(..end); + } + if pending.len()>MAX_ACCESS_UNIT { return Err("Encoder access unit exceeds limit".into()); } + if n==0 { break; } + } + let status=child.wait().await.map_err(|e|format!("Fixed-input encoder: {e}"))?; + if !status.success() { return Err(format!("Fixed-input encoder failed ({status}); check input and encoder availability")); } + if count!=wanted { return Err(format!("Fixed input ended after {count} frames; requested {wanted} (use UU_PROBE_LOOP=1 to repeat)")); } + Ok::<(),String>(()) + }).await.map_err(|_|"Video probe timed out".to_owned())??; + println!("Encoded capture probe: frames={count} h264_bytes={bytes} first_frame_idr=true"); + Ok(()) +} + /// Exercise the real capture/encoder chain without UU auth or a network peer. pub async fn probe(size:(u32,u32),display:String,portal:Option>,path:Option)->Result<(),String> { use std::io::Write; @@ -686,14 +778,7 @@ pub async fn probe(size:(u32,u32),display:String,portal:Optioncrate::capture_setting::QUALITY_AUTO, - Some(v)=>v.parse::().ok().filter(|q|(1..=5).contains(q)).ok_or("UU_PROBE_QUALITY must be 1-5")?, - }; - let settings=VideoSettings {fps,quality,..VideoSettings::default()}; + 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 mut count=0; let mut bytes=0; @@ -822,6 +907,44 @@ pub async fn stream_desktop(pc: Arc, track: Arc=command.as_std().get_args().map(|a|a.to_str().unwrap().to_owned()).collect(); + let expected=encoder_options(&settings,encoder,&gop_frames(30,false).to_string(),None); + assert!(args.windows(expected.len()).any(|w|w==expected)); + let scale=scale_options(&settings,encoder); + assert!(args.windows(scale.len()).any(|w|w==scale)); + assert!(args.windows(4).any(|w|w==["-r","30","-i","frames/%05d.png"])); + assert!(args.windows(2).any(|w|w==["-frames:v","17"])); + assert!(args.windows(4).any(|w|w==["-fps_mode","vfr","-enc_time_base","1/30"])); + assert!(!args.iter().any(|a|["x11grab","rawvideo","-stream_loop"].contains(&a.as_str()))); + } + } + } + } + + #[test] + fn fixed_input_loop_is_an_input_option_and_keeps_paths_literal() { + let spec=FfmpegSpec {program:"ffmpeg".into(),ld_library_path:None,path:None}; + for input in ["clip.y4m","a clip.mp4","frames/%05d.png"] { + let command=probe_input_command(&spec,std::ffi::OsStr::new(input),true,&VideoSettings::default(), + Encoder::Cpu,5,FfmpegTimingMode::Legacy); + let args:Vec<_>=command.as_std().get_args().map(|a|a.to_str().unwrap()).collect(); + assert!(args.windows(2).any(|w|w==["-i",input])); + assert!(args.windows(2).any(|w|w==["-stream_loop","-1"])); + assert!(args.iter().position(|a|*a=="-stream_loop")