meta-pipewire: additional improvements for iMX8MQ
[AGL/meta-agl.git] / meta-pipewire / recipes-multimedia / pipewire / pipewire / 0009-alsa-adjust-delay-depending-on-hardware.patch
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
5
6 Currently PipeWire is able to reproduce audio in systems where
7 DMA granularity is not burst but it could face an xrun.
8
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.
12
13 Signed-off-by: Walter Lozano <walter.lozano@collabora.com>
14 ---
15  spa/plugins/alsa/alsa-pcm.c | 12 +++++++++++-
16  spa/plugins/alsa/alsa-pcm.h |  1 +
17  2 files changed, 12 insertions(+), 1 deletion(-)
18
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);
25  
26         dir = 0;
27 +       state->pcm_is_batch = snd_pcm_hw_params_is_batch(params);
28         period_size = 1024;
29 -       if (snd_pcm_hw_params_is_batch(params)) {
30 +       if (state->pcm_is_batch) {
31                 period_size = 512;
32                 spa_log_warn(state->log, NAME" hardware does double buffering, changing period_size to %ld", period_size);
33         }
34 @@ -639,6 +640,15 @@ static int get_status(struct state *state, snd_pcm_uframes_t *delay, snd_pcm_ufr
35  
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;
44 +                       else
45 +                               *delay = 0;
46 +               }
47         }
48         else {
49                 *delay = avail;
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 {
55  
56         bool have_format;
57         struct spa_audio_info current_format;
58 +       bool pcm_is_batch;
59  
60         snd_pcm_uframes_t buffer_frames;
61         snd_pcm_uframes_t period_frames;
62 -- 
63 2.20.1
64