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
//! Output handler trait for stream callbacks
//!
//! Defines the interface for receiving captured frames and audio buffers.
use crateCMSampleBuffer;
use SCStreamOutputType;
/// Trait for handling stream output
///
/// Implement this trait to receive callbacks when the stream captures frames or audio.
///
/// # Examples
///
/// ## Using a struct
///
/// ```
/// use screencapturekit::stream::{
/// output_trait::SCStreamOutputTrait,
/// output_type::SCStreamOutputType,
/// };
/// use screencapturekit::cm::CMSampleBuffer;
///
/// struct MyHandler;
///
/// impl SCStreamOutputTrait for MyHandler {
/// fn did_output_sample_buffer(&self, sample: CMSampleBuffer, of_type: SCStreamOutputType) {
/// match of_type {
/// SCStreamOutputType::Screen => {
/// println!("Received video frame");
/// }
/// SCStreamOutputType::Audio => {
/// println!("Received audio buffer");
/// }
/// SCStreamOutputType::Microphone => {
/// println!("Received microphone audio");
/// }
/// }
/// }
/// }
/// ```
///
/// ## Using a closure
///
/// Closures that match `Fn(CMSampleBuffer, SCStreamOutputType)` automatically
/// implement this trait:
///
/// ```rust,no_run
/// use screencapturekit::prelude::*;
///
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let content = SCShareableContent::get()?;
/// # let display = &content.displays()[0];
/// # let filter = SCContentFilter::create().with_display(display).with_excluding_windows(&[]).build();
/// # let config = SCStreamConfiguration::default();
/// let mut stream = SCStream::new(&filter, &config);
///
/// stream.add_output_handler(
/// |_sample, _output_type| println!("Got frame!"),
/// SCStreamOutputType::Screen
/// );
/// # Ok(())
/// # }
/// ```
/// Trait implemented by handlers that receive captured samples from an
/// [`SCStream`](crate::stream::SCStream).
///
/// # Concurrency
///
/// Handlers must be `Send + Sync`. `Send` is required because a handler
/// registered on one thread is invoked on a `ScreenCaptureKit` dispatch
/// queue, which runs on different OS threads over time. `Sync` is required
/// because [`SCStream`] internally uses an `RwLock` to allow callbacks for
/// independent output types (Screen / Audio / Microphone) to dispatch
/// concurrently — without `Sync`, two threads could not concurrently
/// invoke `&self` methods through the shared lock guard, even though
/// `ScreenCaptureKit` serialises callbacks per output type in practice.
///
/// In practice, handlers that share state via `Arc<Mutex<…>>`, `Arc<RwLock<…>>`,
/// or atomic primitives satisfy `Sync` automatically. Handlers that capture
/// `Cell` / `RefCell` / `Rc` directly will not — wrap shared state in `Mutex`
/// or `Arc<Mutex<…>>` instead.
/// Blanket implementation for closures
///
/// Any closure matching `Fn(CMSampleBuffer, SCStreamOutputType) + Send + Sync + 'static`
/// can be used directly as an output handler. See the trait docs for
/// the rationale behind the `Sync` bound.