From 46bf99858a0eacffe0d0d01fb7d5781814ce2ec9 Mon Sep 17 00:00:00 2001 From: Mike Brady Date: Fri, 22 May 2015 17:08:57 +0000 Subject: [PATCH] Uses the dac buffer size to offset the timing of the packets by a fixed amount --- player.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/player.c b/player.c index 6840e8ff..838b0e23 100644 --- a/player.c +++ b/player.c @@ -511,8 +511,16 @@ static abuf_t *buffer_get_frame(void) { // it might not be the timestamp of our first packet, however, so we might have to do some calculations. int64_t delta = ((int64_t)first_packet_timestamp-(int64_t)reference_timestamp); + + // Now, if the back end can tell us about its latency, we will set the first packet's time to play to be the exact time. + // Otherwise we will set it to the exact time less the config.dac_buffer_queue_desired_length, a proxy for the lead time we want the stream to appear at the output + // We will probably change this in the future to pick up the lead time from the back end itself + + if (config.output->delay) + first_packet_time_to_play = reference_timestamp_time+((delta+(int64_t)config.latency)<<32)/44100; // using the latency requested... + else + first_packet_time_to_play = reference_timestamp_time+((delta+(int64_t)config.latency-(int64_t)config.dac_buffer_queue_desired_length)<<32)/44100; // using the latency requested... - first_packet_time_to_play = reference_timestamp_time+((delta+(int64_t)config.latency)<<32)/44100; // using the latency requested... if (local_time_now>=first_packet_time_to_play) { debug(1,"First packet is late! It should have played before now. Flushing 0.1 seconds"); player_flush(first_packet_timestamp+4410); @@ -589,7 +597,20 @@ static abuf_t *buffer_get_frame(void) { if (reference_timestamp) { // if we have a reference time uint32_t packet_timestamp=curframe->timestamp; int64_t delta = ((int64_t)packet_timestamp-(int64_t)reference_timestamp); - int64_t time_to_play = reference_timestamp_time+((delta+(int64_t)(config.latency-config.dac_buffer_queue_desired_length))<<32)/44100; // using the latency requested... + int64_t offset = (int64_t)config.latency-(int64_t)config.dac_buffer_queue_desired_length; + int64_t net_offset = delta+offset; + int64_t time_to_play = reference_timestamp_time; + int64_t net_offset_fp_sec; + if (net_offset>=0) { + net_offset_fp_sec = (net_offset<<32)/44100; + time_to_play+=net_offset_fp_sec; // using the latency requested... + debug(2,"Net Offset: %lld, adjusted: %lld.",net_offset,net_offset_fp_sec); + } else { + net_offset_fp_sec = ((-net_offset)<<32)/44100; + time_to_play-=net_offset_fp_sec; + debug(2,"Net Offset: %lld, adjusted: -%lld.",net_offset,net_offset_fp_sec); + } + if (local_time_now>=time_to_play) { do_wait = 0; }