Dr. Lawlor's Code, Robots, & Things

April 8, 2014

OpenCV: fix for V4L VIDIOC_S_CROP error

Filed under: Random Thoughts — Dr. Lawlor @ 5:00 pm

I’ve been playing with the excellent little OpenCV library ArUco for detecting the 3D position and orientation of printed QR-code style “markers” in live video feed from a webcam, but I kept getting this annoying error now and then:

VIDIOC_STREAMON: Invalid argument
VIDIOC_STREAMON: Invalid argument

What’s weird about this is it wouldn’t happen every time, normally things would work fine until this started, and then it’d keep happening until I unpluged and re-plugged the USB camera.  

Turns out, this happens when you press ctrl-C (interrupt) during the middle of a video capture.  Evidently this screws up something in the kernel (dmesg shows “xhci_hcd 0000:00:14.0: ERROR Transfer event for disabled endpoint or incorrect stream ring”), preventing further video streaming from that device.  Operationally it’s quite annoying, and for an autonomous robot, you can’t easily unplug and replug just to get things working again.  

A simple workaround is to catch SIGINT, the signal generated by ctrl-C, and delay shutdown until after the next video frame has fully arrived:

/* Keep the webcam from locking up when you interrupt a frame capture */
volatile int quit_signal=0;
#ifdef __unix__
#include <signal.h>
extern "C" void quit_signal_handler(int signum) {
if (quit_signal!=0) exit(0); // just exit already
printf("Will quit at next camera frame (repeat to kill now)\n");

... in main setup ...
#ifdef __unix__
signal(SIGINT,quit_signal_handler); // listen for ctrl-C
... in video capture loop ...
yourCaptureDevice >> videoImage;
if (quit_signal) exit(0); // exit cleanly on interrupt

My webcam now works reliably, even when I ctrl-C out of the analysis program!   (I’m running Ubuntu 13.11 and OpenCV 2.4.1, but this may be a bug in the Linux kernel 3.11.)



  1. Hey Dr. Lawlor. I seldom leave a message but this time, I must say something to you. Thank you. You saved me. How did you know that? Anyway, thank you very much. (I’m using Ubuntu 14.04 and opencv 1.4.10 and it works perfectly.)

    Comment by MinkyuChoi — March 5, 2015 @ 5:21 am

  2. but how to reset the camera after ctrl+c thanks

    Comment by chafajht — June 20, 2015 @ 8:31 am

    • If your program catches ctrl-C and shuts down cleanly, you don’t get this error.

      But you can still just unplug and replug the camera to make it work again!

      Comment by Dr. Lawlor — June 22, 2015 @ 1:22 am

      • thanks but it’s the internal camera of my laptop !!! :p

        Comment by chafajht — June 27, 2015 @ 4:21 am

  3. Eclipse interrupts with SIGTERM, so by adding
    signal(SIGTERM,quit_signal_handler); // listen for SIGTERM
    you can prevent this issue while working in Eclipse CDT environment.

    Comment by Saren Tasciyan — November 22, 2015 @ 7:23 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: