mirror of
				https://gitlab.freedesktop.org/pulseaudio/pulseaudio.git
				synced 2025-11-03 09:01:50 -05:00 
			
		
		
		
	source/sink: Allow pa_{source, sink}_get_latency_within_thread() to return negative values
The reported latency of source or sink is based on measured initial conditions.
If the conditions contain an error, the estimated latency values may become negative.
This does not indicate that the latency is indeed negative but can be considered
merely an offset error. The current get_latency_in_thread() calls and the
implementations of the PA_{SINK,SOURCE}_MESSAGE_GET_LATENCY messages truncate negative
latencies because they do not make sense from a physical point of view. In fact,
the values are truncated twice, once in the message handler and a second time in
the pa_{source,sink}_get_latency_within_thread() call itself.
This leads to two problems for the latency controller within module-loopback:
- Truncating leads to discontinuities in the latency reports which then trigger
  unwanted end to end latency corrections.
- If a large negative port latency offsets is set, the reported latency is always 0,
  making it impossible to control the end to end latency at all.
This patch is a pre-condition for solving these problems.
It adds a new flag to pa_{sink,source}_get_latency_within_thread() to allow
negative return values. Truncating is also removed in all implementations of the
PA_{SINK,SOURCE}_MESSAGE_GET_LATENCY message handlers. The allow_negative flag
is set to false for all calls of pa_{sink,source}_get_latency_within_thread()
except when used within PA_{SINK,SOURCE}_MESSAGE_GET_LATENCY. This means that the
original behavior is not altered in most cases. Only if a positive latency offset
is set and the message returns a negative value, the reported latency is smaller
because the values are not truncated twice.
Additionally let PA_SOURCE_MESSAGE_GET_LATENCY return -pa_sink_get_latency_within_thread()
for monitor sources because the source gets the data before it is played.
			
			
This commit is contained in:
		
							parent
							
								
									a5fccd4645
								
							
						
					
					
						commit
						fe70b9e11a
					
				
					 38 changed files with 137 additions and 139 deletions
				
			
		| 
						 | 
				
			
			@ -1502,7 +1502,7 @@ int pa_sink_update_rate(pa_sink *s, uint32_t rate, bool passthrough) {
 | 
			
		|||
 | 
			
		||||
/* Called from main thread */
 | 
			
		||||
pa_usec_t pa_sink_get_latency(pa_sink *s) {
 | 
			
		||||
    pa_usec_t usec = 0;
 | 
			
		||||
    int64_t usec = 0;
 | 
			
		||||
 | 
			
		||||
    pa_sink_assert_ref(s);
 | 
			
		||||
    pa_assert_ctl_context();
 | 
			
		||||
| 
						 | 
				
			
			@ -1518,19 +1518,19 @@ pa_usec_t pa_sink_get_latency(pa_sink *s) {
 | 
			
		|||
 | 
			
		||||
    pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL) == 0);
 | 
			
		||||
 | 
			
		||||
    /* usec is unsigned, so check that the offset can be added to usec without
 | 
			
		||||
    /* the return value is unsigned, so check that the offset can be added to usec without
 | 
			
		||||
     * underflowing. */
 | 
			
		||||
    if (-s->port_latency_offset <= (int64_t) usec)
 | 
			
		||||
    if (-s->port_latency_offset <= usec)
 | 
			
		||||
        usec += s->port_latency_offset;
 | 
			
		||||
    else
 | 
			
		||||
        usec = 0;
 | 
			
		||||
 | 
			
		||||
    return usec;
 | 
			
		||||
    return (pa_usec_t)usec;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Called from IO thread */
 | 
			
		||||
pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
 | 
			
		||||
    pa_usec_t usec = 0;
 | 
			
		||||
int64_t pa_sink_get_latency_within_thread(pa_sink *s, bool allow_negative) {
 | 
			
		||||
    int64_t usec = 0;
 | 
			
		||||
    pa_msgobject *o;
 | 
			
		||||
 | 
			
		||||
    pa_sink_assert_ref(s);
 | 
			
		||||
| 
						 | 
				
			
			@ -1551,11 +1551,9 @@ pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s) {
 | 
			
		|||
 | 
			
		||||
    o->process_msg(o, PA_SINK_MESSAGE_GET_LATENCY, &usec, 0, NULL);
 | 
			
		||||
 | 
			
		||||
    /* usec is unsigned, so check that the offset can be added to usec without
 | 
			
		||||
     * underflowing. */
 | 
			
		||||
    if (-s->thread_info.port_latency_offset <= (int64_t) usec)
 | 
			
		||||
        usec += s->thread_info.port_latency_offset;
 | 
			
		||||
    else
 | 
			
		||||
    /* If allow_negative is false, the call should only return positive values, */
 | 
			
		||||
    usec += s->thread_info.port_latency_offset;
 | 
			
		||||
    if (!allow_negative && usec < 0)
 | 
			
		||||
        usec = 0;
 | 
			
		||||
 | 
			
		||||
    return usec;
 | 
			
		||||
| 
						 | 
				
			
			@ -2629,7 +2627,7 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse
 | 
			
		|||
                 * same as the read index. */
 | 
			
		||||
 | 
			
		||||
                /* Get the latency of the sink */
 | 
			
		||||
                usec = pa_sink_get_latency_within_thread(s);
 | 
			
		||||
                usec = pa_sink_get_latency_within_thread(s, false);
 | 
			
		||||
                sink_nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
 | 
			
		||||
                total_nbytes = sink_nbytes + pa_memblockq_get_length(i->thread_info.render_memblockq);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -2691,7 +2689,7 @@ int pa_sink_process_msg(pa_msgobject *o, int code, void *userdata, int64_t offse
 | 
			
		|||
                 * rewind. */
 | 
			
		||||
 | 
			
		||||
                /* Get the latency of the sink */
 | 
			
		||||
                usec = pa_sink_get_latency_within_thread(s);
 | 
			
		||||
                usec = pa_sink_get_latency_within_thread(s, false);
 | 
			
		||||
                nbytes = pa_usec_to_bytes(usec, &s->sample_spec);
 | 
			
		||||
 | 
			
		||||
                if (nbytes > 0)
 | 
			
		||||
| 
						 | 
				
			
			@ -3585,7 +3583,7 @@ void pa_sink_volume_change_push(pa_sink *s) {
 | 
			
		|||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    nc->at = pa_sink_get_latency_within_thread(s);
 | 
			
		||||
    nc->at = pa_sink_get_latency_within_thread(s, false);
 | 
			
		||||
    nc->at += pa_rtclock_now() + s->thread_info.volume_change_extra_delay;
 | 
			
		||||
 | 
			
		||||
    if (s->thread_info.volume_changes_tail) {
 | 
			
		||||
| 
						 | 
				
			
			@ -3695,7 +3693,7 @@ static void pa_sink_volume_change_rewind(pa_sink *s, size_t nbytes) {
 | 
			
		|||
    pa_sink_volume_change *c;
 | 
			
		||||
    pa_volume_t prev_vol = pa_cvolume_avg(&s->thread_info.current_hw_volume);
 | 
			
		||||
    pa_usec_t rewound = pa_bytes_to_usec(nbytes, &s->sample_spec);
 | 
			
		||||
    pa_usec_t limit = pa_sink_get_latency_within_thread(s);
 | 
			
		||||
    pa_usec_t limit = pa_sink_get_latency_within_thread(s, false);
 | 
			
		||||
 | 
			
		||||
    pa_log_debug("latency = %lld", (long long) limit);
 | 
			
		||||
    limit += pa_rtclock_now() + s->thread_info.volume_change_extra_delay;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue