Ejemplo n.º 1
0
        public void Connect(string url)
        {
            if (!string.IsNullOrEmpty(mHost))
            {
                throw new Exception("The host has connected. Please disconnect at first.");
            }

            mHost = url;

            var conf = GetSelectedSdpSemantics();

            mPeer = new RTCPeerConnection(ref conf);
            mPeer.OnIceCandidate        = OnIceCandidate;
            mPeer.OnIceConnectionChange = OnIceConnectionChange;
            mPeer.OnDataChannel         = OnDataChannel;

            DebugUtility.Log(LoggerTags.Online, "Create peer connection : {0}", host);

            var init = new RTCDataChannelInit(true);

            mSendDataChannel         = mPeer.CreateDataChannel("data", ref init);
            mSendDataChannel.OnOpen  = OnDataChannelOpen;
            mSendDataChannel.OnClose = OnDataChannelClose;

            currentTask = StartCoroutine(StartCreationWorkflow(mPeer));
        }
Ejemplo n.º 2
0
    public void DataChannel_CreateDataChannel()
    {
        RTCConfiguration config = default;

        config.iceServers = new RTCIceServer[]
        {
            new RTCIceServer
            {
                urls = new string[] { "stun:stun.l.google.com:19302" }
            }
        };
        var peer     = new RTCPeerConnection(ref config);
        var option1  = new RTCDataChannelInit(true);
        var channel1 = peer.CreateDataChannel("test1", ref option1);

        Assert.AreEqual("test1", channel1.Label);

        var option2  = new RTCDataChannelInit(false);
        var channel2 = peer.CreateDataChannel("test2", ref option2);

        Assert.AreEqual("test2", channel2.Label);

        // It is return -1 when channel is not connected.
        Assert.AreEqual(channel1.Id, -1);
        Assert.AreEqual(channel2.Id, -1);

        peer.Close();
    }
Ejemplo n.º 3
0
 public IRTCDataChannel CreateDataChannel(string label, RTCDataChannelInit options = null) =>
 RTCDataChannel.Create(JsRuntime, JsRuntime.CallJsMethod <JsObjectRef>(NativeObject, "createDataChannel",
                                                                       new object[]
 {
     label,
     options
 }));
        public void CreateDataChannelWithOption()
        {
            RTCConfiguration config = default;

            config.iceServers = new[] { new RTCIceServer {
                                            urls = new[] { "stun:stun.l.google.com:19302" }
                                        } };
            var peer    = new RTCPeerConnection(ref config);
            var options = new RTCDataChannelInit
            {
                id                = 231,
                maxRetransmits    = 1,
                maxPacketLifeTime = null,
                negotiated        = false,
                ordered           = false,
                protocol          = ""
            };
            var channel1 = peer.CreateDataChannel("test1", options);

            Assert.AreEqual("test1", channel1.Label);
            Assert.AreEqual("", channel1.Protocol);
            Assert.NotZero(channel1.MaxRetransmitTime);
            Assert.NotZero(channel1.MaxRetransmits);
            Assert.False(channel1.Ordered);
            Assert.False(channel1.Negotiated);

            // It is return -1 when channel is not connected.
            Assert.AreEqual(channel1.Id, -1);

            channel1.Close();
            peer.Close();
        }
Ejemplo n.º 5
0
        private void InitializeWebRtc()
        {
            WebRTC.Initialize();

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

            _peerConnection = new RTCPeerConnection(ref config);
            _peerConnection.OnIceConnectionChange = state => Debug.LogError("State changed: " + state);
            Debug.LogError("PeerConnection created.");

            var dcInit = new RTCDataChannelInit(true);

            DataChannel         = _peerConnection.CreateDataChannel("dataChannel", ref dcInit);
            DataChannel.OnOpen  = () => Debug.LogError("Data channel opened.");
            DataChannel.OnClose = () => Debug.LogError("Data channel closed.");

            DataChannel.OnMessage = bytes => Debug.LogError("Data channel received data: " + Encoding.UTF8.GetString(bytes));
            Debug.LogError("Data channel created.");
        }
Ejemplo n.º 6
0
        public RTCDataChannel CreateDataChannel(int indexPeer, string label, RTCDataChannelInit option = null)
        {
            RTCDataChannel channel = peers[indexPeer].CreateDataChannel(label, option);

            dataChannels[peers[indexPeer]].Add(channel);
            return(channel);
        }
Ejemplo n.º 7
0
    private void Call()
    {
        callButton.interactable = false;
        Debug.Log("GetSelectedSdpSemantics");
        var configuration = GetSelectedSdpSemantics();

        _pc1 = new RTCPeerConnection(ref configuration);
        Debug.Log("Created local peer connection object pc1");
        _pc1.OnIceCandidate        = pc1OnIceCandidate;
        _pc1.OnIceConnectionChange = pc1OnIceConnectionChange;
        _pc1.OnNegotiationNeeded   = pc1OnNegotiationNeeded;
        _pc2 = new RTCPeerConnection(ref configuration);
        Debug.Log("Created remote peer connection object pc2");
        _pc2.OnIceCandidate        = pc2OnIceCandidate;
        _pc2.OnIceConnectionChange = pc2OnIceConnectionChange;
        _pc2.OnNegotiationNeeded   = pc2OnNegotiationNeeded;
        _pc2.OnTrack = pc2Ontrack;

        RTCDataChannelInit conf = new RTCDataChannelInit(true);

        _pc1.CreateDataChannel("data", ref conf);
        audioStream     = Audio.CaptureStream();
        videoStream     = cam.CaptureStream(1280, 720, 1000000);
        RtImage.texture = cam.targetTexture;
    }
Ejemplo n.º 8
0
    IEnumerator Call()
    {
        callButton.interactable = false;
        Debug.Log("GetSelectedSdpSemantics");
        var configuration = GetSelectedSdpSemantics();

        pc1 = new RTCPeerConnection(ref configuration);
        Debug.Log("Created local peer connection object pc1");
        pc1.OnIceCandidate        = pc1OnIceCandidate;
        pc1.OnIceConnectionChange = pc1OnIceConnectionChange;
        pc2 = new RTCPeerConnection(ref configuration);
        Debug.Log("Created remote peer connection object pc2");
        pc2.OnIceCandidate        = pc2OnIceCandidate;
        pc2.OnIceConnectionChange = pc2OnIceConnectionChange;
        pc2.OnDataChannel         = onDataChannel;

        RTCDataChannelInit conf = new RTCDataChannelInit(true);

        dataChannel        = pc1.CreateDataChannel("data", ref conf);
        dataChannel.OnOpen = onDataChannelOpen;

        Debug.Log("pc1 createOffer start");
        var op = pc1.CreateOffer(ref OfferOptions);

        yield return(op);

        if (!op.IsError)
        {
            yield return(StartCoroutine(OnCreateOfferSuccess(op.Desc)));
        }
        else
        {
            OnCreateSessionDescriptionError(op.Error);
        }
    }
Ejemplo n.º 9
0
 public static Webrtc.RTCDataChannelConfiguration ToNative(this RTCDataChannelInit dataChannelInit) =>
 new Webrtc.RTCDataChannelConfiguration
 {
     IsOrdered      = dataChannelInit.Ordered ?? true,
     MaxRetransmits = dataChannelInit.MaxRetransmits ?? -1,
     Protocol       = dataChannelInit.Protocol ?? string.Empty,
     IsNegotiated   = dataChannelInit.Negotiated ?? false,
     ChannelId      = dataChannelInit.Id ?? 1//WebRTCme.WebRtc.Id
 };
Ejemplo n.º 10
0
        public void CreateDataChannel()
        {
            RTCDataChannelInit DataChannelInit = new RTCDataChannelInit();
            var            _dataChannel        = PeerConnection.CreateDataChannel("Deneme", DataChannelInit);
            RTCDataChannel rTCDataChannel      = (RTCDataChannel)_dataChannel;

            _rTCDataChannel         = rTCDataChannel;
            _rTCDataChannel.OnOpen += _rTCDataChannel_OnOpen;
        }
Ejemplo n.º 11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public RTCDataChannel CreateChannel(string connectionId, string name)
        {
            RTCDataChannelInit conf = new RTCDataChannelInit();

            if (string.IsNullOrEmpty(name))
            {
                name = Guid.NewGuid().ToString();
            }
            return(_mapConnectionIdAndPeer[connectionId].peer.CreateDataChannel(name, conf));
        }
Ejemplo n.º 12
0
 public static Webrtc.DataChannel.Init ToNative(this RTCDataChannelInit dataChannelInit) =>
 new Webrtc.DataChannel.Init
 {
     Ordered             = dataChannelInit.Ordered ?? true,
     MaxRetransmitTimeMs = dataChannelInit.MaxPacketLifeTime ?? -1,
     MaxRetransmits      = dataChannelInit.MaxRetransmits ?? -1,
     Protocol            = dataChannelInit.Protocol ?? string.Empty,
     Negotiated          = dataChannelInit.Negotiated ?? false,
     Id = dataChannelInit.Id ?? 1    //WebRTCme.WebRtc.Id
 };
    private void createPeer()
    {
        log(LogLevel.Log, "Create RTCPeerConnection");
        var peerConfig = new RTCConfiguration {
            iceServers = iceServers
        };

        peer = new RTCPeerConnection(ref peerConfig);
        peer.OnConnectionStateChange = connectionState =>
        {
            log(LogLevel.Log, $"[OnConnectionStateChange] connectionState: {connectionState}");
        };
        peer.OnDataChannel = channel =>
        {
            dataChannel = channel;
            setupDataChannelEventHandler();
            log(LogLevel.Log, $"[OnDataChannel] label: {channel.Label}");
        };
        peer.OnIceCandidate = candidate =>
        {
            log(LogLevel.Log, $"[OnIceCandidate]");
            log(LogLevel.Log, $">>> Send \"takeCandidate\" Command (iceCandidate: '{candidate.Candidate.Substring(0, 10)} ...')");
            signaling.SendIceCandidate(streamId, candidate.Candidate, candidate.SdpMLineIndex.Value, candidate.SdpMid);
        };
        peer.OnIceGatheringStateChange = state =>
        {
            log(LogLevel.Log, $"[OnIceGatheringStateChange] iceGatheringState: {state}");
        };
        peer.OnNegotiationNeeded = () =>
        {
            log(LogLevel.Log, $"[OnNegotiationNeeded]");
        };
        peer.OnTrack = evt =>
        {
            log(LogLevel.Log, $"[OnTrack] kind: {evt.Track.Kind}");
            if (evt.Track is VideoStreamTrack track)
            {
                var texture = track.InitializeReceiver(videoWidth, videoHeight);
                playerDisplay.GetComponent <Renderer>().material.mainTexture = texture;
            }
        };

        var dcOptions = new RTCDataChannelInit();

        log(LogLevel.Log, $"CreateDataChannel label: {dataChannelLabel}");
        dataChannel = peer.CreateDataChannel(dataChannelLabel, dcOptions);
        setupDataChannelEventHandler();
        if (clientType == ClientType.Publisher)
        {
            var videoTrack = new VideoStreamTrack("VideoTrack", videoPlayer.targetTexture);
            peer.AddTrack(videoTrack);
            StartCoroutine(createDesc(RTCSdpType.Offer));
        }
    }
Ejemplo n.º 14
0
        public void CreateAndDeleteDataChannel()
        {
            var context = NativeMethods.ContextCreate(0, encoderType);
            var peer    = NativeMethods.ContextCreatePeerConnection(context);
            var init    = new RTCDataChannelInit(true);
            var channel = NativeMethods.ContextCreateDataChannel(context, peer, "test", ref init);

            NativeMethods.ContextDeleteDataChannel(context, channel);
            NativeMethods.ContextDeletePeerConnection(context, peer);
            NativeMethods.ContextDestroy(0);
        }
Ejemplo n.º 15
0
        public void CreateAndDeleteDataChannel()
        {
            var context    = Context.Create();
            var peerPtr    = context.CreatePeerConnection();
            var init       = new RTCDataChannelInit(true);
            var channelPtr = context.CreateDataChannel(peerPtr, "test", ref init);

            context.DeleteDataChannel(channelPtr);
            context.DeletePeerConnection(peerPtr);
            context.Dispose();
        }
Ejemplo n.º 16
0
        public void CreateAndDeleteDataChannel()
        {
            var value   = NativeMethods.GetHardwareEncoderSupport();
            var context = Context.Create(
                encoderType: value ? EncoderType.Hardware : EncoderType.Software);
            var peerPtr    = context.CreatePeerConnection();
            var init       = new RTCDataChannelInit(true);
            var channelPtr = context.CreateDataChannel(peerPtr, "test", ref init);

            context.DeleteDataChannel(channelPtr);
            context.DeletePeerConnection(peerPtr);
            context.Dispose();
        }
Ejemplo n.º 17
0
        private void connectPeer()
        {
            OnLogEvent.Invoke("new RTCPeerConnection", "");
            peer = new RTCPeerConnection(ref peerConfig);
            peer.OnConnectionStateChange = connectionState =>
            {
                OnLogEvent.Invoke("OnConnectionStateChange", connectionState.ToString());
            };
            peer.OnDataChannel = channel =>
            {
                dataChannel = channel;
                setupDataChannelEventHandler();
                OnLogEvent.Invoke("OnDataChannel", channel.Label);
            };
            peer.OnIceCandidate = candidate =>
            {
                OnLogEvent.Invoke("OnIceCandidate", "");
                OnLogEvent.Invoke("Send IceCandidate", "");
                signaling.SendIceCandidate(streamId, candidate.Candidate, candidate.SdpMLineIndex.Value, candidate.SdpMid);
            };
            peer.OnIceGatheringStateChange = state =>
            {
                OnLogEvent.Invoke("OnIceGatheringStateChange", state.ToString());
            };
            peer.OnNegotiationNeeded = () =>
            {
                OnLogEvent.Invoke("OnNegotiationNeeded", "");
            };
            peer.OnTrack = evt =>
            {
                OnLogEvent.Invoke("OnTrack", evt.Track.Kind.ToString());
                if (evt.Track is VideoStreamTrack track)
                {
                    var texture = track.InitializeReceiver(videoWidth, videoHeight);
                    OnVideoTrack.Invoke(texture);
                }
            };

            var dcOptions = new RTCDataChannelInit();

            OnLogEvent.Invoke("CreateDataChannel", "testDC");
            dataChannel = peer.CreateDataChannel("testDC", dcOptions);
            setupDataChannelEventHandler();
            if (clientType == ClientType.Publisher)
            {
                var videoTrack = new VideoStreamTrack("VideoTrack", renderTexture);
                peer.AddTrack(videoTrack);
                CoroutineHandler.StartStaticCoroutine(sendDesc(RTCSdpType.Offer));
            }
        }
Ejemplo n.º 18
0
        public void CreateDataChannelFailed()
        {
            RTCConfiguration config = default;

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

            RTCDataChannelInit option1 = default;

            Assert.Throws <System.ArgumentException>(() => peer.CreateDataChannel("test1", ref option1));
            peer.Close();
        }
Ejemplo n.º 19
0
    //ピアの作成をする
    void CreatePeer()
    {
        //ローカル
        RTCConfiguration pc_config = new RTCConfiguration();
        var server = new RTCIceServer();

        server.urls          = new string[] { "stun:stun.webrtc.ecl.ntt.com:3478" };
        pc_config.iceServers = new RTCIceServer[] {
            server
        };
        localConnection = new RTCPeerConnection(ref pc_config);
        //データチャネルの作成
        RTCDataChannelInit conf = new RTCDataChannelInit(true);

        localDataChannel         = localConnection.CreateDataChannel("send", ref conf);
        localDataChannel.OnOpen  = new DelegateOnOpen(() => { Debug.Log("データチャネル:localOpen"); });
        localDataChannel.OnClose = new DelegateOnClose(() => { Debug.Log("データチャネル:localClose"); });
        //メディアストリームの追加(現在のバージョンだとメディアストリームは1つまでらしい)
        if (_rtcType == RTCTYPE.OFFER)
        {
            _videoStream = _streamCamera.CaptureStream(800, 450);
        }

        localConnection.OnDataChannel = new DelegateOnDataChannel(x => {
            Debug.Log("ondatachannel");
            remoteDataChannel           = x;
            remoteDataChannel.OnMessage = onDataChannelMessage;
        });
        localConnection.OnTrack = new DelegateOnTrack(x => {
            x.Track;
            //PlayVideo(x.Track);
        });
        localConnection.OnIceConnectionChange =
            new DelegateOnIceConnectionChange(state => { OnIceConnectionChange(localConnection, state); });

        //ICEの登録
        localConnection.OnIceCandidate = new DelegateOnIceCandidate(candidate => {
            if (!string.IsNullOrEmpty(candidate.candidate))
            {
                _localIceCandidate.Add(candidate);
                Debug.Log("アイス:add my Ice" + candidate.candidate);
            }
            else
            {
                Debug.Log("end ice candidate");
            }
        });

        Debug.Log("crete peer");
    }
Ejemplo n.º 20
0
        public IEnumerator SetRemoteDescription()
        {
            var config   = GetConfiguration();
            var peer1    = new RTCPeerConnection(ref config);
            var peer2    = new RTCPeerConnection(ref config);
            var conf     = new RTCDataChannelInit(true);
            var channel1 = peer1.CreateDataChannel("data", ref conf);

            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 desc2 = peer1.RemoteDescription;

            Assert.AreEqual(desc.sdp, desc2.sdp);
            Assert.AreEqual(desc.type, desc2.type);

            channel1.Dispose();
            peer1.Close();
            peer2.Close();
            peer1.Dispose();
            peer2.Dispose();
        }
Ejemplo n.º 21
0
        public IRTCDataChannel CreateDataChannel(string label, RTCDataChannelInit options)// =>
        {
            var nativeOptions = options.ToNative();

            var dataChannel =
                RTCDataChannel.Create(Webrtc.RTCPeerConnection_DataChannel.DataChannelForLabel(
                                          (Webrtc.RTCPeerConnection)NativeObject,
                                          //            RTCDataChannel.Create(((Webrtc.RTCPeerConnection)NativeObject).DataChannelForLabel(
                                          label,
//    config
                                          //options.ToNative()
                                          nativeOptions
                                          ));


            return(dataChannel);
        }
        public IEnumerator PeerConnection_SetRemoteDescription()
        {
            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 channel1 = null;

            var conf = new RTCDataChannelInit(true);

            channel1 = peer1.CreateDataChannel("data", ref conf);

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

            yield return(op1);

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

            yield return(op2);

            var op3 = peer2.SetRemoteDescription(ref op1.desc);

            yield return(op3);

            var op4 = peer2.CreateAnswer(ref options2);

            yield return(op4);

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

            yield return(op5);

            var op6 = peer1.SetRemoteDescription(ref op4.desc);

            yield return(op6);

            channel1.Dispose();
            peer1.Dispose();
            peer2.Dispose();
        }
Ejemplo n.º 23
0
    //ピアの作成をする
    void CreatePeer()
    {
        //ローカル
        RTCConfiguration pc_config = new RTCConfiguration();
        var server = new RTCIceServer();

        server.urls          = new string[] { "stun:stun.webrtc.ecl.ntt.com:3478" };
        pc_config.iceServers = new RTCIceServer[] {
            server
        };
        localConnection = new RTCPeerConnection(ref pc_config);
        RTCDataChannelInit conf = new RTCDataChannelInit(true);

        localDataChannel         = localConnection.CreateDataChannel("send", ref conf);
        localDataChannel.OnOpen  = new DelegateOnOpen(() => { Debug.Log("データチャネル:localOpen"); });
        localDataChannel.OnClose = new DelegateOnClose(() => { Debug.Log("データチャネル:localClose"); });
        //localDataChannel.OnMessage = onDataChannelMessage;
        localConnection.OnDataChannel = new DelegateOnDataChannel(x => {
            Debug.Log("ondatachannel");
            remoteDataChannel           = x;
            remoteDataChannel.OnMessage = onDataChannelMessage;
        });
        localConnection.OnIceConnectionChange =
            new DelegateOnIceConnectionChange(state => { OnIceConnectionChange(localConnection, state); });

        //ICEの登録
        localConnection.OnIceCandidate = new DelegateOnIceCandidate(candidate => {
            if (!string.IsNullOrEmpty(candidate.candidate))
            {
                _localIceCandidate.Add(candidate);
                Debug.Log("アイス:add my Ice" + candidate.candidate);
            }
            else
            {
                Debug.Log("end ice candidate");
            }
        });

        Debug.Log("crete peer");
    }
        public void CreateDataChannelFailed()
        {
            RTCConfiguration config = default;

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

            // Cannot be set along with "maxRetransmits" and "maxPacketLifeTime"
            var options = new RTCDataChannelInit
            {
                id                = 231,
                maxRetransmits    = 1,
                maxPacketLifeTime = 1,
                negotiated        = false,
                ordered           = false,
                protocol          = ""
            };

            Assert.Throws <ArgumentException>(() => peer.CreateDataChannel("test1", options));
            peer.Close();
        }
Ejemplo n.º 25
0
        public IEnumerator CreateAnswer()
        {
            var config = GetConfiguration();

            var peer1 = new RTCPeerConnection(ref config);
            var peer2 = new RTCPeerConnection(ref config);
            var conf  = new RTCDataChannelInit(true);

            peer1.CreateDataChannel("data", ref conf);

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

            Assert.True(op4.IsDone);
            Assert.False(op4.IsError);

            peer1.Close();
            peer2.Close();
            peer1.Dispose();
            peer2.Dispose();
        }
Ejemplo n.º 26
0
        public RTCDataChannel AddDataChannel(int indexPeer)
        {
            var option = new RTCDataChannelInit(true);

            return(peers[indexPeer].CreateDataChannel("test1", ref option));
        }
Ejemplo n.º 27
0
    public IEnumerator DataChannel_EventsAreSentToOther()
    {
        RTCConfiguration config = default;

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

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

        var conf = new RTCDataChannelInit(true);

        channel1 = peer1.CreateDataChannel("data", ref conf);

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

        yield return(op1);

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

        yield return(op2);

        var op3 = peer2.SetRemoteDescription(ref op1.desc);

        yield return(op3);

        var op4 = peer2.CreateAnswer(ref options2);

        yield return(op4);

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

        yield return(op5);

        var op6 = peer1.SetRemoteDescription(ref op4.desc);

        yield return(op6);

        yield return(new WaitUntil(() => peer1.IceConnectionState == RTCIceConnectionState.Connected || peer1.IceConnectionState == RTCIceConnectionState.Completed));

        yield return(new WaitUntil(() => peer2.IceConnectionState == RTCIceConnectionState.Connected || peer2.IceConnectionState == RTCIceConnectionState.Completed));

        yield return(new WaitUntil(() => channel2 != null));

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

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

        channel2.OnMessage = new DelegateOnMessage(bytes => { message2 = System.Text.Encoding.UTF8.GetString(bytes); });
        channel1.Send(message1);
        yield return(new WaitUntil(() => !string.IsNullOrEmpty(message2)));

        Assert.AreEqual(message1, message2);

        byte[] message3 = { 1, 2, 3 };
        byte[] message4 = null;
        channel2.OnMessage = new DelegateOnMessage(bytes => { message4 = bytes; });
        channel1.Send(message3);
        yield return(new WaitUntil(() => message4 != null));

        Assert.AreEqual(message3, message4);

        peer1.Close();
        peer2.Close();
    }
Ejemplo n.º 28
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);
            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);
            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.º 29
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="connectionId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public RTCDataChannel CreateChannel(string connectionId, string name)
        {
            RTCDataChannelInit conf = new RTCDataChannelInit();

            return(_mapConnectionIdAndPeer[connectionId].CreateDataChannel(name, conf));
        }
Ejemplo n.º 30
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();
        }