3 * Unit test for gstrtpbin sending rtp packets using GstBufferList.
4 * Copyright (C) 2009 Branko Subasic <branko dot subasic at axis dot com>
5 * Copyright 2019, Collabora Ltd.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 #include <gst/check/gstcheck.h>
25 #include <gst/rtp/gstrtpbuffer.h>
27 /* UDP/IP is assumed for bandwidth calculation */
28 #define UDP_IP_HEADER_OVERHEAD 28
30 /* This test makes sure that RTP packets sent as buffer lists are sent through
31 * the rtpbin as they are supposed to, and not corrupted in any way.
36 "application/x-rtp, " \
37 "media=(string)video, " \
38 "clock-rate=(int)90000, " \
39 "encoding-name=(string)H264, " \
40 "profile-level-id=(string)4d4015, " \
42 "ssrc=(guint)2633237432, " \
43 "clock-base=(guint)1868267015, " \
44 "seqnum-base=(guint)54229"
47 /* RTP headers and the first 2 bytes of the payload (FU indicator and FU header)
49 static const guint8 rtp_header[2][14] = {
50 {0x80, 0x60, 0xbb, 0xb7, 0x5c, 0xe9, 0x09,
51 0x0d, 0xf5, 0x9c, 0x43, 0x55, 0x1c, 0x86},
52 {0x80, 0x60, 0xbb, 0xb8, 0x5c, 0xe9, 0x09,
53 0x0d, 0xf5, 0x9c, 0x43, 0x55, 0x1c, 0x46}
56 static const guint rtp_header_len[] = {
63 static const char *payload =
64 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
65 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
66 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
67 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
68 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
69 "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
70 "0123456789ABSDEF0123456";
72 static const guint payload_offset[] = {
76 static const guint payload_len[] = {
81 static GstBuffer *original_buffer = NULL;
83 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
86 GST_STATIC_CAPS ("application/x-rtp"));
88 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
91 GST_STATIC_CAPS ("application/x-rtp"));
95 create_original_buffer (void)
97 if (original_buffer != NULL)
98 return original_buffer;
101 gst_buffer_new_wrapped ((guint8 *) payload, strlen (payload));
102 fail_unless (original_buffer != NULL);
104 GST_BUFFER_TIMESTAMP (original_buffer) =
105 gst_clock_get_internal_time (gst_system_clock_obtain ());
107 return original_buffer;
111 create_rtp_packet_buffer (gconstpointer header, gint header_size,
112 GstBuffer * payload_buffer, gint payload_offset, gint payload_size)
115 GstBuffer *sub_buffer;
117 /* Create buffer with RTP header. */
118 buffer = gst_buffer_new_allocate (NULL, header_size, NULL);
119 gst_buffer_fill (buffer, 0, header, header_size);
120 gst_buffer_copy_into (buffer, payload_buffer, GST_BUFFER_COPY_METADATA, 0,
123 /* Create the payload buffer and add it to the current buffer. */
125 gst_buffer_copy_region (payload_buffer, GST_BUFFER_COPY_MEMORY,
126 payload_offset, payload_size);
128 buffer = gst_buffer_append (buffer, sub_buffer);
129 fail_if (buffer == NULL);
135 check_seqnum (GstBuffer * buffer, guint16 seqnum)
140 guint16 current_seqnum;
142 fail_if (buffer == NULL);
144 memory = gst_buffer_get_memory (buffer, 0);
145 ret = gst_memory_map (memory, &info, GST_MAP_READ);
146 fail_if (ret == FALSE);
148 current_seqnum = info.data[2] << 8 | info.data[3];
149 fail_unless (current_seqnum == seqnum);
151 gst_memory_unmap (memory, &info);
152 gst_memory_unref (memory);
156 check_header (GstBuffer * buffer, guint index)
162 fail_if (buffer == NULL);
163 fail_unless (index < 2);
165 memory = gst_buffer_get_memory (buffer, 0);
166 ret = gst_memory_map (memory, &info, GST_MAP_READ);
167 fail_if (ret == FALSE);
169 fail_unless (info.size == rtp_header_len[index]);
171 /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
172 * most likely be changed in gstrtpbin.
174 fail_unless (info.data != NULL);
175 fail_unless_equals_uint64 (*(guint64 *) info.data,
176 *(guint64 *) rtp_header[index]);
177 fail_unless (*(guint16 *) (info.data + 12) ==
178 *(guint16 *) (rtp_header[index] + 12));
180 gst_memory_unmap (memory, &info);
181 gst_memory_unref (memory);
185 check_payload (GstBuffer * buffer, guint index)
191 fail_if (buffer == NULL);
192 fail_unless (index < 2);
194 memory = gst_buffer_get_memory (buffer, 1);
195 ret = gst_memory_map (memory, &info, GST_MAP_READ);
196 fail_if (ret == FALSE);
198 fail_unless (info.size == payload_len[index]);
199 fail_if (info.data != (gpointer) (payload + payload_offset[index]));
200 fail_if (memcmp (info.data, payload + payload_offset[index],
201 payload_len[index]));
203 gst_memory_unmap (memory, &info);
204 gst_memory_unref (memory);
208 check_packet (GstBufferList * list, guint list_index, guint packet_index)
212 fail_unless (list != NULL);
214 fail_unless ((buffer = gst_buffer_list_get (list, list_index)) != NULL);
215 fail_unless (gst_buffer_n_memory (buffer) == 2);
217 fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
218 GST_BUFFER_TIMESTAMP (original_buffer));
220 check_header (buffer, packet_index);
221 check_payload (buffer, packet_index);
225 * Used to verify that the chain_list function is actually implemented by the
226 * element and called when executing the pipeline. This is needed because pads
227 * always have a default chain_list handler which handle buffers in a buffer
228 * list individually, and pushing a list to a pad can succeed even if no
229 * chain_list handler has been set.
231 static gboolean chain_list_func_called;
232 static guint chain_list_bytes_received;
234 /* Create two packets with different payloads. */
235 static GstBufferList *
236 create_buffer_list (void)
239 GstBuffer *orig_buffer;
242 orig_buffer = create_original_buffer ();
243 fail_if (orig_buffer == NULL);
245 list = gst_buffer_list_new ();
246 fail_if (list == NULL);
248 /*** First packet. **/
250 create_rtp_packet_buffer (&rtp_header[0], rtp_header_len[0], orig_buffer,
251 payload_offset[0], payload_len[0]);
252 gst_buffer_list_add (list, buffer);
254 /*** Second packet. ***/
256 create_rtp_packet_buffer (&rtp_header[1], rtp_header_len[1], orig_buffer,
257 payload_offset[1], payload_len[1]);
258 gst_buffer_list_add (list, buffer);
263 /* Check that the correct packets have been pushed out of the element. */
265 sink_chain_list (GstPad * pad, GstObject * parent, GstBufferList * list)
267 GstCaps *current_caps;
270 chain_list_func_called = TRUE;
272 current_caps = gst_pad_get_current_caps (pad);
273 fail_unless (current_caps != NULL);
275 caps = gst_caps_from_string (TEST_CAPS);
276 fail_unless (caps != NULL);
278 fail_unless (gst_caps_is_equal (caps, current_caps));
279 gst_caps_unref (caps);
280 gst_caps_unref (current_caps);
282 fail_unless (GST_IS_BUFFER_LIST (list));
283 fail_unless (gst_buffer_list_length (list) == 2);
285 /* received bytes include lower level protocol overhead */
286 chain_list_bytes_received = gst_buffer_list_calculate_size (list) +
287 2 * UDP_IP_HEADER_OVERHEAD;
289 fail_unless (gst_buffer_list_get (list, 0));
290 check_packet (list, 0, 0);
292 fail_unless (gst_buffer_list_get (list, 1));
293 check_packet (list, 1, 1);
295 gst_buffer_list_unref (list);
300 /* Get the stats of the **first** source of the given type (get_sender) */
302 get_source_stats (GstElement * rtpsession,
303 gboolean get_sender, GstStructure ** source_stats)
306 GValueArray *stats_arr;
309 g_object_get (rtpsession, "stats", &stats, NULL);
311 g_value_get_boxed (gst_structure_get_value (stats, "source-stats"));
312 g_assert (stats_arr != NULL);
313 fail_unless (stats_arr->n_values >= 1);
315 *source_stats = NULL;
316 for (i = 0; i < stats_arr->n_values; i++) {
317 GstStructure *tmp_source_stats;
320 tmp_source_stats = g_value_dup_boxed (&stats_arr->values[i]);
321 gst_structure_get (tmp_source_stats, "is-sender", G_TYPE_BOOLEAN,
324 /* Return the stats of the **first** source of the given type. */
325 if (is_sender == get_sender) {
326 *source_stats = tmp_source_stats;
329 gst_structure_free (tmp_source_stats);
332 gst_structure_free (stats);
335 /* Get the source stats given a session and a source type (get_sender) */
337 get_session_source_stats (GstElement * rtpbin, guint session,
338 gboolean get_sender, GstStructure ** source_stats)
340 GstElement *rtpsession;
342 g_signal_emit_by_name (rtpbin, "get-session", session, &rtpsession);
343 fail_if (rtpsession == NULL);
345 get_source_stats (rtpsession, get_sender, source_stats);
347 gst_object_unref (rtpsession);
350 GST_START_TEST (test_bufferlist)
358 guint64 packets_sent;
359 guint64 packets_received;
361 list = create_buffer_list ();
362 fail_unless (list != NULL);
364 rtpbin = gst_check_setup_element ("rtpbin");
367 gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
368 fail_if (srcpad == NULL);
370 gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
372 fail_if (sinkpad == NULL);
374 gst_pad_set_chain_list_function (sinkpad,
375 GST_DEBUG_FUNCPTR (sink_chain_list));
377 gst_pad_set_active (srcpad, TRUE);
378 gst_pad_set_active (sinkpad, TRUE);
380 caps = gst_caps_from_string (TEST_CAPS);
381 gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
382 gst_caps_unref (caps);
384 gst_element_set_state (rtpbin, GST_STATE_PLAYING);
386 chain_list_func_called = FALSE;
387 fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
388 fail_if (chain_list_func_called == FALSE);
390 /* make sure that stats about the number of sent packets are OK too */
391 get_session_source_stats (rtpbin, 0, TRUE, &stats);
392 fail_if (stats == NULL);
394 gst_structure_get (stats,
395 "packets-sent", G_TYPE_UINT64, &packets_sent,
396 "packets-received", G_TYPE_UINT64, &packets_received, NULL);
397 fail_unless (packets_sent == 2);
398 fail_unless (packets_received == 2);
399 gst_structure_free (stats);
401 gst_pad_set_active (sinkpad, FALSE);
402 gst_pad_set_active (srcpad, FALSE);
404 gst_check_teardown_pad_by_name (rtpbin, "send_rtp_src_0");
405 gst_check_teardown_pad_by_name (rtpbin, "send_rtp_sink_0");
406 gst_check_teardown_element (rtpbin);
412 GST_START_TEST (test_bufferlist_recv)
420 guint64 bytes_received;
421 guint64 packets_received;
423 list = create_buffer_list ();
424 fail_unless (list != NULL);
426 rtpbin = gst_check_setup_element ("rtpsession");
429 gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "recv_rtp_sink");
430 fail_if (srcpad == NULL);
433 gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate, "recv_rtp_src");
434 fail_if (sinkpad == NULL);
436 gst_pad_set_chain_list_function (sinkpad,
437 GST_DEBUG_FUNCPTR (sink_chain_list));
439 gst_pad_set_active (srcpad, TRUE);
440 gst_pad_set_active (sinkpad, TRUE);
442 caps = gst_caps_from_string (TEST_CAPS);
443 gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
444 gst_caps_unref (caps);
446 gst_element_set_state (rtpbin, GST_STATE_PLAYING);
448 chain_list_func_called = FALSE;
449 fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
450 fail_if (chain_list_func_called == FALSE);
452 /* make sure that stats about the number of received packets are OK too */
453 /* The source becomes a sender after probation succeeds, pass TRUE here. */
454 get_source_stats (rtpbin, TRUE, &stats);
455 fail_if (stats == NULL);
457 gst_structure_get (stats,
458 "bytes-received", G_TYPE_UINT64, &bytes_received,
459 "packets-received", G_TYPE_UINT64, &packets_received, NULL);
460 fail_unless (packets_received == 2);
461 fail_unless (bytes_received == chain_list_bytes_received);
462 gst_structure_free (stats);
465 gst_pad_set_active (sinkpad, FALSE);
466 gst_pad_set_active (srcpad, FALSE);
468 gst_check_teardown_pad_by_name (rtpbin, "recv_rtp_src");
469 gst_check_teardown_pad_by_name (rtpbin, "recv_rtp_sink");
470 gst_check_teardown_element (rtpbin);
476 bufferlist_suite (void)
478 Suite *s = suite_create ("BufferList");
480 TCase *tc_chain = tcase_create ("general");
482 /* time out after 30s. */
483 tcase_set_timeout (tc_chain, 10);
485 suite_add_tcase (s, tc_chain);
486 tcase_add_test (tc_chain, test_bufferlist);
487 tcase_add_test (tc_chain, test_bufferlist_recv);
492 GST_CHECK_MAIN (bufferlist);