blob: c0750785283d3ef0b0fe6b8f4bbf4d3c1761e54d [file] [log] [blame]
Siarhei Vishniakou22c88462018-12-13 19:34:53 -08001/*
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
34using android::base::StringPrintf;
35using android::base::unique_fd;
36
37namespace android {
38
39TouchVideoDevice::TouchVideoDevice(int fd, std::string&& name, std::string&& devicePath,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070040 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),
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080047 mReadLocations(readLocations) {
48 mFrames.reserve(MAX_QUEUE_SIZE);
49};
50
51std::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. "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070066 "Make sure device specifies V4L2_CAP_TOUCH");
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080067 return nullptr;
68 }
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070069 ALOGI("Opening video device: driver = %s, card = %s, bus_info = %s, version = %i", cap.driver,
70 cap.card, cap.bus_info, cap.version);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080071 std::string name = reinterpret_cast<const char*>(cap.card);
72
73 struct v4l2_input v4l2_input_struct;
Siarhei Vishniakoufe6e96e2019-03-18 14:12:56 -070074 v4l2_input_struct.index = 0;
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080075 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. "
Prabir Pradhanda7c00c2019-08-29 14:12:42 -070083 "Make sure device specifies V4L2_INPUT_TYPE_TOUCH.");
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080084 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 }
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080094 const uint32_t height = v4l2_fmt.fmt.pix.height;
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060095 const uint32_t width = v4l2_fmt.fmt.pix.width;
96 ALOGI("Frame dimensions: height = %" PRIu32 " width = %" PRIu32, height, width);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080097
Siarhei Vishniakoufe6e96e2019-03-18 14:12:56 -070098 struct v4l2_requestbuffers req = {};
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080099 req.count = NUM_BUFFERS;
100 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
101 req.memory = V4L2_MEMORY_MMAP;
Siarhei Vishniakoufe6e96e2019-03-18 14:12:56 -0700102 // req.reserved is zeroed during initialization, which is required per v4l docs
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800103 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;
Siarhei Vishniakoufe6e96e2019-03-18 14:12:56 -0700116 // buf.reserved and buf.reserved2 are zeroed during initialization, required per v4l docs
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800117 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 }
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600125 if (buf.length != height * width * sizeof(int16_t)) {
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700126 ALOGE("Unexpected value of buf.length = %i (offset = %" PRIu32 ")", buf.length,
127 buf.m.offset);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800128 return nullptr;
129 }
130
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700131 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));
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800134 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.
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700156 return std::unique_ptr<TouchVideoDevice>(new TouchVideoDevice(fd.release(), std::move(name),
157 std::move(devicePath), height,
158 width, readLocations));
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800159}
160
161size_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()),
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700170 std::make_move_iterator(frames.end()));
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800171 if (mFrames.size() > MAX_QUEUE_SIZE) {
172 ALOGE("More than %zu frames have been accumulated. Dropping %zu frames", MAX_QUEUE_SIZE,
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700173 mFrames.size() - MAX_QUEUE_SIZE);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800174 mFrames.erase(mFrames.begin(), mFrames.end() - MAX_QUEUE_SIZE);
175 }
176 return numFrames;
177}
178
179std::vector<TouchVideoFrame> TouchVideoDevice::consumeFrames() {
180 std::vector<TouchVideoFrame> frames = std::move(mFrames);
181 mFrames = {};
182 return frames;
183}
184
185std::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
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700200 ALOGW("The timestamp %ld.%ld was not acquired using CLOCK_MONOTONIC", buf.timestamp.tv_sec,
201 buf.timestamp.tv_usec);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800202 }
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600203 std::vector<int16_t> data(mHeight * mWidth);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800204 const int16_t* readFrom = mReadLocations[buf.index];
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600205 std::copy(readFrom, readFrom + mHeight * mWidth, data.begin());
206 TouchVideoFrame frame(mHeight, mWidth, std::move(data), buf.timestamp);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800207
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 */
220std::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
232TouchVideoDevice::~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));
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700240 result = munmap(bufferAddress, mHeight * mWidth * sizeof(int16_t));
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800241 if (result == -1) {
242 ALOGE("%s: Couldn't unmap: [%s]", __func__, strerror(errno));
243 }
244 }
245}
246
247std::string TouchVideoDevice::dump() const {
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600248 return StringPrintf("Video device %s (%s) : height=%" PRIu32 ", width=%" PRIu32
Prabir Pradhanda7c00c2019-08-29 14:12:42 -0700249 ", fd=%i, hasValidFd=%s",
250 mName.c_str(), mPath.c_str(), mHeight, mWidth, mFd.get(),
251 hasValidFd() ? "true" : "false");
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800252}
253
254} // namespace android