42 gst_device_monitor_add_filter(m_deviceMonitor.get(),
"Video/Source",
nullptr);
43 gst_device_monitor_set_show_all_devices(m_deviceMonitor.get(),
true);
45 m_busObserver.installMessageFilter(
this);
46 gst_device_monitor_start(m_deviceMonitor.get());
48 GList *devices = gst_device_monitor_get_devices(m_deviceMonitor.get());
50 for (GstDevice *device : QGstUtils::GListRangeAdaptor<GstDevice *>(devices)) {
51 addDevice(QGstDeviceHandle{
53 QGstDeviceHandle::HasRef,
67 QList<QCameraDevice> devices;
69 for (
const auto &device : m_videoSources) {
70 QCameraDevicePrivate *info =
new QCameraDevicePrivate;
73 gst_device_get_display_name(device.gstDevice.get()),
75 info->description = desc.toQString();
78 QUniqueGstStructureHandle properties{
79 gst_device_get_properties(device.gstDevice.get()),
82 QGstStructureView view{ properties };
83 auto def = view[
"is-default"].toBool();
84 info->isDefault = def && *def;
88 devices.prepend(info->create());
90 devices.append(info->create());
92 auto caps = QGstCaps(gst_device_get_caps(device.gstDevice.get()), QGstCaps::HasRef);
94 QList<QCameraFormat> formats;
95 QSet<QSize> photoResolutions;
97 int size = caps.size();
98 for (
int i = 0; i < size; ++i) {
99 auto cap = caps.at(i);
100 QList<QVideoFrameFormat::PixelFormat> pixelFormats = cap.pixelFormats();
102 auto frameRate = cap.frameRateRange();
104 if (pixelFormats.isEmpty()) {
105 qCDebug(ltVideoDevices) <<
"pixel format(s) not supported:" << cap;
109 auto addFormatForResolution = [&](QSize resolution) {
110 for (QVideoFrameFormat::PixelFormat pixelFormat : std::as_const(pixelFormats)){
111 auto *f =
new QCameraFormatPrivate{
112 QSharedData(), pixelFormat, resolution, frameRate.min, frameRate.max,
114 formats.append(f->create());
116 photoResolutions.insert(resolution);
119 std::optional<QGRange<QSize>> resolutionRange = cap.resolutionRange();
120 if (resolutionRange) {
121 addFormatForResolution(resolutionRange->min);
122 addFormatForResolution(resolutionRange->max);
124 QSize resolution = cap.resolution();
125 if (resolution.isValid())
126 addFormatForResolution(resolution);
129 info->videoFormats = formats;
131 info->photoResolutions = photoResolutions.values();
137void QGstreamerVideoDevices::
addDevice(QGstDeviceHandle device)
139 Q_ASSERT(gst_device_has_classes(device.get(),
"Video/Source"));
141#if QT_CONFIG(linux_v4l)
142 QUniqueGstStructureHandle propertiesHandle{
143 gst_device_get_properties(device.get()),
145 if (!propertiesHandle.isValid()) {
146 qCDebug(ltVideoDevices) <<
"Skipping device without extra properties:" << device.get();
150 auto properties = QGstStructureView(propertiesHandle.get());
154 if (properties.name().contains(
"pipewire")) {
155 qCDebug(ltVideoDevices) <<
"Skipping pipewire device:" << device.get();
160 static const bool skipImxCapture = qEnvironmentVariableIsSet(
"QT_GSTREAMER_SKIP_IMXCAPTURE");
161 if (skipImxCapture) {
162 const char *name = properties[
"device.product.name"].toString();
163 if (name && std::strstr(name,
"imx-capture")) {
164 qWarning() << Q_FUNC_INFO <<
"Skipping video device with product name" << name;
169 const auto *p = properties[
"device.path"].toString();
171 QUniqueFileDescriptorHandle fd{
172 qt_safe_open(p, O_RDONLY),
176 qCDebug(ltVideoDevices) <<
"Cannot open v4l2 device:" << p;
180 struct v4l2_capability cap;
181 if (::ioctl(fd.get(), VIDIOC_QUERYCAP, &cap) < 0) {
182 qCWarning(ltVideoDevices)
183 <<
"ioctl failed: VIDIOC_QUERYCAP" << qt_error_string(errno) << p;
187 if (cap.device_caps & V4L2_CAP_META_CAPTURE) {
188 qCDebug(ltVideoDevices) <<
"V4L2_CAP_META_CAPTURE device detected" << p;
192 constexpr uint32_t videoCaptureCapabilities =
193 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
195 if (!(cap.capabilities & videoCaptureCapabilities)) {
196 qCDebug(ltVideoDevices)
197 <<
"not a V4L2_CAP_VIDEO_CAPTURE or V4L2_CAP_VIDEO_CAPTURE_MPLANE device" << p;
200 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
201 qCDebug(ltVideoDevices) <<
"not a V4L2_CAP_STREAMING device" << p;
206 if (::ioctl(fd.get(), VIDIOC_G_INPUT, &index) < 0) {
209 qCDebug(ltVideoDevices) <<
"Device does not support VIDIOC_G_INPUT, but it could"
214 qCWarning(ltVideoDevices)
215 <<
"ioctl failed: VIDIOC_G_INPUT" << qt_error_string(errno) << p;
220 qCDebug(ltVideoDevices) <<
"Video device not a v4l2 device:" << propertiesHandle;
224 auto it = ranges::find_if(m_videoSources, [&](
const QGstRecordDevice &a) {
225 return a.gstDevice == device;
228 if (it != m_videoSources.end())
231 m_videoSources.push_back(QGstRecordDevice{
233 QByteArray::number(m_idGenerator),
238 onVideoInputsChanged();
255 QGstDeviceHandle device;
257 switch (message.type()) {
258 case GST_MESSAGE_DEVICE_ADDED:
259 gst_message_parse_device_added(message.message(), &device);
260 addDevice(std::move(device));
262 case GST_MESSAGE_DEVICE_REMOVED:
263 gst_message_parse_device_removed(message.message(), &device);
264 removeDevice(std::move(device));