spa: add some latency helpers

Add more fields to the latency object.
Add methods to create, parse and process latency info.
This commit is contained in:
Wim Taymans 2021-05-24 18:01:19 +02:00
parent 0779862cda
commit b9b89b92b2
15 changed files with 245 additions and 145 deletions

View file

@ -637,6 +637,8 @@ static void input_remove(struct pw_impl_link *this, struct pw_impl_port *port)
spa_list_remove(&this->input_link);
pw_impl_port_emit_link_removed(this->input, this);
pw_impl_port_recalc_latency(this->input);
if ((res = pw_impl_port_use_buffers(port, mix, 0, NULL, 0)) < 0) {
pw_log_warn(NAME" %p: port %p clear error %s", this, port, spa_strerror(res));
}
@ -657,6 +659,8 @@ static void output_remove(struct pw_impl_link *this, struct pw_impl_port *port)
spa_list_remove(&this->output_link);
pw_impl_port_emit_link_removed(this->output, this);
pw_impl_port_recalc_latency(this->output);
/* we don't clear output buffers when the link goes away. They will get
* cleared when the node goes to suspend */
pw_impl_port_release_mix(port, mix);
@ -932,17 +936,6 @@ static bool pw_impl_node_can_reach(struct pw_impl_node *output, struct pw_impl_n
return false;
}
static void recalculate_latencies(struct impl *impl)
{
struct pw_impl_link *this = &impl->this;
/* from output port we get capture latency and propagate this
* on the input port */
pw_impl_port_recalc_latency(this->output);
/* from input port we get playback latency and propagate that
* on the output port */
pw_impl_port_recalc_latency(this->input);
}
static void try_link_controls(struct impl *impl, struct pw_impl_port *output, struct pw_impl_port *input)
{
struct pw_control *cin, *cout;
@ -1156,10 +1149,11 @@ struct pw_impl_link *pw_context_create_link(struct pw_context *context,
pw_impl_port_emit_link_added(output, this);
pw_impl_port_emit_link_added(input, this);
recalculate_latencies(impl);
try_link_controls(impl, output, input);
pw_impl_port_recalc_latency(this->output);
pw_impl_port_recalc_latency(this->input);
pw_impl_node_emit_peer_added(impl->onode, impl->inode);
return this;

View file

@ -25,6 +25,7 @@
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <float.h>
#include <spa/pod/parser.h>
#include <spa/param/audio/format-utils.h>
@ -355,24 +356,23 @@ static int process_latency_param(void *data, int seq,
uint32_t id, uint32_t index, uint32_t next, struct spa_pod *param)
{
struct pw_impl_port *this = data;
uint32_t direction;
struct pw_port_latency latency;
struct spa_latency_info latency;
if (id != SPA_PARAM_Latency)
return -EINVAL;
if (spa_pod_parse_object(param,
SPA_TYPE_OBJECT_ParamLatency, NULL,
SPA_PARAM_LATENCY_direction,SPA_POD_Id(&direction),
SPA_PARAM_LATENCY_quantum,SPA_POD_Float(&latency.quantum),
SPA_PARAM_LATENCY_min, SPA_POD_Int(&latency.min),
SPA_PARAM_LATENCY_max, SPA_POD_Int(&latency.max)) < 0)
if (spa_latency_parse(param, &latency) < 0)
return 0;
if (direction != this->direction)
if (latency.direction != this->direction)
return 0;
pw_log_info("got latency %f %d %d", latency.quantum, latency.min, latency.max);
this->latency[direction] = latency;
pw_log_info("port %p: got %s latency %f-%f %d-%d %"PRIu64"-%"PRIu64, this,
pw_direction_as_string(this->direction),
latency.min_quantum, latency.max_quantum,
latency.min_rate, latency.max_rate,
latency.min_ns, latency.max_ns);
this->latency[this->direction] = latency;
return 0;
}
@ -515,6 +515,9 @@ struct pw_impl_port *pw_context_create_port(
pw_map_init(&this->mix_port_map, 64, 64);
this->latency[SPA_DIRECTION_INPUT] = SPA_LATENCY_INFO(SPA_DIRECTION_INPUT);
this->latency[SPA_DIRECTION_OUTPUT] = SPA_LATENCY_INFO(SPA_DIRECTION_OUTPUT);
if (info)
update_info(this, info);
@ -1288,55 +1291,66 @@ int pw_impl_port_for_each_link(struct pw_impl_port *port,
return res;
}
static void port_set_latency(struct pw_impl_port *port, struct pw_port_latency *latency)
static void port_set_latency(struct pw_impl_port *port, struct spa_latency_info *latency)
{
struct pw_port_latency *current = &port->latency[port->direction];
struct spa_latency_info *current = &port->latency[latency->direction];
struct spa_pod *param;
struct spa_pod_builder b = { 0 };
uint8_t buffer[1024];
if (current->quantum == latency->quantum &&
current->min == latency->min &&
current->max == latency->max)
if (spa_latency_info_compare(current, latency) == 0)
return;
*current = *latency;
pw_log_info("port set latency %d %d %f", latency->min, latency->max, latency->quantum);
pw_log_info("port %p: set %s latency %f-%f %d-%d %"PRIu64"-%"PRIu64, port,
pw_direction_as_string(latency->direction),
latency->min_quantum, latency->max_quantum,
latency->min_rate, latency->max_rate,
latency->min_ns, latency->max_ns);
spa_pod_builder_init(&b, buffer, sizeof(buffer));
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamLatency, SPA_PARAM_Latency,
SPA_PARAM_LATENCY_direction, SPA_POD_Id(port->direction),
SPA_PARAM_LATENCY_quantum, SPA_POD_Float(latency->quantum),
SPA_PARAM_LATENCY_min, SPA_POD_Int(latency->min),
SPA_PARAM_LATENCY_max, SPA_POD_Int(latency->max));
param = spa_latency_build(&b, SPA_PARAM_Latency, latency);
pw_impl_port_set_param(port, SPA_PARAM_Latency, 0, param);
}
int pw_impl_port_recalc_latency(struct pw_impl_port *port)
{
struct pw_impl_link *l;
struct pw_port_latency latency = { .quantum = 0.0f, .min = UINT32_MAX, .max = 0 };
struct spa_latency_info latency;
struct pw_impl_port *other;
latency = SPA_LATENCY_INFO(SPA_DIRECTION_REVERSE(port->direction));
latency.min_quantum = FLT_MAX;
latency.min_rate = INT32_MAX;
latency.min_ns = INT64_MAX;
if (port->direction == PW_DIRECTION_OUTPUT) {
spa_list_for_each(l, &port->links, output_link) {
other = l->input;
latency.quantum = SPA_MAX(latency.quantum, other->latency[other->direction].quantum);
latency.min = SPA_MIN(latency.min, other->latency[other->direction].min);
latency.max = SPA_MAX(latency.max, other->latency[other->direction].max);
spa_latency_info_combine(&latency, &other->latency[other->direction]);
pw_log_info("port %p: peer %p: latency %f-%f %d-%d %"PRIu64"-%"PRIu64,
port, other,
latency.min_quantum, latency.max_quantum,
latency.min_rate, latency.max_rate,
latency.min_ns, latency.max_ns);
}
} else {
spa_list_for_each(l, &port->links, input_link) {
other = l->input;
latency.quantum = SPA_MAX(latency.quantum, other->latency[other->direction].quantum);
latency.min = SPA_MIN(latency.min, other->latency[other->direction].min);
latency.max = SPA_MAX(latency.max, other->latency[other->direction].max);
other = l->output;
spa_latency_info_combine(&latency, &other->latency[other->direction]);
pw_log_info("port %p: peer %p: latency %f-%f %d-%d %"PRIu64"-%"PRIu64,
port, other,
latency.min_quantum, latency.max_quantum,
latency.min_rate, latency.max_rate,
latency.min_ns, latency.max_ns);
}
}
if (latency.min == UINT32_MAX)
latency.min = 0;
if (latency.min_quantum == FLT_MAX)
latency.min_quantum = 0.0f;
if (latency.min_rate == INT32_MAX)
latency.min_rate = 0U;
if (latency.min_ns == INT64_MAX)
latency.min_ns = 0UL;
port_set_latency(port, &latency);
return 0;

View file

@ -36,6 +36,7 @@ extern "C" {
#include <spa/support/plugin.h>
#include <spa/pod/builder.h>
#include <spa/param/latency-utils.h>
#include <spa/utils/result.h>
#include <spa/utils/type-info.h>
@ -686,12 +687,6 @@ struct pw_impl_node {
void *user_data; /**< extra user data */
};
struct pw_port_latency {
float quantum; /** quantum multiplier */
uint32_t min; /** min of all peers */
uint32_t max; /** max of all peers */
};
struct pw_impl_port_mix {
struct spa_list link;
struct spa_list rt_link;
@ -796,7 +791,7 @@ struct pw_impl_port {
} rt; /**< data only accessed from the data thread */
unsigned int added:1;
struct pw_port_latency latency[2]; /**< latencies */
struct spa_latency_info latency[2]; /**< latencies */
void *owner_data; /**< extra owner data */
void *user_data; /**< extra user data */

View file

@ -45,6 +45,7 @@
#include <spa/utils/string.h>
#include <spa/utils/json.h>
#include <spa/debug/types.h>
#include <spa/debug/pod.h>
#include <pipewire/pipewire.h>
#include <pipewire/i18n.h>
@ -835,13 +836,15 @@ on_io_changed(void *userdata, uint32_t id, void *data, uint32_t size)
}
static void
on_param_changed(void *userdata, uint32_t id, const struct spa_pod *format)
on_param_changed(void *userdata, uint32_t id, const struct spa_pod *param)
{
struct data *data = userdata;
if (data->verbose)
printf("stream param change: id=%"PRIu32"\n",
id);
if (data->verbose) {
printf("stream param change: id=%"PRIu32"\n", id);
if (param != NULL)
spa_debug_pod(0, NULL, param);
}
}
static void on_process(void *userdata)