From e64ed167cc49f0dfa19705c6923815dd8763cb8a Mon Sep 17 00:00:00 2001 From: zarazaex69 Date: Mon, 25 May 2026 11:39:22 +0300 Subject: [PATCH] refactor(jitsi): extract openBridgeWS and openBridgeSCTP helpers --- internal/engine/jitsi/jitsi.go | 54 +++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/internal/engine/jitsi/jitsi.go b/internal/engine/jitsi/jitsi.go index be8382c..a199413 100644 --- a/internal/engine/jitsi/jitsi.go +++ b/internal/engine/jitsi/jitsi.go @@ -289,7 +289,7 @@ func (s *Session) Connect(ctx context.Context) error { return nil } -func (s *Session) joinAndOpenBridge(ctx context.Context) (*j.Session, error) { +func (s *Session) joinAndOpenBridge(ctx context.Context) (*j.Session, error) { //nolint:cyclop // sequential setup steps logger.Infof("jitsi: joining %s/%s as %s …", s.host, s.room, s.name) jSess, err := j.Join(ctx, j.Config{ Host: s.host, @@ -305,18 +305,11 @@ func (s *Session) joinAndOpenBridge(ctx context.Context) (*j.Session, error) { needBridge := s.onData != nil || s.onPeerData != nil sctpBridge := needBridge && jSess.ColibriWS == "" - if needBridge && jSess.ColibriWS != "" { - bctx, bcancel := context.WithTimeout(ctx, bridgeOpenTimeout) - err := jSess.OpenBridge(bctx) - bcancel() - if err != nil { + if needBridge && !sctpBridge { + if err := s.openBridgeWS(ctx, jSess); err != nil { _ = jSess.Close() - return nil, fmt.Errorf("open bridge: %w", err) + return nil, err } - s.peerEndpoint.Store(nil) - s.peerVideoSSRC.Store(0) - s.bridgeReady.Store(true) - logger.Infof("jitsi: bridge open colibri-ws (endpoints=%v)", jSess.Endpoints()) } if s.shouldNegotiatePC() { @@ -327,22 +320,43 @@ func (s *Session) joinAndOpenBridge(ctx context.Context) (*j.Session, error) { } if sctpBridge { - bctx, bcancel := context.WithTimeout(ctx, bridgeOpenTimeout) - err := jSess.WaitBridgeSCTP(bctx) - bcancel() - if err != nil { + if err := s.openBridgeSCTP(ctx, jSess); err != nil { _ = jSess.Close() - return nil, fmt.Errorf("open bridge sctp: %w", err) + return nil, err } - s.peerEndpoint.Store(nil) - s.peerVideoSSRC.Store(0) - s.bridgeReady.Store(true) - logger.Infof("jitsi: bridge open sctp (endpoints=%v)", jSess.Endpoints()) } return jSess, nil } +func (s *Session) openBridgeWS(ctx context.Context, jSess *j.Session) error { + bctx, bcancel := context.WithTimeout(ctx, bridgeOpenTimeout) + err := jSess.OpenBridge(bctx) + bcancel() + if err != nil { + return fmt.Errorf("open bridge: %w", err) + } + s.peerEndpoint.Store(nil) + s.peerVideoSSRC.Store(0) + s.bridgeReady.Store(true) + logger.Infof("jitsi: bridge open colibri-ws (endpoints=%v)", jSess.Endpoints()) + return nil +} + +func (s *Session) openBridgeSCTP(ctx context.Context, jSess *j.Session) error { + bctx, bcancel := context.WithTimeout(ctx, bridgeOpenTimeout) + err := jSess.WaitBridgeSCTP(bctx) + bcancel() + if err != nil { + return fmt.Errorf("open bridge sctp: %w", err) + } + s.peerEndpoint.Store(nil) + s.peerVideoSSRC.Store(0) + s.bridgeReady.Store(true) + logger.Infof("jitsi: bridge open sctp (endpoints=%v)", jSess.Endpoints()) + return nil +} + func (s *Session) shouldNegotiatePC() bool { if s.onData != nil { return true