mirror of
https://github.com/aler9/gortsplib
synced 2025-09-27 11:32:08 +08:00
set SSRC of outgoing packets (#803)
In client and server, each format now has a fixed, unique, known in advance SSRC, that is applied to outgoing packets belonging to each format. This is needed to support SRTP/MIKEY, that require each format to have a fixed, unique, and known in advance SSRC. A secondary effect is that SETUP responses now always contain SSRCs of each format, regardless of the fact that the first packet has been produced or not (previously we needed at least one packet, from which the SSRC was extracted).
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package gortsplib
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
@@ -11,15 +12,55 @@ import (
|
||||
"github.com/bluenviron/gortsplib/v4/pkg/rtcpsender"
|
||||
)
|
||||
|
||||
func randUint32() (uint32, error) {
|
||||
var b [4]byte
|
||||
_, err := rand.Read(b[:])
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]), nil
|
||||
}
|
||||
|
||||
func isServerStreamLocalSSRCTaken(ssrc uint32, stream *ServerStream, exclude *serverStreamFormat) bool {
|
||||
for _, sm := range stream.medias {
|
||||
for _, sf := range sm.formats {
|
||||
if sf != exclude && sf.localSSRC == ssrc {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func serverStreamPickLocalSSRC(sf *serverStreamFormat) (uint32, error) {
|
||||
for {
|
||||
ssrc, err := randUint32()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if ssrc != 0 && !isServerStreamLocalSSRCTaken(ssrc, sf.sm.st, sf) {
|
||||
return ssrc, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type serverStreamFormat struct {
|
||||
sm *serverStreamMedia
|
||||
format format.Format
|
||||
|
||||
localSSRC uint32
|
||||
rtcpSender *rtcpsender.RTCPSender
|
||||
rtpPacketsSent *uint64
|
||||
}
|
||||
|
||||
func (sf *serverStreamFormat) initialize() {
|
||||
func (sf *serverStreamFormat) initialize() error {
|
||||
var err error
|
||||
sf.localSSRC, err = serverStreamPickLocalSSRC(sf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sf.rtpPacketsSent = new(uint64)
|
||||
|
||||
sf.rtcpSender = &rtcpsender.RTCPSender{
|
||||
@@ -33,18 +74,26 @@ func (sf *serverStreamFormat) initialize() {
|
||||
},
|
||||
}
|
||||
sf.rtcpSender.Initialize()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sf *serverStreamFormat) localSSRC() (uint32, bool) {
|
||||
stats := sf.rtcpSender.Stats()
|
||||
if stats != nil {
|
||||
return stats.LocalSSRC, true
|
||||
func (sf *serverStreamFormat) close() {
|
||||
if sf.rtcpSender != nil {
|
||||
sf.rtcpSender.Close()
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (sf *serverStreamFormat) writePacketRTP(byts []byte, pkt *rtp.Packet, ntp time.Time) error {
|
||||
func (sf *serverStreamFormat) writePacketRTP(pkt *rtp.Packet, ntp time.Time) error {
|
||||
pkt.SSRC = sf.localSSRC
|
||||
|
||||
byts := make([]byte, sf.sm.st.Server.MaxPacketSize)
|
||||
n, err := pkt.MarshalTo(byts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
byts = byts[:n]
|
||||
|
||||
sf.rtcpSender.ProcessPacket(pkt, ntp, sf.format.PTSEqualsDTS(pkt))
|
||||
|
||||
le := uint64(len(byts))
|
||||
@@ -52,7 +101,7 @@ func (sf *serverStreamFormat) writePacketRTP(byts []byte, pkt *rtp.Packet, ntp t
|
||||
// send unicast
|
||||
for r := range sf.sm.st.activeUnicastReaders {
|
||||
if _, ok := r.setuppedMedias[sf.sm.media]; ok {
|
||||
err := r.writePacketRTP(sf.sm.media, pkt.PayloadType, byts)
|
||||
err := r.writePacketRTPEncoded(sf.sm.media, pkt.PayloadType, byts)
|
||||
if err != nil {
|
||||
r.onStreamWriteError(err)
|
||||
continue
|
||||
|
Reference in New Issue
Block a user