From c540cad994747a32d420c8fc68e3eb5680ea3c64 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Thu, 15 Jul 2021 23:51:25 +0200 Subject: [PATCH] Move pushClientConnection into its own function. --- rtpconn/webclient.go | 106 ++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 52 deletions(-) diff --git a/rtpconn/webclient.go b/rtpconn/webclient.go index 19247d47..622f024a 100644 --- a/rtpconn/webclient.go +++ b/rtpconn/webclient.go @@ -954,68 +954,70 @@ func clientLoop(c *webClient, ws *websocket.Conn) error { } } -func handleAction(c *webClient, a interface{}) error { - switch a := a.(type) { - case pushConnAction: - g := c.group - if g == nil || a.group != g { - return nil +func pushDownConn(c *webClient, id string, up conn.Up, tracks []conn.UpTrack, replace string) error { + var requested []conn.UpTrack + if up != nil { + var old *rtpDownConnection + if replace != "" { + old = getDownConn(c, replace) + } else { + old = getDownConn(c, up.Id()) } - var tracks []conn.UpTrack var override []string - if a.conn != nil { - var old *rtpDownConnection - if a.replace != "" { - old = getDownConn(c, a.replace) - } else { - old = getDownConn(c, a.conn.Id()) - } - if old != nil { - override = old.requested - } - tracks = requestedTracks(c, override, a.conn, a.tracks) + if old != nil { + override = old.requested } + requested = requestedTracks(c, override, up, tracks) + } - if a.replace != "" { - err := delDownConn(c, a.replace) - if err != nil { - log.Printf("Replace: %v", err) - } + if replace != "" { + err := delDownConn(c, replace) + if err != nil { + log.Printf("Replace: %v", err) } + } - if len(tracks) == 0 { - closeDownConn(c, a.id, "") - if a.replace != "" { - closeDownConn(c, a.replace, "") - } - return nil + // closes over replace, which will be modified below + defer func() { + if replace != "" { + closeDownConn(c, replace, "") } + }() - down, _, err := addDownConn(c, a.conn) - if err != nil { - if a.replace != "" { - closeDownConn(c, a.replace, "") - } - if err == os.ErrClosed { - return nil - } - return err - } - done, err := replaceTracks(down, tracks, a.conn) - if err != nil || !done { - if a.replace != "" { - closeDownConn(c, a.replace, "") - } - return err + if len(requested) == 0 { + closeDownConn(c, id, "") + return nil + } + + down, _, err := addDownConn(c, up) + if err != nil { + if err == os.ErrClosed { + return nil } - err = negotiate(c, down, false, a.replace) - if err != nil { - log.Printf("Negotiation failed: %v", err) - if a.replace != "" { - closeDownConn(c, a.replace, "") - } - closeDownConn(c, down.id, "negotiation failed") + return err + } + done, err := replaceTracks(down, requested, up) + if err != nil || !done { + return err + } + err = negotiate(c, down, false, replace) + if err != nil { + log.Printf("Negotiation failed: %v", err) + closeDownConn(c, down.id, "negotiation failed") + return err + } + replace = "" + return nil +} + +func handleAction(c *webClient, a interface{}) error { + switch a := a.(type) { + case pushConnAction: + if c.group == nil || c.group != a.group { + log.Printf("Got connectsions for wrong group") + return nil } + return pushDownConn(c, a.id, a.conn, a.tracks, a.replace) case requestConnsAction: g := c.group if g == nil || a.group != g {