summaryrefslogtreecommitdiffstats
path: root/third_party/pipewire/libpipewire/mozpipewire.cpp
blob: b8068296745e459c6feb68d59b7fcb5e5a7d0255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=4:tabstop=4:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <glib.h>
#include "mozilla/Types.h"
#include "prlink.h"

#include <pipewire/pipewire.h>

#define GET_FUNC(func, lib)                                  \
    func##_fn =                                              \
      (decltype(func##_fn))PR_FindFunctionSymbol(lib, #func) \

#define IS_FUNC_LOADED(func)                                          \
    (func != nullptr)                                                 \

struct GUnixFDList;

extern "C" gint
g_unix_fd_list_get(struct GUnixFDList *list,
                   gint index_,
                   GError **error)
{
  static PRLibrary* gioLib = nullptr;
  static bool gioInitialized = false;
  static gint (*g_unix_fd_list_get_fn)(struct GUnixFDList *list,
               gint index_, GError **error) = nullptr;

  if (!gioInitialized) {
    gioInitialized = true;
    gioLib = PR_LoadLibrary("libgio-2.0.so.0");
    if (!gioLib) {
      return -1;
    }
    GET_FUNC(g_unix_fd_list_get, gioLib);
  }

  if (!g_unix_fd_list_get_fn) {
    return -1;
  }

  return g_unix_fd_list_get_fn(list, index_, error);
}

static struct pw_core * (*pw_context_connect_fn)(struct pw_context *context,
                                              struct pw_properties *properties,
                                              size_t user_data_size);
static struct pw_core * (*pw_context_connect_fd_fn)(struct pw_context *context,
                                                    int fd,
                                                    struct pw_properties *properties,
                                                    size_t user_data_size);
static void (*pw_context_destroy_fn)(struct pw_context *context);
struct pw_context * (*pw_context_new_fn)(struct pw_loop *main_loop,
                                      struct pw_properties *props,
                                      size_t user_data_size);
static int (*pw_core_disconnect_fn)(struct pw_core *core);
static void (*pw_init_fn)(int *argc, char **argv[]);
static void (*pw_proxy_destroy_fn)(struct pw_proxy *proxy);
static void (*pw_stream_add_listener_fn)(struct pw_stream *stream,
                                      struct spa_hook *listener,
                                      const struct pw_stream_events *events,
                                      void *data);
static int (*pw_stream_connect_fn)(struct pw_stream *stream,
                                enum pw_direction direction,
                                uint32_t target_id,
                                enum pw_stream_flags flags,
                                const struct spa_pod **params,
                                uint32_t n_params);
static int (*pw_stream_disconnect_fn)(struct pw_stream *stream);
static struct pw_buffer* (*pw_stream_dequeue_buffer_fn)(struct pw_stream *stream);
static void (*pw_stream_destroy_fn)(struct pw_stream *stream);
static struct pw_stream* (*pw_stream_new_fn)(struct pw_core *core,
                                          const char *name,
                                          struct pw_properties *props);

static int (*pw_stream_queue_buffer_fn)(struct pw_stream *stream,
                                     struct pw_buffer *buffer);
static const char * (*pw_stream_state_as_string_fn)(enum pw_stream_state state);
static int (*pw_stream_update_params_fn)(struct pw_stream *stream,
                                      const struct spa_pod **params,
                                      uint32_t n_params);
static void (*pw_thread_loop_destroy_fn)(struct pw_thread_loop *loop);
static struct pw_loop* (*pw_thread_loop_get_loop_fn)(struct pw_thread_loop *loop);
static struct pw_thread_loop* (*pw_thread_loop_new_fn)(const char *name,
                                                const struct spa_dict *props);
static int (*pw_thread_loop_start_fn)(struct pw_thread_loop *loop);
static void (*pw_thread_loop_stop_fn)(struct pw_thread_loop *loop);
static void (*pw_thread_loop_lock_fn)(struct pw_thread_loop *loop);
static void (*pw_thread_loop_unlock_fn)(struct pw_thread_loop *loop);
static void (*pw_thread_loop_wait_fn)(struct pw_thread_loop *loop);
static void (*pw_thread_loop_signal_fn)(struct pw_thread_loop *loop, bool wait_for_accept);
static struct pw_properties* (*pw_properties_new_string_fn)(const char *str);
static const char* (*pw_get_library_version_fn)();

bool IsPwLibraryLoaded() {
  static bool isLoaded =
         (IS_FUNC_LOADED(pw_context_connect_fn) &&
          IS_FUNC_LOADED(pw_context_connect_fd_fn) &&
          IS_FUNC_LOADED(pw_context_destroy_fn) &&
          IS_FUNC_LOADED(pw_context_new_fn) &&
          IS_FUNC_LOADED(pw_core_disconnect_fn) &&
          IS_FUNC_LOADED(pw_init_fn) &&
          IS_FUNC_LOADED(pw_proxy_destroy_fn) &&
          IS_FUNC_LOADED(pw_stream_add_listener_fn) &&
          IS_FUNC_LOADED(pw_stream_connect_fn) &&
          IS_FUNC_LOADED(pw_stream_disconnect_fn) &&
          IS_FUNC_LOADED(pw_stream_dequeue_buffer_fn) &&
          IS_FUNC_LOADED(pw_stream_destroy_fn) &&
          IS_FUNC_LOADED(pw_stream_new_fn) &&
          IS_FUNC_LOADED(pw_stream_queue_buffer_fn) &&
          IS_FUNC_LOADED(pw_stream_state_as_string_fn) &&
          IS_FUNC_LOADED(pw_stream_update_params_fn) &&
          IS_FUNC_LOADED(pw_thread_loop_destroy_fn) &&
          IS_FUNC_LOADED(pw_thread_loop_get_loop_fn) &&
          IS_FUNC_LOADED(pw_thread_loop_new_fn) &&
          IS_FUNC_LOADED(pw_thread_loop_start_fn) &&
          IS_FUNC_LOADED(pw_thread_loop_stop_fn) &&
          IS_FUNC_LOADED(pw_thread_loop_lock_fn) &&
          IS_FUNC_LOADED(pw_thread_loop_unlock_fn) &&
          IS_FUNC_LOADED(pw_thread_loop_signal_fn) &&
          IS_FUNC_LOADED(pw_thread_loop_wait_fn) &&
          IS_FUNC_LOADED(pw_properties_new_string_fn) &&
          IS_FUNC_LOADED(pw_get_library_version_fn));

  return isLoaded;
}

bool LoadPWLibrary() {
  static PRLibrary* pwLib = nullptr;
  static bool pwInitialized = false;

  //TODO Thread safe
  if (!pwInitialized) {
    pwInitialized = true;
    pwLib = PR_LoadLibrary("libpipewire-0.3.so.0");
    if (!pwLib) {
      return false;
    }

    GET_FUNC(pw_context_connect, pwLib);
    GET_FUNC(pw_context_connect_fd, pwLib);
    GET_FUNC(pw_context_destroy, pwLib);
    GET_FUNC(pw_context_new, pwLib);
    GET_FUNC(pw_core_disconnect, pwLib);
    GET_FUNC(pw_init, pwLib);
    GET_FUNC(pw_stream_add_listener, pwLib);
    GET_FUNC(pw_stream_connect, pwLib);
    GET_FUNC(pw_stream_disconnect, pwLib);
    GET_FUNC(pw_stream_dequeue_buffer, pwLib);
    GET_FUNC(pw_stream_destroy, pwLib);
    GET_FUNC(pw_stream_new, pwLib);
    GET_FUNC(pw_stream_queue_buffer, pwLib);
    GET_FUNC(pw_stream_update_params, pwLib);
    GET_FUNC(pw_stream_state_as_string, pwLib);
    GET_FUNC(pw_thread_loop_destroy, pwLib);
    GET_FUNC(pw_thread_loop_get_loop, pwLib);
    GET_FUNC(pw_thread_loop_new, pwLib);
    GET_FUNC(pw_thread_loop_start, pwLib);
    GET_FUNC(pw_thread_loop_stop, pwLib);
    GET_FUNC(pw_thread_loop_lock, pwLib);
    GET_FUNC(pw_thread_loop_unlock, pwLib);
    GET_FUNC(pw_thread_loop_signal, pwLib);
    GET_FUNC(pw_thread_loop_wait, pwLib);
    GET_FUNC(pw_properties_new_string, pwLib);
    GET_FUNC(pw_get_library_version, pwLib);
    GET_FUNC(pw_proxy_destroy, pwLib);
  }

  return IsPwLibraryLoaded();
}

struct pw_core *
pw_context_connect(struct pw_context *context,
                   struct pw_properties *properties,
                   size_t user_data_size)
{
  if (!LoadPWLibrary()) {
    return nullptr;
  }
  return pw_context_connect_fn(context, properties, user_data_size);
}

struct pw_core *
pw_context_connect_fd(struct pw_context *context,
                      int fd,
                      struct pw_properties *properties,
                      size_t user_data_size)
{
  if (!LoadPWLibrary()) {
    return nullptr;
  }
  return pw_context_connect_fd_fn(context, fd, properties, user_data_size);
}

void
pw_context_destroy(struct pw_context *context)
{
  if (!LoadPWLibrary()) {
    return;
  }
  pw_context_destroy_fn(context);
}

struct pw_context *
pw_context_new(struct pw_loop *main_loop,
               struct pw_properties *props,
               size_t user_data_size)
{
  if (!LoadPWLibrary()) {
    return nullptr;
  }
  return pw_context_new_fn(main_loop, props, user_data_size);
}

int
pw_core_disconnect(struct pw_core *core)
{
  if (!LoadPWLibrary()) {
    return 0;
  }
  return pw_core_disconnect_fn(core);
}

void
pw_init(int *argc, char **argv[])
{
  if (!LoadPWLibrary()) {
    return;
  }
  return pw_init_fn(argc, argv);
}

void
pw_proxy_destroy(struct pw_proxy *proxy)
{
  if (!LoadPWLibrary()) {
    return;
  }
  return pw_proxy_destroy_fn(proxy);
}

void
pw_stream_add_listener(struct pw_stream *stream,
                       struct spa_hook *listener,
                       const struct pw_stream_events *events,
                       void *data)
{
  if (!LoadPWLibrary()) {
    return;
  }
  return pw_stream_add_listener_fn(stream, listener, events, data);
}

int
pw_stream_connect(struct pw_stream *stream,
                  enum pw_direction direction,
                  uint32_t target_id,
                  enum pw_stream_flags flags,
                  const struct spa_pod **params,
                  uint32_t n_params)
{
  if (!LoadPWLibrary()) {
    return 0;
  }
  return pw_stream_connect_fn(stream, direction, target_id, flags,
                              params, n_params);
}

int
pw_stream_disconnect(struct pw_stream *stream)
{
  if (!LoadPWLibrary()) {
    return 0;
  }
  return pw_stream_disconnect_fn(stream);
}

struct pw_buffer *
pw_stream_dequeue_buffer(struct pw_stream *stream)
{
  if (!LoadPWLibrary()) {
    return nullptr;
  }
  return pw_stream_dequeue_buffer_fn(stream);
}

void
pw_stream_destroy(struct pw_stream *stream)
{
  if (!LoadPWLibrary()) {
    return;
  }
  return pw_stream_destroy_fn(stream);
}

struct pw_stream *
pw_stream_new(struct pw_core *core,
              const char *name,
              struct pw_properties *props)
{
  if (!LoadPWLibrary()) {
    return nullptr;
  }
  return pw_stream_new_fn(core, name, props);
}

int
pw_stream_queue_buffer(struct pw_stream *stream,
                       struct pw_buffer *buffer)
{
  if (!LoadPWLibrary()) {
    return 0;
  }
  return pw_stream_queue_buffer_fn(stream, buffer);
}

const char *
pw_stream_state_as_string(enum pw_stream_state state)
{
  if (!LoadPWLibrary()) {
    return nullptr;
  }
  return pw_stream_state_as_string_fn(state);
}

int
pw_stream_update_params(struct pw_stream *stream,
                        const struct spa_pod **params,
                        uint32_t n_params)
{
  if (!LoadPWLibrary()) {
    return 0;
  }
  return pw_stream_update_params_fn(stream, params, n_params);
}

void
pw_thread_loop_destroy(struct pw_thread_loop *loop)
{
  if (!LoadPWLibrary()) {
    return;
  }
  return pw_thread_loop_destroy_fn(loop);
}

struct pw_loop *
pw_thread_loop_get_loop(struct pw_thread_loop *loop)
{
  if (!LoadPWLibrary()) {
    return nullptr;
  }
  return pw_thread_loop_get_loop_fn(loop);
}

struct pw_thread_loop *
pw_thread_loop_new(const char *name,
                   const struct spa_dict *props)
{
  if (!LoadPWLibrary()) {
    return nullptr;
  }
  return pw_thread_loop_new_fn(name, props);
}

int
pw_thread_loop_start(struct pw_thread_loop *loop)
{
  if (!LoadPWLibrary()) {
    return 0;
  }
  return pw_thread_loop_start_fn(loop);
}

void
pw_thread_loop_stop(struct pw_thread_loop *loop)
{
  if (!LoadPWLibrary()) {
    return;
  }
  return pw_thread_loop_stop_fn(loop);
}

void
pw_thread_loop_lock(struct pw_thread_loop *loop)
{
  if (!LoadPWLibrary()) {
    return;
  }
  return pw_thread_loop_lock_fn(loop);
}

void
pw_thread_loop_unlock(struct pw_thread_loop *loop)
{
  if (!LoadPWLibrary()) {
    return;
  }
  return pw_thread_loop_unlock_fn(loop);
}

void
pw_thread_loop_signal(struct pw_thread_loop *loop, bool wait_for_accept)
{
  if (!LoadPWLibrary()) {
    return;
  }
  return pw_thread_loop_signal_fn(loop, wait_for_accept);
}

void
pw_thread_loop_wait(struct pw_thread_loop *loop)
{
  if (!LoadPWLibrary()) {
    return;
  }
  return pw_thread_loop_wait_fn(loop);
}

struct pw_properties *
pw_properties_new_string(const char *str)
{
  if (!LoadPWLibrary()) {
    return nullptr;
  }
  return pw_properties_new_string_fn(str);
}

const char*
pw_get_library_version()
{
  if (!LoadPWLibrary()) {
    return nullptr;
  }
  return pw_get_library_version_fn();
}