1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/video/capture/linux/video_capture_device_factory_linux.h"
9 #if defined(OS_OPENBSD)
10 #include <sys/videoio.h>
12 #include <linux/videodev2.h>
14 #include <sys/ioctl.h>
16 #include "base/files/file_enumerator.h"
17 #include "base/files/scoped_file.h"
18 #include "base/posix/eintr_wrapper.h"
19 #include "base/strings/stringprintf.h"
20 #if defined(OS_CHROMEOS)
21 #include "media/video/capture/linux/video_capture_device_chromeos.h"
23 #include "media/video/capture/linux/video_capture_device_linux.h"
27 static bool HasUsableFormats(int fd) {
29 std::list<int> usable_fourccs;
31 media::VideoCaptureDeviceLinux::GetListOfUsableFourCCs(false,
34 memset(&fmtdesc, 0, sizeof(v4l2_fmtdesc));
35 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
37 while (HANDLE_EINTR(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc)) == 0) {
38 if (std::find(usable_fourccs.begin(), usable_fourccs.end(),
39 fmtdesc.pixelformat) != usable_fourccs.end())
47 VideoCaptureDeviceFactoryLinux::VideoCaptureDeviceFactoryLinux(
48 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
49 : ui_task_runner_(ui_task_runner) {
52 VideoCaptureDeviceFactoryLinux::~VideoCaptureDeviceFactoryLinux() {
55 scoped_ptr<VideoCaptureDevice> VideoCaptureDeviceFactoryLinux::Create(
56 const VideoCaptureDevice::Name& device_name) {
57 DCHECK(thread_checker_.CalledOnValidThread());
58 #if defined(OS_CHROMEOS)
59 VideoCaptureDeviceChromeOS* self =
60 new VideoCaptureDeviceChromeOS(ui_task_runner_, device_name);
62 VideoCaptureDeviceLinux* self = new VideoCaptureDeviceLinux(device_name);
65 return scoped_ptr<VideoCaptureDevice>();
66 // Test opening the device driver. This is to make sure it is available.
67 // We will reopen it again in our worker thread when someone
68 // allocates the camera.
69 base::ScopedFD fd(HANDLE_EINTR(open(device_name.id().c_str(), O_RDONLY)));
71 DVLOG(1) << "Cannot open device";
73 return scoped_ptr<VideoCaptureDevice>();
76 return scoped_ptr<VideoCaptureDevice>(self);
79 void VideoCaptureDeviceFactoryLinux::GetDeviceNames(
80 VideoCaptureDevice::Names* const device_names) {
81 DCHECK(thread_checker_.CalledOnValidThread());
82 DCHECK(device_names->empty());
83 base::FilePath path("/dev/");
84 base::FileEnumerator enumerator(
85 path, false, base::FileEnumerator::FILES, "video*");
87 while (!enumerator.Next().empty()) {
88 base::FileEnumerator::FileInfo info = enumerator.GetInfo();
90 std::string unique_id = path.value() + info.GetName().value();
91 base::ScopedFD fd(HANDLE_EINTR(open(unique_id.c_str(), O_RDONLY)));
93 // Failed to open this device.
96 // Test if this is a V4L2 capture device.
98 if ((HANDLE_EINTR(ioctl(fd.get(), VIDIOC_QUERYCAP, &cap)) == 0) &&
99 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) &&
100 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) {
101 // This is a V4L2 video capture device
102 if (HasUsableFormats(fd.get())) {
103 VideoCaptureDevice::Name device_name(base::StringPrintf("%s", cap.card),
105 device_names->push_back(device_name);
107 DVLOG(1) << "No usable formats reported by " << info.GetName().value();
113 void VideoCaptureDeviceFactoryLinux::GetDeviceSupportedFormats(
114 const VideoCaptureDevice::Name& device,
115 VideoCaptureFormats* supported_formats) {
116 DCHECK(thread_checker_.CalledOnValidThread());
117 if (device.id().empty())
119 base::ScopedFD fd(HANDLE_EINTR(open(device.id().c_str(), O_RDONLY)));
120 if (!fd.is_valid()) {
121 // Failed to open this device.
124 supported_formats->clear();
126 // Retrieve the caps one by one, first get pixel format, then sizes, then
127 // frame rates. See http://linuxtv.org/downloads/v4l-dvb-apis for reference.
128 v4l2_fmtdesc pixel_format = {};
129 pixel_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
130 while (HANDLE_EINTR(ioctl(fd.get(), VIDIOC_ENUM_FMT, &pixel_format)) == 0) {
131 VideoCaptureFormat supported_format;
132 supported_format.pixel_format =
133 VideoCaptureDeviceLinux::V4l2ColorToVideoCaptureColorFormat(
134 (int32)pixel_format.pixelformat);
135 if (supported_format.pixel_format == PIXEL_FORMAT_UNKNOWN) {
136 ++pixel_format.index;
140 v4l2_frmsizeenum frame_size = {};
141 frame_size.pixel_format = pixel_format.pixelformat;
142 while (HANDLE_EINTR(ioctl(fd.get(), VIDIOC_ENUM_FRAMESIZES, &frame_size)) ==
144 if (frame_size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
145 supported_format.frame_size.SetSize(
146 frame_size.discrete.width, frame_size.discrete.height);
147 } else if (frame_size.type == V4L2_FRMSIZE_TYPE_STEPWISE) {
148 // TODO(mcasas): see http://crbug.com/249953, support these devices.
150 } else if (frame_size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
151 // TODO(mcasas): see http://crbug.com/249953, support these devices.
154 v4l2_frmivalenum frame_interval = {};
155 frame_interval.pixel_format = pixel_format.pixelformat;
156 frame_interval.width = frame_size.discrete.width;
157 frame_interval.height = frame_size.discrete.height;
158 std::list<float> frame_rates;
159 while (HANDLE_EINTR(ioctl(
160 fd.get(), VIDIOC_ENUM_FRAMEINTERVALS, &frame_interval)) == 0) {
161 if (frame_interval.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
162 if (frame_interval.discrete.numerator != 0) {
163 frame_rates.push_back(
164 static_cast<float>(frame_interval.discrete.denominator) /
165 static_cast<float>(frame_interval.discrete.numerator));
167 } else if (frame_interval.type == V4L2_FRMIVAL_TYPE_CONTINUOUS) {
168 // TODO(mcasas): see http://crbug.com/249953, support these devices.
171 } else if (frame_interval.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
172 // TODO(mcasas): see http://crbug.com/249953, support these devices.
176 ++frame_interval.index;
179 // Some devices, e.g. Kinect, do not enumerate any frame rates. For these
180 // devices, we do not want to lose all enumeration (pixel format and
181 // resolution), so we return a frame rate of zero instead.
182 if (frame_rates.empty())
183 frame_rates.push_back(0);
185 for (std::list<float>::iterator it = frame_rates.begin();
186 it != frame_rates.end(); ++it) {
187 supported_format.frame_rate = *it;
188 supported_formats->push_back(supported_format);
189 DVLOG(1) << device.name() << " " << supported_format.ToString();
193 ++pixel_format.index;