2 * Copyright (C) 2005 Andy Wingo <wingo@pobox.com>
4 * gstnettimeprovider.c: Unit test for the network time provider
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_refcounts)
29 GstNetTimeProvider *ntp;
32 clock = gst_system_clock_obtain ();
33 fail_unless (clock != NULL, "failed to get system clock");
35 /* one for gstreamer, one for us */
36 ASSERT_OBJECT_REFCOUNT (clock, "system clock", 2);
38 ntp = gst_net_time_provider_new (clock, NULL, 0);
39 fail_unless (ntp != NULL, "failed to create net time provider");
41 /* one for ntp, one for gstreamer, one for us */
42 ASSERT_OBJECT_REFCOUNT (clock, "system clock", 3);
44 ASSERT_OBJECT_REFCOUNT (ntp, "net time provider", 1);
46 gst_object_unref (ntp);
47 ASSERT_OBJECT_REFCOUNT (clock, "net time provider", 2);
49 gst_object_unref (clock);
54 GST_START_TEST (test_functioning)
56 GstNetTimeProvider *ntp;
57 GstNetTimePacket *packet;
60 struct sockaddr_in servaddr;
61 gint port = -1, sockfd, ret;
64 clock = gst_system_clock_obtain ();
65 fail_unless (clock != NULL, "failed to get system clock");
66 ntp = gst_net_time_provider_new (clock, "127.0.0.1", 0);
67 fail_unless (ntp != NULL, "failed to create net time provider");
69 g_object_get (ntp, "port", &port, NULL);
70 fail_unless (port > 0);
72 sockfd = socket (AF_INET, SOCK_DGRAM, 0);
73 fail_if (sockfd < 0, "socket failed");
75 memset (&servaddr, 0, sizeof (servaddr));
76 servaddr.sin_family = AF_INET;
77 servaddr.sin_port = htons (port);
78 inet_aton ("127.0.0.1", &servaddr.sin_addr);
80 packet = gst_net_time_packet_new (NULL);
81 fail_unless (packet != NULL, "failed to create packet");
83 packet->local_time = local = gst_clock_get_time (clock);
85 len = sizeof (servaddr);
86 ret = gst_net_time_packet_send (packet, sockfd,
87 (struct sockaddr *) &servaddr, len);
89 fail_unless (ret == GST_NET_TIME_PACKET_SIZE, "failed to send packet");
93 packet = gst_net_time_packet_receive (sockfd, (struct sockaddr *) &servaddr,
96 fail_unless (packet != NULL, "failed to receive packet");
97 fail_unless (packet->local_time == local, "local time is not the same");
98 fail_unless (packet->remote_time > local, "remote time not after local time");
99 fail_unless (packet->remote_time < gst_clock_get_time (clock),
100 "remote time in the future");
106 gst_object_unref (ntp);
107 gst_object_unref (clock);
113 gst_net_time_provider_suite (void)
115 Suite *s = suite_create ("GstNetTimeProvider");
116 TCase *tc_chain = tcase_create ("generic tests");
118 suite_add_tcase (s, tc_chain);
119 tcase_add_test (tc_chain, test_refcounts);
120 tcase_add_test (tc_chain, test_functioning);
125 GST_CHECK_MAIN (gst_net_time_provider);