Ejemplo n.º 1
0
        public IEnumerator OnAddTrackDelegatesWithEvent()
        {
            var camObj      = new GameObject("Camera");
            var cam         = camObj.AddComponent <Camera>();
            var videoStream = cam.CaptureStream(1280, 720, 1000000);

            yield return(new WaitForSeconds(0.1f));

            var test = new MonoBehaviourTest <SignalingPeers>();

            test.component.SetStream(videoStream);
            yield return(test);

            test.component.CoroutineUpdate();
            yield return(new WaitForSeconds(0.1f));

            bool isCalledOnAddTrack    = false;
            bool isCalledOnRemoveTrack = false;

            videoStream.OnAddTrack = e =>
            {
                Assert.That(e.Track, Is.Not.Null);
                isCalledOnAddTrack = true;
            };
            videoStream.OnRemoveTrack = e =>
            {
                Assert.That(e.Track, Is.Not.Null);
                isCalledOnRemoveTrack = true;
            };

            var width  = 256;
            var height = 256;
            var format = WebRTC.GetSupportedRenderTextureFormat(SystemInfo.graphicsDeviceType);
            var rt     = new UnityEngine.RenderTexture(width, height, 0, format);

            rt.Create();
            var track2 = new VideoStreamTrack("video2", rt);

            yield return(0);

            Assert.That(videoStream.AddTrack(track2), Is.True);
            var op1 = new WaitUntilWithTimeout(() => isCalledOnAddTrack, 5000);

            yield return(op1);

            Assert.That(videoStream.RemoveTrack(track2), Is.True);
            var op2 = new WaitUntilWithTimeout(() => isCalledOnRemoveTrack, 5000);

            yield return(op2);

            test.component.Dispose();
            track2.Dispose();
            foreach (var track in videoStream.GetTracks())
            {
                track.Dispose();
            }
            // wait for disposing video track.
            yield return(0);

            videoStream.Dispose();
            Object.DestroyImmediate(camObj);
            Object.DestroyImmediate(rt);
        }
Ejemplo n.º 2
0
        IEnumerator Start()
        {
            RTCConfiguration config = default;

            config.iceServers = new[]
            {
                new RTCIceServer {
                    urls = new[] { "stun:stun.l.google.com:19302" }
                }
            };

            peers[0] = new RTCPeerConnection(ref config);
            peers[1] = new RTCPeerConnection(ref config);
            RTCDataChannelInit conf = new RTCDataChannelInit(true);

            dataChannel = peers[0].CreateDataChannel("data", ref conf);

            peers[0].OnIceCandidate = candidate =>
            {
                Assert.NotNull(candidate);
                Assert.NotNull(candidate.candidate);
                peers[1].AddIceCandidate(ref candidate);
            };
            peers[1].OnIceCandidate = candidate =>
            {
                Assert.NotNull(candidate);
                Assert.NotNull(candidate.candidate);
                peers[0].AddIceCandidate(ref candidate);
            };
            peers[1].OnTrack = e =>
            {
                Assert.NotNull(e);
                Assert.NotNull(e.Track);
                Assert.NotNull(e.Receiver);
                Assert.NotNull(e.Transceiver);
                peers[1].AddTrack(e.Track);
            };

            if (m_stream != null)
            {
                foreach (var track in m_stream.GetTracks())
                {
                    peers[0].AddTrack(track, m_stream);
                }
            }

            RTCOfferOptions  options1 = default;
            RTCAnswerOptions options2 = default;
            var op1 = peers[0].CreateOffer(ref options1);

            yield return(op1);

            Assert.False(op1.IsError);
            var desc = op1.Desc;
            var op2  = peers[0].SetLocalDescription(ref desc);

            yield return(op2);

            Assert.False(op2.IsError);

            desc.sdp = ReplaceOfferSdpForHardwareEncodeTest(desc.sdp);

            var op3 = peers[1].SetRemoteDescription(ref desc);

            yield return(op3);

            Assert.False(op3.IsError);
            var op4 = peers[1].CreateAnswer(ref options2);

            yield return(op4);

            Assert.False(op4.IsError);
            desc = op4.Desc;
            var op5 = peers[1].SetLocalDescription(ref desc);

            yield return(op5);

            Assert.False(op5.IsError);

            desc.sdp = ReplaceAnswerSdpForHardwareEncodeTest(desc.sdp);

            var op6 = peers[0].SetRemoteDescription(ref desc);

            yield return(op6);

            Assert.False(op6.IsError);

            var op7 = new WaitUntilWithTimeout(() =>
                                               peers[0].IceConnectionState == RTCIceConnectionState.Connected ||
                                               peers[0].IceConnectionState == RTCIceConnectionState.Completed, 5000);

            yield return(op7);

            Assert.True(op7.IsCompleted);

            var op8 = new WaitUntilWithTimeout(() =>
                                               peers[1].IceConnectionState == RTCIceConnectionState.Connected ||
                                               peers[1].IceConnectionState == RTCIceConnectionState.Completed, 5000);

            yield return(op8);

            Assert.True(op8.IsCompleted);

            if (m_stream != null)
            {
                var op9 = new WaitUntilWithTimeout(() => GetPeerSenders(0).Any(), 5000);
                yield return(op9);

                Assert.True(op9.IsCompleted);
            }

            IsTestFinished = true;
        }
Ejemplo n.º 3
0
        public IEnumerator AddIceCandidate()
        {
            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);

            var peer1ReceiveCandidateQueue = new Queue <RTCIceCandidate>();
            var peer2ReceiveCandidateQueue = new Queue <RTCIceCandidate>();

            peer1.OnIceCandidate = candidate => { peer2ReceiveCandidateQueue.Enqueue(candidate); };
            peer2.OnIceCandidate = candidate => { peer1ReceiveCandidateQueue.Enqueue(candidate); };

            var obj    = new GameObject("audio");
            var source = obj.AddComponent <AudioSource>();

            source.clip = AudioClip.Create("test", 480, 2, 48000, false);
            var track = new AudioStreamTrack(source);

            peer1.AddTrack(track);

            var op1 = peer1.CreateOffer();

            yield return(op1);

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

            yield return(op2);

            yield return(new WaitUntil(() => peer2ReceiveCandidateQueue.Any()));

            Assert.That(peer2.AddIceCandidate(peer2ReceiveCandidateQueue.Peek()), Is.False);

            var op3 = peer2.SetRemoteDescription(ref desc);

            yield return(op3);

            Assert.That(peer2.AddIceCandidate(peer2ReceiveCandidateQueue.Dequeue()), Is.True);

            var op4 = peer2.CreateAnswer();

            yield return(op4);

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

            yield return(op5);

            yield return(new WaitUntil(() => peer1ReceiveCandidateQueue.Any()));

            Assert.That(peer1.AddIceCandidate(peer1ReceiveCandidateQueue.Peek()), Is.False);

            var op6 = peer1.SetRemoteDescription(ref desc);

            yield return(op6);

            Assert.That(peer1.AddIceCandidate(peer1ReceiveCandidateQueue.Dequeue()), Is.True);

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

            yield return(op7);

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

            yield return(op8);

            Assert.That(op8.IsCompleted, Is.True);

            foreach (var candidate in peer1ReceiveCandidateQueue)
            {
                Assert.That(peer1.AddIceCandidate(candidate), Is.True);
            }

            peer1ReceiveCandidateQueue.Clear();

            foreach (var candidate in peer2ReceiveCandidateQueue)
            {
                Assert.That(peer2.AddIceCandidate(candidate), Is.True);
            }

            peer2ReceiveCandidateQueue.Clear();

            track.Dispose();
            peer1.Close();
            peer2.Close();
            Object.DestroyImmediate(source.clip);
            Object.DestroyImmediate(obj);
        }
Ejemplo n.º 4
0
            IEnumerator Start()
            {
                RTCConfiguration config = default;

                config.iceServers = new[]
                {
                    new RTCIceServer {
                        urls = new[] { "stun:stun.l.google.com:19302" }
                    }
                };

                pc1Senders           = new List <RTCRtpSender>();
                pc2Senders           = new List <RTCRtpSender>();
                peer1                = new RTCPeerConnection(ref config);
                peer2                = new RTCPeerConnection(ref config);
                peer1.OnIceCandidate = candidate =>
                {
                    Assert.NotNull(candidate);
                    Assert.NotNull(candidate.candidate);
                    peer2.AddIceCandidate(ref candidate);
                };
                peer2.OnIceCandidate = candidate =>
                {
                    Assert.NotNull(candidate);
                    Assert.NotNull(candidate.candidate);
                    peer1.AddIceCandidate(ref candidate);
                };
                peer2.OnTrack = e =>
                {
                    Assert.NotNull(e);
                    Assert.NotNull(e.Track);
                    Assert.NotNull(e.Receiver);
                    Assert.NotNull(e.Transceiver);
                    pc2Senders.Add(peer2.AddTrack(e.Track, m_stream));
                };

                foreach (var track in m_stream.GetTracks())
                {
                    pc1Senders.Add(peer1.AddTrack(track, m_stream));
                }

                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(op7.IsCompleted);

                var op9 = new WaitUntilWithTimeout(() => pc2Senders.Count > 0, 5000);

                yield return(op9);

                Assert.True(op9.IsCompleted);

                m_isFinished = true;
            }
Ejemplo n.º 5
0
        public IEnumerator PeerConnectionStateChange()
        {
            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);

            RTCPeerConnectionState state1    = default;
            RTCPeerConnectionState state2    = default;
            RTCIceConnectionState  iceState1 = default;
            RTCIceConnectionState  iceState2 = default;

            peer1.OnIceCandidate          = candidate => { peer2.AddIceCandidate(candidate); };
            peer2.OnIceCandidate          = candidate => { peer1.AddIceCandidate(candidate); };
            peer1.OnConnectionStateChange = state => { state1 = state; };
            peer2.OnConnectionStateChange = state => { state2 = state; };
            peer1.OnIceConnectionChange   = state => { iceState1 = state; };
            peer2.OnIceConnectionChange   = state => { iceState2 = state; };

            Assert.That(state1, Is.EqualTo(RTCPeerConnectionState.New));
            Assert.That(state2, Is.EqualTo(RTCPeerConnectionState.New));

            var obj    = new GameObject("audio");
            var source = obj.AddComponent <AudioSource>();

            source.clip = AudioClip.Create("test", 480, 2, 48000, false);
            AudioStreamTrack track1 = new AudioStreamTrack(source);

            peer1.AddTrack(track1);

            var op1 = peer1.CreateOffer();

            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();

            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(() =>
                                               state1 == RTCPeerConnectionState.Connected &&
                                               state2 == RTCPeerConnectionState.Connected, 5000);

            yield return(op7);

            Assert.That(op7.IsCompleted, Is.True);

            var op8 = new WaitUntilWithTimeout(() =>
                                               (iceState1 == RTCIceConnectionState.Connected || iceState1 == RTCIceConnectionState.Completed) &&
                                               (iceState2 == RTCIceConnectionState.Connected || iceState2 == RTCIceConnectionState.Completed)
                                               , 5000);

            yield return(op8);

            Assert.That(op8.IsCompleted, Is.True);

            peer1.Close();

            var op9 = new WaitUntilWithTimeout(() =>
                                               state1 == RTCPeerConnectionState.Closed &&
                                               iceState2 == RTCIceConnectionState.Disconnected, 5000);

            yield return(op9);

            Assert.That(op9.IsCompleted, Is.True);

            track1.Dispose();
            peer2.Close();
            Object.DestroyImmediate(source.clip);
            Object.DestroyImmediate(obj);
        }
Ejemplo n.º 6
0
        public IEnumerator RemoteOnRemoveTrack()
        {
            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);

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

            var         stream        = new MediaStream();
            MediaStream receiveStream = null;

            var obj    = new GameObject("audio");
            var source = obj.AddComponent <AudioSource>();

            source.clip = AudioClip.Create("test", 480, 2, 48000, false);
            var track = new AudioStreamTrack(source);

            stream.AddTrack(track);
            RTCRtpSender sender = peer1.AddTrack(track, stream);

            bool isInvokeNegotiationNeeded1 = false;

            peer1.OnNegotiationNeeded = () => isInvokeNegotiationNeeded1 = true;

            bool isInvokeOnRemoveTrack = false;

            peer2.OnTrack = e =>
            {
                Assert.That(e.Streams, Has.Count.EqualTo(1));
                receiveStream = e.Streams.First();
                receiveStream.OnRemoveTrack = ev => isInvokeOnRemoveTrack = true;
            };

            yield return(SignalingOffer(peer1, peer2));

            peer1.RemoveTrack(sender);

            var op9 = new WaitUntilWithTimeout(() => isInvokeNegotiationNeeded1, 5000);

            yield return(op9);

            Assert.That(op9.IsCompleted, Is.True);

            yield return(SignalingOffer(peer1, peer2));

            var op10 = new WaitUntilWithTimeout(() => isInvokeOnRemoveTrack, 5000);

            yield return(op10);

            Assert.That(op10.IsCompleted, Is.True);

            stream.Dispose();
            receiveStream.Dispose();
            track.Dispose();
            peer1.Dispose();
            peer2.Dispose();
            Object.DestroyImmediate(source.clip);
            Object.DestroyImmediate(obj);
        }
Ejemplo n.º 7
0
        IEnumerator Start()
        {
            RTCConfiguration config = default;

            config.iceServers = new[]
            {
                new RTCIceServer {
                    urls = new[] { "stun:stun.l.google.com:19302" }
                }
            };

            peers[0] = new RTCPeerConnection(ref config);
            peers[1] = new RTCPeerConnection(ref config);
            dataChannels[peers[0]] = new List <RTCDataChannel>();
            dataChannels[peers[1]] = new List <RTCDataChannel>();

            RTCDataChannel channel = peers[0].CreateDataChannel("data");

            dataChannels[peers[0]].Add(channel);

            peers[0].OnIceCandidate = candidate =>
            {
                Assert.That(candidate, Is.Not.Null);
                Assert.That(candidate, Is.Not.Null);
                peers[1].AddIceCandidate(candidate);
            };
            peers[1].OnIceCandidate = candidate =>
            {
                Assert.NotNull(candidate);
                Assert.NotNull(candidate.Candidate);
                peers[0].AddIceCandidate(candidate);
            };
            peers[1].OnTrack = e =>
            {
                Assert.NotNull(e);
                Assert.NotNull(e.Track);
                Assert.NotNull(e.Receiver);
                Assert.NotNull(e.Transceiver);
                peers[1].AddTrack(e.Track);
            };
            peers[0].OnDataChannel = e =>
            {
                if (peers[0].ConnectionState == RTCPeerConnectionState.Connected)
                {
                    dataChannels[peers[0]].Add(e);
                }
            };
            peers[1].OnDataChannel = e =>
            {
                if (peers[1].ConnectionState == RTCPeerConnectionState.Connected)
                {
                    dataChannels[peers[1]].Add(e);
                }
            };

            if (m_stream != null)
            {
                foreach (var track in m_stream.GetTracks())
                {
                    peers[0].AddTrack(track, m_stream);
                }
            }

            // Because some platform can't accept H264 codec for receive.
            foreach (var transceiver in peers[0].GetTransceivers())
            {
                transceiver.Direction = RTCRtpTransceiverDirection.SendOnly;
            }

            var op1 = peers[0].CreateOffer();

            yield return(op1);

            Assert.That(op1.IsError, Is.False, op1.Error.message);
            var desc = op1.Desc;
            var op2  = peers[0].SetLocalDescription(ref desc);

            yield return(op2);

            Assert.That(op2.IsError, Is.False, op2.Error.message);

            desc.sdp = ReplaceOfferSdpForHardwareEncodeTest(desc.sdp);
            var op3 = peers[1].SetRemoteDescription(ref desc);

            yield return(op3);

            Assert.That(op3.IsError, Is.False, op3.Error.message);
            var op4 = peers[1].CreateAnswer();

            yield return(op4);

            Assert.That(op4.IsError, Is.False, op4.Error.message);
            desc = op4.Desc;
            var op5 = peers[1].SetLocalDescription(ref desc);

            yield return(op5);

            Assert.That(op5.IsError, Is.False, op5.Error.message);

            desc.sdp = ReplaceAnswerSdpForHardwareEncodeTest(desc.sdp);

            var op6 = peers[0].SetRemoteDescription(ref desc);

            yield return(op6);

            Assert.That(op6.IsError, Is.False, op6.Error.message);

            var op7 = new WaitUntilWithTimeout(() =>
                                               peers[0].IceConnectionState == RTCIceConnectionState.Connected ||
                                               peers[0].IceConnectionState == RTCIceConnectionState.Completed, 5000);

            yield return(op7);

            Assert.That(op7.IsCompleted, Is.True);

            var op8 = new WaitUntilWithTimeout(() =>
                                               peers[1].IceConnectionState == RTCIceConnectionState.Connected ||
                                               peers[1].IceConnectionState == RTCIceConnectionState.Completed, 5000);

            yield return(op8);

            Assert.That(op8.IsCompleted, Is.True);

            if (m_stream != null)
            {
                var op9 = new WaitUntilWithTimeout(() => GetPeerSenders(0).Any(), 5000);
                yield return(op9);

                Assert.That(op9.IsCompleted, Is.True);
            }
            IsTestFinished = true;
        }
Ejemplo n.º 8
0
        public IEnumerator IceConnectionStateChange()
        {
            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);

            peer1.OnIceCandidate = candidate => { peer2.AddIceCandidate(ref candidate); };
            peer2.OnIceCandidate = candidate => { peer1.AddIceCandidate(ref candidate); };

            MediaStream stream = Audio.CaptureStream();

            peer1.AddTrack(stream.GetTracks().First());

            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);

            stream.Dispose();
            peer1.Close();
            peer2.Close();
        }
Ejemplo n.º 9
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(ref candidate); };
            peer2.OnIceCandidate = candidate => { peer1.AddIceCandidate(ref candidate); };
            peer2.OnDataChannel  = channel => { channel2 = channel; };

            var  conf           = new RTCDataChannelInit(true);
            var  channel1       = peer1.CreateDataChannel("data", ref conf);
            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();
        }
Ejemplo n.º 10
0
        public IEnumerator Signaling()
        {
            offerPc.OnIceCandidate  = candidate => answerPc.AddIceCandidate(candidate);
            answerPc.OnIceCandidate = candidate => offerPc.AddIceCandidate(candidate);

            var pc1CreateOffer = offerPc.CreateOffer();

            yield return(pc1CreateOffer);

            Assert.That(pc1CreateOffer.IsError, Is.False, () => $"Failed {nameof(pc1CreateOffer)}, error:{pc1CreateOffer.Error.message}");
            var offerDesc = pc1CreateOffer.Desc;

            var pc1SetLocalDescription = offerPc.SetLocalDescription(ref offerDesc);

            yield return(pc1SetLocalDescription);

            Assert.That(pc1SetLocalDescription.IsError, Is.False, () => $"Failed {nameof(pc1SetLocalDescription)}, error:{pc1SetLocalDescription.Error.message}");

            var pc2SetRemoteDescription = answerPc.SetRemoteDescription(ref offerDesc);

            yield return(pc2SetRemoteDescription);

            Assert.That(pc2SetRemoteDescription.IsError, Is.False, () => $"Failed {nameof(pc2SetRemoteDescription)}, error:{pc2SetRemoteDescription.Error.message}");

            var pc2CreateAnswer = answerPc.CreateAnswer();

            yield return(pc2CreateAnswer);

            Assert.That(pc2CreateAnswer.IsError, Is.False, () => $"Failed {nameof(pc2CreateAnswer)}, error:{pc2CreateAnswer.Error.message}");
            var answerDesc = pc2CreateAnswer.Desc;

            var pc2SetLocalDescription = answerPc.SetLocalDescription(ref answerDesc);

            yield return(pc2SetLocalDescription);

            Assert.That(pc2SetLocalDescription.IsError, Is.False, () => $"Failed {nameof(pc2SetLocalDescription)}, error:{pc2SetLocalDescription.Error.message}");

            var pc1SetRemoteDescription = offerPc.SetRemoteDescription(ref answerDesc);

            yield return(pc1SetRemoteDescription);

            Assert.That(pc1SetRemoteDescription.IsError, Is.False, () => $"Failed {nameof(pc1SetRemoteDescription)}, error:{pc1SetRemoteDescription.Error.message}");

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

            yield return(waitConnectOfferPc);

            Assert.That(waitConnectOfferPc.IsCompleted, Is.True);

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

            yield return(waitConnectAnswerPc);

            Assert.That(waitConnectAnswerPc.IsCompleted, Is.True);

            var checkSenders = new WaitUntilWithTimeout(() => offerPc.GetSenders().Any(), 5000);

            yield return(checkSenders);

            Assert.That(checkSenders.IsCompleted, Is.True);

            var checkReceivers = new WaitUntilWithTimeout(() => answerPc.GetReceivers().Any(), 5000);

            yield return(checkReceivers);

            Assert.That(checkReceivers.IsCompleted, Is.True);
        }
Ejemplo n.º 11
0
        public IEnumerator SendAndReceiveMessageWithExecuteTasks()
        {
            var test  = new MonoBehaviourTest <SignalingPeers>();
            var label = "test";

            RTCDataChannel channel1 = test.component.CreateDataChannel(0, label);

            Assert.That(channel1, Is.Not.Null);
            yield return(test);

            var op1 = new WaitUntilWithTimeout(() => test.component.GetDataChannelList(1).Count > 0, 5000);

            yield return(op1);

            RTCDataChannel channel2 = test.component.GetDataChannelList(1)[0];

            Assert.That(channel2, Is.Not.Null);

            Assert.That(channel1.ReadyState, Is.EqualTo(RTCDataChannelState.Open));
            Assert.That(channel2.ReadyState, Is.EqualTo(RTCDataChannelState.Open));
            Assert.That(channel1.Label, Is.EqualTo(channel2.Label));
            Assert.That(channel1.Id, Is.EqualTo(channel2.Id));

            // send string
            const int    millisecondTimeout = 5000;
            const string message1           = "hello";
            string       message2           = null;

            channel2.OnMessage = bytes => { message2 = System.Text.Encoding.UTF8.GetString(bytes); };
            channel1.Send(message1);
            ExecutePendingTasksWithTimeout(ref message2, millisecondTimeout);
            Assert.That(message1, Is.EqualTo(message2));

            // send byte array
            byte[] message3 = { 1, 2, 3 };
            byte[] message4 = null;
            channel2.OnMessage = bytes => { message4 = bytes; };
            channel1.Send(message3);
            ExecutePendingTasksWithTimeout(ref message4, millisecondTimeout);
            Assert.That(message3, Is.EqualTo(message4));

            // Native Collections Tests
            Vector3[] structData = { Vector3.one, Vector3.zero, Vector3.up, Vector3.down };
            using (var nativeArray = new NativeArray <Vector3>(structData, Allocator.Temp))
            {
                var nativeArrayTestMessageReceiver = default(byte[]);
                channel2.OnMessage = bytes => { nativeArrayTestMessageReceiver = bytes; };

                // Native Array
                var message5 = nativeArray;
                Assert.That(message5.IsCreated, Is.True);
                nativeArrayTestMessageReceiver = null;
                channel1.Send(message5);
                ExecutePendingTasksWithTimeout(ref nativeArrayTestMessageReceiver, millisecondTimeout);
                Assert.That(NativeArrayMemCmp(message5, nativeArrayTestMessageReceiver), Is.True, "Elements of the received message are not the same as the original message.");

                // Native Slice
                var message6 = nativeArray.Slice();
                nativeArrayTestMessageReceiver = null;
                channel1.Send(message6);
                ExecutePendingTasksWithTimeout(ref nativeArrayTestMessageReceiver, millisecondTimeout);
                Assert.That(NativeArrayMemCmp(message6, nativeArrayTestMessageReceiver), Is.True, "Elements of the received message are not the same as the original message.");

#if UNITY_2021_1_OR_NEWER
                // NativeArray.ReadOnly
                var message7 = nativeArray.AsReadOnly();
                nativeArrayTestMessageReceiver = null;
                channel1.Send(message7);
                ExecutePendingTasksWithTimeout(ref nativeArrayTestMessageReceiver, millisecondTimeout);
                Assert.That(NativeArrayMemCmp(message7, nativeArrayTestMessageReceiver), Is.True, "Elements of the received message are not the same as the original message.");
#endif // UNITY_2021_1_OR_NEWER
            }

            test.component.Dispose();
            Object.DestroyImmediate(test.gameObject);
        }
Ejemplo n.º 12
0
        public IEnumerator SendAndReceiveMessage()
        {
            var test  = new MonoBehaviourTest <SignalingPeers>();
            var label = "test";

            RTCDataChannel channel1 = test.component.CreateDataChannel(0, label);

            Assert.That(channel1, Is.Not.Null);
            yield return(test);

            var op1 = new WaitUntilWithTimeout(() => test.component.GetDataChannelList(1).Count > 0, 5000);

            yield return(op1);

            RTCDataChannel channel2 = test.component.GetDataChannelList(1)[0];

            Assert.That(channel2, Is.Not.Null);

            Assert.That(channel1.ReadyState, Is.EqualTo(RTCDataChannelState.Open));
            Assert.That(channel2.ReadyState, Is.EqualTo(RTCDataChannelState.Open));
            Assert.That(channel1.Label, Is.EqualTo(channel2.Label));
            Assert.That(channel1.Id, Is.EqualTo(channel2.Id));

            // send string
            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.That(op10.IsCompleted, Is.True);
            Assert.That(message1, Is.EqualTo(message2));

            // send byte array
            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.That(op11.IsCompleted, Is.True);
            Assert.That(message3, Is.EqualTo(message4));

            // Native Array

            // Native Arrays that are declared in tests that use IEnumerator seem to have some oddities about them
            // they tend to dispose themselves on yields so we recreate the array as needed.

            byte[] comparisonBuffer = { 1, 2, 3 };
            var    nativeArrayTestMessageReceiver = default(byte[]);

            using (var message5 = new NativeArray <byte>(comparisonBuffer, Allocator.Temp))
            {
                Assert.That(message5.IsCreated, Is.True);
                // Only needs to be set once as it will be reused.
                channel2.OnMessage = bytes => { nativeArrayTestMessageReceiver = bytes; };
                channel1.Send(message5);
            }
            var op12 = new WaitUntilWithTimeout(() => nativeArrayTestMessageReceiver != null, 5000);

            yield return(op12);

            Assert.That(op12.IsCompleted, Is.True);
            Assert.That(comparisonBuffer, Is.EqualTo(nativeArrayTestMessageReceiver));

            // Native Slice
            using (var nativeArray = new NativeArray <byte>(comparisonBuffer, Allocator.Temp))
            {
                Assert.That(nativeArray.IsCreated, Is.True);
                var message6 = nativeArray.Slice();
                nativeArrayTestMessageReceiver = null;
                channel1.Send(message6);
            }
            var op13 = new WaitUntilWithTimeout(() => nativeArrayTestMessageReceiver != null, 5000);

            yield return(op13);

            Assert.That(op13.IsCompleted, Is.True);
            Assert.That(comparisonBuffer, Is.EqualTo(nativeArrayTestMessageReceiver));

#if UNITY_2021_1_OR_NEWER
            // NativeArray.ReadOnly
            using (var nativeArray = new NativeArray <byte>(comparisonBuffer, Allocator.Temp))
            {
                Assert.That(nativeArray.IsCreated, Is.True);
                var message7 = nativeArray.AsReadOnly();
                nativeArrayTestMessageReceiver = null;
                channel1.Send(message7);
            }
            var op14 = new WaitUntilWithTimeout(() => nativeArrayTestMessageReceiver != null, 5000);
            yield return(op14);

            Assert.That(op14.IsCompleted, Is.True);
            Assert.That(comparisonBuffer, Is.EqualTo(nativeArrayTestMessageReceiver));
#endif // UNITY_2020_1_OR_NEWER

            test.component.Dispose();
            Object.DestroyImmediate(test.gameObject);
        }
Ejemplo n.º 13
0
        private static IEnumerator SignalingPeers(RTCPeerConnection offerPc, RTCPeerConnection answerPc)
        {
            offerPc.OnIceCandidate  = candidate => answerPc.AddIceCandidate(ref candidate);
            answerPc.OnIceCandidate = candidate => offerPc.AddIceCandidate(ref candidate);

            var offerOption = new RTCOfferOptions {
                offerToReceiveVideo = true
            };
            var answerOption = new RTCAnswerOptions {
                iceRestart = false
            };

            var pc1CreateOffer = offerPc.CreateOffer(ref offerOption);

            yield return(pc1CreateOffer);

            Assert.False(pc1CreateOffer.IsError);
            var offerDesc = pc1CreateOffer.Desc;

            var pc1SetLocalDescription = offerPc.SetLocalDescription(ref offerDesc);

            yield return(pc1SetLocalDescription);

            Assert.False(pc1SetLocalDescription.IsError);

            var pc2SetRemoteDescription = answerPc.SetRemoteDescription(ref offerDesc);

            yield return(pc2SetRemoteDescription);

            Assert.False(pc2SetRemoteDescription.IsError);

            var pc2CreateAnswer = answerPc.CreateAnswer(ref answerOption);

            yield return(pc2CreateAnswer);

            Assert.False(pc2CreateAnswer.IsError);
            var answerDesc = pc2CreateAnswer.Desc;

            var pc2SetLocalDescription = answerPc.SetLocalDescription(ref answerDesc);

            yield return(pc2SetLocalDescription);

            Assert.False(pc2SetLocalDescription.IsError);

            var pc1SetRemoteDescription = offerPc.SetRemoteDescription(ref answerDesc);

            yield return(pc1SetRemoteDescription);

            Assert.False(pc1SetRemoteDescription.IsError);

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

            yield return(waitConnectOfferPc);

            Assert.True(waitConnectOfferPc.IsCompleted);

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

            yield return(waitConnectAnswerPc);

            Assert.True(waitConnectAnswerPc.IsCompleted);

            var checkSenders = new WaitUntilWithTimeout(() => offerPc.GetSenders().Any(), 5000);

            yield return(checkSenders);

            Assert.True(checkSenders.IsCompleted);

            var checkReceivers = new WaitUntilWithTimeout(() => answerPc.GetReceivers().Any(), 5000);

            yield return(checkReceivers);

            Assert.True(checkReceivers.IsCompleted);
        }
Ejemplo n.º 14
0
        public IEnumerator PeerConnectionStateChange()
        {
            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);

            RTCPeerConnectionState state1    = default;
            RTCPeerConnectionState state2    = default;
            RTCIceConnectionState  iceState1 = default;
            RTCIceConnectionState  iceState2 = default;

            peer1.OnIceCandidate          = candidate => { peer2.AddIceCandidate(candidate); };
            peer2.OnIceCandidate          = candidate => { peer1.AddIceCandidate(candidate); };
            peer1.OnConnectionStateChange = state => { state1 = state; };
            peer2.OnConnectionStateChange = state => { state2 = state; };
            peer1.OnIceConnectionChange   = state => { iceState1 = state; };
            peer2.OnIceConnectionChange   = state => { iceState2 = state; };

            Assert.That(state1, Is.EqualTo(RTCPeerConnectionState.New));
            Assert.That(state2, Is.EqualTo(RTCPeerConnectionState.New));

            MediaStream stream = Audio.CaptureStream();

            peer1.AddTrack(stream.GetTracks().First());

            var op1 = peer1.CreateOffer();

            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();

            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(() =>
                                               state1 == RTCPeerConnectionState.Connected &&
                                               state2 == RTCPeerConnectionState.Connected, 5000);

            yield return(op7);

            Assert.That(op7.IsCompleted, Is.True);

            var op8 = new WaitUntilWithTimeout(() =>
                                               (iceState1 == RTCIceConnectionState.Connected || iceState1 == RTCIceConnectionState.Completed) &&
                                               (iceState2 == RTCIceConnectionState.Connected || iceState2 == RTCIceConnectionState.Completed)
                                               , 5000);

            yield return(op8);

            Assert.That(op8.IsCompleted, Is.True);

            peer1.Close();

            var op9 = new WaitUntilWithTimeout(() =>
                                               state1 == RTCPeerConnectionState.Closed &&
                                               iceState2 == RTCIceConnectionState.Disconnected, 5000);

            yield return(op9);

            Assert.That(op9.IsCompleted, Is.True);

            stream.Dispose();
            peer2.Close();
        }
Ejemplo n.º 15
0
        public IEnumerator AddIceCandidate()
        {
            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);

            var peer1ReceiveCandidateQueue = new Queue <RTCIceCandidate>();
            var peer2ReceiveCandidateQueue = new Queue <RTCIceCandidate>();

            peer1.OnIceCandidate = candidate => { peer2ReceiveCandidateQueue.Enqueue(candidate); };
            peer2.OnIceCandidate = candidate => { peer1ReceiveCandidateQueue.Enqueue(candidate); };

            MediaStream stream = Audio.CaptureStream();

            peer1.AddTrack(stream.GetTracks().First());

            var op1 = peer1.CreateOffer();

            yield return(op1);

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

            yield return(op2);

            yield return(new WaitUntil(() => peer2ReceiveCandidateQueue.Any()));

            Assert.That(peer2.AddIceCandidate(peer2ReceiveCandidateQueue.Peek()), Is.False);

            var op3 = peer2.SetRemoteDescription(ref desc);

            yield return(op3);

            Assert.That(peer2.AddIceCandidate(peer2ReceiveCandidateQueue.Dequeue()), Is.True);

            var op4 = peer2.CreateAnswer();

            yield return(op4);

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

            yield return(op5);

            yield return(new WaitUntil(() => peer1ReceiveCandidateQueue.Any()));

            Assert.That(peer1.AddIceCandidate(peer1ReceiveCandidateQueue.Peek()), Is.False);

            var op6 = peer1.SetRemoteDescription(ref desc);

            yield return(op6);

            Assert.That(peer1.AddIceCandidate(peer1ReceiveCandidateQueue.Dequeue()), Is.True);

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

            yield return(op7);

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

            yield return(op8);

            Assert.That(op8.IsCompleted, Is.True);

            foreach (var candidate in peer1ReceiveCandidateQueue)
            {
                Assert.That(peer1.AddIceCandidate(candidate), Is.True);
            }

            peer1ReceiveCandidateQueue.Clear();

            foreach (var candidate in peer2ReceiveCandidateQueue)
            {
                Assert.That(peer2.AddIceCandidate(candidate), Is.True);
            }

            peer2ReceiveCandidateQueue.Clear();

            stream.Dispose();
            peer1.Close();
            peer2.Close();
        }