-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpat_test.go
153 lines (127 loc) · 3.66 KB
/
pat_test.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
package mpegts
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func ExamplePAT_ParsePatSection() {
data := []byte{
0x00, 0xB0, 0x0D, 0x70, 0xE9, 0xDD, 0x00, 0x00,
0x00, 0x01, 0xE0, 0x42, 0xAE, 0xF4, 0xD8, 0x1C,
}
pat := new(PAT)
if err := pat.ParsePatSection(data); err != nil {
panic(err)
}
fmt.Println("TSID", pat.TSID())
for _, item := range pat.Items {
fmt.Printf("Program Number %d PID %d\n", item.PNR(), item.PID())
}
// Output:
// TSID 28905
// Program Number 1 PID 66
}
func ExamplePAT_Packetizer() {
pat := NewPat()
pat.SetVersion(1)
pat.SetTSID(1)
item := NewPatItem()
item.SetPNR(1)
item.SetPID(1031)
pat.Items = append(pat.Items, item)
pat.Finalize()
ts := NewTS(0)
for pack := pat.Packetizer(); pack.Next(ts); ts.IncrementCC() {
// TS Header with Payload start offset
fmt.Printf("%X...\n", ts[:25])
}
// Output:
// 474000100000B00D0001C300000001E407FF2D3E2FFFFFFFFF...
}
func TestPAT_ParsePatSection(t *testing.T) {
assert := assert.New(t)
data := []byte{
0x00, 0xB0, 0x25, 0x00, 0x01, 0xC3, 0x00, 0x00,
0x00, 0x00, 0xE0, 0x10, 0x00, 0x01, 0xE4, 0x07,
0x00, 0x02, 0xE4, 0x08, 0x00, 0x03, 0xE4, 0x09,
0x00, 0x04, 0xE4, 0x0A, 0x00, 0x05, 0xE4, 0x0B,
0x00, 0x06, 0xE4, 0x0C, 0xB2, 0xD1, 0xB8, 0xDE,
}
expectedItems := []struct {
pnr uint16
pid PID
}{
{pnr: 0, pid: 16},
{pnr: 1, pid: 1031},
{pnr: 2, pid: 1032},
{pnr: 3, pid: 1033},
{pnr: 4, pid: 1034},
{pnr: 5, pid: 1035},
{pnr: 6, pid: 1036},
}
pat := new(PAT)
if err := pat.ParsePatSection(data); !assert.NoError(err) {
return
}
assert.Equal(uint8(1), pat.Version())
assert.Equal(uint16(1), pat.TSID())
assert.Equal(7, len(pat.Items))
for i, item := range pat.Items {
expected := expectedItems[i]
assert.Equal(expected.pnr, item.PNR())
assert.Equal(expected.pid, item.PID())
}
}
func TestPAT_Packetize(t *testing.T) {
assert := assert.New(t)
items := []struct {
pnr uint16
pid PID
}{
{pnr: 0, pid: 16},
{pnr: 1, pid: 1031},
{pnr: 2, pid: 1032},
{pnr: 3, pid: 1033},
{pnr: 4, pid: 1034},
{pnr: 5, pid: 1035},
{pnr: 6, pid: 1036},
}
pat := NewPat()
pat.SetVersion(1)
pat.SetTSID(1)
for _, item := range items {
patItem := NewPatItem()
patItem.SetPID(item.pid)
patItem.SetPNR(item.pnr)
pat.Items = append(pat.Items, patItem)
}
pat.Finalize()
//
// OC3.demo.ts
expectedTS := TS{
0x47, 0x40, 0x00, 0x11, 0x00, 0x00, 0xB0, 0x25, 0x00, 0x01, 0xC3, 0x00,
0x00, 0x00, 0x00, 0xE0, 0x10, 0x00, 0x01, 0xE4, 0x07, 0x00, 0x02, 0xE4,
0x08, 0x00, 0x03, 0xE4, 0x09, 0x00, 0x04, 0xE4, 0x0A, 0x00, 0x05, 0xE4,
0x0B, 0x00, 0x06, 0xE4, 0x0C, 0xB2, 0xD1, 0xB8, 0xDE, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF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