Files
gortsplib/servermulticasthandler.go
Alessandro Ros a1396206b5 convert Tracks into Medias and Formats (#155)
* split tracks from medias

* move tracks into dedicated package

* move media into dedicated package

* edit Medias.Marshal() in order to return SDP

* add medias.Find() and simplify examples

* improve coverage

* fix rebase errors

* replace TrackIDs with MediaIDs

* implement media-specific and track-specific callbacks for reading RTCP and RTP packets

* rename publish into record, read into play

* add v2 tag

* rename tracks into formats
2022-12-11 22:03:22 +01:00

94 lines
1.6 KiB
Go

package gortsplib
import (
"net"
"github.com/aler9/gortsplib/v2/pkg/ringbuffer"
)
type typeAndPayload struct {
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.(typeAndPayload)
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(typeAndPayload{
isRTP: true,
payload: payload,
})
}
func (h *serverMulticastHandler) writePacketRTCP(payload []byte) {
h.writeBuffer.Push(typeAndPayload{
isRTP: false,
payload: payload,
})
}