udpsink: handle scather gather from buffers

Iterate the memory blocks on the buffer and send them using sendmsg.
This commit is contained in:
Wim Taymans 2011-04-05 19:15:11 +02:00
parent 4e7f1633e4
commit 6df597cbbf

View file

@ -488,6 +488,8 @@ socket_last_error_message (void)
#endif #endif
} }
#ifdef G_OS_WIN32
/* version without sendmsg */
static GstFlowReturn static GstFlowReturn
gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer) gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
{ {
@ -565,49 +567,47 @@ gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
return GST_FLOW_OK; return GST_FLOW_OK;
} }
#else /* !G_OS_WIN32 */
#if 0 /* version with sendmsg */
#ifndef G_OS_WIN32
static GstFlowReturn static GstFlowReturn
gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * list) gst_multiudpsink_render (GstBaseSink * bsink, GstBuffer * buffer)
{ {
GstMultiUDPSink *sink; GstMultiUDPSink *sink;
GList *clients; GList *clients;
gint ret, size = 0, num = 0, no_clients = 0; gint ret, size = 0, num = 0, no_clients = 0;
struct iovec *iov; struct iovec *iov;
struct msghdr msg = { 0 }; struct msghdr msg = { 0 };
GstBufferListIterator *it; guint n_mem, i;
guint gsize; gpointer bdata;
GstBuffer *buf; gsize bsize;
GstMemory *mem;
sink = GST_MULTIUDPSINK (bsink); sink = GST_MULTIUDPSINK (bsink);
g_return_val_if_fail (list != NULL, GST_FLOW_ERROR);
it = gst_buffer_list_iterate (list);
g_return_val_if_fail (it != NULL, GST_FLOW_ERROR);
while (gst_buffer_list_iterator_next_group (it)) {
msg.msg_iovlen = 0; msg.msg_iovlen = 0;
size = 0; size = 0;
if ((gsize = gst_buffer_list_iterator_n_buffers (it)) == 0) { n_mem = gst_buffer_n_memory (buffer);
goto invalid_list; if (n_mem == 0)
} goto no_data;
iov = (struct iovec *) g_malloc (gsize * sizeof (struct iovec)); iov = (struct iovec *) g_malloc (n_mem * sizeof (struct iovec));
msg.msg_iov = iov; msg.msg_iov = iov;
while ((buf = gst_buffer_list_iterator_next (it))) { for (i = 0; i < n_mem; i++) {
if (GST_BUFFER_SIZE (buf) > UDP_MAX_SIZE) { mem = gst_buffer_peek_memory (buffer, i, GST_MAP_READ);
bdata = gst_memory_map (mem, &bsize, NULL, GST_MAP_READ);
if (bsize > UDP_MAX_SIZE) {
GST_WARNING ("Attempting to send a UDP packet larger than maximum " GST_WARNING ("Attempting to send a UDP packet larger than maximum "
"size (%d > %d)", GST_BUFFER_SIZE (buf), UDP_MAX_SIZE); "size (%d > %d)", bsize, UDP_MAX_SIZE);
} }
msg.msg_iov[msg.msg_iovlen].iov_len = GST_BUFFER_SIZE (buf); msg.msg_iov[msg.msg_iovlen].iov_len = bsize;
msg.msg_iov[msg.msg_iovlen].iov_base = GST_BUFFER_DATA (buf); msg.msg_iov[msg.msg_iovlen].iov_base = bdata;
msg.msg_iovlen++; msg.msg_iovlen++;
size += GST_BUFFER_SIZE (buf);
size += bsize;
} }
sink->bytes_to_serve += size; sink->bytes_to_serve += size;
@ -635,6 +635,11 @@ gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * list)
if (ret < 0) { if (ret < 0) {
if (!socket_error_is_ignorable ()) { if (!socket_error_is_ignorable ()) {
gchar *errormessage = socket_last_error_message ();
GST_WARNING_OBJECT (sink, "client %p gave error %d (%s)", client,
socket_last_error_code (), errormessage);
g_free (errormessage);
break;
break; break;
} }
} else { } else {
@ -649,22 +654,36 @@ gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * list)
} }
g_mutex_unlock (sink->client_lock); g_mutex_unlock (sink->client_lock);
/* unmap all memory again */
for (i = 0; i < n_mem; i++) {
mem = gst_buffer_peek_memory (buffer, i, GST_MAP_READ);
bsize = msg.msg_iov[i].iov_len;
bdata = msg.msg_iov[i].iov_base;
gst_memory_unmap (mem, bdata, bsize);
}
g_free (iov); g_free (iov);
msg.msg_iov = NULL;
GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num, GST_LOG_OBJECT (sink, "sent %d bytes to %d (of %d) clients", size, num,
no_clients); no_clients);
}
gst_buffer_list_iterator_free (it);
return GST_FLOW_OK; return GST_FLOW_OK;
invalid_list: no_data:
gst_buffer_list_iterator_free (it); {
return GST_FLOW_ERROR; return GST_FLOW_OK;
}
} }
#endif #endif
#if 0
/* DISABLED, core sends buffers to our render one by one, we can't really do
* much better */
static GstFlowReturn
gst_multiudpsink_render_list (GstBaseSink * bsink, GstBufferList * list)
{
}
#endif #endif
static void static void