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