1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "TouchVideoDevice.h"
18
19 #define LOG_TAG "TouchVideoDevice"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <linux/videodev2.h>
25 #include <sys/ioctl.h>
26 #include <sys/mman.h>
27 #include <unistd.h>
28 #include <iostream>
29
30 #include <android-base/stringprintf.h>
31 #include <android-base/unique_fd.h>
32 #include <log/log.h>
33
34 using android::base::StringPrintf;
35 using android::base::unique_fd;
36
37 namespace android {
38
TouchVideoDevice(int fd,std::string && name,std::string && devicePath,uint32_t height,uint32_t width,const std::array<const int16_t *,NUM_BUFFERS> & readLocations)39 TouchVideoDevice::TouchVideoDevice(int fd, std::string&& name, std::string&& devicePath,
40 uint32_t height, uint32_t width,
41 const std::array<const int16_t*, NUM_BUFFERS>& readLocations)
42 : mFd(fd),
43 mName(std::move(name)),
44 mPath(std::move(devicePath)),
45 mHeight(height),
46 mWidth(width),
47 mReadLocations(readLocations) {
48 mFrames.reserve(MAX_QUEUE_SIZE);
49 };
50
create(std::string devicePath)51 std::unique_ptr<TouchVideoDevice> TouchVideoDevice::create(std::string devicePath) {
52 unique_fd fd(open(devicePath.c_str(), O_RDWR | O_NONBLOCK));
53 if (fd.get() == INVALID_FD) {
54 ALOGE("Could not open video device %s: %s", devicePath.c_str(), strerror(errno));
55 return nullptr;
56 }
57
58 struct v4l2_capability cap;
59 int result = ioctl(fd.get(), VIDIOC_QUERYCAP, &cap);
60 if (result == -1) {
61 ALOGE("VIDIOC_QUERYCAP failed: %s", strerror(errno));
62 return nullptr;
63 }
64 if (!(cap.capabilities & V4L2_CAP_TOUCH)) {
65 ALOGE("Capability V4L2_CAP_TOUCH is not present, can't use device for heatmap data. "
66 "Make sure device specifies V4L2_CAP_TOUCH");
67 return nullptr;
68 }
69 ALOGI("Opening video device: driver = %s, card = %s, bus_info = %s, version = %i", cap.driver,
70 cap.card, cap.bus_info, cap.version);
71 std::string name = reinterpret_cast<const char*>(cap.card);
72
73 struct v4l2_input v4l2_input_struct;
74 v4l2_input_struct.index = 0;
75 result = ioctl(fd.get(), VIDIOC_ENUMINPUT, &v4l2_input_struct);
76 if (result == -1) {
77 ALOGE("VIDIOC_ENUMINPUT failed: %s", strerror(errno));
78 return nullptr;
79 }
80
81 if (v4l2_input_struct.type != V4L2_INPUT_TYPE_TOUCH) {
82 ALOGE("Video device does not provide touch data. "
83 "Make sure device specifies V4L2_INPUT_TYPE_TOUCH.");
84 return nullptr;
85 }
86
87 struct v4l2_format v4l2_fmt;
88 v4l2_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
89 result = ioctl(fd.get(), VIDIOC_G_FMT, &v4l2_fmt);
90 if (result == -1) {
91 ALOGE("VIDIOC_G_FMT failed: %s", strerror(errno));
92 return nullptr;
93 }
94 const uint32_t height = v4l2_fmt.fmt.pix.height;
95 const uint32_t width = v4l2_fmt.fmt.pix.width;
96 ALOGI("Frame dimensions: height = %" PRIu32 " width = %" PRIu32, height, width);
97
98 struct v4l2_requestbuffers req = {};
99 req.count = NUM_BUFFERS;
100 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
101 req.memory = V4L2_MEMORY_MMAP;
102 // req.reserved is zeroed during initialization, which is required per v4l docs
103 result = ioctl(fd.get(), VIDIOC_REQBUFS, &req);
104 if (result == -1) {
105 ALOGE("VIDIOC_REQBUFS failed: %s", strerror(errno));
106 return nullptr;
107 }
108 if (req.count != NUM_BUFFERS) {
109 ALOGE("Requested %zu buffers, but driver responded with count=%i", NUM_BUFFERS, req.count);
110 return nullptr;
111 }
112
113 struct v4l2_buffer buf = {};
114 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
115 buf.memory = V4L2_MEMORY_MMAP;
116 // buf.reserved and buf.reserved2 are zeroed during initialization, required per v4l docs
117 std::array<const int16_t*, NUM_BUFFERS> readLocations;
118 for (size_t i = 0; i < NUM_BUFFERS; i++) {
119 buf.index = i;
120 result = ioctl(fd.get(), VIDIOC_QUERYBUF, &buf);
121 if (result == -1) {
122 ALOGE("VIDIOC_QUERYBUF failed: %s", strerror(errno));
123 return nullptr;
124 }
125 if (buf.length != height * width * sizeof(int16_t)) {
126 ALOGE("Unexpected value of buf.length = %i (offset = %" PRIu32 ")", buf.length,
127 buf.m.offset);
128 return nullptr;
129 }
130
131 readLocations[i] = static_cast<const int16_t*>(
132 mmap(nullptr /* start anywhere */, buf.length, PROT_READ /* required */,
133 MAP_SHARED /* recommended */, fd.get(), buf.m.offset));
134 if (readLocations[i] == MAP_FAILED) {
135 ALOGE("%s: map failed: %s", __func__, strerror(errno));
136 return nullptr;
137 }
138 }
139
140 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
141 result = ioctl(fd.get(), VIDIOC_STREAMON, &type);
142 if (result == -1) {
143 ALOGE("VIDIOC_STREAMON failed: %s", strerror(errno));
144 return nullptr;
145 }
146
147 for (size_t i = 0; i < NUM_BUFFERS; i++) {
148 buf.index = i;
149 result = ioctl(fd.get(), VIDIOC_QBUF, &buf);
150 if (result == -1) {
151 ALOGE("VIDIOC_QBUF failed for buffer %zu: %s", i, strerror(errno));
152 return nullptr;
153 }
154 }
155 // Using 'new' to access a non-public constructor.
156 return std::unique_ptr<TouchVideoDevice>(new TouchVideoDevice(fd.release(), std::move(name),
157 std::move(devicePath), height,
158 width, readLocations));
159 }
160
readAndQueueFrames()161 size_t TouchVideoDevice::readAndQueueFrames() {
162 std::vector<TouchVideoFrame> frames = readFrames();
163 const size_t numFrames = frames.size();
164 if (numFrames == 0) {
165 // Likely an error occurred
166 return 0;
167 }
168 // Concatenate the vectors, then clip up to maximum size allowed
169 mFrames.insert(mFrames.end(), std::make_move_iterator(frames.begin()),
170 std::make_move_iterator(frames.end()));
171 if (mFrames.size() > MAX_QUEUE_SIZE) {
172 ALOGE("More than %zu frames have been accumulated. Dropping %zu frames", MAX_QUEUE_SIZE,
173 mFrames.size() - MAX_QUEUE_SIZE);
174 mFrames.erase(mFrames.begin(), mFrames.end() - MAX_QUEUE_SIZE);
175 }
176 return numFrames;
177 }
178
consumeFrames()179 std::vector<TouchVideoFrame> TouchVideoDevice::consumeFrames() {
180 std::vector<TouchVideoFrame> frames = std::move(mFrames);
181 mFrames = {};
182 return frames;
183 }
184
readFrame()185 std::optional<TouchVideoFrame> TouchVideoDevice::readFrame() {
186 struct v4l2_buffer buf = {};
187 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
188 buf.memory = V4L2_MEMORY_MMAP;
189 int result = ioctl(mFd.get(), VIDIOC_DQBUF, &buf);
190 if (result == -1) {
191 // EAGAIN means we've reached the end of the read buffer, so it's expected.
192 if (errno != EAGAIN) {
193 ALOGE("VIDIOC_DQBUF failed: %s", strerror(errno));
194 }
195 return std::nullopt;
196 }
197 if ((buf.flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) != V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC) {
198 // We use CLOCK_MONOTONIC for input events, so if the clocks don't match,
199 // we can't compare timestamps. Just log a warning, since this is a driver issue
200 ALOGW("The timestamp %ld.%ld was not acquired using CLOCK_MONOTONIC", buf.timestamp.tv_sec,
201 buf.timestamp.tv_usec);
202 }
203 std::vector<int16_t> data(mHeight * mWidth);
204 const int16_t* readFrom = mReadLocations[buf.index];
205 std::copy(readFrom, readFrom + mHeight * mWidth, data.begin());
206 TouchVideoFrame frame(mHeight, mWidth, std::move(data), buf.timestamp);
207
208 result = ioctl(mFd.get(), VIDIOC_QBUF, &buf);
209 if (result == -1) {
210 ALOGE("VIDIOC_QBUF failed: %s", strerror(errno));
211 }
212 return std::make_optional(std::move(frame));
213 }
214
215 /*
216 * This function should not be called unless buffer is ready! This must be checked with
217 * select, poll, epoll, or some other similar api first.
218 * The oldest frame will be at the beginning of the array.
219 */
readFrames()220 std::vector<TouchVideoFrame> TouchVideoDevice::readFrames() {
221 std::vector<TouchVideoFrame> frames;
222 while (true) {
223 std::optional<TouchVideoFrame> frame = readFrame();
224 if (!frame) {
225 break;
226 }
227 frames.push_back(std::move(*frame));
228 }
229 return frames;
230 }
231
~TouchVideoDevice()232 TouchVideoDevice::~TouchVideoDevice() {
233 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
234 int result = ioctl(mFd.get(), VIDIOC_STREAMOFF, &type);
235 if (result == -1) {
236 ALOGE("VIDIOC_STREAMOFF failed: %s", strerror(errno));
237 }
238 for (const int16_t* buffer : mReadLocations) {
239 void* bufferAddress = static_cast<void*>(const_cast<int16_t*>(buffer));
240 result = munmap(bufferAddress, mHeight * mWidth * sizeof(int16_t));
241 if (result == -1) {
242 ALOGE("%s: Couldn't unmap: [%s]", __func__, strerror(errno));
243 }
244 }
245 }
246
dump() const247 std::string TouchVideoDevice::dump() const {
248 return StringPrintf("Video device %s (%s) : height=%" PRIu32 ", width=%" PRIu32
249 ", fd=%i, hasValidFd=%s",
250 mName.c_str(), mPath.c_str(), mHeight, mWidth, mFd.get(),
251 hasValidFd() ? "true" : "false");
252 }
253
254 } // namespace android
255