Jump over wrong packets from RTSP

This commit is contained in:
Alexey Khit
2023-02-03 12:10:54 +03:00
parent 8cee4179f2
commit d21ce3d27d
2 changed files with 53 additions and 14 deletions
+2
View File
@@ -112,6 +112,8 @@ func rtspHandler(url string) (streamer.Producer, error) {
log.Trace().Msgf("[rtsp] client request:\n%s", msg) log.Trace().Msgf("[rtsp] client request:\n%s", msg)
case *tcp.Response: case *tcp.Response:
log.Trace().Msgf("[rtsp] client response:\n%s", msg) log.Trace().Msgf("[rtsp] client response:\n%s", msg)
case string:
log.Trace().Msgf("[rtsp] client msg: %s", msg)
} }
}) })
} }
+51 -14
View File
@@ -743,6 +743,9 @@ func (c *Conn) Handle() (err error) {
return return
} }
var channelID byte
var size uint16
if buf4[0] != '$' { if buf4[0] != '$' {
switch string(buf4) { switch string(buf4) {
case "RTSP": case "RTSP":
@@ -751,26 +754,62 @@ func (c *Conn) Handle() (err error) {
return return
} }
c.Fire(res) c.Fire(res)
continue
case "OPTI", "TEAR", "DESC", "SETU", "PLAY", "PAUS", "RECO", "ANNO", "GET_", "SET_": case "OPTI", "TEAR", "DESC", "SETU", "PLAY", "PAUS", "RECO", "ANNO", "GET_", "SET_":
var req *tcp.Request var req *tcp.Request
if req, err = tcp.ReadRequest(c.reader); err != nil { if req, err = tcp.ReadRequest(c.reader); err != nil {
return return
} }
c.Fire(req) c.Fire(req)
continue
default: 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 // get data size
channelID := buf4[1] size = binary.BigEndian.Uint16(buf4[2:])
// get data size // skip 4 bytes from c.reader.Peek
size := int(binary.BigEndian.Uint16(buf4[2:])) if _, err = c.reader.Discard(4); err != nil {
return
if _, err = c.reader.Discard(4); err != nil { }
return
} }
// init memory for data // init memory for data
@@ -779,7 +818,7 @@ func (c *Conn) Handle() (err error) {
return return
} }
c.receive += size c.receive += int(size)
if channelID&1 == 0 { if channelID&1 == 0 {
packet := &rtp.Packet{} packet := &rtp.Packet{}
@@ -790,10 +829,8 @@ func (c *Conn) Handle() (err error) {
track := c.channels[channelID] track := c.channels[channelID]
if track != nil { if track != nil {
_ = track.WriteRTP(packet) _ = track.WriteRTP(packet)
//return fmt.Errorf("wrong channelID: %d", channelID)
} else { } else {
continue // TODO: maybe fix this c.Fire("wrong channelID: " + strconv.Itoa(int(channelID)))
//panic("wrong channelID")
} }
} else { } else {
msg := &RTCP{Channel: channelID} msg := &RTCP{Channel: channelID}