Commit graph

352 commits

Author SHA1 Message Date
Wim Taymans
b16b80601d alsa: start playback right away in IRQ mode 2023-03-29 15:13:00 +02:00
Wim Taymans
354836075d alsa: setup sources from the data thread
For IRQ based scheduling we might otherwise be woken up after we only
added one of the fds to the poll loop and then we get an error when we
try to update it afterwards. Instead, add the fds from the data thread
to get things nicely in sync.
2023-03-29 12:21:23 +02:00
Wim Taymans
8c264d2dcb alsa: in IRQ mode, disable sources while we wait
In IRQ mode, disable the ALSA fds while we wait for the graph to produce
data. Otherwise we will wake up very quickly over and over.

When we get more data, we activate the sources again to start the next
cycle.
2023-03-29 11:30:40 +02:00
Wim Taymans
dc5aa21c87 alsa-pcm: use full period size in IRQ mode
We can simply wake up every period and check the timers there is no need
to tweak the period size.
2023-03-29 11:29:43 +02:00
Wim Taymans
aa0d9c5b41 alsa: add disable-tsched to params 2023-03-28 18:27:53 +02:00
Arun Raghavan
26e37b6575 alsa: Implement period-based wakeups
Provides configuration to disable timer-based scheduling. This can be
useful at low latencies, for example, where period-based interrupts
might be more reliable than timers.
2023-03-28 16:22:53 +00:00
Wim Taymans
3698593481 spa: reuse code to clear the timers
We have set_timeout and enable_flush_timer functions to disable the
timers so use those.
2023-03-24 17:55:15 +01:00
Wim Taymans
7b6680ba57 plugins: simplify target_ handling
Drivers should only read the target_ values in the timeout, update the
timeout with the new duration and then update the position.

For the position we simply need to add the previous duration to the
position and then set the new duration + rate.

Otherwise, everything else should read the duration/rate and not use
the target_ values.
2023-03-24 11:36:15 +01:00
Wim Taymans
1bdd5eee69 alsa: update rate/duration from target
Also reorganize some things to reuse more code.
2023-03-23 18:04:36 +01:00
Wim Taymans
2adf8d38d5 node: add target_rate and target_duration in io_clock
Place the target rate and duration in the io clock area.

The driver is meant to read these new values at the start of the cycle
and update the position rate and duration.

This used to be done by the pipewire server when it received the ready
callback from the driver but this is in fact too late. Most driver would
start processing and set the next timeout based on the old rate/duration
instead of the new pending ones.

There is still a fallback for the old behaviour (with a warning) when
the driver doesn't yet update the position.
2023-03-23 17:57:16 +01:00
Wim Taymans
a7322d5043 alsa: improve target delay in ALSA
Don't just limit the max delay of samples we keep in the ALSA ringbuffer
to the buffer_size but to half of it. Make this into a max_delay
variable.

If we have a buffer size of 8192 samples and a headroom of 8192 samples,
when capturing, we would wait for the ringbuffer to contain at least
8192 samples, which would always xrun. When we limit the size to
half, we can still read the data without xruns.

Fixes #2972
2023-03-03 14:59:38 +01:00
Wim Taymans
8ffb74c8e2 alsa: keep more headroom when rate matching
When we are rate matching, keep some more headroom to make sure we
have enough data for the adaptive resampler.

Fixes crackling when following the dummy node and probably also when
following another capture device.
2023-02-02 20:06:58 +01:00
Wim Taymans
1d9640af5a spa: Fix audioconvert overflow when scaling
Add SPA_SCALE32_UP that scales a uint32 without overflow.
Use this for scaling the threshold in ALSA.
Fix the scaling in audioconvert of the buffer size, the scaling was
wrong and it was also causing an overflow resulting in choppy sound in
some cases.

See #2680
2023-01-16 18:28:31 +01:00
Wim Taymans
32a7c85c84 alsa: guard against some invalid values
Avoid division by 0 and other strange things when invalid values
are detected.

Fixes #2953
2023-01-16 16:05:17 +01:00
Wim Taymans
e5ac60b2b2 alsa: increase target for a52 and dca
For encoded format, we need to send bigger chunks to make the encoder
happy. Add a new min_delay variable with this info so that we never
leave less than that amount of samples in the ringbuffer.

Fixes #2650
2022-12-14 12:41:22 +01:00
Jonas Holmberg
6d6a5e2dbb alsa-pcm: avoid an expected resync warning
Log resync message as info when reassigning follower.
2022-12-13 14:47:04 +01:00
Wim Taymans
f44d55f6c2 handle read from timerfd correctly
When reading the timerfd gives an error, we should return right away
because the timeout did not happen.

If we change the timerfd timeout before reading it, we can get -EAGAIN.
Don't log an error in that case but wait for the new timeout.
2022-12-09 17:30:31 +01:00
Wim Taymans
b46d8a8c92 alsa: force playback start when buffer is full
When we try to play data but the ringbuffer is full, we need to start
the device or else we will stay in this situation forever and stay
silent.

Fixes #2830
2022-11-16 20:45:38 +01:00
Jonas Holmberg
22a1e5b848 alsa-pcm: Start playback when there is data
Do not start the playback device until there is data to play. Otherwise
time consuming configuration of other nodes (such as setting hw params
of a capture device) may be done after playback has been started, which
may cause xrun.
2022-10-17 15:21:49 +02:00
Wim Taymans
d22feab92a spa: add macro to simplify array iterations some more
uint32_t i;
	for (i = 0; i < SPA_N_ELEMENTS(some_array); i++)
		.. stuff with some_array[i].foo ...

   becomes:

	SPA_FOR_EACH_ELEMENT_VAR(some_array, p)
		.. stuff with p->foo ..
2022-09-30 16:24:26 +02:00
Jonas Holmberg
dacbeca77e alsa-pcm: Change resync warning logic
Log follower resync messages with info level until there has been a
successful read or write to get rid of the initial warning when starting
a stream.
2022-09-21 16:42:37 +02:00
Wim Taymans
ef39576150 alsa: rate limit some warnings 2022-09-21 15:52:24 +02:00
Wim Taymans
903f831b2d alsa: scale min value back to nominal rate 2022-09-07 23:39:51 +02:00
Wim Taymans
7057cca05e alsa: adjust min and max rates
Use the DSD scaler and interleave factors to calculate the min and
max rates.
2022-09-07 21:58:32 +02:00
Wim Taymans
955815b468 alsa: only scale DSD samplerates 2022-09-07 21:42:45 +02:00
Wim Taymans
68581235ae alsa: fix min dsd rate
We just need to allow from DSD64 onwards. Remove some unused fields.
2022-09-07 20:23:53 +02:00
Wim Taymans
72b6788c68 alsa: don't set impossible rates
When the max rate is smaller than the min rate, don't add the DSD
format at all because it is not possible to play it.

See #93
2022-09-07 19:58:30 +02:00
Wim Taymans
23522651f9 alsa: dsd rates are expressed in byte rate
DSD64 would be a rate of 44100 * 64 / 8. When packed in U32_BE, we would
negotiate 44100 * 8 / 4 (88200) with the device, this means all rates
from 88200 and up are allowed for DSD64 in U32_BE.
2022-09-07 19:54:09 +02:00
Wim Taymans
2054dcf36f alsa: Improve format negotiation
When the device is not running but has a format, close/open the
device to get all the available formats again. Do the same when
setting a format.

Otherwise, the configuration space of the device is restricted to the
current negotiated format and we can't query the other possibilities
or change it.

Fixes #2625
2022-08-31 10:03:39 +02:00
Wim Taymans
54b499b1cf alsa: do the right log level checks
spa_log_level_enabled does not work when a custom log level has
been set. Use spa_log_level_topic_enabled instead.
2022-08-05 15:03:22 +02:00
Wim Taymans
73073eb33f alsa: redirect alsa output to log file
Make a custom snd_output object that redirects output to the log
file.
2022-08-05 14:01:37 +02:00
Wim Taymans
0dc5a08bfd alsa: debug hw_params 2022-08-05 13:00:44 +02:00
Wim Taymans
34c1c1614e alsa: add some more debug
See #2614
2022-08-04 14:25:24 +02:00
Wim Taymans
e22e522ab8 alsa-pcm: limit the target to the buffer size
Don't try to wait for more samples than can fit in the ringbuffer.

See #2603
2022-08-03 12:22:38 +02:00
Wim Taymans
52281b9a7e alsa: check the offset against the size of the buffer
We need to check the last offset against the size of the buffer, not the
remaining size in the buffer.

When the writing is split, this could cause the buffer to be reused
wrongly.

See #2536
2022-07-13 09:37:01 +02:00
Carlos Rafael Giani
7e44909741 alsa: add lower rate bound for DSD playback
This prevents errors when trying to play at a rate
that's lower than DSD64 (the lowest rate available).
2022-07-05 16:56:50 +00:00
Wim Taymans
f6938d8364 alsa: simplify writing
We don't use the input buffers as ringbuffers, so remove that
code and simpify fomr things.
2022-06-24 10:38:13 +02:00
Wim Taymans
0f62d3442c alsa: handle driver bugs better
Use the NEAREST flag when setting a format. This only works for raw
formats and will update the format with the nearest accepted rate
or channels. We can then query the real configured format and use that
for the converter.

This makes things work when a driver tells us it can do 44100Hz but then
refuses and changes the rate to 48000.

See #2197, #2457, #2455, rhbz#2096193
2022-06-23 10:20:49 +02:00
Wim Taymans
b99c71262e alsa: force same clock only for pro audio profile
Assume that capture and playback nodes from a device have different
clocks. This enables the adative resampler to match them. A lot of devices
actually have slightly different rates and would work out of the box
with this fix.

Make an exception when the card is configured in the pro audio profile.
Then we force the same clock on all device nodes and avoid resampling
and rate matching. This can still be changed with a session manager
override.
2022-06-08 17:03:50 +02:00
Wim Taymans
63d2102594 alsa: do resync based on current measurements
Call update time with the currently measured delay and then resync when
needed. Previously we would do the resync in the next cycle.

See #2257
2022-05-27 21:20:51 +02:00
Jonas Holmberg
64c1b62aa8 alsa-pcm: Do not log warning when resync was expected
Log as info instead of warning when alsa sync was expected.
2022-05-11 17:45:27 +02:00
Wim Taymans
9d186cf622 alsa-pcm: scale max_error with quantum size
Adjust the max-error between the hardware delay and the quantum. Limit
this between 256 and quantum/2 to make sure we are not too sensitive.
2022-05-11 10:34:27 +02:00
Wim Taymans
b7845bd702 alsa: scale buffer with frame_scale
Make the alsa buffer a little larger when we scaled the samplerate,
like for DSD. This allows us to go all the way up to the quantum
limit.
2022-04-28 13:04:39 +02:00
Wim Taymans
c48a4bc166 pw-cat: fix DSF playback again
Make the DSD buffers a little bigger because we scale down the rate.
Read an interleaved number of samples to fill one quantum.
2022-04-28 12:32:08 +02:00
Wim Taymans
5a023c8c84 alsa: fix argument type for Long
Fixes #2271
2022-04-21 22:13:16 +02:00
Wim Taymans
1547e5fd2b alsa: limit the buffer-size to quantum-limit * 4
We don't actually need to allocate the largest possible buffer. A buffer
that is near 4 times the quantum-limit is more than enough.

See #1995
2022-04-07 16:01:48 +02:00
Wim Taymans
bcdd1ea811 alsa: set better ranges on properties 2022-03-04 17:21:14 +01:00
Wim Taymans
e28b613404 alsa: don't read more than available samples
Keep the original available samples and use them to avoid reading
a chunk when there is not enough data.
2022-02-16 21:30:54 +01:00
Wim Taymans
56c03c11f8 alsa: resync when quantum changes 2022-02-16 21:30:35 +01:00
Wim Taymans
798228a906 alsa: remove useless code
The resync check can be done in check_position_config.
2022-02-16 21:29:55 +01:00