henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 2 | * libjingle |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * Copyright 2009 Google Inc. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | /* |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | * Author: lexnikitin@google.com (Alexey Nikitin) |
| 30 | * |
| 31 | * V4LLookup provides basic functionality to work with V2L2 devices in Linux |
| 32 | * The functionality is implemented as a class with virtual methods for |
| 33 | * the purpose of unit testing. |
| 34 | */ |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame^] | 35 | #include "webrtc/media/devices/v4llookup.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | |
| 37 | #include <errno.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <linux/types.h> |
| 40 | #include <linux/videodev2.h> |
pbos@webrtc.org | 371243d | 2014-03-07 15:22:04 +0000 | [diff] [blame] | 41 | #include <string.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | #include <sys/ioctl.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | #include <sys/stat.h> |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 44 | #include <sys/types.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | #include <unistd.h> |
| 46 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 47 | #include "webrtc/base/logging.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 48 | |
| 49 | namespace cricket { |
| 50 | |
| 51 | V4LLookup *V4LLookup::v4l_lookup_ = NULL; |
| 52 | |
| 53 | bool V4LLookup::CheckIsV4L2Device(const std::string& device_path) { |
| 54 | // check device major/minor numbers are in the range for video devices. |
| 55 | struct stat s; |
| 56 | |
| 57 | if (lstat(device_path.c_str(), &s) != 0 || !S_ISCHR(s.st_mode)) return false; |
| 58 | |
| 59 | int video_fd = -1; |
| 60 | bool is_v4l2 = false; |
| 61 | |
| 62 | // check major/minur device numbers are in range for video device |
| 63 | if (major(s.st_rdev) == 81) { |
| 64 | dev_t num = minor(s.st_rdev); |
| 65 | if (num <= 63) { |
| 66 | video_fd = ::open(device_path.c_str(), O_RDONLY | O_NONBLOCK); |
| 67 | if ((video_fd >= 0) || (errno == EBUSY)) { |
| 68 | ::v4l2_capability video_caps; |
| 69 | memset(&video_caps, 0, sizeof(video_caps)); |
| 70 | |
| 71 | if ((errno == EBUSY) || |
| 72 | (::ioctl(video_fd, VIDIOC_QUERYCAP, &video_caps) >= 0 && |
| 73 | (video_caps.capabilities & V4L2_CAP_VIDEO_CAPTURE))) { |
| 74 | LOG(LS_INFO) << "Found V4L2 capture device " << device_path; |
| 75 | |
| 76 | is_v4l2 = true; |
| 77 | } else { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 78 | LOG_ERRNO(LS_ERROR) << "VIDIOC_QUERYCAP failed for " << device_path; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | } |
| 80 | } else { |
sergeyu@chromium.org | 9cf037b | 2014-02-07 19:03:26 +0000 | [diff] [blame] | 81 | LOG_ERRNO(LS_ERROR) << "Failed to open " << device_path; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (video_fd >= 0) |
| 87 | ::close(video_fd); |
| 88 | |
| 89 | return is_v4l2; |
| 90 | } |
| 91 | |
| 92 | }; // namespace cricket |