bluez5: change sink/source run state follow transport state

Allow asynchronous changes in transport state in the sinks/sources.

Also allow transport acquire to be actually synchronous, in this case it
must set transport state during acquire call.

Separate driver start/stop from transport start/stop.
This commit is contained in:
Pauli Virtanen 2023-03-06 22:40:33 +02:00 committed by P V
parent 60718c4b4f
commit 1d5c693d33
5 changed files with 439 additions and 145 deletions

View file

@ -114,7 +114,8 @@ struct impl {
struct port port;
unsigned int started:1;
unsigned int transport_acquired:1;
unsigned int start_ready:1;
unsigned int transport_started:1;
unsigned int following:1;
unsigned int matching:1;
unsigned int resampling:1;
@ -258,7 +259,8 @@ static int do_reassign_follower(struct spa_loop *loop,
struct port *port = &this->port;
set_timers(this);
spa_bt_decode_buffer_recover(&port->buffer);
if (this->transport_started)
spa_bt_decode_buffer_recover(&port->buffer);
return 0;
}
@ -561,6 +563,9 @@ static int setup_matching(struct impl *this)
{
struct port *port = &this->port;
if (!this->transport_started)
port->buffer.corr = 1.0;
if (this->position && port->rate_match) {
port->rate_match->rate = 1 / port->buffer.corr;
@ -643,15 +648,15 @@ static int transport_start(struct impl *this)
struct port *port = &this->port;
uint32_t flags;
if (this->transport_acquired)
if (this->transport_started)
return 0;
if (!this->start_ready)
return -EIO;
spa_log_debug(this->log, "%p: transport %p acquire", this,
this->transport);
if ((res = spa_bt_transport_acquire(this->transport, false)) < 0)
return res;
spa_return_val_if_fail(this->transport != NULL, -EIO);
this->transport_acquired = true;
spa_log_debug(this->log, "%p: start transport state:%d",
this, this->transport->state);
flags = this->is_duplex ? 0 : MEDIA_CODEC_FLAG_SINK;
@ -724,25 +729,16 @@ static int transport_start(struct impl *this)
set_duplex_timeout(this, this->duplex_timeout);
}
this->timer_source.data = this;
this->timer_source.fd = this->timerfd;
this->timer_source.func = media_on_timeout;
this->timer_source.mask = SPA_IO_IN;
this->timer_source.rmask = 0;
spa_loop_add_source(this->data_loop, &this->timer_source);
this->sample_count = 0;
setup_matching(this);
set_timers(this);
this->transport_started = true;
return 0;
}
static int do_start(struct impl *this)
{
int res = 0;
int res;
if (this->started)
return 0;
@ -751,16 +747,31 @@ static int do_start(struct impl *this)
this->following = is_following(this);
spa_log_debug(this->log, "%p: start state:%d following:%d",
this, this->transport->state, this->following);
this->start_ready = true;
if (this->transport->state >= SPA_BT_TRANSPORT_STATE_PENDING ||
this->is_duplex || this->codec->bap)
res = transport_start(this);
spa_log_debug(this->log, "%p: start following:%d", this, this->following);
spa_log_debug(this->log, "%p: transport %p acquire", this,
this->transport);
if ((res = spa_bt_transport_acquire(this->transport, false)) < 0) {
this->start_ready = false;
return res;
}
this->timer_source.data = this;
this->timer_source.fd = this->timerfd;
this->timer_source.func = media_on_timeout;
this->timer_source.mask = SPA_IO_IN;
this->timer_source.rmask = 0;
spa_loop_add_source(this->data_loop, &this->timer_source);
setup_matching(this);
set_timers(this);
this->started = true;
return res;
return 0;
}
static int do_remove_source(struct spa_loop *loop,
@ -775,11 +786,6 @@ static int do_remove_source(struct spa_loop *loop,
spa_log_debug(this->log, "%p: remove source", this);
set_duplex_timeout(this, 0);
if (this->source.loop)
spa_loop_remove_source(this->data_loop, &this->source);
if (this->timer_source.loop)
spa_loop_remove_source(this->data_loop, &this->timer_source);
ts.it_value.tv_sec = 0;
@ -791,34 +797,48 @@ static int do_remove_source(struct spa_loop *loop,
return 0;
}
static int transport_stop(struct impl *this)
static int do_remove_transport_source(struct spa_loop *loop,
bool async,
uint32_t seq,
const void *data,
size_t size,
void *user_data)
{
struct impl *this = user_data;
spa_log_debug(this->log, "%p: remove transport source", this);
this->transport_started = false;
set_duplex_timeout(this, 0);
if (this->source.loop)
spa_loop_remove_source(this->data_loop, &this->source);
return 0;
}
static void transport_stop(struct impl *this)
{
struct port *port = &this->port;
int res;
if (!this->transport_started)
return;
spa_log_debug(this->log, "%p: transport stop", this);
spa_loop_invoke(this->data_loop, do_remove_source, 0, NULL, 0, true, this);
spa_loop_invoke(this->data_loop, do_remove_transport_source, 0, NULL, 0, true, this);
if (this->fd >= 0) {
close(this->fd);
this->fd = -1;
}
if (this->transport && this->transport_acquired)
res = spa_bt_transport_release(this->transport);
else
res = 0;
this->transport_acquired = false;
if (this->codec_data)
this->codec->deinit(this->codec_data);
this->codec_data = NULL;
spa_bt_decode_buffer_clear(&port->buffer);
return res;
}
static int do_stop(struct impl *this)
@ -830,7 +850,16 @@ static int do_stop(struct impl *this)
spa_log_debug(this->log, "%p: stop", this);
res = transport_stop(this);
this->start_ready = false;
spa_loop_invoke(this->data_loop, do_remove_source, 0, NULL, 0, true, this);
transport_stop(this);
if (this->transport)
res = spa_bt_transport_release(this->transport);
else
res = 0;
this->started = false;
@ -1450,7 +1479,7 @@ static int produce_buffer(struct impl *this)
}
/* Handle buffering */
if (this->started)
if (this->transport_started)
process_buffering(this);
/* Return if there are no buffers ready to be processed */
@ -1482,6 +1511,9 @@ static int impl_node_process(void *object)
if ((io = port->io) == NULL)
return -EIO;
if (!this->started || !this->transport_started)
return SPA_STATUS_OK;
spa_log_trace(this->log, "%p status:%d", this, io->status);
/* Return if we already have a buffer */
@ -1520,6 +1552,30 @@ static const struct spa_node_methods impl_node = {
.process = impl_node_process,
};
static void transport_state_changed(void *data,
enum spa_bt_transport_state old,
enum spa_bt_transport_state state)
{
struct impl *this = data;
spa_log_debug(this->log, "%p: transport %p state %d->%d", this, this->transport, old, state);
if (state == SPA_BT_TRANSPORT_STATE_ACTIVE)
transport_start(this);
else
transport_stop(this);
if (state == SPA_BT_TRANSPORT_STATE_ERROR) {
uint8_t buffer[1024];
struct spa_pod_builder b = { 0 };
spa_pod_builder_init(&b, buffer, sizeof(buffer));
spa_node_emit_event(&this->hooks,
spa_pod_builder_add_object(&b,
SPA_TYPE_EVENT_Node, SPA_NODE_EVENT_Error));
}
}
static void transport_delay_changed(void *data)
{
struct impl *this = data;
@ -1537,7 +1593,6 @@ static int do_transport_destroy(struct spa_loop *loop,
{
struct impl *this = user_data;
this->transport = NULL;
this->transport_acquired = false;
return 0;
}
@ -1551,6 +1606,7 @@ static void transport_destroy(void *data)
static const struct spa_bt_transport_events transport_events = {
SPA_VERSION_BT_TRANSPORT_EVENTS,
.delay_changed = transport_delay_changed,
.state_changed = transport_state_changed,
.destroy = transport_destroy,
};