3 * Unit test for gstrtpbin sending rtp packets using GstBufferList.
4 * Copyright (C) 2009 Branko Subasic <branko dot subasic at axis dot com>
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.
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.
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.
22 #include <gst/check/gstcheck.h>
24 #include <gst/rtp/gstrtpbuffer.h>
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.
34 "application/x-rtp, " \
35 "media=(string)video, " \
36 "clock-rate=(int)90000, " \
37 "encoding-name=(string)H264, " \
38 "profile-level-id=(string)4d4015, " \
40 "ssrc=(guint)2633237432, " \
41 "clock-base=(guint)1868267015, " \
42 "seqnum-base=(guint)54229"
45 /* RTP headers and the first 2 bytes of the payload (FU indicator and FU header)
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}
54 static const guint rtp_header_len[] = {
59 static GstBuffer *header_buffer[2] = { NULL, NULL };
64 static char *payload =
65 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
66 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
67 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
68 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
69 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
70 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
71 "0123456789ABSDEF0123456";
73 static const guint payload_offset[] = {
77 static const guint payload_len[] = {
82 static GstBuffer *original_buffer = NULL;
84 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
87 GST_STATIC_CAPS ("application/x-rtp"));
89 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
92 GST_STATIC_CAPS ("application/x-rtp"));
96 _create_original_buffer (void)
100 if (original_buffer != NULL)
101 return original_buffer;
103 original_buffer = gst_buffer_new ();
104 fail_unless (original_buffer != NULL);
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 ());
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);
115 return original_buffer;
118 static GstBufferList *
119 _create_buffer_list (void)
122 GstBufferListIterator *it;
123 GstBuffer *orig_buffer;
126 orig_buffer = _create_original_buffer ();
127 fail_if (orig_buffer == NULL);
129 list = gst_buffer_list_new ();
130 fail_if (list == NULL);
132 it = gst_buffer_list_iterate (list);
133 fail_if (it == NULL);
135 /*** First group, i.e. first packet. **/
136 gst_buffer_list_iterator_add_group (it);
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);
147 /* Create the payload buffer and add it to the 1st group
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);
155 /*** Second group, i.e. second packet. ***/
157 /* Create a new group to hold the rtp header and the payload */
158 gst_buffer_list_iterator_add_group (it);
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;
168 /* Add the rtp header to the buffer list */
169 gst_buffer_list_iterator_add (it, buffer);
171 /* Create the payload buffer and add it to the 2d group
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);
178 gst_buffer_list_iterator_free (it);
185 _check_header (GstBuffer * buffer, guint index)
189 fail_if (buffer == NULL);
190 fail_unless (index < 2);
192 fail_unless (GST_BUFFER_SIZE (buffer) == rtp_header_len[index]);
194 /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
195 * most likely be changed in gstrtpbin.
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));
205 _check_payload (GstBuffer * buffer, guint index)
207 fail_if (buffer == NULL);
208 fail_unless (index < 2);
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]));
219 _check_group (GstBufferListIterator * it, guint index, GstCaps * caps)
223 fail_unless (it != NULL);
224 fail_unless (gst_buffer_list_iterator_n_buffers (it) == 2);
225 fail_unless (caps != NULL);
227 fail_unless ((buffer = gst_buffer_list_iterator_next (it)) != NULL);
229 fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
230 GST_BUFFER_TIMESTAMP (original_buffer));
232 fail_unless (gst_caps_is_equal (GST_BUFFER_CAPS (original_buffer),
233 GST_BUFFER_CAPS (buffer)));
235 _check_header (buffer, index);
237 fail_unless ((buffer = gst_buffer_list_iterator_next (it)) != NULL);
238 _check_payload (buffer, index);
243 _sink_chain_list (GstPad * pad, GstBufferList * list)
246 GstBufferListIterator *it;
248 caps = gst_caps_from_string (TEST_CAPS);
249 fail_unless (caps != NULL);
251 fail_unless (GST_IS_BUFFER_LIST (list));
252 fail_unless (gst_buffer_list_n_groups (list) == 2);
254 it = gst_buffer_list_iterate (list);
255 fail_if (it == NULL);
257 fail_unless (gst_buffer_list_iterator_next_group (it));
258 _check_group (it, 0, caps);
260 fail_unless (gst_buffer_list_iterator_next_group (it));
261 _check_group (it, 1, caps);
263 gst_caps_unref (caps);
264 gst_buffer_list_iterator_free (it);
266 gst_buffer_list_unref (list);
273 _set_chain_functions (GstPad * pad)
275 gst_pad_set_chain_list_function (pad, _sink_chain_list);
279 GST_START_TEST (test_bufferlist)
286 list = _create_buffer_list ();
287 fail_unless (list != NULL);
289 rtpbin = gst_check_setup_element ("gstrtpbin");
292 gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
293 fail_if (srcpad == NULL);
295 gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
297 fail_if (sinkpad == NULL);
299 _set_chain_functions (sinkpad);
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);
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);
316 bufferlist_suite (void)
318 Suite *s = suite_create ("BufferList");
320 TCase *tc_chain = tcase_create ("general");
322 /* time out after 30s. */
323 tcase_set_timeout (tc_chain, 10);
325 suite_add_tcase (s, tc_chain);
326 tcase_add_test (tc_chain, test_bufferlist);
331 GST_CHECK_MAIN (bufferlist);