a2dp-sink: don't force flush on every buffer

Don't force flush on every buffer but accumulate enough data
first. This avoids sending many small packets when the quantum is
small.

See #680
This commit is contained in:
Wim Taymans 2021-02-08 10:24:50 +01:00
parent e3d19e5602
commit cd2a7aebaf

View file

@ -364,13 +364,16 @@ static int send_buffer(struct impl *this)
update_num_blocks(this); update_num_blocks(this);
} }
spa_log_trace(this->log, NAME " %p: send %d %u %u %u", spa_log_trace(this->log, NAME " %p: send %d %u %u %u %u",
this, this->frame_count, this->seqnum, this->timestamp, this->buffer_used); this, this->frame_count, this->block_size, this->seqnum,
this->timestamp, this->buffer_used);
written = send(this->flush_source.fd, this->buffer, this->buffer_used, MSG_DONTWAIT | MSG_NOSIGNAL); written = send(this->flush_source.fd, this->buffer,
this->buffer_used, MSG_DONTWAIT | MSG_NOSIGNAL);
reset_buffer(this); reset_buffer(this);
spa_log_debug(this->log, NAME " %p: send %d", this, written); spa_log_trace(this->log, NAME " %p: send %d", this, written);
if (written < 0) { if (written < 0) {
spa_log_debug(this->log, NAME " %p: %m", this); spa_log_debug(this->log, NAME " %p: %m", this);
return -errno; return -errno;
@ -379,6 +382,11 @@ static int send_buffer(struct impl *this)
return written; return written;
} }
static bool want_flush(struct impl *this)
{
return (this->frame_count * this->block_size / this->port.frame_size >= MIN_LATENCY);
}
static bool need_flush(struct impl *this) static bool need_flush(struct impl *this)
{ {
return (this->frame_count >= this->num_blocks); return (this->frame_count >= this->num_blocks);
@ -435,12 +443,12 @@ static int encode_buffer(struct impl *this, const void *data, uint32_t size)
return processed; return processed;
} }
static int flush_buffer(struct impl *this, bool force) static int flush_buffer(struct impl *this)
{ {
spa_log_trace(this->log, NAME" %p: used:%d num_blocks:%d block_size:%d", this, spa_log_trace(this->log, NAME" %p: used:%d num_blocks:%d block_size:%d", this,
this->buffer_used, this->num_blocks, this->block_size); this->buffer_used, this->num_blocks, this->block_size);
if (force || need_flush(this)) if (want_flush(this) || need_flush(this))
return send_buffer(this); return send_buffer(this);
return 0; return 0;
@ -543,7 +551,7 @@ again:
return 0; return 0;
} }
written = flush_buffer(this, true); written = flush_buffer(this);
if (written == -EAGAIN) { if (written == -EAGAIN) {
spa_log_trace(this->log, NAME" %p: delay flush", this); spa_log_trace(this->log, NAME" %p: delay flush", this);
if (now_time - this->last_error > SPA_NSEC_PER_SEC / 2) { if (now_time - this->last_error > SPA_NSEC_PER_SEC / 2) {