[pulseaudio-discuss] What is latency? And other related questions

David Henningsson launchpad.web at epost.diwic.se
Fri Feb 12 00:20:29 PST 2010


Tristin Celestin wrote:
> This application I am writing a plugin for has a mode where it uses a timer to drive the delivery of sound. At every tick, if some condition is filled, the buffer is filled with data, otherwise the application runs.
> 
> I managed to write a plugin that successfully delivers audio using the normal pulseaudio API. I check the size of pa_stream_writable_size every tick, and if it is below some threshold, the application runs and produces sound. This threshold right now is some magic number I stumbled upon, but I think it is related to whatever tlength the server automatically gives me.
> 
> // too high, and there is a big pause before audio begins to play
> #define milliseconds 30
> 
> ...
> 
> int mixlen = pa_usec_to_bytes (milliseconds * PA_USEC_PER_MSEC, &device.spec);
> 
> ...
> 
> pa_threaded_mainloop_lock (device.mainloop);
> free_space = pa_stream_writable_size (device.stream);
> pa_threaded_mainloop_unlock (device.mainloop);
> 
> if (free_space < mixlen)
>     play ();
> else
>    write_to_buffer ();
> 
> 
> I can't write the plugin with the simple API, though, because the only measure I have of the buffer state is pa_simple_get_latency, and this doesn't seem to reflect the state of the sound buffer in the same way that pa_stream_writable_size does. So, I assume I am misunderstanding what "latency" means. I've never used an audio API before, so I don't have any real understanding of the word, just a sense of it from mailing lists and games.
> 
> What happens here either there is a large period (~30 seconds to 90 seconds) of silence before audio begins to play fine, or I get bursts of audio (5-10 seconds) followed by very long bursts of silence. I know this is related to prebuffering, but beyond that I am lost.

First, make sure that your tlength is at least two-three time as large
as mixlen. My guess is that you supply a tlength but for some reason
fail to fill it properly, so you get underruns on the PA frontend, which
makes PA increase tlength even more, leading to higher prebuffering (and
latency).

> So, in short:
> 1. What is latency?

http://en.wikipedia.org/wiki/Latency_%28audio%29

http://pulseaudio.org/wiki/LatencyControl

> 2. How do the meaning of pa_stream_writable_size and pa_simple_get_latency differ in terms of the fill state of the buffer?

pa_stream_writable_size returns the number of bytes that may be written
using pa_stream_write. pa_simple_get_latency shouldn't be used for that
purpose.

In case you haven't seen it:
http://0pointer.de/lennart/projects/pulseaudio/doxygen/

> 3. Would there be an objection to adding something along the lines of pa_simple_writable_size? Does that even make sense?_

I don't understand that question, can you rephrase it?

// David




More information about the pulseaudio-discuss mailing list