Skip to content

Commit

Permalink
partial support for non-blocking ALSA drain
Browse files Browse the repository at this point in the history
Because the BlueALSA server does not have a non-blocking drain
operation, we implement non-blocking drain by omitting the server
drain request. This means that a small amount of audio at the end
of a stream may be lost. Most, possibly all, applications clear the
PCM non-block flag before calling snd_pcm_drain(), so this
incomplete drain is unlikely to impact any real use cases.
  • Loading branch information
borine committed Oct 2, 2023
1 parent dcffc94 commit 28c9605
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/asound/bluealsa-pcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,10 @@ static int bluealsa_drain(snd_pcm_ioplug_t *io) {
return -EIO;
}

/* For a non-blocking drain, we do not wait for the drain to complete. */
if (io->nonblock == 1)
return -EAGAIN;

bool aborted = false;

struct pollfd pfd = { pcm->event_fd, POLLIN, 0 };
Expand Down Expand Up @@ -1002,7 +1006,8 @@ static int bluealsa_poll_revents(snd_pcm_ioplug_t *io, struct pollfd *pfd,
goto fail;

/* This call synchronizes the ring buffer pointers and updates the
* ioplug state. */
* ioplug state. For non-blocking drains it also causes ioplug to drop
* the stream when the buffer is empty. */
snd_pcm_sframes_t avail = snd_pcm_avail(io->pcm);

/* ALSA expects that the event will match stream direction, e.g.
Expand All @@ -1015,8 +1020,13 @@ static int bluealsa_poll_revents(snd_pcm_ioplug_t *io, struct pollfd *pfd,

switch (io->state) {
case SND_PCM_STATE_SETUP:
/* To support non-blocking drain we must report a POLLOUT event
* for playback PCMs here, because the above call to
* snd_pcm_avail() may have changed the state to
* SND_PCM_STATE_SETUP. */
if (io->stream == SND_PCM_STREAM_CAPTURE)
*revents = 0;
ready = false;
*revents = 0;
break;
case SND_PCM_STATE_PREPARED:
/* capture poll should block forever */
Expand Down

0 comments on commit 28c9605

Please sign in to comment.