Fix states handle for RTSP

This commit is contained in:
Alexey Khit
2023-01-13 13:32:09 +03:00
parent fca6c87b2c
commit d92b0f29af
+24 -2
View File
@@ -48,6 +48,22 @@ const (
type State byte type State byte
func (s State) String() string {
switch s {
case StateNone:
return "NONE"
case StateConn:
return "CONN"
case StateSetup:
return "SETUP"
case StatePlay:
return "PLAY"
case StateHandle:
return "HANDLE"
}
return strconv.Itoa(int(s))
}
const ( const (
StateNone State = iota StateNone State = iota
StateConn StateConn
@@ -346,6 +362,10 @@ func (c *Conn) SetupMedia(
c.stateMu.Lock() c.stateMu.Lock()
defer c.stateMu.Unlock() defer c.stateMu.Unlock()
if c.state != StateConn && c.state != StateSetup {
return nil, fmt.Errorf("RTSP SETUP from wrong state: %s", c.state)
}
ch := c.GetChannel(media) ch := c.GetChannel(media)
if ch < 0 { if ch < 0 {
return nil, fmt.Errorf("wrong media: %v", media) return nil, fmt.Errorf("wrong media: %v", media)
@@ -648,15 +668,17 @@ func (c *Conn) Handle() (err error) {
case StatePlay: case StatePlay:
c.state = StateHandle c.state = StateHandle
default: default:
err = fmt.Errorf("RTSP Handle from wrong state: %d", c.state) err = fmt.Errorf("RTSP HANDLE from wrong state: %s", c.state)
c.state = StateNone c.state = StateNone
_ = c.conn.Close() _ = c.conn.Close()
} }
ok := c.state == StateHandle
c.stateMu.Unlock() c.stateMu.Unlock()
if c.state != StateHandle { if !ok {
return return
} }