private void DisposeConnection() { try { Logger.LogInformation("Disposing connection..."); if (_dataChannel != null) { _dataChannel.onopen -= OnDataChannelOpened; _dataChannel.onclose -= OnDataChannelClosed; _dataChannel.close(); } if (_connection != null) { _connection.ondatachannel += OnDataChannelInitialized; _connection.onicecandidate += OnIceCandidateAvailable; _connection.onconnectionstatechange += OnConnectionStateChanged; _connection.close(); _connection.Dispose(); _connection = null; } } catch (Exception ex) { Logger.LogWarning(ex, "Error occurred while trying to dispose the connection."); } }
public async void CheckPeerConnectionEstablishment() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); var aliceConnected = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); var bobConnected = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); var alice = new RTCPeerConnection(); alice.onconnectionstatechange += (state) => { if (state == RTCPeerConnectionState.connected) { logger.LogDebug("Alice connected."); aliceConnected.SetResult(true); } }; alice.addTrack(new MediaStreamTrack(SDPWellKnownMediaFormatsEnum.PCMU)); var aliceOffer = alice.createOffer(); await alice.setLocalDescription(aliceOffer); logger.LogDebug($"alice offer: {aliceOffer.sdp}"); var bob = new RTCPeerConnection(); bob.onconnectionstatechange += (state) => { if (state == RTCPeerConnectionState.connected) { logger.LogDebug("Bob connected."); bobConnected.SetResult(true); } }; bob.addTrack(new MediaStreamTrack(SDPWellKnownMediaFormatsEnum.PCMU)); var setOfferResult = bob.setRemoteDescription(aliceOffer); Assert.Equal(SetDescriptionResultEnum.OK, setOfferResult); var bobAnswer = bob.createAnswer(); await bob.setLocalDescription(bobAnswer); var setAnswerResult = alice.setRemoteDescription(bobAnswer); Assert.Equal(SetDescriptionResultEnum.OK, setAnswerResult); logger.LogDebug($"answer: {bobAnswer.sdp}"); await Task.WhenAny(Task.WhenAll(aliceConnected.Task, bobConnected.Task), Task.Delay(2000)); Assert.True(aliceConnected.Task.IsCompleted); Assert.True(aliceConnected.Task.Result); Assert.True(bobConnected.Task.IsCompleted); Assert.True(bobConnected.Task.Result); bob.close(); alice.close(); }
public async void CheckDataChannelEstablishment() { logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name); logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name); var aliceDataConnected = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); var bobDataOpened = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously); var alice = new RTCPeerConnection(); var dc = await alice.createDataChannel("dc1", null); dc.onopen += () => aliceDataConnected.TrySetResult(true); var aliceOffer = alice.createOffer(); await alice.setLocalDescription(aliceOffer); logger.LogDebug($"alice offer: {aliceOffer.sdp}"); var bob = new RTCPeerConnection(); RTCDataChannel bobData = null; bob.ondatachannel += (chan) => { bobData = chan; bobDataOpened.TrySetResult(true); }; var setOfferResult = bob.setRemoteDescription(aliceOffer); Assert.Equal(SetDescriptionResultEnum.OK, setOfferResult); var bobAnswer = bob.createAnswer(); await bob.setLocalDescription(bobAnswer); var setAnswerResult = alice.setRemoteDescription(bobAnswer); Assert.Equal(SetDescriptionResultEnum.OK, setAnswerResult); logger.LogDebug($"answer: {bobAnswer.sdp}"); await Task.WhenAny(Task.WhenAll(aliceDataConnected.Task, bobDataOpened.Task), Task.Delay(2000)); Assert.True(aliceDataConnected.Task.IsCompleted); Assert.True(aliceDataConnected.Task.Result); Assert.True(bobDataOpened.Task.IsCompleted); Assert.True(bobDataOpened.Task.Result); Assert.True(dc.IsOpened); Assert.True(bobData.IsOpened); bob.close(); alice.close(); }