Rework the wire protocol

Send a command stream over the socket.
Implement a new buffer object that holds the data and commands.
Make iterator and builders to parse and construct buffers.
Rework gstreamer elements to use new API for creating and parsing
buffers.
Add _release_buffer to notify a stream when we are done processing the
buffer. This will eventually go all the way to the server and will allow
us to do more complicated buffer management.
This commit is contained in:
Wim Taymans 2015-08-24 16:41:04 +02:00
parent d0f3f3125b
commit c47fcd8105
14 changed files with 937 additions and 229 deletions

View file

@ -35,7 +35,6 @@
#endif
#include "gstfddepay.h"
#include "wire-protocol.h"
#include <gst/net/gstnetcontrolmessagemeta.h>
#include <gst/gst.h>
@ -48,6 +47,7 @@
#include <sys/socket.h>
#include <unistd.h>
#include <client/pinos.h>
GST_DEBUG_CATEGORY_STATIC (gst_fddepay_debug_category);
#define GST_CAT_DEFAULT gst_fddepay_debug_category
@ -168,66 +168,66 @@ static GstFlowReturn
gst_fddepay_transform_ip (GstBaseTransform * trans, GstBuffer * buf)
{
GstFddepay *fddepay = GST_FDDEPAY (trans);
FDMessage msg;
GstMemory *fdmem = NULL;
PinosBuffer pbuf;
PinosPacketIter it;
GstNetControlMessageMeta * meta;
int *fds = NULL;
int fds_len = 0;
int fd = -1;
GSocketControlMessage *msg = NULL;
const PinosBufferHeader *hdr;
gpointer data;
gsize size;
GError *err = NULL;
GST_DEBUG_OBJECT (fddepay, "transform_ip");
if (gst_buffer_get_size (buf) != sizeof (msg)) {
/* We're guaranteed that we can't `read` from a socket across an attached
* file descriptor so we should get the data in chunks no bigger than
* sizeof(FDMessage) */
GST_WARNING_OBJECT (fddepay, "fddepay: Received wrong amount of data "
"between fds.");
goto error;
}
gst_buffer_extract (buf, 0, &msg, sizeof (msg));
gst_buffer_extract_dup (buf, 0, gst_buffer_get_size (buf), &data, &size);
meta = ((GstNetControlMessageMeta*) gst_buffer_get_meta (
buf, GST_NET_CONTROL_MESSAGE_META_API_TYPE));
if (meta &&
g_socket_control_message_get_msg_type (meta->message) == SCM_RIGHTS) {
fds = g_unix_fd_message_steal_fds ((GUnixFDMessage*) meta->message,
&fds_len);
if (meta) {
msg = meta->message;
gst_buffer_remove_meta (buf, (GstMeta *) meta);
meta = NULL;
}
if (fds == NULL || fds_len != 1) {
GST_WARNING_OBJECT (fddepay, "fddepay: Expect to receive 1 FD for each "
"buffer, received %i", fds_len);
goto error;
}
fd = fds[0];
fcntl (fd, F_SETFD, FD_CLOEXEC);
g_free (fds);
fds = NULL;
/* FIXME: Use stat to find out the size of the file, to make sure that the
* size we've been told is the true size for safety and security. */
fdmem = gst_fd_allocator_alloc (fddepay->fd_allocator, fd,
msg.offset + msg.size, GST_FD_MEMORY_FLAG_NONE);
gst_memory_resize (fdmem, msg.offset, msg.size);
pinos_buffer_init_take_data (&pbuf, data, size, msg);
gst_buffer_remove_all_memory (buf);
gst_buffer_remove_meta (buf,
gst_buffer_get_meta (buf, GST_NET_CONTROL_MESSAGE_META_API_TYPE));
gst_buffer_append_memory (buf, fdmem);
fdmem = NULL;
GST_BUFFER_OFFSET (buf) = msg.seq;
pinos_packet_iter_init (&it, &pbuf);
while (pinos_packet_iter_next (&it)) {
switch (pinos_packet_iter_get_type (&it)) {
case PINOS_PACKET_TYPE_FD_PAYLOAD:
{
GstMemory *fdmem = NULL;
PinosPacketFDPayload p;
int fd;
pinos_packet_iter_parse_fd_payload (&it, &p);
fd = pinos_buffer_get_fd (&pbuf, p.fd_index, &err);
if (fd == -1)
goto error;
fdmem = gst_fd_allocator_alloc (fddepay->fd_allocator, fd,
p.offset + p.size, GST_FD_MEMORY_FLAG_NONE);
gst_memory_resize (fdmem, p.offset, p.size);
gst_buffer_append_memory (buf, fdmem);
break;
}
default:
break;
}
}
hdr = pinos_buffer_get_header (&pbuf, NULL);
GST_BUFFER_OFFSET (buf) = hdr->seq;
return GST_FLOW_OK;
error:
if (fds)
g_free (fds);
if (fd >= 0)
close (fd);
return GST_FLOW_ERROR;
{
GST_ELEMENT_ERROR (fddepay, RESOURCE, SETTINGS, (NULL),
("can't get fd: %s", err->message));
g_clear_error (&err);
return GST_FLOW_ERROR;
}
}

View file

@ -53,7 +53,7 @@
#include <string.h>
#include <unistd.h>
#include "wire-protocol.h"
#include <client/pinos.h>
GST_DEBUG_CATEGORY_STATIC (gst_fdpay_debug_category);
#define GST_CAT_DEFAULT gst_fdpay_debug_category
@ -191,7 +191,7 @@ gst_fdpay_transform_size (GstBaseTransform *trans, GstPadDirection direction,
return FALSE;
} else {
/* transform size going downstream */
*othersize = sizeof (FDMessage);
*othersize = sizeof (PinosBuffer) + 30;
}
return TRUE;
@ -241,40 +241,53 @@ gst_fdpay_transform (GstBaseTransform * trans, GstBuffer * inbuf,
GstMemory *fdmem = NULL;
GstMapInfo info;
GError *err = NULL;
GSocketControlMessage *fdmsg = NULL;
FDMessage msg = { 0, };
PinosBuffer pbuf;
PinosPacketBuilder builder;
PinosBufferHeader hdr;
gsize size;
GST_DEBUG_OBJECT (fdpay, "transform_ip");
fdmem = gst_fdpay_get_fd_memory (fdpay, inbuf);
msg.flags = 0;
msg.seq = GST_BUFFER_OFFSET (inbuf);
msg.pts = GST_BUFFER_TIMESTAMP (inbuf) + GST_ELEMENT_CAST (trans)->base_time;
msg.dts_offset = 0;
msg.size = fdmem->size;
msg.offset = fdmem->offset;
hdr.flags = 0;
hdr.seq = GST_BUFFER_OFFSET (inbuf);
hdr.pts = GST_BUFFER_TIMESTAMP (inbuf) + GST_ELEMENT_CAST (trans)->base_time;
hdr.dts_offset = 0;
fdmsg = g_unix_fd_message_new ();
if (!g_unix_fd_message_append_fd ((GUnixFDMessage*) fdmsg,
gst_fd_memory_get_fd (fdmem), &err)) {
pinos_packet_builder_init (&builder, &hdr);
if (!pinos_packet_builder_add_fd_payload (&builder,
fdmem->offset,
fdmem->size,
gst_fd_memory_get_fd (fdmem),
&err))
goto append_fd_failed;
}
pinos_packet_builder_end (&builder, &pbuf);
gst_memory_unref(fdmem);
fdmem = NULL;
gst_buffer_add_net_control_message_meta (outbuf, fdmsg);
g_clear_object (&fdmsg);
gst_buffer_add_net_control_message_meta (outbuf,
pinos_buffer_get_socket_control_message (&pbuf));
size = pinos_buffer_get_size (&pbuf);
gst_buffer_map (outbuf, &info, GST_MAP_WRITE);
memcpy (info.data, &msg, sizeof (msg));
pinos_buffer_store (&pbuf, info.data);
gst_buffer_unmap (outbuf, &info);
gst_buffer_resize (outbuf, 0, size);
pinos_buffer_clear (&pbuf);
return GST_FLOW_OK;
/* ERRORS */
append_fd_failed:
GST_WARNING_OBJECT (trans, "Appending fd failed: %s", err->message);
gst_memory_unref(fdmem);
g_clear_error (&err);
g_clear_object (&fdmsg);
return GST_FLOW_ERROR;
{
GST_WARNING_OBJECT (trans, "Appending fd failed: %s", err->message);
gst_memory_unref(fdmem);
g_clear_error (&err);
return GST_FLOW_ERROR;
}
}

View file

@ -329,10 +329,12 @@ static GstFlowReturn
gst_pinos_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
{
GstPinosSink *pinossink;
PinosBufferInfo info;
GSocketControlMessage *mesg;
PinosBuffer pbuf;
PinosPacketBuilder builder;
GstMemory *mem = NULL;
GstClockTime pts, dts, base;
PinosBufferHeader hdr;
gsize size;
pinossink = GST_PINOS_SINK (bsink);
@ -348,14 +350,15 @@ gst_pinos_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
else if (!GST_CLOCK_TIME_IS_VALID (dts))
dts = pts;
info.flags = 0;
info.seq = GST_BUFFER_OFFSET (buffer);
info.pts = GST_CLOCK_TIME_IS_VALID (pts) ? pts + base : base;
info.dts_offset = GST_CLOCK_TIME_IS_VALID (dts) && GST_CLOCK_TIME_IS_VALID (pts) ? pts - dts : 0;
info.offset = 0;
info.size = gst_buffer_get_size (buffer);
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);
pinos_packet_builder_init (&builder, &hdr);
mesg = g_unix_fd_message_new ();
if (gst_buffer_n_memory (buffer) == 1
&& gst_is_fd_memory (gst_buffer_peek_memory (buffer, 0))) {
mem = gst_buffer_get_memory (buffer, 0);
@ -365,20 +368,22 @@ gst_pinos_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
GST_INFO_OBJECT (bsink, "Buffer cannot be payloaded without copying");
mem = gst_allocator_alloc (pinossink->allocator, info.size, &params);
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, info.size);
gst_buffer_extract (buffer, 0, minfo.data, size);
gst_memory_unmap (mem, &minfo);
}
g_unix_fd_message_append_fd ((GUnixFDMessage*)mesg, gst_fd_memory_get_fd (mem), NULL);
pinos_packet_builder_add_fd_payload (&builder, 0, size, gst_fd_memory_get_fd (mem), NULL);
gst_memory_unref (mem);
info.message = mesg;
pinos_packet_builder_end (&builder, &pbuf);
pinos_main_loop_lock (pinossink->loop);
if (pinos_stream_get_state (pinossink->stream) != PINOS_STREAM_STATE_STREAMING)
goto streaming_error;
pinos_stream_provide_buffer (pinossink->stream, &info);
pinos_stream_provide_buffer (pinossink->stream, &pbuf);
pinos_main_loop_unlock (pinossink->loop);
return GST_FLOW_OK;

View file

@ -251,9 +251,72 @@ on_new_buffer (GObject *gobject,
gpointer user_data)
{
GstPinosSrc *pinossrc = user_data;
PinosBuffer pbuf;
const PinosBufferHeader *hdr;
PinosPacketIter it;
GstBuffer *buf;
GError *error = NULL;
GST_LOG_OBJECT (pinossrc, "got new buffer");
pinos_stream_capture_buffer (pinossrc->stream, &pbuf);
buf = gst_buffer_new ();
hdr = pinos_buffer_get_header (&pbuf, NULL);
if (GST_CLOCK_TIME_IS_VALID (hdr->pts)) {
if (hdr->pts > GST_ELEMENT_CAST (pinossrc)->base_time)
GST_BUFFER_PTS (buf) = hdr->pts - GST_ELEMENT_CAST (pinossrc)->base_time;
if (GST_BUFFER_PTS (buf) + hdr->dts_offset > 0)
GST_BUFFER_DTS (buf) = GST_BUFFER_PTS (buf) + hdr->dts_offset;
}
GST_BUFFER_OFFSET (buf) = hdr->seq;
pinos_packet_iter_init (&it, &pbuf);
while (pinos_packet_iter_next (&it)) {
switch (pinos_packet_iter_get_type (&it)) {
case PINOS_PACKET_TYPE_FD_PAYLOAD:
{
GstMemory *fdmem = NULL;
PinosPacketFDPayload p;
int fd;
GST_DEBUG ("got fd payload");
pinos_packet_iter_parse_fd_payload (&it, &p);
fd = pinos_buffer_get_fd (&pbuf, p.fd_index, &error);
if (fd == -1)
goto no_fds;
fdmem = gst_fd_allocator_alloc (pinossrc->fd_allocator, fd,
p.offset + p.size, GST_FD_MEMORY_FLAG_NONE);
gst_memory_resize (fdmem, p.offset, p.size);
gst_buffer_append_memory (buf, fdmem);
break;
}
default:
break;
}
}
if (pinossrc->current)
gst_buffer_unref (pinossrc->current);
pinossrc->current = buf;
pinos_stream_release_buffer (pinossrc->stream, &pbuf);
pinos_main_loop_signal (pinossrc->loop, FALSE);
return;
/* ERRORS */
no_fds:
{
GST_ELEMENT_ERROR (pinossrc, RESOURCE, FAILED,
("buffer error: %s", error->message), (NULL));
pinos_main_loop_signal (pinossrc->loop, FALSE);
return;
}
}
static void
@ -442,17 +505,12 @@ static GstFlowReturn
gst_pinos_src_create (GstPushSrc * psrc, GstBuffer ** buffer)
{
GstPinosSrc *pinossrc;
PinosBufferInfo info;
gint *fds, n_fds;
GstMemory *fdmem = NULL;
GstBuffer *buf;
pinossrc = GST_PINOS_SRC (psrc);
if (!pinossrc->negotiated)
goto not_negotiated;
again:
pinos_main_loop_lock (pinossrc->loop);
while (TRUE) {
PinosStreamState state;
@ -466,39 +524,12 @@ again:
if (state != PINOS_STREAM_STATE_STREAMING)
goto streaming_stopped;
GST_LOG_OBJECT (pinossrc, "start capture buffer");
pinos_stream_capture_buffer (pinossrc->stream, &info);
if (info.message != NULL) {
GST_LOG_OBJECT (pinossrc, "no message, retry");
break;
}
break;
}
pinos_main_loop_unlock (pinossrc->loop);
if (g_socket_control_message_get_msg_type (info.message) != SCM_RIGHTS)
goto again;
fds = g_unix_fd_message_steal_fds (G_UNIX_FD_MESSAGE (info.message), &n_fds);
if (n_fds < 1 || fds[0] < 0)
goto again;
fdmem = gst_fd_allocator_alloc (pinossrc->fd_allocator, fds[0],
info.offset + info.size, GST_FD_MEMORY_FLAG_NONE);
gst_memory_resize (fdmem, info.offset, info.size);
buf = gst_buffer_new ();
gst_buffer_append_memory (buf, fdmem);
if (GST_CLOCK_TIME_IS_VALID (info.pts)) {
if (info.pts > GST_ELEMENT_CAST (pinossrc)->base_time)
GST_BUFFER_PTS (buf) = info.pts - GST_ELEMENT_CAST (pinossrc)->base_time;
if (GST_BUFFER_PTS (buf) + info.dts_offset > 0)
GST_BUFFER_DTS (buf) = GST_BUFFER_PTS (buf) + info.dts_offset;
}
GST_BUFFER_OFFSET (buf) = info.seq;
*buffer = buf;
*buffer = pinossrc->current;
pinossrc->current = NULL;
return GST_FLOW_OK;

View file

@ -62,6 +62,8 @@ struct _GstPinosSrc {
PinosContext *ctx;
PinosStream *stream;
GstAllocator *fd_allocator;
GstBuffer *current;
};
struct _GstPinosSrcClass {

View file

@ -1,45 +0,0 @@
/* GStreamer
* Copyright (C) 2014 William Manley <will@williammanley.net>
*
* 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_FDPAY_WIRE_PROTOCOL_H_
#define _GST_FDPAY_WIRE_PROTOCOL_H_
#include <stdint.h>
/**
* @flags: possible flags
* @seq: a sequence number
* @pts: a PTS or presentation timestamp
* @dts_offset: an offset to @pts to get the DTS
* @offset: offset in fd
* @size: size of data in fd
*
* Almost the simplest possible FD passing protocol. Each message should have
* a file-descriptor attached which should be mmapable. The data in the FD can
* be found at offset and is size bytes long. */
typedef struct {
guint32 flags;
guint32 seq;
gint64 pts;
gint64 dts_offset;
guint64 offset;
guint64 size;
} FDMessage;
#endif