Example #1
0
        private void SendInitialStunBindingRequest(IceCandidate iceCandidate, ManualResetEvent iceGatheringCompleteMRE)
        {
            int attempt = 1;

            while (attempt < INITIAL_STUN_BINDING_ATTEMPTS_LIMIT && !IsClosed && !iceCandidate.IsGatheringComplete)
            {
                logger.LogDebug("Sending STUN binding request " + attempt + " from " + iceCandidate.LocalRtpSocket.LocalEndPoint + " to " + iceCandidate.TurnServer.ServerEndPoint + ".");

                STUNv2Message stunRequest = new STUNv2Message(STUNv2MessageTypesEnum.BindingRequest);
                stunRequest.Header.TransactionId = Guid.NewGuid().ToByteArray().Take(12).ToArray();
                byte[] stunReqBytes = stunRequest.ToByteBuffer(null, false);

                iceCandidate.LocalRtpSocket.SendTo(stunReqBytes, iceCandidate.TurnServer.ServerEndPoint);

                Thread.Sleep(INITIAL_STUN_BINDING_PERIOD_MILLISECONDS);

                attempt++;
            }

            iceCandidate.IsGatheringComplete = true;

            // Potentially save a few seconds if all the ICE candidates are now ready.
            if (LocalIceCandidates.All(x => x.IsGatheringComplete))
            {
                iceGatheringCompleteMRE.Set();
            }
        }
        public void IceCandidateStatsData()
        {
            for (int i = 0; i < Instance.iceCandidateStatsList.Count; i++)
            {
                IceCandidateStats ics = Instance.iceCandidateStatsList[i];

                IceCandidate iceCandidate = new IceCandidate();

                iceCandidate.id            = ics.id;
                iceCandidate.type          = ics.type;
                iceCandidate.ip            = ics.ip;
                iceCandidate.port          = ics.port;
                iceCandidate.candidateType = ics.candidateType;
                iceCandidate.transport     = ics.protocol;

                if (iceCandidate.candidateType == "srflex")
                {
                    iceCandidate.candidateType = "srflx";
                }

                if (ics.type.Contains("local"))
                {
                    Instance.localIceCandidates.Add(iceCandidate);
                }

                if (ics.type.Contains("remote"))
                {
                    Instance.remoteIceCandidates.Add(iceCandidate);
                }
            }
        }
        public async Task IceCandidateSendConfigurationWebRtc(string userName, IceCandidate candidate)
        {
            string connectionId    = _sessionService.GetConnectionIdByUserName(userName);
            string userNameCurrent = _sessionService.GetUserNameByConnectionId(Context.ConnectionId);

            await Clients.Client(connectionId).IceCandidateReceivedConfigurationWebRtc(userNameCurrent, candidate);
        }
        public void SendLocalIceCandidate(IceCandidate _candidate)
        {
            executor.Execute(async() =>
            {
                var json = SignalingMessage.CreateJson(_candidate);
                if (isInitiator)
                {
                    if (App.HubConnection.State != HubConnectionState.Connected)//(State != ConnectionState.Connected)
                    {
                        ReportError("Sending ICE candidate in non connected state.");
                        return;
                    }

                    //SendPostMessage(MessageType.Message, _messageUrl, json);
                    //TODO: SignalR Send LocalIceCandidate as  Initiator
                    var isIceSent = await App.HubConnection.InvokeAsync <string>("SendLocalIceCandidate", App.HubConnection.ConnectionId, json);
                    logger.Info(TAG, $"{isIceSent}");
                }
                else
                {
                    //_wsClient.Send(json);
                    //TODO: SignalR Send LocalIceCandidate as  Participant
                    var isIceSent = await App.HubConnection.InvokeAsync <string>("SendLocalIceCandidate", App.HubConnection.ConnectionId, json);
                    logger.Info(TAG, $"{isIceSent}");
                }
            });
        }
Example #5
0
 /**
  * 收到消息
  * @param {Object} msg 消息
  */
 public void onmessage(Dictionary <string, object> msg)
 {
     if (msg.Get <string>("action") == "candidate")
     {
         var json      = msg.Get <Dictionary <string, object> >("candidate");
         var candidate = new IceCandidate(json.Get <string>("sdpMid"), json.Get <int>("sdpMLineIndex"), json.Get <string>("candidate"));
         if (this._candidates != null)
         {
             this._candidates.Add(candidate);
         }
         else
         {
             this.connection.AddIceCandidate(candidate);
         }
     }
     else
     {
         Task.Factory.StartNew(async() =>
         {
             try
             {
                 var task = this.state.onmessage(msg);
                 if (task != null)
                 {
                     await task;
                 }
             }
             catch (Exception exp)
             {
                 Console.WriteLine(exp.Message);
                 this.resetState(false);
             }
         });
     }
 }
Example #6
0
        public void MediaPacketReceived(IceCandidate iceCandidate, byte[] buffer, IPEndPoint remoteEndPoint)
        {
            if ((buffer[0] >= 128) && (buffer[0] <= 191))
            {
                //logger.Debug("A non-STUN packet was received Receiver Client.");

                if (buffer[1] == 0xC8 /* RTCP SR */ || buffer[1] == 0xC9 /* RTCP RR */)
                {
                    // RTCP packet.
                    //webRtcClient.LastSTUNReceiveAt = DateTime.Now;
                }
                else
                {
                    // RTP packet.
                    //int res = peer.SrtpReceiveContext.UnprotectRTP(buffer, buffer.Length);

                    //if (res != 0)
                    //{
                    //    logger.Warn("SRTP unprotect failed, result " + res + ".");
                    //}
                }
            }
            else
            {
                logger.Debug("An unrecognised packet was received on the WebRTC media socket.");
            }
        }
Example #7
0
        public void FabricSetupLocalCandidate(string candidateStr)
        {
            string candidate = candidateStr.Substring(candidateStr.LastIndexOf(':') + 1);

            string[] separatingChars = { " " };
            string[] words           = candidate.Split(separatingChars, StringSplitOptions.None);

            var candidateID        = words[0];
            var candidateTransport = words[2];
            var localCandidate     = words[3];
            var candidateIp        = words[4];
            var candidatePort      = words[5];
            var candidateType      = words[7];

            Debug.WriteLine($"!!!candidateID: {candidateID}, " +
                            $"candidateTransport: {candidateTransport}, " +
                            $"localCandidate: {localCandidate}, " +
                            $"candidateIp: {candidateIp}, " +
                            $"candidatePort: {candidatePort}, " +
                            $"candidateType: {candidateType}");

            IceCandidate localIceCandidateObj = new IceCandidate();

            localIceCandidateObj.id            = localCandidate;
            localIceCandidateObj.type          = "localcandidate";
            localIceCandidateObj.ip            = candidateIp;
            localIceCandidateObj.port          = int.Parse(candidatePort);
            localIceCandidateObj.candidateType = candidateType;
            localIceCandidateObj.transport     = candidateTransport;
            localIceCandidatesList.Add(localIceCandidateObj);
        }
Example #8
0
        public void AppendRemoteIceCandidate(IceCandidate remoteIceCandidate)
        {
            IPAddress candidateIPAddress = null;

            //foreach (var iceCandidate in remoteIceCandidates)
            //{
            //    logger.LogDebug("Appending remote ICE candidate " + iceCandidate.NetworkAddress + ":" + iceCandidate.Port + ".");
            //}

            if (remoteIceCandidate.Transport.ToLower() != "udp")
            {
                logger.LogDebug("Omitting remote non-UDP ICE candidate. " + remoteIceCandidate.RawString + ".");
            }
            else if (!IPAddress.TryParse(remoteIceCandidate.NetworkAddress, out candidateIPAddress))
            {
                logger.LogDebug("Omitting ICE candidate with unrecognised IP Address. " + remoteIceCandidate.RawString + ".");
            }
            else if (candidateIPAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                logger.LogDebug("Omitting IPv6 ICE candidate. " + remoteIceCandidate.RawString + ".");
            }
            else
            {
                // ToDo: Add srflx and relay endpoints as hosts as well.

                if (!_remoteIceCandidates.Any(x => x.NetworkAddress == remoteIceCandidate.NetworkAddress && x.Port == remoteIceCandidate.Port))
                {
                    logger.LogDebug("Adding remote ICE candidate: " + remoteIceCandidate.CandidateType + " " + remoteIceCandidate.NetworkAddress + ":" + remoteIceCandidate.Port + " (" + remoteIceCandidate.RawString + ").");
                    _remoteIceCandidates.Add(remoteIceCandidate);
                }
            }
        }
Example #9
0
        private void AllocateTurn(IceCandidate iceCandidate)
        {
            try
            {
                if (iceCandidate.TurnAllocateAttempts >= MAXIMUM_TURN_ALLOCATE_ATTEMPTS)
                {
                    logger.LogDebug("TURN allocation for local socket " + iceCandidate.LocalAddress + " failed after " + iceCandidate.TurnAllocateAttempts + " attempts.");

                    iceCandidate.IsGatheringComplete = true;
                }
                else
                {
                    iceCandidate.TurnAllocateAttempts++;

                    //logger.LogDebug("Sending STUN connectivity check to client " + client.SocketAddress + ".");

                    STUNv2Message stunRequest = new STUNv2Message(STUNv2MessageTypesEnum.Allocate);
                    stunRequest.Header.TransactionId = Guid.NewGuid().ToByteArray().Take(12).ToArray();
                    stunRequest.Attributes.Add(new STUNv2Attribute(STUNv2AttributeTypesEnum.Lifetime, 3600));
                    stunRequest.Attributes.Add(new STUNv2Attribute(STUNv2AttributeTypesEnum.RequestedTransport, STUNv2AttributeConstants.UdpTransportType));   // UDP
                    byte[] stunReqBytes = stunRequest.ToByteBuffer(null, false);
                    iceCandidate.LocalRtpSocket.SendTo(stunReqBytes, iceCandidate.TurnServer.ServerEndPoint);
                }
            }
            catch (Exception excp)
            {
                logger.LogError("Exception AllocateTurn. " + excp);
            }
        }
Example #10
0
        /// <summary>
        /// Entry point for the reading task which read incoming messages from the
        /// receiving pipe and dispatch them through events to the WebRTC peer.
        /// </summary>
        private void ProcessIncomingMessages()
        {
            // ReadLine() will block while waiting for a new line
            string line;

            while ((line = _recvStream.ReadLine()) != null)
            {
                Console.WriteLine($"[<-] {line}");
                if (line == "ice")
                {
                    string sdpMid        = _recvStream.ReadLine();
                    int    sdpMlineindex = int.Parse(_recvStream.ReadLine());

                    // The ICE candidate is a multi-line field, ends with an empty line
                    string candidate = "";
                    while ((line = _recvStream.ReadLine()) != null)
                    {
                        if (line.Length == 0)
                        {
                            break;
                        }
                        candidate += line;
                        candidate += "\n";
                    }

                    Console.WriteLine($"[<-] ICE candidate: {sdpMid} {sdpMlineindex} {candidate}");
                    var iceCandidate = new IceCandidate
                    {
                        SdpMid        = sdpMid,
                        SdpMlineIndex = sdpMlineindex,
                        Content       = candidate
                    };
                    IceCandidateReceived?.Invoke(iceCandidate);
                }
                else if (line == "sdp")
                {
                    string type = _recvStream.ReadLine();

                    // The SDP message content is a multi-line field, ends with an empty line
                    string sdp = "";
                    while ((line = _recvStream.ReadLine()) != null)
                    {
                        if (line.Length == 0)
                        {
                            break;
                        }
                        sdp += line;
                        sdp += "\n";
                    }

                    Console.WriteLine($"[<-] SDP message: {type} {sdp}");
                    var message = new SdpMessage {
                        Type = SdpMessage.StringToType(type), Content = sdp
                    };
                    SdpMessageReceived?.Invoke(message);
                }
            }
            Console.WriteLine("Finished processing messages");
        }
Example #11
0
 protected override void onIceCandidateEvent(object sender, IceCandidate e)
 {
     if (this.remoteMachine == null)
     {
         return;
     }
     WebSocketClient.SendMessage(this.remoteMachine.machineId, e, msgType.client_onCaller_CreateIceCandite);
 }
        static void IceCandidateCallback(IntPtr userData, IceCandidate iceCandidate)
        {
            var source = GCHandleHelper.FromIntPtr <PeerConnectionObserver>(userData);

            source?.IceCandidateAdded?.Invoke(
                source,
                new EventArgs <RTCIceCandidate>(GetRTCIceCandidate(iceCandidate)));
        }
Example #13
0
    public override Task SendMessageAsync(IceCandidate candidate)
    {
        JSONObject payload = new JSONObject();

        payload.AddField("label", candidate.SdpMlineIndex);
        payload.AddField("id", candidate.SdpMid);
        payload.AddField("candidate", candidate.Content);
        return(SendIOMessage("candidate", payload));
    }
Example #14
0
 /// <summary>
 /// Create a node-dss message from an existing ICE candidate.
 /// </summary>
 /// <param name="candidate">The ICE candidate to serialize.</param>
 /// <returns>The newly create node-dss message containing the serialized ICE candidate.</returns>
 public static Message FromIceCandidate(IceCandidate candidate)
 {
     return(new Message
     {
         MessageType = WireMessageType.Ice,
         Data = $"{candidate.Content}|{candidate.SdpMlineIndex}|{candidate.SdpMid}",
         IceDataSeparator = "|"
     });
 }
Example #15
0
        public void AddRemoteIceCandite(string mchineId, IceCandidate iceCandidate)
        {
            var find = this.beControlleds.FirstOrDefault(o => o.remoteMachine.machineId == mchineId);

            if (find != null)
            {
                find.AddRemoteCandidate(mchineId, iceCandidate);
            }
        }
 static RTCIceCandidate GetRTCIceCandidate(IceCandidate iceCandidate)
 {
     return(new RTCIceCandidate
     {
         SdpMid = iceCandidate.SdpMid,
         Candidate = iceCandidate.Sdp,
         SdpMLineIndex = iceCandidate.MLineIndex
     });
 }
 protected static string ToJsonCandidate(IceCandidate iceCandidate)
 {
     return(JsonConvert.SerializeObject(new
     {
         label = iceCandidate.SdpMLineIndex,
         id = iceCandidate.SdpMid,
         candidate = iceCandidate.Sdp
     }));
 }
Example #18
0
        /// <summary>
        /// 接收到对方的Candidate,进行处理
        /// </summary>
        /// <param name="candidate"></param>
        public void AddRemoteCandidate(string mchineId, IceCandidate iceCandidate)
        {
            var ice = JsonConvert.SerializeObject(iceCandidate);

            if (mchineId == MachineLogic.localMachine().machineId)
            {
                return;
            }
            this.PeerConnection.AddCandidate(iceCandidate);
        }
Example #19
0
 public IceCandidateMessage(IceCandidate iceCandidate)
 {
     MessageType  = SignalingMessageType.SendCandidate;
     IceCandidate = new IceCandidateEx
     {
         Sdp           = iceCandidate.Sdp,
         SdpMid        = iceCandidate.SdpMid,
         SdpMLineIndex = iceCandidate.SdpMLineIndex
     };
 }
Example #20
0
        private void RegisterIceCandidateCallback()
        {
            _webRtcIceCandidateCallback = (handle, candidate, _) =>
            {
                IceCandidate?.Invoke(this, new WebRTCIceCandidateEventArgs(candidate));
            };

            NativeWebRTC.SetIceCandidateCb(Handle, _webRtcIceCandidateCallback).
            ThrowIfFailed("Failed to set ice candidate callback.");
        }
Example #21
0
            /// <summary>
            /// Factory method used to create a new IceCandidate object.
            /// </summary>
            /// <param name="candidate">The candidate id.</param>
            /// <param name="sdpMid">The id of the source media component from which the candidate draws data.</param>
            /// <param name="sdpMLineIndex">Index that indicates which media source is associated with a candidate.</param>
            /// <returns>An ice candidate object with the given handle.</returns>
            public static IceCandidate Create(string candidate = "", string sdpMid = "", int sdpMLineIndex = 0)
            {
                IceCandidate iceCandidate = new IceCandidate()
                {
                    Candidate     = candidate,
                    SdpMid        = sdpMid,
                    SdpMLineIndex = sdpMLineIndex,
                };

                return(iceCandidate);
            }
Example #22
0
        public void InitEncryptionDisabledSession(IceCandidate iceCandidate, IPEndPoint remoteEndPoint)
        {
            if (_audioRtpSession == null || _videoRtpSession == null)
            {
                logger.Debug($"Initialising non encrypted WebRtc session for remote end point {remoteEndPoint.ToString()}.");

                iceCandidate.RemoteRtpEndPoint = remoteEndPoint;

                _audioRtpSession = new RTPSession((int)RTPPayloadTypesEnum.PCMU, null, null);
                _videoRtpSession = new RTPSession(VP8_PAYLOAD_TYPE_ID, null, null);
            }
        }
 public void OnIceCandidate(IceCandidate candidate)
 {
     outerInstance.RunOnUiThread(() =>
     {
         JSONObject json = new JSONObject();
         jsonPut(json, "type", "candidate");
         jsonPut(json, "label", candidate.SdpMLineIndex);
         jsonPut(json, "id", candidate.SdpMid);
         jsonPut(json, "candidate", candidate.Sdp);
         outerInstance.sendMessage(json);
     });
 }
 public void SendLocalIceCandidate(IceCandidate candidate)
 {
     _executor.Execute(() =>
     {
         var iceCandidate = new IceCandidateMessage(candidate)
         {
             SocketId    = _socketId,
             MessageType = SignalingMessageType.SendCandidate
         };
         _wsClient.Send(iceCandidate.ToJson());
     });
 }
Example #25
0
        private void IceCandidateReadytoSend(IceCandidate candidate)
        {
            Dictionary <string, string> message = new Dictionary <string, string>
            {
                { "candidate", candidate.Content },
                { "sdpMlineindex", candidate.SdpMlineIndex.ToString() },
                { "sdpMid", candidate.SdpMid }
            };

            string serializedMessage = JsonConvert.SerializeObject(message);

            SendMessage(serializedMessage);
        }
        public void OnRemoteIceCandidate(IceCandidate candidate)
        {
            Executor.Execute(() =>
            {
                if (PeerConnectionClient == null)
                {
                    Logger.Error(TAG, "Received remote SDP for non-initilized peer connection.");
                    return;
                }

                PeerConnectionClient.AddRemoteIceCandidate(candidate);
            });
        }
Example #27
0
        public void ProcessFullOffer(FullOffer fofFullOffer)
        {
            //process base offer
            ProcessOfferAndMakeReply(fofFullOffer.m_ofrOffer.m_strValue);

            //process all the ice candidates
            for (int i = 0; i < fofFullOffer.m_iceCandidates.Count; i++)
            {
                IceCandidate iceCandidate = fofFullOffer.m_iceCandidates[i];

                AddICE(iceCandidate.m_strType, iceCandidate.m_iIndex, iceCandidate.m_strValue);
            }
        }
        public static ARDSignalingMessage MessageFromJSONString(string json)
        {
            var values = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);
            ARDSignalingMessage message = new ARDSignalingMessage();

            if (values.ContainsKey("type"))
            {
                var type = values["type"] ?? "";
                switch (type)
                {
                case CandidateType:
                    int.TryParse(values["label"], out int label);
                    var candidate = new IceCandidate(values["candidate"], values["id"], label);
                    message = new ARDICECandidateMessage(candidate);
                    break;

                case CandidateRemovalType:

                    break;

                case OfferType:
                    var description = new SessionDescription(SdpType.Offer, values["sdp"]);
                    message = new ARDSessionDescriptionMessage(description);
                    break;

                case AnswerType:
                    description = new SessionDescription(SdpType.Answer, values["sdp"]);
                    message     = new ARDSessionDescriptionMessage(description);
                    break;

                case PrAnswerType:
                    description = new SessionDescription(SdpType.PrAnswer, values["sdp"]);
                    message     = new ARDSessionDescriptionMessage(description);
                    break;

                case ByeType:
                    message = new ARDByeMessage();
                    break;

                default:
                    System.Diagnostics.Debug.WriteLine($"ARDSignalingMessage unexpected type: {type}");
                    break;
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine($"ARDSignalingMessage invalid json: {json}");
            }

            return(message);
        }
Example #29
0
 public void OnGenerateCandidate(IceCandidate iceCandidate)
 {
     Console.WriteLine($"{nameof(OnGenerateCandidate)}: {iceCandidate.Sdp}");
     virtualStudioConnection.SendIceCandidate(new IceCandidateArgs
     {
         ConnectionId = connectionId,
         CandidateDto = new RtcIceCandidateDto
         {
             Candidate     = iceCandidate.Sdp,
             SdpMid        = iceCandidate.SdpMid,
             SdpMLineIndex = iceCandidate.SdpMLineIndex
         }
     });
 }
Example #30
0
        public async Task <IActionResult> AddCandidate(int peerId, [FromBody] IceCandidate candidate)
        {
            // var peer = await _rtcContext.Peers.SingleOrDefaultAsync(p => p.Id == peerId);
            // if(peer == null) {
            //     return NotFound();
            // }
            candidate.PeerId = peerId;
            //candidate.Peer = peer;
            await _rtcContext.IceCandidates.AddAsync(candidate);

            await _rtcContext.SaveChangesAsync();

            return(Ok(candidate));
        }