1 /* GStreamer udpsink unit tests
2 * Copyright (C) 2009 Axis Communications <dev-gstreamer@axis.com>
3 * @author Ognyan Tonchev <ognyan@axis.com>
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.
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.
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.
20 #include <gst/check/gstcheck.h>
21 #include <gst/base/gstbasesink.h>
25 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
30 #define RTP_HEADER_SIZE 12
31 #define RTP_PAYLOAD_SIZE 1024
34 * Number of bytes received in the render function when using buffer lists
36 static guint render_list_bytes_received;
39 * Render function for testing udpsink with buffer lists
42 udpsink_render_list (GstBaseSink * sink, GstBufferList * list)
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);
51 GST_DEBUG ("rendered %" G_GSIZE_FORMAT " bytes", size);
52 render_list_bytes_received += size;
59 set_render_list_function (GstElement * bsink)
61 GstBaseSinkClass *bsclass;
63 bsclass = GST_BASE_SINK_GET_CLASS ((GstBaseSink *) bsink);
65 /* Add callback function for the buffer list tests */
66 bsclass->render_list = udpsink_render_list;
69 static GstBufferList *
70 create_buffer_list (guint * data_size)
73 GstBuffer *rtp_buffer;
74 GstBuffer *data_buffer;
76 list = gst_buffer_list_new ();
78 /*** First group, i.e. first packet. **/
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);
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);
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));
91 /*** Second group, i.e. second packet. ***/
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);
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);
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));
104 /* Calculate the size of the data */
105 *data_size = 2 * RTP_HEADER_SIZE + 2 * RTP_PAYLOAD_SIZE;
111 udpsink_test (gboolean use_buffer_lists)
119 list = create_buffer_list (&data_size);
121 udpsink = gst_check_setup_element ("udpsink");
122 if (use_buffer_lists)
123 set_render_list_function (udpsink);
125 srcpad = gst_check_setup_src_pad_by_name (udpsink, &srctemplate, "sink");
127 gst_element_set_state (udpsink, GST_STATE_PLAYING);
128 gst_pad_set_active (srcpad, TRUE);
130 gst_pad_push_event (srcpad, gst_event_new_stream_start ("hey there!"));
132 gst_segment_init (&segment, GST_FORMAT_TIME);
133 gst_pad_push_event (srcpad, gst_event_new_segment (&segment));
135 fail_unless_equals_int (gst_pad_push_list (srcpad, list), GST_FLOW_OK);
137 gst_check_teardown_pad_by_name (udpsink, "sink");
138 gst_check_teardown_element (udpsink);
140 if (use_buffer_lists)
141 fail_unless_equals_int (data_size, render_list_bytes_received);
144 GST_START_TEST (test_udpsink)
146 udpsink_test (FALSE);
152 GST_START_TEST (test_udpsink_bufferlist)
159 GST_START_TEST (test_udpsink_client_add_remove)
163 /* Note: keep in mind that these are in addition to the client added by
164 * the host/port properties (by default 'localhost:5004' */
166 udpsink = gst_check_setup_element ("udpsink");
167 g_signal_emit_by_name (udpsink, "remove", "localhost", 5004, NULL);
168 gst_object_unref (udpsink);
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);
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);
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);
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);
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);
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);
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);
208 GST_START_TEST (test_udpsink_dscp)
211 GError *error = NULL;
212 GSocket *sock4, *sock6;
215 g_socket_new (G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
216 G_SOCKET_PROTOCOL_UDP, &error);
217 fail_unless (sock4 != NULL && error == NULL);
219 g_socket_new (G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_DATAGRAM,
220 G_SOCKET_PROTOCOL_UDP, &error);
221 fail_unless (sock6 != NULL && error == NULL);
223 udpsink = gst_check_setup_element ("udpsink");
224 g_signal_emit_by_name (udpsink, "add", "127.0.0.1", 5554, NULL);
225 g_object_set (udpsink, "socket", sock4, NULL);
226 g_object_set (udpsink, "socket-v6", sock6, NULL);
228 ASSERT_SET_STATE (udpsink, GST_STATE_READY, GST_STATE_CHANGE_SUCCESS);
230 g_object_set (udpsink, "qos-dscp", 0, NULL);
231 g_object_set (udpsink, "qos-dscp", 63, NULL);
233 ASSERT_SET_STATE (udpsink, GST_STATE_NULL, GST_STATE_CHANGE_SUCCESS);
235 gst_object_unref (udpsink);
236 g_object_unref (sock4);
237 g_object_unref (sock6);
245 Suite *s = suite_create ("udpsink_test");
246 TCase *tc_chain = tcase_create ("linear");
248 suite_add_tcase (s, tc_chain);
250 tcase_add_test (tc_chain, test_udpsink);
251 tcase_add_test (tc_chain, test_udpsink_bufferlist);
252 tcase_add_test (tc_chain, test_udpsink_client_add_remove);
253 tcase_add_test (tc_chain, test_udpsink_dscp);
258 GST_CHECK_MAIN (udpsink)