2015-05-11 18:23:24 +02:00
|
|
|
/* GStreamer
|
|
|
|
|
* Copyright (C) <2015> Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2017-05-23 19:15:33 +02:00
|
|
|
* SECTION:element-pipewiresink
|
2015-05-11 18:23:24 +02:00
|
|
|
*
|
|
|
|
|
* <refsect2>
|
|
|
|
|
* <title>Example launch line</title>
|
|
|
|
|
* |[
|
2017-05-23 19:15:33 +02:00
|
|
|
* gst-launch -v videotestsrc ! pipewiresink
|
|
|
|
|
* ]| Sends a test video source to PipeWire
|
2015-05-11 18:23:24 +02:00
|
|
|
* </refsect2>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
2017-05-23 19:15:33 +02:00
|
|
|
#include "gstpipewiresink.h"
|
2015-05-11 18:23:24 +02:00
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <gio/gunixfdmessage.h>
|
|
|
|
|
#include <gst/allocators/gstfdmemory.h>
|
2016-04-13 13:04:32 +02:00
|
|
|
#include <gst/video/video.h>
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-16 09:20:42 +02:00
|
|
|
#include <spa/buffer.h>
|
2016-07-28 21:19:20 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
#include "gstpipewireformat.h"
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2016-08-24 16:26:58 +02:00
|
|
|
static GQuark process_mem_data_quark;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (pipewire_sink_debug);
|
|
|
|
|
#define GST_CAT_DEFAULT pipewire_sink_debug
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
#define DEFAULT_PROP_MODE GST_PIPEWIRE_SINK_MODE_DEFAULT
|
2016-05-06 17:54:23 +02:00
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
PROP_0,
|
2016-05-05 13:31:18 +02:00
|
|
|
PROP_PATH,
|
2015-12-09 13:27:43 +01:00
|
|
|
PROP_CLIENT_NAME,
|
2016-05-06 17:54:23 +02:00
|
|
|
PROP_STREAM_PROPERTIES,
|
|
|
|
|
PROP_MODE
|
2015-05-11 18:23:24 +02:00
|
|
|
};
|
|
|
|
|
|
2016-05-06 17:54:23 +02:00
|
|
|
GType
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_mode_get_type (void)
|
2016-05-06 17:54:23 +02:00
|
|
|
{
|
|
|
|
|
static volatile gsize mode_type = 0;
|
|
|
|
|
static const GEnumValue mode[] = {
|
2017-05-23 19:15:33 +02:00
|
|
|
{GST_PIPEWIRE_SINK_MODE_DEFAULT, "GST_PIPEWIRE_SINK_MODE_DEFAULT", "default"},
|
|
|
|
|
{GST_PIPEWIRE_SINK_MODE_RENDER, "GST_PIPEWIRE_SINK_MODE_RENDER", "render"},
|
|
|
|
|
{GST_PIPEWIRE_SINK_MODE_PROVIDE, "GST_PIPEWIRE_SINK_MODE_PROVIDE", "provide"},
|
2016-05-06 17:54:23 +02:00
|
|
|
{0, NULL, NULL},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (g_once_init_enter (&mode_type)) {
|
|
|
|
|
GType tmp =
|
2017-05-23 19:15:33 +02:00
|
|
|
g_enum_register_static ("GstPipeWireSinkMode", mode);
|
2016-05-06 17:54:23 +02:00
|
|
|
g_once_init_leave (&mode_type, tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (GType) mode_type;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
static GstStaticPadTemplate gst_pipewire_sink_template =
|
2015-05-11 18:23:24 +02:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
|
GST_PAD_SINK,
|
|
|
|
|
GST_PAD_ALWAYS,
|
2015-05-15 13:34:32 +02:00
|
|
|
GST_STATIC_CAPS_ANY
|
2015-05-11 18:23:24 +02:00
|
|
|
);
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
#define gst_pipewire_sink_parent_class parent_class
|
|
|
|
|
G_DEFINE_TYPE (GstPipeWireSink, gst_pipewire_sink, GST_TYPE_BASE_SINK);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
static void gst_pipewire_sink_set_property (GObject * object, guint prop_id,
|
2015-05-11 18:23:24 +02:00
|
|
|
const GValue * value, GParamSpec * pspec);
|
2017-05-23 19:15:33 +02:00
|
|
|
static void gst_pipewire_sink_get_property (GObject * object, guint prop_id,
|
2015-05-11 18:23:24 +02:00
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_change_state (GstElement * element, GstStateChange transition);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
static gboolean gst_pipewire_sink_setcaps (GstBaseSink * bsink, GstCaps * caps);
|
|
|
|
|
static GstCaps *gst_pipewire_sink_sink_fixate (GstBaseSink * bsink,
|
2015-05-11 18:23:24 +02:00
|
|
|
GstCaps * caps);
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
static GstFlowReturn gst_pipewire_sink_render (GstBaseSink * psink,
|
2015-05-11 18:23:24 +02:00
|
|
|
GstBuffer * buffer);
|
2017-05-23 19:15:33 +02:00
|
|
|
static gboolean gst_pipewire_sink_start (GstBaseSink * basesink);
|
|
|
|
|
static gboolean gst_pipewire_sink_stop (GstBaseSink * basesink);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2015-07-28 17:05:03 +02:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_finalize (GObject * object)
|
2015-07-28 17:05:03 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSink *pwsink = GST_PIPEWIRE_SINK (object);
|
2015-07-28 17:05:03 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
g_object_unref (pwsink->pool);
|
2016-11-26 19:18:51 +01:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_destroy (pwsink->main_loop);
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsink->main_loop = NULL;
|
2016-11-26 19:18:51 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_loop_destroy (pwsink->loop);
|
|
|
|
|
pwsink->loop = NULL;
|
2016-11-26 19:18:51 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsink->properties)
|
|
|
|
|
gst_structure_free (pwsink->properties);
|
|
|
|
|
g_object_unref (pwsink->allocator);
|
|
|
|
|
g_free (pwsink->path);
|
|
|
|
|
g_free (pwsink->client_name);
|
|
|
|
|
g_hash_table_unref (pwsink->buf_ids);
|
2015-07-28 17:05:03 +02:00
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 16:38:20 +01:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
|
2015-12-04 16:38:20 +01:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSink *pwsink = GST_PIPEWIRE_SINK (bsink);
|
2015-12-04 16:38:20 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_query_add_allocation_pool (query, GST_BUFFER_POOL_CAST (pwsink->pool), 0, 0, 0);
|
2015-12-04 16:38:20 +01:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_class_init (GstPipeWireSinkClass * klass)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
GstBaseSinkClass *gstbasesink_class;
|
|
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
|
gstbasesink_class = (GstBaseSinkClass *) klass;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
gobject_class->finalize = gst_pipewire_sink_finalize;
|
|
|
|
|
gobject_class->set_property = gst_pipewire_sink_set_property;
|
|
|
|
|
gobject_class->get_property = gst_pipewire_sink_get_property;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2016-05-05 13:31:18 +02:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_PATH,
|
|
|
|
|
g_param_spec_string ("path",
|
|
|
|
|
"Path",
|
|
|
|
|
"The sink path to connect to (NULL = default)",
|
|
|
|
|
NULL,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
2015-07-28 17:05:03 +02:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_CLIENT_NAME,
|
|
|
|
|
g_param_spec_string ("client-name",
|
|
|
|
|
"Client Name",
|
|
|
|
|
"The client name to use (NULL = default)",
|
|
|
|
|
NULL,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
2015-12-09 13:27:43 +01:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_STREAM_PROPERTIES,
|
|
|
|
|
g_param_spec_boxed ("stream-properties",
|
2016-05-06 17:54:23 +02:00
|
|
|
"Stream properties",
|
2017-05-23 19:15:33 +02:00
|
|
|
"List of PipeWire stream properties",
|
2015-12-09 13:27:43 +01:00
|
|
|
GST_TYPE_STRUCTURE,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
2016-05-06 17:54:23 +02:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_MODE,
|
|
|
|
|
g_param_spec_enum ("mode",
|
|
|
|
|
"Mode",
|
|
|
|
|
"The mode to operate in",
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_TYPE_PIPEWIRE_SINK_MODE,
|
2016-05-06 17:54:23 +02:00
|
|
|
DEFAULT_PROP_MODE,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
gstelement_class->change_state = gst_pipewire_sink_change_state;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
|
|
|
|
gst_element_class_set_static_metadata (gstelement_class,
|
2017-05-23 19:15:33 +02:00
|
|
|
"PipeWire sink", "Sink/Video",
|
|
|
|
|
"Send video to PipeWire", "Wim Taymans <wim.taymans@gmail.com>");
|
2015-05-11 18:23:24 +02:00
|
|
|
|
|
|
|
|
gst_element_class_add_pad_template (gstelement_class,
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_static_pad_template_get (&gst_pipewire_sink_template));
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
gstbasesink_class->set_caps = gst_pipewire_sink_setcaps;
|
|
|
|
|
gstbasesink_class->fixate = gst_pipewire_sink_sink_fixate;
|
|
|
|
|
gstbasesink_class->propose_allocation = gst_pipewire_sink_propose_allocation;
|
|
|
|
|
gstbasesink_class->start = gst_pipewire_sink_start;
|
|
|
|
|
gstbasesink_class->stop = gst_pipewire_sink_stop;
|
|
|
|
|
gstbasesink_class->render = gst_pipewire_sink_render;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_CATEGORY_INIT (pipewire_sink_debug, "pipewiresink", 0,
|
|
|
|
|
"PipeWire Sink");
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
process_mem_data_quark = g_quark_from_static_string ("GstPipeWireSinkProcessMemQuark");
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
2017-09-21 18:57:41 +02:00
|
|
|
#define SPA_PROP_RANGE(min,max) 2,min,max
|
2017-05-12 11:00:25 +02:00
|
|
|
|
|
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
pool_activated (GstPipeWirePool *pool, GstPipeWireSink *sink)
|
2017-05-12 11:00:25 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
struct pw_type *t = sink->type;
|
2017-05-12 11:00:25 +02:00
|
|
|
GstStructure *config;
|
|
|
|
|
GstCaps *caps;
|
|
|
|
|
guint size;
|
|
|
|
|
guint min_buffers;
|
|
|
|
|
guint max_buffers;
|
2017-11-07 17:39:31 +01:00
|
|
|
struct spa_pod_object *port_params[3];
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_pod_builder b = { NULL };
|
2017-05-12 11:00:25 +02:00
|
|
|
uint8_t buffer[1024];
|
|
|
|
|
|
|
|
|
|
config = gst_buffer_pool_get_config (GST_BUFFER_POOL (pool));
|
|
|
|
|
gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers, &max_buffers);
|
|
|
|
|
|
|
|
|
|
spa_pod_builder_init (&b, buffer, sizeof (buffer));
|
2017-11-08 15:48:31 +01:00
|
|
|
spa_pod_builder_push_object (&b, t->param.idBuffers, t->param_alloc_buffers.Buffers);
|
2017-05-22 13:06:18 +02:00
|
|
|
if (size == 0)
|
|
|
|
|
spa_pod_builder_add (&b,
|
2017-09-21 18:57:41 +02:00
|
|
|
":", t->param_alloc_buffers.size, "iru", 0, SPA_PROP_RANGE(0, INT32_MAX), NULL);
|
2017-05-22 13:06:18 +02:00
|
|
|
else
|
|
|
|
|
spa_pod_builder_add (&b,
|
2017-09-21 18:57:41 +02:00
|
|
|
":", t->param_alloc_buffers.size, "ir", size, SPA_PROP_RANGE(size, INT32_MAX), NULL);
|
2017-05-22 13:06:18 +02:00
|
|
|
|
|
|
|
|
spa_pod_builder_add (&b,
|
2017-09-21 18:57:41 +02:00
|
|
|
":", t->param_alloc_buffers.stride, "ir", 0, SPA_PROP_RANGE(0, INT32_MAX),
|
|
|
|
|
":", t->param_alloc_buffers.buffers, "iru", min_buffers,
|
|
|
|
|
SPA_PROP_RANGE(min_buffers,
|
|
|
|
|
max_buffers ? max_buffers : INT32_MAX),
|
|
|
|
|
":", t->param_alloc_buffers.align, "i", 16,
|
|
|
|
|
NULL);
|
2017-11-08 15:48:31 +01:00
|
|
|
port_params[0] = spa_pod_builder_pop_deref (&b);
|
2017-05-22 13:06:18 +02:00
|
|
|
|
2017-11-07 17:39:31 +01:00
|
|
|
port_params[1] = spa_pod_builder_object (&b,
|
|
|
|
|
t->param.idMeta, t->param_alloc_meta_enable.MetaEnable,
|
2017-09-21 18:57:41 +02:00
|
|
|
":", t->param_alloc_meta_enable.type, "I", t->meta.Header,
|
|
|
|
|
":", t->param_alloc_meta_enable.size, "i", sizeof (struct spa_meta_header));
|
|
|
|
|
|
2017-11-07 17:39:31 +01:00
|
|
|
port_params[2] = spa_pod_builder_object (&b,
|
|
|
|
|
t->param.idMeta, t->param_alloc_meta_enable.MetaEnable,
|
2017-09-21 18:57:41 +02:00
|
|
|
":", t->param_alloc_meta_enable.type, "I", t->meta.Ringbuffer,
|
|
|
|
|
":", t->param_alloc_meta_enable.size, "i", sizeof (struct spa_meta_ringbuffer),
|
|
|
|
|
":", t->param_alloc_meta_enable.ringbufferSize, "i", size * SPA_MAX (4,
|
|
|
|
|
SPA_MAX (min_buffers, max_buffers)),
|
|
|
|
|
":", t->param_alloc_meta_enable.ringbufferStride, "i", 0,
|
|
|
|
|
":", t->param_alloc_meta_enable.ringbufferBlocks, "i", 1,
|
|
|
|
|
":", t->param_alloc_meta_enable.ringbufferAlign, "i", 16);
|
2017-05-12 11:00:25 +02:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (sink->main_loop);
|
2017-11-07 17:39:31 +01:00
|
|
|
pw_stream_finish_format (sink->stream, SPA_RESULT_OK, 2, port_params);
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (sink->main_loop);
|
2017-05-12 11:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_init (GstPipeWireSink * sink)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2016-08-25 14:10:38 +02:00
|
|
|
sink->allocator = gst_fd_allocator_new ();
|
2017-05-23 19:15:33 +02:00
|
|
|
sink->pool = gst_pipewire_pool_new ();
|
2017-05-30 19:46:51 +02:00
|
|
|
sink->client_name = pw_get_client_name();
|
2016-05-06 17:54:23 +02:00
|
|
|
sink->mode = DEFAULT_PROP_MODE;
|
2015-12-03 15:59:27 +01:00
|
|
|
|
2017-05-12 11:00:25 +02:00
|
|
|
g_signal_connect (sink->pool, "activated", G_CALLBACK (pool_activated), sink);
|
|
|
|
|
|
2016-08-24 16:26:58 +02:00
|
|
|
sink->buf_ids = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
|
|
|
|
|
(GDestroyNotify) gst_buffer_unref);
|
2016-11-26 19:18:51 +01:00
|
|
|
|
2016-12-20 16:51:57 +01:00
|
|
|
g_queue_init (&sink->queue);
|
|
|
|
|
|
2017-08-08 18:22:44 +02:00
|
|
|
sink->loop = pw_loop_new (NULL);
|
2017-06-01 19:25:01 +02:00
|
|
|
sink->main_loop = pw_thread_loop_new (sink->loop, "pipewire-sink-loop");
|
2017-07-11 12:24:03 +02:00
|
|
|
sink->core = pw_core_new (sink->loop, NULL);
|
2017-08-04 10:18:54 +02:00
|
|
|
sink->type = pw_core_get_type (sink->core);
|
2016-11-26 19:18:51 +01:00
|
|
|
GST_DEBUG ("loop %p %p", sink->loop, sink->main_loop);
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GstCaps *
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
|
|
|
|
GstStructure *structure;
|
|
|
|
|
|
|
|
|
|
caps = gst_caps_make_writable (caps);
|
|
|
|
|
|
|
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
|
|
2015-06-30 18:14:36 +02:00
|
|
|
if (gst_structure_has_name (structure, "video/x-raw")) {
|
|
|
|
|
gst_structure_fixate_field_nearest_int (structure, "width", 320);
|
|
|
|
|
gst_structure_fixate_field_nearest_int (structure, "height", 240);
|
|
|
|
|
gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
|
|
|
|
|
|
|
|
|
|
if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
|
|
|
|
|
gst_structure_fixate_field_nearest_fraction (structure,
|
|
|
|
|
"pixel-aspect-ratio", 1, 1);
|
|
|
|
|
else
|
|
|
|
|
gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
|
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
|
|
if (gst_structure_has_field (structure, "colorimetry"))
|
|
|
|
|
gst_structure_fixate_field_string (structure, "colorimetry", "bt601");
|
|
|
|
|
if (gst_structure_has_field (structure, "chroma-site"))
|
|
|
|
|
gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
|
|
|
|
|
|
|
|
|
|
if (gst_structure_has_field (structure, "interlace-mode"))
|
|
|
|
|
gst_structure_fixate_field_string (structure, "interlace-mode",
|
|
|
|
|
"progressive");
|
|
|
|
|
else
|
|
|
|
|
gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
|
|
|
|
|
"progressive", NULL);
|
|
|
|
|
} else if (gst_structure_has_name (structure, "audio/x-raw")) {
|
|
|
|
|
gst_structure_fixate_field_string (structure, "format", "S16LE");
|
|
|
|
|
gst_structure_fixate_field_nearest_int (structure, "channels", 2);
|
|
|
|
|
gst_structure_fixate_field_nearest_int (structure, "rate", 44100);
|
|
|
|
|
}
|
2015-05-11 18:23:24 +02:00
|
|
|
|
|
|
|
|
caps = GST_BASE_SINK_CLASS (parent_class)->fixate (bsink, caps);
|
|
|
|
|
|
|
|
|
|
return caps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_set_property (GObject * object, guint prop_id,
|
2015-05-11 18:23:24 +02:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSink *pwsink = GST_PIPEWIRE_SINK (object);
|
2015-07-28 17:05:03 +02:00
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
switch (prop_id) {
|
2016-05-05 13:31:18 +02:00
|
|
|
case PROP_PATH:
|
2017-05-23 19:15:33 +02:00
|
|
|
g_free (pwsink->path);
|
|
|
|
|
pwsink->path = g_value_dup_string (value);
|
2016-05-05 13:31:18 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-07-28 17:05:03 +02:00
|
|
|
case PROP_CLIENT_NAME:
|
2017-05-23 19:15:33 +02:00
|
|
|
g_free (pwsink->client_name);
|
|
|
|
|
pwsink->client_name = g_value_dup_string (value);
|
2015-07-28 17:05:03 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-12-09 13:27:43 +01:00
|
|
|
case PROP_STREAM_PROPERTIES:
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsink->properties)
|
|
|
|
|
gst_structure_free (pwsink->properties);
|
|
|
|
|
pwsink->properties =
|
2015-12-09 13:27:43 +01:00
|
|
|
gst_structure_copy (gst_value_get_structure (value));
|
|
|
|
|
break;
|
|
|
|
|
|
2016-05-06 17:54:23 +02:00
|
|
|
case PROP_MODE:
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsink->mode = g_value_get_enum (value);
|
2016-05-06 17:54:23 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_get_property (GObject * object, guint prop_id,
|
2015-05-11 18:23:24 +02:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSink *pwsink = GST_PIPEWIRE_SINK (object);
|
2015-07-28 17:05:03 +02:00
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
switch (prop_id) {
|
2016-05-05 13:31:18 +02:00
|
|
|
case PROP_PATH:
|
2017-05-23 19:15:33 +02:00
|
|
|
g_value_set_string (value, pwsink->path);
|
2016-05-05 13:31:18 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-07-28 17:05:03 +02:00
|
|
|
case PROP_CLIENT_NAME:
|
2017-05-23 19:15:33 +02:00
|
|
|
g_value_set_string (value, pwsink->client_name);
|
2015-07-28 17:05:03 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-12-09 13:27:43 +01:00
|
|
|
case PROP_STREAM_PROPERTIES:
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_value_set_structure (value, pwsink->properties);
|
2015-12-09 13:27:43 +01:00
|
|
|
break;
|
|
|
|
|
|
2016-05-06 17:54:23 +02:00
|
|
|
case PROP_MODE:
|
2017-05-23 19:15:33 +02:00
|
|
|
g_value_set_enum (value, pwsink->mode);
|
2016-05-06 17:54:23 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-24 16:26:58 +02:00
|
|
|
typedef struct {
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSink *sink;
|
2016-08-24 16:26:58 +02:00
|
|
|
guint id;
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_buffer *buf;
|
|
|
|
|
struct spa_meta_header *header;
|
2016-08-24 16:26:58 +02:00
|
|
|
guint flags;
|
2016-12-20 16:51:57 +01:00
|
|
|
goffset offset;
|
2016-08-24 16:26:58 +02:00
|
|
|
} ProcessMemData;
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
static void
|
2016-08-24 16:26:58 +02:00
|
|
|
process_mem_data_destroy (gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
ProcessMemData *data = user_data;
|
|
|
|
|
|
|
|
|
|
gst_object_unref (data->sink);
|
|
|
|
|
g_slice_free (ProcessMemData, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-08-04 10:18:54 +02:00
|
|
|
on_add_buffer (void *_data,
|
|
|
|
|
uint32_t id)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GstPipeWireSink *pwsink = _data;
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_buffer *b;
|
2016-08-24 16:26:58 +02:00
|
|
|
GstBuffer *buf;
|
2017-03-07 11:56:43 +01:00
|
|
|
uint32_t i;
|
2016-08-24 16:26:58 +02:00
|
|
|
ProcessMemData data;
|
2017-08-04 10:18:54 +02:00
|
|
|
struct pw_type *t = pwsink->type;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_LOG_OBJECT (pwsink, "add buffer");
|
2016-05-05 13:31:18 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (!(b = pw_stream_peek_buffer (pwsink->stream, id))) {
|
2016-08-24 16:26:58 +02:00
|
|
|
g_warning ("failed to peek buffer");
|
2015-12-03 15:59:27 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-24 16:26:58 +02:00
|
|
|
buf = gst_buffer_new ();
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
data.sink = gst_object_ref (pwsink);
|
2016-08-24 16:26:58 +02:00
|
|
|
data.id = id;
|
2016-10-03 19:43:42 +02:00
|
|
|
data.buf = b;
|
2017-08-04 10:18:54 +02:00
|
|
|
data.header = spa_buffer_find_meta (b, t->meta.Header);
|
2016-08-24 16:26:58 +02:00
|
|
|
|
|
|
|
|
for (i = 0; i < b->n_datas; i++) {
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_data *d = &b->datas[i];
|
2016-10-04 15:56:33 +02:00
|
|
|
GstMemory *gmem = NULL;
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2017-08-04 10:18:54 +02:00
|
|
|
if (d->type == t->data.MemFd ||
|
|
|
|
|
d->type == t->data.DmaBuf) {
|
2017-05-23 19:15:33 +02:00
|
|
|
gmem = gst_fd_allocator_alloc (pwsink->allocator, dup (d->fd),
|
2017-04-26 18:42:50 +02:00
|
|
|
d->mapoffset + d->maxsize, GST_FD_MEMORY_FLAG_NONE);
|
|
|
|
|
gst_memory_resize (gmem, d->chunk->offset + d->mapoffset, d->chunk->size);
|
|
|
|
|
data.offset = d->mapoffset;
|
|
|
|
|
}
|
2017-08-04 10:18:54 +02:00
|
|
|
else if (d->type == t->data.MemPtr) {
|
2017-04-26 18:42:50 +02:00
|
|
|
gmem = gst_memory_new_wrapped (0, d->data, d->maxsize, d->chunk->offset,
|
|
|
|
|
d->chunk->size, NULL, NULL);
|
|
|
|
|
data.offset = 0;
|
2016-08-24 16:26:58 +02:00
|
|
|
}
|
2016-10-04 15:56:33 +02:00
|
|
|
if (gmem)
|
|
|
|
|
gst_buffer_append_memory (buf, gmem);
|
2016-08-24 16:26:58 +02:00
|
|
|
}
|
|
|
|
|
data.flags = GST_BUFFER_FLAGS (buf);
|
|
|
|
|
gst_mini_object_set_qdata (GST_MINI_OBJECT_CAST (buf),
|
|
|
|
|
process_mem_data_quark,
|
|
|
|
|
g_slice_dup (ProcessMemData, &data),
|
|
|
|
|
process_mem_data_destroy);
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_pool_add_buffer (pwsink->pool, buf);
|
|
|
|
|
g_hash_table_insert (pwsink->buf_ids, GINT_TO_POINTER (id), buf);
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_signal (pwsink->main_loop, FALSE);
|
2016-08-24 16:26:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-08-04 10:18:54 +02:00
|
|
|
on_remove_buffer (void *data,
|
|
|
|
|
uint32_t id)
|
2016-08-24 16:26:58 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GstPipeWireSink *pwsink = data;
|
2016-08-24 16:26:58 +02:00
|
|
|
GstBuffer *buf;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_LOG_OBJECT (pwsink, "remove buffer");
|
|
|
|
|
buf = g_hash_table_lookup (pwsink->buf_ids, GINT_TO_POINTER (id));
|
2017-05-12 11:00:25 +02:00
|
|
|
if (buf) {
|
|
|
|
|
GST_MINI_OBJECT_CAST (buf)->dispose = NULL;
|
2017-05-23 19:15:33 +02:00
|
|
|
if (!gst_pipewire_pool_remove_buffer (pwsink->pool, buf))
|
2017-05-12 11:00:25 +02:00
|
|
|
gst_buffer_ref (buf);
|
2017-05-23 19:15:33 +02:00
|
|
|
if (g_queue_remove (&pwsink->queue, buf))
|
2017-05-12 11:00:25 +02:00
|
|
|
gst_buffer_unref (buf);
|
2017-05-23 19:15:33 +02:00
|
|
|
g_hash_table_remove (pwsink->buf_ids, GINT_TO_POINTER (id));
|
2017-05-12 11:00:25 +02:00
|
|
|
}
|
2016-08-24 16:26:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-08-04 10:18:54 +02:00
|
|
|
on_new_buffer (void *data,
|
|
|
|
|
uint32_t id)
|
2016-08-24 16:26:58 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GstPipeWireSink *pwsink = data;
|
2016-08-24 16:26:58 +02:00
|
|
|
GstBuffer *buf;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_LOG_OBJECT (pwsink, "got new buffer %u", id);
|
|
|
|
|
if (pwsink->stream == NULL) {
|
|
|
|
|
GST_LOG_OBJECT (pwsink, "no stream");
|
2016-08-24 16:26:58 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2017-05-23 19:15:33 +02:00
|
|
|
buf = g_hash_table_lookup (pwsink->buf_ids, GINT_TO_POINTER (id));
|
2016-08-24 16:26:58 +02:00
|
|
|
|
|
|
|
|
if (buf) {
|
2017-05-26 17:26:08 +02:00
|
|
|
gst_buffer_unref (buf);
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_signal (pwsink->main_loop, FALSE);
|
2016-08-24 16:26:58 +02:00
|
|
|
}
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 16:51:57 +01:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
do_send_buffer (GstPipeWireSink *pwsink)
|
2016-12-20 16:51:57 +01:00
|
|
|
{
|
|
|
|
|
GstBuffer *buffer;
|
|
|
|
|
ProcessMemData *data;
|
|
|
|
|
gboolean res;
|
|
|
|
|
guint i;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
buffer = g_queue_pop_head (&pwsink->queue);
|
2017-04-12 10:40:17 +02:00
|
|
|
if (buffer == NULL) {
|
|
|
|
|
GST_WARNING ("out of buffers");
|
2016-12-20 16:51:57 +01:00
|
|
|
return;
|
2017-04-12 10:40:17 +02:00
|
|
|
}
|
2016-12-20 16:51:57 +01:00
|
|
|
|
|
|
|
|
data = gst_mini_object_get_qdata (GST_MINI_OBJECT_CAST (buffer),
|
|
|
|
|
process_mem_data_quark);
|
|
|
|
|
|
|
|
|
|
if (data->header) {
|
|
|
|
|
data->header->seq = GST_BUFFER_OFFSET (buffer);
|
|
|
|
|
data->header->pts = GST_BUFFER_PTS (buffer);
|
|
|
|
|
data->header->dts_offset = GST_BUFFER_DTS (buffer);
|
|
|
|
|
}
|
|
|
|
|
for (i = 0; i < data->buf->n_datas; i++) {
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_data *d = &data->buf->datas[i];
|
2016-12-20 16:51:57 +01:00
|
|
|
GstMemory *mem = gst_buffer_peek_memory (buffer, i);
|
|
|
|
|
d->chunk->offset = mem->offset - data->offset;
|
|
|
|
|
d->chunk->size = mem->size;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (!(res = pw_stream_send_buffer (pwsink->stream, data->id))) {
|
2016-12-20 16:51:57 +01:00
|
|
|
g_warning ("can't send buffer");
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_signal (pwsink->main_loop, FALSE);
|
2017-01-18 18:29:15 +01:00
|
|
|
} else
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsink->need_ready--;
|
2016-12-20 16:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2017-08-04 10:18:54 +02:00
|
|
|
on_need_buffer (void *data)
|
2016-12-20 16:51:57 +01:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GstPipeWireSink *pwsink = data;
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsink->need_ready++;
|
|
|
|
|
GST_DEBUG ("need buffer %u", pwsink->need_ready);
|
|
|
|
|
do_send_buffer (pwsink);
|
2016-12-20 16:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
static void
|
2017-08-04 10:18:54 +02:00
|
|
|
on_state_changed (void *data, enum pw_stream_state old, enum pw_stream_state state, const char *error)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GstPipeWireSink *pwsink = data;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2015-08-26 17:11:10 +02:00
|
|
|
GST_DEBUG ("got stream state %d", state);
|
2015-06-12 12:10:27 +02:00
|
|
|
|
2015-07-08 12:11:55 +02:00
|
|
|
switch (state) {
|
2017-05-23 19:15:33 +02:00
|
|
|
case PW_STREAM_STATE_UNCONNECTED:
|
|
|
|
|
case PW_STREAM_STATE_CONNECTING:
|
|
|
|
|
case PW_STREAM_STATE_CONFIGURE:
|
|
|
|
|
case PW_STREAM_STATE_READY:
|
|
|
|
|
case PW_STREAM_STATE_PAUSED:
|
|
|
|
|
case PW_STREAM_STATE_STREAMING:
|
2015-07-08 12:11:55 +02:00
|
|
|
break;
|
2017-05-23 19:15:33 +02:00
|
|
|
case PW_STREAM_STATE_ERROR:
|
|
|
|
|
GST_ELEMENT_ERROR (pwsink, RESOURCE, FAILED,
|
2017-08-04 10:18:54 +02:00
|
|
|
("stream error: %s", error), (NULL));
|
2015-07-08 12:11:55 +02:00
|
|
|
break;
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_signal (pwsink->main_loop, FALSE);
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
2016-08-24 16:26:58 +02:00
|
|
|
static void
|
2017-11-07 17:39:31 +01:00
|
|
|
on_format_changed (void *data, struct spa_pod_object *format)
|
2016-08-24 16:26:58 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GstPipeWireSink *pwsink = data;
|
2017-04-04 17:29:07 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (gst_buffer_pool_is_active (GST_BUFFER_POOL_CAST (pwsink->pool)))
|
|
|
|
|
pool_activated (pwsink->pool, pwsink);
|
2016-08-24 16:26:58 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSink *pwsink;
|
2016-08-05 16:39:26 +02:00
|
|
|
GPtrArray *possible;
|
2017-05-23 19:15:33 +02:00
|
|
|
enum pw_stream_state state;
|
2017-08-04 10:18:54 +02:00
|
|
|
const char *error = NULL;
|
2016-04-07 11:39:52 +02:00
|
|
|
gboolean res = FALSE;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsink = GST_PIPEWIRE_SINK (bsink);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-08-04 10:18:54 +02:00
|
|
|
possible = gst_caps_to_format_all (caps, pwsink->type->map);
|
2015-12-09 13:27:43 +01:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsink->main_loop);
|
2017-08-04 10:18:54 +02:00
|
|
|
state = pw_stream_get_state (pwsink->stream, &error);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (state == PW_STREAM_STATE_ERROR)
|
2016-04-07 11:39:52 +02:00
|
|
|
goto start_error;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (state == PW_STREAM_STATE_UNCONNECTED) {
|
|
|
|
|
enum pw_stream_flags flags = 0;
|
2016-05-26 12:25:07 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsink->mode != GST_PIPEWIRE_SINK_MODE_PROVIDE)
|
|
|
|
|
flags |= PW_STREAM_FLAG_AUTOCONNECT;
|
2016-05-26 12:25:07 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_stream_connect (pwsink->stream,
|
|
|
|
|
PW_DIRECTION_OUTPUT,
|
|
|
|
|
pwsink->path,
|
2016-05-26 12:25:07 +02:00
|
|
|
flags,
|
2016-11-24 17:00:42 +01:00
|
|
|
possible->len,
|
2017-11-07 17:39:31 +01:00
|
|
|
(const struct spa_pod_object **) possible->pdata);
|
2016-05-06 13:01:52 +02:00
|
|
|
|
|
|
|
|
while (TRUE) {
|
2017-08-04 10:18:54 +02:00
|
|
|
state = pw_stream_get_state (pwsink->stream, &error);
|
2016-05-06 13:01:52 +02:00
|
|
|
|
2017-07-04 10:50:53 +02:00
|
|
|
if (state == PW_STREAM_STATE_READY)
|
2016-05-06 13:01:52 +02:00
|
|
|
break;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (state == PW_STREAM_STATE_ERROR)
|
2016-05-06 13:01:52 +02:00
|
|
|
goto start_error;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_wait (pwsink->main_loop);
|
2016-05-06 13:01:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-08-24 16:26:58 +02:00
|
|
|
res = TRUE;
|
2016-05-06 13:01:52 +02:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsink->main_loop);
|
2015-05-15 15:58:13 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsink->negotiated = res;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2016-04-07 11:39:52 +02:00
|
|
|
return res;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2016-04-07 11:39:52 +02:00
|
|
|
start_error:
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GST_ERROR ("could not start stream: %s", error);
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsink->main_loop);
|
2016-08-18 12:43:25 +02:00
|
|
|
g_ptr_array_unref (possible);
|
2015-05-11 18:23:24 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSink *pwsink;
|
2017-04-04 17:29:07 +02:00
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
2017-08-04 10:18:54 +02:00
|
|
|
const char *error = NULL;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsink = GST_PIPEWIRE_SINK (bsink);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (!pwsink->negotiated)
|
2015-05-11 18:23:24 +02:00
|
|
|
goto not_negotiated;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsink->main_loop);
|
2017-08-04 10:18:54 +02:00
|
|
|
if (pw_stream_get_state (pwsink->stream, &error) != PW_STREAM_STATE_STREAMING)
|
2016-10-03 19:43:42 +02:00
|
|
|
goto done;
|
2016-05-17 20:14:06 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (buffer->pool != GST_BUFFER_POOL_CAST (pwsink->pool)) {
|
2016-08-24 16:26:58 +02:00
|
|
|
GstBuffer *b = NULL;
|
2017-03-28 19:29:08 +02:00
|
|
|
GstMapInfo info = { 0, };
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (!gst_buffer_pool_is_active (GST_BUFFER_POOL_CAST (pwsink->pool)))
|
|
|
|
|
gst_buffer_pool_set_active (GST_BUFFER_POOL_CAST (pwsink->pool), TRUE);
|
2017-05-12 11:00:25 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if ((res = gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL_CAST (pwsink->pool), &b, NULL)) != GST_FLOW_OK)
|
2017-04-04 17:29:07 +02:00
|
|
|
goto done;
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2017-03-28 19:29:08 +02:00
|
|
|
gst_buffer_map (b, &info, GST_MAP_WRITE);
|
|
|
|
|
gst_buffer_extract (buffer, 0, info.data, info.size);
|
|
|
|
|
gst_buffer_unmap (b, &info);
|
2017-04-04 17:29:07 +02:00
|
|
|
gst_buffer_resize (b, 0, gst_buffer_get_size (buffer));
|
2016-08-24 16:26:58 +02:00
|
|
|
buffer = b;
|
2017-05-26 17:26:08 +02:00
|
|
|
} else {
|
2017-04-04 17:29:07 +02:00
|
|
|
gst_buffer_ref (buffer);
|
2017-05-26 17:26:08 +02:00
|
|
|
}
|
2016-08-03 21:33:57 +02:00
|
|
|
|
2017-04-12 10:40:17 +02:00
|
|
|
GST_DEBUG ("push buffer in queue");
|
2017-05-23 19:15:33 +02:00
|
|
|
g_queue_push_tail (&pwsink->queue, buffer);
|
2016-10-03 19:43:42 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsink->need_ready && pwsink->mode == GST_PIPEWIRE_SINK_MODE_PROVIDE)
|
|
|
|
|
do_send_buffer (pwsink);
|
2016-10-28 16:56:33 +02:00
|
|
|
|
2016-10-03 19:43:42 +02:00
|
|
|
done:
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsink->main_loop);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-04-04 17:29:07 +02:00
|
|
|
return res;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
|
|
|
|
not_negotiated:
|
|
|
|
|
{
|
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-07 11:39:52 +02:00
|
|
|
static gboolean
|
|
|
|
|
copy_properties (GQuark field_id,
|
|
|
|
|
const GValue *value,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
struct pw_properties *properties = user_data;
|
2016-04-07 11:39:52 +02:00
|
|
|
|
|
|
|
|
if (G_VALUE_HOLDS_STRING (value))
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_properties_set (properties,
|
|
|
|
|
g_quark_to_string (field_id),
|
|
|
|
|
g_value_get_string (value));
|
2016-04-07 11:39:52 +02:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 16:49:13 +02:00
|
|
|
static const struct pw_stream_events stream_events = {
|
|
|
|
|
PW_VERSION_STREAM_EVENTS,
|
2017-08-04 10:18:54 +02:00
|
|
|
.state_changed = on_state_changed,
|
|
|
|
|
.format_changed = on_format_changed,
|
|
|
|
|
.add_buffer = on_add_buffer,
|
|
|
|
|
.remove_buffer = on_remove_buffer,
|
|
|
|
|
.new_buffer = on_new_buffer,
|
|
|
|
|
.need_buffer = on_need_buffer,
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_start (GstBaseSink * basesink)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSink *pwsink = GST_PIPEWIRE_SINK (basesink);
|
|
|
|
|
struct pw_properties *props;
|
2016-04-07 11:39:52 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsink->negotiated = FALSE;
|
2016-04-07 11:39:52 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsink->properties) {
|
|
|
|
|
props = pw_properties_new (NULL, NULL);
|
|
|
|
|
gst_structure_foreach (pwsink->properties, copy_properties, props);
|
2016-04-07 11:39:52 +02:00
|
|
|
} else {
|
|
|
|
|
props = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsink->main_loop);
|
2017-07-11 12:24:03 +02:00
|
|
|
pwsink->stream = pw_stream_new (pwsink->remote, pwsink->client_name, props);
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsink->pool->stream = pwsink->stream;
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2017-08-04 16:49:13 +02:00
|
|
|
pw_stream_add_listener(pwsink->stream,
|
|
|
|
|
&pwsink->stream_listener,
|
|
|
|
|
&stream_events,
|
|
|
|
|
pwsink);
|
2017-08-04 10:18:54 +02:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsink->main_loop);
|
2016-04-07 11:39:52 +02:00
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_stop (GstBaseSink * basesink)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSink *pwsink = GST_PIPEWIRE_SINK (basesink);
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsink->main_loop);
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsink->stream) {
|
|
|
|
|
pw_stream_disconnect (pwsink->stream);
|
|
|
|
|
pw_stream_destroy (pwsink->stream);
|
|
|
|
|
pwsink->stream = NULL;
|
|
|
|
|
pwsink->pool->stream = NULL;
|
2016-04-07 11:39:52 +02:00
|
|
|
}
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsink->main_loop);
|
2015-05-14 17:46:12 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsink->negotiated = FALSE;
|
2015-05-14 17:46:12 +02:00
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-08-04 10:18:54 +02:00
|
|
|
on_remote_state_changed (void *data, enum pw_remote_state old, enum pw_remote_state state, const char *error)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GstPipeWireSink *pwsink = data;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
GST_DEBUG ("got remote state %d", state);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2015-07-08 12:11:55 +02:00
|
|
|
switch (state) {
|
2017-07-11 12:24:03 +02:00
|
|
|
case PW_REMOTE_STATE_UNCONNECTED:
|
|
|
|
|
case PW_REMOTE_STATE_CONNECTING:
|
|
|
|
|
case PW_REMOTE_STATE_CONNECTED:
|
2015-07-08 12:11:55 +02:00
|
|
|
break;
|
2017-07-11 12:24:03 +02:00
|
|
|
case PW_REMOTE_STATE_ERROR:
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_ELEMENT_ERROR (pwsink, RESOURCE, FAILED,
|
2017-08-04 10:18:54 +02:00
|
|
|
("remote error: %s", error), (NULL));
|
2015-07-08 12:11:55 +02:00
|
|
|
break;
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_signal (pwsink->main_loop, FALSE);
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
2017-08-04 16:49:13 +02:00
|
|
|
static const struct pw_remote_events remote_events = {
|
|
|
|
|
PW_VERSION_REMOTE_EVENTS,
|
2017-08-04 10:18:54 +02:00
|
|
|
.state_changed = on_remote_state_changed,
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_open (GstPipeWireSink * pwsink)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
const char *error = NULL;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
if (pw_thread_loop_start (pwsink->main_loop) != SPA_RESULT_OK)
|
2015-07-08 12:11:55 +02:00
|
|
|
goto mainloop_error;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsink->main_loop);
|
2017-09-15 14:51:39 +02:00
|
|
|
pwsink->remote = pw_remote_new (pwsink->core, NULL, 0);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-08-04 16:49:13 +02:00
|
|
|
pw_remote_add_listener (pwsink->remote,
|
|
|
|
|
&pwsink->remote_listener,
|
|
|
|
|
&remote_events, pwsink);
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
pw_remote_connect (pwsink->remote);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
|
|
|
|
while (TRUE) {
|
2017-08-04 10:18:54 +02:00
|
|
|
enum pw_remote_state state = pw_remote_get_state (pwsink->remote, &error);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
if (state == PW_REMOTE_STATE_CONNECTED)
|
2015-05-11 18:23:24 +02:00
|
|
|
break;
|
|
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
if (state == PW_REMOTE_STATE_ERROR)
|
2015-05-11 18:23:24 +02:00
|
|
|
goto connect_error;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_wait (pwsink->main_loop);
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsink->main_loop);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
/* ERRORS */
|
2015-07-08 12:11:55 +02:00
|
|
|
mainloop_error:
|
|
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_ELEMENT_ERROR (pwsink, RESOURCE, FAILED,
|
2016-11-24 17:00:42 +01:00
|
|
|
("Failed to start mainloop"), (NULL));
|
2015-07-08 12:11:55 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
2015-05-11 18:23:24 +02:00
|
|
|
connect_error:
|
|
|
|
|
{
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsink->main_loop);
|
2015-05-11 18:23:24 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-27 18:16:52 +02:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_close (GstPipeWireSink * pwsink)
|
2015-05-27 18:16:52 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
const char *error = NULL;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsink->main_loop);
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsink->stream) {
|
|
|
|
|
pw_stream_disconnect (pwsink->stream);
|
2015-05-27 18:16:52 +02:00
|
|
|
}
|
2017-07-11 12:24:03 +02:00
|
|
|
if (pwsink->remote) {
|
|
|
|
|
pw_remote_disconnect (pwsink->remote);
|
2015-05-27 18:16:52 +02:00
|
|
|
|
|
|
|
|
while (TRUE) {
|
2017-08-04 10:18:54 +02:00
|
|
|
enum pw_remote_state state = pw_remote_get_state (pwsink->remote, &error);
|
2015-05-27 18:16:52 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
if (state == PW_REMOTE_STATE_UNCONNECTED)
|
2015-05-27 18:16:52 +02:00
|
|
|
break;
|
|
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
if (state == PW_REMOTE_STATE_ERROR)
|
2015-05-27 18:16:52 +02:00
|
|
|
break;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_wait (pwsink->main_loop);
|
2015-05-27 18:16:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsink->main_loop);
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_stop (pwsink->main_loop);
|
2016-11-26 19:18:51 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsink->stream) {
|
|
|
|
|
pw_stream_destroy (pwsink->stream);
|
|
|
|
|
pwsink->stream = NULL;
|
2016-11-26 19:18:51 +01:00
|
|
|
}
|
|
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
if (pwsink->remote) {
|
|
|
|
|
pw_remote_destroy (pwsink->remote);
|
|
|
|
|
pwsink->remote = NULL;
|
2016-11-26 19:18:51 +01:00
|
|
|
}
|
2015-05-27 18:16:52 +02:00
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
static GstStateChangeReturn
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_change_state (GstElement * element, GstStateChange transition)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
|
|
|
|
GstStateChangeReturn ret;
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSink *this = GST_PIPEWIRE_SINK_CAST (element);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
2017-05-23 19:15:33 +02:00
|
|
|
if (!gst_pipewire_sink_open (this))
|
2015-07-08 12:11:55 +02:00
|
|
|
goto open_failed;
|
2015-05-11 18:23:24 +02:00
|
|
|
break;
|
2015-06-05 18:21:18 +02:00
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
|
break;
|
2015-05-11 18:23:24 +02:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
|
/* uncork and start recording */
|
|
|
|
|
break;
|
|
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
|
/* stop recording ASAP by corking */
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
|
break;
|
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
|
break;
|
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_sink_close (this);
|
2015-05-11 18:23:24 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
2015-07-08 12:11:55 +02:00
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
|
open_failed:
|
|
|
|
|
{
|
|
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
|
|
|
|
}
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|