1From 4b95dc3e44dd92bb0792a5a0c566556fc0445eeb Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Mon, 1 Jul 2019 10:37:35 +0800
4Subject: [PATCH 06/14] media: capture: linux: Support libv4l2 plugins
5
6Allow using libv4l2 plugins for linux v4l2 capture devices.
7
8Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
9---
10 chromium/media/capture/BUILD.gn               | 14 +++++
11 .../video/linux/v4l2_capture_device_impl.cc   | 53 ++++++++++++++++++-
12 .../video/linux/v4l2_capture_device_impl.h    | 11 ++++
13 3 files changed, 77 insertions(+), 1 deletion(-)
14
15diff --git a/chromium/media/capture/BUILD.gn b/chromium/media/capture/BUILD.gn
16index c9986f35c..109491e1f 100644
17--- a/chromium/media/capture/BUILD.gn
18+++ b/chromium/media/capture/BUILD.gn
19@@ -2,6 +2,7 @@
20 # Use of this source code is governed by a BSD-style license that can be
21 # found in the LICENSE file.
22
23+import("//build/buildflag_header.gni")
24 import("//build/config/features.gni")
25 import("//build/config/jumbo.gni")
26 import("//build/config/ui.gni")
27@@ -25,6 +26,14 @@ group("capture") {
28   ]
29 }
30
31+buildflag_header("buildflags") {
32+  header = "buildflags.h"
33+
34+  flags = [
35+    "USE_LIBV4L2=$use_v4lplugin",
36+  ]
37+}
38+
39 component("capture_switches") {
40   defines = [ "CAPTURE_IMPLEMENTATION" ]
41   sources = [
42@@ -268,6 +277,11 @@ jumbo_component("capture_lib") {
43       "video/linux/video_capture_device_linux.cc",
44       "video/linux/video_capture_device_linux.h",
45     ]
46+    public_deps += [ ":buildflags" ]
47+
48+    if (use_v4lplugin) {
49+      deps += [ "//media/gpu/v4l2:libv4l2_stubs" ]
50+    }
51   }
52
53   if (is_chromeos) {
54diff --git a/chromium/media/capture/video/linux/v4l2_capture_device_impl.cc b/chromium/media/capture/video/linux/v4l2_capture_device_impl.cc
55index c9040f5de..54acdc95c 100644
56--- a/chromium/media/capture/video/linux/v4l2_capture_device_impl.cc
57+++ b/chromium/media/capture/video/linux/v4l2_capture_device_impl.cc
58@@ -10,19 +10,62 @@
59 #include <sys/poll.h>
60 #include <unistd.h>
61
62+#if BUILDFLAG(USE_LIBV4L2)
63+// Auto-generated for dlopen libv4l2 libraries
64+#include "media/gpu/v4l2/v4l2_stubs.h"
65+#include "third_party/v4l-utils/lib/include/libv4l2.h"
66+
67+#include "base/files/file_path.h"
68+
69+using media_gpu_v4l2::kModuleV4l2;
70+using media_gpu_v4l2::InitializeStubs;
71+using media_gpu_v4l2::StubPathMap;
72+
73+static const base::FilePath::CharType kV4l2Lib[] =
74+    FILE_PATH_LITERAL("/usr/lib/libv4l2.so");
75+#endif
76+
77 namespace media {
78
79 V4L2CaptureDeviceImpl::~V4L2CaptureDeviceImpl() = default;
80
81+V4L2CaptureDeviceImpl::V4L2CaptureDeviceImpl() {
82+#if BUILDFLAG(USE_LIBV4L2)
83+  StubPathMap paths;
84+  paths[kModuleV4l2].push_back(kV4l2Lib);
85+
86+  has_libv4l2_ = InitializeStubs(paths);
87+#endif
88+}
89+
90 int V4L2CaptureDeviceImpl::open(const char* device_name, int flags) {
91-  return ::open(device_name, flags);
92+  int fd = ::open64(device_name, flags);
93+  if (fd < 0)
94+    return fd;
95+
96+#if BUILDFLAG(USE_LIBV4L2)
97+  use_libv4l2_ = false;
98+  if (has_libv4l2_ && v4l2_fd_open(fd, V4L2_DISABLE_CONVERSION) != -1) {
99+    VLOG(2) << "Using libv4l2 for " << device_name;
100+    use_libv4l2_ = true;
101+  }
102+#endif
103+  return fd;
104 }
105
106 int V4L2CaptureDeviceImpl::close(int fd) {
107+#if BUILDFLAG(USE_LIBV4L2)
108+  if (use_libv4l2_)
109+    return v4l2_close(fd);
110+#endif
111   return ::close(fd);
112 }
113
114 int V4L2CaptureDeviceImpl::ioctl(int fd, int request, void* argp) {
115+#if BUILDFLAG(USE_LIBV4L2)
116+  if (use_libv4l2_)
117+    return v4l2_ioctl(fd, request, argp);
118+#endif
119   return ::ioctl(fd, request, argp);
120 }
121
122@@ -32,10 +75,18 @@ void* V4L2CaptureDeviceImpl::mmap(void* start,
123                                   int flags,
124                                   int fd,
125                                   off_t offset) {
126+#if BUILDFLAG(USE_LIBV4L2)
127+  if (use_libv4l2_)
128+    return v4l2_mmap(start, length, prot, flags, fd, offset);
129+#endif
130   return ::mmap(start, length, prot, flags, fd, offset);
131 }
132
133 int V4L2CaptureDeviceImpl::munmap(void* start, size_t length) {
134+#if BUILDFLAG(USE_LIBV4L2)
135+  if (use_libv4l2_)
136+    return v4l2_munmap(start, length);
137+#endif
138   return ::munmap(start, length);
139 }
140
141diff --git a/chromium/media/capture/video/linux/v4l2_capture_device_impl.h b/chromium/media/capture/video/linux/v4l2_capture_device_impl.h
142index 936c8b093..f96c2d434 100644
143--- a/chromium/media/capture/video/linux/v4l2_capture_device_impl.h
144+++ b/chromium/media/capture/video/linux/v4l2_capture_device_impl.h
145@@ -8,6 +8,7 @@
146 #include <poll.h>
147 #include <sys/fcntl.h>
148
149+#include "media/capture/buildflags.h"
150 #include "media/capture/capture_export.h"
151 #include "media/capture/video/linux/v4l2_capture_device.h"
152
153@@ -17,6 +18,8 @@ namespace media {
154 // V4L2 APIs.
155 class CAPTURE_EXPORT V4L2CaptureDeviceImpl : public V4L2CaptureDevice {
156  public:
157+  V4L2CaptureDeviceImpl();
158+
159   int open(const char* device_name, int flags) override;
160   int close(int fd) override;
161   int ioctl(int fd, int request, void* argp) override;
162@@ -32,6 +35,14 @@ class CAPTURE_EXPORT V4L2CaptureDeviceImpl : public V4L2CaptureDevice {
163
164  private:
165   ~V4L2CaptureDeviceImpl() override;
166+
167+#if BUILDFLAG(USE_LIBV4L2)
168+  // Has libv4l2.
169+  bool has_libv4l2_;
170+  // Use libv4l2 when operating |fd|.
171+  bool use_libv4l2_;
172+#endif
173+
174 };
175
176 }  // namespace media
177--
1782.20.1
179
180