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>
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.
35 "application/x-rtp, " \
36 "media=(string)video, " \
37 "clock-rate=(int)90000, " \
38 "encoding-name=(string)H264, " \
39 "profile-level-id=(string)4d4015, " \
41 "ssrc=(guint)2633237432, " \
42 "clock-base=(guint)1868267015, " \
43 "seqnum-base=(guint)54229"
46 /* RTP headers and the first 2 bytes of the payload (FU indicator and FU header)
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}
55 static const guint rtp_header_len[] = {
60 static GstBuffer *header_buffer[2] = { NULL, NULL };
65 static const char *payload =
66 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
67 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
68 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
69 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
70 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
71 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
72 "0123456789ABSDEF0123456";
74 static const guint payload_offset[] = {
78 static const guint payload_len[] = {
83 static GstBuffer *original_buffer = NULL;
85 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
88 GST_STATIC_CAPS ("application/x-rtp"));
90 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
93 GST_STATIC_CAPS ("application/x-rtp"));
97 _create_original_buffer (void)
101 if (original_buffer != NULL)
102 return original_buffer;
104 original_buffer = gst_buffer_new ();
105 fail_unless (original_buffer != NULL);
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 ());
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);
116 return original_buffer;
119 static GstBufferList *
120 _create_buffer_list (void)
123 GstBufferListIterator *it;
124 GstBuffer *orig_buffer;
127 orig_buffer = _create_original_buffer ();
128 fail_if (orig_buffer == NULL);
130 list = gst_buffer_list_new ();
131 fail_if (list == NULL);
133 it = gst_buffer_list_iterate (list);
134 fail_if (it == NULL);
136 /*** First group, i.e. first packet. **/
137 gst_buffer_list_iterator_add_group (it);
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);
148 /* Create the payload buffer and add it to the 1st group
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);
156 /*** Second group, i.e. second packet. ***/
158 /* Create a new group to hold the rtp header and the payload */
159 gst_buffer_list_iterator_add_group (it);
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;
169 /* Add the rtp header to the buffer list */
170 gst_buffer_list_iterator_add (it, buffer);
172 /* Create the payload buffer and add it to the 2d group
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);
179 gst_buffer_list_iterator_free (it);
186 _check_header (GstBuffer * buffer, guint index)
190 fail_if (buffer == NULL);
191 fail_unless (index < 2);
193 fail_unless (GST_BUFFER_SIZE (buffer) == rtp_header_len[index]);
195 /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
196 * most likely be changed in gstrtpbin.
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));
206 _check_payload (GstBuffer * buffer, guint index)
208 fail_if (buffer == NULL);
209 fail_unless (index < 2);
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]));
220 _check_group (GstBufferListIterator * it, guint index, GstCaps * caps)
224 fail_unless (it != NULL);
225 fail_unless (gst_buffer_list_iterator_n_buffers (it) == 2);
226 fail_unless (caps != NULL);
228 fail_unless ((buffer = gst_buffer_list_iterator_next (it)) != NULL);
230 fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
231 GST_BUFFER_TIMESTAMP (original_buffer));
233 fail_unless (gst_caps_is_equal (GST_BUFFER_CAPS (original_buffer),
234 GST_BUFFER_CAPS (buffer)));
236 _check_header (buffer, index);
238 fail_unless ((buffer = gst_buffer_list_iterator_next (it)) != NULL);
239 _check_payload (buffer, index);
244 _sink_chain_list (GstPad * pad, GstBufferList * list)
247 GstBufferListIterator *it;
249 caps = gst_caps_from_string (TEST_CAPS);
250 fail_unless (caps != NULL);
252 fail_unless (GST_IS_BUFFER_LIST (list));
253 fail_unless (gst_buffer_list_n_groups (list) == 2);
255 it = gst_buffer_list_iterate (list);
256 fail_if (it == NULL);
258 fail_unless (gst_buffer_list_iterator_next_group (it));
259 _check_group (it, 0, caps);
261 fail_unless (gst_buffer_list_iterator_next_group (it));
262 _check_group (it, 1, caps);
264 gst_caps_unref (caps);
265 gst_buffer_list_iterator_free (it);
267 gst_buffer_list_unref (list);
274 _set_chain_functions (GstPad * pad)
276 gst_pad_set_chain_list_function (pad, _sink_chain_list);
280 GST_START_TEST (test_bufferlist)
287 list = _create_buffer_list ();
288 fail_unless (list != NULL);
290 rtpbin = gst_check_setup_element ("gstrtpbin");
293 gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
294 fail_if (srcpad == NULL);
296 gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
298 fail_if (sinkpad == NULL);
300 _set_chain_functions (sinkpad);
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);
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);
318 bufferlist_suite (void)
320 Suite *s = suite_create ("BufferList");
322 TCase *tc_chain = tcase_create ("general");
324 /* time out after 30s. */
325 tcase_set_timeout (tc_chain, 10);
327 suite_add_tcase (s, tc_chain);
329 tcase_add_test (tc_chain, test_bufferlist);
335 GST_CHECK_MAIN (bufferlist);