2 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
4 * gstnetclientclock.c: Unit test for the network client clock
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include <gst/check/gstcheck.h>
23 #include <gst/net/gstnet.h>
27 GST_START_TEST (test_instantiation)
29 GstClock *client, *local;
31 local = gst_system_clock_obtain ();
32 client = gst_net_client_clock_new (NULL, "127.0.0.1", 1234, GST_SECOND);
33 fail_unless (local != NULL, "failed to get system clock");
34 fail_unless (client != NULL, "failed to get network client clock");
36 /* one for gstreamer, one for us */
37 ASSERT_OBJECT_REFCOUNT (local, "system clock", 2);
38 ASSERT_OBJECT_REFCOUNT (client, "network client clock", 1);
40 gst_object_unref (client);
42 ASSERT_OBJECT_REFCOUNT (local, "system clock", 2);
44 gst_object_unref (local);
49 GST_START_TEST (test_functioning)
51 GstNetTimeProvider *ntp;
52 GstClock *client, *server;
53 GstClockTime basex, basey, rate_num, rate_denom;
54 GstClockTime servtime, clienttime;
57 server = gst_system_clock_obtain ();
58 fail_unless (server != NULL, "failed to get system clock");
60 /* move the clock ahead 100 seconds */
61 gst_clock_get_calibration (server, &basex, &basey, &rate_num, &rate_denom);
62 basey += 100 * GST_SECOND;
63 gst_clock_set_calibration (server, basex, basey, rate_num, rate_denom);
65 ntp = gst_net_time_provider_new (server, "127.0.0.1", 0);
66 fail_unless (ntp != NULL, "failed to create network time provider");
68 g_object_get (ntp, "port", &port, NULL);
70 client = gst_net_client_clock_new (NULL, "127.0.0.1", port, GST_SECOND);
71 fail_unless (client != NULL, "failed to get network client clock");
73 g_object_get (client, "port", &port, NULL);
75 /* let the clocks synchronize */
76 g_usleep (G_USEC_PER_SEC / 2);
78 servtime = gst_clock_get_time (server);
79 clienttime = gst_clock_get_time (client);
81 /* can't in general make a precise assertion here, because this depends on
82 * system load and a lot of things. however within half a second they should
83 * at least be within 1/10 of a second of each other... */
84 if (servtime > clienttime)
85 fail_unless (servtime - clienttime < 100 * GST_MSECOND,
86 "clocks not in sync (%" GST_TIME_FORMAT ")",
87 GST_TIME_ARGS (servtime - clienttime));
89 fail_unless (clienttime - servtime < 100 * GST_MSECOND,
90 "clocks not in sync (%" GST_TIME_FORMAT ")",
91 GST_TIME_ARGS (clienttime - servtime));
94 g_print ("diff: %" GST_TIME_FORMAT,
95 GST_TIME_ARGS (servtime > clienttime ? servtime - clienttime
96 : clienttime - servtime));
99 /* one for gstreamer, one for ntp, one for us */
100 ASSERT_OBJECT_REFCOUNT (server, "system clock", 3);
101 ASSERT_OBJECT_REFCOUNT (client, "network client clock", 1);
103 gst_object_unref (ntp);
105 ASSERT_OBJECT_REFCOUNT (server, "system clock", 2);
107 gst_object_unref (client);
108 gst_object_unref (server);
114 gst_net_client_clock_suite (void)
116 Suite *s = suite_create ("GstNetClientClock");
117 TCase *tc_chain = tcase_create ("generic tests");
119 tcase_set_timeout (tc_chain, 0);
121 suite_add_tcase (s, tc_chain);
122 tcase_add_test (tc_chain, test_instantiation);
123 tcase_add_test (tc_chain, test_functioning);
128 GST_CHECK_MAIN (gst_net_client_clock);