Debian Codegraph @

modules/lsp-audio-pipewire-lib/include/lsp-plug.in/audio/pipewire/backend.h

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-audio-pipewire-lib
6 * Created on: 6 апр. 2026 г.
7 *
8 * lsp-audio-pipewire-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-audio-pipewire-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-audio-pipewire-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef LSP_PLUG_IN_AUDIO_PIPEWIRE_BACKEND_H_
23 #define LSP_PLUG_IN_AUDIO_PIPEWIRE_BACKEND_H_
24
25 #include <lsp-plug.in/audio/pipewire/version.h>
26
27 #include <lsp-plug.in/audio/iface/backend.h>
28 #include <lsp-plug.in/audio/pipewire/dictionary.h>
29 #include <lsp-plug.in/audio/pipewire/mutex.h>
30 #include <lsp-plug.in/audio/pipewire/registry.h>
31 #include <lsp-plug.in/audio/pipewire/ringbuffer.h>
32
33 #include <pw-headers/pipewire/pipewire.h>
34 #include <pw-headers/pipewire/extensions/client-node.h>
35 #include <pw-headers/pipewire/extensions/metadata.h>
36 #include <pw-headers/spa/param/latency.h>
37 #include <pw-headers/spa/utils/ringbuffer.h>
38
39 namespace lsp
40 {
41 namespace audio
42 {
43 namespace pipewire
44 {
45 /**
46 * Trivially-movable backend
47 */
48 typedef struct LSP_HIDDEN_MODIFIER backend_t: public audio::backend_t
49 {
50 protected:
51 static constexpr size_t MAX_PORT_ID_BYTES = 16;
52
53 enum spa_hook_type_t
54 {
55 HOOK_CORE,
56 HOOK_REGISTRY,
57 HOOK_FILTER,
58 HOOK_METADATA_PROXY,
59 HOOK_METADATA,
60
61 HOOK_TOTAL
62 };
63
64 struct port_data_t;
65
66 typedef struct port_t
67 {
68 uint32_t nType;
69 uint32_t nId;
70 port_data_t *pHandle;
71 pw_buffer *pBuffer;
72 char *sFullId;
73 char sID[MAX_PORT_ID_BYTES];
74 } port_t;
75
76 typedef struct port_data_t
77 {
78 port_id_t nPortId;
79 } port_data_t;
80
81 typedef struct control_header_t
82 {
83 uint32_t timestamp;
84 uint32_t type;
85 } control_header_t;
86
87 protected:
88 static const pw_registry_events registry_events;
89 static const pw_core_events core_events;
90 static const pw_filter_events filter_events;
91 static const spa_thread_utils_methods thread_utils_impl;
92 static const pw_proxy_events metadata_proxy_events;
93 static const pw_metadata_events metadata_events;
94 static const pw_proxy_events link_proxy_events;
95
96 public:
97 char *sClientName;
98 char *sServerName;
99 mutex_t *pMutex;
100 pw_data_loop *pAudioDataLoop;
101 pw_thread_loop *pContextThreadLoop;
102 pw_loop *pAudioLoop;
103 pw_loop *pContextLoop;
104 pw_context *pContext;
105 pw_core *pCore;
106 pw_mempool *pMemPool;
107 pw_registry *pRegistry;
108 pw_filter *pFilter;
109 pw_metadata *pMetadata;
110 spa_thread_utils *pOldThreadUtils;
111
112 registry sRegistry;
113 ringbuffer sRingBuffer;
114 dictionary sClientDict;
115 dictionary sContextDict;
116 spa_thread_utils sThreadUtils;
117 spa_hook vHooks[HOOK_TOTAL];
118
119 void *pUserData;
120 const callbacks_t *pCallbacks;
121 port_t *vPorts;
122 port_id_t nPortFirst;
123 port_id_t nPortCapacity;
124 io_parameters_t sIOParams;
125 io_position_t sIOPosition;
126 uint32_t nLatency;
127 int nSyncRequestId;
128 int nSyncResponseId;
129 int nSyncError;
130 bool bActivated;
131
132 public:
133 explicit backend_t() noexcept;
134 void construct() noexcept;
135
136 protected:
137 // PipeWire registry events
138 static void on_registry_event_global(
139 void *self, uint32_t id,
140 uint32_t permissions, const char *type, uint32_t version,
141 const spa_dict *props);
142 static void on_registry_event_removed(void *self, uint32_t id);
143
144 protected:
145 // PipeWire core events
146 static void on_core_info(void *self, const struct pw_core_info *info);
147 static void on_core_done(void *self, uint32_t id, int seq);
148 static void on_core_error(void *self, uint32_t id, int seq, int res, const char *message);
149
150 protected:
151 static void on_filter_destroy(void *self);
152 static void on_filter_state_changed(void *self, enum pw_filter_state old, enum pw_filter_state state, const char *error);
153 static void on_filter_io_changed(void *self, void *port_data, uint32_t id, void *area, uint32_t size);
154 static void on_filter_param_changed(void *self, void *port_data, uint32_t id, const struct spa_pod *param);
155 static void on_filter_process(void *self, struct spa_io_position *position);
156 static void on_filter_command(void *self, const struct spa_command *command);
157
158 protected:
159 // PipeWire thread utils methods
160 static spa_thread *on_thread_create(void *self, const spa_dict *props, void *(*start)(void*), void *arg);
161 static int on_thread_join(void *self, struct spa_thread *thread, void **retval);
162 static int on_thread_get_rt_range(void *self, const struct spa_dict *props, int *min, int *max);
163 static int on_thread_acquire_rt(void *self, struct spa_thread *thread, int priority);
164 static int on_thread_drop_rt(void *self, struct spa_thread *thread);
165
166 protected:
167 // PipeWire metadata events
168 static void on_metadata_destroy(void *self);
169 static void on_metadata_removed(void *self);
170 static int on_metadata_property(void *self, uint32_t subject, const char *key, const char *type, const char *value);
171
172 protected:
173 // PipeWire link proxy events
174 static void on_link_error(void *data, int seq, int res, const char *message);
175
176 protected:
177 // PipeWire notification source
178 static void on_notify_event(void *self, uint64_t count);
179
180 protected:
181 // PipeWire ringbuffer events
182 static void on_ringbuffer_data_received(void *self, uint64_t count);
183
184 protected:
185 // PipeWire miscellaneous processing
186 static int execute_context_properties_match(void *self, const char *location, const char *action, const char *val, size_t len);
187
188 protected:
189 int sync_core(bool lock) noexcept;
190 status_t make_connection(
191 const connection_params_t *params,
192 const callbacks_t *callbacks,
193 void *user_data) noexcept;
194 status_t activate() noexcept;
195 status_t deactivate() noexcept;
196 void close_connection() noexcept;
197 status_t register_port(port_t *port) noexcept;
198 status_t unregister_port(port_t *port) noexcept;
199 status_t register_ports() noexcept;
200 void unregister_ports() noexcept;
201 void update_sample_rate(const struct spa_pod *param) noexcept;
202 void notify_connection_lost(bool stop) noexcept;
203 void update_latency(uint32_t latency) noexcept;
204 status_t wait_all_ports_registered() noexcept;
205
206 protected:
207 static void init_port(port_t *port) noexcept;
208 port_t *find_port(const char *id) noexcept;
209 void free_port(port_t *port) noexcept;
210 port_t *alloc_port(const char *id, uint32_t flags) noexcept;
211 char *make_port_full_id(const char *id) const noexcept;
212
213 public:
214 static status_t connect(
215 audio::backend_t *self,
216 const connection_params_t *params,
217 const callbacks_t *callbacks,
218 void *user_data);
219 static status_t set_latency(audio::backend_t *self, uint32_t latency);
220 static status_t disconnect(audio::backend_t *self);
221 static void destroy(audio::backend_t *self);
222
223 static port_id_t register_port(audio::backend_t *self, const char *id, uint32_t flags);
224 static status_t unregister_port(audio::backend_t *self, port_id_t port_id);
225 static const char *port_system_name(audio::backend_t *self, port_id_t port_id);
226
227 static status_t connect_ports(audio::backend_t *self, const char *source, const char *destination);
228 static status_t disconnect_ports(audio::backend_t *self, const char *source, const char *destination);
229
230 static size_t audio_buffers_count(audio::backend_t *self, port_id_t port_id);
231 static float *get_audio_buffer(audio::backend_t *self, port_id_t port_id, size_t index);
232
233 static status_t read_midi_event(audio::backend_t *self, port_id_t port_id, midi_event_t *event, uint32_t *index);
234 static uint8_t *write_midi_event(audio::backend_t *self, port_id_t port_id, uint32_t timestamp, uint32_t size);
235
236 } backend_t;
237 } /* namespace pipewire */
238 } /* namespace audio */
239 } /* namespace lsp */
240
241 #endif /* LSP_PLUG_IN_AUDIO_PIPEWIRE_BACKEND_H_ */