forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servermulticasthandler.go
94 lines (75 loc) · 1.63 KB
/
servermulticasthandler.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
package gortsplib
import (
"net"
"github.com/cobalt-robotics/gortsplib/pkg/ringbuffer"
)
type trackTypePayload struct {
trackID int
isRTP bool
payload []byte
}
type serverMulticastHandler struct {
rtpl *serverUDPListener
rtcpl *serverUDPListener
writeBuffer *ringbuffer.RingBuffer
writerDone chan struct{}
}
func newServerMulticastHandler(s *Server) (*serverMulticastHandler, error) {
rtpl, rtcpl, err := newServerUDPListenerMulticastPair(s)
if err != nil {
return nil, err
}
wb, _ := ringbuffer.New(uint64(s.WriteBufferCount))
h := &serverMulticastHandler{
rtpl: rtpl,
rtcpl: rtcpl,
writeBuffer: wb,
writerDone: make(chan struct{}),
}
go h.runWriter()
return h, nil
}
func (h *serverMulticastHandler) close() {
h.rtpl.close()
h.rtcpl.close()
h.writeBuffer.Close()
<-h.writerDone
}
func (h *serverMulticastHandler) ip() net.IP {
return h.rtpl.ip()
}
func (h *serverMulticastHandler) runWriter() {
defer close(h.writerDone)
rtpAddr := &net.UDPAddr{
IP: h.rtpl.ip(),
Port: h.rtpl.port(),
}
rtcpAddr := &net.UDPAddr{
IP: h.rtcpl.ip(),
Port: h.rtcpl.port(),
}
for {
tmp, ok := h.writeBuffer.Pull()
if !ok {
return
}
data := tmp.(trackTypePayload)
if data.isRTP {
h.rtpl.write(data.payload, rtpAddr)
} else {
h.rtcpl.write(data.payload, rtcpAddr)
}
}
}
func (h *serverMulticastHandler) writePacketRTP(payload []byte) {
h.writeBuffer.Push(trackTypePayload{
isRTP: true,
payload: payload,
})
}
func (h *serverMulticastHandler) writePacketRTCP(payload []byte) {
h.writeBuffer.Push(trackTypePayload{
isRTP: false,
payload: payload,
})
}