1 From 38cdfa4483de4c2e91bfccb9c22ec72d9c3720f4 Mon Sep 17 00:00:00 2001
2 From: Walter Lozano <walter.lozano@collabora.com>
3 Date: Sat, 22 Aug 2020 11:51:30 -0300
4 Subject: [PATCH 9/9] alsa: adjust delay depending on hardware
6 Currently PipeWire is able to reproduce audio in systems where
7 DMA granularity is not burst but it could face an xrun.
9 In order to mitigate this issue, adjust the delay PipeWire
10 calculates to make sure that a period is available in the buffer
11 when snd_pcm_hw_params_is_batch == 1.
13 Signed-off-by: Walter Lozano <walter.lozano@collabora.com>
15 spa/plugins/alsa/alsa-pcm.c | 12 +++++++++++-
16 spa/plugins/alsa/alsa-pcm.h | 1 +
17 2 files changed, 12 insertions(+), 1 deletion(-)
19 diff --git a/spa/plugins/alsa/alsa-pcm.c b/spa/plugins/alsa/alsa-pcm.c
20 index 92ef2151..1f15085f 100644
21 --- a/spa/plugins/alsa/alsa-pcm.c
22 +++ b/spa/plugins/alsa/alsa-pcm.c
23 @@ -462,8 +462,9 @@ int spa_alsa_set_format(struct state *state, struct spa_audio_info *fmt, uint32_
24 state->frame_size = info->channels * (snd_pcm_format_physical_width(format) / 8);
27 + state->pcm_is_batch = snd_pcm_hw_params_is_batch(params);
29 - if (snd_pcm_hw_params_is_batch(params)) {
30 + if (state->pcm_is_batch) {
32 spa_log_warn(state->log, NAME" hardware does double buffering, changing period_size to %ld", period_size);
34 @@ -639,6 +640,15 @@ static int get_status(struct state *state, snd_pcm_uframes_t *delay, snd_pcm_ufr
36 if (state->stream == SND_PCM_STREAM_PLAYBACK) {
37 *delay = state->buffer_frames - avail;
38 + if (state->pcm_is_batch) {
39 + /* In this case, as we don't have a good granularity in the
40 + * avail report try to compensate this by tweaking the delay
41 + * and make sure that a period is available in the buffer */
42 + if (*delay > state->period_frames)
43 + *delay = *delay - state->period_frames;
50 diff --git a/spa/plugins/alsa/alsa-pcm.h b/spa/plugins/alsa/alsa-pcm.h
51 index b7a2dd29..3b5c0d7b 100644
52 --- a/spa/plugins/alsa/alsa-pcm.h
53 +++ b/spa/plugins/alsa/alsa-pcm.h
54 @@ -100,6 +100,7 @@ struct state {
57 struct spa_audio_info current_format;
60 snd_pcm_uframes_t buffer_frames;
61 snd_pcm_uframes_t period_frames;