[kinovea-dev] Asynchronous recording

  • From: Joan <joan@xxxxxxxxxxx>
  • To: kinovea-dev@xxxxxxxxxxxxx
  • Date: Wed, 13 Jul 2011 17:36:12 +0200

Hi everyone,
A message (and questions) regarding threading in the Capture screen.

Yesterday I have comitted an important change in the Capture code to fix bug250. The relevant svn blurb is (r479): - On frame grab, switch back to main thread (=UI thread) as fast as possible. - Creation of a producer-consumer class for recording. Spawns a new thread and push frames in a queue. The queue is then processed by this new recording thread. - Removed dropping and flickering protection as they don't fit in the new paradigm. Will have to implement queue overflow protection.


In 0.8.15, there was only two threads for capture, the grabbing thread on which we receive new frames (thread created within AForge library code, we just hook the event handler) and the Main/UI thread.

We were doing too much work in the grabbing thread (i.e: copy the frame, push it to buffer, read other part of buffer, create another copy for saving, send to saving routine, wait for it to return, signal UI to redraw). Doing all this may take longer than the frame grabbing interval, and the grabbing thread was slowed down.

The consequence was frame grabs more further apart in time than the actual framerate, and a resulting video with wrong timing compared to what we put in the header.

Now there are 3 threads and we spend only the minimum time in the grabbing handler.

tl;dr:
Under heavy load the grabbing thread is competing with the other threads and the UI become unresponsive. The frames accumulates in the recording queue. We didn't have this behavior before because the recording operations were synchronous with the grabbing. Grabbing was simply slowed by exactly as much as the CPU needed.

Any comments, ideas, guidelines on how to tackle this issue ?
I experimented with lowering the grabbing thread priority (back to wrong timing) or increasing main thread one (not much help for the recording queue overflow)
Thanks
joan

PS: I'm leaving town tonight until monday, but I'll sure read all your awsome ideas when I come back !


Other related posts:

  • » [kinovea-dev] Asynchronous recording - Joan