Skip to main content

libobs_wrapper\sources/
traits.rs

1use crate::{
2    data::object::ObsObjectTrait,
3    macros::impl_eq_of_obs_object,
4    sources::{_ObsRemoveFilterOnDrop, ObsFilterRef, ObsSourceSignals},
5    utils::ObsError,
6};
7use std::sync::Arc;
8
9#[derive(Debug, Clone)]
10pub struct ObsFilterGuardPair {
11    filter: ObsFilterRef,
12    pub(crate) _guard: Arc<_ObsRemoveFilterOnDrop>,
13}
14
15impl ObsFilterGuardPair {
16    pub(crate) fn new(filter: ObsFilterRef, guard: Arc<_ObsRemoveFilterOnDrop>) -> Self {
17        Self {
18            filter,
19            _guard: guard,
20        }
21    }
22
23    pub fn get_inner(&self) -> &ObsFilterRef {
24        &self.filter
25    }
26
27    pub fn get_inner_mut(&mut self) -> &mut ObsFilterRef {
28        &mut self.filter
29    }
30}
31
32#[allow(private_bounds)]
33pub trait ObsSourceTrait: ObsObjectTrait<Native = *mut libobs::obs_source_t> {
34    fn signals(&self) -> &Arc<ObsSourceSignals>;
35
36    fn get_active_filters(&self) -> Result<Vec<ObsFilterGuardPair>, ObsError>;
37    fn apply_filter(&self, filter: &ObsFilterRef) -> Result<(), ObsError>;
38
39    /// Returns libobs's persistent UUID for this source.
40    ///
41    /// Unlike [`ObsObjectTrait::object_id`], this identifier is intended for persisted
42    /// application state. `object_id()` is runtime-scoped and must never be written to disk.
43    fn persistent_uuid(&self) -> Result<String, ObsError> {
44        let source = self.__native_handle();
45        let runtime = self.runtime().clone();
46        crate::run_with_obs!(runtime, (source), move || unsafe {
47            // Safety: the managed source lease remains alive for the actor call and libobs owns
48            // the returned NUL-terminated UUID string.
49            let uuid = libobs::obs_source_get_uuid(source.get_ptr());
50            if uuid.is_null() {
51                return Err(ObsError::NullPointer(Some(
52                    "obs_source_get_uuid returned null".into(),
53                )));
54            }
55            Ok(std::ffi::CStr::from_ptr(uuid)
56                .to_string_lossy()
57                .into_owned())
58        })?
59    }
60}
61
62impl_eq_of_obs_object!(dyn ObsSourceTrait);