-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
110 lines (93 loc) · 1.79 KB
/
parser.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
package flv
import (
"io"
"net/http"
"os"
"sync"
)
type Parser struct {
closeOnce sync.Once
stop chan struct{}
avcHeader []byte
}
func NewParser() *Parser {
return &Parser{
stop: make(chan struct{}),
}
}
func (p *Parser) New() *Parser {
return &Parser{
stop: make(chan struct{}),
}
}
func (p *Parser) Stop() {
p.closeOnce.Do(func() {
close(p.stop)
})
}
func (p *Parser) Type() string {
return "flv"
}
func (p *Parser) Parse(streamUrl string, out string) (err error) {
req, err := http.NewRequest("GET", streamUrl, nil)
if err != nil {
return err
}
req.Header.Add("User-Agent", "Chrome/59.0.3071.115")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
d, err := NewDemuxer(resp.Body)
if err != nil {
return err
}
defer d.Close()
o, err := os.Create(out)
if err != nil {
return err
}
m, err := NewMuxer(o)
if err != nil {
return err
}
defer m.Close()
flvHeader := GetHeaderCompo()
d.ReadHeader(flvHeader)
m.WriteHeader(flvHeader)
flvHeader.Put()
for {
select {
case <-p.stop:
return nil
default:
flvBody := new(TagCompo)
err := d.ReadTag(flvBody)
if err != nil {
if err == io.EOF {
return nil
} else {
return err
}
}
// // todo(cg): fix video when having a better approach
// if flvBody.ContainAVCSeqHeader() {
// if p.avcHeader == nil {
// p.avcHeader = make([]byte, len(flvBody.TagBodyRaw))
// copy(p.avcHeader, flvBody.TagBodyRaw)
// } else {
// if bytes.Compare(p.avcHeader, flvBody.TagBodyRaw) == 0 {
// flvBody.Free()
// continue
// } else {
// flvBody.Free()
// return fmt.Errorf("video(name = %s) AVC sequence header changed", out)
// }
// }
// }
m.WriteTag(flvBody)
flvBody.Free()
}
}
}