udpsrc: use g_socket_set_option() to set buffer size with newer GLib versions
authorTim-Philipp Müller <tim.muller@collabora.co.uk>
Fri, 15 Feb 2013 14:11:36 +0000 (14:11 +0000)
committerTim-Philipp Müller <tim.muller@collabora.co.uk>
Fri, 15 Feb 2013 14:11:36 +0000 (14:11 +0000)
So we have to worry less about portability.

https://bugzilla.gnome.org/show_bug.cgi?id=692400

gst/udp/gstudpsrc.c

index 82f183c..100f9e2 100644 (file)
 
 #include <gst/net/gstnetaddressmeta.h>
 
+#if GLIB_CHECK_VERSION (2, 35, 7)
+#include <gio/gnetworking.h>
+#else
 #ifdef HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 #endif
+#endif
 
 /* not 100% correct, but a good upper bound for memory allocation purposes */
 #define MAX_IPV4_UDP_PACKET_SIZE (65536 - 8)
@@ -823,7 +827,39 @@ gst_udpsrc_start (GstBaseSrc * bsrc)
       goto getsockname_error;
   }
 
-#ifdef SO_RCVBUF
+#if GLIB_CHECK_VERSION (2, 35, 7)
+  {
+    gint val = 0;
+
+    if (src->buffer_size != 0) {
+      GError *opt_err = NULL;
+
+      GST_INFO_OBJECT (src, "setting udp buffer of %d bytes", src->buffer_size);
+      /* set buffer size, Note that on Linux this is typically limited to a
+       * maximum of around 100K. Also a minimum of 128 bytes is required on
+       * Linux. */
+      if (!g_socket_set_option (src->used_socket, SOL_SOCKET, SO_RCVBUF,
+              src->buffer_size, &opt_err)) {
+        GST_ELEMENT_WARNING (src, RESOURCE, SETTINGS, (NULL),
+            ("Could not create a buffer of requested %d bytes: %s",
+                src->buffer_size, opt_err->message));
+        g_error_free (opt_err);
+        opt_err = NULL;
+      }
+    }
+
+    /* read the value of the receive buffer. Note that on linux this returns
+     * 2x the value we set because the kernel allocates extra memory for
+     * metadata. The default on Linux is about 100K (which is about 50K
+     * without metadata) */
+    if (g_socket_get_option (src->used_socket, SOL_SOCKET, SO_RCVBUF, &val,
+            NULL)) {
+      GST_INFO_OBJECT (src, "have udp buffer of %d bytes", val);
+    } else {
+      GST_DEBUG_OBJECT (src, "could not get udp buffer size");
+    }
+  }
+#elif defined (SO_RCVBUF)
   {
     gint rcvsize, ret;
     socklen_t len;
@@ -857,6 +893,12 @@ gst_udpsrc_start (GstBaseSrc * bsrc)
     else
       GST_DEBUG_OBJECT (src, "could not get udp buffer size");
   }
+#else
+  if (src->buffer_size != 0) {
+    GST_WARNING_OBJECT (src, "don't know how to set udp buffer size on this "
+        "OS. Consider upgrading your GLib to >= 2.35.7 and re-compiling the "
+        "GStreamer udp plugin");
+  }
 #endif
 
   g_socket_set_broadcast (src->used_socket, TRUE);