ofThread and ofVideoGrabber

Hello there!

Having some trouble getting a basic threaded video grabber working as per the example on the ofThread page in the docs. The following compiles but then quits out with a “BAD_EXC_ACCESS” message as soon as it runs. Would very much appreciate some pointers as to where I’m going wrong.

Cheers in advance, sCam

testApp.cpp

  
  
void testApp::setup() {  
    thread.startThread(true, false);  
}  
void testApp::update() {  
    thread.lock();  
    myImage = thread.image;  
    thread.unlock();  
}  
void testApp::draw() {  
	myImage.draw(0, 0);  
}  
void testApp::exit() {  
    thread.stopThread();  
}  
  

MyThread.h

  
  
class MyThread : public ofThread {  
	  
	public:  
	  
	ofVideoGrabber cam;  
	ofImage image;  
	  
	MyThread() {  
		cam.initGrabber(640, 480);  
		cam.setDeviceID(12);  
	}  
	  
    void threadedFunction() {  
        while(isThreadRunning()) {  
            cam.update();  
            if(cam.isFrameNew()) {  
                lock();  
                image.setFromPixels(cam.getPixelsRef());      
                unlock();  
            }     
        }  
    }  
};  
  

I’m not sure but important to know is are you setting your image to draw by value or by reference? In your thread code you set from pixelsRef. I don’t know what setfrompixels does exactly but if you are setting by reference in update by the time you get to draw it might be gone already… but again I don’t know by heart. Can you tell where you get the BAD ACCESS exactly?

Hey!

Thanks for the advice; this could well be wrong! :wink:

However, even if I take EVERYTHING out and try to just simply instantiate an ofVideoGrabber in the thread, as soon as I add a call to update() or grabFrame() in the threadedFunction it still does the same thing. Same result if the call is inside the lock() or not. I’ve also tried setting setUseTextures to false to remove anything GL related, but still no dice.

This leaves me pretty stuck…can anyone shed any light on this?

Cheers, sCam

Hello again,

Think I might have cracked it. I wasn’t initiating the grabber properly; although I was calling setUseTextures, I think I was then screwing that up by not including a false value after w and h for initGrabber. In fact the latter is all you need to do.

With regard the reference issue, I couldn’t get the setup outlined on the ofThread docs page to work with ofImage, so I did something a little different with ofPixels. This all seems to be working…I think!

MyThread.h

  
  
class MyThread : public ofThread {  
	  
	public:  
	  
	ofVideoGrabber cam;  
	ofPixels pixels;  
	  
	MyThread() {  
		cam.initGrabber(320,240,false);  
	}  
	  
	void threadedFunction() {  
		while(isThreadRunning()) {  
			cam.update();  
			if(cam.isFrameNew()) {  
				lock();  
				pixels = cam.getPixelsRef();   
				unlock();  
			}	  
		}  
	}  
};  
  

testApp.cpp

  
  
void testApp::setup() {  
	//videoTexture is an ofTexture  
	videoTexture.allocate(320,240, GL_RGB);  
	thread.startThread(true, false);  
}  
void testApp::update() {  
	thread.lock();  
	videoTexture.loadData(thread.pixels);  
	thread.unlock();  
}  
void testApp::draw() {  
	videoTexture.draw(0, 0);  
}  
void testApp::exit() {  
	thread.stopThread();  
}  
  

I know that this is kind of an old topic, but i just had the same problem while handling images in a thread.

To be able to get the Pixels of a videoGrabber into an image, it is important to setUseTexture to false.

The setup of videoGrabber and image looks like this:

  
        vidGrabber.initGrabber(camWidth, camHeight, false);  
        exportImage.setUseTexture(false);  

I’ll be pushing my hole code to github later today: https://github.com/wirbrennen/ofxDocuApp

best,
/m

Yeah. The documentation is messed up. The ofThread example just crashes like no tomorrow.

The question is why? though. Why is it impossible to use a texture with a threaded VideoGrabber?

Apologies for digging up an old thread.

Because OpenGL functions can only be called from the thread where the context was created, in this case the window. Thus, it is not possible to do things like uploading a texture to the graphics card in other threads. @MatthiasEsterl has the correct technique.

For another example see this addon https://github.com/bakercp/ofxIpVideoGrabber which connects to an ip video source in one thread, sets the pixels, but only uploads the pixels to the graphics card in the main loop (i.e. when update() is called).

1 Like

i just tried the ofxDocuApp and works great. But i am struggling to create multiple instances of it. I am trying to get multiple video cams grabbing each in their own thread.
has this worked for you?
i get crash notices at different places after the app starts.

Could you provide a little bit more insight into your issue (code, errors)?
I’ve never tried to hock up more than one camera, but from my understanding, it should work.

thanks for taking the time. i figured out how to create multiple instances of video grabber and have each in a different thread. all is good :slight_smile:

Any chance, you post your solution for future reference?

for sure it is on my todo list, once i clean it up more.

here are my two experiments