OLD | NEW |
| (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/video/capture/linux/v4l2_capture_delegate_multi_plane.h" | |
6 | |
7 #include <sys/mman.h> | |
8 | |
9 namespace media { | |
10 | |
11 V4L2CaptureDelegateMultiPlane::V4L2CaptureDelegateMultiPlane( | |
12 const VideoCaptureDevice::Name& device_name, | |
13 const scoped_refptr<base::SingleThreadTaskRunner>& v4l2_task_runner, | |
14 int power_line_frequency) | |
15 : V4L2CaptureDelegate(device_name, | |
16 v4l2_task_runner, | |
17 power_line_frequency) { | |
18 } | |
19 | |
20 V4L2CaptureDelegateMultiPlane::~V4L2CaptureDelegateMultiPlane() { | |
21 } | |
22 | |
23 scoped_refptr<V4L2CaptureDelegate::BufferTracker> | |
24 V4L2CaptureDelegateMultiPlane::CreateBufferTracker() const { | |
25 return make_scoped_refptr(new BufferTrackerMPlane()); | |
26 } | |
27 | |
28 bool V4L2CaptureDelegateMultiPlane::FillV4L2Format( | |
29 v4l2_format* format, | |
30 uint32_t width, | |
31 uint32_t height, | |
32 uint32_t pixelformat_fourcc) const { | |
33 format->fmt.pix_mp.width = width; | |
34 format->fmt.pix_mp.height = height; | |
35 format->fmt.pix_mp.pixelformat = pixelformat_fourcc; | |
36 | |
37 const size_t num_v4l2_planes = | |
38 V4L2CaptureDelegate::GetNumPlanesForFourCc(pixelformat_fourcc); | |
39 if (num_v4l2_planes == 0u) | |
40 return false; | |
41 DCHECK_LE(num_v4l2_planes, static_cast<size_t>(VIDEO_MAX_PLANES)); | |
42 format->fmt.pix_mp.num_planes = num_v4l2_planes; | |
43 | |
44 v4l2_planes_.resize(num_v4l2_planes); | |
45 return true; | |
46 } | |
47 | |
48 void V4L2CaptureDelegateMultiPlane::FinishFillingV4L2Buffer( | |
49 v4l2_buffer* buffer) const { | |
50 buffer->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
51 buffer->length = v4l2_planes_.size(); | |
52 | |
53 static const struct v4l2_plane empty_plane = {}; | |
54 std::fill(v4l2_planes_.begin(), v4l2_planes_.end(), empty_plane); | |
55 buffer->m.planes = v4l2_planes_.data(); | |
56 } | |
57 | |
58 void V4L2CaptureDelegateMultiPlane::SendBuffer( | |
59 const scoped_refptr<BufferTracker>& buffer_tracker, | |
60 const v4l2_format& format) const { | |
61 DCHECK_EQ(capture_format().pixel_format, PIXEL_FORMAT_I420); | |
62 const size_t y_stride = format.fmt.pix_mp.plane_fmt[0].bytesperline; | |
63 const size_t u_stride = format.fmt.pix_mp.plane_fmt[1].bytesperline; | |
64 const size_t v_stride = format.fmt.pix_mp.plane_fmt[2].bytesperline; | |
65 DCHECK_GE(y_stride, 1u * capture_format().frame_size.width()); | |
66 DCHECK_GE(u_stride, 1u * capture_format().frame_size.width() / 2); | |
67 DCHECK_GE(v_stride, 1u * capture_format().frame_size.width() / 2); | |
68 client()->OnIncomingCapturedYuvData(buffer_tracker->GetPlaneStart(0), | |
69 buffer_tracker->GetPlaneStart(1), | |
70 buffer_tracker->GetPlaneStart(2), | |
71 y_stride, | |
72 u_stride, | |
73 v_stride, | |
74 capture_format(), | |
75 rotation(), | |
76 base::TimeTicks::Now()); | |
77 } | |
78 | |
79 bool V4L2CaptureDelegateMultiPlane::BufferTrackerMPlane::Init( | |
80 int fd, | |
81 const v4l2_buffer& buffer) { | |
82 for (size_t p = 0; p < buffer.length; ++p) { | |
83 // Some devices require mmap() to be called with both READ and WRITE. | |
84 // See http://crbug.com/178582. | |
85 void* const start = | |
86 mmap(NULL, buffer.m.planes[p].length, PROT_READ | PROT_WRITE, | |
87 MAP_SHARED, fd, buffer.m.planes[p].m.mem_offset); | |
88 if (start == MAP_FAILED) { | |
89 DLOG(ERROR) << "Error mmap()ing a V4L2 buffer into userspace"; | |
90 return false; | |
91 } | |
92 AddMmapedPlane(static_cast<uint8_t*>(start), buffer.m.planes[p].length); | |
93 DVLOG(3) << "Mmap()ed plane #" << p << " of " << buffer.m.planes[p].length | |
94 << "B"; | |
95 } | |
96 return true; | |
97 } | |
98 | |
99 } // namespace media | |
OLD | NEW |