-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransport_udp.go
191 lines (152 loc) · 3.02 KB
/
transport_udp.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
package rtsp
import (
"errors"
"fmt"
"math/rand"
"net"
"sync"
)
type conn struct {
mediaID int
rtpConn *net.UDPConn
rtcpConn *net.UDPConn
lock sync.Mutex
}
func (c *conn) loopRTP(wg *sync.WaitGroup, handler MediaHandler, onError func(error)) {
defer wg.Done()
buf := make([]byte, 0x10000)
for {
n, _, err := c.rtpConn.ReadFromUDP(buf)
if err != nil {
if errors.Is(err, net.ErrClosed) {
return
}
c.lock.Lock()
c.rtpConn.Close()
c.rtpConn = nil
c.lock.Unlock()
onError(fmt.Errorf("read rtp: %w", err))
return
}
handler.OnRTP(c.mediaID, buf[:n])
}
}
func (c *conn) loopRTCP(wg *sync.WaitGroup, handler MediaHandler, onError func(error)) {
defer wg.Done()
buf := make([]byte, 0x800)
for {
n, _, err := c.rtcpConn.ReadFromUDP(buf)
if err != nil {
if errors.Is(err, net.ErrClosed) {
return
}
c.lock.Lock()
c.rtcpConn.Close()
c.rtcpConn = nil
c.lock.Unlock()
onError(fmt.Errorf("read rtcp: %w", err))
return
}
handler.OnRTCP(c.mediaID, buf[:n])
}
}
func (c *conn) start(wg *sync.WaitGroup, handler MediaHandler, onError func(error)) {
wg.Add(2)
go c.loopRTP(wg, handler, onError)
go c.loopRTCP(wg, handler, onError)
}
func (c *conn) close() {
c.lock.Lock()
defer c.lock.Unlock()
if c.rtpConn != nil {
c.rtpConn.Close()
c.rtpConn = nil
}
if c.rtcpConn != nil {
c.rtcpConn.Close()
c.rtcpConn = nil
}
}
type TransportUDP struct {
connList []*conn
onceClose sync.Once
onceError sync.Once
err chan error
}
func NewTransportUDP() *TransportUDP {
return &TransportUDP{
err: make(chan error, 1),
}
}
// Setup prepares the transport and returns the transport parameters.
func (t *TransportUDP) Setup(mediaID int) (string, error) {
a := &net.UDPAddr{
IP: net.IPv4zero,
}
var (
err error
rtpPort, rtcpPort int
rtpConn, rtcpConn *net.UDPConn
)
const (
minPort = 10000
maxPort = 65000
)
for {
// rtpPort random number in range 10000-65000
rtpPort = minPort + (rand.Intn(maxPort - minPort))
// rtpPort should be even
rtpPort &^= 1
rtcpPort = rtpPort + 1
a.Port = rtpPort
rtpConn, err = net.ListenUDP("udp", a)
if err != nil {
continue
}
a.Port = rtcpPort
rtcpConn, err = net.ListenUDP("udp", a)
if err != nil {
rtpConn.Close()
continue
}
break
}
c := &conn{
mediaID: mediaID,
rtpConn: rtpConn,
rtcpConn: rtcpConn,
}
t.connList = append(t.connList, c)
transport := fmt.Sprintf(
"RTP/AVP;unicast;client_port=%d-%d",
rtpPort,
rtcpPort,
)
return transport, nil
}
// Play starts receigin RTP/RTCP packets.
func (t *TransportUDP) Play(handler MediaHandler) {
var wg sync.WaitGroup
for _, c := range t.connList {
c.start(&wg, handler, t.onError)
}
go func() {
wg.Wait()
t.onError(nil)
}()
}
func (t *TransportUDP) Close() {
t.onceClose.Do(func() {
for _, c := range t.connList {
c.close()
}
})
}
func (t *TransportUDP) onError(err error) {
t.onceError.Do(func() {
t.err <- err
})
}
func (t *TransportUDP) Err() <-chan error {
return t.err
}