microsoft / MixedReality-WebRTC

MixedReality-WebRTC is a collection of components to help mixed reality app developers integrate audio and video real-time communication into their application and improve their collaborative experience
https://microsoft.github.io/MixedReality-WebRTC/
MIT License
913 stars 283 forks source link

UWP - DataChannelNotOpenException #716

Open roman-richter opened 3 years ago

roman-richter commented 3 years ago

Hello,

my goal is to create a data channel for my UWP app, so it will be possible to send data from one PC to another. I created a solution according to documentation, but I'm getting this exception:

Microsoft.MixedReality.WebRTC.DataChannelNotOpenException: 'Data channel is not open yet, cannot send message. Wait for the StateChanged event until State is Open.'

The problem is that the state of my DataChannel will never become 'Open' and is stuck at 'Connecting' state. Similarly, the IceStateChanged event will only get fired with 'Checking' state and never with 'Connected' one. I have read that this could happen, when there is no data channel created when CreateOffer() method is called, but that's not my case, because I'm creating it right in the beginning. Here's list of everything I do in my code:

  1. create new PeerConnection and initialize it
  2. create listener for DataChannelAdded event and then create new data channel with AddDataChannelAsync(1, "1", true, false) - I'm using out-of-band data channel, since my app will use only one data channel
  3. create listeners for other events (LocalSdpReadytoSend, IceCandidateReadytoSend, Connected, RenegotiationNeeded & IceStateChanged)
  4. connect to my signaling server via socket.io (later listen here for Offer/Answer/IceCandidate messages and handle them as well)
  5. send a message to my signaling server, that app is ready for communication
  6. when a second app connects to signaling server Offer/Answer and IceCandidate messages are exchanged
  7. after some time I try to send a message with channel.SendMessage(byteArray), but it fails with mentioned exception

Am I missing something or doing something wrong here?

Both of my PCs are in the same network and they are able to ping each other so Internet connection shouldn't be a problem. I also have another two questions that may be related to this:

torepaulsson commented 3 years ago

I had an issue with getting the data channel up and running, it was because I didn't specify a IceCandidate.SdpMid when I was sending the candidates, for some reason the other side (which was another implementation of webrtc) did not specify this, unsure why. I saw in logs that the there was "application0" ids sent around, so I hardcoded that and it started working. I also had to wait until I got the DataChannel.StateChanged and checked that the state was open, it took under a second for this event to occur though.. So my tip is to double check those messages.

Hardcoding application0 worked for me since right now only thing we are using is the data channels.

djee-ms commented 3 years ago

IceCandidate's SdpMlineIndex and SdpMid properties are always 0. Is this correct behavior?

If you only have 1 data channel and no audio/video tracks, then yes, there's a single media line with id=0.

Are local descriptions set automatically?

Yes, the local description created by CreateOffer() and CreateAnswer() is automatically applied internally by the native library before the SDP message is passed to LocalOfferReadyToSend.

Hardcoding application0 worked for me

Not sure why that is.