private void InitMediaSettings() { screenCaptureDeviceDescr = new ScreenCaptureDevice { Resolution = new Size(1920, 1080), CaptureMouse = true, AspectRatio = true, CaptureType = VideoCaptureType.DXGIDeskDupl, UseHardware = true, Fps = 30, }; videoEncoderSettings = new VideoEncoderSettings { Resolution = new Size(1920, 1080), Encoder = VideoEncoderMode.H264, Profile = H264Profile.Main, BitrateMode = BitrateControlMode.CBR, Bitrate = 2500, MaxBitrate = 5000, FrameRate = 30, LowLatency = true, }; videoSettings = new VideoStreamSettings { Enabled = true, SessionId = "video_" + Guid.NewGuid().ToString(), NetworkParams = new NetworkSettings(), CaptureDescription = null, EncodingParams = videoEncoderSettings, }; audioEncoderSettings = new AudioEncoderSettings { SampleRate = 8000, Channels = 1, Encoding = "PCMU", }; audioSettings = new AudioStreamSettings { Enabled = true, SessionId = "audio_" + Guid.NewGuid().ToString(), NetworkParams = new NetworkSettings(), CaptureParams = new AudioCaptureSettings(), EncodingParams = audioEncoderSettings, }; }
public static List <VideoSourceItem> GetVideoSources(bool ForceUpdate = false) { // List<VideoSourceItem> items = new List<VideoSourceItem>(); //var captureProperties = Config.Data.ScreenCaptureProperties; if (videoSourceItems == null || ForceUpdate) { videoSourceItems = new List <VideoSourceItem>(); int monitorIndex = 1; foreach (var screen in Screen.AllScreens) { var friendlyName = MediaToolkit.Utils.DisplayHelper.GetFriendlyScreenName(screen); var bounds = screen.Bounds; ScreenCaptureDevice device = new ScreenCaptureDevice { CaptureRegion = bounds, DisplayRegion = bounds, Name = screen.DeviceName, Resolution = bounds.Size, //Properties = captureProperties, DeviceId = screen.DeviceName, }; var monitorDescr = bounds.Width + "x" + bounds.Height; if (!string.IsNullOrEmpty(friendlyName)) { monitorDescr = friendlyName + " " + monitorDescr; } var monitorName = "Display " + monitorIndex + " (" + monitorDescr + ")"; //var name = screen.DeviceName; //if (!string.IsNullOrEmpty(friendlyName)) //{ // name += " (" + friendlyName + " " + bounds.Width + "x" + bounds.Height + ") "; //} device.Name = monitorName; videoSourceItems.Add(new VideoSourceItem { Name = monitorName,//screen.DeviceName,//+ "" + s.Bounds.ToString(), DeviceId = device.DeviceId, CaptureRegion = device.CaptureRegion, }); monitorIndex++; } var customRegion = new Rectangle(10, 10, 640, 480); ScreenCaptureDevice customRegionDescr = new ScreenCaptureDevice { CaptureRegion = customRegion, DisplayRegion = Rectangle.Empty, Resolution = customRegion.Size, Name = "Screen Region", DeviceId = "ScreenRegion", }; videoSourceItems.Add(new VideoSourceItem { Name = customRegionDescr.Name,//+ "" + s.Bounds.ToString(), DeviceId = customRegionDescr.DeviceId, CaptureRegion = customRegionDescr.CaptureRegion, }); if (videoSourceItems.Count > 1) { var allScreenRect = SystemInformation.VirtualScreen; ScreenCaptureDevice device = new ScreenCaptureDevice { DisplayRegion = allScreenRect, CaptureRegion = allScreenRect, Resolution = allScreenRect.Size, //Properties = captureProperties, Name = "All Displays (" + allScreenRect.Width + "x" + allScreenRect.Height + ")", DeviceId = "AllScreens", }; videoSourceItems.Add(new VideoSourceItem { Name = device.Name,//+ "" + s.Bounds.ToString(), DeviceId = device.DeviceId, CaptureRegion = device.CaptureRegion, }); } var captDevices = MediaToolkit.MediaFoundation.MfTool.FindUvcDevices(); if (captDevices.Count > 0) { var captItems = captDevices.Select(d => new VideoSourceItem { Name = d.Name, DeviceId = d.DeviceId, CaptureRegion = new Rectangle(new System.Drawing.Point(0, 0), d.Resolution), IsUvcDevice = true, }); videoSourceItems.AddRange(captItems); } } return(new List <VideoSourceItem>(videoSourceItems)); }
private StreamSession CreateSession() { var session = StreamSession.Default(); var transport = TransportMode.Udp; if (PropertyNetwork.UnicastProtocol == ProtocolKind.TCP) { transport = TransportMode.Tcp; } session.StreamName = Name; session.NetworkIpAddress = PropertyNetwork.Network ?? "0.0.0.0"; session.CommunicationPort = PropertyNetwork.Port; if (PropertyNetwork.Port > 0) { session.CommunicationPort = PropertyNetwork.Port; } else if (PropertyNetwork.CommunicationPort > 0) { session.CommunicationPort = PropertyNetwork.CommunicationPort; } else { session.CommunicationPort = Helpers.NetworkHelper.FindAvailableTcpPort(); } //if(PropertyNetwork.CommunicationPort <=0) //{ // session.CommunicationPort = PropertyNetwork.Port; //} //else //{ // session.CommunicationPort = PropertyNetwork.CommunicationPort; //} //int communicationPort = PropertyNetwork.Port; //if (communicationPort <= 0) //{// если порт не задан - ищем свободный начиная с 808 // //communicationPort = GetRandomTcpPort(); // var freeTcpPorts = MediaToolkit.Utils.NetTools.GetFreePortRange(System.Net.Sockets.ProtocolType.Tcp, 1, 808); // if (freeTcpPorts != null && freeTcpPorts.Count() > 0) // { // communicationPort = freeTcpPorts.FirstOrDefault(); // } //} ////PropertyNetwork.CommunicationPort = communicationPort; //session.CommunicationPort = communicationPort; session.TransportMode = transport; bool isMulticast = !PropertyNetwork.IsUnicast; session.IsMulticast = isMulticast; if (isMulticast) { //VAlidate.... session.MutlicastAddress = PropertyNetwork.MulticastIp; session.MutlicastPort1 = PropertyNetwork.MulticasVideoPort; session.MutlicastPort2 = PropertyNetwork.MulticasAudioPort; } var videoSettings = session.VideoSettings; var encoderResolution = new Size(PropertyVideo.ResolutionWidth, PropertyVideo.ResolutionHeight); videoSettings.StreamFlags &= ~VideoStreamFlags.UseEncoderResoulutionFromSource; if (AdvancedSettings.UseResolutionFromCaptureSource) { videoSettings.StreamFlags |= VideoStreamFlags.UseEncoderResoulutionFromSource; } else { encoderResolution = new Size(AdvancedSettings.Width, AdvancedSettings.Height); } var videoEncoderSettings = videoSettings.EncoderSettings; videoEncoderSettings.EncoderId = AdvancedSettings.EncoderId; videoEncoderSettings.Bitrate = AdvancedSettings.Bitrate; videoEncoderSettings.MaxBitrate = AdvancedSettings.MaxBitrate; videoEncoderSettings.Width = encoderResolution.Width; videoEncoderSettings.Height = encoderResolution.Height; videoEncoderSettings.FrameRate = new MediaRatio(AdvancedSettings.Fps, 1); videoEncoderSettings.Profile = AdvancedSettings.H264Profile; videoEncoderSettings.LowLatency = AdvancedSettings.LowLatency; var captureRegion = PropertyVideo.VideoRect; var captureType = PropertyVideo.CaptType; //var captureFps = PropertyVideo.CaptFps; var captureFps = AdvancedSettings.Fps; var useHardware = PropertyVideo.CaptUseHardware; var screenCaptureProperties = new ScreenCaptureProperties { CaptureMouse = PropertyVideo.CaptureMouse, CaptureType = captureType, // VideoCaptureType.DXGIDeskDupl, UseHardware = useHardware, Fps = captureFps, ShowDebugInfo = false, ShowDebugBorder = PropertyVideo.ShowCaptureBorder, AspectRatio = AdvancedSettings.KeepAspectRatio, }; VideoCaptureDevice captureDevice = null; //var videoSourceItem = PropertyVideo.Display; if (PropertyVideo.IsUvcDevice) { captureDevice = new UvcDevice { Name = PropertyVideo.DeviceName, Resolution = captureRegion.Size, DeviceId = PropertyVideo.DeviceId, }; } else { captureDevice = new ScreenCaptureDevice { CaptureRegion = captureRegion, DisplayRegion = captureRegion, Name = PropertyVideo.DeviceName, Resolution = captureRegion.Size, Properties = screenCaptureProperties, DeviceId = PropertyVideo.DeviceId, }; } if (PropertyAudio.IsEnabled) { var deviceId = PropertyAudio.DeviceId; var audioDevice = MediaToolkit.AudioTool.GetAudioCaptureDevices().FirstOrDefault(d => d.DeviceId == deviceId); if (audioDevice != null) { session.AudioSettings.Enabled = true; audioDevice.Properties = new WasapiCaptureProperties(); session.AudioSettings.CaptureDevice = audioDevice; } } else { session.AudioSettings.Enabled = false; } logger.Info("CaptureDevice: " + captureRegion); session.VideoSettings.CaptureDevice = captureDevice; return(session); }
private void UpdateVideoSources() { List <ComboBoxItem> items = new List <ComboBoxItem>(); var captureProperties = Config.Data.ScreenCaptureProperties; int monitorIndex = 1; foreach (var screen in Screen.AllScreens) { var friendlyName = MediaToolkit.Utils.DisplayHelper.GetFriendlyScreenName(screen); var bounds = screen.Bounds; ScreenCaptureDevice device = new ScreenCaptureDevice { CaptureRegion = bounds, DisplayRegion = bounds, Name = screen.DeviceName, Resolution = bounds.Size, Properties = captureProperties, DeviceId = screen.DeviceName, }; var monitorDescr = bounds.Width + "x" + bounds.Height; if (!string.IsNullOrEmpty(friendlyName)) { monitorDescr = friendlyName + " " + monitorDescr; } var monitorName = "Screen " + monitorIndex + " (" + monitorDescr + ")"; //var name = screen.DeviceName; //if (!string.IsNullOrEmpty(friendlyName)) //{ // name += " (" + friendlyName + " " + bounds.Width + "x" + bounds.Height + ") "; //} device.Name = monitorName; items.Add(new ComboBoxItem { Name = monitorName,//screen.DeviceName,//+ "" + s.Bounds.ToString(), Tag = device, }); monitorIndex++; } if (items.Count > 1) { var allScreenRect = SystemInformation.VirtualScreen; ScreenCaptureDevice device = new ScreenCaptureDevice { DisplayRegion = allScreenRect, CaptureRegion = allScreenRect, Resolution = allScreenRect.Size, Properties = captureProperties, Name = "All Screens (" + allScreenRect.Width + "x" + allScreenRect.Height + ")", DeviceId = "AllScreens", }; items.Add(new ComboBoxItem { Name = device.Name,//+ "" + s.Bounds.ToString(), Tag = device, }); } var customRegion = Config.Data.SelectAreaRectangle; ScreenCaptureDevice customRegionDescr = new ScreenCaptureDevice { CaptureRegion = customRegion, DisplayRegion = Rectangle.Empty, Resolution = customRegion.Size, Properties = captureProperties, Name = "Screen Region", DeviceId = "ScreenRegion", }; items.Add(new ComboBoxItem { Name = customRegionDescr.Name, Tag = customRegionDescr, }); var captDevices = MediaToolkit.MediaFoundation.MfTool.FindUvcDevices(); if (captDevices.Count > 0) { var captItems = captDevices.Select(d => new ComboBoxItem { Name = d.Name, Tag = d, }); items.AddRange(captItems); } WindowCaptureDevice windowCapture = new WindowCaptureDevice { ProcName = "NotFound", hWnd = IntPtr.Zero, ClientRect = Rectangle.Empty, Resolution = Size.Empty, Properties = captureProperties, Name = "_Application Window", DeviceId = "AppWindow", }; Process p = Process.GetProcessesByName("calc").FirstOrDefault(); if (p != null) { var hwnd = p.MainWindowHandle; var procName = p.ProcessName; var clientRect = User32.GetClientRect(hwnd); windowCapture.ProcName = procName; windowCapture.hWnd = hwnd; windowCapture.ClientRect = clientRect; windowCapture.Resolution = clientRect.Size; windowCapture.Name = "_Application Window (" + procName + ")"; } items.Add(new ComboBoxItem { Name = windowCapture.Name, Tag = windowCapture, }); videoSourceItems = new BindingList <ComboBoxItem>(items); videoSourceComboBox.DisplayMember = "Name"; videoSourceComboBox.DataSource = videoSourceItems; }
public void Setup(VideoCaptureDevice captDeviceSettings) { if (captDeviceSettings.CaptureMode == CaptureMode.Screen) { ScreenCaptSettings = (ScreenCaptureDevice)captDeviceSettings; LoadCaptureTypes(); var captureProps = ScreenCaptSettings.Properties; captureMouseCheckBox.Checked = captureProps.CaptureMouse; var fps = captureProps.Fps; if (fps > fpsNumeric.Maximum) { fps = (int)fpsNumeric.Maximum; } else if (fps < fpsNumeric.Minimum) { fps = (int)fpsNumeric.Minimum; } var captureType = captureProps.CaptureType; var captureItem = captureTypes.FirstOrDefault(i => (VideoCaptureType)i.Tag == captureType) ?? captureTypes.FirstOrDefault(); captureTypesComboBox.SelectedItem = captureItem; fpsNumeric.Value = fps; showDebugInfoCheckBox.Checked = captureProps.ShowDebugInfo; showCaptureBorderCheckBox.Checked = captureProps.ShowDebugBorder; screenCaptureDetailsPanel.Visible = true; } else if (captDeviceSettings.CaptureMode == CaptureMode.AppWindow) { WindowsCaptSettings = (WindowCaptureDevice)captDeviceSettings; LoadCaptureTypes(); var captureProps = WindowsCaptSettings.Properties; captureMouseCheckBox.Checked = captureProps.CaptureMouse; var fps = captureProps.Fps; if (fps > fpsNumeric.Maximum) { fps = (int)fpsNumeric.Maximum; } else if (fps < fpsNumeric.Minimum) { fps = (int)fpsNumeric.Minimum; } var captureType = captureProps.CaptureType; var captureItem = captureTypes.FirstOrDefault(i => (VideoCaptureType)i.Tag == captureType) ?? captureTypes.FirstOrDefault(); captureTypesComboBox.SelectedItem = captureItem; fpsNumeric.Value = fps; showDebugInfoCheckBox.Checked = captureProps.ShowDebugInfo; showCaptureBorderCheckBox.Checked = captureProps.ShowDebugBorder; screenCaptureDetailsPanel.Visible = true; } }
// private Screen currentScreen = null; private void httpStartButton_Click(object sender, EventArgs e) { //currentScreen = HttpGetCurrentScreen(); var srcRect = HttpGetCurrentScreen(); //currentScreen.Bounds; //var srcRect = currentScreen.Bounds; var _destWidth = (int)httpDestWidthNumeric.Value; var _destHeight = (int)httpDestHeightNumeric.Value; var destSize = new Size(_destWidth, _destHeight); var ratio = srcRect.Width / (double)srcRect.Height; int destWidth = destSize.Width; int destHeight = (int)(destWidth / ratio); if (ratio < 1) { destHeight = destSize.Height; destWidth = (int)(destHeight * ratio); } destSize = new Size(destWidth, destHeight); var fps = httpFpsNumeric.Value; var addr = httpAddrTextBox.Text; var port = (int)httpPortNumeric.Value; VideoCaptureType captureType = (VideoCaptureType)captureTypesComboBox.SelectedItem; httpScreenSource = new ScreenSource(); ScreenCaptureDevice captureParams = new ScreenCaptureDevice { CaptureRegion = srcRect, Resolution = destSize, }; captureParams.Properties.CaptureType = captureType;//CaptureType.DXGIDeskDupl, captureParams.Properties.Fps = (int)fps; captureParams.Properties.CaptureMouse = true; captureParams.Properties.AspectRatio = true; captureParams.Properties.UseHardware = false; if (captureType == VideoCaptureType.GDI || captureType == VideoCaptureType.GDIPlus) {// масштабируем на энкодере captureParams.Resolution = new Size(srcRect.Width, srcRect.Height); } httpScreenSource.Setup(captureParams); httpStreamer = new VideoHttpStreamer(httpScreenSource); NetworkSettings networkParams = new NetworkSettings { RemoteAddr = addr, RemotePort = port, }; VideoEncoderSettings encodingParams = new VideoEncoderSettings { Width = destSize.Width, // options.Width, Height = destSize.Height, // options.Height, //Resolution = destSize, FrameRate = new MediaRatio((int)fps, 1), EncoderId = "mjpeg", }; httpStreamer.Setup(encodingParams, networkParams); httpStreamer.Start(); httpScreenSource.Start(); statisticForm.Location = srcRect.Location; // statisticForm.Start(); }
public void Setup(HttpScreenStreamerArgs args) { logger.Debug("HttpScreenStreamer::Setup() " + args.ToString()); if (state != MediaState.Closed) { throw new InvalidOperationException("Invalid state " + State); } errorCode = 0; //var srcRect = System.Windows.Forms.Screen.PrimaryScreen.Bounds; //var destSize = new Size(1920, 1080); var srcRect = args.CaptureRegion; var destSize = args.Resolution; var ratio = srcRect.Width / (double)srcRect.Height; int destWidth = destSize.Width; int destHeight = (int)(destWidth / ratio); if (ratio < 1) { destHeight = destSize.Height; destWidth = (int)(destHeight * ratio); } destSize = new Size(destWidth, destHeight); var captureType = args.CaptureTypes; var captureProp = new ScreenCaptureProperties { CaptureType = captureType, Fps = (int)args.Fps, CaptureMouse = args.CaptureMouse, AspectRatio = true, UseHardware = false, }; ScreenCaptureDevice captureParams = new ScreenCaptureDevice { CaptureRegion = srcRect, Resolution = destSize, Properties = captureProp, //CaptureType = captureType, //Fps = (int)args.Fps, //CaptureMouse = args.CaptureMouse, //AspectRatio = true, //UseHardware = false, }; if (captureType == VideoCaptureType.GDI || captureType == VideoCaptureType.GDILayered || captureType == VideoCaptureType.GDIPlus || captureType == VideoCaptureType.Datapath) {// масштабируем на энкодере captureParams.Resolution = new Size(srcRect.Width, srcRect.Height); } VideoEncoderSettings encodingParams = new VideoEncoderSettings { EncoderFormat = VideoCodingFormat.JPEG, //Resolution = destSize, Width = destSize.Width, Height = destSize.Height, FrameRate = new MediaRatio(captureParams.Properties.Fps, 1), EncoderId = "mjpeg", }; NetworkSettings networkParams = new NetworkSettings { RemoteAddr = args.Addres, RemotePort = args.Port, }; try { httpScreenSource = new ScreenSource(); httpScreenSource.Setup(captureParams); httpScreenSource.CaptureStopped += HttpScreenSource_CaptureStopped; httpStreamer = new VideoHttpStreamer(httpScreenSource); httpStreamer.Setup(encodingParams, networkParams); httpStreamer.StreamerStopped += HttpStreamer_StreamerStopped; state = MediaState.Initialized; } catch (Exception ex) { logger.Error(ex); errorCode = 100503; Close(); throw; } }