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.
u->smoother_interval = PA_MIN (u->smoother_interval * 2, SMOOTHER_MAX_INTERVAL);
}
-static pa_usec_t sink_get_latency(struct userdata *u) {
- pa_usec_t r;
+static int64_t sink_get_latency(struct userdata *u) {
int64_t delay;
pa_usec_t now1, now2;
delay = (int64_t) pa_bytes_to_usec(u->write_count, &u->sink->sample_spec) - (int64_t) now2;
- r = delay >= 0 ? (pa_usec_t) delay : 0;
-
if (u->memchunk.memblock)
- r += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
+ delay += pa_bytes_to_usec(u->memchunk.length, &u->sink->sample_spec);
- return r;
+ return delay;
}
static int build_pollfd(struct userdata *u) {
switch (code) {
case PA_SINK_MESSAGE_GET_LATENCY: {
- pa_usec_t r = 0;
+ int64_t r = 0;
if (u->pcm_handle)
r = sink_get_latency(u);
- *((pa_usec_t*) data) = r;
+ *((int64_t*) data) = r;
return 0;
}
u->smoother_interval = PA_MIN (u->smoother_interval * 2, SMOOTHER_MAX_INTERVAL);
}
-static pa_usec_t source_get_latency(struct userdata *u) {
+static int64_t source_get_latency(struct userdata *u) {
int64_t delay;
pa_usec_t now1, now2;
delay = (int64_t) now2 - (int64_t) pa_bytes_to_usec(u->read_count, &u->source->sample_spec);
- return delay >= 0 ? (pa_usec_t) delay : 0;
+ return delay;
}
static int build_pollfd(struct userdata *u) {
switch (code) {
case PA_SOURCE_MESSAGE_GET_LATENCY: {
- pa_usec_t r = 0;
+ int64_t r = 0;
if (u->pcm_handle)
r = source_get_latency(u);
- *((pa_usec_t*) data) = r;
+ *((int64_t*) data) = r;
return 0;
}
case PA_SINK_MESSAGE_GET_LATENCY: {
if (u->read_smoother) {
- pa_usec_t wi, ri;
+ int64_t wi, ri;
ri = pa_smoother_get(u->read_smoother, pa_rtclock_now());
wi = pa_bytes_to_usec(u->write_index + u->write_block_size, &u->sample_spec);
- *((pa_usec_t*) data) = wi > ri ? wi - ri : 0;
+ *((int64_t*) data) = wi - ri;
} else {
- pa_usec_t ri, wi;
+ int64_t ri, wi;
ri = pa_rtclock_now() - u->started_at;
wi = pa_bytes_to_usec(u->write_index, &u->sample_spec);
- *((pa_usec_t*) data) = wi > ri ? wi - ri : 0;
+ *((int64_t*) data) = wi - ri;
}
- *((pa_usec_t*) data) += u->sink->thread_info.fixed_latency;
+ *((int64_t*) data) += u->sink->thread_info.fixed_latency;
return 0;
}
}
break;
case PA_SOURCE_MESSAGE_GET_LATENCY: {
- pa_usec_t wi, ri;
+ int64_t wi, ri;
if (u->read_smoother) {
wi = pa_smoother_get(u->read_smoother, pa_rtclock_now());
ri = pa_bytes_to_usec(u->read_index, &u->sample_spec);
- *((pa_usec_t*) data) = (wi > ri ? wi - ri : 0) + u->source->thread_info.fixed_latency;
+ *((int64_t*) data) = wi - ri + u->source->thread_info.fixed_latency;
} else
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
break;
case PA_SOURCE_MESSAGE_GET_LATENCY: {
- pa_usec_t wi, ri;
+ int64_t wi, ri;
if (u->read_smoother) {
wi = pa_smoother_get(u->read_smoother, pa_rtclock_now());
ri = pa_bytes_to_usec(u->read_index, &u->sample_spec);
- *((pa_usec_t*) data) = u->source->thread_info.fixed_latency + wi > ri ? u->source->thread_info.fixed_latency + wi - ri : 0;
+ *((int64_t*) data) = u->source->thread_info.fixed_latency + wi - ri;
} else
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
break;
case PA_SINK_MESSAGE_GET_LATENCY: {
- pa_usec_t wi, ri;
+ int64_t wi, ri;
if (u->read_smoother) {
ri = pa_smoother_get(u->read_smoother, pa_rtclock_now());
wi = pa_bytes_to_usec(u->write_index, &u->sample_spec);
}
- *((pa_usec_t*) data) = u->sink->thread_info.fixed_latency + wi > ri ? u->sink->thread_info.fixed_latency + wi - ri : 0;
+ *((int64_t*) data) = u->sink->thread_info.fixed_latency + wi - ri;
return 0;
}
* source output is first shut down, the source second. */
if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state) ||
!PA_SOURCE_OUTPUT_IS_LINKED(u->source_output->thread_info.state)) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
- *((pa_usec_t*) data) =
+ *((int64_t*) data) =
/* Get the latency of the master source */
- pa_source_get_latency_within_thread(u->source_output->source) +
+ pa_source_get_latency_within_thread(u->source_output->source, true) +
/* Add the latency internal to our source output on top */
pa_bytes_to_usec(pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq), &u->source_output->source->sample_spec) +
/* and the buffering we do on the source */
* sink input is first shut down, the sink second. */
if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
!PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
- *((pa_usec_t*) data) =
+ *((int64_t*) data) =
/* Get the latency of the master sink */
- pa_sink_get_latency_within_thread(u->sink_input->sink) +
+ pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
/* Add the latency internal to our sink input on top */
pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
pa_usec_t now, latency;
now = pa_rtclock_now();
- latency = pa_source_get_latency_within_thread(u->source_output->source);
+ latency = pa_source_get_latency_within_thread(u->source_output->source, false);
delay = pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq);
delay = (u->source_output->thread_info.resampler ? pa_resampler_request(u->source_output->thread_info.resampler, delay) : delay);
pa_sink_input_assert_io_context(u->sink_input);
now = pa_rtclock_now();
- latency = pa_sink_get_latency_within_thread(u->sink_input->sink);
+ latency = pa_sink_get_latency_within_thread(u->sink_input->sink, false);
delay = pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq);
delay = (u->sink_input->thread_info.resampler ? pa_resampler_request(u->sink_input->thread_info.resampler, delay) : delay);
jack_nframes_t l, ft, d;
jack_latency_range_t r;
size_t n;
+ int32_t number_of_frames;
/* This is the "worst-case" latency */
jack_port_get_latency_range(u->port[0], JackPlaybackLatency, &r);
ft = jack_frame_time(u->client);
d = ft > u->saved_frame_time ? ft - u->saved_frame_time : 0;
- l = l > d ? l - d : 0;
+ number_of_frames = (int32_t)l - d;
}
/* Convert it to usec */
- n = l * pa_frame_size(&u->sink->sample_spec);
- *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
+ if (number_of_frames > 0) {
+ n = number_of_frames * pa_frame_size(&u->sink->sample_spec);
+ *((int64_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
+ } else {
+ n = - number_of_frames * pa_frame_size(&u->sink->sample_spec);
+ *((int64_t*) data) = - (int64_t)pa_bytes_to_usec(n, &u->sink->sample_spec);
+ }
return 0;
}
/* Convert it to usec */
n = l * pa_frame_size(&u->source->sample_spec);
- *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
+ *((int64_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
return 0;
}
}
case PA_SINK_MESSAGE_GET_LATENCY: {
- *((pa_usec_t *) data) = get_latency_us(PA_OBJECT(o));
+ *((int64_t *) data) = get_latency_us(PA_OBJECT(o));
return 0;
}
}
}
case PA_SOURCE_MESSAGE_GET_LATENCY: {
- *((pa_usec_t *) data) = get_latency_us(PA_OBJECT(o));
+ *((int64_t *) data) = get_latency_us(PA_OBJECT(o));
return 0;
}
}
}
case PA_SINK_MESSAGE_GET_LATENCY: {
- pa_usec_t x, y, c, *delay = data;
+ pa_usec_t x, y, c;
+ int64_t *delay = data;
x = pa_rtclock_now();
y = pa_smoother_get(u->thread_info.smoother, x);
c = pa_bytes_to_usec(u->thread_info.counter, &u->sink->sample_spec);
- if (y < c)
- *delay = c - y;
- else
- *delay = 0;
+ *delay = (int64_t)c - y;
return 0;
}
* sink input is first shut down, the sink second. */
if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
!PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
- *((pa_usec_t*) data) =
+ *((int64_t*) data) =
/* Get the latency of the master sink */
- pa_sink_get_latency_within_thread(u->sink_input->sink) +
+ pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
/* Add the latency internal to our sink input on top */
pa_bytes_to_usec(pa_memblockq_get_length(u->output_q) +
r = pa_smoother_get(u->smoother, pa_rtclock_now());
w = pa_bytes_to_usec((uint64_t) u->offset + u->memchunk.length, &u->sink->sample_spec);
- *((pa_usec_t*) data) = w > r ? w - r : 0;
+ *((int64_t*) data) = (int64_t)w - r;
return 0;
}
* sink input is first shut down, the sink second. */
if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
!PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
- *((pa_usec_t*) data) =
+ *((int64_t*) data) =
/* Get the latency of the master sink */
- pa_sink_get_latency_within_thread(u->sink_input->sink) +
+ pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
/* Add the latency internal to our sink input on top */
pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
/* Send current source latency and timestamp with the message */
push_time = pa_rtclock_now();
- current_source_latency = pa_source_get_latency_within_thread(u->source_output->source);
+ current_source_latency = pa_source_get_latency_within_thread(u->source_output->source, false);
pa_asyncmsgq_post(u->asyncmsgq, PA_MSGOBJECT(u->sink_input), SINK_INPUT_MESSAGE_POST, PA_UINT_TO_PTR(current_source_latency), push_time, chunk, NULL);
u->send_counter += (int64_t) chunk->length;
u->latency_snapshot.send_counter = u->send_counter;
/* Add content of delay memblockq to the source latency */
- u->latency_snapshot.source_latency = pa_source_get_latency_within_thread(u->source_output->source) +
+ u->latency_snapshot.source_latency = pa_source_get_latency_within_thread(u->source_output->source, false) +
pa_bytes_to_usec(length, &u->source_output->source->sample_spec);
u->latency_snapshot.source_timestamp = pa_rtclock_now();
/* Add the time between push and post */
time_delta += pa_rtclock_now() - (pa_usec_t) offset;
/* Add the sink latency */
- time_delta += pa_sink_get_latency_within_thread(u->sink_input->sink);
+ time_delta += pa_sink_get_latency_within_thread(u->sink_input->sink, false);
/* The source latency report includes the audio in the chunk,
* but since we already pushed the chunk to the memblockq, we need
u->latency_snapshot.recv_counter = u->output_thread_info.recv_counter;
u->latency_snapshot.loopback_memblockq_length = pa_memblockq_get_length(u->memblockq);
/* Add content of render memblockq to sink latency */
- u->latency_snapshot.sink_latency = pa_sink_get_latency_within_thread(u->sink_input->sink) +
+ u->latency_snapshot.sink_latency = pa_sink_get_latency_within_thread(u->sink_input->sink, false) +
pa_bytes_to_usec(length, &u->sink_input->sink->sample_spec);
u->latency_snapshot.sink_timestamp = pa_rtclock_now();
pa_usec_t now;
now = pa_rtclock_now();
- *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0ULL;
+ *((int64_t*) data) = (int64_t)u->timestamp - (int64_t)now;
return 0;
}
pa_usec_t now;
now = pa_rtclock_now();
- *((pa_usec_t*) data) = u->timestamp > now ? u->timestamp - now : 0;
+ *((int64_t*) data) = (int64_t)u->timestamp - (int64_t)now;
return 0;
}
n += u->memchunk.length;
- *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
+ *((int64_t*) data) = pa_bytes_to_usec(n, &u->sink->sample_spec);
return 0;
}
}
n = (size_t) l;
#endif
- *((pa_usec_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
+ *((int64_t*) data) = pa_bytes_to_usec(n, &u->source->sample_spec);
return 0;
}
}
* make sure we don't access it yet */
if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
!PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
- *((pa_usec_t*) data) =
+ *((int64_t*) data) =
/* Get the latency of the master sink */
- pa_sink_get_latency_within_thread(u->sink_input->sink) +
+ pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
/* Add the latency internal to our sink input on top */
pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
* source output is first shut down, the source second. */
if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state) ||
!PA_SOURCE_OUTPUT_IS_LINKED(u->source_output->thread_info.state)) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
- *((pa_usec_t*) data) =
+ *((int64_t*) data) =
/* Get the latency of the master source */
- pa_source_get_latency_within_thread(u->source_output->source) +
+ pa_source_get_latency_within_thread(u->source_output->source, true) +
/* Add the latency internal to our source output on top */
pa_bytes_to_usec(pa_memblockq_get_length(u->source_output->thread_info.delay_memblockq), &u->source_output->source->sample_spec);
now = pa_rtclock_now();
left_to_fill = u->timestamp > now ? u->timestamp - now : 0ULL;
- *((pa_usec_t*) data) = u->block_usec > left_to_fill ? u->block_usec - left_to_fill : 0ULL;
+ *((int64_t*) data) = (int64_t)u->block_usec - left_to_fill;
return 0;
}
switch (code) {
case PA_SINK_MESSAGE_GET_LATENCY:
- *((pa_usec_t*) data) = sink_get_latency(u, &PA_SINK(o)->sample_spec);
+ *((int64_t*) data) = sink_get_latency(u, &PA_SINK(o)->sample_spec);
return 0;
case PA_SINK_MESSAGE_SET_STATE:
pa_usec_t remote_latency;
if (!PA_SINK_IS_LINKED(u->sink->thread_info.state)) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
if (!u->stream) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
if (pa_stream_get_state(u->stream) != PA_STREAM_READY) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
if (pa_stream_get_latency(u->stream, &remote_latency, &negative) < 0) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
- *((pa_usec_t*) data) = remote_latency;
+ *((int64_t*) data) = remote_latency;
return 0;
}
case PA_SINK_MESSAGE_SET_STATE:
pa_usec_t remote_latency;
if (!PA_SOURCE_IS_LINKED(u->source->thread_info.state)) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
if (!u->stream) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
if (pa_stream_get_state(u->stream) != PA_STREAM_READY) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
if (pa_stream_get_latency(u->stream, &remote_latency, &negative) < 0) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
if (negative)
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = - (int64_t)remote_latency;
else
- *((pa_usec_t*) data) = remote_latency;
+ *((int64_t*) data) = remote_latency;
return 0;
}
}
case PA_SINK_MESSAGE_GET_LATENCY: {
- pa_usec_t yl, yr, *usec = data;
+ pa_usec_t yl, yr;
+ int64_t *usec = data;
yl = pa_bytes_to_usec((uint64_t) u->counter, &u->sink->sample_spec);
yr = pa_smoother_get(u->smoother, pa_rtclock_now());
- *usec = yl > yr ? yl - yr : 0;
+ *usec = (int64_t)yl - yr;
return 0;
}
}
case PA_SOURCE_MESSAGE_GET_LATENCY: {
- pa_usec_t yr, yl, *usec = data;
+ pa_usec_t yr, yl;
+ int64_t *usec = data;
yl = pa_bytes_to_usec((uint64_t) u->counter, &PA_SOURCE(o)->sample_spec);
yr = pa_smoother_get(u->smoother, pa_rtclock_now());
- *usec = yr > yl ? yr - yl : 0;
+ *usec = (int64_t)yr - yl;
return 0;
}
* sink input is first shut down, the sink second. */
if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
!PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
- *((pa_usec_t*) data) =
+ *((int64_t*) data) =
/* Get the latency of the master sink */
- pa_sink_get_latency_within_thread(u->sink_input->sink) +
+ pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
/* Add the latency internal to our sink input on top */
pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
/* (4) IF YOU NEED THE LATENCY FOR SOMETHING ACQUIRE IT LIKE THIS: */
current_latency =
/* Get the latency of the master sink */
- pa_sink_get_latency_within_thread(i->sink) +
+ pa_sink_get_latency_within_thread(i->sink, false) +
/* Add the latency internal to our sink input on top */
pa_bytes_to_usec(pa_memblockq_get_length(i->thread_info.render_memblockq), &i->sink->sample_spec);
case PA_SINK_MESSAGE_GET_LATENCY:
/* there's no real latency here */
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
*((pa_usec_t*) data) =
/* Get the latency of the master source */
- pa_source_get_latency_within_thread(u->source_output->source) +
+ pa_source_get_latency_within_thread(u->source_output->source, true) +
/* Add the latency internal to our source output on top */
/* FIXME, no idea what I am doing here */
#if 0
if (PA_SOURCE_OUTPUT_IS_LINKED(state) && o->thread_info.state == PA_SOURCE_OUTPUT_INIT && o->source) {
- u->skip = pa_usec_to_bytes(PA_CLIP_SUB(pa_source_get_latency_within_thread(o->source),
+ u->skip = pa_usec_to_bytes(PA_CLIP_SUB(pa_source_get_latency_within_thread(o->source, false),
u->latency),
&o->sample_spec);
* sink input is first shut down, the sink second. */
if (!PA_SINK_IS_LINKED(u->sink->thread_info.state) ||
!PA_SINK_INPUT_IS_LINKED(u->sink_input->thread_info.state)) {
- *((pa_usec_t*) data) = 0;
+ *((int64_t*) data) = 0;
return 0;
}
- *((pa_usec_t*) data) =
+ *((int64_t*) data) =
/* Get the latency of the master sink */
- pa_sink_get_latency_within_thread(u->sink_input->sink) +
+ pa_sink_get_latency_within_thread(u->sink_input->sink, true) +
/* Add the latency internal to our sink input on top */
pa_bytes_to_usec(pa_memblockq_get_length(u->sink_input->thread_info.render_memblockq), &u->sink_input->sink->sample_spec);
pa_usec_t r = 0;
if (u->hwo)
r = sink_get_latency(u);
- *((pa_usec_t*) data) = r;
+ *((int64_t*) data) = (int64_t)r;
return 0;
}
pa_usec_t r = 0;
if (u->hwi)
r = source_get_latency(u);
- *((pa_usec_t*) data) = r;
+ *((int64_t*) data) = (int64_t)r;
return 0;
}
r = io_sink_get_latency(u);
}
- *((pa_usec_t*) data) = r;
+ *((int64_t*) data) = (int64_t)r;
return 0;
}
r = io_source_get_latency(u);
}
- *((pa_usec_t*) data) = r;
+ *((int64_t*) data) = (int64_t)r;
return 0;
}
pa_asyncmsgq_post(u->thread_mq.inq, PA_MSGOBJECT(u->sink), PA_SINK_MESSAGE_SET_RAOP_STATE, PA_INT_TO_PTR(state), 0, NULL, NULL);
}
-static pa_usec_t sink_get_latency(const struct userdata *u) {
- pa_usec_t r, now;
+static int64_t sink_get_latency(const struct userdata *u) {
+ pa_usec_t now;
int64_t latency;
pa_assert(u);
now = pa_smoother_get(u->smoother, now);
latency = pa_bytes_to_usec(u->write_count, &u->sink->sample_spec) - (int64_t) now;
- r = latency >= 0 ? (pa_usec_t) latency : 0;
- return r;
+ return latency;
}
static int sink_process_msg(pa_msgobject *o, int code, void *data, int64_t offset, pa_memchunk *chunk) {
}
case PA_SINK_MESSAGE_GET_LATENCY: {
- pa_usec_t r = 0;
+ int64_t r = 0;
if (pa_raop_client_can_stream(u->raop))
r = sink_get_latency(u);
- *((pa_usec_t*) data) = r;
+ *((int64_t*) data) = r;
return 0;
}
pa_log_debug("wi=%lu ri=%lu", (unsigned long) wi, (unsigned long) ri);
- sink_delay = pa_sink_get_latency_within_thread(s->sink_input->sink);
+ sink_delay = pa_sink_get_latency_within_thread(s->sink_input->sink, false);
render_delay = pa_bytes_to_usec(pa_memblockq_get_length(s->sink_input->thread_info.render_memblockq), &s->sink_input->sink->sample_spec);
if (ri > render_delay+sink_delay)
s->read_index = pa_memblockq_get_read_index(s->memblockq);
s->write_index = pa_memblockq_get_write_index(s->memblockq);
s->render_memblockq_length = pa_memblockq_get_length(s->sink_input->thread_info.render_memblockq);
- s->current_sink_latency = pa_sink_get_latency_within_thread(s->sink_input->sink);
+ s->current_sink_latency = pa_sink_get_latency_within_thread(s->sink_input->sink, false);
s->underrun_for = s->sink_input->thread_info.underrun_for;
s->playing_for = s->sink_input->thread_info.playing_for;
switch (code) {
case SOURCE_OUTPUT_MESSAGE_UPDATE_LATENCY:
/* Atomically get a snapshot of all timing parameters... */
- s->current_monitor_latency = o->source->monitor_of ? pa_sink_get_latency_within_thread(o->source->monitor_of) : 0;
- s->current_source_latency = pa_source_get_latency_within_thread(o->source);
+ s->current_monitor_latency = o->source->monitor_of ? pa_sink_get_latency_within_thread(o->source->monitor_of, false) : 0;
+ s->current_source_latency = pa_source_get_latency_within_thread(o->source, false);
s->on_the_fly_snapshot = pa_atomic_load(&s->on_the_fly);
return 0;
}
pa_usec_t *r = userdata;
r[0] += pa_bytes_to_usec(pa_memblockq_get_length(i->thread_info.render_memblockq), &i->sink->sample_spec);
- r[1] += pa_sink_get_latency_within_thread(i->sink);
+ r[1] += pa_sink_get_latency_within_thread(i->sink, false);
return 0;
}
/* 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();
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);
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;
* 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);
* 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)
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) {
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;
void pa_sink_invalidate_requested_latency(pa_sink *s, bool dynamic);
-pa_usec_t pa_sink_get_latency_within_thread(pa_sink *s);
+int64_t pa_sink_get_latency_within_thread(pa_sink *s, bool allow_negative);
/* Called from the main thread, from sink-input.c only. The normal way to set
* the sink reference volume is to call pa_sink_set_volume(), but the flat
* of the queued data is actually still changeable. Hence
* FIXME! */
- latency = pa_sink_get_latency_within_thread(o->source->monitor_of);
+ latency = pa_sink_get_latency_within_thread(o->source->monitor_of, false);
n = pa_usec_to_bytes(latency, &o->source->sample_spec);
pa_usec_t *r = userdata;
r[0] += pa_bytes_to_usec(pa_memblockq_get_length(o->thread_info.delay_memblockq), &o->source->sample_spec);
- r[1] += pa_source_get_latency_within_thread(o->source);
+ r[1] += pa_source_get_latency_within_thread(o->source, false);
return 0;
}
/* Called from main thread */
pa_usec_t pa_source_get_latency(pa_source *s) {
- pa_usec_t usec;
+ int64_t usec;
pa_source_assert_ref(s);
pa_assert_ctl_context();
pa_assert_se(pa_asyncmsgq_send(s->asyncmsgq, PA_MSGOBJECT(s), PA_SOURCE_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_source_get_latency_within_thread(pa_source *s) {
- pa_usec_t usec = 0;
+int64_t pa_source_get_latency_within_thread(pa_source *s, bool allow_negative) {
+ int64_t usec = 0;
pa_msgobject *o;
pa_source_assert_ref(s);
o->process_msg(o, PA_SOURCE_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;
case PA_SOURCE_MESSAGE_GET_LATENCY:
if (s->monitor_of) {
- *((pa_usec_t*) userdata) = 0;
+ *((int64_t*) userdata) = -pa_sink_get_latency_within_thread(s->monitor_of, true);
return 0;
}
return;
}
- nc->at = pa_source_get_latency_within_thread(s);
+ nc->at = pa_source_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) {
/*** To be called exclusively by source output drivers, from IO context */
void pa_source_invalidate_requested_latency(pa_source *s, bool dynamic);
-pa_usec_t pa_source_get_latency_within_thread(pa_source *s);
+int64_t pa_source_get_latency_within_thread(pa_source *s, bool allow_negative);
/* Called from the main thread, from source-output.c only. The normal way to
* set the source reference volume is to call pa_source_set_volume(), but the