tests: udpsrc: ensure test won't timeout if the buffers are already received
[platform/upstream/gstreamer.git] / tests / check / elements / udpsrc.c
1 /* GStreamer UDP source unit tests
2  * Copyright (C) 2011 Tim-Philipp Müller <tim centricular net>
3  *
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.
8  *
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.
13  *
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.
18  */
19 #include <gst/check/gstcheck.h>
20 #include <gio/gio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
25     GST_PAD_SINK,
26     GST_PAD_ALWAYS,
27     GST_STATIC_CAPS_ANY);
28
29 static gboolean
30 udpsrc_setup (GstElement ** udpsrc, GSocket ** socket,
31     GstPad ** sinkpad, GSocketAddress ** sa)
32 {
33   GInetAddress *ia;
34   int port = 0;
35   gchar *s;
36
37   *udpsrc = gst_check_setup_element ("udpsrc");
38   fail_unless (*udpsrc != NULL);
39   g_object_set (*udpsrc, "port", 0, NULL);
40
41   *sinkpad = gst_check_setup_sink_pad_by_name (*udpsrc, &sinktemplate, "src");
42   fail_unless (*sinkpad != NULL);
43   gst_pad_set_active (*sinkpad, TRUE);
44
45   gst_element_set_state (*udpsrc, GST_STATE_PLAYING);
46   g_object_get (*udpsrc, "port", &port, NULL);
47   GST_INFO ("udpsrc port = %d", port);
48
49   *socket = g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
50       G_SOCKET_PROTOCOL_UDP, NULL);
51
52   if (*socket == NULL) {
53     GST_WARNING ("Could not create IPv4 UDP socket for unit test");
54     return FALSE;
55   }
56
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);
60   g_free (s);
61   *sa = g_inet_socket_address_new (ia, port);
62   g_object_unref (ia);
63
64   return TRUE;
65 }
66
67 GST_START_TEST (test_udpsrc_empty_packet)
68 {
69   GSocketAddress *sa = NULL;
70   GstElement *udpsrc = NULL;
71   GSocket *socket = NULL;
72   GstPad *sinkpad = NULL;
73
74   if (!udpsrc_setup (&udpsrc, &socket, &sinkpad, &sa))
75     goto no_socket;
76
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) {
80       GstMapInfo map;
81       GstBuffer *buf;
82       guint len = 0;
83
84       GST_INFO ("sent 6 bytes");
85
86       g_mutex_lock (&check_mutex);
87       len = g_list_length (buffers);
88       while (len < 1) {
89         g_cond_wait (&check_cond, &check_mutex);
90         len = g_list_length (buffers);
91         GST_INFO ("%u buffers", len);
92       }
93
94       /* wait a bit more for a second buffer */
95       if (len < 2) {
96         g_cond_wait_until (&check_cond, &check_mutex,
97             g_get_monotonic_time () + G_TIME_SPAN_SECOND / 100);
98
99         len = g_list_length (buffers);
100         GST_INFO ("%u buffers", len);
101       }
102
103       fail_unless (len == 1 || len == 2);
104
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);
111
112       /* if there's another buffer, it should be 0 bytes */
113       if (len == 2) {
114         buf = GST_BUFFER (g_list_nth_data (buffers, 0));
115         fail_unless_equals_int (gst_buffer_get_size (buf), 0);
116       }
117       g_mutex_unlock (&check_mutex);
118     } else {
119       GST_WARNING ("send_to(6 bytes) failed");
120     }
121   } else {
122     GST_WARNING ("send_to(0 bytes) failed");
123   }
124
125 no_socket:
126
127   gst_element_set_state (udpsrc, GST_STATE_NULL);
128
129   gst_check_drop_buffers ();
130   gst_check_teardown_pad_by_name (udpsrc, "src");
131   gst_check_teardown_element (udpsrc);
132
133   g_object_unref (socket);
134   g_object_unref (sa);
135 }
136
137 GST_END_TEST;
138
139 GST_START_TEST (test_udpsrc)
140 {
141   GSocketAddress *sa = NULL;
142   GstElement *udpsrc = NULL;
143   GSocket *socket = NULL;
144   GstPad *sinkpad = NULL;
145   GstBuffer *buf;
146   GstMemory *mem;
147   gchar data[48000];
148   gsize max_size;
149   int i, len = 0;
150
151   for (i = 0; i < G_N_ELEMENTS (data); ++i)
152     data[i] = i & 0xff;
153
154   if (!udpsrc_setup (&udpsrc, &socket, &sinkpad, &sa))
155     goto no_socket;
156
157   if (g_socket_send_to (socket, sa, data, 48000, NULL, NULL) != 48000)
158     goto send_failure;
159
160   if (g_socket_send_to (socket, sa, data, 21000, NULL, NULL) != 21000)
161     goto send_failure;
162
163   if (g_socket_send_to (socket, sa, data, 500, NULL, NULL) != 500)
164     goto send_failure;
165
166   if (g_socket_send_to (socket, sa, data, 1600, NULL, NULL) != 1600)
167     goto send_failure;
168
169   if (g_socket_send_to (socket, sa, data, 1400, NULL, NULL) != 1400)
170     goto send_failure;
171
172   GST_INFO ("sent some packets");
173
174   g_mutex_lock (&check_mutex);
175   len = g_list_length (buffers);
176   while (len < 5) {
177     g_cond_wait (&check_cond, &check_mutex);
178     len = g_list_length (buffers);
179     GST_INFO ("%u buffers", len);
180   }
181
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);
190
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);
197
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);
204
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);
211
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);
218
219   g_list_foreach (buffers, (GFunc) gst_buffer_unref, NULL);
220   g_list_free (buffers);
221   buffers = NULL;
222
223   g_mutex_unlock (&check_mutex);
224
225 no_socket:
226 send_failure:
227
228   gst_element_set_state (udpsrc, GST_STATE_NULL);
229
230   gst_check_drop_buffers ();
231   gst_check_teardown_pad_by_name (udpsrc, "src");
232   gst_check_teardown_element (udpsrc);
233
234   g_object_unref (socket);
235   g_object_unref (sa);
236 }
237
238 GST_END_TEST;
239
240 static Suite *
241 udpsrc_suite (void)
242 {
243   Suite *s = suite_create ("udpsrc");
244   TCase *tc_chain = tcase_create ("udpsrc");
245
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);
249   return s;
250 }
251
252 GST_CHECK_MAIN (udpsrc)