Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destroy RTCPeerConnection?

I can use the following code to create a new peer connection object:

var peer = new RTCPeerConnection();

When this happens, chrome shows it as new connection object in chrome://webrtc-internals/

I would like to destroy this object later. How to do it? I tried

peer.close();

but this does not seem to do anything, since the peer variable is still of type RTCPeerConnection and I can still see it active in chrome://webrtc-internals/.

If I unset the variable, like

peer=false;

it still shows it in chrome://webrtc-internals/. Yet if I close the webpage then it immediately disappears from chrome://webrtc-internals/.

What is the proper way to free the RTCPeerConnection object? I am asking because if I do not free them, I am occasionally getting refusal from web browser to create new RTCPeerConnection because there are too many of them in use.

like image 355
Tomas M Avatar asked Aug 06 '26 15:08

Tomas M


1 Answers

Actually

peer.close();

is a right way to do it.

You can also do something like this:

peer.close();
peer = null;

Here is some snippets from original WebRTC code samples:

function hangup() {
  console.log('Ending call');
  pc1.close();
  pc2.close();
  pc1 = null;
  pc2 = null;
  hangupButton.disabled = true;
  callButton.disabled = false;
}

https://github.com/webrtc/samples/blob/gh-pages/src/content/peerconnection/pc1/js/main.js#L175

like image 124
Rubycon Avatar answered Aug 09 '26 15:08

Rubycon