From 8fc6bf35aec0c3a2e0bb59fc339f04f20f64b616 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Mon, 14 Nov 2022 16:05:14 +0100 Subject: [PATCH] improve client-read examples in order to setup only needed tracks --- .../main.go | 18 +++++-------- .../main.go | 22 +++++++--------- examples/client-read-codec-h264/main.go | 24 +++++++---------- examples/client-read-codec-mpeg4audio/main.go | 26 ++++++++----------- 4 files changed, 37 insertions(+), 53 deletions(-) diff --git a/examples/client-read-codec-h264-convert-to-jpeg/main.go b/examples/client-read-codec-h264-convert-to-jpeg/main.go index de7452b6..f5d7bed7 100644 --- a/examples/client-read-codec-h264-convert-to-jpeg/main.go +++ b/examples/client-read-codec-h264-convert-to-jpeg/main.go @@ -62,15 +62,15 @@ func main() { } // find the H264 track - h264TrackID, h264track := func() (int, *gortsplib.TrackH264) { - for i, track := range tracks { + h264track := func() *gortsplib.TrackH264 { + for _, track := range tracks { if h264track, ok := track.(*gortsplib.TrackH264); ok { - return i, h264track + return h264track } } - return -1, nil + return nil }() - if h264TrackID < 0 { + if h264track == nil { panic("H264 track not found") } @@ -98,10 +98,6 @@ func main() { // called when a RTP packet arrives saveCount := 0 c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) { - if ctx.TrackID != h264TrackID { - return - } - // convert RTP packets into NALUs nalus, _, err := rtpDec.Decode(ctx.Packet) if err != nil { @@ -134,8 +130,8 @@ func main() { } } - // setup and read all tracks - err = c.SetupAndPlay(tracks, baseURL) + // setup and read the H264 track only + err = c.SetupAndPlay(gortsplib.Tracks{h264track}, baseURL) if err != nil { panic(err) } diff --git a/examples/client-read-codec-h264-save-to-disk/main.go b/examples/client-read-codec-h264-save-to-disk/main.go index 8791b849..0af077ed 100644 --- a/examples/client-read-codec-h264-save-to-disk/main.go +++ b/examples/client-read-codec-h264-save-to-disk/main.go @@ -34,15 +34,15 @@ func main() { } // find the H264 track - h264TrackID, h264track := func() (int, *gortsplib.TrackH264) { - for i, track := range tracks { - if h264track, ok := track.(*gortsplib.TrackH264); ok { - return i, h264track + track := func() *gortsplib.TrackH264 { + for _, track := range tracks { + if track, ok := track.(*gortsplib.TrackH264); ok { + return track } } - return -1, nil + return nil }() - if h264TrackID < 0 { + if track == nil { panic("H264 track not found") } @@ -51,17 +51,13 @@ func main() { rtpDec.Init() // setup H264->MPEGTS muxer - mpegtsMuxer, err := newMPEGTSMuxer(h264track.SafeSPS(), h264track.SafePPS()) + mpegtsMuxer, err := newMPEGTSMuxer(track.SafeSPS(), track.SafePPS()) if err != nil { panic(err) } // called when a RTP packet arrives c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) { - if ctx.TrackID != h264TrackID { - return - } - // convert RTP packets into NALUs nalus, pts, err := rtpDec.Decode(ctx.Packet) if err != nil { @@ -72,8 +68,8 @@ func main() { mpegtsMuxer.encode(nalus, pts) } - // setup and read all tracks - err = c.SetupAndPlay(tracks, baseURL) + // setup and read the H264 track only + err = c.SetupAndPlay(gortsplib.Tracks{track}, baseURL) if err != nil { panic(err) } diff --git a/examples/client-read-codec-h264/main.go b/examples/client-read-codec-h264/main.go index 34d5dc17..dbf3b491 100644 --- a/examples/client-read-codec-h264/main.go +++ b/examples/client-read-codec-h264/main.go @@ -39,15 +39,15 @@ func main() { } // find the H264 track - h264TrackID, h264track := func() (int, *gortsplib.TrackH264) { - for i, track := range tracks { - if h264track, ok := track.(*gortsplib.TrackH264); ok { - return i, h264track + track := func() *gortsplib.TrackH264 { + for _, track := range tracks { + if track, ok := track.(*gortsplib.TrackH264); ok { + return track } } - return -1, nil + return nil }() - if h264TrackID < 0 { + if track == nil { panic("H264 track not found") } @@ -63,21 +63,17 @@ func main() { defer h264RawDec.close() // if SPS and PPS are present into the SDP, send them to the decoder - sps := h264track.SafeSPS() + sps := track.SafeSPS() if sps != nil { h264RawDec.decode(sps) } - pps := h264track.SafePPS() + pps := track.SafePPS() if pps != nil { h264RawDec.decode(pps) } // called when a RTP packet arrives c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) { - if ctx.TrackID != h264TrackID { - return - } - // convert RTP packets into NALUs nalus, _, err := rtpDec.Decode(ctx.Packet) if err != nil { @@ -100,8 +96,8 @@ func main() { } } - // setup and read all tracks - err = c.SetupAndPlay(tracks, baseURL) + // setup and read the H264 track only + err = c.SetupAndPlay(gortsplib.Tracks{track}, baseURL) if err != nil { panic(err) } diff --git a/examples/client-read-codec-mpeg4audio/main.go b/examples/client-read-codec-mpeg4audio/main.go index dfe501c6..4afd7eaa 100644 --- a/examples/client-read-codec-mpeg4audio/main.go +++ b/examples/client-read-codec-mpeg4audio/main.go @@ -36,33 +36,29 @@ func main() { } // find the MPEG4-audio track - mpeg4audioTrack, mpeg4audioTrackID := func() (*gortsplib.TrackMPEG4Audio, int) { - for i, track := range tracks { + track := func() *gortsplib.TrackMPEG4Audio { + for _, track := range tracks { if tt, ok := track.(*gortsplib.TrackMPEG4Audio); ok { - return tt, i + return tt } } - return nil, -1 + return nil }() - if mpeg4audioTrack == nil { + if track == nil { panic("MPEG4-audio track not found") } // setup decoder dec := &rtpmpeg4audio.Decoder{ - SampleRate: mpeg4audioTrack.Config.SampleRate, - SizeLength: mpeg4audioTrack.SizeLength, - IndexLength: mpeg4audioTrack.IndexLength, - IndexDeltaLength: mpeg4audioTrack.IndexDeltaLength, + SampleRate: track.Config.SampleRate, + SizeLength: track.SizeLength, + IndexLength: track.IndexLength, + IndexDeltaLength: track.IndexDeltaLength, } dec.Init() // called when a RTP packet arrives c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) { - if ctx.TrackID != mpeg4audioTrackID { - return - } - // decode MPEG4-audio AUs from the RTP packet aus, _, err := dec.Decode(ctx.Packet) if err != nil { @@ -75,8 +71,8 @@ func main() { } } - // setup and read all tracks - err = c.SetupAndPlay(tracks, baseURL) + // setup and read the MPEG4-audio track only + err = c.SetupAndPlay(gortsplib.Tracks{track}, baseURL) if err != nil { panic(err) }