1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::{Cursor, Write};
use std::rc::Rc;
use std::time::Duration;

use crate::packet::Packet as TsPacket;
use crate::pes::PES;
use crate::pid::PID;
use crate::result::Result;
use crate::section::{WithHeader, WithSyntaxSection};
use crate::subtable_id::{SubtableID, SubtableIDer};
use crate::{EIT, PAT, PMT, SDT};

pub struct Buf(pub Cursor<Vec<u8>>);

impl Buf {
    #[inline(always)]
    fn reset(&mut self) {
        self.0.set_position(0);
        self.0.get_mut().clear();
    }

    #[inline(always)]
    fn is_empty(&self) -> bool {
        self.0.position() == 0
    }

    #[inline(always)]
    pub fn sz(&self) -> usize {
        self.0.position() as usize
    }
}

impl Default for Buf {
    fn default() -> Self {
        Buf(Cursor::new(Vec::with_capacity(2048)))
    }
}

pub struct Section {
    /// parent table-id
    table_id: SubtableID,

    /// number inside table sections
    number: u8,

    /// full section size with header, data, CRC
    sz: usize,

    pub buf: Buf,
}

impl Section {
    fn new(table_id: SubtableID, number: u8, sz: usize) -> Section {
        Section {
            table_id,
            number,
            sz,
            buf: Default::default(),
        }
    }

    #[inline(always)]
    fn into_ref(self) -> SectionRef {
        Rc::new(RefCell::new(Box::new(self)))
    }

    /// section consumed all data
    #[inline(always)]
    fn done(&self) -> bool {
        self.sz_need() == 0
    }

    /// sz need to read
    #[inline(always)]
    fn sz_need(&self) -> usize {
        self.sz - self.buf.sz()
    }
}

type SectionRef = Rc<RefCell<Box<Section>>>;

pub struct Sections(pub Vec<SectionRef>);

impl Sections {
    #[inline(always)]
    fn get_mut(&mut self, number: u8) -> Option<&mut SectionRef> {
        self.0.iter_mut().find(|s| s.borrow().number == number)
    }

    #[inline(always)]
    fn push(&mut self, s: SectionRef) {
        self.0.push(s);
        self.0
            .sort_unstable_by(|a, b| a.borrow().number.cmp(&b.borrow().number));
    }
}

impl Default for Sections {
    fn default() -> Self {
        Sections(Vec::with_capacity(1))
    }
}

pub struct Table {
    /// mpeg-ts last-section-number
    last_section_number: u8,
    pub sections: Sections,
}

impl Table {
    fn new(last_section_number: u8) -> Table {
        Table {
            last_section_number,
            sections: Default::default(),
        }
    }

    #[inline(always)]
    fn done(&self) -> bool {
        match self.sections.0.len() {
            0 => false,
            n => {
                let last = (&self.sections.0[n - 1]).borrow();
                let first = (&self.sections.0[0]).borrow();

                first.number == 0
                    && last.number == self.last_section_number
                    && first.done()
                    && last.done()
            }
        }
    }
}

struct Tables {
    map: HashMap<SubtableID, Table>,
    /// current demuxing section
    current: Option<SectionRef>,
}

impl Tables {}

impl Default for Tables {
    fn default() -> Self {
        Tables {
            map: HashMap::new(),
            current: None,
        }
    }
}

pub struct Packet {
    pub pid: PID,

    pub offset: usize,

    /// presentation time stamp
    pub pts: Option<Duration>,

    /// decode time stamp
    pub dts: Option<Duration>,

    pub buf: Buf,

    /// got ts PUSI
    started: bool,
}

impl Packet {
    fn new(pid: PID) -> Packet {
        Packet {
            pid,
            offset: 0,
            pts: None,
            dts: None,
            buf: Default::default(),
            started: false,
        }
    }
}

#[derive(Default)]
struct Packets(HashMap<PID, Packet>);

/// pid, packet-constructed
#[derive(Debug)]
struct PMTPids(Vec<(PID, bool)>);

impl PMTPids {
    #[inline(always)]
    fn has(&self, pid: PID) -> bool {
        self.0.iter().any(|p| (*p).0 == pid)
    }

    #[inline(always)]
    fn push_uniq(&mut self, pid: PID) {
        if !self.has(pid) {
            self.0.push((pid, false))
        }
    }

    /// got pid? and pid is parsed and packet builded
    #[inline(always)]
    fn is_packet_builded(&self, pid: PID) -> Option<bool> {
        self.0.iter().find(|p| p.0 == pid).map(|p| p.1)
    }

    #[inline(always)]
    fn set_is_packet_builded(&mut self, pid: PID, v: bool) {
        if let Some(p) = self.0.iter_mut().find(|p| p.0 == pid) {
            p.1 = v;
        }
    }

    /// all pids are parsed?
    #[inline(always)]
    #[allow(dead_code)]
    fn are_all_packets_builded(&self) -> bool {
        !self.0.iter().any(|p| !(*p).1)
    }
}

impl Default for PMTPids {
    fn default() -> Self {
        PMTPids(Vec::with_capacity(3))
    }
}

pub trait DemuxerEvents {
    fn on_table(&mut self, _: SubtableID, _: &Table) {}
    fn on_packet(&mut self, _: &Packet) {}
}

/// TODO: use tree, redix tree here
/// TODO: add benches
pub struct Demuxer<T>
where
    T: DemuxerEvents,
{
    offset: usize,

    pat: Tables,
    pmt: Tables,
    eit: Tables,
    sdt: Tables,

    #[allow(dead_code)]
    nit: Tables,
    #[allow(dead_code)]
    cat: Tables,
    #[allow(dead_code)]
    bat: Tables,

    packets: Packets,

    // TODO: add PID with state(is-parsed or not)
    //       for multiple PMTs
    pmt_pids: PMTPids,

    events: T,
}

unsafe impl<T> Send for Demuxer<T> where T: DemuxerEvents + Send {}

impl<T> Demuxer<T>
where
    T: DemuxerEvents,
{
    pub fn new(events: T) -> Demuxer<T> {
        Demuxer {
            offset: 0,

            pat: Default::default(),
            pmt: Default::default(),
            eit: Default::default(),
            sdt: Default::default(),
            nit: Default::default(),
            cat: Default::default(),
            bat: Default::default(),

            pmt_pids: Default::default(),

            packets: Default::default(),

            events,
        }
    }

    /// cache pmt pids
    // TODO: also do via iterator
    // TODO: .iter().collect() for lazy collection
    #[inline(always)]
    fn build_pmt_pids(&mut self) {
        for (_, table) in self.pat.map.iter() {
            for section_ref in table.sections.0.iter() {
                let section = (*section_ref).borrow();
                let raw = section.buf.0.get_ref().as_slice();
                let pat = PAT::new(raw);

                // TODO: refactor via iter/to-iter
                for pid in pat
                    .programs()
                    .filter_map(Result::ok)
                    .filter(|p| p.pid().is_program_map())
                    .map(|p| PID::from(p.pid()))
                {
                    self.pmt_pids.push_uniq(pid)
                }
            }
        }
    }

    /// build packets cache
    // TODO: also do via iterator
    // TODO: .iter().collect() for lazy collection
    #[inline(always)]
    fn build_packets(&mut self) {
        for (_, table) in self.pmt.map.iter() {
            for section_ref in table.sections.0.iter() {
                let section = (*section_ref).borrow();
                let raw = section.buf.0.get_ref().as_slice();
                let pmt = PMT::new(raw);

                // TODO: refactor via iter/to-iter
                for pid in pmt
                    .streams()
                    .filter_map(Result::ok)
                    .map(|s| PID::from(s.pid()))
                {
                    self.packets
                        .0
                        .entry(pid)
                        .or_insert_with(|| Packet::new(pid));
                }
            }
        }
    }

    // TODO: move to macros?
    #[inline(always)]
    fn demux_section(&mut self, pid_or_pmt: (PID, bool), pkt: &TsPacket) -> Result<()> {
        let tables = match pid_or_pmt {
            (PID::PAT, false) => &mut self.pat,
            (PID::SDT, false) => &mut self.sdt,
            (PID::EIT, false) => &mut self.eit,
            (PID::NIT, false) => &mut self.nit,
            (PID::CAT, false) => &mut self.cat,
            (_, true) => &mut self.pmt,
            _ => unreachable!(),
        };

        let buf = pkt.buf_payload_section()?;

        if pkt.pusi() {
            let (id, sz, section_number, last_section_number) = match pid_or_pmt {
                (PID::PAT, false) => {
                    let s = PAT::try_new(buf)?;
                    (
                        s.subtable_id(),
                        s.sz(),
                        s.section_number(),
                        s.last_section_number(),
                    )
                }
                (PID::SDT, false) => {
                    let s = SDT::try_new(buf)?;
                    (
                        s.subtable_id(),
                        s.sz(),
                        s.section_number(),
                        s.last_section_number(),
                    )
                }
                (PID::EIT, false) => {
                    let s = EIT::try_new(buf)?;
                    (
                        s.subtable_id(),
                        s.sz(),
                        s.section_number(),
                        s.last_section_number(),
                    )
                }
                (_, true) => {
                    let s = PMT::try_new(buf)?;
                    (
                        s.subtable_id(),
                        s.sz(),
                        s.section_number(),
                        s.last_section_number(),
                    )
                }
                _ => unreachable!(),
            };

            let table = tables
                .map
                .entry(id)
                .or_insert_with(|| Table::new(last_section_number));

            let section_ref = match table.sections.get_mut(section_number) {
                Some(section_ref) => {
                    let mut section = (*section_ref).borrow_mut();
                    section.buf.reset();
                    section.sz = sz;

                    section_ref.clone()
                }
                None => {
                    let section_ref = Section::new(id, section_number, sz).into_ref();
                    table.sections.push(section_ref.clone());
                    section_ref
                }
            };

            tables.current = Some(section_ref);
        }

        if let Some(section_ref) = &tables.current {
            {
                let mut section = (*section_ref).borrow_mut();
                let sz_need = section.sz_need();

                // remove null/padding bytes
                let buf = if buf.len() > sz_need {
                    &buf[0..sz_need]
                } else {
                    buf
                };

                section.buf.0.write_all(buf)?;
            }

            {
                let section = (*section_ref).borrow();
                if section.done() {
                    if let Some(table) = tables.map.get(&section.table_id) {
                        if table.done() {
                            // emit
                            self.events.on_table(section.table_id, &table);
                        }
                    }
                }
            }
        }

        Ok(())
    }

    pub fn demux(&mut self, raw: &[u8]) -> Result<()> {
        if self.demux_tables(raw)? {
            return Ok(());
        }

        self.demux_packets(raw)
    }

    /// ffmpeg::avformat_open_input analog
    /// probe input
    /// return: is pid handled?
    pub fn demux_tables(&mut self, raw: &[u8]) -> Result<(bool)> {
        self.offset += raw.len();

        let pkt = TsPacket::new(&raw)?;
        let pid = pkt.pid();

        if pid.is_null() {
            // null packet PID
            return Ok(true);
        }

        match pid {
            PID::PAT => {
                self.demux_section((pid, false), &pkt)?;

                // extract pids from PAT
                if self.pmt_pids.0.is_empty() {
                    self.pmt_pids.0.clear();
                    self.packets.0.clear();
                    self.build_pmt_pids();
                }
            }
            PID::SDT | PID::EIT /* | PID::NIT | PID::CAT | PID::BAT */ =>
                self.demux_section((pid, false), &pkt)?,

            PID::Other(..) => {
                // PAT not ready yet
                // wait for PAT
                if self.pmt_pids.0.is_empty() {
                    return Ok(true);
                }

                match self.pmt_pids.is_packet_builded(pid) {
                    Some(true) => { // got PMT and already builded
                        self.demux_section((pid, true), &pkt)?;
                    },
                    Some(false) => { // got PMT and not builded
                        self.demux_section((pid, true), &pkt)?;

                        self.build_packets();

                        self.pmt_pids.set_is_packet_builded(pid, true);
                    },
                    None => {return Ok(false); }
                }
            }
            _ => {}
        }

        Ok(true)
    }

    /// ffmpeg::av_read_frame analog
    pub fn demux_packets(&mut self, raw: &[u8]) -> Result<()> {
        self.offset += raw.len();

        let pkt = TsPacket::new(&raw)?;
        let pid = pkt.pid();

        if pid.is_null() // null packet PID
        && !pid.is_other() // PID is section/table PID
        // PAT not ready yet
        // wait for pat
        && !self.pmt_pids.0.is_empty()
        {
            return Ok(());
        }

        let mut packet = match self.packets.0.get_mut(&pid) {
            Some(packet) => packet,
            None => return Ok(()), // packet is not builder - wait fot PMT
        };

        let mut buf = pkt.buf_payload_pes()?;

        if pkt.pusi() {
            let pes = PES::new(buf);

            if !packet.buf.is_empty() {
                // emit
                self.events.on_packet(packet);
            }

            packet.buf.reset();
            packet.started = true;
            packet.offset += self.offset + raw.len() - buf.len();
            packet.pts = pes.pts().map(Duration::from);
            packet.dts = pes.dts().map(Duration::from);

            buf = pes.buf_seek_payload();
        }

        if packet.started {
            packet.buf.0.write_all(buf)?;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {}