mirror of
https://github.com/aler9/gortsplib
synced 2025-09-27 19:42:11 +08:00
fix race condition in WritePacketRTP() (#334)
This commit is contained in:
@@ -2,6 +2,7 @@ package gortsplib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pion/rtcp"
|
"github.com/pion/rtcp"
|
||||||
@@ -14,6 +15,28 @@ import (
|
|||||||
"github.com/bluenviron/gortsplib/v3/pkg/rtpreorderer"
|
"github.com/bluenviron/gortsplib/v3/pkg/rtpreorderer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// workaround until this gets tagged:
|
||||||
|
// https://github.com/pion/rtp/pull/234
|
||||||
|
func rtpPacketMarshalToSafe(p *rtp.Packet, buf []byte) (n int, err error) {
|
||||||
|
n, err = p.Header.MarshalTo(buf)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make sure the buffer is large enough to hold the packet.
|
||||||
|
if n+len(p.Payload)+int(p.PaddingSize) > len(buf) {
|
||||||
|
return 0, io.ErrShortBuffer
|
||||||
|
}
|
||||||
|
|
||||||
|
m := copy(buf[n:], p.Payload)
|
||||||
|
|
||||||
|
if p.Header.Padding {
|
||||||
|
buf[n+m+int(p.PaddingSize-1)] = p.PaddingSize
|
||||||
|
}
|
||||||
|
|
||||||
|
return n + m + int(p.PaddingSize), nil
|
||||||
|
}
|
||||||
|
|
||||||
type clientFormat struct {
|
type clientFormat struct {
|
||||||
c *Client
|
c *Client
|
||||||
cm *clientMedia
|
cm *clientMedia
|
||||||
@@ -76,7 +99,7 @@ func (ct *clientFormat) stop() {
|
|||||||
|
|
||||||
func (ct *clientFormat) writePacketRTPWithNTP(pkt *rtp.Packet, ntp time.Time) error {
|
func (ct *clientFormat) writePacketRTPWithNTP(pkt *rtp.Packet, ntp time.Time) error {
|
||||||
byts := make([]byte, udpMaxPayloadSize)
|
byts := make([]byte, udpMaxPayloadSize)
|
||||||
n, err := pkt.MarshalTo(byts)
|
n, err := rtpPacketMarshalToSafe(pkt, byts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@@ -69,7 +69,7 @@ func (sm *serverStreamMedia) allocateMulticastHandler(s *Server) error {
|
|||||||
|
|
||||||
func (sm *serverStreamMedia) WritePacketRTPWithNTP(ss *ServerStream, pkt *rtp.Packet, ntp time.Time) {
|
func (sm *serverStreamMedia) WritePacketRTPWithNTP(ss *ServerStream, pkt *rtp.Packet, ntp time.Time) {
|
||||||
byts := make([]byte, udpMaxPayloadSize)
|
byts := make([]byte, udpMaxPayloadSize)
|
||||||
n, err := pkt.MarshalTo(byts)
|
n, err := rtpPacketMarshalToSafe(pkt, byts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user