-
Notifications
You must be signed in to change notification settings - Fork 199
/
client_udp_listener.go
192 lines (157 loc) · 3.73 KB
/
client_udp_listener.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
package gortsplib
import (
"crypto/rand"
"math/big"
"net"
"strconv"
"sync/atomic"
"time"
"github.com/bluenviron/gortsplib/v4/pkg/multicast"
)
func int64Ptr(v int64) *int64 {
return &v
}
func randInRange(maxVal int) (int, error) {
b := big.NewInt(int64(maxVal + 1))
n, err := rand.Int(rand.Reader, b)
if err != nil {
return 0, err
}
return int(n.Int64()), nil
}
func allocateUDPListenerPair(c *Client) (*clientUDPListener, *clientUDPListener, error) {
// choose two consecutive ports in range 65535-10000
// RTP port must be even and RTCP port odd
for {
v, err := randInRange((65535 - 10000) / 2)
if err != nil {
return nil, nil, err
}
rtpPort := v*2 + 10000
rtcpPort := rtpPort + 1
rtpListener := &clientUDPListener{
c: c,
multicastEnable: false,
multicastSourceIP: nil,
address: net.JoinHostPort("", strconv.FormatInt(int64(rtpPort), 10)),
}
err = rtpListener.initialize()
if err != nil {
continue
}
rtcpListener := &clientUDPListener{
c: c,
multicastEnable: false,
multicastSourceIP: nil,
address: net.JoinHostPort("", strconv.FormatInt(int64(rtcpPort), 10)),
}
err = rtcpListener.initialize()
if err != nil {
rtpListener.close()
continue
}
return rtpListener, rtcpListener, nil
}
}
type packetConn interface {
net.PacketConn
SetReadBuffer(int) error
}
type clientUDPListener struct {
c *Client
multicastEnable bool
multicastSourceIP net.IP
address string
pc packetConn
readFunc readFunc
readIP net.IP
readPort int
writeAddr *net.UDPAddr
running bool
lastPacketTime *int64
done chan struct{}
}
func (u *clientUDPListener) initialize() error {
if u.multicastEnable {
intf, err := multicast.InterfaceForSource(u.multicastSourceIP)
if err != nil {
return err
}
u.pc, err = multicast.NewSingleConn(intf, u.address, u.c.ListenPacket)
if err != nil {
return err
}
} else {
tmp, err := u.c.ListenPacket(restrictNetwork("udp", u.address))
if err != nil {
return err
}
u.pc = tmp.(*net.UDPConn)
}
err := u.pc.SetReadBuffer(udpKernelReadBufferSize)
if err != nil {
u.pc.Close()
return err
}
u.lastPacketTime = int64Ptr(0)
return 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() {
u.running = true
u.pc.SetReadDeadline(time.Time{})
u.done = make(chan struct{})
go u.run()
}
func (u *clientUDPListener) stop() {
u.pc.SetReadDeadline(time.Now())
<-u.done
u.running = false
}
func (u *clientUDPListener) run() {
defer close(u.done)
var buf []byte
createNewBuffer := func() {
buf = make([]byte, udpMaxPayloadSize+1)
}
createNewBuffer()
for {
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.c.AnyPortEnable && u.readPort == 0 {
u.readPort = uaddr.Port
} else if u.readPort != uaddr.Port {
continue
}
now := u.c.timeNow()
atomic.StoreInt64(u.lastPacketTime, now.Unix())
atomic.AddUint64(u.c.BytesReceived, uint64(n))
if u.readFunc(buf[:n]) {
createNewBuffer()
}
}
}
func (u *clientUDPListener) write(payload []byte) error {
atomic.AddUint64(u.c.BytesSent, uint64(len(payload)))
// 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.c.WriteTimeout))
_, err := u.pc.WriteTo(payload, u.writeAddr)
return err
}