-
Notifications
You must be signed in to change notification settings - Fork 51
/
data_nit.go
80 lines (65 loc) · 2.21 KB
/
data_nit.go
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
package astits
import (
"fmt"
"github.com/asticode/go-astikit"
)
// NITData represents a NIT data
// Page: 29 | Chapter: 5.2.1 | Link: https://www.dvb.org/resources/public/standards/a38_dvb-si_specification.pdf
// (barbashov) the link above can be broken, alternative: https://dvb.org/wp-content/uploads/2019/12/a038_tm1217r37_en300468v1_17_1_-_rev-134_-_si_specification.pdf
type NITData struct {
NetworkDescriptors []*Descriptor
NetworkID uint16
TransportStreams []*NITDataTransportStream
}
// NITDataTransportStream represents a NIT data transport stream
type NITDataTransportStream struct {
OriginalNetworkID uint16
TransportDescriptors []*Descriptor
TransportStreamID uint16
}
// parseNITSection parses a NIT section
func parseNITSection(i *astikit.BytesIterator, tableIDExtension uint16) (d *NITData, err error) {
// Create data
d = &NITData{NetworkID: tableIDExtension}
// Network descriptors
if d.NetworkDescriptors, err = parseDescriptors(i); err != nil {
err = fmt.Errorf("astits: parsing descriptors failed: %w", err)
return
}
// Get next bytes
var bs []byte
if bs, err = i.NextBytesNoCopy(2); err != nil {
err = fmt.Errorf("astits: fetching next bytes failed: %w", err)
return
}
// Transport stream loop length
transportStreamLoopLength := int(uint16(bs[0]&0xf)<<8 | uint16(bs[1]))
// Transport stream loop
offsetEnd := i.Offset() + transportStreamLoopLength
for i.Offset() < offsetEnd {
// Create transport stream
ts := &NITDataTransportStream{}
// Get next bytes
if bs, err = i.NextBytesNoCopy(2); err != nil {
err = fmt.Errorf("astits: fetching next bytes failed: %w", err)
return
}
// Transport stream ID
ts.TransportStreamID = uint16(bs[0])<<8 | uint16(bs[1])
// Get next bytes
if bs, err = i.NextBytesNoCopy(2); err != nil {
err = fmt.Errorf("astits: fetching next bytes failed: %w", err)
return
}
// Original network ID
ts.OriginalNetworkID = uint16(bs[0])<<8 | uint16(bs[1])
// Transport descriptors
if ts.TransportDescriptors, err = parseDescriptors(i); err != nil {
err = fmt.Errorf("astits: parsing descriptors failed: %w", err)
return
}
// Append transport stream
d.TransportStreams = append(d.TransportStreams, ts)
}
return
}