Commit graph

43 commits

Author SHA1 Message Date
Hans de Goede
1de07a6a31 Merge branch 'spa-v4l2-log-fix' into 'master'
spa: v4l2: Add missing v4l2_log_topic_init() call to device/udev impl_init()

See merge request pipewire/pipewire!2117
2025-10-27 18:41:05 +01:00
Sam James
2cec77e7df *: unify config.h handling
config.h needs to be consistently included before any standard headers
if we ever want to set feature test macros (like _GNU_SOURCE or whatever)
inside. It can lead to hard-to-debug issues without that.

It can also be problematic just for our own HAVE_* that it may define
if it's not consistently made available before our own headers. Just
always include it first, before everything.

We already did this in many files, just not consistently.
2025-05-30 10:24:13 +00:00
psykose
2d071d658f spa: use a separate logind dependency separate from systemd
non-systemd systems also have logind, in the form of elogind, which works to
resolve the v4l2 video source race just as well. permit finding elogind, by
using a separate dep object.
2024-09-21 21:56:34 +00:00
Hans de Goede
2a6ba61264 spa: v4l2: Use systemd-logind to listen for access changes
There is a race between logind applying ACLs to allow the active session
of a locally present user access to devices with a udev uaccess tag
(like /dev/video# nodes) getting applied vs wireplumber/pipewire starting.

Wireplumber/pipewire are part of the (user) default.target, which gets
started as soon as a user logs in and systemd --user is started for that
user, where as logind only starts applying the ACLs after the gnome-shell
associated logind session has been created.

This race may cause pipewire to not see v4l2 video sources at login,
this can be reproduced with these steps:

1. sudo setfacl --remove-all /dev/video*
2. systemctl --user restart pipewire
3. Now wp-ctl status will not show any video devices
   (like if pipewire was started before the udev uaccess ACLs got applied)
4. Do a switch to another (test) user without logging out, e.g. in GNOME
   go to the top right system menu press the power on/off icon and select
   "Switch User..."
5. Switch back to your normal user. Run getfacl /dev/video0 this will show
   your user has access now.
6. wp-ctl status should show the camera now, but it does not.

Fix pipewire not seeing v4l2 sources in this case by making v4l2-udev
monitor systemd-logind session changes and redoing the access() checks
on /dev/video# nodes when the session changes.

Closes: #3539
Closes: #3960
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2024-09-11 07:26:41 +00:00
Hans de Goede
ab245947e5 spa: v4l2: Remove start_inotify() call from impl_on_fd_events()
The spa_loop_add_source() call for udev event monitoring which uses
impl_on_fd_events() is done from start_monitor() which also unconditionally
calls start_inotify().

Since start_monitor() already always calls start_inotify() there is no
scenario where start_inotify() has not been called yet when
impl_on_fd_events() gets called. So the start_inotify() call in
impl_on_fd_events() is redundant, remove it.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2024-09-11 07:26:41 +00:00
Hans de Goede
e48a1595a6 spa: v4l2: Add missing v4l2_log_topic_init() call to device/udev impl_init()
Wireplumber creates SPA objects of the "api.v4l2.enum.udev" type without
ever creating any "api.v4l2.source" objects.

This was causing the v4l2_log_topic variable to never get initialized,
causing spa_log_xxx() calls in v4l2-udev.c to not work.

Call v4l2_log_topic_init() from v4l2-udev's impl_init() to fix this.

v4l2-device's impl_init() is also missing a call to v4l2_log_topic_init(),
add this as well.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2024-09-10 17:42:26 +02:00
Hans de Goede
46f89d8009 spa: v4l2: Remove start_watching_device() loop from start_inotify()
Now that start_monitor() (which calls start_inotify()) is called before
enum_devices() it no longer is necessary to call start_watching_device()
for devices which have been enumerated before start_inotify() gets
called (since there will not be any such devices anymore).

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2024-08-07 22:38:16 +00:00
Hans de Goede
584168caab spa: v4l2: call start_monitor() before enum_devices()
This fixes 2 races wrt probing v4l2 devices:

1. Before this change there was a window where a new udev device can get
added between the udev_enumerate_scan_devices() call in enum_devices() and
the udev_monitor_enable_receiving(this->umonitor); call. If this window was
hit then enum_devices() would not see the device and no udev-event for it
would be received either causing the device to not be seen.

Enabling udev event monitoring before calling udev_enumerate_scan_devices()
fixes this. Note that the code is already prepared to deal with getting
multiple add/change events for the same udev device, so hitting the new
race window where PipeWire may receive both an add- or change-event and
also sees + probes the device from enum_devices() is not a problem.

2. Before this change devices added by enum_devices() would not have
inotify monitoring activated right away because notify.fd = -1 at this
time turning start_watching_device() into a no-op.

These devices without inotify monitoring would then have their access
checked by process_device() calling check_access().

Then after all devices have been enumerated start_monitor() would call
start_inotify() which calls start_watching_device() for all devices added
by enum_devices(). This leaves a window where the ACL can change without
there being an inotify watch for it.

Calling start_monitor() before enum_devices() puts start_inotify()
notify before enum_devices() so that the add_device() calls done
by enum_devices() will now successfully call start_watching_device()
closing this window.

PipeWire is somewhat likely to not notify ACL changes because of this
because PipeWire is part of the systemd user default.target, where as
logind only starts applying the ACLs after GNOME has created the seat
for the GNOME session. So on first login we have PipeWire starting
and logind applying the ACLs at the same time, which allows for the ACL
change to hit the small race window where PipeWire is not monitoring
for ACL changes. Fixing this second race should hopefully resolve
issue #3960.

Closes: #3960
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
2024-08-07 22:38:16 +00:00
Barnabás Pőcze
ad975c93ee spa: alsa,v4l2: avoid double access check on inotify events
Previously both `impl_on_notify_events()` and `process_{device,card}()`
called `check_access()`. Avoid that by merging `ACTION_ADD` and
`ACTION_DISABLE` into a single `ACTION_CHANGE` and let `process_{device,card}()`
call `check_access()` and decide what to do.
2024-02-09 18:32:53 +01:00
Barnabás Pőcze
0158b5dcb6 spa: v4l2: handle IN_IGNORED events from inotify 2024-02-09 18:32:53 +01:00
Barnabás Pőcze
69f9e75458 spa: alsa,v4l2: avoid double lookup on inotify events
Split up `process_{device,card}()` to have a separate function that does
the lookup based on the udev device, and only use that when there is
no available reference to the actual device/card object.
2024-02-09 18:32:53 +01:00
Barnabás Pőcze
57a5417703 spa: v4l2: use proper log topic in udev monitor 2024-02-09 18:32:53 +01:00
Barnabás Pőcze
5fa34988fa spa: alsa,v4l2: use proper enum for action
An enum provides better debugging experience
and makes the source code easier to grasp.
2024-02-09 18:32:53 +01:00
Barnabás Pőcze
18cfb808bd spa: alsa,v4l2: simplify inotify read loop exit condition
The removed condition is strictly a subset of the condition
that is checked one line below, so remove it.
2024-02-09 18:32:53 +01:00
Barnabás Pőcze
66c7966b75 spa: v4l2: remove ignored flag
Nothing sets it.
2024-02-09 18:32:53 +01:00
Barnabás Pőcze
3bbccccd05 spa: v4l2: use a separate watch for each device
Instead of watching /dev, use a separate watch for each device.
This is supposed to achieve the same result as the now reverted
88f0dbd6fc ("v4l2: don't set inotify on /dev"):

  Doing inotify on /dev is not a good idea because we will be woken up by
  a lot of unrelated events.

  There is a report of a performance regression on some IO benchmark
  because of lock contention within the fsnotify subsystem due to this.

  Instead, just watch for attribute changes on the /dev/videoX files
  directly. We are only interested in attribute changes, udev should
  notify us when the file is added or removed.

Fixes #3439
2023-09-07 10:17:29 +02:00
Barnabás Pőcze
53ce1ee576 Revert "v4l2: don't set inotify on /dev"
This reverts commit 88f0dbd6fc.
2023-09-07 10:12:35 +02:00
Barnabás Pőcze
1110dbb7c1 Revert "v4l2: handle inotify errors"
This reverts commit 07b8954dc8.
2023-09-07 10:12:30 +02:00
Wim Taymans
07b8954dc8 v4l2: handle inotify errors
Handle inotify errors and remove the source.

See #3439
2023-08-21 16:27:00 +02:00
Wim Taymans
88f0dbd6fc v4l2: don't set inotify on /dev
Doing inotify on /dev is not a good idea because we will be woken up by
a lot of unrelated events.

There is a report of a performance regression on some IO benchmark
because of lock contention within the fsnotify subsystem due to this.

Instead, just watch for attribute changes on the /dev/videoX files
directly. We are only interested in attribute changes, udev should
notify us when the file is added or removed.
2023-07-31 13:16:17 +02:00
Ashok Sidipotu
6cd29aed05 spa: v4l2: get the device number of the v4l2 device
This value can be used in filtering the camera devices in consultation with
libcamera
2023-07-19 09:21:03 +00:00
Barnabás Pőcze
934ab3036e treewide: use SPDX tags to specify copyright information
SPDX tags make the licensing information easy to understand and clear,
and they are machine parseable.

See https://spdx.dev for more information.
2023-02-16 10:54:48 +00:00
Wim Taymans
f7c4909243 udev: use devpath for the sysfs.path property
So that it matches what pulseaudio does.

Fixes #2779
2022-10-20 16:44:37 +02:00
Wim Taymans
03f918bf15 udev: prefix vendor.id and product.id with 0x
They are hex strings so prefix them with 0x to make sure they get
handled like that in properties.

Fixes #2527
2022-07-11 11:55:42 +02:00
Wim Taymans
bdfde2fdf0 Revert "spa: leave vendor.id and product.id in hex"
This reverts commit 0d33760b71.
2022-07-11 11:23:08 +02:00
Wim Taymans
0d33760b71 spa: leave vendor.id and product.id in hex
There is no reason to store this as anything other than the original
hex string. Especially for compatibility with pulseaudio.

See !1298
2022-06-30 08:38:52 +02:00
Wim Taymans
4660e16d5b meson: enable some more warnings
Fix some warnings
2022-01-27 11:07:17 +01:00
Pauli Virtanen
a67f38f790 spa/v4l2: better device product names on current kernels
udev/kernel sometimes give "Video Capture N" as ID_V4L_PRODUCT, which is
bogus as a device product name.  The ID_MODEL* field seem to always have
a sensible product name.

Get device.product.name always from ID_MODEL*, and use ID_V4L_PRODUCT
only as the last fallback.
2022-01-23 12:57:11 +00:00
Philippe Normand
6b1c483cf7 spa: v4l2: Convert buf structure to union and fix alignment
The inotify_event field has a variable sized type, so it's recommended to store
it at the end of the storage unit. Fixes gnu-variable-sized-type-not-at-end warning.
2021-10-25 07:26:15 +00:00
Julian Bouzas
4732aea0c6 spa: clear all devices when udev monitor stops
Otherwise the udev device leaks when restarting the monitor
2021-07-13 17:03:30 +00:00
Wim Taymans
46ef88e520 spa: save the old change_mask and restore when emitting full
When we add a new listener to an object, it will emit the full state
of the object. For this it temporarily sets the change_mask to all
changes. Restore the previous state after this or else we might not
emit the right change_mask for the next listener.

Consider the case where one there are two listeners on an object.
The object emits a change and the first listener wants to enumerate the
changed params. For this is adds a new listener and then triggers the
enumeration. If we set the change_mask to 0 after adding the listener,
the second listener would get a 0 change_mask and fail to update
its state.
2021-05-27 15:21:44 +02:00
Peter Hutterer
71c6a175c4 Drop double semicolons 2021-05-20 07:24:22 +00:00
Peter Hutterer
7697ed0757 treewide: replace strcmp() == 0 with spa_streq()
This change is only done in source files for now, header files will be done
separately.
2021-05-18 22:10:27 +10:00
Peter Hutterer
65f5f2a6f8 alsa/v4l2: convdert udev's VID/PID from hex to decimal
udev's ID_MODEL_ID and ID_VENDOR_ID are inconsistent: always 4-digit hex but
sound devices are prefixed with 0x, v4l devices are not. Depending on the
actual ID, the value will look like decimal (1234) or hex (a234).

pw-dump will then print those as either decimal integers (i.e. 0x1234 becomes
decimal 1234) or double (i.e. a234 becomes 41524.00).

Make this consistent by converting the string from hex do decimal where we
get it.
2021-05-18 07:35:00 +00:00
Peter Hutterer
2405f0942b spa/buffer: rename SPA_MEMBER to SPA_PTROFF
SPA_MEMBER is misleading, all we're doing here is pointer+offset and a
type-casting the result. Rename to SPA_PTROFF which is more expressive (and
has the same number of characters so we don't need to re-indent).
2021-05-06 09:39:39 +00:00
Pauli Virtanen
20e383cea7 v4l2: remove device if inotify reports it's no longer accessible 2021-04-11 16:38:10 +00:00
Björn Daase
5913eb098c treewide: fix issues found by codespell 2021-03-16 19:11:25 +00:00
Wim Taymans
2b44f42845 pass the complete buffer size to snprintf
There is no reason to pass size-1, snprintf will always put a \0
at the end.
2021-02-02 12:09:29 +01:00
Wim Taymans
22c793aa87 v4l2: add inotify support
Wait with inotify until we can access the device node before emiting
the new device.
Clean up alsa-udev and make it more like v4l2-udev
2020-12-17 10:43:57 +01:00
Wim Taymans
a1846c9780 udev: don't loop forever on errors
When we can't find the v4l2 device id, unref the device and continue
instead of looping forever and consuming all memory.

Fixes #219
2020-03-24 15:54:58 +01:00
Wim Taymans
f391353c7f Make interface types a string
This is more in line with wayland and it allows us to create new
interfaces in modules without having to add anything to the type
enum. It also removes some lookups to map type_id to readable
name in debug.
2019-12-19 13:36:04 +01:00
Wim Taymans
77e07e1948 improve properties on globals and ports 2019-09-20 13:28:06 +02:00
Wim Taymans
6756a3c8fc monitor: remove monitor API and use device
Remove the monitor API, we can use the device API for it. Make sure
we support creating devices (like alsa) from another device (udev).

Use new object.id to store the object id in the object properties. Use
the port.id/node.id etc to make relations to other objects.
2019-09-20 13:04:14 +02:00
Renamed from spa/plugins/v4l2/v4l2-monitor.c (Browse further)