mediamtx/serverudp.go

114 lines
2.1 KiB
Go
Raw Normal View History

2019-12-31 12:48:17 +00:00
package main
import (
"net"
"sync/atomic"
2020-01-26 17:08:15 +00:00
"time"
2020-07-12 20:53:22 +00:00
"github.com/aler9/gortsplib"
2020-10-05 19:07:34 +00:00
"github.com/aler9/gortsplib/multibuffer"
2019-12-31 12:48:17 +00:00
)
2020-09-20 12:17:36 +00:00
const (
udpReadBufferSize = 2048
)
type udpBufAddrPair struct {
2020-01-26 17:08:15 +00:00
buf []byte
addr *net.UDPAddr
2020-01-26 17:08:15 +00:00
}
2020-09-05 11:19:55 +00:00
type serverUDP struct {
2020-07-12 20:53:22 +00:00
p *program
pc *net.UDPConn
2020-07-12 20:53:22 +00:00
streamType gortsplib.StreamType
2020-10-05 19:07:34 +00:00
readBuf *multibuffer.MultiBuffer
2020-06-27 11:38:35 +00:00
writec chan udpBufAddrPair
done chan struct{}
2019-12-31 12:48:17 +00:00
}
2020-09-05 11:19:55 +00:00
func newServerUDP(p *program, port int, streamType gortsplib.StreamType) (*serverUDP, error) {
pc, err := net.ListenUDP("udp", &net.UDPAddr{
2019-12-31 12:48:17 +00:00
Port: port,
})
if err != nil {
return nil, err
}
2020-09-05 11:19:55 +00:00
l := &serverUDP{
2020-07-12 20:53:22 +00:00
p: p,
pc: pc,
2020-07-12 20:53:22 +00:00
streamType: streamType,
2020-10-05 19:07:34 +00:00
readBuf: multibuffer.New(2, udpReadBufferSize),
writec: make(chan udpBufAddrPair),
2020-07-12 20:53:22 +00:00
done: make(chan struct{}),
2019-12-31 12:48:17 +00:00
}
l.log("opened on :%d", port)
return l, nil
}
2020-09-05 11:19:55 +00:00
func (l *serverUDP) log(format string, args ...interface{}) {
2019-12-31 12:48:17 +00:00
var label string
2020-07-12 20:53:22 +00:00
if l.streamType == gortsplib.StreamTypeRtp {
2019-12-31 12:48:17 +00:00
label = "RTP"
} else {
label = "RTCP"
}
2020-09-19 13:23:12 +00:00
l.p.log("[UDP/"+label+" server] "+format, args...)
2019-12-31 12:48:17 +00:00
}
2020-09-05 11:19:55 +00:00
func (l *serverUDP) run() {
writeDone := make(chan struct{})
2020-01-26 17:08:15 +00:00
go func() {
defer close(writeDone)
for w := range l.writec {
l.pc.SetWriteDeadline(time.Now().Add(l.p.conf.WriteTimeout))
l.pc.WriteTo(w.buf, w.addr)
2020-05-10 13:38:01 +00:00
}
}()
for {
2020-09-20 12:17:36 +00:00
buf := l.readBuf.Next()
n, addr, err := l.pc.ReadFromUDP(buf)
2020-05-10 13:38:01 +00:00
if err != nil {
break
}
2019-12-31 12:48:17 +00:00
pub := l.p.udpPublishersMap.get(makeUDPPublisherAddr(addr.IP, addr.Port))
if pub == nil {
continue
}
// client sent RTP on RTCP port or vice-versa
if pub.streamType != l.streamType {
continue
2020-06-27 11:38:35 +00:00
}
atomic.StoreInt64(pub.client.udpLastFrameTimes[pub.trackId], time.Now().Unix())
pub.client.rtcpReceivers[pub.trackId].OnFrame(l.streamType, buf[:n])
l.p.readersMap.forwardFrame(pub.client.path,
pub.trackId,
l.streamType,
buf[:n])
2020-05-10 13:38:01 +00:00
}
close(l.writec)
<-writeDone
2020-05-10 13:38:01 +00:00
2020-05-10 14:23:57 +00:00
close(l.done)
2020-05-10 13:38:01 +00:00
}
2020-09-05 11:19:55 +00:00
func (l *serverUDP) close() {
l.pc.Close()
2020-05-10 13:38:01 +00:00
<-l.done
2019-12-31 12:48:17 +00:00
}
2020-09-05 11:19:55 +00:00
func (l *serverUDP) write(data []byte, addr *net.UDPAddr) {
l.writec <- udpBufAddrPair{data, addr}
}