Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: media/video/capture/linux/video_capture_device_factory_linux.cc

Issue 877193005: Reland (2): Linux Video Capture: Add support for multiplanar YUV420 format enumeration (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Bug fix Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/video/capture/linux/video_capture_device_factory_linux.h" 5 #include "media/video/capture/linux/video_capture_device_factory_linux.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #if defined(OS_OPENBSD) 9 #if defined(OS_OPENBSD)
10 #include <sys/videoio.h> 10 #include <sys/videoio.h>
11 #else 11 #else
12 #include <linux/videodev2.h> 12 #include <linux/videodev2.h>
13 #endif 13 #endif
14 #include <sys/ioctl.h> 14 #include <sys/ioctl.h>
15 15
16 #include "base/files/file_enumerator.h" 16 #include "base/files/file_enumerator.h"
17 #include "base/files/scoped_file.h" 17 #include "base/files/scoped_file.h"
18 #include "base/posix/eintr_wrapper.h" 18 #include "base/posix/eintr_wrapper.h"
19 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
20 #if defined(OS_CHROMEOS) 20 #if defined(OS_CHROMEOS)
21 #include "media/video/capture/linux/video_capture_device_chromeos.h" 21 #include "media/video/capture/linux/video_capture_device_chromeos.h"
22 #endif 22 #endif
23 #include "media/video/capture/linux/video_capture_device_linux.h" 23 #include "media/video/capture/linux/video_capture_device_linux.h"
24 24
25 namespace media { 25 namespace media {
26 26
27 static bool HasUsableFormats(int fd) { 27 static bool HasUsableFormats(int fd, uint32 capabilities) {
28 const std::list<int>& usable_fourccs = 28 const std::list<int>& usable_fourccs =
29 VideoCaptureDeviceLinux::GetListOfUsableFourCCs(false); 29 VideoCaptureDeviceLinux::GetListOfUsableFourCCs(false);
30 30
31 v4l2_fmtdesc fmtdesc = {}; 31 static const struct {
32 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 32 int capability;
33 for (; HANDLE_EINTR(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc)) == 0; 33 v4l2_buf_type buf_type;
34 ++fmtdesc.index) { 34 } kCapabilityAndBufferTypes[] = {
35 if (std::find(usable_fourccs.begin(), usable_fourccs.end(), 35 {V4L2_CAP_VIDEO_CAPTURE, V4L2_BUF_TYPE_VIDEO_CAPTURE},
36 fmtdesc.pixelformat) != usable_fourccs.end()) 36 {V4L2_CAP_VIDEO_CAPTURE_MPLANE, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE}
37 return true; 37 };
38
39 for (const auto& capability_and_buffer_type : kCapabilityAndBufferTypes) {
40 v4l2_fmtdesc fmtdesc = {};
41 if (capabilities & capability_and_buffer_type.capability) {
42 fmtdesc.type = capability_and_buffer_type.buf_type;
43 for (; HANDLE_EINTR(ioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc)) == 0;
44 ++fmtdesc.index) {
45 if (std::find(usable_fourccs.begin(), usable_fourccs.end(),
46 fmtdesc.pixelformat) != usable_fourccs.end())
47 return true;
48 }
49 }
38 } 50 }
39 return false; 51 return false;
40 } 52 }
41 53
54 static std::list<float> GetFrameRateList(int fd,
55 uint32 fourcc,
56 uint32 width,
57 uint32 height) {
58 std::list<float> frame_rates;
59
60 v4l2_frmivalenum frame_interval = {};
61 frame_interval.pixel_format = fourcc;
62 frame_interval.width = width;
63 frame_interval.height = height;
64 for (; HANDLE_EINTR(ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS,
65 &frame_interval)) == 0; ++frame_interval.index) {
66 if (frame_interval.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
67 if (frame_interval.discrete.numerator != 0) {
68 frame_rates.push_back(frame_interval.discrete.denominator /
69 static_cast<float>(frame_interval.discrete.numerator));
70 }
71 } else if (frame_interval.type == V4L2_FRMIVAL_TYPE_CONTINUOUS ||
72 frame_interval.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
73 // TODO(mcasas): see http://crbug.com/249953, support these devices.
74 NOTIMPLEMENTED();
75 break;
76 }
77 }
78 // Some devices, e.g. Kinect, do not enumerate any frame rates, see
79 // http://crbug.com/412284. Set their frame_rate to zero.
80 if (frame_rates.empty())
81 frame_rates.push_back(0);
82 return frame_rates;
83 }
84
85 static void GetSupportedFormatsForV4L2BufferType(
86 int fd,
87 v4l2_buf_type buf_type,
88 media::VideoCaptureFormats* supported_formats) {
89 v4l2_fmtdesc v4l2_format = {};
90 v4l2_format.type = buf_type;
91 for (; HANDLE_EINTR(ioctl(fd, VIDIOC_ENUM_FMT, &v4l2_format)) == 0;
92 ++v4l2_format.index) {
93 VideoCaptureFormat supported_format;
94 supported_format.pixel_format =
95 VideoCaptureDeviceLinux::V4l2FourCcToChromiumPixelFormat(
96 v4l2_format.pixelformat);
97
98 if (supported_format.pixel_format == PIXEL_FORMAT_UNKNOWN)
99 continue;
100
101 v4l2_frmsizeenum frame_size = {};
102 frame_size.pixel_format = v4l2_format.pixelformat;
103 for (; HANDLE_EINTR(ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frame_size)) == 0;
104 ++frame_size.index) {
105 if (frame_size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
106 supported_format.frame_size.SetSize(frame_size.discrete.width,
107 frame_size.discrete.height);
108 } else if (frame_size.type == V4L2_FRMSIZE_TYPE_STEPWISE ||
109 frame_size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
110 // TODO(mcasas): see http://crbug.com/249953, support these devices.
111 NOTIMPLEMENTED();
112 }
113
114 const std::list<float> frame_rates = GetFrameRateList(
115 fd, v4l2_format.pixelformat, frame_size.discrete.width,
116 frame_size.discrete.height);
117 for (const auto& frame_rate : frame_rates) {
118 supported_format.frame_rate = frame_rate;
119 supported_formats->push_back(supported_format);
120 DVLOG(1) << supported_format.ToString();
121 }
122 }
123 }
124 }
125
42 VideoCaptureDeviceFactoryLinux::VideoCaptureDeviceFactoryLinux( 126 VideoCaptureDeviceFactoryLinux::VideoCaptureDeviceFactoryLinux(
43 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) 127 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
44 : ui_task_runner_(ui_task_runner) { 128 : ui_task_runner_(ui_task_runner) {
45 } 129 }
46 130
47 VideoCaptureDeviceFactoryLinux::~VideoCaptureDeviceFactoryLinux() { 131 VideoCaptureDeviceFactoryLinux::~VideoCaptureDeviceFactoryLinux() {
48 } 132 }
49 133
50 scoped_ptr<VideoCaptureDevice> VideoCaptureDeviceFactoryLinux::Create( 134 scoped_ptr<VideoCaptureDevice> VideoCaptureDeviceFactoryLinux::Create(
51 const VideoCaptureDevice::Name& device_name) { 135 const VideoCaptureDevice::Name& device_name) {
52 DCHECK(thread_checker_.CalledOnValidThread()); 136 DCHECK(thread_checker_.CalledOnValidThread());
53 #if defined(OS_CHROMEOS) 137 #if defined(OS_CHROMEOS)
54 VideoCaptureDeviceChromeOS* self = 138 VideoCaptureDeviceChromeOS* self =
55 new VideoCaptureDeviceChromeOS(ui_task_runner_, device_name); 139 new VideoCaptureDeviceChromeOS(ui_task_runner_, device_name);
56 #else 140 #else
57 VideoCaptureDeviceLinux* self = new VideoCaptureDeviceLinux(device_name); 141 VideoCaptureDeviceLinux* self = new VideoCaptureDeviceLinux(device_name);
58 #endif 142 #endif
59 if (!self) 143 if (!self)
60 return scoped_ptr<VideoCaptureDevice>(); 144 return scoped_ptr<VideoCaptureDevice>();
61 // Test opening the device driver. This is to make sure it is available. 145 // Test opening the device driver. This is to make sure it is available.
62 // We will reopen it again in our worker thread when someone 146 // We will reopen it again in our worker thread when someone
63 // allocates the camera. 147 // allocates the camera.
64 base::ScopedFD fd(HANDLE_EINTR(open(device_name.id().c_str(), O_RDONLY))); 148 base::ScopedFD fd(HANDLE_EINTR(open(device_name.id().c_str(), O_RDONLY)));
65 if (!fd.is_valid()) { 149 if (!fd.is_valid()) {
66 DVLOG(1) << "Cannot open device"; 150 DLOG(ERROR) << "Cannot open device";
67 delete self; 151 delete self;
68 return scoped_ptr<VideoCaptureDevice>(); 152 return scoped_ptr<VideoCaptureDevice>();
69 } 153 }
70 154
71 return scoped_ptr<VideoCaptureDevice>(self); 155 return scoped_ptr<VideoCaptureDevice>(self);
72 } 156 }
73 157
74 void VideoCaptureDeviceFactoryLinux::GetDeviceNames( 158 void VideoCaptureDeviceFactoryLinux::GetDeviceNames(
75 VideoCaptureDevice::Names* const device_names) { 159 VideoCaptureDevice::Names* const device_names) {
76 DCHECK(thread_checker_.CalledOnValidThread()); 160 DCHECK(thread_checker_.CalledOnValidThread());
77 DCHECK(device_names->empty()); 161 DCHECK(device_names->empty());
78 const base::FilePath path("/dev/"); 162 const base::FilePath path("/dev/");
79 base::FileEnumerator enumerator( 163 base::FileEnumerator enumerator(
80 path, false, base::FileEnumerator::FILES, "video*"); 164 path, false, base::FileEnumerator::FILES, "video*");
81 165
82 while (!enumerator.Next().empty()) { 166 while (!enumerator.Next().empty()) {
83 const base::FileEnumerator::FileInfo info = enumerator.GetInfo(); 167 const base::FileEnumerator::FileInfo info = enumerator.GetInfo();
84 const std::string unique_id = path.value() + info.GetName().value(); 168 const std::string unique_id = path.value() + info.GetName().value();
85 const base::ScopedFD fd(HANDLE_EINTR(open(unique_id.c_str(), O_RDONLY))); 169 const base::ScopedFD fd(HANDLE_EINTR(open(unique_id.c_str(), O_RDONLY)));
86 if (!fd.is_valid()) { 170 if (!fd.is_valid()) {
87 DLOG(ERROR) << "Couldn't open " << info.GetName().value(); 171 DLOG(ERROR) << "Couldn't open " << info.GetName().value();
88 continue; 172 continue;
89 } 173 }
90 // Test if this is a V4L2 capture device and if it has at least one 174 // Test if this is a V4L2 capture device and if it has at least one
91 // supported capture format. Devices that have capture and output 175 // supported capture format. Devices that have capture and output
92 // capabilities at the same time are memory-to-memory and are skipped, see 176 // capabilities at the same time are memory-to-memory and are skipped, see
93 // http://crbug.com/139356. 177 // http://crbug.com/139356.
94 v4l2_capability cap; 178 v4l2_capability cap;
95 if ((HANDLE_EINTR(ioctl(fd.get(), VIDIOC_QUERYCAP, &cap)) == 0) && 179 if ((HANDLE_EINTR(ioctl(fd.get(), VIDIOC_QUERYCAP, &cap)) == 0) &&
96 (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE && 180 ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE ||
97 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) && 181 cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) &&
98 HasUsableFormats(fd.get())) { 182 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT) &&
99 device_names->push_front(VideoCaptureDevice::Name( 183 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE)) &&
100 base::StringPrintf("%s", cap.card), unique_id)); 184 HasUsableFormats(fd.get(), cap.capabilities)) {
185 VideoCaptureDevice::Name device_name(base::StringPrintf("%s", cap.card),
186 unique_id);
187 device_names->push_back(device_name);
101 } 188 }
102 } 189 }
103 } 190 }
104 191
105 void VideoCaptureDeviceFactoryLinux::GetDeviceSupportedFormats( 192 void VideoCaptureDeviceFactoryLinux::GetDeviceSupportedFormats(
106 const VideoCaptureDevice::Name& device, 193 const VideoCaptureDevice::Name& device,
107 VideoCaptureFormats* supported_formats) { 194 VideoCaptureFormats* supported_formats) {
108 DCHECK(thread_checker_.CalledOnValidThread()); 195 DCHECK(thread_checker_.CalledOnValidThread());
109 if (device.id().empty()) 196 if (device.id().empty())
110 return; 197 return;
111 base::ScopedFD fd(HANDLE_EINTR(open(device.id().c_str(), O_RDONLY))); 198 base::ScopedFD fd(HANDLE_EINTR(open(device.id().c_str(), O_RDONLY)));
112 if (!fd.is_valid()) // Failed to open this device. 199 if (!fd.is_valid()) // Failed to open this device.
113 return; 200 return;
114 supported_formats->clear(); 201 supported_formats->clear();
115 202
116 // Retrieve the caps one by one, first get pixel format, then sizes, then 203 const v4l2_buf_type kCaptureTypes[] = {V4L2_BUF_TYPE_VIDEO_CAPTURE,
117 // frame rates. 204 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE};
118 v4l2_fmtdesc pixel_format = {}; 205 for (const auto& buf_type : kCaptureTypes)
119 pixel_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 206 GetSupportedFormatsForV4L2BufferType(fd.get(), buf_type, supported_formats);
120 for (; HANDLE_EINTR(ioctl(fd.get(), VIDIOC_ENUM_FMT, &pixel_format)) == 0;
121 ++pixel_format.index) {
122 VideoCaptureFormat supported_format;
123 supported_format.pixel_format =
124 VideoCaptureDeviceLinux::V4l2FourCcToChromiumPixelFormat(
125 pixel_format.pixelformat);
126 if (supported_format.pixel_format == PIXEL_FORMAT_UNKNOWN)
127 continue;
128
129 v4l2_frmsizeenum frame_size = {};
130 frame_size.pixel_format = pixel_format.pixelformat;
131 for (; HANDLE_EINTR(ioctl(fd.get(), VIDIOC_ENUM_FRAMESIZES,
132 &frame_size)) == 0;
133 ++frame_size.index) {
134 if (frame_size.type == V4L2_FRMSIZE_TYPE_DISCRETE) {
135 supported_format.frame_size.SetSize(
136 frame_size.discrete.width, frame_size.discrete.height);
137 } else if (frame_size.type == V4L2_FRMSIZE_TYPE_STEPWISE ||
138 frame_size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS) {
139 // TODO(mcasas): see http://crbug.com/249953, support these devices.
140 NOTIMPLEMENTED();
141 }
142
143 v4l2_frmivalenum frame_interval = {};
144 frame_interval.pixel_format = pixel_format.pixelformat;
145 frame_interval.width = frame_size.discrete.width;
146 frame_interval.height = frame_size.discrete.height;
147 std::list<float> frame_rates;
148 for (; HANDLE_EINTR(ioctl(fd.get(), VIDIOC_ENUM_FRAMEINTERVALS,
149 &frame_interval)) == 0;
150 ++frame_interval.index) {
151 if (frame_interval.type == V4L2_FRMIVAL_TYPE_DISCRETE) {
152 if (frame_interval.discrete.numerator != 0) {
153 frame_rates.push_back(frame_interval.discrete.denominator /
154 static_cast<float>(frame_interval.discrete.numerator));
155 }
156 } else if (frame_interval.type == V4L2_FRMIVAL_TYPE_CONTINUOUS ||
157 frame_interval.type == V4L2_FRMIVAL_TYPE_STEPWISE) {
158 // TODO(mcasas): see http://crbug.com/249953, support these devices.
159 NOTIMPLEMENTED();
160 break;
161 }
162 }
163 // Some devices, e.g. Kinect, do not enumerate any frame rates, see
164 // http://crbug.com/412284. Set their frame_rate to zero.
165 if (frame_rates.empty())
166 frame_rates.push_back(0.0f);
167
168 for (const auto& it : frame_rates) {
169 supported_format.frame_rate = it;
170 supported_formats->push_back(supported_format);
171 DVLOG(1) << device.name() << " " << supported_format.ToString();
172 }
173 }
174 }
175 return; 207 return;
176 } 208 }
177 209
178 // static 210 // static
179 VideoCaptureDeviceFactory* 211 VideoCaptureDeviceFactory*
180 VideoCaptureDeviceFactory::CreateVideoCaptureDeviceFactory( 212 VideoCaptureDeviceFactory::CreateVideoCaptureDeviceFactory(
181 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) { 213 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
182 return new VideoCaptureDeviceFactoryLinux(ui_task_runner); 214 return new VideoCaptureDeviceFactoryLinux(ui_task_runner);
183 } 215 }
184 216
185 } // namespace media 217 } // namespace media
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698