Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

clippy

Richie McIlroybbd80f325925
diff --git a/crates/enc-ffmpeg/src/mux/segmented_stream.rs b/crates/enc-ffmpeg/src/mux/segmented_stream.rs
index 8dab6f9ae..91df680cc 100644
--- a/crates/enc-ffmpeg/src/mux/segmented_stream.rs
+++ b/crates/enc-ffmpeg/src/mux/segmented_stream.rs
@@ -37,10 +37,10 @@ fn atomic_write_json<T: Serialize>(path: &Path, data: &T) -> std::io::Result<()>
}
fn sync_file(path: &Path) {
- if let Ok(file) = std::fs::File::open(path) {
- if let Err(e) = file.sync_all() {
- tracing::warn!("File fsync failed for {}: {e}", path.display());
- }
+ if let Ok(file) = std::fs::File::open(path)
+ && let Err(e) = file.sync_all()
+ {
+ tracing::warn!("File fsync failed for {}: {e}", path.display());
}
}
@@ -161,7 +161,7 @@ impl SegmentedVideoEncoder {
set_opt("media_seg_name", "segment_$Number%03d$.m4s");
set_opt(
"seg_duration",
- &config.segment_duration.as_secs().to_string(),
+ &config.segment_duration.as_secs_f64().to_string(),
);
set_opt("use_timeline", "0");
set_opt("use_template", "1");
@@ -428,30 +428,29 @@ impl SegmentedVideoEncoder {
for entry in entries.flatten() {
let path = entry.path();
- if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
- if name.starts_with("segment_") && name.ends_with(".m4s.tmp") {
- let final_name = name.trim_end_matches(".tmp");
- let final_path = self.base_path.join(final_name);
-
- if let Ok(metadata) = std::fs::metadata(&path) {
- if metadata.len() > 0 {
- if let Err(e) = std::fs::rename(&path, &final_path) {
- tracing::warn!(
- "Failed to rename tmp segment {} to {}: {}",
- path.display(),
- final_path.display(),
- e
- );
- } else {
- tracing::debug!(
- "Finalized pending segment: {} ({} bytes)",
- final_path.display(),
- metadata.len()
- );
- sync_file(&final_path);
- }
- }
- }
+ if let Some(name) = path.file_name().and_then(|n| n.to_str())
+ && name.starts_with("segment_")
+ && name.ends_with(".m4s.tmp")
+ && let Ok(metadata) = std::fs::metadata(&path)
+ && metadata.len() > 0
+ {
+ let final_name = name.trim_end_matches(".tmp");
+ let final_path = self.base_path.join(final_name);
+
+ if let Err(e) = std::fs::rename(&path, &final_path) {
+ tracing::warn!(
+ "Failed to rename tmp segment {} to {}: {}",
+ path.display(),
+ final_path.display(),
+ e
+ );
+ } else {
+ tracing::debug!(
+ "Finalized pending segment: {} ({} bytes)",
+ final_path.display(),
+ metadata.len()
+ );
+ sync_file(&final_path);
}
}
}
@@ -474,20 +473,17 @@ impl SegmentedVideoEncoder {
for entry in entries.flatten() {
let path = entry.path();
- if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
- if name.starts_with("segment_") && name.ends_with(".m4s") && !name.contains(".tmp")
- {
- if let Some(index_str) = name
- .strip_prefix("segment_")
- .and_then(|s| s.strip_suffix(".m4s"))
- {
- if let Ok(index) = index_str.parse::<u32>() {
- if !completed_indices.contains(&index) {
- orphaned.push((index, path));
- }
- }
- }
- }
+ if let Some(name) = path.file_name().and_then(|n| n.to_str())
+ && name.starts_with("segment_")
+ && name.ends_with(".m4s")
+ && !name.contains(".tmp")
+ && let Some(index_str) = name
+ .strip_prefix("segment_")
+ .and_then(|s| s.strip_suffix(".m4s"))
+ && let Ok(index) = index_str.parse::<u32>()
+ && !completed_indices.contains(&index)
+ {
+ orphaned.push((index, path));
}
}
diff --git a/crates/recording/src/cursor.rs b/crates/recording/src/cursor.rs
index 405c97bf5..796093d03 100644
--- a/crates/recording/src/cursor.rs
+++ b/crates/recording/src/cursor.rs
@@ -72,11 +72,8 @@ pub fn spawn_cursor_recorder(
use cap_utils::spawn_actor;
use device_query::{DeviceQuery, DeviceState};
use futures::future::Either;
- use std::{
- hash::{DefaultHasher, Hash, Hasher},
- pin::pin,
- time::Duration,
- };
+ use sha2::{Digest, Sha256};
+ use std::{pin::pin, time::Duration};
use tracing::{error, info};
let stop_token = CancellationToken::new();
@@ -100,7 +97,7 @@ pub fn spawn_cursor_recorder(
let mut last_flush = Instant::now();
let flush_interval = Duration::from_secs(CURSOR_FLUSH_INTERVAL_SECS);
- let mut last_cursor_id = "default".to_string();
+ let mut last_cursor_id: Option<String> = None;
loop {
let sleep = tokio::time::sleep(Duration::from_millis(16));
@@ -116,51 +113,57 @@ pub fn spawn_cursor_recorder(
let position = cap_cursor_capture::RawCursorPosition::get();
let position_changed = position != last_position;
- let cursor_id = if position_changed {
+ if position_changed {
last_position = position;
- if let Some(data) = get_cursor_data() {
- let mut hasher = DefaultHasher::default();
- data.image.hash(&mut hasher);
- let id = hasher.finish();
-
- 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);
- response.cursors.insert(
- id,
- Cursor {
- file_name,
- id: response.next_cursor_id,
- hotspot: data.hotspot,
- shape: data.shape,
- },
- );
- response.next_cursor_id += 1;
- }
+ }
+
+ let cursor_id = if let Some(data) = get_cursor_data() {
+ 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);
+ 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 = cursor_id.clone();
cursor_id
- } else {
- last_cursor_id.clone()
- }
+ };
+ last_cursor_id = Some(cursor_id.clone());
+ Some(cursor_id)
} else {
last_cursor_id.clone()
};
+ let Some(cursor_id) = cursor_id else {
+ continue;
+ };
+
if position_changed {
let cropped_norm_pos = position
.relative_to_display(display)