39 gst_device_monitor_add_filter(m_deviceMonitor.get(),
"Video/Source",
nullptr);
40 gst_device_monitor_set_show_all_devices(m_deviceMonitor.get(),
true);
42 m_busObserver.installMessageFilter(
this);
43 gst_device_monitor_start(m_deviceMonitor.get());
45 GList *devices = gst_device_monitor_get_devices(m_deviceMonitor.get());
47 for (GstDevice *device : QGstUtils::GListRangeAdaptor<GstDevice *>(devices)) {
48 addDevice(QGstDeviceHandle{
50 QGstDeviceHandle::HasRef,
64 QList<QCameraDevice> devices;
66 for (
const auto &device : m_videoSources) {
67 QCameraDevicePrivate *info =
new QCameraDevicePrivate;
70 gst_device_get_display_name(device.gstDevice.get()),
72 info->description = desc.toQString();
75 QUniqueGstStructureHandle properties{
76 gst_device_get_properties(device.gstDevice.get()),
79 QGstStructureView view{ properties };
80 auto def = view[
"is-default"].toBool();
81 info->isDefault = def && *def;
85 devices.prepend(info->create());
87 devices.append(info->create());
89 auto caps = QGstCaps(gst_device_get_caps(device.gstDevice.get()), QGstCaps::HasRef);
91 QList<QCameraFormat> formats;
92 QSet<QSize> photoResolutions;
94 int size = caps.size();
95 for (
int i = 0; i < size; ++i) {
96 auto cap = caps.at(i);
97 QList<QVideoFrameFormat::PixelFormat> pixelFormats = cap.pixelFormats();
99 auto frameRate = cap.frameRateRange();
101 if (pixelFormats.isEmpty()) {
102 qCDebug(ltVideoDevices) <<
"pixel format(s) not supported:" << cap;
106 auto addFormatForResolution = [&](QSize resolution) {
107 for (QVideoFrameFormat::PixelFormat pixelFormat : std::as_const(pixelFormats)){
108 auto *f =
new QCameraFormatPrivate{
109 QSharedData(), pixelFormat, resolution, frameRate.min, frameRate.max,
111 formats.append(f->create());
113 photoResolutions.insert(resolution);
116 std::optional<QGRange<QSize>> resolutionRange = cap.resolutionRange();
117 if (resolutionRange) {
118 addFormatForResolution(resolutionRange->min);
119 addFormatForResolution(resolutionRange->max);
121 QSize resolution = cap.resolution();
122 if (resolution.isValid())
123 addFormatForResolution(resolution);
126 info->videoFormats = formats;
128 info->photoResolutions = photoResolutions.values();
134void QGstreamerVideoDevices::
addDevice(QGstDeviceHandle device)
136 Q_ASSERT(gst_device_has_classes(device.get(),
"Video/Source"));
138#if QT_CONFIG(linux_v4l)
139 QUniqueGstStructureHandle propertiesHandle{
140 gst_device_get_properties(device.get()),
142 if (!propertiesHandle.isValid()) {
143 qCDebug(ltVideoDevices) <<
"Skipping device without extra properties:" << device.get();
147 auto properties = QGstStructureView(propertiesHandle.get());
151 if (properties.name().contains(
"pipewire")) {
152 qCDebug(ltVideoDevices) <<
"Skipping pipewire device:" << device.get();
157 static const bool skipImxCapture = qEnvironmentVariableIsSet(
"QT_GSTREAMER_SKIP_IMXCAPTURE");
158 if (skipImxCapture) {
159 const char *name = properties[
"device.product.name"].toString();
160 if (name && std::strstr(name,
"imx-capture")) {
161 qWarning() << Q_FUNC_INFO <<
"Skipping video device with product name" << name;
166 const auto *p = properties[
"device.path"].toString();
168 QUniqueFileDescriptorHandle fd{
169 qt_safe_open(p, O_RDONLY),
173 qCDebug(ltVideoDevices) <<
"Cannot open v4l2 device:" << p;
177 struct v4l2_capability cap;
178 if (::ioctl(fd.get(), VIDIOC_QUERYCAP, &cap) < 0) {
179 qCWarning(ltVideoDevices)
180 <<
"ioctl failed: VIDIOC_QUERYCAP" << qt_error_string(errno) << p;
184 if (cap.device_caps & V4L2_CAP_META_CAPTURE) {
185 qCDebug(ltVideoDevices) <<
"V4L2_CAP_META_CAPTURE device detected" << p;
189 constexpr uint32_t videoCaptureCapabilities =
190 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE;
192 if (!(cap.capabilities & videoCaptureCapabilities)) {
193 qCDebug(ltVideoDevices)
194 <<
"not a V4L2_CAP_VIDEO_CAPTURE or V4L2_CAP_VIDEO_CAPTURE_MPLANE device" << p;
197 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
198 qCDebug(ltVideoDevices) <<
"not a V4L2_CAP_STREAMING device" << p;
203 if (::ioctl(fd.get(), VIDIOC_G_INPUT, &index) < 0) {
206 qCDebug(ltVideoDevices) <<
"Device does not support VIDIOC_G_INPUT, but it could"
211 qCWarning(ltVideoDevices)
212 <<
"ioctl failed: VIDIOC_G_INPUT" << qt_error_string(errno) << p;
217 qCDebug(ltVideoDevices) <<
"Video device not a v4l2 device:" << propertiesHandle;
221 auto it = std::find_if(m_videoSources.begin(), m_videoSources.end(),
222 [&](
const QGstRecordDevice &a) {
return a.gstDevice == device; });
224 if (it != m_videoSources.end())
227 m_videoSources.push_back(QGstRecordDevice{
229 QByteArray::number(m_idGenerator),
234 onVideoInputsChanged();
239 auto it = std::find_if(m_videoSources.begin(), m_videoSources.end(),
240 [&](
const QGstRecordDevice &a) {
return a.gstDevice == device; });
242 if (it != m_videoSources.end()) {
243 m_videoSources.erase(it);
244 onVideoInputsChanged();
250 QGstDeviceHandle device;
252 switch (message.type()) {
253 case GST_MESSAGE_DEVICE_ADDED:
254 gst_message_parse_device_added(message.message(), &device);
255 addDevice(std::move(device));
257 case GST_MESSAGE_DEVICE_REMOVED:
258 gst_message_parse_device_removed(message.message(), &device);
259 removeDevice(std::move(device));