• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         // A user-space grip suppression process may be processing the video frames, and holding
173         // back the input events. This could result in video frames being produced without the
174         // matching input events. Drop the oldest frame here to prepare for the next input event.
175         mFrames.erase(mFrames.begin(), mFrames.end() - MAX_QUEUE_SIZE);
176     }
177     return numFrames;
178 }
179 
consumeFrames()180 std::vector<TouchVideoFrame> TouchVideoDevice::consumeFrames() {
181     std::vector<TouchVideoFrame> frames = std::move(mFrames);
182     mFrames = {};
183     return frames;
184 }
185 
readFrame()186 std::optional<TouchVideoFrame> TouchVideoDevice::readFrame() {
187     struct v4l2_buffer buf = {};
188     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
189     buf.memory = V4L2_MEMORY_MMAP;
190     int result = ioctl(mFd.get(), VIDIOC_DQBUF, &buf);
191     if (result == -1) {
192         // EAGAIN means we've reached the end of the read buffer, so it's expected.
193         if (errno != EAGAIN) {
194             ALOGE("VIDIOC_DQBUF failed: %s", strerror(errno));
195         }
196         return std::nullopt;
197     }
198     if ((buf.flags & V4L2_BUF_FLAG_TIMESTAMP_MASK) != V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC) {
199         // We use CLOCK_MONOTONIC for input events, so if the clocks don't match,
200         // we can't compare timestamps. Just log a warning, since this is a driver issue
201         ALOGW("The timestamp %ld.%ld was not acquired using CLOCK_MONOTONIC", buf.timestamp.tv_sec,
202               buf.timestamp.tv_usec);
203     }
204     std::vector<int16_t> data(mHeight * mWidth);
205     const int16_t* readFrom = mReadLocations[buf.index];
206     std::copy(readFrom, readFrom + mHeight * mWidth, data.begin());
207     TouchVideoFrame frame(mHeight, mWidth, std::move(data), buf.timestamp);
208 
209     result = ioctl(mFd.get(), VIDIOC_QBUF, &buf);
210     if (result == -1) {
211         ALOGE("VIDIOC_QBUF failed: %s", strerror(errno));
212     }
213     return std::make_optional(std::move(frame));
214 }
215 
216 /*
217  * This function should not be called unless buffer is ready! This must be checked with
218  * select, poll, epoll, or some other similar api first.
219  * The oldest frame will be at the beginning of the array.
220  */
readFrames()221 std::vector<TouchVideoFrame> TouchVideoDevice::readFrames() {
222     std::vector<TouchVideoFrame> frames;
223     while (true) {
224         std::optional<TouchVideoFrame> frame = readFrame();
225         if (!frame) {
226             break;
227         }
228         frames.push_back(std::move(*frame));
229     }
230     return frames;
231 }
232 
~TouchVideoDevice()233 TouchVideoDevice::~TouchVideoDevice() {
234     enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
235     int result = ioctl(mFd.get(), VIDIOC_STREAMOFF, &type);
236     if (result == -1) {
237         ALOGE("VIDIOC_STREAMOFF failed: %s", strerror(errno));
238     }
239     for (const int16_t* buffer : mReadLocations) {
240         void* bufferAddress = static_cast<void*>(const_cast<int16_t*>(buffer));
241         result = munmap(bufferAddress, mHeight * mWidth * sizeof(int16_t));
242         if (result == -1) {
243             ALOGE("%s: Couldn't unmap: [%s]", __func__, strerror(errno));
244         }
245     }
246 }
247 
dump() const248 std::string TouchVideoDevice::dump() const {
249     return StringPrintf("Video device %s (%s) : height=%" PRIu32 ", width=%" PRIu32
250                         ", fd=%i, hasValidFd=%s",
251                         mName.c_str(), mPath.c_str(), mHeight, mWidth, mFd.get(),
252                         hasValidFd() ? "true" : "false");
253 }
254 
255 } // namespace android
256