From d21ce3d27d3300ecd5e000130f00958c7abf36ae Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Fri, 3 Feb 2023 12:10:54 +0300 Subject: [PATCH] Jump over wrong packets from RTSP --- cmd/rtsp/rtsp.go | 2 ++ pkg/rtsp/conn.go | 65 +++++++++++++++++++++++++++++++++++++----------- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/cmd/rtsp/rtsp.go b/cmd/rtsp/rtsp.go index 1951d0e9..ce9a723b 100644 --- a/cmd/rtsp/rtsp.go +++ b/cmd/rtsp/rtsp.go @@ -112,6 +112,8 @@ func rtspHandler(url string) (streamer.Producer, error) { log.Trace().Msgf("[rtsp] client request:\n%s", msg) case *tcp.Response: log.Trace().Msgf("[rtsp] client response:\n%s", msg) + case string: + log.Trace().Msgf("[rtsp] client msg: %s", msg) } }) } diff --git a/pkg/rtsp/conn.go b/pkg/rtsp/conn.go index 39523ca6..bc9fe734 100644 --- a/pkg/rtsp/conn.go +++ b/pkg/rtsp/conn.go @@ -743,6 +743,9 @@ func (c *Conn) Handle() (err error) { return } + var channelID byte + var size uint16 + if buf4[0] != '$' { switch string(buf4) { case "RTSP": @@ -751,26 +754,62 @@ func (c *Conn) Handle() (err error) { return } c.Fire(res) + continue + case "OPTI", "TEAR", "DESC", "SETU", "PLAY", "PAUS", "RECO", "ANNO", "GET_", "SET_": var req *tcp.Request if req, err = tcp.ReadRequest(c.reader); err != nil { return } c.Fire(req) + continue + default: - return fmt.Errorf("RTSP wrong input") + for i := 0; ; i++ { + // search next start symbol + if _, err = c.reader.ReadBytes('$'); err != nil { + return err + } + + if channelID, err = c.reader.ReadByte(); err != nil { + return err + } + + // check if channel ID exists + if c.channels[channelID] == nil { + continue + } + + buf4 = make([]byte, 2) + if _, err = io.ReadFull(c.reader, buf4); err != nil { + return err + } + + // check if size good for RTP + size = binary.BigEndian.Uint16(buf4) + if size <= 1500 { + break + } + + // 10 tries to find good packet + if i >= 10 { + return fmt.Errorf("RTSP wrong input") + } + } + + c.Fire("RTSP wrong input") } - continue - } + } else { + // hope that the odd channels are always RTCP + channelID = buf4[1] - // hope that the odd channels are always RTCP - channelID := buf4[1] + // get data size + size = binary.BigEndian.Uint16(buf4[2:]) - // get data size - size := int(binary.BigEndian.Uint16(buf4[2:])) - - if _, err = c.reader.Discard(4); err != nil { - return + // skip 4 bytes from c.reader.Peek + if _, err = c.reader.Discard(4); err != nil { + return + } } // init memory for data @@ -779,7 +818,7 @@ func (c *Conn) Handle() (err error) { return } - c.receive += size + c.receive += int(size) if channelID&1 == 0 { packet := &rtp.Packet{} @@ -790,10 +829,8 @@ func (c *Conn) Handle() (err error) { track := c.channels[channelID] if track != nil { _ = track.WriteRTP(packet) - //return fmt.Errorf("wrong channelID: %d", channelID) } else { - continue // TODO: maybe fix this - //panic("wrong channelID") + c.Fire("wrong channelID: " + strconv.Itoa(int(channelID))) } } else { msg := &RTCP{Channel: channelID}