From a119764b9644549cf04a595ea26f28048dfc2c01 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sun, 19 Jul 2020 18:25:13 +0200 Subject: [PATCH] add ConnClient.LoopUDP() --- conn-client.go | 21 +++++++++++++++++++-- examples/client-udp.go | 17 +---------------- utils.go | 2 +- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/conn-client.go b/conn-client.go index 3e3dd2ce..13534efb 100644 --- a/conn-client.go +++ b/conn-client.go @@ -20,8 +20,9 @@ import ( ) const ( - clientReadBufferSize = 4096 - clientWriteBufferSize = 4096 + clientReadBufferSize = 4096 + clientWriteBufferSize = 4096 + clientTcpKeepalivePeriod = 30 * time.Second ) // Track is a track available in a certain URL. @@ -403,3 +404,19 @@ func (c *ConnClient) Play(u *url.URL) (*Response, error) { } } } + +// LoopUDP is called after setupping UDP tracks and calling Play(); it keeps +// the TCP connection open through keepalives, and returns when the TCP +// connection closes. +func (c *ConnClient) LoopUDP(u *url.URL) (error) { + keepaliveTicker := time.NewTicker(clientTcpKeepalivePeriod) + defer keepaliveTicker.Stop() + + for { + <- keepaliveTicker.C + _, err := c.Options(u) + if err != nil { + return err + } + } +} diff --git a/examples/client-udp.go b/examples/client-udp.go index 46a18fcb..89ac15ea 100644 --- a/examples/client-udp.go +++ b/examples/client-udp.go @@ -67,21 +67,6 @@ func main() { panic(err) } - done := make(chan struct{}) - - // send periodic keepalive - go func() { - keepaliveTicker := time.NewTicker(30 * time.Second) - for range keepaliveTicker.C { - _, err = rconn.Options(u) - if err != nil { - fmt.Println("connection is closed") - close(done) - break - } - } - }() - // receive RTP packets for trackId, l := range rtpListeners { go func(trackId int, l net.PacketConn) { @@ -112,5 +97,5 @@ func main() { }(trackId, l) } - <-done + panic(rconn.LoopUDP(u)) } diff --git a/utils.go b/utils.go index 6c18612a..1239ee53 100644 --- a/utils.go +++ b/utils.go @@ -40,7 +40,7 @@ const ( // StreamTypeRtp means that the stream contains RTP packets StreamTypeRtp StreamType = iota + 1 - // StreamTypeRtp means that the stream contains RTCP packets + // StreamTypeRtcp means that the stream contains RTCP packets StreamTypeRtcp )