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.
This commit is contained in:
Sergey Stepanov 2021-12-17 16:02:37 +03:00
parent ad822a624d
commit 24ff0f2ea2
No known key found for this signature in database
GPG key ID: A56B4929BAA8556B
3 changed files with 25 additions and 3 deletions

View file

@ -134,10 +134,10 @@
<script src="/static/js/room.js?v=3"></script>
<script src="/static/js/network/ajax.js?v=3"></script>
<script src="/static/js/network/socket.js?v=4"></script>
<script src="/static/js/network/rtcp.js?v=3"></script>
<script src="/static/js/network/rtcp.js?v=4"></script>
<script src="/static/js/recording.js?v=1"></script>
<script src="/static/js/stats/stats.js?v=2"></script>
<script src="/static/js/controller.js?v=6"></script>
<script src="/static/js/controller.js?v=7"></script>
<script src="/static/js/input/keyboard.js?v=5"></script>
<script src="/static/js/input/touch.js?v=3"></script>
<script src="/static/js/input/joystick.js?v=3"></script>

View file

@ -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);

View file

@ -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);