Add -Wwrite-strings to the configure flags
[platform/upstream/gstreamer.git] / tests / check / elements / rtpbin_buffer_list.c
1 /* GStreamer
2  *
3  * Unit test for gstrtpbin sending rtp packets using GstBufferList.
4  * Copyright (C) 2009 Branko Subasic <branko dot subasic at axis dot com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include <gst/check/gstcheck.h>
23
24 #include <gst/rtp/gstrtpbuffer.h>
25
26
27
28 /* This test makes sure that RTP packets sent as buffer lists are sent through
29  * the rtpbin as they are supposed to, and not corrupted in any way.
30  */
31
32
33 #define TEST_CAPS \
34   "application/x-rtp, "                \
35   "media=(string)video, "              \
36   "clock-rate=(int)90000, "            \
37   "encoding-name=(string)H264, "       \
38   "profile-level-id=(string)4d4015, "  \
39   "payload=(int)96, "                  \
40   "ssrc=(guint)2633237432, "           \
41   "clock-base=(guint)1868267015, "     \
42   "seqnum-base=(guint)54229"
43
44
45 /* RTP headers and the first 2 bytes of the payload (FU indicator and FU header)
46  */
47 static const guint8 rtp_header[2][14] = {
48   {0x80, 0x60, 0xbb, 0xb7, 0x5c, 0xe9, 0x09,
49       0x0d, 0xf5, 0x9c, 0x43, 0x55, 0x1c, 0x86},
50   {0x80, 0x60, 0xbb, 0xb8, 0x5c, 0xe9, 0x09,
51       0x0d, 0xf5, 0x9c, 0x43, 0x55, 0x1c, 0x46}
52 };
53
54 static const guint rtp_header_len[] = {
55   sizeof rtp_header[0],
56   sizeof rtp_header[1]
57 };
58
59 static GstBuffer *header_buffer[2] = { NULL, NULL };
60
61
62 /* Some payload.
63  */
64 static const char *payload =
65     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
66     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
67     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
68     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
69     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
70     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
71     "0123456789ABSDEF0123456";
72
73 static const guint payload_offset[] = {
74   0, 498
75 };
76
77 static const guint payload_len[] = {
78   498, 5
79 };
80
81
82 static GstBuffer *original_buffer = NULL;
83
84 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
85     GST_PAD_SINK,
86     GST_PAD_ALWAYS,
87     GST_STATIC_CAPS ("application/x-rtp"));
88
89 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
90     GST_PAD_SRC,
91     GST_PAD_ALWAYS,
92     GST_STATIC_CAPS ("application/x-rtp"));
93
94
95 static GstBuffer *
96 _create_original_buffer (void)
97 {
98   GstCaps *caps;
99
100   if (original_buffer != NULL)
101     return original_buffer;
102
103   original_buffer = gst_buffer_new ();
104   fail_unless (original_buffer != NULL);
105
106   gst_buffer_set_data (original_buffer, (guint8 *) payload, strlen (payload));
107   GST_BUFFER_TIMESTAMP (original_buffer) =
108       gst_clock_get_internal_time (gst_system_clock_obtain ());
109
110   caps = gst_caps_from_string (TEST_CAPS);
111   fail_unless (caps != NULL);
112   gst_buffer_set_caps (original_buffer, caps);
113   gst_caps_unref (caps);
114
115   return original_buffer;
116 }
117
118 static GstBufferList *
119 _create_buffer_list (void)
120 {
121   GstBufferList *list;
122   GstBufferListIterator *it;
123   GstBuffer *orig_buffer;
124   GstBuffer *buffer;
125
126   orig_buffer = _create_original_buffer ();
127   fail_if (orig_buffer == NULL);
128
129   list = gst_buffer_list_new ();
130   fail_if (list == NULL);
131
132   it = gst_buffer_list_iterate (list);
133   fail_if (it == NULL);
134
135   /*** First group, i.e. first packet. **/
136   gst_buffer_list_iterator_add_group (it);
137
138   /* Create buffer with RTP header and add it to the 1st group */
139   buffer = gst_buffer_new ();
140   GST_BUFFER_MALLOCDATA (buffer) = g_memdup (&rtp_header[0], rtp_header_len[0]);
141   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
142   GST_BUFFER_SIZE (buffer) = rtp_header_len[0];
143   gst_buffer_copy_metadata (buffer, orig_buffer, GST_BUFFER_COPY_ALL);
144   header_buffer[0] = buffer;
145   gst_buffer_list_iterator_add (it, buffer);
146
147   /* Create the payload buffer and add it to the 1st group
148    */
149   buffer =
150       gst_buffer_create_sub (orig_buffer, payload_offset[0], payload_len[0]);
151   fail_if (buffer == NULL);
152   gst_buffer_list_iterator_add (it, buffer);
153
154
155   /***  Second group, i.e. second packet. ***/
156
157   /* Create a new group to hold the rtp header and the payload */
158   gst_buffer_list_iterator_add_group (it);
159
160   /* Create buffer with RTP header and add it to the 2nd group */
161   buffer = gst_buffer_new ();
162   GST_BUFFER_MALLOCDATA (buffer) = g_memdup (&rtp_header[1], rtp_header_len[1]);
163   GST_BUFFER_DATA (buffer) = GST_BUFFER_MALLOCDATA (buffer);
164   GST_BUFFER_SIZE (buffer) = rtp_header_len[1];
165   gst_buffer_copy_metadata (buffer, orig_buffer, GST_BUFFER_COPY_ALL);
166   header_buffer[1] = buffer;
167
168   /* Add the rtp header to the buffer list */
169   gst_buffer_list_iterator_add (it, buffer);
170
171   /* Create the payload buffer and add it to the 2d group
172    */
173   buffer =
174       gst_buffer_create_sub (orig_buffer, payload_offset[1], payload_len[1]);
175   fail_if (buffer == NULL);
176   gst_buffer_list_iterator_add (it, buffer);
177
178   gst_buffer_list_iterator_free (it);
179
180   return list;
181 }
182
183
184 static void
185 _check_header (GstBuffer * buffer, guint index)
186 {
187   guint8 *data;
188
189   fail_if (buffer == NULL);
190   fail_unless (index < 2);
191
192   fail_unless (GST_BUFFER_SIZE (buffer) == rtp_header_len[index]);
193
194   /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
195    * most likely be changed in gstrtpbin.
196    */
197   fail_unless ((data = GST_BUFFER_DATA (buffer)) != NULL);
198   fail_unless_equals_uint64 (*(guint64 *) data, *(guint64 *) rtp_header[index]);
199   fail_unless (*(guint16 *) (data + 12) ==
200       *(guint16 *) (rtp_header[index] + 12));
201 }
202
203
204 static void
205 _check_payload (GstBuffer * buffer, guint index)
206 {
207   fail_if (buffer == NULL);
208   fail_unless (index < 2);
209
210   fail_unless (GST_BUFFER_SIZE (buffer) == payload_len[index]);
211   fail_if (GST_BUFFER_DATA (buffer) !=
212       (gpointer) (payload + payload_offset[index]));
213   fail_if (memcmp (GST_BUFFER_DATA (buffer), payload + payload_offset[index],
214           payload_len[index]));
215 }
216
217
218 static void
219 _check_group (GstBufferListIterator * it, guint index, GstCaps * caps)
220 {
221   GstBuffer *buffer;
222
223   fail_unless (it != NULL);
224   fail_unless (gst_buffer_list_iterator_n_buffers (it) == 2);
225   fail_unless (caps != NULL);
226
227   fail_unless ((buffer = gst_buffer_list_iterator_next (it)) != NULL);
228
229   fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
230       GST_BUFFER_TIMESTAMP (original_buffer));
231
232   fail_unless (gst_caps_is_equal (GST_BUFFER_CAPS (original_buffer),
233           GST_BUFFER_CAPS (buffer)));
234
235   _check_header (buffer, index);
236
237   fail_unless ((buffer = gst_buffer_list_iterator_next (it)) != NULL);
238   _check_payload (buffer, index);
239 }
240
241
242 static GstFlowReturn
243 _sink_chain_list (GstPad * pad, GstBufferList * list)
244 {
245   GstCaps *caps;
246   GstBufferListIterator *it;
247
248   caps = gst_caps_from_string (TEST_CAPS);
249   fail_unless (caps != NULL);
250
251   fail_unless (GST_IS_BUFFER_LIST (list));
252   fail_unless (gst_buffer_list_n_groups (list) == 2);
253
254   it = gst_buffer_list_iterate (list);
255   fail_if (it == NULL);
256
257   fail_unless (gst_buffer_list_iterator_next_group (it));
258   _check_group (it, 0, caps);
259
260   fail_unless (gst_buffer_list_iterator_next_group (it));
261   _check_group (it, 1, caps);
262
263   gst_caps_unref (caps);
264   gst_buffer_list_iterator_free (it);
265
266   gst_buffer_list_unref (list);
267
268   return GST_FLOW_OK;
269 }
270
271
272 static void
273 _set_chain_functions (GstPad * pad)
274 {
275   gst_pad_set_chain_list_function (pad, _sink_chain_list);
276 }
277
278
279 GST_START_TEST (test_bufferlist)
280 {
281   GstElement *rtpbin;
282   GstPad *sinkpad;
283   GstPad *srcpad;
284   GstBufferList *list;
285
286   list = _create_buffer_list ();
287   fail_unless (list != NULL);
288
289   rtpbin = gst_check_setup_element ("gstrtpbin");
290
291   srcpad =
292       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
293   fail_if (srcpad == NULL);
294   sinkpad =
295       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
296       "send_rtp_src_0");
297   fail_if (sinkpad == NULL);
298
299   _set_chain_functions (sinkpad);
300
301   gst_pad_set_active (sinkpad, TRUE);
302   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
303   fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
304   gst_pad_set_active (sinkpad, FALSE);
305
306   gst_check_teardown_pad_by_name (rtpbin, "send_rtp_src_0");
307   gst_check_teardown_pad_by_name (rtpbin, "send_rtp_sink_0");
308   gst_check_teardown_element (rtpbin);
309 }
310
311 GST_END_TEST;
312
313
314
315 static Suite *
316 bufferlist_suite (void)
317 {
318   Suite *s = suite_create ("BufferList");
319
320   TCase *tc_chain = tcase_create ("general");
321
322   /* time out after 30s. */
323   tcase_set_timeout (tc_chain, 10);
324
325   suite_add_tcase (s, tc_chain);
326   tcase_add_test (tc_chain, test_bufferlist);
327
328   return s;
329 }
330
331 GST_CHECK_MAIN (bufferlist);