Skip to main content

descrambler_feed/
descrambler_feed.rs

1//! Feed a scrambled TS through `CaDescrambler` over in-memory devices.
2//!
3//! The wrapper keeps only the PIDs the CAM needs (here just the CAT PID, as no
4//! service has been added) and returns whatever the data device descrambled.
5//!
6//! Run: `cargo run -p dvb-ci-runtime --example descrambler_feed`
7
8use std::time::Duration;
9
10use dvb_ci_runtime::{
11    CaDescrambler, Driver, DriverBounds, MockCaDevice, MockCiDataDevice, StackBounds,
12    TransportBounds,
13};
14use mpeg_ts::ts::{TS_PACKET_SIZE, TS_SYNC_BYTE};
15
16const CAT_PID: u16 = 0x0001;
17const VIDEO_PID: u16 = 0x0100;
18const MAX_STALLED_WRITES: u32 = 4;
19
20fn driver_bounds() -> DriverBounds {
21    DriverBounds {
22        stack: StackBounds {
23            transport: TransportBounds {
24                poll_interval: Duration::from_millis(100),
25                reply_timeout: Duration::from_secs(1),
26                max_setup_attempts: 5,
27                max_reassembly_len: 2 * 1024 * 1024,
28                max_outbound_queue: 64,
29            },
30            max_sessions: 32,
31        },
32        max_pending_notifications: 1024,
33        slot_ready_grace: Duration::from_secs(12),
34        slot_retry: Duration::from_millis(250),
35    }
36}
37
38fn packet(pid: u16) -> [u8; TS_PACKET_SIZE] {
39    let mut p = [0xFF; TS_PACKET_SIZE];
40    p[0] = TS_SYNC_BYTE;
41    p[1..3].copy_from_slice(&pid.to_be_bytes());
42    p
43}
44
45fn main() -> std::io::Result<()> {
46    let driver = Driver::new(MockCaDevice::new([]), driver_bounds());
47    let module_output = packet(CAT_PID).to_vec();
48    let data = MockCiDataDevice::new([module_output]);
49    let mut descrambler = CaDescrambler::new(driver, data, MAX_STALLED_WRITES);
50
51    println!("required PIDs: {:?}", descrambler.required_pids());
52
53    let mut input = Vec::new();
54    input.extend_from_slice(&packet(CAT_PID));
55    input.extend_from_slice(&packet(VIDEO_PID));
56    let out = descrambler.feed_ts(&input)?;
57
58    println!("fed {} packets", input.len() / TS_PACKET_SIZE);
59    println!(
60        "written to the CAM: {} packet(s)",
61        descrambler.ci().written_ts().len() / TS_PACKET_SIZE
62    );
63    println!("descrambled back: {} packet(s)", out.len() / TS_PACKET_SIZE);
64    Ok(())
65}