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.41k stars 429 forks source link

datachannel not working #1074

Open maurosorrentino opened 6 months ago

maurosorrentino commented 6 months ago

can you please tell me how you made the datachannel work? I successfully exchanged everything needed (offer and answer have m=datachannel) and the connection state of peer connection is connected. I am only creating 1 datachannel before the offer, rdc.readyState is open but I'm not able to send messages for some reasons. if I look at rdc.negotiated in ondatachannel the answerer tells me it's false, how do I make it pass to true? offer and answer have both nm=application 9 UDP/DTLS/SCTP webrtc-datachannel and the state of peer connection is connected (I even tested it by closing the connection to the server and make a brand new peer connection and when that happens the other client says peer connection state is disconnected)

peerConnection.ondatachannel += (rdc) => { rdc.send("test"); Console.WriteLine($"dc state {rdc.readyState}"); // returns open Console.WriteLine($"datac state {rdc.negotiated}"); // returns false this.rdc = rdc; rdc.onopen += () => Console.WriteLine($"Data channel {rdc.label} open."); // not working rdc.onclose += () => Console.WriteLine($"Data channel {rdc.label} closed."); // the following never fires rdc.onmessage += (datachan, type, data) => { Console.WriteLine($"type {type}"); string msg = Encoding.UTF8.GetString(data); Console.WriteLine($"Data channel {datachan.label} message {type} received: {msg}."); // Do a string echo. rdc.send($"echo: {msg}"); }; }; return peerConnection;

ChristopheI commented 5 months ago

Did you try "WebRTCGetStartedDataChannel" example ?

maurosorrentino commented 5 months ago

Hi, thank you for your answer. Yes, I took the code from there

ris-work commented 5 months ago

From what I've seen, the RTCDataChannel.onopen event does not fire. Can someone confirm whether this is the case for them too? onmessage fires properly whenever there is a message. It becomes difficult to start an action when the DC is open, now... Unfortunately, I had to move it to onmessage as a temp workaround.

All my code is based on the WebRTC DataChannel 'Getting Started'.

Thank you.

ris-work commented 5 months ago

A temporary workaround that works, but if you like living dangerously:

pc.ondatachannel += (rdc) =>
            { // Put it here
                rdc.onopen += () => logger.LogDebug($"Data channel {rdc.label} opened."); // Instead of here
                rdc.onclose += () => logger.LogDebug($"Data channel {rdc.label} closed.");
                rdc.onmessage += (datachan, type, data) =>
                {
                }
            }
ris-work commented 5 months ago

I can confirm that onopen at least fires, but at the wrong time, much later, at least on FreeBSD. SipSorcery works wonderfully (i.e. without major issues) on FreeBSD on dotnet 8 (pkg/latest) BTW. I think it has something to do with deadlocks or resource exhaustion. It takes minutes to fire and the performance is abysmal (nearly 25Mbps, megabits or 3.13MB/s (megabytes)) on a third generation intel. Since async is basically micro/lightweight state machines, I have also encountered similar issues on Rust and webrtc-rs where something blocks something else from happening.

maurosorrentino commented 5 months ago

hi @ris-work , would you mind sharing your code on sorrentino.mauro95@gmail.com ? to me also onmessage doesn't work and I don't understand why, I took the how to use code from their example. I didn't have much time this month to look into this though