From ca1afb8f4afd2f8449e846f47f4a469f30479fd8 Mon Sep 17 00:00:00 2001 From: Vivien Didelot <vivien.didelot@savoirfairelinux.com> Date: Tue, 10 Jun 2014 16:58:00 -0400 Subject: [PATCH] VideoV4l2Device: open device in constructor Refs #47657 Change-Id: I97d4907ef38fc74d52c4fa5c02f1b63889015dc4 --- .../src/video/v4l2/video_device_monitor.cpp | 25 +++++-------------- daemon/src/video/v4l2/video_v4l2.cpp | 8 +++++- daemon/src/video/v4l2/video_v4l2.h | 2 +- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/daemon/src/video/v4l2/video_device_monitor.cpp b/daemon/src/video/v4l2/video_device_monitor.cpp index f38a980e26..4e3524cd14 100644 --- a/daemon/src/video/v4l2/video_device_monitor.cpp +++ b/daemon/src/video/v4l2/video_device_monitor.cpp @@ -84,7 +84,7 @@ class VideoDeviceMonitorImpl { std::vector<VideoV4l2Device>::const_iterator findDevice(const std::string &name) const; NON_COPYABLE(VideoDeviceMonitorImpl); void delDevice(const std::string &node); - bool addDevice(const std::string &dev); + void addDevice(const std::string &dev); std::vector<VideoV4l2Device> devices_; std::thread thread_; mutable std::mutex mutex_; @@ -170,8 +170,7 @@ udev_failed: std::stringstream ss; ss << "/dev/video" << idx; try { - if (!addDevice(ss.str())) - return; + addDevice(ss.str()); } catch (const std::runtime_error &e) { ERROR("%s", e.what()); return; @@ -277,9 +276,8 @@ void VideoDeviceMonitorImpl::run() if (!strcmp(action, "add")) { DEBUG("udev: adding %s", node); try { - if (addDevice(node)) { - Manager::instance().getVideoManager()->deviceEvent(); - } + addDevice(node); + Manager::instance().getVideoManager()->deviceEvent(); } catch (const std::runtime_error &e) { ERROR("%s", e.what()); } @@ -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_); - int fd = open(dev.c_str(), O_RDWR); - if (fd == -1) { - ERROR("Problem opening device"); - strErr(); - return false; - } - - const string s(dev); - VideoV4l2Device v(fd, s); + VideoV4l2Device v(dev); giveUniqueName(v, devices_); devices_.push_back(v); - - ::close(fd); - return true; } vector<string> diff --git a/daemon/src/video/v4l2/video_v4l2.cpp b/daemon/src/video/v4l2/video_v4l2.cpp index 3385c6816e..55d474a44e 100644 --- a/daemon/src/video/v4l2/video_v4l2.cpp +++ b/daemon/src/video/v4l2/video_v4l2.cpp @@ -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_() { + int fd = open(device.c_str(), O_RDWR); + if (fd == -1) + throw std::runtime_error("could not open device"); + v4l2_capability cap; if (ioctl(fd, VIDIOC_QUERYCAP, &cap)) throw std::runtime_error("could not query capabilities"); @@ -356,6 +360,8 @@ VideoV4l2Device::VideoV4l2Device(int fd, const string &device) : input.index = ++idx; } + + ::close(fd); } vector<string> VideoV4l2Device::getChannelList() const diff --git a/daemon/src/video/v4l2/video_v4l2.h b/daemon/src/video/v4l2/video_v4l2.h index 0e1d46b9e3..15af31cc32 100644 --- a/daemon/src/video/v4l2/video_v4l2.h +++ b/daemon/src/video/v4l2/video_v4l2.h @@ -94,7 +94,7 @@ class VideoV4l2Device { /** * @throw std::runtime_error */ - VideoV4l2Device(int fd, const std::string &device); + VideoV4l2Device(const std::string &device); std::string device; std::string name; -- GitLab