From 24ff0f2ea26688faa599a9e6a4b7dbcd0a10ee37 Mon Sep 17 00:00:00 2001 From: Sergey Stepanov Date: Fri, 17 Dec 2021 16:02:37 +0300 Subject: [PATCH] Close WebRTC connections on disconnect Not closing RTCPeerConnection and co will cause eventually high processor load in Chrome. Also, Chrome has some GC issues related to WebRTC, see: https://bugs.chromium.org/p/chromium/issues/detail?id=825576. --- web/index.html | 4 ++-- web/js/controller.js | 3 ++- web/js/network/rtcp.js | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/web/index.html b/web/index.html index cd668d80..19b55325 100644 --- a/web/index.html +++ b/web/index.html @@ -134,10 +134,10 @@ - + - + diff --git a/web/js/controller.js b/web/js/controller.js index 55708867..acf73adf 100644 --- a/web/js/controller.js +++ b/web/js/controller.js @@ -449,6 +449,7 @@ event.sub(CONNECTION_CLOSED, () => { input.poll().disable(); socket.abort(); + rtcp.stop(); }); event.sub(LATENCY_CHECK_REQUESTED, onLatencyCheck); event.sub(GAMEPAD_CONNECTED, () => message.show('Gamepad connected')); @@ -472,4 +473,4 @@ // initial app state setState(app.state.eden); -})(document, event, env, gameList, input, KEY, log, message, recording, room, settings, socket, stats, stream, utils); +})(document, event, env, gameList, input, KEY, log, message, recording, room, rtcp, settings, socket, stats, stream, utils); diff --git a/web/js/network/rtcp.js b/web/js/network/rtcp.js index 6dd48274..b785671e 100644 --- a/web/js/network/rtcp.js +++ b/web/js/network/rtcp.js @@ -68,6 +68,26 @@ const rtcp = (() => { } } + const stop = () => { + if (mediaStream) { + mediaStream.getTracks().forEach(t => { + t.stop(); + mediaStream.removeTrack(t); + }); + mediaStream = null; + } + if (connection) { + connection.close(); + connection = null; + } + if (inputChannel) { + inputChannel.close(); + inputChannel = null; + } + candidates = Array(); + log.info('[rtcp] WebRTC has been closed'); + } + const ice = (() => { const ICE_TIMEOUT = 2000; let timeForIceGathering; @@ -165,5 +185,6 @@ const rtcp = (() => { isConnected: () => connected, isInputReady: () => inputReady, getConnection: () => connection, + stop, } })(event, socket, env, log);