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.
6 #include <linux/videodev2.h>
8 #include <sys/eventfd.h>
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/callback.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/numerics/safe_conversions.h"
17 #include "content/common/gpu/media/v4l2_image_processor.h"
18 #include "media/base/bind_to_current_loop.h"
20 #define NOTIFY_ERROR() \
22 LOG(ERROR) << "calling NotifyError()"; \
26 #define IOCTL_OR_ERROR_RETURN_VALUE(type, arg, value) \
28 if (device_->Ioctl(type, arg) != 0) { \
29 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \
34 #define IOCTL_OR_ERROR_RETURN(type, arg) \
35 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, ((void)0))
37 #define IOCTL_OR_ERROR_RETURN_FALSE(type, arg) \
38 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, false)
40 #define IOCTL_OR_LOG_ERROR(type, arg) \
42 if (device_->Ioctl(type, arg) != 0) \
43 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \
48 V4L2ImageProcessor::InputRecord::InputRecord() : at_device(false) {
51 V4L2ImageProcessor::InputRecord::~InputRecord() {
54 V4L2ImageProcessor::OutputRecord::OutputRecord()
55 : at_device(false), at_client(false) {
58 V4L2ImageProcessor::OutputRecord::~OutputRecord() {
61 V4L2ImageProcessor::JobRecord::JobRecord() {
64 V4L2ImageProcessor::JobRecord::~JobRecord() {
67 V4L2ImageProcessor::V4L2ImageProcessor(const scoped_refptr
<V4L2Device
>& device
)
68 : input_format_(media::VideoFrame::UNKNOWN
),
69 output_format_(media::VideoFrame::UNKNOWN
),
70 input_format_fourcc_(0),
71 output_format_fourcc_(0),
72 input_planes_count_(0),
73 output_planes_count_(0),
74 child_message_loop_proxy_(base::MessageLoopProxy::current()),
76 device_thread_("V4L2ImageProcessorThread"),
77 device_poll_thread_("V4L2ImageProcessorDevicePollThread"),
78 input_streamon_(false),
79 input_buffer_queued_count_(0),
80 output_streamon_(false),
81 output_buffer_queued_count_(0),
82 device_weak_factory_(this) {
85 V4L2ImageProcessor::~V4L2ImageProcessor() {
86 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
87 DCHECK(!device_thread_
.IsRunning());
88 DCHECK(!device_poll_thread_
.IsRunning());
90 DestroyInputBuffers();
91 DestroyOutputBuffers();
94 void V4L2ImageProcessor::NotifyError() {
95 if (!child_message_loop_proxy_
->BelongsToCurrentThread())
96 child_message_loop_proxy_
->PostTask(FROM_HERE
, error_cb_
);
101 bool V4L2ImageProcessor::Initialize(media::VideoFrame::Format input_format
,
102 media::VideoFrame::Format output_format
,
103 gfx::Size input_visible_size
,
104 gfx::Size output_visible_size
,
105 gfx::Size output_allocated_size
,
106 const base::Closure
& error_cb
) {
107 DCHECK(!error_cb
.is_null());
108 error_cb_
= error_cb
;
110 // TODO(posciak): Replace Exynos-specific format/parameter hardcoding in this
111 // class with proper capability enumeration.
112 DCHECK_EQ(input_format
, media::VideoFrame::I420
);
113 DCHECK_EQ(output_format
, media::VideoFrame::NV12
);
115 input_format_
= input_format
;
116 output_format_
= output_format
;
117 input_format_fourcc_
= V4L2Device::VideoFrameFormatToV4L2PixFmt(input_format
);
118 output_format_fourcc_
=
119 V4L2Device::VideoFrameFormatToV4L2PixFmt(output_format
);
121 if (!input_format_fourcc_
|| !output_format_fourcc_
) {
122 LOG(ERROR
) << "Unrecognized format(s)";
126 input_visible_size_
= input_visible_size
;
127 output_visible_size_
= output_visible_size
;
128 output_allocated_size_
= output_allocated_size
;
130 input_planes_count_
= media::VideoFrame::NumPlanes(input_format
);
131 DCHECK_LE(input_planes_count_
, static_cast<size_t>(VIDEO_MAX_PLANES
));
132 output_planes_count_
= media::VideoFrame::NumPlanes(output_format
);
133 DCHECK_LE(output_planes_count_
, static_cast<size_t>(VIDEO_MAX_PLANES
));
135 // Capabilities check.
136 struct v4l2_capability caps
;
137 memset(&caps
, 0, sizeof(caps
));
138 const __u32 kCapsRequired
= V4L2_CAP_VIDEO_CAPTURE_MPLANE
|
139 V4L2_CAP_VIDEO_OUTPUT_MPLANE
| V4L2_CAP_STREAMING
;
140 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYCAP
, &caps
);
141 if ((caps
.capabilities
& kCapsRequired
) != kCapsRequired
) {
142 LOG(ERROR
) << "Initialize(): ioctl() failed: VIDIOC_QUERYCAP: "
143 "caps check failed: 0x" << std::hex
<< caps
.capabilities
;
147 if (!CreateInputBuffers() || !CreateOutputBuffers())
150 if (!device_thread_
.Start()) {
151 LOG(ERROR
) << "Initialize(): encoder thread failed to start";
155 // StartDevicePoll will NOTIFY_ERROR on failure, so IgnoreResult is fine here.
156 device_thread_
.message_loop()->PostTask(
158 base::Bind(base::IgnoreResult(&V4L2ImageProcessor::StartDevicePoll
),
159 base::Unretained(this)));
161 DVLOG(1) << "V4L2ImageProcessor initialized for "
163 << media::VideoFrame::FormatToString(input_format
)
164 << ", output_format:"
165 << media::VideoFrame::FormatToString(output_format
)
166 << ", input_visible_size: " << input_visible_size
.ToString()
167 << ", input_allocated_size: " << input_allocated_size_
.ToString()
168 << ", output_visible_size: " << output_visible_size
.ToString()
169 << ", output_allocated_size: " << output_allocated_size
.ToString();
174 void V4L2ImageProcessor::Process(const scoped_refptr
<media::VideoFrame
>& frame
,
175 const FrameReadyCB
& cb
) {
176 DVLOG(3) << __func__
<< ": ts=" << frame
->timestamp().InMilliseconds();
178 scoped_ptr
<JobRecord
> job_record(new JobRecord());
179 job_record
->frame
= frame
;
180 job_record
->ready_cb
= cb
;
182 device_thread_
.message_loop()->PostTask(
184 base::Bind(&V4L2ImageProcessor::ProcessTask
,
185 base::Unretained(this),
186 base::Passed(&job_record
)));
189 void V4L2ImageProcessor::ProcessTask(scoped_ptr
<JobRecord
> job_record
) {
190 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
192 input_queue_
.push(make_linked_ptr(job_record
.release()));
196 void V4L2ImageProcessor::Destroy() {
197 DVLOG(3) << __func__
;
198 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
200 // If the device thread is running, destroy using posted task.
201 if (device_thread_
.IsRunning()) {
202 device_thread_
.message_loop()->PostTask(
204 base::Bind(&V4L2ImageProcessor::DestroyTask
, base::Unretained(this)));
205 // Wait for tasks to finish/early-exit.
206 device_thread_
.Stop();
208 // Otherwise DestroyTask() is not needed.
209 DCHECK(!device_poll_thread_
.IsRunning());
210 DCHECK(!device_weak_factory_
.HasWeakPtrs());
216 void V4L2ImageProcessor::DestroyTask() {
217 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
219 device_weak_factory_
.InvalidateWeakPtrs();
221 // Stop streaming and the device_poll_thread_.
225 bool V4L2ImageProcessor::CreateInputBuffers() {
226 DVLOG(3) << __func__
;
227 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
228 DCHECK(!input_streamon_
);
230 struct v4l2_control control
;
231 memset(&control
, 0, sizeof(control
));
232 control
.id
= V4L2_CID_ROTATE
;
234 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CTRL
, &control
);
236 memset(&control
, 0, sizeof(control
));
237 control
.id
= V4L2_CID_HFLIP
;
239 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CTRL
, &control
);
241 memset(&control
, 0, sizeof(control
));
242 control
.id
= V4L2_CID_VFLIP
;
244 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CTRL
, &control
);
246 memset(&control
, 0, sizeof(control
));
247 control
.id
= V4L2_CID_ALPHA_COMPONENT
;
249 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CTRL
, &control
);
251 struct v4l2_format format
;
252 memset(&format
, 0, sizeof(format
));
253 format
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
254 format
.fmt
.pix_mp
.width
= input_visible_size_
.width();
255 format
.fmt
.pix_mp
.height
= input_visible_size_
.height();
256 format
.fmt
.pix_mp
.pixelformat
= input_format_fourcc_
;
257 format
.fmt
.pix_mp
.num_planes
= input_planes_count_
;
258 for (size_t i
= 0; i
< input_planes_count_
; ++i
) {
259 format
.fmt
.pix_mp
.plane_fmt
[i
].sizeimage
=
260 media::VideoFrame::PlaneAllocationSize(
261 input_format_
, i
, input_allocated_size_
);
262 format
.fmt
.pix_mp
.plane_fmt
[i
].bytesperline
=
263 base::checked_cast
<__u32
>(input_allocated_size_
.width());
265 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT
, &format
);
267 input_allocated_size_
= V4L2Device::CodedSizeFromV4L2Format(format
);
268 DCHECK(gfx::Rect(input_allocated_size_
).Contains(
269 gfx::Rect(input_visible_size_
)));
271 struct v4l2_crop crop
;
272 memset(&crop
, 0, sizeof(crop
));
273 crop
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
276 crop
.c
.width
= base::checked_cast
<__u32
>(input_visible_size_
.width());
277 crop
.c
.height
= base::checked_cast
<__u32
>(input_visible_size_
.height());
278 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CROP
, &crop
);
280 struct v4l2_requestbuffers reqbufs
;
281 memset(&reqbufs
, 0, sizeof(reqbufs
));
282 reqbufs
.count
= kInputBufferCount
;
283 reqbufs
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
284 reqbufs
.memory
= V4L2_MEMORY_USERPTR
;
285 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS
, &reqbufs
);
287 DCHECK(input_buffer_map_
.empty());
288 input_buffer_map_
.resize(reqbufs
.count
);
290 for (size_t i
= 0; i
< input_buffer_map_
.size(); ++i
)
291 free_input_buffers_
.push_back(i
);
296 bool V4L2ImageProcessor::CreateOutputBuffers() {
297 DVLOG(3) << __func__
;
298 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
299 DCHECK(!output_streamon_
);
301 struct v4l2_format format
;
302 memset(&format
, 0, sizeof(format
));
303 format
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
304 format
.fmt
.pix_mp
.width
= output_allocated_size_
.width();
305 format
.fmt
.pix_mp
.height
= output_allocated_size_
.height();
306 format
.fmt
.pix_mp
.pixelformat
= output_format_fourcc_
;
307 format
.fmt
.pix_mp
.num_planes
= output_planes_count_
;
308 for (size_t i
= 0; i
< output_planes_count_
; ++i
) {
309 format
.fmt
.pix_mp
.plane_fmt
[i
].sizeimage
=
310 media::VideoFrame::PlaneAllocationSize(
311 output_format_
, i
, output_allocated_size_
);
312 format
.fmt
.pix_mp
.plane_fmt
[i
].bytesperline
=
313 base::checked_cast
<__u32
>(output_allocated_size_
.width());
315 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT
, &format
);
317 gfx::Size adjusted_allocated_size
=
318 V4L2Device::CodedSizeFromV4L2Format(format
);
319 DCHECK(gfx::Rect(adjusted_allocated_size
).Contains(
320 gfx::Rect(output_allocated_size_
)));
321 output_allocated_size_
= adjusted_allocated_size
;
323 struct v4l2_crop crop
;
324 memset(&crop
, 0, sizeof(crop
));
325 crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
328 crop
.c
.width
= base::checked_cast
<__u32
>(output_visible_size_
.width());
329 crop
.c
.height
= base::checked_cast
<__u32
>(output_visible_size_
.height());
330 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CROP
, &crop
);
332 struct v4l2_requestbuffers reqbufs
;
333 memset(&reqbufs
, 0, sizeof(reqbufs
));
334 reqbufs
.count
= kOutputBufferCount
;
335 reqbufs
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
336 reqbufs
.memory
= V4L2_MEMORY_MMAP
;
337 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS
, &reqbufs
);
339 DCHECK(output_buffer_map_
.empty());
340 output_buffer_map_
.resize(reqbufs
.count
);
341 for (size_t i
= 0; i
< output_buffer_map_
.size(); ++i
) {
342 OutputRecord
& output_record
= output_buffer_map_
[i
];
343 output_record
.fds
.resize(output_planes_count_
);
344 for (size_t j
= 0; j
< output_planes_count_
; ++j
) {
345 struct v4l2_exportbuffer expbuf
;
346 memset(&expbuf
, 0, sizeof(expbuf
));
347 expbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
350 expbuf
.flags
= O_CLOEXEC
;
351 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_EXPBUF
, &expbuf
);
352 output_record
.fds
[j
] = expbuf
.fd
;
354 free_output_buffers_
.push_back(i
);
360 void V4L2ImageProcessor::DestroyInputBuffers() {
361 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
362 DCHECK(!input_streamon_
);
364 struct v4l2_requestbuffers reqbufs
;
365 memset(&reqbufs
, 0, sizeof(reqbufs
));
367 reqbufs
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
368 reqbufs
.memory
= V4L2_MEMORY_USERPTR
;
369 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS
, &reqbufs
);
371 input_buffer_map_
.clear();
372 free_input_buffers_
.clear();
375 void V4L2ImageProcessor::DestroyOutputBuffers() {
376 DCHECK(child_message_loop_proxy_
->BelongsToCurrentThread());
377 DCHECK(!output_streamon_
);
379 for (size_t buf
= 0; buf
< output_buffer_map_
.size(); ++buf
) {
380 OutputRecord
& output_record
= output_buffer_map_
[buf
];
381 for (size_t plane
= 0; plane
< output_record
.fds
.size(); ++plane
)
382 close(output_record
.fds
[plane
]);
383 output_record
.fds
.clear();
386 struct v4l2_requestbuffers reqbufs
;
387 memset(&reqbufs
, 0, sizeof(reqbufs
));
389 reqbufs
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
390 reqbufs
.memory
= V4L2_MEMORY_MMAP
;
391 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS
, &reqbufs
);
393 output_buffer_map_
.clear();
394 free_output_buffers_
.clear();
397 void V4L2ImageProcessor::DevicePollTask(bool poll_device
) {
398 DCHECK_EQ(device_poll_thread_
.message_loop(), base::MessageLoop::current());
401 if (!device_
->Poll(poll_device
, &event_pending
)) {
406 // All processing should happen on ServiceDeviceTask(), since we shouldn't
407 // touch encoder state from this thread.
408 device_thread_
.message_loop()->PostTask(
410 base::Bind(&V4L2ImageProcessor::ServiceDeviceTask
,
411 base::Unretained(this)));
414 void V4L2ImageProcessor::ServiceDeviceTask() {
415 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
416 // ServiceDeviceTask() should only ever be scheduled from DevicePollTask(),
418 // * device_poll_thread_ is running normally
419 // * device_poll_thread_ scheduled us, but then a DestroyTask() shut it down,
420 // in which case we should early-out.
421 if (!device_poll_thread_
.message_loop())
427 if (!device_
->ClearDevicePollInterrupt())
431 (input_buffer_queued_count_
> 0 && output_buffer_queued_count_
> 0);
433 device_poll_thread_
.message_loop()->PostTask(
435 base::Bind(&V4L2ImageProcessor::DevicePollTask
,
436 base::Unretained(this),
439 DVLOG(2) << __func__
<< ": buffer counts: INPUT["
440 << input_queue_
.size() << "] => DEVICE["
441 << free_input_buffers_
.size() << "+"
442 << input_buffer_queued_count_
<< "/"
443 << input_buffer_map_
.size() << "->"
444 << free_output_buffers_
.size() << "+"
445 << output_buffer_queued_count_
<< "/"
446 << output_buffer_map_
.size() << "] => CLIENT["
447 << output_buffer_map_
.size() - output_buffer_queued_count_
-
448 free_output_buffers_
.size() << "]";
451 void V4L2ImageProcessor::Enqueue() {
452 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
454 const int old_inputs_queued
= input_buffer_queued_count_
;
455 while (!input_queue_
.empty() && !free_input_buffers_
.empty()) {
456 if (!EnqueueInputRecord())
459 if (old_inputs_queued
== 0 && input_buffer_queued_count_
!= 0) {
460 // We started up a previously empty queue.
461 // Queue state changed; signal interrupt.
462 if (!device_
->SetDevicePollInterrupt())
464 // VIDIOC_STREAMON if we haven't yet.
465 if (!input_streamon_
) {
466 __u32 type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
467 IOCTL_OR_ERROR_RETURN(VIDIOC_STREAMON
, &type
);
468 input_streamon_
= true;
472 // TODO(posciak): Fix this to be non-Exynos specific.
473 // Exynos GSC is liable to race conditions if more than one output buffer is
474 // simultaneously enqueued, so enqueue just one.
475 if (output_buffer_queued_count_
== 0 && !free_output_buffers_
.empty()) {
476 const int old_outputs_queued
= output_buffer_queued_count_
;
477 if (!EnqueueOutputRecord())
479 if (old_outputs_queued
== 0 && output_buffer_queued_count_
!= 0) {
480 // We just started up a previously empty queue.
481 // Queue state changed; signal interrupt.
482 if (!device_
->SetDevicePollInterrupt())
484 // Start VIDIOC_STREAMON if we haven't yet.
485 if (!output_streamon_
) {
486 __u32 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
487 IOCTL_OR_ERROR_RETURN(VIDIOC_STREAMON
, &type
);
488 output_streamon_
= true;
492 DCHECK_LE(output_buffer_queued_count_
, 1);
495 void V4L2ImageProcessor::Dequeue() {
496 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
498 // Dequeue completed input (VIDEO_OUTPUT) buffers,
499 // and recycle to the free list.
500 struct v4l2_buffer dqbuf
;
501 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
502 while (input_buffer_queued_count_
> 0) {
503 DCHECK(input_streamon_
);
504 memset(&dqbuf
, 0, sizeof(dqbuf
));
505 memset(&planes
, 0, sizeof(planes
));
506 dqbuf
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
507 dqbuf
.memory
= V4L2_MEMORY_USERPTR
;
508 dqbuf
.m
.planes
= planes
;
509 dqbuf
.length
= input_planes_count_
;
510 if (device_
->Ioctl(VIDIOC_DQBUF
, &dqbuf
) != 0) {
511 if (errno
== EAGAIN
) {
512 // EAGAIN if we're just out of buffers to dequeue.
515 PLOG(ERROR
) << "ioctl() failed: VIDIOC_DQBUF";
519 InputRecord
& input_record
= input_buffer_map_
[dqbuf
.index
];
520 DCHECK(input_record
.at_device
);
521 input_record
.at_device
= false;
522 input_record
.frame
= NULL
;
523 free_input_buffers_
.push_back(dqbuf
.index
);
524 input_buffer_queued_count_
--;
527 // Dequeue completed output (VIDEO_CAPTURE) buffers, recycle to the free list.
528 // Return the finished buffer to the client via the job ready callback.
529 while (output_buffer_queued_count_
> 0) {
530 DCHECK(output_streamon_
);
531 memset(&dqbuf
, 0, sizeof(dqbuf
));
532 memset(&planes
, 0, sizeof(planes
));
533 dqbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
534 dqbuf
.memory
= V4L2_MEMORY_MMAP
;
535 dqbuf
.m
.planes
= planes
;
536 dqbuf
.length
= output_planes_count_
;
537 if (device_
->Ioctl(VIDIOC_DQBUF
, &dqbuf
) != 0) {
538 if (errno
== EAGAIN
) {
539 // EAGAIN if we're just out of buffers to dequeue.
542 PLOG(ERROR
) << "ioctl() failed: VIDIOC_DQBUF";
546 OutputRecord
& output_record
= output_buffer_map_
[dqbuf
.index
];
547 DCHECK(output_record
.at_device
);
548 output_record
.at_device
= false;
549 output_record
.at_client
= true;
550 output_buffer_queued_count_
--;
552 // Jobs are always processed in FIFO order.
553 DCHECK(!running_jobs_
.empty());
554 linked_ptr
<JobRecord
> job_record
= running_jobs_
.front();
557 scoped_refptr
<media::VideoFrame
> output_frame
=
558 media::VideoFrame::WrapExternalDmabufs(
560 output_allocated_size_
,
561 gfx::Rect(output_visible_size_
),
562 output_visible_size_
,
564 job_record
->frame
->timestamp(),
565 media::BindToCurrentLoop(
566 base::Bind(&V4L2ImageProcessor::ReuseOutputBuffer
,
567 device_weak_factory_
.GetWeakPtr(),
570 DVLOG(3) << "Processing finished, returning frame, ts="
571 << output_frame
->timestamp().InMilliseconds();
573 child_message_loop_proxy_
->PostTask(
574 FROM_HERE
, base::Bind(job_record
->ready_cb
, output_frame
));
578 void V4L2ImageProcessor::ReuseOutputBuffer(int index
) {
579 DVLOG(3) << "Reusing output buffer, index=" << index
;
580 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
582 OutputRecord
& output_record
= output_buffer_map_
[index
];
583 DCHECK(output_record
.at_client
);
584 DCHECK(!output_record
.at_device
);
585 output_record
.at_client
= false;
586 free_output_buffers_
.push_back(index
);
591 bool V4L2ImageProcessor::EnqueueInputRecord() {
592 DCHECK(!input_queue_
.empty());
593 DCHECK(!free_input_buffers_
.empty());
595 // Enqueue an input (VIDEO_OUTPUT) buffer for an input video frame.
596 linked_ptr
<JobRecord
> job_record
= input_queue_
.front();
598 const int index
= free_input_buffers_
.back();
599 InputRecord
& input_record
= input_buffer_map_
[index
];
600 DCHECK(!input_record
.at_device
);
601 input_record
.frame
= job_record
->frame
;
602 struct v4l2_buffer qbuf
;
603 struct v4l2_plane qbuf_planes
[VIDEO_MAX_PLANES
];
604 memset(&qbuf
, 0, sizeof(qbuf
));
605 memset(qbuf_planes
, 0, sizeof(qbuf_planes
));
607 qbuf
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
608 qbuf
.memory
= V4L2_MEMORY_USERPTR
;
609 qbuf
.m
.planes
= qbuf_planes
;
610 qbuf
.length
= input_planes_count_
;
611 for (size_t i
= 0; i
< input_planes_count_
; ++i
) {
612 qbuf
.m
.planes
[i
].bytesused
= media::VideoFrame::PlaneAllocationSize(
613 input_record
.frame
->format(), i
, input_allocated_size_
);
614 qbuf
.m
.planes
[i
].length
= qbuf
.m
.planes
[i
].bytesused
;
615 qbuf
.m
.planes
[i
].m
.userptr
=
616 reinterpret_cast<unsigned long>(input_record
.frame
->data(i
));
618 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF
, &qbuf
);
619 input_record
.at_device
= true;
620 running_jobs_
.push(job_record
);
621 free_input_buffers_
.pop_back();
622 input_buffer_queued_count_
++;
624 DVLOG(3) << __func__
<< ": enqueued frame ts="
625 << job_record
->frame
->timestamp().InMilliseconds() << " to device.";
630 bool V4L2ImageProcessor::EnqueueOutputRecord() {
631 DCHECK(!free_output_buffers_
.empty());
633 // Enqueue an output (VIDEO_CAPTURE) buffer.
634 const int index
= free_output_buffers_
.back();
635 OutputRecord
& output_record
= output_buffer_map_
[index
];
636 DCHECK(!output_record
.at_device
);
637 struct v4l2_buffer qbuf
;
638 struct v4l2_plane qbuf_planes
[VIDEO_MAX_PLANES
];
639 memset(&qbuf
, 0, sizeof(qbuf
));
640 memset(qbuf_planes
, 0, sizeof(qbuf_planes
));
642 qbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
643 qbuf
.memory
= V4L2_MEMORY_MMAP
;
644 qbuf
.m
.planes
= qbuf_planes
;
645 qbuf
.length
= output_planes_count_
;
646 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF
, &qbuf
);
647 output_record
.at_device
= true;
648 free_output_buffers_
.pop_back();
649 output_buffer_queued_count_
++;
653 bool V4L2ImageProcessor::StartDevicePoll() {
654 DVLOG(3) << __func__
<< ": starting device poll";
655 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
656 DCHECK(!device_poll_thread_
.IsRunning());
658 // Start up the device poll thread and schedule its first DevicePollTask().
659 if (!device_poll_thread_
.Start()) {
660 LOG(ERROR
) << "StartDevicePoll(): Device thread failed to start";
664 // Enqueue a poll task with no devices to poll on - will wait only for the
666 device_poll_thread_
.message_loop()->PostTask(
669 &V4L2ImageProcessor::DevicePollTask
, base::Unretained(this), false));
674 bool V4L2ImageProcessor::StopDevicePoll() {
675 DVLOG(3) << __func__
<< ": stopping device poll";
676 if (device_thread_
.IsRunning())
677 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
679 // Signal the DevicePollTask() to stop, and stop the device poll thread.
680 if (!device_
->SetDevicePollInterrupt())
682 device_poll_thread_
.Stop();
684 // Clear the interrupt now, to be sure.
685 if (!device_
->ClearDevicePollInterrupt())
688 if (input_streamon_
) {
689 __u32 type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
690 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF
, &type
);
692 input_streamon_
= false;
694 if (output_streamon_
) {
695 __u32 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
696 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF
, &type
);
698 output_streamon_
= false;
700 // Reset all our accounting info.
701 while (!input_queue_
.empty())
704 while (!running_jobs_
.empty())
707 free_input_buffers_
.clear();
708 for (size_t i
= 0; i
< input_buffer_map_
.size(); ++i
) {
709 InputRecord
& input_record
= input_buffer_map_
[i
];
710 input_record
.at_device
= false;
711 input_record
.frame
= NULL
;
712 free_input_buffers_
.push_back(i
);
714 input_buffer_queued_count_
= 0;
716 free_output_buffers_
.clear();
717 for (size_t i
= 0; i
< output_buffer_map_
.size(); ++i
) {
718 OutputRecord
& output_record
= output_buffer_map_
[i
];
719 output_record
.at_device
= false;
720 if (!output_record
.at_client
)
721 free_output_buffers_
.push_back(i
);
723 output_buffer_queued_count_
= 0;
728 } // namespace content