internal void Play(VideoEncoderSettings inputPars, VideoEncoderSettings outputPars, NetworkSettings networkPars) { logger.Debug("RemoteDesktopClient::Play(...)"); VideoReceiver = new VideoReceiver(); VideoReceiver.Setup(inputPars, outputPars, networkPars); VideoReceiver.UpdateBuffer += VideoReceiver_UpdateBuffer; VideoReceiver.Play(); }
private void SetupVideo(ScreencastChannelInfo videoChannelInfo) { tracer.Verb("ScreenCastControl::SetupVideo(...)"); //logger.Debug("SetupVideo(...)"); var videoInfo = videoChannelInfo.MediaInfo as VideoChannelInfo; if (videoInfo == null) { return; } var videoAddr = videoChannelInfo.Address; if (videoChannelInfo.Transport == TransportMode.Tcp) { videoAddr = ServerAddr; } var videoPort = videoChannelInfo.Port; var inputPars = new VideoEncoderSettings { //Resolution = videoInfo.Resolution, Width = videoInfo.Resolution.Width, Height = videoInfo.Resolution.Height, FrameRate = new MediaRatio(videoInfo.Fps, 1), }; var outputPars = new VideoEncoderSettings { //Resolution = videoInfo.Resolution, Width = videoInfo.Resolution.Width, Height = videoInfo.Resolution.Height, //AspectRatio = new MediaRatio(1, 1), //Resolution = new System.Drawing.Size(1920, 1080); FrameRate = new MediaRatio(videoInfo.Fps, 1), }; var networkPars = new NetworkSettings { LocalAddr = videoAddr, LocalPort = videoPort, TransportMode = videoChannelInfo.Transport, SSRC = videoChannelInfo.SSRC, }; VideoReceiver = new VideoReceiver(); VideoReceiver.UpdateBuffer += VideoReceiver_UpdateBuffer; VideoReceiver.Setup(inputPars, outputPars, networkPars); d3dProvider = new D3D11RendererProvider(); d3dProvider.Init(VideoReceiver.sharedTexture); //d3dRenderer.Setup(VideoReceiver.sharedTexture); }
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(); } }