protected virtual void Dispose(bool disposing)
        {
            // clean all unmanaged resources (if any)

            if (disposing)
            {
                // clean all managed resources
                if (_dataChannel != null)
                {
                    _dataChannel.Close();
                    _dataChannel = null;
                }
                if (_sctp != null)
                {
                    _sctp.Stop();
                    _sctp = null;
                }
                if (_dtls != null)
                {
                    _dtls.Stop();
                    _dtls = null;
                }
                if (_ice != null)
                {
                    _ice.Stop();
                    _ice = null;
                }
                if (_gatherer != null)
                {
                    _gatherer.Close();
                    _gatherer = null;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Closes a peer connection.
        /// </summary>
        //private async Task ClosePeerConnection()
        private void ClosePeerConnection()
        {
            lock (MediaLock)
            {
                if (_peerConnection != null)
                {
                    _peerId = -1;
                    if (_mediaStream != null)
                    {
                        foreach (var track in _mediaStream.GetTracks())
                        {
                            // Check Track Status before action to avoid reference errors
                            // CRASH condition previously on non-XAML usage
                            if (track != null)
                            {
                                if (track.Enabled)
                                {
                                    track.Stop();
                                }
                                _mediaStream.RemoveTrack(track);
                            }
                        }
                    }
                    _mediaStream = null;

                    // TODO: Cleanup DataChannel
                    if (_peerSendDataChannel != null)
                    {
                        _peerSendDataChannel.Close();
                        _peerSendDataChannel = null;
                    }

                    if (_peerReceiveDataChannel != null)
                    {
                        _peerReceiveDataChannel.Close();
                        _peerReceiveDataChannel = null;
                    }

                    OnPeerConnectionClosed?.Invoke();

                    _peerConnection.Close(); // Slow, so do this after UI updated and camera turned off

                    SessionId = null;
    #if ORTCLIB
                    OrtcStatsManager.Instance.CallEnded();
    #endif
                    _peerConnection = null;

                    OnReadyToConnect?.Invoke();

                    // TODO: handle GC
                    //GC.Collect(); // Ensure all references are truly dropped.
                }
            }
        }
Beispiel #3
0
    private void CloseEverything()
    {
        if (localConnection.ConnectionState != RTCPeerConnectionState.Closed)
        {
            localConnection.Close();
        }
        if (dataChannel.ReadyState != RTCDataChannelState.Closed)
        {
            dataChannel.Close();
        }

        localConnection = null;
        dataChannel     = null;

        socket = null;
    }
Beispiel #4
0
        public void Close()
        {
            try
            {
                signaling?.Close();
                signaling = null;
            }
            catch (Exception ex)
            {
                OnErrorEvent.Invoke("signaling.close", ex.Message);
            }

            try
            {
                if (dataChannel != null)
                {
                    dataChannel.OnOpen    = null;
                    dataChannel.OnMessage = null;
                    dataChannel.OnClose   = null;
                    dataChannel.Close();
                    dataChannel = null;
                }
            }
            catch (Exception ex)
            {
                OnErrorEvent.Invoke("dataChannel dispose", ex.Message);
            }

            try
            {
                peer.OnConnectionStateChange   = null;
                peer.OnDataChannel             = null;
                peer.OnIceCandidate            = null;
                peer.OnIceGatheringStateChange = null;
                peer.OnNegotiationNeeded       = null;
                peer.OnTrack = null;
                peer.Close();
                peer.Dispose();
                peer = null;
            }
            catch (Exception ex)
            {
                OnErrorEvent.Invoke("peer dispose", ex.Message);
            }
        }
        private void SelectedPeerChanged(Peer oldPeer, Peer peer)
        {
            // When the selected peer changes tear down the old datachannel and prepare for a new connection.
            if (_dataChannel != null)
            {
                _dataChannel.Close();
                _sctp.Stop();
                _dtls.Stop();
                _ice.Stop();
            }


            Conversation  = string.Empty;
            Message       = string.Empty;
            IsSendEnabled = false;

            InitializeORTC();
        }
Beispiel #6
0
        public IEnumerator SendThrowsExceptionAfterClose()
        {
            var test = new MonoBehaviourTest <SignalingPeers>();

            yield return(test);

            RTCDataChannel channel = test.component.CreateDataChannel(0, "test");

            byte[] message1 = { 1, 2, 3 };
            string message2 = "123";

            Assert.DoesNotThrow(() => channel.Send(message1));
            Assert.DoesNotThrow(() => channel.Send(message2));
            channel.Close();
            Assert.Throws <InvalidOperationException>(() => channel.Send(message1));
            Assert.Throws <InvalidOperationException>(() => channel.Send(message2));

            test.component.Dispose();
            yield return(0);
        }
        public IEnumerator SendThrowsExceptionAfterClose()
        {
            var            test    = new MonoBehaviourTest <SignalingPeers>();
            RTCDataChannel channel = test.component.CreateDataChannel(0, "test");

            yield return(test);

            byte[] message1 = { 1, 2, 3 };
            string message2 = "123";

            var op1 = new WaitUntilWithTimeout(() => channel.ReadyState == RTCDataChannelState.Open, 5000);

            yield return(op1);

            Assert.That(op1.IsCompleted, Is.True);
            Assert.That(() => channel.Send(message1), Throws.Nothing);
            Assert.That(() => channel.Send(message2), Throws.Nothing);
            channel.Close();
            Assert.That(() => channel.Send(message1), Throws.TypeOf <InvalidOperationException>());
            Assert.That(() => channel.Send(message2), Throws.TypeOf <InvalidOperationException>());
            test.component.Dispose();
            Object.DestroyImmediate(test.gameObject);
        }
Beispiel #8
0
        public IEnumerator EventsAreSentToOther()
        {
            RTCConfiguration config = default;

            config.iceServers = new[] { new RTCIceServer {
                                            urls = new[] { "stun:stun.l.google.com:19302" }
                                        } };
            var            peer1    = new RTCPeerConnection(ref config);
            var            peer2    = new RTCPeerConnection(ref config);
            RTCDataChannel channel2 = null;

            peer1.OnIceCandidate = candidate => { peer2.AddIceCandidate(candidate); };
            peer2.OnIceCandidate = candidate => { peer1.AddIceCandidate(candidate); };
            peer2.OnDataChannel  = channel => { channel2 = channel; };

            var  channel1       = peer1.CreateDataChannel("data");
            bool channel1Opened = false;
            bool channel1Closed = false;

            channel1.OnOpen  = () => { channel1Opened = true; };
            channel1.OnClose = () => { channel1Closed = true; };

            RTCOfferOptions  options1 = default;
            RTCAnswerOptions options2 = default;
            var op1 = peer1.CreateOffer(ref options1);

            yield return(op1);

            var desc = op1.Desc;
            var op2  = peer1.SetLocalDescription(ref desc);

            yield return(op2);

            var op3 = peer2.SetRemoteDescription(ref desc);

            yield return(op3);

            var op4 = peer2.CreateAnswer(ref options2);

            yield return(op4);

            desc = op4.Desc;
            var op5 = peer2.SetLocalDescription(ref desc);

            yield return(op5);

            var op6 = peer1.SetRemoteDescription(ref desc);

            yield return(op6);

            var op7 = new WaitUntilWithTimeout(
                () => peer1.IceConnectionState == RTCIceConnectionState.Connected ||
                peer1.IceConnectionState == RTCIceConnectionState.Completed, 5000);

            yield return(op7);

            Assert.True(op7.IsCompleted);
            var op8 = new WaitUntilWithTimeout(
                () => peer2.IceConnectionState == RTCIceConnectionState.Connected ||
                peer2.IceConnectionState == RTCIceConnectionState.Completed, 5000);

            yield return(op8);

            Assert.True(op8.IsCompleted);
            var op9 = new WaitUntilWithTimeout(() => channel2 != null, 5000);

            yield return(op9);

            Assert.True(op9.IsCompleted);

            Assert.True(channel1Opened);
            Assert.AreEqual(channel1.Label, channel2.Label);
            Assert.AreEqual(channel1.Id, channel2.Id);

            const string message1 = "hello";
            string       message2 = null;

            channel2.OnMessage = bytes => { message2 = System.Text.Encoding.UTF8.GetString(bytes); };
            channel1.Send(message1);
            var op10 = new WaitUntilWithTimeout(() => !string.IsNullOrEmpty(message2), 5000);

            yield return(op10);

            Assert.True(op10.IsCompleted);
            Assert.AreEqual(message1, message2);

            byte[] message3 = { 1, 2, 3 };
            byte[] message4 = null;
            channel2.OnMessage = bytes => { message4 = bytes; };
            channel1.Send(message3);
            var op11 = new WaitUntilWithTimeout(() => message4 != null, 5000);

            yield return(op11);

            Assert.True(op11.IsCompleted);
            Assert.AreEqual(message3, message4);

            channel1.Close();
            var op12 = new WaitUntilWithTimeout(() => channel1Closed, 5000);

            yield return(op12);

            Assert.True(op12.IsCompleted);

            channel2.Close();
            peer1.Close();
            peer2.Close();
        }
Beispiel #9
0
 public void Close()
 {
     _dataChannel.Close();
 }
 public void Close() => _dataChannel.Close();