Skip to main content

screencapturekit/stream/configuration/
internal.rs

1use std::ffi::c_void;
2use std::fmt;
3
4/// Opaque wrapper around `SCStreamConfiguration`
5///
6/// Configuration for a screen capture stream, including dimensions,
7/// pixel format, audio settings, and other capture parameters.
8///
9/// # Examples
10///
11/// ```
12/// use screencapturekit::stream::configuration::SCStreamConfiguration;
13///
14/// let config = SCStreamConfiguration::new()
15///     .with_width(1920)
16///     .with_height(1080);
17/// ```
18#[repr(transparent)]
19pub struct SCStreamConfiguration(pub(crate) *const c_void);
20
21impl PartialEq for SCStreamConfiguration {
22    fn eq(&self, other: &Self) -> bool {
23        self.0 == other.0
24    }
25}
26
27impl Eq for SCStreamConfiguration {}
28
29impl std::hash::Hash for SCStreamConfiguration {
30    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
31        self.0.hash(state);
32    }
33}
34
35impl SCStreamConfiguration {
36    pub(crate) fn internal_init() -> Self {
37        unsafe {
38            let ptr = crate::ffi::sc_stream_configuration_create();
39            Self(ptr)
40        }
41    }
42
43    pub(crate) fn as_ptr(&self) -> *const c_void {
44        self.0
45    }
46}
47
48impl Drop for SCStreamConfiguration {
49    fn drop(&mut self) {
50        if !self.0.is_null() {
51            unsafe {
52                crate::ffi::sc_stream_configuration_release(self.0);
53            }
54        }
55    }
56}
57
58impl Clone for SCStreamConfiguration {
59    /// Clone the configuration by bumping the underlying Objective-C
60    /// reference count.
61    ///
62    /// **Note**: this is **not** a `memcpy`. `Clone::clone` crosses the
63    /// Swift FFI boundary and calls `sc_stream_configuration_retain`
64    /// (which performs an Objective-C `retain`). For most callers the
65    /// cost is irrelevant, but if you're cloning an
66    /// `SCStreamConfiguration` per frame on the hot path, prefer to
67    /// share an `Arc<SCStreamConfiguration>` (or `&SCStreamConfiguration`)
68    /// and clone *that* instead.
69    fn clone(&self) -> Self {
70        unsafe { Self(crate::ffi::sc_stream_configuration_retain(self.0)) }
71    }
72}
73
74unsafe impl Send for SCStreamConfiguration {}
75unsafe impl Sync for SCStreamConfiguration {}
76
77impl fmt::Debug for SCStreamConfiguration {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        f.debug_struct("SCStreamConfiguration")
80            .field("ptr", &self.0)
81            .finish()
82    }
83}
84
85impl fmt::Display for SCStreamConfiguration {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        write!(f, "SCStreamConfiguration")
88    }
89}