bluez5: Fix sink timeout for BAP

Data should be written to the ISO Stream fd every 10ms or 7.5ms depending
on the configuration selected.
This commit is contained in:
Frédéric Danis 2022-07-22 18:18:54 +02:00 committed by Wim Taymans
parent 071730ecab
commit fd1b331353

View file

@ -172,6 +172,10 @@ struct impl {
uint8_t tmp_buffer[BUFFER_SIZE];
uint32_t tmp_buffer_used;
uint32_t fd_buffer_size;
/* Times */
uint64_t start_time;
uint64_t total_samples;
};
#define CHECK_PORT(this,d,p) ((d) == SPA_DIRECTION_INPUT && (p) == 0)
@ -618,6 +622,27 @@ static void enable_flush(struct impl *this, bool enabled, uint64_t timeout)
this->flush_timerfd, 0, &ts, NULL);
}
static uint64_t get_next_bap_timeout(struct impl *this)
{
struct port *port = &this->port;
uint64_t playback_time = 0, elapsed_time = 0, next_time = 0;
struct timespec now;
uint64_t now_time;
spa_system_clock_gettime(this->data_system, CLOCK_MONOTONIC, &now);
now_time = SPA_TIMESPEC_TO_NSEC(&now);
if (this->start_time == 0)
this->start_time = now_time;
playback_time = (this->total_samples * SPA_NSEC_PER_SEC) / port->current_format.info.raw.rate;
if (now_time > this->start_time)
elapsed_time = now_time - this->start_time;
if (elapsed_time < playback_time)
next_time = playback_time - elapsed_time;
return next_time;
}
static int flush_data(struct impl *this, uint64_t now_time)
{
int written;
@ -694,6 +719,7 @@ again:
port->ready_offset = 0;
}
total_frames += n_frames;
this->total_samples += n_frames;
spa_log_trace(this->log, "%p: written %u frames", this, total_frames);
}
@ -730,6 +756,20 @@ again:
return written;
}
else if (written > 0) {
if (this->codec->bap) {
uint64_t timeout = get_next_bap_timeout(this);
reset_buffer(this);
if (!spa_list_is_empty(&port->ready)) {
spa_log_debug(this->log, "%p: flush after %d ns", this, (unsigned int)timeout);
if (timeout == 0)
goto again;
else
enable_flush(this, true, timeout);
} else {
enable_flush(this, false, 0);
}
} else {
/*
* We cannot write all data we have at once, since this can exceed
* device buffers. We'll want a limited number of "excess"
@ -780,6 +820,7 @@ again:
enable_flush(this, false, 0);
}
}
}
else {
/* Don't want to flush yet, or failed to write anything */
spa_log_trace(this->log, "%p: skip flush", this);
@ -998,6 +1039,9 @@ static int do_remove_source(struct spa_loop *loop,
struct impl *this = user_data;
struct itimerspec ts;
this->start_time = 0;
this->total_samples = 0;
if (this->source.loop)
spa_loop_remove_source(this->data_loop, &this->source);
ts.it_value.tv_sec = 0;