Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I have also faced the same problem while capturing frames from IP camera. I searched on different forums and dug into the source files to pinpoint the line, function and the file where the grab call is getting stuck. So the function CvCapture_FFMPEG::grabFrame() (sources/modules/highgui/src/cap_ffmpeg_impl.h) is stuck in while loop (check the file for clarity) when IP camera is disconnected and somehow the timeout interrupt is not working in this case. You can check yourself by adding log statements at different points in the CvCapture_FFMPEG::grabFrame() function.

So now coming to the solution. There are two ways: First, make a separate thread in which you call the grab frame function. Monitor this thread in the main thread. When the grab function blocks the thread then kill this thread on timeout (which you can decide) and respawn again if you want to retry grabbing the frame. The other way around is to compile OpenCV with Gstreamer instead of ffmpeg (install gstreamer libraries and set the flags as WITH_GSTREAMER=ON and WITH_FFMPEG=OFF when compiling the OpenCV). After compiling with Gstreamer test the grab function: it will return false when your IP camera is disconnected (the way you expected the grab function to work).