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