Skip to main content

rustduck_core/
context.rs

1use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
2use std::rc::Rc;
3
4use rustduck_sys::bridge::ffi;
5
6use crate::buffer::encode_utf16;
7use crate::error::native_exception;
8use crate::{Error, Report, Result};
9
10/// Standards mask used by a [`DuckContext`].
11#[repr(transparent)]
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
13pub struct Standards(pub u32);
14
15impl Standards {
16    /// No standards selected.
17    pub const NONE: Self = Self(0x00);
18    /// MPEG standards.
19    pub const MPEG: Self = Self(0x01);
20    /// DVB standards.
21    pub const DVB: Self = Self(0x02);
22    /// SCTE standards.
23    pub const SCTE: Self = Self(0x04);
24    /// ATSC standards.
25    pub const ATSC: Self = Self(0x08);
26    /// ISDB standards.
27    pub const ISDB: Self = Self(0x10);
28    /// Japanese standards.
29    pub const JAPAN: Self = Self(0x20);
30    /// ABNT standards.
31    pub const ABNT: Self = Self(0x40);
32
33    /// Returns the raw TSDuck standards bitmask.
34    pub const fn bits(self) -> u32 {
35        self.0
36    }
37}
38
39impl BitOr for Standards {
40    type Output = Self;
41    fn bitor(self, rhs: Self) -> Self::Output {
42        Self(self.0 | rhs.0)
43    }
44}
45impl BitOrAssign for Standards {
46    fn bitor_assign(&mut self, rhs: Self) {
47        self.0 |= rhs.0;
48    }
49}
50impl BitAnd for Standards {
51    type Output = Self;
52    fn bitand(self, rhs: Self) -> Self::Output {
53        Self(self.0 & rhs.0)
54    }
55}
56impl BitAndAssign for Standards {
57    fn bitand_assign(&mut self, rhs: Self) {
58        self.0 &= rhs.0;
59    }
60}
61impl Not for Standards {
62    type Output = Self;
63    fn not(self) -> Self::Output {
64        Self(!self.0)
65    }
66}
67
68/// A TSDuck execution context borrowing a [`Report`].
69///
70/// `DuckContext` is `!Send + !Sync`. The native owner is declared before its
71/// report dependency, so it is destroyed first.
72pub struct DuckContext {
73    native: cxx::UniquePtr<ffi::RustDuckDuckContext>,
74    report: Rc<Report>,
75}
76
77impl DuckContext {
78    /// Creates a context retaining `report` until native context destruction.
79    pub fn new(report: Rc<Report>) -> Result<Rc<Self>> {
80        let native = ffi::rustduck_context_new(report.native())
81            .map_err(|error| native_exception("DuckContext::new", error))?;
82        if native.is_null() {
83            return Err(Error::NativeFailure {
84                operation: "DuckContext::new",
85            });
86        }
87        Ok(Rc::new(Self { native, report }))
88    }
89
90    /// Returns the retained report.
91    pub fn report(&self) -> &Rc<Report> {
92        &self.report
93    }
94
95    /// Sets the default character set.
96    pub fn set_default_charset(&mut self, charset: &str) -> Result<()> {
97        let charset = encode_utf16(charset);
98        let accepted =
99            ffi::rustduck_context_set_default_charset(self.native.pin_mut(), &charset)
100                .map_err(|error| native_exception("DuckContext::set_default_charset", error))?;
101        accepted.then_some(()).ok_or(Error::NativeFailure {
102            operation: "DuckContext::set_default_charset",
103        })
104    }
105
106    /// Sets the default conditional-access system identifier.
107    pub fn set_default_cas_id(&mut self, cas_id: u16) -> Result<()> {
108        ffi::rustduck_context_set_default_cas_id(self.native.pin_mut(), cas_id)
109            .map_err(|error| native_exception("DuckContext::set_default_cas_id", error))
110    }
111
112    /// Sets the default private data specifier.
113    pub fn set_default_pds(&mut self, pds: u32) -> Result<()> {
114        ffi::rustduck_context_set_default_pds(self.native.pin_mut(), pds)
115            .map_err(|error| native_exception("DuckContext::set_default_pds", error))
116    }
117
118    /// Adds standards to the active mask.
119    pub fn add_standards(&mut self, standards: Standards) -> Result<()> {
120        ffi::rustduck_context_add_standards(self.native.pin_mut(), standards.bits())
121            .map_err(|error| native_exception("DuckContext::add_standards", error))
122    }
123
124    /// Returns the active standards mask.
125    pub fn standards(&self) -> Result<Standards> {
126        ffi::rustduck_context_standards(
127            self.native
128                .as_ref()
129                .expect("DuckContext construction rejects null owners"),
130        )
131        .map(Standards)
132        .map_err(|error| native_exception("DuckContext::standards", error))
133    }
134
135    /// Replaces the active standards mask.
136    pub fn reset_standards(&mut self, standards: Standards) -> Result<()> {
137        ffi::rustduck_context_reset_standards(self.native.pin_mut(), standards.bits())
138            .map_err(|error| native_exception("DuckContext::reset_standards", error))
139    }
140
141    /// Sets the time-reference offset in milliseconds.
142    pub fn set_time_reference_offset(&mut self, offset_millis: i64) -> Result<()> {
143        ffi::rustduck_context_set_time_reference_offset(self.native.pin_mut(), offset_millis)
144            .map_err(|error| native_exception("DuckContext::set_time_reference_offset", error))
145    }
146
147    /// Selects a named time reference.
148    pub fn set_time_reference(&mut self, name: &str) -> Result<()> {
149        let name = encode_utf16(name);
150        let accepted = ffi::rustduck_context_set_time_reference(self.native.pin_mut(), &name)
151            .map_err(|error| native_exception("DuckContext::set_time_reference", error))?;
152        accepted.then_some(()).ok_or(Error::NativeFailure {
153            operation: "DuckContext::set_time_reference",
154        })
155    }
156
157    pub(crate) fn native(&self) -> &ffi::RustDuckDuckContext {
158        self.native
159            .as_ref()
160            .expect("DuckContext construction rejects null owners")
161    }
162}