コード例 #1
0
        private void audioPlayButton_Click(object sender, EventArgs e)
        {
            audioReceiver = new AudioReceiver();
            var addr = audioAddrTextBox.Text;
            var port = (int)audioPortNumeric.Value;

            var transport = (TransportMode)transportComboBox.SelectedItem;

            var sampleRate  = (int)sampleRateNumeric.Value;
            var channels    = (int)channelsNumeric.Value;
            var networkPars = new NetworkSettings
            {
                LocalAddr     = addr,
                LocalPort     = port,
                TransportMode = transport,
            };

            var audioPars = new AudioEncoderSettings
            {
                SampleRate = sampleRate,
                Channels   = channels,
                Encoding   = "ulaw",
                DeviceId   = currentDirectSoundDeviceInfo?.Guid.ToString() ?? "",
            };

            audioReceiver.SetWaveformPainter(this.waveformPainter1);

            audioReceiver.Setup(audioPars, networkPars);
            audioReceiver.Play();
        }
コード例 #2
0
    // Use this for initialization
    void Awake()
    {
        InitCharacterMesh();

        changeTPoseToNPose();
        setUpSkeleton();

        // from old code
        Time.fixedDeltaTime = (float)0.04;
        characterTimer      = new TimeController();
        characterTimer.setTimeMillis(0);

        bapReceiver   = new BAPReceiver(NUMBER_OF_BAPS, BAP_RECEIVER_PORT);
        fapReceiver   = new FAPReceiver(NUMBER_OF_FAPS + 1, FAP_RECEIVER_PORT);
        audioReceiver = new AudioReceiver(AUDIO_RECEIVER_PORT);

        lastFAPFrame  = new AnimationParametersFrame(NUMBER_OF_FAPS + 1, 0);
        lastBAPFrame  = new AnimationParametersFrame(NUMBER_OF_BAPS, 0);
        cptFrames     = 0;
        commandSender = new CommandSender(GretaServersHost, CMD_SENDER_PORT);
        agentPlaying  = false;

        _currentAudioSource = getBone("Head").gameObject.AddComponent <AudioSource>();
        audioFilePlayer     = new AudioFilePlayer();
    }
コード例 #3
0
        public void Close()
        {
            if (videoRendererSink != null)
            {
                videoRendererSink.Stop();
                //videoRendererSink.Close();
                videoRendererSink = null;
            }

            if (VideoReceiver != null)
            {
                VideoReceiver.DataReceived -= VideoReceiver_DataReceived;
                VideoReceiver.Stop();
                VideoReceiver = null;
            }

            if (AudioReceiver != null)
            {
                AudioReceiver.Stop();
                AudioReceiver = null;
            }

            if (factory != null)
            {
                factory.Closed -= Factory_Closed;

                factory.Abort();
                factory = null;
            }

            //state = ClientState.Disconnected;
        }
コード例 #4
0
        public void Close()
        {
            if (imageProvider != null)
            {
                imageProvider.Close();

            }

            if (VideoReceiver != null)
            {
                VideoReceiver.UpdateBuffer -= VideoReceiver_UpdateBuffer;
                VideoReceiver.Stop();
                VideoReceiver = null;
            }

            if (AudioReceiver != null)
            {
                AudioReceiver.Stop();
                AudioReceiver = null;
            }


            if (factory != null)
            {
                factory.Abort();
                factory = null;
            }
        }
コード例 #5
0
ファイル: AudioVisualizer.cs プロジェクト: haruo666/VJ149
    // Use this for initialization
    void Start()
    {
        audio = GetComponent<AudioSource>();
        audioreceiver = GetComponent<AudioReceiver>(); // Reference the audioreceiver script.
        Debug.Log (audioreceiver.teststr);

        lr = GetComponent<LineRenderer> ();

        cube = GameObject.Find ("Cube");
    }
コード例 #6
0
        private void SetupAudio(ScreencastChannelInfo audioChannelInfo)
        {
            tracer.Verb("ScreenCastControl::SetupAudio(...)");

            //logger.Debug("SetupAudio(...)");

            var audioInfo = audioChannelInfo.MediaInfo as AudioChannelInfo;

            if (audioInfo == null)
            {
                return;
            }

            var audioAddr = audioChannelInfo.Address;

            if (audioChannelInfo.Transport == TransportMode.Tcp)
            {
                audioAddr = ServerAddr;
            }

            var audioPort = audioChannelInfo.Port;

            AudioReceiver = new AudioReceiver();
            var networkPars = new NetworkSettings
            {
                LocalAddr     = audioAddr,
                LocalPort     = audioPort,
                TransportMode = audioChannelInfo.Transport,
                SSRC          = audioChannelInfo.SSRC,
            };

            var audioDeviceId = "";

            try
            {
                var devices = NAudio.Wave.DirectSoundOut.Devices;
                var device  = devices.FirstOrDefault();
                audioDeviceId = device?.Guid.ToString() ?? "";
            }
            catch (Exception ex)
            {
                tracer.Error(ex);
                //logger.Error(ex);
            }

            var audioPars = new AudioEncoderSettings
            {
                SampleRate = audioInfo.SampleRate,
                Channels   = audioInfo.Channels,
                Encoding   = "ulaw",
                DeviceId   = audioDeviceId,
            };

            AudioReceiver.Setup(audioPars, networkPars);
        }
コード例 #7
0
ファイル: LoudnessObject.cs プロジェクト: ninonote/VJ149
    // Use this for initialization
    void Start()
    {
        audio = GetComponent<AudioSource>();
        audioreceiver = GetComponent<AudioReceiver>(); // Reference the audioreceiver script.
        Debug.Log (audioreceiver.teststr);

        lr = GetComponent<LineRenderer> ();

        //cube = GameObject.Find ("LoudnessObject");
        loudnessobj = GameObject.FindGameObjectWithTag ("LoudnessObject");
    }
コード例 #8
0
    public Receiver(int receiverId, int senderId, IPEndPoint senderEndPoint)
    {
        ReceiverId     = receiverId;
        SenderId       = senderId;
        SenderEndPoint = senderEndPoint;
        VideoMessages  = new SortedDictionary <int, VideoSenderMessage>();

        videoMessageAssembler = new VideoMessageAssembler(ReceiverId, SenderEndPoint);
        textureSetUpdater     = new TextureSetUpdater(ReceiverId, SenderEndPoint);
        audioReceiver         = new AudioReceiver();

        heartbeatStopWatch   = Stopwatch.StartNew();
        receivedAnyStopWatch = Stopwatch.StartNew();
    }
コード例 #9
0
 // Start is called before the first frame update
 void Start()
 {
     wasAPIInterface = AudioListener.GetComponent <WasAPIInterface>();
     barCount        = wasAPIInterface.SpectrumSize;
     colorStep       = 1 / (float)barCount;
     for (int i = 0; i < barCount; i++)
     {
         GameObject instance = Instantiate(CapsulePrefab, Vector3.zero + (-(barCount / 2) + i) * (new Vector3(1, 0, 0)), Quaternion.identity);
         instance.name += $"_{i}";
         primaryBarInstance.Add(instance);
         AudioReceiver audioReceiver = instance.GetComponent <AudioReceiver>();
         audioReceiver.spectrumPart = i;
         audioReceiver.Material     = getMaterialForStep(i);
         AudioListener.GetComponent <WasAPIInterface>().AddAudioReceiver(audioReceiver);
     }
 }
コード例 #10
0
    /// <summary>
    /// Callback invoked due to inheriting from MonoBehaviour. Initializes the engine and statically placed emitters.
    /// </summary>
    void Start()
    {
        AudioSettings.outputSampleRate = outputSampleRate;

        audioReceiver = Object.FindObjectOfType(typeof(AudioReceiver)) as AudioReceiver;
        
        AudioEmitter[] emitters = Object.FindObjectsOfType(typeof(AudioEmitter)) as AudioEmitter[];
        for (int i = 0; i < emitters.Length; i++)
        {
            if (!string.IsNullOrEmpty(emitters[i].audioDataFileName))
            {
                audioEmitters.Add(emitters[i]);
                emitters[i].Initialize(false);
            }
            else
                Debug.Log("An emitter wth no specified audioDataFileName was ignored");
        }
        readyToUse = true;
    }
コード例 #11
0
    /// <summary>
    /// Callback invoked due to inheriting from MonoBehaviour. Initializes the engine and statically placed emitters.
    /// </summary>
    void Start()
    {
        AudioSettings.outputSampleRate = outputSampleRate;

        audioReceiver = Object.FindObjectOfType(typeof(AudioReceiver)) as AudioReceiver;

        AudioEmitter[] emitters = Object.FindObjectsOfType(typeof(AudioEmitter)) as AudioEmitter[];
        for (int i = 0; i < emitters.Length; i++)
        {
            if (!string.IsNullOrEmpty(emitters[i].audioDataFileName))
            {
                audioEmitters.Add(emitters[i]);
                emitters[i].Initialize(false);
            }
            else
            {
                Debug.Log("An emitter wth no specified audioDataFileName was ignored");
            }
        }
        readyToUse = true;
    }
コード例 #12
0
        public void Close()
        {
            if (d3dProvider != null)
            {
                d3dProvider.Close();
                d3dProvider = null;
            }

            if (d3dRenderer != null)
            {
                d3dRenderer.Shutdown();
            }

            if (VideoReceiver != null)
            {
                VideoReceiver.UpdateBuffer -= VideoReceiver_UpdateBuffer;
                VideoReceiver.Stop();
                VideoReceiver = null;
            }

            if (AudioReceiver != null)
            {
                AudioReceiver.Stop();
                AudioReceiver = null;
            }

            if (factory != null)
            {
                factory.Closed -= Factory_Closed;

                factory.Abort();
                factory = null;
            }

            //state = ClientState.Disconnected;
        }
コード例 #13
0
    /// <summary>
    /// Processes sound which is emitting with 3D enabled.
    /// Calculates stereo panning, depth, falloff and doppler effect (if enabled).
    /// </summary>
    /// <param name="audioReceiver">The receiver (the one and only).</param>
    public void Process3D(AudioReceiver audioReceiver)
    {
		lastProcessSampleIndex = sampleIndex;
		
        float distance = Vector3.Distance(audioReceiver.transform.position, transform.position);
        if (distance <= soundSettings3D.playRadius)
        {
            // Calculate a panning for when the emitter is in front or behind the audio receiver.
            // The panning will go between 0.5 and 1.0, applied on both the channels.
            Vector3 forward = audioReceiver.transform.forward;
            Vector3 toOther = (transform.position - audioReceiver.transform.position).normalized;
            float panningFrontBack = (Vector3.Dot(forward, toOther) + 1.5f) / 2.5f;
			panningFrontBack = soundSettings3D.frontBoostStrength * panningFrontBack + (1 - soundSettings3D.frontBoostStrength);
            
            // Calculate a panning for left and right.
            Vector3 right = audioReceiver.transform.right;
            float panningLeftRight = Vector3.Dot(right, toOther);
            // Add the constant power factor to the output channels.
            // This in turn is also scaled, going between 0.2 and 1.0.
            float multiplier = Mathf.Sqrt(2) / 2;
            float angle = panningLeftRight * 0.5f;
            leftPan = multiplier * (Mathf.Cos(angle) - Mathf.Sin(angle));
            rightPan = multiplier * (Mathf.Cos(angle) + Mathf.Sin(angle));
			leftPan = soundSettings3D.panningStrength * leftPan + (1 - soundSettings3D.panningStrength);
			rightPan = soundSettings3D.panningStrength * rightPan + (1 - soundSettings3D.panningStrength);

            // Apply the front and back panning.
            leftPan *= panningFrontBack;
            rightPan *= panningFrontBack;

            // Further more, multiply the result by a falloff, currently being Min(1 - log(10 * distance / playRadius), 1), ranged between 0 and 1.
            multiplier = Mathf.Min(1 - Mathf.Log10(10 * distance / soundSettings3D.playRadius), 1.0f);
            leftPan *= multiplier;
            rightPan *= multiplier;

            // Doppler effect will also be taken into consideration (if doppler level is set to greater than zero).
            Vector3 distVector = transform.position - audioReceiver.transform.position;
            float vrr = Vector3.Dot(audioReceiver.Velocity * soundSettings3D.dopplerLevel, distVector) / distance; // The relative velocity of the receiver.
            float vsr = Vector3.Dot(velocity * soundSettings3D.dopplerLevel, distVector) / distance;               // The relative velocity of the source.
            float newFrequency = (soundSettings3D.soundSpeed + vrr) / (soundSettings3D.soundSpeed + vsr) * audioData.Frequency;
            frequencyFactor = newFrequency / audioData.Frequency;
			
			//calculate delays
            Vector3 earOffset = audioReceiver.transform.right * audioReceiver.transform.localScale.x;
            float distanceL1 = Vector3.Distance(transform.position, audioReceiver.transform.position - earOffset);
            float distanceR1 = Vector3.Distance(transform.position, audioReceiver.transform.position + earOffset);
            delayL1 = distanceL1 / soundSettings3D.soundSpeed;
            delayR1 = distanceR1 / soundSettings3D.soundSpeed;
			
			//calculate delays a while later for extrapolation (defined by extrapolationTime)
            float distanceL2 = Vector3.Distance(transform.position + velocity*0.01f, audioReceiver.transform.position - earOffset);
            float distanceR2 = Vector3.Distance(transform.position + velocity*0.01f, audioReceiver.transform.position + earOffset);
            delayL2 = distanceL2 / soundSettings3D.soundSpeed;
            delayR2 = distanceR2 / soundSettings3D.soundSpeed;
			Debug.Log(distanceL2 - distanceL1);
        }
        else
        {
            leftPan = 0.0f;
            rightPan = 0.0f;
            frequencyFactor = 1.0f;
            delayL1 = 0;
            delayR1 = 0;
            delayL2 = 0;
            delayR2 = 0;
        }
    }
コード例 #14
0
        public void Connect(string addr, int port)
        {
            tracer.Verb("ScreenCastControl::Connecting(...) " + addr + " " + port);
            hWnd = this.Handle;//this.Parent.Parent.Handle;
            //logger.Debug("RemoteDesktopClient::Connecting(...) " + addr + " " + port);

            state = ClientState.Connecting;

            cancelled = false;
            errorCode = ErrorCode.Ok;

            this.ServerAddr = addr;
            this.ServerPort = port;

            this.ClientId = RngProvider.GetRandomNumber().ToString();

            var address = "net.tcp://" + ServerAddr + "/ScreenCaster";

            if (this.ServerPort > 0)
            {
                address = "net.tcp://" + ServerAddr + ":" + ServerPort + "/ScreenCaster";
            }

            //Console.WriteLine(address);

            UpdateControls();

            mainTask = Task.Run(() =>
            {
                //int maxTryCount = 10;
                uint tryCount = 0;

                bool forceReconnect = (MaxTryConnectCount == uint.MaxValue);

                while ((forceReconnect || tryCount <= MaxTryConnectCount) && !cancelled)
                {
                    tracer.Verb("ScreenCastControl::Connecting count: " + tryCount);

                    //logger.Debug("Connecting count: " + tryCount);
                    //errorMessage = "";
                    errorCode = ErrorCode.Ok;

                    try
                    {
                        var uri = new Uri(address);

                        NetTcpSecurity security = new NetTcpSecurity
                        {
                            Mode = SecurityMode.None,
                        };

                        var binding = new NetTcpBinding
                        {
                            ReceiveTimeout = TimeSpan.MaxValue,//TimeSpan.FromSeconds(10),
                            SendTimeout    = TimeSpan.FromSeconds(5),
                            OpenTimeout    = TimeSpan.FromSeconds(5),

                            Security = security,
                        };

                        //var _uri = new Uri("net.tcp://" + ServerAddr + ":" + 0 + "/ScreenCaster");
                        //factory = new ChannelFactory<IScreenCastService>(binding, new EndpointAddress(_uri));

                        //var viaUri = new Uri("net.tcp://" + ServerAddr + ":" + ServerPort + "/ScreenCaster");
                        //factory.Endpoint.EndpointBehaviors.Add(new System.ServiceModel.Description.ClientViaBehavior(viaUri));

                        factory         = new ChannelFactory <IScreenCastService>(binding, new EndpointAddress(uri));
                        factory.Closed += Factory_Closed;

                        var channel = factory.CreateChannel();

                        try
                        {
                            var channelInfos = channel.GetChannelInfos();

                            state = ClientState.Connected;
                            UpdateControls();
                            Connected?.Invoke();

                            if (channelInfos == null)
                            {
                                errorCode = ErrorCode.NotReady;
                                throw new Exception("Server not configured");
                            }

                            var videoChannelInfo = channelInfos.FirstOrDefault(c => c.MediaInfo is VideoChannelInfo);
                            if (videoChannelInfo != null)
                            {
                                if (videoChannelInfo.Transport == TransportMode.Tcp && videoChannelInfo.ClientsCount > 0)
                                {
                                    errorCode = ErrorCode.IsBusy;
                                    throw new Exception("Server is busy");
                                }
                                SetupVideo(videoChannelInfo);
                            }

                            var audioChannelInfo = channelInfos.FirstOrDefault(c => c.MediaInfo is AudioChannelInfo);
                            if (audioChannelInfo != null)
                            {
                                if (audioChannelInfo.Transport == TransportMode.Tcp && videoChannelInfo.ClientsCount > 0)
                                {
                                    errorCode = ErrorCode.IsBusy;
                                    throw new Exception("Server is busy");
                                }

                                SetupAudio(audioChannelInfo);
                            }

                            if (VideoReceiver != null)
                            {
                                VideoReceiver.Play();
                                // d3dRenderer.Start();
                            }

                            if (AudioReceiver != null)
                            {
                                AudioReceiver.Play();
                            }

                            channel.PostMessage(new ServerRequest {
                                Command = "Ping"
                            });
                            tryCount = 0;

                            state = ClientState.Running;
                            UpdateControls();

                            while (state == ClientState.Running)
                            {
                                try
                                {
                                    channel.PostMessage(new ServerRequest {
                                        Command = "Ping"
                                    });
                                    if (d3dRenderer.ErrorCode != 0)
                                    {
                                        tracer.Warn("ScreenCastControl::imageProvider.ErrorCode: " + d3dRenderer.ErrorCode);

                                        // logger.Debug("imageProvider.ErrorCode: " + videoRenderer.ErrorCode);
                                        //Process render error...
                                    }

                                    //TODO::
                                    // Receivers errors...

                                    syncEvent.WaitOne(1000);
                                }
                                catch (Exception ex)
                                {
                                    state     = ClientState.Interrupted;
                                    errorCode = ErrorCode.Interrupted;
                                }
                            }
                        }
                        finally
                        {
                            CloseChannel(channel);
                        }
                    }
                    catch (EndpointNotFoundException ex)
                    {
                        errorCode = ErrorCode.NotFound;

                        tracer.Error(ex.Message);

                        //logger.Error(ex.Message);

                        //Console.WriteLine(ex.Message);
                    }
                    catch (Exception ex)
                    {
                        tracer.Error(ex);
                        //logger.Error(ex);

                        if (errorCode == ErrorCode.Ok)
                        {
                            errorCode = ErrorCode.Fail;
                        }

                        //Console.WriteLine(ex);
                    }
                    finally
                    {
                        Close();
                    }

                    if (!cancelled)
                    {
                        if (errorCode != ErrorCode.Ok)
                        {
                            UpdateControls();

                            tryCount++;

                            var statusStr = "Attempting to connect..."; // + tryCount; // + " of " + maxTryCount;

                            SetStatus(statusStr);
                        }

                        Thread.Sleep(1000);
                        continue;
                    }
                    else
                    {
                        errorCode = ErrorCode.Cancelled;
                        break;
                    }
                }//while end

                cancelled = false;

                state = ClientState.Disconnected;
                UpdateControls();

                Disconnected?.Invoke(null);
            });
        }
コード例 #15
0
ファイル: AudioSunBurst.cs プロジェクト: ninonote/VJ149
 void Start()
 {
     audio = GetComponent<AudioSource>();
     audioreceiver = GetComponent<AudioReceiver>(); // Reference the audioreceiver script.
 }
コード例 #16
0
    /// <summary>
    /// Processes sound which is emitting with 3D enabled.
    /// Calculates stereo panning, depth, falloff and doppler effect (if enabled).
    /// </summary>
    /// <param name="audioReceiver">The receiver (the one and only).</param>
    public void Process3D(AudioReceiver audioReceiver)
    {
        lastProcessSampleIndex = sampleIndex;

        float distance = Vector3.Distance(audioReceiver.transform.position, transform.position);

        if (distance <= soundSettings3D.playRadius)
        {
            // Calculate a panning for when the emitter is in front or behind the audio receiver.
            // The panning will go between 0.5 and 1.0, applied on both the channels.
            Vector3 forward          = audioReceiver.transform.forward;
            Vector3 toOther          = (transform.position - audioReceiver.transform.position).normalized;
            float   panningFrontBack = (Vector3.Dot(forward, toOther) + 1.5f) / 2.5f;
            panningFrontBack = soundSettings3D.frontBoostStrength * panningFrontBack + (1 - soundSettings3D.frontBoostStrength);

            // Calculate a panning for left and right.
            Vector3 right            = audioReceiver.transform.right;
            float   panningLeftRight = Vector3.Dot(right, toOther);
            // Add the constant power factor to the output channels.
            // This in turn is also scaled, going between 0.2 and 1.0.
            float multiplier = Mathf.Sqrt(2) / 2;
            float angle      = panningLeftRight * 0.5f;
            leftPan  = multiplier * (Mathf.Cos(angle) - Mathf.Sin(angle));
            rightPan = multiplier * (Mathf.Cos(angle) + Mathf.Sin(angle));
            leftPan  = soundSettings3D.panningStrength * leftPan + (1 - soundSettings3D.panningStrength);
            rightPan = soundSettings3D.panningStrength * rightPan + (1 - soundSettings3D.panningStrength);

            // Apply the front and back panning.
            leftPan  *= panningFrontBack;
            rightPan *= panningFrontBack;

            // Further more, multiply the result by a falloff, currently being Min(1 - log(10 * distance / playRadius), 1), ranged between 0 and 1.
            multiplier = Mathf.Min(1 - Mathf.Log10(10 * distance / soundSettings3D.playRadius), 1.0f);
            leftPan   *= multiplier;
            rightPan  *= multiplier;

            // Doppler effect will also be taken into consideration (if doppler level is set to greater than zero).
            Vector3 distVector   = transform.position - audioReceiver.transform.position;
            float   vrr          = Vector3.Dot(audioReceiver.Velocity * soundSettings3D.dopplerLevel, distVector) / distance; // The relative velocity of the receiver.
            float   vsr          = Vector3.Dot(velocity * soundSettings3D.dopplerLevel, distVector) / distance;               // The relative velocity of the source.
            float   newFrequency = (soundSettings3D.soundSpeed + vrr) / (soundSettings3D.soundSpeed + vsr) * audioData.Frequency;
            frequencyFactor = newFrequency / audioData.Frequency;

            //calculate delays
            Vector3 earOffset  = audioReceiver.transform.right * audioReceiver.transform.localScale.x;
            float   distanceL1 = Vector3.Distance(transform.position, audioReceiver.transform.position - earOffset);
            float   distanceR1 = Vector3.Distance(transform.position, audioReceiver.transform.position + earOffset);
            delayL1 = distanceL1 / soundSettings3D.soundSpeed;
            delayR1 = distanceR1 / soundSettings3D.soundSpeed;

            //calculate delays a while later for extrapolation (defined by extrapolationTime)
            float distanceL2 = Vector3.Distance(transform.position + velocity * 0.01f, audioReceiver.transform.position - earOffset);
            float distanceR2 = Vector3.Distance(transform.position + velocity * 0.01f, audioReceiver.transform.position + earOffset);
            delayL2 = distanceL2 / soundSettings3D.soundSpeed;
            delayR2 = distanceR2 / soundSettings3D.soundSpeed;
            Debug.Log(distanceL2 - distanceL1);
        }
        else
        {
            leftPan         = 0.0f;
            rightPan        = 0.0f;
            frequencyFactor = 1.0f;
            delayL1         = 0;
            delayR1         = 0;
            delayL2         = 0;
            delayR2         = 0;
        }
    }
コード例 #17
0
        private void ClientProc()
        {
            var address = "net.tcp://" + ServerAddr + "/ScreenCaster";

            if (this.ServerPort > 0)
            {
                address = "net.tcp://" + ServerAddr + ":" + ServerPort + "/ScreenCaster";
            }
            
            try
            {

                var uri = new Uri(address);
                this.ClientId = RngProvider.GetRandomNumber().ToString();

                //NetTcpSecurity security = new NetTcpSecurity
                //{
                //    Mode = SecurityMode.Transport,
                //    Transport = new TcpTransportSecurity
                //    {
                //        ClientCredentialType = TcpClientCredentialType.Windows,
                //        ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign,
                //    },
                //};

                NetTcpSecurity security = new NetTcpSecurity
                {
                    Mode = SecurityMode.None,
                };

                var binding = new NetTcpBinding
                {
                    ReceiveTimeout = TimeSpan.MaxValue,//TimeSpan.FromSeconds(10),
                    SendTimeout = TimeSpan.FromSeconds(10),
                    Security = security,
                };

                factory = new ChannelFactory<IScreenCastService>(binding, new EndpointAddress(uri));
                var channel = factory.CreateChannel();

                try
                {
                    //channel.PostMessage(new ServerRequest { Command = "Ping" });

                    var channelInfos = channel.GetChannelInfos();

                    if (channelInfos == null)
                    {
                        logger.Error("channelInfos == null");
                        return;
                    }


                    TransportMode transportMode = TransportMode.Udp;
                    var videoChannelInfo = channelInfos.FirstOrDefault(c => c.MediaInfo is VideoChannelInfo);

                    if (videoChannelInfo != null)
                    {
                        transportMode = videoChannelInfo.Transport;

                        if(transportMode == TransportMode.Tcp)
                        {
                            if (videoChannelInfo.ClientsCount > 0)
                            {
                                throw new Exception("Server is busy");
                            }
                        }

                        var videoAddr = videoChannelInfo.Address;

                        if(transportMode == TransportMode.Tcp)
                        {
                            videoAddr = ServerAddr;
                        }

                        var videoPort = videoChannelInfo.Port;

                        //if (string.IsNullOrEmpty(videoAddr))
                        //{
                        //    //channel.Play()
                        //}

                        //if (transportMode == TransportMode.Tcp)
                        //{
                        //   var res = channel.Play(channelInfos);
                        //}

                        var videoInfo = videoChannelInfo.MediaInfo as VideoChannelInfo;
                        if (videoInfo != null)
                        {
                            var inputPars = new VideoEncoderSettings
                            {
                                Resolution = videoInfo.Resolution,
                                //Width = videoInfo.Resolution.Width,
                                //Height = videoInfo.Resolution.Height,
                                FrameRate = new MediaRatio(videoInfo.Fps,
                            };

                            var outputPars = new VideoEncoderSettings
                            {
                                //Width = 640,//2560,
                                //Height = 480,//1440,
                                //Width = 1920,
                                //Height = 1080,

                                //FrameRate = 30,

                                //Width = videoInfo.Resolution.Width,
                                //Height = videoInfo.Resolution.Height,
                                Resolution = videoInfo.Resolution,
                                FrameRate = videoInfo.Fps,

                            };

                            //bool keepRatio = true;
                            //if (keepRatio)
                            //{
                            //    var srcSize = new Size(inputPars.Width, inputPars.Height);
                            //    var destSize = new Size(outputPars.Width, outputPars.Height);


                            //    var ratio = srcSize.Width / (double)srcSize.Height;
                            //    int destWidth = destSize.Width;
                            //    int destHeight = (int)(destWidth / ratio);
                            //    if (ratio < 1)
                            //    {
                            //        destHeight = destSize.Height;
                            //        destWidth = (int)(destHeight * ratio);
                            //    }
                            //    outputPars.Width = destWidth;
                            //    outputPars.Height = destHeight;
                            //}



                            var networkPars = new NetworkSettings
                            {
                                LocalAddr = videoAddr,
                                LocalPort = videoPort,
                                TransportMode = transportMode,

                                SSRC = videoChannelInfo.SSRC,
                            };

                            VideoReceiver = new VideoReceiver();


                            VideoReceiver.Setup(inputPars, outputPars, networkPars);
                            VideoReceiver.UpdateBuffer += VideoReceiver_UpdateBuffer;
                        }

                    }


                    var audioChannelInfo =channelInfos.FirstOrDefault(c => c.MediaInfo is AudioChannelInfo);
                    if (audioChannelInfo != null)
                    {
                        var audioInfo = audioChannelInfo.MediaInfo as AudioChannelInfo;
                        if (audioInfo != null)
                        {

                            var audioAddr = audioChannelInfo.Address;
                            transportMode = audioChannelInfo.Transport;

                            if (transportMode == TransportMode.Tcp)
                            {
                                audioAddr = ServerAddr;
                            }

                            if (transportMode == TransportMode.Tcp)
                            {
                                if (audioChannelInfo.ClientsCount > 0)
                                {
                                    throw new Exception("Server is busy");
                                }
                            }

                            var audioPort = audioChannelInfo.Port;

                            AudioReceiver = new AudioReceiver();

                            var networkPars = new NetworkSettings
                            {
                                LocalAddr = audioAddr,
                                LocalPort = audioPort,
                                TransportMode = transportMode,

                                SSRC = audioChannelInfo.SSRC,

                            };

                            var audioDeviceId = "";
                            try
                            {
                                var devices = DirectSoundOut.Devices;
                                var device = devices.FirstOrDefault();
                                audioDeviceId = device?.Guid.ToString() ?? "";
                            }
                            catch(Exception ex)
                            {
                                logger.Error(ex);
                            }


                            var audioPars = new AudioEncoderSettings
                            {
                                SampleRate = audioInfo.SampleRate,
                                Channels = audioInfo.Channels,
                                Encoding = "ulaw",
                                DeviceId = audioDeviceId,//currentDirectSoundDeviceInfo?.Guid.ToString() ?? "",
                            };

                            AudioReceiver.Setup(audioPars, networkPars);
                        }

                    }


                    if (VideoReceiver != null)
                    {
                        VideoReceiver.Play();
                    }

                    if (AudioReceiver != null)
                    {
                        AudioReceiver.Play();
                    }


                    running = true;

                    State = ClientState.Connected;

                    OnStateChanged(State);

                    while (running)
                    {

                        channel.PostMessage(new ServerRequest { Command = "Ping" });


                        syncEvent.WaitOne(1000);

                        //InternalCommand command = null;
                        //do
                        //{
                        //    command = DequeueCommand();
                        //    if (command != null)
                        //    {
                        //        ProcessCommand(command);
                        //    }

                        //} while (command != null);
                    }


                }
                finally
                {
                    running = false;

                    State = ClientState.Disconnected;
                    OnStateChanged(State);

                    try
                    {
                        var c = (IClientChannel)channel;
                        if (c.State != CommunicationState.Faulted)
                        {
                            c.Close();
                        }
                        else
                        {
                            c.Abort();
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);


                State = ClientState.Faulted;
                OnStateChanged(State);

                //Close();
            }
            finally
            {
                Close();
            }
        }