OLD | NEW |
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 <errno.h> | 5 #include <errno.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <linux/videodev2.h> | 7 #include <linux/videodev2.h> |
8 #include <poll.h> | 8 #include <poll.h> |
9 #include <string.h> | 9 #include <string.h> |
10 #include <sys/eventfd.h> | 10 #include <sys/eventfd.h> |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 V4L2ImageProcessor::OutputRecord::~OutputRecord() {} | 50 V4L2ImageProcessor::OutputRecord::~OutputRecord() {} |
51 | 51 |
52 V4L2ImageProcessor::JobRecord::JobRecord() : output_buffer_index(-1) {} | 52 V4L2ImageProcessor::JobRecord::JobRecord() : output_buffer_index(-1) {} |
53 | 53 |
54 V4L2ImageProcessor::JobRecord::~JobRecord() {} | 54 V4L2ImageProcessor::JobRecord::~JobRecord() {} |
55 | 55 |
56 V4L2ImageProcessor::V4L2ImageProcessor(const scoped_refptr<V4L2Device>& device) | 56 V4L2ImageProcessor::V4L2ImageProcessor(const scoped_refptr<V4L2Device>& device) |
57 : input_format_(PIXEL_FORMAT_UNKNOWN), | 57 : input_format_(PIXEL_FORMAT_UNKNOWN), |
58 output_format_(PIXEL_FORMAT_UNKNOWN), | 58 output_format_(PIXEL_FORMAT_UNKNOWN), |
59 input_memory_type_(V4L2_MEMORY_USERPTR), | 59 input_memory_type_(V4L2_MEMORY_USERPTR), |
| 60 output_memory_type_(V4L2_MEMORY_MMAP), |
60 input_format_fourcc_(0), | 61 input_format_fourcc_(0), |
61 output_format_fourcc_(0), | 62 output_format_fourcc_(0), |
62 input_planes_count_(0), | 63 input_planes_count_(0), |
63 output_planes_count_(0), | 64 output_planes_count_(0), |
64 child_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 65 child_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
65 device_(device), | 66 device_(device), |
66 device_thread_("V4L2ImageProcessorThread"), | 67 device_thread_("V4L2ImageProcessorThread"), |
67 device_poll_thread_("V4L2ImageProcessorDevicePollThread"), | 68 device_poll_thread_("V4L2ImageProcessorDevicePollThread"), |
68 input_streamon_(false), | 69 input_streamon_(false), |
69 input_buffer_queued_count_(0), | 70 input_buffer_queued_count_(0), |
(...skipping 23 matching lines...) Expand all Loading... |
93 | 94 |
94 void V4L2ImageProcessor::NotifyErrorOnChildThread( | 95 void V4L2ImageProcessor::NotifyErrorOnChildThread( |
95 const base::Closure& error_cb) { | 96 const base::Closure& error_cb) { |
96 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 97 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
97 error_cb_.Run(); | 98 error_cb_.Run(); |
98 } | 99 } |
99 | 100 |
100 bool V4L2ImageProcessor::Initialize(VideoPixelFormat input_format, | 101 bool V4L2ImageProcessor::Initialize(VideoPixelFormat input_format, |
101 VideoPixelFormat output_format, | 102 VideoPixelFormat output_format, |
102 v4l2_memory input_memory_type, | 103 v4l2_memory input_memory_type, |
| 104 v4l2_memory output_memory_type, |
103 gfx::Size input_visible_size, | 105 gfx::Size input_visible_size, |
104 gfx::Size input_allocated_size, | 106 gfx::Size input_allocated_size, |
105 gfx::Size output_visible_size, | 107 gfx::Size output_visible_size, |
106 gfx::Size output_allocated_size, | 108 gfx::Size output_allocated_size, |
107 int num_buffers, | 109 int num_buffers, |
108 const base::Closure& error_cb) { | 110 const base::Closure& error_cb) { |
109 DCHECK(!error_cb.is_null()); | 111 DCHECK(!error_cb.is_null()); |
110 DCHECK_GT(num_buffers, 0); | 112 DCHECK_GT(num_buffers, 0); |
111 DCHECK(input_memory_type == V4L2_MEMORY_USERPTR || | 113 DCHECK(input_memory_type == V4L2_MEMORY_USERPTR || |
112 input_memory_type == V4L2_MEMORY_DMABUF); | 114 input_memory_type == V4L2_MEMORY_DMABUF); |
| 115 DCHECK(output_memory_type == V4L2_MEMORY_MMAP || |
| 116 output_memory_type == V4L2_MEMORY_DMABUF); |
113 error_cb_ = error_cb; | 117 error_cb_ = error_cb; |
114 | 118 |
115 input_format_ = input_format; | 119 input_format_ = input_format; |
116 output_format_ = output_format; | 120 output_format_ = output_format; |
117 input_format_fourcc_ = V4L2Device::VideoPixelFormatToV4L2PixFmt(input_format); | 121 input_format_fourcc_ = V4L2Device::VideoPixelFormatToV4L2PixFmt(input_format); |
118 output_format_fourcc_ = | 122 output_format_fourcc_ = |
119 V4L2Device::VideoPixelFormatToV4L2PixFmt(output_format); | 123 V4L2Device::VideoPixelFormatToV4L2PixFmt(output_format); |
120 num_buffers_ = num_buffers; | 124 num_buffers_ = num_buffers; |
121 | 125 |
122 if (!input_format_fourcc_ || !output_format_fourcc_) { | 126 if (!input_format_fourcc_ || !output_format_fourcc_) { |
123 LOG(ERROR) << "Unrecognized format(s)"; | 127 LOG(ERROR) << "Unrecognized format(s)"; |
124 return false; | 128 return false; |
125 } | 129 } |
126 | 130 |
127 input_memory_type_ = input_memory_type; | 131 input_memory_type_ = input_memory_type; |
| 132 output_memory_type_ = output_memory_type; |
128 input_visible_size_ = input_visible_size; | 133 input_visible_size_ = input_visible_size; |
129 input_allocated_size_ = input_allocated_size; | 134 input_allocated_size_ = input_allocated_size; |
130 output_visible_size_ = output_visible_size; | 135 output_visible_size_ = output_visible_size; |
131 output_allocated_size_ = output_allocated_size; | 136 output_allocated_size_ = output_allocated_size; |
132 | 137 |
133 // Capabilities check. | 138 // Capabilities check. |
134 struct v4l2_capability caps; | 139 struct v4l2_capability caps; |
135 memset(&caps, 0, sizeof(caps)); | 140 memset(&caps, 0, sizeof(caps)); |
136 const __u32 kCapsRequired = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; | 141 const __u32 kCapsRequired = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; |
137 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYCAP, &caps); | 142 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYCAP, &caps); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 format.fmt.pix_mp.pixelformat = pixelformat; | 215 format.fmt.pix_mp.pixelformat = pixelformat; |
211 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_TRY_FMT, &format); | 216 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_TRY_FMT, &format); |
212 | 217 |
213 *num_planes = format.fmt.pix_mp.num_planes; | 218 *num_planes = format.fmt.pix_mp.num_planes; |
214 *size = V4L2Device::CodedSizeFromV4L2Format(format); | 219 *size = V4L2Device::CodedSizeFromV4L2Format(format); |
215 DVLOG(1) << __func__ << ": adjusted output coded size=" << size->ToString() | 220 DVLOG(1) << __func__ << ": adjusted output coded size=" << size->ToString() |
216 << ", num_planes=" << *num_planes; | 221 << ", num_planes=" << *num_planes; |
217 return true; | 222 return true; |
218 } | 223 } |
219 | 224 |
220 void V4L2ImageProcessor::Process(const scoped_refptr<VideoFrame>& frame, | 225 bool V4L2ImageProcessor::Process(const scoped_refptr<VideoFrame>& frame, |
221 int output_buffer_index, | 226 int output_buffer_index, |
| 227 std::vector<base::ScopedFD> output_dmabuf_fds, |
222 const FrameReadyCB& cb) { | 228 const FrameReadyCB& cb) { |
223 DVLOG(3) << __func__ << ": ts=" << frame->timestamp().InMilliseconds(); | 229 DVLOG(3) << __func__ << ": ts=" << frame->timestamp().InMilliseconds(); |
| 230 size_t expected_num_fds = |
| 231 (output_memory_type_ == V4L2_MEMORY_DMABUF ? output_planes_count_ : 0); |
| 232 if (expected_num_fds != output_dmabuf_fds.size()) { |
| 233 LOG(ERROR) << __func__ << ": wrong number of output fds. Expected " |
| 234 << expected_num_fds << ", actual " << output_dmabuf_fds.size(); |
| 235 return false; |
| 236 } |
224 | 237 |
225 std::unique_ptr<JobRecord> job_record(new JobRecord()); | 238 std::unique_ptr<JobRecord> job_record(new JobRecord()); |
226 job_record->frame = frame; | 239 job_record->frame = frame; |
227 job_record->output_buffer_index = output_buffer_index; | 240 job_record->output_buffer_index = output_buffer_index; |
| 241 job_record->output_dmabuf_fds = std::move(output_dmabuf_fds); |
228 job_record->ready_cb = cb; | 242 job_record->ready_cb = cb; |
229 | 243 |
230 device_thread_.task_runner()->PostTask( | 244 device_thread_.task_runner()->PostTask( |
231 FROM_HERE, base::Bind(&V4L2ImageProcessor::ProcessTask, | 245 FROM_HERE, base::Bind(&V4L2ImageProcessor::ProcessTask, |
232 base::Unretained(this), base::Passed(&job_record))); | 246 base::Unretained(this), base::Passed(&job_record))); |
| 247 return true; |
233 } | 248 } |
234 | 249 |
235 void V4L2ImageProcessor::ProcessTask(std::unique_ptr<JobRecord> job_record) { | 250 void V4L2ImageProcessor::ProcessTask(std::unique_ptr<JobRecord> job_record) { |
236 int index = job_record->output_buffer_index; | 251 int index = job_record->output_buffer_index; |
237 DVLOG(3) << __func__ << ": Reusing output buffer, index=" << index; | 252 DVLOG(3) << __func__ << ": Reusing output buffer, index=" << index; |
238 DCHECK(device_thread_.task_runner()->BelongsToCurrentThread()); | 253 DCHECK(device_thread_.task_runner()->BelongsToCurrentThread()); |
| 254 DCHECK(output_buffer_map_[index].dmabuf_fds.empty()); |
| 255 output_buffer_map_[index].dmabuf_fds = |
| 256 std::move(job_record->output_dmabuf_fds); |
239 | 257 |
240 EnqueueOutput(index); | 258 EnqueueOutput(index); |
241 input_queue_.push(make_linked_ptr(job_record.release())); | 259 input_queue_.push(make_linked_ptr(job_record.release())); |
242 EnqueueInput(); | 260 EnqueueInput(); |
243 } | 261 } |
244 | 262 |
245 bool V4L2ImageProcessor::Reset() { | 263 bool V4L2ImageProcessor::Reset() { |
246 DVLOG(3) << __func__; | 264 DVLOG(3) << __func__; |
247 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 265 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
248 DCHECK(device_thread_.IsRunning()); | 266 DCHECK(device_thread_.IsRunning()); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 crop.c.left = 0; | 400 crop.c.left = 0; |
383 crop.c.top = 0; | 401 crop.c.top = 0; |
384 crop.c.width = base::checked_cast<__u32>(output_visible_size_.width()); | 402 crop.c.width = base::checked_cast<__u32>(output_visible_size_.width()); |
385 crop.c.height = base::checked_cast<__u32>(output_visible_size_.height()); | 403 crop.c.height = base::checked_cast<__u32>(output_visible_size_.height()); |
386 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CROP, &crop); | 404 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CROP, &crop); |
387 | 405 |
388 struct v4l2_requestbuffers reqbufs; | 406 struct v4l2_requestbuffers reqbufs; |
389 memset(&reqbufs, 0, sizeof(reqbufs)); | 407 memset(&reqbufs, 0, sizeof(reqbufs)); |
390 reqbufs.count = num_buffers_; | 408 reqbufs.count = num_buffers_; |
391 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 409 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
392 reqbufs.memory = V4L2_MEMORY_MMAP; | 410 reqbufs.memory = output_memory_type_; |
393 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); | 411 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); |
394 if (static_cast<int>(reqbufs.count) != num_buffers_) { | 412 if (static_cast<int>(reqbufs.count) != num_buffers_) { |
395 LOG(ERROR) << "Failed to allocate output buffers. reqbufs.count=" | 413 LOG(ERROR) << "Failed to allocate output buffers. reqbufs.count=" |
396 << reqbufs.count << ", num_buffers=" << num_buffers_; | 414 << reqbufs.count << ", num_buffers=" << num_buffers_; |
397 return false; | 415 return false; |
398 } | 416 } |
399 | 417 |
400 DCHECK(output_buffer_map_.empty()); | 418 DCHECK(output_buffer_map_.empty()); |
401 output_buffer_map_.resize(reqbufs.count); | 419 output_buffer_map_.resize(reqbufs.count); |
402 | 420 |
(...skipping 16 matching lines...) Expand all Loading... |
419 } | 437 } |
420 | 438 |
421 void V4L2ImageProcessor::DestroyOutputBuffers() { | 439 void V4L2ImageProcessor::DestroyOutputBuffers() { |
422 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 440 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
423 DCHECK(!output_streamon_); | 441 DCHECK(!output_streamon_); |
424 | 442 |
425 struct v4l2_requestbuffers reqbufs; | 443 struct v4l2_requestbuffers reqbufs; |
426 memset(&reqbufs, 0, sizeof(reqbufs)); | 444 memset(&reqbufs, 0, sizeof(reqbufs)); |
427 reqbufs.count = 0; | 445 reqbufs.count = 0; |
428 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 446 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
429 reqbufs.memory = V4L2_MEMORY_MMAP; | 447 reqbufs.memory = output_memory_type_; |
430 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); | 448 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); |
431 | 449 |
432 output_buffer_map_.clear(); | 450 output_buffer_map_.clear(); |
433 } | 451 } |
434 | 452 |
435 void V4L2ImageProcessor::DevicePollTask(bool poll_device) { | 453 void V4L2ImageProcessor::DevicePollTask(bool poll_device) { |
436 DCHECK(device_poll_thread_.task_runner()->BelongsToCurrentThread()); | 454 DCHECK(device_poll_thread_.task_runner()->BelongsToCurrentThread()); |
437 | 455 |
438 bool event_pending; | 456 bool event_pending; |
439 if (!device_->Poll(poll_device, &event_pending)) { | 457 if (!device_->Poll(poll_device, &event_pending)) { |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 input_buffer_queued_count_--; | 578 input_buffer_queued_count_--; |
561 } | 579 } |
562 | 580 |
563 // Dequeue completed output (VIDEO_CAPTURE) buffers, recycle to the free list. | 581 // Dequeue completed output (VIDEO_CAPTURE) buffers, recycle to the free list. |
564 // Return the finished buffer to the client via the job ready callback. | 582 // Return the finished buffer to the client via the job ready callback. |
565 while (output_buffer_queued_count_ > 0) { | 583 while (output_buffer_queued_count_ > 0) { |
566 DCHECK(output_streamon_); | 584 DCHECK(output_streamon_); |
567 memset(&dqbuf, 0, sizeof(dqbuf)); | 585 memset(&dqbuf, 0, sizeof(dqbuf)); |
568 memset(&planes, 0, sizeof(planes)); | 586 memset(&planes, 0, sizeof(planes)); |
569 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 587 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
570 dqbuf.memory = V4L2_MEMORY_MMAP; | 588 dqbuf.memory = output_memory_type_; |
571 dqbuf.m.planes = planes; | 589 dqbuf.m.planes = planes; |
572 dqbuf.length = output_planes_count_; | 590 dqbuf.length = output_planes_count_; |
573 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { | 591 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { |
574 if (errno == EAGAIN) { | 592 if (errno == EAGAIN) { |
575 // EAGAIN if we're just out of buffers to dequeue. | 593 // EAGAIN if we're just out of buffers to dequeue. |
576 break; | 594 break; |
577 } | 595 } |
578 PLOG(ERROR) << "ioctl() failed: VIDIOC_DQBUF"; | 596 PLOG(ERROR) << "ioctl() failed: VIDIOC_DQBUF"; |
579 NotifyError(); | 597 NotifyError(); |
580 return; | 598 return; |
581 } | 599 } |
582 OutputRecord& output_record = output_buffer_map_[dqbuf.index]; | 600 OutputRecord& output_record = output_buffer_map_[dqbuf.index]; |
583 DCHECK(output_record.at_device); | 601 DCHECK(output_record.at_device); |
584 output_record.at_device = false; | 602 output_record.at_device = false; |
| 603 output_record.dmabuf_fds.clear(); |
585 output_buffer_queued_count_--; | 604 output_buffer_queued_count_--; |
586 | 605 |
587 // Jobs are always processed in FIFO order. | 606 // Jobs are always processed in FIFO order. |
588 DCHECK(!running_jobs_.empty()); | 607 DCHECK(!running_jobs_.empty()); |
589 linked_ptr<JobRecord> job_record = running_jobs_.front(); | 608 linked_ptr<JobRecord> job_record = running_jobs_.front(); |
590 running_jobs_.pop(); | 609 running_jobs_.pop(); |
591 | 610 |
592 DVLOG(3) << "Processing finished, returning frame, index=" << dqbuf.index; | 611 DVLOG(3) << "Processing finished, returning frame, index=" << dqbuf.index; |
593 | 612 |
594 child_task_runner_->PostTask( | 613 child_task_runner_->PostTask( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 DCHECK_LT(static_cast<size_t>(index), output_buffer_map_.size()); | 666 DCHECK_LT(static_cast<size_t>(index), output_buffer_map_.size()); |
648 // Enqueue an output (VIDEO_CAPTURE) buffer. | 667 // Enqueue an output (VIDEO_CAPTURE) buffer. |
649 OutputRecord& output_record = output_buffer_map_[index]; | 668 OutputRecord& output_record = output_buffer_map_[index]; |
650 DCHECK(!output_record.at_device); | 669 DCHECK(!output_record.at_device); |
651 struct v4l2_buffer qbuf; | 670 struct v4l2_buffer qbuf; |
652 struct v4l2_plane qbuf_planes[VIDEO_MAX_PLANES]; | 671 struct v4l2_plane qbuf_planes[VIDEO_MAX_PLANES]; |
653 memset(&qbuf, 0, sizeof(qbuf)); | 672 memset(&qbuf, 0, sizeof(qbuf)); |
654 memset(qbuf_planes, 0, sizeof(qbuf_planes)); | 673 memset(qbuf_planes, 0, sizeof(qbuf_planes)); |
655 qbuf.index = index; | 674 qbuf.index = index; |
656 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 675 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
657 qbuf.memory = V4L2_MEMORY_MMAP; | 676 qbuf.memory = output_memory_type_; |
| 677 if (output_memory_type_ == V4L2_MEMORY_DMABUF) { |
| 678 for (size_t i = 0; i < output_record.dmabuf_fds.size(); ++i) |
| 679 qbuf_planes[i].m.fd = output_record.dmabuf_fds[i].get(); |
| 680 } |
658 qbuf.m.planes = qbuf_planes; | 681 qbuf.m.planes = qbuf_planes; |
659 qbuf.length = output_planes_count_; | 682 qbuf.length = output_planes_count_; |
660 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); | 683 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); |
661 output_record.at_device = true; | 684 output_record.at_device = true; |
662 output_buffer_queued_count_++; | 685 output_buffer_queued_count_++; |
663 return true; | 686 return true; |
664 } | 687 } |
665 | 688 |
666 void V4L2ImageProcessor::StartDevicePoll() { | 689 void V4L2ImageProcessor::StartDevicePoll() { |
667 DVLOG(3) << __func__ << ": starting device poll"; | 690 DVLOG(3) << __func__ << ": starting device poll"; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 output_buffer_queued_count_ = 0; | 755 output_buffer_queued_count_ = 0; |
733 } | 756 } |
734 | 757 |
735 void V4L2ImageProcessor::FrameReady(const FrameReadyCB& cb, | 758 void V4L2ImageProcessor::FrameReady(const FrameReadyCB& cb, |
736 int output_buffer_index) { | 759 int output_buffer_index) { |
737 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 760 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
738 cb.Run(output_buffer_index); | 761 cb.Run(output_buffer_index); |
739 } | 762 } |
740 | 763 |
741 } // namespace media | 764 } // namespace media |
OLD | NEW |