Add reconnection feature

This commit is contained in:
Alexey Khit
2022-11-04 17:18:12 +03:00
parent 0231fc3a90
commit e9ea7a0b1f
6 changed files with 216 additions and 135 deletions
+5 -7
View File
@@ -469,10 +469,6 @@ func (c *Conn) Close() error {
const transport = "RTP/AVP/TCP;unicast;interleaved="
func (c *Conn) Accept() error {
//if c.state != StateServerInit {
// panic("wrong state")
//}
for {
req, err := tcp.ReadRequest(c.reader)
if err != nil {
@@ -600,6 +596,10 @@ func (c *Conn) Handle() (err error) {
defer func() {
if c.closed {
err = nil
} else {
// may have gotten here because of the deadline
// so close the connection to stop keepalive
_ = c.conn.Close()
}
}()
@@ -712,13 +712,11 @@ func (c *Conn) Handle() (err error) {
}
}
const KeepAlive = time.Second * 25
func (c *Conn) keepalive() {
// TODO: rewrite to RTCP
req := &tcp.Request{Method: MethodOptions, URL: c.URL}
for {
time.Sleep(KeepAlive)
time.Sleep(time.Second * 25)
if c.closed {
return
}
+9 -5
View File
@@ -2,6 +2,7 @@ package rtsp
import (
"encoding/json"
"fmt"
"github.com/AlexxIT/go2rtc/pkg/streamer"
"strconv"
)
@@ -27,13 +28,16 @@ func (c *Conn) GetTrack(media *streamer.Media, codec *streamer.Codec) *streamer.
}
func (c *Conn) Start() error {
if c.mode == ModeServerProducer {
return nil
switch c.mode {
case ModeClientProducer:
if err := c.Play(); err != nil {
return err
}
case ModeServerProducer:
default:
return fmt.Errorf("start wrong mode: %d", c.mode)
}
if err := c.Play(); err != nil {
return err
}
return c.Handle()
}
+3 -3
View File
@@ -75,13 +75,13 @@ func (m *Media) AV() bool {
return m.Kind == KindVideo || m.Kind == KindAudio
}
func (m *Media) MatchCodec(codec *Codec) bool {
func (m *Media) MatchCodec(codec *Codec) *Codec {
for _, c := range m.Codecs {
if c.Match(codec) {
return true
return c
}
}
return false
return nil
}
func (m *Media) MatchMedia(media *Media) *Codec {