Skip to content

Commit

Permalink
aplay: reduce ALSA buffering
Browse files Browse the repository at this point in the history
It seems that chromium on the iPhone cannot adjust A/V sync when
the audio latency exceeds about 400ms. So to keep the default
delay when using bluealsa-aplay below that critical value, change
the default ALSA hw params to lower values which are still
compatible with the majority of sound cards.
  • Loading branch information
borine committed Nov 11, 2024
1 parent b9db5b6 commit 7a57398
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/spellcheck-wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ ATRAC
BlueALSA
BlueZ
Fraunhofer
iPhone
IWYU
LDAC
LHDC
Expand Down
14 changes: 7 additions & 7 deletions doc/bluealsa-aplay.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ OPTIONS

--pcm-buffer-time=INT
Set the playback PCM buffer duration time to *INT* microseconds.
The default is 500000. It is recommended to choose a buffer time that is
The default is 200000. It is recommended to choose a buffer time that is
an exact multiple of the period time to avoid potential issues with some
ALSA plugins (see --pcm-period-time option below).
ALSA may choose the nearest available alternative if the requested value is
Expand All @@ -101,7 +101,7 @@ OPTIONS

--pcm-period-time=INT
Set the playback PCM period duration time to *INT* microseconds.
The default is 100000.
The default is 50000.
ALSA may choose the nearest available alternative if the requested value is
not supported.

Expand Down Expand Up @@ -232,12 +232,12 @@ Instead it will choose its own values, which can lead to rounding errors in the
period size calculation when used with the ALSA `rate` plugin. To avoid this,
it is recommended to explicitly define the hardware period size and buffer size
for dmix in your ALSA configuration. For example, suppose we want a period time
of 100000 µs and a buffer holding 5 periods with an Intel 'PCH' card:
of 50000 µs and a buffer holding 4 periods with an Intel 'PCH' card:

::

defaults.dmix.PCH.period_time 100000
defaults.dmix.PCH.periods 5
defaults.dmix.PCH.period_time 50000
defaults.dmix.PCH.periods 4

Alternatively we can define a PCM with the required setting:

Expand All @@ -250,8 +250,8 @@ Alternatively we can define a PCM with the required setting:
ipc_key 12345
slave {
pcm "hw:0,0"
period_time 100000
periods 5
period_time 50000
periods 4
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions utils/aplay/alsa-pcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ static int alsa_pcm_set_sw_params(snd_pcm_t *pcm, snd_pcm_uframes_t buffer_size,
goto fail;
}

/* start the transfer when the buffer is full (or almost full) */
snd_pcm_uframes_t threshold = (buffer_size / period_size) * period_size;
/* start the transfer when the buffer is half full - this allows spare
* capacity to accommodate bursts and short breaks in the bluetooth stream. */
snd_pcm_uframes_t threshold = buffer_size / 2;
if ((err = snd_pcm_sw_params_set_start_threshold(pcm, params, threshold)) != 0) {
snprintf(buf, sizeof(buf), "Set start threshold: %s: %lu", snd_strerror(err), threshold);
goto fail;
Expand Down
7 changes: 5 additions & 2 deletions utils/aplay/aplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ static bool ba_profile_a2dp = true;
static bool ba_addr_any = false;
static bdaddr_t *ba_addrs = NULL;
static size_t ba_addrs_count = 0;
static unsigned int pcm_buffer_time = 500000;
static unsigned int pcm_period_time = 100000;
/* Many devices (e.g. the iPhone) cannot handle very high audio latency. To keep
* the overall latency below 400ms we choose ALSA hw parameters such that the
* ALSA latency is below 200ms. */
static unsigned int pcm_buffer_time = 200000;
static unsigned int pcm_period_time = 50000;

/* local PCM muted state for software mute */
static bool pcm_muted = false;
Expand Down

0 comments on commit 7a57398

Please sign in to comment.