-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmpeg2ts.go
132 lines (117 loc) · 3.11 KB
/
mpeg2ts.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
package mpeg2ts
import (
"fmt"
"os"
)
func New(chunkSize int) *MPEG2TS {
m := MPEG2TS{}
m.PacketList, _ = NewPacketList(chunkSize)
return &m
}
func LoadStandardTS(fname string) (*MPEG2TS, error) {
return loadFile(fname, PacketSizeDefault)
}
func LoadStandardTSWithFEC(fname string) (*MPEG2TS, error) {
return loadFile(fname, PacketSizeWithFEC)
}
func loadFile(fname string, packetLength int) (*MPEG2TS, error) {
file, err := os.Open(fname)
if err != nil {
return nil, err
}
defer file.Close()
var fsize int64
if fi, err := file.Stat(); err == nil {
fsize = fi.Size()
} else {
return nil, err
}
if fsize < PacketSizeDefault {
return nil, fmt.Errorf("filesize (%d) is smaller than the minimum (%d)", fsize, PacketSizeDefault)
}
m := New(packetLength) //NewWithPacketCount(fsize/int64(packetLength), packetLength)
packetBuffer := make([]byte, packetLength)
i := 0
for {
// fmt.Println(i)
n, err := file.Read(packetBuffer)
if err != nil {
// Readエラー処理
if err.Error() == "EOF" {
break
}
return nil, err
}
if n < packetLength {
return nil, fmt.Errorf("sirikire %d", n)
}
err = m.PacketList.AddBytes(packetBuffer, packetLength)
if err != nil {
return nil, err
}
i++
}
return m, nil
}
func (m MPEG2TS) CheckStream() StreamCheckResult {
cr := StreamCheckResult{}
ci := map[PID]byte{}
dc := 0
for i := uint16(0); i < 0x2000; i++ {
ci[PID(i)] = byte(16)
}
for i, p := range m.PacketList.All() {
if p.PID == PID_NullPacket {
continue
}
// if ci[p.PID] == 16 {
// fmt.Printf("PID: %d ci: nil != pci: %d\r\n", p.PID, p.ContinuityCheckIndex)
// } else {
// fmt.Printf("PID: %d ci: %d != pci: %d\r\n", p.PID, (ci[p.PID]+1)%16, p.ContinuityCheckIndex)
// }
if ci[p.PID] == 16 {
// 初期値
if p.AdaptationFieldControl != 0 && p.AdaptationFieldControl != 2 {
ci[p.PID] = p.ContinuityCheckIndex
} else {
ci[p.PID] = 1
// fmt.Println("skip")
}
} else if (ci[p.PID]+1)%16 != p.ContinuityCheckIndex {
if p.AdaptationFieldControl != 0 && p.AdaptationFieldControl != 2 {
dc++
ci[p.PID] = p.ContinuityCheckIndex
cr.DropList = append(cr.DropList, struct {
Description string
Index int
}{"frame drop detected", i})
// fmt.Println("Continuity check error: index " + strconv.Itoa(i))
}
// fmt.Printf("PID: %d ci: %d != pci: %d\r\n", p.PID, (ci[p.PID]+1)%16, p.ContinuityCheckIndex)
// return errors.New("Continuity check error: index " + strconv.Itoa(i))
} else {
if p.AdaptationFieldControl != 0 && p.AdaptationFieldControl != 2 {
ci[p.PID] = p.ContinuityCheckIndex
} else {
// fmt.Println("skip")
}
}
}
cr.DropCount = dc
// fmt.Println("Drop frame:", dc)
return cr
}
func (m *MPEG2TS) FilterByPIDs(pids ...PID) *MPEG2TS {
mx := New(m.chunkSize)
for _, p := range m.PacketList.All() {
for _, id := range pids {
if p.PID == id {
// fmt.Println(p.Index)
// fmt.Printf("%#v\r\n", p.Data)
mx.AddPacket(p)
break
}
}
}
return mx
}