Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgstreamervideodevices.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 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
5
6#include <QtMultimedia/qmediadevices.h>
7#include <QtMultimedia/private/qcameradevice_p.h>
8#include <QtCore/qloggingcategory.h>
9#include <QtCore/qset.h>
10#include <QtCore/private/quniquehandle_types_p.h>
11
12#include <common/qgst_p.h>
13#include <common/qgst_debug_p.h>
14#include <common/qgstutils_p.h>
15#include <common/qglist_helper_p.h>
16
17#if QT_CONFIG(linux_v4l)
18# include <linux/videodev2.h>
19# include <errno.h>
20#endif
21
23
24Q_STATIC_LOGGING_CATEGORY(ltVideoDevices, "qt.multimedia.gstreamer.videodevices");
25
26QGstreamerVideoDevices::QGstreamerVideoDevices(QPlatformMediaIntegration *integration)
31 },
36 },
37 }
38{
39 gst_device_monitor_add_filter(m_deviceMonitor.get(), "Video/Source", nullptr);
40 gst_device_monitor_set_show_all_devices(m_deviceMonitor.get(), true);
41
42 m_busObserver.installMessageFilter(this);
43 gst_device_monitor_start(m_deviceMonitor.get());
44
45 GList *devices = gst_device_monitor_get_devices(m_deviceMonitor.get());
46
47 for (GstDevice *device : QGstUtils::GListRangeAdaptor<GstDevice *>(devices)) {
48 addDevice(QGstDeviceHandle{
49 device,
50 QGstDeviceHandle::HasRef,
51 });
52 }
53
54 g_list_free(devices);
55}
56
57QGstreamerVideoDevices::~QGstreamerVideoDevices()
58{
59 gst_device_monitor_stop(m_deviceMonitor.get());
60}
61
62QList<QCameraDevice> QGstreamerVideoDevices::findVideoInputs() const
63{
64 QList<QCameraDevice> devices;
65
66 for (const auto &device : m_videoSources) {
67 QCameraDevicePrivate *info = new QCameraDevicePrivate;
68
69 QGString desc{
70 gst_device_get_display_name(device.gstDevice.get()),
71 };
72 info->description = desc.toQString();
73 info->id = device.id;
74
75 QUniqueGstStructureHandle properties{
76 gst_device_get_properties(device.gstDevice.get()),
77 };
78 if (properties) {
79 QGstStructureView view{ properties };
80 auto def = view["is-default"].toBool();
81 info->isDefault = def && *def;
82 }
83
84 if (info->isDefault)
85 devices.prepend(info->create());
86 else
87 devices.append(info->create());
88
89 auto caps = QGstCaps(gst_device_get_caps(device.gstDevice.get()), QGstCaps::HasRef);
90 if (caps) {
91 QList<QCameraFormat> formats;
92 QSet<QSize> photoResolutions;
93
94 int size = caps.size();
95 for (int i = 0; i < size; ++i) {
96 auto cap = caps.at(i);
97 auto pixelFormat = cap.pixelFormat();
98 auto frameRate = cap.frameRateRange();
99
100 if (pixelFormat == QVideoFrameFormat::PixelFormat::Format_Invalid) {
101 qCDebug(ltVideoDevices) << "pixel format not supported:" << cap;
102 continue; // skip pixel formats that we don't support
103 }
104
105 auto addFormatForResolution = [&](QSize resolution) {
106 auto *f = new QCameraFormatPrivate{
107 QSharedData(), pixelFormat, resolution, frameRate.min, frameRate.max,
108 };
109 formats.append(f->create());
110 photoResolutions.insert(resolution);
111 };
112
113 std::optional<QGRange<QSize>> resolutionRange = cap.resolutionRange();
114 if (resolutionRange) {
115 addFormatForResolution(resolutionRange->min);
116 addFormatForResolution(resolutionRange->max);
117 } else {
118 QSize resolution = cap.resolution();
119 if (resolution.isValid())
120 addFormatForResolution(resolution);
121 }
122 }
123 info->videoFormats = formats;
124 // ### sort resolutions?
125 info->photoResolutions = photoResolutions.values();
126 }
127 }
128 return devices;
129}
130
131void QGstreamerVideoDevices::addDevice(QGstDeviceHandle device)
132{
133 Q_ASSERT(gst_device_has_classes(device.get(), "Video/Source"));
134
135#if QT_CONFIG(linux_v4l)
136 QUniqueGstStructureHandle propertiesHandle{
137 gst_device_get_properties(device.get()),
138 };
139 if (!propertiesHandle.isValid()) {
140 qCDebug(ltVideoDevices) << "Skipping device without extra properties:" << device.get();
141 return;
142 }
143
144 auto properties = QGstStructureView(propertiesHandle.get());
145
146 // Pipewire devices causes infinite futex wait in gst_pipewire_src_change_state after calling
147 // QGstreamerMediaCaptureSession::setCameraActive() with true, so we skip adding them:
148 if (properties.name().contains("pipewire")) {
149 qCDebug(ltVideoDevices) << "Skipping pipewire device:" << device.get();
150 return;
151 }
152
153 // QTBUG-140092: NXP's CSI video device "imx-capture" may fail. Can be skipped via env var:
154 static const bool skipImxCapture = qEnvironmentVariableIsSet("QT_GSTREAMER_SKIP_IMXCAPTURE");
155 if (skipImxCapture) {
156 const char *name = properties["device.product.name"].toString();
157 if (name && std::strstr(name, "imx-capture")) {
158 qWarning() << Q_FUNC_INFO << "Skipping video device with product name" << name;
159 return;
160 }
161 }
162
163 const auto *p = properties["device.path"].toString();
164 if (p) {
165 QUniqueFileDescriptorHandle fd{
166 qt_safe_open(p, O_RDONLY),
167 };
168
169 if (!fd) {
170 qCDebug(ltVideoDevices) << "Cannot open v4l2 device:" << p;
171 return;
172 }
173
174 struct v4l2_capability cap;
175 if (::ioctl(fd.get(), VIDIOC_QUERYCAP, &cap) < 0) {
176 qCWarning(ltVideoDevices)
177 << "ioctl failed: VIDIOC_QUERYCAP" << qt_error_string(errno) << p;
178 return;
179 }
180
181 if (cap.device_caps & V4L2_CAP_META_CAPTURE) {
182 qCDebug(ltVideoDevices) << "V4L2_CAP_META_CAPTURE device detected" << p;
183 return;
184 }
185
186 constexpr uint32_t videoCaptureCapabilities =
187 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
188
189 if (!(cap.capabilities & videoCaptureCapabilities)) {
190 qCDebug(ltVideoDevices)
191 << "not a V4L2_CAP_VIDEO_CAPTURE or V4L2_CAP_VIDEO_CAPTURE_MPLANE device" << p;
192 return;
193 }
194 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
195 qCDebug(ltVideoDevices) << "not a V4L2_CAP_STREAMING device" << p;
196 return;
197 }
198
199 int index;
200 if (::ioctl(fd.get(), VIDIOC_G_INPUT, &index) < 0) {
201 switch (errno) {
202 case ENOTTY:
203 qCDebug(ltVideoDevices) << "Device does not support VIDIOC_G_INPUT, but it could"
204 "still work" << p;
205 break;
206
207 default:
208 qCWarning(ltVideoDevices)
209 << "ioctl failed: VIDIOC_G_INPUT" << qt_error_string(errno) << p;
210 return;
211 }
212 }
213 } else {
214 qCDebug(ltVideoDevices) << "Video device not a v4l2 device:" << propertiesHandle;
215 }
216#endif
217
218 auto it = std::find_if(m_videoSources.begin(), m_videoSources.end(),
219 [&](const QGstRecordDevice &a) { return a.gstDevice == device; });
220
221 if (it != m_videoSources.end())
222 return;
223
224 m_videoSources.push_back(QGstRecordDevice{
225 std::move(device),
226 QByteArray::number(m_idGenerator),
227 });
228
229 m_idGenerator++;
230
231 onVideoInputsChanged();
232}
233
234void QGstreamerVideoDevices::removeDevice(QGstDeviceHandle device)
235{
236 auto it = std::find_if(m_videoSources.begin(), m_videoSources.end(),
237 [&](const QGstRecordDevice &a) { return a.gstDevice == device; });
238
239 if (it != m_videoSources.end()) {
240 m_videoSources.erase(it);
241 onVideoInputsChanged();
242 }
243}
244
245bool QGstreamerVideoDevices::processBusMessage(const QGstreamerMessage &message)
246{
247 QGstDeviceHandle device;
248
249 switch (message.type()) {
250 case GST_MESSAGE_DEVICE_ADDED:
251 gst_message_parse_device_added(message.message(), &device);
252 addDevice(std::move(device));
253 break;
254 case GST_MESSAGE_DEVICE_REMOVED:
255 gst_message_parse_device_removed(message.message(), &device);
256 removeDevice(std::move(device));
257 break;
258 default:
259 break;
260 }
261
262 return false;
263}
264
265GstDevice *QGstreamerVideoDevices::videoDevice(const QByteArray &id) const
266{
267 auto it = std::find_if(m_videoSources.begin(), m_videoSources.end(),
268 [&](const QGstRecordDevice &a) { return a.id == id; });
269 return it != m_videoSources.end() ? it->gstDevice.get() : nullptr;
270}
271
272QT_END_NAMESPACE
GstDevice * videoDevice(const QByteArray &id) const
bool processBusMessage(const QGstreamerMessage &message) override
QGstreamerVideoDevices(QPlatformMediaIntegration *integration)
void removeDevice(QGstDeviceHandle)
void addDevice(QGstDeviceHandle)
QList< QCameraDevice > findVideoInputs() const override
Combined button and popup list for selecting options.