libv4l2: error turning on stream: Invalid argument VIDIOC_STREAMON: Invalid argument

12,604

This occure when camera is not released by your program - for example when you kill it using Ctrl + C. You can use custom way to exit camera reading loop - for example wait for Esc:

while (1) { // camera loop
    cap.read(frame);
    ...
    (frame processing)
    ...
    if (waitKey(20) == 27) { // wait for 'Esc' key press for 20ms
        break; //  if 'Esc' key is pressed, break loop
    }
}

Or better, you can catch Ctrl + C (SIGINT signal):

bool stop = false;

void sigIntHandler(int signal) {
    stop = true;
}


std::signal(SIGINT, sigIntHandler);

while (!stop) { // camera loop
    cap.read(frame);
    ...
    (frame processing)
    ...
}

See: Signal handling in C++ reference

Share:
12,604

Related videos on Youtube

The Beast
Author by

The Beast

Updated on September 18, 2022

Comments

  • The Beast
    The Beast over 1 year

    I'm working on camera using opencv 3.0.0 and C++ with Ubuntu 14.04 ,

    When I run a program using the camera and stop it with Ctrl + C, I can't get it run for a second time and it gives me the following:

    libv4l2: error turning on stream: Invalid argument
    VIDIOC_STREAMON: Invalid argument
    Cannot read a frame from video stream
    

    I have tried lsusb and the camera (it's built in in laptop not external) is not recognized. Also with cheeze it tells me that there is an error when trying to open the camera.

    The only solution is to reboot the laptop.

    Any suggestion will be appreciated :) thanks