diff --git a/crates/client/src/share/capture.rs b/crates/client/src/share/capture.rs index 4ac27d5..2b5fad6 100644 --- a/crates/client/src/share/capture.rs +++ b/crates/client/src/share/capture.rs @@ -30,13 +30,13 @@ use spa::{ utils::{Direction, Fraction, Rectangle, SpaTypes}, }; -use super::frame::Frame; +use super::{ + fake, + frame::{BYTES_PER_PIXEL, DEFAULT_REFRESH, FRAME_QUEUE, Frame}, +}; const NEGOTIATE_TIMEOUT: Duration = Duration::from_secs(10); const STOP_CHECK: Duration = Duration::from_millis(100); -const DEFAULT_REFRESH: u32 = 60; -const FRAME_QUEUE: usize = 2; -const BYTES_PER_PIXEL: usize = 4; type Negotiated = (u32, u32, u32); @@ -48,6 +48,7 @@ pub struct Capture { stop: Arc, thread: Option>, _session: Option>, + _fake: Option, } impl Drop for Capture { @@ -61,7 +62,17 @@ impl Drop for Capture { pub async fn open() -> Result { if let Ok(spec) = std::env::var("YUMUSH_TEST_CAPTURE") { - return Ok(fake(&spec)); + let (width, height, refresh, frames, generator) = fake::start(&spec); + return Ok(Capture { + width, + height, + refresh, + frames, + stop: Arc::new(AtomicBool::new(false)), + thread: None, + _session: None, + _fake: Some(generator), + }); } let screencast = Screencast::new().await.map_err(text)?; let session = screencast @@ -126,68 +137,10 @@ pub async fn open() -> Result { stop, thread: Some(thread), _session: Some(session), + _fake: None, }) } -fn fake(spec: &str) -> Capture { - let parsed: Option<(u32, u32, u32)> = spec.split_once('@').and_then(|(size, fps)| { - let (width, height) = size.split_once('x')?; - Some((width.parse().ok()?, height.parse().ok()?, fps.parse().ok()?)) - }); - let (width, height, refresh) = parsed - .filter(|(width, height, fps)| *width > 0 && *height > 0 && *fps > 0) - .unwrap_or((1280, 720, DEFAULT_REFRESH)); - let (frames_tx, frames_rx) = sync_channel(FRAME_QUEUE); - let stop = Arc::new(AtomicBool::new(false)); - let thread = { - let stop = stop.clone(); - std::thread::Builder::new() - .name("share-capture-fake".to_string()) - .spawn(move || { - let started = Instant::now(); - let interval = Duration::from_secs_f64(1.0 / f64::from(refresh)); - let mut number: u64 = 0; - while !stop.load(Ordering::Relaxed) { - let bar = (number as usize * 8) % width as usize; - let mut bgra = vec![40u8; (width * height) as usize * BYTES_PER_PIXEL]; - for y in 0..height as usize { - let row = y * width as usize; - for x in bar..(bar + 32).min(width as usize) { - bgra[(row + x) * BYTES_PER_PIXEL..][..3].fill(255); - } - if y < 16 { - for x in 0..(32 * 16).min(width as usize) { - let value = if (number >> (x / 16)) & 1 == 1 { 255 } else { 0 }; - bgra[(row + x) * BYTES_PER_PIXEL..][..3].fill(value); - } - } - } - let frame = Frame { - width, - height, - stride: width * BYTES_PER_PIXEL as u32, - timestamp_ms: u32::try_from(started.elapsed().as_millis()) - .unwrap_or(u32::MAX), - bgra, - }; - let _ = frames_tx.try_send(frame); - number += 1; - std::thread::sleep(interval); - } - }) - .ok() - }; - Capture { - width, - height, - refresh, - frames: frames_rx, - stop, - thread, - _session: None, - } -} - fn text(error: impl std::fmt::Display) -> String { error.to_string() } diff --git a/crates/client/src/share/capture_windows.rs b/crates/client/src/share/capture_windows.rs index 01a18ff..c376962 100644 --- a/crates/client/src/share/capture_windows.rs +++ b/crates/client/src/share/capture_windows.rs @@ -14,11 +14,10 @@ use windows_capture::{ }, }; -use super::frame::Frame; - -const DEFAULT_REFRESH: u32 = 60; -const FRAME_QUEUE: usize = 2; -const BYTES_PER_PIXEL: usize = 4; +use super::{ + fake, + frame::{BYTES_PER_PIXEL, DEFAULT_REFRESH, FRAME_QUEUE, Frame}, +}; pub struct Capture { pub width: u32, @@ -26,6 +25,7 @@ pub struct Capture { pub refresh: u32, pub frames: Receiver, control: Option>, + _fake: Option, } impl Drop for Capture { @@ -37,6 +37,17 @@ impl Drop for Capture { } pub async fn open() -> Result { + if let Ok(spec) = std::env::var("YUMUSH_TEST_CAPTURE") { + let (width, height, refresh, frames, generator) = fake::start(&spec); + return Ok(Capture { + width, + height, + refresh, + frames, + control: None, + _fake: Some(generator), + }); + } let monitor = Monitor::primary().map_err(text)?; let width = monitor.width().map_err(text)?; let height = monitor.height().map_err(text)?; @@ -65,6 +76,7 @@ pub async fn open() -> Result { refresh, frames: frames_rx, control: Some(control), + _fake: None, }) } diff --git a/crates/client/src/share/fake.rs b/crates/client/src/share/fake.rs new file mode 100644 index 0000000..bad4235 --- /dev/null +++ b/crates/client/src/share/fake.rs @@ -0,0 +1,76 @@ +use std::{ + sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + mpsc::{Receiver, sync_channel}, + }, + thread::JoinHandle, + time::{Duration, Instant}, +}; + +use super::frame::{BYTES_PER_PIXEL, DEFAULT_REFRESH, FRAME_QUEUE, Frame}; + +pub struct Generator { + stop: Arc, + thread: Option>, +} + +impl Drop for Generator { + fn drop(&mut self) { + self.stop.store(true, Ordering::Relaxed); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } +} + +pub fn start(spec: &str) -> (u32, u32, u32, Receiver, Generator) { + let parsed: Option<(u32, u32, u32)> = spec.split_once('@').and_then(|(size, fps)| { + let (width, height) = size.split_once('x')?; + Some((width.parse().ok()?, height.parse().ok()?, fps.parse().ok()?)) + }); + let (width, height, refresh) = parsed + .filter(|(width, height, fps)| *width > 0 && *height > 0 && *fps > 0) + .unwrap_or((1280, 720, DEFAULT_REFRESH)); + let (frames_tx, frames_rx) = sync_channel(FRAME_QUEUE); + let stop = Arc::new(AtomicBool::new(false)); + let thread = { + let stop = stop.clone(); + std::thread::Builder::new() + .name("share-capture-fake".to_string()) + .spawn(move || { + let started = Instant::now(); + let interval = Duration::from_secs_f64(1.0 / f64::from(refresh)); + let mut number: u64 = 0; + while !stop.load(Ordering::Relaxed) { + let bar = (number as usize * 8) % width as usize; + let mut bgra = vec![40u8; (width * height) as usize * BYTES_PER_PIXEL]; + for y in 0..height as usize { + let row = y * width as usize; + for x in bar..(bar + 32).min(width as usize) { + bgra[(row + x) * BYTES_PER_PIXEL..][..3].fill(255); + } + if y < 16 { + for x in 0..(32 * 16).min(width as usize) { + let value = if (number >> (x / 16)) & 1 == 1 { 255 } else { 0 }; + bgra[(row + x) * BYTES_PER_PIXEL..][..3].fill(value); + } + } + } + let frame = Frame { + width, + height, + stride: width * BYTES_PER_PIXEL as u32, + timestamp_ms: u32::try_from(started.elapsed().as_millis()) + .unwrap_or(u32::MAX), + bgra, + }; + let _ = frames_tx.try_send(frame); + number += 1; + std::thread::sleep(interval); + } + }) + .ok() + }; + (width, height, refresh, frames_rx, Generator { stop, thread }) +} diff --git a/crates/client/src/share/frame.rs b/crates/client/src/share/frame.rs index ab534a4..13349b4 100644 --- a/crates/client/src/share/frame.rs +++ b/crates/client/src/share/frame.rs @@ -2,6 +2,10 @@ use common::share::Layer; use crate::media::decode::Engine; +pub const DEFAULT_REFRESH: u32 = 60; +pub const FRAME_QUEUE: usize = 2; +pub const BYTES_PER_PIXEL: usize = 4; + pub struct Frame { pub width: u32, pub height: u32, diff --git a/crates/client/src/share/mod.rs b/crates/client/src/share/mod.rs index 4c037cc..6e40239 100644 --- a/crates/client/src/share/mod.rs +++ b/crates/client/src/share/mod.rs @@ -9,6 +9,8 @@ pub mod capture; #[cfg(target_os = "windows")] #[path = "capture_windows.rs"] pub mod capture; +#[cfg(any(target_os = "linux", target_os = "windows"))] +pub mod fake; pub mod convert; pub mod encode; pub mod frame; diff --git a/crates/client/src/voice/audio.rs b/crates/client/src/voice/audio.rs index 8f21faf..be543a8 100644 --- a/crates/client/src/voice/audio.rs +++ b/crates/client/src/voice/audio.rs @@ -207,11 +207,21 @@ pub fn supervise( stop: &AtomicBool, mut attempt: impl FnMut() -> Result<(), Box>, ) { + let mut last: Option = None; while !stop.load(Ordering::Relaxed) { let pause = match attempt() { - Ok(()) => RETRY_AFTER_CHANGE, + Ok(()) => { + last = None; + RETRY_AFTER_CHANGE + } Err(error) => { - warn!("{name}: {error}, retrying"); + let message = error.to_string(); + if last.as_deref() == Some(message.as_str()) { + tracing::debug!("{name}: {message}, retrying"); + } else { + warn!("{name}: {message}, retrying"); + last = Some(message); + } RETRY_AFTER_ERROR } }; diff --git a/harness/fps.py b/harness/fps.py index 62d766d..a502f93 100644 --- a/harness/fps.py +++ b/harness/fps.py @@ -26,7 +26,10 @@ end = time.time() + seconds while time.time() < end: p = f"{state}/shots/fps.png" t = time.time() - subprocess.run(["grim", p], env=env, check=True) + if os.environ.get("WIN"): + subprocess.run(["./win.sh", "shot", "fps"], check=True, stdout=subprocess.DEVNULL) + else: + subprocess.run(["grim", p], env=env, check=True) samples.append((t, read(p))) (t0, n0), (t1, n1) = samples[0], samples[-1] print(f"frames {n0}->{n1} in {t1-t0:.2f}s = {(n1-n0)/(t1-t0):.1f} fps, {len(samples)} shots") diff --git a/harness/win.sh b/harness/win.sh index e0f5523..11e9c02 100755 --- a/harness/win.sh +++ b/harness/win.sh @@ -16,5 +16,10 @@ case "$1" in run) ssh_win "schtasks /create /f /tn yumush_$2 /sc once /st 00:00 /it /ru yumush /tr \"$3\" | Out-Null; schtasks /run /tn yumush_$2";; ssh) shift; ssh_win "$@";; + sync) + ssh_win 'Stop-Process -Name client,server -ErrorAction SilentlyContinue' || true + (cd .. && (git ls-files -co --exclude-standard; echo fullchain.pem; echo privkey.pem; echo configs/server_config.toml) | grep -v "^harness/" | tar czf /tmp/claude-1000/src.tgz -T -) + scp -q -i ~/vm/yumush-win/id_ed25519 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -P 2222 /tmp/claude-1000/src.tgz yumush@127.0.0.1:src.tgz + ssh_win 'tar xmzf src.tgz -C yumush; cd yumush; $env:Path += ";C:\Users\yumush\.cargo\bin"; cargo build --profile harness -p client -p server 2>&1 | Select-String -Pattern "^error|^warning|Finished|error\[" -Context 0,8 | Select -First 40';; esac sleep "${UI_DELAY:-0.3}"