1 /*
2 * libjingle
3 * Copyright 2009 Google Inc.
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 /*
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 */
35 #include "talk/media/devices/v4llookup.h"
36
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <linux/types.h>
40 #include <linux/videodev2.h>
41 #include <string.h>
42 #include <sys/ioctl.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <unistd.h>
46
47 #include "webrtc/base/logging.h"
48
49 namespace cricket {
50
51 V4LLookup *V4LLookup::v4l_lookup_ = NULL;
52
CheckIsV4L2Device(const std::string & device_path)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 {
78 LOG_ERRNO(LS_ERROR) << "VIDIOC_QUERYCAP failed for " << device_path;
79 }
80 } else {
81 LOG_ERRNO(LS_ERROR) << "Failed to open " << device_path;
82 }
83 }
84 }
85
86 if (video_fd >= 0)
87 ::close(video_fd);
88
89 return is_v4l2;
90 }
91
92 }; // namespace cricket
93