endpoint.c 30.9 KB
Newer Older
George Kiagiadakis's avatar
George Kiagiadakis committed
1
2
3
4
5
/* WirePlumber
 *
 * Copyright © 2019 Collabora Ltd.
 *    @author George Kiagiadakis <george.kiagiadakis@collabora.com>
 *
George Kiagiadakis's avatar
George Kiagiadakis committed
6
 * SPDX-License-Identifier: MIT
George Kiagiadakis's avatar
George Kiagiadakis committed
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
/**
 * SECTION: Endpoint
 *
 * An endpoint is an abstraction layer that represents a physical place where
 * audio can be routed to/from.
 *
 * Examples of endpoints on a desktop-like system:
 * * Laptop speakers
 * * Laptop webcam
 * * USB microphone
 * * Docking station stereo jack port
 * * USB 5.1 Digital audio output
 *
 * Examples of endpoints on a car:
 * * Driver seat speakers
 * * Front right seat microphone array
 * * Rear left seat headphones
 * * Bluetooth phone gateway
 * * All speakers
 *
 * In ALSA terms, an endpoint may be representing an ALSA subdevice 1-to-1
 * (therefore a single alsa-source/alsa-sink node in pipewire),
 * but it may as well be representing a part of this subdevice (for instance,
 * only the front stereo channels, or only the rear stereo), or it may represent
 * a combination of devices (for instance, playing to all speakers of a system
 * while they are plugged on different sound cards).
 *
 * An endpoint is not necessarily tied to a device that is present on this
 * system using ALSA or V4L. It may also represent a hardware device that
 * can be accessed in some hardware-specific path and is not accessible to
 * applications through pipewire. In this case, the endpoint can only used
 * for controlling the hardware, or - if the appropriate EndpointLink object
 * is also implemented - it can be used to route media from some other
 * hardware endpoint.
 *
 * ## Streams
 *
 * An endpoint can contain multiple streams, which represent different,
 * controllable paths that can be used to reach this endpoint.
 * Streams can be used to implement grouping of applications based on their
 * role or other things.
 *
 * Examples of streams on an audio output endpoint: "multimedia", "radio",
 * "phone". In this example, an audio player would be routed through the
 * "multimedia" stream, for instance, while a voip app would be routed through
 * "phone". This would allow lowering the volume of the audio player while the
 * call is in progress by using the standard volume control of the "multimedia"
 * stream.
 *
 * Examples of streams on an audio capture endpoint: "standard",
 * "voice recognition". In this example, the "standard" capture gives a
 * real-time capture from the microphone, while "voice recognition" gives a
 * slightly delayed and DSP-optimized for speech input, which can be used
 * as input in a voice recognition engine.
 *
 * A stream is described as a dictionary GVariant (a{sv}) with the following
 * standard keys available:
 * "id": the id of the stream
 * "name": the name of the stream
 *
 * ## Controls
 *
 * An endpoint can have multiple controls, which can control anything in the
 * path of media. Typically, audio streams have volume and mute controls, while
 * video streams have hue, brightness, contrast, etc... Controls can be linked
 * to a specific stream, but may as well be global and apply to all streams
 * of the endpoint. This can be used to implement a master volume, for instance.
 *
 * A control is described as a dictionary GVariant (a{sv}) with the following
 * standard keys available:
 * "id": the id of the control
 * "stream-id": the id of the stream that this control applies to
 * "name": the name of the control
 * "type": a GVariant type string
 * "range": a tuple (min, max)
84
 * "default-value": the default value
85
86
 */

87
88
89
#include "endpoint.h"
#include "error.h"
#include "factory.h"
90
#include "private.h"
91

George Kiagiadakis's avatar
George Kiagiadakis committed
92
93
94
95
96
typedef struct _WpEndpointPrivate WpEndpointPrivate;
struct _WpEndpointPrivate
{
  gchar *name;
  gchar media_class[40];
97
  guint direction;
98
  guint64 creation_time;
99
100
  GPtrArray *streams;
  GPtrArray *controls;
George Kiagiadakis's avatar
George Kiagiadakis committed
101
  GPtrArray *links;
102
  GWeakRef core;
George Kiagiadakis's avatar
George Kiagiadakis committed
103
104
105
106
};

enum {
  PROP_0,
107
  PROP_CORE,
George Kiagiadakis's avatar
George Kiagiadakis committed
108
109
  PROP_NAME,
  PROP_MEDIA_CLASS,
110
  PROP_DIRECTION,
111
  PROP_CREATION_TIME,
George Kiagiadakis's avatar
George Kiagiadakis committed
112
113
};

114
115
116
117
118
119
120
enum {
  SIGNAL_NOTIFY_CONTROL_VALUE,
  NUM_SIGNALS
};

static guint32 signals[NUM_SIGNALS];

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
static void wp_endpoint_async_initable_init (gpointer iface,
    gpointer iface_data);

G_DEFINE_ABSTRACT_TYPE_WITH_CODE (WpEndpoint, wp_endpoint, G_TYPE_OBJECT,
  G_ADD_PRIVATE (WpEndpoint)
  G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE,
                         wp_endpoint_async_initable_init))

static void
wp_endpoint_init_async (GAsyncInitable *initable, int io_priority,
    GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data)
{
}

static gboolean
wp_endpoint_init_finish (GAsyncInitable *initable, GAsyncResult *result,
    GError **error)
{
  g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);

  return g_task_propagate_boolean (G_TASK (result), error);
}

static void
wp_endpoint_async_initable_init (gpointer iface, gpointer iface_data)
{
  GAsyncInitableIface *ai_iface = iface;

  ai_iface->init_async = wp_endpoint_init_async;
  ai_iface->init_finish = wp_endpoint_init_finish;
}
George Kiagiadakis's avatar
George Kiagiadakis committed
152
153
154
155
156
157

static void
wp_endpoint_init (WpEndpoint * self)
{
  WpEndpointPrivate *priv = wp_endpoint_get_instance_private (self);

158
159
  g_weak_ref_init (&priv->core, NULL);

160
  priv->creation_time = (guint64) g_get_monotonic_time ();
161
162
163
164
  priv->streams =
      g_ptr_array_new_with_free_func ((GDestroyNotify) g_variant_unref);
  priv->controls =
      g_ptr_array_new_with_free_func ((GDestroyNotify) g_variant_unref);
George Kiagiadakis's avatar
George Kiagiadakis committed
165
166
167
168
169
170
171
172
  priv->links = g_ptr_array_new_with_free_func (g_object_unref);
}

static void
wp_endpoint_dispose (GObject * object)
{
  WpEndpointPrivate *priv =
      wp_endpoint_get_instance_private (WP_ENDPOINT (object));
173
  WpEndpointLink *l = NULL;
George Kiagiadakis's avatar
George Kiagiadakis committed
174
175

  /* wp_endpoint_link_destroy removes elements from the array,
176
177
178
179
   * so traversing in reverse order is er and less complicated */
  while (priv->links->len > 0) {
    l = g_ptr_array_steal_index (priv->links, priv->links->len - 1);
    wp_endpoint_link_destroy(l);
George Kiagiadakis's avatar
George Kiagiadakis committed
180
181
182
183
184
185
186
187
188
189
190
  }

  G_OBJECT_CLASS (wp_endpoint_parent_class)->dispose (object);
}

static void
wp_endpoint_finalize (GObject * object)
{
  WpEndpointPrivate *priv =
      wp_endpoint_get_instance_private (WP_ENDPOINT (object));

191
192
193
  g_debug ("%s:%p destroyed: %s", G_OBJECT_TYPE_NAME (object), object,
      priv->name);

194
195
  g_ptr_array_unref (priv->streams);
  g_ptr_array_unref (priv->controls);
George Kiagiadakis's avatar
George Kiagiadakis committed
196
  g_ptr_array_unref (priv->links);
197
  g_free (priv->name);
198
  g_weak_ref_clear (&priv->core);
George Kiagiadakis's avatar
George Kiagiadakis committed
199
200
201
202
203
204
205
206
207
208
209
210

  G_OBJECT_CLASS (wp_endpoint_parent_class)->finalize (object);
}

static void
wp_endpoint_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec)
{
  WpEndpointPrivate *priv =
      wp_endpoint_get_instance_private (WP_ENDPOINT (object));

  switch (property_id) {
211
212
213
  case PROP_CORE:
    g_weak_ref_set (&priv->core, g_value_get_object (value));
    break;
George Kiagiadakis's avatar
George Kiagiadakis committed
214
215
216
217
218
219
220
  case PROP_NAME:
    priv->name = g_value_dup_string (value);
    break;
  case PROP_MEDIA_CLASS:
    strncpy (priv->media_class, g_value_get_string (value),
        sizeof (priv->media_class) - 1);
    break;
221
222
223
  case PROP_DIRECTION:
    priv->direction = g_value_get_uint(value);
    break;
224
225
226
  case PROP_CREATION_TIME:
    priv->creation_time = g_value_get_uint64(value);
    break;
George Kiagiadakis's avatar
George Kiagiadakis committed
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}

static void
wp_endpoint_get_property (GObject * object, guint property_id, GValue * value,
    GParamSpec * pspec)
{
  WpEndpointPrivate *priv =
      wp_endpoint_get_instance_private (WP_ENDPOINT (object));

  switch (property_id) {
241
242
243
  case PROP_CORE:
    g_value_take_object (value, g_weak_ref_get (&priv->core));
    break;
George Kiagiadakis's avatar
George Kiagiadakis committed
244
245
246
247
248
249
  case PROP_NAME:
    g_value_set_string (value, priv->name);
    break;
  case PROP_MEDIA_CLASS:
    g_value_set_string (value, priv->media_class);
    break;
250
251
252
  case PROP_DIRECTION:
    g_value_set_uint (value, priv->direction);
    break;
253
254
255
  case PROP_CREATION_TIME:
    g_value_set_uint64 (value, priv->creation_time);
    break;
George Kiagiadakis's avatar
George Kiagiadakis committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}

static void
wp_endpoint_class_init (WpEndpointClass * klass)
{
  GObjectClass * object_class = (GObjectClass *) klass;

  object_class->dispose = wp_endpoint_dispose;
  object_class->finalize = wp_endpoint_finalize;
  object_class->get_property = wp_endpoint_get_property;
  object_class->set_property = wp_endpoint_set_property;

272
273
274
275
276
  g_object_class_install_property (object_class, PROP_CORE,
      g_param_spec_object ("core", "core", "The wireplumber core",
          WP_TYPE_CORE,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

277
278
279
280
  /**
   * WpEndpoint::name:
   * The name of the endpoint.
   */
George Kiagiadakis's avatar
George Kiagiadakis committed
281
282
283
284
  g_object_class_install_property (object_class, PROP_NAME,
      g_param_spec_string ("name", "name", "The name of the endpoint", NULL,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));

285
286
287
288
289
290
291
292
293
294
  /**
   * WpEndpoint::media-class:
   * The media class describes the type of media that this endpoint handles.
   * This should be the same as PipeWire media class strings.
   * For instance:
   * * Audio/Sink
   * * Audio/Source
   * * Video/Source
   * * Stream/Audio/Source
   */
George Kiagiadakis's avatar
George Kiagiadakis committed
295
296
297
298
  g_object_class_install_property (object_class, PROP_MEDIA_CLASS,
      g_param_spec_string ("media-class", "media-class",
          "The media class of the endpoint", NULL,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
299
300
301
302

  signals[SIGNAL_NOTIFY_CONTROL_VALUE] = g_signal_new ("notify-control-value",
      G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
      G_TYPE_NONE, 1, G_TYPE_UINT);
303
304
305
306
307
308
309
310
311

  /**
   * WpEndpoint::direction:
   * The direction of the endpoint: input = 0, output = 1.
   */
  g_object_class_install_property (object_class, PROP_DIRECTION,
      g_param_spec_uint ("direction", "direction",
          "The direction of the endpoint", 0, 1, 0,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
312
313
314
315
316
317
318
319
320
321

  /**
   * WpEndpoint::creation-time:
   * The creation time of the endpoint in monolitic time
   */
  g_object_class_install_property (object_class, PROP_CREATION_TIME,
      g_param_spec_uint64 ("creation-time", "creation-time",
          "The time that this endpoint was created, in monotonic time",
          0, G_MAXUINT64, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

George Kiagiadakis's avatar
George Kiagiadakis committed
322
323
}

324
325
326
327
328
329
330
331
332
/**
 * wp_endpoint_new_finish:
 * @initable: the #GAsyncInitable from the callback
 * @res: the #GAsyncResult from the callback
 * @error: return location for errors, or NULL to ignore
 *
 * Finishes the async construction of #WpEndpoint.
 */
WpEndpoint *
Julian Bouzas's avatar
Julian Bouzas committed
333
wp_endpoint_new_finish (GObject *initable, GAsyncResult *res, GError **error)
334
335
336
337
338
{
  GAsyncInitable *ai = G_ASYNC_INITABLE(initable);
  return WP_ENDPOINT(g_async_initable_new_finish(ai, res, error));
}

339
340
341
342
/**
 * wp_endpoint_register:
 * @self: the endpoint
 *
343
 * Registers the endpoint on the #WpCore.
344
345
 */
void
346
wp_endpoint_register (WpEndpoint * self)
347
348
{
  WpEndpointPrivate *priv;
349
  g_autoptr (WpCore) core = NULL;
350
351
352
353

  g_return_if_fail (WP_IS_ENDPOINT (self));

  priv = wp_endpoint_get_instance_private (self);
354
355
  core = g_weak_ref_get (&priv->core);
  g_return_if_fail (core != NULL);
356
357
358
359

  g_info ("WpEndpoint:%p registering '%s' (%s)", self, priv->name,
      priv->media_class);

360
  wp_core_register_object (core, g_object_ref (self));
361
362
363
364
365
366
367
368
369
370
371
372
373
}

/**
 * wp_endpoint_unregister:
 * @self: the endpoint
 *
 * Unregisters the endpoint from the session manager, if it was registered
 * and the session manager object still exists
 */
void
wp_endpoint_unregister (WpEndpoint * self)
{
  WpEndpointPrivate *priv;
374
  g_autoptr (WpCore) core = NULL;
375
  WpEndpointLink *l = NULL;
376
377
378
379

  g_return_if_fail (WP_IS_ENDPOINT (self));

  priv = wp_endpoint_get_instance_private (self);
380
381
382

  /* unlink before unregistering so that policy modules
   * can find dangling unlinked endpoints */
383
384
385
386
  while (priv->links->len > 0) {
    l = g_ptr_array_steal_index_fast(priv->links, priv->links->len - 1);
    wp_endpoint_link_destroy(l);
  }
387

388
389
  core = g_weak_ref_get (&priv->core);
  if (core) {
390
391
392
    g_info ("WpEndpoint:%p unregistering '%s' (%s)", self, priv->name,
        priv->media_class);

393
    wp_core_remove_object (core, self);
394
395
396
  }
}

397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/**
 * wp_endpoint_get_core:
 * @self: the endpoint
 *
 * Returns: (transfer full): the core on which this endpoint is registered
 */
WpCore *
wp_endpoint_get_core (WpEndpoint * self)
{
  WpEndpointPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), NULL);

  priv = wp_endpoint_get_instance_private (self);
  return g_weak_ref_get (&priv->core);
}

George Kiagiadakis's avatar
George Kiagiadakis committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
const gchar *
wp_endpoint_get_name (WpEndpoint * self)
{
  WpEndpointPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), NULL);

  priv = wp_endpoint_get_instance_private (self);
  return priv->name;
}

const gchar *
wp_endpoint_get_media_class (WpEndpoint * self)
{
  WpEndpointPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), NULL);

  priv = wp_endpoint_get_instance_private (self);
  return priv->media_class;
}

436
437
438
439
440
441
442
443
444
445
446
guint
wp_endpoint_get_direction (WpEndpoint * self)
{
  WpEndpointPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), -1);

  priv = wp_endpoint_get_instance_private (self);
  return priv->direction;
}

447
448
449
450
451
452
453
454
455
456
457
guint64
wp_endpoint_get_creation_time (WpEndpoint * self)
{
  WpEndpointPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), -1);

  priv = wp_endpoint_get_instance_private (self);
  return priv->creation_time;
}

458
459
460
461
462
/**
 * wp_endpoint_register_stream:
 * @self: the endpoint
 * @stream: (transfer floating): a dictionary GVariant with the stream info
 */
463
464
void
wp_endpoint_register_stream (WpEndpoint * self, GVariant * stream)
George Kiagiadakis's avatar
George Kiagiadakis committed
465
{
466
  WpEndpointPrivate *priv;
George Kiagiadakis's avatar
George Kiagiadakis committed
467

468
469
470
471
472
  g_return_if_fail (WP_IS_ENDPOINT (self));
  g_return_if_fail (g_variant_is_of_type (stream, G_VARIANT_TYPE_VARDICT));

  priv = wp_endpoint_get_instance_private (self);
  g_ptr_array_add (priv->streams, g_variant_ref_sink (stream));
George Kiagiadakis's avatar
George Kiagiadakis committed
473
474
}

475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
GVariant *
wp_endpoint_get_stream (WpEndpoint * self, guint32 stream_id)
{
  WpEndpointPrivate *priv;
  guint32 id;
  gint i;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), NULL);

  priv = wp_endpoint_get_instance_private (self);
  for (i = 0; i < priv->streams->len; i++) {
    GVariant *v = g_ptr_array_index (priv->streams, i);
    if (g_variant_lookup (v, "id", "u", &id) && id == stream_id) {
      return g_variant_ref (v);
    }
  }

  return NULL;
}

495
496
497
498
499
500
501
502
/**
 * wp_endpoint_list_streams:
 * @self: the endpoint
 *
 * Returns: (transfer floating): a floating GVariant that contains an array of
 *    dictionaries (aa{sv}) where each dictionary contains information about
 *    a single stream
 */
503
504
GVariant *
wp_endpoint_list_streams (WpEndpoint * self)
George Kiagiadakis's avatar
George Kiagiadakis committed
505
{
506
507
  WpEndpointPrivate *priv;

George Kiagiadakis's avatar
George Kiagiadakis committed
508
509
  g_return_val_if_fail (WP_IS_ENDPOINT (self), NULL);

510
511
512
  priv = wp_endpoint_get_instance_private (self);
  return g_variant_new_array (G_VARIANT_TYPE_VARDICT,
      (GVariant * const *) priv->streams->pdata, priv->streams->len);
George Kiagiadakis's avatar
George Kiagiadakis committed
513
514
}

515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
guint32
wp_endpoint_find_stream (WpEndpoint * self, const gchar * name)
{
  WpEndpointPrivate *priv;
  const gchar *tmp = NULL;
  guint32 id = WP_STREAM_ID_NONE;
  gint i;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), WP_STREAM_ID_NONE);
  g_return_val_if_fail (name != NULL, WP_STREAM_ID_NONE);

  priv = wp_endpoint_get_instance_private (self);
  for (i = 0; i < priv->streams->len; i++) {
    GVariant *v = g_ptr_array_index (priv->streams, i);
    if (g_variant_lookup (v, "name", "&s", &tmp) && !g_strcmp0 (tmp, name)) {
      /* found, return the id */
      g_variant_lookup (v, "id", "u", &id);
      break;
    }
  }

  return id;
}

539
540
541
542
543
/**
 * wp_endpoint_register_control:
 * @self: the endpoint
 * @control: (transfer floating): a dictionary GVariant with the control info
 */
544
545
void
wp_endpoint_register_control (WpEndpoint * self, GVariant * control)
George Kiagiadakis's avatar
George Kiagiadakis committed
546
{
547
548
549
550
  WpEndpointPrivate *priv;

  g_return_if_fail (WP_IS_ENDPOINT (self));
  g_return_if_fail (g_variant_is_of_type (control, G_VARIANT_TYPE_VARDICT));
George Kiagiadakis's avatar
George Kiagiadakis committed
551

552
553
  priv = wp_endpoint_get_instance_private (self);
  g_ptr_array_add (priv->controls, g_variant_ref_sink (control));
George Kiagiadakis's avatar
George Kiagiadakis committed
554
555
}

556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
GVariant *
wp_endpoint_get_control (WpEndpoint * self, guint32 control_id)
{
  WpEndpointPrivate *priv;
  guint32 id;
  gint i;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), NULL);

  priv = wp_endpoint_get_instance_private (self);
  for (i = 0; i < priv->controls->len; i++) {
    GVariant *v = g_ptr_array_index (priv->controls, i);
    if (g_variant_lookup (v, "id", "u", &id) && id == control_id) {
      return g_variant_ref (v);
    }
  }

  return NULL;
}

576
577
578
579
580
581
582
583
/**
 * wp_endpoint_list_controls:
 * @self: the endpoint
 *
 * Returns: (transfer floating): a floating GVariant that contains an array of
 *    dictionaries (aa{sv}) where each dictionary contains information about
 *    a single control
 */
584
585
GVariant *
wp_endpoint_list_controls (WpEndpoint * self)
George Kiagiadakis's avatar
George Kiagiadakis committed
586
{
587
588
  WpEndpointPrivate *priv;

George Kiagiadakis's avatar
George Kiagiadakis committed
589
590
  g_return_val_if_fail (WP_IS_ENDPOINT (self), NULL);

591
592
593
  priv = wp_endpoint_get_instance_private (self);
  return g_variant_new_array (G_VARIANT_TYPE_VARDICT,
      (GVariant * const *) priv->controls->pdata, priv->controls->len);
George Kiagiadakis's avatar
George Kiagiadakis committed
594
595
}

596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
guint32
wp_endpoint_find_control (WpEndpoint * self, guint32 stream_id,
    const gchar * name)
{
  WpEndpointPrivate *priv;
  const gchar *tmp = NULL;
  guint32 tmp_id = WP_STREAM_ID_NONE;
  guint32 id = WP_CONTROL_ID_NONE;
  gint i;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), WP_CONTROL_ID_NONE);
  g_return_val_if_fail (name != NULL, WP_CONTROL_ID_NONE);

  priv = wp_endpoint_get_instance_private (self);
  for (i = 0; i < priv->controls->len; i++) {
    GVariant *v = g_ptr_array_index (priv->controls, i);

    /*
     * if the stream-id exists, it must match @stream_id
     * if it doesn't exist, then @stream_id must be NONE
     */
    if (g_variant_lookup (v, "stream-id", "u", &tmp_id)) {
      if (stream_id != tmp_id)
        continue;
    } else if (stream_id != WP_STREAM_ID_NONE) {
      continue;
    }

    if (g_variant_lookup (v, "name", "&s", &tmp) && !g_strcmp0 (tmp, name)) {
      /* found, return the id */
      g_variant_lookup (v, "id", "u", &id);
      break;
    }
  }

  return id;
}

634
635
636
637
638
/**
 * wp_endpoint_get_control_value: (virtual get_control_value)
 * @self: the endpoint
 * @control_id: the id of the control to set
 *
639
640
 * Returns a GVariant that holds the value of the control. The type
 * should be the same type specified in the control variant's "type" field.
641
642
643
644
645
646
 *
 * On error, NULL will be returned.
 *
 * Returns: (transfer floating) (nullable): a dictionary GVariant containing
 *    the control value
 */
647
648
GVariant *
wp_endpoint_get_control_value (WpEndpoint * self, guint32 control_id)
George Kiagiadakis's avatar
George Kiagiadakis committed
649
{
650
  g_return_val_if_fail (WP_IS_ENDPOINT (self), NULL);
George Kiagiadakis's avatar
George Kiagiadakis committed
651

652
653
654
655
  if (WP_ENDPOINT_GET_CLASS (self)->get_control_value)
    return WP_ENDPOINT_GET_CLASS (self)->get_control_value (self, control_id);
  else
    return NULL;
George Kiagiadakis's avatar
George Kiagiadakis committed
656
657
}

658
659
660
661
662
663
664
665
666
667
668
669
/**
 * wp_endpoint_set_control_value: (virtual set_control_value)
 * @self: the endpoint
 * @control_id: the id of the control to set
 * @value: (transfer none): the value to set on the control
 *
 * Sets the @value on the specified control. The implementation should
 * call wp_endpoint_notify_control_value() if the value has been changed
 * in order to signal the change.
 *
 * Returns: TRUE on success, FALSE on failure
 */
George Kiagiadakis's avatar
George Kiagiadakis committed
670
gboolean
671
672
wp_endpoint_set_control_value (WpEndpoint * self, guint32 control_id,
    GVariant * value)
George Kiagiadakis's avatar
George Kiagiadakis committed
673
{
674
675
  gboolean ret = FALSE;

George Kiagiadakis's avatar
George Kiagiadakis committed
676
677
  g_return_val_if_fail (WP_IS_ENDPOINT (self), FALSE);

678
  if (WP_ENDPOINT_GET_CLASS (self)->set_control_value)
679
    ret = WP_ENDPOINT_GET_CLASS (self)->set_control_value (self, control_id,
680
        value);
681
682
683
684
685

  if (g_variant_is_floating (value))
    g_variant_unref (value);

  return ret;
686
687
}

688
689
690
691
692
693
694
695
696
/**
 * wp_endpoint_notify_control_value:
 * @self: the endpoint
 * @control_id: the id of the control
 *
 * Emits the "notify-control-value" signal so that others can be informed
 * about a value change in some of the controls. This is meant to be used
 * by subclasses only.
 */
697
698
699
700
701
void
wp_endpoint_notify_control_value (WpEndpoint * self, guint32 control_id)
{
  g_return_if_fail (WP_IS_ENDPOINT (self));
  g_signal_emit (self, signals[SIGNAL_NOTIFY_CONTROL_VALUE], 0, control_id);
George Kiagiadakis's avatar
George Kiagiadakis committed
702
703
}

704
705
706
707
708
709
/**
 * wp_endpoint_is_linked:
 * @self: the endpoint
 *
 * Returns: TRUE if there is at least one link associated with this endpoint
 */
George Kiagiadakis's avatar
George Kiagiadakis committed
710
711
712
713
714
715
716
717
718
719
720
gboolean
wp_endpoint_is_linked (WpEndpoint * self)
{
  WpEndpointPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), FALSE);

  priv = wp_endpoint_get_instance_private (self);
  return (priv->links->len > 0);
}

721
722
723
724
725
726
727
/**
 * wp_endpoint_get_links:
 * @self: the endpoint
 *
 * Returns: (transfer none) (element-type WpEndpointLink): an array of
 *    #WpEndpointLink objects that are currently associated with this endpoint
 */
George Kiagiadakis's avatar
George Kiagiadakis committed
728
729
730
731
732
733
734
735
736
737
738
GPtrArray *
wp_endpoint_get_links (WpEndpoint * self)
{
  WpEndpointPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT (self), NULL);

  priv = wp_endpoint_get_instance_private (self);
  return priv->links;
}

739
740
741
742
743
744
745
746
747
748
/**
 * wp_endpoint_unlink:
 * @self: the endpoint
 *
 * Unlinks all the endpoints linked to this endpoint
 */
void
wp_endpoint_unlink (WpEndpoint * self)
{
  WpEndpointPrivate *priv;
749
  WpEndpointLink *l = NULL;
750
751
752
753
754

  g_return_if_fail (WP_IS_ENDPOINT (self));

  priv = wp_endpoint_get_instance_private (self);

755
756
757
758
  while (priv->links->len > 0) {
    l = g_ptr_array_steal_index_fast(priv->links, priv->links->len - 1);
    wp_endpoint_link_destroy(l);
  }
759
760
}

George Kiagiadakis's avatar
George Kiagiadakis committed
761
762
763
764

typedef struct _WpEndpointLinkPrivate WpEndpointLinkPrivate;
struct _WpEndpointLinkPrivate
{
765
  GWeakRef src;
George Kiagiadakis's avatar
George Kiagiadakis committed
766
  guint32 src_stream;
767
  GWeakRef sink;
George Kiagiadakis's avatar
George Kiagiadakis committed
768
769
770
  guint32 sink_stream;
};

771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
enum {
  LINKPROP_0,
  LINKPROP_SRC,
  LINKPROP_SRC_STREAM,
  LINKPROP_SINK,
  LINKPROP_SINK_STREAM,
};

static void wp_endpoint_link_async_initable_init (gpointer iface,
    gpointer iface_data);

G_DEFINE_ABSTRACT_TYPE_WITH_CODE (WpEndpointLink, wp_endpoint_link, G_TYPE_OBJECT,
    G_ADD_PRIVATE (WpEndpointLink)
    G_IMPLEMENT_INTERFACE (G_TYPE_ASYNC_INITABLE,
                           wp_endpoint_link_async_initable_init))
George Kiagiadakis's avatar
George Kiagiadakis committed
786
787

static void
788
endpoint_link_finalize (GObject * object)
George Kiagiadakis's avatar
George Kiagiadakis committed
789
{
790
791
792
793
794
795
  WpEndpointLinkPrivate *priv =
      wp_endpoint_link_get_instance_private (WP_ENDPOINT_LINK (object));

  /* Clear the endpoint weak reaferences */
  g_weak_ref_clear(&priv->src);
  g_weak_ref_clear(&priv->sink);
George Kiagiadakis's avatar
George Kiagiadakis committed
796
797
798
}

static void
799
800
endpoint_link_set_property (GObject * object, guint property_id,
    const GValue * value, GParamSpec * pspec)
George Kiagiadakis's avatar
George Kiagiadakis committed
801
{
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
  WpEndpointLinkPrivate *priv =
      wp_endpoint_link_get_instance_private (WP_ENDPOINT_LINK (object));

  switch (property_id) {
  case LINKPROP_SRC:
    g_weak_ref_set (&priv->src, g_value_get_object (value));
    break;
  case LINKPROP_SRC_STREAM:
    priv->src_stream = g_value_get_uint(value);
    break;
  case LINKPROP_SINK:
    g_weak_ref_set (&priv->sink, g_value_get_object (value));
    break;
  case LINKPROP_SINK_STREAM:
    priv->sink_stream = g_value_get_uint(value);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
George Kiagiadakis's avatar
George Kiagiadakis committed
822
823
}

824
825
826
static void
endpoint_link_get_property (GObject * object, guint property_id,
    GValue * value, GParamSpec * pspec)
George Kiagiadakis's avatar
George Kiagiadakis committed
827
{
828
829
  WpEndpointLinkPrivate *priv =
      wp_endpoint_link_get_instance_private (WP_ENDPOINT_LINK (object));
George Kiagiadakis's avatar
George Kiagiadakis committed
830

831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
  switch (property_id) {
  case LINKPROP_SRC:
    g_value_take_object (value, g_weak_ref_get (&priv->src));
    break;
  case LINKPROP_SRC_STREAM:
    g_value_set_uint (value, priv->src_stream);
    break;
  case LINKPROP_SINK:
    g_value_take_object (value, g_weak_ref_get (&priv->sink));
    break;
  case LINKPROP_SINK_STREAM:
    g_value_set_uint (value, priv->sink_stream);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}
George Kiagiadakis's avatar
George Kiagiadakis committed
849

850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
static void
wp_endpoint_link_init_async (GAsyncInitable *initable, int io_priority,
    GCancellable *cancellable, GAsyncReadyCallback callback, gpointer data)
{
  WpEndpointLink *link = WP_ENDPOINT_LINK(initable);
  WpEndpointLinkPrivate *priv =
      wp_endpoint_link_get_instance_private (WP_ENDPOINT_LINK (initable));
  g_autoptr (WpEndpoint) src = g_weak_ref_get (&priv->src);
  g_autoptr (WpEndpoint) sink = g_weak_ref_get (&priv->sink);
  g_autoptr (GVariant) src_props = NULL;
  g_autoptr (GVariant) sink_props = NULL;
  WpEndpointPrivate *endpoint_priv;

  /* Prepare the endpoints */
  if (!WP_ENDPOINT_GET_CLASS (src)->prepare_link (src, priv->src_stream, link,
865
866
      &src_props, NULL)) {
    g_critical ("Failed to prepare link on source endpoint");
867
868
869
    return;
  }
  if (!WP_ENDPOINT_GET_CLASS (sink)->prepare_link (sink, priv->sink_stream,
870
871
      link, &sink_props, NULL)) {
    g_critical ("Failed to prepare link on sink endpoint");
872
873
874
875
876
877
    return;
  }

  /* Create the link */
  g_return_if_fail (WP_ENDPOINT_LINK_GET_CLASS (link)->create);
  if (!WP_ENDPOINT_LINK_GET_CLASS (link)->create (link, src_props,
878
879
      sink_props, NULL)) {
    g_critical ("Failed to create link in src and sink endpoints");
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
    return;
  }

  /* Register the link on the endpoints */
  endpoint_priv = wp_endpoint_get_instance_private (src);
  g_ptr_array_add (endpoint_priv->links, g_object_ref (link));
  endpoint_priv = wp_endpoint_get_instance_private (sink);
  g_ptr_array_add (endpoint_priv->links, g_object_ref (link));
}

static gboolean
wp_endpoint_link_init_finish (GAsyncInitable *initable, GAsyncResult *result,
    GError **error)
{
  g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);

  return g_task_propagate_boolean (G_TASK (result), error);
}

static void
wp_endpoint_link_async_initable_init (gpointer iface, gpointer iface_data)
{
  GAsyncInitableIface *ai_iface = iface;

  ai_iface->init_async = wp_endpoint_link_init_async;
  ai_iface->init_finish = wp_endpoint_link_init_finish;
}

static void
wp_endpoint_link_init (WpEndpointLink * self)
{
  WpEndpointLinkPrivate *priv = wp_endpoint_link_get_instance_private (self);

  /* Init the endpoint weak references */
  g_weak_ref_init (&priv->src, NULL);
  g_weak_ref_init (&priv->sink, NULL);
}

static void
wp_endpoint_link_class_init (WpEndpointLinkClass * klass)
{
  GObjectClass *object_class = (GObjectClass *) klass;

  object_class->finalize = endpoint_link_finalize;
  object_class->set_property = endpoint_link_set_property;
  object_class->get_property = endpoint_link_get_property;

  g_object_class_install_property (object_class, LINKPROP_SRC,
      g_param_spec_object ("src", "src", "The src endpoint", WP_TYPE_ENDPOINT,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, LINKPROP_SRC_STREAM,
      g_param_spec_uint ("src-stream", "src-stream", "The src stream",
          0, G_MAXUINT, 0,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, LINKPROP_SINK,
      g_param_spec_object ("sink", "sink", "The sink endpoint", WP_TYPE_ENDPOINT,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (object_class, LINKPROP_SINK_STREAM,
      g_param_spec_uint ("sink-stream", "sink-stream", "The sink stream",
          0, G_MAXUINT, 0,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
George Kiagiadakis's avatar
George Kiagiadakis committed
941
942
}

Julian Bouzas's avatar
Julian Bouzas committed
943
944
945
946
947
948
949
950
/**
 * wp_endpoint_link_get_source_endpoint:
 * @self: the endpoint
 *
 * Gets the source endpoint of the link
 *
 * Returns: (transfer full): the source endpoint
 */
George Kiagiadakis's avatar
George Kiagiadakis committed
951
952
953
954
955
956
957
958
WpEndpoint *
wp_endpoint_link_get_source_endpoint (WpEndpointLink * self)
{
  WpEndpointLinkPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT_LINK (self), NULL);

  priv = wp_endpoint_link_get_instance_private (self);
959
  return g_weak_ref_get (&priv->src);
George Kiagiadakis's avatar
George Kiagiadakis committed
960
961
962
963
964
965
966
967
968
969
970
971
972
}

guint32
wp_endpoint_link_get_source_stream (WpEndpointLink * self)
{
  WpEndpointLinkPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT_LINK (self), 0);

  priv = wp_endpoint_link_get_instance_private (self);
  return priv->src_stream;
}

Julian Bouzas's avatar
Julian Bouzas committed
973
974
975
976
977
978
979
980
/**
 * wp_endpoint_link_get_sink_endpoint:
 * @self: the endpoint
 *
 * Gets the sink endpoint of the link
 *
 * Returns: (transfer full): the sink endpoint
 */
George Kiagiadakis's avatar
George Kiagiadakis committed
981
982
983
984
985
986
987
988
WpEndpoint *
wp_endpoint_link_get_sink_endpoint (WpEndpointLink * self)
{
  WpEndpointLinkPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT_LINK (self), NULL);

  priv = wp_endpoint_link_get_instance_private (self);
989
  return g_weak_ref_get (&priv->sink);
George Kiagiadakis's avatar
George Kiagiadakis committed
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
}

guint32
wp_endpoint_link_get_sink_stream (WpEndpointLink * self)
{
  WpEndpointLinkPrivate *priv;

  g_return_val_if_fail (WP_IS_ENDPOINT_LINK (self), 0);

  priv = wp_endpoint_link_get_instance_private (self);
  return priv->sink_stream;
}

1003
1004
1005
1006
void
wp_endpoint_link_new (WpCore * core, WpEndpoint * src, guint32 src_stream,
    WpEndpoint * sink, guint32 sink_stream, GAsyncReadyCallback ready,
    gpointer data)
George Kiagiadakis's avatar
George Kiagiadakis committed
1007
1008
{
  const gchar *src_factory = NULL, *sink_factory = NULL;
1009
1010
  GVariantBuilder b;
  g_autoptr (GVariant) link_props = NULL;
George Kiagiadakis's avatar
George Kiagiadakis committed
1011

1012
1013
1014
1015
  g_return_if_fail (WP_IS_ENDPOINT (src));
  g_return_if_fail (WP_IS_ENDPOINT (sink));
  g_return_if_fail (WP_ENDPOINT_GET_CLASS (src)->prepare_link);
  g_return_if_fail (WP_ENDPOINT_GET_CLASS (sink)->prepare_link);
George Kiagiadakis's avatar
George Kiagiadakis committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025

  /* find the factory */

  if (WP_ENDPOINT_GET_CLASS (src)->get_endpoint_link_factory)
    src_factory = WP_ENDPOINT_GET_CLASS (src)->get_endpoint_link_factory (src);
  if (WP_ENDPOINT_GET_CLASS (sink)->get_endpoint_link_factory)
    sink_factory = WP_ENDPOINT_GET_CLASS (sink)->get_endpoint_link_factory (sink);

  if (src_factory || sink_factory) {
    if (src_factory && sink_factory && strcmp (src_factory, sink_factory) != 0) {
1026
1027
1028
      g_critical ("It is not possible to link endpoints that both specify"
          "different custom link factories");
      return;
George Kiagiadakis's avatar
George Kiagiadakis committed
1029
1030
1031
1032
1033
1034
    } else if (sink_factory)
      src_factory = sink_factory;
  } else {
    src_factory = "pipewire-simple-endpoint-link";
  }

1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
  /* Build the properties */
  g_variant_builder_init (&b, G_VARIANT_TYPE_VARDICT);
  g_variant_builder_add (&b, "{sv}", "src",
      g_variant_new_uint64 ((guint64)src));
  g_variant_builder_add (&b, "{sv}", "src-stream",
      g_variant_new_uint32 (src_stream));
  g_variant_builder_add (&b, "{sv}", "sink",
      g_variant_new_uint64 ((guint64)sink));
  g_variant_builder_add (&b, "{sv}", "sink-stream",
      g_variant_new_uint32 (sink_stream));
  link_props = g_variant_builder_end (&b);

  /* Create the link object async */
  wp_factory_make (core, src_factory, WP_TYPE_ENDPOINT_LINK, link_props, ready,
      data);
}
George Kiagiadakis's avatar
George Kiagiadakis committed
1051

1052
1053
1054
1055
1056
1057
WpEndpointLink *
wp_endpoint_link_new_finish (GObject *initable, GAsyncResult *res,
    GError **error)
{
  GAsyncInitable *ai = G_ASYNC_INITABLE(initable);
  return WP_ENDPOINT_LINK(g_async_initable_new_finish(ai, res, error));
George Kiagiadakis's avatar
George Kiagiadakis committed
1058
1059
1060
1061
1062
1063
1064
}

void
wp_endpoint_link_destroy (WpEndpointLink * self)
{
  WpEndpointLinkPrivate *priv;
  WpEndpointPrivate *endpoint_priv;
1065
1066
  g_autoptr (WpEndpoint) src = NULL;
  g_autoptr (WpEndpoint) sink = NULL;
George Kiagiadakis's avatar
George Kiagiadakis committed
1067
1068
1069
1070
1071

  g_return_if_fail (WP_IS_ENDPOINT_LINK (self));
  g_return_if_fail (WP_ENDPOINT_LINK_GET_CLASS (self)->destroy);

  priv = wp_endpoint_link_get_instance_private (self);
1072
1073
  src = g_weak_ref_get (&priv->src);
  sink = g_weak_ref_get (&priv->sink);
George Kiagiadakis's avatar
George Kiagiadakis committed
1074

1075
1076
  WP_ENDPOINT_LINK_GET_CLASS (self)->destroy (self);

1077
1078
1079
1080
  if (src && WP_ENDPOINT_GET_CLASS (src)->release_link)
    WP_ENDPOINT_GET_CLASS (src)->release_link (src, self);
  if (sink && WP_ENDPOINT_GET_CLASS (sink)->release_link)
    WP_ENDPOINT_GET_CLASS (sink)->release_link (sink, self);
George Kiagiadakis's avatar
George Kiagiadakis committed
1081

1082
1083
1084
1085
1086
1087
1088
1089
  if (src) {
    endpoint_priv = wp_endpoint_get_instance_private (src);
    g_ptr_array_remove_fast (endpoint_priv->links, self);
  }
  if (sink) {
    endpoint_priv = wp_endpoint_get_instance_private (sink);
    g_ptr_array_remove_fast (endpoint_priv->links, self);
  }
George Kiagiadakis's avatar
George Kiagiadakis committed
1090
}