rustduck-sys 1.0.0

Raw Rust FFI bindings to the C ABI used by TSDuck's Python API
Documentation
//! Exact raw declarations for TSDuck's exported application ABI.
//!
//! TSDuck is implemented in C++. Its complete C++ class API does not have a
//! stable C ABI. The `tspy*` layer is the official C-compatible high-level ABI
//! used by TSDuck's Python module.
//!
//! # Safety
//!
//! These declarations are an unsafe ABI escape hatch. Callers are responsible
//! for native ownership, nullability, buffer lifetime, thread affinity,
//! callback containment, error interpretation, and matching every allocation
//! with its TSDuck delete operation. Prefer `rustduck` or `rustduck-core`'s safe API.

use std::mem::MaybeUninit;
use std::os::raw::{c_long, c_void};

include!(concat!(env!("OUT_DIR"), "/tsduck_version.rs"));

/// Callback invoked by synchronous or asynchronous Python-compatible reports.
pub type LogCallback = unsafe extern "C" fn(severity: i32, message: *const u8, size: usize);

/// Callback invoked by a Python-compatible plugin event handler.
pub type PluginCallback = unsafe extern "C" fn(
    event_code: u32,
    plugin_name: *const u8,
    plugin_name_size: usize,
    plugin_index: usize,
    plugin_count: usize,
    bitrate: usize,
    plugin_packets: usize,
    total_packets: usize,
    data: *const u8,
    data_size: usize,
    data_max_size: usize,
    data_read_only: bool,
    event_data: *mut c_void,
) -> bool;

/// Target-specific layout measurements for the repository-owned `tspy` mirror.
///
/// The native probe is compiled with the same TSDuck headers as the bridge. It
/// verifies this crate's raw declarations, not private TSDuck C++ layout.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TspyAbiLayout {
    /// Major version from the TSDuck headers seen by the native compiler.
    pub tsduck_header_version_major: u32,
    /// Minor version from the TSDuck headers seen by the native compiler.
    pub tsduck_header_version_minor: u32,
    /// `sizeof(tspyTSProcessorArgs)`.
    pub ts_processor_args_size: usize,
    /// `alignof(tspyTSProcessorArgs)`.
    pub ts_processor_args_align: usize,
    /// Field offsets in declaration order.
    pub ts_processor_args_offsets: [usize; 16],
    /// `sizeof(tspyInputSwitcherArgs)`.
    pub input_switcher_args_size: usize,
    /// `alignof(tspyInputSwitcherArgs)`.
    pub input_switcher_args_align: usize,
    /// Field offsets in declaration order.
    pub input_switcher_args_offsets: [usize; 23],
    /// `sizeof(uint32_t)`.
    pub uint32_size: usize,
    /// `alignof(uint32_t)`.
    pub uint32_align: usize,
    /// `sizeof(size_t)`.
    pub size_t_size: usize,
    /// `alignof(size_t)`.
    pub size_t_align: usize,
    /// `sizeof(bool)`.
    pub bool_size: usize,
    /// `alignof(bool)`.
    pub bool_align: usize,
    /// `sizeof(long)`.
    pub long_size: usize,
    /// `alignof(long)`.
    pub long_align: usize,
}

/// Returns the native layout of the repository-owned raw ABI mirror.
#[must_use]
pub fn tspy_abi_layout() -> TspyAbiLayout {
    let mut layout = MaybeUninit::<TspyAbiLayout>::uninit();

    // SAFETY: `layout` is non-null aligned writable storage for the complete
    // result. The native probe initializes every field and does not retain it.
    unsafe {
        rustduck_tspy_query_abi_layout(layout.as_mut_ptr());
        layout.assume_init()
    }
}

/// Arguments passed to `tspyStartTSProcessor`.
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct TSProcessorArgs {
    pub ignore_joint_termination: c_long,
    pub buffer_size: c_long,
    pub max_flushed_packets: c_long,
    pub max_input_packets: c_long,
    pub max_output_packets: c_long,
    pub initial_input_packets: c_long,
    pub add_input_stuffing_0: c_long,
    pub add_input_stuffing_1: c_long,
    pub add_start_stuffing: c_long,
    pub add_stop_stuffing: c_long,
    pub bitrate: c_long,
    pub bitrate_adjust_interval: c_long,
    pub receive_timeout: c_long,
    pub log_plugin_index: c_long,
    pub plugins: *mut u8,
    pub plugins_size: usize,
}

/// Arguments passed to `tspyStartInputSwitcher`.
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct InputSwitcherArgs {
    pub fast_switch: c_long,
    pub delayed_switch: c_long,
    pub terminate: c_long,
    pub reuse_port: c_long,
    pub first_input: c_long,
    pub primary_input: c_long,
    pub cycle_count: c_long,
    pub buffered_packets: c_long,
    pub max_input_packets: c_long,
    pub max_output_packets: c_long,
    pub sock_buffer: c_long,
    pub remote_server_port: c_long,
    pub receive_timeout: c_long,
    pub plugins: *mut u8,
    pub plugins_size: usize,
    pub event_command: *mut u8,
    pub event_command_size: usize,
    pub event_udp_addr: *mut u8,
    pub event_udp_addr_size: usize,
    pub event_udp_port: c_long,
    pub local_addr: *mut u8,
    pub local_addr_size: usize,
    pub event_ttl: c_long,
}

unsafe extern "C" {
    fn rustduck_tspy_query_abi_layout(layout: *mut TspyAbiLayout);

    pub fn tspyVersionInteger() -> u32;
    pub fn tspyVersionString(buffer: *mut u8, size: *mut usize);

    pub fn tspyNullReport() -> *mut c_void;
    pub fn tspyStdErrReport() -> *mut c_void;
    pub fn tspyNewAsyncReport(
        severity: i32,
        sync_log: bool,
        timed_log: bool,
        log_msg_count: usize,
    ) -> *mut c_void;
    pub fn tspyNewPySyncReport(callback: LogCallback, severity: i32) -> *mut c_void;
    pub fn tspyNewPyAsyncReport(
        callback: LogCallback,
        severity: i32,
        sync_log: bool,
        log_msg_count: usize,
    ) -> *mut c_void;
    pub fn tspyDeleteReport(report: *mut c_void);
    pub fn tspyTerminateAsyncReport(report: *mut c_void);
    pub fn tspySetMaxSeverity(report: *mut c_void, severity: i32);
    pub fn tspyLogReport(report: *mut c_void, severity: i32, message: *const u8, size: usize);
    pub fn tspyReportHeader(severity: i32, buffer: *mut u8, size: *mut usize);

    pub fn tspyNewDuckContext(report: *mut c_void) -> *mut c_void;
    pub fn tspyDeleteDuckContext(duck: *mut c_void);
    pub fn tspyDuckContextSetDefaultCharset(
        duck: *mut c_void,
        name: *const u8,
        size: usize,
    ) -> bool;
    pub fn tspyDuckContextSetDefaultCASId(duck: *mut c_void, cas: u16);
    pub fn tspyDuckContextSetDefaultPDS(duck: *mut c_void, pds: u32);
    pub fn tspyDuckContextAddStandards(duck: *mut c_void, mask: u32);
    pub fn tspyDuckContextStandards(duck: *mut c_void) -> u32;
    pub fn tspyDuckContextResetStandards(duck: *mut c_void, mask: u32);
    pub fn tspyDuckContextSetTimeReferenceOffset(duck: *mut c_void, offset: i64);
    pub fn tspyDuckContextSetTimeReference(duck: *mut c_void, name: *const u8, size: usize)
    -> bool;

    pub fn tspyNewPyPluginEventHandler(callback: PluginCallback) -> *mut c_void;
    pub fn tspyDeletePyPluginEventHandler(handler: *mut c_void);
    pub fn tspyPyPluginEventHandlerUpdateData(
        event_data: *mut c_void,
        data: *mut c_void,
        size: usize,
    );

    pub fn tspyNewSectionFile(duck: *mut c_void) -> *mut c_void;
    pub fn tspyDeleteSectionFile(section_file: *mut c_void);
    pub fn tspySectionFileClear(section_file: *mut c_void);
    pub fn tspySectionFileBinarySize(section_file: *mut c_void) -> usize;
    pub fn tspySectionFileSectionsCount(section_file: *mut c_void) -> usize;
    pub fn tspySectionFileTablesCount(section_file: *mut c_void) -> usize;
    pub fn tspySectionFileSetCRCValidation(section_file: *mut c_void, mode: i32);
    pub fn tspySectionLoadBuffer(section_file: *mut c_void, buffer: *const u8, size: usize)
    -> bool;
    pub fn tspySectionSaveBuffer(section_file: *mut c_void, buffer: *mut u8, size: *mut usize);
    pub fn tspySectionFileLoadBinary(
        section_file: *mut c_void,
        name: *const u8,
        size: usize,
    ) -> bool;
    pub fn tspySectionFileSaveBinary(
        section_file: *mut c_void,
        name: *const u8,
        size: usize,
    ) -> bool;
    pub fn tspySectionFileLoadXML(section_file: *mut c_void, name: *const u8, size: usize) -> bool;
    pub fn tspySectionFileSaveXML(section_file: *mut c_void, name: *const u8, size: usize) -> bool;
    pub fn tspySectionFileSaveJSON(section_file: *mut c_void, name: *const u8, size: usize)
    -> bool;
    pub fn tspySectionFileToXML(
        section_file: *mut c_void,
        buffer: *mut u8,
        size: *mut usize,
    ) -> usize;
    pub fn tspySectionFileToJSON(
        section_file: *mut c_void,
        buffer: *mut u8,
        size: *mut usize,
    ) -> usize;
    pub fn tspySectionFileReorganizeEITs(
        section_file: *mut c_void,
        year: i32,
        month: i32,
        day: i32,
    );

    pub fn tspyNewSystemMonitor(
        report: *mut c_void,
        config: *const u8,
        config_size: usize,
    ) -> *mut c_void;
    pub fn tspyDeleteSystemMonitor(monitor: *mut c_void);
    pub fn tspyStartSystemMonitor(monitor: *mut c_void);
    pub fn tspyStopSystemMonitor(monitor: *mut c_void);
    pub fn tspyWaitSystemMonitor(monitor: *mut c_void);

    pub fn tspyPluginEventHandlerRegister(
        registry: *mut c_void,
        handler: *mut c_void,
        event_code: u32,
    );
    pub fn tspyPluginEventHandlerRegisterInput(registry: *mut c_void, handler: *mut c_void);
    pub fn tspyPluginEventHandlerRegisterOutput(registry: *mut c_void, handler: *mut c_void);

    pub fn tspyNewTSProcessor(report: *mut c_void) -> *mut c_void;
    pub fn tspyDeleteTSProcessor(processor: *mut c_void);
    pub fn tspyStartTSProcessor(processor: *mut c_void, args: *const TSProcessorArgs) -> bool;
    pub fn tspyAbortTSProcessor(processor: *mut c_void);
    pub fn tspyWaitTSProcessor(processor: *mut c_void);

    pub fn tspyNewInputSwitcher(report: *mut c_void) -> *mut c_void;
    pub fn tspyDeleteInputSwitcher(switcher: *mut c_void);
    pub fn tspyStartInputSwitcher(switcher: *mut c_void, args: *const InputSwitcherArgs) -> bool;
    pub fn tspyInputSwitcherSetInput(switcher: *mut c_void, index: usize);
    pub fn tspyInputSwitcherNextInput(switcher: *mut c_void);
    pub fn tspyInputSwitcherPreviousInput(switcher: *mut c_void);
    pub fn tspyInputSwitcherCurrentInput(switcher: *mut c_void) -> usize;
    pub fn tspyStopInputSwitcher(switcher: *mut c_void);
    pub fn tspyWaitInputSwitcher(switcher: *mut c_void);

}