Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Add cancellation support to system audio task

Richie McIlroy77250d687a7e
diff --git a/crates/recording/src/sources/screen_capture/macos.rs b/crates/recording/src/sources/screen_capture/macos.rs
index 6f0b8e56d..e8685feb5 100644
--- a/crates/recording/src/sources/screen_capture/macos.rs
+++ b/crates/recording/src/sources/screen_capture/macos.rs
@@ -795,27 +795,35 @@ impl output_pipeline::AudioSource for SystemAudioSource {
drop_counter,
) = config;
- ctx.tasks().spawn("system-audio", async move {
- loop {
- match error_rx.recv().await {
- Ok(err) => {
- if is_system_stop_error(err.as_ref()) {
- warn!("Screen capture audio stream stopped by the system");
- return Err(anyhow!(system_stop_message()));
- }
+ let cancel_token = CancellationToken::new();
- return Err(anyhow!("{err}"));
+ ctx.tasks().spawn("system-audio", {
+ let cancel = cancel_token.child_token();
+ async move {
+ loop {
+ tokio::select! {
+ biased;
+ _ = cancel.cancelled() => break,
+ result = error_rx.recv() => {
+ match result {
+ Ok(err) => {
+ if is_system_stop_error(err.as_ref()) {
+ warn!("Screen capture audio stream stopped by the system");
+ return Err(anyhow!(system_stop_message()));
+ }
+ return Err(anyhow!("{err}"));
+ }
+ Err(broadcast::error::RecvError::Closed) => break,
+ Err(broadcast::error::RecvError::Lagged(_)) => continue,
+ }
+ }
}
- Err(broadcast::error::RecvError::Closed) => break,
- Err(broadcast::error::RecvError::Lagged(_)) => continue,
}
- }
- Ok(())
+ Ok(())
+ }
});
- let cancel_token = CancellationToken::new();
-
let stats_cancel = cancel_token.clone();
tokio::spawn(
async move {