arib-cli 0.3.0

Reads the signalling of ARIB broadcasts, as an example of the arib crate
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! The signalling of both transports, taken down to what the commands print.

use std::ops::ControlFlow;
use std::time::{Duration, Instant};

use arib::demux::{Packet, SignalingEvent};
use arib::genre::Genre;
use arib::mmt::message::Message;
use arib::text::decode;
use chrono::{NaiveDateTime, TimeDelta};
use tracing::warn;

use crate::component::{Component, Stream as ElementaryStream};
use crate::input::{Format, Input};

/// A network, and the streams it is made of.
#[derive(Clone, Debug)]
pub struct Network {
    pub id: u16,
    pub name: String,
    pub streams: Vec<Stream>,
}

/// A transport stream, or a TLV stream, of a network.
#[derive(Clone, Debug)]
pub struct Stream {
    pub id: u16,
    pub original_network_id: u16,
    pub service_ids: Vec<u16>,
}

/// The services of a stream, as the SDT or the MH-SDT has them.
#[derive(Clone, Debug)]
pub struct Services {
    pub stream_id: u16,
    pub original_network_id: u16,
    /// Whether the stream is the one being read, rather than another of the network.
    pub actual: bool,
    pub services: Vec<Service>,
}

#[derive(Clone, Debug, Default)]
pub struct Service {
    pub id: u16,
    pub service_type: Option<u8>,
    pub provider: String,
    pub name: String,
}

/// Events of a service, as the EIT or the MH-EIT has them.
#[derive(Clone, Debug)]
pub struct Events {
    pub service_id: u16,
    /// Whether these are the present and following events of the stream being read, rather than
    /// its schedule or those of another stream.
    pub present_following: bool,
    /// Of the present and following events, 0 carries the present one and 1 the following one.
    pub section_number: u8,
    pub events: Vec<Event>,
}

#[derive(Clone, Debug)]
pub struct Event {
    pub id: u16,
    pub start_time: Option<NaiveDateTime>,
    pub duration: Option<TimeDelta>,
    pub name: String,
    pub text: String,
    /// What the component descriptors of the event tell of its streams.
    pub components: Vec<Component>,
    pub genres: Vec<Genre>,
    /// The detailed description, as items like "番組内容" or "出演者" and what they say.
    pub details: Vec<(String, String)>,
    /// Whether the event is scrambled, to be viewed with a contract.
    pub scrambled: bool,
}

impl Event {
    /// Takes in what another entry of the same event tells and this one does not: the schedule
    /// names an event in one section and details it in another. Tells whether anything was.
    pub fn merge(&mut self, other: Event) -> bool {
        let mut merged = false;
        if self.name.is_empty() && !other.name.is_empty() {
            self.name = other.name;
            self.text = other.text;
            merged = true;
        }
        if self.components.is_empty() && !other.components.is_empty() {
            self.components = other.components;
            merged = true;
        }
        if self.genres.is_empty() && !other.genres.is_empty() {
            self.genres = other.genres;
            merged = true;
        }
        if self.details.is_empty() && !other.details.is_empty() {
            self.details = other.details;
            merged = true;
        }
        merged
    }
}

/// The descriptions and the items of an extended event descriptor, as they are coded.
type ItemBytes<'a> = Vec<(&'a [u8], &'a [u8])>;

/// Puts the items of the extended event descriptors of an event together, in the order of the
/// descriptors, an item without a description continuing the one before it. The bytes are joined
/// before they are decoded, as an item may be split between descriptors mid-character.
fn join_items(
    mut descriptors: Vec<(u8, ItemBytes<'_>)>,
    decode: impl Fn(&[u8]) -> String,
) -> Vec<(String, String)> {
    descriptors.sort_by_key(|(descriptor_number, _)| *descriptor_number);

    let mut items: Vec<(Vec<u8>, Vec<u8>)> = Vec::new();
    for (description, item) in descriptors.into_iter().flat_map(|(_, items)| items) {
        match items.last_mut() {
            Some((_, last)) if description.is_empty() => last.extend_from_slice(item),
            _ => items.push((description.to_vec(), item.to_vec())),
        }
    }

    items
        .into_iter()
        .map(|(description, item)| (decode(&description), decode(&item)))
        .collect()
}

/// The streams of a service, as the PMT or the MPT lists them.
#[derive(Clone, Debug)]
pub struct Streams {
    pub service_id: u16,
    pub streams: Vec<ElementaryStream>,
}

#[derive(Clone, Debug)]
pub enum Si {
    Network(Network),
    Services(Services),
    Events(Events),
    Streams(Streams),
}

/// Reads the signalling of the stream until it ends, `timeout` passes, or `on_si` breaks.
pub fn read(
    input: Input,
    timeout: Duration,
    mut on_si: impl FnMut(Si) -> ControlFlow<()>,
) -> anyhow::Result<()> {
    let deadline = Instant::now() + timeout;
    let packets: Box<dyn Iterator<Item = arib::Result<Packet>>> = match input.format {
        // The signalling is broadcast in the clear, so no card is needed to read it.
        Format::Ts => Box::new(arib::ts::demux::Demuxer::new(input.reader)),
        Format::Mmt => Box::new(arib::mmt::demux::Demuxer::new(input.reader)),
    };

    for packet in packets {
        if Instant::now() >= deadline {
            break;
        }

        let event = match packet {
            Ok(Packet::Signaling(event)) => event,
            Ok(Packet::Media(_)) => continue,
            Err(arib::Error::Io(error)) => return Err(error.into()),
            Err(error) => {
                warn!(%error, "Could not read a packet");
                continue;
            }
        };

        if let Some(si) = si_of(event)
            && on_si(si).is_break()
        {
            break;
        }
    }

    Ok(())
}

fn si_of(event: SignalingEvent) -> Option<Si> {
    use arib::mmt::table::Table as MmtTable;
    use arib::tlv::si::Table as TlvTable;
    use arib::ts::table::Table as TsTable;

    Some(match event {
        SignalingEvent::Ts {
            table_id, table, ..
        } => match table {
            TsTable::Nit(nit) if table_id == 0x40 => Si::Network(ts::network(nit)),
            TsTable::Sdt(sdt) => Si::Services(ts::services(table_id == 0x42, sdt)),
            TsTable::Eit(eit) => Si::Events(ts::events(table_id == 0x4E, eit)),
            TsTable::Pmt(pmt) => Si::Streams(ts::streams(pmt)),
            _ => return None,
        },
        SignalingEvent::Tlv(TlvTable::TlvNit(nit)) => Si::Network(mmt::network(nit)),
        SignalingEvent::Mmt(Message::Pa(message)) => {
            return message.tables.into_iter().find_map(|table| match table {
                MmtTable::Mpt(mpt) => mmt::streams(mpt).map(Si::Streams),
                _ => None,
            });
        }
        SignalingEvent::Mmt(Message::M2Section(message)) => match message.table {
            MmtTable::MhSdt(sdt) => Si::Services(mmt::services(sdt)),
            MmtTable::MhEit(eit) => Si::Events(mmt::events(eit)),
            _ => return None,
        },
        _ => return None,
    })
}

mod ts {
    use arib::ts::descriptor::Descriptor;
    use arib::ts::table::{Eit, Nit, Pmt, Sdt};

    use super::*;
    use crate::component::{self, Audio};

    pub fn streams(pmt: Pmt) -> Streams {
        Streams {
            service_id: pmt.program_number,
            streams: pmt
                .streams
                .into_iter()
                .map(|stream| {
                    let component_tag =
                        stream
                            .descriptors
                            .iter()
                            .find_map(|descriptor| match descriptor {
                                Descriptor::StreamIdentifier(descriptor) => {
                                    Some(u16::from(descriptor.component_tag))
                                }
                                _ => None,
                            });
                    let (kind, codec) =
                        component::of_stream_type(stream.stream_type, component_tag);
                    ElementaryStream {
                        id: stream.elementary_pid,
                        component_tag,
                        kind,
                        codec,
                        details: vec![],
                    }
                })
                .collect(),
        }
    }

    fn components(descriptors: &[Descriptor]) -> Vec<Component> {
        descriptors
            .iter()
            .filter_map(|descriptor| match descriptor {
                Descriptor::Component(descriptor) => Some(Component {
                    component_tag: u16::from(descriptor.component_tag),
                    details: component::video_of_component_type(descriptor.component_type),
                }),
                Descriptor::AudioComponent(descriptor) => Some(Component {
                    component_tag: u16::from(descriptor.component_tag),
                    details: Audio {
                        component_type: descriptor.component_type,
                        sampling_rate: descriptor.sampling_rate,
                        main_component_flag: descriptor.main_component_flag,
                        iso_639_language_code: descriptor.iso_639_language_code,
                        iso_639_language_code_2: descriptor.iso_639_language_code_2,
                        text: &decode(&descriptor.text),
                    }
                    .details(),
                }),
                _ => None,
            })
            .collect()
    }

    pub fn network(nit: Nit) -> Network {
        Network {
            id: nit.network_id,
            name: nit
                .descriptors
                .iter()
                .find_map(|descriptor| match descriptor {
                    Descriptor::NetworkName(descriptor) => Some(decode(&descriptor.network_name)),
                    _ => None,
                })
                .unwrap_or_default(),
            streams: nit
                .transport_streams
                .into_iter()
                .map(|stream| Stream {
                    id: stream.transport_stream_id,
                    original_network_id: stream.original_network_id,
                    service_ids: stream
                        .descriptors
                        .iter()
                        .filter_map(|descriptor| match descriptor {
                            Descriptor::ServiceList(list) => Some(&list.services),
                            _ => None,
                        })
                        .flatten()
                        .map(|service| service.service_id)
                        .collect(),
                })
                .collect(),
        }
    }

    pub fn services(actual: bool, sdt: Sdt) -> Services {
        Services {
            stream_id: sdt.transport_stream_id,
            original_network_id: sdt.original_network_id,
            actual,
            services: sdt
                .services
                .into_iter()
                .map(|service| {
                    let descriptor =
                        service
                            .descriptors
                            .iter()
                            .find_map(|descriptor| match descriptor {
                                Descriptor::Service(descriptor) => Some(descriptor),
                                _ => None,
                            });
                    Service {
                        id: service.service_id,
                        service_type: descriptor.map(|descriptor| descriptor.service_type),
                        provider: descriptor
                            .map(|descriptor| decode(&descriptor.service_provider_name))
                            .unwrap_or_default(),
                        name: descriptor
                            .map(|descriptor| decode(&descriptor.service_name))
                            .unwrap_or_default(),
                    }
                })
                .collect(),
        }
    }

    pub fn events(present_following: bool, eit: Eit) -> Events {
        Events {
            service_id: eit.service_id,
            present_following,
            section_number: eit.section_number,
            events: eit
                .events
                .into_iter()
                .map(|event| {
                    let descriptor =
                        event
                            .descriptors
                            .iter()
                            .find_map(|descriptor| match descriptor {
                                Descriptor::ShortEvent(descriptor) => Some(descriptor),
                                _ => None,
                            });
                    Event {
                        id: event.event_id,
                        start_time: event.start_time,
                        duration: event.duration,
                        name: descriptor
                            .map(|descriptor| decode(&descriptor.event_name))
                            .unwrap_or_default(),
                        text: descriptor
                            .map(|descriptor| decode(&descriptor.text))
                            .unwrap_or_default(),
                        components: components(&event.descriptors),
                        genres: event
                            .descriptors
                            .iter()
                            .filter_map(|descriptor| match descriptor {
                                Descriptor::Content(descriptor) => Some(&descriptor.items),
                                _ => None,
                            })
                            .flatten()
                            .map(|item| item.genre)
                            .collect(),
                        details: join_items(
                            event
                                .descriptors
                                .iter()
                                .filter_map(|descriptor| match descriptor {
                                    Descriptor::ExtendedEvent(descriptor) => Some((
                                        descriptor.descriptor_number,
                                        descriptor
                                            .items
                                            .iter()
                                            .map(|item| {
                                                (&item.item_description[..], &item.item[..])
                                            })
                                            .collect(),
                                    )),
                                    _ => None,
                                })
                                .collect(),
                            decode,
                        ),
                        scrambled: event.free_ca_mode,
                    }
                })
                .collect(),
        }
    }
}

mod mmt {
    use arib::mmt::descriptor::Descriptor;
    use arib::mmt::table::{MhEit, MhSdt, Mpt};
    use arib::tlv::si::{Descriptor as TlvDescriptor, TlvNit};

    use super::*;

    /// The strings of MMT are UTF-8, unlike those of MPEG-2 TS, which are in the ARIB 8-bit
    /// encoding.
    fn utf8(bytes: &[u8]) -> String {
        String::from_utf8_lossy(bytes).into_owned()
    }

    /// The assets of the package of the MPT, which is the service of the same ID.
    pub fn streams(mpt: Mpt) -> Option<Streams> {
        let service_id = u16::from_be_bytes(mpt.mmt_package_id.as_slice().try_into().ok()?);

        Some(Streams {
            service_id,
            streams: mpt
                .assets
                .into_iter()
                .filter_map(|asset| {
                    let id = asset.locations.last()?.packet_id()?;
                    let component_tag =
                        asset
                            .asset_descriptors
                            .iter()
                            .find_map(|descriptor| match descriptor {
                                Descriptor::MhStreamIdentifier(descriptor) => {
                                    Some(descriptor.component_tag)
                                }
                                _ => None,
                            });
                    let (kind, codec) = crate::component::of_asset_type(asset.asset_type);
                    Some(ElementaryStream {
                        id,
                        component_tag,
                        kind,
                        codec,
                        details: components(&asset.asset_descriptors)
                            .into_iter()
                            .flat_map(|component| component.details)
                            .collect(),
                    })
                })
                .collect(),
        })
    }

    fn components(descriptors: &[Descriptor]) -> Vec<Component> {
        descriptors
            .iter()
            .filter_map(|descriptor| match descriptor {
                Descriptor::VideoComponent(descriptor) => Some(Component {
                    component_tag: descriptor.component_tag,
                    details: crate::component::video_of_mmt(descriptor),
                }),
                Descriptor::MhAudioComponent(descriptor) => Some(Component {
                    component_tag: descriptor.component_tag,
                    details: crate::component::Audio {
                        component_type: descriptor.component_type,
                        sampling_rate: descriptor.sampling_rate,
                        main_component_flag: descriptor.main_component_flag,
                        iso_639_language_code: descriptor.iso_639_language_code,
                        iso_639_language_code_2: descriptor.iso_639_language_code_2,
                        text: &utf8(&descriptor.text),
                    }
                    .details(),
                }),
                _ => None,
            })
            .collect()
    }

    pub fn network(nit: TlvNit) -> Network {
        Network {
            id: nit.original_network_id,
            name: nit
                .descriptors
                .iter()
                .find_map(|descriptor| match descriptor {
                    TlvDescriptor::NetworkName(descriptor) => Some(utf8(&descriptor.network_name)),
                    _ => None,
                })
                .unwrap_or_default(),
            streams: nit
                .tlv_streams
                .into_iter()
                .map(|stream| Stream {
                    id: stream.tlv_stream_id,
                    original_network_id: stream.original_network_id,
                    service_ids: stream
                        .descriptors
                        .iter()
                        .filter_map(|descriptor| match descriptor {
                            TlvDescriptor::ServiceList(list) => Some(&list.services),
                            _ => None,
                        })
                        .flatten()
                        .map(|service| service.service_id)
                        .collect(),
                })
                .collect(),
        }
    }

    pub fn services(sdt: MhSdt) -> Services {
        Services {
            stream_id: sdt.tlv_stream_id,
            original_network_id: sdt.original_network_id,
            actual: sdt.table_id == 0x9F,
            services: sdt
                .services
                .into_iter()
                .map(|service| {
                    let descriptor =
                        service
                            .descriptors
                            .iter()
                            .find_map(|descriptor| match descriptor {
                                Descriptor::MhService(descriptor) => Some(descriptor),
                                _ => None,
                            });
                    Service {
                        id: service.service_id,
                        service_type: descriptor.map(|descriptor| descriptor.service_type),
                        provider: descriptor
                            .map(|descriptor| utf8(&descriptor.service_provider_name))
                            .unwrap_or_default(),
                        name: descriptor
                            .map(|descriptor| utf8(&descriptor.service_name))
                            .unwrap_or_default(),
                    }
                })
                .collect(),
        }
    }

    pub fn events(eit: MhEit) -> Events {
        Events {
            service_id: eit.service_id,
            present_following: eit.table_id == 0x8B,
            section_number: eit.section_number,
            events: eit
                .events
                .into_iter()
                .map(|event| {
                    let descriptor =
                        event
                            .descriptors
                            .iter()
                            .find_map(|descriptor| match descriptor {
                                Descriptor::MhShortEvent(descriptor) => Some(descriptor),
                                _ => None,
                            });
                    Event {
                        id: event.event_id,
                        start_time: event.start_time,
                        duration: event.duration,
                        name: descriptor
                            .map(|descriptor| utf8(&descriptor.event_name))
                            .unwrap_or_default(),
                        text: descriptor
                            .map(|descriptor| utf8(&descriptor.text))
                            .unwrap_or_default(),
                        components: components(&event.descriptors),
                        genres: event
                            .descriptors
                            .iter()
                            .filter_map(|descriptor| match descriptor {
                                Descriptor::MhContent(descriptor) => Some(&descriptor.items),
                                _ => None,
                            })
                            .flatten()
                            .map(|item| item.genre)
                            .collect(),
                        details: join_items(
                            event
                                .descriptors
                                .iter()
                                .filter_map(|descriptor| match descriptor {
                                    Descriptor::MhExtendedEvent(descriptor) => Some((
                                        descriptor.descriptor_number,
                                        descriptor
                                            .items
                                            .iter()
                                            .map(|item| {
                                                (&item.item_description[..], &item.item[..])
                                            })
                                            .collect(),
                                    )),
                                    _ => None,
                                })
                                .collect(),
                            utf8,
                        ),
                        scrambled: event.free_ca_mode,
                    }
                })
                .collect(),
        }
    }
}