PipeWire: latency-utils.h Source File

PipeWire 0.3.66

Loading...
Searching...
No Matches
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/latency.h>
42
43static inline int
44 spa_latency_info_compare(const struct spa_latency_info *a, struct spa_latency_info *b)
45{
46 if (a->min_quantum == b->min_quantum &&
47 a->max_quantum == b->max_quantum &&
48 a->min_rate == b->min_rate &&
49 a->max_rate == b->max_rate &&
50 a->min_ns == b->min_ns &&
51 a->max_ns == b->max_ns)
52 return 0;
53 return 1;
54}
55
56static inline void
57 spa_latency_info_combine_start(struct spa_latency_info *info, enum spa_direction direction)
58{
59 *info = SPA_LATENCY_INFO(direction,
60 .min_quantum = FLT_MAX,
61 .max_quantum = 0.0f,
62 .min_rate = UINT32_MAX,
63 .max_rate = 0,
64 .min_ns = UINT64_MAX,
65 .max_ns = 0);
66}
67static inline void
68 spa_latency_info_combine_finish(struct spa_latency_info *info)
69{
70 if (info->min_quantum == FLT_MAX)
71 info->min_quantum = 0;
72 if (info->min_rate == UINT32_MAX)
73 info->min_rate = 0;
74 if (info->min_ns == UINT64_MAX)
75 info->min_ns = 0;
76}
77
78static inline int
79 spa_latency_info_combine(struct spa_latency_info *info, const struct spa_latency_info *other)
80{
81 if (info->direction != other->direction)
82 return -EINVAL;
83 if (other->min_quantum < info->min_quantum)
84 info->min_quantum = other->min_quantum;
85 if (other->max_quantum > info->max_quantum)
86 info->max_quantum = other->max_quantum;
87 if (other->min_rate < info->min_rate)
88 info->min_rate = other->min_rate;
89 if (other->max_rate > info->max_rate)
90 info->max_rate = other->max_rate;
91 if (other->min_ns < info->min_ns)
92 info->min_ns = other->min_ns;
93 if (other->max_ns > info->max_ns)
94 info->max_ns = other->max_ns;
95 return 0;
96}
97
98static inline int
99 spa_latency_parse(const struct spa_pod *latency, struct spa_latency_info *info)
100{
101 int res;
102 spa_zero(*info);
103 if ((res = spa_pod_parse_object(latency,
104 SPA_TYPE_OBJECT_ParamLatency, NULL,
105 SPA_PARAM_LATENCY_direction, SPA_POD_Id(&info->direction),
106 SPA_PARAM_LATENCY_minQuantum, SPA_POD_OPT_Float(&info->min_quantum),
107 SPA_PARAM_LATENCY_maxQuantum, SPA_POD_OPT_Float(&info->max_quantum),
108 SPA_PARAM_LATENCY_minRate, SPA_POD_OPT_Int(&info->min_rate),
109 SPA_PARAM_LATENCY_maxRate, SPA_POD_OPT_Int(&info->max_rate),
110 SPA_PARAM_LATENCY_minNs, SPA_POD_OPT_Long(&info->min_ns),
111 SPA_PARAM_LATENCY_maxNs, SPA_POD_OPT_Long(&info->max_ns))) < 0)
112 return res;
113 info->direction = (enum spa_direction)(info->direction & 1);
114 return 0;
115}
116
117static inline struct spa_pod *
118 spa_latency_build(struct spa_pod_builder *builder, uint32_t id, const struct spa_latency_info *info)
119{
120 return (struct spa_pod *)spa_pod_builder_add_object(builder,
121 SPA_TYPE_OBJECT_ParamLatency, id,
122 SPA_PARAM_LATENCY_direction, SPA_POD_Id(info->direction),
123 SPA_PARAM_LATENCY_minQuantum, SPA_POD_Float(info->min_quantum),
124 SPA_PARAM_LATENCY_maxQuantum, SPA_POD_Float(info->max_quantum),
125 SPA_PARAM_LATENCY_minRate, SPA_POD_Int(info->min_rate),
126 SPA_PARAM_LATENCY_maxRate, SPA_POD_Int(info->max_rate),
127 SPA_PARAM_LATENCY_minNs, SPA_POD_Long(info->min_ns),
128 SPA_PARAM_LATENCY_maxNs, SPA_POD_Long(info->max_ns));
129}
130
131static inline int
132 spa_process_latency_parse(const struct spa_pod *latency, struct spa_process_latency_info *info)
133{
134 int res;
135 spa_zero(*info);
136 if ((res = spa_pod_parse_object(latency,
137 SPA_TYPE_OBJECT_ParamProcessLatency, NULL,
138 SPA_PARAM_PROCESS_LATENCY_quantum, SPA_POD_OPT_Float(&info->quantum),
139 SPA_PARAM_PROCESS_LATENCY_rate, SPA_POD_OPT_Int(&info->rate),
140 SPA_PARAM_PROCESS_LATENCY_ns, SPA_POD_OPT_Long(&info->ns))) < 0)
141 return res;
142 return 0;
143}
144
145static inline struct spa_pod *
146 spa_process_latency_build(struct spa_pod_builder *builder, uint32_t id,
147 const struct spa_process_latency_info *info)
148{
149 return (struct spa_pod *)spa_pod_builder_add_object(builder,
150 SPA_TYPE_OBJECT_ParamProcessLatency, id,
151 SPA_PARAM_PROCESS_LATENCY_quantum, SPA_POD_Float(info->quantum),
152 SPA_PARAM_PROCESS_LATENCY_rate, SPA_POD_Int(info->rate),
153 SPA_PARAM_PROCESS_LATENCY_ns, SPA_POD_Long(info->ns));
154}
155
156static inline int
157 spa_process_latency_info_add(const struct spa_process_latency_info *process,
158 struct spa_latency_info *info)
159{
160 info->min_quantum += process->quantum;
161 info->max_quantum += process->quantum;
162 info->min_rate += process->rate;
163 info->max_rate += process->rate;
164 info->min_ns += process->ns;
165 info->max_ns += process->ns;
166 return 0;
167}
168
173#ifdef __cplusplus
174} /* extern "C" */
175#endif
176
177#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:49
spa_latency_parse
static int spa_latency_parse(const struct spa_pod *latency, struct spa_latency_info *info)
Definition: latency-utils.h:104
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:151
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:84
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:123
SPA_LATENCY_INFO
#define SPA_LATENCY_INFO(dir,...)
Definition: latency.h:68
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:162
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:137
spa_latency_info_combine_finish
static void spa_latency_info_combine_finish(struct spa_latency_info *info)
Definition: latency-utils.h:73
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:62
SPA_PARAM_PROCESS_LATENCY_rate
@ SPA_PARAM_PROCESS_LATENCY_rate
latency (Int) relative to rate
Definition: latency.h:74
SPA_PARAM_PROCESS_LATENCY_ns
@ SPA_PARAM_PROCESS_LATENCY_ns
latency (Long) in nanoseconds
Definition: latency.h:75
SPA_PARAM_PROCESS_LATENCY_quantum
@ SPA_PARAM_PROCESS_LATENCY_quantum
latency relative to quantum (Float)
Definition: latency.h:73
SPA_PARAM_LATENCY_maxNs
@ SPA_PARAM_LATENCY_maxNs
max latency (Long) in nanoseconds
Definition: latency.h:53
SPA_PARAM_LATENCY_minRate
@ SPA_PARAM_LATENCY_minRate
min latency (Int) relative to rate
Definition: latency.h:50
SPA_PARAM_LATENCY_minQuantum
@ SPA_PARAM_LATENCY_minQuantum
min latency relative to quantum (Float)
Definition: latency.h:48
SPA_PARAM_LATENCY_maxRate
@ SPA_PARAM_LATENCY_maxRate
max latency (Int) relative to rate
Definition: latency.h:51
SPA_PARAM_LATENCY_maxQuantum
@ SPA_PARAM_LATENCY_maxQuantum
max latency relative to quantum (Float)
Definition: latency.h:49
SPA_PARAM_LATENCY_direction
@ SPA_PARAM_LATENCY_direction
direction, input/output (Id enum spa_direction)
Definition: latency.h:47
SPA_PARAM_LATENCY_minNs
@ SPA_PARAM_LATENCY_minNs
min latency (Long) in nanoseconds
Definition: latency.h:52
SPA_POD_OPT_Float
#define SPA_POD_OPT_Float(val)
Definition: parser.h:546
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:542
spa_pod_builder_add_object
#define spa_pod_builder_add_object(b, type, id,...)
Definition: builder.h:679
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:596
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:544
SPA_TYPE_OBJECT_ParamLatency
@ SPA_TYPE_OBJECT_ParamLatency
Definition: spa/include/spa/utils/type.h:104
SPA_TYPE_OBJECT_ParamProcessLatency
@ SPA_TYPE_OBJECT_ParamProcessLatency
Definition: spa/include/spa/utils/type.h:105
spa_zero
#define spa_zero(x)
Definition: defs.h:433
spa_direction
spa_direction
Definition: defs.h:108
latency.h
spa/param/latency.h
parser.h
spa/pod/parser.h
spa_latency_info
helper structure for managing latency objects
Definition: latency.h:57
spa_latency_info::min_rate
uint32_t min_rate
Definition: latency.h:61
spa_latency_info::max_rate
uint32_t max_rate
Definition: latency.h:62
spa_latency_info::direction
enum spa_direction direction
Definition: latency.h:58
spa_latency_info::max_quantum
float max_quantum
Definition: latency.h:60
spa_latency_info::min_quantum
float min_quantum
Definition: latency.h:59
spa_latency_info::min_ns
uint64_t min_ns
Definition: latency.h:63
spa_latency_info::max_ns
uint64_t max_ns
Definition: latency.h:64
spa_pod_builder
Definition: builder.h:73
spa_pod
Definition: pod/pod.h:63
spa_process_latency_info
Helper structure for managing process latency objects.
Definition: latency.h:79
spa_process_latency_info::quantum
float quantum
Definition: latency.h:80
spa_process_latency_info::rate
uint32_t rate
Definition: latency.h:81
spa_process_latency_info::ns
uint64_t ns
Definition: latency.h:82