From f6a83a91e06d344d0ef515d669bc750e6d40eaa5 Mon Sep 17 00:00:00 2001 From: Pauli Virtanen Date: Tue, 15 Feb 2022 11:40:36 +0200 Subject: [PATCH] bluez5: a2dp-sink: Fail flush on EAGAIN instead of delaying it If we get an EAGAIN, the device has started lagging in processing its packets. We should not try to stuff the same packet in again, because the device will just lag more. Instead, just drop current data, and hope bitpool reduction takes care of it. Also increase bitpool only if the socket buffer is empty. --- spa/plugins/bluez5/a2dp-sink.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/spa/plugins/bluez5/a2dp-sink.c b/spa/plugins/bluez5/a2dp-sink.c index 4528d1651..755f4e88b 100644 --- a/spa/plugins/bluez5/a2dp-sink.c +++ b/spa/plugins/bluez5/a2dp-sink.c @@ -650,16 +650,25 @@ again: } written = flush_buffer(this); + if (written == -EAGAIN) { - spa_log_trace(this->log, "%p: delay flush", this); + spa_log_trace(this->log, "%p: fail flush", this); if (now_time - this->last_error > SPA_NSEC_PER_SEC / 2) { + spa_log_trace(this->log, "%p: reduce bitpool", this); this->codec->reduce_bitpool(this->codec_data); this->last_error = now_time; } - this->need_flush = true; - enable_flush(this, true); + + /* + * The socket buffer is full, and the device is not processing data + * fast enough, so should just skip this packet. There will be a sound + * glitch in any case. + */ + written = this->buffer_used; + reset_buffer(this); } - else if (written < 0) { + + if (written < 0) { spa_log_trace(this->log, "%p: error flushing %s", this, spa_strerror(written)); reset_buffer(this); @@ -669,7 +678,10 @@ again: else if (written > 0) { reset_buffer(this); if (now_time - this->last_error > SPA_NSEC_PER_SEC) { - this->codec->increase_bitpool(this->codec_data); + if (get_transport_unused_size(this) == (int)this->fd_buffer_size) { + spa_log_trace(this->log, "%p: increase bitpool", this); + this->codec->increase_bitpool(this->codec_data); + } this->last_error = now_time; } if (!spa_list_is_empty(&port->ready))