Commit f5081712 authored by Pavel Rojtberg's avatar Pavel Rojtberg

v4l: remove needless CvCaptureCAM_V4L_CPP wrapper

possible source of memory leaks and unneeded complexity
parent 93d6e800
...@@ -265,7 +265,7 @@ struct buffer ...@@ -265,7 +265,7 @@ struct buffer
static unsigned int n_buffers = 0; static unsigned int n_buffers = 0;
struct CvCaptureCAM_V4L struct CvCaptureCAM_V4L : public CvCapture
{ {
int deviceHandle; int deviceHandle;
int bufferIndex; int bufferIndex;
...@@ -297,6 +297,13 @@ struct CvCaptureCAM_V4L ...@@ -297,6 +297,13 @@ struct CvCaptureCAM_V4L
/* V4L2 control variables */ /* V4L2 control variables */
Range focus, brightness, contrast, saturation, hue, gain, exposure; Range focus, brightness, contrast, saturation, hue, gain, exposure;
bool open(int _index);
virtual double getProperty(int) const;
virtual bool setProperty(int, double);
virtual bool grabFrame();
virtual IplImage* retrieveFrame(int);
Range getRange(int property_id) const { Range getRange(int property_id) const {
switch (property_id) { switch (property_id) {
case CV_CAP_PROP_BRIGHTNESS: case CV_CAP_PROP_BRIGHTNESS:
...@@ -320,7 +327,7 @@ struct CvCaptureCAM_V4L ...@@ -320,7 +327,7 @@ struct CvCaptureCAM_V4L
} }
} }
~CvCaptureCAM_V4L(); virtual ~CvCaptureCAM_V4L();
}; };
static void icvCloseCAM_V4L( CvCaptureCAM_V4L* capture ); static void icvCloseCAM_V4L( CvCaptureCAM_V4L* capture );
...@@ -743,53 +750,50 @@ static int _capture_V4L2 (CvCaptureCAM_V4L *capture) ...@@ -743,53 +750,50 @@ static int _capture_V4L2 (CvCaptureCAM_V4L *capture)
* this also causes buffers to be reallocated if the frame size was changed. * this also causes buffers to be reallocated if the frame size was changed.
*/ */
static bool v4l2_reset( CvCaptureCAM_V4L* capture) { static bool v4l2_reset( CvCaptureCAM_V4L* capture) {
int index = capture->index;
icvCloseCAM_V4L(capture); icvCloseCAM_V4L(capture);
capture->index = index;
return _capture_V4L2(capture) == 1; return _capture_V4L2(capture) == 1;
} }
static CvCaptureCAM_V4L * icvCaptureFromCAM_V4L (int index) bool CvCaptureCAM_V4L::open(int _index)
{ {
int autoindex = 0; int autoindex = 0;
index = -1; // set the capture to closed state
if (!numCameras) if (!numCameras)
icvInitCapture_V4L(); /* Havent called icvInitCapture yet - do it now! */ icvInitCapture_V4L(); /* Havent called icvInitCapture yet - do it now! */
if (!numCameras) if (!numCameras)
return NULL; /* Are there any /dev/video input sources? */ return false; /* Are there any /dev/video input sources? */
//search index in indexList //search index in indexList
if ( (index>-1) && ! ((1 << index) & indexList) ) if ( (_index>-1) && ! ((1 << _index) & indexList) )
{ {
fprintf( stderr, "VIDEOIO ERROR: V4L: index %d is not correct!\n",index); fprintf( stderr, "VIDEOIO ERROR: V4L: index %d is not correct!\n",_index);
return NULL; /* Did someone ask for not correct video source number? */ return false; /* Did someone ask for not correct video source number? */
} }
/* Select camera, or rather, V4L video source */ /* Select camera, or rather, V4L video source */
if (index<0) { // Asking for the first device available if (_index<0) { // Asking for the first device available
for (; autoindex<MAX_CAMERAS;autoindex++) for (; autoindex<MAX_CAMERAS;autoindex++)
if (indexList & (1<<autoindex)) if (indexList & (1<<autoindex))
break; break;
if (autoindex==MAX_CAMERAS) if (autoindex==MAX_CAMERAS)
return NULL; return false;
index=autoindex; _index=autoindex;
autoindex++;// i can recall icvOpenCAM_V4l with index=-1 for next camera autoindex++;// i can recall icvOpenCAM_V4l with index=-1 for next camera
} }
CvCaptureCAM_V4L* capture = new CvCaptureCAM_V4L(); // will throw on OOM index = _index;
capture->index = index; FirstCapture = 1;
capture->FirstCapture = 1; width = DEFAULT_V4L_WIDTH;
capture->width = DEFAULT_V4L_WIDTH; height = DEFAULT_V4L_HEIGHT;
capture->height = DEFAULT_V4L_HEIGHT; fps = DEFAULT_V4L_FPS;
capture->fps = DEFAULT_V4L_FPS; convert_rgb = true;
capture->convert_rgb = true;
if (_capture_V4L2 (capture) == -1) {
icvCloseCAM_V4L(capture);
delete capture;
return NULL;
}
return capture; return _capture_V4L2(this) == 1;
}; /* End icvOpenCAM_V4L */ }
static int read_frame_v4l2(CvCaptureCAM_V4L* capture) { static int read_frame_v4l2(CvCaptureCAM_V4L* capture) {
v4l2_buffer buf = v4l2_buffer(); v4l2_buffer buf = v4l2_buffer();
...@@ -1863,12 +1867,13 @@ static int icvSetPropertyCAM_V4L( CvCaptureCAM_V4L* capture, ...@@ -1863,12 +1867,13 @@ static int icvSetPropertyCAM_V4L( CvCaptureCAM_V4L* capture,
static void icvCloseCAM_V4L( CvCaptureCAM_V4L* capture ){ static void icvCloseCAM_V4L( CvCaptureCAM_V4L* capture ){
/* Deallocate space - Hopefully, no leaks */ /* Deallocate space - Hopefully, no leaks */
if (capture) if (capture->index > -1)
{ {
if (capture->deviceHandle != -1)
{ {
capture->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; capture->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == ioctl(capture->deviceHandle, VIDIOC_STREAMOFF, &capture->type)) { if (-1 == ioctl(capture->deviceHandle, VIDIOC_STREAMOFF, &capture->type)) {
perror ("Unable to stop the stream."); perror ("Unable to stop the stream");
} }
for (unsigned int n_buffers_ = 0; n_buffers_ < capture->req.count; ++n_buffers_) for (unsigned int n_buffers_ = 0; n_buffers_ < capture->req.count; ++n_buffers_)
...@@ -1888,77 +1893,44 @@ static void icvCloseCAM_V4L( CvCaptureCAM_V4L* capture ){ ...@@ -1888,77 +1893,44 @@ static void icvCloseCAM_V4L( CvCaptureCAM_V4L* capture ){
if (capture->deviceHandle != -1) if (capture->deviceHandle != -1)
close(capture->deviceHandle); close(capture->deviceHandle);
if (capture->frame.imageData) cvFree(&capture->frame.imageData); if (capture->frame.imageData)
//cvFree((void **)capture); cvFree(&capture->frame.imageData);
}
};
class CvCaptureCAM_V4L_CPP : public CvCapture
{
public:
CvCaptureCAM_V4L_CPP() { captureV4L = 0; }
virtual ~CvCaptureCAM_V4L_CPP() { close(); }
bool open( int index ); capture->index = -1; // flag that the capture is closed
void close(); }
virtual double getProperty(int) const;
virtual bool setProperty(int, double);
virtual bool grabFrame();
virtual IplImage* retrieveFrame(int);
protected:
CvCaptureCAM_V4L* captureV4L;
}; };
bool CvCaptureCAM_V4L_CPP::open( int index ) bool CvCaptureCAM_V4L::grabFrame()
{ {
close(); return icvGrabFrameCAM_V4L( this );
captureV4L = icvCaptureFromCAM_V4L(index);
return captureV4L != 0;
} }
void CvCaptureCAM_V4L_CPP::close() IplImage* CvCaptureCAM_V4L::retrieveFrame(int)
{ {
if( captureV4L ) return icvRetrieveFrameCAM_V4L( this, 0 );
{
delete captureV4L;
captureV4L = NULL;
}
} }
bool CvCaptureCAM_V4L_CPP::grabFrame() double CvCaptureCAM_V4L::getProperty( int propId ) const
{ {
return captureV4L ? icvGrabFrameCAM_V4L( captureV4L ) : false; return icvGetPropertyCAM_V4L( this, propId );
} }
IplImage* CvCaptureCAM_V4L_CPP::retrieveFrame(int) bool CvCaptureCAM_V4L::setProperty( int propId, double value )
{ {
return captureV4L ? icvRetrieveFrameCAM_V4L( captureV4L, 0 ) : 0; return icvSetPropertyCAM_V4L( this, propId, value );
}
double CvCaptureCAM_V4L_CPP::getProperty( int propId ) const
{
return captureV4L ? icvGetPropertyCAM_V4L( captureV4L, propId ) : 0.0;
}
bool CvCaptureCAM_V4L_CPP::setProperty( int propId, double value )
{
return captureV4L ? icvSetPropertyCAM_V4L( captureV4L, propId, value ) : false;
} }
} // end namespace cv } // end namespace cv
CvCapture* cvCreateCameraCapture_V4L( int index ) CvCapture* cvCreateCameraCapture_V4L( int index )
{ {
cv::CvCaptureCAM_V4L_CPP* capture = new cv::CvCaptureCAM_V4L_CPP; cv::CvCaptureCAM_V4L* capture = new cv::CvCaptureCAM_V4L();
if( capture->open( index )) if(capture->open(index))
return capture; return capture;
delete capture; delete capture;
return 0; return NULL;
} }
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment