rustduck_core/input_switcher/
config.rs1use std::fmt;
4use std::sync::Arc;
5
6use crate::plugin::PluginOptions;
7use crate::{Error, MemoryInput, MemoryOutput, Result};
8
9#[derive(Clone)]
11pub enum SwitcherInput {
12 Native(PluginOptions),
14 Memory(Arc<MemoryInput>),
16}
17
18impl fmt::Debug for SwitcherInput {
19 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
20 match self {
21 Self::Native(value) => formatter.debug_tuple("Native").field(value).finish(),
22 Self::Memory(_) => formatter.debug_tuple("Memory").finish(),
23 }
24 }
25}
26
27#[derive(Clone)]
29pub enum SwitcherOutput {
30 Native(PluginOptions),
32 Memory(Arc<MemoryOutput>),
34}
35
36impl fmt::Debug for SwitcherOutput {
37 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
38 match self {
39 Self::Native(value) => formatter.debug_tuple("Native").field(value).finish(),
40 Self::Memory(_) => formatter.debug_tuple("Memory").finish(),
41 }
42 }
43}
44
45#[derive(Debug, Clone)]
47pub struct InputSwitcherConfig {
48 pub fast_switch: bool,
50 pub delayed_switch: bool,
52 pub terminate: bool,
54 pub reuse_port: bool,
56 pub first_input: i64,
58 pub primary_input: i64,
60 pub cycle_count: i64,
62 pub buffered_packets: i64,
64 pub max_input_packets: i64,
66 pub max_output_packets: i64,
68 pub sock_buffer: i64,
70 pub remote_server_port: i64,
72 pub receive_timeout: i64,
74 pub event_command: String,
76 pub event_udp_address: String,
78 pub event_udp_port: i64,
80 pub event_local_address: String,
82 pub event_ttl: i64,
84 pub application_name: String,
86 pub inputs: Vec<SwitcherInput>,
88 pub output: Option<SwitcherOutput>,
90}
91
92impl Default for InputSwitcherConfig {
93 fn default() -> Self {
94 Self {
95 fast_switch: false,
96 delayed_switch: false,
97 terminate: false,
98 reuse_port: false,
99 first_input: 0,
100 primary_input: -1,
101 cycle_count: 1,
102 buffered_packets: 0,
103 max_input_packets: 0,
104 max_output_packets: 0,
105 sock_buffer: 0,
106 remote_server_port: 0,
107 receive_timeout: 0,
108 event_command: String::new(),
109 event_udp_address: String::new(),
110 event_udp_port: 0,
111 event_local_address: String::new(),
112 event_ttl: 0,
113 application_name: String::new(),
114 inputs: Vec::new(),
115 output: None,
116 }
117 }
118}
119
120pub(crate) fn validate(config: &InputSwitcherConfig) -> Result<()> {
121 if config.inputs.is_empty() {
122 return Err(Error::InvalidArgument {
123 field: "inputs",
124 message: "must not be empty".to_owned(),
125 });
126 }
127 for value in [
128 config.first_input,
129 config.primary_input,
130 config.cycle_count,
131 config.buffered_packets,
132 config.max_input_packets,
133 config.max_output_packets,
134 config.sock_buffer,
135 config.remote_server_port,
136 config.receive_timeout,
137 config.event_udp_port,
138 config.event_ttl,
139 ] {
140 if !fits_native_long(value) {
141 return Err(Error::InvalidArgument {
142 field: "switcher option",
143 message: "does not fit the native long type".to_owned(),
144 });
145 }
146 }
147 for input in &config.inputs {
148 if let SwitcherInput::Native(plugin) = input {
149 plugin.validate_not_memory("inputs")?;
150 }
151 }
152 if let Some(SwitcherOutput::Native(plugin)) = &config.output {
153 plugin.validate_not_memory("output")?;
154 }
155 Ok(())
156}
157
158fn fits_native_long(value: i64) -> bool {
159 size_of::<std::ffi::c_long>() != size_of::<i32>()
160 || (i64::from(i32::MIN)..=i64::from(i32::MAX)).contains(&value)
161}