rustduck_core/processor/
config.rs1use std::fmt;
4use std::sync::Arc;
5
6use crate::plugin::{PluginChain, PluginOptions};
7use crate::{Error, MemoryInput, MemoryOutput, Result};
8
9#[derive(Clone)]
11pub enum ProcessorInput {
12 Native(PluginOptions),
14 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#[derive(Clone)]
29pub enum ProcessorOutput {
30 Native(PluginOptions),
32 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#[derive(Debug, Clone)]
47pub struct ProcessorConfig {
48 pub ignore_joint_termination: bool,
50 pub buffer_size: i64,
52 pub max_flushed_packets: i64,
54 pub max_input_packets: i64,
56 pub max_output_packets: i64,
58 pub initial_input_packets: i64,
60 pub add_input_stuffing: [i64; 2],
62 pub add_start_stuffing: i64,
64 pub add_stop_stuffing: i64,
66 pub bitrate: i64,
68 pub bitrate_adjust_interval: i64,
70 pub receive_timeout: i64,
72 pub log_plugin_index: bool,
74 pub application_name: String,
76 pub input: Option<ProcessorInput>,
78 pub plugins: PluginChain,
80 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}