screencapturekit 1.5.4

Safe Rust bindings for Apple's ScreenCaptureKit framework - screen and audio capture on macOS
Documentation
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
//! Audio capture configuration
//!
//! Methods for configuring audio capture, sample rate, and channel count.
//!
//! ## Supported Values
//!
//! `ScreenCaptureKit` supports specific sample rates and channel counts:
//!
//! | Sample Rate | Description |
//! |-------------|-------------|
//! | 8000 Hz | Low quality, telephony |
//! | 16000 Hz | Speech quality |
//! | 24000 Hz | Medium quality |
//! | 48000 Hz | Professional audio (default) |
//!
//! | Channel Count | Description |
//! |---------------|-------------|
//! | 1 | Mono |
//! | 2 | Stereo (default) |

use crate::utils::ffi_string::{ffi_string_from_buffer, SMALL_BUFFER_SIZE};

use super::internal::SCStreamConfiguration;

/// Audio sample rate for capture
///
/// `ScreenCaptureKit` supports a fixed set of sample rates. Using values outside
/// this set will result in the system defaulting to 48000 Hz.
///
/// # Examples
///
/// ```
/// use screencapturekit::stream::configuration::audio::AudioSampleRate;
///
/// // Get the Hz value
/// assert_eq!(AudioSampleRate::Rate48000.as_hz(), 48000);
///
/// // Use default (48000 Hz)
/// let rate = AudioSampleRate::default();
/// assert_eq!(rate, AudioSampleRate::Rate48000);
/// ```
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum AudioSampleRate {
    /// 8000 Hz - Low quality, suitable for telephony
    Rate8000 = 8000,
    /// 16000 Hz - Speech quality
    Rate16000 = 16000,
    /// 24000 Hz - Medium quality
    Rate24000 = 24000,
    /// 48000 Hz - Professional audio quality (default)
    #[default]
    Rate48000 = 48000,
}

impl AudioSampleRate {
    /// Get the sample rate in Hz
    #[must_use]
    pub const fn as_hz(self) -> i32 {
        self as i32
    }

    /// Create from Hz value, returning None if unsupported
    ///
    /// # Examples
    ///
    /// ```
    /// use screencapturekit::stream::configuration::audio::AudioSampleRate;
    ///
    /// assert_eq!(AudioSampleRate::from_hz(48000), Some(AudioSampleRate::Rate48000));
    /// assert_eq!(AudioSampleRate::from_hz(44100), None); // Not supported
    /// ```
    #[must_use]
    pub const fn from_hz(hz: i32) -> Option<Self> {
        match hz {
            8000 => Some(Self::Rate8000),
            16000 => Some(Self::Rate16000),
            24000 => Some(Self::Rate24000),
            48000 => Some(Self::Rate48000),
            _ => None,
        }
    }
}

impl std::fmt::Display for AudioSampleRate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} Hz", self.as_hz())
    }
}

impl From<AudioSampleRate> for i32 {
    fn from(rate: AudioSampleRate) -> Self {
        rate.as_hz()
    }
}

/// Audio channel configuration for capture
///
/// `ScreenCaptureKit` supports mono (1 channel) or stereo (2 channels) audio.
/// Using other values will result in the system defaulting to stereo.
///
/// # Examples
///
/// ```
/// use screencapturekit::stream::configuration::audio::AudioChannelCount;
///
/// // Get the channel count
/// assert_eq!(AudioChannelCount::Stereo.as_count(), 2);
///
/// // Use default (stereo)
/// let channels = AudioChannelCount::default();
/// assert_eq!(channels, AudioChannelCount::Stereo);
/// ```
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum AudioChannelCount {
    /// Mono - single channel audio
    Mono = 1,
    /// Stereo - two channel audio (default)
    #[default]
    Stereo = 2,
}

impl AudioChannelCount {
    /// Get the channel count as an integer
    #[must_use]
    pub const fn as_count(self) -> i32 {
        self as i32
    }

    /// Create from channel count, returning None if unsupported
    ///
    /// # Examples
    ///
    /// ```
    /// use screencapturekit::stream::configuration::audio::AudioChannelCount;
    ///
    /// assert_eq!(AudioChannelCount::from_count(2), Some(AudioChannelCount::Stereo));
    /// assert_eq!(AudioChannelCount::from_count(6), None); // Not supported
    /// ```
    #[must_use]
    pub const fn from_count(count: i32) -> Option<Self> {
        match count {
            1 => Some(Self::Mono),
            2 => Some(Self::Stereo),
            _ => None,
        }
    }
}

impl std::fmt::Display for AudioChannelCount {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Mono => write!(f, "Mono (1 channel)"),
            Self::Stereo => write!(f, "Stereo (2 channels)"),
        }
    }
}

impl From<AudioChannelCount> for i32 {
    fn from(count: AudioChannelCount) -> Self {
        count.as_count()
    }
}

impl SCStreamConfiguration {
    /// Enable or disable audio capture
    ///
    /// # Examples
    ///
    /// ```
    /// use screencapturekit::prelude::*;
    ///
    /// let mut config = SCStreamConfiguration::default();
    /// config.set_captures_audio(true);
    /// assert!(config.captures_audio());
    /// ```
    pub fn set_captures_audio(&mut self, captures_audio: bool) -> &mut Self {
        unsafe {
            crate::ffi::sc_stream_configuration_set_captures_audio(self.as_ptr(), captures_audio);
        }
        self
    }

    /// Enable or disable audio capture (builder pattern)
    #[must_use]
    pub fn with_captures_audio(mut self, captures_audio: bool) -> Self {
        self.set_captures_audio(captures_audio);
        self
    }

    /// Check if audio capture is enabled
    pub fn captures_audio(&self) -> bool {
        unsafe { crate::ffi::sc_stream_configuration_get_captures_audio(self.as_ptr()) }
    }

    /// Set the audio sample rate
    ///
    /// Accepts either an [`AudioSampleRate`] enum or a raw `i32` Hz value.
    ///
    /// # Supported Values
    ///
    /// `ScreenCaptureKit` supports: 8000, 16000, 24000, 48000 Hz.
    /// Other values will default to 48000 Hz.
    ///
    /// # Examples
    ///
    /// ```
    /// use screencapturekit::prelude::*;
    /// use screencapturekit::stream::configuration::audio::AudioSampleRate;
    ///
    /// // Using the enum (recommended)
    /// let config = SCStreamConfiguration::new()
    ///     .with_sample_rate(AudioSampleRate::Rate48000);
    ///
    /// // Using raw value (still works)
    /// let config = SCStreamConfiguration::new()
    ///     .with_sample_rate(48000);
    /// ```
    pub fn set_sample_rate(&mut self, sample_rate: impl Into<i32>) -> &mut Self {
        unsafe {
            crate::ffi::sc_stream_configuration_set_sample_rate(
                self.as_ptr(),
                sample_rate.into() as isize,
            );
        }
        self
    }

    /// Set the audio sample rate (builder pattern)
    #[must_use]
    pub fn with_sample_rate(mut self, sample_rate: impl Into<i32>) -> Self {
        self.set_sample_rate(sample_rate);
        self
    }

    /// Get the configured audio sample rate in Hz
    pub fn sample_rate(&self) -> i32 {
        // FFI returns isize but sample rate fits in i32 (typical values: 44100, 48000)
        #[allow(clippy::cast_possible_truncation)]
        unsafe {
            crate::ffi::sc_stream_configuration_get_sample_rate(self.as_ptr()) as i32
        }
    }

    /// Get the configured audio sample rate as an enum
    ///
    /// Returns `None` if the current sample rate is not a supported value.
    pub fn audio_sample_rate(&self) -> Option<AudioSampleRate> {
        AudioSampleRate::from_hz(self.sample_rate())
    }

    /// Set the number of audio channels
    ///
    /// Accepts either an [`AudioChannelCount`] enum or a raw `i32` value.
    ///
    /// # Supported Values
    ///
    /// `ScreenCaptureKit` supports: 1 (mono), 2 (stereo).
    /// Other values will default to stereo.
    ///
    /// # Examples
    ///
    /// ```
    /// use screencapturekit::prelude::*;
    /// use screencapturekit::stream::configuration::audio::AudioChannelCount;
    ///
    /// // Using the enum (recommended)
    /// let config = SCStreamConfiguration::new()
    ///     .with_channel_count(AudioChannelCount::Stereo);
    ///
    /// // Using raw value (still works)
    /// let config = SCStreamConfiguration::new()
    ///     .with_channel_count(2);
    /// ```
    pub fn set_channel_count(&mut self, channel_count: impl Into<i32>) -> &mut Self {
        unsafe {
            crate::ffi::sc_stream_configuration_set_channel_count(
                self.as_ptr(),
                channel_count.into() as isize,
            );
        }
        self
    }

    /// Set the number of audio channels (builder pattern)
    #[must_use]
    pub fn with_channel_count(mut self, channel_count: impl Into<i32>) -> Self {
        self.set_channel_count(channel_count);
        self
    }

    /// Get the configured channel count
    pub fn channel_count(&self) -> i32 {
        // FFI returns isize but channel count fits in i32 (typical values: 1-8)
        #[allow(clippy::cast_possible_truncation)]
        unsafe {
            crate::ffi::sc_stream_configuration_get_channel_count(self.as_ptr()) as i32
        }
    }

    /// Get the configured channel count as an enum
    ///
    /// Returns `None` if the current channel count is not a supported value.
    pub fn audio_channel_count(&self) -> Option<AudioChannelCount> {
        AudioChannelCount::from_count(self.channel_count())
    }

    /// Enable microphone capture (macOS 15.0+)
    ///
    /// When set to `true`, the stream will capture audio from the microphone
    /// in addition to system/application audio (if `captures_audio` is also enabled).
    ///
    /// **Note**: Requires `NSMicrophoneUsageDescription` in your app's Info.plist
    /// for microphone access permission.
    ///
    /// # Availability
    /// macOS 15.0+. On earlier versions, this setting has no effect.
    ///
    /// # Example
    /// ```rust,no_run
    /// use screencapturekit::prelude::*;
    ///
    /// let config = SCStreamConfiguration::new()
    ///     .with_captures_audio(true)       // System audio
    ///     .with_captures_microphone(true)  // Microphone audio (macOS 15.0+)
    ///     .with_sample_rate(48000)
    ///     .with_channel_count(2);
    /// ```
    pub fn set_captures_microphone(&mut self, captures_microphone: bool) -> &mut Self {
        unsafe {
            crate::ffi::sc_stream_configuration_set_captures_microphone(
                self.as_ptr(),
                captures_microphone,
            );
        }
        self
    }

    /// Enable microphone capture (builder pattern)
    #[must_use]
    pub fn with_captures_microphone(mut self, captures_microphone: bool) -> Self {
        self.set_captures_microphone(captures_microphone);
        self
    }

    /// Get whether microphone capture is enabled (macOS 15.0+).
    pub fn captures_microphone(&self) -> bool {
        unsafe { crate::ffi::sc_stream_configuration_get_captures_microphone(self.as_ptr()) }
    }

    /// Exclude current process audio from capture.
    ///
    /// When set to `true`, the stream will not capture audio from the current
    /// process, preventing feedback loops in recording applications.
    ///
    /// # Example
    /// ```rust,no_run
    /// use screencapturekit::prelude::*;
    ///
    /// let config = SCStreamConfiguration::new()
    ///     .with_captures_audio(true)
    ///     .with_excludes_current_process_audio(true); // Prevent feedback
    /// ```
    pub fn set_excludes_current_process_audio(&mut self, excludes: bool) -> &mut Self {
        unsafe {
            crate::ffi::sc_stream_configuration_set_excludes_current_process_audio(
                self.as_ptr(),
                excludes,
            );
        }
        self
    }

    /// Exclude current process audio (builder pattern)
    #[must_use]
    pub fn with_excludes_current_process_audio(mut self, excludes: bool) -> Self {
        self.set_excludes_current_process_audio(excludes);
        self
    }

    /// Get whether current process audio is excluded from capture.
    pub fn excludes_current_process_audio(&self) -> bool {
        unsafe {
            crate::ffi::sc_stream_configuration_get_excludes_current_process_audio(self.as_ptr())
        }
    }

    /// Set microphone capture device ID (macOS 15.0+).
    ///
    /// Specifies which microphone device to capture from.
    ///
    /// # Availability
    /// macOS 15.0+. On earlier versions, this setting has no effect.
    ///
    /// # Example
    /// ```rust,no_run
    /// use screencapturekit::prelude::*;
    ///
    /// let mut config = SCStreamConfiguration::new()
    ///     .with_captures_microphone(true);
    /// config.set_microphone_capture_device_id("AppleHDAEngineInput:1B,0,1,0:1");
    /// ```
    pub fn set_microphone_capture_device_id(&mut self, device_id: &str) -> &mut Self {
        unsafe {
            if let Ok(c_id) = std::ffi::CString::new(device_id) {
                crate::ffi::sc_stream_configuration_set_microphone_capture_device_id(
                    self.as_ptr(),
                    c_id.as_ptr(),
                );
            }
        }
        self
    }

    /// Set microphone capture device ID (builder pattern)
    #[must_use]
    pub fn with_microphone_capture_device_id(mut self, device_id: &str) -> Self {
        self.set_microphone_capture_device_id(device_id);
        self
    }

    /// Clear microphone capture device ID, reverting to default system microphone
    pub fn clear_microphone_capture_device_id(&mut self) -> &mut Self {
        unsafe {
            crate::ffi::sc_stream_configuration_set_microphone_capture_device_id(
                self.as_ptr(),
                std::ptr::null(),
            );
        }
        self
    }

    /// Get microphone capture device ID (macOS 15.0+).
    pub fn microphone_capture_device_id(&self) -> Option<String> {
        unsafe {
            ffi_string_from_buffer(SMALL_BUFFER_SIZE, |buf, len| {
                crate::ffi::sc_stream_configuration_get_microphone_capture_device_id(
                    self.as_ptr(),
                    buf,
                    len,
                )
            })
        }
    }
}