Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Fix single instance plugin (#800)

Brendan Alland5ba9847a936
diff --git a/Cargo.lock b/Cargo.lock
index 6da168520..a9c4b6fbb 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1105,6 +1105,7 @@ dependencies = [
"serde",
"specta",
"tokio",
+ "tokio-util",
"tracing",
]
@@ -1191,6 +1192,7 @@ dependencies = [
"tempfile",
"thiserror 1.0.69",
"tokio",
+ "tokio-util",
"tracing",
"windows 0.58.0",
"windows-capture",
@@ -1237,6 +1239,7 @@ dependencies = [
"tempfile",
"thiserror 1.0.69",
"tokio",
+ "tokio-util",
"tracing",
"tracing-subscriber",
"windows 0.58.0",
diff --git a/Cargo.toml b/Cargo.toml
index 7386f16e6..1f6b84969 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -59,6 +59,7 @@ percent-encoding = "2.3.1"
[workspace.lints.clippy]
dbg_macro = "deny"
+let_underscore_future = "deny"
[patch.crates-io]
screencapturekit = { git = "https://github.com/CapSoftware/screencapturekit-rs", rev = "7ff1e103742e56c8f6c2e940b5e52684ed0bed69" } # branch = "cap-main"
diff --git a/apps/desktop/src-tauri/src/camera.rs b/apps/desktop/src-tauri/src/camera.rs
index f9c659f96..6b5ad7c95 100644
--- a/apps/desktop/src-tauri/src/camera.rs
+++ b/apps/desktop/src-tauri/src/camera.rs
@@ -1,11 +1,8 @@
use cap_media::{feeds::RawCameraFrame, frame_ws::WSFrame};
use flume::Sender;
+use tokio_util::sync::CancellationToken;
-pub async fn create_camera_preview_ws() -> (
- Sender<RawCameraFrame>,
- u16,
- tauri::async_runtime::Sender<()>,
-) {
+pub async fn create_camera_preview_ws() -> (Sender<RawCameraFrame>, u16, CancellationToken) {
let (camera_tx, mut _camera_rx) = flume::bounded::<RawCameraFrame>(4);
let (_camera_tx, camera_rx) = flume::bounded::<WSFrame>(4);
std::thread::spawn(move || {
diff --git a/apps/desktop/src-tauri/src/lib.rs b/apps/desktop/src-tauri/src/lib.rs
index 040308cf7..dc5f5a9cc 100644
--- a/apps/desktop/src-tauri/src/lib.rs
+++ b/apps/desktop/src-tauri/src/lib.rs
@@ -1864,7 +1864,8 @@ pub async fn run(recording_logging_handle: LoggingHandle) {
.find(|arg| arg.ends_with(".cap"))
.map(PathBuf::from)
else {
- let _ = ShowCapWindow::Main.show(app);
+ let app = app.clone();
+ tokio::spawn(async move { ShowCapWindow::Main.show(&app).await });
return;
};
@@ -2111,7 +2112,7 @@ pub async fn run(recording_logging_handle: LoggingHandle) {
}
} else {
let handle = handle.clone();
- let _ = tokio::spawn(async move { ShowCapWindow::Main.show(&handle).await });
+ tokio::spawn(async move { ShowCapWindow::Main.show(&handle).await });
}
}
tauri::RunEvent::ExitRequested { code, api, .. } => {
diff --git a/crates/camera-directshow/Cargo.toml b/crates/camera-directshow/Cargo.toml
index b4aadbd61..7833edf5d 100644
--- a/crates/camera-directshow/Cargo.toml
+++ b/crates/camera-directshow/Cargo.toml
@@ -3,7 +3,7 @@ name = "cap-camera-directshow"
version = "0.1.0"
edition = "2024"
-[dependencies]
+[target.'cfg(target_os= "windows")'.dependencies]
windows-core = { version = "0.60" }
windows = { version = "0.60", features = [
"Win32_System_Com",
diff --git a/crates/camera-mediafoundation/Cargo.toml b/crates/camera-mediafoundation/Cargo.toml
index f514b427f..74cb0a92b 100644
--- a/crates/camera-mediafoundation/Cargo.toml
+++ b/crates/camera-mediafoundation/Cargo.toml
@@ -3,7 +3,7 @@ name = "cap-camera-mediafoundation"
version = "0.1.0"
edition = "2024"
-[dependencies]
+[target.'cfg(target_os= "windows")'.dependencies]
windows-core = { version = "0.60" }
windows = { version = "0.60", features = [
"Win32_Media_MediaFoundation",
diff --git a/crates/camera-windows/Cargo.toml b/crates/camera-windows/Cargo.toml
index 65e95c58a..47b9fad1d 100644
--- a/crates/camera-windows/Cargo.toml
+++ b/crates/camera-windows/Cargo.toml
@@ -5,13 +5,14 @@ edition = "2024"
[dependencies]
ffmpeg = { workspace = true }
+thiserror.workspace = true
+
cap-camera-mediafoundation = { path = "../camera-mediafoundation" }
cap-camera-directshow = { path = "../camera-directshow" }
+[target.'cfg(target_os= "windows")'.dependencies]
windows = { version = "0.60", features = ["Win32_Media_MediaFoundation"] }
windows-core = { version = "0.60" }
-thiserror.workspace = true
-inquire = "0.7.5"
[lints]
workspace = true
diff --git a/crates/editor/Cargo.toml b/crates/editor/Cargo.toml
index 54f5f4647..d25c29698 100644
--- a/crates/editor/Cargo.toml
+++ b/crates/editor/Cargo.toml
@@ -22,3 +22,4 @@ sentry.workspace = true
futures = "0.3.31"
tracing.workspace = true
flume.workspace = true
+tokio-util = "0.7.15"
diff --git a/crates/editor/src/editor_instance.rs b/crates/editor/src/editor_instance.rs
index 4b353e6bd..459e484fd 100644
--- a/crates/editor/src/editor_instance.rs
+++ b/crates/editor/src/editor_instance.rs
@@ -15,6 +15,8 @@ use std::ops::Deref;
use std::sync::Mutex as StdMutex;
use std::{path::PathBuf, sync::Arc};
use tokio::sync::{mpsc, watch, Mutex};
+use tokio_util::sync::CancellationToken;
+use tracing::trace;
pub struct EditorInstance {
pub project_path: PathBuf,
@@ -29,7 +31,7 @@ pub struct EditorInstance {
watch::Sender<ProjectConfiguration>,
watch::Receiver<ProjectConfiguration>,
),
- ws_shutdown: Arc<StdMutex<Option<mpsc::Sender<()>>>>,
+ ws_shutdown_token: CancellationToken,
pub segments: Arc<Vec<Segment>>,
meta: RecordingMeta,
}
@@ -62,7 +64,7 @@ impl EditorInstance {
let (frame_tx, frame_rx) = flume::bounded(4);
- let (ws_port, ws_shutdown) = create_frame_ws(frame_rx).await;
+ let (ws_port, ws_shutdown_token) = create_frame_ws(frame_rx).await;
let render_constants = Arc::new(
RenderVideoConstants::new(&recordings.segments, &recording_meta, meta)
@@ -93,7 +95,7 @@ impl EditorInstance {
on_state_change: Box::new(on_state_change),
preview_tx,
project_config: watch::channel(project),
- ws_shutdown: Arc::new(StdMutex::new(Some(ws_shutdown))),
+ ws_shutdown_token,
segments: Arc::new(segments),
meta: recording_meta,
});
@@ -109,31 +111,29 @@ impl EditorInstance {
}
pub async fn dispose(&self) {
- println!("Disposing EditorInstance");
+ trace!("Disposing EditorInstance");
let mut state = self.state.lock().await;
// Stop playback
if let Some(handle) = state.playback_task.take() {
- println!("Stopping playback");
+ trace!("Stopping playback");
handle.stop();
}
// Stop preview
if let Some(task) = state.preview_task.take() {
- println!("Stopping preview");
+ trace!("Stopping preview");
task.abort();
task.await.ok(); // Await the task to ensure it's fully stopped
}
// Stop WebSocket server
- if let Some(ws_shutdown) = self.ws_shutdown.lock().unwrap().take() {
- println!("Shutting down WebSocket server");
- let _ = ws_shutdown.send(());
- }
+ trace!("Shutting down WebSocket server");
+ self.ws_shutdown_token.cancel();
// Stop renderer
- println!("Stopping renderer");
+ trace!("Stopping renderer");
self.renderer.stop().await;
// // Clear audio data
diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml
index 9ab6acb5f..52a00b63a 100644
--- a/crates/media/Cargo.toml
+++ b/crates/media/Cargo.toml
@@ -37,6 +37,7 @@ tokio.workspace = true
cap-fail = { version = "0.1.0", path = "../fail" }
image = { version = "0.25.2", features = ["gif"] }
gif = "0.13.1"
+tokio-util = "0.7.15"
[target.'cfg(target_os = "macos")'.dependencies]
cidre = { workspace = true }
diff --git a/crates/media/src/frame_ws.rs b/crates/media/src/frame_ws.rs
index 6cb5f512b..d37d8df04 100644
--- a/crates/media/src/frame_ws.rs
+++ b/crates/media/src/frame_ws.rs
@@ -2,6 +2,7 @@ use std::sync::Arc;
use flume::Receiver;
use tokio::sync::mpsc;
+use tokio_util::sync::CancellationToken;
use crate::feeds::RawCameraFrame;
@@ -12,7 +13,7 @@ pub struct WSFrame {
pub stride: u32,
}
-pub async fn create_frame_ws(frame_rx: Receiver<WSFrame>) -> (u16, mpsc::Sender<()>) {
+pub async fn create_frame_ws(frame_rx: Receiver<WSFrame>) -> (u16, CancellationToken) {
use axum::{
extract::{
ws::{Message, WebSocket, WebSocketUpgrade},
@@ -81,17 +82,18 @@ pub async fn create_frame_ws(frame_rx: Receiver<WSFrame>) -> (u16, mpsc::Sender<
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
tracing::info!("WebSocket server listening on port {}", port);
- let (shutdown_tx, mut shutdown_rx) = mpsc::channel::<()>(1);
+ let cancel_token = CancellationToken::new();
+ let cancel_token_child = cancel_token.child_token();
tokio::spawn(async move {
let server = axum::serve(listener, router.into_make_service());
tokio::select! {
_ = server => {},
- _ = shutdown_rx.recv() => {
+ _ = cancel_token.cancelled() => {
println!("WebSocket server shutting down");
}
}
});
- (port, shutdown_tx)
+ (port, cancel_token_child)
}
diff --git a/crates/recording/Cargo.toml b/crates/recording/Cargo.toml
index 413fcab9a..f55ad2033 100644
--- a/crates/recording/Cargo.toml
+++ b/crates/recording/Cargo.toml
@@ -30,6 +30,7 @@ either = "1.13.0"
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
relative-path = "1.9.3"
futures = "0.3.31"
+tokio-util = "0.7.15"
[target.'cfg(target_os = "macos")'.dependencies]
screencapturekit = "0.3.5"
diff --git a/crates/recording/src/cursor.rs b/crates/recording/src/cursor.rs
index 958511fff..bd03d9369 100644
--- a/crates/recording/src/cursor.rs
+++ b/crates/recording/src/cursor.rs
@@ -14,6 +14,7 @@ use cap_utils::spawn_actor;
use device_query::{DeviceQuery, DeviceState};
use futures::future::Either;
use tokio::sync::oneshot;
+use tokio_util::sync::CancellationToken;
use tracing::{error, info};
pub struct Cursor {
@@ -33,13 +34,13 @@ pub struct CursorActorResponse {
}
pub struct CursorActor {
- shutdown_tx: oneshot::Sender<()>,
+ stop: CancellationToken,
rx: oneshot::Receiver<CursorActorResponse>,
}
impl CursorActor {
pub async fn stop(self) -> CursorActorResponse {
- let _ = self.shutdown_tx.send(());
+ self.stop.cancel();
self.rx.await.unwrap()
}
}
@@ -54,9 +55,10 @@ pub fn spawn_cursor_recorder(
next_cursor_id: u32,
start_time: SystemTime,
) -> CursorActor {
- let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();
+ let stop_token = CancellationToken::new();
let (tx, rx) = oneshot::channel();
+ let stop_token_child = stop_token.child_token();
spawn_actor(async move {
let device_state = DeviceState::new();
let mut last_mouse_state = device_state.get_mouse();
@@ -76,7 +78,8 @@ pub fn spawn_cursor_recorder(
loop {
let sleep = tokio::time::sleep(Duration::from_millis(10));
- let Either::Right(_) = futures::future::select(&mut shutdown_rx, pin!(sleep)).await
+ let Either::Right(_) =
+ futures::future::select(pin!(stop_token_child.cancelled()), pin!(sleep)).await
else {
break;
};
@@ -227,7 +230,10 @@ pub fn spawn_cursor_recorder(
let _ = tx.send(response);
});
- CursorActor { shutdown_tx, rx }
+ CursorActor {
+ stop: stop_token,
+ rx,
+ }
}
#[derive(Debug)]
diff --git a/crates/utils/Cargo.toml b/crates/utils/Cargo.toml
index 7ba70195e..891cb98a4 100644
--- a/crates/utils/Cargo.toml
+++ b/crates/utils/Cargo.toml
@@ -7,7 +7,6 @@ edition = "2021"
nix = { version = "0.29.0", features = ["fs"] }
[target.'cfg(windows)'.dependencies]
-tokio.workspace = true
windows = { version = "0.58.0", features = [
"Win32_Foundation",
"Win32_System",