Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test: non-blocking synthetic feed admission and a scripted slow consumer

Richie McIlroy72a9fec1526f
diff --git a/crates/recording/src/feeds/camera.rs b/crates/recording/src/feeds/camera.rs
index 7907c2573..983abdaf8 100644
--- a/crates/recording/src/feeds/camera.rs
+++ b/crates/recording/src/feeds/camera.rs
@@ -1834,6 +1834,18 @@ impl Message<Unlock> for CameraFeed {
pub mod synthetic {
use super::*;
+ impl Message<crate::feeds::Stall> for CameraFeed {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: crate::feeds::Stall,
+ _: &mut Context<Self, Self::Reply>,
+ ) -> Self::Reply {
+ tokio::time::sleep(msg.0).await;
+ }
+ }
+
pub struct SyntheticCamera {
feed: ActorRef<CameraFeed>,
_done_rx: std::sync::Mutex<mpsc::Receiver<()>>,
@@ -1884,6 +1896,17 @@ pub mod synthetic {
let _ = self.feed.tell(NewNativeFrame(frame)).await;
}
+ /// Hands a frame over the way the macOS capture callback does
+ /// (`try_send`, never waiting for mailbox capacity).
+ pub fn try_push_native(&self, frame: NativeCameraFrame) -> crate::feeds::Admission {
+ crate::feeds::Admission::of(self.feed.tell(NewNativeFrame(frame)).try_send())
+ }
+
+ /// Blocks the feed actor for `duration` (scripted slow consumer).
+ pub fn stall(&self, duration: Duration) -> crate::feeds::Admission {
+ crate::feeds::Admission::of(self.feed.tell(crate::feeds::Stall(duration)).try_send())
+ }
+
pub async fn push(&self, frame: FFmpegVideoFrame) {
let _ = self.feed.tell(NewFrame(frame)).await;
}
diff --git a/crates/recording/src/feeds/microphone.rs b/crates/recording/src/feeds/microphone.rs
index 7d221903f..df542c01f 100644
--- a/crates/recording/src/feeds/microphone.rs
+++ b/crates/recording/src/feeds/microphone.rs
@@ -3096,6 +3096,18 @@ pub mod synthetic {
unsafe { std::mem::zeroed() }
}
+ impl Message<crate::feeds::Stall> for MicrophoneFeed {
+ type Reply = ();
+
+ async fn handle(
+ &mut self,
+ msg: crate::feeds::Stall,
+ _: &mut Context<Self, Self::Reply>,
+ ) -> Self::Reply {
+ tokio::time::sleep(msg.0).await;
+ }
+ }
+
pub struct SyntheticMicrophone {
feed: ActorRef<MicrophoneFeed>,
sample_rate: u32,
@@ -3157,6 +3169,30 @@ pub mod synthetic {
})
.await;
}
+
+ /// Hands samples over the way the capture callback does
+ /// (`enqueue_microphone_samples`: `try_send`, never waiting).
+ pub fn try_push(&self, samples: &[f32], timestamp: Timestamp) -> crate::feeds::Admission {
+ let data = samples.iter().flat_map(|s| s.to_ne_bytes()).collect();
+ crate::feeds::Admission::of(
+ self.feed
+ .tell(MicrophoneSamples {
+ stream_id: STREAM_ID,
+ data,
+ format: SampleFormat::F32,
+ sample_rate: self.sample_rate,
+ channels: self.channels,
+ info: zero_callback_info(),
+ timestamp,
+ })
+ .try_send(),
+ )
+ }
+
+ /// Blocks the feed actor for `duration` (scripted slow consumer).
+ pub fn stall(&self, duration: Duration) -> crate::feeds::Admission {
+ crate::feeds::Admission::of(self.feed.tell(crate::feeds::Stall(duration)).try_send())
+ }
}
}
diff --git a/crates/recording/src/feeds/mod.rs b/crates/recording/src/feeds/mod.rs
index 3bf6a9a9c..09263bf3b 100644
--- a/crates/recording/src/feeds/mod.rs
+++ b/crates/recording/src/feeds/mod.rs
@@ -1,2 +1,31 @@
pub mod camera;
pub mod microphone;
+
+/// Outcome of a synthetic device's hand-off. Device callbacks never wait for
+/// a feed: they `try_send` and the frame is lost when the feed's mailbox is
+/// full or the feed has stopped. Test-only sources hand over the same way so
+/// a feed that cannot keep up is visible as a rejection, not as a late
+/// source.
+#[cfg(feature = "test-utils")]
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum Admission {
+ Accepted,
+ Full,
+ Closed,
+}
+
+#[cfg(feature = "test-utils")]
+impl Admission {
+ fn of<M, E>(result: Result<(), kameo::error::SendError<M, E>>) -> Self {
+ match result {
+ Ok(()) => Self::Accepted,
+ Err(kameo::error::SendError::MailboxFull(_)) => Self::Full,
+ Err(_) => Self::Closed,
+ }
+ }
+}
+
+/// Test-only slow consumer: the feed actor handles nothing else for the
+/// given time, as a feed blocked on a downstream consumer would.
+#[cfg(feature = "test-utils")]
+pub struct Stall(pub std::time::Duration);