2023-02-08 18:12:00 +01:00
|
|
|
/* GStreamer */
|
|
|
|
|
/* SPDX-FileCopyrightText: Copyright © 2018 Wim Taymans */
|
|
|
|
|
/* SPDX-License-Identifier: MIT */
|
2015-05-11 18:23:24 +02:00
|
|
|
|
|
|
|
|
/**
|
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>
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-10 17:17:34 +01:00
|
|
|
#define PW_ENABLE_DEPRECATED
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
#include "config.h"
|
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>
|
2019-09-10 11:05:38 +02:00
|
|
|
#include <unistd.h>
|
2015-05-11 18:23:24 +02:00
|
|
|
#include <sys/socket.h>
|
2016-07-28 21:19:20 +02:00
|
|
|
|
2019-10-25 15:01:02 +02:00
|
|
|
#include <spa/pod/builder.h>
|
|
|
|
|
#include <spa/utils/result.h>
|
2024-11-26 17:45:41 +01:00
|
|
|
#include <spa/utils/dll.h>
|
2019-10-25 15:01:02 +02:00
|
|
|
|
2020-07-31 11:44:46 +02:00
|
|
|
#include <gst/video/video.h>
|
|
|
|
|
|
2024-03-12 17:36:50 +01:00
|
|
|
#include "gstpipewireclock.h"
|
2017-05-23 19:15:33 +02:00
|
|
|
#include "gstpipewireformat.h"
|
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
|
2024-12-03 11:54:52 +01:00
|
|
|
#define DEFAULT_PROP_SLAVE_METHOD GST_PIPEWIRE_SINK_SLAVE_METHOD_NONE
|
2025-01-24 21:07:55 +05:30
|
|
|
#define DEFAULT_PROP_USE_BUFFERPOOL USE_BUFFERPOOL_AUTO
|
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,
|
2022-03-06 14:10:55 +02:00
|
|
|
PROP_TARGET_OBJECT,
|
2015-12-09 13:27:43 +01:00
|
|
|
PROP_CLIENT_NAME,
|
2022-06-03 13:00:52 +02:00
|
|
|
PROP_CLIENT_PROPERTIES,
|
2016-05-06 17:54:23 +02:00
|
|
|
PROP_STREAM_PROPERTIES,
|
2018-01-19 17:54:47 +01:00
|
|
|
PROP_MODE,
|
2024-12-03 11:54:52 +01:00
|
|
|
PROP_FD,
|
2025-01-24 21:07:55 +05:30
|
|
|
PROP_SLAVE_METHOD,
|
|
|
|
|
PROP_USE_BUFFERPOOL,
|
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
|
|
|
{
|
2021-04-05 10:28:26 +02:00
|
|
|
static gsize mode_type = 0;
|
2016-05-06 17:54:23 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 11:54:52 +01:00
|
|
|
GType
|
|
|
|
|
gst_pipewire_sink_slave_method_get_type (void)
|
|
|
|
|
{
|
|
|
|
|
static gsize method_type = 0;
|
|
|
|
|
static const GEnumValue method[] = {
|
|
|
|
|
{GST_PIPEWIRE_SINK_SLAVE_METHOD_NONE, "GST_PIPEWIRE_SINK_SLAVE_METHOD_NONE", "none"},
|
|
|
|
|
{GST_PIPEWIRE_SINK_SLAVE_METHOD_RESAMPLE, "GST_PIPEWIRE_SINK_SLAVE_METHOD_RESAMPLE", "resample"},
|
|
|
|
|
{0, NULL, NULL},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (g_once_init_enter (&method_type)) {
|
|
|
|
|
GType tmp =
|
|
|
|
|
g_enum_register_static ("GstPipeWireSinkSlaveMethod", method);
|
|
|
|
|
g_once_init_leave (&method_type, tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (GType) method_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);
|
|
|
|
|
|
2024-12-27 10:39:36 +05:30
|
|
|
static gboolean gst_pipewire_sink_event (GstBaseSink *sink, GstEvent *event);
|
|
|
|
|
|
2024-03-12 17:36:50 +01:00
|
|
|
static GstClock *
|
|
|
|
|
gst_pipewire_sink_provide_clock (GstElement * elem)
|
|
|
|
|
{
|
|
|
|
|
GstPipeWireSink *pwsink = GST_PIPEWIRE_SINK (elem);
|
|
|
|
|
GstClock *clock;
|
|
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (pwsink);
|
|
|
|
|
if (!GST_OBJECT_FLAG_IS_SET (pwsink, GST_ELEMENT_FLAG_PROVIDE_CLOCK))
|
|
|
|
|
goto clock_disabled;
|
|
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
if (pwsink->stream->clock)
|
|
|
|
|
clock = GST_CLOCK_CAST (gst_object_ref (pwsink->stream->clock));
|
2024-03-12 17:36:50 +01:00
|
|
|
else
|
|
|
|
|
clock = NULL;
|
|
|
|
|
GST_OBJECT_UNLOCK (pwsink);
|
|
|
|
|
|
|
|
|
|
return clock;
|
|
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
|
clock_disabled:
|
|
|
|
|
{
|
|
|
|
|
GST_DEBUG_OBJECT (pwsink, "clock provide disabled");
|
|
|
|
|
GST_OBJECT_UNLOCK (pwsink);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_clear_object (&pwsink->stream);
|
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
|
|
|
|
2025-01-24 21:07:55 +05:30
|
|
|
if (pwsink->use_bufferpool != USE_BUFFERPOOL_NO)
|
2025-03-26 11:55:13 -04:00
|
|
|
gst_query_add_allocation_pool (query, GST_BUFFER_POOL_CAST (pwsink->stream->pool), 0,
|
|
|
|
|
PIPEWIRE_POOL_MIN_BUFFERS, PIPEWIRE_POOL_MAX_BUFFERS);
|
2025-01-24 21:07:55 +05:30
|
|
|
|
2023-02-03 13:02:49 +08:00
|
|
|
gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
|
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 |
|
2022-03-06 14:10:55 +02:00
|
|
|
G_PARAM_STATIC_STRINGS |
|
|
|
|
|
G_PARAM_DEPRECATED));
|
|
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_TARGET_OBJECT,
|
|
|
|
|
g_param_spec_string ("target-object",
|
|
|
|
|
"Target object",
|
|
|
|
|
"The sink name/serial to connect to (NULL = default)",
|
|
|
|
|
NULL,
|
|
|
|
|
G_PARAM_READWRITE |
|
2016-05-05 13:31:18 +02:00
|
|
|
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));
|
|
|
|
|
|
2022-06-03 13:00:52 +02:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_CLIENT_PROPERTIES,
|
|
|
|
|
g_param_spec_boxed ("client-properties",
|
|
|
|
|
"Client properties",
|
|
|
|
|
"List of PipeWire client properties",
|
|
|
|
|
GST_TYPE_STRUCTURE,
|
|
|
|
|
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 |
|
2025-03-24 22:11:36 -04:00
|
|
|
G_PARAM_STATIC_STRINGS |
|
|
|
|
|
GST_PARAM_MUTABLE_READY));
|
2016-05-06 17:54:23 +02:00
|
|
|
|
2018-01-19 17:54:47 +01:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_FD,
|
|
|
|
|
g_param_spec_int ("fd",
|
|
|
|
|
"Fd",
|
|
|
|
|
"The fd to connect with",
|
2018-01-22 17:05:13 +08:00
|
|
|
-1, G_MAXINT, -1,
|
2018-01-19 17:54:47 +01:00
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
2024-12-03 11:54:52 +01:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_SLAVE_METHOD,
|
|
|
|
|
g_param_spec_enum ("slave-method",
|
|
|
|
|
"Slave Method",
|
|
|
|
|
"Algorithm used to match the rate of the masterclock",
|
|
|
|
|
GST_TYPE_PIPEWIRE_SINK_SLAVE_METHOD,
|
|
|
|
|
DEFAULT_PROP_SLAVE_METHOD,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
2025-01-24 21:07:55 +05:30
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_USE_BUFFERPOOL,
|
|
|
|
|
g_param_spec_boolean ("use-bufferpool",
|
|
|
|
|
"Use bufferpool",
|
|
|
|
|
"Use bufferpool (default: true for video, false for audio)",
|
|
|
|
|
DEFAULT_PROP_USE_BUFFERPOOL,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2024-12-03 11:54:52 +01:00
|
|
|
|
2024-03-12 17:36:50 +01:00
|
|
|
gstelement_class->provide_clock = gst_pipewire_sink_provide_clock;
|
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,
|
2024-03-06 19:59:32 +01:00
|
|
|
"PipeWire sink", "Sink/Audio/Video",
|
|
|
|
|
"Send audio/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->render = gst_pipewire_sink_render;
|
2024-12-27 10:39:36 +05:30
|
|
|
gstbasesink_class->event = gst_pipewire_sink_event;
|
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");
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 11:00:25 +02:00
|
|
|
static void
|
2024-06-19 17:40:55 +02:00
|
|
|
gst_pipewire_sink_update_params (GstPipeWireSink *sink)
|
2017-05-12 11:00:25 +02:00
|
|
|
{
|
2024-06-19 17:40:55 +02:00
|
|
|
GstPipeWirePool *pool = sink->stream->pool;
|
2017-05-12 11:00:25 +02:00
|
|
|
GstStructure *config;
|
|
|
|
|
GstCaps *caps;
|
|
|
|
|
guint size;
|
|
|
|
|
guint min_buffers;
|
|
|
|
|
guint max_buffers;
|
2020-07-31 11:44:46 +02:00
|
|
|
const struct spa_pod *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];
|
2019-01-22 17:38:23 +01:00
|
|
|
struct spa_pod_frame f;
|
2025-03-28 09:48:35 -04:00
|
|
|
guint n_params = 0;
|
2017-05-12 11:00:25 +02:00
|
|
|
|
|
|
|
|
config = gst_buffer_pool_get_config (GST_BUFFER_POOL (pool));
|
|
|
|
|
gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers, &max_buffers);
|
|
|
|
|
|
2025-03-26 11:55:13 -04:00
|
|
|
/* We cannot dynamically grow the pool */
|
|
|
|
|
if (max_buffers == 0) {
|
|
|
|
|
GST_WARNING_OBJECT (sink, "cannot support unlimited buffers in pool");
|
|
|
|
|
max_buffers = PIPEWIRE_POOL_MAX_BUFFERS;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-12 11:00:25 +02:00
|
|
|
spa_pod_builder_init (&b, buffer, sizeof (buffer));
|
2019-01-22 17:38:23 +01:00
|
|
|
spa_pod_builder_push_object (&b, &f, SPA_TYPE_OBJECT_ParamBuffers, SPA_PARAM_Buffers);
|
2023-11-09 10:22:10 +01:00
|
|
|
spa_pod_builder_add (&b,
|
|
|
|
|
SPA_PARAM_BUFFERS_size, SPA_POD_CHOICE_RANGE_Int(size, size, INT32_MAX),
|
|
|
|
|
0);
|
2025-03-14 16:07:28 +05:30
|
|
|
if (sink->is_video) {
|
|
|
|
|
/* MUST have n_datas == n_planes */
|
|
|
|
|
spa_pod_builder_add (&b,
|
|
|
|
|
SPA_PARAM_BUFFERS_blocks,
|
|
|
|
|
SPA_POD_Int(GST_VIDEO_INFO_N_PLANES (&pool->video_info)), 0);
|
|
|
|
|
}
|
2017-05-22 13:06:18 +02:00
|
|
|
|
2019-01-16 11:05:12 +01:00
|
|
|
spa_pod_builder_add (&b,
|
|
|
|
|
SPA_PARAM_BUFFERS_stride, SPA_POD_CHOICE_RANGE_Int(0, 0, INT32_MAX),
|
2025-03-26 11:55:13 -04:00
|
|
|
/* At this stage, we will request as many buffers as we _might_ need as
|
|
|
|
|
* the default, since we can't grow the pool once this is set */
|
2020-08-06 12:58:13 +02:00
|
|
|
SPA_PARAM_BUFFERS_buffers, SPA_POD_CHOICE_RANGE_Int(
|
2025-03-26 11:55:13 -04:00
|
|
|
max_buffers, min_buffers, max_buffers),
|
2025-03-20 21:13:03 -04:00
|
|
|
SPA_PARAM_BUFFERS_dataType, SPA_POD_CHOICE_FLAGS_Int(1<<SPA_DATA_MemFd),
|
2018-09-05 16:41:07 +02:00
|
|
|
0);
|
2025-03-28 09:48:35 -04:00
|
|
|
port_params[n_params++] = spa_pod_builder_pop (&b, &f);
|
2017-05-22 13:06:18 +02:00
|
|
|
|
2025-03-28 09:48:35 -04:00
|
|
|
port_params[n_params++] = spa_pod_builder_add_object (&b,
|
2018-08-27 15:03:11 +02:00
|
|
|
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
|
2023-10-27 16:29:05 +02:00
|
|
|
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_Header),
|
2019-01-16 11:05:12 +01:00
|
|
|
SPA_PARAM_META_size, SPA_POD_Int(sizeof (struct spa_meta_header)));
|
2017-09-21 18:57:41 +02:00
|
|
|
|
2025-03-28 09:48:35 -04:00
|
|
|
if (sink->is_video) {
|
|
|
|
|
port_params[n_params++] = spa_pod_builder_add_object (&b,
|
|
|
|
|
SPA_TYPE_OBJECT_ParamMeta, SPA_PARAM_Meta,
|
|
|
|
|
SPA_PARAM_META_type, SPA_POD_Id(SPA_META_VideoCrop),
|
|
|
|
|
SPA_PARAM_META_size, SPA_POD_Int(sizeof (struct spa_meta_region)));
|
|
|
|
|
}
|
2017-05-12 11:00:25 +02:00
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_thread_loop_lock (sink->stream->core->loop);
|
2025-03-28 09:48:35 -04:00
|
|
|
pw_stream_update_params (sink->stream->pwstream, port_params, n_params);
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_thread_loop_unlock (sink->stream->core->loop);
|
2017-05-12 11:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-19 17:40:55 +02:00
|
|
|
static void
|
|
|
|
|
pool_activated (GstPipeWirePool *pool, GstPipeWireSink *sink)
|
|
|
|
|
{
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "activated");
|
2024-06-19 17:40:56 +02:00
|
|
|
g_cond_signal (&sink->stream->pool->cond);
|
2024-06-19 17:40:55 +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
|
|
|
{
|
2024-05-29 17:56:38 +03:00
|
|
|
sink->stream = gst_pipewire_stream_new (GST_ELEMENT (sink));
|
|
|
|
|
|
2016-05-06 17:54:23 +02:00
|
|
|
sink->mode = DEFAULT_PROP_MODE;
|
2025-01-24 21:07:55 +05:30
|
|
|
sink->use_bufferpool = DEFAULT_PROP_USE_BUFFERPOOL;
|
2025-03-04 22:28:14 +05:30
|
|
|
sink->is_video = false;
|
2015-12-03 15:59:27 +01:00
|
|
|
|
2024-03-12 17:36:50 +01:00
|
|
|
GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
|
|
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
g_signal_connect (sink->stream->pool, "activated", G_CALLBACK (pool_activated), sink);
|
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;
|
2025-03-04 22:28:14 +05:30
|
|
|
GstPipeWireSink *pwsink = GST_PIPEWIRE_SINK(bsink);
|
2015-05-11 18:23:24 +02:00
|
|
|
|
|
|
|
|
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")) {
|
2025-03-04 22:28:14 +05:30
|
|
|
pwsink->is_video = true;
|
2015-06-30 18:14:36 +02:00
|
|
|
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);
|
2022-11-02 13:20:28 +05:30
|
|
|
gst_structure_fixate_field_nearest_int (structure, "rate", 44100);
|
|
|
|
|
} else if (gst_structure_has_name (structure, "audio/mpeg")) {
|
|
|
|
|
gst_structure_fixate_field_string (structure, "format", "Encoded");
|
|
|
|
|
gst_structure_fixate_field_nearest_int (structure, "channels", 2);
|
|
|
|
|
gst_structure_fixate_field_nearest_int (structure, "rate", 44100);
|
|
|
|
|
} else if (gst_structure_has_name (structure, "audio/x-flac")) {
|
|
|
|
|
gst_structure_fixate_field_string (structure, "format", "Encoded");
|
|
|
|
|
gst_structure_fixate_field_nearest_int (structure, "channels", 2);
|
2015-06-30 18:14:36 +02:00
|
|
|
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:
|
2024-05-29 17:56:38 +03:00
|
|
|
g_free (pwsink->stream->path);
|
|
|
|
|
pwsink->stream->path = g_value_dup_string (value);
|
2016-05-05 13:31:18 +02:00
|
|
|
break;
|
|
|
|
|
|
2022-03-06 14:10:55 +02:00
|
|
|
case PROP_TARGET_OBJECT:
|
2024-05-29 17:56:38 +03:00
|
|
|
g_free (pwsink->stream->target_object);
|
|
|
|
|
pwsink->stream->target_object = g_value_dup_string (value);
|
2022-03-06 14:10:55 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-07-28 17:05:03 +02:00
|
|
|
case PROP_CLIENT_NAME:
|
2024-05-29 17:56:38 +03:00
|
|
|
g_free (pwsink->stream->client_name);
|
|
|
|
|
pwsink->stream->client_name = g_value_dup_string (value);
|
2015-07-28 17:05:03 +02:00
|
|
|
break;
|
|
|
|
|
|
2022-06-03 13:00:52 +02:00
|
|
|
case PROP_CLIENT_PROPERTIES:
|
2024-05-29 17:56:38 +03:00
|
|
|
if (pwsink->stream->client_properties)
|
|
|
|
|
gst_structure_free (pwsink->stream->client_properties);
|
|
|
|
|
pwsink->stream->client_properties =
|
2022-06-03 13:00:52 +02:00
|
|
|
gst_structure_copy (gst_value_get_structure (value));
|
|
|
|
|
break;
|
|
|
|
|
|
2015-12-09 13:27:43 +01:00
|
|
|
case PROP_STREAM_PROPERTIES:
|
2024-05-29 17:56:38 +03:00
|
|
|
if (pwsink->stream->stream_properties)
|
|
|
|
|
gst_structure_free (pwsink->stream->stream_properties);
|
|
|
|
|
pwsink->stream->stream_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;
|
|
|
|
|
|
2018-01-19 17:54:47 +01:00
|
|
|
case PROP_FD:
|
2024-05-29 17:56:38 +03:00
|
|
|
pwsink->stream->fd = g_value_get_int (value);
|
2018-01-19 17:54:47 +01:00
|
|
|
break;
|
|
|
|
|
|
2024-12-03 11:54:52 +01:00
|
|
|
case PROP_SLAVE_METHOD:
|
|
|
|
|
pwsink->slave_method = g_value_get_enum (value);
|
|
|
|
|
break;
|
|
|
|
|
|
2025-01-24 21:07:55 +05:30
|
|
|
case PROP_USE_BUFFERPOOL:
|
|
|
|
|
if(g_value_get_boolean (value))
|
|
|
|
|
pwsink->use_bufferpool = USE_BUFFERPOOL_YES;
|
|
|
|
|
else
|
|
|
|
|
pwsink->use_bufferpool = USE_BUFFERPOOL_NO;
|
|
|
|
|
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:
|
2024-05-29 17:56:38 +03:00
|
|
|
g_value_set_string (value, pwsink->stream->path);
|
2016-05-05 13:31:18 +02:00
|
|
|
break;
|
|
|
|
|
|
2022-03-06 14:10:55 +02:00
|
|
|
case PROP_TARGET_OBJECT:
|
2024-05-29 17:56:38 +03:00
|
|
|
g_value_set_string (value, pwsink->stream->target_object);
|
2022-03-06 14:10:55 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-07-28 17:05:03 +02:00
|
|
|
case PROP_CLIENT_NAME:
|
2024-05-29 17:56:38 +03:00
|
|
|
g_value_set_string (value, pwsink->stream->client_name);
|
2015-07-28 17:05:03 +02:00
|
|
|
break;
|
|
|
|
|
|
2022-06-03 13:00:52 +02:00
|
|
|
case PROP_CLIENT_PROPERTIES:
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_value_set_structure (value, pwsink->stream->client_properties);
|
2022-06-03 13:00:52 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-12-09 13:27:43 +01:00
|
|
|
case PROP_STREAM_PROPERTIES:
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_value_set_structure (value, pwsink->stream->stream_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;
|
|
|
|
|
|
2018-01-19 17:54:47 +01:00
|
|
|
case PROP_FD:
|
2024-05-29 17:56:38 +03:00
|
|
|
g_value_set_int (value, pwsink->stream->fd);
|
2018-01-19 17:54:47 +01:00
|
|
|
break;
|
|
|
|
|
|
2024-12-03 11:54:52 +01:00
|
|
|
case PROP_SLAVE_METHOD:
|
|
|
|
|
g_value_set_enum (value, pwsink->slave_method);
|
|
|
|
|
break;
|
|
|
|
|
|
2025-01-24 21:07:55 +05:30
|
|
|
case PROP_USE_BUFFERPOOL:
|
|
|
|
|
g_value_set_boolean (value, !!pwsink->use_bufferpool);
|
|
|
|
|
break;
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-03 11:54:52 +01:00
|
|
|
static void rate_match_resample(GstPipeWireSink *pwsink)
|
|
|
|
|
{
|
|
|
|
|
GstPipeWireStream *stream = pwsink->stream;
|
|
|
|
|
double err, corr;
|
|
|
|
|
struct pw_time ts;
|
|
|
|
|
guint64 queued, now, elapsed, target;
|
|
|
|
|
|
|
|
|
|
if (!pwsink->rate_match)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
pw_stream_get_time_n(stream->pwstream, &ts, sizeof(ts));
|
|
|
|
|
now = pw_stream_get_nsec(stream->pwstream);
|
|
|
|
|
if (ts.now != 0)
|
|
|
|
|
elapsed = gst_util_uint64_scale_int (now - ts.now, ts.rate.denom, GST_SECOND * ts.rate.num);
|
|
|
|
|
else
|
|
|
|
|
elapsed = 0;
|
|
|
|
|
|
|
|
|
|
queued = ts.queued - ts.size;
|
|
|
|
|
target = elapsed;
|
|
|
|
|
err = ((gint64)queued - ((gint64)target));
|
|
|
|
|
|
|
|
|
|
corr = spa_dll_update(&stream->dll, SPA_CLAMPD(err, -128.0, 128.0));
|
|
|
|
|
|
|
|
|
|
stream->err_wdw = (double)ts.rate.denom/ts.size;
|
|
|
|
|
|
|
|
|
|
double avg = (stream->err_avg * stream->err_wdw + (err - stream->err_avg)) / (stream->err_wdw + 1.0);
|
|
|
|
|
stream->err_var = (stream->err_var * stream->err_wdw +
|
|
|
|
|
(err - stream->err_avg) * (err - avg)) / (stream->err_wdw + 1.0);
|
|
|
|
|
stream->err_avg = avg;
|
|
|
|
|
|
|
|
|
|
if (stream->last_ts == 0 || stream->last_ts + SPA_NSEC_PER_SEC < now) {
|
|
|
|
|
double bw;
|
|
|
|
|
|
|
|
|
|
stream->last_ts = now;
|
|
|
|
|
|
|
|
|
|
if (stream->err_var == 0.0)
|
|
|
|
|
bw = 0.0;
|
|
|
|
|
else
|
|
|
|
|
bw = fabs(stream->err_avg) / sqrt(fabs(stream->err_var));
|
|
|
|
|
|
|
|
|
|
spa_dll_set_bw(&stream->dll, SPA_CLAMPD(bw, 0.001, SPA_DLL_BW_MAX), ts.size, ts.rate.denom);
|
|
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (pwsink, "q:%"PRIi64"/%"PRIi64" e:%"PRIu64" err:%+03f corr:%f %f %f %f",
|
|
|
|
|
ts.queued, ts.size, elapsed, err, corr,
|
|
|
|
|
stream->err_avg, stream->err_var, stream->dll.bw);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pw_stream_set_rate (stream->pwstream, corr);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-11 18:23:24 +02:00
|
|
|
static void
|
2018-03-22 16:40:27 +01:00
|
|
|
on_add_buffer (void *_data, struct pw_buffer *b)
|
2015-05-11 18:23:24 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GstPipeWireSink *pwsink = _data;
|
2024-05-28 16:49:16 +03:00
|
|
|
GST_DEBUG_OBJECT (pwsink, "add pw_buffer %p", b);
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_pipewire_pool_wrap_buffer (pwsink->stream->pool, b);
|
2016-08-24 16:26:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2018-03-22 16:40:27 +01:00
|
|
|
on_remove_buffer (void *_data, struct pw_buffer *b)
|
2016-08-24 16:26:58 +02:00
|
|
|
{
|
2018-03-22 16:40:27 +01:00
|
|
|
GstPipeWireSink *pwsink = _data;
|
2025-03-20 21:13:03 -04:00
|
|
|
|
2024-05-28 16:47:15 +03:00
|
|
|
GST_DEBUG_OBJECT (pwsink, "remove pw_buffer %p", b);
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_pipewire_pool_remove_buffer (pwsink->stream->pool, b);
|
2024-05-28 17:34:45 +03:00
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
if (!gst_pipewire_pool_has_buffers (pwsink->stream->pool) &&
|
|
|
|
|
!GST_BUFFER_POOL_IS_FLUSHING (GST_BUFFER_POOL_CAST (pwsink->stream->pool))) {
|
2025-03-20 21:13:03 -04:00
|
|
|
if (pwsink->mode != GST_PIPEWIRE_SINK_MODE_PROVIDE) {
|
|
|
|
|
GST_ELEMENT_ERROR (pwsink, RESOURCE, NOT_FOUND,
|
|
|
|
|
("all buffers have been removed"),
|
|
|
|
|
("PipeWire link to remote node was destroyed"));
|
|
|
|
|
}
|
2024-05-28 17:34:45 +03:00
|
|
|
}
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
2016-12-20 16:51:57 +01:00
|
|
|
static void
|
2020-04-23 15:56:12 +02:00
|
|
|
do_send_buffer (GstPipeWireSink *pwsink, GstBuffer *buffer)
|
2016-12-20 16:51:57 +01:00
|
|
|
{
|
2018-03-22 16:40:27 +01:00
|
|
|
GstPipeWirePoolData *data;
|
2024-11-26 17:45:41 +01:00
|
|
|
GstPipeWireStream *stream = pwsink->stream;
|
2016-12-20 16:51:57 +01:00
|
|
|
gboolean res;
|
|
|
|
|
guint i;
|
2018-03-22 16:40:27 +01:00
|
|
|
struct spa_buffer *b;
|
2016-12-20 16:51:57 +01:00
|
|
|
|
2018-03-22 16:40:27 +01:00
|
|
|
data = gst_pipewire_pool_get_data(buffer);
|
|
|
|
|
|
|
|
|
|
b = data->b->buffer;
|
2016-12-20 16:51:57 +01:00
|
|
|
|
|
|
|
|
if (data->header) {
|
|
|
|
|
data->header->seq = GST_BUFFER_OFFSET (buffer);
|
|
|
|
|
data->header->pts = GST_BUFFER_PTS (buffer);
|
2023-12-21 11:12:39 +01:00
|
|
|
if (GST_BUFFER_DTS(buffer) != GST_CLOCK_TIME_NONE)
|
|
|
|
|
data->header->dts_offset = GST_BUFFER_DTS (buffer) - GST_BUFFER_PTS (buffer);
|
|
|
|
|
else
|
|
|
|
|
data->header->dts_offset = 0;
|
2016-12-20 16:51:57 +01:00
|
|
|
}
|
2020-07-31 11:44:46 +02:00
|
|
|
if (data->crop) {
|
|
|
|
|
GstVideoCropMeta *meta = gst_buffer_get_video_crop_meta (buffer);
|
|
|
|
|
if (meta) {
|
|
|
|
|
data->crop->region.position.x = meta->x;
|
|
|
|
|
data->crop->region.position.y = meta->y;
|
|
|
|
|
data->crop->region.size.width = meta->width;
|
|
|
|
|
data->crop->region.size.height = meta->width;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-26 17:45:41 +01:00
|
|
|
data->b->size = 0;
|
2025-03-12 17:34:19 +05:30
|
|
|
|
|
|
|
|
spa_assert(b->n_datas == gst_buffer_n_memory(buffer));
|
|
|
|
|
|
2018-03-22 16:40:27 +01:00
|
|
|
for (i = 0; i < b->n_datas; i++) {
|
|
|
|
|
struct spa_data *d = &b->datas[i];
|
2016-12-20 16:51:57 +01:00
|
|
|
GstMemory *mem = gst_buffer_peek_memory (buffer, i);
|
2022-06-01 04:03:37 -06:00
|
|
|
d->chunk->offset = mem->offset;
|
2017-11-21 12:30:15 +01:00
|
|
|
d->chunk->size = mem->size;
|
2024-11-26 17:45:41 +01:00
|
|
|
d->chunk->stride = stream->pool->video_info.stride[i];
|
|
|
|
|
|
|
|
|
|
data->b->size += mem->size / 4;
|
2016-12-20 16:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-03 13:02:49 +08:00
|
|
|
GstVideoMeta *meta = gst_buffer_get_video_meta (buffer);
|
|
|
|
|
if (meta) {
|
|
|
|
|
if (meta->n_planes == b->n_datas) {
|
2025-03-12 17:34:19 +05:30
|
|
|
uint32_t n_planes = GST_VIDEO_INFO_N_PLANES (&data->pool->video_info);
|
2023-02-03 19:45:50 +08:00
|
|
|
gsize video_size = 0;
|
2025-03-12 17:34:19 +05:30
|
|
|
|
|
|
|
|
for (i = 0; i < n_planes; i++) {
|
2023-02-03 13:02:49 +08:00
|
|
|
struct spa_data *d = &b->datas[i];
|
2025-03-12 17:34:19 +05:30
|
|
|
|
2023-02-03 13:02:49 +08:00
|
|
|
d->chunk->stride = meta->stride[i];
|
2025-03-26 11:49:02 -04:00
|
|
|
d->chunk->offset = meta->offset[i] - video_size;
|
2023-02-03 19:45:50 +08:00
|
|
|
|
|
|
|
|
video_size += d->chunk->size;
|
2023-02-03 13:02:49 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2025-03-12 17:34:19 +05:30
|
|
|
GST_ERROR_OBJECT (pwsink, "plane num not matching, meta:%u buffer:%u",
|
|
|
|
|
meta->n_planes, b->n_datas);
|
2023-02-03 13:02:49 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 17:45:41 +01:00
|
|
|
if ((res = pw_stream_queue_buffer (stream->pwstream, data->b)) < 0) {
|
2024-12-27 10:39:36 +05:30
|
|
|
GST_WARNING_OBJECT (pwsink, "can't send buffer %s", spa_strerror(res));
|
|
|
|
|
} else {
|
|
|
|
|
data->queued = TRUE;
|
|
|
|
|
GST_LOG_OBJECT(pwsink, "queued pwbuffer: %p; gstbuffer %p ",data->b, buffer);
|
2020-04-23 15:56:12 +02:00
|
|
|
}
|
2024-11-26 17:45:41 +01:00
|
|
|
|
2024-12-03 11:54:52 +01:00
|
|
|
switch (pwsink->slave_method) {
|
|
|
|
|
case GST_PIPEWIRE_SINK_SLAVE_METHOD_NONE:
|
|
|
|
|
break;
|
|
|
|
|
case GST_PIPEWIRE_SINK_SLAVE_METHOD_RESAMPLE:
|
|
|
|
|
rate_match_resample(pwsink);
|
|
|
|
|
break;
|
2024-11-26 17:45:41 +01:00
|
|
|
}
|
2016-12-20 16:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2018-03-22 16:40:27 +01:00
|
|
|
on_process (void *data)
|
2016-12-20 16:51:57 +01:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GstPipeWireSink *pwsink = data;
|
2024-06-19 17:37:34 +02:00
|
|
|
GST_LOG_OBJECT (pwsink, "signal");
|
2024-05-29 17:56:38 +03:00
|
|
|
g_cond_signal (&pwsink->stream->pool->cond);
|
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
|
|
|
|
2024-06-19 17:37:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsink, "got stream state \"%s\" (%d)",
|
|
|
|
|
pw_stream_state_as_string(state), 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_PAUSED:
|
2022-05-30 12:54:33 +03:00
|
|
|
break;
|
2017-05-23 19:15:33 +02:00
|
|
|
case PW_STREAM_STATE_STREAMING:
|
2024-05-29 17:56:38 +03:00
|
|
|
if (pw_stream_is_driving (pwsink->stream->pwstream))
|
|
|
|
|
pw_stream_trigger_process (pwsink->stream->pwstream);
|
2015-07-08 12:11:55 +02:00
|
|
|
break;
|
2017-05-23 19:15:33 +02:00
|
|
|
case PW_STREAM_STATE_ERROR:
|
2023-11-08 18:12:59 +02:00
|
|
|
/* make the error permanent, if it is not already;
|
|
|
|
|
pw_stream_set_error() will recursively call us again */
|
2024-05-29 17:56:38 +03:00
|
|
|
if (pw_stream_get_state (pwsink->stream->pwstream, NULL) != PW_STREAM_STATE_ERROR)
|
|
|
|
|
pw_stream_set_error (pwsink->stream->pwstream, -EPIPE, "%s", error);
|
2023-11-08 18:12:59 +02:00
|
|
|
else
|
|
|
|
|
GST_ELEMENT_ERROR (pwsink, RESOURCE, FAILED,
|
|
|
|
|
("stream error: %s", error), (NULL));
|
2015-07-08 12:11:55 +02:00
|
|
|
break;
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_thread_loop_signal (pwsink->stream->core->loop, FALSE);
|
2015-05-11 18:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
2016-08-24 16:26:58 +02:00
|
|
|
static void
|
2019-11-21 16:14:50 +01:00
|
|
|
on_param_changed (void *data, uint32_t id, const struct spa_pod *param)
|
2016-08-24 16:26:58 +02:00
|
|
|
{
|
2017-08-04 10:18:54 +02:00
|
|
|
GstPipeWireSink *pwsink = data;
|
2024-06-19 17:40:56 +02:00
|
|
|
GstPipeWirePool *pool = pwsink->stream->pool;
|
2017-04-04 17:29:07 +02:00
|
|
|
|
2019-11-21 16:14:50 +01:00
|
|
|
if (param == NULL || id != SPA_PARAM_Format)
|
2023-11-16 17:06:52 +01:00
|
|
|
return;
|
2019-03-11 15:09:28 +01:00
|
|
|
|
2024-06-19 17:40:56 +02:00
|
|
|
GST_OBJECT_LOCK (pool);
|
|
|
|
|
while (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool))) {
|
|
|
|
|
GST_DEBUG_OBJECT (pool, "waiting for pool to become active");
|
|
|
|
|
g_cond_wait(&pool->cond, GST_OBJECT_GET_LOCK (pool));
|
|
|
|
|
}
|
|
|
|
|
GST_OBJECT_UNLOCK (pool);
|
|
|
|
|
|
|
|
|
|
gst_pipewire_sink_update_params (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;
|
2024-05-02 00:37:28 +02:00
|
|
|
g_autoptr(GPtrArray) possible = NULL;
|
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;
|
2024-03-15 17:24:27 +01:00
|
|
|
GstStructure *config, *s;
|
2020-04-23 15:56:12 +02:00
|
|
|
guint size;
|
|
|
|
|
guint min_buffers;
|
|
|
|
|
guint max_buffers;
|
2021-09-16 10:05:58 +02:00
|
|
|
struct timespec abstime;
|
2024-03-15 17:24:27 +01:00
|
|
|
gint rate;
|
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
|
|
|
|
2024-03-15 17:24:27 +01:00
|
|
|
s = gst_caps_get_structure (caps, 0);
|
2024-11-26 17:45:41 +01:00
|
|
|
if (gst_structure_has_name (s, "audio/x-raw")) {
|
2024-03-15 17:24:27 +01:00
|
|
|
gst_structure_get_int (s, "rate", &rate);
|
2024-11-26 17:45:41 +01:00
|
|
|
pwsink->rate = rate;
|
|
|
|
|
pwsink->rate_match = true;
|
2025-01-24 21:07:55 +05:30
|
|
|
|
|
|
|
|
/* Don't provide bufferpool for audio if not requested by the application/user */
|
|
|
|
|
if (pwsink->use_bufferpool != USE_BUFFERPOOL_YES)
|
|
|
|
|
pwsink->use_bufferpool = USE_BUFFERPOOL_NO;
|
2024-11-26 17:45:41 +01:00
|
|
|
} else {
|
|
|
|
|
pwsink->rate = rate = 0;
|
|
|
|
|
pwsink->rate_match = false;
|
2025-03-04 22:28:14 +05:30
|
|
|
pwsink->is_video = true;
|
2024-11-26 17:45:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spa_dll_set_bw(&pwsink->stream->dll, SPA_DLL_BW_MIN, 4096, rate);
|
2024-03-15 17:24:27 +01:00
|
|
|
|
2024-02-02 02:20:29 +01:00
|
|
|
possible = gst_caps_to_format_all (caps);
|
2015-12-09 13:27:43 +01:00
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_thread_loop_lock (pwsink->stream->core->loop);
|
|
|
|
|
state = pw_stream_get_state (pwsink->stream->pwstream, &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) {
|
2023-10-21 09:42:15 +02:00
|
|
|
enum pw_stream_flags flags;
|
2022-03-06 14:10:55 +02:00
|
|
|
uint32_t target_id;
|
2024-03-15 17:24:27 +01:00
|
|
|
struct spa_dict_item items[3];
|
|
|
|
|
uint32_t n_items = 0;
|
|
|
|
|
char buf[64];
|
2016-05-26 12:25:07 +02:00
|
|
|
|
2023-10-21 09:42:15 +02:00
|
|
|
flags = PW_STREAM_FLAG_ASYNC;
|
2024-11-26 16:57:14 +01:00
|
|
|
flags |= PW_STREAM_FLAG_EARLY_PROCESS;
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsink->mode != GST_PIPEWIRE_SINK_MODE_PROVIDE)
|
|
|
|
|
flags |= PW_STREAM_FLAG_AUTOCONNECT;
|
2018-08-15 13:16:11 +02:00
|
|
|
else
|
|
|
|
|
flags |= PW_STREAM_FLAG_DRIVER;
|
2016-05-26 12:25:07 +02:00
|
|
|
|
2025-03-20 21:13:03 -04:00
|
|
|
#ifdef HAVE_GSTREAMER_SHM_ALLOCATOR
|
|
|
|
|
flags |= PW_STREAM_FLAG_ALLOC_BUFFERS;
|
|
|
|
|
pwsink->stream->pool->allocate_memory = true;
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
target_id = pwsink->stream->path ? (uint32_t)atoi(pwsink->stream->path) : PW_ID_ANY;
|
2022-03-06 14:10:55 +02:00
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
if (pwsink->stream->target_object) {
|
2022-03-06 14:10:55 +02:00
|
|
|
uint64_t serial;
|
|
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
items[n_items++] = SPA_DICT_ITEM_INIT(PW_KEY_TARGET_OBJECT, pwsink->stream->target_object);
|
2024-03-15 17:24:27 +01:00
|
|
|
|
2022-03-06 14:10:55 +02:00
|
|
|
/* If target.object is a name, set it also to node.target */
|
2024-05-29 17:56:38 +03:00
|
|
|
if (!spa_atou64(pwsink->stream->target_object, &serial, 0)) {
|
2022-03-06 14:10:55 +02:00
|
|
|
target_id = PW_ID_ANY;
|
2024-03-15 17:24:27 +01:00
|
|
|
/* XXX deprecated but the portal and some example apps only
|
|
|
|
|
* provide the object id */
|
2024-05-29 17:56:38 +03:00
|
|
|
items[n_items++] = SPA_DICT_ITEM_INIT(PW_KEY_NODE_TARGET, pwsink->stream->target_object);
|
2022-03-06 14:10:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-15 17:24:27 +01:00
|
|
|
if (rate != 0) {
|
|
|
|
|
snprintf(buf, sizeof(buf), "1/%u", rate);
|
|
|
|
|
items[n_items++] = SPA_DICT_ITEM_INIT(PW_KEY_NODE_RATE, buf);
|
|
|
|
|
}
|
|
|
|
|
if (n_items > 0)
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_stream_update_properties (pwsink->stream->pwstream, &SPA_DICT_INIT(items, n_items));
|
2022-03-06 14:10:55 +02:00
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_stream_connect (pwsink->stream->pwstream,
|
2017-05-23 19:15:33 +02:00
|
|
|
PW_DIRECTION_OUTPUT,
|
2022-03-06 14:10:55 +02:00
|
|
|
target_id,
|
2016-05-26 12:25:07 +02:00
|
|
|
flags,
|
2017-11-20 15:26:44 +01:00
|
|
|
(const struct spa_pod **) possible->pdata,
|
|
|
|
|
possible->len);
|
2016-05-06 13:01:52 +02:00
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_thread_loop_get_time (pwsink->stream->core->loop, &abstime,
|
2021-09-16 10:05:58 +02:00
|
|
|
GST_PIPEWIRE_DEFAULT_TIMEOUT * SPA_NSEC_PER_SEC);
|
|
|
|
|
|
2016-05-06 13:01:52 +02:00
|
|
|
while (TRUE) {
|
2024-05-29 17:56:38 +03:00
|
|
|
state = pw_stream_get_state (pwsink->stream->pwstream, &error);
|
2016-05-06 13:01:52 +02:00
|
|
|
|
2021-06-18 13:34:35 +03:00
|
|
|
if (state >= PW_STREAM_STATE_PAUSED)
|
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;
|
|
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
if (pw_thread_loop_timed_wait_full (pwsink->stream->core->loop, &abstime) < 0) {
|
2021-09-16 10:05:58 +02:00
|
|
|
error = "timeout";
|
|
|
|
|
goto start_error;
|
|
|
|
|
}
|
2016-05-06 13:01:52 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_pipewire_clock_reset (GST_PIPEWIRE_CLOCK (pwsink->stream->clock), 0);
|
2024-03-12 17:36:50 +01:00
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pwsink->stream->pool));
|
2020-04-23 15:56:12 +02:00
|
|
|
gst_buffer_pool_config_get_params (config, NULL, &size, &min_buffers, &max_buffers);
|
|
|
|
|
gst_buffer_pool_config_set_params (config, caps, size, min_buffers, max_buffers);
|
2025-03-20 21:13:03 -04:00
|
|
|
if (pwsink->is_video) {
|
|
|
|
|
gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
|
|
|
|
|
#ifdef HAVE_GSTREAMER_SHM_ALLOCATOR
|
|
|
|
|
gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_ALIGNMENT);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pwsink->stream->pool), config);
|
2020-04-23 15:56:12 +02:00
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_thread_loop_unlock (pwsink->stream->core->loop);
|
2015-05-15 15:58:13 +02:00
|
|
|
|
2024-06-28 01:47:09 +02:00
|
|
|
pwsink->negotiated = TRUE;
|
2015-05-11 18:23:24 +02:00
|
|
|
|
2024-06-28 01:47:09 +02:00
|
|
|
return TRUE;
|
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
|
|
|
{
|
2024-05-28 12:25:43 +03:00
|
|
|
GST_ERROR_OBJECT (pwsink, "could not start stream: %s", error);
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_thread_loop_unlock (pwsink->stream->core->loop);
|
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;
|
|
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
if (buffer->pool != GST_BUFFER_POOL_CAST (pwsink->stream->pool) &&
|
|
|
|
|
!gst_buffer_pool_is_active (GST_BUFFER_POOL_CAST (pwsink->stream->pool))) {
|
2021-01-28 19:17:03 +01:00
|
|
|
GstStructure *config;
|
|
|
|
|
GstCaps *caps;
|
|
|
|
|
guint size, min_buffers, max_buffers;
|
|
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pwsink->stream->pool));
|
2021-01-28 19:17:03 +01:00
|
|
|
gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers, &max_buffers);
|
|
|
|
|
|
2023-10-27 16:56:05 +02:00
|
|
|
if (size == 0) {
|
|
|
|
|
gsize maxsize;
|
|
|
|
|
gst_buffer_get_sizes (buffer, NULL, &maxsize);
|
|
|
|
|
size = maxsize;
|
|
|
|
|
}
|
2021-01-28 19:17:03 +01:00
|
|
|
|
|
|
|
|
gst_buffer_pool_config_set_params (config, caps, size, min_buffers, max_buffers);
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST (pwsink->stream->pool), config);
|
2021-01-28 19:17:03 +01:00
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_buffer_pool_set_active (GST_BUFFER_POOL_CAST (pwsink->stream->pool), TRUE);
|
2021-01-28 19:17:03 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_thread_loop_lock (pwsink->stream->core->loop);
|
|
|
|
|
if (pw_stream_get_state (pwsink->stream->pwstream, &error) != PW_STREAM_STATE_STREAMING)
|
2020-05-20 13:53:51 +02:00
|
|
|
goto done_unlock;
|
2016-05-17 20:14:06 +02:00
|
|
|
|
2024-05-29 17:56:38 +03:00
|
|
|
if (buffer->pool != GST_BUFFER_POOL_CAST (pwsink->stream->pool)) {
|
2025-01-03 14:38:54 +09:00
|
|
|
gsize offset = 0;
|
2025-01-17 12:19:07 +01:00
|
|
|
gsize buf_size = gst_buffer_get_size (buffer);
|
2025-01-03 14:38:54 +09:00
|
|
|
|
2025-03-04 22:28:14 +05:30
|
|
|
GST_TRACE_OBJECT(pwsink, "Buffer is not from pipewirepool, copying into our pool");
|
|
|
|
|
|
2025-01-03 14:38:54 +09:00
|
|
|
/* For some streams, the buffer size is changed and may exceed the acquired
|
|
|
|
|
* buffer size which is acquired from the pool of pipewiresink. Need split
|
|
|
|
|
* the buffer and send them in turn for this case */
|
|
|
|
|
while (buf_size) {
|
|
|
|
|
GstBuffer *b = NULL;
|
|
|
|
|
GstMapInfo info = { 0, };
|
|
|
|
|
GstBufferPoolAcquireParams params = { 0, };
|
|
|
|
|
|
|
|
|
|
pw_thread_loop_unlock (pwsink->stream->core->loop);
|
|
|
|
|
|
2025-01-17 17:08:07 +01:00
|
|
|
params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_LAST;
|
2025-01-17 16:33:15 +01:00
|
|
|
res = gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL_CAST (pwsink->stream->pool),
|
|
|
|
|
&b, ¶ms);
|
|
|
|
|
if (res == GST_FLOW_CUSTOM_ERROR_1) {
|
2025-01-21 12:19:52 -05:00
|
|
|
res = gst_base_sink_wait_preroll (bsink);
|
|
|
|
|
if (res != GST_FLOW_OK)
|
2025-01-17 16:33:15 +01:00
|
|
|
goto done;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (res != GST_FLOW_OK)
|
2025-01-03 14:38:54 +09:00
|
|
|
goto done;
|
|
|
|
|
|
2025-03-04 22:28:14 +05:30
|
|
|
if (pwsink->is_video) {
|
|
|
|
|
GstVideoFrame src, dst;
|
|
|
|
|
gboolean copied = FALSE;
|
|
|
|
|
buf_size = 0; // to break from the loop
|
|
|
|
|
|
2025-03-26 09:51:53 -04:00
|
|
|
/* splitting of buffers in the case of video might break the frame layout
|
|
|
|
|
* and that seems to be causing issues while retrieving the buffers on the receiver
|
|
|
|
|
* side. Hence use the video_frame_map to copy the buffer of bigger size into the
|
|
|
|
|
* pipewirepool's buffer */
|
|
|
|
|
|
|
|
|
|
if (!gst_video_frame_map (&dst, &pwsink->stream->pool->video_info, b, GST_MAP_WRITE)) {
|
2025-03-04 22:28:14 +05:30
|
|
|
GST_ERROR_OBJECT(pwsink, "Failed to map dest buffer");
|
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!gst_video_frame_map (&src, &pwsink->stream->pool->video_info, buffer, GST_MAP_READ)) {
|
|
|
|
|
gst_video_frame_unmap (&dst);
|
|
|
|
|
GST_ERROR_OBJECT(pwsink, "Failed to map src buffer");
|
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copied = gst_video_frame_copy (&dst, &src);
|
|
|
|
|
|
|
|
|
|
gst_video_frame_unmap (&src);
|
|
|
|
|
gst_video_frame_unmap (&dst);
|
|
|
|
|
|
|
|
|
|
if (!copied) {
|
|
|
|
|
GST_ERROR_OBJECT(pwsink, "Failed to copy the frame");
|
|
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
gst_buffer_map (b, &info, GST_MAP_WRITE);
|
|
|
|
|
gsize extract_size = (buf_size <= info.maxsize) ? buf_size: info.maxsize;
|
|
|
|
|
gst_buffer_extract (buffer, offset, info.data, info.maxsize);
|
|
|
|
|
gst_buffer_unmap (b, &info);
|
|
|
|
|
gst_buffer_resize (b, 0, extract_size);
|
|
|
|
|
gst_buffer_copy_into(b, buffer, GST_BUFFER_COPY_METADATA, 0, -1);
|
|
|
|
|
buf_size -= extract_size;
|
|
|
|
|
offset += extract_size;
|
|
|
|
|
}
|
2025-01-03 14:38:54 +09:00
|
|
|
|
|
|
|
|
pw_thread_loop_lock (pwsink->stream->core->loop);
|
|
|
|
|
if (pw_stream_get_state (pwsink->stream->pwstream, &error) != PW_STREAM_STATE_STREAMING) {
|
|
|
|
|
gst_buffer_unref (b);
|
|
|
|
|
goto done_unlock;
|
|
|
|
|
}
|
2020-04-23 15:56:12 +02:00
|
|
|
|
2025-01-03 14:38:54 +09:00
|
|
|
do_send_buffer (pwsink, b);
|
|
|
|
|
gst_buffer_unref (b);
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2025-01-03 14:38:54 +09:00
|
|
|
if (pw_stream_is_driving (pwsink->stream->pwstream))
|
|
|
|
|
pw_stream_trigger_process (pwsink->stream->pwstream);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2025-03-04 22:28:14 +05:30
|
|
|
GST_TRACE_OBJECT(pwsink, "Buffer is from pipewirepool");
|
|
|
|
|
|
2025-01-03 14:38:54 +09:00
|
|
|
do_send_buffer (pwsink, buffer);
|
2016-08-03 21:33:57 +02:00
|
|
|
|
2025-01-03 14:38:54 +09:00
|
|
|
if (pw_stream_is_driving (pwsink->stream->pwstream))
|
|
|
|
|
pw_stream_trigger_process (pwsink->stream->pwstream);
|
2020-04-23 15:56:12 +02:00
|
|
|
}
|
2016-10-03 19:43:42 +02:00
|
|
|
|
2020-05-20 13:53:51 +02:00
|
|
|
done_unlock:
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_thread_loop_unlock (pwsink->stream->core->loop);
|
2020-05-20 13:53:51 +02:00
|
|
|
done:
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-04 16:49:13 +02:00
|
|
|
static const struct pw_stream_events stream_events = {
|
2019-12-18 11:29:11 +01:00
|
|
|
PW_VERSION_STREAM_EVENTS,
|
|
|
|
|
.state_changed = on_state_changed,
|
|
|
|
|
.param_changed = on_param_changed,
|
|
|
|
|
.add_buffer = on_add_buffer,
|
|
|
|
|
.remove_buffer = on_remove_buffer,
|
|
|
|
|
.process = on_process,
|
2017-08-04 10:18:54 +02:00
|
|
|
};
|
|
|
|
|
|
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:
|
2024-05-29 17:56:38 +03:00
|
|
|
if (!gst_pipewire_stream_open (this->stream, &stream_events))
|
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:
|
2025-03-24 22:11:36 -04:00
|
|
|
/* If we are a driver, we shouldn't try to also provide the clock, as we
|
|
|
|
|
* _are_ the clock for the graph. For that case, we rely on the pipeline
|
|
|
|
|
* clock to drive the pipeline (and thus the graph). */
|
|
|
|
|
if (this->mode == GST_PIPEWIRE_SINK_MODE_PROVIDE)
|
|
|
|
|
GST_OBJECT_FLAG_UNSET (this, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
|
|
|
|
|
|
2024-06-11 09:55:00 -04:00
|
|
|
/* the initial stream state is active, which is needed for linking and
|
|
|
|
|
* negotiation to happen and the bufferpool to be set up. We don't know
|
|
|
|
|
* if we'll go to plaing, so we deactivate the stream until that
|
|
|
|
|
* transition happens. This is janky, but because of how bins propagate
|
|
|
|
|
* state changes one transition at a time, there may not be a better way
|
|
|
|
|
* to do this. PAUSED -> READY -> PAUSED transitions, this is a noop */
|
|
|
|
|
pw_thread_loop_lock (this->stream->core->loop);
|
|
|
|
|
pw_stream_set_active(this->stream->pwstream, false);
|
|
|
|
|
pw_thread_loop_unlock (this->stream->core->loop);
|
2025-01-17 16:33:15 +01:00
|
|
|
gst_pipewire_pool_set_paused(this->stream->pool, TRUE);
|
2015-06-05 18:21:18 +02:00
|
|
|
break;
|
2015-05-11 18:23:24 +02:00
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
2020-05-21 12:26:13 +02:00
|
|
|
/* stop play ASAP by corking */
|
2025-01-17 16:33:15 +01:00
|
|
|
gst_pipewire_pool_set_paused(this->stream->pool, TRUE);
|
2024-05-29 17:56:38 +03:00
|
|
|
pw_thread_loop_lock (this->stream->core->loop);
|
|
|
|
|
pw_stream_set_active(this->stream->pwstream, false);
|
|
|
|
|
pw_thread_loop_unlock (this->stream->core->loop);
|
2015-05-11 18:23:24 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
|
|
switch (transition) {
|
2024-12-30 11:48:29 +09:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
|
/* For some cases, the param_changed event is earlier than the state switch
|
|
|
|
|
* from paused state to playing state which will wait until buffer pool is ready.
|
|
|
|
|
* Guarantee to finish preoll if needed to active buffer pool before uncorking and
|
|
|
|
|
* starting play */
|
2025-01-17 16:33:15 +01:00
|
|
|
gst_pipewire_pool_set_paused(this->stream->pool, FALSE);
|
2024-12-30 11:48:29 +09:00
|
|
|
pw_thread_loop_lock (this->stream->core->loop);
|
|
|
|
|
pw_stream_set_active(this->stream->pwstream, true);
|
|
|
|
|
pw_thread_loop_unlock (this->stream->core->loop);
|
|
|
|
|
break;
|
2015-05-11 18:23:24 +02:00
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
|
break;
|
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_buffer_pool_set_active(GST_BUFFER_POOL_CAST(this->stream->pool), FALSE);
|
|
|
|
|
this->negotiated = FALSE;
|
2015-05-11 18:23:24 +02:00
|
|
|
break;
|
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
2024-05-29 17:56:38 +03:00
|
|
|
gst_pipewire_stream_close (this->stream);
|
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
|
|
|
}
|
2024-12-27 10:39:36 +05:30
|
|
|
|
|
|
|
|
static gboolean gst_pipewire_sink_event (GstBaseSink *sink, GstEvent *event) {
|
|
|
|
|
GstPipeWireSink *pw_sink = GST_PIPEWIRE_SINK(sink);
|
2025-01-17 11:49:42 +05:30
|
|
|
GstState current_state = GST_ELEMENT(sink)->current_state;
|
2024-12-27 10:39:36 +05:30
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
|
case GST_EVENT_FLUSH_START:
|
|
|
|
|
{
|
|
|
|
|
GST_DEBUG_OBJECT (pw_sink, "flush-start");
|
|
|
|
|
pw_thread_loop_lock (pw_sink->stream->core->loop);
|
2025-01-17 11:49:42 +05:30
|
|
|
|
|
|
|
|
/* The stream would be already inactive if the sink is not PLAYING */
|
|
|
|
|
if (current_state == GST_STATE_PLAYING)
|
|
|
|
|
pw_stream_set_active(pw_sink->stream->pwstream, false);
|
|
|
|
|
|
|
|
|
|
gst_buffer_pool_set_flushing(GST_BUFFER_POOL_CAST(pw_sink->stream->pool), TRUE);
|
2024-12-27 10:39:36 +05:30
|
|
|
pw_stream_flush(pw_sink->stream->pwstream, false);
|
|
|
|
|
pw_thread_loop_unlock (pw_sink->stream->core->loop);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
|
{
|
|
|
|
|
GST_DEBUG_OBJECT (pw_sink, "flush-stop");
|
|
|
|
|
pw_thread_loop_lock (pw_sink->stream->core->loop);
|
2025-01-17 11:49:42 +05:30
|
|
|
|
|
|
|
|
/* The stream needs to remain inactive if the sink is not PLAYING */
|
|
|
|
|
if (current_state == GST_STATE_PLAYING)
|
|
|
|
|
pw_stream_set_active(pw_sink->stream->pwstream, true);
|
|
|
|
|
|
|
|
|
|
gst_buffer_pool_set_flushing(GST_BUFFER_POOL_CAST(pw_sink->stream->pool), FALSE);
|
2024-12-27 10:39:36 +05:30
|
|
|
pw_thread_loop_unlock (pw_sink->stream->core->loop);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
|
2025-01-17 16:33:15 +01:00
|
|
|
}
|