forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_udpl.go
211 lines (179 loc) · 4.05 KB
/
client_udpl.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
package gortsplib
import (
"crypto/rand"
"math/big"
"net"
"strconv"
"sync/atomic"
"time"
"golang.org/x/net/ipv4"
)
func randInRange(max int) int {
b := big.NewInt(int64(max + 1))
n, _ := rand.Int(rand.Reader, b)
return int(n.Int64())
}
type clientUDPListener struct {
anyPortEnable bool
writeTimeout time.Duration
pc *net.UDPConn
cm *clientMedia
isRTP bool
readIP net.IP
readPort int
writeAddr *net.UDPAddr
running bool
lastPacketTime *int64
readerDone chan struct{}
}
func newClientUDPListenerPair(
listenPacket func(network, address string) (net.PacketConn, error),
anyPortEnable bool,
writeTimeout time.Duration,
cm *clientMedia,
) (*clientUDPListener, *clientUDPListener) {
// choose two consecutive ports in range 65535-10000
// RTP port must be even and RTCP port odd
for {
rtpPort := randInRange((65535-10000)/2)*2 + 10000
rtpListener, err := newClientUDPListener(
listenPacket,
anyPortEnable,
writeTimeout,
false,
":"+strconv.FormatInt(int64(rtpPort), 10),
cm,
true)
if err != nil {
continue
}
rtcpPort := rtpPort + 1
rtcpListener, err := newClientUDPListener(
listenPacket,
anyPortEnable,
writeTimeout,
false,
":"+strconv.FormatInt(int64(rtcpPort), 10),
cm,
false)
if err != nil {
rtpListener.close()
continue
}
return rtpListener, rtcpListener
}
}
func newClientUDPListener(
listenPacket func(network, address string) (net.PacketConn, error),
anyPortEnable bool,
writeTimeout time.Duration,
multicast bool,
address string,
cm *clientMedia,
isRTP bool,
) (*clientUDPListener, error) {
var pc *net.UDPConn
if multicast {
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
tmp, err := listenPacket("udp", "224.0.0.0:"+port)
if err != nil {
return nil, err
}
p := ipv4.NewPacketConn(tmp)
err = p.SetMulticastTTL(multicastTTL)
if err != nil {
return nil, err
}
intfs, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, intf := range intfs {
err := p.JoinGroup(&intf, &net.UDPAddr{IP: net.ParseIP(host)})
if err != nil {
return nil, err
}
}
pc = tmp.(*net.UDPConn)
} else {
tmp, err := listenPacket("udp", address)
if err != nil {
return nil, err
}
pc = tmp.(*net.UDPConn)
}
err := pc.SetReadBuffer(udpKernelReadBufferSize)
if err != nil {
return nil, err
}
return &clientUDPListener{
anyPortEnable: anyPortEnable,
writeTimeout: writeTimeout,
pc: pc,
cm: cm,
isRTP: isRTP,
lastPacketTime: func() *int64 {
v := int64(0)
return &v
}(),
}, nil
}
func (u *clientUDPListener) close() {
if u.running {
u.stop()
}
u.pc.Close()
}
func (u *clientUDPListener) port() int {
return u.pc.LocalAddr().(*net.UDPAddr).Port
}
func (u *clientUDPListener) start(forPlay bool) {
u.running = true
u.pc.SetReadDeadline(time.Time{})
u.readerDone = make(chan struct{})
go u.runReader(forPlay)
}
func (u *clientUDPListener) stop() {
u.pc.SetReadDeadline(time.Now())
<-u.readerDone
}
func (u *clientUDPListener) runReader(forPlay bool) {
defer close(u.readerDone)
var readFunc func([]byte) error
if u.isRTP {
readFunc = u.cm.readRTP
} else {
readFunc = u.cm.readRTCP
}
for {
buf := make([]byte, maxPacketSize+1)
n, addr, err := u.pc.ReadFrom(buf)
if err != nil {
return
}
uaddr := addr.(*net.UDPAddr)
if !u.readIP.Equal(uaddr.IP) {
continue
}
// in case of anyPortEnable, store the port of the first packet we receive.
// this reduces security issues
if u.anyPortEnable && u.readPort == 0 {
u.readPort = uaddr.Port
} else if u.readPort != uaddr.Port {
continue
}
now := time.Now()
atomic.StoreInt64(u.lastPacketTime, now.Unix())
readFunc(buf[:n])
}
}
func (u *clientUDPListener) write(payload []byte) error {
// no mutex is needed here since Write() has an internal lock.
// https://github.com/golang/go/issues/27203#issuecomment-534386117
u.pc.SetWriteDeadline(time.Now().Add(u.writeTimeout))
_, err := u.pc.WriteTo(payload, u.writeAddr)
return err
}