Commit graph

1246 commits

Author SHA1 Message Date
Wim Taymans
236b7ee0e2 acp: Add idxset comparison and reverse search operations
Add contains() function that checks if an item is in an idxset. Add
isdisjoint(), issubset(), issuperset() and equals() functions that
element-wise compare two idxsets.

Add complementary functions to the existing idxset search(), first(),
next() functions that work in the reverse direction: reverse_search(),
last() and previous().

Add steal_last() to remove and return the last item in the idxset.

[Alper: original patches for PulseAudio, split from other commit]
Co-developed-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
Link: fb63e58931
Link: ec668ac44b
Link: 97d9c28579
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
a208716724 alsa-ucm: Split profile creation into verb and profile parts
To support having multiple profiles per UCM verb, split the profile
creation into two parts based on whether they should run once for each
verb or for each profile (maybe multiple times per verb).

Link: 638574c0b7
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
7883da6f52 alsa-ucm: Make mapping creation independent from indvidual profiles
The ucm_create_mapping() function is not idempotent. It looks like it
was meant to be called once per device for the devices of a UCM verb
and takes a profile argument simply because a verb has generated a
single profile so far.

Make sure creating mappings per device and adding those mappings to the
profiles happens as separate steps to make it easier to split UCM verbs
and profiles as concepts.

Link: aa5ced3887
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
07a347d3b6 alsa-ucm: Stop conflating profile name with UCM verb name
So far each profile had the exact name as their associated UCM verb,
which caused the one to be used where the other should have been.
Explicitly get and use the verb name where that was intended, and make
sure things about profiles aren't named after verbs.

Link: 4527890416
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
f4c66de9f0 alsa-ucm: Let profiles know their associated UCM verb
Currently each UCM verb generates one profile named the same as the
verb, meaning it's trivial to know which verb the profile belongs to.
This will be slightly harder to do when we generate multiple profiles
per UCM verb (e.g. to make use of conflicting devices).

It would still be possible to parse the profile name to get the UCM
verb, but instead let's keep track of the struct instance representing
the profile's associated verb. This also lets us remove a block of code
searching for the verb by its name.

Co-authored-by: Jaroslav Kysela <perex@perex.cz>
[Alper: Reused Jaroslav's UCM profile context changes for UCM verb
instead of combined devices.]
Link: 9fc7064b9a
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Jaroslav Kysela
a5d87a43df alsa-ucm: Set profiles by their struct instance, not their name
While switching profiles, it's possible that we will want to do more
work besides switching UCM verbs. The alsa-card module already has our
profiles as structs, but passes in only the names instead of the entire
struct. Make things work with the struct instead, so we can add other
things (like a UCM context) to it and use those here.

Co-authored-by: Tanu Kaskinen <tanuk@iki.fi>
[Alper: Split into its own commit and integrated Tanu's snippet.]
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
Link: 880ff393f1
[Wim: Apply to acp_card_set_profile() as well]
Co-authored-by: Wim Taymans <wtaymans@redhat.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
396382ebb2 alsa-ucm: Add enable, disable, status helpers for devices
Right now manipulating device status is done inline once while setting a
port. However, we will need to reuse this code to disable conflicting
devices of a device we want to enable. Split it into enable and disable
helper functions.

There is another issue with the device enable logic, where trying to
disabling an already disabled device sometimes fails. To avoid that,
implement a status helper and check if the device we want to enable is
already enabled/disabled before trying to do so.

Link: 9b06e8fef4
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
3ef2e874b2 alsa-ucm: Make modifiers track conflicting/supported devices as idxsets
Modifiers currently keep their conflicting and supported devices's
names, and these names are resolved to devices every time we need to use
them. Instead, resolve these device names while creating the modifier
struct and keep track of the resulting device structs in idxsets, same
as how device structs keep track of their support relations.

Link: 9b06e8fef4
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
36af5f074a alsa-ucm: Always create device conflicting/supported device idxsets
This is intended to make the current and upcoming code a bit clearer, as
we won't need to constantly check for the existence of these idxsets
before using or operating on them.

Link: d8c89de24d
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Hector Martin
9c868740c2 alsa: ucm: Fix spurious mixer path removal on initial profile set
The code that removes the mixer path if probing fails can be called in
the path that sets a non-off device profile on hotplug *before*
card->active_profile is updated, which results in spuriously removing
the mixer path. By this point, context->ucm->active_verb would be set
to the same as the profile name, so we can use that instead to avoid
the issue.

On Apple Silicon machines with the UCM profiles in the Asahi Linux repo,
this manifests as the headphones jack having hardware volume controls
*only* if PA is started with headphones connected and until they are
disconnected. Hotplugs end up triggering the bad codepath, and it falls
back to software volume (which is particularly a problem when the
hardware volume happens to be very low or 0 at that point).

Signed-off-by: Hector Martin <marcan@marcan.st>
Link: 3e038c6d88
2023-12-04 15:34:48 +00:00
Igor V. Kovalenko
3e45464603 alsa-util: Fix pa_alsa_get_supported_formats fallback.
Looks like original intention was to scan over sample formats supported by PA,
but code does the scan by list of alsa formats. Reverse the map and adjust
fallback case which now can use the same map.

Link: 5ab2b9cb0e
2023-12-04 15:34:48 +00:00
Igor V. Kovalenko
082680d8a2 alsa-util: Perform format and rate detection before setting HW params
Perform detection of supported sample format and rates just after device is
opened, before `snd_pcm_hw_params()` is called for the first time. This fixes a
problem where device restricts available sample rates after HW params are set
preventing sample rate detection (seen with UAC2 devices and kernel 6.1.9)

Bug: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/issues/1414
Bug: https://github.com/alsa-project/alsa-lib/issues/119
Link: aed52c507f
[Wim: Apply to acp add_pro_profile(), enable pa_alsa_supported_*()]
2023-12-04 15:34:48 +00:00
Igor V. Kovalenko
79ee2fac73 alsa-util: Dump probed rates
Link: 8152f39603
2023-12-04 15:34:48 +00:00
Igor V. Kovalenko
ceb1c38ee9 alsa-util: Add more standard sample rates
Link: 86c5fbab57
2023-12-04 15:34:48 +00:00
Jaroslav Kysela
84120e1167 alsa-mixer: extend pa_alsa_mixer_find with the subdevice check
The full identifier check must be executed for the new melem
creation, otherwise the duplicate control element code check
is reached.

Example (using the snd-aloop driver):

numid=56,iface=PCM,name='PCM Notify',device=1,subdevice=1
numid=62,iface=PCM,name='PCM Notify',device=1,subdevice=2

Link: 81a051089f
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
2023-12-04 15:34:48 +00:00
Jaroslav Kysela
4066bbaf09 alsa-mixer: fix the re-attach code for the mixer control element
The new helem must be tracked and old helem must be cleared
to make the code work properly. Introduce the pointer to helem
as the private value for melem and add the necessary code.

Also, add a check for the duplicate mixer elements. The duplicate
mixer element invokes the abort check in alsa-lib. Print a warning
instead and handle the exit gracefully.

Link: d1675df0cd
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
2023-12-04 15:34:48 +00:00
Jaroslav Kysela
3177f41cb4 alsa-mixer: allow to re-attach the mixer control element
It may be possible that the ALSA control element appears
again. Allow this combination by checking, if the pulseaudio
mixer element already exists. Do not create the duplicate
mixer element in this case.

Link: def8eb074e
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
2023-12-04 15:34:48 +00:00
Jaroslav Kysela
2c3e8d90d3 alsa-ucm: remove extra space from the device.intended_roles property
Before:
  device.intended_roles = " voice"

After:
  device.intended_roles = "voice"

Link: eb870fcba9
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
2023-12-04 15:34:48 +00:00
George Kiagiadakis
f5546d27c1 acp: avoid copying structure into itself
sync_mixer() calls d->set_volume(d, &d->real_volume);
which makes v and &dev->real_volume point to the same memory area
and valgrind complains:

Source and destination overlap in memcpy(0xcc53e2c, 0xcc53e2c, 260)
   at 0x488CFA0: __GI_memcpy (vg_replace_strmem.c:1121)
   by 0xBB0803F: set_volume (acp.c:1143)
   by 0xBB0EDCB: acp_device_set_port (acp.c:1897)
   by 0xBA9CD87: impl_set_param (alsa-acp-device.c:757)

because the compiler apparently implicitly converts this into a memcpy()
and memcpy(3) explicitly says "The memory areas must not overlap."
2023-12-02 20:30:40 +00:00
Wim Taymans
a532c2abdb acp: add support for hiding profiles and ports
api.acp.hidden-ports and api.acp.hidden-profiles can be used to pass
a json array of ports and profiles to hide. They will not show and
will not be selectable.
2023-11-30 17:40:48 +01:00
Wim Taymans
85a9e30908 alsa: clamp buffer_frames correctly
Don't try to multiple the max_buffer_size with the frame scale or else
we might try to set a min_buffer_size larger than the max_buffer_size.

Instead, use the frame_scale only to scale the quantum_limit and then
clamp against the max_buffer size.

See #3000
2023-11-30 13:28:16 +01:00
Arun Raghavan
c2e4e336ac alsa: Correctly bail if pitch ctls are not available
The `goto error` got misplaced while refactoring this code for
bind-ctls.
2023-11-29 15:41:24 +00:00
Wim Taymans
eca1f5685e alsa: place defaults in properties as fallback
We place the currently configured runtime settings in the properties but
clear the values when not negotiated. Don't do that but place the
defaults (when set) instead.
2023-11-27 16:20:21 +01:00
Arun Raghavan
dec1102574 alsa: Move ctl sources to a main loop
It doesn't make sense to hang these on the data loop, so let's have
these on the main loop instead. Also avoids a potential crash while
removing them (since removal happens on the main loop and the data loop
might be polling while we're doing the remove).
2023-11-22 16:00:25 +00:00
Wim Taymans
49cdb468c2 alsa: also use interpolated time as nsec in IRQ mode
Don't use the current time as the nsec field in the graph clock because
it can jitter a lot. Instead, use the smoothed next_time, like we do
for timer based scheduling.

Since we track the current time against the rate converted ideal time,
lock on to the first timestamp when we reset the dll.

See #3657
2023-11-20 13:14:54 +01:00
Wim Taymans
179fbb51fc alsa: return 0 from successful resume
Don't return the value of the last snd_pcm_resume() call because that
might be -ENOSYS when resume is not implemented for the card and then
the non-error (because we used drop/prepare later) propagates and
logs an error.
2023-11-15 10:10:56 +01:00
Wim Taymans
0da4ea3b30 alsa: handle poll error
Recover after a poll error. This fixes resume after suspend.

Fixes #3646
2023-11-15 09:56:24 +01:00
Wim Taymans
443751bf16 alsa: remove err argument in alsa_recover()
We don't actually use it.
2023-11-15 09:55:58 +01:00
Wim Taymans
7b756c9090 alsa: improve debug 2023-11-15 09:55:36 +01:00
Wim Taymans
3baead9b0b alsa: reset ready_offset when we clear the queue
So that we start from the first sample of a new ready buffer.
2023-11-10 16:12:15 +01:00
Barnabás Pőcze
e0bfdb9f46 spa: alsa: fix leaks in get_data_path()
Coverity rightfully complains that assigning to `path`
will lose its previous value leading to resource leaks.
2023-11-10 03:29:49 +01:00
Pauli Virtanen
636a9c611d acp: Respect XDG base directory spec when loading configs
Backport from Pulseaudio. Reimplement get_data_path. We'll look for the
override files similarly as we do for other config files
(XDG_CONFIG_HOME then /etc then install location), instead of looking at
the Pulseaudio locations ~/.local/share/pulseaudio etc.

Upstream commits:

From: SimonP <simonp.git@gmail.com>

alsa-mixer: Respect XDG base directory spec when loading profile sets

Try $XDG_DATA_HOME, then $XDG_DATA_DIRS, and finally fall back to old behaviour.

From: SimonP <simonp.git@gmail.com>

alsa-mixer: Respect XDG base directory spec when loading path configs

Try $XDG_DATA_HOME, then $XDG_DATA_DIRS, and finally fall back to old
behaviour (prefix-defined directory).

core-util: Ignore non-absolute XDG base dirs

These are invalid per the spec.
2023-11-09 20:36:15 +00:00
Arun Raghavan
3bbd1e66d2 alsa: Prevent pitch element probe from stepping on bind ctls
Since they're both using the same device, we want to make sure one
doesn't cause the other to be closed on failure.
2023-11-09 09:24:09 -05:00
Arun Raghavan
caf7eb33e4 alsa: Fix ctl device cleanup order
Reorder removing sources, closing the ctl device, and freeing memory, so
that there's no chance of races causing a crash.
2023-11-09 09:23:07 -05:00
Arun Raghavan
2871a65b1f alsa: Add a mechanism to bind ALSA controls as prop params
This adds an api.alsa.bind-ctls property to alsa-pcm sink and source
nodes, to bind a property to an ALSA PCM ctl. The property is an array
of ctl names that should be bound.

This can be handy, for example, to bind the Playback/Capture Rate
controls on a USB gadget, in order to track the PCM's state via a node
param.

This is currently wired to be read-only, but it should be easy enough to
make it writable.
2023-11-07 23:22:53 -05:00
Arun Raghavan
6bae812ce0 alsa: Share node/port info emission code between sink and source
It's mostly the same, and it allows us to trigger this code in the
shared alsa-pcm.c code as well.
2023-11-07 14:15:12 -05:00
Barnabás Pőcze
5b128cdbe9 spa: alsa: compress-offload: use spa_assert_not_reached()
Use `spa_assert_not_reached()` instead of `assert(false)`
as it is more descriptive and the error message is clearer.
2023-11-06 19:01:20 +01:00
Wim Taymans
6f6bf204d6 alsa: remove redundant snd_pcm_recover()
Because we now always _drop/_prepare_/_start, the snd_pcm_recover()
before that is no longer useful.

Retry snd_pcm_resume() after suspend when -EAGAIN and fall back to
_drop/_prepare/_start when that fails.
2023-11-06 17:48:02 +01:00
Wim Taymans
032955fffe alsa: disable resample when pitch element is available
We need to disable the resampler when there is a pitch element. This was
correctly done in setup_matching but not in check_position_config().

See #3628
2023-11-06 15:09:35 +01:00
Wim Taymans
9cdb3607e1 alsa: remove device properties when closed
When the device has not configured a format, remove the properties that
depend on the format so that they don't limit what we can configure the
device with next.

See #3613
2023-10-30 11:20:52 +01:00
Wim Taymans
771f71f622 alsa: force the rate and quantum for iec958 formats
See #2284
2023-10-19 10:43:42 +02:00
Wim Taymans
c9c98312cb alsa: don't limit reads to avail
The ringbuffer can warp around and then there is really more available.
2023-10-18 15:44:07 +02:00
Wim Taymans
db5516ccd8 alsa: limit read to available frames
We calculate the available frames in read_sync but add another
check in read_frames so that we don't attempt to read more frames
than we have available to avoid xruns.
2023-10-18 14:59:40 +02:00
Wim Taymans
a595d3dddb alsa: after a resync, reset the dll
Otherwise we might still be overcompensating for the previous errors and
generate more errors.

Fixes #3584
2023-10-18 14:49:46 +02:00
Wim Taymans
6b8c267e91 alsa: improve headroom calculations
Make a function to recalculate the headroom and call it whenever the
resample state of the node can change.

When we are IRQ based scheduling but need to resample, we are actually
not driving the graph whit IRQ and need to adjust our period size and
headroom as if we are using timers.
2023-10-18 14:45:47 +02:00
Wim Taymans
2e066e6b1e alsa: only use avail_update when woken up with IRQ
When we are following a different clock or resampling, use update
to get more accurate update results.
2023-10-18 14:42:33 +02:00
Wim Taymans
840a9487a4 alsa: don't try to link when prepare fails 2023-10-16 10:32:51 +02:00
Hector Martin
fd969dab28 alsa: Ignore PCM devices with udev env ACP_IGNORE
When checking that a card has all of its PCM devices available, ignore
any specific device with the ACP_IGNORE udev environment variable. This
mirrors how we ignore whole cards, but specifically allows non-PipeWire
software to own specific PCM devices.

Note that this does not actually stop PipeWire from using those
subdevices right now, we assume UCM configs take care of that. This
should probably be implemented later to ensure PipeWire always stays
away from them, but for now this fixes the issue where it refuses to
probe the entire card.

Fixes: #3570

Signed-off-by: Hector Martin <marcan@marcan.st>
2023-10-14 14:14:10 +09:00
Wim Taymans
11320cf203 tweak number of buffers
In most cases we can use just 1 buffer.

The alsa-pcm-source needs at least 2 buffers so increment the min
limit.
2023-10-13 14:00:29 +02:00
Hector Martin
89e9da8cc0 alsa: Do not attempt to prepare linked PCMs
All linked PCMs prepare together. If we prepare the secondaries, that
action clobbers the write pointer of every PCM every time, which then
causes playback to fail to start due to lack of data.

Signed-off-by: Hector Martin <marcan@marcan.st>
2023-10-12 21:38:55 +09:00