blob: 76df3a184516b95c430d156d0f06c249b7c974ac [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,
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060040 uint32_t height, uint32_t width,
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080041 const std::array<const int16_t*, NUM_BUFFERS>& readLocations) :
42 mFd(fd), mName(std::move(name)), mPath(std::move(devicePath)),
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060043 mHeight(height), mWidth(width),
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080044 mReadLocations(readLocations) {
45 mFrames.reserve(MAX_QUEUE_SIZE);
46};
47
48std::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 result = ioctl(fd.get(), VIDIOC_ENUMINPUT, &v4l2_input_struct);
72 if (result == -1) {
73 ALOGE("VIDIOC_ENUMINPUT failed: %s", strerror(errno));
74 return nullptr;
75 }
76
77 if (v4l2_input_struct.type != V4L2_INPUT_TYPE_TOUCH) {
78 ALOGE("Video device does not provide touch data. "
79 "Make sure device specifies V4L2_INPUT_TYPE_TOUCH.");
80 return nullptr;
81 }
82
83 struct v4l2_format v4l2_fmt;
84 v4l2_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
85 result = ioctl(fd.get(), VIDIOC_G_FMT, &v4l2_fmt);
86 if (result == -1) {
87 ALOGE("VIDIOC_G_FMT failed: %s", strerror(errno));
88 return nullptr;
89 }
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080090 const uint32_t height = v4l2_fmt.fmt.pix.height;
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -060091 const uint32_t width = v4l2_fmt.fmt.pix.width;
92 ALOGI("Frame dimensions: height = %" PRIu32 " width = %" PRIu32, height, width);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -080093
94 struct v4l2_requestbuffers req;
95 req.count = NUM_BUFFERS;
96 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
97 req.memory = V4L2_MEMORY_MMAP;
98 result = ioctl(fd.get(), VIDIOC_REQBUFS, &req);
99 if (result == -1) {
100 ALOGE("VIDIOC_REQBUFS failed: %s", strerror(errno));
101 return nullptr;
102 }
103 if (req.count != NUM_BUFFERS) {
104 ALOGE("Requested %zu buffers, but driver responded with count=%i", NUM_BUFFERS, req.count);
105 return nullptr;
106 }
107
108 struct v4l2_buffer buf = {};
109 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
110 buf.memory = V4L2_MEMORY_MMAP;
111 std::array<const int16_t*, NUM_BUFFERS> readLocations;
112 for (size_t i = 0; i < NUM_BUFFERS; i++) {
113 buf.index = i;
114 result = ioctl(fd.get(), VIDIOC_QUERYBUF, &buf);
115 if (result == -1) {
116 ALOGE("VIDIOC_QUERYBUF failed: %s", strerror(errno));
117 return nullptr;
118 }
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600119 if (buf.length != height * width * sizeof(int16_t)) {
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800120 ALOGE("Unexpected value of buf.length = %i (offset = %" PRIu32 ")",
121 buf.length, buf.m.offset);
122 return nullptr;
123 }
124
125 readLocations[i] = static_cast<const int16_t*>(mmap(nullptr /* start anywhere */,
126 buf.length, PROT_READ /* required */, MAP_SHARED /* recommended */,
127 fd.get(), buf.m.offset));
128 if (readLocations[i] == MAP_FAILED) {
129 ALOGE("%s: map failed: %s", __func__, strerror(errno));
130 return nullptr;
131 }
132 }
133
134 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
135 result = ioctl(fd.get(), VIDIOC_STREAMON, &type);
136 if (result == -1) {
137 ALOGE("VIDIOC_STREAMON failed: %s", strerror(errno));
138 return nullptr;
139 }
140
141 for (size_t i = 0; i < NUM_BUFFERS; i++) {
142 buf.index = i;
143 result = ioctl(fd.get(), VIDIOC_QBUF, &buf);
144 if (result == -1) {
145 ALOGE("VIDIOC_QBUF failed for buffer %zu: %s", i, strerror(errno));
146 return nullptr;
147 }
148 }
149 // Using 'new' to access a non-public constructor.
150 return std::unique_ptr<TouchVideoDevice>(new TouchVideoDevice(
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600151 fd.release(), std::move(name), std::move(devicePath), height, width, readLocations));
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800152}
153
154size_t TouchVideoDevice::readAndQueueFrames() {
155 std::vector<TouchVideoFrame> frames = readFrames();
156 const size_t numFrames = frames.size();
157 if (numFrames == 0) {
158 // Likely an error occurred
159 return 0;
160 }
161 // Concatenate the vectors, then clip up to maximum size allowed
162 mFrames.insert(mFrames.end(), std::make_move_iterator(frames.begin()),
163 std::make_move_iterator(frames.end()));
164 if (mFrames.size() > MAX_QUEUE_SIZE) {
165 ALOGE("More than %zu frames have been accumulated. Dropping %zu frames", MAX_QUEUE_SIZE,
166 mFrames.size() - MAX_QUEUE_SIZE);
167 mFrames.erase(mFrames.begin(), mFrames.end() - MAX_QUEUE_SIZE);
168 }
169 return numFrames;
170}
171
172std::vector<TouchVideoFrame> TouchVideoDevice::consumeFrames() {
173 std::vector<TouchVideoFrame> frames = std::move(mFrames);
174 mFrames = {};
175 return frames;
176}
177
178std::optional<TouchVideoFrame> TouchVideoDevice::readFrame() {
179 struct v4l2_buffer buf = {};
180 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
181 buf.memory = V4L2_MEMORY_MMAP;
182 int result = ioctl(mFd.get(), VIDIOC_DQBUF, &buf);
183 if (result == -1) {
184 // EAGAIN means we've reached the end of the read buffer, so it's expected.
185 if (errno != EAGAIN) {
186 ALOGE("VIDIOC_DQBUF failed: %s", strerror(errno));
187 }
188 return std::nullopt;
189 }
190 if ((buf.flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) != V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC) {
191 // We use CLOCK_MONOTONIC for input events, so if the clocks don't match,
192 // we can't compare timestamps. Just log a warning, since this is a driver issue
193 ALOGW("The timestamp %ld.%ld was not acquired using CLOCK_MONOTONIC",
194 buf.timestamp.tv_sec, buf.timestamp.tv_usec);
195 }
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600196 std::vector<int16_t> data(mHeight * mWidth);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800197 const int16_t* readFrom = mReadLocations[buf.index];
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600198 std::copy(readFrom, readFrom + mHeight * mWidth, data.begin());
199 TouchVideoFrame frame(mHeight, mWidth, std::move(data), buf.timestamp);
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800200
201 result = ioctl(mFd.get(), VIDIOC_QBUF, &buf);
202 if (result == -1) {
203 ALOGE("VIDIOC_QBUF failed: %s", strerror(errno));
204 }
205 return std::make_optional(std::move(frame));
206}
207
208/*
209 * This function should not be called unless buffer is ready! This must be checked with
210 * select, poll, epoll, or some other similar api first.
211 * The oldest frame will be at the beginning of the array.
212 */
213std::vector<TouchVideoFrame> TouchVideoDevice::readFrames() {
214 std::vector<TouchVideoFrame> frames;
215 while (true) {
216 std::optional<TouchVideoFrame> frame = readFrame();
217 if (!frame) {
218 break;
219 }
220 frames.push_back(std::move(*frame));
221 }
222 return frames;
223}
224
225TouchVideoDevice::~TouchVideoDevice() {
226 enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
227 int result = ioctl(mFd.get(), VIDIOC_STREAMOFF, &type);
228 if (result == -1) {
229 ALOGE("VIDIOC_STREAMOFF failed: %s", strerror(errno));
230 }
231 for (const int16_t* buffer : mReadLocations) {
232 void* bufferAddress = static_cast<void*>(const_cast<int16_t*>(buffer));
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600233 result = munmap(bufferAddress, mHeight * mWidth * sizeof(int16_t));
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800234 if (result == -1) {
235 ALOGE("%s: Couldn't unmap: [%s]", __func__, strerror(errno));
236 }
237 }
238}
239
240std::string TouchVideoDevice::dump() const {
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600241 return StringPrintf("Video device %s (%s) : height=%" PRIu32 ", width=%" PRIu32
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800242 ", fd=%i, hasValidFd=%s",
Siarhei Vishniakoufda3aee2019-02-15 17:10:53 -0600243 mName.c_str(), mPath.c_str(), mHeight, mWidth, mFd.get(),
Siarhei Vishniakou22c88462018-12-13 19:34:53 -0800244 hasValidFd() ? "true" : "false");
245}
246
247} // namespace android