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 device_fd_(-1), |
48 device_poll_interrupt_fd_(-1) {} | 45 device_poll_interrupt_fd_(-1), |
46 use_libv4l2_(false) { | |
47 } | |
49 | 48 |
50 GenericV4L2Device::~GenericV4L2Device() { | 49 GenericV4L2Device::~GenericV4L2Device() { |
51 if (device_poll_interrupt_fd_ != -1) { | 50 #if defined(USE_LIBV4L2) |
52 close(device_poll_interrupt_fd_); | 51 if (use_libv4l2_ && device_fd_ != -1) |
Pawel Osciak
2015/04/21 09:19:46
Technically use_libv4l2_ is only true if device_fd
wuchengli
2015/04/21 09:25:24
Right. My thought was the same -- keeping the chec
| |
53 device_poll_interrupt_fd_ = -1; | 52 v4l2_close(device_fd_.release()); |
54 } | 53 #endif |
55 if (device_fd_ != -1) { | |
56 v4l2_close(device_fd_); | |
57 device_fd_ = -1; | |
58 } | |
59 } | 54 } |
60 | 55 |
61 int GenericV4L2Device::Ioctl(int request, void* arg) { | 56 int GenericV4L2Device::Ioctl(int request, void* arg) { |
62 return HANDLE_EINTR(v4l2_ioctl(device_fd_, request, arg)); | 57 #if defined(USE_LIBV4L2) |
58 if (use_libv4l2_) | |
59 return HANDLE_EINTR(v4l2_ioctl(device_fd_.get(), request, arg)); | |
60 #endif | |
61 return HANDLE_EINTR(ioctl(device_fd_.get(), request, arg)); | |
63 } | 62 } |
64 | 63 |
65 bool GenericV4L2Device::Poll(bool poll_device, bool* event_pending) { | 64 bool GenericV4L2Device::Poll(bool poll_device, bool* event_pending) { |
66 struct pollfd pollfds[2]; | 65 struct pollfd pollfds[2]; |
67 nfds_t nfds; | 66 nfds_t nfds; |
68 int pollfd = -1; | 67 int pollfd = -1; |
69 | 68 |
70 pollfds[0].fd = device_poll_interrupt_fd_; | 69 pollfds[0].fd = device_poll_interrupt_fd_.get(); |
71 pollfds[0].events = POLLIN | POLLERR; | 70 pollfds[0].events = POLLIN | POLLERR; |
72 nfds = 1; | 71 nfds = 1; |
73 | 72 |
74 if (poll_device) { | 73 if (poll_device) { |
75 DVLOG(3) << "Poll(): adding device fd to poll() set"; | 74 DVLOG(3) << "Poll(): adding device fd to poll() set"; |
76 pollfds[nfds].fd = device_fd_; | 75 pollfds[nfds].fd = device_fd_.get(); |
77 pollfds[nfds].events = POLLIN | POLLOUT | POLLERR | POLLPRI; | 76 pollfds[nfds].events = POLLIN | POLLOUT | POLLERR | POLLPRI; |
78 pollfd = nfds; | 77 pollfd = nfds; |
79 nfds++; | 78 nfds++; |
80 } | 79 } |
81 | 80 |
82 if (HANDLE_EINTR(poll(pollfds, nfds, -1)) == -1) { | 81 if (HANDLE_EINTR(poll(pollfds, nfds, -1)) == -1) { |
83 DPLOG(ERROR) << "poll() failed"; | 82 DPLOG(ERROR) << "poll() failed"; |
84 return false; | 83 return false; |
85 } | 84 } |
86 *event_pending = (pollfd != -1 && pollfds[pollfd].revents & POLLPRI); | 85 *event_pending = (pollfd != -1 && pollfds[pollfd].revents & POLLPRI); |
87 return true; | 86 return true; |
88 } | 87 } |
89 | 88 |
90 void* GenericV4L2Device::Mmap(void* addr, | 89 void* GenericV4L2Device::Mmap(void* addr, |
91 unsigned int len, | 90 unsigned int len, |
92 int prot, | 91 int prot, |
93 int flags, | 92 int flags, |
94 unsigned int offset) { | 93 unsigned int offset) { |
95 return mmap(addr, len, prot, flags, device_fd_, offset); | 94 return mmap(addr, len, prot, flags, device_fd_.get(), offset); |
96 } | 95 } |
97 | 96 |
98 void GenericV4L2Device::Munmap(void* addr, unsigned int len) { | 97 void GenericV4L2Device::Munmap(void* addr, unsigned int len) { |
99 munmap(addr, len); | 98 munmap(addr, len); |
100 } | 99 } |
101 | 100 |
102 bool GenericV4L2Device::SetDevicePollInterrupt() { | 101 bool GenericV4L2Device::SetDevicePollInterrupt() { |
103 DVLOG(3) << "SetDevicePollInterrupt()"; | 102 DVLOG(3) << "SetDevicePollInterrupt()"; |
104 | 103 |
105 const uint64 buf = 1; | 104 const uint64 buf = 1; |
106 if (HANDLE_EINTR(write(device_poll_interrupt_fd_, &buf, sizeof(buf))) == -1) { | 105 if (HANDLE_EINTR(write(device_poll_interrupt_fd_.get(), &buf, sizeof(buf))) == |
106 -1) { | |
107 DPLOG(ERROR) << "SetDevicePollInterrupt(): write() failed"; | 107 DPLOG(ERROR) << "SetDevicePollInterrupt(): write() failed"; |
108 return false; | 108 return false; |
109 } | 109 } |
110 return true; | 110 return true; |
111 } | 111 } |
112 | 112 |
113 bool GenericV4L2Device::ClearDevicePollInterrupt() { | 113 bool GenericV4L2Device::ClearDevicePollInterrupt() { |
114 DVLOG(3) << "ClearDevicePollInterrupt()"; | 114 DVLOG(3) << "ClearDevicePollInterrupt()"; |
115 | 115 |
116 uint64 buf; | 116 uint64 buf; |
117 if (HANDLE_EINTR(read(device_poll_interrupt_fd_, &buf, sizeof(buf))) == -1) { | 117 if (HANDLE_EINTR(read(device_poll_interrupt_fd_.get(), &buf, sizeof(buf))) == |
118 -1) { | |
118 if (errno == EAGAIN) { | 119 if (errno == EAGAIN) { |
119 // No interrupt flag set, and we're reading nonblocking. Not an error. | 120 // No interrupt flag set, and we're reading nonblocking. Not an error. |
120 return true; | 121 return true; |
121 } else { | 122 } else { |
122 DPLOG(ERROR) << "ClearDevicePollInterrupt(): read() failed"; | 123 DPLOG(ERROR) << "ClearDevicePollInterrupt(): read() failed"; |
123 return false; | 124 return false; |
124 } | 125 } |
125 } | 126 } |
126 return true; | 127 return true; |
127 } | 128 } |
(...skipping 13 matching lines...) Expand all Loading... | |
141 case kEncoder: | 142 case kEncoder: |
142 device_path = kEncoderDevice; | 143 device_path = kEncoderDevice; |
143 break; | 144 break; |
144 case kImageProcessor: | 145 case kImageProcessor: |
145 device_path = kImageProcessorDevice; | 146 device_path = kImageProcessorDevice; |
146 break; | 147 break; |
147 } | 148 } |
148 | 149 |
149 DVLOG(2) << "Initialize(): opening device: " << device_path; | 150 DVLOG(2) << "Initialize(): opening device: " << device_path; |
150 // Open the video device. | 151 // Open the video device. |
151 device_fd_ = HANDLE_EINTR(open(device_path, O_RDWR | O_NONBLOCK | O_CLOEXEC)); | 152 device_fd_.reset( |
153 HANDLE_EINTR(open(device_path, O_RDWR | O_NONBLOCK | O_CLOEXEC))); | |
152 if (device_fd_ == -1) { | 154 if (device_fd_ == -1) { |
153 return false; | 155 return false; |
154 } | 156 } |
155 #if defined(USE_LIBV4L2) | 157 #if defined(USE_LIBV4L2) |
156 if (HANDLE_EINTR(v4l2_fd_open(device_fd_, V4L2_DISABLE_CONVERSION)) == -1) { | 158 if (HANDLE_EINTR(v4l2_fd_open(device_fd_.get(), V4L2_DISABLE_CONVERSION)) != |
157 v4l2_close(device_fd_); | 159 -1) { |
158 return false; | 160 DVLOG(2) << "Using libv4l2 for " << device_path; |
161 use_libv4l2_ = true; | |
159 } | 162 } |
160 #endif | 163 #endif |
161 | 164 |
162 device_poll_interrupt_fd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); | 165 device_poll_interrupt_fd_.reset(eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC)); |
163 if (device_poll_interrupt_fd_ == -1) { | 166 if (device_poll_interrupt_fd_ == -1) { |
164 return false; | 167 return false; |
165 } | 168 } |
166 return true; | 169 return true; |
167 } | 170 } |
168 | 171 |
169 bool GenericV4L2Device::CanCreateEGLImageFrom(uint32_t v4l2_pixfmt) { | 172 bool GenericV4L2Device::CanCreateEGLImageFrom(uint32_t v4l2_pixfmt) { |
170 static uint32_t kEGLImageDrmFmtsSupported[] = { | 173 static uint32_t kEGLImageDrmFmtsSupported[] = { |
171 DRM_FORMAT_ARGB8888, | 174 DRM_FORMAT_ARGB8888, |
172 #if defined(ARCH_CPU_ARMEL) | 175 #if defined(ARCH_CPU_ARMEL) |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 StubPathMap paths; | 292 StubPathMap paths; |
290 paths[kModuleV4l2].push_back(kV4l2Lib); | 293 paths[kModuleV4l2].push_back(kV4l2Lib); |
291 | 294 |
292 return InitializeStubs(paths); | 295 return InitializeStubs(paths); |
293 #else | 296 #else |
294 return true; | 297 return true; |
295 #endif | 298 #endif |
296 } | 299 } |
297 | 300 |
298 } // namespace content | 301 } // namespace content |
OLD | NEW |