forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn-client-udpl.go
54 lines (45 loc) · 1.14 KB
/
conn-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
package gortsplib
import (
"net"
"strconv"
)
// ConnClientUdpListener is a UDP listener created by SetupUDP() to receive UDP frames.
type ConnClientUdpListener struct {
c *ConnClient
pc net.PacketConn
trackId int
streamType StreamType
publisherIp net.IP
publisherPort int
}
func newConnClientUdpListener(c *ConnClient, port int, trackId int, streamType StreamType) (*ConnClientUdpListener, error) {
pc, err := c.conf.ListenPacket("udp", ":"+strconv.FormatInt(int64(port), 10))
if err != nil {
return nil, err
}
return &ConnClientUdpListener{
c: c,
pc: pc,
trackId: trackId,
streamType: streamType,
}, nil
}
// Close closes the listener.
func (l *ConnClientUdpListener) Close() {
l.pc.Close()
}
// Read reads a frame from the publisher.
func (l *ConnClientUdpListener) Read(buf []byte) (int, error) {
for {
n, addr, err := l.pc.ReadFrom(buf)
if err != nil {
return 0, err
}
uaddr := addr.(*net.UDPAddr)
if !l.publisherIp.Equal(uaddr.IP) || l.publisherPort != uaddr.Port {
continue
}
l.c.rtcpReceivers[l.trackId].OnFrame(l.streamType, buf[:n])
return n, nil
}
}