Skip to main content

rustduck_core/processor/
config.rs

1//! Processor configuration, endpoint selection, and pre-native validation.
2
3use std::fmt;
4use std::sync::Arc;
5
6use crate::plugin::{PluginChain, PluginOptions};
7use crate::{Error, MemoryInput, MemoryOutput, Result};
8
9/// Input selected for a processor.
10#[derive(Clone)]
11pub enum ProcessorInput {
12    /// An ordinary TSDuck input plugin.
13    Native(PluginOptions),
14    /// A typed bounded `memory` input endpoint.
15    Memory(Arc<MemoryInput>),
16}
17
18impl fmt::Debug for ProcessorInput {
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/// Output selected for a processor.
28#[derive(Clone)]
29pub enum ProcessorOutput {
30    /// An ordinary TSDuck output plugin.
31    Native(PluginOptions),
32    /// A typed bounded `memory` output endpoint.
33    Memory(Arc<MemoryOutput>),
34}
35
36impl fmt::Debug for ProcessorOutput {
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 `TSProcessor` start.
46#[derive(Debug, Clone)]
47pub struct ProcessorConfig {
48    /// Ignore joint-termination plugin options.
49    pub ignore_joint_termination: bool,
50    /// Global packet-buffer size in bytes.
51    pub buffer_size: i64,
52    /// Maximum processed packets before flushing; zero selects TSDuck's default.
53    pub max_flushed_packets: i64,
54    /// Maximum packets per input operation; zero selects TSDuck's default.
55    pub max_input_packets: i64,
56    /// Maximum packets per output operation; zero is unlimited.
57    pub max_output_packets: i64,
58    /// Packets read before processing begins; zero selects TSDuck's default.
59    pub initial_input_packets: i64,
60    /// Input stuffing pair: null packets and interval.
61    pub add_input_stuffing: [i64; 2],
62    /// Null packets before the input stream.
63    pub add_start_stuffing: i64,
64    /// Null packets after the input stream.
65    pub add_stop_stuffing: i64,
66    /// Fixed input bitrate; zero leaves it unspecified.
67    pub bitrate: i64,
68    /// Bitrate adjustment interval in milliseconds.
69    pub bitrate_adjust_interval: i64,
70    /// Input receive timeout in milliseconds; zero selects TSDuck's default.
71    pub receive_timeout: i64,
72    /// Include plugin indices in log messages.
73    pub log_plugin_index: bool,
74    /// Application name passed to TSDuck's plugin parser.
75    pub application_name: String,
76    /// Optional typed input.
77    pub input: Option<ProcessorInput>,
78    /// Ordered processor plugins.
79    pub plugins: PluginChain,
80    /// Optional typed output.
81    pub output: Option<ProcessorOutput>,
82}
83
84impl Default for ProcessorConfig {
85    fn default() -> Self {
86        Self {
87            ignore_joint_termination: false,
88            buffer_size: 16 * 1024 * 1024,
89            max_flushed_packets: 0,
90            max_input_packets: 0,
91            max_output_packets: 0,
92            initial_input_packets: 0,
93            add_input_stuffing: [0, 0],
94            add_start_stuffing: 0,
95            add_stop_stuffing: 0,
96            bitrate: 0,
97            bitrate_adjust_interval: 5_000,
98            receive_timeout: 0,
99            log_plugin_index: false,
100            application_name: String::new(),
101            input: None,
102            plugins: PluginChain::default(),
103            output: None,
104        }
105    }
106}
107
108pub(crate) fn validate(config: &ProcessorConfig) -> Result<()> {
109    for value in [
110        config.buffer_size,
111        config.max_flushed_packets,
112        config.max_input_packets,
113        config.max_output_packets,
114        config.initial_input_packets,
115        config.add_input_stuffing[0],
116        config.add_input_stuffing[1],
117        config.add_start_stuffing,
118        config.add_stop_stuffing,
119        config.bitrate,
120        config.bitrate_adjust_interval,
121        config.receive_timeout,
122    ] {
123        if !fits_native_long(value) {
124            return Err(Error::InvalidArgument {
125                field: "processor option",
126                message: "does not fit the native long type".to_owned(),
127            });
128        }
129    }
130    if let Some(ProcessorInput::Native(plugin)) = &config.input {
131        plugin.validate_not_memory("input")?;
132    }
133    if let Some(ProcessorOutput::Native(plugin)) = &config.output {
134        plugin.validate_not_memory("output")?;
135    }
136    for plugin in config.plugins.iter() {
137        plugin.validate_not_memory("plugins")?;
138    }
139    Ok(())
140}
141
142pub(crate) fn memory_input(config: &ProcessorConfig) -> Option<Arc<MemoryInput>> {
143    match &config.input {
144        Some(ProcessorInput::Memory(endpoint)) => Some(Arc::clone(endpoint)),
145        _ => None,
146    }
147}
148
149pub(crate) fn memory_output(config: &ProcessorConfig) -> Option<Arc<MemoryOutput>> {
150    match &config.output {
151        Some(ProcessorOutput::Memory(endpoint)) => Some(Arc::clone(endpoint)),
152        _ => None,
153    }
154}
155
156fn fits_native_long(value: i64) -> bool {
157    size_of::<std::ffi::c_long>() != size_of::<i32>()
158        || (i64::from(i32::MIN)..=i64::from(i32::MAX)).contains(&value)
159}