1 /* GStreamer UDP source unit tests
2 * Copyright (C) 2011 Tim-Philipp Müller <tim centricular net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
19 #include <gst/check/gstcheck.h>
24 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
30 udpsrc_setup (GstElement ** udpsrc, GSocket ** socket,
31 GstPad ** sinkpad, GSocketAddress ** sa)
37 *udpsrc = gst_check_setup_element ("udpsrc");
38 fail_unless (*udpsrc != NULL);
39 g_object_set (*udpsrc, "port", 0, NULL);
41 *sinkpad = gst_check_setup_sink_pad_by_name (*udpsrc, &sinktemplate, "src");
42 fail_unless (*sinkpad != NULL);
43 gst_pad_set_active (*sinkpad, TRUE);
45 gst_element_set_state (*udpsrc, GST_STATE_PLAYING);
46 g_object_get (*udpsrc, "port", &port, NULL);
47 GST_INFO ("udpsrc port = %d", port);
49 *socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
50 G_SOCKET_PROTOCOL_UDP, NULL);
52 if (*socket == NULL) {
53 GST_WARNING ("Could not create IPv4 UDP socket for unit test");
57 ia = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
58 s = g_inet_address_to_string (ia);
59 GST_LOG ("inet address %s", s);
61 *sa = g_inet_socket_address_new (ia, port);
67 GST_START_TEST (test_udpsrc_empty_packet)
69 GSocketAddress *sa = NULL;
70 GstElement *udpsrc = NULL;
71 GSocket *socket = NULL;
72 GstPad *sinkpad = NULL;
74 if (!udpsrc_setup (&udpsrc, &socket, &sinkpad, &sa))
77 if (g_socket_send_to (socket, sa, "HeLL0", 0, NULL, NULL) == 0) {
78 GST_INFO ("sent 0 bytes");
79 if (g_socket_send_to (socket, sa, "HeLL0", 6, NULL, NULL) == 6) {
84 GST_INFO ("sent 6 bytes");
86 g_mutex_lock (&check_mutex);
87 len = g_list_length (buffers);
89 g_cond_wait (&check_cond, &check_mutex);
90 len = g_list_length (buffers);
91 GST_INFO ("%u buffers", len);
94 /* wait a bit more for a second buffer */
96 g_cond_wait_until (&check_cond, &check_mutex,
97 g_get_monotonic_time () + G_TIME_SPAN_SECOND / 100);
99 len = g_list_length (buffers);
100 GST_INFO ("%u buffers", len);
103 fail_unless (len == 1 || len == 2);
105 /* last buffer should be our HeLL0 string */
106 buf = GST_BUFFER (g_list_nth_data (buffers, len - 1));
107 gst_buffer_map (buf, &map, GST_MAP_READ);
108 fail_unless_equals_int (map.size, 6);
109 fail_unless_equals_string ((gchar *) map.data, "HeLL0");
110 gst_buffer_unmap (buf, &map);
112 /* if there's another buffer, it should be 0 bytes */
114 buf = GST_BUFFER (g_list_nth_data (buffers, 0));
115 fail_unless_equals_int (gst_buffer_get_size (buf), 0);
117 g_mutex_unlock (&check_mutex);
119 GST_WARNING ("send_to(6 bytes) failed");
122 GST_WARNING ("send_to(0 bytes) failed");
127 gst_element_set_state (udpsrc, GST_STATE_NULL);
129 gst_check_drop_buffers ();
130 gst_check_teardown_pad_by_name (udpsrc, "src");
131 gst_check_teardown_element (udpsrc);
133 g_object_unref (socket);
139 GST_START_TEST (test_udpsrc)
141 GSocketAddress *sa = NULL;
142 GstElement *udpsrc = NULL;
143 GSocket *socket = NULL;
144 GstPad *sinkpad = NULL;
151 for (i = 0; i < G_N_ELEMENTS (data); ++i)
154 if (!udpsrc_setup (&udpsrc, &socket, &sinkpad, &sa))
157 if (g_socket_send_to (socket, sa, data, 48000, NULL, NULL) != 48000)
160 if (g_socket_send_to (socket, sa, data, 21000, NULL, NULL) != 21000)
163 if (g_socket_send_to (socket, sa, data, 500, NULL, NULL) != 500)
166 if (g_socket_send_to (socket, sa, data, 1600, NULL, NULL) != 1600)
169 if (g_socket_send_to (socket, sa, data, 1400, NULL, NULL) != 1400)
172 GST_INFO ("sent some packets");
174 g_mutex_lock (&check_mutex);
175 len = g_list_length (buffers);
177 g_cond_wait (&check_cond, &check_mutex);
178 len = g_list_length (buffers);
179 GST_INFO ("%u buffers", len);
182 /* check that large packets are made up of multiple memory chunks and that
183 * the first one is fairly small */
184 buf = GST_BUFFER (g_list_nth_data (buffers, 0));
185 fail_unless_equals_int (gst_buffer_get_size (buf), 48000);
186 fail_unless_equals_int (gst_buffer_n_memory (buf), 2);
187 mem = gst_buffer_peek_memory (buf, 0);
188 gst_memory_get_sizes (mem, NULL, &max_size);
189 fail_unless (max_size <= 2000);
191 buf = GST_BUFFER (g_list_nth_data (buffers, 1));
192 fail_unless_equals_int (gst_buffer_get_size (buf), 21000);
193 fail_unless_equals_int (gst_buffer_n_memory (buf), 2);
194 mem = gst_buffer_peek_memory (buf, 0);
195 gst_memory_get_sizes (mem, NULL, &max_size);
196 fail_unless (max_size <= 2000);
198 buf = GST_BUFFER (g_list_nth_data (buffers, 2));
199 fail_unless_equals_int (gst_buffer_get_size (buf), 500);
200 fail_unless_equals_int (gst_buffer_n_memory (buf), 1);
201 mem = gst_buffer_peek_memory (buf, 0);
202 gst_memory_get_sizes (mem, NULL, &max_size);
203 fail_unless (max_size <= 2000);
205 buf = GST_BUFFER (g_list_nth_data (buffers, 3));
206 fail_unless_equals_int (gst_buffer_get_size (buf), 1600);
207 fail_unless_equals_int (gst_buffer_n_memory (buf), 2);
208 mem = gst_buffer_peek_memory (buf, 0);
209 gst_memory_get_sizes (mem, NULL, &max_size);
210 fail_unless (max_size <= 2000);
212 buf = GST_BUFFER (g_list_nth_data (buffers, 4));
213 fail_unless_equals_int (gst_buffer_get_size (buf), 1400);
214 fail_unless_equals_int (gst_buffer_n_memory (buf), 1);
215 mem = gst_buffer_peek_memory (buf, 0);
216 gst_memory_get_sizes (mem, NULL, &max_size);
217 fail_unless (max_size <= 2000);
219 g_list_foreach (buffers, (GFunc) gst_buffer_unref, NULL);
220 g_list_free (buffers);
223 g_mutex_unlock (&check_mutex);
228 gst_element_set_state (udpsrc, GST_STATE_NULL);
230 gst_check_drop_buffers ();
231 gst_check_teardown_pad_by_name (udpsrc, "src");
232 gst_check_teardown_element (udpsrc);
234 g_object_unref (socket);
243 Suite *s = suite_create ("udpsrc");
244 TCase *tc_chain = tcase_create ("udpsrc");
246 suite_add_tcase (s, tc_chain);
247 tcase_add_test (tc_chain, test_udpsrc_empty_packet);
248 tcase_add_test (tc_chain, test_udpsrc);
252 GST_CHECK_MAIN (udpsrc)