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 create_rtp_buffer_fields (gconstpointer header, gint header_size,
136 GstBuffer * payload_buffer, gint payload_offset, gint payload_size,
137 guint16 seqnum, guint32 timestamp)
146 create_rtp_packet_buffer (header, header_size, payload_buffer,
147 payload_offset, payload_size);
148 fail_if (buffer == NULL);
150 memory = gst_buffer_get_memory (buffer, 0);
151 ret = gst_memory_map (memory, &info, GST_MAP_READ);
152 fail_if (ret == FALSE);
154 info.data[2] = (seqnum >> 8) & 0xff;
155 info.data[3] = seqnum & 0xff;
157 tmp = (guint32 *) & (info.data[4]);
158 *tmp = g_htonl (timestamp);
160 gst_memory_unmap (memory, &info);
161 gst_memory_unref (memory);
167 check_seqnum (GstBuffer * buffer, guint16 seqnum)
172 guint16 current_seqnum;
174 fail_if (buffer == NULL);
176 memory = gst_buffer_get_memory (buffer, 0);
177 ret = gst_memory_map (memory, &info, GST_MAP_READ);
178 fail_if (ret == FALSE);
180 current_seqnum = info.data[2] << 8 | info.data[3];
181 fail_unless (current_seqnum == seqnum);
183 gst_memory_unmap (memory, &info);
184 gst_memory_unref (memory);
188 check_header (GstBuffer * buffer, guint index)
194 fail_if (buffer == NULL);
195 fail_unless (index < 2);
197 memory = gst_buffer_get_memory (buffer, 0);
198 ret = gst_memory_map (memory, &info, GST_MAP_READ);
199 fail_if (ret == FALSE);
201 fail_unless (info.size == rtp_header_len[index]);
203 /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
204 * most likely be changed in gstrtpbin.
206 fail_unless (info.data != NULL);
207 fail_unless_equals_uint64 (*(guint64 *) info.data,
208 *(guint64 *) rtp_header[index]);
209 fail_unless (*(guint16 *) (info.data + 12) ==
210 *(guint16 *) (rtp_header[index] + 12));
212 gst_memory_unmap (memory, &info);
213 gst_memory_unref (memory);
217 check_payload (GstBuffer * buffer, guint index)
223 fail_if (buffer == NULL);
224 fail_unless (index < 2);
226 memory = gst_buffer_get_memory (buffer, 1);
227 ret = gst_memory_map (memory, &info, GST_MAP_READ);
228 fail_if (ret == FALSE);
230 fail_unless (info.size == payload_len[index]);
231 fail_if (info.data != (gpointer) (payload + payload_offset[index]));
232 fail_if (memcmp (info.data, payload + payload_offset[index],
233 payload_len[index]));
235 gst_memory_unmap (memory, &info);
236 gst_memory_unref (memory);
240 check_packet (GstBufferList * list, guint list_index, guint packet_index)
244 fail_unless (list != NULL);
246 fail_unless ((buffer = gst_buffer_list_get (list, list_index)) != NULL);
247 fail_unless (gst_buffer_n_memory (buffer) == 2);
249 fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
250 GST_BUFFER_TIMESTAMP (original_buffer));
252 check_header (buffer, packet_index);
253 check_payload (buffer, packet_index);
257 * Used to verify that the chain_list function is actually implemented by the
258 * element and called when executing the pipeline. This is needed because pads
259 * always have a default chain_list handler which handle buffers in a buffer
260 * list individually, and pushing a list to a pad can succeed even if no
261 * chain_list handler has been set.
263 static gboolean chain_list_func_called;
264 static guint chain_list_bytes_received;
266 /* Create two packets with different payloads. */
267 static GstBufferList *
268 create_buffer_list (void)
271 GstBuffer *orig_buffer;
274 orig_buffer = create_original_buffer ();
275 fail_if (orig_buffer == NULL);
277 list = gst_buffer_list_new ();
278 fail_if (list == NULL);
280 /*** First packet. **/
282 create_rtp_packet_buffer (&rtp_header[0], rtp_header_len[0], orig_buffer,
283 payload_offset[0], payload_len[0]);
284 gst_buffer_list_add (list, buffer);
286 /*** Second packet. ***/
288 create_rtp_packet_buffer (&rtp_header[1], rtp_header_len[1], orig_buffer,
289 payload_offset[1], payload_len[1]);
290 gst_buffer_list_add (list, buffer);
295 /* Check that the correct packets have been pushed out of the element. */
297 sink_chain_list (GstPad * pad, GstObject * parent, GstBufferList * list)
299 GstCaps *current_caps;
302 chain_list_func_called = TRUE;
304 current_caps = gst_pad_get_current_caps (pad);
305 fail_unless (current_caps != NULL);
307 caps = gst_caps_from_string (TEST_CAPS);
308 fail_unless (caps != NULL);
310 fail_unless (gst_caps_is_equal (caps, current_caps));
311 gst_caps_unref (caps);
312 gst_caps_unref (current_caps);
314 fail_unless (GST_IS_BUFFER_LIST (list));
315 fail_unless (gst_buffer_list_length (list) == 2);
317 /* received bytes include lower level protocol overhead */
318 chain_list_bytes_received = gst_buffer_list_calculate_size (list) +
319 2 * UDP_IP_HEADER_OVERHEAD;
321 fail_unless (gst_buffer_list_get (list, 0));
322 check_packet (list, 0, 0);
324 fail_unless (gst_buffer_list_get (list, 1));
325 check_packet (list, 1, 1);
327 gst_buffer_list_unref (list);
333 /* Non-consecutive seqnums makes probation fail. */
334 static GstBufferList *
335 create_buffer_list_fail_probation (void)
338 GstBuffer *orig_buffer;
341 guint16 seqnums[] = { 1, 3, 5 };
344 orig_buffer = create_original_buffer ();
345 fail_if (orig_buffer == NULL);
347 list = gst_buffer_list_new ();
348 fail_if (list == NULL);
350 for (i = 0; i < sizeof (seqnums) / sizeof (seqnums[0]); i++) {
352 create_rtp_buffer_fields (&rtp_header[0], rtp_header_len[0],
353 orig_buffer, payload_offset[0], payload_len[0], seqnums[i], 0);
354 gst_buffer_list_add (list, buffer);
360 /* When probation fails this function shouldn't be called. */
362 sink_chain_list_probation_failed (GstPad * pad, GstObject * parent,
363 GstBufferList * list)
365 chain_list_func_called = TRUE;
370 /* Get the stats of the **first** source of the given type (get_sender) */
372 get_source_stats (GstElement * rtpsession,
373 gboolean get_sender, GstStructure ** source_stats)
376 GValueArray *stats_arr;
379 g_object_get (rtpsession, "stats", &stats, NULL);
381 g_value_get_boxed (gst_structure_get_value (stats, "source-stats"));
382 g_assert (stats_arr != NULL);
383 fail_unless (stats_arr->n_values >= 1);
385 *source_stats = NULL;
386 for (i = 0; i < stats_arr->n_values; i++) {
387 GstStructure *tmp_source_stats;
390 tmp_source_stats = g_value_dup_boxed (&stats_arr->values[i]);
391 gst_structure_get (tmp_source_stats, "is-sender", G_TYPE_BOOLEAN,
394 /* Return the stats of the **first** source of the given type. */
395 if (is_sender == get_sender) {
396 *source_stats = tmp_source_stats;
399 gst_structure_free (tmp_source_stats);
402 gst_structure_free (stats);
405 /* Get the source stats given a session and a source type (get_sender) */
407 get_session_source_stats (GstElement * rtpbin, guint session,
408 gboolean get_sender, GstStructure ** source_stats)
410 GstElement *rtpsession;
412 g_signal_emit_by_name (rtpbin, "get-session", session, &rtpsession);
413 fail_if (rtpsession == NULL);
415 get_source_stats (rtpsession, get_sender, source_stats);
417 gst_object_unref (rtpsession);
420 GST_START_TEST (test_bufferlist)
428 guint64 packets_sent;
429 guint64 packets_received;
431 list = create_buffer_list ();
432 fail_unless (list != NULL);
434 rtpbin = gst_check_setup_element ("rtpbin");
437 gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
438 fail_if (srcpad == NULL);
440 gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
442 fail_if (sinkpad == NULL);
444 gst_pad_set_chain_list_function (sinkpad,
445 GST_DEBUG_FUNCPTR (sink_chain_list));
447 gst_pad_set_active (srcpad, TRUE);
448 gst_pad_set_active (sinkpad, TRUE);
450 caps = gst_caps_from_string (TEST_CAPS);
451 gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
452 gst_caps_unref (caps);
454 gst_element_set_state (rtpbin, GST_STATE_PLAYING);
456 chain_list_func_called = FALSE;
457 fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
458 fail_if (chain_list_func_called == FALSE);
460 /* make sure that stats about the number of sent packets are OK too */
461 get_session_source_stats (rtpbin, 0, TRUE, &stats);
462 fail_if (stats == NULL);
464 gst_structure_get (stats,
465 "packets-sent", G_TYPE_UINT64, &packets_sent,
466 "packets-received", G_TYPE_UINT64, &packets_received, NULL);
467 fail_unless (packets_sent == 2);
468 fail_unless (packets_received == 2);
469 gst_structure_free (stats);
471 gst_pad_set_active (sinkpad, FALSE);
472 gst_pad_set_active (srcpad, FALSE);
474 gst_check_teardown_pad_by_name (rtpbin, "send_rtp_src_0");
475 gst_check_teardown_pad_by_name (rtpbin, "send_rtp_sink_0");
476 gst_check_teardown_element (rtpbin);
482 GST_START_TEST (test_bufferlist_recv)
490 guint64 bytes_received;
491 guint64 packets_received;
493 list = create_buffer_list ();
494 fail_unless (list != NULL);
496 rtpbin = gst_check_setup_element ("rtpsession");
499 gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "recv_rtp_sink");
500 fail_if (srcpad == NULL);
503 gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate, "recv_rtp_src");
504 fail_if (sinkpad == NULL);
506 gst_pad_set_chain_list_function (sinkpad,
507 GST_DEBUG_FUNCPTR (sink_chain_list));
509 gst_pad_set_active (srcpad, TRUE);
510 gst_pad_set_active (sinkpad, TRUE);
512 caps = gst_caps_from_string (TEST_CAPS);
513 gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
514 gst_caps_unref (caps);
516 gst_element_set_state (rtpbin, GST_STATE_PLAYING);
518 chain_list_func_called = FALSE;
519 fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
520 fail_if (chain_list_func_called == FALSE);
522 /* make sure that stats about the number of received packets are OK too */
523 /* The source becomes a sender after probation succeeds, pass TRUE here. */
524 get_source_stats (rtpbin, TRUE, &stats);
525 fail_if (stats == NULL);
527 gst_structure_get (stats,
528 "bytes-received", G_TYPE_UINT64, &bytes_received,
529 "packets-received", G_TYPE_UINT64, &packets_received, NULL);
530 fail_unless (packets_received == 2);
531 fail_unless (bytes_received == chain_list_bytes_received);
532 gst_structure_free (stats);
535 gst_pad_set_active (sinkpad, FALSE);
536 gst_pad_set_active (srcpad, FALSE);
538 gst_check_teardown_pad_by_name (rtpbin, "recv_rtp_src");
539 gst_check_teardown_pad_by_name (rtpbin, "recv_rtp_sink");
540 gst_check_teardown_element (rtpbin);
546 GST_START_TEST (test_bufferlist_recv_probation_failed)
554 list = create_buffer_list_fail_probation ();
555 fail_unless (list != NULL);
557 rtpbin = gst_check_setup_element ("rtpsession");
560 gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "recv_rtp_sink");
561 fail_if (srcpad == NULL);
564 gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate, "recv_rtp_src");
565 fail_if (sinkpad == NULL);
567 gst_pad_set_chain_list_function (sinkpad,
568 GST_DEBUG_FUNCPTR (sink_chain_list_probation_failed));
570 gst_pad_set_active (srcpad, TRUE);
571 gst_pad_set_active (sinkpad, TRUE);
573 caps = gst_caps_from_string (TEST_CAPS);
574 gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
575 gst_caps_unref (caps);
577 gst_element_set_state (rtpbin, GST_STATE_PLAYING);
579 chain_list_func_called = FALSE;
580 fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
581 /* when probation fails the list should not be pushed at all, and the
582 * chain_list functions should not be called, fail if it has been. */
583 fail_if (chain_list_func_called == TRUE);
585 gst_pad_set_active (sinkpad, FALSE);
586 gst_pad_set_active (srcpad, FALSE);
588 gst_check_teardown_pad_by_name (rtpbin, "recv_rtp_src");
589 gst_check_teardown_pad_by_name (rtpbin, "recv_rtp_sink");
590 gst_check_teardown_element (rtpbin);
597 bufferlist_suite (void)
599 Suite *s = suite_create ("BufferList");
601 TCase *tc_chain = tcase_create ("general");
603 /* time out after 30s. */
604 tcase_set_timeout (tc_chain, 10);
606 suite_add_tcase (s, tc_chain);
607 tcase_add_test (tc_chain, test_bufferlist);
608 tcase_add_test (tc_chain, test_bufferlist_recv);
609 tcase_add_test (tc_chain, test_bufferlist_recv_probation_failed);
614 GST_CHECK_MAIN (bufferlist);