From cd18a22655ed8fcd2570fed9f42d4dc8198917f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 14 Feb 2021 17:17:06 +0100 Subject: [PATCH] WebRTC events only for active peer connection. --- internal/session/session.go | 6 +++++- internal/types/session.go | 2 +- internal/webrtc/manager.go | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/session/session.go b/internal/session/session.go index d5f72921..11c3c967 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -191,7 +191,11 @@ func (session *SessionCtx) SetWebRTCPeer(webrtcPeer types.WebRTCPeer) { session.webrtcPeer = webrtcPeer } -func (session *SessionCtx) SetWebRTCConnected(connected bool) { +func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, connected bool) { + if webrtcPeer != session.webrtcPeer { + return + } + session.webrtcConnected = connected session.manager.emmiter.Emit("state_changed", session) diff --git a/internal/types/session.go b/internal/types/session.go index 7bc09769..c2a3a772 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -56,7 +56,7 @@ type Session interface { // webrtc SetWebRTCPeer(webrtcPeer WebRTCPeer) - SetWebRTCConnected(connected bool) + SetWebRTCConnected(webrtcPeer WebRTCPeer, connected bool) GetWebRTCPeer() WebRTCPeer } diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index da7b0b2d..c94b651c 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -263,13 +263,13 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { switch state { case webrtc.PeerConnectionStateConnected: - session.SetWebRTCConnected(true) + session.SetWebRTCConnected(peer, true) case webrtc.PeerConnectionStateDisconnected: fallthrough case webrtc.PeerConnectionStateFailed: connection.Close() case webrtc.PeerConnectionStateClosed: - session.SetWebRTCConnected(false) + session.SetWebRTCConnected(peer, false) videoStream.RemoveListener(&listener) // should be stream stopped