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

@ -0,0 +1,117 @@
/* Simple Plugin API
*
* Copyright © 2021 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef SPA_PARAM_LATENCY_UTILS_H
#define SPA_PARAM_LATENCY_UTILS_H
#ifdef __cplusplus
extern "C" {
#endif
#include <spa/pod/parser.h>
struct spa_latency_info {
enum spa_direction direction;
float min_quantum;
float max_quantum;
uint32_t min_rate;
uint32_t max_rate;
uint64_t min_ns;
uint64_t max_ns;
};
#define SPA_LATENCY_INFO(dir) (struct spa_latency_info) { .direction = (dir) }
static inline int
spa_latency_info_compare(const struct spa_latency_info *a, struct spa_latency_info *b)
{
if (a->min_quantum == b->min_quantum &&
a->max_quantum == b->max_quantum &&
a->min_rate == b->min_rate &&
a->max_rate == b->max_rate &&
a->min_ns == b->min_ns &&
a->max_ns == b->max_ns)
return 0;
return 1;
}
static inline int
spa_latency_info_combine(struct spa_latency_info *info, const struct spa_latency_info *other)
{
if (info->direction != other->direction)
return -EINVAL;
if (info->min_quantum == 0.0f || other->min_quantum < info->min_quantum)
info->min_quantum = other->min_quantum;
if (other->max_quantum > info->max_quantum)
info->max_quantum = other->max_quantum;
if (info->min_rate == 0U || other->min_rate < info->min_rate)
info->min_rate = other->min_rate;
if (other->max_rate > info->max_rate)
info->max_rate = other->max_rate;
if (info->min_ns == 0UL || other->min_ns < info->min_ns)
info->min_ns = other->min_ns;
if (other->max_ns > info->max_ns)
info->max_ns = other->max_ns;
return 0;
}
static inline int
spa_latency_parse(const struct spa_pod *latency, struct spa_latency_info *info)
{
int res;
spa_zero(*info);
if ((res = spa_pod_parse_object(latency,
SPA_TYPE_OBJECT_ParamLatency, NULL,
SPA_PARAM_LATENCY_direction, SPA_POD_Id(&info->direction),
SPA_PARAM_LATENCY_minQuantum, SPA_POD_OPT_Float(&info->min_quantum),
SPA_PARAM_LATENCY_maxQuantum, SPA_POD_OPT_Float(&info->max_quantum),
SPA_PARAM_LATENCY_minRate, SPA_POD_OPT_Int(&info->min_rate),
SPA_PARAM_LATENCY_maxRate, SPA_POD_OPT_Int(&info->max_rate),
SPA_PARAM_LATENCY_minNs, SPA_POD_OPT_Long(&info->min_ns),
SPA_PARAM_LATENCY_maxNs, SPA_POD_OPT_Long(&info->max_ns))) < 0)
return res;
info->direction &= 1;
return 0;
}
static inline struct spa_pod *
spa_latency_build(struct spa_pod_builder *builder, uint32_t id, const struct spa_latency_info *info)
{
return spa_pod_builder_add_object(builder,
SPA_TYPE_OBJECT_ParamLatency, id,
SPA_PARAM_LATENCY_direction, SPA_POD_Id(info->direction),
SPA_PARAM_LATENCY_minQuantum, SPA_POD_Float(info->min_quantum),
SPA_PARAM_LATENCY_maxQuantum, SPA_POD_Float(info->max_quantum),
SPA_PARAM_LATENCY_minRate, SPA_POD_Int(info->min_rate),
SPA_PARAM_LATENCY_maxRate, SPA_POD_Int(info->max_rate),
SPA_PARAM_LATENCY_minNs, SPA_POD_Long(info->min_ns),
SPA_PARAM_LATENCY_maxNs, SPA_POD_Long(info->max_ns));
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* SPA_PARAM_LATENCY_UTILS_H */

View file

@ -177,9 +177,12 @@ enum spa_param_route {
enum spa_param_latency {
SPA_PARAM_LATENCY_START,
SPA_PARAM_LATENCY_direction, /**< direction, input/output (Id enum spa_direction) */
SPA_PARAM_LATENCY_quantum, /**< latency relative to quantum (Float) */
SPA_PARAM_LATENCY_min, /**< min latency (Int) relative to rate */
SPA_PARAM_LATENCY_max, /**< max latency (Int) relative to rate */
SPA_PARAM_LATENCY_minQuantum, /**< min latency relative to quantum (Float) */
SPA_PARAM_LATENCY_maxQuantum, /**< max latency relative to quantum (Float) */
SPA_PARAM_LATENCY_minRate, /**< min latency (Int) relative to rate */
SPA_PARAM_LATENCY_maxRate, /**< max latency (Int) relative to rate */
SPA_PARAM_LATENCY_minNs, /**< min latency (Long) in nanoseconds */
SPA_PARAM_LATENCY_maxNs, /**< max latency (Long) in nanoseconds */
};
/**

View file

@ -390,9 +390,12 @@ static const struct spa_type_info spa_type_profiler[] = {
static const struct spa_type_info spa_type_param_latency[] = {
{ SPA_PARAM_LATENCY_START, SPA_TYPE_Id, SPA_TYPE_INFO_PARAM_LATENCY_BASE, spa_type_param, },
{ SPA_PARAM_LATENCY_direction, SPA_TYPE_Id, SPA_TYPE_INFO_PARAM_LATENCY_BASE "direction", spa_type_direction, },
{ SPA_PARAM_LATENCY_quantum, SPA_TYPE_Float, SPA_TYPE_INFO_PARAM_LATENCY_BASE "quantum", NULL, },
{ SPA_PARAM_LATENCY_min, SPA_TYPE_Int, SPA_TYPE_INFO_PARAM_LATENCY_BASE "min", NULL, },
{ SPA_PARAM_LATENCY_max, SPA_TYPE_Int, SPA_TYPE_INFO_PARAM_LATENCY_BASE "max", NULL, },
{ SPA_PARAM_LATENCY_minQuantum, SPA_TYPE_Float, SPA_TYPE_INFO_PARAM_LATENCY_BASE "minQuantum", NULL, },
{ SPA_PARAM_LATENCY_maxQuantum, SPA_TYPE_Float, SPA_TYPE_INFO_PARAM_LATENCY_BASE "maxQuantum", NULL, },
{ SPA_PARAM_LATENCY_minRate, SPA_TYPE_Int, SPA_TYPE_INFO_PARAM_LATENCY_BASE "minRate", NULL, },
{ SPA_PARAM_LATENCY_maxRate, SPA_TYPE_Int, SPA_TYPE_INFO_PARAM_LATENCY_BASE "maxRate", NULL, },
{ SPA_PARAM_LATENCY_minNs, SPA_TYPE_Long, SPA_TYPE_INFO_PARAM_LATENCY_BASE "minNs", NULL, },
{ SPA_PARAM_LATENCY_maxNs, SPA_TYPE_Long, SPA_TYPE_INFO_PARAM_LATENCY_BASE "maxNs", NULL, },
{ 0, 0, NULL, NULL },
};

View file

@ -459,12 +459,7 @@ impl_node_port_enum_params(void *object, int seq,
case SPA_PARAM_Latency:
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamLatency, id,
SPA_PARAM_LATENCY_direction, SPA_POD_Id(direction),
SPA_PARAM_LATENCY_quantum, SPA_POD_Float(this->latency_quantum),
SPA_PARAM_LATENCY_min, SPA_POD_Int(this->latency),
SPA_PARAM_LATENCY_max, SPA_POD_Int(this->latency));
param = spa_latency_build(&b, id, &this->latency);
break;
default:
return 0;
@ -775,7 +770,9 @@ impl_init(const struct spa_handle_factory *factory,
spa_hook_list_init(&this->hooks);
this->stream = SND_PCM_STREAM_PLAYBACK;
this->latency_quantum = 1.0f;
this->latency = SPA_LATENCY_INFO(SPA_DIRECTION_INPUT);
this->latency.min_quantum = 1.0f;
this->latency.max_quantum = 1.0f;
this->info_all = SPA_NODE_CHANGE_MASK_FLAGS |
SPA_NODE_CHANGE_MASK_PROPS |

View file

@ -457,12 +457,7 @@ impl_node_port_enum_params(void *object, int seq,
case SPA_PARAM_Latency:
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamLatency, id,
SPA_PARAM_LATENCY_direction, SPA_POD_Id(direction),
SPA_PARAM_LATENCY_quantum, SPA_POD_Float(this->latency_quantum),
SPA_PARAM_LATENCY_min, SPA_POD_Int(this->latency),
SPA_PARAM_LATENCY_max, SPA_POD_Int(this->latency));
param = spa_latency_build(&b, id, &this->latency);
break;
default:
return 0;
@ -794,8 +789,9 @@ impl_init(const struct spa_handle_factory *factory,
spa_hook_list_init(&this->hooks);
this->stream = SND_PCM_STREAM_CAPTURE;
this->latency_quantum = 1.0f;
this->latency = SPA_LATENCY_INFO(SPA_DIRECTION_OUTPUT);
this->latency.min_quantum = 1.0f;
this->latency.max_quantum = 1.0f;
this->info_all = SPA_NODE_CHANGE_MASK_FLAGS |
SPA_NODE_CHANGE_MASK_PROPS |

View file

@ -630,7 +630,7 @@ int spa_alsa_set_format(struct state *state, struct spa_audio_info *fmt, uint32_
state->headroom = SPA_MIN(state->headroom, state->buffer_frames);
state->start_delay = state->default_start_delay;
state->latency = state->headroom;
state->latency.min_rate = state->latency.max_rate = state->headroom;
state->period_frames = period_size;
periods = state->buffer_frames / state->period_frames;

View file

@ -45,6 +45,7 @@ extern "C" {
#include <spa/node/io.h>
#include <spa/debug/types.h>
#include <spa/param/param.h>
#include <spa/param/latency-utils.h>
#include <spa/param/audio/format-utils.h>
#include "dll.h"
@ -192,8 +193,7 @@ struct state {
struct spa_dll dll;
double max_error;
float latency_quantum;
uint32_t latency;
struct spa_latency_info latency;
};
int

View file

@ -36,6 +36,7 @@
#include <spa/utils/list.h>
#include <spa/monitor/device.h>
#include <spa/param/audio/format.h>
#include <spa/param/latency-utils.h>
#include <spa/pod/filter.h>
#define NAME "alsa-bridge"
@ -578,13 +579,13 @@ impl_node_port_enum_params(void *object, int seq,
case SPA_PARAM_Latency:
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamLatency, id,
SPA_PARAM_LATENCY_direction, SPA_POD_Id(direction),
SPA_PARAM_LATENCY_quantum, SPA_POD_Float(1.0f),
SPA_PARAM_LATENCY_min, SPA_POD_Int(0),
SPA_PARAM_LATENCY_max, SPA_POD_Int(0));
{
struct spa_latency_info info = {
.direction = direction,
.min_quantum = 1.0f, .max_quantum = 1.0f };
param = spa_latency_build(&b, id, &info);
break;
}
default:
return 0;
}

View file

@ -40,6 +40,7 @@
#include <spa/node/keys.h>
#include <spa/param/audio/format-utils.h>
#include <spa/param/param.h>
#include <spa/param/latency-utils.h>
#include <spa/pod/filter.h>
#include <spa/debug/types.h>
#include <spa/debug/pod.h>
@ -176,9 +177,7 @@ struct impl {
uint32_t src_remap[SPA_AUDIO_MAX_CHANNELS];
uint32_t dst_remap[SPA_AUDIO_MAX_CHANNELS];
float latency_quantum[2];
uint32_t latency_min[2];
uint32_t latency_max[2];
struct spa_latency_info latency[2];
float empty[MAX_SAMPLES + MAX_ALIGN];
};
@ -814,12 +813,7 @@ impl_node_port_enum_params(void *object, int seq,
case SPA_PARAM_Latency:
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamLatency, id,
SPA_PARAM_LATENCY_direction, SPA_POD_Id(direction),
SPA_PARAM_LATENCY_quantum, SPA_POD_Float(this->latency_quantum[direction]),
SPA_PARAM_LATENCY_min, SPA_POD_Int(this->latency_min[direction]),
SPA_PARAM_LATENCY_max, SPA_POD_Int(this->latency_max[direction]));
param = spa_latency_build(&b, id, &this->latency[direction]);
break;
default:
return 0;
@ -936,18 +930,16 @@ static int port_set_latency(void *object,
enum spa_direction other = SPA_DIRECTION_REVERSE(direction);
uint32_t i;
spa_log_debug(this->log, NAME " %p: set latency", this);
spa_log_info(this->log, NAME " %p: set latency", this);
if (latency == NULL) {
this->latency_quantum[other] = 0;
this->latency_min[other] = 0;
this->latency_max[other] = 0;
this->latency[other] = SPA_LATENCY_INFO(other);
} else {
if (spa_pod_parse_object(latency,
SPA_TYPE_OBJECT_ParamLatency, NULL,
SPA_PARAM_LATENCY_quantum,SPA_POD_Float(&this->latency_quantum[other]),
SPA_PARAM_LATENCY_min, SPA_POD_Int(&this->latency_min[other]),
SPA_PARAM_LATENCY_max, SPA_POD_Int(&this->latency_max[other])) < 0)
struct spa_latency_info info;
if (spa_latency_parse(latency, &info) < 0 ||
info.direction != other)
return -EINVAL;
this->latency[other] = info;
}
if (direction == SPA_DIRECTION_OUTPUT) {
for (i = 0; i < this->port_count; i++) {

View file

@ -37,6 +37,7 @@
#include <spa/node/utils.h>
#include <spa/node/io.h>
#include <spa/param/audio/format-utils.h>
#include <spa/param/latency-utils.h>
#include <spa/param/param.h>
#include <spa/pod/filter.h>
#include <spa/debug/types.h>
@ -126,9 +127,7 @@ struct impl {
unsigned int is_passthrough:1;
unsigned int started:1;
float latency_quantum[2];
uint32_t latency_min[2];
uint32_t latency_max[2];
struct spa_latency_info latency[2];
uint32_t src_remap[SPA_AUDIO_MAX_CHANNELS];
uint32_t dst_remap[SPA_AUDIO_MAX_CHANNELS];
@ -572,12 +571,7 @@ impl_node_port_enum_params(void *object, int seq,
case SPA_PARAM_Latency:
switch (result.index) {
case 0:
param = spa_pod_builder_add_object(&b,
SPA_TYPE_OBJECT_ParamLatency, id,
SPA_PARAM_LATENCY_direction, SPA_POD_Id(direction),
SPA_PARAM_LATENCY_quantum, SPA_POD_Float(this->latency_quantum[direction]),
SPA_PARAM_LATENCY_min, SPA_POD_Int(this->latency_min[direction]),
SPA_PARAM_LATENCY_max, SPA_POD_Int(this->latency_max[direction]));
param = spa_latency_build(&b, id, &this->latency[direction]);
break;
default:
return 0;
@ -700,16 +694,13 @@ static int port_set_latency(void *object,
spa_log_debug(this->log, NAME " %p: set latency", this);
if (latency == NULL) {
this->latency_quantum[other] = 0;
this->latency_min[other] = 0;
this->latency_max[other] = 0;
this->latency[other] = SPA_LATENCY_INFO(other);
} else {
if (spa_pod_parse_object(latency,
SPA_TYPE_OBJECT_ParamLatency, NULL,
SPA_PARAM_LATENCY_quantum,SPA_POD_Float(&this->latency_quantum[other]),
SPA_PARAM_LATENCY_min, SPA_POD_Int(&this->latency_min[other]),
SPA_PARAM_LATENCY_max, SPA_POD_Int(&this->latency_max[other])) < 0)
struct spa_latency_info info;
if (spa_latency_parse(latency, &info) < 0 ||
info.direction != other)
return -EINVAL;
this->latency[other] = info;
}
if (direction == SPA_DIRECTION_INPUT) {
for (i = 0; i < this->port_count; i++) {
@ -1137,6 +1128,9 @@ impl_init(const struct spa_handle_factory *factory,
spa_hook_list_init(&this->hooks);
this->latency[SPA_DIRECTION_INPUT] = SPA_LATENCY_INFO(SPA_DIRECTION_INPUT);
this->latency[SPA_DIRECTION_OUTPUT] = SPA_LATENCY_INFO(SPA_DIRECTION_OUTPUT);
this->node.iface = SPA_INTERFACE_INIT(
SPA_TYPE_INTERFACE_Node,
SPA_VERSION_NODE,