tests: udpsink: make test work in environments without IPv6
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-good / tests / check / elements / udpsink.c
1 /* GStreamer udpsink unit tests
2  * Copyright (C) 2009 Axis Communications <dev-gstreamer@axis.com>
3  * @author Ognyan Tonchev <ognyan@axis.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 #include <gst/check/gstcheck.h>
21 #include <gst/base/gstbasesink.h>
22 #include <gio/gio.h>
23 #include <stdlib.h>
24
25 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
26     GST_PAD_SRC,
27     GST_PAD_ALWAYS,
28     GST_STATIC_CAPS_ANY);
29
30 #define RTP_HEADER_SIZE 12
31 #define RTP_PAYLOAD_SIZE 1024
32
33 /*
34  * Number of bytes received in the render function when using buffer lists
35  */
36 static guint render_list_bytes_received;
37
38 /*
39  * Render function for testing udpsink with buffer lists
40  */
41 static GstFlowReturn
42 udpsink_render_list (GstBaseSink * sink, GstBufferList * list)
43 {
44   guint i, num;
45
46   num = gst_buffer_list_length (list);
47   for (i = 0; i < num; ++i) {
48     GstBuffer *buf = gst_buffer_list_get (list, i);
49     gsize size = gst_buffer_get_size (buf);
50
51     GST_DEBUG ("rendered %" G_GSIZE_FORMAT " bytes", size);
52     render_list_bytes_received += size;
53   }
54
55   return GST_FLOW_OK;
56 }
57
58 static void
59 set_render_list_function (GstElement * bsink)
60 {
61   GstBaseSinkClass *bsclass;
62
63   bsclass = GST_BASE_SINK_GET_CLASS ((GstBaseSink *) bsink);
64
65   /* Add callback function for the buffer list tests */
66   bsclass->render_list = udpsink_render_list;
67 }
68
69 static GstBufferList *
70 create_buffer_list (guint * data_size)
71 {
72   GstBufferList *list;
73   GstBuffer *rtp_buffer;
74   GstBuffer *data_buffer;
75
76   list = gst_buffer_list_new ();
77
78   /*** First group, i.e. first packet. **/
79
80   /* Create the RTP header buffer */
81   rtp_buffer = gst_buffer_new_allocate (NULL, RTP_HEADER_SIZE, NULL);
82   gst_buffer_memset (rtp_buffer, 0, 0, RTP_HEADER_SIZE);
83
84   /* Create the buffer that holds the payload */
85   data_buffer = gst_buffer_new_allocate (NULL, RTP_PAYLOAD_SIZE, NULL);
86   gst_buffer_memset (data_buffer, 0, 0, RTP_PAYLOAD_SIZE);
87
88   /* Create a new group to hold the rtp header and the payload */
89   gst_buffer_list_add (list, gst_buffer_append (rtp_buffer, data_buffer));
90
91   /***  Second group, i.e. second packet. ***/
92
93   /* Create the RTP header buffer */
94   rtp_buffer = gst_buffer_new_allocate (NULL, RTP_HEADER_SIZE, NULL);
95   gst_buffer_memset (rtp_buffer, 0, 0, RTP_HEADER_SIZE);
96
97   /* Create the buffer that holds the payload */
98   data_buffer = gst_buffer_new_allocate (NULL, RTP_PAYLOAD_SIZE, NULL);
99   gst_buffer_memset (data_buffer, 0, 0, RTP_PAYLOAD_SIZE);
100
101   /* Create a new group to hold the rtp header and the payload */
102   gst_buffer_list_add (list, gst_buffer_append (rtp_buffer, data_buffer));
103
104   /* Calculate the size of the data */
105   *data_size = 2 * RTP_HEADER_SIZE + 2 * RTP_PAYLOAD_SIZE;
106
107   return list;
108 }
109
110 static void
111 udpsink_test (gboolean use_buffer_lists)
112 {
113   GstSegment segment;
114   GstElement *udpsink;
115   GstPad *srcpad;
116   GstBufferList *list;
117   guint data_size;
118
119   list = create_buffer_list (&data_size);
120
121   udpsink = gst_check_setup_element ("udpsink");
122   if (use_buffer_lists)
123     set_render_list_function (udpsink);
124
125   srcpad = gst_check_setup_src_pad_by_name (udpsink, &srctemplate, "sink");
126
127   gst_element_set_state (udpsink, GST_STATE_PLAYING);
128   gst_pad_set_active (srcpad, TRUE);
129
130   gst_pad_push_event (srcpad, gst_event_new_stream_start ("hey there!"));
131
132   gst_segment_init (&segment, GST_FORMAT_TIME);
133   gst_pad_push_event (srcpad, gst_event_new_segment (&segment));
134
135   fail_unless_equals_int (gst_pad_push_list (srcpad, list), GST_FLOW_OK);
136
137   gst_check_teardown_pad_by_name (udpsink, "sink");
138   gst_check_teardown_element (udpsink);
139
140   if (use_buffer_lists)
141     fail_unless_equals_int (data_size, render_list_bytes_received);
142 }
143
144 GST_START_TEST (test_udpsink)
145 {
146   udpsink_test (FALSE);
147 }
148
149 GST_END_TEST;
150
151
152 GST_START_TEST (test_udpsink_bufferlist)
153 {
154   udpsink_test (TRUE);
155 }
156
157 GST_END_TEST;
158
159 GST_START_TEST (test_udpsink_client_add_remove)
160 {
161   GstElement *udpsink;
162
163   /* Note: keep in mind that these are in addition to the client added by
164    * the host/port properties (by default 'localhost:5004' */
165
166   udpsink = gst_check_setup_element ("udpsink");
167   g_signal_emit_by_name (udpsink, "remove", "localhost", 5004, NULL);
168   gst_object_unref (udpsink);
169
170   udpsink = gst_check_setup_element ("udpsink");
171   g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
172   gst_object_unref (udpsink);
173
174   udpsink = gst_check_setup_element ("udpsink");
175   g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
176   g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
177   gst_object_unref (udpsink);
178
179   udpsink = gst_check_setup_element ("udpsink");
180   g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
181   g_signal_emit_by_name (udpsink, "remove", "127.0.0.1", 5554, NULL);
182   gst_object_unref (udpsink);
183
184   udpsink = gst_check_setup_element ("udpsink");
185   g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
186   g_signal_emit_by_name (udpsink, "remove", "127.0.0.1", 5555, NULL);
187   gst_object_unref (udpsink);
188
189   udpsink = gst_check_setup_element ("udpsink");
190   g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
191   g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5555, NULL);
192   gst_object_unref (udpsink);
193
194   udpsink = gst_check_setup_element ("udpsink");
195   g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
196   g_signal_emit_by_name (udpsink, "add", "10.2.0.1", 5554, NULL);
197   gst_object_unref (udpsink);
198
199   udpsink = gst_check_setup_element ("udpsink");
200   g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
201   g_signal_emit_by_name (udpsink, "add", "10.2.0.1", 5554, NULL);
202   g_signal_emit_by_name (udpsink, "remove", "127.0.0.1", 5554, NULL);
203   gst_object_unref (udpsink);
204 }
205
206 GST_END_TEST;
207
208 GST_START_TEST (test_udpsink_dscp)
209 {
210   GstElement *udpsink;
211   GError *error = NULL;
212   GSocket *sock4, *sock6;
213   gboolean have_ipv6;
214
215   sock4 =
216       g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
217       G_SOCKET_PROTOCOL_UDP, &error);
218   fail_unless (sock4 != NULL && error == NULL);
219
220   sock6 =
221       g_socket_new (G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_DATAGRAM,
222       G_SOCKET_PROTOCOL_UDP, &error);
223   have_ipv6 = sock6 != NULL && error == NULL;
224   g_clear_error (&error);
225
226   udpsink = gst_check_setup_element ("udpsink");
227   g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
228   g_object_set (udpsink, "socket", sock4, NULL);
229
230   if (have_ipv6)
231     g_object_set (udpsink, "socket-v6", sock6, NULL);
232
233   ASSERT_SET_STATE (udpsink, GST_STATE_READY, GST_STATE_CHANGE_SUCCESS);
234
235   g_object_set (udpsink, "qos-dscp", 0, NULL);
236   g_object_set (udpsink, "qos-dscp", 63, NULL);
237
238   ASSERT_SET_STATE (udpsink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
239
240   gst_object_unref (udpsink);
241   g_object_unref (sock4);
242   g_clear_object (&sock6);
243 }
244
245 GST_END_TEST;
246
247 static Suite *
248 udpsink_suite (void)
249 {
250   Suite *s = suite_create ("udpsink_test");
251   TCase *tc_chain = tcase_create ("linear");
252
253   suite_add_tcase (s, tc_chain);
254
255   tcase_add_test (tc_chain, test_udpsink);
256   tcase_add_test (tc_chain, test_udpsink_bufferlist);
257   tcase_add_test (tc_chain, test_udpsink_client_add_remove);
258   tcase_add_test (tc_chain, test_udpsink_dscp);
259
260   return s;
261 }
262
263 GST_CHECK_MAIN (udpsink)