Esempio n. 1
0
        void SetupConnectionWithPeer(string peer)
        {
            if (!peers.ContainsKey(peer))
            {
                WebRtcNative curWebRtc = new WebRtcNative();
                curWebRtc.AddServerConfig("stun:stun.l.google.com:19302", string.Empty, string.Empty);
                curWebRtc.AddServerConfig("stun:stun.anyfirewall.com:3478", string.Empty, string.Empty);
                curWebRtc.AddServerConfig("stun:stun.stunprotocol.org:3478", string.Empty, string.Empty);

                curWebRtc.OnIceCandidate += (string sdp_mid, int sdp_mline_index, string sdp) =>
                {
                    CurWebRtc_OnIceCandidate(peer, sdp_mid, sdp_mline_index, sdp);
                };
                curWebRtc.OnSuccessOffer += (string sdp) =>
                {
                    CurWebRtc_OnSuccessOffer(peer, sdp);
                };
                curWebRtc.OnSuccessAnswer += (string sdp) =>
                {
                    CurWebRtc_OnSuccessAnswer(peer, sdp);
                };
                curWebRtc.OnDataMessage += (string message) =>
                {
                    CurWebRtc_OnDataMessage(peer, message);
                };
                curWebRtc.OnDataChannel += (string label) =>
                {
                    CurWebRtc_OnDataChannel(peer, label);
                };
                curWebRtc.OnDataBinaryMessage += (byte[] data) =>
                {
                    CurWebRtc_OnDataBinaryMessage(peer, data);
                };
                curWebRtc.OnError   += CurWebRtc_OnError;
                curWebRtc.OnFailure += CurWebRtc_OnFailure;
                //curWebRtc.InitializePeerConnection();
                peers[peer] = curWebRtc;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initial WebRtc Component
        /// </summary>
        protected void RtcInitial()
        {
            try
            {
                WebRtcNative.InitializeSSL();
                ServerUrls.GetStunUrls().ForEach(url =>
                {
                    // Set Stun Servers
                    _WebRtc.AddServerConfig(url, string.Empty, string.Empty);
                });

                // Get audio control
                _WebRtc.SetAudio(true);
                _WebRtc.OpenVideoCaptureDevice("");
                // Set Video Capturer
                {
                    IVedioCaptureInfo cptr = new ScreenCaptureInfo();
                    _WebRtc.SetVideoCapturer(cptr.Width, cptr.Height, cptr.CaptureFps);
                }
                // Relative Connection callback
                {
                    // Be triggered On Success to generate Offer
                    _WebRtc.OnSuccessOffer += (sdp) =>
                    {
                        OfferSpdMsg msg = new OfferSpdMsg
                        {
                            Command = SocketMessage.OfferSpd,
                            SDP     = sdp
                        };
                        _SocketClient.Send(JsonConvert.SerializeObject(msg));
                    };

                    // Be triggered On Success to generate ICE
                    _WebRtc.OnIceCandidate += (sdp_mid, sdp_mline_index, sdp) =>
                    {
                        IceCandidateMsg msg = new IceCandidateMsg
                        {
                            Command       = SocketMessage.OnIceCandidate,
                            SDP           = sdp,
                            SdpMid        = sdp_mid,
                            SdpMlineIndex = sdp_mline_index,
                        };
                        _SocketClient.Send(JsonConvert.SerializeObject(msg));
                    };

                    // Be triggered On OfferRequest
                    _WebRtc.OnSuccessAnswer += (sdp) =>
                    {
                        SuccessAnswerMsg msg = new SuccessAnswerMsg
                        {
                            Command = SocketMessage.OnSuccessAnswer,
                            SDP     = sdp
                        };
                        _SocketClient.Send(JsonConvert.SerializeObject(msg));
                    };
                }
                // Set Local and Remote callback
                {
                    _WebRtc.OnRenderLocal  += _OnRenderLocalCallback;
                    _WebRtc.OnRenderRemote += _OnRenderRemoteCallback;
                }
                // Set Other callback
                {
                    _WebRtc.OnSuccessAnswer     += (sdp) => { Debug.WriteLine($"Success Get SDP:{sdp}"); };
                    _WebRtc.OnFailure           += (failure) => { Debug.WriteLine($"Failure Msg:{failure}"); };
                    _WebRtc.OnError             += (error) => { Debug.WriteLine($"Error Msg:{error}"); };
                    _WebRtc.OnDataMessage       += (dmsg) => { Debug.WriteLine($"OnDataMessage: {dmsg}"); };
                    _WebRtc.OnDataBinaryMessage += (dmsg) => { Debug.WriteLine($"OnDataBinaryMessage: {dmsg.Length}"); };
                }
            }
            catch (Exception ex) { }
        }