1
0
Fork 0
forked from External/mediamtx

propagate H264 packets throughout the server

This commit is contained in:
aler9 2022-03-02 22:52:05 +01:00 committed by Alessandro Ros
parent a59ddf7176
commit d929197b21
15 changed files with 160 additions and 255 deletions

View file

@ -12,9 +12,6 @@ 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"
@ -186,11 +183,6 @@ 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(),
@ -205,8 +197,20 @@ func (s *rtspSource) runInner() bool {
s.parent.onSourceStaticSetNotReady(pathSourceStaticSetNotReadyReq{source: s})
}()
c.OnPacketRTP = func(trackID int, pkt *rtp.Packet) {
res.stream.writePacketRTP(trackID, pkt)
c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) {
if ctx.H264NALUs != nil {
res.stream.writeData(ctx.TrackID, &data{
rtp: ctx.Packet,
ptsEqualsDTS: ctx.PTSEqualsDTS,
h264NALUs: append([][]byte(nil), ctx.H264NALUs...),
h264PTS: ctx.H264PTS,
})
} else {
res.stream.writeData(ctx.TrackID, &data{
rtp: ctx.Packet,
ptsEqualsDTS: ctx.PTSEqualsDTS,
})
}
}
_, err = c.Play(nil)
@ -230,131 +234,6 @@ func (s *rtspSource) runInner() bool {
}
}
func (s *rtspSource) handleMissingH264Params(c *gortsplib.Client, tracks gortsplib.Tracks) error {
h264Track, h264TrackID := func() (*gortsplib.TrackH264, int) {
for i, t := range tracks {
if th264, ok := t.(*gortsplib.TrackH264); ok {
if th264.SPS() == nil {
return th264, i
}
}
}
return nil, -1
}()
if h264TrackID < 0 {
return nil
}
if h264Track.SPS() != nil && h264Track.PPS() != 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
decoder := &rtph264.Decoder{}
decoder.Init()
var sps []byte
var pps []byte
paramsReceived := make(chan struct{})
c.OnPacketRTP = func(trackID int, pkt *rtp.Packet) {
streamMutex.RLock()
defer streamMutex.RUnlock()
if stream == nil {
if trackID != h264TrackID {
return
}
select {
case <-paramsReceived:
return
default:
}
nalus, _, err := decoder.Decode(pkt)
if err != nil {
return
}
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.writePacketRTP(trackID, pkt)
}
}
_, err := c.Play(nil)
if err != nil {
return err
}
readErr := make(chan error)
go func() {
readErr <- c.Wait()
}()
timeout := time.NewTimer(15 * time.Second)
defer timeout.Stop()
select {
case err := <-readErr:
return err
case <-timeout.C:
c.Close()
<-readErr
return fmt.Errorf("source did not send H264 parameters in time")
case <-paramsReceived:
s.log(logger.Info, "H264 parameters extracted")
h264Track.SetSPS(sps)
h264Track.SetPPS(pps)
res := s.parent.onSourceStaticSetReady(pathSourceStaticSetReadyReq{
source: s,
tracks: tracks,
})
if res.err != nil {
c.Close()
<-readErr
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 <-readErr
}
}
// onSourceAPIDescribe implements source.
func (*rtspSource) onSourceAPIDescribe() interface{} {
return struct {