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_header (GstBuffer * buffer, guint index)
141 fail_if (buffer == NULL);
142 fail_unless (index < 2);
144 memory = gst_buffer_get_memory (buffer, 0);
145 ret = gst_memory_map (memory, &info, GST_MAP_READ);
146 fail_if (ret == FALSE);
148 fail_unless (info.size == rtp_header_len[index]);
150 /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
151 * most likely be changed in gstrtpbin.
153 fail_unless (info.data != NULL);
154 fail_unless_equals_uint64 (*(guint64 *) info.data,
155 *(guint64 *) rtp_header[index]);
156 fail_unless (*(guint16 *) (info.data + 12) ==
157 *(guint16 *) (rtp_header[index] + 12));
159 gst_memory_unmap (memory, &info);
160 gst_memory_unref (memory);
164 check_payload (GstBuffer * buffer, guint index)
170 fail_if (buffer == NULL);
171 fail_unless (index < 2);
173 memory = gst_buffer_get_memory (buffer, 1);
174 ret = gst_memory_map (memory, &info, GST_MAP_READ);
175 fail_if (ret == FALSE);
177 fail_unless (info.size == payload_len[index]);
178 fail_if (info.data != (gpointer) (payload + payload_offset[index]));
179 fail_if (memcmp (info.data, payload + payload_offset[index],
180 payload_len[index]));
182 gst_memory_unmap (memory, &info);
183 gst_memory_unref (memory);
187 check_packet (GstBufferList * list, guint list_index, guint packet_index)
191 fail_unless (list != NULL);
193 fail_unless ((buffer = gst_buffer_list_get (list, list_index)) != NULL);
194 fail_unless (gst_buffer_n_memory (buffer) == 2);
196 fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
197 GST_BUFFER_TIMESTAMP (original_buffer));
199 check_header (buffer, packet_index);
200 check_payload (buffer, packet_index);
204 * Used to verify that the chain_list function is actually implemented by the
205 * element and called when executing the pipeline. This is needed because pads
206 * always have a default chain_list handler which handle buffers in a buffer
207 * list individually, and pushing a list to a pad can succeed even if no
208 * chain_list handler has been set.
210 static gboolean chain_list_func_called;
211 static guint chain_list_bytes_received;
213 /* Create two packets with different payloads. */
214 static GstBufferList *
215 create_buffer_list (void)
218 GstBuffer *orig_buffer;
221 orig_buffer = create_original_buffer ();
222 fail_if (orig_buffer == NULL);
224 list = gst_buffer_list_new ();
225 fail_if (list == NULL);
227 /*** First packet. **/
229 create_rtp_packet_buffer (&rtp_header[0], rtp_header_len[0], orig_buffer,
230 payload_offset[0], payload_len[0]);
231 gst_buffer_list_add (list, buffer);
233 /*** Second packet. ***/
235 create_rtp_packet_buffer (&rtp_header[1], rtp_header_len[1], orig_buffer,
236 payload_offset[1], payload_len[1]);
237 gst_buffer_list_add (list, buffer);
242 /* Check that the correct packets have been pushed out of the element. */
244 sink_chain_list (GstPad * pad, GstObject * parent, GstBufferList * list)
246 GstCaps *current_caps;
249 chain_list_func_called = TRUE;
251 current_caps = gst_pad_get_current_caps (pad);
252 fail_unless (current_caps != NULL);
254 caps = gst_caps_from_string (TEST_CAPS);
255 fail_unless (caps != NULL);
257 fail_unless (gst_caps_is_equal (caps, current_caps));
258 gst_caps_unref (caps);
259 gst_caps_unref (current_caps);
261 fail_unless (GST_IS_BUFFER_LIST (list));
262 fail_unless (gst_buffer_list_length (list) == 2);
264 /* received bytes include lower level protocol overhead */
265 chain_list_bytes_received = gst_buffer_list_calculate_size (list) +
266 2 * UDP_IP_HEADER_OVERHEAD;
268 fail_unless (gst_buffer_list_get (list, 0));
269 check_packet (list, 0, 0);
271 fail_unless (gst_buffer_list_get (list, 1));
272 check_packet (list, 1, 1);
274 gst_buffer_list_unref (list);
279 /* Get the stats of the **first** source of the given type (get_sender) */
281 get_source_stats (GstElement * rtpsession,
282 gboolean get_sender, GstStructure ** source_stats)
285 GValueArray *stats_arr;
288 g_object_get (rtpsession, "stats", &stats, NULL);
290 g_value_get_boxed (gst_structure_get_value (stats, "source-stats"));
291 g_assert (stats_arr != NULL);
292 fail_unless (stats_arr->n_values >= 1);
294 *source_stats = NULL;
295 for (i = 0; i < stats_arr->n_values; i++) {
296 GstStructure *tmp_source_stats;
299 tmp_source_stats = g_value_dup_boxed (&stats_arr->values[i]);
300 gst_structure_get (tmp_source_stats, "is-sender", G_TYPE_BOOLEAN,
303 /* Return the stats of the **first** source of the given type. */
304 if (is_sender == get_sender) {
305 *source_stats = tmp_source_stats;
308 gst_structure_free (tmp_source_stats);
311 gst_structure_free (stats);
314 /* Get the source stats given a session and a source type (get_sender) */
316 get_session_source_stats (GstElement * rtpbin, guint session,
317 gboolean get_sender, GstStructure ** source_stats)
319 GstElement *rtpsession;
321 g_signal_emit_by_name (rtpbin, "get-session", session, &rtpsession);
322 fail_if (rtpsession == NULL);
324 get_source_stats (rtpsession, get_sender, source_stats);
326 gst_object_unref (rtpsession);
329 GST_START_TEST (test_bufferlist)
337 guint64 packets_sent;
338 guint64 packets_received;
340 list = create_buffer_list ();
341 fail_unless (list != NULL);
343 rtpbin = gst_check_setup_element ("rtpbin");
346 gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
347 fail_if (srcpad == NULL);
349 gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
351 fail_if (sinkpad == NULL);
353 gst_pad_set_chain_list_function (sinkpad,
354 GST_DEBUG_FUNCPTR (sink_chain_list));
356 gst_pad_set_active (srcpad, TRUE);
357 gst_pad_set_active (sinkpad, TRUE);
359 caps = gst_caps_from_string (TEST_CAPS);
360 gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
361 gst_caps_unref (caps);
363 gst_element_set_state (rtpbin, GST_STATE_PLAYING);
365 chain_list_func_called = FALSE;
366 fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
367 fail_if (chain_list_func_called == FALSE);
369 /* make sure that stats about the number of sent packets are OK too */
370 get_session_source_stats (rtpbin, 0, TRUE, &stats);
371 fail_if (stats == NULL);
373 gst_structure_get (stats,
374 "packets-sent", G_TYPE_UINT64, &packets_sent,
375 "packets-received", G_TYPE_UINT64, &packets_received, NULL);
376 fail_unless (packets_sent == 2);
377 fail_unless (packets_received == 2);
378 gst_structure_free (stats);
380 gst_pad_set_active (sinkpad, FALSE);
381 gst_pad_set_active (srcpad, FALSE);
383 gst_check_teardown_pad_by_name (rtpbin, "send_rtp_src_0");
384 gst_check_teardown_pad_by_name (rtpbin, "send_rtp_sink_0");
385 gst_check_teardown_element (rtpbin);
391 GST_START_TEST (test_bufferlist_recv)
399 guint64 bytes_received;
400 guint64 packets_received;
402 list = create_buffer_list ();
403 fail_unless (list != NULL);
405 rtpbin = gst_check_setup_element ("rtpsession");
408 gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "recv_rtp_sink");
409 fail_if (srcpad == NULL);
412 gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate, "recv_rtp_src");
413 fail_if (sinkpad == NULL);
415 gst_pad_set_chain_list_function (sinkpad,
416 GST_DEBUG_FUNCPTR (sink_chain_list));
418 gst_pad_set_active (srcpad, TRUE);
419 gst_pad_set_active (sinkpad, TRUE);
421 caps = gst_caps_from_string (TEST_CAPS);
422 gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
423 gst_caps_unref (caps);
425 gst_element_set_state (rtpbin, GST_STATE_PLAYING);
427 chain_list_func_called = FALSE;
428 fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
429 fail_if (chain_list_func_called == FALSE);
431 /* make sure that stats about the number of received packets are OK too */
432 /* The source becomes a sender after probation succeeds, pass TRUE here. */
433 get_source_stats (rtpbin, TRUE, &stats);
434 fail_if (stats == NULL);
436 gst_structure_get (stats,
437 "bytes-received", G_TYPE_UINT64, &bytes_received,
438 "packets-received", G_TYPE_UINT64, &packets_received, NULL);
439 fail_unless (packets_received == 2);
440 fail_unless (bytes_received == chain_list_bytes_received);
441 gst_structure_free (stats);
444 gst_pad_set_active (sinkpad, FALSE);
445 gst_pad_set_active (srcpad, FALSE);
447 gst_check_teardown_pad_by_name (rtpbin, "recv_rtp_src");
448 gst_check_teardown_pad_by_name (rtpbin, "recv_rtp_sink");
449 gst_check_teardown_element (rtpbin);
455 bufferlist_suite (void)
457 Suite *s = suite_create ("BufferList");
459 TCase *tc_chain = tcase_create ("general");
461 /* time out after 30s. */
462 tcase_set_timeout (tc_chain, 10);
464 suite_add_tcase (s, tc_chain);
465 tcase_add_test (tc_chain, test_bufferlist);
466 tcase_add_test (tc_chain, test_bufferlist_recv);
471 GST_CHECK_MAIN (bufferlist);