From ccdd3ebabbe274c435500268a2f7e291297de451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C3=ABl=20Carr=C3=A9?= <rafael.carre@savoirfairelinux.com> Date: Wed, 29 Jun 2011 10:54:49 -0400 Subject: [PATCH] * #6179: VideoV4l2List uses a signed device index to signal an error Add a method to find a device by its name (to be used later in preferences) --- sflphone-common/src/video/test/test_v4l2.cpp | 6 +++++- sflphone-common/src/video/video_v4l2_list.cpp | 15 +++++++++++++-- sflphone-common/src/video/video_v4l2_list.h | 5 +++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/sflphone-common/src/video/test/test_v4l2.cpp b/sflphone-common/src/video/test/test_v4l2.cpp index 86799185c1..60518af8cb 100644 --- a/sflphone-common/src/video/test/test_v4l2.cpp +++ b/sflphone-common/src/video/test/test_v4l2.cpp @@ -41,10 +41,14 @@ using namespace sfl_video; int main() { - unsigned idx; + int idx; VideoV4l2List list; std::vector<std::string> v = list.getDeviceList(); idx = list.getDeviceIndex(); + if (idx < 0) { + cout << "No devices found!" << endl; + return 0; + } list.setDevice(idx); size_t i, n = v.size(); diff --git a/sflphone-common/src/video/video_v4l2_list.cpp b/sflphone-common/src/video/video_v4l2_list.cpp index ec2bc80d56..c944309335 100644 --- a/sflphone-common/src/video/video_v4l2_list.cpp +++ b/sflphone-common/src/video/video_v4l2_list.cpp @@ -57,7 +57,7 @@ extern "C" { namespace sfl_video { -VideoV4l2List::VideoV4l2List() : _currentDevice(0) +VideoV4l2List::VideoV4l2List() : _currentDevice(-1) { #ifdef HAVE_UDEV struct udev *udev; @@ -196,6 +196,8 @@ bool VideoV4l2List::addDevice(const std::string &dev) VideoV4l2Device v(fd, s); GiveUniqueName(v, devices); devices.push_back(v); + if (_currentDevice < 0) + _currentDevice = 0; close(fd); return true; @@ -230,7 +232,16 @@ std::vector<std::string> VideoV4l2List::getDeviceList(void) return v; } -unsigned VideoV4l2List::getDeviceIndex() +int VideoV4l2List::getDeviceIndex(const std::string &name) +{ + for (size_t i = 0; i < devices.size(); i++) + if (devices[i].name == name) + return i; + + return -1; +} + +int VideoV4l2List::getDeviceIndex() { return _currentDevice; } diff --git a/sflphone-common/src/video/video_v4l2_list.h b/sflphone-common/src/video/video_v4l2_list.h index 40c031cc5b..9e076b8afb 100644 --- a/sflphone-common/src/video/video_v4l2_list.h +++ b/sflphone-common/src/video/video_v4l2_list.h @@ -43,7 +43,8 @@ class VideoV4l2List { VideoV4l2List(); void setDevice(unsigned index); - unsigned getDeviceIndex(void); + int getDeviceIndex(void); + int getDeviceIndex(const std::string &name); std::vector<std::string> getDeviceList(void); VideoV4l2Device &getDevice(void); @@ -53,7 +54,7 @@ class VideoV4l2List { */ bool addDevice(const std::string &dev); std::vector<VideoV4l2Device> devices; - unsigned _currentDevice; + int _currentDevice; }; } // namespace sfl_video -- GitLab