2015-04-28 17:36:44 +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-pipewiresrc
|
2015-04-28 17:36:44 +02:00
|
|
|
*
|
|
|
|
|
* <refsect2>
|
|
|
|
|
* <title>Example launch line</title>
|
|
|
|
|
* |[
|
2017-05-23 19:15:33 +02:00
|
|
|
* gst-launch -v pipewiresrc ! videoconvert ! ximagesink
|
|
|
|
|
* ]| Shows pipewire output in an X window.
|
2015-04-28 17:36:44 +02:00
|
|
|
* </refsect2>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
2017-05-23 19:15:33 +02:00
|
|
|
#include "gstpipewiresrc.h"
|
|
|
|
|
#include "gstpipewireformat.h"
|
2015-04-28 17:36:44 +02:00
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <gio/gunixfdmessage.h>
|
2015-12-02 20:43:37 +01:00
|
|
|
#include <gst/net/gstnetclientclock.h>
|
2015-04-28 17:36:44 +02:00
|
|
|
#include <gst/allocators/gstfdmemory.h>
|
2016-04-13 13:04:32 +02:00
|
|
|
#include <gst/video/video.h>
|
2015-04-28 17:36:44 +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 "gstpipewireclock.h"
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2016-07-21 18:38:24 +02:00
|
|
|
static GQuark process_mem_data_quark;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (pipewire_src_debug);
|
|
|
|
|
#define GST_CAT_DEFAULT pipewire_src_debug
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-17 11:49:57 +02:00
|
|
|
#define DEFAULT_ALWAYS_COPY false
|
|
|
|
|
|
2015-04-28 17:36:44 +02:00
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
PROP_0,
|
2015-07-28 17:05:03 +02:00
|
|
|
PROP_PATH,
|
|
|
|
|
PROP_CLIENT_NAME,
|
2015-12-09 13:27:43 +01:00
|
|
|
PROP_STREAM_PROPERTIES,
|
2017-05-17 11:49:57 +02:00
|
|
|
PROP_ALWAYS_COPY,
|
2015-04-28 17:36:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
static GstStaticPadTemplate gst_pipewire_src_template =
|
2015-04-28 17:36:44 +02:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
|
GST_PAD_SRC,
|
|
|
|
|
GST_PAD_ALWAYS,
|
2015-05-15 13:34:32 +02:00
|
|
|
GST_STATIC_CAPS_ANY
|
2015-04-28 17:36:44 +02:00
|
|
|
);
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
#define gst_pipewire_src_parent_class parent_class
|
|
|
|
|
G_DEFINE_TYPE (GstPipeWireSrc, gst_pipewire_src, GST_TYPE_PUSH_SRC);
|
2015-04-28 17:36:44 +02:00
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_change_state (GstElement * element, GstStateChange transition);
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
static gboolean gst_pipewire_src_negotiate (GstBaseSrc * basesrc);
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
static GstFlowReturn gst_pipewire_src_create (GstPushSrc * psrc,
|
2015-04-28 17:36:44 +02:00
|
|
|
GstBuffer ** buffer);
|
2017-05-23 19:15:33 +02:00
|
|
|
static gboolean gst_pipewire_src_unlock (GstBaseSrc * basesrc);
|
|
|
|
|
static gboolean gst_pipewire_src_unlock_stop (GstBaseSrc * basesrc);
|
|
|
|
|
static gboolean gst_pipewire_src_start (GstBaseSrc * basesrc);
|
|
|
|
|
static gboolean gst_pipewire_src_stop (GstBaseSrc * basesrc);
|
|
|
|
|
static gboolean gst_pipewire_src_event (GstBaseSrc * src, GstEvent * event);
|
|
|
|
|
static gboolean gst_pipewire_src_query (GstBaseSrc * src, GstQuery * query);
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2015-05-20 12:01:13 +02:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_set_property (GObject * object, guint prop_id,
|
2015-05-20 12:01:13 +02:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (object);
|
2015-05-20 12:01:13 +02:00
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2015-07-09 17:58:54 +02:00
|
|
|
case PROP_PATH:
|
2017-05-23 19:15:33 +02:00
|
|
|
g_free (pwsrc->path);
|
|
|
|
|
pwsrc->path = g_value_dup_string (value);
|
2015-05-20 12:01:13 +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 (pwsrc->client_name);
|
|
|
|
|
pwsrc->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 (pwsrc->properties)
|
|
|
|
|
gst_structure_free (pwsrc->properties);
|
|
|
|
|
pwsrc->properties =
|
2015-12-09 13:27:43 +01:00
|
|
|
gst_structure_copy (gst_value_get_structure (value));
|
|
|
|
|
break;
|
|
|
|
|
|
2017-05-17 11:49:57 +02:00
|
|
|
case PROP_ALWAYS_COPY:
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsrc->always_copy = g_value_get_boolean (value);
|
2017-05-17 11:49:57 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-05-20 12:01:13 +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_src_get_property (GObject * object, guint prop_id,
|
2015-05-20 12:01:13 +02:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (object);
|
2015-05-20 12:01:13 +02:00
|
|
|
|
|
|
|
|
switch (prop_id) {
|
2015-07-09 17:58:54 +02:00
|
|
|
case PROP_PATH:
|
2017-05-23 19:15:33 +02:00
|
|
|
g_value_set_string (value, pwsrc->path);
|
2015-05-20 12:01:13 +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, pwsrc->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, pwsrc->properties);
|
2015-12-09 13:27:43 +01:00
|
|
|
break;
|
|
|
|
|
|
2017-05-17 11:49:57 +02:00
|
|
|
case PROP_ALWAYS_COPY:
|
2017-05-23 19:15:33 +02:00
|
|
|
g_value_set_boolean (value, pwsrc->always_copy);
|
2017-05-17 11:49:57 +02:00
|
|
|
break;
|
|
|
|
|
|
2015-05-20 12:01:13 +02:00
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-02 20:43:37 +01:00
|
|
|
static GstClock *
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_provide_clock (GstElement * elem)
|
2015-12-02 20:43:37 +01:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (elem);
|
2015-12-02 20:43:37 +01:00
|
|
|
GstClock *clock;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_OBJECT_LOCK (pwsrc);
|
|
|
|
|
if (!GST_OBJECT_FLAG_IS_SET (pwsrc, GST_ELEMENT_FLAG_PROVIDE_CLOCK))
|
2015-12-02 20:43:37 +01:00
|
|
|
goto clock_disabled;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsrc->clock && pwsrc->is_live)
|
|
|
|
|
clock = GST_CLOCK_CAST (gst_object_ref (pwsrc->clock));
|
2015-12-02 20:43:37 +01:00
|
|
|
else
|
|
|
|
|
clock = NULL;
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_OBJECT_UNLOCK (pwsrc);
|
2015-12-02 20:43:37 +01:00
|
|
|
|
|
|
|
|
return clock;
|
|
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
|
clock_disabled:
|
|
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsrc, "clock provide disabled");
|
|
|
|
|
GST_OBJECT_UNLOCK (pwsrc);
|
2015-12-02 20:43:37 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-20 16:51:57 +01:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
clear_queue (GstPipeWireSrc *pwsrc)
|
2016-12-20 16:51:57 +01:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
g_queue_foreach (&pwsrc->queue, (GFunc) gst_mini_object_unref, NULL);
|
|
|
|
|
g_queue_clear (&pwsrc->queue);
|
2016-12-20 16:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-20 12:01:13 +02:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_finalize (GObject * object)
|
2015-05-20 12:01:13 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (object);
|
2015-05-20 12:01:13 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
clear_queue (pwsrc);
|
2016-11-25 13:06:23 +01:00
|
|
|
|
2017-07-11 20:54:10 +02:00
|
|
|
pw_core_destroy (pwsrc->core);
|
|
|
|
|
pwsrc->core = NULL;
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_destroy (pwsrc->main_loop);
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsrc->main_loop = NULL;
|
|
|
|
|
pw_loop_destroy (pwsrc->loop);
|
|
|
|
|
pwsrc->loop = NULL;
|
2016-11-25 13:06:23 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsrc->properties)
|
|
|
|
|
gst_structure_free (pwsrc->properties);
|
|
|
|
|
g_object_unref (pwsrc->fd_allocator);
|
|
|
|
|
if (pwsrc->clock)
|
|
|
|
|
gst_object_unref (pwsrc->clock);
|
|
|
|
|
g_free (pwsrc->path);
|
|
|
|
|
g_free (pwsrc->client_name);
|
|
|
|
|
g_hash_table_unref (pwsrc->buf_ids);
|
2015-05-20 12:01:13 +02:00
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-28 17:36:44 +02:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_class_init (GstPipeWireSrcClass * klass)
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
GstBaseSrcClass *gstbasesrc_class;
|
|
|
|
|
GstPushSrcClass *gstpushsrc_class;
|
|
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
|
gstbasesrc_class = (GstBaseSrcClass *) klass;
|
|
|
|
|
gstpushsrc_class = (GstPushSrcClass *) klass;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
gobject_class->finalize = gst_pipewire_src_finalize;
|
|
|
|
|
gobject_class->set_property = gst_pipewire_src_set_property;
|
|
|
|
|
gobject_class->get_property = gst_pipewire_src_get_property;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2015-05-20 12:01:13 +02:00
|
|
|
g_object_class_install_property (gobject_class,
|
2015-07-09 17:58:54 +02:00
|
|
|
PROP_PATH,
|
|
|
|
|
g_param_spec_string ("path",
|
|
|
|
|
"Path",
|
|
|
|
|
"The source path to connect to (NULL = default)",
|
2015-05-20 12:01:13 +02:00
|
|
|
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-05-20 12:01:13 +02:00
|
|
|
|
2015-12-09 13:27:43 +01:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_STREAM_PROPERTIES,
|
|
|
|
|
g_param_spec_boxed ("stream-properties",
|
|
|
|
|
"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));
|
|
|
|
|
|
2017-05-17 11:49:57 +02:00
|
|
|
g_object_class_install_property (gobject_class,
|
|
|
|
|
PROP_ALWAYS_COPY,
|
|
|
|
|
g_param_spec_boolean ("always-copy",
|
|
|
|
|
"Always copy",
|
|
|
|
|
"Always copy the buffer and data",
|
|
|
|
|
DEFAULT_ALWAYS_COPY,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
gstelement_class->provide_clock = gst_pipewire_src_provide_clock;
|
|
|
|
|
gstelement_class->change_state = gst_pipewire_src_change_state;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
|
|
|
|
gst_element_class_set_static_metadata (gstelement_class,
|
2017-05-23 19:15:33 +02:00
|
|
|
"PipeWire source", "Source/Video",
|
|
|
|
|
"Uses PipeWire to create video", "Wim Taymans <wim.taymans@gmail.com>");
|
2015-04-28 17:36:44 +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_src_template));
|
|
|
|
|
|
|
|
|
|
gstbasesrc_class->negotiate = gst_pipewire_src_negotiate;
|
|
|
|
|
gstbasesrc_class->unlock = gst_pipewire_src_unlock;
|
|
|
|
|
gstbasesrc_class->unlock_stop = gst_pipewire_src_unlock_stop;
|
|
|
|
|
gstbasesrc_class->start = gst_pipewire_src_start;
|
|
|
|
|
gstbasesrc_class->stop = gst_pipewire_src_stop;
|
|
|
|
|
gstbasesrc_class->event = gst_pipewire_src_event;
|
|
|
|
|
gstbasesrc_class->query = gst_pipewire_src_query;
|
|
|
|
|
gstpushsrc_class->create = gst_pipewire_src_create;
|
|
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (pipewire_src_debug, "pipewiresrc", 0,
|
|
|
|
|
"PipeWire Source");
|
|
|
|
|
|
|
|
|
|
process_mem_data_quark = g_quark_from_static_string ("GstPipeWireSrcProcessMemQuark");
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_init (GstPipeWireSrc * src)
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
|
|
|
|
/* we operate in time */
|
|
|
|
|
gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
|
|
|
|
|
|
2015-12-02 20:43:37 +01:00
|
|
|
GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
|
|
|
|
|
|
2017-05-17 11:49:57 +02:00
|
|
|
src->always_copy = DEFAULT_ALWAYS_COPY;
|
|
|
|
|
|
2016-04-12 17:05:51 +02:00
|
|
|
g_queue_init (&src->queue);
|
|
|
|
|
|
2015-04-28 17:36:44 +02:00
|
|
|
src->fd_allocator = gst_fd_allocator_new ();
|
2017-05-30 19:46:51 +02:00
|
|
|
src->client_name = pw_get_client_name ();
|
2016-08-24 16:26:58 +02:00
|
|
|
src->buf_ids = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) gst_buffer_unref);
|
2016-11-25 13:06:23 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
src->loop = pw_loop_new ();
|
2017-06-01 19:25:01 +02:00
|
|
|
src->main_loop = pw_thread_loop_new (src->loop, "pipewire-main-loop");
|
2017-07-11 12:24:03 +02:00
|
|
|
src->core = pw_core_new (src->loop, NULL);
|
2016-11-25 13:06:23 +01:00
|
|
|
GST_DEBUG ("loop %p, mainloop %p", src->loop, src->main_loop);
|
2017-05-17 11:49:57 +02:00
|
|
|
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
2015-08-31 16:47:32 +02:00
|
|
|
typedef struct {
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *src;
|
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-07-21 18:38:24 +02:00
|
|
|
} ProcessMemData;
|
2015-08-31 16:47:32 +02:00
|
|
|
|
|
|
|
|
static void
|
2016-07-21 18:38:24 +02:00
|
|
|
process_mem_data_destroy (gpointer user_data)
|
2015-08-31 16:47:32 +02:00
|
|
|
{
|
2016-07-21 18:38:24 +02:00
|
|
|
ProcessMemData *data = user_data;
|
2015-08-31 16:47:32 +02:00
|
|
|
|
2016-07-28 21:19:20 +02:00
|
|
|
gst_object_unref (data->src);
|
2016-07-21 18:38:24 +02:00
|
|
|
g_slice_free (ProcessMemData, data);
|
2015-08-31 16:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
2016-08-24 16:26:58 +02:00
|
|
|
static gboolean
|
|
|
|
|
buffer_recycle (GstMiniObject *obj)
|
|
|
|
|
{
|
|
|
|
|
ProcessMemData *data;
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *src;
|
2016-09-09 16:05:58 +02:00
|
|
|
|
2016-08-24 16:26:58 +02:00
|
|
|
gst_mini_object_ref (obj);
|
|
|
|
|
data = gst_mini_object_get_qdata (obj,
|
|
|
|
|
process_mem_data_quark);
|
|
|
|
|
GST_BUFFER_FLAGS (obj) = data->flags;
|
2016-09-13 17:43:57 +02:00
|
|
|
src = data->src;
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2016-09-09 16:05:58 +02:00
|
|
|
GST_LOG_OBJECT (obj, "recycle buffer");
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (src->main_loop);
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_stream_recycle_buffer (src->stream, data->id);
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (src->main_loop);
|
2016-08-24 16:26:58 +02:00
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-28 17:36:44 +02:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
on_add_buffer (struct pw_listener *listener,
|
|
|
|
|
struct pw_stream *stream,
|
2016-11-24 17:00:42 +01:00
|
|
|
guint id)
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = SPA_CONTAINER_OF (listener, GstPipeWireSrc, stream_add_buffer);
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_buffer *b;
|
2016-07-28 21:19:20 +02:00
|
|
|
GstBuffer *buf;
|
2017-03-07 11:56:43 +01:00
|
|
|
uint32_t i;
|
2016-07-28 21:19:20 +02:00
|
|
|
ProcessMemData data;
|
2017-07-11 12:24:03 +02:00
|
|
|
struct pw_remote *remote = pwsrc->stream->remote;
|
|
|
|
|
struct pw_core *core = remote->core;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_LOG_OBJECT (pwsrc, "add buffer");
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (!(b = pw_stream_peek_buffer (pwsrc->stream, id))) {
|
2016-08-24 16:26:58 +02:00
|
|
|
g_warning ("failed to peek buffer");
|
2015-08-25 16:36:01 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2015-08-24 16:41:04 +02:00
|
|
|
|
2016-07-28 21:19:20 +02:00
|
|
|
buf = gst_buffer_new ();
|
2016-08-24 16:26:58 +02:00
|
|
|
GST_MINI_OBJECT_CAST (buf)->dispose = buffer_recycle;
|
2015-08-24 16:41:04 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
data.src = gst_object_ref (pwsrc);
|
2016-08-24 16:26:58 +02:00
|
|
|
data.id = id;
|
2016-09-09 16:05:58 +02:00
|
|
|
data.buf = b;
|
2017-07-11 12:24:03 +02:00
|
|
|
data.header = spa_buffer_find_meta (b, core->type.meta.Header);
|
2016-04-07 12:14:16 +02:00
|
|
|
|
2016-07-28 21:19:20 +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-07-21 18:38:24 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
if (d->type == core->type.data.MemFd || d->type == core->type.data.DmaBuf) {
|
2017-05-23 19:15:33 +02:00
|
|
|
gmem = gst_fd_allocator_alloc (pwsrc->fd_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-07-11 12:24:03 +02:00
|
|
|
else if (d->type == core->type.data.MemPtr) {
|
2017-04-26 18:42:50 +02:00
|
|
|
gmem = gst_memory_new_wrapped (0, d->data, d->maxsize, d->chunk->offset + d->mapoffset,
|
|
|
|
|
d->chunk->size, NULL, NULL);
|
|
|
|
|
data.offset = 0;
|
2015-08-24 16:41:04 +02:00
|
|
|
}
|
2016-10-04 15:56:33 +02:00
|
|
|
if (gmem)
|
|
|
|
|
gst_buffer_append_memory (buf, gmem);
|
2015-08-24 16:41:04 +02:00
|
|
|
}
|
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
|
|
|
g_hash_table_insert (pwsrc->buf_ids, GINT_TO_POINTER (id), buf);
|
2016-08-24 16:26:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
on_remove_buffer (struct pw_listener *listener,
|
|
|
|
|
struct pw_stream *stream,
|
2016-11-24 17:00:42 +01:00
|
|
|
guint id)
|
2016-08-24 16:26:58 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = SPA_CONTAINER_OF (listener, GstPipeWireSrc, stream_remove_buffer);
|
2016-08-24 16:26:58 +02:00
|
|
|
GstBuffer *buf;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_LOG_OBJECT (pwsrc, "remove buffer");
|
|
|
|
|
buf = g_hash_table_lookup (pwsrc->buf_ids, GINT_TO_POINTER (id));
|
2017-01-31 09:57:58 +01:00
|
|
|
if (buf) {
|
|
|
|
|
GList *walk;
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2017-01-31 09:57:58 +01:00
|
|
|
GST_MINI_OBJECT_CAST (buf)->dispose = NULL;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
walk = pwsrc->queue.head;
|
2017-01-31 09:57:58 +01:00
|
|
|
while (walk) {
|
|
|
|
|
GList *next = walk->next;
|
|
|
|
|
|
|
|
|
|
if (walk->data == buf) {
|
|
|
|
|
gst_buffer_unref (buf);
|
2017-05-23 19:15:33 +02:00
|
|
|
g_queue_delete_link (&pwsrc->queue, walk);
|
2017-01-31 09:57:58 +01:00
|
|
|
}
|
|
|
|
|
walk = next;
|
|
|
|
|
}
|
2017-05-23 19:15:33 +02:00
|
|
|
g_hash_table_remove (pwsrc->buf_ids, GINT_TO_POINTER (id));
|
2017-01-31 09:57:58 +01:00
|
|
|
}
|
2016-08-24 16:26:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
on_new_buffer (struct pw_listener *listener,
|
|
|
|
|
struct pw_stream *stream,
|
2016-11-24 17:00:42 +01:00
|
|
|
guint id)
|
2016-08-24 16:26:58 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = SPA_CONTAINER_OF (listener, GstPipeWireSrc, stream_new_buffer);
|
2016-08-24 16:26:58 +02:00
|
|
|
GstBuffer *buf;
|
2016-10-28 16:56:33 +02:00
|
|
|
ProcessMemData *data;
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_meta_header *h;
|
2016-10-28 16:56:33 +02:00
|
|
|
guint i;
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
buf = g_hash_table_lookup (pwsrc->buf_ids, GINT_TO_POINTER (id));
|
2016-10-28 16:56:33 +02:00
|
|
|
if (buf == NULL) {
|
|
|
|
|
g_warning ("unknown buffer %d", id);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_LOG_OBJECT (pwsrc, "got new buffer %p", buf);
|
2016-05-17 09:38:30 +02:00
|
|
|
|
2016-10-28 16:56:33 +02:00
|
|
|
data = gst_mini_object_get_qdata (GST_MINI_OBJECT_CAST (buf),
|
|
|
|
|
process_mem_data_quark);
|
|
|
|
|
h = data->header;
|
|
|
|
|
if (h) {
|
|
|
|
|
GST_INFO ("pts %" G_GUINT64_FORMAT ", dts_offset %"G_GUINT64_FORMAT, h->pts, h->dts_offset);
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2016-10-28 16:56:33 +02:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (h->pts)) {
|
|
|
|
|
GST_BUFFER_PTS (buf) = h->pts;
|
|
|
|
|
if (GST_BUFFER_PTS (buf) + h->dts_offset > 0)
|
|
|
|
|
GST_BUFFER_DTS (buf) = GST_BUFFER_PTS (buf) + h->dts_offset;
|
2016-09-09 16:05:58 +02:00
|
|
|
}
|
2016-10-28 16:56:33 +02:00
|
|
|
GST_BUFFER_OFFSET (buf) = h->seq;
|
2016-05-05 13:31:18 +02:00
|
|
|
}
|
2016-10-28 16:56:33 +02:00
|
|
|
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-10-28 16:56:33 +02:00
|
|
|
GstMemory *mem = gst_buffer_peek_memory (buf, i);
|
2016-12-20 16:51:57 +01:00
|
|
|
mem->offset = d->chunk->offset + data->offset;
|
2016-12-15 14:57:34 +01:00
|
|
|
mem->size = d->chunk->size;
|
2016-10-28 16:56:33 +02:00
|
|
|
}
|
2017-05-17 11:49:57 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsrc->always_copy)
|
2017-05-17 11:49:57 +02:00
|
|
|
buf = gst_buffer_copy_deep (buf);
|
|
|
|
|
else
|
|
|
|
|
gst_buffer_ref (buf);
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
g_queue_push_tail (&pwsrc->queue, buf);
|
2016-10-28 16:56:33 +02:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_signal (pwsrc->main_loop, FALSE);
|
2015-08-24 16:41:04 +02:00
|
|
|
return;
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
on_state_changed (struct pw_listener *listener,
|
|
|
|
|
struct pw_stream *stream)
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = SPA_CONTAINER_OF (listener, GstPipeWireSrc, stream_state_changed);
|
|
|
|
|
enum pw_stream_state state = stream->state;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG ("got stream state %s", pw_stream_state_as_string (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 (pwsrc, RESOURCE, FAILED,
|
2016-11-24 17:00:42 +01:00
|
|
|
("stream error: %s", stream->error), (NULL));
|
2015-07-08 12:11:55 +02:00
|
|
|
break;
|
2015-04-29 17:51:51 +02:00
|
|
|
}
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_signal (pwsrc->main_loop, FALSE);
|
2015-04-29 17:51:51 +02:00
|
|
|
}
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2016-04-28 11:18:10 +02:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
parse_stream_properties (GstPipeWireSrc *pwsrc, struct pw_properties *props)
|
2016-04-28 11:18:10 +02:00
|
|
|
{
|
|
|
|
|
const gchar *var;
|
2016-09-16 13:13:41 +02:00
|
|
|
gboolean is_live;
|
2016-04-28 11:18:10 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_OBJECT_LOCK (pwsrc);
|
|
|
|
|
var = pw_properties_get (props, "pipewire.latency.is-live");
|
|
|
|
|
is_live = pwsrc->is_live = var ? (atoi (var) == 1) : FALSE;
|
2016-04-29 16:51:56 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
var = pw_properties_get (props, "pipewire.latency.min");
|
|
|
|
|
pwsrc->min_latency = var ? (GstClockTime) atoi (var) : 0;
|
2016-04-29 16:51:56 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
var = pw_properties_get (props, "pipewire.latency.max");
|
|
|
|
|
pwsrc->max_latency = var ? (GstClockTime) atoi (var) : GST_CLOCK_TIME_NONE;
|
|
|
|
|
GST_OBJECT_UNLOCK (pwsrc);
|
2016-09-16 13:13:41 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsrc, "live %d", is_live);
|
2016-10-28 16:56:33 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_base_src_set_live (GST_BASE_SRC (pwsrc), is_live);
|
2016-04-28 11:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-10 15:31:05 +02:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_stream_start (GstPipeWireSrc *pwsrc)
|
2015-07-10 15:31:05 +02:00
|
|
|
{
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsrc->main_loop);
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsrc, "doing stream start");
|
2015-07-14 15:47:18 +02:00
|
|
|
while (TRUE) {
|
2017-05-23 19:15:33 +02:00
|
|
|
enum pw_stream_state state = pwsrc->stream->state;
|
2015-07-14 15:47:18 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsrc, "waiting for STREAMING, now %s", pw_stream_state_as_string (state));
|
|
|
|
|
if (state == PW_STREAM_STATE_STREAMING)
|
2015-07-14 15:47:18 +02:00
|
|
|
break;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (state == PW_STREAM_STATE_ERROR)
|
2015-07-14 15:47:18 +02:00
|
|
|
goto start_error;
|
|
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
if (pwsrc->remote->state == PW_REMOTE_STATE_ERROR)
|
2017-01-17 10:27:58 +01:00
|
|
|
goto start_error;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_wait (pwsrc->main_loop);
|
2015-07-14 15:47:18 +02:00
|
|
|
}
|
2016-04-28 11:18:10 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
parse_stream_properties (pwsrc, pwsrc->stream->properties);
|
|
|
|
|
GST_DEBUG_OBJECT (pwsrc, "signal started");
|
|
|
|
|
pwsrc->started = TRUE;
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_signal (pwsrc->main_loop, FALSE);
|
|
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2016-04-28 16:42:25 +02:00
|
|
|
|
2017-04-18 17:57:04 +02:00
|
|
|
return TRUE;
|
2015-07-14 15:47:18 +02:00
|
|
|
|
|
|
|
|
start_error:
|
|
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsrc, "error starting stream");
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2015-07-14 15:47:18 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
2015-07-10 15:31:05 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
static enum pw_stream_state
|
|
|
|
|
wait_negotiated (GstPipeWireSrc *this)
|
2016-04-28 16:42:25 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
enum pw_stream_state state;
|
2016-09-16 13:13:41 +02:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (this->main_loop);
|
2016-09-16 13:13:41 +02:00
|
|
|
while (TRUE) {
|
2016-11-24 17:00:42 +01:00
|
|
|
state = this->stream->state;
|
2016-09-16 13:13:41 +02:00
|
|
|
|
2016-10-28 16:56:33 +02:00
|
|
|
GST_DEBUG_OBJECT (this, "waiting for started signal, state now %s",
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_stream_state_as_string (state));
|
2017-01-17 10:27:58 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (state == PW_STREAM_STATE_ERROR)
|
2016-09-16 13:13:41 +02:00
|
|
|
break;
|
|
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
if (this->remote->state == PW_REMOTE_STATE_ERROR)
|
2017-01-17 10:27:58 +01:00
|
|
|
break;
|
|
|
|
|
|
2016-09-16 13:13:41 +02:00
|
|
|
if (this->started)
|
|
|
|
|
break;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_wait (this->main_loop);
|
2016-04-28 16:42:25 +02:00
|
|
|
}
|
2016-10-28 16:56:33 +02:00
|
|
|
GST_DEBUG_OBJECT (this, "got started signal");
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (this->main_loop);
|
2016-09-16 13:13:41 +02:00
|
|
|
|
|
|
|
|
return state;
|
2016-04-28 16:42:25 +02:00
|
|
|
}
|
|
|
|
|
|
2015-04-29 17:51:51 +02:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_negotiate (GstBaseSrc * basesrc)
|
2015-04-29 17:51:51 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (basesrc);
|
2015-05-14 17:46:12 +02:00
|
|
|
GstCaps *thiscaps;
|
|
|
|
|
GstCaps *caps = NULL;
|
|
|
|
|
GstCaps *peercaps = NULL;
|
|
|
|
|
gboolean result = FALSE;
|
2016-08-05 16:39:26 +02:00
|
|
|
GPtrArray *possible;
|
2015-05-14 17:46:12 +02:00
|
|
|
|
|
|
|
|
/* first see what is possible on our source pad */
|
|
|
|
|
thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
|
|
|
|
|
GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
|
|
|
|
|
/* nothing or anything is allowed, we're done */
|
2015-05-15 13:34:32 +02:00
|
|
|
if (thiscaps == NULL)
|
2015-05-14 17:46:12 +02:00
|
|
|
goto no_nego_needed;
|
|
|
|
|
|
|
|
|
|
if (G_UNLIKELY (gst_caps_is_empty (thiscaps)))
|
|
|
|
|
goto no_caps;
|
|
|
|
|
|
|
|
|
|
/* get the peer caps */
|
|
|
|
|
peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), thiscaps);
|
|
|
|
|
GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
|
|
|
|
|
if (peercaps) {
|
|
|
|
|
/* The result is already a subset of our caps */
|
|
|
|
|
caps = peercaps;
|
|
|
|
|
gst_caps_unref (thiscaps);
|
2015-04-28 17:36:44 +02:00
|
|
|
} else {
|
2015-05-14 17:46:12 +02:00
|
|
|
/* no peer, work with our own caps then */
|
|
|
|
|
caps = thiscaps;
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
2016-07-20 17:29:34 +02:00
|
|
|
if (caps == NULL || gst_caps_is_empty (caps))
|
|
|
|
|
goto no_common_caps;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2016-07-20 17:29:34 +02:00
|
|
|
GST_DEBUG_OBJECT (basesrc, "have common caps: %" GST_PTR_FORMAT, caps);
|
2015-08-26 15:44:29 +02:00
|
|
|
|
2016-07-20 17:29:34 +02:00
|
|
|
/* open a connection with these caps */
|
2017-07-11 12:24:03 +02:00
|
|
|
possible = gst_caps_to_format_all (caps, pwsrc->remote->core->type.map);
|
2017-06-21 11:17:12 +02:00
|
|
|
gst_caps_unref (caps);
|
2015-06-12 12:10:27 +02:00
|
|
|
|
2016-07-20 17:29:34 +02:00
|
|
|
/* first disconnect */
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsrc->main_loop);
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsrc->stream->state != PW_STREAM_STATE_UNCONNECTED) {
|
2016-07-20 17:29:34 +02:00
|
|
|
GST_DEBUG_OBJECT (basesrc, "disconnect capture");
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_stream_disconnect (pwsrc->stream);
|
2015-05-14 17:46:12 +02:00
|
|
|
while (TRUE) {
|
2017-05-23 19:15:33 +02:00
|
|
|
enum pw_stream_state state = pwsrc->stream->state;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (basesrc, "waiting for UNCONNECTED, now %s", pw_stream_state_as_string (state));
|
|
|
|
|
if (state == PW_STREAM_STATE_UNCONNECTED)
|
2015-05-14 17:46:12 +02:00
|
|
|
break;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (state == PW_STREAM_STATE_ERROR) {
|
2016-08-05 16:39:26 +02:00
|
|
|
g_ptr_array_unref (possible);
|
2015-05-14 17:46:12 +02:00
|
|
|
goto connect_error;
|
2016-07-20 17:29:34 +02:00
|
|
|
}
|
2015-04-29 17:51:51 +02:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_wait (pwsrc->main_loop);
|
2015-05-14 17:46:12 +02:00
|
|
|
}
|
2016-07-20 17:29:34 +02:00
|
|
|
}
|
2015-04-29 17:51:51 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (basesrc, "connect capture with path %s", pwsrc->path);
|
|
|
|
|
pw_stream_connect (pwsrc->stream,
|
|
|
|
|
PW_DIRECTION_INPUT,
|
|
|
|
|
PW_STREAM_MODE_BUFFER,
|
|
|
|
|
pwsrc->path,
|
|
|
|
|
PW_STREAM_FLAG_AUTOCONNECT,
|
|
|
|
|
possible->len,
|
2017-06-21 15:14:25 +02:00
|
|
|
(const struct spa_format **)possible->pdata);
|
2017-06-21 11:17:12 +02:00
|
|
|
g_ptr_array_free (possible, TRUE);
|
2016-07-20 17:29:34 +02:00
|
|
|
|
|
|
|
|
while (TRUE) {
|
2017-05-23 19:15:33 +02:00
|
|
|
enum pw_stream_state state = pwsrc->stream->state;
|
2016-07-20 17:29:34 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (basesrc, "waiting for PAUSED, now %s", pw_stream_state_as_string (state));
|
|
|
|
|
if (state == PW_STREAM_STATE_PAUSED ||
|
|
|
|
|
state == PW_STREAM_STATE_STREAMING)
|
2016-07-20 17:29:34 +02:00
|
|
|
break;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (state == PW_STREAM_STATE_ERROR)
|
2016-07-20 17:29:34 +02:00
|
|
|
goto connect_error;
|
|
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
if (pwsrc->remote->state == PW_REMOTE_STATE_ERROR)
|
2017-01-17 10:27:58 +01:00
|
|
|
goto connect_error;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_wait (pwsrc->main_loop);
|
2015-04-29 17:51:51 +02:00
|
|
|
}
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2016-07-20 17:29:34 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
result = gst_pipewire_src_stream_start (pwsrc);
|
2016-07-20 17:29:34 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsrc->negotiated = result;
|
2015-07-14 15:47:18 +02:00
|
|
|
|
2015-05-14 17:46:12 +02:00
|
|
|
return result;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2015-05-14 17:46:12 +02:00
|
|
|
no_nego_needed:
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
2015-05-14 17:46:12 +02:00
|
|
|
GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
|
|
|
|
|
if (thiscaps)
|
|
|
|
|
gst_caps_unref (thiscaps);
|
|
|
|
|
return TRUE;
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
2015-05-14 17:46:12 +02:00
|
|
|
no_caps:
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
2015-05-14 17:46:12 +02:00
|
|
|
GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
|
|
|
|
|
("No supported formats found"),
|
|
|
|
|
("This element did not produce valid caps"));
|
|
|
|
|
if (thiscaps)
|
|
|
|
|
gst_caps_unref (thiscaps);
|
2016-07-20 17:29:34 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
no_common_caps:
|
|
|
|
|
{
|
|
|
|
|
GST_ELEMENT_ERROR (basesrc, STREAM, FORMAT,
|
|
|
|
|
("No supported formats found"),
|
|
|
|
|
("This element does not have formats in common with the peer"));
|
|
|
|
|
if (caps)
|
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
return FALSE;
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
2015-04-29 17:51:51 +02:00
|
|
|
connect_error:
|
|
|
|
|
{
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2015-04-29 17:51:51 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
2017-03-17 11:58:09 +01:00
|
|
|
#define PROP(f,key,type,...) \
|
2017-03-31 11:48:24 +02:00
|
|
|
SPA_POD_PROP (f,key,0,type,1,__VA_ARGS__)
|
2017-05-22 13:06:18 +02:00
|
|
|
#define PROP_U_MM(f,key,type,...) \
|
|
|
|
|
SPA_POD_PROP (f,key,SPA_POD_PROP_FLAG_UNSET | \
|
|
|
|
|
SPA_POD_PROP_RANGE_MIN_MAX,type,3,__VA_ARGS__)
|
|
|
|
|
|
2016-07-28 21:19:20 +02:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
on_format_changed (struct pw_listener *listener,
|
|
|
|
|
struct pw_stream *stream,
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_format *format)
|
2016-07-28 21:19:20 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = SPA_CONTAINER_OF (listener, GstPipeWireSrc, stream_format_changed);
|
2016-07-28 21:19:20 +02:00
|
|
|
GstCaps *caps;
|
2016-09-22 08:55:30 +02:00
|
|
|
gboolean res;
|
2017-07-11 12:24:03 +02:00
|
|
|
struct pw_remote *remote = stream->remote;
|
|
|
|
|
struct pw_core *core = remote->core;
|
2016-07-28 21:19:20 +02:00
|
|
|
|
2017-05-17 11:55:48 +02:00
|
|
|
if (format == NULL) {
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsrc, "clear format");
|
|
|
|
|
pw_stream_finish_format (pwsrc->stream, SPA_RESULT_OK, NULL, 0);
|
2017-05-17 17:24:25 +08:00
|
|
|
return;
|
2017-05-17 11:55:48 +02:00
|
|
|
}
|
2017-05-17 17:24:25 +08:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
caps = gst_caps_from_format (format, core->type.map);
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsrc, "we got format %" GST_PTR_FORMAT, caps);
|
|
|
|
|
res = gst_base_src_set_caps (GST_BASE_SRC (pwsrc), caps);
|
2016-07-28 21:19:20 +02:00
|
|
|
gst_caps_unref (caps);
|
2016-08-24 16:26:58 +02:00
|
|
|
|
2016-09-22 08:55:30 +02:00
|
|
|
if (res) {
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_param *params[2];
|
|
|
|
|
struct spa_pod_builder b = { NULL };
|
2017-05-22 13:06:18 +02:00
|
|
|
uint8_t buffer[512];
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_pod_frame f[2];
|
2017-03-17 11:58:09 +01:00
|
|
|
|
|
|
|
|
spa_pod_builder_init (&b, buffer, sizeof (buffer));
|
2017-07-11 12:24:03 +02:00
|
|
|
spa_pod_builder_object (&b, &f[0], 0, core->type.param_alloc_buffers.Buffers,
|
|
|
|
|
PROP_U_MM (&f[1], core->type.param_alloc_buffers.size, SPA_POD_TYPE_INT, 0, 0, INT32_MAX),
|
|
|
|
|
PROP_U_MM (&f[1], core->type.param_alloc_buffers.stride, SPA_POD_TYPE_INT, 0, 0, INT32_MAX),
|
|
|
|
|
PROP_U_MM (&f[1], core->type.param_alloc_buffers.buffers, SPA_POD_TYPE_INT, 16, 0, INT32_MAX),
|
|
|
|
|
PROP (&f[1], core->type.param_alloc_buffers.align, SPA_POD_TYPE_INT, 16));
|
2017-05-25 13:28:15 +02:00
|
|
|
params[0] = SPA_POD_BUILDER_DEREF (&b, f[0].ref, struct spa_param);
|
2017-05-22 13:06:18 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
spa_pod_builder_object (&b, &f[0], 0, core->type.param_alloc_meta_enable.MetaEnable,
|
|
|
|
|
PROP (&f[1], core->type.param_alloc_meta_enable.type, SPA_POD_TYPE_ID, core->type.meta.Header),
|
|
|
|
|
PROP (&f[1], core->type.param_alloc_meta_enable.size, SPA_POD_TYPE_INT, sizeof (struct spa_meta_header)));
|
2017-05-25 13:28:15 +02:00
|
|
|
params[1] = SPA_POD_BUILDER_DEREF (&b, f[0].ref, struct spa_param);
|
2016-09-30 17:07:38 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsrc, "doing finish format");
|
|
|
|
|
pw_stream_finish_format (pwsrc->stream, SPA_RESULT_OK, params, 2);
|
2016-09-22 08:55:30 +02:00
|
|
|
} else {
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_WARNING_OBJECT (pwsrc, "finish format with error");
|
|
|
|
|
pw_stream_finish_format (pwsrc->stream, SPA_RESULT_INVALID_MEDIA_TYPE, NULL, 0);
|
2016-09-22 08:55:30 +02:00
|
|
|
}
|
2016-07-28 21:19:20 +02:00
|
|
|
}
|
|
|
|
|
|
2016-01-07 15:07:15 +01:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_unlock (GstBaseSrc * basesrc)
|
2016-01-07 15:07:15 +01:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (basesrc);
|
2016-01-07 15:07:15 +01:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsrc->main_loop);
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsrc, "setting flushing");
|
|
|
|
|
pwsrc->flushing = TRUE;
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_signal (pwsrc->main_loop, FALSE);
|
|
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2016-01-07 15:07:15 +01:00
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_unlock_stop (GstBaseSrc * basesrc)
|
2016-01-07 15:07:15 +01:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc = GST_PIPEWIRE_SRC (basesrc);
|
2016-01-07 15:07:15 +01:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsrc->main_loop);
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_DEBUG_OBJECT (pwsrc, "unsetting flushing");
|
|
|
|
|
pwsrc->flushing = FALSE;
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2016-01-07 15:07:15 +01:00
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-13 13:04:32 +02:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_event (GstBaseSrc * src, GstEvent * event)
|
2016-04-13 13:04:32 +02:00
|
|
|
{
|
|
|
|
|
gboolean res = FALSE;
|
|
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
|
case GST_EVENT_CUSTOM_UPSTREAM:
|
|
|
|
|
if (gst_video_event_is_force_key_unit (event)) {
|
|
|
|
|
GstClockTime running_time;
|
|
|
|
|
gboolean all_headers;
|
|
|
|
|
guint count;
|
|
|
|
|
|
|
|
|
|
gst_video_event_parse_upstream_force_key_unit (event,
|
|
|
|
|
&running_time, &all_headers, &count);
|
|
|
|
|
|
2016-07-28 21:19:20 +02:00
|
|
|
#if 0
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_buffer_builder_init (&b);
|
2016-05-17 20:14:06 +02:00
|
|
|
|
2016-04-13 13:04:32 +02:00
|
|
|
refresh.last_id = 0;
|
|
|
|
|
refresh.request_type = all_headers ? 1 : 0;
|
|
|
|
|
refresh.pts = running_time;
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_buffer_builder_add_refresh_request (&b, &refresh);
|
2016-05-17 20:14:06 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_buffer_builder_end (&b, &pbuf);
|
2016-04-13 13:04:32 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_OBJECT_LOCK (pwsrc);
|
|
|
|
|
if (pwsrc->stream_state == PW_STREAM_STATE_STREAMING) {
|
|
|
|
|
GST_DEBUG_OBJECT (pwsrc, "send refresh request");
|
|
|
|
|
pw_stream_send_buffer (pwsrc->stream, &pbuf);
|
2016-04-13 13:04:32 +02:00
|
|
|
}
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_OBJECT_UNLOCK (pwsrc);
|
2016-04-13 13:04:32 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_buffer_unref (&pbuf);
|
2016-07-28 21:19:20 +02:00
|
|
|
#endif
|
2016-04-13 13:04:32 +02:00
|
|
|
res = TRUE;
|
|
|
|
|
} else {
|
|
|
|
|
res = GST_BASE_SRC_CLASS (parent_class)->event (src, event);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
res = GST_BASE_SRC_CLASS (parent_class)->event (src, event);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 11:18:10 +02:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_query (GstBaseSrc * src, GstQuery * query)
|
2016-04-28 11:18:10 +02:00
|
|
|
{
|
|
|
|
|
gboolean res = FALSE;
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc;
|
2016-04-28 11:18:10 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsrc = GST_PIPEWIRE_SRC (src);
|
2016-04-28 11:18:10 +02:00
|
|
|
|
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
|
case GST_QUERY_LATENCY:
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_OBJECT_LOCK (pwsrc);
|
|
|
|
|
pwsrc->min_latency = 10000000;
|
|
|
|
|
pwsrc->max_latency = GST_CLOCK_TIME_NONE;
|
|
|
|
|
gst_query_set_latency (query, pwsrc->is_live, pwsrc->min_latency, pwsrc->max_latency);
|
|
|
|
|
GST_OBJECT_UNLOCK (pwsrc);
|
2016-04-28 11:18:10 +02:00
|
|
|
res = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
res = GST_BASE_SRC_CLASS (parent_class)->query (src, query);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-28 17:36:44 +02:00
|
|
|
static GstFlowReturn
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_create (GstPushSrc * psrc, GstBuffer ** buffer)
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc;
|
2016-04-29 16:51:56 +02:00
|
|
|
GstClockTime pts, dts, base_time;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsrc = GST_PIPEWIRE_SRC (psrc);
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (!pwsrc->negotiated)
|
2015-04-28 17:36:44 +02:00
|
|
|
goto not_negotiated;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsrc->main_loop);
|
2015-05-15 15:58:13 +02:00
|
|
|
while (TRUE) {
|
2017-05-23 19:15:33 +02:00
|
|
|
enum pw_stream_state state;
|
2015-06-12 12:10:27 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsrc->flushing)
|
2016-01-07 15:07:15 +01:00
|
|
|
goto streaming_stopped;
|
|
|
|
|
|
2017-07-18 15:28:14 +02:00
|
|
|
if (pwsrc->stream == NULL)
|
|
|
|
|
goto streaming_error;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
state = pwsrc->stream->state;
|
|
|
|
|
if (state == PW_STREAM_STATE_ERROR)
|
2015-06-12 12:10:27 +02:00
|
|
|
goto streaming_error;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (state != PW_STREAM_STATE_STREAMING)
|
2015-05-15 15:58:13 +02:00
|
|
|
goto streaming_stopped;
|
|
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
*buffer = g_queue_pop_head (&pwsrc->queue);
|
2017-01-31 09:57:58 +01:00
|
|
|
GST_DEBUG ("popped buffer %p", *buffer);
|
2016-04-12 17:05:51 +02:00
|
|
|
if (*buffer != NULL)
|
2015-08-25 16:36:01 +02:00
|
|
|
break;
|
2016-04-12 17:05:51 +02:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_wait (pwsrc->main_loop);
|
2015-05-15 15:58:13 +02:00
|
|
|
}
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2016-04-29 16:51:56 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsrc->is_live)
|
2016-09-20 11:20:43 +02:00
|
|
|
base_time = GST_ELEMENT_CAST (psrc)->base_time;
|
|
|
|
|
else
|
|
|
|
|
base_time = 0;
|
|
|
|
|
|
2016-04-29 16:51:56 +02:00
|
|
|
pts = GST_BUFFER_PTS (*buffer);
|
|
|
|
|
dts = GST_BUFFER_DTS (*buffer);
|
2016-04-28 16:42:25 +02:00
|
|
|
|
2016-04-29 16:51:56 +02:00
|
|
|
if (GST_CLOCK_TIME_IS_VALID (pts))
|
|
|
|
|
pts = (pts >= base_time ? pts - base_time : 0);
|
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (dts))
|
|
|
|
|
dts = (dts >= base_time ? dts - base_time : 0);
|
2016-04-28 16:42:25 +02:00
|
|
|
|
2016-04-29 16:51:56 +02:00
|
|
|
GST_INFO ("pts %" G_GUINT64_FORMAT ", dts %"G_GUINT64_FORMAT
|
|
|
|
|
", base-time %"GST_TIME_FORMAT" -> %"GST_TIME_FORMAT", %"GST_TIME_FORMAT,
|
|
|
|
|
GST_BUFFER_PTS (*buffer), GST_BUFFER_DTS (*buffer), GST_TIME_ARGS (base_time),
|
|
|
|
|
GST_TIME_ARGS (pts), GST_TIME_ARGS (dts));
|
2016-04-28 16:42:25 +02:00
|
|
|
|
2016-04-29 16:51:56 +02:00
|
|
|
GST_BUFFER_PTS (*buffer) = pts;
|
|
|
|
|
GST_BUFFER_DTS (*buffer) = dts;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
|
|
|
|
|
|
not_negotiated:
|
|
|
|
|
{
|
|
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
|
|
|
|
}
|
2015-06-12 12:10:27 +02:00
|
|
|
streaming_error:
|
|
|
|
|
{
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2015-06-12 12:10:27 +02:00
|
|
|
return GST_FLOW_ERROR;
|
|
|
|
|
}
|
2015-05-15 15:58:13 +02:00
|
|
|
streaming_stopped:
|
|
|
|
|
{
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2015-05-15 15:58:13 +02:00
|
|
|
return GST_FLOW_FLUSHING;
|
|
|
|
|
}
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_start (GstBaseSrc * basesrc)
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_stop (GstBaseSrc * basesrc)
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *pwsrc;
|
2015-08-24 16:55:29 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsrc = GST_PIPEWIRE_SRC (basesrc);
|
2016-04-12 11:58:33 +02:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsrc->main_loop);
|
2017-05-23 19:15:33 +02:00
|
|
|
clear_queue (pwsrc);
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2015-08-24 16:55:29 +02:00
|
|
|
|
2015-04-28 17:36:44 +02:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2017-07-11 12:24:03 +02:00
|
|
|
on_remote_state_changed (struct pw_listener *listener,
|
|
|
|
|
struct pw_remote *remote)
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
2017-07-11 12:24:03 +02:00
|
|
|
GstPipeWireSrc *pwsrc = SPA_CONTAINER_OF (listener, GstPipeWireSrc, remote_state_changed);
|
|
|
|
|
enum pw_remote_state state = remote->state;
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
GST_DEBUG ("got remote state %s", pw_remote_state_as_string (state));
|
2015-04-28 17:36:44 +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 (pwsrc, RESOURCE, FAILED,
|
2017-07-11 12:24:03 +02:00
|
|
|
("remote error: %s", remote->error), (NULL));
|
2015-07-08 12:11:55 +02:00
|
|
|
break;
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_signal (pwsrc->main_loop, FALSE);
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-09 13:27:43 +01: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;
|
2015-12-09 13:27:43 +01: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));
|
2015-12-09 13:27:43 +01:00
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-28 17:36:44 +02:00
|
|
|
static gboolean
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_open (GstPipeWireSrc * pwsrc)
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
struct pw_properties *props;
|
2015-04-29 17:51:51 +02:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
if (pw_thread_loop_start (pwsrc->main_loop) != SPA_RESULT_OK)
|
2015-07-08 12:11:55 +02:00
|
|
|
goto mainloop_failed;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_lock (pwsrc->main_loop);
|
2017-07-11 15:57:20 +02:00
|
|
|
if ((pwsrc->remote = pw_remote_new (pwsrc->core, NULL)) == NULL)
|
|
|
|
|
goto no_remote;
|
2015-04-29 17:51:51 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
pw_signal_add (&pwsrc->remote->state_changed, &pwsrc->remote_state_changed, on_remote_state_changed);
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
pw_remote_connect (pwsrc->remote);
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2015-04-29 17:51:51 +02:00
|
|
|
while (TRUE) {
|
2017-07-11 12:24:03 +02:00
|
|
|
enum pw_remote_state state = pwsrc->remote->state;
|
2015-04-29 17:51:51 +02:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
GST_DEBUG ("waiting for CONNECTED, now %s", pw_remote_state_as_string (state));
|
|
|
|
|
if (state == PW_REMOTE_STATE_CONNECTED)
|
2015-04-29 17:51:51 +02:00
|
|
|
break;
|
|
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
if (state == PW_REMOTE_STATE_ERROR)
|
2015-04-29 17:51:51 +02:00
|
|
|
goto connect_error;
|
|
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_wait (pwsrc->main_loop);
|
2015-04-29 17:51:51 +02:00
|
|
|
}
|
2015-04-28 17:36:44 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
if (pwsrc->properties) {
|
|
|
|
|
props = pw_properties_new (NULL, NULL);
|
|
|
|
|
gst_structure_foreach (pwsrc->properties, copy_properties, props);
|
2015-12-09 13:27:43 +01:00
|
|
|
} else {
|
|
|
|
|
props = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 15:57:20 +02:00
|
|
|
if ((pwsrc->stream = pw_stream_new (pwsrc->remote, pwsrc->client_name, props)) == NULL)
|
|
|
|
|
goto no_stream;
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_signal_add (&pwsrc->stream->state_changed, &pwsrc->stream_state_changed, on_state_changed);
|
|
|
|
|
pw_signal_add (&pwsrc->stream->format_changed, &pwsrc->stream_format_changed, on_format_changed);
|
|
|
|
|
pw_signal_add (&pwsrc->stream->add_buffer, &pwsrc->stream_add_buffer, on_add_buffer);
|
|
|
|
|
pw_signal_add (&pwsrc->stream->remove_buffer, &pwsrc->stream_remove_buffer, on_remove_buffer);
|
|
|
|
|
pw_signal_add (&pwsrc->stream->new_buffer, &pwsrc->stream_new_buffer, on_new_buffer);
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pwsrc->clock = gst_pipewire_clock_new (pwsrc->stream);
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2015-05-14 17:46:12 +02:00
|
|
|
|
2015-04-28 17:36:44 +02:00
|
|
|
return TRUE;
|
2015-04-29 17:51:51 +02:00
|
|
|
|
|
|
|
|
/* ERRORS */
|
2015-07-08 12:11:55 +02:00
|
|
|
mainloop_failed:
|
|
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_ELEMENT_ERROR (pwsrc, RESOURCE, FAILED, ("error starting mainloop"), (NULL));
|
2015-07-08 12:11:55 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
2017-07-11 15:57:20 +02:00
|
|
|
no_remote:
|
|
|
|
|
{
|
|
|
|
|
GST_ELEMENT_ERROR (pwsrc, RESOURCE, FAILED, ("can't create remote"), (NULL));
|
|
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2015-04-29 17:51:51 +02:00
|
|
|
connect_error:
|
|
|
|
|
{
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
2015-04-29 17:51:51 +02:00
|
|
|
return FALSE;
|
|
|
|
|
}
|
2017-07-11 15:57:20 +02:00
|
|
|
no_stream:
|
|
|
|
|
{
|
|
|
|
|
GST_ELEMENT_ERROR (pwsrc, RESOURCE, FAILED, ("can't create stream"), (NULL));
|
|
|
|
|
pw_thread_loop_unlock (pwsrc->main_loop);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-08 12:11:55 +02:00
|
|
|
static void
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_close (GstPipeWireSrc * pwsrc)
|
2015-07-08 12:11:55 +02:00
|
|
|
{
|
2017-05-23 19:15:33 +02:00
|
|
|
clear_queue (pwsrc);
|
2016-11-25 13:06:23 +01:00
|
|
|
|
2017-06-01 19:25:01 +02:00
|
|
|
pw_thread_loop_stop (pwsrc->main_loop);
|
2016-11-25 13:06:23 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
pw_stream_destroy (pwsrc->stream);
|
|
|
|
|
pwsrc->stream = NULL;
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2017-07-11 12:24:03 +02:00
|
|
|
pw_remote_destroy (pwsrc->remote);
|
|
|
|
|
pwsrc->remote = NULL;
|
2016-11-24 17:00:42 +01:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
GST_OBJECT_LOCK (pwsrc);
|
|
|
|
|
g_clear_object (&pwsrc->clock);
|
|
|
|
|
GST_OBJECT_UNLOCK (pwsrc);
|
2015-07-08 12:11:55 +02:00
|
|
|
}
|
|
|
|
|
|
2015-04-28 17:36:44 +02:00
|
|
|
static GstStateChangeReturn
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_change_state (GstElement * element, GstStateChange transition)
|
2015-04-28 17:36:44 +02:00
|
|
|
{
|
|
|
|
|
GstStateChangeReturn ret;
|
2017-05-23 19:15:33 +02:00
|
|
|
GstPipeWireSrc *this = GST_PIPEWIRE_SRC_CAST (element);
|
2015-04-28 17:36:44 +02:00
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
2017-05-23 19:15:33 +02:00
|
|
|
if (!gst_pipewire_src_open (this))
|
2015-07-08 12:11:55 +02:00
|
|
|
goto open_failed;
|
2015-04-28 17:36:44 +02:00
|
|
|
break;
|
2015-05-14 17:46:12 +02:00
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
|
break;
|
2015-04-28 17:36:44 +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) {
|
2016-04-28 16:42:25 +02:00
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
2017-05-23 19:15:33 +02:00
|
|
|
if (wait_negotiated (this) == PW_STREAM_STATE_ERROR)
|
2016-09-16 13:13:41 +02:00
|
|
|
goto open_failed;
|
|
|
|
|
|
2016-04-28 16:42:25 +02:00
|
|
|
if (gst_base_src_is_live (GST_BASE_SRC (element)))
|
|
|
|
|
ret = GST_STATE_CHANGE_NO_PREROLL;
|
|
|
|
|
break;
|
2015-04-28 17:36:44 +02:00
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
|
break;
|
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
2015-05-14 17:46:12 +02:00
|
|
|
this->negotiated = FALSE;
|
2015-04-28 17:36:44 +02:00
|
|
|
break;
|
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
2017-05-23 19:15:33 +02:00
|
|
|
gst_pipewire_src_close (this);
|
2015-04-28 17:36:44 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
2015-07-08 12:11:55 +02:00
|
|
|
|
|
|
|
|
/* ERRORS */
|
|
|
|
|
open_failed:
|
|
|
|
|
{
|
|
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
|
|
|
|
}
|
2015-04-28 17:36:44 +02:00
|
|
|
}
|