/
example.go
156 lines (135 loc) · 2.99 KB
/
example.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
package main
import (
"log"
"os"
"github.com/nareix/mp4"
)
var (
VideoWidth int
VideoHeight int
)
func main() {
RtspReader := RtspClientNew()
quit := false
sps := []byte{}
pps := []byte{}
fuBuffer := []byte{}
syncCount := 0
// rtp timestamp: 90 kHz clock rate
// 1 sec = timestamp 90000
timeScale := 90000
type NALU struct {
ts int
data []byte
sync bool
}
var lastNALU *NALU
var mp4w *mp4.SimpleH264Writer
outfile, _ := os.Create("out.mp4")
endWriteNALU := func() {
log.Println("finish write")
if mp4w != nil {
if err := mp4w.Finish(); err != nil {
panic(err)
}
}
}
writeNALU := func(sync bool, ts int, payload []byte) {
if mp4w == nil {
mp4w = &mp4.SimpleH264Writer{
SPS: sps,
PPS: pps,
TimeScale: timeScale,
W: outfile,
Width: VideoWidth,
Height: VideoHeight,
}
//log.Println("SPS:\n"+hex.Dump(sps), "\nPPS:\n"+hex.Dump(pps))
}
curNALU := &NALU{
ts: ts,
sync: sync,
data: payload,
}
if lastNALU != nil {
log.Println("write", lastNALU.sync, len(lastNALU.data))
if err := mp4w.WriteNALU(lastNALU.sync, curNALU.ts - lastNALU.ts, lastNALU.data); err != nil {
panic(err)
}
}
lastNALU = curNALU
}
handleNALU := func(nalType byte, payload []byte, ts int64) {
if nalType == 7 {
if len(sps) == 0 {
sps = payload
}
} else if nalType == 8 {
if len(pps) == 0 {
pps = payload
}
} else if nalType == 5 {
// keyframe
syncCount++
if syncCount == 5 {
quit = true
}
writeNALU(true, int(ts), payload)
} else {
// non-keyframe
if syncCount > 0 {
writeNALU(false, int(ts), payload)
}
}
}
if status, message := RtspReader.Client("rtsp://admin:123456@94.242.52.34:5543/mpeg4cif"); status {
log.Println("connected")
i := 0
for {
i++
//read 100 frame and exit loop
if quit {
break
}
select {
case data := <-RtspReader.outgoing:
//log.Println("packet recive")
if data[0] == 36 && data[1] == 0 {
cc := data[4] & 0xF
//rtp header
rtphdr := 12 + cc*4
//packet time
ts := (int64(data[8]) << 24) + (int64(data[9]) << 16) + (int64(data[10]) << 8) + (int64(data[11]))
//packet number
packno := (int64(data[6]) << 8) + int64(data[7])
if false {
log.Println("packet num", packno)
}
nalType := data[4+rtphdr] & 0x1F
if nalType >= 1 && nalType <= 23 {
handleNALU(nalType, data[4+rtphdr:], ts)
} else if nalType == 28 {
isStart := data[4+rtphdr+1]&0x80 != 0
isEnd := data[4+rtphdr+1]&0x40 != 0
nalType := data[4+rtphdr+1]&0x1F
nri := (data[4+rtphdr+1]&0x60)>>5
if isStart {
fuBuffer = []byte{0}
}
fuBuffer = append(fuBuffer, data[4+rtphdr+2:]...)
if isEnd {
fuBuffer[0] = nalType|(nri<<5)|0x80
handleNALU(nalType, fuBuffer, ts)
}
}
}
case <-RtspReader.signals:
log.Println("exit signal by class rtsp")
}
}
} else {
log.Println("error", message)
}
endWriteNALU()
RtspReader.Close()
}