From 1b1fb131b353c249c75a402d9ce7b28d2d5b3422 Mon Sep 17 00:00:00 2001 From: StevenPuttemans Date: Tue, 17 Sep 2013 13:05:28 +0200 Subject: [PATCH] Attempt at fixing bug 3186 --- modules/highgui/src/cap_v4l.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/modules/highgui/src/cap_v4l.cpp b/modules/highgui/src/cap_v4l.cpp index 045c6f889c..cfb115328a 100644 --- a/modules/highgui/src/cap_v4l.cpp +++ b/modules/highgui/src/cap_v4l.cpp @@ -248,6 +248,10 @@ make & enjoy! #define CHANNEL_NUMBER 1 #define MAX_CAMERAS 8 +// Extra global values declared for error capture +#define CAPV4L2_CAMERA_UNPLUGGED 2 +#define CAPV4L2_OK 1 +#define CAPV4L2_FAIL 0 // default and maximum number of V4L buffers, not including last, 'special' buffer #define MAX_V4L_BUFFERS 10 @@ -1236,10 +1240,10 @@ static int read_frame_v4l2(CvCaptureCAM_V4L* capture) { //set timestamp in capture struct to be timestamp of most recent frame capture->timestamp = buf.timestamp; - return 1; + return CAPV4L2_CAMERA_UNPLUGGED; } -static void mainloop_v4l2(CvCaptureCAM_V4L* capture) { +static int mainloop_v4l2(CvCaptureCAM_V4L* capture) { unsigned int count; count = 1; @@ -1273,8 +1277,11 @@ static void mainloop_v4l2(CvCaptureCAM_V4L* capture) { break; } - if (read_frame_v4l2 (capture)) - break; + switch(int readresult = read_frame_v4l2(capture) ) + case CAM_UNPLUGGED: + return CAPV4L2_FAIL; + default: + return CAPV4L2_OK; } } } @@ -1354,7 +1361,10 @@ static int icvGrabFrameCAM_V4L(CvCaptureCAM_V4L* capture) { { // skip first frame. it is often bad -- this is unnotied in traditional apps, // but could be fatal if bad jpeg is enabled - mainloop_v4l2(capture); + if(!mainloop_v4l2(capture)){ + fprintf( stderr, "HIGHGUI ERROR: V4L: Could not capture image.\n"); + return 0; + } } #endif @@ -1367,7 +1377,10 @@ static int icvGrabFrameCAM_V4L(CvCaptureCAM_V4L* capture) { if (V4L2_SUPPORT == 1) { - mainloop_v4l2(capture); + if(!mainloop_v4l2(capture)){ + fprintf( stderr, "HIGHGUI ERROR: V4L: Could not capture image.\n"); + return 0; + } } #endif /* HAVE_CAMV4L2 */