From afda24d6d82eb7b6e1a806fc532a81a2162c09bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Blin?= <sebastien.blin@savoirfairelinux.com> Date: Wed, 2 Sep 2020 14:29:53 -0400 Subject: [PATCH] v4l2: use serial id Change-Id: I7f4d6e5baef04b99a2cf1435aa6848aff7bd84df --- .../video/v4l2/video_device_monitor_impl.cpp | 82 +++++++------------ 1 file changed, 30 insertions(+), 52 deletions(-) diff --git a/src/media/video/v4l2/video_device_monitor_impl.cpp b/src/media/video/v4l2/video_device_monitor_impl.cpp index 0a36bb675d..36ff035465 100644 --- a/src/media/video/v4l2/video_device_monitor_impl.cpp +++ b/src/media/video/v4l2/video_device_monitor_impl.cpp @@ -82,33 +82,11 @@ private: }; std::string -getDeviceString(udev* udev, const std::string& dev_path) +getDeviceString(struct udev_device* udev_device) { - std::string unique_device_string; - struct stat statbuf; - if (stat(dev_path.c_str(), &statbuf) < 0) { - return {}; - } - auto type = S_ISBLK(statbuf.st_mode) ? 'b' : S_ISCHR(statbuf.st_mode) ? 'c' : 0; - - auto opened_dev = udev_device_new_from_devnum(udev, type, statbuf.st_rdev); - auto dev = opened_dev; - - while (dev != nullptr) { - auto serial = udev_device_get_sysattr_value(dev, "serial"); - if (nullptr == serial) { - dev = udev_device_get_parent(dev); - } else { - unique_device_string += udev_device_get_sysattr_value(dev, "idVendor"); - unique_device_string += udev_device_get_sysattr_value(dev, "idProduct"); - unique_device_string += serial; - break; - } - } - if (opened_dev) { - udev_device_unref(opened_dev); - } - return unique_device_string; + if (auto serial = udev_device_get_property_value(udev_device, "ID_SERIAL")) + return serial; + throw std::invalid_argument("No ID_SERIAL detected"); } static int @@ -167,14 +145,14 @@ VideoDeviceMonitorImpl::VideoDeviceMonitorImpl(VideoDeviceMonitor* monitor) // udev_device_get_devnode will fail continue; } - auto unique_name = getDeviceString(udev_, path); - JAMI_DBG("udev: adding device with id %s", unique_name.c_str()); - std::map<std::string, std::string> info = {{"devPath", path}}; - std::vector<std::map<std::string, std::string>> devInfo = {info}; try { + auto unique_name = getDeviceString(dev); + JAMI_DBG("udev: adding device with id %s", unique_name.c_str()); + std::map<std::string, std::string> info = {{"devPath", path}}; + std::vector<std::map<std::string, std::string>> devInfo = {info}; monitor_->addDevice(unique_name, &devInfo); currentPathToId_.emplace(path, unique_name); - } catch (const std::runtime_error& e) { + } catch (const std::exception& e) { JAMI_ERR("%s", e.what()); } } @@ -253,30 +231,30 @@ VideoDeviceMonitorImpl::run() // udev_device_get_devnode will fail break; } - auto unique_name = getDeviceString(udev_, path); - - const char* action = udev_device_get_action(dev); - if (!strcmp(action, "add")) { - JAMI_DBG("udev: adding device with id %s", unique_name.c_str()); - std::map<std::string, std::string> info = {{"devPath", path}}; - std::vector<std::map<std::string, std::string>> devInfo = {info}; - try { + try { + auto unique_name = getDeviceString(dev); + + const char* action = udev_device_get_action(dev); + if (!strcmp(action, "add")) { + JAMI_DBG("udev: adding device with id %s", unique_name.c_str()); + std::map<std::string, std::string> info = {{"devPath", path}}; + std::vector<std::map<std::string, std::string>> devInfo = {info}; monitor_->addDevice(unique_name, &devInfo); currentPathToId_.emplace(path, unique_name); - } catch (const std::runtime_error& e) { - JAMI_ERR("%s", e.what()); - } - } else if (!strcmp(action, "remove")) { - auto it = currentPathToId_.find(path); - if (it != currentPathToId_.end()) { - JAMI_DBG("udev: removing %s", it->second.c_str()); - monitor_->removeDevice(it->second); - currentPathToId_.erase(it); - } else { - // In case of fallback - JAMI_DBG("udev: removing %s", path); - monitor_->removeDevice(path); + } else if (!strcmp(action, "remove")) { + auto it = currentPathToId_.find(path); + if (it != currentPathToId_.end()) { + JAMI_DBG("udev: removing %s", it->second.c_str()); + monitor_->removeDevice(it->second); + currentPathToId_.erase(it); + } else { + // In case of fallback + JAMI_DBG("udev: removing %s", path); + monitor_->removeDevice(path); + } } + } catch (const std::exception& e) { + JAMI_ERR("%s", e.what()); } } udev_device_unref(dev); -- GitLab