forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_multicast_writer.go
108 lines (88 loc) · 1.86 KB
/
server_multicast_writer.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
package gortsplib
import (
"fmt"
"net"
"github.com/bluenviron/gortsplib/v3/pkg/ringbuffer"
)
type typeAndPayload struct {
isRTP bool
payload []byte
}
type serverMulticastWriter struct {
rtpl *serverUDPListener
rtcpl *serverUDPListener
writeBuffer *ringbuffer.RingBuffer
writerDone chan struct{}
}
func newServerMulticastWriter(s *Server) (*serverMulticastWriter, error) {
res := make(chan net.IP)
select {
case s.streamMulticastIP <- streamMulticastIPReq{res: res}:
case <-s.ctx.Done():
return nil, fmt.Errorf("terminated")
}
ip := <-res
rtpl, rtcpl, err := newServerUDPListenerMulticastPair(
s.ListenPacket,
s.WriteTimeout,
s.MulticastRTPPort,
s.MulticastRTCPPort,
ip,
)
if err != nil {
return nil, err
}
wb, _ := ringbuffer.New(uint64(s.WriteBufferCount))
h := &serverMulticastWriter{
rtpl: rtpl,
rtcpl: rtcpl,
writeBuffer: wb,
writerDone: make(chan struct{}),
}
go h.runWriter()
return h, nil
}
func (h *serverMulticastWriter) close() {
h.rtpl.close()
h.rtcpl.close()
h.writeBuffer.Close()
<-h.writerDone
}
func (h *serverMulticastWriter) ip() net.IP {
return h.rtpl.ip()
}
func (h *serverMulticastWriter) 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.(typeAndPayload)
if data.isRTP {
h.rtpl.write(data.payload, rtpAddr)
} else {
h.rtcpl.write(data.payload, rtcpAddr)
}
}
}
func (h *serverMulticastWriter) writePacketRTP(payload []byte) {
h.writeBuffer.Push(typeAndPayload{
isRTP: true,
payload: payload,
})
}
func (h *serverMulticastWriter) writePacketRTCP(payload []byte) {
h.writeBuffer.Push(typeAndPayload{
isRTP: false,
payload: payload,
})
}