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 const auto *p = properties["device.path"].toString();
154 if (p) {
155 QUniqueFileDescriptorHandle fd{
156 qt_safe_open(p, O_RDONLY),
157 };
158
159 if (!fd) {
160 qCDebug(ltVideoDevices) << "Cannot open v4l2 device:" << p;
161 return;
162 }
163
164 struct v4l2_capability cap;
165 if (::ioctl(fd.get(), VIDIOC_QUERYCAP, &cap) < 0) {
166 qCWarning(ltVideoDevices)
167 << "ioctl failed: VIDIOC_QUERYCAP" << qt_error_string(errno) << p;
168 return;
169 }
170
171 if (cap.device_caps & V4L2_CAP_META_CAPTURE) {
172 qCDebug(ltVideoDevices) << "V4L2_CAP_META_CAPTURE device detected" << p;
173 return;
174 }
175
176 constexpr uint32_t videoCaptureCapabilities =
177 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
178
179 if (!(cap.capabilities & videoCaptureCapabilities)) {
180 qCDebug(ltVideoDevices)
181 << "not a V4L2_CAP_VIDEO_CAPTURE or V4L2_CAP_VIDEO_CAPTURE_MPLANE device" << p;
182 return;
183 }
184 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
185 qCDebug(ltVideoDevices) << "not a V4L2_CAP_STREAMING device" << p;
186 return;
187 }
188
189 int index;
190 if (::ioctl(fd.get(), VIDIOC_G_INPUT, &index) < 0) {
191 switch (errno) {
192 case ENOTTY:
193 qCDebug(ltVideoDevices) << "Device does not support VIDIOC_G_INPUT, but it could"
194 "still work" << p;
195 break;
196
197 default:
198 qCWarning(ltVideoDevices)
199 << "ioctl failed: VIDIOC_G_INPUT" << qt_error_string(errno) << p;
200 return;
201 }
202 }
203 } else {
204 qCDebug(ltVideoDevices) << "Video device not a v4l2 device:" << propertiesHandle;
205 }
206#endif
207
208 auto it = std::find_if(m_videoSources.begin(), m_videoSources.end(),
209 [&](const QGstRecordDevice &a) { return a.gstDevice == device; });
210
211 if (it != m_videoSources.end())
212 return;
213
214 m_videoSources.push_back(QGstRecordDevice{
215 std::move(device),
216 QByteArray::number(m_idGenerator),
217 });
218
219 m_idGenerator++;
220
221 onVideoInputsChanged();
222}
223
224void QGstreamerVideoDevices::removeDevice(QGstDeviceHandle device)
225{
226 auto it = std::find_if(m_videoSources.begin(), m_videoSources.end(),
227 [&](const QGstRecordDevice &a) { return a.gstDevice == device; });
228
229 if (it != m_videoSources.end()) {
230 m_videoSources.erase(it);
231 onVideoInputsChanged();
232 }
233}
234
235bool QGstreamerVideoDevices::processBusMessage(const QGstreamerMessage &message)
236{
237 QGstDeviceHandle device;
238
239 switch (message.type()) {
240 case GST_MESSAGE_DEVICE_ADDED:
241 gst_message_parse_device_added(message.message(), &device);
242 addDevice(std::move(device));
243 break;
244 case GST_MESSAGE_DEVICE_REMOVED:
245 gst_message_parse_device_removed(message.message(), &device);
246 removeDevice(std::move(device));
247 break;
248 default:
249 break;
250 }
251
252 return false;
253}
254
255GstDevice *QGstreamerVideoDevices::videoDevice(const QByteArray &id) const
256{
257 auto it = std::find_if(m_videoSources.begin(), m_videoSources.end(),
258 [&](const QGstRecordDevice &a) { return a.id == id; });
259 return it != m_videoSources.end() ? it->gstDevice.get() : nullptr;
260}
261
262QT_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.