From 107d7b618dec26d4d4e1d7ad9c07f7b24e8d03d8 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Mon, 12 Dec 2022 18:13:00 +0100 Subject: [PATCH] fix support for LPCM tracks with no explicit channel count (https://github.com/aler9/rtsp-simple-server/issues/1292) --- track_lpcm.go | 18 ++++++++++++------ track_test.go | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/track_lpcm.go b/track_lpcm.go index f75732f8..96fc77bb 100644 --- a/track_lpcm.go +++ b/track_lpcm.go @@ -39,8 +39,8 @@ func newTrackLPCMFromMediaDescription( bitDepth = 24 } - tmp := strings.SplitN(clock, "/", 32) - if len(tmp) != 2 { + tmp := strings.SplitN(clock, "/", 2) + if len(tmp) < 1 { return nil, fmt.Errorf("invalid clock (%v)", clock) } @@ -49,16 +49,22 @@ func newTrackLPCMFromMediaDescription( return nil, err } - channelCount, err := strconv.ParseInt(tmp[1], 10, 64) - if err != nil { - return nil, err + var channelCount int + if len(tmp) >= 2 { + tmp, err := strconv.ParseInt(tmp[1], 10, 64) + if err != nil { + return nil, err + } + channelCount = int(tmp) + } else { + channelCount = 1 } return &TrackLPCM{ PayloadType: payloadType, BitDepth: bitDepth, SampleRate: int(sampleRate), - ChannelCount: int(channelCount), + ChannelCount: channelCount, trackBase: trackBase{ control: control, }, diff --git a/track_test.go b/track_test.go index e6e5dade..5def013c 100644 --- a/track_test.go +++ b/track_test.go @@ -95,6 +95,28 @@ func TestTrackNewFromMediaDescription(t *testing.T) { ChannelCount: 2, }, }, + { + "lpcm 16 with no explicit channel", + &psdp.MediaDescription{ + MediaName: psdp.MediaName{ + Media: "audio", + Protos: []string{"RTP", "AVP"}, + Formats: []string{"97"}, + }, + Attributes: []psdp.Attribute{ + { + Key: "rtpmap", + Value: "97 L16/16000", + }, + }, + }, + &TrackLPCM{ + PayloadType: 97, + BitDepth: 16, + SampleRate: 16000, + ChannelCount: 1, + }, + }, { "lpcm 24", &psdp.MediaDescription{