2 * Copyright (C) 2016 Centricular Ltd.
3 * Author: Arun Raghavan <arun@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
27 #define GST_NET_API /* empty */
29 /* We need the internal netclock estimation function to (re)run the code on
30 * captured samples, plus its dependencies for the build to succeed. */
31 #include "../../libs/gst/net/gstntppacket.c"
32 #include "../../libs/gst/net/gstnettimepacket.c"
33 #include "../../libs/gst/net/gstnetclientclock.c"
34 #include "../../libs/gst/net/gstnetutils.c"
36 static gchar *input = NULL;
37 static gboolean debug = FALSE;
38 static gint rtt_limit = 0;
40 static GOptionEntry entries[] = {
41 {"input", 'i', 0, G_OPTION_ARG_FILENAME, &input,
42 "Clock reading file containing one local and remote time readings, one "
45 {"rtt-limit", 'r', 0, G_OPTION_ARG_INT64, &rtt_limit,
46 "Round trip time limit on packets (in ms)", "MSEC"},
47 {"debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Verbose debug output", NULL},
52 parse_time_values (const gchar * line, GstClockTime * local_1,
53 GstClockTime * remote_1, GstClockTime * remote_2, GstClockTime * local_2)
61 split = g_strsplit (line, " ", -1);
63 if (g_strv_length (split) != 4)
66 if (!g_ascii_string_to_unsigned (split[0], 10, 0, G_MAXUINT64, local_1, NULL))
69 if (!g_ascii_string_to_unsigned (split[1], 10, 0, G_MAXUINT64, remote_1,
73 if (!g_ascii_string_to_unsigned (split[2], 10, 0, G_MAXUINT64, remote_2,
77 if (!g_ascii_string_to_unsigned (split[3], 10, 0, G_MAXUINT64, local_2, NULL))
87 main (int argc, char *argv[])
89 GstNetClientInternalClock *clock;
94 GOptionContext *context;
98 context = g_option_context_new (NULL);
99 g_option_context_add_main_entries (context, entries, NULL);
100 g_option_context_add_group (context, gst_init_get_option_group ());
102 if (!g_option_context_parse (context, &argc, &argv, &error)) {
103 g_print ("Failed to parse options: %s\n\n", error->message);
104 g_error_free (error);
109 if (!(channel = g_io_channel_new_file (input, "r", NULL))) {
110 g_print ("Could not read input file: %s\n", input);
114 if (!(channel = g_io_channel_unix_new (0))) {
115 g_print ("Could not read stdin");
120 clock = g_object_new (GST_TYPE_NET_CLIENT_INTERNAL_CLOCK, NULL);
121 bus = gst_bus_new ();
123 /* FIXME: Find a way to do this without touching the structure internals */
125 clock->roundtrip_limit = rtt_limit * GST_MSECOND;
126 clock->busses = g_list_prepend (clock->busses, bus);
128 while ((status = g_io_channel_read_line (channel, &line, NULL, NULL,
129 &error)) == G_IO_STATUS_NORMAL) {
130 GstClockTime local_1, remote_1, remote_2, local_2;
133 if (!parse_time_values (line, &local_1, &remote_1, &remote_2, &local_2)) {
134 g_print ("Failed to get local/remote time values from: %s\n", line);
139 g_print ("%s", line);
141 gst_net_client_internal_clock_observe_times (clock, local_1, remote_1,
146 if ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ELEMENT))) {
147 const GstStructure *st;
150 st = gst_message_get_structure (message);
151 str = gst_structure_to_string (st);
153 g_print ("%s\n", str);
156 gst_message_unref (message);
160 if (status == G_IO_CHANNEL_ERROR) {
161 g_print ("Error reading file: %s\n", error->message);
162 g_error_free (error);
166 g_io_channel_unref (channel);
168 gst_object_unref (bus);