Skip to content

Commit

Permalink
pulseaudio: Make use of suggestedLatency stream parameter. (#849)
Browse files Browse the repository at this point in the history
This will translate the client provided suggestedLatency from
seconds to microseconds and pass them to Pulseaudio, so
the client has some control over required latency.

Invalid (= negative) values get mapped to requesting minimum
supportable latency, ie. 0 milliseconds.

This replaces the previous hard-coded latency of ~2 msecs,
which turned out to be too low on some tested mid-range
systems and caused audio buffer underruns and audio artifacts.

Signed-off-by: Mario Kleiner <[email protected]>
  • Loading branch information
kleinerm authored Oct 30, 2023
1 parent 24c8d57 commit 4af3321
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/hostapi/pulseaudio/pa_linux_pulseaudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,16 @@ PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,

stream->inputDevice = inputParameters->device;

/* Convert positive suggestedLatency from seconds to microseconds, otherwise default to zero. */
if (inputParameters->suggestedLatency >= 0)
{
stream->suggestedLatencyUSecs = (unsigned int) (inputParameters->suggestedLatency * 1e6 + 1.0f);
}
else
{
stream->suggestedLatencyUSecs = 0;
}

/*
* This is too much as most of the time there is not much
* stuff in buffer but it's enough if we are doing blocked
Expand Down Expand Up @@ -1227,6 +1237,16 @@ PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi,
}

stream->outputDevice = outputParameters->device;

/* Convert positive suggestedLatency from seconds to microseconds, otherwise default to zero. */
if (outputParameters->suggestedLatency >= 0)
{
stream->suggestedLatencyUSecs = (unsigned int) (outputParameters->suggestedLatency * 1e6 + 1.0f);
}
else
{
stream->suggestedLatencyUSecs = 0;
}
}

else
Expand Down
2 changes: 1 addition & 1 deletion src/hostapi/pulseaudio/pa_linux_pulseaudio_cb.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ PaError PaPulseAudio_StartStreamCb( PaStream * s )
const char *pulseaudioName = NULL;
pa_operation *pulseaudioOperation = NULL;
int waitLoop = 0;
unsigned int pulseaudioReqFrameSize = (1024 * 2);
unsigned int pulseaudioReqFrameSize = stream->suggestedLatencyUSecs;

stream->isActive = 0;
stream->isStopped = 1;
Expand Down
1 change: 1 addition & 0 deletions src/hostapi/pulseaudio/pa_linux_pulseaudio_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ typedef struct PaPulseAudio_Stream
pa_stream *inputStream;
pa_buffer_attr outputBufferAttr;
pa_buffer_attr inputBufferAttr;
unsigned int suggestedLatencyUSecs;
int outputUnderflows;
int outputChannelCount;
int inputChannelCount;
Expand Down

0 comments on commit 4af3321

Please sign in to comment.