netclock: Add simple network clock server and client examples
authorJan Schmidt <jan@centricular.com>
Sat, 10 Jan 2015 10:42:00 +0000 (21:42 +1100)
committerJan Schmidt <jan@centricular.com>
Wed, 21 Jan 2015 11:27:18 +0000 (22:27 +1100)
configure.ac
tests/examples/Makefile.am
tests/examples/netclock/.gitignore [new file with mode: 0644]
tests/examples/netclock/Makefile.am [new file with mode: 0644]
tests/examples/netclock/netclock-client.c [new file with mode: 0644]
tests/examples/netclock/netclock-server.c [new file with mode: 0644]

index 2c72da9..46cb060 100644 (file)
@@ -801,6 +801,7 @@ tests/examples/launch/Makefile
 tests/examples/manual/Makefile
 tests/examples/memory/Makefile
 tests/examples/metadata/Makefile
+tests/examples/netclock/Makefile
 tests/examples/queue/Makefile
 tests/examples/streams/Makefile
 tests/examples/typefind/Makefile
index 7454b5c..376bac8 100644 (file)
@@ -17,6 +17,7 @@ always_dirs = \
        manual     \
        memory   \
        metadata   \
+       netclock \
        queue      \
        stepping \
        streams \
diff --git a/tests/examples/netclock/.gitignore b/tests/examples/netclock/.gitignore
new file mode 100644 (file)
index 0000000..bf0b328
--- /dev/null
@@ -0,0 +1,2 @@
+netclock-server
+netclock-client
diff --git a/tests/examples/netclock/Makefile.am b/tests/examples/netclock/Makefile.am
new file mode 100644 (file)
index 0000000..9506997
--- /dev/null
@@ -0,0 +1,11 @@
+noinst_PROGRAMS = netclock-server netclock-client
+
+netclock_server_LDADD = \
+       $(top_builddir)/libs/gst/net/libgstnet-@GST_API_VERSION@.la \
+       $(GST_OBJ_LIBS)
+netclock_server_CFLAGS = $(GST_OBJ_CFLAGS)
+
+netclock_client_LDADD = \
+       $(top_builddir)/libs/gst/net/libgstnet-@GST_API_VERSION@.la \
+       $(GST_OBJ_LIBS)
+netclock_client_CFLAGS = $(GST_OBJ_CFLAGS)
diff --git a/tests/examples/netclock/netclock-client.c b/tests/examples/netclock/netclock-client.c
new file mode 100644 (file)
index 0000000..2082297
--- /dev/null
@@ -0,0 +1,59 @@
+#include <stdlib.h>
+#include <gst/gst.h>
+#include <gst/net/gstnetclientclock.h>
+
+static gboolean
+handle_bus_message (GstBus * bus, GstMessage * message, GstClock * client_clock)
+{
+  if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ELEMENT) {
+    const GstStructure *s = gst_message_get_structure (message);
+    gchar *str;
+
+    if (s == NULL)
+      return TRUE;
+    str = gst_structure_to_string (s);
+    g_print ("%s\n", str);
+    g_free (str);
+  }
+  return TRUE;
+}
+
+gint
+main (gint argc, gchar * argv[])
+{
+  GMainLoop *loop;
+  gchar *host;
+  guint16 port;
+  GstClock *client_clock;
+  GstBus *bus;
+
+  gst_init (&argc, &argv);
+
+  if (argc < 3) {
+    g_print ("Usage: netclock-client <host> <port>\n");
+    return 1;
+  }
+
+  host = argv[1];
+  port = atoi (argv[2]);
+
+  client_clock = gst_net_client_clock_new (NULL, host, port, 0);
+  if (client_clock == NULL) {
+    g_printerr ("Failed to create network clock client\n");
+    return 1;
+  }
+
+  bus = gst_bus_new ();
+  gst_bus_add_watch (bus, (GstBusFunc) handle_bus_message, client_clock);
+  g_object_set (G_OBJECT (client_clock), "bus", bus, NULL);
+
+  loop = g_main_loop_new (NULL, FALSE);
+
+  g_main_loop_run (loop);
+
+  /* cleanup */
+  g_main_loop_unref (loop);
+  g_object_unref (client_clock);
+
+  return 0;
+}
diff --git a/tests/examples/netclock/netclock-server.c b/tests/examples/netclock/netclock-server.c
new file mode 100644 (file)
index 0000000..46513e2
--- /dev/null
@@ -0,0 +1,59 @@
+/* GStreamer
+ * Copyright (C) 2014 Jan Schmidt <jan@centricular.com>
+ *
+ * netclock-server.c: Publish a network clock provider
+ *
+ * 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.
+ */
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <gst/gst.h>
+#include <gst/net/gstnettimeprovider.h>
+
+gint
+main (gint argc, gchar * argv[])
+{
+  GMainLoop *loop;
+  GstClock *clock;
+  GstNetTimeProvider *net_clock;
+  int clock_port = 0;
+
+  gst_init (&argc, &argv);
+
+  if (argc > 1) {
+    clock_port = atoi (argv[1]);
+  }
+
+  loop = g_main_loop_new (NULL, FALSE);
+
+  clock = gst_system_clock_obtain ();
+  net_clock = gst_net_time_provider_new (clock, NULL, clock_port);
+  gst_object_unref (clock);
+
+  g_object_get (net_clock, "port", &clock_port, NULL);
+
+  g_print ("Published network clock on port %u\n", clock_port);
+
+  g_main_loop_run (loop);
+
+  /* cleanup */
+  g_main_loop_unref (loop);
+
+  return 0;
+}