improve client-read examples in order to setup only needed tracks

This commit is contained in:
aler9
2022-11-14 16:05:14 +01:00
parent 402cae203e
commit 8fc6bf35ae
4 changed files with 37 additions and 53 deletions

View File

@@ -62,15 +62,15 @@ func main() {
} }
// find the H264 track // find the H264 track
h264TrackID, h264track := func() (int, *gortsplib.TrackH264) { h264track := func() *gortsplib.TrackH264 {
for i, track := range tracks { for _, track := range tracks {
if h264track, ok := track.(*gortsplib.TrackH264); ok { 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") panic("H264 track not found")
} }
@@ -98,10 +98,6 @@ func main() {
// called when a RTP packet arrives // called when a RTP packet arrives
saveCount := 0 saveCount := 0
c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) { c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) {
if ctx.TrackID != h264TrackID {
return
}
// convert RTP packets into NALUs // convert RTP packets into NALUs
nalus, _, err := rtpDec.Decode(ctx.Packet) nalus, _, err := rtpDec.Decode(ctx.Packet)
if err != nil { if err != nil {
@@ -134,8 +130,8 @@ func main() {
} }
} }
// setup and read all tracks // setup and read the H264 track only
err = c.SetupAndPlay(tracks, baseURL) err = c.SetupAndPlay(gortsplib.Tracks{h264track}, baseURL)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@@ -34,15 +34,15 @@ func main() {
} }
// find the H264 track // find the H264 track
h264TrackID, h264track := func() (int, *gortsplib.TrackH264) { track := func() *gortsplib.TrackH264 {
for i, track := range tracks { for _, track := range tracks {
if h264track, ok := track.(*gortsplib.TrackH264); ok { if track, ok := track.(*gortsplib.TrackH264); ok {
return i, h264track return track
} }
} }
return -1, nil return nil
}() }()
if h264TrackID < 0 { if track == nil {
panic("H264 track not found") panic("H264 track not found")
} }
@@ -51,17 +51,13 @@ func main() {
rtpDec.Init() rtpDec.Init()
// setup H264->MPEGTS muxer // setup H264->MPEGTS muxer
mpegtsMuxer, err := newMPEGTSMuxer(h264track.SafeSPS(), h264track.SafePPS()) mpegtsMuxer, err := newMPEGTSMuxer(track.SafeSPS(), track.SafePPS())
if err != nil { if err != nil {
panic(err) panic(err)
} }
// called when a RTP packet arrives // called when a RTP packet arrives
c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) { c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) {
if ctx.TrackID != h264TrackID {
return
}
// convert RTP packets into NALUs // convert RTP packets into NALUs
nalus, pts, err := rtpDec.Decode(ctx.Packet) nalus, pts, err := rtpDec.Decode(ctx.Packet)
if err != nil { if err != nil {
@@ -72,8 +68,8 @@ func main() {
mpegtsMuxer.encode(nalus, pts) mpegtsMuxer.encode(nalus, pts)
} }
// setup and read all tracks // setup and read the H264 track only
err = c.SetupAndPlay(tracks, baseURL) err = c.SetupAndPlay(gortsplib.Tracks{track}, baseURL)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@@ -39,15 +39,15 @@ func main() {
} }
// find the H264 track // find the H264 track
h264TrackID, h264track := func() (int, *gortsplib.TrackH264) { track := func() *gortsplib.TrackH264 {
for i, track := range tracks { for _, track := range tracks {
if h264track, ok := track.(*gortsplib.TrackH264); ok { if track, ok := track.(*gortsplib.TrackH264); ok {
return i, h264track return track
} }
} }
return -1, nil return nil
}() }()
if h264TrackID < 0 { if track == nil {
panic("H264 track not found") panic("H264 track not found")
} }
@@ -63,21 +63,17 @@ func main() {
defer h264RawDec.close() defer h264RawDec.close()
// if SPS and PPS are present into the SDP, send them to the decoder // if SPS and PPS are present into the SDP, send them to the decoder
sps := h264track.SafeSPS() sps := track.SafeSPS()
if sps != nil { if sps != nil {
h264RawDec.decode(sps) h264RawDec.decode(sps)
} }
pps := h264track.SafePPS() pps := track.SafePPS()
if pps != nil { if pps != nil {
h264RawDec.decode(pps) h264RawDec.decode(pps)
} }
// called when a RTP packet arrives // called when a RTP packet arrives
c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) { c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) {
if ctx.TrackID != h264TrackID {
return
}
// convert RTP packets into NALUs // convert RTP packets into NALUs
nalus, _, err := rtpDec.Decode(ctx.Packet) nalus, _, err := rtpDec.Decode(ctx.Packet)
if err != nil { if err != nil {
@@ -100,8 +96,8 @@ func main() {
} }
} }
// setup and read all tracks // setup and read the H264 track only
err = c.SetupAndPlay(tracks, baseURL) err = c.SetupAndPlay(gortsplib.Tracks{track}, baseURL)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@@ -36,33 +36,29 @@ func main() {
} }
// find the MPEG4-audio track // find the MPEG4-audio track
mpeg4audioTrack, mpeg4audioTrackID := func() (*gortsplib.TrackMPEG4Audio, int) { track := func() *gortsplib.TrackMPEG4Audio {
for i, track := range tracks { for _, track := range tracks {
if tt, ok := track.(*gortsplib.TrackMPEG4Audio); ok { 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") panic("MPEG4-audio track not found")
} }
// setup decoder // setup decoder
dec := &rtpmpeg4audio.Decoder{ dec := &rtpmpeg4audio.Decoder{
SampleRate: mpeg4audioTrack.Config.SampleRate, SampleRate: track.Config.SampleRate,
SizeLength: mpeg4audioTrack.SizeLength, SizeLength: track.SizeLength,
IndexLength: mpeg4audioTrack.IndexLength, IndexLength: track.IndexLength,
IndexDeltaLength: mpeg4audioTrack.IndexDeltaLength, IndexDeltaLength: track.IndexDeltaLength,
} }
dec.Init() dec.Init()
// called when a RTP packet arrives // called when a RTP packet arrives
c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) { c.OnPacketRTP = func(ctx *gortsplib.ClientOnPacketRTPCtx) {
if ctx.TrackID != mpeg4audioTrackID {
return
}
// decode MPEG4-audio AUs from the RTP packet // decode MPEG4-audio AUs from the RTP packet
aus, _, err := dec.Decode(ctx.Packet) aus, _, err := dec.Decode(ctx.Packet)
if err != nil { if err != nil {
@@ -75,8 +71,8 @@ func main() {
} }
} }
// setup and read all tracks // setup and read the MPEG4-audio track only
err = c.SetupAndPlay(tracks, baseURL) err = c.SetupAndPlay(gortsplib.Tracks{track}, baseURL)
if err != nil { if err != nil {
panic(err) panic(err)
} }