detect when the connection with the server is closed when publishing with udp (#6)

This commit is contained in:
aler9
2020-10-16 23:13:24 +02:00
parent b647e5ee31
commit c792387d79
5 changed files with 64 additions and 50 deletions

View File

@@ -732,18 +732,19 @@ func (c *ConnClient) Play(u *url.URL) (*base.Response, error) {
return res, nil return res, nil
} }
// LoopUDP must be called after SetupUDP() and Play(); it keeps // LoopUDP must be called after Play() or Record(); it keeps
// the TCP connection open with keepalives, and returns when the TCP // the TCP connection open with keepalives, and returns when the TCP
// connection closes. // connection closes.
func (c *ConnClient) LoopUDP() error { func (c *ConnClient) LoopUDP() error {
if c.state != connClientStateReading { if c.state != connClientStateReading && c.state != connClientStatePublishing {
return fmt.Errorf("can be called only after a successful Play()") return fmt.Errorf("can be called only after a successful Play() or Record()")
} }
if *c.streamProtocol != StreamProtocolUDP { if *c.streamProtocol != StreamProtocolUDP {
return fmt.Errorf("stream protocol is not UDP") return fmt.Errorf("stream protocol is not UDP")
} }
if c.state == connClientStateReading {
readDone := make(chan error) readDone := make(chan error)
go func() { go func() {
for { for {
@@ -803,6 +804,12 @@ func (c *ConnClient) LoopUDP() error {
} }
} }
// connClientStatePublishing
c.conf.Conn.SetReadDeadline(time.Time{}) // disable deadline
var res base.Response
return res.Read(c.br)
}
// Announce writes an ANNOUNCE request and reads a Response. // Announce writes an ANNOUNCE request and reads a Response.
func (c *ConnClient) Announce(u *url.URL, tracks Tracks) (*base.Response, error) { func (c *ConnClient) Announce(u *url.URL, tracks Tracks) (*base.Response, error) {
if c.streamUrl != nil { if c.streamUrl != nil {

View File

@@ -161,6 +161,7 @@ func TestConnClientDialPublishUDP(t *testing.T) {
var conn *ConnClient var conn *ConnClient
defer func() { defer func() {
conn.Close() conn.Close()
conn.LoopUDP()
}() }()
go func() { go func() {

View File

@@ -59,6 +59,7 @@ func main() {
// write frames to the server // write frames to the server
err = conn.WriteFrameTCP(track.Id, gortsplib.StreamTypeRtp, buf[:n]) err = conn.WriteFrameTCP(track.Id, gortsplib.StreamTypeRtp, buf[:n])
if err != nil { if err != nil {
fmt.Println("connection is closed (%s)", err)
break break
} }
} }

View File

@@ -62,4 +62,8 @@ func main() {
break break
} }
} }
// wait until the connection is closed
err = conn.LoopUDP()
fmt.Println("connection is closed (%s)", err)
} }

View File

@@ -56,6 +56,7 @@ func main() {
}(trackId) }(trackId)
} }
// wait until the connection is closed
err = conn.LoopUDP() err = conn.LoopUDP()
fmt.Println("connection is closed (%s)", err) fmt.Println("connection is closed (%s)", err)
} }