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
48// `Clone::clone` is not a `memcpy`: it crosses the Swift FFI boundary and calls
49// `sc_stream_configuration_retain` (an Objective-C `retain`). For most callers
50// the cost is irrelevant, but if you're cloning an `SCStreamConfiguration` per
51// frame on the hot path, prefer sharing an `Arc<SCStreamConfiguration>` (or
52// `&SCStreamConfiguration`) and cloning *that* instead.
53crate::utils::retained::sc_retained!(
54    SCStreamConfiguration,
55    retain = crate::ffi::sc_stream_configuration_retain,
56    release = crate::ffi::sc_stream_configuration_release,
57);
58
59unsafe impl Send for SCStreamConfiguration {}
60unsafe impl Sync for SCStreamConfiguration {}
61
62impl fmt::Debug for SCStreamConfiguration {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        f.debug_struct("SCStreamConfiguration")
65            .field("ptr", &self.0)
66            .finish()
67    }
68}
69
70impl fmt::Display for SCStreamConfiguration {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        write!(f, "SCStreamConfiguration")
73    }
74}