Skip to main content

libobs_wrapper\encoders/
traits.rs

1use std::ffi::CStr;
2
3use crate::{data::object::ObsObjectTrait, run_with_obs, utils::ObsError};
4
5pub trait ObsEncoderTrait: ObsObjectTrait<Native = *mut libobs::obs_encoder> {
6    /// Returns the codec identifier reported by the live encoder instance.
7    fn codec(&self) -> Result<Option<String>, ObsError> {
8        let encoder_ptr = self.__native_handle();
9        run_with_obs!(self.runtime(), (encoder_ptr), move || {
10            // Safety: the managed encoder handle remains alive for the actor call.
11            let codec = unsafe { libobs::obs_encoder_get_codec(encoder_ptr.get_ptr()) };
12            if codec.is_null() {
13                None
14            } else {
15                // Safety: libobs returns a borrowed NUL-terminated string for the encoder lifetime.
16                Some(
17                    unsafe { CStr::from_ptr(codec) }
18                        .to_string_lossy()
19                        .into_owned(),
20                )
21            }
22        })
23    }
24
25    fn is_active(&self) -> Result<bool, ObsError> {
26        let encoder_ptr = self.__native_handle();
27
28        run_with_obs!(self.runtime(), (encoder_ptr), move || {
29            // Safety: The pointer is valid because we are using a smart pointer
30            unsafe { libobs::obs_encoder_active(encoder_ptr.get_ptr()) }
31        })
32    }
33}