Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
wip: Screen capture stride bug
Richie McIlroy960b29fc0c7a
diff --git a/apps/desktop/src-tauri/Cargo.lock b/apps/desktop/src-tauri/Cargo.lockindex 6d44fe982..a9e24d907 100644--- a/apps/desktop/src-tauri/Cargo.lock+++ b/apps/desktop/src-tauri/Cargo.lock@@ -648,6 +648,7 @@ version = "0.0.1"dependencies = ["block","cfg-if",+ "core-foundation","libc","winapi",]diff --git a/apps/desktop/src-tauri/src/capture/Cargo.toml b/apps/desktop/src-tauri/src/capture/Cargo.tomlindex 3aa768672..2062caad0 100644--- a/apps/desktop/src-tauri/src/capture/Cargo.toml+++ b/apps/desktop/src-tauri/src/capture/Cargo.toml@@ -7,4 +7,5 @@ version = "0.0.1"block = "0.1.6"cfg-if = "1.0.0"libc = "0.2.153"-winapi = "0.3.9"\ No newline at end of file+winapi = "0.3.9"+core-foundation = "0.9.4"\ No newline at end of filediff --git a/apps/desktop/src-tauri/src/capture/README.md b/apps/desktop/src-tauri/src/capture/README.mdnew file mode 100644index 000000000..0ae0c20da--- /dev/null+++ b/apps/desktop/src-tauri/src/capture/README.md@@ -0,0 +1,4 @@+# Capture module. The main recording engine with bindings for macOS, Windows and Linux.++This has been derived from the awesome https://github.com/quadrupleslap/scrap crate, which is no longer maintained.+diff --git a/apps/desktop/src-tauri/src/capture/src/common/quartz.rs b/apps/desktop/src-tauri/src/capture/src/common/quartz.rsindex 47439447d..e4478eec7 100644--- a/apps/desktop/src-tauri/src/capture/src/common/quartz.rs+++ b/apps/desktop/src-tauri/src/capture/src/common/quartz.rs@@ -17,7 +17,7 @@ impl Capturer {display.0,width,height,- quartz::PixelFormat::YCbCr420Video,+ quartz::PixelFormat::Argb8888,Default::default(),move |inner| {if let Ok(mut f) = f.lock() {@@ -84,4 +84,21 @@ impl Display {pub fn height(&self) -> usize {self.0.height()}-}\ No newline at end of file++ pub fn stride(&self) -> usize {+ self.0.stride()+ }++ pub fn bits_per_pixel(&self) -> usize {+ self.0.bits_per_pixel()+ }++ pub fn bits_per_sample(&self) -> usize {+ self.0.bits_per_sample()+ }++ pub fn samples_per_pixel(&self) -> usize {+ self.0.samples_per_pixel()+ }+}+diff --git a/apps/desktop/src-tauri/src/capture/src/quartz/capturer.rs b/apps/desktop/src-tauri/src/capture/src/quartz/capturer.rsindex 969038060..6d83f693c 100644--- a/apps/desktop/src-tauri/src/capture/src/quartz/capturer.rs+++ b/apps/desktop/src-tauri/src/capture/src/quartz/capturer.rs@@ -36,7 +36,7 @@ impl Capturer {let queue = unsafe {dispatch_queue_create(- b"quadrupleslap.scrap\0".as_ptr() as *const i8,+ b"cap.capture\0".as_ptr() as *const i8,ptr::null_mut())};diff --git a/apps/desktop/src-tauri/src/capture/src/quartz/display.rs b/apps/desktop/src-tauri/src/capture/src/quartz/display.rsindex 39813c74f..4e904256b 100644--- a/apps/desktop/src-tauri/src/capture/src/quartz/display.rs+++ b/apps/desktop/src-tauri/src/capture/src/quartz/display.rs@@ -46,6 +46,24 @@ impl Display {}}+ pub fn stride(self) -> usize {+ unsafe {+ CGDisplayBytesPerRow(self.0) as usize+ }+ }++ pub fn bits_per_pixel(self) -> usize {+ unsafe { CGDisplayBitsPerPixel(self.0) as usize }+ }++ pub fn bits_per_sample(self) -> usize {+ unsafe { CGDisplayBitsPerSample(self.0) as usize }+ }++ pub fn samples_per_pixel(self) -> usize {+ unsafe { CGDisplaySamplesPerPixel(self.0) as usize }+ }+pub fn is_builtin(self) -> bool {unsafe { CGDisplayIsBuiltin(self.0) != 0 }}@@ -61,4 +79,5 @@ impl Display {pub fn is_online(self) -> bool {unsafe { CGDisplayIsOnline(self.0) != 0 }}-}\ No newline at end of file+}+diff --git a/apps/desktop/src-tauri/src/capture/src/quartz/ffi.rs b/apps/desktop/src-tauri/src/capture/src/quartz/ffi.rsindex 994b294a3..ec4abaef1 100644--- a/apps/desktop/src-tauri/src/capture/src/quartz/ffi.rs+++ b/apps/desktop/src-tauri/src/capture/src/quartz/ffi.rs@@ -161,6 +161,7 @@ extern {pub fn CGMainDisplayID() -> u32;pub fn CGDisplayCopyDisplayMode(display: u32) -> CGDisplayModeRef;+// pub fn CGDisplayModeGetHeight(mode: CGDisplayModeRef) -> libc::size_t;// pub fn CGDisplayModeGetWidth(mode: CGDisplayModeRef) -> libc::size_t;pub fn CGDisplayModeGetPixelHeight(mode: CGDisplayModeRef) -> libc::size_t;@@ -170,6 +171,10 @@ extern {// pub fn CGDisplayModeCopyPixelEncoding(mode: CGDisplayModeRef) -> CFStringRef;pub fn CGDisplayPixelsWide(display: u32) -> usize;pub fn CGDisplayPixelsHigh(display: u32) -> usize;+ pub fn CGDisplayBytesPerRow(display: u32) -> usize;+ pub fn CGDisplayBitsPerPixel(display: u32) -> usize;+ pub fn CGDisplayBitsPerSample(display: u32) -> usize;+ pub fn CGDisplaySamplesPerPixel(display: u32) -> usize;pub fn CGGetOnlineDisplayList(max_displays: u32,@@ -198,6 +203,8 @@ extern {options: u32,seed: *mut u32) -> i32;+ pub fn IOSurfaceGetBaseAddressOfPlane(buffer: IOSurfaceRef, index: usize) -> *mut c_void;+ pub fn IOSurfaceGetBytesPerRowOfPlane(buffer: IOSurfaceRef, index: usize) -> usize;// Dispatchdiff --git a/apps/desktop/src-tauri/src/media.rs b/apps/desktop/src-tauri/src/media.rsindex b09d8e4dc..80790468f 100644--- a/apps/desktop/src-tauri/src/media.rs+++ b/apps/desktop/src-tauri/src/media.rs@@ -62,7 +62,6 @@ impl MediaRecorder {let host = cpal::default_host();let devices = host.devices().expect("Failed to get devices");let display = Display::primary().expect("Failed to find primary display");-let mut w = max_screen_width;let mut h = max_screen_height;@@ -71,12 +70,14 @@ impl MediaRecorder {let capture_size = adjusted_width * adjusted_height * 4;let (audio_tx, audio_rx) = tokio::sync::mpsc::channel::<Vec<u8>>(2048);let (video_tx, video_rx) = tokio::sync::mpsc::channel::<Vec<u8>>(2048);-+ let calculated_stride = (adjusted_width * 4) as usize;+println!("Display width: {}", w);println!("Display height: {}", h);println!("Adjusted width: {}", adjusted_width);println!("Adjusted height: {}", adjusted_height);println!("Capture size: {}", capture_size);+ println!("Calculated stride: {}", calculated_stride);let audio_start_time = Arc::new(Mutex::new(None));let video_start_time = Arc::new(Mutex::new(None));@@ -305,14 +306,12 @@ impl MediaRecorder {let frame_clone = frame.to_vec();std::thread::spawn(move || {let mut frame_data = Vec::with_capacity(capture_size.try_into().unwrap());- let stride = w_cloned * 4;println!("Frame length: {}", frame_clone.len());- println!("Stride: {}", stride);let rt = tokio::runtime::Runtime::new().unwrap();for row in 0..adjusted_height {- let start: usize = row as usize * stride as usize;- let end: usize = start + stride as usize;+ let start: usize = row as usize * calculated_stride as usize;+ let end: usize = start + calculated_stride as usize;let mut row_data = frame_clone[start..end].to_vec();for chunk in row_data.chunks_mut(4) {chunk.swap(0, 2);@@ -356,37 +355,28 @@ impl MediaRecorder {match capturer.frame() {Ok(frame) => {- let y_plane_size = adjusted_width * adjusted_height;- let uv_plane_size = y_plane_size / 4;- let total_size = y_plane_size + uv_plane_size * 2;-- if frame.len() >= total_size {- frame_data.extend_from_slice(&frame[..y_plane_size]);- for i in 0..uv_plane_size {- frame_data.push(frame[y_plane_size + i]);- frame_data.push(frame[y_plane_size + uv_plane_size + i]);- }-- if let Some(sender) = &video_channel_sender {- if sender.try_send(frame_data).is_err() {- eprintln!("Channel send error. Dropping data.");- }+ for row in 0..adjusted_height {+ let start: usize = row as usize * calculated_stride as usize;+ let end: usize = start + calculated_stride as usize;+ frame_data.extend_from_slice(&frame[start..end]);+ }+ if let Some(sender) = &video_channel_sender {+ if sender.try_send(frame_data).is_err() {+ eprintln!("Channel send error. Dropping data.");}+ }- let mut first_frame_time_guard = video_start_time_clone.try_lock();+ let mut first_frame_time_guard = video_start_time_clone.try_lock();- if let Ok(ref mut start_time_option) = first_frame_time_guard {- if start_time_option.is_none() {- **start_time_option = Some(Instant::now());+ if let Ok(ref mut start_time_option) = first_frame_time_guard {+ if start_time_option.is_none() {+ **start_time_option = Some(Instant::now());- println!("Video start time captured");- }+ println!("Video start time captured");}-- frame_count += 1;- } else {- eprintln!("Unexpected frame size: {}", frame.len());}++ frame_count += 1;},Err(error) if error.kind() == WouldBlock => {std::thread::sleep(Duration::from_millis(1));@@ -446,8 +436,8 @@ impl MediaRecorder {let mut ffmpeg_video_command: Vec<String> = vec!["-f", "rawvideo",- "-pix_fmt", "nv12",- "-s", &format!("{}x{}", w, adjusted_height),+ "-pix_fmt", "bgra",+ "-s", &format!("{}x{}", adjusted_width, adjusted_height),"-r", "30","-thread_queue_size", "4096","-i", "pipe:0",