Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
Add checked_duration_since for timestamp types
Richie McIlroyfd30b14ffab2
diff --git a/crates/recording/src/output_pipeline/core.rs b/crates/recording/src/output_pipeline/core.rsindex 1c5ff7b8b..56ec9e95a 100644--- a/crates/recording/src/output_pipeline/core.rs+++ b/crates/recording/src/output_pipeline/core.rs@@ -464,10 +464,12 @@ fn spawn_video_encoder<TMutex: VideoMuxer<VideoFrame = TVideo::Frame>, TVideo: Vlet _ = first_tx.send(timestamp);}+ let duration = timestamp.checked_duration_since(timestamps).unwrap_or(Duration::ZERO);+muxer.lock().await- .send_video_frame(frame, timestamp.duration_since(timestamps))+ .send_video_frame(frame, duration).map_err(|e| anyhow!("Error queueing video frame: {e}"))?;}@@ -476,12 +478,16 @@ fn spawn_video_encoder<TMutex: VideoMuxer<VideoFrame = TVideo::Frame>, TVideo: V}).await;+ muxer.lock().await.stop();++ if let Some(Err(e)) = res {+ return Err(e);+ }+if res.is_none() {info!("mux-video cancelled");}- muxer.lock().await.stop();-Ok(())});}@@ -512,7 +518,7 @@ impl PreparedAudioSources {let _ = first_tx.send(frame.timestamp);}- let timestamp = frame.timestamp.duration_since(timestamps);+ let timestamp = frame.timestamp.checked_duration_since(timestamps).unwrap_or(Duration::ZERO);if let Err(e) = muxer.lock().await.send_audio_frame(frame, timestamp) {error!("Audio encoder: {e}");}diff --git a/crates/timestamp/src/lib.rs b/crates/timestamp/src/lib.rsindex c5f3ffb7c..aafa3dd3e 100644--- a/crates/timestamp/src/lib.rs+++ b/crates/timestamp/src/lib.rs@@ -32,6 +32,17 @@ impl Timestamp {}}+ pub fn checked_duration_since(&self, start: Timestamps) -> Option<Duration> {+ match self {+ Self::Instant(instant) => instant.checked_duration_since(start.instant),+ Self::SystemTime(time) => time.duration_since(start.system_time).ok(),+ #[cfg(windows)]+ Self::PerformanceCounter(counter) => counter.checked_duration_since(start.performance_counter),+ #[cfg(target_os = "macos")]+ Self::MachAbsoluteTime(time) => time.checked_duration_since(start.mach_absolute_time),+ }+ }+pub fn from_cpal(instant: cpal::StreamInstant) -> Self {#[cfg(windows)]{diff --git a/crates/timestamp/src/macos.rs b/crates/timestamp/src/macos.rsindex a316d7533..b72653350 100644--- a/crates/timestamp/src/macos.rs+++ b/crates/timestamp/src/macos.rs@@ -30,6 +30,15 @@ impl MachAbsoluteTimestamp {Duration::from_nanos((diff as f64 * freq) as u64)}+ pub fn checked_duration_since(&self, other: Self) -> Option<Duration> {+ let info = TimeBaseInfo::new();+ let freq = info.numer as f64 / info.denom as f64;++ let diff = self.0.checked_sub(other.0)?;++ Some(Duration::from_nanos((diff as f64 * freq) as u64))+ }+pub fn from_cpal(instant: cpal::StreamInstant) -> Self {use cpal::host::coreaudio::StreamInstantExt;diff --git a/crates/timestamp/src/win.rs b/crates/timestamp/src/win.rsindex ee2edc6dd..d3efe2d44 100644--- a/crates/timestamp/src/win.rs+++ b/crates/timestamp/src/win.rs@@ -46,6 +46,25 @@ impl PerformanceCounterTimestamp {}}+ pub fn checked_duration_since(&self, other: Self) -> Option<Duration> {+ let freq = perf_freq() as i128;+ debug_assert!(freq > 0);++ let diff = self.0 as i128 - other.0 as i128;++ if diff < 0 {+ None+ } else {+ let diff = diff as u128;+ let freq = freq as u128;++ let secs = diff / freq;+ let nanos = ((diff % freq) * 1_000_000_000u128) / freq;++ Some(Duration::new(secs as u64, nanos as u32))+ }+ }+pub fn now() -> Self {let mut value = 0;unsafe { QueryPerformanceCounter(&mut value).unwrap() };