sipsorcery-org / sipsorcery

A WebRTC, SIP and VoIP library for C# and .NET. Designed for real-time communications apps.
https://sipsorcery-org.github.io/sipsorcery
Other
1.42k stars 431 forks source link

Closing PeerConnection is not done correctly ? #1058

Closed ChristopheI closed 7 months ago

ChristopheI commented 7 months ago

Using the current code (master branch) and the basic sample "FfmpegToWebRTC" slightly updated, it seems that RTCPeerConnection are not closed correctly.

On remote side (HTML/JS), event RTCPeerConnection.onclose and/or track.onended are never called.

I updated the sample in this way:

C# Part: I updated "Program.cs" slighlty. I store the WebRtcClient in global variable When the key "c" (for closed) is used, I use this webRtcClient.pc.Close("normal"); // To close the RTCPeerConnection which should close first local track

The cs file updated: Program.zip

HTML/JS Part: I adde/updated the JS with this:

                pc.onclose= () => {
                    console.log("pc close");
                };

                pc.ontrack = ({ track, streams: [stream] }) => {
                    track.onunmute = () => {
                        console.log("Adding track to video control.");
                        videoControl.srcObject = stream;
                    };

                    track.onended = () => {
                        console.log("Track ended.");
                    };
                };

I'm expecting to see after using the key "c" (or even "q" to quit the program) in the JS console: Track ended. pc close

But it never occurs ... Am I doing something wrong ? Or there is a real problem ?

sipsorcery commented 7 months ago

I confirmed it wasn't working as well.

This commit to master should fix.

Prior to that fix the Chrome PC DTLS state was staying as open. Post the fix it transtitions to closed.

image

ChristopheI commented 7 months ago

You saved my day ! I tried to find the source of the pb without success.

I think also that DataChannels must be automatically closed when the RTCPeerConnection is closed: see #1059.

If you don't mind, it will create to have a new release with these fixes