diff --git a/spa/plugins/audioconvert/audioconvert.c b/spa/plugins/audioconvert/audioconvert.c index 6ddc44165..ac6107005 100644 --- a/spa/plugins/audioconvert/audioconvert.c +++ b/spa/plugins/audioconvert/audioconvert.c @@ -38,6 +38,7 @@ #define NAME "audioconvert" #define MAX_BUFFERS 32 +#define MAX_PORTS 128 #define PROP_DEFAULT_TRUNCATE false #define PROP_DEFAULT_DITHER 0 @@ -99,17 +100,6 @@ struct buffer { struct spa_meta_header *h; }; -struct port { - uint32_t id; - - struct spa_io_buffers *io; - - bool have_format; - struct spa_audio_info format; - - struct spa_node *node; -}; - struct link { struct spa_node *out_node; uint32_t out_port; @@ -137,30 +127,22 @@ struct impl { const struct spa_node_callbacks *callbacks; void *user_data; - struct port in_port; - struct port out_port; - int n_links; struct link links[8]; + int n_nodes; + struct spa_node *nodes[8]; bool started; - struct spa_handle *hnd_fmt_in; + struct spa_handle *hnd_fmt[2]; struct spa_handle *hnd_channelmix; struct spa_handle *hnd_resample; - struct spa_handle *hnd_fmt_out; - struct spa_node *fmt_in; + struct spa_node *fmt[2]; struct spa_node *channelmix; struct spa_node *resample; - struct spa_node *fmt_out; }; -#define CHECK_PORT(this,d,id) (id == 0) -#define GET_IN_PORT(this,id) (&this->in_port) -#define GET_OUT_PORT(this,id) (&this->out_port) -#define GET_PORT(this,d,id) (d == SPA_DIRECTION_INPUT ? GET_IN_PORT(this,id) : GET_OUT_PORT(this,id)) - static int make_link(struct impl *this, struct spa_node *out_node, uint32_t out_port, struct spa_node *in_node, uint32_t in_port, @@ -219,6 +201,42 @@ static void clean_link(struct impl *this, struct link *link) link->buffers = NULL; } +static int debug_params(struct impl *this, struct spa_node *node, + enum spa_direction direction, uint32_t port_id, uint32_t id, struct spa_pod *filter) +{ + struct type *t = &this->type; + struct spa_pod_builder b = { 0 }; + uint8_t buffer[4096]; + uint32_t state, flag; + struct spa_pod *format; + int res; + + flag = 0; + if (id == t->param.idEnumFormat) + flag |= SPA_DEBUG_FLAG_FORMAT; + + spa_log_error(this->log, "formats:"); + + state = 0; + while (true) { + spa_pod_builder_init(&b, buffer, sizeof(buffer)); + res = spa_node_port_enum_params(node, + direction, port_id, + id, &state, + NULL, &format, &b); + if (res <= 0) + break; + + spa_debug_pod(format, flag); + } + + spa_log_error(this->log, "failed filter:"); + if (filter) + spa_debug_pod(filter, flag); + + return 0; +} + static int negotiate_link_format(struct impl *this, struct link *link) { struct type *t = &this->type; @@ -250,8 +268,11 @@ static int negotiate_link_format(struct impl *this, struct link *link) if ((res = spa_node_port_enum_params(link->out_node, SPA_DIRECTION_OUTPUT, link->out_port, t->param.idEnumFormat, &state, - filter, &format, &b)) <= 0) + filter, &format, &b)) <= 0) { + debug_params(this, link->out_node, SPA_DIRECTION_OUTPUT, link->out_port, + t->param.idEnumFormat, filter); return -ENOTSUP; + } filter = format; } @@ -260,13 +281,16 @@ static int negotiate_link_format(struct impl *this, struct link *link) if ((res = spa_node_port_enum_params(link->in_node, SPA_DIRECTION_INPUT, link->in_port, t->param.idEnumFormat, &state, - filter, &format, &b)) <= 0) + filter, &format, &b)) <= 0) { + debug_params(this, link->in_node, SPA_DIRECTION_INPUT, link->in_port, + t->param.idEnumFormat, filter); return -ENOTSUP; - + } filter = format; } spa_pod_fixate(filter); + spa_debug_pod(filter, SPA_DEBUG_FLAG_FORMAT); if (link->out_node != NULL) { if ((res = spa_node_port_set_param(link->out_node, @@ -289,53 +313,34 @@ static int negotiate_link_format(struct impl *this, struct link *link) static int setup_convert(struct impl *this) { - struct port *inport, *outport; - struct spa_node *prev = NULL; int i, j, res; - struct type *t = &this->type; - - inport = GET_PORT(this, SPA_DIRECTION_INPUT, 0); - outport = GET_PORT(this, SPA_DIRECTION_OUTPUT, 0); + struct spa_audio_info informat, outformat; spa_log_info(this->log, NAME " %p: %d/%d@%d.%d->%d/%d@%d.%d", this, - inport->format.info.raw.format, - inport->format.info.raw.channels, - inport->format.info.raw.rate, - inport->format.info.raw.layout, - outport->format.info.raw.format, - outport->format.info.raw.channels, - outport->format.info.raw.rate, - outport->format.info.raw.layout); + informat.info.raw.format, + informat.info.raw.channels, + informat.info.raw.rate, + informat.info.raw.layout, + outformat.info.raw.format, + outformat.info.raw.channels, + outformat.info.raw.rate, + outformat.info.raw.layout); if (this->n_links > 0) return 0; + this->n_nodes = 0; /* unpack */ - make_link(this, NULL, 0, this->fmt_in, 0, &inport->format); - prev = this->fmt_in; - + this->nodes[this->n_nodes++] = this->fmt[SPA_DIRECTION_INPUT]; /* down mix */ - if (inport->format.info.raw.channels > outport->format.info.raw.channels) { - make_link(this, prev, 0, this->channelmix, 0, NULL); - prev = this->channelmix; - } - + this->nodes[this->n_nodes++] = this->channelmix; /* resample */ - if (inport->format.info.raw.rate != outport->format.info.raw.rate) { - make_link(this, prev, 0, this->resample, 0, NULL); - prev = this->resample; - } - - /* up mix */ - if (inport->format.info.raw.channels < outport->format.info.raw.channels) { - make_link(this, prev, 0, this->channelmix, 0, NULL); - prev = this->channelmix; - } - - make_link(this, prev, 0, this->fmt_out, 0, NULL); - + this->nodes[this->n_nodes++] = this->resample; /* pack */ - make_link(this, this->fmt_out, 0, NULL, 0, &outport->format); + this->nodes[this->n_nodes++] = this->fmt[SPA_DIRECTION_OUTPUT]; + + for (i = 0; i < this->n_nodes - 1; i++) + make_link(this, this->nodes[i], 0, this->nodes[i+1], 0, NULL); for (i = 0, j = this->n_links - 1; j >= i; i++, j--) { if ((res = negotiate_link_format(this, &this->links[i])) < 0) @@ -343,13 +348,6 @@ static int setup_convert(struct impl *this) if ((res = negotiate_link_format(this, &this->links[j])) < 0) return res; } - - - spa_node_port_set_io(inport->node, SPA_DIRECTION_INPUT, 0, - t->io.Buffers, inport->io, sizeof(struct spa_io_buffers)); - spa_node_port_set_io(outport->node, SPA_DIRECTION_OUTPUT, 0, - t->io.Buffers, outport->io, sizeof(struct spa_io_buffers)); - return 0; } @@ -374,16 +372,22 @@ static int negotiate_link_buffers(struct impl *this, struct link *link) if ((res = spa_node_port_enum_params(link->out_node, SPA_DIRECTION_OUTPUT, link->out_port, t->param.idBuffers, &state, - param, ¶m, &b)) <= 0) + param, ¶m, &b)) <= 0) { + debug_params(this, link->out_node, SPA_DIRECTION_OUTPUT, link->out_port, + t->param.idBuffers, param); return -ENOTSUP; + } } if (link->in_node != NULL) { state = 0; if ((res = spa_node_port_enum_params(link->in_node, SPA_DIRECTION_INPUT, link->in_port, t->param.idBuffers, &state, - param, ¶m, &b)) <= 0) + param, ¶m, &b)) <= 0) { + debug_params(this, link->in_node, SPA_DIRECTION_INPUT, link->in_port, + t->param.idBuffers, param); return -ENOTSUP; + } } spa_pod_fixate(param); @@ -415,6 +419,9 @@ static int negotiate_link_buffers(struct impl *this, struct link *link) NULL) < 0) return -EINVAL; + spa_log_debug(this->log, "%p: buffers %d, blocks %d, size %d, align %d", + this, buffers, blocks, size, align); + datas = alloca(sizeof(struct spa_data) * blocks); memset(datas, 0, sizeof(struct spa_data) * blocks); aligns = alloca(sizeof(uint32_t) * blocks); @@ -480,13 +487,13 @@ static int setup_buffers(struct impl *this, enum spa_direction direction) spa_log_debug(this->log, NAME " %p: %d", this, direction); if (direction == SPA_DIRECTION_INPUT) { - for (i = 1; i < this->n_links-1; i++) { + for (i = 0; i < this->n_links; i++) { if ((res = negotiate_link_buffers(this, &this->links[i])) < 0) spa_log_error(this->log, NAME " %p: buffers %d failed %s", this, i, spa_strerror(res)); } } else { - for (i = this->n_links-2; i > 0 ; i--) { + for (i = this->n_links-1; i >= 0 ; i--) { if ((res = negotiate_link_buffers(this, &this->links[i])) < 0) spa_log_error(this->log, NAME " %p: buffers %d failed %s", this, i, spa_strerror(res)); @@ -514,6 +521,7 @@ static int impl_node_set_param(struct spa_node *node, uint32_t id, uint32_t flag static int impl_node_send_command(struct spa_node *node, const struct spa_command *command) { struct impl *this; + int res; spa_return_val_if_fail(node != NULL, -EINVAL); spa_return_val_if_fail(command != NULL, -EINVAL); @@ -521,6 +529,9 @@ static int impl_node_send_command(struct spa_node *node, const struct spa_comman this = SPA_CONTAINER_OF(node, struct impl, node); if (SPA_COMMAND_TYPE(command) == this->type.command_node.Start) { + if ((res = setup_convert(this)) < 0) + goto error; + setup_buffers(this, SPA_DIRECTION_INPUT); this->started = true; } else if (SPA_COMMAND_TYPE(command) == this->type.command_node.Pause) { this->started = false; @@ -528,6 +539,10 @@ static int impl_node_send_command(struct spa_node *node, const struct spa_comman return -ENOTSUP; return 0; + + error: + spa_log_error(this->log, "error %s", spa_strerror(res)); + return res; } static int @@ -554,16 +569,14 @@ impl_node_get_n_ports(struct spa_node *node, uint32_t *n_output_ports, uint32_t *max_output_ports) { + struct impl *this; + spa_return_val_if_fail(node != NULL, -EINVAL); - if (n_input_ports) - *n_input_ports = 1; - if (max_input_ports) - *max_input_ports = 1; - if (n_output_ports) - *n_output_ports = 1; - if (max_output_ports) - *max_output_ports = 1; + this = SPA_CONTAINER_OF(node, struct impl, node); + + spa_node_get_n_ports(this->fmt[SPA_DIRECTION_INPUT], n_input_ports, max_input_ports, NULL, NULL); + spa_node_get_n_ports(this->fmt[SPA_DIRECTION_OUTPUT], NULL, NULL, n_output_ports, max_output_ports); return 0; } @@ -575,25 +588,39 @@ impl_node_get_port_ids(struct spa_node *node, uint32_t *output_ids, uint32_t n_output_ids) { + struct impl *this; + spa_return_val_if_fail(node != NULL, -EINVAL); - if (n_input_ids && input_ids) - input_ids[0] = 0; - if (n_output_ids > 0 && output_ids) - output_ids[0] = 0; + this = SPA_CONTAINER_OF(node, struct impl, node); + + spa_node_get_port_ids(this->fmt[SPA_DIRECTION_INPUT], input_ids, n_input_ids, NULL, 0); + spa_node_get_port_ids(this->fmt[SPA_DIRECTION_OUTPUT], NULL, 0, output_ids, n_output_ids); return 0; } static int impl_node_add_port(struct spa_node *node, enum spa_direction direction, uint32_t port_id) { - return -ENOTSUP; + struct impl *this; + + spa_return_val_if_fail(node != NULL, -EINVAL); + + this = SPA_CONTAINER_OF(node, struct impl, node); + + return spa_node_add_port(this->fmt[direction], direction, port_id); } static int impl_node_remove_port(struct spa_node *node, enum spa_direction direction, uint32_t port_id) { - return -ENOTSUP; + struct impl *this; + + spa_return_val_if_fail(node != NULL, -EINVAL); + + this = SPA_CONTAINER_OF(node, struct impl, node); + + return spa_node_remove_port(this->fmt[direction], direction, port_id); } static int @@ -603,18 +630,13 @@ impl_node_port_get_info(struct spa_node *node, const struct spa_port_info **info) { struct impl *this; - struct port *port; spa_return_val_if_fail(node != NULL, -EINVAL); spa_return_val_if_fail(info != NULL, -EINVAL); this = SPA_CONTAINER_OF(node, struct impl, node); - spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL); - - port = GET_PORT(this, direction, port_id); - - return spa_node_port_get_info(port->node, direction, port_id, info); + return spa_node_port_get_info(this->fmt[direction], direction, port_id, info); } static int @@ -626,61 +648,13 @@ impl_node_port_enum_params(struct spa_node *node, struct spa_pod_builder *builder) { struct impl *this; - struct port *port; spa_return_val_if_fail(node != NULL, -EINVAL); this = SPA_CONTAINER_OF(node, struct impl, node); - spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL); - - port = GET_PORT(this, direction, port_id); - - return spa_node_port_enum_params(port->node, direction, port_id, id, index, - filter, result, builder); -} - -static int port_set_format(struct spa_node *node, - enum spa_direction direction, - uint32_t port_id, - uint32_t flags, - const struct spa_pod *format) -{ - struct impl *this = SPA_CONTAINER_OF(node, struct impl, node); - struct port *port, *other; - struct type *t = &this->type; - int res = 0; - - port = GET_PORT(this, direction, port_id); - other = GET_PORT(this, SPA_DIRECTION_REVERSE(direction), port_id); - - if (format == NULL) { - clean_convert(this); - port->have_format = false; - } else { - struct spa_audio_info info = { 0 }; - - spa_pod_object_parse(format, - "I", &info.media_type, - "I", &info.media_subtype); - - if (info.media_type != t->media_type.audio || - info.media_subtype != t->media_subtype.raw) - return -EINVAL; - - if (spa_format_audio_raw_parse(format, &info.info.raw, &t->format_audio) < 0) - return -EINVAL; - - clean_convert(this); - port->have_format = true; - port->format = info; - - if (other->have_format) - res = setup_convert(this); - - spa_log_debug(this->log, NAME " %p: set format on port %d %d", this, port_id, res); - } - return res; + return spa_node_port_enum_params(this->fmt[direction], direction, port_id, + id, index, filter, result, builder); } static int @@ -690,22 +664,12 @@ impl_node_port_set_param(struct spa_node *node, const struct spa_pod *param) { struct impl *this; - struct port *port; - struct type *t; spa_return_val_if_fail(node != NULL, -EINVAL); this = SPA_CONTAINER_OF(node, struct impl, node); - t = &this->type; - spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL); - - port = GET_PORT(this, direction, port_id); - - if (id == t->param.idFormat) - return port_set_format(node, direction, port_id, flags, param); - else - return spa_node_port_set_param(port->node, direction, port_id, id, flags, param); + return spa_node_port_set_param(this->fmt[direction], direction, port_id, id, flags, param); } static int @@ -716,22 +680,12 @@ impl_node_port_use_buffers(struct spa_node *node, uint32_t n_buffers) { struct impl *this; - struct port *port; - int res; spa_return_val_if_fail(node != NULL, -EINVAL); this = SPA_CONTAINER_OF(node, struct impl, node); - spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL); - - port = GET_PORT(this, direction, port_id); - - res = spa_node_port_use_buffers(port->node, direction, port_id, buffers, n_buffers); - if (res < 0) - return res; - - return setup_buffers(this, direction); + return spa_node_port_use_buffers(this->fmt[direction], direction, port_id, buffers, n_buffers); } static int @@ -744,17 +698,12 @@ impl_node_port_alloc_buffers(struct spa_node *node, uint32_t *n_buffers) { struct impl *this; - struct port *port; spa_return_val_if_fail(node != NULL, -EINVAL); this = SPA_CONTAINER_OF(node, struct impl, node); - spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL); - - port = GET_PORT(this, direction, port_id); - - return spa_node_port_alloc_buffers(port->node, direction, port_id, + return spa_node_port_alloc_buffers(this->fmt[direction], direction, port_id, params, n_params, buffers, n_buffers); } @@ -764,38 +713,23 @@ impl_node_port_set_io(struct spa_node *node, uint32_t id, void *data, size_t size) { struct impl *this; - struct port *port; - struct type *t; spa_return_val_if_fail(node != NULL, -EINVAL); this = SPA_CONTAINER_OF(node, struct impl, node); - t = &this->type; - spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL); - - port = GET_PORT(this, direction, port_id); - - if (id == t->io.Buffers) - port->io = data; - - return spa_node_port_set_io(port->node, direction, port_id, id, data, size); + return spa_node_port_set_io(this->fmt[direction], direction, port_id, id, data, size); } static int impl_node_port_reuse_buffer(struct spa_node *node, uint32_t port_id, uint32_t buffer_id) { struct impl *this; - struct port *port; spa_return_val_if_fail(node != NULL, -EINVAL); this = SPA_CONTAINER_OF(node, struct impl, node); - spa_return_val_if_fail(CHECK_PORT(this, SPA_DIRECTION_OUTPUT, port_id), -EINVAL); - - port = GET_PORT(this, SPA_DIRECTION_OUTPUT, port_id); - - return spa_node_port_reuse_buffer(port->node, port_id, buffer_id); + return spa_node_port_reuse_buffer(this->fmt[SPA_DIRECTION_OUTPUT], port_id, buffer_id); } static int @@ -805,23 +739,18 @@ impl_node_port_send_command(struct spa_node *node, const struct spa_command *command) { struct impl *this; - struct port *port; spa_return_val_if_fail(node != NULL, -EINVAL); this = SPA_CONTAINER_OF(node, struct impl, node); - spa_return_val_if_fail(CHECK_PORT(this, SPA_DIRECTION_OUTPUT, port_id), -EINVAL); - - port = GET_PORT(this, direction, port_id); - - return spa_node_port_send_command(port->node, direction, port_id, command); + return spa_node_port_send_command(this->fmt[direction], direction, port_id, command); } static int impl_node_process(struct spa_node *node) { struct impl *this; - int i, res = SPA_STATUS_OK; + int r, i, res = SPA_STATUS_OK; spa_return_val_if_fail(node != NULL, -EINVAL); @@ -829,17 +758,20 @@ static int impl_node_process(struct spa_node *node) spa_log_trace(this->log, NAME " %p: process %d", this, this->n_links); - for (i = 1; i < this->n_links; i++) { - int r = spa_node_process(this->links[i].out_node); - if (i == 1) + for (i = 0; i < this->n_nodes; i++) { + r = spa_node_process(this->nodes[i]); + spa_log_trace(this->log, NAME " %p: process %d %d", this, i, r); + + if (i == 0) res |= r & SPA_STATUS_NEED_BUFFER; - if (i == this->n_links - 1) + if (i == this->n_nodes-1) res |= r & SPA_STATUS_HAVE_BUFFER; if (!SPA_FLAG_CHECK(r, SPA_STATUS_HAVE_BUFFER)) { - if (SPA_FLAG_CHECK(r, SPA_STATUS_NEED_BUFFER) && i == 1) + if (SPA_FLAG_CHECK(r, SPA_STATUS_NEED_BUFFER) && i == 0) break; - i = res = SPA_STATUS_OK; + res = SPA_STATUS_OK; + i = -1; continue; } } @@ -926,7 +858,6 @@ impl_init(const struct spa_handle_factory *factory, uint32_t n_support) { struct impl *this; - struct port *port; uint32_t i; size_t size; void *iface; @@ -953,47 +884,39 @@ impl_init(const struct spa_handle_factory *factory, this->node = impl_node; - this->hnd_fmt_in = SPA_MEMBER(this, sizeof(struct impl), struct spa_handle); + this->hnd_fmt[SPA_DIRECTION_INPUT] = SPA_MEMBER(this, sizeof(struct impl), struct spa_handle); spa_handle_factory_init(&spa_fmtconvert_factory, - this->hnd_fmt_in, + this->hnd_fmt[SPA_DIRECTION_INPUT], info, support, n_support); size = spa_handle_factory_get_size(&spa_fmtconvert_factory, info); - this->hnd_channelmix = SPA_MEMBER(this->hnd_fmt_in, size, struct spa_handle); + this->hnd_channelmix = SPA_MEMBER(this->hnd_fmt[SPA_DIRECTION_INPUT], size, struct spa_handle); spa_handle_factory_init(&spa_channelmix_factory, this->hnd_channelmix, info, support, n_support); size = spa_handle_factory_get_size(&spa_channelmix_factory, info); - this->hnd_fmt_out = SPA_MEMBER(this->hnd_channelmix, size, struct spa_handle); + this->hnd_fmt[SPA_DIRECTION_OUTPUT] = SPA_MEMBER(this->hnd_channelmix, size, struct spa_handle); spa_handle_factory_init(&spa_fmtconvert_factory, - this->hnd_fmt_out, + this->hnd_fmt[SPA_DIRECTION_OUTPUT], info, support, n_support); size = spa_handle_factory_get_size(&spa_fmtconvert_factory, info); - this->hnd_resample = SPA_MEMBER(this->hnd_fmt_out, size, struct spa_handle); + this->hnd_resample = SPA_MEMBER(this->hnd_fmt[SPA_DIRECTION_OUTPUT], size, struct spa_handle); spa_handle_factory_init(&spa_resample_factory, this->hnd_resample, info, support, n_support); size = spa_handle_factory_get_size(&spa_resample_factory, info); - spa_handle_get_interface(this->hnd_fmt_in, this->type.node, &iface); - this->fmt_in = iface; - spa_handle_get_interface(this->hnd_fmt_out, this->type.node, &iface); - this->fmt_out = iface; + spa_handle_get_interface(this->hnd_fmt[SPA_DIRECTION_INPUT], this->type.node, &iface); + this->fmt[SPA_DIRECTION_INPUT] = iface; + spa_handle_get_interface(this->hnd_fmt[SPA_DIRECTION_OUTPUT], this->type.node, &iface); + this->fmt[SPA_DIRECTION_OUTPUT] = iface; spa_handle_get_interface(this->hnd_channelmix, this->type.node, &iface); this->channelmix = iface; spa_handle_get_interface(this->hnd_resample, this->type.node, &iface); this->resample = iface; - port = GET_OUT_PORT(this, 0); - port->id = 0; - port->node = this->fmt_out; - - port = GET_IN_PORT(this, 0); - port->id = 0; - port->node = this->fmt_in; - props_reset(&this->props); return 0; diff --git a/spa/plugins/audioconvert/channelmix.c b/spa/plugins/audioconvert/channelmix.c index 0c34481ed..f0d087e25 100644 --- a/spa/plugins/audioconvert/channelmix.c +++ b/spa/plugins/audioconvert/channelmix.c @@ -474,7 +474,7 @@ impl_node_port_enum_params(struct spa_node *node, ":", t->param_buffers.buffers, "iru", 1, SPA_POD_PROP_MIN_MAX(1, MAX_BUFFERS), ":", t->param_buffers.blocks, "i", port->blocks, - ":", t->param_buffers.size, "iru", 1024 * port->stride, + ":", t->param_buffers.size, "iru", 2048 * port->stride, SPA_POD_PROP_MIN_MAX(16 * port->stride, INT32_MAX / port->stride), ":", t->param_buffers.stride, "i", port->stride, ":", t->param_buffers.align, "i", 16); diff --git a/spa/plugins/audioconvert/fmtconvert.c b/spa/plugins/audioconvert/fmtconvert.c index ba2a01496..73fa93b7a 100644 --- a/spa/plugins/audioconvert/fmtconvert.c +++ b/spa/plugins/audioconvert/fmtconvert.c @@ -39,6 +39,7 @@ #define DEFAULT_CHANNELS 2 #define MAX_BUFFERS 32 +#define MAX_PORTS 128 #define PROP_DEFAULT_TRUNCATE false #define PROP_DEFAULT_DITHER 0 @@ -64,10 +65,19 @@ struct buffer { struct spa_meta_header *h; }; +struct format { + struct spa_audio_info format; + uint32_t stride; + uint32_t blocks; + uint32_t size; +}; + struct port { uint32_t id; + bool valid; struct spa_io_buffers *io; + struct spa_io_control_range *ctrl; struct spa_port_info info; bool have_format; @@ -139,8 +149,11 @@ struct impl { const struct spa_node_callbacks *callbacks; void *user_data; - struct port in_port; - struct port out_port; + struct port ports[2][MAX_PORTS]; + uint32_t n_ports[2]; + + struct format formats[2]; + uint32_t n_formats[2]; bool started; @@ -149,50 +162,73 @@ struct impl { convert_func_t convert; }; -#define CHECK_PORT(this,d,id) (id == 0) -#define GET_IN_PORT(this,id) (&this->in_port) -#define GET_OUT_PORT(this,id) (&this->out_port) -#define GET_PORT(this,d,id) (d == SPA_DIRECTION_INPUT ? GET_IN_PORT(this,id) : GET_OUT_PORT(this,id)) +#define CHECK_FREE_PORT(this,d,id) (id < MAX_PORTS && !GET_PORT(this,d,id)->valid) +#define CHECK_PORT(this,d,id) (id < MAX_PORTS && GET_PORT(this,d,id)->valid) +#define GET_PORT(this,d,id) (&this->ports[d][id]) +#define GET_IN_PORT(this,id) GET_PORT(this,SPA_DIRECTION_INPUT,id) +#define GET_OUT_PORT(this,id) GET_PORT(this,SPA_DIRECTION_OUTPUT,id) + +static int collect_format(struct impl *this, enum spa_direction direction, struct format *fmt) +{ + int i, idx, ch = 0; + + *fmt = this->formats[direction]; + for (i = 0, idx = 0; idx < this->n_ports[direction] && i < MAX_PORTS; i++) { + struct port *p = GET_PORT(this, direction, i); + if (!p->valid) + continue; + idx++; + if (!p->have_format) + return -1; + fmt->format = p->format; + ch += p->format.info.raw.channels; + } + fmt->format.info.raw.channels = ch; + + return 0; +} static int setup_convert(struct impl *this) { - struct port *inport, *outport; uint32_t src_fmt, dst_fmt; struct type *t = &this->type; + struct format informat, outformat; - inport = GET_PORT(this, SPA_DIRECTION_INPUT, 0); - outport = GET_PORT(this, SPA_DIRECTION_OUTPUT, 0); + if (collect_format(this, SPA_DIRECTION_INPUT, &informat) < 0) + return -1; + if (collect_format(this, SPA_DIRECTION_OUTPUT, &outformat) < 0) + return -1; - src_fmt = inport->format.info.raw.format; - dst_fmt = outport->format.info.raw.format; + src_fmt = informat.format.info.raw.format; + dst_fmt = outformat.format.info.raw.format; spa_log_info(this->log, NAME " %p: %s/%d@%d.%d->%s/%d@%d.%d", this, spa_type_map_get_type(this->map, src_fmt), - inport->format.info.raw.channels, - inport->format.info.raw.rate, - inport->format.info.raw.layout, + informat.format.info.raw.channels, + informat.format.info.raw.rate, + informat.format.info.raw.layout, spa_type_map_get_type(this->map, dst_fmt), - outport->format.info.raw.channels, - outport->format.info.raw.rate, - outport->format.info.raw.layout); + outformat.format.info.raw.channels, + outformat.format.info.raw.rate, + outformat.format.info.raw.layout); - if (inport->format.info.raw.channels != outport->format.info.raw.channels) + if (informat.format.info.raw.channels != outformat.format.info.raw.channels) return -EINVAL; - if (inport->format.info.raw.rate != outport->format.info.raw.rate) + if (informat.format.info.raw.rate != outformat.format.info.raw.rate) return -EINVAL; /* find fast path */ this->conv[0] = find_conv_info(&t->audio_format, src_fmt, dst_fmt); if (this->conv[0] != NULL) { - if (inport->format.info.raw.layout == SPA_AUDIO_LAYOUT_INTERLEAVED) { - if (outport->format.info.raw.layout == SPA_AUDIO_LAYOUT_INTERLEAVED) + if (informat.format.info.raw.layout == SPA_AUDIO_LAYOUT_INTERLEAVED) { + if (outformat.format.info.raw.layout == SPA_AUDIO_LAYOUT_INTERLEAVED) this->convert = this->conv[0]->i2i; else this->convert = this->conv[0]->i2d; } else { - if (outport->format.info.raw.layout == SPA_AUDIO_LAYOUT_INTERLEAVED) + if (outformat.format.info.raw.layout == SPA_AUDIO_LAYOUT_INTERLEAVED) this->convert = this->conv[0]->d2i; else this->convert = this->conv[0]->i2i; @@ -260,17 +296,43 @@ impl_node_get_n_ports(struct spa_node *node, uint32_t *n_output_ports, uint32_t *max_output_ports) { + struct impl *this; + spa_return_val_if_fail(node != NULL, -EINVAL); - if (n_input_ports) - *n_input_ports = 1; - if (max_input_ports) - *max_input_ports = 1; - if (n_output_ports) - *n_output_ports = 1; - if (max_output_ports) - *max_output_ports = 1; + this = SPA_CONTAINER_OF(node, struct impl, node); + if (n_input_ports) + *n_input_ports = this->n_ports[SPA_DIRECTION_INPUT]; + if (max_input_ports) + *max_input_ports = MAX_PORTS; + if (n_output_ports) + *n_output_ports = this->n_ports[SPA_DIRECTION_OUTPUT]; + if (max_output_ports) + *max_output_ports = MAX_PORTS; + + return 0; +} + +static inline struct port *iterate_ports(struct impl *this, enum spa_direction direction, uint32_t max, uint32_t *state) +{ + for (; (*state & 0xffff) < MAX_PORTS && (*state >> 16) < max; (*state)++) { + struct port *p = &this->ports[direction][(*state) & 0xffff]; + if (p->valid) { + (*state) += 0x10001; + return p; + } + } + return NULL; +} + +static int collect_ports(struct impl *this, enum spa_direction direction, uint32_t *ids, uint32_t n_ids) +{ + int i, idx; + for (i = 0, idx = 0; i < MAX_PORTS && idx < n_ids; i++) { + if (this->ports[direction][i].valid) + ids[idx++] = i; + } return 0; } @@ -281,25 +343,76 @@ impl_node_get_port_ids(struct spa_node *node, uint32_t *output_ids, uint32_t n_output_ids) { + struct impl *this; + spa_return_val_if_fail(node != NULL, -EINVAL); - if (n_input_ids && input_ids) - input_ids[0] = 0; - if (n_output_ids > 0 && output_ids) - output_ids[0] = 0; + this = SPA_CONTAINER_OF(node, struct impl, node); + if (input_ids) + collect_ports(this, SPA_DIRECTION_INPUT, input_ids, n_input_ids); + if (output_ids) + collect_ports(this, SPA_DIRECTION_OUTPUT, output_ids, n_output_ids); + + return 0; +} + +static int init_port(struct impl *this, enum spa_direction direction, uint32_t port_id, uint32_t flags) +{ + struct port *port; + + port = GET_PORT(this, direction, port_id); + port->valid = true; + port->id = port_id; + + spa_list_init(&port->queue); + port->info.flags = flags; + this->n_ports[direction]++; + port->have_format = false; return 0; } static int impl_node_add_port(struct spa_node *node, enum spa_direction direction, uint32_t port_id) { - return -ENOTSUP; + struct impl *this; + + spa_return_val_if_fail(node != NULL, -EINVAL); + + this = SPA_CONTAINER_OF(node, struct impl, node); + + spa_return_val_if_fail(CHECK_FREE_PORT(this, direction, port_id), -EINVAL); + + init_port(this, direction, port_id, + SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS | + SPA_PORT_INFO_FLAG_REMOVABLE); + + spa_log_debug(this->log, NAME " %p: add port %d", this, port_id); + + return 0; } static int impl_node_remove_port(struct spa_node *node, enum spa_direction direction, uint32_t port_id) { - return -ENOTSUP; + struct impl *this; + struct port *port; + + spa_return_val_if_fail(node != NULL, -EINVAL); + + this = SPA_CONTAINER_OF(node, struct impl, node); + + spa_return_val_if_fail(CHECK_PORT(this, direction, port_id), -EINVAL); + + port = GET_PORT (this, direction, port_id); + + this->n_ports[direction]--; + if (port->have_format) + this->n_formats[direction]--; + spa_memzero(port, sizeof(struct port)); + + spa_log_debug(this->log, NAME " %p: remove port %d", this, port_id); + + return 0; } static int @@ -332,26 +445,26 @@ static int port_enum_formats(struct spa_node *node, { struct impl *this = SPA_CONTAINER_OF(node, struct impl, node); struct type *t = &this->type; - struct port *other; + struct spa_audio_info *other; - other = GET_PORT(this, SPA_DIRECTION_REVERSE(direction), 0); + other = &this->formats[SPA_DIRECTION_REVERSE(direction)].format; switch (*index) { case 0: - if (other->have_format) { + if (other->info.raw.channels > 0) { *param = spa_pod_builder_object(builder, t->param.idEnumFormat, t->format, "I", t->media_type.audio, "I", t->media_subtype.raw, - ":", t->format_audio.format, "Ieu", other->format.info.raw.format, - SPA_POD_PROP_ENUM(3, other->format.info.raw.format, + ":", t->format_audio.format, "Ieu", other->info.raw.format, + SPA_POD_PROP_ENUM(3, other->info.raw.format, t->audio_format.F32, t->audio_format.F32_OE), - ":", t->format_audio.layout, "ieu", other->format.info.raw.layout, + ":", t->format_audio.layout, "ieu", other->info.raw.layout, SPA_POD_PROP_ENUM(2, SPA_AUDIO_LAYOUT_INTERLEAVED, SPA_AUDIO_LAYOUT_NON_INTERLEAVED), - ":", t->format_audio.rate, "i", other->format.info.raw.rate, - ":", t->format_audio.channels, "i", other->format.info.raw.channels); + ":", t->format_audio.rate, "i", other->info.raw.rate, + ":", t->format_audio.channels, "i", other->info.raw.channels); } else { *param = spa_pod_builder_object(builder, t->param.idEnumFormat, t->format, @@ -465,33 +578,30 @@ impl_node_port_enum_params(struct spa_node *node, return res; } else if (id == t->param.idBuffers) { + uint32_t buffers, size; + if (!port->have_format) return -EIO; if (*index > 0) return 0; if (other->n_buffers > 0) { - param = spa_pod_builder_object(&b, - id, t->param_buffers.Buffers, - ":", t->param_buffers.buffers, "iru", other->n_buffers, - SPA_POD_PROP_MIN_MAX(1, MAX_BUFFERS), - ":", t->param_buffers.blocks, "i", port->blocks, - ":", t->param_buffers.size, "i", (other->size / other->stride) * - port->stride, - ":", t->param_buffers.stride, "i", port->stride, - ":", t->param_buffers.align, "i", 16); - } - else { - param = spa_pod_builder_object(&b, - id, t->param_buffers.Buffers, - ":", t->param_buffers.buffers, "iru", 1, - SPA_POD_PROP_MIN_MAX(1, MAX_BUFFERS), - ":", t->param_buffers.blocks, "i", port->blocks, - ":", t->param_buffers.size, "iru", 1024 * port->stride, - SPA_POD_PROP_MIN_MAX(16 * port->stride, INT32_MAX / port->stride), - ":", t->param_buffers.stride, "i", port->stride, - ":", t->param_buffers.align, "i", 16); + buffers = other->n_buffers; + size = other->size / other->stride; + } else { + buffers = 1; + size = 1024; } + + param = spa_pod_builder_object(&b, + id, t->param_buffers.Buffers, + ":", t->param_buffers.buffers, "iru", buffers, + SPA_POD_PROP_MIN_MAX(2, MAX_BUFFERS), + ":", t->param_buffers.blocks, "i", port->blocks, + ":", t->param_buffers.size, "iru", size * port->stride, + SPA_POD_PROP_MIN_MAX(16 * port->stride, INT32_MAX / port->stride), + ":", t->param_buffers.stride, "i", port->stride, + ":", t->param_buffers.align, "i", 16); } else if (id == t->param.idMeta) { if (!port->have_format) @@ -554,6 +664,14 @@ static int clear_buffers(struct impl *this, struct port *port) } return 0; } +static int compatible_format(struct spa_audio_info *info, struct spa_audio_info *info2) +{ + if (info->info.raw.format != info2->info.raw.format || + info->info.raw.layout != info2->info.raw.layout || + info->info.raw.rate != info2->info.raw.rate) + return -EINVAL; + return 0; +} static int port_set_format(struct spa_node *node, enum spa_direction direction, @@ -562,19 +680,20 @@ static int port_set_format(struct spa_node *node, const struct spa_pod *format) { struct impl *this = SPA_CONTAINER_OF(node, struct impl, node); - struct port *port, *other; + struct port *port; struct type *t = &this->type; int res = 0; port = GET_PORT(this, direction, port_id); - other = GET_PORT(this, SPA_DIRECTION_REVERSE(direction), port_id); if (format == NULL) { if (port->have_format) { port->have_format = false; + this->n_formats[direction]--; + this->formats[direction].format.info.raw.channels -= port->format.info.raw.channels; clear_buffers(this, port); + this->convert = NULL; } - this->convert = NULL; } else { struct spa_audio_info info = { 0 }; @@ -589,6 +708,16 @@ static int port_set_format(struct spa_node *node, if (spa_format_audio_raw_parse(format, &info.info.raw, &t->format_audio) < 0) return -EINVAL; + if (this->n_formats[direction] > 0) { + if (compatible_format(&info, &this->formats[direction].format) < 0) + return -EINVAL; + this->formats[direction].format.info.raw.channels += info.info.raw.channels; + } + else { + this->formats[direction].format = info; + } + this->n_formats[direction]++; + port->have_format = true; port->format = info; @@ -602,11 +731,12 @@ static int port_set_format(struct spa_node *node, port->blocks = info.info.raw.channels; } - if (other->have_format) + if (this->n_formats[SPA_DIRECTION_INPUT] == this->n_ports[SPA_DIRECTION_INPUT] && + this->n_formats[SPA_DIRECTION_OUTPUT] == this->n_ports[SPA_DIRECTION_OUTPUT]) res = setup_convert(this); - spa_log_debug(this->log, NAME " %p: set format on port %d %d %d %d", - this, port_id, res, port->stride, other->stride); + spa_log_debug(this->log, NAME " %p: set format on port %d %d %d", + this, port_id, res, port->stride); } return res; } @@ -724,20 +854,21 @@ impl_node_port_set_io(struct spa_node *node, port = GET_PORT(this, direction, port_id); - if (id == t->io.Buffers) { - spa_log_trace(this->log, NAME " %p: port %d update buffer io %p", - this, port_id, data); + spa_log_trace(this->log, NAME " %p: port %d:%d update io %d %p", + this, direction, port_id, id, data); + + if (id == t->io.Buffers) port->io = data; - } + else if (id == t->io.ControlRange) + port->ctrl = data; else return -ENOENT; return 0; } -static void recycle_buffer(struct impl *this, uint32_t id) +static void recycle_buffer(struct impl *this, struct port *port, uint32_t id) { - struct port *port = GET_OUT_PORT(this, 0); struct buffer *b = &port->buffers[id]; if (SPA_FLAG_CHECK(b->flags, BUFFER_FLAG_OUT)) { @@ -765,6 +896,7 @@ static struct buffer *dequeue_buffer(struct impl *this, struct port *port) static int impl_node_port_reuse_buffer(struct spa_node *node, uint32_t port_id, uint32_t buffer_id) { struct impl *this; + struct port *port; spa_return_val_if_fail(node != NULL, -EINVAL); @@ -772,7 +904,9 @@ static int impl_node_port_reuse_buffer(struct spa_node *node, uint32_t port_id, spa_return_val_if_fail(CHECK_PORT(this, SPA_DIRECTION_OUTPUT, port_id), -EINVAL); - recycle_buffer(this, buffer_id); + port = GET_OUT_PORT(this, port_id); + + recycle_buffer(this, port, buffer_id); return 0; } @@ -786,18 +920,119 @@ impl_node_port_send_command(struct spa_node *node, return -ENOTSUP; } +static int process_split(struct impl *this) +{ + struct port *inport, *outport; + struct spa_io_buffers *inio, *outio; + struct buffer *inbuf, *outbuf; + struct spa_buffer *inb, *outb; + const void **src_datas; + void **dst_datas; + uint32_t n_src_datas, n_outs, n_dst_datas; + int i, j, res = 0, n_bytes = 0, maxsize; + uint32_t size; + + inport = GET_IN_PORT(this, 0); + inio = inport->io; + spa_return_val_if_fail(inio != NULL, -EIO); + + spa_log_trace(this->log, NAME " %p: status %p %d", this, inio, inio->status); + + if (inio->status != SPA_STATUS_HAVE_BUFFER) + return SPA_STATUS_NEED_BUFFER; + if (inio->buffer_id >= inport->n_buffers) + return inio->status = -EINVAL; + + inbuf = &inport->buffers[inio->buffer_id]; + inb = inbuf->outbuf; + + n_src_datas = inb->n_datas; + src_datas = alloca(sizeof(void*) * n_src_datas); + + size = inb->datas[0].chunk->size; + for (i = 0; i < n_src_datas; i++) + src_datas[i] = SPA_MEMBER(inb->datas[i].data, inport->offset, void); + + n_outs = this->n_ports[SPA_DIRECTION_OUTPUT]; + dst_datas = alloca(sizeof(void*) * MAX_PORTS); + n_dst_datas = 0; + + for (i = 0; i < n_outs; i++) { + outport = GET_OUT_PORT(this, i); + outio = outport->io; + if (outio == NULL) + continue; + spa_log_trace(this->log, NAME " %p: %d %p %d %d %d", this, i, + outio, outio->status, outio->buffer_id, outport->stride); + + if (outio->status == SPA_STATUS_HAVE_BUFFER) { + res |= SPA_STATUS_HAVE_BUFFER; + continue; + } + + if (outio->buffer_id < outport->n_buffers) { + recycle_buffer(this, outport, outio->buffer_id); + outio->buffer_id = SPA_ID_INVALID; + } + + if ((outbuf = dequeue_buffer(this, outport)) == NULL) + return outio->status = -EPIPE; + + outb = outbuf->outbuf; + + maxsize = outb->datas[0].maxsize; + if (outport->ctrl) + maxsize = SPA_MIN(outport->ctrl->max_size, maxsize); + maxsize = (maxsize / outport->stride) * inport->stride; + n_bytes = SPA_MIN(size - inport->offset, maxsize); + + for (j = 0; j < outb->n_datas; j++) { + dst_datas[n_dst_datas++] = outb->datas[j].data; + outb->datas[j].chunk->offset = 0; + outb->datas[j].chunk->size = (n_bytes / inport->stride) * outport->stride; + } + + outio->status = SPA_STATUS_HAVE_BUFFER; + outio->buffer_id = outb->id; + res |= SPA_STATUS_HAVE_BUFFER; + } + + spa_log_trace(this->log, NAME " %p: %d %d %d %d %d %d", this, + n_src_datas, n_dst_datas, n_bytes, inport->offset, size, inport->stride); + + if (n_dst_datas > 0) { + this->convert(this, n_dst_datas, dst_datas, n_src_datas, src_datas, n_bytes); + + inport->offset += n_bytes; + if (inport->offset >= size) { + inio->status = SPA_STATUS_NEED_BUFFER; + inport->offset = 0; + res |= SPA_STATUS_NEED_BUFFER; + } + } + return res; +} + static int impl_node_process(struct spa_node *node) { struct impl *this; +#if 0 struct port *outport, *inport; struct spa_io_buffers *outio, *inio; struct buffer *sbuf, *dbuf; int res = 0; +#endif spa_return_val_if_fail(node != NULL, -EINVAL); this = SPA_CONTAINER_OF(node, struct impl, node); + if (this->n_ports[SPA_DIRECTION_OUTPUT] >= 1) + return process_split(this); + else + return -ENOTSUP; + +#if 0 outport = GET_OUT_PORT(this, 0); inport = GET_IN_PORT(this, 0); @@ -818,7 +1053,7 @@ static int impl_node_process(struct spa_node *node) /* recycle */ if (outio->buffer_id < outport->n_buffers) { - recycle_buffer(this, outio->buffer_id); + recycle_buffer(this, outport, outio->buffer_id); outio->buffer_id = SPA_ID_INVALID; } @@ -832,12 +1067,12 @@ static int impl_node_process(struct spa_node *node) { int i, n_bytes, maxsize; + uint32_t size; struct spa_buffer *sb = sbuf->outbuf, *db = dbuf->outbuf; uint32_t n_src_datas = sb->n_datas; uint32_t n_dst_datas = db->n_datas; const void *src_datas[n_src_datas]; void *dst_datas[n_dst_datas]; - uint32_t size; size = sb->datas[0].chunk->size; maxsize = (db->datas[0].maxsize / outport->stride) * inport->stride; @@ -868,6 +1103,7 @@ static int impl_node_process(struct spa_node *node) res |= SPA_STATUS_HAVE_BUFFER; return res; +#endif } static const struct spa_node impl_node = { @@ -929,7 +1165,6 @@ impl_init(const struct spa_handle_factory *factory, uint32_t n_support) { struct impl *this; - struct port *port; uint32_t i; spa_return_val_if_fail(factory != NULL, -EINVAL); @@ -954,15 +1189,8 @@ impl_init(const struct spa_handle_factory *factory, this->node = impl_node; - port = GET_OUT_PORT(this, 0); - port->id = 0; - port->info.flags = SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS; - spa_list_init(&port->queue); - - port = GET_IN_PORT(this, 0); - port->id = 0; - port->info.flags = SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS; - spa_list_init(&port->queue); + init_port(this, SPA_DIRECTION_OUTPUT, 0, SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS); + init_port(this, SPA_DIRECTION_INPUT, 0, SPA_PORT_INFO_FLAG_CAN_USE_BUFFERS); props_reset(&this->props); diff --git a/spa/plugins/audioconvert/resample.c b/spa/plugins/audioconvert/resample.c index 0ac17f377..3d2c410f8 100644 --- a/spa/plugins/audioconvert/resample.c +++ b/spa/plugins/audioconvert/resample.c @@ -440,8 +440,9 @@ impl_node_port_enum_params(struct spa_node *node, ":", t->param_buffers.buffers, "iru", other->n_buffers, SPA_POD_PROP_MIN_MAX(1, MAX_BUFFERS), ":", t->param_buffers.blocks, "i", port->blocks, - ":", t->param_buffers.size, "i", (other->size / other->stride) * + ":", t->param_buffers.size, "iru", (other->size / other->stride) * port->stride, + SPA_POD_PROP_MIN_MAX(16 * port->stride, INT32_MAX / port->stride), ":", t->param_buffers.stride, "i", port->stride, ":", t->param_buffers.align, "i", 16); } else { diff --git a/src/modules/module-client-node/client-stream.c b/src/modules/module-client-node/client-stream.c index 4f689c641..5661fb722 100644 --- a/src/modules/module-client-node/client-stream.c +++ b/src/modules/module-client-node/client-stream.c @@ -906,7 +906,7 @@ static void client_node_initialized(void *data) media_type == impl->type.media_type.audio && media_subtype == impl->type.media_subtype.raw) { if ((impl->adapter = pw_load_spa_interface("audioconvert/libspa-audioconvert", - "splitter", SPA_TYPE__Node, NULL, 0, NULL)) == NULL) + "audioconvert", SPA_TYPE__Node, NULL, 0, NULL)) == NULL) return; impl->use_converter = true; diff --git a/src/pipewire/stream.c b/src/pipewire/stream.c index dc97cd472..3ce7b4c87 100644 --- a/src/pipewire/stream.c +++ b/src/pipewire/stream.c @@ -105,7 +105,6 @@ struct stream { struct queue dequeued; struct queue queued; - uint32_t n_orig_params; uint32_t n_init_params; struct spa_pod **init_params; @@ -113,6 +112,7 @@ struct stream { struct spa_pod **params; struct spa_pod *format; + struct spa_pod *conv_format; uint32_t pending_seq; bool disconnecting; @@ -123,10 +123,6 @@ struct stream { bool free_data; struct data data; - - bool use_converter; - struct spa_node *convert; - struct spa_io_buffers conv_io; }; @@ -166,80 +162,6 @@ static inline struct buffer *pop_queue(struct stream *stream, struct queue *queu return buffer; } -/* check if the server format is compatible with the requested - * formats, if not, set up converters when allowed */ -static int configure_converter(struct stream *impl) -{ - struct pw_type *t = impl->t; - int i, res; - struct spa_pod *param; - - impl->use_converter = false; - - /* check if format compatible with filter */ - for (i = 0; i < impl->n_orig_params; i++) { - uint8_t buffer[4096]; - struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, 4096); - struct spa_pod *filtered; - - param = impl->init_params[i]; - - if (spa_pod_is_object_type(param, t->spa_format)) { - if (spa_pod_filter(&b, &filtered, impl->format, param) >= 0) { - pw_log_debug("stream %p: format matches filter", impl); - return 0; - } - } - } - - if (impl->convert == NULL) - return -ENOTSUP; - - /* configure the converter */ - if ((res = spa_node_port_set_param(impl->convert, - impl->direction, 0, - t->param.idFormat, 0, - impl->format)) < 0) - return res; - - - /* try to configure the other end */ - for (i = 0; i < impl->n_orig_params; i++) { - param = impl->init_params[i]; - - if (spa_pod_is_object_type(param, t->spa_format)) { - if ((res = spa_node_port_set_param(impl->convert, - SPA_DIRECTION_REVERSE(impl->direction), 0, - t->param.idFormat, - SPA_NODE_PARAM_FLAG_FIXATE, - param)) < 0) - continue; - - /* other end set and fixated */ - impl->use_converter = true; - break; - } - } - /* when we get here without valid configured converter we fail */ - if (!impl->use_converter) - return -ENOTSUP; - - if (impl->io != &impl->conv_io) { - pw_log_debug("stream %p: update io %p %p", impl, impl->io, &impl->conv_io); - res = spa_node_port_set_io(impl->convert, - impl->direction, 0, - t->io.Buffers, - impl->io, sizeof(struct spa_io_buffers)); - impl->io = &impl->conv_io; - res = spa_node_port_set_io(impl->convert, - SPA_DIRECTION_REVERSE(impl->direction), 0, - t->io.Buffers, - impl->io, sizeof(struct spa_io_buffers)); - } - - return 0; -} - static bool stream_set_state(struct pw_stream *stream, enum pw_stream_state state, const char *error) { enum pw_stream_state old = stream->state; @@ -307,8 +229,6 @@ static int impl_send_command(struct spa_node *node, const struct spa_command *co if (impl->direction == SPA_DIRECTION_INPUT) { impl->io->status = SPA_STATUS_NEED_BUFFER; impl->io->buffer_id = SPA_ID_INVALID; - impl->conv_io.status = SPA_STATUS_NEED_BUFFER; - impl->conv_io.buffer_id = SPA_ID_INVALID; } else { call_process(impl); @@ -390,17 +310,7 @@ static int impl_port_set_io(struct spa_node *node, enum spa_direction direction, if (id == t->io.Buffers && size >= sizeof(struct spa_io_buffers)) { pw_log_debug("stream %p: set io %d %p %zd", impl, id, data, size); - - if (impl->use_converter) { - impl->io = &impl->conv_io; - res = spa_node_port_set_io(impl->convert, - direction, 0, id, data, size); - res = spa_node_port_set_io(impl->convert, - SPA_DIRECTION_REVERSE(direction), 0, - id, impl->io, size); - } - else - impl->io = data; + impl->io = data; } else res = -ENOENT; @@ -481,9 +391,11 @@ static int port_set_format(struct spa_node *node, struct stream *impl = SPA_CONTAINER_OF(node, struct stream, impl_node); struct pw_stream *stream = &impl->this; struct pw_type *t = impl->t; - int res, count; + int count; - pw_log_debug("stream %p: format changed", impl); + pw_log_debug("stream %p: format changed:", impl); + if (pw_log_level >= SPA_LOG_LEVEL_DEBUG) + spa_debug_pod(format, SPA_DEBUG_FLAG_FORMAT); if (impl->format) free(impl->format); @@ -491,18 +403,14 @@ static int port_set_format(struct spa_node *node, if (spa_pod_is_object_type(format, t->spa_format)) { impl->format = pw_spa_pod_copy(format); ((struct spa_pod_object*)impl->format)->body.id = t->param.idFormat; - - if ((res = configure_converter(impl)) < 0) { - pw_stream_finish_format(stream, res, NULL, 0); - return res; - } } else impl->format = NULL; count = spa_hook_list_call(&stream->listener_list, struct pw_stream_events, - format_changed, impl->format); + format_changed, + impl->format); if (count == 0) pw_stream_finish_format(stream, 0, NULL, 0); @@ -603,9 +511,6 @@ static int impl_port_use_buffers(struct spa_node *node, enum spa_direction direc clear_buffers(stream); - if (impl->use_converter) - SPA_FLAG_SET(flags, PW_STREAM_FLAG_MAP_BUFFERS); - for (i = 0; i < n_buffers; i++) { int buf_size = 0; struct buffer *b = &impl->buffers[i]; @@ -639,35 +544,6 @@ static int impl_port_use_buffers(struct spa_node *node, enum spa_direction direc buffers[i]->n_datas, size); } - if (impl->use_converter) { - struct spa_data datas[1]; - uint32_t data_aligns[1]; - - if ((res = spa_node_port_use_buffers(impl->convert, - impl->direction, 0, - buffers, - n_buffers)) < 0) - return res; - - n_buffers = 6; - - datas[0].type = t->data.MemPtr; - datas[0].maxsize = size; - data_aligns[0] = 16; - - buffers = spa_buffer_alloc_array(n_buffers, 0, - 0, NULL, - 1, datas, - data_aligns); - if (buffers == NULL) - return -ENOMEM; - - if ((res = spa_node_port_use_buffers(impl->convert, - SPA_DIRECTION_REVERSE(impl->direction), 0, - buffers, n_buffers)) < 0) - return res; - } - for (i = 0; i < n_buffers; i++) { struct buffer *b = &impl->buffers[i]; @@ -760,23 +636,13 @@ static int impl_node_process_output(struct spa_node *node) pw_log_trace("stream %p: no more buffers %p", stream, io); } } - if (io->status == SPA_STATUS_HAVE_BUFFER && impl->use_converter) { - res = spa_node_process(impl->convert); - if (SPA_FLAG_CHECK(res, SPA_STATUS_NEED_BUFFER)) - call_process(impl); - - if (!SPA_FLAG_CHECK(res, SPA_STATUS_HAVE_BUFFER)) + if (!SPA_FLAG_CHECK(impl->flags, PW_STREAM_FLAG_DRIVER)) { + call_process(impl); + if (spa_ringbuffer_get_read_index(&impl->queued.ring, &index) > 0 && + io->status == SPA_STATUS_NEED_BUFFER) goto again; - } else { - if (!SPA_FLAG_CHECK(impl->flags, PW_STREAM_FLAG_DRIVER)) { - call_process(impl); - if (spa_ringbuffer_get_read_index(&impl->queued.ring, &index) > 0 && - io->status == SPA_STATUS_NEED_BUFFER) - goto again; - } - res = io->status; } - + res = io->status; pw_log_trace("stream %p: res %d", stream, res); return res; @@ -975,10 +841,6 @@ set_init_params(struct pw_stream *stream, const struct spa_pod **init_params) { struct stream *impl = SPA_CONTAINER_OF(stream, struct stream, this); - struct pw_type *t = impl->t; -#define CONVERT_AUDIO (1<<0) -#define CONVERT_VIDEO (1<<1) - uint32_t convert_mask = 0; int i; if (impl->init_params) { @@ -989,59 +851,9 @@ set_init_params(struct pw_stream *stream, } if (n_init_params > 0) { impl->init_params = malloc(n_init_params * sizeof(struct spa_pod *)); - for (i = 0; i < n_init_params; i++) { + for (i = 0; i < n_init_params; i++) impl->init_params[i] = pw_spa_pod_copy(init_params[i]); - - if (spa_pod_is_object_type(impl->init_params[i], t->spa_format)) { - uint32_t media_type, media_subtype; - - spa_pod_object_parse(impl->init_params[i], - "I", &media_type, - "I", &media_subtype); - - if (media_type == impl->type.media_type.audio && - media_subtype == impl->type.media_subtype.raw) - SPA_FLAG_SET(convert_mask, CONVERT_AUDIO); - else if (media_type == impl->type.media_type.video && - media_subtype == impl->type.media_subtype.raw) - SPA_FLAG_SET(convert_mask, CONVERT_VIDEO); - } - } } - impl->n_orig_params = n_init_params; - - if (convert_mask && !SPA_FLAG_CHECK(impl->flags, PW_STREAM_FLAG_NO_CONVERT)) { - uint32_t state = 0; - int res; - - if (SPA_FLAG_CHECK(convert_mask, CONVERT_AUDIO)) { - if ((impl->convert = pw_load_spa_interface("audioconvert/libspa-audioconvert", - "audioconvert", SPA_TYPE__Node, NULL, 0, NULL)) == NULL) - goto done; - } - if (SPA_FLAG_CHECK(convert_mask, CONVERT_VIDEO)) { - if ((impl->convert = pw_load_spa_interface("videoconvert/libspa-videoconvert", - "videoconvert", SPA_TYPE__Node, NULL, 0, NULL)) == NULL) - goto done; - } - - while (true) { - uint8_t buffer[4096]; - struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, 4096); - struct spa_pod *param; - - if ((res = spa_node_port_enum_params(impl->convert, - impl->direction, 0, - t->param.idEnumFormat, &state, - NULL, ¶m, &b)) <= 0) - break; - - impl->init_params = realloc(impl->init_params, - (n_init_params + 1) * sizeof(struct spa_pod *)); - impl->init_params[n_init_params++] = pw_spa_pod_copy(param); - } - } - done: impl->n_init_params = n_init_params; }