| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qv4l2filedescriptor_p.h" |
| 5 | |
| 6 | #include <sys/ioctl.h> |
| 7 | #include <private/qcore_unix_p.h> |
| 8 | |
| 9 | #include <linux/videodev2.h> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | int xioctl(int fd, int request, void *arg) |
| 14 | { |
| 15 | int res; |
| 16 | |
| 17 | do { |
| 18 | res = ::ioctl(fd: fd, request: request, arg); |
| 19 | } while (res == -1 && EINTR == errno); |
| 20 | |
| 21 | return res; |
| 22 | } |
| 23 | |
| 24 | QV4L2FileDescriptor::QV4L2FileDescriptor(int descriptor) : m_descriptor(descriptor) |
| 25 | { |
| 26 | Q_ASSERT(descriptor >= 0); |
| 27 | } |
| 28 | |
| 29 | QV4L2FileDescriptor::~QV4L2FileDescriptor() |
| 30 | { |
| 31 | qt_safe_close(fd: m_descriptor); |
| 32 | } |
| 33 | |
| 34 | bool QV4L2FileDescriptor::call(int request, void *arg) const |
| 35 | { |
| 36 | return ::xioctl(fd: m_descriptor, request, arg) >= 0; |
| 37 | } |
| 38 | |
| 39 | bool QV4L2FileDescriptor::requestBuffers(quint32 memoryType, quint32 &buffersCount) const |
| 40 | { |
| 41 | v4l2_requestbuffers req = {}; |
| 42 | req.count = buffersCount; |
| 43 | req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 44 | req.memory = memoryType; |
| 45 | |
| 46 | if (!call(VIDIOC_REQBUFS, arg: &req)) |
| 47 | return false; |
| 48 | |
| 49 | buffersCount = req.count; |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | bool QV4L2FileDescriptor::startStream() |
| 54 | { |
| 55 | int type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 56 | if (!call(VIDIOC_STREAMON, arg: &type)) |
| 57 | return false; |
| 58 | |
| 59 | m_streamStarted = true; |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool QV4L2FileDescriptor::stopStream() |
| 64 | { |
| 65 | int type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 66 | auto result = call(VIDIOC_STREAMOFF, arg: &type); |
| 67 | m_streamStarted = false; |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | QT_END_NAMESPACE |
| 72 | |