mediamtx/internal/core/stream.go

107 lines
2.1 KiB
Go

package core
import (
"sync"
"github.com/aler9/gortsplib"
"github.com/pion/rtcp"
"github.com/pion/rtp/v2"
)
type streamNonRTSPReadersMap struct {
mutex sync.RWMutex
ma map[reader]struct{}
}
func newStreamNonRTSPReadersMap() *streamNonRTSPReadersMap {
return &streamNonRTSPReadersMap{
ma: make(map[reader]struct{}),
}
}
func (m *streamNonRTSPReadersMap) close() {
m.mutex.Lock()
defer m.mutex.Unlock()
m.ma = nil
}
func (m *streamNonRTSPReadersMap) add(r reader) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.ma[r] = struct{}{}
}
func (m *streamNonRTSPReadersMap) remove(r reader) {
m.mutex.Lock()
defer m.mutex.Unlock()
delete(m.ma, r)
}
func (m *streamNonRTSPReadersMap) forwardPacketRTP(trackID int, pkt *rtp.Packet) {
m.mutex.RLock()
defer m.mutex.RUnlock()
for c := range m.ma {
c.onReaderPacketRTP(trackID, pkt)
}
}
func (m *streamNonRTSPReadersMap) forwardPacketRTCP(trackID int, pkt rtcp.Packet) {
m.mutex.RLock()
defer m.mutex.RUnlock()
for c := range m.ma {
c.onReaderPacketRTCP(trackID, pkt)
}
}
type stream struct {
nonRTSPReaders *streamNonRTSPReadersMap
rtspStream *gortsplib.ServerStream
}
func newStream(tracks gortsplib.Tracks) *stream {
s := &stream{
nonRTSPReaders: newStreamNonRTSPReadersMap(),
rtspStream: gortsplib.NewServerStream(tracks),
}
return s
}
func (s *stream) close() {
s.nonRTSPReaders.close()
s.rtspStream.Close()
}
func (s *stream) tracks() gortsplib.Tracks {
return s.rtspStream.Tracks()
}
func (s *stream) readerAdd(r reader) {
if _, ok := r.(pathRTSPSession); !ok {
s.nonRTSPReaders.add(r)
}
}
func (s *stream) readerRemove(r reader) {
if _, ok := r.(pathRTSPSession); !ok {
s.nonRTSPReaders.remove(r)
}
}
func (s *stream) writePacketRTP(trackID int, pkt *rtp.Packet) {
// forward to RTSP readers
s.rtspStream.WritePacketRTP(trackID, pkt)
// forward to non-RTSP readers
s.nonRTSPReaders.forwardPacketRTP(trackID, pkt)
}
func (s *stream) writePacketRTCP(trackID int, pkt rtcp.Packet) {
// forward to RTSP readers
s.rtspStream.WritePacketRTCP(trackID, pkt)
// forward to non-RTSP readers
s.nonRTSPReaders.forwardPacketRTCP(trackID, pkt)
}