Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

clippy + comments

Richie McIlroyfd951b11e6e1
diff --git a/apps/desktop/src-tauri/src/general_settings.rs b/apps/desktop/src-tauri/src/general_settings.rs
index f587deea1..50ea5a9bf 100644
--- a/apps/desktop/src-tauri/src/general_settings.rs
+++ b/apps/desktop/src-tauri/src/general_settings.rs
@@ -344,14 +344,22 @@ fn register_bundled_muxer_binary(_app: &AppHandle) {
&& let Some(dir) = exe.parent()
{
let candidate = dir.join(bundled_muxer_bin_name());
- if candidate.exists() {
- unsafe {
- std::env::set_var(cap_recording::oop_muxer::ENV_BIN_PATH, &candidate);
+ if candidate.is_file() {
+ match cap_recording::oop_muxer::set_muxer_binary_override(candidate.clone()) {
+ Ok(()) => {
+ tracing::info!(
+ path = %candidate.display(),
+ "Registered executable-adjacent cap-muxer binary for out-of-process muxer"
+ );
+ }
+ Err(existing) => {
+ tracing::debug!(
+ existing = %existing.display(),
+ candidate = %candidate.display(),
+ "cap-muxer override already registered; keeping existing"
+ );
+ }
}
- tracing::info!(
- path = %candidate.display(),
- "Registered executable-adjacent cap-muxer binary for out-of-process muxer"
- );
}
}
}
diff --git a/apps/desktop/src-tauri/src/posthog.rs b/apps/desktop/src-tauri/src/posthog.rs
index 09e3bd64d..54872b2b3 100644
--- a/apps/desktop/src-tauri/src/posthog.rs
+++ b/apps/desktop/src-tauri/src/posthog.rs
@@ -354,7 +354,6 @@ pub fn set_telemetry_enabled(enabled: bool) {
TELEMETRY_ENABLED.store(enabled, Ordering::Release);
}
-#[allow(dead_code)]
pub fn telemetry_enabled() -> bool {
TELEMETRY_ENABLED.load(Ordering::Acquire)
}
@@ -368,7 +367,7 @@ pub fn async_capture_event(app: &AppHandle, event: PostHogEvent) {
.ok()
.flatten()
.map(|s| s.enable_telemetry)
- .unwrap_or(true);
+ .unwrap_or_else(telemetry_enabled);
TELEMETRY_ENABLED.store(live_enabled, Ordering::Release);
if !live_enabled {
return;
diff --git a/crates/cap-muxer/src/main.rs b/crates/cap-muxer/src/main.rs
index 3c556e702..91add0159 100644
--- a/crates/cap-muxer/src/main.rs
+++ b/crates/cap-muxer/src/main.rs
@@ -9,7 +9,7 @@ use std::ffi::CString;
use std::io::{self, BufReader, Write};
use std::path::PathBuf;
use std::process::ExitCode;
-use std::sync::{Arc, Condvar, Mutex};
+use std::sync::{Arc, Condvar, Mutex, PoisonError};
const EXIT_OK: u8 = 0;
const EXIT_PROTOCOL_ERROR: u8 = 10;
@@ -131,12 +131,12 @@ impl FrameQueue {
fn push(&self, frame: Frame) {
let frame_bytes = frame_size_hint(&frame);
- let mut guard = self.state.lock().expect("cap-muxer frame queue poisoned");
+ let mut guard = self.state.lock().unwrap_or_else(PoisonError::into_inner);
while guard.bytes + frame_bytes > guard.capacity_bytes && !guard.frames.is_empty() {
guard = self
.space_cv
.wait(guard)
- .expect("cap-muxer frame queue poisoned");
+ .unwrap_or_else(PoisonError::into_inner);
}
guard.bytes = guard.bytes.saturating_add(frame_bytes);
guard.frames.push_back(frame);
@@ -145,7 +145,7 @@ impl FrameQueue {
}
fn mark_reader_done(&self, err: Option<ProtocolError>) {
- let mut guard = self.state.lock().expect("cap-muxer frame queue poisoned");
+ let mut guard = self.state.lock().unwrap_or_else(PoisonError::into_inner);
guard.reader_done = true;
if err.is_some() {
guard.reader_err = err;
@@ -156,7 +156,7 @@ impl FrameQueue {
}
fn pop(&self) -> PopResult {
- let mut guard = self.state.lock().expect("cap-muxer frame queue poisoned");
+ let mut guard = self.state.lock().unwrap_or_else(PoisonError::into_inner);
loop {
if let Some(frame) = guard.frames.pop_front() {
let size = frame_size_hint(&frame);
@@ -175,7 +175,7 @@ impl FrameQueue {
guard = self
.data_cv
.wait(guard)
- .expect("cap-muxer frame queue poisoned");
+ .unwrap_or_else(PoisonError::into_inner);
}
}
}
diff --git a/crates/recording/src/output_pipeline/core.rs b/crates/recording/src/output_pipeline/core.rs
index b354a8cfc..269d1a9be 100644
--- a/crates/recording/src/output_pipeline/core.rs
+++ b/crates/recording/src/output_pipeline/core.rs
@@ -529,6 +529,7 @@ pub(crate) fn send_with_stall_budget_futures<T>(
}
}
+#[cfg_attr(not(any(target_os = "macos", test)), allow(dead_code))]
pub(crate) fn send_with_stall_budget_flume<T>(
tx: &flume::Sender<T>,
frame: T,
diff --git a/crates/recording/src/output_pipeline/oop_fragmented_m4s_win.rs b/crates/recording/src/output_pipeline/oop_fragmented_m4s_win.rs
index 67990eb16..46aa90a0f 100644
--- a/crates/recording/src/output_pipeline/oop_fragmented_m4s_win.rs
+++ b/crates/recording/src/output_pipeline/oop_fragmented_m4s_win.rs
@@ -558,7 +558,7 @@ impl WindowsOOPFragmentedM4SMuxer {
};
let normalized_ts = normalize_timestamp(timestamp, &mut first_timestamp);
- if let Err(e) = encode_one(
+ encode_one(
&mut encoder,
&mut subprocess,
&mut tracker,
@@ -566,9 +566,7 @@ impl WindowsOOPFragmentedM4SMuxer {
normalized_ts,
&mut slow_encode_count,
&mut total_frames,
- ) {
- return Err(e);
- }
+ )?;
}
let final_ts = last_timestamp
diff --git a/crates/recording/src/output_pipeline/oop_muxer.rs b/crates/recording/src/output_pipeline/oop_muxer.rs
index 919a531f4..de82e7e96 100644
--- a/crates/recording/src/output_pipeline/oop_muxer.rs
+++ b/crates/recording/src/output_pipeline/oop_muxer.rs
@@ -5,11 +5,12 @@ use cap_muxer_protocol::{
STREAM_INDEX_VIDEO, StartParams, write_frame,
};
use std::{
+ collections::VecDeque,
io::{BufWriter, Write},
path::{Path, PathBuf},
process::{Child, ChildStderr, ChildStdin, Command, ExitStatus, Stdio},
sync::{
- Arc,
+ Arc, OnceLock,
atomic::{AtomicBool, AtomicU64, Ordering},
},
thread::JoinHandle,
@@ -23,7 +24,23 @@ const RESPAWN_COOLDOWN: Duration = Duration::from_millis(500);
const RESPAWN_STABILITY_WINDOW: Duration = Duration::from_secs(5);
pub const EXIT_DISK_FULL: u8 = 60;
+static MUXER_BINARY_OVERRIDE: OnceLock<PathBuf> = OnceLock::new();
+
+pub fn set_muxer_binary_override(path: PathBuf) -> Result<(), PathBuf> {
+ MUXER_BINARY_OVERRIDE.set(path)
+}
+
pub fn resolve_muxer_binary() -> Result<PathBuf> {
+ if let Some(override_path) = MUXER_BINARY_OVERRIDE.get() {
+ if override_path.exists() {
+ return Ok(override_path.clone());
+ }
+ return Err(anyhow!(
+ "registered cap-muxer override points to missing path: {}",
+ override_path.display()
+ ));
+ }
+
if let Ok(override_path) = std::env::var(ENV_BIN_PATH) {
let path = PathBuf::from(override_path);
if path.exists() {
@@ -119,7 +136,7 @@ pub struct MuxerSubprocess {
child: Option<Child>,
stdin: Option<BufWriter<ChildStdin>>,
stderr_thread: Option<JoinHandle<Vec<String>>>,
- stderr_lines: Arc<parking_lot::Mutex<Vec<String>>>,
+ stderr_lines: Arc<parking_lot::Mutex<VecDeque<String>>>,
bin_path: PathBuf,
config: MuxerSubprocessConfig,
health_tx: Option<HealthSender>,
@@ -172,8 +189,15 @@ impl MuxerSubprocess {
.take()
.ok_or_else(|| MuxerSubprocessError::Spawn(anyhow!("subprocess stderr missing")))?;
- let stderr_lines = Arc::new(parking_lot::Mutex::new(Vec::<String>::new()));
- let stderr_thread = Some(spawn_stderr_reader(stderr, stderr_lines.clone()));
+ let stderr_lines = Arc::new(parking_lot::Mutex::new(VecDeque::<String>::new()));
+ let stderr_thread = match spawn_stderr_reader(stderr, stderr_lines.clone()) {
+ Ok(handle) => Some(handle),
+ Err(e) => {
+ let _ = child.kill();
+ let _ = child.wait();
+ return Err(e);
+ }
+ };
let mut subprocess = Self {
child: Some(child),
@@ -391,7 +415,7 @@ impl MuxerSubprocess {
}
fn snapshot_stderr(&self) -> Vec<String> {
- self.stderr_lines.lock().clone()
+ self.stderr_lines.lock().iter().cloned().collect()
}
fn report_crashed(&self, reason: &str) {
@@ -479,12 +503,17 @@ impl MuxerSubprocess {
exit_code,
stderr_tail.iter().rev().take(3).collect::<Vec<_>>()
);
- if exit_code == Some(EXIT_DISK_FULL as i32) {
+ let is_disk_full = exit_code == Some(EXIT_DISK_FULL as i32);
+ if is_disk_full {
self.report_disk_full(&reason);
} else {
self.report_crashed(&reason);
}
- return Err(MuxerSubprocessError::Crashed(reason));
+ return Err(if is_disk_full {
+ MuxerSubprocessError::DiskFull(reason)
+ } else {
+ MuxerSubprocessError::Crashed(reason)
+ });
}
info!(
@@ -524,8 +553,8 @@ pub struct MuxerSubprocessReport {
fn spawn_stderr_reader(
stderr: ChildStderr,
- lines: Arc<parking_lot::Mutex<Vec<String>>>,
-) -> JoinHandle<Vec<String>> {
+ lines: Arc<parking_lot::Mutex<VecDeque<String>>>,
+) -> Result<JoinHandle<Vec<String>>, MuxerSubprocessError> {
std::thread::Builder::new()
.name("cap-muxer-stderr".into())
.spawn(move || {
@@ -537,9 +566,9 @@ fn spawn_stderr_reader(
Ok(line) => {
debug!(target: "cap_muxer_stderr", "{line}");
let mut guard = lines.lock();
- guard.push(line.clone());
+ guard.push_back(line.clone());
if guard.len() > STDERR_RING_LIMIT {
- guard.remove(0);
+ guard.pop_front();
}
drop(guard);
final_lines.push(line);
@@ -555,7 +584,9 @@ fn spawn_stderr_reader(
}
final_lines
})
- .expect("failed to spawn cap-muxer-stderr thread")
+ .map_err(|e| {
+ MuxerSubprocessError::Spawn(anyhow!("failed to spawn cap-muxer-stderr thread: {e}"))
+ })
}
pub struct RespawningMuxerSubprocess {