I’m trying to capture the output of a simple OF program on a Raspberry Pi 3. I’ve looked in here and found a few different approaches, but none seems to work on this hardware, so I tried to grab the screen every time something changes, and later on I would put all the saved images together to a movie file with ffmpeg. Doing this in the main thread stalls the program until an image is saved, making the program not responsive at all (the program receives input from a keyboard and displays the output as strings).
I’m trying to do this with threads, following the third example from ofBook (ofBook - Threads). Here’s my class definition in threadedClass.h (please bear with me, my C++ knowledge is next to nothing):
class ImageSaver: public ofThread {
public:
ImageSaver() {
this->saved = true;
}
void save(int width, int height, int ndx) {
this->width = width;
this->height = height;
this->index = ndx;
this->imgName = ofToString(index) + ".jpg";
this->saved = false;
startThread();
}
bool saved;
private:
void threadedFunction() {
img.grabScreen(0, 0, width, height);
img.saveImage(imgName);
this->saved = true;
}
ofImage img;
string imgName;
int index;
int width;
int height;
};
Here’s how I import it in ofApp.h:
vector<unique_ptr<ImageSaver>> imgSavers;
And here’s how I’m trying to save the output in ofApp.cpp, whenever input is received from the keyboard:
frameCounter++;
if (captureScreen) {
foundAvailableThread = false;
for (int i = 0; i < imgSavers.size(); i++) {
if (imgSavers[i].saved) {
imgSaves[i].save(width, height, frameCounter);
foundAvailableThread = true;
break;
}
}
if (!foundAvailableThread) {
imgSavers.push_back(move(unique_ptr<ImageSaver>(new ImageSaver)));
imgSavers.back()->save(width, height, frameCounter);
}
captureScreen = false;
}
The above code is happening in draw() because the keyboard input is received via OSC instead of keyPressed() or keyRelease().
The program compiles and projects the text written with the keyboard fine. It also saves .jpg images with the frame number in bin/data, but they’re all black. I read in the ofBook chapter that all GL operations have to happen in the main thread, because openGL is single-threaded, but I guess there must be a way to do this, since addons like ofxVideoRecorder are threaded (though its code is too complicated for me to understand).
Any help appreciated.
1 post - 1 participant
Read full topic