Commit graph

303 commits

Author SHA1 Message Date
Wim Taymans
2833ff1272 alsa: split irq and timer wakeup functions
Because it's easy to do and avoids some runtime checks.
2024-02-14 16:25:20 +01:00
Dimitrios Katsaros
8608c03b76 alsa: Reduce jitter in irq based software timestamping
We would timestamp within an unlikely block, which would introduce
additional jitter to current_time, which would have an impact on
the performance of the timer sensitive code.
2024-02-14 15:03:31 +00:00
Wim Taymans
0b67c10a9c alsa: always reevaluate matching when driver changed
Always reevaluate the rate matching even when we did not change the
follower state.

It is possible that we were a follower from some node with the same
clock and now become a follower of a node with a different clock. The
follower state doesn't change but we need to activate the rate matching
logic in that case.

Fixes rate matching in pro audio (playback) when capture and playback
are moved to another driver.
2024-02-01 11:07:18 +01:00
Wim Taymans
803c82abb9 alsa: use chmap by default when using UCM
Fixes #2648
2024-01-25 10:26:11 +01:00
Wim Taymans
846f994ac8 alsa: increase buffers when using small buffer
We can increase the MAX_LATENCY again if we increase the amount of
buffers when we are using a small buffer.

Normally we ask for 4 * quantum-limit as the buffer. This should be good
to use 1 buffer and quantum-limit as the quantum with enough headroom
to not run out of buffers.

If we are however using less buffer-frames we need to be careful and
allocate an extra buffer. Imagine using a buffer of 4096 frames, we can
support a quantum of up to 2048 frames if we use 2 buffers.

See #3744
2024-01-22 12:10:51 +01:00
Wim Taymans
c5b845bc90 alsa: improve max-latency property
Half of the buffersize is not enough to support as a max-quantum, we
need to divide by (4 * frame_scale) to allow some headroom and account
for the DSD scaling. We do the same calculation to suggest a buffer size
using the quantum-limit.

See #3744
2024-01-22 10:20:24 +01:00
Wim Taymans
c7519c73ac spa: small cleanups
Use snprint to ensure null terminated strings.
Initialize res, it would be uninitialized when setting a NULL Latency
param.
2024-01-10 11:35:54 +01:00
Dimitrios Katsaros
1d3013f0f9 ALSA: Remove alsa timer from IRQ scheduling
As part of the setup for IRQ based scheduling, a period event
was installed. Not only is a timer based polling unecessary for
IRQ scheduling, depending on the state of the system, the timer
could fire far enough from the IRQ, causing alsa wakeup events
with no data in the ring buffer. Pipewire would identify these
events as an "early wakeup", adding an extra quantum of time
to the next_time estimate, skewing the clock and causing issues
with apps that depend on precise timing.
2023-12-13 20:56:50 +01:00
Wim Taymans
5980f6988a Revert "alsa: also use interpolated time as nsec in IRQ mode"
This reverts commit 49cdb468c2.

We should not do this, the nsec field should be relatable to the clock
monotonic time. If we use the estimated time, without actually using it
as a timer, we might end up with a wakeup time in the future compared to
the MONOTONIC clock time.

Instead, you can use the estimated current time simply by subtracting
the rate corrected duration from the next_nsec. This is really only
useful for some selected use cases (like in the JACK library).

This fixes some issues where in pro-audio mode, a client would try to
compare the current MONOTONIC time to nsec and find that it is in the
past.

This commit was done in an attempt to fix #3657 but it turned out the
real problem was something else.
2023-12-12 13:09:30 +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
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
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
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
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
Wim Taymans
e940361c94 alsa: don't check early wakeup when PCM stopped
When the PCM is stopped, don't check for early wakeup because if we
are early, we will never be on time in the next iteration either because the
PCM is stopped and doesn't advance.

Also don't try to align when stopped.

See #3565
2023-10-12 15:28:53 +02:00
Wim Taymans
0ca1acb3d5 alsa: don't resync capture when linked
We should be aligned correctly.
2023-10-12 15:28:21 +02:00
Wim Taymans
080c40d938 alsa: handle errors from get status
And skip a cycle.
2023-10-12 15:17:48 +02:00
Wim Taymans
a8636b04cb alsa: always read/write follower samples
Ignore errors such as -EAGAIN, just write what we have.

See #3565
2023-10-12 11:40:50 +02:00
Wim Taymans
a9659d9dce alsa: add Tag set and enum support
So that we can see the tags on the sinks and sources.
2023-10-11 15:56:52 +02:00
Wim Taymans
77651e130e alsa: add extra checks
Check if the device is opened when doing _prepare or _start. When it was
a follower but failed to open, we might otherwise crash.

Fixes #3554
2023-10-11 11:39:45 +02:00
Wim Taymans
2278dd1460 acp: only disable tsched when linking
Disable timer based scheduling only if we are going to link the devices
together.

See #3556
2023-10-09 12:28:46 +02:00
Wim Taymans
896fea62c2 alsa: add api.alsa.auto-link option
Add an option to automatically use snd_pcm_link when the follower clock
is matching the driver. Only set this to true in pro-audio and when
nodes are scheduled together.

See #3556
2023-10-09 10:13:43 +02:00
Wim Taymans
96c12c2988 alsa: do playback sync even when alsa is paused
We only start the ALSA pcm after we get our first buffer.

We still need to do the sync through (and get the number of prefilled
samples) to make sure we set our new timeout and don't keep on waking
up quickly while the graph fetches the first buffer.
2023-10-02 15:38:52 +02:00
Wim Taymans
099e2cf27a alsa: silence an error after resync 2023-09-22 11:23:51 +02:00
Wim Taymans
03840a2a9a alsa: fix recover logic
We can't call spa_alsa functions from the data thread. Iterate ourselves
over the linked devices that we all need to recover as a group.
2023-09-20 15:38:41 +02:00
Wim Taymans
a56ed47c5e alsa: set the driver rate in the clock io
Rename some variables to make it clear that we are dealing with the rate
and duration of the driver.
Set the driver rate in the clock instead of the target_rate, which we
might have rejected.
2023-09-20 11:32:34 +02:00
Wim Taymans
20db9e2d70 alsa: link driver and follower
When we are using the same clock (!matching) try to link the two PCM
devices together. This starts and stops the devices at the same time and
gives better latency.
2023-09-19 13:04:43 +02:00
Wim Taymans
ee1bb2362d alsa: sync followers from the driver
Keep track or the follower state and do the sync from the driver
wakeup.

This improves sync between devices because the sync no longer
depends on when the node was woken up in the graph and scheduled. We
would have been able to handle this difference by using the htimestamp
but that doesn't seen to work reliably on some hardware.

This is also essential for the interrupt based scheduling.
2023-09-18 14:53:15 +02:00
Wim Taymans
c51f6ccbd7 alsa: rework the state sync code
Make one function to sync the state with the realtime thread.
2023-09-18 14:46:56 +02:00
Wim Taymans
86f1d14b72 alsa: keep states in global list 2023-09-18 09:40:52 +02:00
Barnabás Pőcze
fc72208fa0 spa: alsa: mark spa_alsa_silence() static 2023-09-16 17:38:51 +02:00