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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 | 218 |
214 *num_planes = format.fmt.pix_mp.num_planes; | 219 *num_planes = format.fmt.pix_mp.num_planes; |
215 *size = V4L2Device::CodedSizeFromV4L2Format(format); | 220 *size = V4L2Device::CodedSizeFromV4L2Format(format); |
216 DVLOG(1) << __func__ << ": adjusted output coded size=" << size->ToString() | 221 DVLOG(1) << __func__ << ": adjusted output coded size=" << size->ToString() |
217 << ", num_planes=" << *num_planes; | 222 << ", num_planes=" << *num_planes; |
218 return true; | 223 return true; |
219 } | 224 } |
220 | 225 |
221 void V4L2ImageProcessor::Process(const scoped_refptr<VideoFrame>& frame, | 226 void V4L2ImageProcessor::Process(const scoped_refptr<VideoFrame>& frame, |
222 int output_buffer_index, | 227 int output_buffer_index, |
228 std::vector<base::ScopedFD> output_dmabuf_fds, | |
223 const FrameReadyCB& cb) { | 229 const FrameReadyCB& cb) { |
224 DVLOG(3) << __func__ << ": ts=" << frame->timestamp().InMilliseconds(); | 230 DVLOG(3) << __func__ << ": ts=" << frame->timestamp().InMilliseconds(); |
231 if (output_memory_type_ == V4L2_MEMORY_DMABUF) { | |
232 DCHECK_EQ(output_planes_count_, output_dmabuf_fds.size()); | |
Pawel Osciak
2016/09/14 04:17:29
Sorry, I meant an if instead of DCHECK. The doc re
wuchengli
2016/09/14 05:17:27
I see. I added a boolean return value to indicate
| |
233 } else { | |
234 DCHECK_EQ(0u, output_dmabuf_fds.size()); | |
235 } | |
225 | 236 |
226 std::unique_ptr<JobRecord> job_record(new JobRecord()); | 237 std::unique_ptr<JobRecord> job_record(new JobRecord()); |
227 job_record->frame = frame; | 238 job_record->frame = frame; |
228 job_record->output_buffer_index = output_buffer_index; | 239 job_record->output_buffer_index = output_buffer_index; |
240 job_record->output_dmabuf_fds = std::move(output_dmabuf_fds); | |
229 job_record->ready_cb = cb; | 241 job_record->ready_cb = cb; |
230 | 242 |
231 device_thread_.task_runner()->PostTask( | 243 device_thread_.task_runner()->PostTask( |
232 FROM_HERE, base::Bind(&V4L2ImageProcessor::ProcessTask, | 244 FROM_HERE, base::Bind(&V4L2ImageProcessor::ProcessTask, |
233 base::Unretained(this), base::Passed(&job_record))); | 245 base::Unretained(this), base::Passed(&job_record))); |
234 } | 246 } |
235 | 247 |
236 void V4L2ImageProcessor::ProcessTask(std::unique_ptr<JobRecord> job_record) { | 248 void V4L2ImageProcessor::ProcessTask(std::unique_ptr<JobRecord> job_record) { |
237 int index = job_record->output_buffer_index; | 249 int index = job_record->output_buffer_index; |
238 DVLOG(3) << __func__ << ": Reusing output buffer, index=" << index; | 250 DVLOG(3) << __func__ << ": Reusing output buffer, index=" << index; |
239 DCHECK(device_thread_.task_runner()->BelongsToCurrentThread()); | 251 DCHECK(device_thread_.task_runner()->BelongsToCurrentThread()); |
252 output_buffer_map_[index].dmabuf_fds = | |
253 std::move(job_record->output_dmabuf_fds); | |
240 | 254 |
241 EnqueueOutput(index); | 255 EnqueueOutput(index); |
242 input_queue_.push(make_linked_ptr(job_record.release())); | 256 input_queue_.push(make_linked_ptr(job_record.release())); |
243 EnqueueInput(); | 257 EnqueueInput(); |
244 } | 258 } |
245 | 259 |
246 void V4L2ImageProcessor::Destroy() { | 260 void V4L2ImageProcessor::Destroy() { |
247 DVLOG(3) << __func__; | 261 DVLOG(3) << __func__; |
248 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 262 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
249 | 263 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
368 crop.c.left = 0; | 382 crop.c.left = 0; |
369 crop.c.top = 0; | 383 crop.c.top = 0; |
370 crop.c.width = base::checked_cast<__u32>(output_visible_size_.width()); | 384 crop.c.width = base::checked_cast<__u32>(output_visible_size_.width()); |
371 crop.c.height = base::checked_cast<__u32>(output_visible_size_.height()); | 385 crop.c.height = base::checked_cast<__u32>(output_visible_size_.height()); |
372 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CROP, &crop); | 386 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_CROP, &crop); |
373 | 387 |
374 struct v4l2_requestbuffers reqbufs; | 388 struct v4l2_requestbuffers reqbufs; |
375 memset(&reqbufs, 0, sizeof(reqbufs)); | 389 memset(&reqbufs, 0, sizeof(reqbufs)); |
376 reqbufs.count = num_buffers_; | 390 reqbufs.count = num_buffers_; |
377 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 391 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
378 reqbufs.memory = V4L2_MEMORY_MMAP; | 392 reqbufs.memory = output_memory_type_; |
379 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); | 393 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); |
380 if (static_cast<int>(reqbufs.count) != num_buffers_) { | 394 if (static_cast<int>(reqbufs.count) != num_buffers_) { |
381 LOG(ERROR) << "Failed to allocate output buffers. reqbufs.count=" | 395 LOG(ERROR) << "Failed to allocate output buffers. reqbufs.count=" |
382 << reqbufs.count << ", num_buffers=" << num_buffers_; | 396 << reqbufs.count << ", num_buffers=" << num_buffers_; |
383 return false; | 397 return false; |
384 } | 398 } |
385 | 399 |
386 DCHECK(output_buffer_map_.empty()); | 400 DCHECK(output_buffer_map_.empty()); |
387 output_buffer_map_.resize(reqbufs.count); | 401 output_buffer_map_.resize(reqbufs.count); |
388 | 402 |
(...skipping 16 matching lines...) Expand all Loading... | |
405 } | 419 } |
406 | 420 |
407 void V4L2ImageProcessor::DestroyOutputBuffers() { | 421 void V4L2ImageProcessor::DestroyOutputBuffers() { |
408 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 422 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
409 DCHECK(!output_streamon_); | 423 DCHECK(!output_streamon_); |
410 | 424 |
411 struct v4l2_requestbuffers reqbufs; | 425 struct v4l2_requestbuffers reqbufs; |
412 memset(&reqbufs, 0, sizeof(reqbufs)); | 426 memset(&reqbufs, 0, sizeof(reqbufs)); |
413 reqbufs.count = 0; | 427 reqbufs.count = 0; |
414 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 428 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
415 reqbufs.memory = V4L2_MEMORY_MMAP; | 429 reqbufs.memory = output_memory_type_; |
416 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); | 430 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); |
417 | 431 |
418 output_buffer_map_.clear(); | 432 output_buffer_map_.clear(); |
419 } | 433 } |
420 | 434 |
421 void V4L2ImageProcessor::DevicePollTask(bool poll_device) { | 435 void V4L2ImageProcessor::DevicePollTask(bool poll_device) { |
422 DCHECK(device_poll_thread_.task_runner()->BelongsToCurrentThread()); | 436 DCHECK(device_poll_thread_.task_runner()->BelongsToCurrentThread()); |
423 | 437 |
424 bool event_pending; | 438 bool event_pending; |
425 if (!device_->Poll(poll_device, &event_pending)) { | 439 if (!device_->Poll(poll_device, &event_pending)) { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
540 input_buffer_queued_count_--; | 554 input_buffer_queued_count_--; |
541 } | 555 } |
542 | 556 |
543 // Dequeue completed output (VIDEO_CAPTURE) buffers, recycle to the free list. | 557 // Dequeue completed output (VIDEO_CAPTURE) buffers, recycle to the free list. |
544 // Return the finished buffer to the client via the job ready callback. | 558 // Return the finished buffer to the client via the job ready callback. |
545 while (output_buffer_queued_count_ > 0) { | 559 while (output_buffer_queued_count_ > 0) { |
546 DCHECK(output_streamon_); | 560 DCHECK(output_streamon_); |
547 memset(&dqbuf, 0, sizeof(dqbuf)); | 561 memset(&dqbuf, 0, sizeof(dqbuf)); |
548 memset(&planes, 0, sizeof(planes)); | 562 memset(&planes, 0, sizeof(planes)); |
549 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 563 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
550 dqbuf.memory = V4L2_MEMORY_MMAP; | 564 dqbuf.memory = output_memory_type_; |
551 dqbuf.m.planes = planes; | 565 dqbuf.m.planes = planes; |
552 dqbuf.length = output_planes_count_; | 566 dqbuf.length = output_planes_count_; |
553 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { | 567 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { |
554 if (errno == EAGAIN) { | 568 if (errno == EAGAIN) { |
555 // EAGAIN if we're just out of buffers to dequeue. | 569 // EAGAIN if we're just out of buffers to dequeue. |
556 break; | 570 break; |
557 } | 571 } |
558 PLOG(ERROR) << "ioctl() failed: VIDIOC_DQBUF"; | 572 PLOG(ERROR) << "ioctl() failed: VIDIOC_DQBUF"; |
559 NotifyError(); | 573 NotifyError(); |
560 return; | 574 return; |
561 } | 575 } |
562 OutputRecord& output_record = output_buffer_map_[dqbuf.index]; | 576 OutputRecord& output_record = output_buffer_map_[dqbuf.index]; |
563 DCHECK(output_record.at_device); | 577 DCHECK(output_record.at_device); |
564 output_record.at_device = false; | 578 output_record.at_device = false; |
579 output_record.dmabuf_fds.clear(); | |
565 output_buffer_queued_count_--; | 580 output_buffer_queued_count_--; |
566 | 581 |
567 // Jobs are always processed in FIFO order. | 582 // Jobs are always processed in FIFO order. |
568 DCHECK(!running_jobs_.empty()); | 583 DCHECK(!running_jobs_.empty()); |
569 linked_ptr<JobRecord> job_record = running_jobs_.front(); | 584 linked_ptr<JobRecord> job_record = running_jobs_.front(); |
570 running_jobs_.pop(); | 585 running_jobs_.pop(); |
571 | 586 |
572 DVLOG(3) << "Processing finished, returning frame, index=" << dqbuf.index; | 587 DVLOG(3) << "Processing finished, returning frame, index=" << dqbuf.index; |
573 | 588 |
574 child_task_runner_->PostTask( | 589 child_task_runner_->PostTask( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
627 DCHECK_LT(static_cast<size_t>(index), output_buffer_map_.size()); | 642 DCHECK_LT(static_cast<size_t>(index), output_buffer_map_.size()); |
628 // Enqueue an output (VIDEO_CAPTURE) buffer. | 643 // Enqueue an output (VIDEO_CAPTURE) buffer. |
629 OutputRecord& output_record = output_buffer_map_[index]; | 644 OutputRecord& output_record = output_buffer_map_[index]; |
630 DCHECK(!output_record.at_device); | 645 DCHECK(!output_record.at_device); |
631 struct v4l2_buffer qbuf; | 646 struct v4l2_buffer qbuf; |
632 struct v4l2_plane qbuf_planes[VIDEO_MAX_PLANES]; | 647 struct v4l2_plane qbuf_planes[VIDEO_MAX_PLANES]; |
633 memset(&qbuf, 0, sizeof(qbuf)); | 648 memset(&qbuf, 0, sizeof(qbuf)); |
634 memset(qbuf_planes, 0, sizeof(qbuf_planes)); | 649 memset(qbuf_planes, 0, sizeof(qbuf_planes)); |
635 qbuf.index = index; | 650 qbuf.index = index; |
636 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | 651 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
637 qbuf.memory = V4L2_MEMORY_MMAP; | 652 qbuf.memory = output_memory_type_; |
653 if (output_memory_type_ == V4L2_MEMORY_DMABUF) { | |
654 for (size_t i = 0; i < output_record.dmabuf_fds.size(); ++i) | |
655 qbuf_planes[i].m.fd = output_record.dmabuf_fds[i].get(); | |
656 } | |
638 qbuf.m.planes = qbuf_planes; | 657 qbuf.m.planes = qbuf_planes; |
639 qbuf.length = output_planes_count_; | 658 qbuf.length = output_planes_count_; |
640 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); | 659 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); |
641 output_record.at_device = true; | 660 output_record.at_device = true; |
642 output_buffer_queued_count_++; | 661 output_buffer_queued_count_++; |
643 return true; | 662 return true; |
644 } | 663 } |
645 | 664 |
646 bool V4L2ImageProcessor::StartDevicePoll() { | 665 bool V4L2ImageProcessor::StartDevicePoll() { |
647 DVLOG(3) << __func__ << ": starting device poll"; | 666 DVLOG(3) << __func__ << ": starting device poll"; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
712 return true; | 731 return true; |
713 } | 732 } |
714 | 733 |
715 void V4L2ImageProcessor::FrameReady(const FrameReadyCB& cb, | 734 void V4L2ImageProcessor::FrameReady(const FrameReadyCB& cb, |
716 int output_buffer_index) { | 735 int output_buffer_index) { |
717 DCHECK(child_task_runner_->BelongsToCurrentThread()); | 736 DCHECK(child_task_runner_->BelongsToCurrentThread()); |
718 cb.Run(output_buffer_index); | 737 cb.Run(output_buffer_index); |
719 } | 738 } |
720 | 739 |
721 } // namespace media | 740 } // namespace media |
OLD | NEW |