I am facing an issue: sometimes frames fust stop coming (VideoCaptureDevice.NewFrame stops firing). It happens after camera restart. I tried:
- Changing web camera
- Changing PC
- Increasing timeout between starting and stopping camera (makes error less common, but does not eliminates it)
On some PCs and webcameras it happens more frequently, on some - less, but I was able to reproduce it almost every time.
While researching this issue I found out that:
- Problem persists after restarting the software (or at least appears shortly after)
- You can run camera in different resolution, but if you choose the same - you won't get any frames
- Looks like camera isn't occupied by some other threads - I've checked that everything's finished correctly before restarting
I wrote a sample that demonstrates the issue:
- Code: Select all
using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Testers
{
public partial class AForgeTester : Form
{
int successfulTests = 0;
System.Timers.Timer notificationTimer = new System.Timers.Timer(10000);
VideoCaptureDevice capture = null;
bool exiting = false;
public AForgeTester()
{
InitializeComponent();
notificationTimer.AutoReset = false;
notificationTimer.Elapsed += NotificationTimer_Elapsed;
}
private void Start()
{
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
capture = new VideoCaptureDevice(videoDevices[0].MonikerString);
capture.VideoResolution = capture.VideoCapabilities[1]; //In my case it is 1280x720
capture.NewFrame += NewFrame;
capture.Start();
notificationTimer.Start();
}
private void NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap frame = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = frame;
if (notificationTimer.Enabled)
{
notificationTimer.Stop();
}
else
{
AddMessage("Frame finally recieved");
}
successfulTests++;
label1.Invoke((Action)(() =>
{
label1.Text = $"Successful tests: {successfulTests}";
}));
new Task(() =>
{
Stop();
//System.Threading.Thread.Sleep(1000); //I tried different timeouts here
if (!exiting)
{
Start();
}
}).Start();
}
void AddMessage(string message)
{
DateTime time = DateTime.Now;
richTextBox1.Invoke((Action)(() =>
{
richTextBox1.AppendText($"{time.ToString("HH:mm:ss:fff")} {message}\n");
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();
}));
}
private void Stop()
{
capture.NewFrame -= NewFrame;
capture.Stop();
capture = null;
}
private void button1_Click(object sender, EventArgs e)
{
AddMessage("Starting test");
successfulTests = 0;
Start();
}
private void NotificationTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
AddMessage("Freeze"); //Problem detected: frames stopped coming
}
private void AForgeTester_FormClosing(object sender, FormClosingEventArgs e)
{
exiting = true;
if (capture !=null)
{
capture.Stop();
}
}
}
}
Am I doing something wrong, or is it a library or hardware flaw? Are there any walkarounds for this issue?
I am struggling with this issue for several weeks already, so any comments or ideas would be appreciated.
Thank you.