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 | 5 |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <libdrm/drm_fourcc.h> | 7 #include <libdrm/drm_fourcc.h> |
8 #include <linux/videodev2.h> | 8 #include <linux/videodev2.h> |
9 #include <poll.h> | 9 #include <poll.h> |
10 #include <sys/eventfd.h> | 10 #include <sys/eventfd.h> |
(...skipping 11 matching lines...) Expand all Loading... |
22 // Auto-generated for dlopen libv4l2 libraries | 22 // Auto-generated for dlopen libv4l2 libraries |
23 #include "content/common/gpu/media/v4l2_stubs.h" | 23 #include "content/common/gpu/media/v4l2_stubs.h" |
24 #include "third_party/v4l-utils/lib/include/libv4l2.h" | 24 #include "third_party/v4l-utils/lib/include/libv4l2.h" |
25 | 25 |
26 using content_common_gpu_media::kModuleV4l2; | 26 using content_common_gpu_media::kModuleV4l2; |
27 using content_common_gpu_media::InitializeStubs; | 27 using content_common_gpu_media::InitializeStubs; |
28 using content_common_gpu_media::StubPathMap; | 28 using content_common_gpu_media::StubPathMap; |
29 | 29 |
30 static const base::FilePath::CharType kV4l2Lib[] = | 30 static const base::FilePath::CharType kV4l2Lib[] = |
31 FILE_PATH_LITERAL("/usr/lib/libv4l2.so"); | 31 FILE_PATH_LITERAL("/usr/lib/libv4l2.so"); |
32 #else | |
33 #define v4l2_close close | |
34 #define v4l2_ioctl ioctl | |
35 #endif | 32 #endif |
36 | 33 |
37 namespace content { | 34 namespace content { |
38 | 35 |
39 namespace { | 36 namespace { |
40 const char kDecoderDevice[] = "/dev/video-dec"; | 37 const char kDecoderDevice[] = "/dev/video-dec"; |
41 const char kEncoderDevice[] = "/dev/video-enc"; | 38 const char kEncoderDevice[] = "/dev/video-enc"; |
42 const char kImageProcessorDevice[] = "/dev/gsc0"; | 39 const char kImageProcessorDevice[] = "/dev/gsc0"; |
43 } | 40 } |
44 | 41 |
45 GenericV4L2Device::GenericV4L2Device(Type type) | 42 GenericV4L2Device::GenericV4L2Device(Type type) |
46 : type_(type), | 43 : type_(type), |
47 device_fd_(-1), | 44 use_libv4l2_(false) { |
48 device_poll_interrupt_fd_(-1) {} | 45 } |
49 | 46 |
50 GenericV4L2Device::~GenericV4L2Device() { | 47 GenericV4L2Device::~GenericV4L2Device() { |
51 if (device_poll_interrupt_fd_ != -1) { | 48 #if defined(USE_LIBV4L2) |
52 close(device_poll_interrupt_fd_); | 49 if (use_libv4l2_ && device_fd_.is_valid()) |
53 device_poll_interrupt_fd_ = -1; | 50 v4l2_close(device_fd_.release()); |
54 } | 51 #endif |
55 if (device_fd_ != -1) { | |
56 v4l2_close(device_fd_); | |
57 device_fd_ = -1; | |
58 } | |
59 } | 52 } |
60 | 53 |
61 int GenericV4L2Device::Ioctl(int request, void* arg) { | 54 int GenericV4L2Device::Ioctl(int request, void* arg) { |
62 return HANDLE_EINTR(v4l2_ioctl(device_fd_, request, arg)); | 55 #if defined(USE_LIBV4L2) |
| 56 if (use_libv4l2_) |
| 57 return HANDLE_EINTR(v4l2_ioctl(device_fd_.get(), request, arg)); |
| 58 #endif |
| 59 return HANDLE_EINTR(ioctl(device_fd_.get(), request, arg)); |
63 } | 60 } |
64 | 61 |
65 bool GenericV4L2Device::Poll(bool poll_device, bool* event_pending) { | 62 bool GenericV4L2Device::Poll(bool poll_device, bool* event_pending) { |
66 struct pollfd pollfds[2]; | 63 struct pollfd pollfds[2]; |
67 nfds_t nfds; | 64 nfds_t nfds; |
68 int pollfd = -1; | 65 int pollfd = -1; |
69 | 66 |
70 pollfds[0].fd = device_poll_interrupt_fd_; | 67 pollfds[0].fd = device_poll_interrupt_fd_.get(); |
71 pollfds[0].events = POLLIN | POLLERR; | 68 pollfds[0].events = POLLIN | POLLERR; |
72 nfds = 1; | 69 nfds = 1; |
73 | 70 |
74 if (poll_device) { | 71 if (poll_device) { |
75 DVLOG(3) << "Poll(): adding device fd to poll() set"; | 72 DVLOG(3) << "Poll(): adding device fd to poll() set"; |
76 pollfds[nfds].fd = device_fd_; | 73 pollfds[nfds].fd = device_fd_.get(); |
77 pollfds[nfds].events = POLLIN | POLLOUT | POLLERR | POLLPRI; | 74 pollfds[nfds].events = POLLIN | POLLOUT | POLLERR | POLLPRI; |
78 pollfd = nfds; | 75 pollfd = nfds; |
79 nfds++; | 76 nfds++; |
80 } | 77 } |
81 | 78 |
82 if (HANDLE_EINTR(poll(pollfds, nfds, -1)) == -1) { | 79 if (HANDLE_EINTR(poll(pollfds, nfds, -1)) == -1) { |
83 DPLOG(ERROR) << "poll() failed"; | 80 DPLOG(ERROR) << "poll() failed"; |
84 return false; | 81 return false; |
85 } | 82 } |
86 *event_pending = (pollfd != -1 && pollfds[pollfd].revents & POLLPRI); | 83 *event_pending = (pollfd != -1 && pollfds[pollfd].revents & POLLPRI); |
87 return true; | 84 return true; |
88 } | 85 } |
89 | 86 |
90 void* GenericV4L2Device::Mmap(void* addr, | 87 void* GenericV4L2Device::Mmap(void* addr, |
91 unsigned int len, | 88 unsigned int len, |
92 int prot, | 89 int prot, |
93 int flags, | 90 int flags, |
94 unsigned int offset) { | 91 unsigned int offset) { |
95 return mmap(addr, len, prot, flags, device_fd_, offset); | 92 return mmap(addr, len, prot, flags, device_fd_.get(), offset); |
96 } | 93 } |
97 | 94 |
98 void GenericV4L2Device::Munmap(void* addr, unsigned int len) { | 95 void GenericV4L2Device::Munmap(void* addr, unsigned int len) { |
99 munmap(addr, len); | 96 munmap(addr, len); |
100 } | 97 } |
101 | 98 |
102 bool GenericV4L2Device::SetDevicePollInterrupt() { | 99 bool GenericV4L2Device::SetDevicePollInterrupt() { |
103 DVLOG(3) << "SetDevicePollInterrupt()"; | 100 DVLOG(3) << "SetDevicePollInterrupt()"; |
104 | 101 |
105 const uint64 buf = 1; | 102 const uint64 buf = 1; |
106 if (HANDLE_EINTR(write(device_poll_interrupt_fd_, &buf, sizeof(buf))) == -1) { | 103 if (HANDLE_EINTR(write(device_poll_interrupt_fd_.get(), &buf, sizeof(buf))) == |
| 104 -1) { |
107 DPLOG(ERROR) << "SetDevicePollInterrupt(): write() failed"; | 105 DPLOG(ERROR) << "SetDevicePollInterrupt(): write() failed"; |
108 return false; | 106 return false; |
109 } | 107 } |
110 return true; | 108 return true; |
111 } | 109 } |
112 | 110 |
113 bool GenericV4L2Device::ClearDevicePollInterrupt() { | 111 bool GenericV4L2Device::ClearDevicePollInterrupt() { |
114 DVLOG(3) << "ClearDevicePollInterrupt()"; | 112 DVLOG(3) << "ClearDevicePollInterrupt()"; |
115 | 113 |
116 uint64 buf; | 114 uint64 buf; |
117 if (HANDLE_EINTR(read(device_poll_interrupt_fd_, &buf, sizeof(buf))) == -1) { | 115 if (HANDLE_EINTR(read(device_poll_interrupt_fd_.get(), &buf, sizeof(buf))) == |
| 116 -1) { |
118 if (errno == EAGAIN) { | 117 if (errno == EAGAIN) { |
119 // No interrupt flag set, and we're reading nonblocking. Not an error. | 118 // No interrupt flag set, and we're reading nonblocking. Not an error. |
120 return true; | 119 return true; |
121 } else { | 120 } else { |
122 DPLOG(ERROR) << "ClearDevicePollInterrupt(): read() failed"; | 121 DPLOG(ERROR) << "ClearDevicePollInterrupt(): read() failed"; |
123 return false; | 122 return false; |
124 } | 123 } |
125 } | 124 } |
126 return true; | 125 return true; |
127 } | 126 } |
(...skipping 13 matching lines...) Expand all Loading... |
141 case kEncoder: | 140 case kEncoder: |
142 device_path = kEncoderDevice; | 141 device_path = kEncoderDevice; |
143 break; | 142 break; |
144 case kImageProcessor: | 143 case kImageProcessor: |
145 device_path = kImageProcessorDevice; | 144 device_path = kImageProcessorDevice; |
146 break; | 145 break; |
147 } | 146 } |
148 | 147 |
149 DVLOG(2) << "Initialize(): opening device: " << device_path; | 148 DVLOG(2) << "Initialize(): opening device: " << device_path; |
150 // Open the video device. | 149 // Open the video device. |
151 device_fd_ = HANDLE_EINTR(open(device_path, O_RDWR | O_NONBLOCK | O_CLOEXEC)); | 150 device_fd_.reset( |
152 if (device_fd_ == -1) { | 151 HANDLE_EINTR(open(device_path, O_RDWR | O_NONBLOCK | O_CLOEXEC))); |
| 152 if (!device_fd_.is_valid()) { |
153 return false; | 153 return false; |
154 } | 154 } |
155 #if defined(USE_LIBV4L2) | 155 #if defined(USE_LIBV4L2) |
156 if (HANDLE_EINTR(v4l2_fd_open(device_fd_, V4L2_DISABLE_CONVERSION)) == -1) { | 156 if (HANDLE_EINTR(v4l2_fd_open(device_fd_.get(), V4L2_DISABLE_CONVERSION)) != |
157 v4l2_close(device_fd_); | 157 -1) { |
158 return false; | 158 DVLOG(2) << "Using libv4l2 for " << device_path; |
| 159 use_libv4l2_ = true; |
159 } | 160 } |
160 #endif | 161 #endif |
161 | 162 |
162 device_poll_interrupt_fd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); | 163 device_poll_interrupt_fd_.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC)); |
163 if (device_poll_interrupt_fd_ == -1) { | 164 if (!device_poll_interrupt_fd_.is_valid()) { |
164 return false; | 165 return false; |
165 } | 166 } |
166 return true; | 167 return true; |
167 } | 168 } |
168 | 169 |
169 bool GenericV4L2Device::CanCreateEGLImageFrom(uint32_t v4l2_pixfmt) { | 170 bool GenericV4L2Device::CanCreateEGLImageFrom(uint32_t v4l2_pixfmt) { |
170 static uint32_t kEGLImageDrmFmtsSupported[] = { | 171 static uint32_t kEGLImageDrmFmtsSupported[] = { |
171 DRM_FORMAT_ARGB8888, | 172 DRM_FORMAT_ARGB8888, |
172 #if defined(ARCH_CPU_ARMEL) | 173 #if defined(ARCH_CPU_ARMEL) |
173 DRM_FORMAT_NV12, | 174 DRM_FORMAT_NV12, |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 StubPathMap paths; | 290 StubPathMap paths; |
290 paths[kModuleV4l2].push_back(kV4l2Lib); | 291 paths[kModuleV4l2].push_back(kV4l2Lib); |
291 | 292 |
292 return InitializeStubs(paths); | 293 return InitializeStubs(paths); |
293 #else | 294 #else |
294 return true; | 295 return true; |
295 #endif | 296 #endif |
296 } | 297 } |
297 | 298 |
298 } // namespace content | 299 } // namespace content |
OLD | NEW |