Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
Merge remote-tracking branch 'origin/main' into perf/cursor-recorder-skip-unchanged-image
Aatman Jain (AJ)5c25bbfc68fd
diff --cc crates/recording/src/cursor.rsindex 57644c55d,e108fe111..022f5336a--- a/crates/recording/src/cursor.rs+++ b/crates/recording/src/cursor.rs@@@ -334,8 -479,9 +479,8 @@@ pub fn spawn_cursor_recorder}use device_query::{DeviceQuery, DeviceState};- use sha2::{Digest, Sha256};use std::time::Duration;- use tracing::info;+ use tracing::{error, info};let stop_token = CancellationToken::new();let (tx, rx) = oneshot::channel();@@@ -421,8 -571,43 +571,13 @@@}let cursor_id = if let Some(data) = get_cursor_data() {- let cursor_id =- cursor_id_for_image(data, &mut response, &cursors_dir, &mut last_cursor_image);- let hash_bytes = Sha256::digest(&data.image);- let id = u64::from_le_bytes(- hash_bytes[..8]- .try_into()- .expect("sha256 produces at least 8 bytes"),++ let cursor_id = cursor_id_for_image(++ data,++ &mut response,++ &cursors_dir,++ writer.as_ref(),++ &mut last_cursor_image,+ );-- let cursor_id = if let Some(existing_id) = response.cursors.get(&id) {- existing_id.id.to_string()- } else {- let cursor_id = response.next_cursor_id.to_string();- let file_name = format!("cursor_{cursor_id}.png");- let cursor_path = cursors_dir.join(&file_name);-- if let Ok(image) = image::load_from_memory(&data.image) {- let rgba_image = image.into_rgba8();-- let saved = match &writer {- Some(writer) => writer.save_image(cursor_path, rgba_image),- None => save_cursor_image(&cursor_path, &rgba_image),- };- if saved {- response.cursors.insert(- id,- Cursor {- file_name,- id: response.next_cursor_id,- hotspot: data.hotspot,- shape: data.shape,- },- );- response.next_cursor_id += 1;- }- }-- cursor_id- };last_cursor_id = Some(cursor_id.clone());Some(cursor_id)} else {@@@ -539,72 -731,6 +701,73 @@@}}+fn cursor_id_for_image(+ data: CursorData,+ response: &mut CursorActorResponse,+ cursors_dir: &Path,++ writer: Option<&CursorFileWriter>,+ last_cursor_image: &mut Option<(Vec<u8>, String)>,+) -> String {+ use sha2::{Digest, Sha256};- use tracing::{error, info};++ // Hashing a large macOS cursor TIFF costs milliseconds, and the image+ // rarely changes between ticks.+ if let Some((image, cursor_id)) = last_cursor_image+ && *image == data.image+ {+ return cursor_id.clone();+ }++ let hash_bytes = Sha256::digest(&data.image);+ let id = u64::from_le_bytes(+ hash_bytes[..8]+ .try_into()+ .expect("sha256 produces at least 8 bytes"),+ );++ let cursor_id = if let Some(existing_id) = response.cursors.get(&id) {+ existing_id.id.to_string()+ } else {+ let cursor_id = response.next_cursor_id.to_string();+ let file_name = format!("cursor_{cursor_id}.png");+ let cursor_path = cursors_dir.join(&file_name);++ if let Ok(image) = image::load_from_memory(&data.image) {+ let rgba_image = image.into_rgba8();+- if let Err(e) = rgba_image.save(&cursor_path) {- error!("Failed to save cursor image: {}", e);- } else {- info!("Saved cursor {cursor_id} image to: {:?}", file_name);++ let saved = match writer {++ Some(writer) => writer.save_image(cursor_path, rgba_image),++ None => save_cursor_image(&cursor_path, &rgba_image),++ };++ if saved {+ #[cfg(target_os = "macos")]+ let shape = cap_cursor_info::CursorShapeMacOS::from_hash(&hex::encode(hash_bytes))+ .map(Into::into);+ #[cfg(not(target_os = "macos"))]+ let shape = data.shape;+ response.cursors.insert(+ id,+ Cursor {+ file_name,+ id: response.next_cursor_id,+ hotspot: data.hotspot,+ shape,+ },+ );+ response.next_cursor_id += 1;+ }+ }++ cursor_id+ };++ *last_cursor_image = response+ .cursors+ .contains_key(&id)+ .then(|| (data.image, cursor_id.clone()));+ cursor_id+}+#[derive(Debug)]struct CursorData {image: Vec<u8>,@@@ -1137,125 -1266,184 +1300,316 @@@ mod input_epoch_tests}}+ #[cfg(test)]+ mod incremental_writer_tests {+ use super::*;+ use std::time::Duration;++ fn response(events: usize) -> CursorActorResponse {+ CursorActorResponse {+ cursors: Cursors::default(),+ next_cursor_id: 1,+ moves: (0..events)+ .map(|i| CursorMoveEvent {+ active_modifiers: vec![],+ cursor_id: "0".to_string(),+ time_ms: i as f64 * 16.0,+ x: i as f64 / events as f64,+ y: 0.5,+ })+ .collect(),+ clicks: (0..events / 10)+ .map(|i| CursorClickEvent {+ active_modifiers: vec![],+ cursor_num: 0,+ cursor_id: "0".to_string(),+ time_ms: i as f64 * 160.0,+ down: i % 2 == 0,+ })+ .collect(),+ keyboard_presses: (0..events / 10)+ .map(|i| KeyPressEvent {+ key: "a".to_string(),+ key_code: "KeyA".to_string(),+ time_ms: i as f64 * 160.0,+ down: i % 2 == 0,+ })+ .collect(),+ }+ }++ fn outputs(dir: &Path, name: &str) -> IncrementalCaptureOutputs {+ IncrementalCaptureOutputs {+ cursor: Some(dir.join(format!("{name}-cursor.json"))),+ keyboard: Some(dir.join(format!("{name}-keyboard.bin"))),+ }+ }++ #[test]+ fn writer_thread_produces_the_same_files_as_inline_flushes() {+ let dir = tempfile::tempdir().unwrap();+ let full = response(500);++ let inline = outputs(dir.path(), "inline");+ IncrementalSnapshot::capture(&inline, &full).write(&inline);++ let threaded = outputs(dir.path(), "threaded");+ let writer = CursorFileWriter::spawn(threaded.clone()).unwrap();+ writer.flush(|| IncrementalSnapshot::capture(&threaded, &response(10)));+ writer.finish(Some(IncrementalSnapshot::capture(&threaded, &full)));++ for (a, b) in [+ (&inline.cursor, &threaded.cursor),+ (&inline.keyboard, &threaded.keyboard),+ ] {+ let (a, b) = (a.as_ref().unwrap(), b.as_ref().unwrap());+ assert_eq!(std::fs::read(a).unwrap(), std::fs::read(b).unwrap());+ }+ }++ #[cfg(unix)]+ #[test]+ fn flushes_do_not_wait_for_a_blocked_writer() {+ use std::ffi::CString;+ use std::io::Read;+ use std::os::unix::ffi::OsStrExt;+ use std::os::unix::fs::OpenOptionsExt;++ let dir = tempfile::tempdir().unwrap();+ let fifo = dir.path().join("cursor.json");+ let fifo_c = CString::new(fifo.as_os_str().as_bytes()).unwrap();+ assert_eq!(unsafe { libc::mkfifo(fifo_c.as_ptr(), 0o600) }, 0);+ let outputs = IncrementalCaptureOutputs {+ cursor: Some(fifo.clone()),+ keyboard: Some(dir.path().join("keyboard.bin")),+ };++ // Opening a FIFO for writing blocks until a reader arrives, which+ // stands in for a stalled output volume.+ let writer = CursorFileWriter::spawn(outputs.clone()).unwrap();+ let started = Instant::now();+ for events in [10, 20, 30, 40] {+ writer.flush(|| IncrementalSnapshot::capture(&outputs, &response(events)));+ }+ assert!(writer.save_image(+ dir.path().join("cursor_0.png"),+ image::RgbaImage::from_pixel(8, 8, image::Rgba([20, 40, 80, 255])),+ ));+ assert!(started.elapsed() < Duration::from_millis(500));++ let mut drain = std::fs::OpenOptions::new()+ .read(true)+ .write(true)+ .custom_flags(libc::O_NONBLOCK)+ .open(&fifo)+ .unwrap();+ let done = Arc::new(AtomicBool::new(false));+ let drainer = {+ let done = done.clone();+ std::thread::spawn(move || {+ let mut buf = [0u8; 4096];+ while !done.load(Ordering::Acquire) {+ if !matches!(drain.read(&mut buf), Ok(n) if n > 0) {+ std::thread::sleep(Duration::from_millis(1));+ }+ }+ })+ };++ let final_response = response(50);+ let unsaved = writer.finish(Some(IncrementalSnapshot::capture(+ &outputs,+ &final_response,+ )));+ done.store(true, Ordering::Release);+ drainer.join().unwrap();++ assert!(unsaved.is_empty());+ assert!(dir.path().join("cursor_0.png").exists());+ let expected = dir.path().join("expected-keyboard.bin");+ flush_keyboard_data(&expected, &final_response.keyboard_presses);+ assert_eq!(+ std::fs::read(dir.path().join("keyboard.bin")).unwrap(),+ std::fs::read(expected).unwrap()+ );+ }++ #[test]+ fn unsaved_cursor_images_are_reported() {+ let dir = tempfile::tempdir().unwrap();+ let writer = CursorFileWriter::spawn(IncrementalCaptureOutputs {+ cursor: None,+ keyboard: None,+ })+ .unwrap();+ let missing = dir.path().join("missing").join("cursor_0.png");+ assert!(writer.save_image(+ missing.clone(),+ image::RgbaImage::from_pixel(8, 8, image::Rgba([20, 40, 80, 255])),+ ));+ let saved = dir.path().join("cursor_1.png");+ assert!(writer.save_image(+ saved.clone(),+ image::RgbaImage::from_pixel(8, 8, image::Rgba([20, 40, 80, 255])),+ ));+ assert_eq!(writer.finish(None), vec![missing]);+ assert!(saved.exists());+ }++ #[test]+ fn cursor_images_are_retried_until_storage_recovers() {+ let dir = tempfile::tempdir().unwrap();+ let writer = CursorFileWriter::spawn(IncrementalCaptureOutputs {+ cursor: None,+ keyboard: None,+ })+ .unwrap();+ let cursors_dir = dir.path().join("cursors");+ let path = cursors_dir.join("cursor_0.png");+ assert!(writer.save_image(+ path.clone(),+ image::RgbaImage::from_pixel(8, 8, image::Rgba([20, 40, 80, 255])),+ ));+ std::thread::sleep(Duration::from_millis(100));+ assert!(!path.exists());+ std::fs::create_dir_all(&cursors_dir).unwrap();+ let deadline = Instant::now() + CURSOR_IMAGE_RETRY_INTERVAL * 3;+ while !path.exists() && Instant::now() < deadline {+ std::thread::sleep(Duration::from_millis(20));+ }+ assert!(path.exists());+ assert!(writer.finish(None).is_empty());+ }+ }+++#[cfg(test)]+mod cursor_image_tests {+ use super::*;++ fn png(color: [u8; 4]) -> Vec<u8> {+ let mut png = std::io::Cursor::new(Vec::new());+ image::RgbaImage::from_pixel(8, 8, image::Rgba(color))+ .write_to(&mut png, image::ImageFormat::Png)+ .unwrap();+ png.into_inner()+ }++ fn cursor_data(image: Vec<u8>) -> CursorData {+ CursorData {+ image,+ hotspot: XY::new(0.0, 0.0),+ #[cfg(not(target_os = "macos"))]+ shape: None,+ }+ }++ fn empty_response() -> CursorActorResponse {+ CursorActorResponse {+ cursors: Cursors::default(),+ next_cursor_id: 0,+ moves: vec![],+ clicks: vec![],+ keyboard_presses: vec![],+ }+ }++ #[test]+ fn an_unchanged_cursor_image_reuses_its_id_without_hashing() {+ let dir = tempfile::tempdir().unwrap();+ let mut response = empty_response();+ let mut last = None;+ let red = png([255, 0, 0, 255]);++ assert_eq!(+ cursor_id_for_image(+ cursor_data(red.clone()),+ &mut response,+ dir.path(),++ None,+ &mut last+ ),+ "0"+ );+ assert_eq!(response.cursors.len(), 1);++ // The hash path would register this image again; the fast path never looks.+ response.cursors.clear();+ assert_eq!(- cursor_id_for_image(cursor_data(red), &mut response, dir.path(), &mut last),++ cursor_id_for_image(cursor_data(red), &mut response, dir.path(), None, &mut last),+ "0"+ );+ assert!(response.cursors.is_empty());+ }++ #[test]+ fn a_changed_cursor_image_gets_hashed_and_a_new_id() {+ let dir = tempfile::tempdir().unwrap();+ let mut response = empty_response();+ let mut last = None;+ let red = png([255, 0, 0, 255]);+ let blue = png([0, 0, 255, 255]);++ for (image, expected) in [(red.clone(), "0"), (blue, "1"), (red, "0")] {+ assert_eq!(- cursor_id_for_image(cursor_data(image), &mut response, dir.path(), &mut last),++ cursor_id_for_image(++ cursor_data(image),++ &mut response,++ dir.path(),++ None,++ &mut last++ ),+ expected+ );+ }+ assert_eq!(response.cursors.len(), 2);+ assert!(dir.path().join("cursor_0.png").exists());+ assert!(dir.path().join("cursor_1.png").exists());+ }++ #[test]+ fn a_cursor_image_that_fails_to_decode_is_not_cached() {+ let dir = tempfile::tempdir().unwrap();+ let mut response = empty_response();+ let mut last = None;++ let id = cursor_id_for_image(+ cursor_data(b"not an image".to_vec()),+ &mut response,+ dir.path(),++ None,+ &mut last,+ );+ assert_eq!(id, "0");+ assert!(response.cursors.is_empty());+ assert!(last.is_none());+ }++ #[test]+ fn a_cursor_image_that_fails_to_save_is_retried() {+ let dir = tempfile::tempdir().unwrap();+ let mut response = empty_response();+ let mut last = None;+ let red = png([255, 0, 0, 255]);++ // A directory at the cursor's path makes the PNG write fail.+ let cursor_path = dir.path().join("cursor_0.png");+ std::fs::create_dir(&cursor_path).unwrap();+ cursor_id_for_image(+ cursor_data(red.clone()),+ &mut response,+ dir.path(),++ None,+ &mut last,+ );+ assert!(response.cursors.is_empty());+ assert!(last.is_none());++ std::fs::remove_dir(&cursor_path).unwrap();+ assert_eq!(- cursor_id_for_image(cursor_data(red), &mut response, dir.path(), &mut last),++ cursor_id_for_image(cursor_data(red), &mut response, dir.path(), None, &mut last),+ "0"+ );+ assert_eq!(response.cursors.len(), 1);+ assert!(cursor_path.is_file());+ }+}