I'm developing a screencasting utility in C++.
It basically captures desktop frames and creates an AVI file. The algorithm is as follows:
- Create a thread:
this->m_hThread=CreateThread(NULL,0,thScreenCapture,this,0,NULL);
Capture desktop in
thScreenCapture
n times per second (like 5 fps).obj->Capture();
In Capture(), append the bitmap data to the avi file.
this->appendBitmapToAvi(this->avifile, bmp);
This utility also records sound. So, in method thScreenCapture, sound data is also being appended to the avi file.
The problem is that a lag occurs between frames and sound when more than 6 frames (this can change depending on hardware configuration) are captured per second.
I'm seeking for a solution to optimize the algorithm. A solution may be buffering frames in memory, not appending all of them to the avi file on-the-fly. But this makes the code more complex because I have to deal with the sound data that's being captured in a different thread.
What do you suggest to increase the fps value that this utility supports without losing synchronization?