简体   繁体   English

泄漏RTCPeerConnections。 甚至在页面刷新之后

[英]Leaking RTCPeerConnections. Even after page refresh

I have a pretty simple RTCPeerConnection app running. 我有一个非常简单的RTCPeerConnection应用程序运行。 Uses Firebase for signaling. 使用Firebase进行信令。 The RTCPeerConnections get established, and then I take the stream, and do the following with it: RTCPeerConnections建立,然后我获取流,并使用它执行以下操作:

let streamURL = window.URL.createObjectURL(stream);

Then I take that streamURL and set the video.src = streamURL . 然后我获取streamURL并设置video.src = streamURL That is how I get it to see the remote users' video. 这就是我如何看到远程用户的视频。 Then, when I am done with the connection (I want to end the conversation) I do the following: 然后,当我完成连接(我想结束对话)时,我会执行以下操作:

peerConnection.getLocalStreams().forEach(stream=>{
    stream.getTracks().forEach(t=>{
        t.stop();
        stream.removeTrack(t);
    });
});
peerConnection.close();

This ends the connection, including turns off the green light for my local webcam. 这样就结束了连接,包括关闭本地网络摄像头的绿灯。 This tells me that things have ended for the most part. 这告诉我事情已经结束了大部分。

Then I check chrome://webrtc-internals and the connection is still there. 然后我检查chrome:// webrtc-internals并且连接仍在那里。 Even after I refresh, the connection is there. 即使我刷新后,连接就在那里。 That seems odd. 这看起来很奇怪。 How can it still be there, even after a refresh? 即使刷新后它怎么还能存在? Please help. 请帮忙。 Am I not ending the RTCPeerConnection correctly? 我没有正确结束RTCPeerConnection吗? I thought this was the right way. 我认为这是正确的方法。 I just can't figure out how the webrtc internals can still show it EVEN AFTER a page refresh. 我只是无法弄清楚即使页面刷新后webrtc内部仍然可以显示它。 PLEASE HELP! 请帮忙!

You're doing it right. 你说得对。 The only thing missing is: 唯一缺少的是:

peerConnection = null;

to let the peer connection itself be garbage collected. 让对等连接本身被垃圾收集。

As I've mentioned elsewhere I'd avoid createObjectURL and use video.srcObject = stream directly instead, but that has to do with releasing the camera should you forget to stop all tracks. 正如我在其他地方提到的,我会避免使用createObjectURL并直接使用video.srcObject = stream ,但这与释放相机有关,如果你忘记停止所有轨道。

I wouldn't read too much into Chrome's webrtc-internals page, because looking at peer connections is useful for debugging, even after the fact. 我不会过多地阅读Chrome的webrtc-internals页面,因为查看对等连接对于调试很有用,即使事后也是如此。 The important question is: Are there any JavaScript or wire observable signs of a connection being open? 重要的问题是:是否有任何JavaScript或线可观察到的连接标志?

Example

You can try this https fiddle in two different tabs or windows and see things failing remotely. 您可以在两个不同的选项卡或窗口中尝试此https小提琴 ,并查看远程失败的内容。

Make sure you can see both fiddles at once, and click the [Start!] button in one of them to connect. 确保您可以同时看到两个小提琴,然后单击其中一个中的[开始!]按钮进行连接。 You should see video in both places. 您应该在两个地方看到视频。 Now when you click the [Stop!] button, you should see the following output on the other end (it may take a few seconds for all three to appear): 现在,当您单击[停止!]按钮时,您应该在另一端看到以下输出(可能需要几秒钟才能显示所有三个):

disconnected
failed
closed

Because I check for the "failed" state and close the connection on it (though in production it's more common to use data channels to communicate hangups ). 因为我检查"failed"状态并关闭它上面的连接(虽然在生产中使用数据通道来传递挂断更常见)。

Chrome

That said, I won't fully rule out a bug in Chrome, in that it may hang on to resources longer than it should, as I see the same thing you're describing (but note that eg "packets sent" drops to zero when I click "Stop!", which is the important part). 也就是说,我不会完全排除Chrome中的错误,因为它可能会延长资源的时间,因为我看到你正在描述的相同的事情(但请注意,例如“发送的数据包”降至零当我点击“停止!”,这是重要的部分)。

Contrast with Firefox, where, when I go to about:webrtc (its equivalent to chrome://webrtc-internals) and hit refresh and the [Clear History] button, my peer connections are gone from the list. 与Firefox对比,当我转到:webrtc(相当于chrome:// webrtc-internals)和点击刷新以及[清除历史记录]按钮时,我的对等连接从列表中消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM