return values instead of wrapping them in needless results

This commit is contained in:
2026-09-21 01:26:53 +03:00
parent b2dd93fb09
commit e6e0d5b2b4
4 changed files with 26 additions and 24 deletions
+5 -2
View File
@@ -78,14 +78,17 @@ pub(crate) fn start(output_path: PathBuf) -> Result<AudioSession> {
} }
impl AudioSession { impl AudioSession {
pub fn stop(self) -> Result<PathBuf> { /// Stop the capture thread and hand back the PCM file it wrote. Joining can
/// only fail if the thread panicked, which is logged rather than returned -
/// the recorded audio is still usable either way.
pub fn stop(self) -> PathBuf {
self.running.store(false, Ordering::Relaxed); self.running.store(false, Ordering::Relaxed);
if let Some(t) = self.thread { if let Some(t) = self.thread {
if let Ok(Err(ref e)) = t.join() { if let Ok(Err(ref e)) = t.join() {
tracing::warn!("audio capture thread reported error on stop: {e:#}"); tracing::warn!("audio capture thread reported error on stop: {e:#}");
} }
} }
Ok(self.output_path) self.output_path
} }
/// True if the capture thread flagged an abnormal exit (endpoint lost, /// True if the capture thread flagged an abnormal exit (endpoint lost,
+10 -13
View File
@@ -45,13 +45,13 @@ const MAX_ENCODE_WIDTH: u32 = 3840;
const AUTO_ENCODE_WIDTH: u32 = 1920; const AUTO_ENCODE_WIDTH: u32 = 1920;
const FALLBACK_REFRESH_HZ: u32 = 60; const FALLBACK_REFRESH_HZ: u32 = 60;
/// Returns the cap to apply to the encode width, or None for native res. /// The cap to apply to the encode width in pixels.
/// Auto keeps the historic 1920 cap so nothing changes for existing users; /// Auto keeps the historic 1920 cap so nothing changes for existing users;
/// Full records at native res up to `MAX_ENCODE_WIDTH` (3840). /// Full records at native res up to `MAX_ENCODE_WIDTH` (3840).
fn encode_width_limit(quality: CaptureQuality) -> Option<u32> { fn encode_width_limit(quality: CaptureQuality) -> u32 {
match quality { match quality {
CaptureQuality::Auto => Some(AUTO_ENCODE_WIDTH), CaptureQuality::Auto => AUTO_ENCODE_WIDTH,
CaptureQuality::Full => Some(MAX_ENCODE_WIDTH), CaptureQuality::Full => MAX_ENCODE_WIDTH,
} }
} }
@@ -63,7 +63,7 @@ pub(crate) struct CaptureSetup {
pub height: u32, pub height: u32,
pub crop: Option<CaptureRect>, pub crop: Option<CaptureRect>,
pub pipe_fps: u32, pub pipe_fps: u32,
pub encode_width_limit: Option<u32>, pub encode_width_limit: u32,
pub frame_counter: Arc<std::sync::atomic::AtomicU64>, pub frame_counter: Arc<std::sync::atomic::AtomicU64>,
} }
@@ -453,12 +453,9 @@ impl GraphicsCaptureApiHandler for CaptureHandler {
let h = c.height.min(setup.height.saturating_sub(y.cast_unsigned())); let h = c.height.min(setup.height.saturating_sub(y.cast_unsigned()));
filter_parts.push(format!("crop={w}:{h}:{x}:{y}")); filter_parts.push(format!("crop={w}:{h}:{x}:{y}"));
} }
if let Some(limit) = setup.encode_width_limit { let limit = setup.encode_width_limit;
if setup.width > limit { if setup.width > limit {
filter_parts.push(format!("scale={limit}:-2")); filter_parts.push(format!("scale={limit}:-2"));
} else {
filter_parts.push("scale=trunc(iw/2)*2:trunc(ih/2)*2".to_string());
}
} else { } else {
filter_parts.push("scale=trunc(iw/2)*2:trunc(ih/2)*2".to_string()); filter_parts.push("scale=trunc(iw/2)*2:trunc(ih/2)*2".to_string());
} }
@@ -716,7 +713,7 @@ fn compute_output_dims(
monitor_w: u32, monitor_w: u32,
monitor_h: u32, monitor_h: u32,
crop: Option<CaptureRect>, crop: Option<CaptureRect>,
width_limit: Option<u32>, width_limit: u32,
) -> (u32, u32) { ) -> (u32, u32) {
let (raw_w, raw_h) = match crop { let (raw_w, raw_h) = match crop {
Some(c) => { Some(c) => {
@@ -729,7 +726,7 @@ fn compute_output_dims(
} }
None => (monitor_w, monitor_h), None => (monitor_w, monitor_h),
}; };
let cap = width_limit.unwrap_or(u32::MAX); let cap = width_limit;
if raw_w > cap { if raw_w > cap {
let scale = f64::from(cap) / f64::from(raw_w); let scale = f64::from(cap) / f64::from(raw_w);
let nh = crate::encode::round_u32(f64::from(raw_h) * scale); let nh = crate::encode::round_u32(f64::from(raw_h) * scale);
+8
View File
@@ -29,6 +29,10 @@ pub(crate) fn round_u32(v: f64) -> u32 {
clippy::cast_possible_truncation, clippy::cast_possible_truncation,
reason = "value is clamped into u32 range on the line above the cast" reason = "value is clamped into u32 range on the line above the cast"
)] )]
#[allow(
clippy::cast_sign_loss,
reason = "value is clamped to 0.0 or above on the line above the cast"
)]
let out = v.round().clamp(0.0, f64::from(u32::MAX)) as u32; let out = v.round().clamp(0.0, f64::from(u32::MAX)) as u32;
out out
} }
@@ -40,6 +44,10 @@ pub(crate) fn round_u64(v: f64) -> u64 {
clippy::cast_possible_truncation, clippy::cast_possible_truncation,
reason = "value is clamped into u64 range on the line above the cast" reason = "value is clamped into u64 range on the line above the cast"
)] )]
#[allow(
clippy::cast_sign_loss,
reason = "value is clamped to 0.0 or above on the line above the cast"
)]
let out = v.round().clamp(0.0, 18_446_744_073_709_551_615.0) as u64; let out = v.round().clamp(0.0, 18_446_744_073_709_551_615.0) as u64;
out out
} }
+3 -9
View File
@@ -602,9 +602,7 @@ fn pause_capture(state: State<'_, AppState>) -> Result<(), String> {
.map_err(|e| format!("fixing fps: {e:#}"))?; .map_err(|e| format!("fixing fps: {e:#}"))?;
let path = if fixed_path.exists() { fixed_path } else { raw_path }; let path = if fixed_path.exists() { fixed_path } else { raw_path };
let audio_path = lock(&state.audio) let audio_path = lock(&state.audio).take().map(|a| a.stop());
.take()
.and_then(|a| a.stop().ok());
let new_clicks: Vec<clicks::Click> = lock(&state.click_recorder) let new_clicks: Vec<clicks::Click> = lock(&state.click_recorder)
.take() .take()
@@ -701,9 +699,7 @@ fn restart_audio_capture(
let mut ss = lock(&state.session); let mut ss = lock(&state.session);
let ss = ss.as_mut().ok_or_else(|| "no session in progress".to_string())?; let ss = ss.as_mut().ok_or_else(|| "no session in progress".to_string())?;
if let Some(a) = lock(&state.audio).take() { if let Some(a) = lock(&state.audio).take() {
if let Ok(p) = a.stop() { ss.audio_segments.push(a.stop());
ss.audio_segments.push(p);
}
} }
ss.audio_segments.len() + 1 ss.audio_segments.len() + 1
}; };
@@ -747,9 +743,7 @@ fn stop_capture(app: AppHandle, state: State<'_, AppState>) -> Result<RecordedCl
let live_audio = lock(&state.audio).take(); let live_audio = lock(&state.audio).take();
if let Some(a) = live_audio { if let Some(a) = live_audio {
if let Ok(p) = a.stop() { ss.audio_segments.push(a.stop());
ss.audio_segments.push(p);
}
} }
let live_clicks = lock(&state.click_recorder) let live_clicks = lock(&state.click_recorder)