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    /// All defined standards.
33    pub const ALL: Self = Self(0x7f);
34
35    /// Returns the raw TSDuck standards bitmask.
36    pub const fn bits(self) -> u32 {
37        self.0
38    }
39
40    /// Returns all defined standards.
41    pub const fn all() -> Self {
42        Self::ALL
43    }
44}
45
46impl BitOr for Standards {
47    type Output = Self;
48    fn bitor(self, rhs: Self) -> Self::Output {
49        Self(self.0 | rhs.0)
50    }
51}
52impl BitOrAssign for Standards {
53    fn bitor_assign(&mut self, rhs: Self) {
54        self.0 |= rhs.0;
55    }
56}
57impl BitAnd for Standards {
58    type Output = Self;
59    fn bitand(self, rhs: Self) -> Self::Output {
60        Self(self.0 & rhs.0)
61    }
62}
63impl BitAndAssign for Standards {
64    fn bitand_assign(&mut self, rhs: Self) {
65        self.0 &= rhs.0;
66    }
67}
68impl Not for Standards {
69    type Output = Self;
70    fn not(self) -> Self::Output {
71        Self(!self.0 & Self::ALL.0)
72    }
73}
74
75/// A TSDuck execution context borrowing a [`Report`].
76///
77/// `DuckContext` is `!Send + !Sync`. The native owner is declared before its
78/// report dependency, so it is destroyed first.
79pub struct DuckContext {
80    native: cxx::UniquePtr<ffi::RustDuckDuckContext>,
81    report: Rc<Report>,
82}
83
84impl DuckContext {
85    /// Creates a context retaining `report` until native context destruction.
86    pub fn new(report: Rc<Report>) -> Result<Rc<Self>> {
87        let native = ffi::rustduck_context_new(report.native())
88            .map_err(|error| native_exception("DuckContext::new", error))?;
89        if native.is_null() {
90            return Err(Error::NativeFailure {
91                operation: "DuckContext::new",
92            });
93        }
94        Ok(Rc::new(Self { native, report }))
95    }
96
97    /// Returns the retained report.
98    pub fn report(&self) -> &Rc<Report> {
99        &self.report
100    }
101
102    /// Sets the default character set.
103    pub fn set_default_charset(&mut self, charset: &str) -> Result<()> {
104        let charset = encode_utf16(charset);
105        let accepted =
106            ffi::rustduck_context_set_default_charset(self.native.pin_mut(), &charset)
107                .map_err(|error| native_exception("DuckContext::set_default_charset", error))?;
108        accepted.then_some(()).ok_or(Error::NativeFailure {
109            operation: "DuckContext::set_default_charset",
110        })
111    }
112
113    /// Sets the default conditional-access system identifier.
114    pub fn set_default_cas_id(&mut self, cas_id: u16) -> Result<()> {
115        ffi::rustduck_context_set_default_cas_id(self.native.pin_mut(), cas_id)
116            .map_err(|error| native_exception("DuckContext::set_default_cas_id", error))
117    }
118
119    /// Sets the default private data specifier.
120    pub fn set_default_pds(&mut self, pds: u32) -> Result<()> {
121        ffi::rustduck_context_set_default_pds(self.native.pin_mut(), pds)
122            .map_err(|error| native_exception("DuckContext::set_default_pds", error))
123    }
124
125    /// Adds standards to the active mask.
126    pub fn add_standards(&mut self, standards: Standards) -> Result<()> {
127        ffi::rustduck_context_add_standards(self.native.pin_mut(), standards.bits())
128            .map_err(|error| native_exception("DuckContext::add_standards", error))
129    }
130
131    /// Returns the active standards mask.
132    pub fn standards(&self) -> Result<Standards> {
133        ffi::rustduck_context_standards(
134            self.native
135                .as_ref()
136                .expect("DuckContext construction rejects null owners"),
137        )
138        .map(Standards)
139        .map_err(|error| native_exception("DuckContext::standards", error))
140    }
141
142    /// Replaces the active standards mask.
143    pub fn reset_standards(&mut self, standards: Standards) -> Result<()> {
144        ffi::rustduck_context_reset_standards(self.native.pin_mut(), standards.bits())
145            .map_err(|error| native_exception("DuckContext::reset_standards", error))
146    }
147
148    /// Sets the time-reference offset in milliseconds.
149    pub fn set_time_reference_offset(&mut self, offset_millis: i64) -> Result<()> {
150        ffi::rustduck_context_set_time_reference_offset(self.native.pin_mut(), offset_millis)
151            .map_err(|error| native_exception("DuckContext::set_time_reference_offset", error))
152    }
153
154    /// Selects a named time reference.
155    pub fn set_time_reference(&mut self, name: &str) -> Result<()> {
156        let name = encode_utf16(name);
157        let accepted = ffi::rustduck_context_set_time_reference(self.native.pin_mut(), &name)
158            .map_err(|error| native_exception("DuckContext::set_time_reference", error))?;
159        accepted.then_some(()).ok_or(Error::NativeFailure {
160            operation: "DuckContext::set_time_reference",
161        })
162    }
163
164    pub(crate) fn native(&self) -> &ffi::RustDuckDuckContext {
165        self.native
166            .as_ref()
167            .expect("DuckContext construction rejects null owners")
168    }
169}
170
171#[cfg(test)]
172mod tests {
173    use super::Standards;
174
175    #[test]
176    fn all_covers_every_defined_standard() {
177        let defined = Standards::MPEG
178            | Standards::DVB
179            | Standards::SCTE
180            | Standards::ATSC
181            | Standards::ISDB
182            | Standards::JAPAN
183            | Standards::ABNT;
184        assert_eq!(Standards::ALL.bits(), 0x7f);
185        assert_eq!(Standards::all().bits(), 0x7f);
186        assert_eq!(defined.bits(), Standards::ALL.bits());
187    }
188
189    #[test]
190    fn not_keeps_only_defined_standard_bits() {
191        assert_eq!((!Standards::NONE).bits(), Standards::ALL.bits());
192        assert_eq!((!Standards::ALL).bits(), Standards::NONE.bits());
193        assert_eq!((!Standards::MPEG).bits(), 0x7e);
194        assert_eq!((!Standards(0xffff_ffff)).bits(), Standards::NONE.bits());
195        assert_eq!((!Standards(0x80)).bits(), Standards::ALL.bits());
196    }
197}