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