AForge.NET

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

Render and record

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

Render and record

Postby e.preto » Sun Feb 18, 2018 1:56 am

Hello,

I'm using Aforge to render and record at the same time 2 usb capture devices (1920x1080 and 720x576 of resolution).

For recording the input i'm using AsyncVideoSource, and for render I'm using VideoCaptureDevice as code above :
Code: Select all
// create first video source
VideoCaptureDevice videoSourceEndo = new VideoCaptureDevice(videoDevices[cb_endo.SelectedIndex].MonikerString);
AsyncVideoSource asyncVideoSourceEndo = new AsyncVideoSource(videoSourceEndo);
asyncVideoSourceEndo.SkipFramesIfBusy = false;

videoSourcePlayer1.VideoSource = asyncVideoSourceEndo;
videoSourcePlayer1.Start();

videoSourceEndo.NewFrame += VideoSourceEndo_NewFrame;

// create second video source
if (cb_eco.Enabled == true)
{
System.Threading.Thread.Sleep(500);

VideoCaptureDevice videoSourceEco = new VideoCaptureDevice(videoDevices[cb_eco.SelectedIndex].MonikerString);
AsyncVideoSource asyncVideoSourceEco = new AsyncVideoSource(videoSourceEco);
asyncVideoSourceEco.SkipFramesIfBusy = false;

videoSourcePlayer2.VideoSource = asyncVideoSourceEco;
videoSourcePlayer2.Start();

videoSourceEco.NewFrame += VideoSourceEco_NewFrame;
}


When I'm capture the 2 devices, I'm getting 60FPS and 25FPS however when recording de FPS drops to 25FPS (both of them).

Is this normal !? I'm using AsyncVideoSource correctly?
e.preto
 
Posts: 6
Joined: Thu Feb 15, 2018 6:55 pm

Re: Render and record

Postby andrew.kirillov » Sun Feb 18, 2018 9:28 am

Hello,

The purpose of AsyncVideoSource is to make sure processing of video frames is done in a different thread, so the capturing thread is not blocked by lengthy operation. So you can do capturing and processing in parallel. However, if your video processing is slow, then capture thread will get blocked eventually and so FPS will drop. In your case, capture thread already gets new video frame from camera, while processing thread is still saving the previous one. And so capture thread gets blocked, waiting till processing threads becomes free to accept new frame.
With best regards,
Andrew


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

Re: Render and record

Postby e.preto » Mon Feb 19, 2018 5:48 pm

andrew.kirillov wrote:In your case, capture thread already gets new video frame from camera, while processing thread is still saving the previous one. And so capture thread gets blocked, waiting till processing threads becomes free to accept new frame.


You have any recommandation to resolve this problem?!

I forgot to paste the new frame code previously:

Code: Select all
private void VideoSourceEco_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
  if (isRecording && writer2 != null && writer2.IsOpen)
  {
   writer2.WriteVideoFrame(eventArgs.Frame);
  }
}

private void VideoSourceEndo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
  if (isRecording && writer1 != null && writer1.IsOpen)
  {
   writer1.WriteVideoFrame(eventArgs.Frame);
  }
}
e.preto
 
Posts: 6
Joined: Thu Feb 15, 2018 6:55 pm

Re: Render and record

Postby andrew.kirillov » Tue Feb 20, 2018 8:12 am

e.preto wrote:You have any recommandation to resolve this problem?!

Try playing with different codecs, maybe less aggressive compression rate.
With best regards,
Andrew


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

Re: Render and record

Postby e.preto » Tue Feb 20, 2018 5:32 pm

I already tried this before and no significant result with this resolution.

Memory buffer with unsaved images, could that be a solution?!

Thanks a lot Andrew for your support and quick response!
e.preto
 
Posts: 6
Joined: Thu Feb 15, 2018 6:55 pm

Re: Render and record

Postby andrew.kirillov » Tue Feb 20, 2018 6:18 pm

e.preto wrote:Memory buffer with unsaved images, could that be a solution?!

Yes, you can do that. You can create a dedicated thread for video saving only. And then in the NewFrame event handler you can just add new video frames into a saving queue. Then the recording thread will simply pull out images from the queue and save them into video. The only problem is that if video saving is still slower than generation of new images, then the queue size will grow quickly. You may drop some video frames if it is acceptable. But if not, then we have a problem.
With best regards,
Andrew


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

Re: Render and record

Postby e.preto » Sat Mar 17, 2018 6:39 am

andrew.kirillov wrote:Yes, you can do that. You can create a dedicated thread for video saving only. And then in the NewFrame event handler you can just add new video frames into a saving queue. Then the recording thread will simply pull out images from the queue and save them into video. The only problem is that if video saving is still slower than generation of new images, then the queue size will grow quickly. You may drop some video frames if it is acceptable. But if not, then we have a problem.


Hi,

I made the all the changes I said before. My solution was implement a ring buffer that only have the frames (AsyncVideoSource side only add new frame) and on another thread i'm recording the frames (just pick a new frame and add to video). However when I click for recordind the frames drops as previously.

I don't really know the origin of the problem because I measured all the times (frames to buffer = 0ms), and this behavior don't seem right. The video is perfect, with expected fps, but the preview is not right.

Can someone help figure out the origin of the problem?!
e.preto
 
Posts: 6
Joined: Thu Feb 15, 2018 6:55 pm




Return to AForge.NET Framework