Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / tests / check / elements / udpsink.c
1 /* GStreamer RTP payloader 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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 #include <gst/check/gstcheck.h>
21 #include <gst/base/gstbasesink.h>
22 #include <stdlib.h>
23 #include <unistd.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 (GstBaseSink * sink, GstBufferList * list)
43 {
44   GstBufferListIterator *it;
45
46   fail_if (!list);
47
48   /*
49    * Count the size of the rtp header and the payload in the buffer list.
50    */
51
52   it = gst_buffer_list_iterate (list);
53
54   /* Loop through all groups */
55   while (gst_buffer_list_iterator_next_group (it)) {
56     GstBuffer *buf;
57     /* Loop through all buffers in the current group */
58     while ((buf = gst_buffer_list_iterator_next (it))) {
59       guint size;
60
61       size = GST_BUFFER_SIZE (buf);
62       GST_DEBUG ("rendered %u bytes", size);
63
64       render_list_bytes_received += size;
65     }
66   }
67
68   gst_buffer_list_iterator_free (it);
69
70   return GST_FLOW_OK;
71 }
72
73 static void
74 _set_render_function (GstElement * bsink)
75 {
76   GstBaseSinkClass *bsclass;
77   bsclass = GST_BASE_SINK_GET_CLASS ((GstBaseSink *) bsink);
78   /* Add callback function for the buffer list tests */
79   bsclass->render_list = udpsink_render;
80 }
81
82 static GstBufferList *
83 _create_buffer_list (guint * data_size)
84 {
85   GstBufferList *list;
86   GstBufferListIterator *it;
87   GstBuffer *rtp_buffer;
88   GstBuffer *data_buffer;
89
90   list = gst_buffer_list_new ();
91   it = gst_buffer_list_iterate (list);
92
93   /*** First group, i.e. first packet. **/
94
95   /* Create the RTP header buffer */
96   rtp_buffer = gst_buffer_new ();
97   GST_BUFFER_MALLOCDATA (rtp_buffer) = g_malloc (RTP_HEADER_SIZE);
98   GST_BUFFER_DATA (rtp_buffer) = GST_BUFFER_MALLOCDATA (rtp_buffer);
99   GST_BUFFER_SIZE (rtp_buffer) = RTP_HEADER_SIZE;
100   memset (GST_BUFFER_DATA (rtp_buffer), 0, RTP_HEADER_SIZE);
101
102   /* Create the buffer that holds the payload */
103   data_buffer = gst_buffer_new ();
104   GST_BUFFER_MALLOCDATA (data_buffer) = g_malloc (RTP_PAYLOAD_SIZE);
105   GST_BUFFER_DATA (data_buffer) = GST_BUFFER_MALLOCDATA (data_buffer);
106   GST_BUFFER_SIZE (data_buffer) = RTP_PAYLOAD_SIZE;
107   memset (GST_BUFFER_DATA (data_buffer), 0, RTP_PAYLOAD_SIZE);
108
109   /* Create a new group to hold the rtp header and the payload */
110   gst_buffer_list_iterator_add_group (it);
111   gst_buffer_list_iterator_add (it, rtp_buffer);
112   gst_buffer_list_iterator_add (it, data_buffer);
113
114   /***  Second group, i.e. second packet. ***/
115
116   /* Create the RTP header buffer */
117   rtp_buffer = gst_buffer_new ();
118   GST_BUFFER_MALLOCDATA (rtp_buffer) = g_malloc (RTP_HEADER_SIZE);
119   GST_BUFFER_DATA (rtp_buffer) = GST_BUFFER_MALLOCDATA (rtp_buffer);
120   GST_BUFFER_SIZE (rtp_buffer) = RTP_HEADER_SIZE;
121   memset (GST_BUFFER_DATA (rtp_buffer), 0, RTP_HEADER_SIZE);
122
123   /* Create the buffer that holds the payload */
124   data_buffer = gst_buffer_new ();
125   GST_BUFFER_MALLOCDATA (data_buffer) = g_malloc (RTP_PAYLOAD_SIZE);
126   GST_BUFFER_DATA (data_buffer) = GST_BUFFER_MALLOCDATA (data_buffer);
127   GST_BUFFER_SIZE (data_buffer) = RTP_PAYLOAD_SIZE;
128   memset (GST_BUFFER_DATA (data_buffer), 0, RTP_PAYLOAD_SIZE);
129
130   /* Create a new group to hold the rtp header and the payload */
131   gst_buffer_list_iterator_add_group (it);
132   gst_buffer_list_iterator_add (it, rtp_buffer);
133   gst_buffer_list_iterator_add (it, data_buffer);
134
135   /* Calculate the size of the data */
136   *data_size = 2 * RTP_HEADER_SIZE + 2 * RTP_PAYLOAD_SIZE;
137
138   gst_buffer_list_iterator_free (it);
139
140   return list;
141 }
142
143 static void
144 udpsink_test (gboolean use_buffer_lists)
145 {
146   GstElement *udpsink;
147   GstPad *srcpad;
148   GstBufferList *list;
149   guint data_size;
150
151   list = _create_buffer_list (&data_size);
152
153   udpsink = gst_check_setup_element ("udpsink");
154   if (use_buffer_lists)
155     _set_render_function (udpsink);
156
157   srcpad = gst_check_setup_src_pad_by_name (udpsink, &srctemplate, "sink");
158
159   gst_element_set_state (udpsink, GST_STATE_PLAYING);
160
161   gst_pad_push_event (srcpad, gst_event_new_new_segment_full (FALSE, 1.0, 1.0,
162           GST_FORMAT_TIME, 0, -1, 0));
163
164   gst_pad_push_list (srcpad, list);
165
166   gst_check_teardown_pad_by_name (udpsink, "sink");
167   gst_check_teardown_element (udpsink);
168
169   if (use_buffer_lists)
170     fail_unless_equals_int (data_size, render_list_bytes_received);
171 }
172
173 GST_START_TEST (test_udpsink)
174 {
175   udpsink_test (FALSE);
176 }
177
178 GST_END_TEST;
179 GST_START_TEST (test_udpsink_bufferlist)
180 {
181   udpsink_test (TRUE);
182 }
183
184 GST_END_TEST;
185
186 /*
187  * Creates the test suite.
188  *
189  * Returns: pointer to the test suite.
190  */
191 static Suite *
192 udpsink_suite (void)
193 {
194   Suite *s = suite_create ("udpsink_test");
195
196   TCase *tc_chain = tcase_create ("linear");
197
198   /* Set timeout to 60 seconds. */
199   tcase_set_timeout (tc_chain, 60);
200
201   suite_add_tcase (s, tc_chain);
202   tcase_add_test (tc_chain, test_udpsink);
203   tcase_add_test (tc_chain, test_udpsink_bufferlist);
204   return s;
205 }
206
207 GST_CHECK_MAIN (udpsink)