libcamera: Introduce V4L2Device base class

The V4L2 devices and subdevices share a few common operations,like
opening and closing a device node, and perform IOCTLs on the device.

With the forthcoming introduction of support for V4L2 controls, the
quantity of shared code will increase, as the control support
implementation is identical for the two derived classes.

To maximize code re-use and avoid duplications, provide a V4L2Device
base class which groups the common operations and members.

The newly introduced base class provides methods to open/close a device
node, access the file descriptor, and perform IOCTLs on the device.

Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
This commit is contained in:
Jacopo Mondi
2019-06-19 15:46:44 +02:00
parent 3a6c4bd146
commit d6bb56a90a
7 changed files with 234 additions and 141 deletions
+41
View File
@@ -0,0 +1,41 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* v4l2_device.h - Common base for V4L2 video devices and subdevices
*/
#ifndef __LIBCAMERA_V4L2_DEVICE_H__
#define __LIBCAMERA_V4L2_DEVICE_H__
#include <string>
#include "log.h"
namespace libcamera {
class V4L2Device : protected Loggable
{
public:
void close();
bool isOpen() const { return fd_ != -1; }
const std::string &deviceNode() const { return deviceNode_; }
protected:
V4L2Device(const std::string &deviceNode);
~V4L2Device();
int open(unsigned int flags);
int ioctl(unsigned long request, void *argp);
int fd() { return fd_; }
private:
std::string deviceNode_;
int fd_;
};
} /* namespace libcamera */
#endif /* __LIBCAMERA_V4L2_DEVICE_H__ */
+2 -4
View File
@@ -16,6 +16,7 @@
#include "formats.h"
#include "log.h"
#include "media_object.h"
#include "v4l2_device.h"
namespace libcamera {
@@ -28,7 +29,7 @@ struct V4L2SubdeviceFormat {
const std::string toString() const;
};
class V4L2Subdevice : protected Loggable
class V4L2Subdevice : public V4L2Device
{
public:
explicit V4L2Subdevice(const MediaEntity *entity);
@@ -37,8 +38,6 @@ public:
~V4L2Subdevice();
int open();
bool isOpen() const;
void close();
const MediaEntity *entity() const { return entity_; }
@@ -65,7 +64,6 @@ private:
Rectangle *rect);
const MediaEntity *entity_;
int fd_;
};
} /* namespace libcamera */
+2 -5
View File
@@ -18,6 +18,7 @@
#include "formats.h"
#include "log.h"
#include "v4l2_device.h"
namespace libcamera {
@@ -112,7 +113,7 @@ public:
const std::string toString() const;
};
class V4L2VideoDevice : protected Loggable
class V4L2VideoDevice : public V4L2Device
{
public:
explicit V4L2VideoDevice(const std::string &deviceNode);
@@ -123,13 +124,11 @@ public:
V4L2VideoDevice &operator=(const V4L2VideoDevice &) = delete;
int open();
bool isOpen() const;
void close();
const char *driverName() const { return caps_.driver(); }
const char *deviceName() const { return caps_.card(); }
const char *busName() const { return caps_.bus_info(); }
const std::string &deviceNode() const { return deviceNode_; }
int getFormat(V4L2DeviceFormat *format);
int setFormat(V4L2DeviceFormat *format);
@@ -171,8 +170,6 @@ private:
Buffer *dequeueBuffer();
void bufferAvailable(EventNotifier *notifier);
std::string deviceNode_;
int fd_;
V4L2Capability caps_;
enum v4l2_buf_type bufferType_;