spa: simplify start/pause

Use the sync option in invoke to implement stop.
This commit is contained in:
Wim Taymans 2018-01-18 11:57:23 +01:00
parent 67b06d4ad2
commit 155243a27c
6 changed files with 501 additions and 729 deletions

View file

@ -177,65 +177,31 @@ static int impl_node_set_param(struct spa_node *node, uint32_t id, uint32_t flag
return 0;
}
static int do_send_done(struct spa_loop *loop, bool async, uint32_t seq, const void *data, size_t size, void *user_data)
{
struct state *this = user_data;
this->callbacks->done(this->callbacks_data, seq, *(int*)data);
return 0;
}
static int do_command(struct spa_loop *loop, bool async, uint32_t seq, const void *data, size_t size, void *user_data)
{
struct state *this = user_data;
int res;
const struct spa_command *cmd = data;
if (SPA_COMMAND_TYPE(cmd) == this->type.command_node.Start ||
SPA_COMMAND_TYPE(cmd) == this->type.command_node.Pause) {
res = spa_node_port_send_command(&this->node, SPA_DIRECTION_INPUT, 0, cmd);
} else
res = -ENOTSUP;
if (async) {
spa_loop_invoke(this->main_loop,
do_send_done,
seq,
&res,
sizeof(res),
false,
this);
}
return res;
}
static int impl_node_send_command(struct spa_node *node, const struct spa_command *command)
{
struct state *this;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(command != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct state, node);
if (SPA_COMMAND_TYPE(command) == this->type.command_node.Start ||
SPA_COMMAND_TYPE(command) == this->type.command_node.Pause) {
if (SPA_COMMAND_TYPE(command) == this->type.command_node.Start) {
if (!this->have_format)
return -EIO;
if (this->n_buffers == 0)
return -EIO;
return spa_loop_invoke(this->data_loop,
do_command,
++this->seq,
command,
SPA_POD_SIZE(command),
false,
this);
if ((res = spa_alsa_start(this, false)) < 0)
return res;
} else if (SPA_COMMAND_TYPE(command) == this->type.command_node.Pause) {
if ((res = spa_alsa_pause(this, false)) < 0)
return res;
} else
return -ENOTSUP;
return 0;
}
static int
@ -601,23 +567,8 @@ static int
impl_node_port_send_command(struct spa_node *node,
enum spa_direction direction, uint32_t port_id, const struct spa_command *command)
{
struct state *this;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct state, node);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
if (SPA_COMMAND_TYPE(command) == this->type.command_node.Pause) {
res = spa_alsa_pause(this, false);
} else if (SPA_COMMAND_TYPE(command) == this->type.command_node.Start) {
res = spa_alsa_start(this, false);
} else
res = -ENOTSUP;
return res;
return -ENOTSUP;
}
static int impl_node_process_input(struct spa_node *node)

View file

@ -165,56 +165,10 @@ static int impl_node_set_param(struct spa_node *node, uint32_t id, uint32_t flag
return 0;
}
static int do_send_done(struct spa_loop *loop, bool async, uint32_t seq, const void *data, size_t size, void *user_data)
{
struct state *this = user_data;
this->callbacks->done(this->callbacks_data, seq, *(int*)data);
return 0;
}
static int do_start(struct spa_loop *loop, bool async, uint32_t seq, const void *data, size_t size, void *user_data)
{
struct state *this = user_data;
int res;
res = spa_alsa_start(this, false);
if (async) {
spa_loop_invoke(this->main_loop,
do_send_done,
seq,
&res,
sizeof(res),
false,
this);
}
return res;
}
static int do_pause(struct spa_loop *loop, bool async, uint32_t seq, const void *data, size_t size, void *user_data)
{
struct state *this = user_data;
int res;
res = spa_alsa_pause(this, false);
if (async) {
spa_loop_invoke(this->main_loop,
do_send_done,
seq,
&res,
sizeof(res),
false,
this);
}
return res;
}
static int impl_node_send_command(struct spa_node *node, const struct spa_command *command)
{
struct state *this;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(command != NULL, -EINVAL);
@ -227,14 +181,11 @@ static int impl_node_send_command(struct spa_node *node, const struct spa_comman
if (this->n_buffers == 0)
return -EIO;
return spa_loop_invoke(this->data_loop, do_start, ++this->seq, NULL, 0, false, this);
if ((res = spa_alsa_start(this, false)) < 0)
return res;
} else if (SPA_COMMAND_TYPE(command) == this->type.command_node.Pause) {
if (!this->have_format)
return -EIO;
if (this->n_buffers == 0)
return -EIO;
return spa_loop_invoke(this->data_loop, do_pause, ++this->seq, NULL, 0, false, this);
if ((res = spa_alsa_pause(this, false)) < 0)
return res;
} else
return -ENOTSUP;
@ -641,23 +592,8 @@ static int
impl_node_port_send_command(struct spa_node *node,
enum spa_direction direction, uint32_t port_id, const struct spa_command *command)
{
struct state *this;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct state, node);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
if (SPA_COMMAND_TYPE(command) == this->type.command_node.Pause) {
res = spa_alsa_pause(this, false);
} else if (SPA_COMMAND_TYPE(command) == this->type.command_node.Start) {
res = spa_alsa_start(this, false);
} else
res = -ENOTSUP;
return res;
return -ENOTSUP;
}
static int impl_node_process_input(struct spa_node *node)

View file

@ -413,6 +413,7 @@ pull_frames(struct state *state,
if (total_frames == 0 && do_pull) {
total_frames = SPA_MIN(frames, state->threshold);
spa_log_warn(state->log, "write %zd frames of silence", total_frames);
snd_pcm_areas_silence(my_areas, offset, state->channels, total_frames, state->format);
state->underrun += total_frames;
underrun = true;
@ -660,6 +661,7 @@ static void alsa_on_capture_timeout_event(struct spa_source *source)
int spa_alsa_start(struct state *state, bool xrun_recover)
{
int err;
struct itimerspec ts;
if (state->started)
return 0;
@ -697,7 +699,12 @@ int spa_alsa_start(struct state *state, bool xrun_recover)
}
state->alsa_started = true;
}
state->source.func(&state->source);
ts.it_value.tv_sec = 0;
ts.it_value.tv_nsec = 1;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
timerfd_settime(state->timerfd, 0, &ts, NULL);
state->started = true;
@ -712,7 +719,15 @@ static int do_remove_source(struct spa_loop *loop,
void *user_data)
{
struct state *state = user_data;
struct itimerspec ts;
spa_loop_remove_source(state->data_loop, &state->source);
ts.it_value.tv_sec = 0;
ts.it_value.tv_nsec = 0;
ts.it_interval.tv_sec = 0;
ts.it_interval.tv_nsec = 0;
timerfd_settime(state->timerfd, 0, &ts, NULL);
return 0;
}