private void StartListener()
        {
            try
            {
                while (State > MediaConnectionState.NotConnected)
                {
                    byte[] received = UdpClient.Receive(ref _localEndpoint);

                    if (BitConverter.ToInt16(new byte[] { received[1], received[0] }, 0) == 2)
                    {
                        string ip = "";
                        for (int i = 8; i < received.Length; i++)
                        {
                            if (received[i] == 0)
                            {
                                break;
                            }
                            else
                            {
                                ip += (char)received[i];
                            }
                        }

                        _localEndpoint = new IPEndPoint(IPAddress.Parse(ip), BitConverter.ToUInt16(new byte[] { received[received.Length - 1], received[received.Length - 2] }, 0));

                        SelectProtocol(_localEndpoint);
                    }
                    else if (received[0] == 0x80 || received[0] == 0x90)
                    {
                        while (SecretKey == null)
                        {
                            Thread.Sleep(100);
                        }

                        //Console.WriteLine($"{received[0]} {received[1]} {received[2]} {received[3]} {received[4]} {received[5]} {received[6]} {received[7]} {received[8]} {received[9]} {received[10]} {received[11]}");

                        /*
                         * var ok = RTPPacketHeader.Read(SecretKey, received, out var _);
                         *
                         * Console.WriteLine($"{ok.Type} {ok.Flags} {ok.Sequence} {ok.Timestamp} {ok.SSRC} {ok.HasExtensions}");
                         */
                        // not much point in doing this rn since the decryption fails

                        if (_parentClient.Config.ParseIncomingRTPData)
                        {
                            try
                            {
                                var header = RTPPacketHeader.Read(SecretKey, received, out byte[] payload);

                                OnUdpPacket?.Invoke(this, new MediaPacketEventArgs(header, payload));
                            }
                            catch (SodiumException) { }
                        }
                    }
                }
            }
            catch { }

            UdpClient.Close();
        }
Example #2
0
        private void StartListener()
        {
            int id     = WebSocket.Id;
            var client = UdpClient;

            try
            {
                while (JustifyThread(id))
                {
                    byte[] received = client.Receive(ref _localEndpoint);

                    if (BitConverter.ToInt16(new byte[] { received[1], received[0] }, 0) == 2)
                    {
                        string ip = "";
                        for (int i = 8; i < received.Length; i++)
                        {
                            if (received[i] == 0)
                            {
                                break;
                            }
                            else
                            {
                                ip += (char)received[i];
                            }
                        }

                        _localEndpoint = new IPEndPoint(IPAddress.Parse(ip), BitConverter.ToUInt16(new byte[] { received[received.Length - 1], received[received.Length - 2] }, 0));

                        WebSocket.SelectProtocol(_localEndpoint);
                    }
                    else if (received[0] == 0x80 || received[0] == 0x90)
                    {
                        while (SecretKey == null)
                        {
                            Thread.Sleep(100);
                        }

                        try
                        {
                            var header = RTPPacketHeader.Read(SecretKey, received, out byte[] payload);

                            HandlePacket(header, payload);
                        }
                        catch (SodiumException) { }
                    }
                }
            }
            catch (Exception ex)
            {
                Log("Listener err: " + ex);
            }

            client?.Close();

            Log("Killed listener for " + id);
        }