PipeWire: spa/param/latency-utils.h Source File

PipeWire 0.3.51

latency-utils.h
Go to the documentation of this file.
1/* Simple Plugin API
2 *
3 * Copyright © 2021 Wim Taymans
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#ifndef SPA_PARAM_LATENCY_UTILS_H
26#define SPA_PARAM_LATENCY_UTILS_H
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
37#include <float.h>
38
39#include <spa/pod/builder.h>
40#include <spa/pod/parser.h>
41#include <spa/param/param.h>
42
43struct spa_latency_info {
44 enum spa_direction direction;
45 float min_quantum;
46 float max_quantum;
47 uint32_t min_rate;
48 uint32_t max_rate;
49 uint64_t min_ns;
50 uint64_t max_ns;
51};
52
53#define SPA_LATENCY_INFO(dir,...) (struct spa_latency_info) { .direction = (dir), ## __VA_ARGS__ }
54
55 static inline int
56 spa_latency_info_compare(const struct spa_latency_info *a, struct spa_latency_info *b)
57{
58 if (a->min_quantum == b->min_quantum &&
59 a->max_quantum == b->max_quantum &&
60 a->min_rate == b->min_rate &&
61 a->max_rate == b->max_rate &&
62 a->min_ns == b->min_ns &&
63 a->max_ns == b->max_ns)
64 return 0;
65 return 1;
66}
67
68static inline void
69 spa_latency_info_combine_start(struct spa_latency_info *info, enum spa_direction direction)
70{
71 *info = SPA_LATENCY_INFO(direction,
72 .min_quantum = FLT_MAX,
73 .max_quantum = 0.0f,
74 .min_rate = UINT32_MAX,
75 .max_rate = 0,
76 .min_ns = UINT64_MAX,
77 .max_ns = 0);
78}
79static inline void
80 spa_latency_info_combine_finish(struct spa_latency_info *info)
81{
82 if (info->min_quantum == FLT_MAX)
83 info->min_quantum = 0;
84 if (info->min_rate == UINT32_MAX)
85 info->min_rate = 0;
86 if (info->min_ns == UINT64_MAX)
87 info->min_ns = 0;
88}
89
90static inline int
91 spa_latency_info_combine(struct spa_latency_info *info, const struct spa_latency_info *other)
92{
93 if (info->direction != other->direction)
94 return -EINVAL;
95 if (other->min_quantum < info->min_quantum)
96 info->min_quantum = other->min_quantum;
97 if (other->max_quantum > info->max_quantum)
98 info->max_quantum = other->max_quantum;
99 if (other->min_rate < info->min_rate)
100 info->min_rate = other->min_rate;
101 if (other->max_rate > info->max_rate)
102 info->max_rate = other->max_rate;
103 if (other->min_ns < info->min_ns)
104 info->min_ns = other->min_ns;
105 if (other->max_ns > info->max_ns)
106 info->max_ns = other->max_ns;
107 return 0;
108}
109
110static inline int
111 spa_latency_parse(const struct spa_pod *latency, struct spa_latency_info *info)
112{
113 int res;
114 spa_zero(*info);
115 if ((res = spa_pod_parse_object(latency,
116 SPA_TYPE_OBJECT_ParamLatency, NULL,
117 SPA_PARAM_LATENCY_direction, SPA_POD_Id(&info->direction),
118 SPA_PARAM_LATENCY_minQuantum, SPA_POD_OPT_Float(&info->min_quantum),
119 SPA_PARAM_LATENCY_maxQuantum, SPA_POD_OPT_Float(&info->max_quantum),
120 SPA_PARAM_LATENCY_minRate, SPA_POD_OPT_Int(&info->min_rate),
121 SPA_PARAM_LATENCY_maxRate, SPA_POD_OPT_Int(&info->max_rate),
122 SPA_PARAM_LATENCY_minNs, SPA_POD_OPT_Long(&info->min_ns),
123 SPA_PARAM_LATENCY_maxNs, SPA_POD_OPT_Long(&info->max_ns))) < 0)
124 return res;
125 info->direction = (enum spa_direction)(info->direction & 1);
126 return 0;
127}
128
129static inline struct spa_pod *
130 spa_latency_build(struct spa_pod_builder *builder, uint32_t id, const struct spa_latency_info *info)
131{
132 return (struct spa_pod *)spa_pod_builder_add_object(builder,
133 SPA_TYPE_OBJECT_ParamLatency, id,
134 SPA_PARAM_LATENCY_direction, SPA_POD_Id(info->direction),
135 SPA_PARAM_LATENCY_minQuantum, SPA_POD_Float(info->min_quantum),
136 SPA_PARAM_LATENCY_maxQuantum, SPA_POD_Float(info->max_quantum),
137 SPA_PARAM_LATENCY_minRate, SPA_POD_Int(info->min_rate),
138 SPA_PARAM_LATENCY_maxRate, SPA_POD_Int(info->max_rate),
139 SPA_PARAM_LATENCY_minNs, SPA_POD_Long(info->min_ns),
140 SPA_PARAM_LATENCY_maxNs, SPA_POD_Long(info->max_ns));
141}
142
143struct spa_process_latency_info {
144 float quantum;
145 uint32_t rate;
146 uint64_t ns;
147};
148
149#define SPA_PROCESS_LATENCY_INFO_INIT(...) (struct spa_process_latency_info) { __VA_ARGS__ }
150
151 static inline int
152 spa_process_latency_parse(const struct spa_pod *latency, struct spa_process_latency_info *info)
153{
154 int res;
155 spa_zero(*info);
156 if ((res = spa_pod_parse_object(latency,
157 SPA_TYPE_OBJECT_ParamProcessLatency, NULL,
158 SPA_PARAM_PROCESS_LATENCY_quantum, SPA_POD_OPT_Float(&info->quantum),
159 SPA_PARAM_PROCESS_LATENCY_rate, SPA_POD_OPT_Int(&info->rate),
160 SPA_PARAM_PROCESS_LATENCY_ns, SPA_POD_OPT_Long(&info->ns))) < 0)
161 return res;
162 return 0;
163}
164
165static inline struct spa_pod *
166 spa_process_latency_build(struct spa_pod_builder *builder, uint32_t id,
167 const struct spa_process_latency_info *info)
168{
169 return (struct spa_pod *)spa_pod_builder_add_object(builder,
170 SPA_TYPE_OBJECT_ParamProcessLatency, id,
171 SPA_PARAM_PROCESS_LATENCY_quantum, SPA_POD_Float(info->quantum),
172 SPA_PARAM_PROCESS_LATENCY_rate, SPA_POD_Int(info->rate),
173 SPA_PARAM_PROCESS_LATENCY_ns, SPA_POD_Long(info->ns));
174}
175
176static inline int
177 spa_process_latency_info_add(const struct spa_process_latency_info *process,
178 struct spa_latency_info *info)
179{
180 info->min_quantum += process->quantum;
181 info->max_quantum += process->quantum;
182 info->min_rate += process->rate;
183 info->max_rate += process->rate;
184 info->min_ns += process->ns;
185 info->max_ns += process->ns;
186 return 0;
187}
188
193#ifdef __cplusplus
194} /* extern "C" */
195#endif
196
197#endif /* SPA_PARAM_LATENCY_UTILS_H */
builder.h
spa/pod/builder.h
spa_latency_info_compare
static int spa_latency_info_compare(const struct spa_latency_info *a, struct spa_latency_info *b)
Definition: latency-utils.h:62
spa_latency_parse
static int spa_latency_parse(const struct spa_pod *latency, struct spa_latency_info *info)
Definition: latency-utils.h:117
spa_process_latency_build
static struct spa_pod * spa_process_latency_build(struct spa_pod_builder *builder, uint32_t id, const struct spa_process_latency_info *info)
Definition: latency-utils.h:173
spa_latency_info_combine
static int spa_latency_info_combine(struct spa_latency_info *info, const struct spa_latency_info *other)
Definition: latency-utils.h:97
spa_latency_build
static struct spa_pod * spa_latency_build(struct spa_pod_builder *builder, uint32_t id, const struct spa_latency_info *info)
Definition: latency-utils.h:136
SPA_LATENCY_INFO
#define SPA_LATENCY_INFO(dir,...)
Definition: latency-utils.h:59
spa_process_latency_info_add
static int spa_process_latency_info_add(const struct spa_process_latency_info *process, struct spa_latency_info *info)
Definition: latency-utils.h:184
spa_process_latency_parse
static int spa_process_latency_parse(const struct spa_pod *latency, struct spa_process_latency_info *info)
Definition: latency-utils.h:159
spa_latency_info_combine_finish
static void spa_latency_info_combine_finish(struct spa_latency_info *info)
Definition: latency-utils.h:86
spa_latency_info_combine_start
static void spa_latency_info_combine_start(struct spa_latency_info *info, enum spa_direction direction)
Definition: latency-utils.h:75
SPA_PARAM_PROCESS_LATENCY_rate
@ SPA_PARAM_PROCESS_LATENCY_rate
latency (Int) relative to rate
Definition: param.h:204
SPA_PARAM_PROCESS_LATENCY_ns
@ SPA_PARAM_PROCESS_LATENCY_ns
latency (Long) in nanoseconds
Definition: param.h:205
SPA_PARAM_PROCESS_LATENCY_quantum
@ SPA_PARAM_PROCESS_LATENCY_quantum
latency relative to quantum (Float)
Definition: param.h:203
SPA_PARAM_LATENCY_maxNs
@ SPA_PARAM_LATENCY_maxNs
max latency (Long) in nanoseconds
Definition: param.h:197
SPA_PARAM_LATENCY_minRate
@ SPA_PARAM_LATENCY_minRate
min latency (Int) relative to rate
Definition: param.h:194
SPA_PARAM_LATENCY_minQuantum
@ SPA_PARAM_LATENCY_minQuantum
min latency relative to quantum (Float)
Definition: param.h:192
SPA_PARAM_LATENCY_maxRate
@ SPA_PARAM_LATENCY_maxRate
max latency (Int) relative to rate
Definition: param.h:195
SPA_PARAM_LATENCY_maxQuantum
@ SPA_PARAM_LATENCY_maxQuantum
max latency relative to quantum (Float)
Definition: param.h:193
SPA_PARAM_LATENCY_direction
@ SPA_PARAM_LATENCY_direction
direction, input/output (Id enum spa_direction)
Definition: param.h:191
SPA_PARAM_LATENCY_minNs
@ SPA_PARAM_LATENCY_minNs
min latency (Long) in nanoseconds
Definition: param.h:196
SPA_POD_OPT_Float
#define SPA_POD_OPT_Float(val)
Definition: parser.h:535
SPA_POD_Long
#define SPA_POD_Long(val)
Definition: vararg.h:85
SPA_POD_Id
#define SPA_POD_Id(val)
Definition: vararg.h:69
SPA_POD_OPT_Int
#define SPA_POD_OPT_Int(val)
Definition: parser.h:531
spa_pod_builder_add_object
#define spa_pod_builder_add_object(b, type, id,...)
Definition: builder.h:677
SPA_POD_Float
#define SPA_POD_Float(val)
Definition: vararg.h:96
spa_pod_parse_object
#define spa_pod_parse_object(pod, type, id,...)
Definition: parser.h:585
SPA_POD_Int
#define SPA_POD_Int(val)
Definition: vararg.h:74
SPA_POD_OPT_Long
#define SPA_POD_OPT_Long(val)
Definition: parser.h:533
SPA_TYPE_OBJECT_ParamLatency
@ SPA_TYPE_OBJECT_ParamLatency
Definition: type.h:104
SPA_TYPE_OBJECT_ParamProcessLatency
@ SPA_TYPE_OBJECT_ParamProcessLatency
Definition: type.h:105
spa_zero
#define spa_zero(x)
Definition: defs.h:385
spa_direction
spa_direction
Definition: defs.h:90
param.h
spa/param/param.h
parser.h
spa/pod/parser.h
spa_latency_info
Definition: latency-utils.h:48
spa_latency_info::min_rate
uint32_t min_rate
Definition: latency-utils.h:52
spa_latency_info::max_rate
uint32_t max_rate
Definition: latency-utils.h:53
spa_latency_info::direction
enum spa_direction direction
Definition: latency-utils.h:49
spa_latency_info::max_quantum
float max_quantum
Definition: latency-utils.h:51
spa_latency_info::min_quantum
float min_quantum
Definition: latency-utils.h:50
spa_latency_info::min_ns
uint64_t min_ns
Definition: latency-utils.h:54
spa_latency_info::max_ns
uint64_t max_ns
Definition: latency-utils.h:55
spa_pod_builder
Definition: builder.h:73
spa_pod
Definition: pod.h:63
spa_process_latency_info
Definition: latency-utils.h:149
spa_process_latency_info::quantum
float quantum
Definition: latency-utils.h:150
spa_process_latency_info::rate
uint32_t rate
Definition: latency-utils.h:151
spa_process_latency_info::ns
uint64_t ns
Definition: latency-utils.h:152