forked from EasyDarwin/EasyDarwin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp-server.go
284 lines (271 loc) · 6.98 KB
/
udp-server.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package rtsp
import (
"bytes"
"fmt"
"log"
"net"
"strconv"
"strings"
"time"
"github.com/penggy/EasyGoLib/utils"
)
type UDPServer struct {
*Session
*RTSPClient
APort int
AConn *net.UDPConn
AControlPort int
AControlConn *net.UDPConn
VPort int
VConn *net.UDPConn
VControlPort int
VControlConn *net.UDPConn
Stoped bool
}
func (s *UDPServer) AddInputBytes(bytes int) {
if s.Session != nil {
s.Session.InBytes += bytes
return
}
if s.RTSPClient != nil {
s.RTSPClient.InBytes += bytes
return
}
panic(fmt.Errorf("session and RTSPClient both nil"))
}
func (s *UDPServer) HandleRTP(pack *RTPPack) {
if s.Session != nil {
for _, v := range s.Session.RTPHandles {
v(pack)
}
return
}
if s.RTSPClient != nil {
for _, v := range s.RTSPClient.RTPHandles {
v(pack)
}
return
}
panic(fmt.Errorf("session and RTSPClient both nil"))
}
func (s *UDPServer) Logger() *log.Logger {
if s.Session != nil {
return s.Session.logger
}
if s.RTSPClient != nil {
return s.RTSPClient.logger
}
panic(fmt.Errorf("session and RTSPClient both nil"))
}
func (s *UDPServer) Stop() {
if s.Stoped {
return
}
s.Stoped = true
if s.AConn != nil {
s.AConn.Close()
s.AConn = nil
}
if s.AControlConn != nil {
s.AControlConn.Close()
s.AControlConn = nil
}
if s.VConn != nil {
s.VConn.Close()
s.VConn = nil
}
if s.VControlConn != nil {
s.VControlConn.Close()
s.VControlConn = nil
}
}
func (s *UDPServer) SetupAudio() (err error) {
var (
logger = s.Logger()
addr *net.UDPAddr
)
if addr, err = net.ResolveUDPAddr("udp", ":0"); err != nil {
return
}
if s.AConn, err = net.ListenUDP("udp", addr); err != nil {
return
}
networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576)
if err = s.AConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio conn set read buffer error, %v", err)
}
if err = s.AConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio conn set write buffer error, %v", err)
}
la := s.AConn.LocalAddr().String()
strPort := la[strings.LastIndex(la, ":")+1:]
s.APort, err = strconv.Atoi(strPort)
if err != nil {
return
}
go func() {
bufUDP := make([]byte, UDP_BUF_SIZE)
logger.Printf("udp server start listen audio port[%d]", s.APort)
defer logger.Printf("udp server stop listen audio port[%d]", s.APort)
timer := time.Unix(0, 0)
for !s.Stoped {
if n, _, err := s.AConn.ReadFromUDP(bufUDP); err == nil {
elapsed := time.Now().Sub(timer)
if elapsed >= 30*time.Second {
logger.Printf("Package recv from AConn.len:%d\n", n)
timer = time.Now()
}
rtpBytes := make([]byte, n)
s.AddInputBytes(n)
copy(rtpBytes, bufUDP)
pack := &RTPPack{
Type: RTP_TYPE_AUDIO,
Buffer: bytes.NewBuffer(rtpBytes),
}
s.HandleRTP(pack)
} else {
logger.Println("udp server read audio pack error", err)
continue
}
}
}()
addr, err = net.ResolveUDPAddr("udp", ":0")
if err != nil {
return
}
s.AControlConn, err = net.ListenUDP("udp", addr)
if err != nil {
return
}
if err = s.AControlConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio control conn set read buffer error, %v", err)
}
if err = s.AControlConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server audio control conn set write buffer error, %v", err)
}
la = s.AControlConn.LocalAddr().String()
strPort = la[strings.LastIndex(la, ":")+1:]
s.AControlPort, err = strconv.Atoi(strPort)
if err != nil {
return
}
go func() {
bufUDP := make([]byte, UDP_BUF_SIZE)
logger.Printf("udp server start listen audio control port[%d]", s.AControlPort)
defer logger.Printf("udp server stop listen audio control port[%d]", s.AControlPort)
for !s.Stoped {
if n, _, err := s.AControlConn.ReadFromUDP(bufUDP); err == nil {
//logger.Printf("Package recv from AControlConn.len:%d\n", n)
rtpBytes := make([]byte, n)
s.AddInputBytes(n)
copy(rtpBytes, bufUDP)
pack := &RTPPack{
Type: RTP_TYPE_AUDIOCONTROL,
Buffer: bytes.NewBuffer(rtpBytes),
}
s.HandleRTP(pack)
} else {
logger.Println("udp server read audio control pack error", err)
continue
}
}
}()
return
}
func (s *UDPServer) SetupVideo() (err error) {
var (
logger = s.Logger()
addr *net.UDPAddr
)
addr, err = net.ResolveUDPAddr("udp", ":0")
if err != nil {
return
}
s.VConn, err = net.ListenUDP("udp", addr)
if err != nil {
return
}
networkBuffer := utils.Conf().Section("rtsp").Key("network_buffer").MustInt(1048576)
if err = s.VConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server video conn set read buffer error, %v", err)
}
if err = s.VConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server video conn set write buffer error, %v", err)
}
la := s.VConn.LocalAddr().String()
strPort := la[strings.LastIndex(la, ":")+1:]
s.VPort, err = strconv.Atoi(strPort)
if err != nil {
return
}
go func() {
bufUDP := make([]byte, UDP_BUF_SIZE)
logger.Printf("udp server start listen video port[%d]", s.VPort)
defer logger.Printf("udp server stop listen video port[%d]", s.VPort)
timer := time.Unix(0, 0)
for !s.Stoped {
var n int
if n, _, err = s.VConn.ReadFromUDP(bufUDP); err == nil {
elapsed := time.Now().Sub(timer)
if elapsed >= 30*time.Second {
logger.Printf("Package recv from VConn.len:%d\n", n)
timer = time.Now()
}
rtpBytes := make([]byte, n)
s.AddInputBytes(n)
copy(rtpBytes, bufUDP)
pack := &RTPPack{
Type: RTP_TYPE_VIDEO,
Buffer: bytes.NewBuffer(rtpBytes),
}
s.HandleRTP(pack)
} else {
logger.Println("udp server read video pack error", err)
continue
}
}
}()
addr, err = net.ResolveUDPAddr("udp", ":0")
if err != nil {
return
}
s.VControlConn, err = net.ListenUDP("udp", addr)
if err != nil {
return
}
if err = s.VControlConn.SetReadBuffer(networkBuffer); err != nil {
logger.Printf("udp server video control conn set read buffer error, %v", err)
}
if err = s.VControlConn.SetWriteBuffer(networkBuffer); err != nil {
logger.Printf("udp server video control conn set write buffer error, %v", err)
}
la = s.VControlConn.LocalAddr().String()
strPort = la[strings.LastIndex(la, ":")+1:]
s.VControlPort, err = strconv.Atoi(strPort)
if err != nil {
return
}
go func() {
bufUDP := make([]byte, UDP_BUF_SIZE)
logger.Printf("udp server start listen video control port[%d]", s.VControlPort)
defer logger.Printf("udp server stop listen video control port[%d]", s.VControlPort)
for !s.Stoped {
var n int
if n, _, err = s.VControlConn.ReadFromUDP(bufUDP); err == nil {
//logger.Printf("Package recv from VControlConn.len:%d\n", n)
rtpBytes := make([]byte, n)
s.AddInputBytes(n)
copy(rtpBytes, bufUDP)
pack := &RTPPack{
Type: RTP_TYPE_VIDEOCONTROL,
Buffer: bytes.NewBuffer(rtpBytes),
}
s.HandleRTP(pack)
} else {
logger.Println("udp server read video control pack error", err)
continue
}
}
}()
return
}