PipeWire  0.3.59
utils.h
Go to the documentation of this file.
1 /* Simple Plugin API
2  *
3  * Copyright © 2019 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_NODE_UTILS_H
26 #define SPA_NODE_UTILS_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
37 #include <spa/pod/builder.h>
38 
39 #include <spa/node/node.h>
40 
42  struct spa_pod_builder *builder;
44 };
45 
46 static inline void spa_result_func_node_params(void *data,
47  int seq, int res, uint32_t type, const void *result)
48 {
49  struct spa_result_node_params_data *d =
51  const struct spa_result_node_params *r =
52  (const struct spa_result_node_params *) result;
53  uint32_t offset = d->builder->state.offset;
55  return;
56  d->data.next = r->next;
57  d->data.param = spa_pod_builder_deref(d->builder, offset);
58 }
59 
60 static inline int spa_node_enum_params_sync(struct spa_node *node,
61  uint32_t id, uint32_t *index,
62  const struct spa_pod *filter,
63  struct spa_pod **param,
64  struct spa_pod_builder *builder)
65 {
67  struct spa_hook listener = {{0}};
68  static const struct spa_node_events node_events = {
70  .info = NULL,
71  .port_info = NULL,
73  };
74  int res;
75 
76  res = spa_node_add_listener(node, &listener, &node_events, &data);
77  if (res >= 0) {
78  res = spa_node_enum_params(node, 0, id, *index, 1, filter);
79  spa_hook_remove(&listener);
80  }
81 
82  if (data.data.param == NULL) {
83  if (res > 0)
84  res = 0;
85  } else {
86  *index = data.data.next;
87  *param = data.data.param;
88  res = 1;
89  }
90  return res;
91 }
92 
93 static inline int spa_node_port_enum_params_sync(struct spa_node *node,
94  enum spa_direction direction, uint32_t port_id,
95  uint32_t id, uint32_t *index,
96  const struct spa_pod *filter,
97  struct spa_pod **param,
98  struct spa_pod_builder *builder)
99 {
101  struct spa_hook listener = {{0}};
102  static const struct spa_node_events node_events = {
104  .info = NULL,
105  .port_info = NULL,
106  .result = spa_result_func_node_params,
107  };
108  int res;
109 
110  res = spa_node_add_listener(node, &listener, &node_events, &data);
111  if (res >= 0) {
112  res = spa_node_port_enum_params(node, 0, direction, port_id,
113  id, *index, 1, filter);
114  spa_hook_remove(&listener);
115  }
116 
117  if (data.data.param == NULL) {
118  if (res > 0)
119  res = 0;
120  } else {
121  *index = data.data.next;
122  *param = data.data.param;
123  res = 1;
124  }
125  return res;
126 }
127 
128 #define spa_node_emit(hooks,method,version,...) \
129  spa_hook_list_call_simple(hooks, struct spa_node_events, \
130  method, version, ##__VA_ARGS__)
131 
132 #define spa_node_emit_info(hooks,...) spa_node_emit(hooks,info, 0, __VA_ARGS__)
133 #define spa_node_emit_port_info(hooks,...) spa_node_emit(hooks,port_info, 0, __VA_ARGS__)
134 #define spa_node_emit_result(hooks,...) spa_node_emit(hooks,result, 0, __VA_ARGS__)
135 #define spa_node_emit_event(hooks,...) spa_node_emit(hooks,event, 0, __VA_ARGS__)
136 
137 
138 #define spa_node_call(callbacks,method,version,...) \
139 ({ \
140  int _res = -ENOTSUP; \
141  spa_callbacks_call_res(callbacks, struct spa_node_callbacks, \
142  _res, method, version, ##__VA_ARGS__); \
143  _res; \
144 })
145 
146 #define spa_node_call_ready(hook,...) spa_node_call(hook, ready, 0, __VA_ARGS__)
147 #define spa_node_call_reuse_buffer(hook,...) spa_node_call(hook, reuse_buffer, 0, __VA_ARGS__)
148 #define spa_node_call_xrun(hook,...) spa_node_call(hook, xrun, 0, __VA_ARGS__)
149 
154 #ifdef __cplusplus
155 } /* extern "C" */
156 #endif
157 
158 #endif /* SPA_NODE_UTILS_H */
spa/pod/builder.h
static void spa_hook_remove(struct spa_hook *hook)
Remove a hook.
Definition: hook.h:392
static void spa_result_func_node_params(void *data, int seq, int res, uint32_t type, const void *result)
Definition: utils.h:51
static int spa_node_enum_params_sync(struct spa_node *node, uint32_t id, uint32_t *index, const struct spa_pod *filter, struct spa_pod **param, struct spa_pod_builder *builder)
Definition: utils.h:65
#define spa_node_add_listener(n,...)
Adds an event listener on node.
Definition: node.h:721
#define spa_node_port_enum_params(n,...)
Enumerate all possible parameters of id on port_id of node that are compatible with filter.
Definition: node.h:748
static int spa_node_port_enum_params_sync(struct spa_node *node, enum spa_direction direction, uint32_t port_id, uint32_t id, uint32_t *index, const struct spa_pod *filter, struct spa_pod **param, struct spa_pod_builder *builder)
Definition: utils.h:98
#define spa_node_enum_params(n,...)
Enumerate the parameters of a node.
Definition: node.h:730
#define SPA_VERSION_NODE_EVENTS
Definition: node.h:198
static int spa_pod_builder_raw_padded(struct spa_pod_builder *builder, const void *data, uint32_t size)
Definition: builder.h:180
#define SPA_POD_SIZE(pod)
Definition: pod.h:50
static struct spa_pod * spa_pod_builder_deref(struct spa_pod_builder *builder, uint32_t offset)
Definition: builder.h:113
spa_direction
Definition: defs.h:108
spa/node/node.h
A hook, contains the structure with functions and the data passed to the functions.
Definition: hook.h:351
events from the spa_node.
Definition: node.h:196
uint32_t version
version of this structure
Definition: node.h:199
Definition: node.h:61
uint32_t offset
Definition: builder.h:53
Definition: builder.h:73
struct spa_pod_builder_state state
Definition: builder.h:77
Definition: pod.h:63
Definition: utils.h:46
struct spa_result_node_params data
Definition: utils.h:48
struct spa_pod_builder * builder
Definition: utils.h:47
the result of enum_params or port_enum_params.
Definition: node.h:172
struct spa_pod * param
the result param
Definition: node.h:176
uint32_t next
next index of iteration
Definition: node.h:175
uint32_t index
index of parameter
Definition: node.h:174