test: rtpbin_buffer_list: add a test for the case of failed probation
[platform/upstream/gstreamer.git] / tests / check / elements / rtpbin_buffer_list.c
1 /* GStreamer
2  *
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.
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <gst/check/gstcheck.h>
24
25 #include <gst/rtp/gstrtpbuffer.h>
26
27 /* UDP/IP is assumed for bandwidth calculation */
28 #define UDP_IP_HEADER_OVERHEAD 28
29
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.
32  */
33
34
35 #define TEST_CAPS \
36   "application/x-rtp, "                \
37   "media=(string)video, "              \
38   "clock-rate=(int)90000, "            \
39   "encoding-name=(string)H264, "       \
40   "profile-level-id=(string)4d4015, "  \
41   "payload=(int)96, "                  \
42   "ssrc=(guint)2633237432, "           \
43   "clock-base=(guint)1868267015, "     \
44   "seqnum-base=(guint)54229"
45
46
47 /* RTP headers and the first 2 bytes of the payload (FU indicator and FU header)
48  */
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}
54 };
55
56 static const guint rtp_header_len[] = {
57   sizeof rtp_header[0],
58   sizeof rtp_header[1]
59 };
60
61 /* Some payload.
62  */
63 static const char *payload =
64     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
65     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
66     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
67     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
68     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
69     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
70     "0123456789ABSDEF0123456";
71
72 static const guint payload_offset[] = {
73   0, 498
74 };
75
76 static const guint payload_len[] = {
77   498, 5
78 };
79
80
81 static GstBuffer *original_buffer = NULL;
82
83 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
84     GST_PAD_SINK,
85     GST_PAD_ALWAYS,
86     GST_STATIC_CAPS ("application/x-rtp"));
87
88 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
89     GST_PAD_SRC,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS ("application/x-rtp"));
92
93
94 static GstBuffer *
95 create_original_buffer (void)
96 {
97   if (original_buffer != NULL)
98     return original_buffer;
99
100   original_buffer =
101       gst_buffer_new_wrapped ((guint8 *) payload, strlen (payload));
102   fail_unless (original_buffer != NULL);
103
104   GST_BUFFER_TIMESTAMP (original_buffer) =
105       gst_clock_get_internal_time (gst_system_clock_obtain ());
106
107   return original_buffer;
108 }
109
110 static GstBuffer *
111 create_rtp_packet_buffer (gconstpointer header, gint header_size,
112     GstBuffer * payload_buffer, gint payload_offset, gint payload_size)
113 {
114   GstBuffer *buffer;
115   GstBuffer *sub_buffer;
116
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,
121       -1);
122
123   /* Create the payload buffer and add it to the current buffer. */
124   sub_buffer =
125       gst_buffer_copy_region (payload_buffer, GST_BUFFER_COPY_MEMORY,
126       payload_offset, payload_size);
127
128   buffer = gst_buffer_append (buffer, sub_buffer);
129   fail_if (buffer == NULL);
130
131   return buffer;
132 }
133
134 static GstBuffer *
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)
138 {
139   GstBuffer *buffer;
140   GstMemory *memory;
141   GstMapInfo info;
142   gboolean ret;
143   guint32 *tmp;
144
145   buffer =
146       create_rtp_packet_buffer (header, header_size, payload_buffer,
147       payload_offset, payload_size);
148   fail_if (buffer == NULL);
149
150   memory = gst_buffer_get_memory (buffer, 0);
151   ret = gst_memory_map (memory, &info, GST_MAP_READ);
152   fail_if (ret == FALSE);
153
154   info.data[2] = (seqnum >> 8) & 0xff;
155   info.data[3] = seqnum & 0xff;
156
157   tmp = (guint32 *) & (info.data[4]);
158   *tmp = g_htonl (timestamp);
159
160   gst_memory_unmap (memory, &info);
161   gst_memory_unref (memory);
162
163   return buffer;
164 }
165
166 static void
167 check_seqnum (GstBuffer * buffer, guint16 seqnum)
168 {
169   GstMemory *memory;
170   GstMapInfo info;
171   gboolean ret;
172   guint16 current_seqnum;
173
174   fail_if (buffer == NULL);
175
176   memory = gst_buffer_get_memory (buffer, 0);
177   ret = gst_memory_map (memory, &info, GST_MAP_READ);
178   fail_if (ret == FALSE);
179
180   current_seqnum = info.data[2] << 8 | info.data[3];
181   fail_unless (current_seqnum == seqnum);
182
183   gst_memory_unmap (memory, &info);
184   gst_memory_unref (memory);
185 }
186
187 static void
188 check_header (GstBuffer * buffer, guint index)
189 {
190   GstMemory *memory;
191   GstMapInfo info;
192   gboolean ret;
193
194   fail_if (buffer == NULL);
195   fail_unless (index < 2);
196
197   memory = gst_buffer_get_memory (buffer, 0);
198   ret = gst_memory_map (memory, &info, GST_MAP_READ);
199   fail_if (ret == FALSE);
200
201   fail_unless (info.size == rtp_header_len[index]);
202
203   /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
204    * most likely be changed in gstrtpbin.
205    */
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));
211
212   gst_memory_unmap (memory, &info);
213   gst_memory_unref (memory);
214 }
215
216 static void
217 check_payload (GstBuffer * buffer, guint index)
218 {
219   GstMemory *memory;
220   GstMapInfo info;
221   gboolean ret;
222
223   fail_if (buffer == NULL);
224   fail_unless (index < 2);
225
226   memory = gst_buffer_get_memory (buffer, 1);
227   ret = gst_memory_map (memory, &info, GST_MAP_READ);
228   fail_if (ret == FALSE);
229
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]));
234
235   gst_memory_unmap (memory, &info);
236   gst_memory_unref (memory);
237 }
238
239 static void
240 check_packet (GstBufferList * list, guint list_index, guint packet_index)
241 {
242   GstBuffer *buffer;
243
244   fail_unless (list != NULL);
245
246   fail_unless ((buffer = gst_buffer_list_get (list, list_index)) != NULL);
247   fail_unless (gst_buffer_n_memory (buffer) == 2);
248
249   fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
250       GST_BUFFER_TIMESTAMP (original_buffer));
251
252   check_header (buffer, packet_index);
253   check_payload (buffer, packet_index);
254 }
255
256 /*
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.
262  */
263 static gboolean chain_list_func_called;
264 static guint chain_list_bytes_received;
265
266 /* Create two packets with different payloads. */
267 static GstBufferList *
268 create_buffer_list (void)
269 {
270   GstBufferList *list;
271   GstBuffer *orig_buffer;
272   GstBuffer *buffer;
273
274   orig_buffer = create_original_buffer ();
275   fail_if (orig_buffer == NULL);
276
277   list = gst_buffer_list_new ();
278   fail_if (list == NULL);
279
280   /*** First packet. **/
281   buffer =
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);
285
286   /***  Second packet. ***/
287   buffer =
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);
291
292   return list;
293 }
294
295 /* Check that the correct packets have been pushed out of the element. */
296 static GstFlowReturn
297 sink_chain_list (GstPad * pad, GstObject * parent, GstBufferList * list)
298 {
299   GstCaps *current_caps;
300   GstCaps *caps;
301
302   chain_list_func_called = TRUE;
303
304   current_caps = gst_pad_get_current_caps (pad);
305   fail_unless (current_caps != NULL);
306
307   caps = gst_caps_from_string (TEST_CAPS);
308   fail_unless (caps != NULL);
309
310   fail_unless (gst_caps_is_equal (caps, current_caps));
311   gst_caps_unref (caps);
312   gst_caps_unref (current_caps);
313
314   fail_unless (GST_IS_BUFFER_LIST (list));
315   fail_unless (gst_buffer_list_length (list) == 2);
316
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;
320
321   fail_unless (gst_buffer_list_get (list, 0));
322   check_packet (list, 0, 0);
323
324   fail_unless (gst_buffer_list_get (list, 1));
325   check_packet (list, 1, 1);
326
327   gst_buffer_list_unref (list);
328
329   return GST_FLOW_OK;
330 }
331
332
333 /* Non-consecutive seqnums makes probation fail. */
334 static GstBufferList *
335 create_buffer_list_fail_probation (void)
336 {
337   GstBufferList *list;
338   GstBuffer *orig_buffer;
339   GstBuffer *buffer;
340
341   guint16 seqnums[] = { 1, 3, 5 };
342   guint i;
343
344   orig_buffer = create_original_buffer ();
345   fail_if (orig_buffer == NULL);
346
347   list = gst_buffer_list_new ();
348   fail_if (list == NULL);
349
350   for (i = 0; i < sizeof (seqnums) / sizeof (seqnums[0]); i++) {
351     buffer =
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);
355   }
356
357   return list;
358 }
359
360 /* When probation fails this function shouldn't be called. */
361 static GstFlowReturn
362 sink_chain_list_probation_failed (GstPad * pad, GstObject * parent,
363     GstBufferList * list)
364 {
365   chain_list_func_called = TRUE;
366   return GST_FLOW_OK;
367 }
368
369
370 /* Get the stats of the **first** source of the given type (get_sender) */
371 static void
372 get_source_stats (GstElement * rtpsession,
373     gboolean get_sender, GstStructure ** source_stats)
374 {
375   GstStructure *stats;
376   GValueArray *stats_arr;
377   guint i;
378
379   g_object_get (rtpsession, "stats", &stats, NULL);
380   stats_arr =
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);
384
385   *source_stats = NULL;
386   for (i = 0; i < stats_arr->n_values; i++) {
387     GstStructure *tmp_source_stats;
388     gboolean is_sender;
389
390     tmp_source_stats = g_value_dup_boxed (&stats_arr->values[i]);
391     gst_structure_get (tmp_source_stats, "is-sender", G_TYPE_BOOLEAN,
392         &is_sender, NULL);
393
394     /* Return the stats of the **first** source of the given type. */
395     if (is_sender == get_sender) {
396       *source_stats = tmp_source_stats;
397       break;
398     }
399     gst_structure_free (tmp_source_stats);
400   }
401
402   gst_structure_free (stats);
403 }
404
405 /* Get the source stats given a session and a source type (get_sender) */
406 static void
407 get_session_source_stats (GstElement * rtpbin, guint session,
408     gboolean get_sender, GstStructure ** source_stats)
409 {
410   GstElement *rtpsession;
411
412   g_signal_emit_by_name (rtpbin, "get-session", session, &rtpsession);
413   fail_if (rtpsession == NULL);
414
415   get_source_stats (rtpsession, get_sender, source_stats);
416
417   gst_object_unref (rtpsession);
418 }
419
420 GST_START_TEST (test_bufferlist)
421 {
422   GstElement *rtpbin;
423   GstPad *srcpad;
424   GstPad *sinkpad;
425   GstCaps *caps;
426   GstBufferList *list;
427   GstStructure *stats;
428   guint64 packets_sent;
429   guint64 packets_received;
430
431   list = create_buffer_list ();
432   fail_unless (list != NULL);
433
434   rtpbin = gst_check_setup_element ("rtpbin");
435
436   srcpad =
437       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
438   fail_if (srcpad == NULL);
439   sinkpad =
440       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
441       "send_rtp_src_0");
442   fail_if (sinkpad == NULL);
443
444   gst_pad_set_chain_list_function (sinkpad,
445       GST_DEBUG_FUNCPTR (sink_chain_list));
446
447   gst_pad_set_active (srcpad, TRUE);
448   gst_pad_set_active (sinkpad, TRUE);
449
450   caps = gst_caps_from_string (TEST_CAPS);
451   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
452   gst_caps_unref (caps);
453
454   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
455
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);
459
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);
463
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);
470
471   gst_pad_set_active (sinkpad, FALSE);
472   gst_pad_set_active (srcpad, FALSE);
473
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);
477 }
478
479 GST_END_TEST;
480
481
482 GST_START_TEST (test_bufferlist_recv)
483 {
484   GstElement *rtpbin;
485   GstPad *srcpad;
486   GstPad *sinkpad;
487   GstCaps *caps;
488   GstBufferList *list;
489   GstStructure *stats;
490   guint64 bytes_received;
491   guint64 packets_received;
492
493   list = create_buffer_list ();
494   fail_unless (list != NULL);
495
496   rtpbin = gst_check_setup_element ("rtpsession");
497
498   srcpad =
499       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "recv_rtp_sink");
500   fail_if (srcpad == NULL);
501
502   sinkpad =
503       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate, "recv_rtp_src");
504   fail_if (sinkpad == NULL);
505
506   gst_pad_set_chain_list_function (sinkpad,
507       GST_DEBUG_FUNCPTR (sink_chain_list));
508
509   gst_pad_set_active (srcpad, TRUE);
510   gst_pad_set_active (sinkpad, TRUE);
511
512   caps = gst_caps_from_string (TEST_CAPS);
513   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
514   gst_caps_unref (caps);
515
516   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
517
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);
521
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);
526
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);
533
534
535   gst_pad_set_active (sinkpad, FALSE);
536   gst_pad_set_active (srcpad, FALSE);
537
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);
541 }
542
543 GST_END_TEST;
544
545
546 GST_START_TEST (test_bufferlist_recv_probation_failed)
547 {
548   GstElement *rtpbin;
549   GstPad *srcpad;
550   GstPad *sinkpad;
551   GstCaps *caps;
552   GstBufferList *list;
553
554   list = create_buffer_list_fail_probation ();
555   fail_unless (list != NULL);
556
557   rtpbin = gst_check_setup_element ("rtpsession");
558
559   srcpad =
560       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "recv_rtp_sink");
561   fail_if (srcpad == NULL);
562
563   sinkpad =
564       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate, "recv_rtp_src");
565   fail_if (sinkpad == NULL);
566
567   gst_pad_set_chain_list_function (sinkpad,
568       GST_DEBUG_FUNCPTR (sink_chain_list_probation_failed));
569
570   gst_pad_set_active (srcpad, TRUE);
571   gst_pad_set_active (sinkpad, TRUE);
572
573   caps = gst_caps_from_string (TEST_CAPS);
574   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
575   gst_caps_unref (caps);
576
577   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
578
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);
584
585   gst_pad_set_active (sinkpad, FALSE);
586   gst_pad_set_active (srcpad, FALSE);
587
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);
591 }
592
593 GST_END_TEST;
594
595
596 static Suite *
597 bufferlist_suite (void)
598 {
599   Suite *s = suite_create ("BufferList");
600
601   TCase *tc_chain = tcase_create ("general");
602
603   /* time out after 30s. */
604   tcase_set_timeout (tc_chain, 10);
605
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);
610
611   return s;
612 }
613
614 GST_CHECK_MAIN (bufferlist);