Introduce the concept of a Node

Make an object for a processing node.
Implement a sink node. Make it possible to implement Sink and Source
interfaces to provide input/output from the node.
Improve pinosdepay to track fds and handle format changes.
This commit is contained in:
Wim Taymans 2016-05-05 13:31:18 +02:00
parent 7597e48e02
commit b885d40390
27 changed files with 3150 additions and 160 deletions

View file

@ -53,8 +53,13 @@
GST_DEBUG_CATEGORY_STATIC (gst_pinos_depay_debug_category);
#define GST_CAT_DEFAULT gst_pinos_depay_debug_category
/* prototypes */
static GQuark fdids_quark;
enum
{
PROP_0,
PROP_CAPS,
};
/* pad templates */
static GstStaticPadTemplate gst_pinos_depay_src_template =
@ -71,22 +76,97 @@ GST_STATIC_PAD_TEMPLATE ("sink",
/* class initialization */
G_DEFINE_TYPE (GstPinosDepay, gst_pinos_depay, GST_TYPE_ELEMENT);
G_DEFINE_TYPE_WITH_CODE (GstPinosDepay, gst_pinos_depay, GST_TYPE_ELEMENT,
GST_DEBUG_CATEGORY_INIT (gst_pinos_depay_debug_category, "pinosdepay", 0,
"debug category for pinosdepay element"));
static gboolean
gst_pinos_depay_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
{
GstPinosDepay *depay = GST_PINOS_DEPAY (parent);
gboolean res = FALSE;
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEGMENT:
{
GstSegment segment;
gst_segment_init (&segment, GST_FORMAT_TIME);
res = gst_pad_push_event (depay->srcpad, gst_event_new_segment (&segment));
break;
}
case GST_EVENT_CAPS:
{
GstCaps *caps;
GstStructure *str;
gst_event_parse_caps (event, &caps);
str = gst_caps_get_structure (caps, 0);
depay->pinos_input = gst_structure_has_name (str, "application/x-pinos");
gst_event_unref (event);
res = gst_pad_push_event (depay->srcpad, gst_event_new_caps (depay->caps));
break;
}
default:
res = gst_pad_event_default (pad, parent, event);
break;
}
return res;
}
static void
release_fds (GstPinosDepay *this, GstBuffer *buffer)
{
GArray *fdids;
guint i;
PinosBufferBuilder b;
PinosPacketReleaseFDPayload r;
PinosBuffer pbuf;
gsize size;
gpointer data;
GstBuffer *outbuf;
GstEvent *ev;
fdids = gst_mini_object_steal_qdata (GST_MINI_OBJECT_CAST (buffer),
fdids_quark);
if (fdids == NULL)
return;
pinos_buffer_builder_init (&b);
for (i = 0; i < fdids->len; i++) {
r.id = g_array_index (fdids, guint32, i);
GST_LOG ("release fd index %d", r.id);
pinos_buffer_builder_add_release_fd_payload (&b, &r);
}
pinos_buffer_builder_end (&b, &pbuf);
g_array_unref (fdids);
data = pinos_buffer_steal (&pbuf, &size, NULL);
outbuf = gst_buffer_new_wrapped (data, size);
ev = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
gst_structure_new ("GstNetworkMessage",
"object", G_TYPE_OBJECT, this,
"buffer", GST_TYPE_BUFFER, outbuf, NULL));
gst_buffer_unref (outbuf);
gst_pad_push_event (this->sinkpad, ev);
g_object_unref (this);
}
static GstFlowReturn
gst_pinos_depay_chain (GstPad *pad, GstObject * parent, GstBuffer * buffer)
{
GstPinosDepay *depay = GST_PINOS_DEPAY (parent);
GstBuffer *outbuf;
GstBuffer *outbuf = NULL;
GstMapInfo info;
PinosBuffer pbuf;
PinosBufferIter it;
GstNetControlMessageMeta * meta;
GSocketControlMessage *msg = NULL;
GError *err = NULL;
GArray *fdids = NULL;
meta = ((GstNetControlMessageMeta*) gst_buffer_get_meta (
buffer, GST_NET_CONTROL_MESSAGE_META_API_TYPE));
@ -96,13 +176,6 @@ gst_pinos_depay_chain (GstPad *pad, GstObject * parent, GstBuffer * buffer)
meta = NULL;
}
if (msg == NULL) {
gst_buffer_unref (buffer);
return GST_FLOW_OK;
}
outbuf = gst_buffer_new ();
gst_buffer_map (buffer, &info, GST_MAP_READ);
pinos_buffer_init_data (&pbuf, info.data, info.size, msg);
@ -113,9 +186,21 @@ gst_pinos_depay_chain (GstPad *pad, GstObject * parent, GstBuffer * buffer)
{
PinosPacketHeader hdr;
if (!pinos_buffer_iter_parse_header (&it, &hdr))
if (!pinos_buffer_iter_parse_header (&it, &hdr))
goto error;
if (outbuf == NULL)
outbuf = gst_buffer_new ();
GST_INFO ("pts %" G_GUINT64_FORMAT ", dts_offset %"G_GUINT64_FORMAT, hdr.pts, hdr.dts_offset);
#if 0
if (GST_CLOCK_TIME_IS_VALID (hdr.pts)) {
GST_BUFFER_PTS (outbuf) = hdr.pts;
if (GST_BUFFER_PTS (outbuf) + hdr.dts_offset > 0)
GST_BUFFER_DTS (outbuf) = GST_BUFFER_PTS (outbuf) + hdr.dts_offset;
}
#endif
GST_BUFFER_OFFSET (outbuf) = hdr.seq;
break;
}
@ -131,10 +216,35 @@ gst_pinos_depay_chain (GstPad *pad, GstObject * parent, GstBuffer * buffer)
if (fd == -1)
goto error;
if (outbuf == NULL)
outbuf = gst_buffer_new ();
fdmem = gst_fd_allocator_alloc (depay->fd_allocator, fd,
p.offset + p.size, GST_FD_MEMORY_FLAG_NONE);
gst_memory_resize (fdmem, p.offset, p.size);
gst_buffer_append_memory (outbuf, fdmem);
if (fdids == NULL)
fdids = g_array_new (FALSE, FALSE, sizeof (guint32));
GST_LOG ("track fd index %d", p.id);
g_array_append_val (fdids, p.id);
break;
}
case PINOS_PACKET_TYPE_FORMAT_CHANGE:
{
PinosPacketFormatChange change;
GstCaps *caps;
if (!pinos_buffer_iter_parse_format_change (&it, &change))
goto error;
GST_DEBUG ("got format change %d %s", change.id, change.format);
caps = gst_caps_from_string (change.format);
if (caps) {
gst_caps_take (&depay->caps, caps);
gst_pad_push_event (depay->srcpad, gst_event_new_caps (depay->caps));
}
break;
}
default:
@ -145,7 +255,17 @@ gst_pinos_depay_chain (GstPad *pad, GstObject * parent, GstBuffer * buffer)
gst_buffer_unmap (buffer, &info);
gst_buffer_unref (buffer);
return gst_pad_push (depay->srcpad, outbuf);
if (outbuf) {
if (fdids != NULL) {
gst_mini_object_set_qdata (GST_MINI_OBJECT_CAST (outbuf),
fdids_quark, fdids, NULL);
gst_mini_object_weak_ref (GST_MINI_OBJECT_CAST (outbuf),
(GstMiniObjectNotify) release_fds, g_object_ref (depay));
}
return gst_pad_push (depay->srcpad, outbuf);
}
else
return GST_FLOW_OK;
error:
{
@ -157,6 +277,45 @@ error:
}
}
static void
gst_pinos_depay_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstPinosDepay *depay = GST_PINOS_DEPAY (object);
switch (prop_id) {
case PROP_CAPS:
{
const GstCaps *caps;
caps = gst_value_get_caps (value);
gst_caps_replace (&depay->caps, (GstCaps *)caps);
break;
}
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_pinos_depay_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstPinosDepay *depay = GST_PINOS_DEPAY (object);
switch (prop_id) {
case PROP_CAPS:
gst_value_set_caps (value, depay->caps);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_pinos_depay_finalize (GObject * object)
{
@ -164,6 +323,7 @@ gst_pinos_depay_finalize (GObject * object)
GST_DEBUG_OBJECT (depay, "finalize");
gst_caps_replace (&depay->caps, NULL);
g_object_unref (depay->fd_allocator);
G_OBJECT_CLASS (gst_pinos_depay_parent_class)->finalize (object);
@ -176,6 +336,15 @@ gst_pinos_depay_class_init (GstPinosDepayClass * klass)
GstElementClass *element_class =
GST_ELEMENT_CLASS (klass);
gobject_class->finalize = gst_pinos_depay_finalize;
gobject_class->set_property = gst_pinos_depay_set_property;
gobject_class->get_property = gst_pinos_depay_get_property;
g_object_class_install_property (gobject_class, PROP_CAPS,
g_param_spec_boxed ("caps", "Caps",
"The caps of the source pad", GST_TYPE_CAPS,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
/* Setting up pads and setting metadata should be moved to
base_class_init if you intend to subclass this class. */
gst_element_class_add_pad_template (element_class,
@ -188,7 +357,10 @@ gst_pinos_depay_class_init (GstPinosDepayClass * klass)
"Pinos Depayloader for zero-copy IPC via Pinos",
"Wim Taymans <wim.taymans@gmail.com>");
gobject_class->finalize = gst_pinos_depay_finalize;
GST_DEBUG_CATEGORY_INIT (gst_pinos_depay_debug_category, "pinosdepay", 0,
"debug category for pinosdepay element");
fdids_quark = g_quark_from_static_string ("GstPinosDepayFDIds");
}
static void
@ -199,6 +371,7 @@ gst_pinos_depay_init (GstPinosDepay * depay)
depay->sinkpad = gst_pad_new_from_static_template (&gst_pinos_depay_sink_template, "sink");
gst_pad_set_chain_function (depay->sinkpad, gst_pinos_depay_chain);
gst_pad_set_event_function (depay->sinkpad, gst_pinos_depay_sink_event);
gst_element_add_pad (GST_ELEMENT (depay), depay->sinkpad);
depay->fd_allocator = gst_fd_allocator_new ();

View file

@ -38,6 +38,9 @@ struct _GstPinosDepay
{
GstElement parent;
GstCaps *caps;
gboolean pinos_input;
GstPad *srcpad, *sinkpad;
GstAllocator *fd_allocator;
};

737
pinos/gst/gstpinosprovide.c Normal file
View file

@ -0,0 +1,737 @@
/* 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.
*/
/**
* SECTION:element-pinossink
*
* <refsect2>
* <title>Example launch line</title>
* |[
* gst-launch -v videotestsrc ! pinossink
* ]| Sends a test video source to pinos
* </refsect2>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstpinossink.h"
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <unistd.h>
#include <gio/gunixfdmessage.h>
#include <gst/allocators/gstfdmemory.h>
#include <gst/video/video.h>
#include "gsttmpfileallocator.h"
GST_DEBUG_CATEGORY_STATIC (pinos_sink_debug);
#define GST_CAT_DEFAULT pinos_sink_debug
enum
{
PROP_0,
PROP_CLIENT_NAME,
PROP_STREAM_PROPERTIES
};
#define PINOSS_VIDEO_CAPS GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL)
static GstStaticPadTemplate gst_pinos_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS_ANY
);
#define gst_pinos_sink_parent_class parent_class
G_DEFINE_TYPE (GstPinosSink, gst_pinos_sink, GST_TYPE_BASE_SINK);
static void gst_pinos_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_pinos_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static GstStateChangeReturn
gst_pinos_sink_change_state (GstElement * element, GstStateChange transition);
static gboolean gst_pinos_sink_setcaps (GstBaseSink * bsink, GstCaps * caps);
static GstCaps *gst_pinos_sink_sink_fixate (GstBaseSink * bsink,
GstCaps * caps);
static GstFlowReturn gst_pinos_sink_render (GstBaseSink * psink,
GstBuffer * buffer);
static gboolean gst_pinos_sink_start (GstBaseSink * basesink);
static gboolean gst_pinos_sink_stop (GstBaseSink * basesink);
static void
gst_pinos_sink_finalize (GObject * object)
{
GstPinosSink *pinossink = GST_PINOS_SINK (object);
if (pinossink->properties)
gst_structure_free (pinossink->properties);
g_hash_table_unref (pinossink->fdids);
g_object_unref (pinossink->allocator);
g_free (pinossink->client_name);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static gboolean
gst_pinos_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
{
GstPinosSink *pinossink = GST_PINOS_SINK (bsink);
gst_query_add_allocation_param (query, pinossink->allocator, NULL);
return TRUE;
}
static void
gst_pinos_sink_class_init (GstPinosSinkClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
GstBaseSinkClass *gstbasesink_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gstbasesink_class = (GstBaseSinkClass *) klass;
gobject_class->finalize = gst_pinos_sink_finalize;
gobject_class->set_property = gst_pinos_sink_set_property;
gobject_class->get_property = gst_pinos_sink_get_property;
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));
g_object_class_install_property (gobject_class,
PROP_STREAM_PROPERTIES,
g_param_spec_boxed ("stream-properties",
"stream properties",
"list of pinos stream properties",
GST_TYPE_STRUCTURE,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
gstelement_class->change_state = gst_pinos_sink_change_state;
gst_element_class_set_static_metadata (gstelement_class,
"Pinos sink", "Sink/Video",
"Send video to pinos", "Wim Taymans <wim.taymans@gmail.com>");
gst_element_class_add_pad_template (gstelement_class,
gst_static_pad_template_get (&gst_pinos_sink_template));
gstbasesink_class->set_caps = gst_pinos_sink_setcaps;
gstbasesink_class->fixate = gst_pinos_sink_sink_fixate;
gstbasesink_class->propose_allocation = gst_pinos_sink_propose_allocation;
gstbasesink_class->start = gst_pinos_sink_start;
gstbasesink_class->stop = gst_pinos_sink_stop;
gstbasesink_class->render = gst_pinos_sink_render;
GST_DEBUG_CATEGORY_INIT (pinos_sink_debug, "pinossink", 0,
"Pinos Sink");
}
static void
gst_pinos_sink_init (GstPinosSink * sink)
{
sink->allocator = gst_tmpfile_allocator_new ();
sink->client_name = pinos_client_name();
sink->fdids = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL,
(GDestroyNotify) gst_buffer_unref);
}
static GstCaps *
gst_pinos_sink_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
{
GstStructure *structure;
caps = gst_caps_make_writable (caps);
structure = gst_caps_get_structure (caps, 0);
if (gst_structure_has_name (structure, "video/x-raw")) {
gst_structure_fixate_field_nearest_int (structure, "width", 320);
gst_structure_fixate_field_nearest_int (structure, "height", 240);
gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
gst_structure_fixate_field_nearest_fraction (structure,
"pixel-aspect-ratio", 1, 1);
else
gst_structure_set (structure, "pixel-aspect-ratio", GST_TYPE_FRACTION, 1, 1,
NULL);
if (gst_structure_has_field (structure, "colorimetry"))
gst_structure_fixate_field_string (structure, "colorimetry", "bt601");
if (gst_structure_has_field (structure, "chroma-site"))
gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
if (gst_structure_has_field (structure, "interlace-mode"))
gst_structure_fixate_field_string (structure, "interlace-mode",
"progressive");
else
gst_structure_set (structure, "interlace-mode", G_TYPE_STRING,
"progressive", NULL);
} else if (gst_structure_has_name (structure, "audio/x-raw")) {
gst_structure_fixate_field_string (structure, "format", "S16LE");
gst_structure_fixate_field_nearest_int (structure, "channels", 2);
gst_structure_fixate_field_nearest_int (structure, "rate", 44100);
}
caps = GST_BASE_SINK_CLASS (parent_class)->fixate (bsink, caps);
return caps;
}
static void
gst_pinos_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstPinosSink *pinossink = GST_PINOS_SINK (object);
switch (prop_id) {
case PROP_CLIENT_NAME:
g_free (pinossink->client_name);
pinossink->client_name = g_value_dup_string (value);
break;
case PROP_STREAM_PROPERTIES:
if (pinossink->properties)
gst_structure_free (pinossink->properties);
pinossink->properties =
gst_structure_copy (gst_value_get_structure (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_pinos_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
GstPinosSink *pinossink = GST_PINOS_SINK (object);
switch (prop_id) {
case PROP_CLIENT_NAME:
g_value_set_string (value, pinossink->client_name);
break;
case PROP_STREAM_PROPERTIES:
gst_value_set_structure (value, pinossink->properties);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
on_new_buffer (GObject *gobject,
gpointer user_data)
{
GstPinosSink *pinossink = user_data;
PinosBuffer *pbuf;
PinosBufferIter it;
GST_LOG_OBJECT (pinossink, "got new buffer");
if (!pinos_stream_peek_buffer (pinossink->stream, &pbuf)) {
g_warning ("failed to capture buffer");
return;
}
pinos_buffer_iter_init (&it, pbuf);
while (pinos_buffer_iter_next (&it)) {
switch (pinos_buffer_iter_get_type (&it)) {
case PINOS_PACKET_TYPE_RELEASE_FD_PAYLOAD:
{
PinosPacketReleaseFDPayload p;
if (!pinos_buffer_iter_parse_release_fd_payload (&it, &p))
continue;
GST_LOG ("fd index %d is released", p.id);
g_hash_table_remove (pinossink->fdids, GINT_TO_POINTER (p.id));
break;
}
case PINOS_PACKET_TYPE_REFRESH_REQUEST:
{
PinosPacketRefreshRequest p;
if (!pinos_buffer_iter_parse_refresh_request (&it, &p))
continue;
GST_LOG ("refresh request");
gst_pad_push_event (GST_BASE_SINK_PAD (pinossink),
gst_video_event_new_upstream_force_key_unit (p.pts,
p.request_type == 1, 0));
break;
}
default:
break;
}
}
}
static void
on_stream_notify (GObject *gobject,
GParamSpec *pspec,
gpointer user_data)
{
PinosStreamState state;
PinosStream *stream = PINOS_STREAM (gobject);
GstPinosSink *pinossink = user_data;
state = pinos_stream_get_state (stream);
GST_DEBUG ("got stream state %d", state);
switch (state) {
case PINOS_STREAM_STATE_UNCONNECTED:
case PINOS_STREAM_STATE_CONNECTING:
case PINOS_STREAM_STATE_STARTING:
case PINOS_STREAM_STATE_STREAMING:
case PINOS_STREAM_STATE_READY:
break;
case PINOS_STREAM_STATE_ERROR:
GST_ELEMENT_ERROR (pinossink, RESOURCE, FAILED,
("stream error: %s",
pinos_stream_get_error (stream)->message), (NULL));
break;
}
pinos_main_loop_signal (pinossink->loop, FALSE);
}
static gboolean
gst_pinos_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
{
GstPinosSink *pinossink;
gchar *str;
PinosStreamState state;
gboolean res = FALSE;
pinossink = GST_PINOS_SINK (bsink);
str = gst_caps_to_string (caps);
pinos_main_loop_lock (pinossink->loop);
state = pinos_stream_get_state (pinossink->stream);
if (state == PINOS_STREAM_STATE_ERROR)
goto start_error;
if (state == PINOS_STREAM_STATE_STREAMING) {
PinosBufferBuilder builder;
PinosPacketFormatChange change;
PinosBuffer pbuf;
pinos_buffer_builder_init (&builder);
change.id = 1;
change.format = str;
pinos_buffer_builder_add_format_change (&builder, &change);
pinos_buffer_builder_end (&builder, &pbuf);
res = pinos_stream_send_buffer (pinossink->stream, &pbuf);
pinos_buffer_clear (&pbuf);
} else {
GBytes *format = g_bytes_new_take (str, strlen (str) + 1);
res = pinos_stream_start (pinossink->stream, format, PINOS_STREAM_MODE_BUFFER);
while (TRUE) {
state = pinos_stream_get_state (pinossink->stream);
if (state == PINOS_STREAM_STATE_STREAMING)
break;
if (state == PINOS_STREAM_STATE_ERROR)
goto start_error;
pinos_main_loop_wait (pinossink->loop);
}
}
pinos_main_loop_unlock (pinossink->loop);
pinossink->negotiated = res;
return res;
start_error:
{
GST_ERROR ("could not start stream");
pinos_main_loop_unlock (pinossink->loop);
return FALSE;
}
}
static GstFlowReturn
gst_pinos_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
{
GstPinosSink *pinossink;
PinosBuffer pbuf;
PinosBufferBuilder builder;
GstMemory *mem = NULL;
GstClockTime pts, dts, base;
PinosPacketHeader hdr;
PinosPacketFDPayload p;
gsize size;
GError *err = NULL;
gboolean tmpfile, res;
pinossink = GST_PINOS_SINK (bsink);
if (!pinossink->negotiated)
goto not_negotiated;
base = GST_ELEMENT_CAST (bsink)->base_time;
pts = GST_BUFFER_PTS (buffer);
dts = GST_BUFFER_DTS (buffer);
if (!GST_CLOCK_TIME_IS_VALID (pts))
pts = dts;
else if (!GST_CLOCK_TIME_IS_VALID (dts))
dts = pts;
hdr.flags = 0;
hdr.seq = GST_BUFFER_OFFSET (buffer);
hdr.pts = GST_CLOCK_TIME_IS_VALID (pts) ? pts + base : base;
hdr.dts_offset = GST_CLOCK_TIME_IS_VALID (dts) && GST_CLOCK_TIME_IS_VALID (pts) ? pts - dts : 0;
size = gst_buffer_get_size (buffer);
if (gst_buffer_n_memory (buffer) == 1
&& gst_is_fd_memory (gst_buffer_peek_memory (buffer, 0))) {
mem = gst_buffer_get_memory (buffer, 0);
tmpfile = gst_is_tmpfile_memory (mem);
} else {
GstMapInfo minfo;
GstAllocationParams params = {0, 0, 0, 0, { NULL, }};
GST_INFO_OBJECT (bsink, "Buffer cannot be payloaded without copying");
mem = gst_allocator_alloc (pinossink->allocator, size, &params);
if (!gst_memory_map (mem, &minfo, GST_MAP_WRITE))
goto map_error;
gst_buffer_extract (buffer, 0, minfo.data, size);
gst_memory_unmap (mem, &minfo);
tmpfile = TRUE;
}
pinos_buffer_builder_init (&builder);
pinos_buffer_builder_add_header (&builder, &hdr);
p.fd_index = pinos_buffer_builder_add_fd (&builder, gst_fd_memory_get_fd (mem), &err);
if (p.fd_index == -1)
goto add_fd_failed;
p.id = pinossink->id_counter++;
p.offset = 0;
p.size = size;
pinos_buffer_builder_add_fd_payload (&builder, &p);
pinos_buffer_builder_end (&builder, &pbuf);
gst_memory_unref (mem);
pinos_main_loop_lock (pinossink->loop);
if (pinos_stream_get_state (pinossink->stream) != PINOS_STREAM_STATE_STREAMING)
goto streaming_error;
res = pinos_stream_send_buffer (pinossink->stream, &pbuf);
pinos_buffer_clear (&pbuf);
pinos_main_loop_unlock (pinossink->loop);
if (res && !tmpfile) {
/* keep the buffer around until we get the release fd message */
g_hash_table_insert (pinossink->fdids, GINT_TO_POINTER (p.id), gst_buffer_ref (buffer));
}
return GST_FLOW_OK;
not_negotiated:
{
return GST_FLOW_NOT_NEGOTIATED;
}
map_error:
{
GST_ELEMENT_ERROR (pinossink, RESOURCE, FAILED,
("failed to map buffer"), (NULL));
return GST_FLOW_ERROR;
}
add_fd_failed:
{
GST_ELEMENT_ERROR (pinossink, RESOURCE, FAILED,
("failed to add fd: %s", err->message), (NULL));
pinos_buffer_builder_clear (&builder);
return GST_FLOW_ERROR;
}
streaming_error:
{
pinos_main_loop_unlock (pinossink->loop);
return GST_FLOW_ERROR;
}
}
static gboolean
copy_properties (GQuark field_id,
const GValue *value,
gpointer user_data)
{
PinosProperties *properties = user_data;
if (G_VALUE_HOLDS_STRING (value))
pinos_properties_set (properties,
g_quark_to_string (field_id),
g_value_get_string (value));
return TRUE;
}
static gboolean
gst_pinos_sink_start (GstBaseSink * basesink)
{
GstPinosSink *pinossink = GST_PINOS_SINK (basesink);
PinosProperties *props;
pinossink->negotiated = FALSE;
if (pinossink->properties) {
props = pinos_properties_new (NULL, NULL);
gst_structure_foreach (pinossink->properties, copy_properties, props);
} else {
props = NULL;
}
pinos_main_loop_lock (pinossink->loop);
pinossink->stream = pinos_stream_new (pinossink->ctx, pinossink->client_name, props);
g_signal_connect (pinossink->stream, "notify::state", (GCallback) on_stream_notify, pinossink);
g_signal_connect (pinossink->stream, "new-buffer", (GCallback) on_new_buffer, pinossink);
pinos_stream_connect_provide (pinossink->stream, 0, g_bytes_new_static ("ANY", strlen ("ANY")+1));
while (TRUE) {
PinosStreamState state = pinos_stream_get_state (pinossink->stream);
if (state == PINOS_STREAM_STATE_READY)
break;
if (state == PINOS_STREAM_STATE_ERROR)
goto connect_error;
pinos_main_loop_wait (pinossink->loop);
}
pinos_main_loop_unlock (pinossink->loop);
pinossink->negotiated = TRUE;
return TRUE;
connect_error:
{
GST_ERROR ("could not connect stream");
pinos_main_loop_unlock (pinossink->loop);
return FALSE;
}
}
static gboolean
gst_pinos_sink_stop (GstBaseSink * basesink)
{
GstPinosSink *pinossink = GST_PINOS_SINK (basesink);
pinos_main_loop_lock (pinossink->loop);
if (pinossink->stream) {
pinos_stream_stop (pinossink->stream);
pinos_stream_disconnect (pinossink->stream);
g_clear_object (&pinossink->stream);
}
pinos_main_loop_unlock (pinossink->loop);
pinossink->negotiated = FALSE;
return TRUE;
}
static void
on_context_notify (GObject *gobject,
GParamSpec *pspec,
gpointer user_data)
{
GstPinosSink *pinossink = user_data;
PinosContext *ctx = PINOS_CONTEXT (gobject);
PinosContextState state;
state = pinos_context_get_state (ctx);
GST_DEBUG ("got context state %d", state);
switch (state) {
case PINOS_CONTEXT_STATE_UNCONNECTED:
case PINOS_CONTEXT_STATE_CONNECTING:
case PINOS_CONTEXT_STATE_REGISTERING:
case PINOS_CONTEXT_STATE_READY:
break;
case PINOS_CONTEXT_STATE_ERROR:
GST_ELEMENT_ERROR (pinossink, RESOURCE, FAILED,
("context error: %s",
pinos_context_get_error (pinossink->ctx)->message), (NULL));
break;
}
pinos_main_loop_signal (pinossink->loop, FALSE);
}
static gboolean
gst_pinos_sink_open (GstPinosSink * pinossink)
{
GError *error = NULL;
pinossink->context = g_main_context_new ();
GST_DEBUG ("context %p", pinossink->context);
pinossink->loop = pinos_main_loop_new (pinossink->context, "pinos-sink-loop");
if (!pinos_main_loop_start (pinossink->loop, &error))
goto mainloop_error;
pinos_main_loop_lock (pinossink->loop);
pinossink->ctx = pinos_context_new (pinossink->context, g_get_application_name (), NULL);
g_signal_connect (pinossink->ctx, "notify::state", (GCallback) on_context_notify, pinossink);
pinos_context_connect(pinossink->ctx, PINOS_CONTEXT_FLAGS_NONE);
while (TRUE) {
PinosContextState state = pinos_context_get_state (pinossink->ctx);
if (state == PINOS_CONTEXT_STATE_READY)
break;
if (state == PINOS_CONTEXT_STATE_ERROR)
goto connect_error;
pinos_main_loop_wait (pinossink->loop);
}
pinos_main_loop_unlock (pinossink->loop);
return TRUE;
/* ERRORS */
mainloop_error:
{
GST_ELEMENT_ERROR (pinossink, RESOURCE, FAILED,
("Failed to start mainloop: %s", error->message), (NULL));
return FALSE;
}
connect_error:
{
pinos_main_loop_unlock (pinossink->loop);
return FALSE;
}
}
static gboolean
gst_pinos_sink_close (GstPinosSink * pinossink)
{
pinos_main_loop_lock (pinossink->loop);
if (pinossink->stream) {
pinos_stream_disconnect (pinossink->stream);
}
if (pinossink->ctx) {
pinos_context_disconnect (pinossink->ctx);
while (TRUE) {
PinosContextState state = pinos_context_get_state (pinossink->ctx);
if (state == PINOS_CONTEXT_STATE_UNCONNECTED)
break;
if (state == PINOS_CONTEXT_STATE_ERROR)
break;
pinos_main_loop_wait (pinossink->loop);
}
}
pinos_main_loop_unlock (pinossink->loop);
pinos_main_loop_stop (pinossink->loop);
g_clear_object (&pinossink->loop);
g_clear_object (&pinossink->stream);
g_clear_object (&pinossink->ctx);
g_main_context_unref (pinossink->context);
return TRUE;
}
static GstStateChangeReturn
gst_pinos_sink_change_state (GstElement * element, GstStateChange transition)
{
GstStateChangeReturn ret;
GstPinosSink *this = GST_PINOS_SINK_CAST (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
if (!gst_pinos_sink_open (this))
goto open_failed;
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
/* uncork and start recording */
break;
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
/* stop recording ASAP by corking */
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
g_hash_table_remove_all (this->fdids);
break;
case GST_STATE_CHANGE_READY_TO_NULL:
g_hash_table_remove_all (this->fdids);
gst_pinos_sink_close (this);
break;
default:
break;
}
return ret;
/* ERRORS */
open_failed:
{
return GST_STATE_CHANGE_FAILURE;
}
}

View file

@ -0,0 +1,79 @@
/* 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.
*/
#ifndef __GST_PINOS_SINK_H__
#define __GST_PINOS_SINK_H__
#include <gst/gst.h>
#include <gst/base/gstbasesink.h>
#include <client/pinos.h>
G_BEGIN_DECLS
#define GST_TYPE_PINOS_SINK \
(gst_pinos_sink_get_type())
#define GST_PINOS_SINK(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_PINOS_SINK,GstPinosSink))
#define GST_PINOS_SINK_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_PINOS_SINK,GstPinosSinkClass))
#define GST_IS_PINOS_SINK(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_PINOS_SINK))
#define GST_IS_PINOS_SINK_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_PINOS_SINK))
#define GST_PINOS_SINK_CAST(obj) \
((GstPinosSink *) (obj))
typedef struct _GstPinosSink GstPinosSink;
typedef struct _GstPinosSinkClass GstPinosSinkClass;
/**
* GstPinosSink:
*
* Opaque data structure.
*/
struct _GstPinosSink {
GstBaseSink element;
/*< private >*/
gchar *client_name;
/* video state */
gboolean negotiated;
GMainContext *context;
PinosMainLoop *loop;
PinosContext *ctx;
PinosStream *stream;
GstAllocator *allocator;
GstStructure *properties;
guint32 id_counter;
GHashTable *fdids;
};
struct _GstPinosSinkClass {
GstBaseSinkClass parent_class;
};
GType gst_pinos_sink_get_type (void);
G_END_DECLS
#endif /* __GST_PINOS_SINK_H__ */

View file

@ -52,6 +52,7 @@ GST_DEBUG_CATEGORY_STATIC (pinos_sink_debug);
enum
{
PROP_0,
PROP_PATH,
PROP_CLIENT_NAME,
PROP_STREAM_PROPERTIES
};
@ -95,6 +96,7 @@ gst_pinos_sink_finalize (GObject * object)
gst_structure_free (pinossink->properties);
g_hash_table_unref (pinossink->fdids);
g_object_unref (pinossink->allocator);
g_free (pinossink->path);
g_free (pinossink->client_name);
G_OBJECT_CLASS (parent_class)->finalize (object);
@ -124,6 +126,15 @@ gst_pinos_sink_class_init (GstPinosSinkClass * klass)
gobject_class->set_property = gst_pinos_sink_set_property;
gobject_class->get_property = gst_pinos_sink_get_property;
g_object_class_install_property (gobject_class,
PROP_PATH,
g_param_spec_string ("path",
"Path",
"The sink path to connect to (NULL = default)",
NULL,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (gobject_class,
PROP_CLIENT_NAME,
g_param_spec_string ("client-name",
@ -222,6 +233,11 @@ gst_pinos_sink_set_property (GObject * object, guint prop_id,
GstPinosSink *pinossink = GST_PINOS_SINK (object);
switch (prop_id) {
case PROP_PATH:
g_free (pinossink->path);
pinossink->path = g_value_dup_string (value);
break;
case PROP_CLIENT_NAME:
g_free (pinossink->client_name);
pinossink->client_name = g_value_dup_string (value);
@ -247,6 +263,10 @@ gst_pinos_sink_get_property (GObject * object, guint prop_id,
GstPinosSink *pinossink = GST_PINOS_SINK (object);
switch (prop_id) {
case PROP_PATH:
g_value_set_string (value, pinossink->path);
break;
case PROP_CLIENT_NAME:
g_value_set_string (value, pinossink->client_name);
break;
@ -270,6 +290,11 @@ on_new_buffer (GObject *gobject,
PinosBufferIter it;
GST_LOG_OBJECT (pinossink, "got new buffer");
if (pinossink->stream == NULL) {
GST_LOG_OBJECT (pinossink, "no stream");
return;
}
if (!pinos_stream_peek_buffer (pinossink->stream, &pbuf)) {
g_warning ("failed to capture buffer");
return;
@ -466,6 +491,8 @@ gst_pinos_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
gst_memory_unref (mem);
GST_LOG ("sending fd index %d", p.id);
pinos_main_loop_lock (pinossink->loop);
if (pinos_stream_get_state (pinossink->stream) != PINOS_STREAM_STATE_STREAMING)
goto streaming_error;
@ -537,7 +564,7 @@ gst_pinos_sink_start (GstBaseSink * basesink)
g_signal_connect (pinossink->stream, "notify::state", (GCallback) on_stream_notify, pinossink);
g_signal_connect (pinossink->stream, "new-buffer", (GCallback) on_new_buffer, pinossink);
pinos_stream_connect_provide (pinossink->stream, 0, g_bytes_new_static ("ANY", strlen ("ANY")+1));
pinos_stream_connect_sink (pinossink->stream, pinossink->path, 0, g_bytes_new_static ("ANY", strlen ("ANY")+1));
while (TRUE) {
PinosStreamState state = pinos_stream_get_state (pinossink->stream);

View file

@ -52,6 +52,7 @@ struct _GstPinosSink {
GstBaseSink element;
/*< private >*/
gchar *path;
gchar *client_name;
/* video state */

View file

@ -435,10 +435,11 @@ on_new_buffer (GObject *gobject,
break;
}
}
g_queue_push_tail (&pinossrc->queue, buf);
pinos_main_loop_signal (pinossrc->loop, FALSE);
if (buf) {
g_queue_push_tail (&pinossrc->queue, buf);
pinos_main_loop_signal (pinossrc->loop, FALSE);
}
return;
/* ERRORS */