rustduck-core 1.1.0

Safe typed TSDuck core bindings for rustduck
Documentation
//! Input-switcher configuration, endpoint selection, and validation.

use std::fmt;
use std::sync::Arc;

use crate::plugin::PluginOptions;
use crate::{Error, MemoryInput, MemoryOutput, Result};

/// One typed switcher input.
#[derive(Clone)]
pub enum SwitcherInput {
    /// An ordinary TSDuck input plugin.
    Native(PluginOptions),
    /// A bounded typed memory input.
    Memory(Arc<MemoryInput>),
}

impl fmt::Debug for SwitcherInput {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Native(value) => formatter.debug_tuple("Native").field(value).finish(),
            Self::Memory(_) => formatter.debug_tuple("Memory").finish(),
        }
    }
}

/// Typed switcher output.
#[derive(Clone)]
pub enum SwitcherOutput {
    /// An ordinary TSDuck output plugin.
    Native(PluginOptions),
    /// A bounded typed memory output.
    Memory(Arc<MemoryOutput>),
}

impl fmt::Debug for SwitcherOutput {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Native(value) => formatter.debug_tuple("Native").field(value).finish(),
            Self::Memory(_) => formatter.debug_tuple("Memory").finish(),
        }
    }
}

/// Typed configuration for one `InputSwitcher` start.
#[derive(Debug, Clone)]
pub struct InputSwitcherConfig {
    /// Select fast switching.
    pub fast_switch: bool,
    /// Delay switching until the next safe packet boundary.
    pub delayed_switch: bool,
    /// Terminate when all inputs terminate.
    pub terminate: bool,
    /// Reuse the remote-control port.
    pub reuse_port: bool,
    /// Initial input index.
    pub first_input: i64,
    /// Primary input index, or `-1` when unset.
    pub primary_input: i64,
    /// Number of input cycles.
    pub cycle_count: i64,
    /// Buffered packets.
    pub buffered_packets: i64,
    /// Maximum packets per input operation.
    pub max_input_packets: i64,
    /// Maximum packets per output operation.
    pub max_output_packets: i64,
    /// Socket buffer size in bytes.
    pub sock_buffer: i64,
    /// Remote-control server port.
    pub remote_server_port: i64,
    /// Receive timeout in milliseconds.
    pub receive_timeout: i64,
    /// External switch-event command.
    pub event_command: String,
    /// Remote event UDP address.
    pub event_udp_address: String,
    /// Remote event UDP port.
    pub event_udp_port: i64,
    /// Local event UDP address.
    pub event_local_address: String,
    /// Event UDP TTL.
    pub event_ttl: i64,
    /// Application name passed to TSDuck's plugin parser.
    pub application_name: String,
    /// Typed input plugins.
    pub inputs: Vec<SwitcherInput>,
    /// Optional typed output plugin.
    pub output: Option<SwitcherOutput>,
}

impl Default for InputSwitcherConfig {
    fn default() -> Self {
        Self {
            fast_switch: false,
            delayed_switch: false,
            terminate: false,
            reuse_port: false,
            first_input: 0,
            primary_input: -1,
            cycle_count: 1,
            buffered_packets: 0,
            max_input_packets: 0,
            max_output_packets: 0,
            sock_buffer: 0,
            remote_server_port: 0,
            receive_timeout: 0,
            event_command: String::new(),
            event_udp_address: String::new(),
            event_udp_port: 0,
            event_local_address: String::new(),
            event_ttl: 0,
            application_name: String::new(),
            inputs: Vec::new(),
            output: None,
        }
    }
}

pub(crate) fn validate(config: &InputSwitcherConfig) -> Result<()> {
    if config.inputs.is_empty() {
        return Err(Error::InvalidArgument {
            field: "inputs",
            message: "must not be empty".to_owned(),
        });
    }
    for value in [
        config.first_input,
        config.primary_input,
        config.cycle_count,
        config.buffered_packets,
        config.max_input_packets,
        config.max_output_packets,
        config.sock_buffer,
        config.remote_server_port,
        config.receive_timeout,
        config.event_udp_port,
        config.event_ttl,
    ] {
        if !fits_native_long(value) {
            return Err(Error::InvalidArgument {
                field: "switcher option",
                message: "does not fit the native long type".to_owned(),
            });
        }
    }
    for input in &config.inputs {
        if let SwitcherInput::Native(plugin) = input {
            plugin.validate_not_memory("inputs")?;
        }
    }
    if let Some(SwitcherOutput::Native(plugin)) = &config.output {
        plugin.validate_not_memory("output")?;
    }
    Ok(())
}

fn fits_native_long(value: i64) -> bool {
    size_of::<std::ffi::c_long>() != size_of::<i32>()
        || (i64::from(i32::MIN)..=i64::from(i32::MAX)).contains(&value)
}