rtsp source: support cameras that don't provide SPS and PPS inside the SDP (#411) (#707)

This commit is contained in:
aler9
2021-11-22 22:55:12 +01:00
parent 29ee78ce38
commit c7b2ae83df
2 changed files with 268 additions and 0 deletions

View File

@@ -12,6 +12,9 @@ import (
"github.com/aler9/gortsplib"
"github.com/aler9/gortsplib/pkg/base"
"github.com/aler9/gortsplib/pkg/h264"
"github.com/aler9/gortsplib/pkg/rtph264"
"github.com/pion/rtp"
"github.com/aler9/rtsp-simple-server/internal/conf"
"github.com/aler9/rtsp-simple-server/internal/logger"
@@ -183,6 +186,11 @@ func (s *rtspSource) runInner() bool {
}
}
err = s.handleMissingH264Params(c, tracks)
if err != nil {
return err
}
res := s.parent.onSourceStaticSetReady(pathSourceStaticSetReadyReq{
Source: s,
Tracks: c.Tracks(),
@@ -226,6 +234,150 @@ func (s *rtspSource) runInner() bool {
}
}
func (s *rtspSource) handleMissingH264Params(c *gortsplib.Client, tracks gortsplib.Tracks) error {
h264TrackID := func() int {
for i, t := range tracks {
if t.IsH264() {
return i
}
}
return -1
}()
if h264TrackID < 0 {
return nil
}
_, err := tracks[h264TrackID].ExtractConfigH264()
if err == nil {
return nil
}
s.log(logger.Info, "source has not provided H264 parameters (SPS and PPS)"+
" inside the SDP; extracting them from the stream...")
var streamMutex sync.RWMutex
var stream *stream
var payloadType uint8
decoder := rtph264.NewDecoder()
var sps []byte
var pps []byte
paramsReceived := make(chan struct{})
c.OnPacketRTP = func(trackID int, payload []byte) {
streamMutex.RLock()
defer streamMutex.RUnlock()
if stream == nil {
if trackID != h264TrackID {
return
}
select {
case <-paramsReceived:
return
default:
}
var pkt rtp.Packet
err := pkt.Unmarshal(payload)
if err != nil {
return
}
nalus, _, err := decoder.Decode(&pkt)
if err != nil {
return
}
payloadType = pkt.Header.PayloadType
for _, nalu := range nalus {
typ := h264.NALUType(nalu[0] & 0x1F)
switch typ {
case h264.NALUTypeSPS:
sps = nalu
if sps != nil && pps != nil {
close(paramsReceived)
}
case h264.NALUTypePPS:
pps = nalu
if sps != nil && pps != nil {
close(paramsReceived)
}
}
}
} else {
stream.onPacketRTP(trackID, payload)
}
}
c.OnPacketRTCP = func(trackID int, payload []byte) {
streamMutex.RLock()
defer streamMutex.RUnlock()
if stream != nil {
stream.onPacketRTCP(trackID, payload)
}
}
_, err = c.Play(nil)
if err != nil {
return err
}
waitError := make(chan error)
go func() {
waitError <- c.Wait()
}()
timeout := time.NewTimer(15 * time.Second)
defer timeout.Stop()
select {
case err := <-waitError:
return err
case <-timeout.C:
return fmt.Errorf("source did not send H264 parameters in time")
case <-paramsReceived:
s.log(logger.Info, "H264 parameters extracted")
track, err := gortsplib.NewTrackH264(payloadType, &gortsplib.TrackConfigH264{
SPS: sps,
PPS: pps,
})
if err != nil {
return err
}
tracks[h264TrackID] = track
res := s.parent.onSourceStaticSetReady(pathSourceStaticSetReadyReq{
Source: s,
Tracks: tracks,
})
if res.Err != nil {
return res.Err
}
func() {
streamMutex.Lock()
defer streamMutex.Unlock()
stream = res.Stream
}()
s.log(logger.Info, "ready")
defer func() {
s.parent.OnSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{Source: s})
}()
}
return <-waitError
}
// onSourceAPIDescribe implements source.
func (*rtspSource) onSourceAPIDescribe() interface{} {
return struct {