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

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

Issue 2143903003: [WIP] Move media/capture to device/capture (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include "media/capture/video/linux/v4l2_capture_delegate.h"
6
7 #include <poll.h>
8 #include <sys/fcntl.h>
9 #include <sys/ioctl.h>
10 #include <sys/mman.h>
11 #include <utility>
12
13 #include "base/bind.h"
14 #include "base/files/file_enumerator.h"
15 #include "base/posix/eintr_wrapper.h"
16 #include "base/strings/stringprintf.h"
17 #include "build/build_config.h"
18 #include "media/base/bind_to_current_loop.h"
19 #include "media/capture/video/linux/video_capture_device_linux.h"
20
21 namespace media {
22
23 // Desired number of video buffers to allocate. The actual number of allocated
24 // buffers by v4l2 driver can be higher or lower than this number.
25 // kNumVideoBuffers should not be too small, or Chrome may not return enough
26 // buffers back to driver in time.
27 const uint32_t kNumVideoBuffers = 4;
28 // Timeout in milliseconds v4l2_thread_ blocks waiting for a frame from the hw.
29 // This value has been fine tuned. Before changing or modifying it see
30 // https://crbug.com/470717
31 const int kCaptureTimeoutMs = 1000;
32 // The number of continuous timeouts tolerated before treated as error.
33 const int kContinuousTimeoutLimit = 10;
34 // MJPEG is preferred if the requested width or height is larger than this.
35 const int kMjpegWidth = 640;
36 const int kMjpegHeight = 480;
37 // Typical framerate, in fps
38 const int kTypicalFramerate = 30;
39
40 // V4L2 color formats supported by V4L2CaptureDelegate derived classes.
41 // This list is ordered by precedence of use -- but see caveats for MJPEG.
42 static struct {
43 uint32_t fourcc;
44 VideoPixelFormat pixel_format;
45 size_t num_planes;
46 } const kSupportedFormatsAndPlanarity[] = {
47 {V4L2_PIX_FMT_YUV420, PIXEL_FORMAT_I420, 1},
48 {V4L2_PIX_FMT_YUYV, PIXEL_FORMAT_YUY2, 1},
49 {V4L2_PIX_FMT_UYVY, PIXEL_FORMAT_UYVY, 1},
50 {V4L2_PIX_FMT_RGB24, PIXEL_FORMAT_RGB24, 1},
51 // MJPEG is usually sitting fairly low since we don't want to have to
52 // decode. However, it is needed for large resolutions due to USB bandwidth
53 // limitations, so GetListOfUsableFourCcs() can duplicate it on top, see
54 // that method.
55 {V4L2_PIX_FMT_MJPEG, PIXEL_FORMAT_MJPEG, 1},
56 // JPEG works as MJPEG on some gspca webcams from field reports, see
57 // https://code.google.com/p/webrtc/issues/detail?id=529, put it as the
58 // least preferred format.
59 {V4L2_PIX_FMT_JPEG, PIXEL_FORMAT_MJPEG, 1},
60 };
61
62 // Fill in |format| with the given parameters.
63 static void FillV4L2Format(v4l2_format* format,
64 uint32_t width,
65 uint32_t height,
66 uint32_t pixelformat_fourcc) {
67 memset(format, 0, sizeof(*format));
68 format->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
69 format->fmt.pix.width = width;
70 format->fmt.pix.height = height;
71 format->fmt.pix.pixelformat = pixelformat_fourcc;
72 }
73
74 // Fills all parts of |buffer|.
75 static void FillV4L2Buffer(v4l2_buffer* buffer, int index) {
76 memset(buffer, 0, sizeof(*buffer));
77 buffer->memory = V4L2_MEMORY_MMAP;
78 buffer->index = index;
79 buffer->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
80 }
81
82 static void FillV4L2RequestBuffer(v4l2_requestbuffers* request_buffer,
83 int count) {
84 memset(request_buffer, 0, sizeof(*request_buffer));
85 request_buffer->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
86 request_buffer->memory = V4L2_MEMORY_MMAP;
87 request_buffer->count = count;
88 }
89
90 // Returns the input |fourcc| as a std::string four char representation.
91 static std::string FourccToString(uint32_t fourcc) {
92 return base::StringPrintf("%c%c%c%c", fourcc & 0xFF, (fourcc >> 8) & 0xFF,
93 (fourcc >> 16) & 0xFF, (fourcc >> 24) & 0xFF);
94 }
95
96 // Class keeping track of a SPLANE V4L2 buffer, mmap()ed on construction and
97 // munmap()ed on destruction.
98 class V4L2CaptureDelegate::BufferTracker
99 : public base::RefCounted<BufferTracker> {
100 public:
101 BufferTracker();
102 // Abstract method to mmap() given |fd| according to |buffer|.
103 bool Init(int fd, const v4l2_buffer& buffer);
104
105 const uint8_t* start() const { return start_; }
106 size_t payload_size() const { return payload_size_; }
107 void set_payload_size(size_t payload_size) {
108 DCHECK_LE(payload_size, length_);
109 payload_size_ = payload_size;
110 }
111
112 private:
113 friend class base::RefCounted<BufferTracker>;
114 virtual ~BufferTracker();
115
116 uint8_t* start_;
117 size_t length_;
118 size_t payload_size_;
119 };
120
121 // static
122 size_t V4L2CaptureDelegate::GetNumPlanesForFourCc(uint32_t fourcc) {
123 for (const auto& fourcc_and_pixel_format : kSupportedFormatsAndPlanarity) {
124 if (fourcc_and_pixel_format.fourcc == fourcc)
125 return fourcc_and_pixel_format.num_planes;
126 }
127 DVLOG(1) << "Unknown fourcc " << FourccToString(fourcc);
128 return 0;
129 }
130
131 // static
132 VideoPixelFormat V4L2CaptureDelegate::V4l2FourCcToChromiumPixelFormat(
133 uint32_t v4l2_fourcc) {
134 for (const auto& fourcc_and_pixel_format : kSupportedFormatsAndPlanarity) {
135 if (fourcc_and_pixel_format.fourcc == v4l2_fourcc)
136 return fourcc_and_pixel_format.pixel_format;
137 }
138 // Not finding a pixel format is OK during device capabilities enumeration.
139 // Let the caller decide if PIXEL_FORMAT_UNKNOWN is an error or
140 // not.
141 DVLOG(1) << "Unsupported pixel format: " << FourccToString(v4l2_fourcc);
142 return PIXEL_FORMAT_UNKNOWN;
143 }
144
145 // static
146 std::list<uint32_t> V4L2CaptureDelegate::GetListOfUsableFourCcs(
147 bool prefer_mjpeg) {
148 std::list<uint32_t> supported_formats;
149 for (const auto& format : kSupportedFormatsAndPlanarity)
150 supported_formats.push_back(format.fourcc);
151
152 // Duplicate MJPEG on top of the list depending on |prefer_mjpeg|.
153 if (prefer_mjpeg)
154 supported_formats.push_front(V4L2_PIX_FMT_MJPEG);
155
156 return supported_formats;
157 }
158
159 V4L2CaptureDelegate::V4L2CaptureDelegate(
160 const VideoCaptureDevice::Name& device_name,
161 const scoped_refptr<base::SingleThreadTaskRunner>& v4l2_task_runner,
162 int power_line_frequency)
163 : v4l2_task_runner_(v4l2_task_runner),
164 device_name_(device_name),
165 power_line_frequency_(power_line_frequency),
166 is_capturing_(false),
167 timeout_count_(0),
168 rotation_(0) {}
169
170 void V4L2CaptureDelegate::AllocateAndStart(
171 int width,
172 int height,
173 float frame_rate,
174 std::unique_ptr<VideoCaptureDevice::Client> client) {
175 DCHECK(v4l2_task_runner_->BelongsToCurrentThread());
176 DCHECK(client);
177 client_ = std::move(client);
178
179 // Need to open camera with O_RDWR after Linux kernel 3.3.
180 device_fd_.reset(HANDLE_EINTR(open(device_name_.id().c_str(), O_RDWR)));
181 if (!device_fd_.is_valid()) {
182 SetErrorState(FROM_HERE, "Failed to open V4L2 device driver file.");
183 return;
184 }
185
186 v4l2_capability cap = {};
187 if (!((HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_QUERYCAP, &cap)) == 0) &&
188 ((cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) &&
189 !(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)))) {
190 device_fd_.reset();
191 SetErrorState(FROM_HERE, "This is not a V4L2 video capture device");
192 return;
193 }
194
195 // Get supported video formats in preferred order. For large resolutions,
196 // favour mjpeg over raw formats.
197 const std::list<uint32_t>& desired_v4l2_formats =
198 GetListOfUsableFourCcs(width > kMjpegWidth || height > kMjpegHeight);
199 std::list<uint32_t>::const_iterator best = desired_v4l2_formats.end();
200
201 v4l2_fmtdesc fmtdesc = {};
202 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
203 for (; HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_ENUM_FMT, &fmtdesc)) == 0;
204 ++fmtdesc.index) {
205 best = std::find(desired_v4l2_formats.begin(), best, fmtdesc.pixelformat);
206 }
207 if (best == desired_v4l2_formats.end()) {
208 SetErrorState(FROM_HERE, "Failed to find a supported camera format.");
209 return;
210 }
211
212 DVLOG(1) << "Chosen pixel format is " << FourccToString(*best);
213 FillV4L2Format(&video_fmt_, width, height, *best);
214
215 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_S_FMT, &video_fmt_)) < 0) {
216 SetErrorState(FROM_HERE, "Failed to set video capture format");
217 return;
218 }
219 const VideoPixelFormat pixel_format =
220 V4l2FourCcToChromiumPixelFormat(video_fmt_.fmt.pix.pixelformat);
221 if (pixel_format == PIXEL_FORMAT_UNKNOWN) {
222 SetErrorState(FROM_HERE, "Unsupported pixel format");
223 return;
224 }
225
226 // Set capture framerate in the form of capture interval.
227 v4l2_streamparm streamparm = {};
228 streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
229 // The following line checks that the driver knows about framerate get/set.
230 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_G_PARM, &streamparm)) >= 0) {
231 // Now check if the device is able to accept a capture framerate set.
232 if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
233 // |frame_rate| is float, approximate by a fraction.
234 streamparm.parm.capture.timeperframe.numerator =
235 media::kFrameRatePrecision;
236 streamparm.parm.capture.timeperframe.denominator =
237 (frame_rate) ? (frame_rate * media::kFrameRatePrecision)
238 : (kTypicalFramerate * media::kFrameRatePrecision);
239
240 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_S_PARM, &streamparm)) <
241 0) {
242 SetErrorState(FROM_HERE, "Failed to set camera framerate");
243 return;
244 }
245 DVLOG(2) << "Actual camera driverframerate: "
246 << streamparm.parm.capture.timeperframe.denominator << "/"
247 << streamparm.parm.capture.timeperframe.numerator;
248 }
249 }
250 // TODO(mcasas): what should be done if the camera driver does not allow
251 // framerate configuration, or the actual one is different from the desired?
252
253 // Set anti-banding/anti-flicker to 50/60Hz. May fail due to not supported
254 // operation (|errno| == EINVAL in this case) or plain failure.
255 if ((power_line_frequency_ == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ||
256 (power_line_frequency_ == V4L2_CID_POWER_LINE_FREQUENCY_60HZ) ||
257 (power_line_frequency_ == V4L2_CID_POWER_LINE_FREQUENCY_AUTO)) {
258 struct v4l2_control control = {};
259 control.id = V4L2_CID_POWER_LINE_FREQUENCY;
260 control.value = power_line_frequency_;
261 const int retval =
262 HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_S_CTRL, &control));
263 if (retval != 0)
264 DVLOG(1) << "Error setting power line frequency removal";
265 }
266
267 capture_format_.frame_size.SetSize(video_fmt_.fmt.pix.width,
268 video_fmt_.fmt.pix.height);
269 capture_format_.frame_rate = frame_rate;
270 capture_format_.pixel_format = pixel_format;
271
272 v4l2_requestbuffers r_buffer;
273 FillV4L2RequestBuffer(&r_buffer, kNumVideoBuffers);
274 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_REQBUFS, &r_buffer)) < 0) {
275 SetErrorState(FROM_HERE, "Error requesting MMAP buffers from V4L2");
276 return;
277 }
278 for (unsigned int i = 0; i < r_buffer.count; ++i) {
279 if (!MapAndQueueBuffer(i)) {
280 SetErrorState(FROM_HERE, "Allocate buffer failed");
281 return;
282 }
283 }
284
285 v4l2_buf_type capture_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
286 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_STREAMON, &capture_type)) <
287 0) {
288 SetErrorState(FROM_HERE, "VIDIOC_STREAMON failed");
289 return;
290 }
291
292 is_capturing_ = true;
293 // Post task to start fetching frames from v4l2.
294 v4l2_task_runner_->PostTask(
295 FROM_HERE, base::Bind(&V4L2CaptureDelegate::DoCapture, this));
296 }
297
298 void V4L2CaptureDelegate::StopAndDeAllocate() {
299 DCHECK(v4l2_task_runner_->BelongsToCurrentThread());
300 // The order is important: stop streaming, clear |buffer_pool_|,
301 // thus munmap()ing the v4l2_buffers, and then return them to the OS.
302 v4l2_buf_type capture_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
303 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_STREAMOFF, &capture_type)) <
304 0) {
305 SetErrorState(FROM_HERE, "VIDIOC_STREAMOFF failed");
306 return;
307 }
308
309 buffer_tracker_pool_.clear();
310
311 v4l2_requestbuffers r_buffer;
312 FillV4L2RequestBuffer(&r_buffer, 0);
313 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_REQBUFS, &r_buffer)) < 0)
314 SetErrorState(FROM_HERE, "Failed to VIDIOC_REQBUFS with count = 0");
315
316 // At this point we can close the device.
317 // This is also needed for correctly changing settings later via VIDIOC_S_FMT.
318 device_fd_.reset();
319 is_capturing_ = false;
320 client_.reset();
321 }
322
323 void V4L2CaptureDelegate::SetRotation(int rotation) {
324 DCHECK(v4l2_task_runner_->BelongsToCurrentThread());
325 DCHECK(rotation >= 0 && rotation < 360 && rotation % 90 == 0);
326 rotation_ = rotation;
327 }
328
329 V4L2CaptureDelegate::~V4L2CaptureDelegate() {}
330
331 bool V4L2CaptureDelegate::MapAndQueueBuffer(int index) {
332 v4l2_buffer buffer;
333 FillV4L2Buffer(&buffer, index);
334
335 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_QUERYBUF, &buffer)) < 0) {
336 DLOG(ERROR) << "Error querying status of a MMAP V4L2 buffer";
337 return false;
338 }
339
340 const scoped_refptr<BufferTracker> buffer_tracker(new BufferTracker());
341 if (!buffer_tracker->Init(device_fd_.get(), buffer)) {
342 DLOG(ERROR) << "Error creating BufferTracker";
343 return false;
344 }
345 buffer_tracker_pool_.push_back(buffer_tracker);
346
347 // Enqueue the buffer in the drivers incoming queue.
348 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_QBUF, &buffer)) < 0) {
349 DLOG(ERROR) << "Error enqueuing a V4L2 buffer back into the driver";
350 return false;
351 }
352 return true;
353 }
354
355 void V4L2CaptureDelegate::DoCapture() {
356 DCHECK(v4l2_task_runner_->BelongsToCurrentThread());
357 if (!is_capturing_)
358 return;
359
360 pollfd device_pfd = {};
361 device_pfd.fd = device_fd_.get();
362 device_pfd.events = POLLIN;
363 const int result = HANDLE_EINTR(poll(&device_pfd, 1, kCaptureTimeoutMs));
364 if (result < 0) {
365 SetErrorState(FROM_HERE, "Poll failed");
366 return;
367 }
368 // Check if poll() timed out; track the amount of times it did in a row and
369 // throw an error if it times out too many times.
370 if (result == 0) {
371 timeout_count_++;
372 if (timeout_count_ >= kContinuousTimeoutLimit) {
373 SetErrorState(FROM_HERE,
374 "Multiple continuous timeouts while read-polling.");
375 timeout_count_ = 0;
376 return;
377 }
378 } else {
379 timeout_count_ = 0;
380 }
381
382 // Deenqueue, send and reenqueue a buffer if the driver has filled one in.
383 if (device_pfd.revents & POLLIN) {
384 v4l2_buffer buffer;
385 FillV4L2Buffer(&buffer, 0);
386
387 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_DQBUF, &buffer)) < 0) {
388 SetErrorState(FROM_HERE, "Failed to dequeue capture buffer");
389 return;
390 }
391
392 buffer_tracker_pool_[buffer.index]->set_payload_size(buffer.bytesused);
393 const scoped_refptr<BufferTracker>& buffer_tracker =
394 buffer_tracker_pool_[buffer.index];
395
396 base::TimeDelta timestamp =
397 base::TimeDelta::FromSeconds(buffer.timestamp.tv_sec) +
398 base::TimeDelta::FromMicroseconds(buffer.timestamp.tv_usec);
399 client_->OnIncomingCapturedData(
400 buffer_tracker->start(), buffer_tracker->payload_size(),
401 capture_format_, rotation_, base::TimeTicks::Now(), timestamp);
402
403 if (HANDLE_EINTR(ioctl(device_fd_.get(), VIDIOC_QBUF, &buffer)) < 0) {
404 SetErrorState(FROM_HERE, "Failed to enqueue capture buffer");
405 return;
406 }
407 }
408
409 v4l2_task_runner_->PostTask(
410 FROM_HERE, base::Bind(&V4L2CaptureDelegate::DoCapture, this));
411 }
412
413 void V4L2CaptureDelegate::SetErrorState(
414 const tracked_objects::Location& from_here,
415 const std::string& reason) {
416 DCHECK(v4l2_task_runner_->BelongsToCurrentThread());
417 is_capturing_ = false;
418 client_->OnError(from_here, reason);
419 }
420
421 V4L2CaptureDelegate::BufferTracker::BufferTracker() {}
422
423 V4L2CaptureDelegate::BufferTracker::~BufferTracker() {
424 if (start_ == nullptr)
425 return;
426 const int result = munmap(start_, length_);
427 PLOG_IF(ERROR, result < 0) << "Error munmap()ing V4L2 buffer";
428 }
429
430 bool V4L2CaptureDelegate::BufferTracker::Init(int fd,
431 const v4l2_buffer& buffer) {
432 // Some devices require mmap() to be called with both READ and WRITE.
433 // See http://crbug.com/178582.
434 void* const start = mmap(NULL, buffer.length, PROT_READ | PROT_WRITE,
435 MAP_SHARED, fd, buffer.m.offset);
436 if (start == MAP_FAILED) {
437 DLOG(ERROR) << "Error mmap()ing a V4L2 buffer into userspace";
438 return false;
439 }
440 start_ = static_cast<uint8_t*>(start);
441 length_ = buffer.length;
442 payload_size_ = 0;
443 return true;
444 }
445
446 } // namespace media
OLDNEW
« no previous file with comments | « media/capture/video/linux/v4l2_capture_delegate.h ('k') | media/capture/video/linux/video_capture_device_chromeos.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698