-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathrecord.rs
More file actions
1092 lines (1004 loc) · 38.2 KB
/
Copy pathrecord.rs
File metadata and controls
1092 lines (1004 loc) · 38.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use cap_project::{
InstantRecordingMeta, Platform, ProjectConfiguration, RecordingMeta, RecordingMetaInner,
};
use cap_recording::{
CameraFeed, MicrophoneFeed,
feeds::{camera, microphone},
instant_recording,
screen_capture::ScreenCaptureTarget,
studio_recording::{self, ActorHandle as StudioActorHandle},
};
use clap::{Args, ValueEnum};
use futures::FutureExt;
use kameo::Actor as _;
use scap_targets::{DisplayId, WindowId};
use serde::Serialize;
use std::{
env::current_dir,
io::IsTerminal,
path::{Path, PathBuf},
process::Stdio,
sync::Arc,
time::{Duration, Instant},
};
use tokio::io::AsyncBufReadExt;
use uuid::Uuid;
use crate::{
OutputFormat, resolve_format,
session::{self, Session, SessionStatus},
write_json, write_json_line,
};
/// Recording inputs shared between the foreground recorder, the `--detach` parent, and the
/// re-exec'd background worker. Kept in one struct so the worker can be invoked with exactly the
/// flags the parent received (see [`RecordParams::to_cli_args`]).
#[derive(Args, Clone)]
pub struct RecordParams {
#[command(flatten)]
target: RecordTargets,
/// Recording mode to use
#[arg(long, value_enum, default_value_t = RecordMode::Studio)]
mode: RecordMode,
/// Capture from the camera with this device id (see `cap targets cameras`)
#[arg(long)]
camera: Option<String>,
/// Capture from the microphone with this device name (see `cap targets mics`)
#[arg(long)]
mic: Option<String>,
/// Whether to capture system audio
#[arg(long)]
system_audio: bool,
/// Path to save the '.cap' project to (defaults to <recordingId>.cap in the working directory)
#[arg(long)]
path: Option<PathBuf>,
/// Maximum fps to record at (clamped to 1-120; camera recordings follow the desktop camera cap)
#[arg(long)]
fps: Option<u32>,
/// Stop automatically after N seconds
#[arg(long)]
duration: Option<f64>,
}
impl RecordParams {
fn validate(&self) -> Result<(), String> {
if self.duration.is_some_and(|duration| {
// `> u64::MAX` would panic in `Duration::from_secs_f64` (used by `wait_for_stop`).
!duration.is_finite() || duration <= 0.0 || duration > u64::MAX as f64
}) {
return Err("Duration must be a positive, finite number of seconds".to_string());
}
if self.fps == Some(0) {
return Err("--fps must be greater than 0".to_string());
}
Ok(())
}
fn to_cli_args(&self) -> Vec<String> {
let mut args = Vec::new();
if let Some(id) = &self.target.screen {
args.push("--screen".to_string());
args.push(id.to_string());
}
if let Some(id) = &self.target.window {
args.push("--window".to_string());
args.push(id.to_string());
}
args.push("--mode".to_string());
args.push(self.mode.to_string());
if let Some(camera) = &self.camera {
args.push("--camera".to_string());
args.push(camera.clone());
}
if let Some(mic) = &self.mic {
args.push("--mic".to_string());
args.push(mic.clone());
}
if self.system_audio {
args.push("--system-audio".to_string());
}
if let Some(path) = &self.path {
args.push("--path".to_string());
args.push(path.display().to_string());
}
if let Some(fps) = self.fps {
args.push("--fps".to_string());
args.push(fps.to_string());
}
if let Some(duration) = self.duration {
args.push("--duration".to_string());
args.push(duration.to_string());
}
args
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)]
pub enum RecordMode {
Studio,
Instant,
}
impl std::fmt::Display for RecordMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Studio => f.write_str("studio"),
Self::Instant => f.write_str("instant"),
}
}
}
#[derive(Args)]
pub struct RecordStart {
#[command(flatten)]
params: RecordParams,
/// Record in the background and return immediately; stop later with `cap record stop`
#[arg(long)]
detach: bool,
/// Output format for status events
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
}
/// Hidden worker entrypoint. `cap record start --detach` re-execs the binary as
/// `cap record __session-run` so the recording outlives the parent process.
#[derive(Args)]
pub struct SessionRunArgs {
#[command(flatten)]
params: RecordParams,
#[arg(long)]
session_id: String,
}
#[derive(Args)]
pub struct RecordStopArgs {
/// recordingId returned by `cap record start --detach`
#[arg(long)]
id: Option<String>,
/// The '.cap' project path of the recording to stop (alternative to --id)
#[arg(long)]
path: Option<PathBuf>,
/// Seconds to wait for the recording to finalize before giving up
#[arg(long, default_value_t = 30.0)]
timeout: f64,
/// Output format for status events
#[arg(long, value_enum, default_value_t = OutputFormat::Text)]
format: OutputFormat,
}
impl RecordStart {
pub async fn run(self, json: bool) -> Result<(), String> {
let format = resolve_format(json, self.format);
self.params.validate()?;
if self.detach {
run_detached(self.params, format).await
} else {
run_foreground(self.params, format).await
}
}
}
async fn run_foreground(params: RecordParams, format: OutputFormat) -> Result<(), String> {
match foreground_inner(params, format).await {
Ok(()) => Ok(()),
Err(error) => {
if format == OutputFormat::Json {
let _ = write_json_line(&RecordEvent::Error { error: &error });
}
Err(error)
}
}
}
async fn foreground_inner(params: RecordParams, format: OutputFormat) -> Result<(), String> {
let interactive = std::io::stdin().is_terminal();
// Without --duration the recorder blocks for a stop signal. A non-interactive caller (agent, CI,
// /dev/null stdin) hits EOF immediately and would otherwise produce a ~0s recording that exits
// successfully. Require an explicit duration, or point them at the detached lifecycle.
if params.duration.is_none() && !interactive {
return Err(
"Recording without --duration requires an interactive terminal; pass --duration <seconds>, \
or use `cap record start --detach` and stop it later with `cap record stop`"
.to_string(),
);
}
let recording_id = new_recording_id();
let target = resolve_target(¶ms)?;
let path = resolve_path(¶ms, &recording_id)?;
let actor = start_recording(¶ms, target, path.clone()).await?;
let path_display = path.display().to_string();
// The recording is now writing to disk, so every path from here must finalize the actor.
if let Err(error) = emit_record_event(
format,
&RecordEvent::Started {
recording_id: &recording_id,
pid: std::process::id(),
path: &path_display,
},
) {
let _ = actor.stop().await;
return Err(error);
}
if params.duration.is_none() && interactive && format == OutputFormat::Text {
println!("Press Enter to stop (or send SIGINT/SIGTERM)");
}
let completed = finalize(actor, params.duration, interactive, None).await?;
emit_stopped(format, &completed)
}
async fn run_detached(params: RecordParams, format: OutputFormat) -> Result<(), String> {
match detached_inner(params, format).await {
Ok(()) => Ok(()),
Err(error) => {
if format == OutputFormat::Json {
let _ = write_json_line(&RecordEvent::Error { error: &error });
}
Err(error)
}
}
}
async fn detached_inner(params: RecordParams, format: OutputFormat) -> Result<(), String> {
let recording_id = new_recording_id();
// Validate the target up front so a bad --screen/--window fails fast with a clear message rather
// than surfacing later from the background worker's log.
resolve_target(¶ms)?;
let path = resolve_path(¶ms, &recording_id)?;
let path = if path.is_absolute() {
path
} else {
current_dir()
.map_err(|e| format!("Could not determine current directory: {e}"))?
.join(path)
};
let exe =
std::env::current_exe().map_err(|e| format!("Could not locate the cap executable: {e}"))?;
let sessions_dir = session::sessions_dir()?;
std::fs::create_dir_all(&sessions_dir)
.map_err(|e| format!("Could not create sessions dir: {e}"))?;
let log_path = session::log_file(&recording_id)?;
let log = std::fs::File::create(&log_path)
.map_err(|e| format!("Could not create session log {}: {e}", log_path.display()))?;
let log_err = log
.try_clone()
.map_err(|e| format!("Could not prepare session log: {e}"))?;
let mut worker_params = params.clone();
worker_params.path = Some(path.clone());
let mut command = std::process::Command::new(&exe);
command
.arg("record")
.arg("__session-run")
.arg("--session-id")
.arg(&recording_id)
.args(worker_params.to_cli_args())
.stdin(Stdio::null())
.stdout(Stdio::from(log))
.stderr(Stdio::from(log_err));
// Detach from the parent so closing the parent's shell/pipeline does not signal the recording
// worker. On unix this means a new process group; on Windows, no console + a new process group so
// a Ctrl-C / console-close in the launching terminal does not reach the worker.
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
command.process_group(0);
}
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const DETACHED_PROCESS: u32 = 0x0000_0008;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
command.creation_flags(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP);
}
let child = command
.spawn()
.map_err(|e| format!("Could not start the recording worker: {e}"))?;
let pid = child.id();
let session = wait_for_session_ready(&recording_id, child).await?;
if let Err(error) = emit_record_event(
format,
&RecordEvent::Started {
recording_id: &recording_id,
pid,
path: &session.path.display().to_string(),
},
) {
// The worker is already recording in its own process group. If we cannot hand the caller the
// recordingId it could never stop it, so tear the recording down rather than leak an orphan.
let _ = session::request_stop(&recording_id);
if session::process_alive(pid) {
session::terminate(pid);
}
return Err(format!(
"{error}; background recording {recording_id} was stopped because its start event could not be delivered"
));
}
Ok(())
}
async fn wait_for_session_ready(
recording_id: &str,
mut child: std::process::Child,
) -> Result<Session, String> {
let deadline = Instant::now() + Duration::from_secs(20);
loop {
if let Ok(Some(status)) = child.try_wait() {
// The worker has provably exited. A clean `Stopped` is success (a short `--duration` run can
// finish before the parent's first poll); a leftover `Recording` means it died mid-recording
// without finalizing, so fall through to the error rather than report a healthy start.
if let Ok(session) = session::read_session(recording_id) {
match session.status {
SessionStatus::Stopped => return Ok(session),
SessionStatus::Error => {
return Err(session
.error
.unwrap_or_else(|| "recording worker failed to start".to_string()));
}
SessionStatus::Recording => {}
}
}
let log = session::log_file(recording_id)
.map(|p| p.display().to_string())
.unwrap_or_default();
return Err(format!(
"recording worker exited before finalizing the recording ({status}); see {log}"
));
}
if let Ok(session) = session::read_session(recording_id) {
match session.status {
SessionStatus::Recording | SessionStatus::Stopped => return Ok(session),
SessionStatus::Error => {
let _ = child.kill();
return Err(session
.error
.unwrap_or_else(|| "recording failed to start".to_string()));
}
}
}
if Instant::now() >= deadline {
let _ = child.kill();
return Err("timed out waiting for the recording to start".to_string());
}
tokio::time::sleep(Duration::from_millis(150)).await;
}
}
impl SessionRunArgs {
pub async fn run(self) -> Result<(), String> {
let recording_id = self.session_id;
match session_worker(self.params, &recording_id).await {
Ok(()) => Ok(()),
Err(error) => {
// Record the failure so the parent (`record start`) and `record stop` can surface it
// instead of an opaque exit. Reuse the startedAt/path the worker may already have
// written for the Recording session so the Error row keeps the real start time, which
// `record status` sorts by.
let prior = session::read_session(&recording_id).ok();
let _ = session::write_session(&Session {
pid: std::process::id(),
path: prior.as_ref().map(|s| s.path.clone()).unwrap_or_default(),
status: SessionStatus::Error,
started_at: prior
.as_ref()
.and_then(|s| s.started_at)
.or_else(session::now_unix),
recording_meta_exists: None,
error: Some(error.clone()),
recording_id,
});
Err(error)
}
}
}
}
async fn session_worker(params: RecordParams, recording_id: &str) -> Result<(), String> {
let target = resolve_target(¶ms)?;
let path = params
.path
.clone()
.ok_or_else(|| "internal: detached worker started without --path".to_string())?;
let actor = start_recording(¶ms, target, path.clone()).await?;
// Stamp the start time once; reusing it for the Stopped write keeps `startedAt` meaning the start
// (not the stop), which `list_sessions` relies on to sort recordings newest-first.
let started_at = session::now_unix();
session::write_session(&Session {
recording_id: recording_id.to_string(),
pid: std::process::id(),
path: path.clone(),
status: SessionStatus::Recording,
started_at,
recording_meta_exists: None,
error: None,
})?;
let stop_path = session::stop_file(recording_id)?;
let completed = finalize(actor, params.duration, false, Some(&stop_path)).await?;
let recording_meta_exists = completed
.project_path()
.join("recording-meta.json")
.exists();
session::write_session(&Session {
recording_id: recording_id.to_string(),
pid: std::process::id(),
path: completed.project_path().to_path_buf(),
status: SessionStatus::Stopped,
started_at,
recording_meta_exists: Some(recording_meta_exists),
error: None,
})
}
impl RecordStopArgs {
pub async fn run(self, json: bool) -> Result<(), String> {
let format = resolve_format(json, self.format);
match self.run_inner(format).await {
Ok(()) => Ok(()),
Err(error) => {
if format == OutputFormat::Json {
let _ = write_json_line(&RecordEvent::Error { error: &error });
}
Err(error)
}
}
}
async fn run_inner(self, format: OutputFormat) -> Result<(), String> {
let session = resolve_session(self.id.as_deref(), self.path.as_deref())?;
let id = session.recording_id.clone();
if session.status == SessionStatus::Stopped {
emit_record_event(
format,
&RecordEvent::Stopped {
path: &session.path.display().to_string(),
recording_meta_exists: session.recording_meta_exists.unwrap_or(true),
},
)?;
session::cleanup(&id);
return Ok(());
}
session::request_stop(&id)?;
// The stop file is the authoritative cross-platform stop signal; only also SIGTERM a live pid
// so a recycled pid belonging to an unrelated process is never signalled.
if session::process_alive(session.pid) {
session::terminate(session.pid);
}
let deadline = Instant::now() + Duration::from_secs_f64(self.timeout.max(0.0));
loop {
let current = session::read_session(&id).unwrap_or_else(|_| session.clone());
match current.status {
SessionStatus::Stopped => {
emit_record_event(
format,
&RecordEvent::Stopped {
path: ¤t.path.display().to_string(),
recording_meta_exists: current.recording_meta_exists.unwrap_or(true),
},
)?;
session::cleanup(&id);
return Ok(());
}
SessionStatus::Error => {
session::cleanup(&id);
return Err(current
.error
.unwrap_or_else(|| "recording failed".to_string()));
}
SessionStatus::Recording => {}
}
if !session::process_alive(current.pid) {
// The worker died without flipping its status. Trust the on-disk recording-meta.json
// as the source of truth for whether the .cap finalized.
let recording_meta_exists = current.path.join("recording-meta.json").exists();
session::cleanup(&id);
return if recording_meta_exists {
emit_record_event(
format,
&RecordEvent::Stopped {
path: ¤t.path.display().to_string(),
recording_meta_exists: true,
},
)
} else {
Err("recording process exited without finalizing the recording".to_string())
};
}
if Instant::now() >= deadline {
return Err(format!(
"timed out after {}s waiting for recording '{id}' to stop",
self.timeout
));
}
tokio::time::sleep(Duration::from_millis(150)).await;
}
}
}
fn resolve_session(id: Option<&str>, path: Option<&Path>) -> Result<Session, String> {
if let Some(id) = id {
return session::read_session(id);
}
let sessions = session::list_sessions()?;
if let Some(path) = path {
let wanted = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
return sessions
.into_iter()
.find(|s| {
s.path == path
|| std::fs::canonicalize(&s.path).is_ok_and(|resolved| resolved == wanted)
})
.ok_or_else(|| format!("No recording session found for path {}", path.display()));
}
let mut active: Vec<Session> = sessions
.into_iter()
.filter(|s| s.status == SessionStatus::Recording)
.collect();
match active.len() {
0 => Err(
"No active recording sessions. Start one with `cap record start --detach`".to_string(),
),
1 => Ok(active.remove(0)),
_ => {
let ids: Vec<&str> = active.iter().map(|s| s.recording_id.as_str()).collect();
Err(format!(
"Multiple active recordings; pass --id <recordingId>. Active: {ids:?}"
))
}
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SessionStatusRow {
recording_id: String,
pid: u32,
path: PathBuf,
status: SessionStatus,
alive: bool,
#[serde(skip_serializing_if = "Option::is_none")]
started_at: Option<u64>,
}
pub fn status(format: OutputFormat) -> Result<(), String> {
let rows: Vec<SessionStatusRow> = session::list_sessions()?
.into_iter()
.map(|s| SessionStatusRow {
alive: s.status == SessionStatus::Recording && session::process_alive(s.pid),
recording_id: s.recording_id,
pid: s.pid,
path: s.path,
status: s.status,
started_at: s.started_at,
})
.collect();
match format {
OutputFormat::Json => write_json(&rows),
OutputFormat::Text => {
if rows.is_empty() {
println!("No recording sessions");
return Ok(());
}
for row in &rows {
let status = match row.status {
SessionStatus::Recording if row.alive => "recording",
SessionStatus::Recording => "recording (process gone)",
SessionStatus::Stopped => "stopped",
SessionStatus::Error => "error",
};
println!(
"{} [{status}] pid {} {}",
row.recording_id,
row.pid,
row.path.display()
);
}
Ok(())
}
}
}
enum ActorHandle {
Studio(StudioActorHandle),
Instant(instant_recording::ActorHandle),
}
impl ActorHandle {
async fn stop(&self) -> Result<CompletedRecording, String> {
match self {
Self::Studio(actor) => actor
.stop()
.await
.map(Box::new)
.map(CompletedRecording::Studio)
.map_err(|e| e.to_string()),
Self::Instant(actor) => actor
.stop()
.await
.map(CompletedRecording::Instant)
.map_err(|e| e.to_string()),
}
}
}
enum CompletedRecording {
Studio(Box<studio_recording::CompletedRecording>),
Instant(instant_recording::CompletedRecording),
}
impl CompletedRecording {
fn project_path(&self) -> &Path {
match self {
Self::Studio(recording) => &recording.project_path,
Self::Instant(recording) => &recording.project_path,
}
}
}
async fn start_recording(
params: &RecordParams,
target: ScreenCaptureTarget,
path: PathBuf,
) -> Result<ActorHandle, String> {
let mut studio_builder = studio_recording::Actor::builder(path.clone(), target.clone())
.with_system_audio(params.system_audio);
let mut instant_builder =
instant_recording::Actor::builder(path, target).with_system_audio(params.system_audio);
let mut camera_active = false;
// Feeds must be locked and attached before build(); the lock keeps the device open for the whole
// recording, so the feed actor handle itself does not need to be retained.
if let Some(device_id) = params.camera.as_deref() {
let info = cap_camera::list_cameras()
.find(|c| c.device_id() == device_id)
.ok_or_else(|| {
let available: Vec<String> = cap_camera::list_cameras()
.map(|c| c.device_id().to_string())
.collect();
format!(
"Camera with id '{device_id}' not found. Available device ids: {available:?} \
(see `cap targets cameras`)"
)
})?;
let id = camera::DeviceOrModelID::from_info(&info);
let camera_feed = CameraFeed::spawn(CameraFeed::default());
camera_feed
.ask(camera::SetInput { id, settings: None })
.await
.map_err(|e| format!("Failed to set camera input: {e}"))?
.await
.map_err(|e| format!("Camera failed to connect: {e}"))?;
let lock = camera_feed
.ask(camera::Lock)
.await
.map_err(|e| format!("Failed to lock camera feed: {e}"))?;
let lock = Arc::new(lock);
studio_builder = studio_builder.with_camera_feed(lock.clone());
instant_builder = instant_builder.with_camera_feed(lock);
camera_active = true;
}
if let Some(mic_name) = params.mic.as_deref() {
let available = MicrophoneFeed::list();
if !available.contains_key(mic_name) {
let names: Vec<&str> = available.keys().map(String::as_str).collect();
return Err(format!(
"Microphone '{mic_name}' not found. Available: {names:?} (see `cap targets mics`)"
));
}
let (error_tx, _error_rx) = flume::bounded(16);
let mic_feed = MicrophoneFeed::spawn(MicrophoneFeed::new(error_tx));
mic_feed
.ask(microphone::SetInput {
label: mic_name.to_string(),
settings: None,
})
.await
.map_err(|e| format!("Failed to set mic input: {e}"))?
.await
.map_err(|e| format!("Mic failed to connect: {e}"))?;
// The stream needs a moment to warm up before locking on slower devices.
tokio::time::sleep(Duration::from_millis(100)).await;
let lock = mic_feed
.ask(microphone::Lock)
.await
.map_err(|e| format!("Failed to lock mic feed: {e}"))?;
let lock = Arc::new(lock);
studio_builder = studio_builder.with_mic_feed(lock.clone());
instant_builder = instant_builder.with_mic_feed(lock);
}
match params.mode {
RecordMode::Studio => {
let builder = cap_recording::RecordingDefaults::default().apply_to_studio_builder(
studio_builder,
camera_active,
params.fps,
);
builder
.build(
#[cfg(target_os = "macos")]
Some(cap_recording::SendableShareableContent::from(
cidre::sc::ShareableContent::current()
.await
.map_err(|e| format!("Failed to read shareable content: {e}"))?,
)),
)
.await
.map(ActorHandle::Studio)
.map_err(|e| e.to_string())
}
RecordMode::Instant => {
let mut builder = instant_builder;
builder = builder.with_max_output_size(
cap_recording::RecordingDefaults::default().instant_mode_max_resolution,
);
if let Some(fps) = params.fps {
builder = builder.with_max_fps(fps);
}
builder
.build(
#[cfg(target_os = "macos")]
Some(cap_recording::SendableShareableContent::from(
cidre::sc::ShareableContent::current()
.await
.map_err(|e| format!("Failed to read shareable content: {e}"))?,
)),
)
.await
.map(ActorHandle::Instant)
.map_err(|e| e.to_string())
}
}
}
/// Wait for the stop trigger, then finalize the recording. A panic between start and stop would
/// otherwise drop the actor without writing recording-meta.json (`ActorHandle` has no `Drop`),
/// leaving an unrecoverable .cap; catch it and best-effort finalize so the recording is recoverable.
///
/// Studio recordings are fragmented for crash recovery, so a graceful stop leaves the display track
/// as a directory of fragments with status `NeedsRemux`. We then run the shared
/// `RecoveryManager::remux_if_needed` — the same remux the desktop runs after stop — so the `.cap`
/// is immediately exportable instead of `cap export`/`cap upload` failing to open a fragment
/// directory as a video.
async fn finalize(
actor: ActorHandle,
duration: Option<f64>,
interactive: bool,
stop_file: Option<&Path>,
) -> Result<CompletedRecording, String> {
let outcome = std::panic::AssertUnwindSafe(async {
wait_for_stop(duration, interactive, stop_file).await;
actor.stop().await.map_err(|e| e.to_string())
})
.catch_unwind()
.await;
let completed = match outcome {
Ok(Ok(completed)) => completed,
Ok(Err(error)) => return Err(error),
Err(_) => actor
.stop()
.await
.map_err(|e| format!("recording panicked; finalize failed: {e}"))?,
};
finalize_completed(completed).await
}
async fn finalize_completed(completed: CompletedRecording) -> Result<CompletedRecording, String> {
match &completed {
CompletedRecording::Studio(recording) => {
let project_path = recording.project_path.clone();
tokio::task::spawn_blocking(move || {
cap_recording::recovery::RecoveryManager::remux_if_needed(&project_path)
})
.await
.map_err(|e| format!("recording finalize task failed: {e}"))?
.map_err(|e| format!("Failed to remux recording: {e}"))?;
}
CompletedRecording::Instant(recording) => {
finalize_instant_output(recording.project_path.clone()).await?;
persist_instant_recording_meta(recording)?;
}
}
Ok(completed)
}
async fn finalize_instant_output(project_path: PathBuf) -> Result<(), String> {
let output_path = project_path.join("content/output.mp4");
let audio_dir = project_path.join("content/audio");
if std::fs::metadata(&output_path)
.map(|metadata| metadata.len() > 0)
.unwrap_or(false)
&& !audio_dir.exists()
{
return Ok(());
}
let display_dir = project_path.join("content/display");
tokio::task::spawn_blocking(move || {
cap_recording::recovery::RecoveryManager::finalize_instant_output(
&display_dir,
&audio_dir,
&output_path,
)
})
.await
.map_err(|e| format!("instant recording finalize task failed: {e}"))?
.map_err(|e| format!("Failed to finalize instant recording: {e}"))?;
Ok(())
}
fn persist_instant_recording_meta(
recording: &instant_recording::CompletedRecording,
) -> Result<(), String> {
let pretty_name = recording
.project_path
.file_stem()
.and_then(|name| name.to_str())
.filter(|name| !name.is_empty())
.unwrap_or("Cap Recording")
.to_string();
let meta = match &recording.meta {
InstantRecordingMeta::Complete { .. } => recording.meta.clone(),
InstantRecordingMeta::InProgress { .. } => InstantRecordingMeta::Failed {
error: "instant recording stopped before completion".to_string(),
},
InstantRecordingMeta::Failed { .. } => recording.meta.clone(),
};
RecordingMeta {
platform: Some(Platform::default()),
project_path: recording.project_path.clone(),
pretty_name,
sharing: None,
inner: RecordingMetaInner::Instant(meta),
upload: None,
audio_only: false,
}
.save_for_project()
.map_err(|e| format!("Failed to save instant recording meta: {e}"))?;
ProjectConfiguration::default()
.write(&recording.project_path)
.map_err(|e| format!("Failed to save instant project config: {e}"))
}
fn emit_stopped(format: OutputFormat, completed: &CompletedRecording) -> Result<(), String> {
let recording_meta_exists = completed
.project_path()
.join("recording-meta.json")
.exists();
emit_record_event(
format,
&RecordEvent::Stopped {
path: &completed.project_path().display().to_string(),
recording_meta_exists,
},
)
}
fn new_recording_id() -> String {
Uuid::new_v4().simple().to_string()[..12].to_string()
}
fn resolve_path(params: &RecordParams, recording_id: &str) -> Result<PathBuf, String> {
match ¶ms.path {
Some(path) => Ok(path.clone()),
None => current_dir()
.map_err(|e| format!("Could not determine current directory: {e}"))
.map(|dir| dir.join(format!("{recording_id}.cap"))),
}
}
fn resolve_target(params: &RecordParams) -> Result<ScreenCaptureTarget, String> {
match (¶ms.target.screen, ¶ms.target.window) {
(Some(id), _) => cap_recording::screen_capture::list_displays()
.into_iter()
.find(|s| &s.0.id == id)
.map(|(s, _)| ScreenCaptureTarget::Display { id: s.id })
.ok_or_else(|| {
let available: Vec<String> = cap_recording::screen_capture::list_displays()
.into_iter()
.map(|(s, _)| s.id.to_string())
.collect();
format!(
"Screen with id '{id}' not found. Available screen ids: {available:?} \
(see `cap targets screens`)"
)
}),
(_, Some(id)) => cap_recording::screen_capture::list_windows()
.into_iter()
.find(|s| &s.0.id == id)
.map(|(s, _)| ScreenCaptureTarget::Window { id: s.id })
.ok_or_else(|| {
format!(
"Window with id '{id}' not found. Run `cap targets windows` to list window ids"
)
}),
_ => Err(
"No target specified; pass --screen <id> or --window <id> (see `cap targets`)"
.to_string(),
),
}
}
/// Block until the recording should stop: the duration elapses, the user presses Enter (interactive
/// only), the process receives SIGINT/SIGTERM, or a detached worker's stop file appears. Every branch
/// resolves so the caller can finalize the recording gracefully instead of being killed mid-write.
async fn wait_for_stop(duration: Option<f64>, interactive: bool, stop_file: Option<&Path>) {
#[cfg(unix)]
let mut term = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).ok();
tokio::select! {
_ = async {
match duration {
Some(d) => tokio::time::sleep(Duration::from_secs_f64(d)).await,
None => std::future::pending::<()>().await,
}
} => {}
_ = async {
if duration.is_none() && interactive {
let _ = tokio::io::BufReader::new(tokio::io::stdin())
.read_line(&mut String::new())
.await;
} else {
std::future::pending::<()>().await;
}
} => {}
_ = tokio::signal::ctrl_c() => {}
_ = async {
#[cfg(unix)]
if let Some(term) = term.as_mut() {
term.recv().await;
return;
}
std::future::pending::<()>().await;
} => {}
_ = async {
match stop_file {
Some(path) => loop {
if session::stop_requested(path) {
return;
}
tokio::time::sleep(Duration::from_millis(150)).await;
},