tests: Fix compile warning on mingw64
[platform/upstream/gstreamer.git] / tests / misc / netclock-replay.c
1 /* GStreamer
2  * Copyright (C) 2016 Centricular Ltd.
3  * Author: Arun Raghavan <arun@centricular.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26
27 /* We need the internal netclock estimation function to (re)run the code on
28  * captured samples, plus its dependencies for the build to succeed. */
29 #include "../../libs/gst/net/gstntppacket.c"
30 #include "../../libs/gst/net/gstnetclientclock.c"
31
32 static gchar *input = NULL;
33 static gboolean debug = FALSE;
34 static gint rtt_limit = 0;
35
36 static GOptionEntry entries[] = {
37   {"input", 'i', 0, G_OPTION_ARG_FILENAME, &input,
38         "Clock reading file containing one local and remote time readings, one "
39         "per line",
40       "FILE"},
41   {"rtt-limit", 'r', 0, G_OPTION_ARG_INT64, &rtt_limit,
42       "Round trip time limit on packets (in ms)", "MSEC"},
43   {"debug", 'd', 0, G_OPTION_ARG_NONE, &debug, "Verbose debug output", NULL},
44   {NULL,}
45 };
46
47 int
48 main (int argc, char *argv[])
49 {
50   GstNetClientInternalClock *clock;
51   GstBus *bus;
52   GIOChannel *channel;
53   GIOStatus status;
54   GError *error = NULL;
55   GOptionContext *context;
56   gchar *line;
57   int ret = 1;
58
59   context = g_option_context_new (NULL);
60   g_option_context_add_main_entries (context, entries, NULL);
61   g_option_context_add_group (context, gst_init_get_option_group ());
62
63   if (!g_option_context_parse (context, &argc, &argv, &error)) {
64     g_print ("Failed to parse options: %s\n\n", error->message);
65     g_error_free (error);
66     return 1;
67   }
68
69   if (input) {
70     if (!(channel = g_io_channel_new_file (input, "r", NULL))) {
71       g_print ("Could not read input file: %s\n", input);
72       return 1;
73     }
74   } else {
75     if (!(channel = g_io_channel_unix_new (0))) {
76       g_print ("Could not read stdin");
77       return 1;
78     }
79   }
80
81   clock = g_object_new (GST_TYPE_NET_CLIENT_INTERNAL_CLOCK, NULL);
82   bus = gst_bus_new ();
83
84   /* FIXME: Find a way to do this without touching the structure internals */
85   if (rtt_limit)
86     clock->roundtrip_limit = rtt_limit * GST_MSECOND;
87   clock->busses = g_list_prepend (clock->busses, bus);
88
89   while ((status = g_io_channel_read_line (channel, &line, NULL, NULL,
90               &error)) == G_IO_STATUS_NORMAL) {
91     GstClockTime local_1, local_2, remote_1, remote_2;
92     GstMessage *message;
93
94     if (sscanf (line, "%" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT " %"
95             G_GUINT64_FORMAT " %" G_GUINT64_FORMAT, &local_1, &remote_1,
96             &remote_2, &local_2) != 4) {
97       g_print ("Failed to get local/remote time values from: %s\n", line);
98       goto done;
99     }
100
101     if (debug)
102       g_print ("%s", line);
103
104     gst_net_client_internal_clock_observe_times (clock, local_1, remote_1,
105         remote_2, local_2);
106
107     g_free (line);
108
109     if ((message = gst_bus_pop_filtered (bus, GST_MESSAGE_ELEMENT))) {
110       const GstStructure *st;
111       gchar *str;
112
113       st = gst_message_get_structure (message);
114       str = gst_structure_to_string (st);
115
116       g_print ("%s\n", str);
117
118       g_free (str);
119       gst_message_unref (message);
120     }
121   }
122
123   if (status == G_IO_CHANNEL_ERROR) {
124     g_print ("Error reading file: %s\n", error->message);
125     g_error_free (error);
126     goto done;
127   }
128
129   g_io_channel_unref (channel);
130   g_free (input);
131   gst_object_unref (bus);
132
133   ret = 0;
134
135 done:
136   return ret;
137 }