Пример #1
0
        private void SignalingParametersReady(SignalingParameters signalingParameters)
        {
            _logger.Debug(TAG, "Room connection completed.");

            if (_connectionParameters.IsLoopback &&
                (!signalingParameters.IsInitiator || signalingParameters.OfferSdp != null))
            {
                ReportError("Loopback room is busy.");
                return;
            }

            if (!_connectionParameters.IsLoopback && !signalingParameters.IsInitiator &&
                signalingParameters.OfferSdp == null)
            {
                _logger.Warning(TAG, "No offer SDP in room response.");
            }

            _initiator  = signalingParameters.IsInitiator;
            _messageUrl = GetMessageUrl(_connectionParameters, signalingParameters);
            _leaveUrl   = GetLeaveUrl(_connectionParameters, signalingParameters);

            _logger.Debug(TAG, $"Message URL: {_messageUrl}");
            _logger.Debug(TAG, $"Leave URL: {_leaveUrl}");

            State = ConnectionState.Connected;

            _signalingEvents.OnChannelConnected(signalingParameters);

            _wsClient.Connect(signalingParameters.WssUrl, signalingParameters.WssPostUrl);
            _wsClient.Register(_connectionParameters.RoomId, signalingParameters.ClientId);
        }
Пример #2
0
        private async Task RoomResponseParseAsync(string response, RoomParametersFetcherCallback callback)
        {
            _logger.Debug(TAG, $"Room response: {response}");
            try
            {
                var joinResponse = JsonConvert.DeserializeObject <JoinResponse>(response);

                if (joinResponse.Result != JoinResultType.Success)
                {
                    callback?.Invoke(null, $"Room response error: {joinResponse.Result}");
                    return;
                }


                var iceCandidates = new List <IceCandidate>();

                SessionDescription offerSdp = null;

                var serverParams = joinResponse.ServerParams;

                if (!serverParams.IsInitiator)
                {
                    var i = 0;
                    foreach (var messageString in serverParams.Messages)
                    {
                        _logger.Debug(TAG, $"GAE-> # {i} {messageString}");
                        var message     = JsonConvert.DeserializeObject <Dictionary <string, string> >(messageString);
                        var messageType = message["type"];
                        switch (messageType)
                        {
                        case "offer":
                            offerSdp =
                                new SessionDescription(SessionDescription.GetSdpTypeFromString(messageType),
                                                       message["sdp"]);
                            break;

                        case "candidate":

                            var iceCandidate = new IceCandidate(message["candidate"], message["id"],
                                                                int.Parse(message["label"]));
                            iceCandidates.Add(iceCandidate);
                            break;

                        default:
                            _logger.Error(TAG, $"Unknown message: {messageString}");
                            break;
                        }

                        i++;
                    }
                }

                _logger.Debug(TAG, $"RoomId: {serverParams.RoomId}. ClientId: {serverParams.ClientId}");
                _logger.Debug(TAG, $"Initiator: {serverParams.IsInitiator}");
                _logger.Debug(TAG, $"WSS url: {serverParams.WssUrl}");
                _logger.Debug(TAG, $"WSS POST url: {serverParams.WssPostUrl}");


                var iceServers = new List <IceServer>();

                iceServers.AddRange(IceServersFromPCConfig(serverParams.PcConfig));

                var isTurnPresent = false;
                foreach (var iceServer in iceServers)
                {
                    _logger.Debug(TAG, $"IceServer: {iceServer}");
                    foreach (var url in iceServer.Urls)
                    {
                        if (url.StartsWith("turn:"))
                        {
                            isTurnPresent = true;
                            break;
                        }
                    }
                }


                if (!isTurnPresent && !string.IsNullOrEmpty(serverParams.IceServerUrl))
                {
                    var turnsServers = await RequestTurnServersAsync(serverParams.IceServerUrl);

                    if (turnsServers.error != null)
                    {
                        callback?.Invoke(null, turnsServers.error);
                        return;
                    }

                    foreach (var iceServer in turnsServers.iceServers)
                    {
                        _logger.Debug(TAG, $"TurnServer: {iceServer}");
                    }

                    iceServers.AddRange(turnsServers.iceServers);
                }

                var signalingParameters = new SignalingParameters()
                {
                    IceServers    = iceServers.ToArray(),
                    IsInitiator   = serverParams.IsInitiator,
                    ClientId      = serverParams.ClientId,
                    WssUrl        = serverParams.WssUrl,
                    WssPostUrl    = serverParams.WssPostUrl,
                    OfferSdp      = offerSdp,
                    IceCandidates = iceCandidates.ToArray()
                };

                callback?.Invoke(signalingParameters, null);
            }
            catch (JsonException ex)
            {
                callback?.Invoke(null, $"Room JSON parsing error: {ex.Message}");
            }
            catch (Exception ex)
            {
                callback?.Invoke(null, $"Room error: {ex.Message}");
            }
        }
Пример #3
0
 private static string GetLeaveUrl(RoomConnectionParameters connectionParameters,
                                   SignalingParameters signalingParameters)
 {
     return(connectionParameters.RoomUrl + "/" + ROOM_LEAVE + "/" + connectionParameters.RoomId + "/" +
            signalingParameters.ClientId + GetQueryString(connectionParameters));
 }