blob: 011f63ac289318cd493d907a4c8b1b21a2ab35ed [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00002 * libjingle
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 * Copyright 2009 Google Inc.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00004 *
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.org28e20752013-07-10 00:45:36 +000029 * 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 */
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/devices/v4llookup.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37#include <errno.h>
38#include <fcntl.h>
39#include <linux/types.h>
40#include <linux/videodev2.h>
pbos@webrtc.org371243d2014-03-07 15:22:04 +000041#include <string.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#include <sys/ioctl.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include <sys/stat.h>
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000044#include <sys/types.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045#include <unistd.h>
46
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000047#include "webrtc/base/logging.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
49namespace cricket {
50
51V4LLookup *V4LLookup::v4l_lookup_ = NULL;
52
53bool 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.org9cf037b2014-02-07 19:03:26 +000078 LOG_ERRNO(LS_ERROR) << "VIDIOC_QUERYCAP failed for " << device_path;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 }
80 } else {
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000081 LOG_ERRNO(LS_ERROR) << "Failed to open " << device_path;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082 }
83 }
84 }
85
86 if (video_fd >= 0)
87 ::close(video_fd);
88
89 return is_v4l2;
90}
91
92}; // namespace cricket