mirror of
https://github.com/aler9/gortsplib
synced 2025-10-04 06:46:42 +08:00

RTP packets were previously take from a buffer pool. This was messing up the Client, since that buffer pool was used by multiple routines at once, and was probably messing up the Server too, since packets can be pushed to different queues and there's no guarantee that these queues have an overall size less than ReadBufferCount. This buffer pool is removed; this decreases performance but avoids bugs.
93 lines
1.8 KiB
Go
93 lines
1.8 KiB
Go
package gortsplib
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/pion/rtcp"
|
|
"github.com/pion/rtp"
|
|
|
|
"github.com/aler9/gortsplib/v2/pkg/media"
|
|
)
|
|
|
|
type serverStreamMedia struct {
|
|
media *media.Media
|
|
formats map[uint8]*serverStreamFormat
|
|
multicastHandler *serverMulticastHandler
|
|
}
|
|
|
|
func newServerStreamMedia(medi *media.Media) *serverStreamMedia {
|
|
return &serverStreamMedia{
|
|
media: medi,
|
|
}
|
|
}
|
|
|
|
func (sm *serverStreamMedia) close() {
|
|
for _, tr := range sm.formats {
|
|
if tr.rtcpSender != nil {
|
|
tr.rtcpSender.Close()
|
|
}
|
|
}
|
|
|
|
if sm.multicastHandler != nil {
|
|
sm.multicastHandler.close()
|
|
}
|
|
}
|
|
|
|
func (sm *serverStreamMedia) allocateMulticastHandler(s *Server) error {
|
|
if sm.multicastHandler == nil {
|
|
mh, err := newServerMulticastHandler(s)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sm.multicastHandler = mh
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (sm *serverStreamMedia) WritePacketRTPWithNTP(ss *ServerStream, pkt *rtp.Packet, ntp time.Time) {
|
|
byts := make([]byte, maxPacketSize)
|
|
n, err := pkt.MarshalTo(byts)
|
|
if err != nil {
|
|
return
|
|
}
|
|
byts = byts[:n]
|
|
|
|
forma := sm.formats[pkt.PayloadType]
|
|
|
|
forma.rtcpSender.ProcessPacket(pkt, ntp, forma.format.PTSEqualsDTS(pkt))
|
|
|
|
// send unicast
|
|
for r := range ss.activeUnicastReaders {
|
|
sm, ok := r.setuppedMedias[sm.media]
|
|
if ok {
|
|
sm.writePacketRTP(byts)
|
|
}
|
|
}
|
|
|
|
// send multicast
|
|
if sm.multicastHandler != nil {
|
|
sm.multicastHandler.writePacketRTP(byts)
|
|
}
|
|
}
|
|
|
|
func (sm *serverStreamMedia) writePacketRTCP(ss *ServerStream, pkt rtcp.Packet) {
|
|
byts, err := pkt.Marshal()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// send unicast
|
|
for r := range ss.activeUnicastReaders {
|
|
sm, ok := r.setuppedMedias[sm.media]
|
|
if ok {
|
|
sm.writePacketRTCP(byts)
|
|
}
|
|
}
|
|
|
|
// send multicast
|
|
if sm.multicastHandler != nil {
|
|
sm.multicastHandler.writePacketRTCP(byts)
|
|
}
|
|
}
|