From 57cb2e99d115f7326fe92d1e3866afcc35bb92a4 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 6 Mar 2022 16:08:59 +0100 Subject: [PATCH] HLS client: support dynamic H264 SPS/PPS --- internal/hls/client_video_processor.go | 65 ++++++-------------------- 1 file changed, 13 insertions(+), 52 deletions(-) diff --git a/internal/hls/client_video_processor.go b/internal/hls/client_video_processor.go index 917b3c7b..17a82460 100644 --- a/internal/hls/client_video_processor.go +++ b/internal/hls/client_video_processor.go @@ -23,11 +23,8 @@ type clientVideoProcessor struct { onData func(time.Duration, [][]byte) logger ClientLogger - trackInitialized bool - queue chan clientVideoProcessorData - sps []byte - pps []byte - clockStartRTC time.Time + queue chan clientVideoProcessorData + clockStartRTC time.Time } func newClientVideoProcessor( @@ -48,6 +45,16 @@ func newClientVideoProcessor( } func (p *clientVideoProcessor) run() error { + track, err := gortsplib.NewTrackH264(96, nil, nil, nil) + if err != nil { + return err + } + + err = p.onTrack(track) + if err != nil { + return err + } + for { select { case item := <-p.queue: @@ -87,40 +94,7 @@ func (p *clientVideoProcessor) doProcess( for _, nalu := range nalus { typ := h264.NALUType(nalu[0] & 0x1F) - switch typ { - case h264.NALUTypeSPS: - if p.sps == nil { - p.sps = append([]byte(nil), nalu...) - - if !p.trackInitialized && p.pps != nil { - p.trackInitialized = true - err := p.initializeTrack() - if err != nil { - return err - } - } - } - - // remove since it's not needed - continue - - case h264.NALUTypePPS: - if p.pps == nil { - p.pps = append([]byte(nil), nalu...) - - if !p.trackInitialized && p.sps != nil { - p.trackInitialized = true - err := p.initializeTrack() - if err != nil { - return err - } - } - } - - // remove since it's not needed - continue - - case h264.NALUTypeAccessUnitDelimiter: + if typ == h264.NALUTypeAccessUnitDelimiter { // remove since it's not needed continue } @@ -132,10 +106,6 @@ func (p *clientVideoProcessor) doProcess( return nil } - if !p.trackInitialized { - return nil - } - p.onData(pts, outNALUs) return nil } @@ -150,12 +120,3 @@ func (p *clientVideoProcessor) process( case <-p.ctx.Done(): } } - -func (p *clientVideoProcessor) initializeTrack() error { - track, err := gortsplib.NewTrackH264(96, p.sps, p.pps, nil) - if err != nil { - return err - } - - return p.onTrack(track) -}