Skip to content
Snippets Groups Projects
Commit ca1afb8f authored by Vivien Didelot's avatar Vivien Didelot
Browse files

VideoV4l2Device: open device in constructor

Refs #47657

Change-Id: I97d4907ef38fc74d52c4fa5c02f1b63889015dc4
parent cc1e88b7
Branches
Tags
No related merge requests found
...@@ -84,7 +84,7 @@ class VideoDeviceMonitorImpl { ...@@ -84,7 +84,7 @@ class VideoDeviceMonitorImpl {
std::vector<VideoV4l2Device>::const_iterator findDevice(const std::string &name) const; std::vector<VideoV4l2Device>::const_iterator findDevice(const std::string &name) const;
NON_COPYABLE(VideoDeviceMonitorImpl); NON_COPYABLE(VideoDeviceMonitorImpl);
void delDevice(const std::string &node); void delDevice(const std::string &node);
bool addDevice(const std::string &dev); void addDevice(const std::string &dev);
std::vector<VideoV4l2Device> devices_; std::vector<VideoV4l2Device> devices_;
std::thread thread_; std::thread thread_;
mutable std::mutex mutex_; mutable std::mutex mutex_;
...@@ -170,8 +170,7 @@ udev_failed: ...@@ -170,8 +170,7 @@ udev_failed:
std::stringstream ss; std::stringstream ss;
ss << "/dev/video" << idx; ss << "/dev/video" << idx;
try { try {
if (!addDevice(ss.str())) addDevice(ss.str());
return;
} catch (const std::runtime_error &e) { } catch (const std::runtime_error &e) {
ERROR("%s", e.what()); ERROR("%s", e.what());
return; return;
...@@ -277,9 +276,8 @@ void VideoDeviceMonitorImpl::run() ...@@ -277,9 +276,8 @@ void VideoDeviceMonitorImpl::run()
if (!strcmp(action, "add")) { if (!strcmp(action, "add")) {
DEBUG("udev: adding %s", node); DEBUG("udev: adding %s", node);
try { try {
if (addDevice(node)) { addDevice(node);
Manager::instance().getVideoManager()->deviceEvent(); Manager::instance().getVideoManager()->deviceEvent();
}
} catch (const std::runtime_error &e) { } catch (const std::runtime_error &e) {
ERROR("%s", e.what()); ERROR("%s", e.what());
} }
...@@ -319,24 +317,13 @@ void VideoDeviceMonitorImpl::delDevice(const string &node) ...@@ -319,24 +317,13 @@ void VideoDeviceMonitorImpl::delDevice(const string &node)
} }
} }
bool VideoDeviceMonitorImpl::addDevice(const string &dev) void VideoDeviceMonitorImpl::addDevice(const string &dev)
{ {
std::lock_guard<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
int fd = open(dev.c_str(), O_RDWR); VideoV4l2Device v(dev);
if (fd == -1) {
ERROR("Problem opening device");
strErr();
return false;
}
const string s(dev);
VideoV4l2Device v(fd, s);
giveUniqueName(v, devices_); giveUniqueName(v, devices_);
devices_.push_back(v); devices_.push_back(v);
::close(fd);
return true;
} }
vector<string> vector<string>
......
...@@ -328,9 +328,13 @@ VideoV4l2Size VideoV4l2Channel::getSize(const string &name) const ...@@ -328,9 +328,13 @@ VideoV4l2Size VideoV4l2Channel::getSize(const string &name) const
} }
VideoV4l2Device::VideoV4l2Device(int fd, const string &device) : VideoV4l2Device::VideoV4l2Device(const string &device) :
device(device), name(), channels_() device(device), name(), channels_()
{ {
int fd = open(device.c_str(), O_RDWR);
if (fd == -1)
throw std::runtime_error("could not open device");
v4l2_capability cap; v4l2_capability cap;
if (ioctl(fd, VIDIOC_QUERYCAP, &cap)) if (ioctl(fd, VIDIOC_QUERYCAP, &cap))
throw std::runtime_error("could not query capabilities"); throw std::runtime_error("could not query capabilities");
...@@ -356,6 +360,8 @@ VideoV4l2Device::VideoV4l2Device(int fd, const string &device) : ...@@ -356,6 +360,8 @@ VideoV4l2Device::VideoV4l2Device(int fd, const string &device) :
input.index = ++idx; input.index = ++idx;
} }
::close(fd);
} }
vector<string> VideoV4l2Device::getChannelList() const vector<string> VideoV4l2Device::getChannelList() const
......
...@@ -94,7 +94,7 @@ class VideoV4l2Device { ...@@ -94,7 +94,7 @@ class VideoV4l2Device {
/** /**
* @throw std::runtime_error * @throw std::runtime_error
*/ */
VideoV4l2Device(int fd, const std::string &device); VideoV4l2Device(const std::string &device);
std::string device; std::string device;
std::string name; std::string name;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment