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/numerics/safe_conversions.h"
16 #include "content/common/gpu/media/v4l2_image_processor.h"
17 #include "media/base/bind_to_current_loop.h"
19 #define NOTIFY_ERROR() \
21 LOG(ERROR) << "calling NotifyError()"; \
25 #define IOCTL_OR_ERROR_RETURN_VALUE(type, arg, value, type_str) \
27 if (device_->Ioctl(type, arg) != 0) { \
28 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << type_str; \
33 #define IOCTL_OR_ERROR_RETURN(type, arg) \
34 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, ((void)0), #type)
36 #define IOCTL_OR_ERROR_RETURN_FALSE(type, arg) \
37 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, false, #type)
39 #define IOCTL_OR_LOG_ERROR(type, arg) \
41 if (device_->Ioctl(type, arg) != 0) \
42 PLOG(ERROR) << __func__ << "(): ioctl() failed: " << #type; \
47 V4L2ImageProcessor::InputRecord::InputRecord() : at_device(false) {
50 V4L2ImageProcessor::InputRecord::~InputRecord() {
53 V4L2ImageProcessor::OutputRecord::OutputRecord()
54 : at_device(false), at_client(false) {
57 V4L2ImageProcessor::OutputRecord::~OutputRecord() {
60 V4L2ImageProcessor::JobRecord::JobRecord() {
63 V4L2ImageProcessor::JobRecord::~JobRecord() {
66 V4L2ImageProcessor::V4L2ImageProcessor(const scoped_refptr
<V4L2Device
>& device
)
67 : input_format_(media::PIXEL_FORMAT_UNKNOWN
),
68 output_format_(media::PIXEL_FORMAT_UNKNOWN
),
69 input_format_fourcc_(0),
70 output_format_fourcc_(0),
71 input_planes_count_(0),
72 output_planes_count_(0),
73 child_task_runner_(base::ThreadTaskRunnerHandle::Get()),
75 device_thread_("V4L2ImageProcessorThread"),
76 device_poll_thread_("V4L2ImageProcessorDevicePollThread"),
77 input_streamon_(false),
78 input_buffer_queued_count_(0),
79 output_streamon_(false),
80 output_buffer_queued_count_(0),
81 device_weak_factory_(this) {
84 V4L2ImageProcessor::~V4L2ImageProcessor() {
85 DCHECK(child_task_runner_
->BelongsToCurrentThread());
86 DCHECK(!device_thread_
.IsRunning());
87 DCHECK(!device_poll_thread_
.IsRunning());
89 DestroyInputBuffers();
90 DestroyOutputBuffers();
93 void V4L2ImageProcessor::NotifyError() {
94 if (!child_task_runner_
->BelongsToCurrentThread())
95 child_task_runner_
->PostTask(FROM_HERE
, error_cb_
);
100 bool V4L2ImageProcessor::Initialize(media::VideoPixelFormat input_format
,
101 media::VideoPixelFormat output_format
,
102 gfx::Size input_visible_size
,
103 gfx::Size output_visible_size
,
104 gfx::Size output_allocated_size
,
105 const base::Closure
& error_cb
) {
106 DCHECK(!error_cb
.is_null());
107 error_cb_
= error_cb
;
109 // TODO(posciak): Replace Exynos-specific format/parameter hardcoding in this
110 // class with proper capability enumeration.
111 DCHECK_EQ(input_format
, media::PIXEL_FORMAT_I420
);
112 DCHECK_EQ(output_format
, media::PIXEL_FORMAT_NV12
);
114 input_format_
= input_format
;
115 output_format_
= output_format
;
116 input_format_fourcc_
= V4L2Device::VideoPixelFormatToV4L2PixFmt(input_format
);
117 output_format_fourcc_
=
118 V4L2Device::VideoPixelFormatToV4L2PixFmt(output_format
);
120 if (!input_format_fourcc_
|| !output_format_fourcc_
) {
121 LOG(ERROR
) << "Unrecognized format(s)";
125 input_visible_size_
= input_visible_size
;
126 output_visible_size_
= output_visible_size
;
127 output_allocated_size_
= output_allocated_size
;
129 input_planes_count_
= media::VideoFrame::NumPlanes(input_format
);
130 DCHECK_LE(input_planes_count_
, static_cast<size_t>(VIDEO_MAX_PLANES
));
131 output_planes_count_
= media::VideoFrame::NumPlanes(output_format
);
132 DCHECK_LE(output_planes_count_
, static_cast<size_t>(VIDEO_MAX_PLANES
));
134 // Capabilities check.
135 struct v4l2_capability caps
;
136 memset(&caps
, 0, sizeof(caps
));
137 const __u32 kCapsRequired
= V4L2_CAP_VIDEO_CAPTURE_MPLANE
|
138 V4L2_CAP_VIDEO_OUTPUT_MPLANE
| V4L2_CAP_STREAMING
;
139 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYCAP
, &caps
);
140 if ((caps
.capabilities
& kCapsRequired
) != kCapsRequired
) {
141 LOG(ERROR
) << "Initialize(): ioctl() failed: VIDIOC_QUERYCAP: "
142 "caps check failed: 0x" << std::hex
<< caps
.capabilities
;
146 if (!CreateInputBuffers() || !CreateOutputBuffers())
149 if (!device_thread_
.Start()) {
150 LOG(ERROR
) << "Initialize(): encoder thread failed to start";
154 // StartDevicePoll will NOTIFY_ERROR on failure, so IgnoreResult is fine here.
155 device_thread_
.message_loop()->PostTask(
157 base::Bind(base::IgnoreResult(&V4L2ImageProcessor::StartDevicePoll
),
158 base::Unretained(this)));
160 DVLOG(1) << "V4L2ImageProcessor initialized for "
161 << " input_format:" << media::VideoPixelFormatToString(input_format
)
162 << ", output_format:"
163 << media::VideoPixelFormatToString(output_format
)
164 << ", input_visible_size: " << input_visible_size
.ToString()
165 << ", input_allocated_size: " << input_allocated_size_
.ToString()
166 << ", output_visible_size: " << output_visible_size
.ToString()
167 << ", output_allocated_size: " << output_allocated_size
.ToString();
172 void V4L2ImageProcessor::Process(const scoped_refptr
<media::VideoFrame
>& frame
,
173 const FrameReadyCB
& cb
) {
174 DVLOG(3) << __func__
<< ": ts=" << frame
->timestamp().InMilliseconds();
176 scoped_ptr
<JobRecord
> job_record(new JobRecord());
177 job_record
->frame
= frame
;
178 job_record
->ready_cb
= cb
;
180 device_thread_
.message_loop()->PostTask(
182 base::Bind(&V4L2ImageProcessor::ProcessTask
,
183 base::Unretained(this),
184 base::Passed(&job_record
)));
187 void V4L2ImageProcessor::ProcessTask(scoped_ptr
<JobRecord
> job_record
) {
188 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
190 input_queue_
.push(make_linked_ptr(job_record
.release()));
194 void V4L2ImageProcessor::Destroy() {
195 DVLOG(3) << __func__
;
196 DCHECK(child_task_runner_
->BelongsToCurrentThread());
198 // If the device thread is running, destroy using posted task.
199 if (device_thread_
.IsRunning()) {
200 device_thread_
.message_loop()->PostTask(
202 base::Bind(&V4L2ImageProcessor::DestroyTask
, base::Unretained(this)));
203 // Wait for tasks to finish/early-exit.
204 device_thread_
.Stop();
206 // Otherwise DestroyTask() is not needed.
207 DCHECK(!device_poll_thread_
.IsRunning());
208 DCHECK(!device_weak_factory_
.HasWeakPtrs());
214 void V4L2ImageProcessor::DestroyTask() {
215 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
217 device_weak_factory_
.InvalidateWeakPtrs();
219 // Stop streaming and the device_poll_thread_.
223 bool V4L2ImageProcessor::CreateInputBuffers() {
224 DVLOG(3) << __func__
;
225 DCHECK(child_task_runner_
->BelongsToCurrentThread());
226 DCHECK(!input_streamon_
);
228 struct v4l2_control control
;
229 memset(&control
, 0, sizeof(control
));
230 control
.id
= V4L2_CID_ROTATE
;
232 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CTRL
, &control
);
234 memset(&control
, 0, sizeof(control
));
235 control
.id
= V4L2_CID_HFLIP
;
237 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CTRL
, &control
);
239 memset(&control
, 0, sizeof(control
));
240 control
.id
= V4L2_CID_VFLIP
;
242 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CTRL
, &control
);
244 memset(&control
, 0, sizeof(control
));
245 control
.id
= V4L2_CID_ALPHA_COMPONENT
;
247 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CTRL
, &control
);
249 struct v4l2_format format
;
250 memset(&format
, 0, sizeof(format
));
251 format
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
252 format
.fmt
.pix_mp
.width
= input_visible_size_
.width();
253 format
.fmt
.pix_mp
.height
= input_visible_size_
.height();
254 format
.fmt
.pix_mp
.pixelformat
= input_format_fourcc_
;
255 format
.fmt
.pix_mp
.num_planes
= input_planes_count_
;
256 for (size_t i
= 0; i
< input_planes_count_
; ++i
) {
257 format
.fmt
.pix_mp
.plane_fmt
[i
].sizeimage
=
258 media::VideoFrame::PlaneSize(input_format_
, i
, input_allocated_size_
)
260 format
.fmt
.pix_mp
.plane_fmt
[i
].bytesperline
=
261 base::checked_cast
<__u32
>(input_allocated_size_
.width());
263 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT
, &format
);
265 input_allocated_size_
= V4L2Device::CodedSizeFromV4L2Format(format
);
266 DCHECK(gfx::Rect(input_allocated_size_
).Contains(
267 gfx::Rect(input_visible_size_
)));
269 struct v4l2_crop crop
;
270 memset(&crop
, 0, sizeof(crop
));
271 crop
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
274 crop
.c
.width
= base::checked_cast
<__u32
>(input_visible_size_
.width());
275 crop
.c
.height
= base::checked_cast
<__u32
>(input_visible_size_
.height());
276 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CROP
, &crop
);
278 struct v4l2_requestbuffers reqbufs
;
279 memset(&reqbufs
, 0, sizeof(reqbufs
));
280 reqbufs
.count
= kInputBufferCount
;
281 reqbufs
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
282 reqbufs
.memory
= V4L2_MEMORY_USERPTR
;
283 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS
, &reqbufs
);
285 DCHECK(input_buffer_map_
.empty());
286 input_buffer_map_
.resize(reqbufs
.count
);
288 for (size_t i
= 0; i
< input_buffer_map_
.size(); ++i
)
289 free_input_buffers_
.push_back(i
);
294 bool V4L2ImageProcessor::CreateOutputBuffers() {
295 DVLOG(3) << __func__
;
296 DCHECK(child_task_runner_
->BelongsToCurrentThread());
297 DCHECK(!output_streamon_
);
299 struct v4l2_format format
;
300 memset(&format
, 0, sizeof(format
));
301 format
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
302 format
.fmt
.pix_mp
.width
= output_allocated_size_
.width();
303 format
.fmt
.pix_mp
.height
= output_allocated_size_
.height();
304 format
.fmt
.pix_mp
.pixelformat
= output_format_fourcc_
;
305 format
.fmt
.pix_mp
.num_planes
= output_planes_count_
;
306 for (size_t i
= 0; i
< output_planes_count_
; ++i
) {
307 format
.fmt
.pix_mp
.plane_fmt
[i
].sizeimage
=
308 media::VideoFrame::PlaneSize(output_format_
, i
, output_allocated_size_
)
310 format
.fmt
.pix_mp
.plane_fmt
[i
].bytesperline
=
311 base::checked_cast
<__u32
>(output_allocated_size_
.width());
313 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT
, &format
);
315 gfx::Size adjusted_allocated_size
=
316 V4L2Device::CodedSizeFromV4L2Format(format
);
317 DCHECK(gfx::Rect(adjusted_allocated_size
).Contains(
318 gfx::Rect(output_allocated_size_
)));
319 output_allocated_size_
= adjusted_allocated_size
;
321 struct v4l2_crop crop
;
322 memset(&crop
, 0, sizeof(crop
));
323 crop
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
326 crop
.c
.width
= base::checked_cast
<__u32
>(output_visible_size_
.width());
327 crop
.c
.height
= base::checked_cast
<__u32
>(output_visible_size_
.height());
328 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CROP
, &crop
);
330 struct v4l2_requestbuffers reqbufs
;
331 memset(&reqbufs
, 0, sizeof(reqbufs
));
332 reqbufs
.count
= kOutputBufferCount
;
333 reqbufs
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
334 reqbufs
.memory
= V4L2_MEMORY_MMAP
;
335 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS
, &reqbufs
);
337 DCHECK(output_buffer_map_
.empty());
338 output_buffer_map_
.resize(reqbufs
.count
);
339 for (size_t i
= 0; i
< output_buffer_map_
.size(); ++i
) {
340 OutputRecord
& output_record
= output_buffer_map_
[i
];
341 output_record
.fds
.resize(output_planes_count_
);
342 for (size_t j
= 0; j
< output_planes_count_
; ++j
) {
343 struct v4l2_exportbuffer expbuf
;
344 memset(&expbuf
, 0, sizeof(expbuf
));
345 expbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
348 expbuf
.flags
= O_CLOEXEC
;
349 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_EXPBUF
, &expbuf
);
350 output_record
.fds
[j
] = expbuf
.fd
;
352 free_output_buffers_
.push_back(i
);
358 void V4L2ImageProcessor::DestroyInputBuffers() {
359 DCHECK(child_task_runner_
->BelongsToCurrentThread());
360 DCHECK(!input_streamon_
);
362 struct v4l2_requestbuffers reqbufs
;
363 memset(&reqbufs
, 0, sizeof(reqbufs
));
365 reqbufs
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
366 reqbufs
.memory
= V4L2_MEMORY_USERPTR
;
367 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS
, &reqbufs
);
369 input_buffer_map_
.clear();
370 free_input_buffers_
.clear();
373 void V4L2ImageProcessor::DestroyOutputBuffers() {
374 DCHECK(child_task_runner_
->BelongsToCurrentThread());
375 DCHECK(!output_streamon_
);
377 for (size_t buf
= 0; buf
< output_buffer_map_
.size(); ++buf
) {
378 OutputRecord
& output_record
= output_buffer_map_
[buf
];
379 for (size_t plane
= 0; plane
< output_record
.fds
.size(); ++plane
)
380 close(output_record
.fds
[plane
]);
381 output_record
.fds
.clear();
384 struct v4l2_requestbuffers reqbufs
;
385 memset(&reqbufs
, 0, sizeof(reqbufs
));
387 reqbufs
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
388 reqbufs
.memory
= V4L2_MEMORY_MMAP
;
389 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS
, &reqbufs
);
391 output_buffer_map_
.clear();
392 free_output_buffers_
.clear();
395 void V4L2ImageProcessor::DevicePollTask(bool poll_device
) {
396 DCHECK_EQ(device_poll_thread_
.message_loop(), base::MessageLoop::current());
399 if (!device_
->Poll(poll_device
, &event_pending
)) {
404 // All processing should happen on ServiceDeviceTask(), since we shouldn't
405 // touch encoder state from this thread.
406 device_thread_
.message_loop()->PostTask(
408 base::Bind(&V4L2ImageProcessor::ServiceDeviceTask
,
409 base::Unretained(this)));
412 void V4L2ImageProcessor::ServiceDeviceTask() {
413 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
414 // ServiceDeviceTask() should only ever be scheduled from DevicePollTask(),
416 // * device_poll_thread_ is running normally
417 // * device_poll_thread_ scheduled us, but then a DestroyTask() shut it down,
418 // in which case we should early-out.
419 if (!device_poll_thread_
.message_loop())
425 if (!device_
->ClearDevicePollInterrupt())
429 (input_buffer_queued_count_
> 0 && output_buffer_queued_count_
> 0);
431 device_poll_thread_
.message_loop()->PostTask(
433 base::Bind(&V4L2ImageProcessor::DevicePollTask
,
434 base::Unretained(this),
437 DVLOG(2) << __func__
<< ": buffer counts: INPUT["
438 << input_queue_
.size() << "] => DEVICE["
439 << free_input_buffers_
.size() << "+"
440 << input_buffer_queued_count_
<< "/"
441 << input_buffer_map_
.size() << "->"
442 << free_output_buffers_
.size() << "+"
443 << output_buffer_queued_count_
<< "/"
444 << output_buffer_map_
.size() << "] => CLIENT["
445 << output_buffer_map_
.size() - output_buffer_queued_count_
-
446 free_output_buffers_
.size() << "]";
449 void V4L2ImageProcessor::Enqueue() {
450 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
452 const int old_inputs_queued
= input_buffer_queued_count_
;
453 while (!input_queue_
.empty() && !free_input_buffers_
.empty()) {
454 if (!EnqueueInputRecord())
457 if (old_inputs_queued
== 0 && input_buffer_queued_count_
!= 0) {
458 // We started up a previously empty queue.
459 // Queue state changed; signal interrupt.
460 if (!device_
->SetDevicePollInterrupt())
462 // VIDIOC_STREAMON if we haven't yet.
463 if (!input_streamon_
) {
464 __u32 type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
465 IOCTL_OR_ERROR_RETURN(VIDIOC_STREAMON
, &type
);
466 input_streamon_
= true;
470 // TODO(posciak): Fix this to be non-Exynos specific.
471 // Exynos GSC is liable to race conditions if more than one output buffer is
472 // simultaneously enqueued, so enqueue just one.
473 if (output_buffer_queued_count_
== 0 && !free_output_buffers_
.empty()) {
474 const int old_outputs_queued
= output_buffer_queued_count_
;
475 if (!EnqueueOutputRecord())
477 if (old_outputs_queued
== 0 && output_buffer_queued_count_
!= 0) {
478 // We just started up a previously empty queue.
479 // Queue state changed; signal interrupt.
480 if (!device_
->SetDevicePollInterrupt())
482 // Start VIDIOC_STREAMON if we haven't yet.
483 if (!output_streamon_
) {
484 __u32 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
485 IOCTL_OR_ERROR_RETURN(VIDIOC_STREAMON
, &type
);
486 output_streamon_
= true;
490 DCHECK_LE(output_buffer_queued_count_
, 1);
493 void V4L2ImageProcessor::Dequeue() {
494 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
496 // Dequeue completed input (VIDEO_OUTPUT) buffers,
497 // and recycle to the free list.
498 struct v4l2_buffer dqbuf
;
499 struct v4l2_plane planes
[VIDEO_MAX_PLANES
];
500 while (input_buffer_queued_count_
> 0) {
501 DCHECK(input_streamon_
);
502 memset(&dqbuf
, 0, sizeof(dqbuf
));
503 memset(&planes
, 0, sizeof(planes
));
504 dqbuf
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
505 dqbuf
.memory
= V4L2_MEMORY_USERPTR
;
506 dqbuf
.m
.planes
= planes
;
507 dqbuf
.length
= input_planes_count_
;
508 if (device_
->Ioctl(VIDIOC_DQBUF
, &dqbuf
) != 0) {
509 if (errno
== EAGAIN
) {
510 // EAGAIN if we're just out of buffers to dequeue.
513 PLOG(ERROR
) << "ioctl() failed: VIDIOC_DQBUF";
517 InputRecord
& input_record
= input_buffer_map_
[dqbuf
.index
];
518 DCHECK(input_record
.at_device
);
519 input_record
.at_device
= false;
520 input_record
.frame
= NULL
;
521 free_input_buffers_
.push_back(dqbuf
.index
);
522 input_buffer_queued_count_
--;
525 // Dequeue completed output (VIDEO_CAPTURE) buffers, recycle to the free list.
526 // Return the finished buffer to the client via the job ready callback.
527 while (output_buffer_queued_count_
> 0) {
528 DCHECK(output_streamon_
);
529 memset(&dqbuf
, 0, sizeof(dqbuf
));
530 memset(&planes
, 0, sizeof(planes
));
531 dqbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
532 dqbuf
.memory
= V4L2_MEMORY_MMAP
;
533 dqbuf
.m
.planes
= planes
;
534 dqbuf
.length
= output_planes_count_
;
535 if (device_
->Ioctl(VIDIOC_DQBUF
, &dqbuf
) != 0) {
536 if (errno
== EAGAIN
) {
537 // EAGAIN if we're just out of buffers to dequeue.
540 PLOG(ERROR
) << "ioctl() failed: VIDIOC_DQBUF";
544 OutputRecord
& output_record
= output_buffer_map_
[dqbuf
.index
];
545 DCHECK(output_record
.at_device
);
546 output_record
.at_device
= false;
547 output_record
.at_client
= true;
548 output_buffer_queued_count_
--;
550 // Jobs are always processed in FIFO order.
551 DCHECK(!running_jobs_
.empty());
552 linked_ptr
<JobRecord
> job_record
= running_jobs_
.front();
555 scoped_refptr
<media::VideoFrame
> output_frame
=
556 media::VideoFrame::WrapExternalDmabufs(
558 output_allocated_size_
,
559 gfx::Rect(output_visible_size_
),
560 output_visible_size_
,
562 job_record
->frame
->timestamp());
563 output_frame
->AddDestructionObserver(media::BindToCurrentLoop(
564 base::Bind(&V4L2ImageProcessor::ReuseOutputBuffer
,
565 device_weak_factory_
.GetWeakPtr(),
568 DVLOG(3) << "Processing finished, returning frame, ts="
569 << output_frame
->timestamp().InMilliseconds();
571 child_task_runner_
->PostTask(
572 FROM_HERE
, base::Bind(job_record
->ready_cb
, output_frame
));
576 void V4L2ImageProcessor::ReuseOutputBuffer(int index
) {
577 DVLOG(3) << "Reusing output buffer, index=" << index
;
578 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
580 OutputRecord
& output_record
= output_buffer_map_
[index
];
581 DCHECK(output_record
.at_client
);
582 DCHECK(!output_record
.at_device
);
583 output_record
.at_client
= false;
584 free_output_buffers_
.push_back(index
);
589 bool V4L2ImageProcessor::EnqueueInputRecord() {
590 DCHECK(!input_queue_
.empty());
591 DCHECK(!free_input_buffers_
.empty());
593 // Enqueue an input (VIDEO_OUTPUT) buffer for an input video frame.
594 linked_ptr
<JobRecord
> job_record
= input_queue_
.front();
596 const int index
= free_input_buffers_
.back();
597 InputRecord
& input_record
= input_buffer_map_
[index
];
598 DCHECK(!input_record
.at_device
);
599 input_record
.frame
= job_record
->frame
;
600 struct v4l2_buffer qbuf
;
601 struct v4l2_plane qbuf_planes
[VIDEO_MAX_PLANES
];
602 memset(&qbuf
, 0, sizeof(qbuf
));
603 memset(qbuf_planes
, 0, sizeof(qbuf_planes
));
605 qbuf
.type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
606 qbuf
.memory
= V4L2_MEMORY_USERPTR
;
607 qbuf
.m
.planes
= qbuf_planes
;
608 qbuf
.length
= input_planes_count_
;
609 for (size_t i
= 0; i
< input_planes_count_
; ++i
) {
610 qbuf
.m
.planes
[i
].bytesused
= media::VideoFrame::PlaneSize(
611 input_record
.frame
->format(), i
, input_allocated_size_
).GetArea();
612 qbuf
.m
.planes
[i
].length
= qbuf
.m
.planes
[i
].bytesused
;
613 qbuf
.m
.planes
[i
].m
.userptr
=
614 reinterpret_cast<unsigned long>(input_record
.frame
->data(i
));
616 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF
, &qbuf
);
617 input_record
.at_device
= true;
618 running_jobs_
.push(job_record
);
619 free_input_buffers_
.pop_back();
620 input_buffer_queued_count_
++;
622 DVLOG(3) << __func__
<< ": enqueued frame ts="
623 << job_record
->frame
->timestamp().InMilliseconds() << " to device.";
628 bool V4L2ImageProcessor::EnqueueOutputRecord() {
629 DCHECK(!free_output_buffers_
.empty());
631 // Enqueue an output (VIDEO_CAPTURE) buffer.
632 const int index
= free_output_buffers_
.back();
633 OutputRecord
& output_record
= output_buffer_map_
[index
];
634 DCHECK(!output_record
.at_device
);
635 struct v4l2_buffer qbuf
;
636 struct v4l2_plane qbuf_planes
[VIDEO_MAX_PLANES
];
637 memset(&qbuf
, 0, sizeof(qbuf
));
638 memset(qbuf_planes
, 0, sizeof(qbuf_planes
));
640 qbuf
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
641 qbuf
.memory
= V4L2_MEMORY_MMAP
;
642 qbuf
.m
.planes
= qbuf_planes
;
643 qbuf
.length
= output_planes_count_
;
644 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF
, &qbuf
);
645 output_record
.at_device
= true;
646 free_output_buffers_
.pop_back();
647 output_buffer_queued_count_
++;
651 bool V4L2ImageProcessor::StartDevicePoll() {
652 DVLOG(3) << __func__
<< ": starting device poll";
653 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
654 DCHECK(!device_poll_thread_
.IsRunning());
656 // Start up the device poll thread and schedule its first DevicePollTask().
657 if (!device_poll_thread_
.Start()) {
658 LOG(ERROR
) << "StartDevicePoll(): Device thread failed to start";
662 // Enqueue a poll task with no devices to poll on - will wait only for the
664 device_poll_thread_
.message_loop()->PostTask(
667 &V4L2ImageProcessor::DevicePollTask
, base::Unretained(this), false));
672 bool V4L2ImageProcessor::StopDevicePoll() {
673 DVLOG(3) << __func__
<< ": stopping device poll";
674 if (device_thread_
.IsRunning())
675 DCHECK_EQ(device_thread_
.message_loop(), base::MessageLoop::current());
677 // Signal the DevicePollTask() to stop, and stop the device poll thread.
678 if (!device_
->SetDevicePollInterrupt())
680 device_poll_thread_
.Stop();
682 // Clear the interrupt now, to be sure.
683 if (!device_
->ClearDevicePollInterrupt())
686 if (input_streamon_
) {
687 __u32 type
= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE
;
688 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF
, &type
);
690 input_streamon_
= false;
692 if (output_streamon_
) {
693 __u32 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
;
694 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF
, &type
);
696 output_streamon_
= false;
698 // Reset all our accounting info.
699 while (!input_queue_
.empty())
702 while (!running_jobs_
.empty())
705 free_input_buffers_
.clear();
706 for (size_t i
= 0; i
< input_buffer_map_
.size(); ++i
) {
707 InputRecord
& input_record
= input_buffer_map_
[i
];
708 input_record
.at_device
= false;
709 input_record
.frame
= NULL
;
710 free_input_buffers_
.push_back(i
);
712 input_buffer_queued_count_
= 0;
714 free_output_buffers_
.clear();
715 for (size_t i
= 0; i
< output_buffer_map_
.size(); ++i
) {
716 OutputRecord
& output_record
= output_buffer_map_
[i
];
717 output_record
.at_device
= false;
718 if (!output_record
.at_client
)
719 free_output_buffers_
.push_back(i
);
721 output_buffer_queued_count_
= 0;
726 } // namespace content