This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
forked from asticode/go-astits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demuxer.go
163 lines (143 loc) · 4.09 KB
/
demuxer.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
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
package astits
import (
"context"
"io"
"github.com/pkg/errors"
)
// Sync byte
const syncByte = '\x47'
// Errors
var (
ErrNoMorePackets = errors.New("astits: no more packets")
ErrPacketMustStartWithASyncByte = errors.New("astits: packet must start with a sync byte")
)
// Demuxer represents a demuxer
// https://en.wikipedia.org/wiki/MPEG_transport_stream
// http://seidl.cs.vsb.cz/download/dvb/DVB_Poster.pdf
// http://www.etsi.org/deliver/etsi_en/300400_300499/300468/01.13.01_40/en_300468v011301o.pdf
type Demuxer struct {
ctx context.Context
dataBuffer []*Data
optPacketSize int
optPacketsParser PacketsParser
packetBuffer *packetBuffer
packetPool *packetPool
programMap programMap
r io.Reader
}
// PacketsParser represents an object capable of parsing a set of packets containing a unique payload spanning over those packets
// Use the skip returned argument to indicate whether the default process should still be executed on the set of packets
type PacketsParser func(ps []*Packet) (ds []*Data, skip bool, err error)
// New creates a new transport stream based on a reader
func New(ctx context.Context, r io.Reader, opts ...func(*Demuxer)) (d *Demuxer) {
// Init
d = &Demuxer{
ctx: ctx,
packetPool: newPacketPool(),
programMap: newProgramMap(),
r: r,
}
// Apply options
for _, opt := range opts {
opt(d)
}
return
}
// OptPacketSize returns the option to set the packet size
func OptPacketSize(packetSize int) func(*Demuxer) {
return func(d *Demuxer) {
d.optPacketSize = packetSize
}
}
// OptPacketsParser returns the option to set the packets parser
func OptPacketsParser(p PacketsParser) func(*Demuxer) {
return func(d *Demuxer) {
d.optPacketsParser = p
}
}
// NextPacket retrieves the next packet
func (dmx *Demuxer) NextPacket() (p *Packet, err error) {
// Check ctx error
// TODO Handle ctx error another way since if the read blocks, everything blocks
// Maybe execute everything in a goroutine and listen the ctx channel in the same for loop
if err = dmx.ctx.Err(); err != nil {
return
}
// Create packet buffer if not exists
if dmx.packetBuffer == nil {
if dmx.packetBuffer, err = newPacketBuffer(dmx.r, dmx.optPacketSize); err != nil {
err = errors.Wrap(err, "astits: creating packet buffer failed")
return
}
}
// Fetch next packet from buffer
if p, err = dmx.packetBuffer.next(); err != nil {
if err != ErrNoMorePackets {
err = errors.Wrap(err, "astits: fetching next packet from buffer failed")
}
return
}
return
}
// NextData retrieves the next data
func (dmx *Demuxer) NextData() (d *Data, err error) {
// Check data buffer
if len(dmx.dataBuffer) > 0 {
d = dmx.dataBuffer[0]
dmx.dataBuffer = dmx.dataBuffer[1:]
return
}
// Loop through packets
var p *Packet
var ps []*Packet
var ds []*Data
for {
// Get next packet
if p, err = dmx.NextPacket(); err != nil {
// We don't dump the packet pool since we don't want incomplete data
if err == ErrNoMorePackets {
return
}
err = errors.Wrap(err, "astits: fetching next packet failed")
return
}
// Add packet to the pool
if ps = dmx.packetPool.add(p); len(ps) == 0 {
continue
}
// Parse data
if ds, err = parseData(ps, dmx.optPacketsParser, dmx.programMap); err != nil {
err = errors.Wrap(err, "astits: building new data failed")
return
}
// Check whether there is data to be processed
if len(ds) > 0 {
// Process data
d = ds[0]
dmx.dataBuffer = append(dmx.dataBuffer, ds[1:]...)
// Update program map
for _, v := range ds {
if v.PAT != nil {
for _, pgm := range v.PAT.Programs {
// Program number 0 is reserved to NIT
if pgm.ProgramNumber > 0 {
dmx.programMap.set(pgm.ProgramMapID, pgm.ProgramNumber)
}
}
}
}
return
}
}
}
// Rewind rewinds the demuxer reader
func (dmx *Demuxer) Rewind() (n int64, err error) {
dmx.dataBuffer = []*Data{}
dmx.packetBuffer = nil
dmx.packetPool = newPacketPool()
if n, err = rewind(dmx.r); err != nil {
err = errors.Wrap(err, "astits: rewinding reader failed")
return
}
return
}