Skip to main content

rustduck_core/input_switcher/
config.rs

1//! Input-switcher configuration, endpoint selection, and validation.
2
3use std::fmt;
4use std::sync::Arc;
5
6use crate::plugin::PluginOptions;
7use crate::{Error, MemoryInput, MemoryOutput, Result};
8
9/// One typed switcher input.
10#[derive(Clone)]
11pub enum SwitcherInput {
12    /// An ordinary TSDuck input plugin.
13    Native(PluginOptions),
14    /// A bounded typed memory input.
15    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/// Typed switcher output.
28#[derive(Clone)]
29pub enum SwitcherOutput {
30    /// An ordinary TSDuck output plugin.
31    Native(PluginOptions),
32    /// A bounded typed memory output.
33    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/// Typed configuration for one `InputSwitcher` start.
46#[derive(Debug, Clone)]
47pub struct InputSwitcherConfig {
48    /// Select fast switching.
49    pub fast_switch: bool,
50    /// Delay switching until the next safe packet boundary.
51    pub delayed_switch: bool,
52    /// Terminate when all inputs terminate.
53    pub terminate: bool,
54    /// Reuse the remote-control port.
55    pub reuse_port: bool,
56    /// Initial input index.
57    pub first_input: i64,
58    /// Primary input index, or `-1` when unset.
59    pub primary_input: i64,
60    /// Number of input cycles.
61    pub cycle_count: i64,
62    /// Buffered packets.
63    pub buffered_packets: i64,
64    /// Maximum packets per input operation.
65    pub max_input_packets: i64,
66    /// Maximum packets per output operation.
67    pub max_output_packets: i64,
68    /// Socket buffer size in bytes.
69    pub sock_buffer: i64,
70    /// Remote-control server port.
71    pub remote_server_port: i64,
72    /// Receive timeout in milliseconds.
73    pub receive_timeout: i64,
74    /// External switch-event command.
75    pub event_command: String,
76    /// Remote event UDP address.
77    pub event_udp_address: String,
78    /// Remote event UDP port.
79    pub event_udp_port: i64,
80    /// Local event UDP address.
81    pub event_local_address: String,
82    /// Event UDP TTL.
83    pub event_ttl: i64,
84    /// Application name passed to TSDuck's plugin parser.
85    pub application_name: String,
86    /// Typed input plugins.
87    pub inputs: Vec<SwitcherInput>,
88    /// Optional typed output plugin.
89    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}