AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

VideoCaptureDevice.NewFrame stops firing

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

VideoCaptureDevice.NewFrame stops firing

Postby AndrewR » Fri Nov 09, 2018 2:52 pm

Hello,
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.
AndrewR
 
Posts: 3
Joined: Fri Feb 19, 2016 11:53 am

Re: VideoCaptureDevice.NewFrame stops firing

Postby andrew.kirillov » Fri Nov 09, 2018 9:48 pm

Hello,

To isolate the issue a bit ... Did you try running any of the examples provided with AForge.NET Framework running with your camera? Can you reproduce the same issue? If examples work fine, then look for a bug in your application.

Did you try other applications as well with your camera? Just to make sure it is not a problem of driver or something?
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3437
Joined: Fri Jan 23, 2009 9:12 am
Location: UK




Return to AForge.NET Framework