1 From 4696c0071d7a713d3e415f7d0cf29364a2b19aa2 Mon Sep 17 00:00:00 2001
2 From: George Kiagiadakis <george.kiagiadakis@collabora.com>
3 Date: Tue, 19 Feb 2019 18:23:19 +0200
4 Subject: [PATCH] gst: Implement new pwaudio{src,sink} elements, based on
7 These are much more reliable elements to use for audio data.
8 * GstAudioBaseSink provides a reliable clock implementation based
9 on the number of samples read/written
10 * on the pipewire side we make sure to dequeue, fill and enqueue
11 a single buffer inside the process() function, which avoids
14 Both elements share a common ringbuffer that actually implements
15 the pipewire integration.
17 Upstream-Status: Denied
18 See https://gitlab.freedesktop.org/pipewire/pipewire/merge_requests/140
20 src/gst/gstpipewire.c | 8 +-
21 src/gst/gstpwaudioringbuffer.c | 542 +++++++++++++++++++++++++++++++++
22 src/gst/gstpwaudioringbuffer.h | 83 +++++
23 src/gst/gstpwaudiosink.c | 200 ++++++++++++
24 src/gst/gstpwaudiosink.h | 48 +++
25 src/gst/gstpwaudiosrc.c | 200 ++++++++++++
26 src/gst/gstpwaudiosrc.h | 48 +++
27 src/gst/meson.build | 6 +
28 8 files changed, 1134 insertions(+), 1 deletion(-)
29 create mode 100644 src/gst/gstpwaudioringbuffer.c
30 create mode 100644 src/gst/gstpwaudioringbuffer.h
31 create mode 100644 src/gst/gstpwaudiosink.c
32 create mode 100644 src/gst/gstpwaudiosink.h
33 create mode 100644 src/gst/gstpwaudiosrc.c
34 create mode 100644 src/gst/gstpwaudiosrc.h
36 diff --git a/src/gst/gstpipewire.c b/src/gst/gstpipewire.c
37 index 4040264b..68fd446f 100644
38 --- a/src/gst/gstpipewire.c
39 +++ b/src/gst/gstpipewire.c
41 #include "gstpipewiresrc.h"
42 #include "gstpipewiresink.h"
43 #include "gstpipewiredeviceprovider.h"
44 +#include "gstpwaudiosrc.h"
45 +#include "gstpwaudiosink.h"
47 GST_DEBUG_CATEGORY (pipewire_debug);
49 @@ -52,12 +54,16 @@ plugin_init (GstPlugin *plugin)
50 GST_TYPE_PIPEWIRE_SRC);
51 gst_element_register (plugin, "pipewiresink", GST_RANK_NONE,
52 GST_TYPE_PIPEWIRE_SINK);
53 + gst_element_register (plugin, "pwaudiosrc", GST_RANK_NONE,
54 + GST_TYPE_PW_AUDIO_SRC);
55 + gst_element_register (plugin, "pwaudiosink", GST_RANK_NONE,
56 + GST_TYPE_PW_AUDIO_SINK);
58 if (!gst_device_provider_register (plugin, "pipewiredeviceprovider",
59 GST_RANK_PRIMARY + 1, GST_TYPE_PIPEWIRE_DEVICE_PROVIDER))
62 - GST_DEBUG_CATEGORY_INIT (pipewire_debug, "pipewire", 0, "PipeWirie elements");
63 + GST_DEBUG_CATEGORY_INIT (pipewire_debug, "pipewire", 0, "PipeWire elements");
67 diff --git a/src/gst/gstpwaudioringbuffer.c b/src/gst/gstpwaudioringbuffer.c
69 index 00000000..989b2cd7
71 +++ b/src/gst/gstpwaudioringbuffer.c
75 + * Copyright © 2018 Wim Taymans
76 + * Copyright © 2019 Collabora Ltd.
77 + * @author George Kiagiadakis <george.kiagiadakis@collabora.com>
79 + * Permission is hereby granted, free of charge, to any person obtaining a
80 + * copy of this software and associated documentation files (the "Software"),
81 + * to deal in the Software without restriction, including without limitation
82 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83 + * and/or sell copies of the Software, and to permit persons to whom the
84 + * Software is furnished to do so, subject to the following conditions:
86 + * The above copyright notice and this permission notice (including the next
87 + * paragraph) shall be included in all copies or substantial portions of the
90 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
91 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
92 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
93 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
94 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
95 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
96 + * DEALINGS IN THE SOFTWARE.
103 +#include "gstpwaudioringbuffer.h"
105 +#include <spa/param/audio/format-utils.h>
106 +#include <spa/pod/builder.h>
108 +GST_DEBUG_CATEGORY_STATIC (pw_audio_ring_buffer_debug);
109 +#define GST_CAT_DEFAULT pw_audio_ring_buffer_debug
111 +#define gst_pw_audio_ring_buffer_parent_class parent_class
112 +G_DEFINE_TYPE (GstPwAudioRingBuffer, gst_pw_audio_ring_buffer, GST_TYPE_AUDIO_RING_BUFFER);
123 +gst_pw_audio_ring_buffer_init (GstPwAudioRingBuffer * self)
125 + self->loop = pw_loop_new (NULL);
126 + self->main_loop = pw_thread_loop_new (self->loop, "pw-audioringbuffer-loop");
127 + self->core = pw_core_new (self->loop, NULL, 0);
131 +gst_pw_audio_ring_buffer_finalize (GObject * object)
133 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (object);
135 + pw_core_destroy (self->core);
136 + pw_thread_loop_destroy (self->main_loop);
137 + pw_loop_destroy (self->loop);
141 +gst_pw_audio_ring_buffer_set_property (GObject * object, guint prop_id,
142 + const GValue * value, GParamSpec * pspec)
144 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (object);
148 + self->elem = g_value_get_object (value);
151 + case PROP_DIRECTION:
152 + self->direction = g_value_get_int (value);
156 + self->props = g_value_get_pointer (value);
160 + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166 +on_remote_state_changed (void *data, enum pw_remote_state old,
167 + enum pw_remote_state state, const char *error)
169 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (data);
171 + GST_DEBUG_OBJECT (self->elem, "got remote state %d", state);
174 + case PW_REMOTE_STATE_UNCONNECTED:
175 + case PW_REMOTE_STATE_CONNECTING:
176 + case PW_REMOTE_STATE_CONNECTED:
178 + case PW_REMOTE_STATE_ERROR:
179 + GST_ELEMENT_ERROR (self->elem, RESOURCE, FAILED,
180 + ("remote error: %s", error), (NULL));
183 + pw_thread_loop_signal (self->main_loop, FALSE);
186 +static const struct pw_remote_events remote_events = {
187 + PW_VERSION_REMOTE_EVENTS,
188 + .state_changed = on_remote_state_changed,
192 +wait_for_remote_state (GstPwAudioRingBuffer *self,
193 + enum pw_remote_state target)
196 + enum pw_remote_state state = pw_remote_get_state (self->remote, NULL);
197 + if (state == target)
199 + if (state == PW_REMOTE_STATE_ERROR)
201 + pw_thread_loop_wait (self->main_loop);
206 +gst_pw_audio_ring_buffer_open_device (GstAudioRingBuffer *buf)
208 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (buf);
210 + GST_DEBUG_OBJECT (self->elem, "open device");
212 + if (pw_thread_loop_start (self->main_loop) < 0)
213 + goto mainloop_error;
215 + pw_thread_loop_lock (self->main_loop);
217 + self->remote = pw_remote_new (self->core, NULL, 0);
218 + pw_remote_add_listener (self->remote, &self->remote_listener, &remote_events,
221 + if (self->props->fd == -1)
222 + pw_remote_connect (self->remote);
224 + pw_remote_connect_fd (self->remote, self->props->fd);
226 + GST_DEBUG_OBJECT (self->elem, "waiting for connection");
228 + if (!wait_for_remote_state (self, PW_REMOTE_STATE_CONNECTED))
229 + goto connect_error;
231 + pw_thread_loop_unlock (self->main_loop);
238 + GST_ELEMENT_ERROR (self->elem, RESOURCE, FAILED,
239 + ("Failed to start mainloop"), (NULL));
244 + pw_thread_loop_unlock (self->main_loop);
250 +gst_pw_audio_ring_buffer_close_device (GstAudioRingBuffer *buf)
252 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (buf);
254 + GST_DEBUG_OBJECT (self->elem, "closing device");
256 + pw_thread_loop_lock (self->main_loop);
257 + if (self->remote) {
258 + pw_remote_disconnect (self->remote);
259 + wait_for_remote_state (self, PW_REMOTE_STATE_UNCONNECTED);
261 + pw_thread_loop_unlock (self->main_loop);
263 + pw_thread_loop_stop (self->main_loop);
265 + if (self->remote) {
266 + pw_remote_destroy (self->remote);
267 + self->remote = NULL;
273 +on_stream_state_changed (void *data, enum pw_stream_state old,
274 + enum pw_stream_state state, const char *error)
276 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (data);
278 + GST_DEBUG_OBJECT (self->elem, "got stream state: %s",
279 + pw_stream_state_as_string (state));
282 + case PW_STREAM_STATE_UNCONNECTED:
283 + GST_ELEMENT_ERROR (self->elem, RESOURCE, FAILED,
284 + ("stream disconnected unexpectedly"), (NULL));
286 + case PW_STREAM_STATE_CONNECTING:
287 + case PW_STREAM_STATE_CONFIGURE:
288 + case PW_STREAM_STATE_READY:
289 + case PW_STREAM_STATE_PAUSED:
290 + case PW_STREAM_STATE_STREAMING:
292 + case PW_STREAM_STATE_ERROR:
293 + GST_ELEMENT_ERROR (self->elem, RESOURCE, FAILED,
294 + ("stream error: %s", error), (NULL));
297 + pw_thread_loop_signal (self->main_loop, FALSE);
301 +wait_for_stream_state (GstPwAudioRingBuffer *self,
302 + enum pw_stream_state target)
305 + enum pw_stream_state state = pw_stream_get_state (self->stream, NULL);
306 + if (state >= target)
308 + if (state == PW_STREAM_STATE_ERROR || state == PW_STREAM_STATE_UNCONNECTED)
310 + pw_thread_loop_wait (self->main_loop);
315 +on_stream_format_changed (void *data, const struct spa_pod *format)
317 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (data);
318 + const struct spa_pod *params[1];
319 + struct spa_pod_builder b = { NULL };
320 + uint8_t buffer[512];
322 + spa_pod_builder_init (&b, buffer, sizeof (buffer));
323 + params[0] = spa_pod_builder_add_object (&b,
324 + SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers,
325 + SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(16, 1, INT32_MAX),
326 + SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(1),
327 + SPA_PARAM_BUFFERS_size, SPA_POD_Int(self->segsize),
328 + SPA_PARAM_BUFFERS_stride, SPA_POD_Int(self->bpf),
329 + SPA_PARAM_BUFFERS_align, SPA_POD_Int(16));
331 + GST_DEBUG_OBJECT (self->elem, "doing finish format, buffer size:%d", self->segsize);
332 + pw_stream_finish_format (self->stream, 0, params, 1);
336 +on_stream_process (void *data)
338 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (data);
339 + GstAudioRingBuffer *buf = GST_AUDIO_RING_BUFFER (data);
340 + struct pw_buffer *b;
341 + struct spa_data *d;
342 + gint size; /*< size to read/write from/to the spa buffer */
343 + gint offset; /*< offset to read/write from/to in the spa buffer */
344 + gint segment; /*< the current segment number in the ringbuffer */
345 + guint8 *ringptr; /*< pointer to the beginning of the current segment */
346 + gint segsize; /*< the size of one segment in the ringbuffer */
347 + gint copy_size; /*< the bytes to copy in one memcpy() invocation */
348 + gint remain; /*< remainder of bytes available in the spa buffer */
350 + if (g_atomic_int_get (&buf->state) != GST_AUDIO_RING_BUFFER_STATE_STARTED) {
351 + GST_LOG_OBJECT (self->elem, "ring buffer is not started");
355 + b = pw_stream_dequeue_buffer (self->stream);
357 + GST_WARNING_OBJECT (self->elem, "no pipewire buffer available");
361 + d = &b->buffer->datas[0];
363 + if (self->direction == PW_DIRECTION_OUTPUT) {
364 + /* in output mode, always fill the entire spa buffer */
365 + offset = d->chunk->offset = 0;
366 + size = d->chunk->size = d->maxsize;
367 + b->size = size / self->bpf;
369 + offset = SPA_MIN (d->chunk->offset, d->maxsize);
370 + size = SPA_MIN (d->chunk->size, d->maxsize - offset);
374 + gst_audio_ring_buffer_prepare_read (buf, &segment, &ringptr, &segsize);
376 + /* in INPUT (src) mode, it is possible that the skew algorithm
377 + * advances the ringbuffer behind our back */
378 + if (self->segoffset > 0 && self->cur_segment != segment)
379 + self->segoffset = 0;
381 + copy_size = SPA_MIN (size, segsize - self->segoffset);
383 + if (self->direction == PW_DIRECTION_OUTPUT) {
384 + memcpy (((guint8*) d->data) + offset, ringptr + self->segoffset,
387 + memcpy (ringptr + self->segoffset, ((guint8*) d->data) + offset,
391 + remain = size - (segsize - self->segoffset);
393 + GST_TRACE_OBJECT (self->elem,
394 + "seg %d: %s %d bytes remained:%d offset:%d segoffset:%d", segment,
395 + self->direction == PW_DIRECTION_INPUT ? "INPUT" : "OUTPUT",
396 + copy_size, remain, offset, self->segoffset);
399 + offset += (segsize - self->segoffset);
402 + /* write silence on the segment we just read */
403 + if (self->direction == PW_DIRECTION_OUTPUT)
404 + gst_audio_ring_buffer_clear (buf, segment);
406 + /* notify that we have read a complete segment */
407 + gst_audio_ring_buffer_advance (buf, 1);
408 + self->segoffset = 0;
410 + self->segoffset += size;
411 + self->cur_segment = segment;
413 + } while (remain > 0);
415 + pw_stream_queue_buffer (self->stream, b);
418 +static const struct pw_stream_events stream_events = {
419 + PW_VERSION_STREAM_EVENTS,
420 + .state_changed = on_stream_state_changed,
421 + .format_changed = on_stream_format_changed,
422 + .process = on_stream_process,
426 +copy_properties (GQuark field_id, const GValue *value, gpointer user_data)
428 + struct pw_properties *properties = user_data;
430 + if (G_VALUE_HOLDS_STRING (value))
431 + pw_properties_set (properties,
432 + g_quark_to_string (field_id),
433 + g_value_get_string (value));
438 +gst_pw_audio_ring_buffer_acquire (GstAudioRingBuffer *buf,
439 + GstAudioRingBufferSpec *spec)
441 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (buf);
442 + struct pw_properties *props;
443 + struct spa_pod_builder b = { NULL };
444 + uint8_t buffer[512];
445 + const struct spa_pod *params[1];
447 + g_return_val_if_fail (spec, FALSE);
448 + g_return_val_if_fail (GST_AUDIO_INFO_IS_VALID (&spec->info), FALSE);
449 + g_return_val_if_fail (!self->stream, TRUE); /* already acquired */
451 + g_return_val_if_fail (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW, FALSE);
452 + g_return_val_if_fail (GST_AUDIO_INFO_IS_FLOAT (&spec->info), FALSE);
454 + GST_DEBUG_OBJECT (self->elem, "acquire");
456 + /* construct param & props objects */
458 + if (self->props->properties) {
459 + props = pw_properties_new (NULL, NULL);
460 + gst_structure_foreach (self->props->properties, copy_properties, props);
465 + spa_pod_builder_init (&b, buffer, sizeof (buffer));
466 + params[0] = spa_pod_builder_add_object (&b,
467 + SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
468 + SPA_FORMAT_mediaType, SPA_POD_Id (SPA_MEDIA_TYPE_audio),
469 + SPA_FORMAT_mediaSubtype, SPA_POD_Id (SPA_MEDIA_SUBTYPE_raw),
470 + SPA_FORMAT_AUDIO_format, SPA_POD_Id (SPA_AUDIO_FORMAT_F32),
471 + SPA_FORMAT_AUDIO_rate, SPA_POD_Int (GST_AUDIO_INFO_RATE (&spec->info)),
472 + SPA_FORMAT_AUDIO_channels, SPA_POD_Int (GST_AUDIO_INFO_CHANNELS (&spec->info)));
474 + self->segsize = spec->segsize;
475 + self->bpf = GST_AUDIO_INFO_BPF (&spec->info);
476 + self->rate = GST_AUDIO_INFO_RATE (&spec->info);
477 + self->segoffset = 0;
479 + /* connect stream */
481 + pw_thread_loop_lock (self->main_loop);
483 + GST_DEBUG_OBJECT (self->elem, "creating stream");
485 + self->stream = pw_stream_new (self->remote, self->props->client_name, props);
486 + pw_stream_add_listener(self->stream, &self->stream_listener, &stream_events,
489 + if (pw_stream_connect (self->stream,
491 + self->props->path ? (uint32_t)atoi(self->props->path) : SPA_ID_INVALID,
492 + PW_STREAM_FLAG_AUTOCONNECT |
493 + PW_STREAM_FLAG_MAP_BUFFERS |
494 + PW_STREAM_FLAG_RT_PROCESS,
498 + GST_DEBUG_OBJECT (self->elem, "waiting for stream READY");
500 + if (!wait_for_stream_state (self, PW_STREAM_STATE_READY))
503 + pw_thread_loop_unlock (self->main_loop);
505 + /* allocate the internal ringbuffer */
507 + spec->seglatency = spec->segtotal + 1;
508 + buf->size = spec->segtotal * spec->segsize;
509 + buf->memory = g_malloc (buf->size);
511 + gst_audio_format_fill_silence (buf->spec.info.finfo, buf->memory,
514 + GST_DEBUG_OBJECT (self->elem, "acquire done");
520 + GST_ERROR_OBJECT (self->elem, "could not start stream");
521 + pw_stream_destroy (self->stream);
522 + self->stream = NULL;
523 + pw_thread_loop_unlock (self->main_loop);
529 +gst_pw_audio_ring_buffer_release (GstAudioRingBuffer *buf)
531 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (buf);
533 + GST_DEBUG_OBJECT (self->elem, "release");
535 + pw_thread_loop_lock (self->main_loop);
536 + if (self->stream) {
537 + spa_hook_remove (&self->stream_listener);
538 + pw_stream_disconnect (self->stream);
539 + pw_stream_destroy (self->stream);
540 + self->stream = NULL;
542 + pw_thread_loop_unlock (self->main_loop);
544 + /* free the buffer */
545 + g_free (buf->memory);
546 + buf->memory = NULL;
552 +gst_pw_audio_ring_buffer_delay (GstAudioRingBuffer *buf)
554 + GstPwAudioRingBuffer *self = GST_PW_AUDIO_RING_BUFFER (buf);
557 + if (!self->stream || pw_stream_get_time (self->stream, &t) < 0)
560 + if (self->direction == PW_DIRECTION_OUTPUT) {
561 + /* on output streams, we set the pw_buffer.size in frames,
562 + so no conversion is necessary */
565 + /* on input streams, pw_buffer.size is set by pw_stream in ticks,
566 + so we need to convert it to frames and also add segoffset, which
567 + is the number of bytes we have read but not advertised yet, as
568 + the segment is incomplete */
569 + if (t.rate.denom > 0)
571 + gst_util_uint64_scale (t.queued, self->rate * t.rate.num, t.rate.denom)
572 + + self->segoffset / self->bpf;
574 + return self->segoffset / self->bpf;
581 +gst_pw_audio_ring_buffer_class_init (GstPwAudioRingBufferClass * klass)
583 + GObjectClass *gobject_class;
584 + GstAudioRingBufferClass *gstaudiorbuf_class;
586 + gobject_class = (GObjectClass *) klass;
587 + gstaudiorbuf_class = (GstAudioRingBufferClass *) klass;
589 + gobject_class->finalize = gst_pw_audio_ring_buffer_finalize;
590 + gobject_class->set_property = gst_pw_audio_ring_buffer_set_property;
592 + gstaudiorbuf_class->open_device = gst_pw_audio_ring_buffer_open_device;
593 + gstaudiorbuf_class->acquire = gst_pw_audio_ring_buffer_acquire;
594 + gstaudiorbuf_class->release = gst_pw_audio_ring_buffer_release;
595 + gstaudiorbuf_class->close_device = gst_pw_audio_ring_buffer_close_device;
596 + gstaudiorbuf_class->delay = gst_pw_audio_ring_buffer_delay;
598 + g_object_class_install_property (gobject_class, PROP_ELEMENT,
599 + g_param_spec_object ("element", "Element", "The audio source or sink",
601 + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
603 + g_object_class_install_property (gobject_class, PROP_DIRECTION,
604 + g_param_spec_int ("direction", "Direction", "The stream direction",
605 + PW_DIRECTION_INPUT, PW_DIRECTION_OUTPUT, PW_DIRECTION_INPUT,
606 + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
608 + g_object_class_install_property (gobject_class, PROP_PROPS,
609 + g_param_spec_pointer ("props", "Properties", "The properties struct",
610 + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
612 + GST_DEBUG_CATEGORY_INIT (pw_audio_ring_buffer_debug, "pwaudioringbuffer", 0,
613 + "PipeWire Audio Ring Buffer");
615 diff --git a/src/gst/gstpwaudioringbuffer.h b/src/gst/gstpwaudioringbuffer.h
617 index 00000000..f47f668a
619 +++ b/src/gst/gstpwaudioringbuffer.h
623 + * Copyright © 2018 Wim Taymans
624 + * Copyright © 2019 Collabora Ltd.
625 + * @author George Kiagiadakis <george.kiagiadakis@collabora.com>
627 + * Permission is hereby granted, free of charge, to any person obtaining a
628 + * copy of this software and associated documentation files (the "Software"),
629 + * to deal in the Software without restriction, including without limitation
630 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
631 + * and/or sell copies of the Software, and to permit persons to whom the
632 + * Software is furnished to do so, subject to the following conditions:
634 + * The above copyright notice and this permission notice (including the next
635 + * paragraph) shall be included in all copies or substantial portions of the
638 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
639 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
640 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
641 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
642 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
643 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
644 + * DEALINGS IN THE SOFTWARE.
647 +#ifndef __GST_PW_AUDIO_RING_BUFFER_H__
648 +#define __GST_PW_AUDIO_RING_BUFFER_H__
650 +#include <gst/gst.h>
651 +#include <gst/audio/audio.h>
652 +#include <pipewire/pipewire.h>
656 +#define GST_TYPE_PW_AUDIO_RING_BUFFER \
657 + (gst_pw_audio_ring_buffer_get_type ())
659 +G_DECLARE_FINAL_TYPE(GstPwAudioRingBuffer, gst_pw_audio_ring_buffer,
660 + GST, PW_AUDIO_RING_BUFFER, GstAudioRingBuffer);
662 +typedef struct _GstPwAudioRingBufferProps GstPwAudioRingBufferProps;
664 +struct _GstPwAudioRingBuffer
666 + GstAudioRingBuffer parent;
670 + enum pw_direction direction;
671 + GstPwAudioRingBufferProps *props;
674 + struct pw_loop *loop;
675 + struct pw_thread_loop *main_loop;
677 + struct pw_core *core;
678 + struct pw_remote *remote;
679 + struct spa_hook remote_listener;
681 + struct pw_stream *stream;
682 + struct spa_hook stream_listener;
688 + /* on_stream_process() state */
693 +struct _GstPwAudioRingBufferProps
696 + gchar *client_name;
697 + GstStructure *properties;
704 diff --git a/src/gst/gstpwaudiosink.c b/src/gst/gstpwaudiosink.c
706 index 00000000..6cb71385
708 +++ b/src/gst/gstpwaudiosink.c
712 + * Copyright © 2018 Wim Taymans
713 + * Copyright © 2019 Collabora Ltd.
714 + * @author George Kiagiadakis <george.kiagiadakis@collabora.com>
716 + * Permission is hereby granted, free of charge, to any person obtaining a
717 + * copy of this software and associated documentation files (the "Software"),
718 + * to deal in the Software without restriction, including without limitation
719 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
720 + * and/or sell copies of the Software, and to permit persons to whom the
721 + * Software is furnished to do so, subject to the following conditions:
723 + * The above copyright notice and this permission notice (including the next
724 + * paragraph) shall be included in all copies or substantial portions of the
727 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
728 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
729 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
730 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
731 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
732 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
733 + * DEALINGS IN THE SOFTWARE.
736 +#ifdef HAVE_CONFIG_H
740 +#include "gstpwaudiosink.h"
742 +GST_DEBUG_CATEGORY_STATIC (pw_audio_sink_debug);
743 +#define GST_CAT_DEFAULT pw_audio_sink_debug
745 +G_DEFINE_TYPE (GstPwAudioSink, gst_pw_audio_sink, GST_TYPE_AUDIO_BASE_SINK);
752 + PROP_STREAM_PROPERTIES,
756 +static GstStaticPadTemplate gst_pw_audio_sink_template =
757 +GST_STATIC_PAD_TEMPLATE ("sink",
760 + GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_NE (F32))
761 + ", layout = (string)\"interleaved\"")
766 +gst_pw_audio_sink_init (GstPwAudioSink * self)
768 + self->props.fd = -1;
772 +gst_pw_audio_sink_finalize (GObject * object)
774 + GstPwAudioSink *pwsink = GST_PW_AUDIO_SINK (object);
776 + g_free (pwsink->props.path);
777 + g_free (pwsink->props.client_name);
778 + if (pwsink->props.properties)
779 + gst_structure_free (pwsink->props.properties);
783 +gst_pw_audio_sink_set_property (GObject * object, guint prop_id,
784 + const GValue * value, GParamSpec * pspec)
786 + GstPwAudioSink *pwsink = GST_PW_AUDIO_SINK (object);
790 + g_free (pwsink->props.path);
791 + pwsink->props.path = g_value_dup_string (value);
794 + case PROP_CLIENT_NAME:
795 + g_free (pwsink->props.client_name);
796 + pwsink->props.client_name = g_value_dup_string (value);
799 + case PROP_STREAM_PROPERTIES:
800 + if (pwsink->props.properties)
801 + gst_structure_free (pwsink->props.properties);
802 + pwsink->props.properties =
803 + gst_structure_copy (gst_value_get_structure (value));
807 + pwsink->props.fd = g_value_get_int (value);
811 + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
817 +gst_pw_audio_sink_get_property (GObject * object, guint prop_id,
818 + GValue * value, GParamSpec * pspec)
820 + GstPwAudioSink *pwsink = GST_PW_AUDIO_SINK (object);
824 + g_value_set_string (value, pwsink->props.path);
827 + case PROP_CLIENT_NAME:
828 + g_value_set_string (value, pwsink->props.client_name);
831 + case PROP_STREAM_PROPERTIES:
832 + gst_value_set_structure (value, pwsink->props.properties);
836 + g_value_set_int (value, pwsink->props.fd);
840 + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
845 +static GstAudioRingBuffer *
846 +gst_pw_audio_sink_create_ringbuffer (GstAudioBaseSink * sink)
848 + GstPwAudioSink *self = GST_PW_AUDIO_SINK (sink);
849 + GstAudioRingBuffer *buffer;
851 + GST_DEBUG_OBJECT (sink, "creating ringbuffer");
852 + buffer = g_object_new (GST_TYPE_PW_AUDIO_RING_BUFFER,
854 + "direction", PW_DIRECTION_OUTPUT,
855 + "props", &self->props,
857 + GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
863 +gst_pw_audio_sink_class_init (GstPwAudioSinkClass * klass)
865 + GObjectClass *gobject_class;
866 + GstElementClass *gstelement_class;
867 + GstAudioBaseSinkClass *gstaudiobsink_class;
869 + gobject_class = (GObjectClass *) klass;
870 + gstelement_class = (GstElementClass *) klass;
871 + gstaudiobsink_class = (GstAudioBaseSinkClass *) klass;
873 + gobject_class->finalize = gst_pw_audio_sink_finalize;
874 + gobject_class->set_property = gst_pw_audio_sink_set_property;
875 + gobject_class->get_property = gst_pw_audio_sink_get_property;
877 + gstaudiobsink_class->create_ringbuffer = gst_pw_audio_sink_create_ringbuffer;
879 + g_object_class_install_property (gobject_class, PROP_PATH,
880 + g_param_spec_string ("path", "Path",
881 + "The sink path to connect to (NULL = default)", NULL,
882 + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
884 + g_object_class_install_property (gobject_class, PROP_CLIENT_NAME,
885 + g_param_spec_string ("client-name", "Client Name",
886 + "The client name to use (NULL = default)", NULL,
887 + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
889 + g_object_class_install_property (gobject_class, PROP_STREAM_PROPERTIES,
890 + g_param_spec_boxed ("stream-properties", "Stream properties",
891 + "List of PipeWire stream properties", GST_TYPE_STRUCTURE,
892 + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
894 + g_object_class_install_property (gobject_class, PROP_FD,
895 + g_param_spec_int ("fd", "Fd", "The fd to connect with", -1, G_MAXINT, -1,
896 + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
898 + gst_element_class_set_static_metadata (gstelement_class,
899 + "PipeWire Audio sink", "Sink/Audio",
900 + "Send audio to PipeWire",
901 + "George Kiagiadakis <george.kiagiadakis@collabora.com>");
903 + gst_element_class_add_pad_template (gstelement_class,
904 + gst_static_pad_template_get (&gst_pw_audio_sink_template));
906 + GST_DEBUG_CATEGORY_INIT (pw_audio_sink_debug, "pwaudiosink", 0,
907 + "PipeWire Audio Sink");
910 diff --git a/src/gst/gstpwaudiosink.h b/src/gst/gstpwaudiosink.h
912 index 00000000..7ed0de7b
914 +++ b/src/gst/gstpwaudiosink.h
918 + * Copyright © 2018 Wim Taymans
919 + * Copyright © 2019 Collabora Ltd.
920 + * @author George Kiagiadakis <george.kiagiadakis@collabora.com>
922 + * Permission is hereby granted, free of charge, to any person obtaining a
923 + * copy of this software and associated documentation files (the "Software"),
924 + * to deal in the Software without restriction, including without limitation
925 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
926 + * and/or sell copies of the Software, and to permit persons to whom the
927 + * Software is furnished to do so, subject to the following conditions:
929 + * The above copyright notice and this permission notice (including the next
930 + * paragraph) shall be included in all copies or substantial portions of the
933 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
934 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
935 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
936 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
937 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
938 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
939 + * DEALINGS IN THE SOFTWARE.
942 +#ifndef __GST_PW_AUDIO_SINK_H__
943 +#define __GST_PW_AUDIO_SINK_H__
945 +#include "gstpwaudioringbuffer.h"
949 +#define GST_TYPE_PW_AUDIO_SINK \
950 + (gst_pw_audio_sink_get_type ())
952 +G_DECLARE_FINAL_TYPE(GstPwAudioSink, gst_pw_audio_sink,
953 + GST, PW_AUDIO_SINK, GstAudioBaseSink);
955 +struct _GstPwAudioSink
957 + GstAudioBaseSink parent;
958 + GstPwAudioRingBufferProps props;
964 diff --git a/src/gst/gstpwaudiosrc.c b/src/gst/gstpwaudiosrc.c
966 index 00000000..6c522982
968 +++ b/src/gst/gstpwaudiosrc.c
972 + * Copyright © 2018 Wim Taymans
973 + * Copyright © 2019 Collabora Ltd.
974 + * @author George Kiagiadakis <george.kiagiadakis@collabora.com>
976 + * Permission is hereby granted, free of charge, to any person obtaining a
977 + * copy of this software and associated documentation files (the "Software"),
978 + * to deal in the Software without restriction, including without limitation
979 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
980 + * and/or sell copies of the Software, and to permit persons to whom the
981 + * Software is furnished to do so, subject to the following conditions:
983 + * The above copyright notice and this permission notice (including the next
984 + * paragraph) shall be included in all copies or substantial portions of the
987 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
988 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
989 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
990 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
991 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
992 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
993 + * DEALINGS IN THE SOFTWARE.
996 +#ifdef HAVE_CONFIG_H
1000 +#include "gstpwaudiosrc.h"
1002 +GST_DEBUG_CATEGORY_STATIC (pw_audio_src_debug);
1003 +#define GST_CAT_DEFAULT pw_audio_src_debug
1005 +G_DEFINE_TYPE (GstPwAudioSrc, gst_pw_audio_src, GST_TYPE_AUDIO_BASE_SRC);
1012 + PROP_STREAM_PROPERTIES,
1016 +static GstStaticPadTemplate gst_pw_audio_src_template =
1017 +GST_STATIC_PAD_TEMPLATE ("src",
1020 + GST_STATIC_CAPS (GST_AUDIO_CAPS_MAKE (GST_AUDIO_NE (F32))
1021 + ", layout = (string)\"interleaved\"")
1026 +gst_pw_audio_src_init (GstPwAudioSrc * self)
1028 + self->props.fd = -1;
1032 +gst_pw_audio_src_finalize (GObject * object)
1034 + GstPwAudioSrc *self = GST_PW_AUDIO_SRC (object);
1036 + g_free (self->props.path);
1037 + g_free (self->props.client_name);
1038 + if (self->props.properties)
1039 + gst_structure_free (self->props.properties);
1043 +gst_pw_audio_src_set_property (GObject * object, guint prop_id,
1044 + const GValue * value, GParamSpec * pspec)
1046 + GstPwAudioSrc *self = GST_PW_AUDIO_SRC (object);
1048 + switch (prop_id) {
1050 + g_free (self->props.path);
1051 + self->props.path = g_value_dup_string (value);
1054 + case PROP_CLIENT_NAME:
1055 + g_free (self->props.client_name);
1056 + self->props.client_name = g_value_dup_string (value);
1059 + case PROP_STREAM_PROPERTIES:
1060 + if (self->props.properties)
1061 + gst_structure_free (self->props.properties);
1062 + self->props.properties =
1063 + gst_structure_copy (gst_value_get_structure (value));
1067 + self->props.fd = g_value_get_int (value);
1071 + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1077 +gst_pw_audio_src_get_property (GObject * object, guint prop_id,
1078 + GValue * value, GParamSpec * pspec)
1080 + GstPwAudioSrc *self = GST_PW_AUDIO_SRC (object);
1082 + switch (prop_id) {
1084 + g_value_set_string (value, self->props.path);
1087 + case PROP_CLIENT_NAME:
1088 + g_value_set_string (value, self->props.client_name);
1091 + case PROP_STREAM_PROPERTIES:
1092 + gst_value_set_structure (value, self->props.properties);
1096 + g_value_set_int (value, self->props.fd);
1100 + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1105 +static GstAudioRingBuffer *
1106 +gst_pw_audio_src_create_ringbuffer (GstAudioBaseSrc * sink)
1108 + GstPwAudioSrc *self = GST_PW_AUDIO_SRC (sink);
1109 + GstAudioRingBuffer *buffer;
1111 + GST_DEBUG_OBJECT (sink, "creating ringbuffer");
1112 + buffer = g_object_new (GST_TYPE_PW_AUDIO_RING_BUFFER,
1114 + "direction", PW_DIRECTION_INPUT,
1115 + "props", &self->props,
1117 + GST_DEBUG_OBJECT (sink, "created ringbuffer @%p", buffer);
1123 +gst_pw_audio_src_class_init (GstPwAudioSrcClass * klass)
1125 + GObjectClass *gobject_class;
1126 + GstElementClass *gstelement_class;
1127 + GstAudioBaseSrcClass *gstaudiobsrc_class;
1129 + gobject_class = (GObjectClass *) klass;
1130 + gstelement_class = (GstElementClass *) klass;
1131 + gstaudiobsrc_class = (GstAudioBaseSrcClass *) klass;
1133 + gobject_class->finalize = gst_pw_audio_src_finalize;
1134 + gobject_class->set_property = gst_pw_audio_src_set_property;
1135 + gobject_class->get_property = gst_pw_audio_src_get_property;
1137 + gstaudiobsrc_class->create_ringbuffer = gst_pw_audio_src_create_ringbuffer;
1139 + g_object_class_install_property (gobject_class, PROP_PATH,
1140 + g_param_spec_string ("path", "Path",
1141 + "The sink path to connect to (NULL = default)", NULL,
1142 + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1144 + g_object_class_install_property (gobject_class, PROP_CLIENT_NAME,
1145 + g_param_spec_string ("client-name", "Client Name",
1146 + "The client name to use (NULL = default)", NULL,
1147 + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1149 + g_object_class_install_property (gobject_class, PROP_STREAM_PROPERTIES,
1150 + g_param_spec_boxed ("stream-properties", "Stream properties",
1151 + "List of PipeWire stream properties", GST_TYPE_STRUCTURE,
1152 + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1154 + g_object_class_install_property (gobject_class, PROP_FD,
1155 + g_param_spec_int ("fd", "Fd", "The fd to connect with", -1, G_MAXINT, -1,
1156 + G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1158 + gst_element_class_set_static_metadata (gstelement_class,
1159 + "PipeWire Audio source", "Source/Audio",
1160 + "Receive audio from PipeWire",
1161 + "George Kiagiadakis <george.kiagiadakis@collabora.com>");
1163 + gst_element_class_add_pad_template (gstelement_class,
1164 + gst_static_pad_template_get (&gst_pw_audio_src_template));
1166 + GST_DEBUG_CATEGORY_INIT (pw_audio_src_debug, "pwaudiosrc", 0,
1167 + "PipeWire Audio Src");
1170 diff --git a/src/gst/gstpwaudiosrc.h b/src/gst/gstpwaudiosrc.h
1171 new file mode 100644
1172 index 00000000..c46e644c
1174 +++ b/src/gst/gstpwaudiosrc.h
1178 + * Copyright © 2018 Wim Taymans
1179 + * Copyright © 2019 Collabora Ltd.
1180 + * @author George Kiagiadakis <george.kiagiadakis@collabora.com>
1182 + * Permission is hereby granted, free of charge, to any person obtaining a
1183 + * copy of this software and associated documentation files (the "Software"),
1184 + * to deal in the Software without restriction, including without limitation
1185 + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1186 + * and/or sell copies of the Software, and to permit persons to whom the
1187 + * Software is furnished to do so, subject to the following conditions:
1189 + * The above copyright notice and this permission notice (including the next
1190 + * paragraph) shall be included in all copies or substantial portions of the
1193 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1194 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1195 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1196 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1197 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1198 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1199 + * DEALINGS IN THE SOFTWARE.
1202 +#ifndef __GST_PW_AUDIO_SRC_H__
1203 +#define __GST_PW_AUDIO_SRC_H__
1205 +#include "gstpwaudioringbuffer.h"
1209 +#define GST_TYPE_PW_AUDIO_SRC \
1210 + (gst_pw_audio_src_get_type ())
1212 +G_DECLARE_FINAL_TYPE(GstPwAudioSrc, gst_pw_audio_src,
1213 + GST, PW_AUDIO_SRC, GstAudioBaseSrc);
1215 +struct _GstPwAudioSrc
1217 + GstAudioBaseSrc parent;
1218 + GstPwAudioRingBufferProps props;
1224 diff --git a/src/gst/meson.build b/src/gst/meson.build
1225 index ad0e0801..0e922347 100644
1226 --- a/src/gst/meson.build
1227 +++ b/src/gst/meson.build
1228 @@ -6,6 +6,9 @@ pipewire_gst_sources = [
1229 'gstpipewirepool.c',
1230 'gstpipewiresink.c',
1232 + 'gstpwaudioringbuffer.c',
1233 + 'gstpwaudiosink.c',
1234 + 'gstpwaudiosrc.c',
1237 pipewire_gst_headers = [
1238 @@ -15,6 +18,9 @@ pipewire_gst_headers = [
1239 'gstpipewirepool.h',
1240 'gstpipewiresink.h',
1242 + 'gstpwaudioringbuffer.h',
1243 + 'gstpwaudiosink.h',
1244 + 'gstpwaudiosrc.h',
1247 pipewire_gst_c_args = [