Improve async handling

Don't use special callback in node to receive the results. Instead,
use a generic result callback to receive the result. This makes things
a bit more symetric and generic again because then you can choose how
to match the result to the request and you have a generic way to handle
both the sync and async case. We can then also remove the wait method.
This also makes the remote interface and spa interface to objects very
similar.

Make a helper object to receive and dispatch results. Use this in the
helper for enum_params.

Make device use the same result callbacks.
This commit is contained in:
Wim Taymans 2019-02-25 12:29:57 +01:00
parent 98463b689b
commit d2c18c7b1a
64 changed files with 1298 additions and 1141 deletions

View file

@ -277,24 +277,29 @@ static int impl_set_callbacks(struct spa_device *device,
}
static int impl_enum_params(struct spa_device *device,
uint32_t id, uint32_t *index,
const struct spa_pod *filter,
struct spa_pod **result,
struct spa_pod_builder *builder)
static int impl_enum_params(struct spa_device *device, int seq,
uint32_t id, uint32_t start, uint32_t num,
const struct spa_pod *filter)
{
struct impl *this;
struct spa_pod *param;
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_result_device_params result;
uint32_t count = 0;
int res;
spa_return_val_if_fail(device != NULL, -EINVAL);
spa_return_val_if_fail(index != NULL, -EINVAL);
spa_return_val_if_fail(builder != NULL, -EINVAL);
spa_return_val_if_fail(num != 0, -EINVAL);
this = SPA_CONTAINER_OF(device, struct impl, device);
spa_return_val_if_fail(this->callbacks && this->callbacks->result, -EIO);
result.id = id;
result.next = start;
next:
result.index = result.next++;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
switch (id) {
@ -303,17 +308,17 @@ static int impl_enum_params(struct spa_device *device,
uint32_t list[] = { SPA_PARAM_EnumProfile,
SPA_PARAM_Profile };
if (*index < SPA_N_ELEMENTS(list))
if (result.index < SPA_N_ELEMENTS(list))
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamList, id,
SPA_PARAM_LIST_id, SPA_POD_Id(list[*index]));
SPA_PARAM_LIST_id, SPA_POD_Id(list[result.index]));
else
return 0;
break;
}
case SPA_PARAM_EnumProfile:
{
switch (*index) {
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamProfile, id,
@ -333,7 +338,7 @@ static int impl_enum_params(struct spa_device *device,
}
case SPA_PARAM_Profile:
{
switch (*index) {
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamProfile, id,
@ -348,12 +353,16 @@ static int impl_enum_params(struct spa_device *device,
return -ENOENT;
}
(*index)++;
if (spa_pod_filter(builder, result, param, filter) < 0)
if (spa_pod_filter(&b, &result.param, param, filter) < 0)
goto next;
return 1;
if ((res = this->callbacks->result(this->callbacks_data, seq, 0, &result)) != 0)
return res;
if (++count != num)
goto next;
return 0;
}
static int impl_set_param(struct spa_device *device,

View file

@ -48,28 +48,29 @@ static void reset_props(struct props *props)
props->max_latency = default_max_latency;
}
static int impl_node_enum_params(struct spa_node *node,
static int impl_node_enum_params(struct spa_node *node, int seq,
uint32_t id, uint32_t start, uint32_t num,
const struct spa_pod *filter,
spa_result_func_t func, void *data)
const struct spa_pod *filter)
{
struct state *this;
struct spa_pod *param;
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_result_node_enum_params result;
struct spa_result_node_params result;
uint32_t count = 0;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(num != 0, -EINVAL);
spa_return_val_if_fail(func != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct state, node);
spa_return_val_if_fail(this->callbacks && this->callbacks->result, -EIO);
result.id = id;
result.next = start;
next:
result.index = result.next++;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
switch (id) {
@ -78,10 +79,10 @@ static int impl_node_enum_params(struct spa_node *node,
uint32_t list[] = { SPA_PARAM_PropInfo,
SPA_PARAM_Props };
if (result.next < SPA_N_ELEMENTS(list))
if (result.index < SPA_N_ELEMENTS(list))
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamList, id,
SPA_PARAM_LIST_id, SPA_POD_Id(list[result.next]));
SPA_PARAM_LIST_id, SPA_POD_Id(list[result.index]));
else
return 0;
break;
@ -90,7 +91,7 @@ static int impl_node_enum_params(struct spa_node *node,
{
struct props *p = &this->props;
switch (result.next) {
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_PropInfo, id,
@ -135,7 +136,7 @@ static int impl_node_enum_params(struct spa_node *node,
{
struct props *p = &this->props;
switch (result.next) {
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_Props, id,
@ -154,12 +155,10 @@ static int impl_node_enum_params(struct spa_node *node,
return -ENOENT;
}
result.next++;
if (spa_pod_filter(&b, &result.param, param, filter) < 0)
goto next;
if ((res = func(data, count, &result)) != 0)
if ((res = this->callbacks->result(this->callbacks_data, seq, 0, &result)) != 0)
return res;
if (++count != num)
@ -274,12 +273,6 @@ static void emit_port_info(struct state *this)
}
}
static int
impl_node_sync(struct spa_node *node)
{
return 0;
}
static int
impl_node_set_callbacks(struct spa_node *node,
const struct spa_node_callbacks *callbacks,
@ -312,32 +305,33 @@ static int impl_node_remove_port(struct spa_node *node, enum spa_direction direc
}
static int
impl_node_port_enum_params(struct spa_node *node,
impl_node_port_enum_params(struct spa_node *node, int seq,
enum spa_direction direction, uint32_t port_id,
uint32_t id, uint32_t start, uint32_t num,
const struct spa_pod *filter,
spa_result_func_t func, void *data)
const struct spa_pod *filter)
{
struct state *this;
struct spa_pod *param;
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_result_node_enum_params result;
struct spa_result_node_params result;
uint32_t count = 0;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(num != 0, -EINVAL);
spa_return_val_if_fail(func != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct state, node);
spa_return_val_if_fail(this->callbacks && this->callbacks->result, -EIO);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
result.id = id;
result.next = start;
next:
result.index = result.next++;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
switch (id) {
@ -349,21 +343,21 @@ impl_node_port_enum_params(struct spa_node *node,
SPA_PARAM_Meta,
SPA_PARAM_IO, };
if (result.next < SPA_N_ELEMENTS(list))
if (result.index < SPA_N_ELEMENTS(list))
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamList, id,
SPA_PARAM_LIST_id, SPA_POD_Id(list[result.next]));
SPA_PARAM_LIST_id, SPA_POD_Id(list[result.index]));
else
return 0;
break;
}
case SPA_PARAM_EnumFormat:
return spa_alsa_enum_format(this, start, num, filter, func, data);
return spa_alsa_enum_format(this, seq, start, num, filter);
case SPA_PARAM_Format:
if (!this->have_format)
return -EIO;
if (result.next > 0)
if (result.index > 0)
return 0;
param = spa_format_audio_raw_build(&b, id, &this->current_format.info.raw);
@ -372,7 +366,7 @@ impl_node_port_enum_params(struct spa_node *node,
case SPA_PARAM_Buffers:
if (!this->have_format)
return -EIO;
if (result.next > 0)
if (result.index > 0)
return 0;
param = spa_pod_builder_add_object(&b,
@ -391,7 +385,7 @@ impl_node_port_enum_params(struct spa_node *node,
if (!this->have_format)
return -EIO;
switch (result.next) {
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamMeta, id,
@ -404,7 +398,7 @@ impl_node_port_enum_params(struct spa_node *node,
break;
case SPA_PARAM_IO:
switch (result.next) {
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamIO, id,
@ -432,12 +426,10 @@ impl_node_port_enum_params(struct spa_node *node,
return -ENOENT;
}
result.next++;
if (spa_pod_filter(&b, &result.param, param, filter) < 0)
goto next;
if ((res = func(data, count, &result)) != 0)
if ((res = this->callbacks->result(this->callbacks_data, seq, 0, &result)) != 0)
return res;
if (++count != num)
@ -665,7 +657,6 @@ static int impl_node_process(struct spa_node *node)
static const struct spa_node impl_node = {
SPA_VERSION_NODE,
.set_callbacks = impl_node_set_callbacks,
.sync = impl_node_sync,
.enum_params = impl_node_enum_params,
.set_param = impl_node_set_param,
.set_io = impl_node_set_io,

View file

@ -48,30 +48,31 @@ static void reset_props(struct props *props)
props->max_latency = default_max_latency;
}
static int impl_node_enum_params(struct spa_node *node,
static int impl_node_enum_params(struct spa_node *node, int seq,
uint32_t id, uint32_t start, uint32_t num,
const struct spa_pod *filter,
spa_result_func_t func, void *data)
const struct spa_pod *filter)
{
struct state *this;
struct spa_pod *param;
uint8_t buffer[1024];
struct spa_pod_builder b = { 0 };
struct props *p;
struct spa_result_node_enum_params result;
struct spa_result_node_params result;
uint32_t count = 0;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(num != 0, -EINVAL);
spa_return_val_if_fail(func != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct state, node);
spa_return_val_if_fail(this->callbacks && this->callbacks->result, -EIO);
p = &this->props;
result.id = id;
result.next = start;
next:
result.index = result.next++;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
switch (id) {
@ -80,16 +81,16 @@ static int impl_node_enum_params(struct spa_node *node,
uint32_t list[] = { SPA_PARAM_PropInfo,
SPA_PARAM_Props, };
if (result.next < SPA_N_ELEMENTS(list))
if (result.index < SPA_N_ELEMENTS(list))
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamList, id,
SPA_PARAM_LIST_id, SPA_POD_Id(list[result.next]));
SPA_PARAM_LIST_id, SPA_POD_Id(list[result.index]));
else
return 0;
break;
}
case SPA_PARAM_PropInfo:
switch (result.next) {
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_PropInfo, id,
@ -131,7 +132,7 @@ static int impl_node_enum_params(struct spa_node *node,
break;
case SPA_PARAM_Props:
switch (result.next) {
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_Props, id,
@ -150,12 +151,10 @@ static int impl_node_enum_params(struct spa_node *node,
return -ENOENT;
}
result.next++;
if (spa_pod_filter(&b, &result.param, param, filter) < 0)
goto next;
if ((res = func(data, count, &result)) != 0)
if ((res = this->callbacks->result(this->callbacks_data, seq, 0, &result)) != 0)
return res;
if (++count != num)
@ -317,31 +316,33 @@ static void recycle_buffer(struct state *this, uint32_t buffer_id)
}
static int
impl_node_port_enum_params(struct spa_node *node,
impl_node_port_enum_params(struct spa_node *node, int seq,
enum spa_direction direction, uint32_t port_id,
uint32_t id, uint32_t start, uint32_t num,
const struct spa_pod *filter,
spa_result_func_t func, void *data)
const struct spa_pod *filter)
{
struct state *this;
struct spa_pod *param;
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
struct spa_result_node_enum_params result;
struct spa_result_node_params result;
uint32_t count = 0;
int res;
spa_return_val_if_fail(node != NULL, -EINVAL);
spa_return_val_if_fail(num != 0, -EINVAL);
spa_return_val_if_fail(func != NULL, -EINVAL);
this = SPA_CONTAINER_OF(node, struct state, node);
spa_return_val_if_fail(this->callbacks && this->callbacks->result, -EIO);
spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL);
result.next = start;
result.id = id;
result.next = start;
next:
result.index = result.next++;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
switch (id) {
@ -352,21 +353,21 @@ impl_node_port_enum_params(struct spa_node *node,
SPA_PARAM_Buffers,
SPA_PARAM_Meta };
if (result.next < SPA_N_ELEMENTS(list))
if (result.index < SPA_N_ELEMENTS(list))
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamList, id,
SPA_PARAM_LIST_id, SPA_POD_Id(list[result.next]));
SPA_PARAM_LIST_id, SPA_POD_Id(list[result.index]));
else
return 0;
break;
}
case SPA_PARAM_EnumFormat:
return spa_alsa_enum_format(this, start, num, filter, func, data);
return spa_alsa_enum_format(this, seq, start, num, filter);
case SPA_PARAM_Format:
if (!this->have_format)
return -EIO;
if (result.next > 0)
if (result.index > 0)
return 0;
param = spa_format_audio_raw_build(&b, id, &this->current_format.info.raw);
@ -375,7 +376,7 @@ impl_node_port_enum_params(struct spa_node *node,
case SPA_PARAM_Buffers:
if (!this->have_format)
return -EIO;
if (result.next > 0)
if (result.index > 0)
return 0;
param = spa_pod_builder_add_object(&b,
@ -394,7 +395,7 @@ impl_node_port_enum_params(struct spa_node *node,
if (!this->have_format)
return -EIO;
switch (result.next) {
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamMeta, id,
@ -407,7 +408,7 @@ impl_node_port_enum_params(struct spa_node *node,
break;
case SPA_PARAM_IO:
switch (result.next) {
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamIO, id,
@ -429,12 +430,10 @@ impl_node_port_enum_params(struct spa_node *node,
return -ENOENT;
}
result.next++;
if (spa_pod_filter(&b, &result.param, param, filter) < 0)
goto next;
if ((res = func(data, count, &result)) != 0)
if ((res = this->callbacks->result(this->callbacks_data, seq, 0, &result)) != 0)
return res;
if (++count != num)

View file

@ -217,9 +217,8 @@ static void sanitize_map(snd_pcm_chmap_t* map)
}
int
spa_alsa_enum_format(struct state *state, uint32_t start, uint32_t num,
const struct spa_pod *filter,
spa_result_func_t func, void *data)
spa_alsa_enum_format(struct state *state, int seq, uint32_t start, uint32_t num,
const struct spa_pod *filter)
{
snd_pcm_t *hndl;
snd_pcm_hw_params_t *params;
@ -236,7 +235,7 @@ spa_alsa_enum_format(struct state *state, uint32_t start, uint32_t num,
int res;
bool opened;
struct spa_pod_frame f[2];
struct spa_result_node_enum_params result;
struct spa_result_node_params result;
uint32_t count = 0;
opened = state->opened;
@ -246,6 +245,8 @@ spa_alsa_enum_format(struct state *state, uint32_t start, uint32_t num,
result.next = start;
next:
result.index = result.next++;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
hndl = state->hndl;
@ -316,11 +317,11 @@ spa_alsa_enum_format(struct state *state, uint32_t start, uint32_t num,
uint32_t channel;
snd_pcm_chmap_t* map;
if (maps[result.next] == NULL) {
if (maps[result.index] == NULL) {
snd_pcm_free_chmaps(maps);
goto enum_end;
}
map = &maps[result.next]->map;
map = &maps[result.index]->map;
spa_log_debug(state->log, "map %d channels", map->channels);
sanitize_map(map);
@ -338,7 +339,7 @@ spa_alsa_enum_format(struct state *state, uint32_t start, uint32_t num,
snd_pcm_free_chmaps(maps);
}
else {
if (result.next > 0)
if (result.index > 0)
goto enum_end;
spa_pod_builder_push_choice(&b, &f[1], SPA_CHOICE_None, 0);
@ -354,12 +355,10 @@ spa_alsa_enum_format(struct state *state, uint32_t start, uint32_t num,
fmt = spa_pod_builder_pop(&b, &f[0]);
result.next++;
if ((res = spa_pod_filter(&b, &result.param, fmt, filter)) < 0)
goto next;
if ((res = func(data, count, &result)) != 0)
if ((res = state->callbacks->result(state->callbacks_data, seq, 0, &result)) != 0)
goto exit;
if (++count != num)

View file

@ -143,10 +143,9 @@ struct state {
};
int
spa_alsa_enum_format(struct state *state,
spa_alsa_enum_format(struct state *state, int seq,
uint32_t start, uint32_t num,
const struct spa_pod *filter,
spa_result_func_t func, void *data);
const struct spa_pod *filter);
int spa_alsa_set_format(struct state *state, struct spa_audio_info *info, uint32_t flags);