Skip to main content

libobs_simple\output/
configure.rs

1use libobs_wrapper::{
2    data::ObsData,
3    settings::{PropertyValue, SettingsSchema},
4    utils::ObsError,
5};
6
7/// Whether generic simple-output settings may fall back to plugin defaults.
8#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
9pub enum ConfigurationMode {
10    /// Keep plugin defaults for absent/rejected generic settings and describe that in the report.
11    #[default]
12    Permissive,
13    /// Fail when a requested generic setting cannot be applied exactly.
14    Strict,
15}
16
17/// What happened to one requested setting.
18#[derive(Clone, Debug, PartialEq, Eq)]
19pub enum SettingDisposition {
20    Applied,
21    Unsupported,
22    Rejected { reason: String },
23}
24
25/// Effective result of one generic setting request.
26#[derive(Clone, Debug, PartialEq, Eq)]
27pub struct SettingConfiguration {
28    pub component: String,
29    pub name: String,
30    pub requested: String,
31    pub disposition: SettingDisposition,
32}
33
34/// Structured report produced while building a simple output.
35#[derive(Clone, Debug, Default, PartialEq, Eq)]
36pub struct OutputConfigurationReport {
37    pub output_plugin_id: String,
38    pub video_encoder_plugin_id: String,
39    pub audio_encoder_plugin_ids: Vec<String>,
40    pub settings: Vec<SettingConfiguration>,
41    pub fallback_reasons: Vec<String>,
42    pub attempts: Vec<EncoderAttempt>,
43}
44
45/// One concrete video-encoder attempt made by a managed simple workflow.
46#[derive(Clone, Debug, PartialEq, Eq)]
47pub struct EncoderAttempt {
48    pub plugin_id: String,
49    pub stage: EncoderAttemptStage,
50    pub outcome: EncoderAttemptOutcome,
51}
52
53#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54pub enum EncoderAttemptStage {
55    Creation,
56    Start,
57}
58
59#[derive(Clone, Debug, PartialEq, Eq)]
60pub enum EncoderAttemptOutcome {
61    Succeeded,
62    Failed {
63        error: String,
64        fallback_eligible: bool,
65    },
66}
67
68impl OutputConfigurationReport {
69    pub(crate) fn setting(
70        &mut self,
71        component: impl Into<String>,
72        name: &str,
73        requested: &PropertyValue,
74        disposition: SettingDisposition,
75    ) {
76        self.settings.push(SettingConfiguration {
77            component: component.into(),
78            name: name.to_owned(),
79            requested: format!("{requested:?}"),
80            disposition,
81        });
82    }
83}
84
85/// Applies a common cross-encoder setting while recording the effective result.
86pub(crate) fn set_reported(
87    schema: &SettingsSchema,
88    settings: &mut ObsData,
89    component: &str,
90    name: &str,
91    value: PropertyValue,
92    mode: ConfigurationMode,
93    report: &mut OutputConfigurationReport,
94) -> Result<(), ObsError> {
95    if schema.property(name).is_none() {
96        report.setting(component, name, &value, SettingDisposition::Unsupported);
97        return match mode {
98            ConfigurationMode::Permissive => Ok(()),
99            ConfigurationMode::Strict => Err(ObsError::PropertyNotFound {
100                name: name.to_owned(),
101            }),
102        };
103    }
104
105    match schema.set(settings, name, value.clone()) {
106        Ok(()) => {
107            report.setting(component, name, &value, SettingDisposition::Applied);
108            Ok(())
109        }
110        Err(
111            error @ (ObsError::PropertyValueNotAllowed { .. }
112            | ObsError::PropertyValueTypeMismatch { .. }),
113        ) => {
114            report.setting(
115                component,
116                name,
117                &value,
118                SettingDisposition::Rejected {
119                    reason: error.to_string(),
120                },
121            );
122            match mode {
123                ConfigurationMode::Permissive => {
124                    log::debug!(
125                        "Keeping OBS default because generic setting '{name}' is not accepted by the selected plugin"
126                    );
127                    Ok(())
128                }
129                ConfigurationMode::Strict => Err(error),
130            }
131        }
132        Err(error) => Err(error),
133    }
134}
135
136/// Compatibility helper for existing callers that intentionally use permissive plugin defaults.
137pub(super) fn set_if_supported(
138    schema: &SettingsSchema,
139    settings: &mut ObsData,
140    name: &str,
141    value: PropertyValue,
142) -> Result<(), ObsError> {
143    let mut ignored_report = OutputConfigurationReport::default();
144    set_reported(
145        schema,
146        settings,
147        "unspecified",
148        name,
149        value,
150        ConfigurationMode::Permissive,
151        &mut ignored_report,
152    )
153}
154
155#[cfg(test)]
156mod tests {
157    use super::*;
158
159    #[test]
160    fn report_records_selected_plugins_and_attempts_without_log_parsing() {
161        let mut report = OutputConfigurationReport {
162            output_plugin_id: "ffmpeg_muxer".into(),
163            video_encoder_plugin_id: "obs_x264".into(),
164            audio_encoder_plugin_ids: vec!["ffmpeg_aac".into()],
165            ..Default::default()
166        };
167        report
168            .fallback_reasons
169            .push("hardware encoder unavailable".into());
170        report.attempts.push(EncoderAttempt {
171            plugin_id: "obs_nvenc_h264_tex".into(),
172            stage: EncoderAttemptStage::Creation,
173            outcome: EncoderAttemptOutcome::Failed {
174                error: "encoder unavailable".into(),
175                fallback_eligible: true,
176            },
177        });
178
179        assert_eq!(report.output_plugin_id, "ffmpeg_muxer");
180        assert_eq!(report.fallback_reasons.len(), 1);
181        assert_eq!(report.attempts.len(), 1);
182    }
183}