From 83d915eca1f4f3e1cb28b2d7444890d911e7f9a9 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 31 May 2018 14:03:02 +0300 Subject: [PATCH] videoio(v4l): don't pre-scan or cache list of available cameras --- modules/videoio/src/cap_v4l.cpp | 88 ++++++++++++--------------------- 1 file changed, 32 insertions(+), 56 deletions(-) diff --git a/modules/videoio/src/cap_v4l.cpp b/modules/videoio/src/cap_v4l.cpp index e292b7290a..f236131ad7 100644 --- a/modules/videoio/src/cap_v4l.cpp +++ b/modules/videoio/src/cap_v4l.cpp @@ -347,43 +347,10 @@ static int icvSetPropertyCAM_V4L( CvCaptureCAM_V4L* capture, int property_id, /*********************** Implementations ***************************************/ -static int numCameras = 0; -static int indexList = 0; - CvCaptureCAM_V4L::~CvCaptureCAM_V4L() { icvCloseCAM_V4L(this); } -/* Simple test program: Find number of Video Sources available. - Start from 0 and go to MAX_CAMERAS while checking for the device with that name. - If it fails on the first attempt of /dev/video0, then check if /dev/video is valid. - Returns the global numCameras with the correct value (we hope) */ - -static void icvInitCapture_V4L() { - int deviceHandle; - int CameraNumber; - char deviceName[MAX_DEVICE_DRIVER_NAME]; - - CameraNumber = 0; - while(CameraNumber < MAX_CAMERAS) { - /* Print the CameraNumber at the end of the string with a width of one character */ - sprintf(deviceName, "/dev/video%1d", CameraNumber); - /* Test using an open to see if this new device name really does exists. */ - deviceHandle = open(deviceName, O_RDONLY); - if (deviceHandle != -1) { - /* This device does indeed exist - add it to the total so far */ - // add indexList - indexList|=(1 << CameraNumber); - numCameras++; - } - if (deviceHandle != -1) - close(deviceHandle); - /* Set up to test the next /dev/video source in line */ - CameraNumber++; - } /* End while */ - -}; /* End icvInitCapture_V4L */ - static bool try_palette_v4l2(CvCaptureCAM_V4L* capture) { capture->form = v4l2_format(); @@ -785,39 +752,48 @@ static bool v4l2_reset( CvCaptureCAM_V4L* capture) { bool CvCaptureCAM_V4L::open(int _index) { - int autoindex = 0; - char _deviceName[MAX_DEVICE_DRIVER_NAME]; - - if (!numCameras) - icvInitCapture_V4L(); /* Haven't called icvInitCapture yet - do it now! */ - if (!numCameras) - return false; /* Are there any /dev/video input sources? */ - - //search index in indexList - if ( (_index>-1) && ! ((1 << _index) & indexList) ) - { - fprintf( stderr, "VIDEOIO ERROR: V4L: index %d is not correct!\n",_index); - return false; /* Did someone ask for not correct video source number? */ - } - + cv::String name; /* Select camera, or rather, V4L video source */ - if (_index<0) { // Asking for the first device available - for (; autoindex