test: rtpbin_buffer_list: add function to check sequence number
[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 void
135 check_seqnum (GstBuffer * buffer, guint16 seqnum)
136 {
137   GstMemory *memory;
138   GstMapInfo info;
139   gboolean ret;
140   guint16 current_seqnum;
141
142   fail_if (buffer == NULL);
143
144   memory = gst_buffer_get_memory (buffer, 0);
145   ret = gst_memory_map (memory, &info, GST_MAP_READ);
146   fail_if (ret == FALSE);
147
148   current_seqnum = info.data[2] << 8 | info.data[3];
149   fail_unless (current_seqnum == seqnum);
150
151   gst_memory_unmap (memory, &info);
152   gst_memory_unref (memory);
153 }
154
155 static void
156 check_header (GstBuffer * buffer, guint index)
157 {
158   GstMemory *memory;
159   GstMapInfo info;
160   gboolean ret;
161
162   fail_if (buffer == NULL);
163   fail_unless (index < 2);
164
165   memory = gst_buffer_get_memory (buffer, 0);
166   ret = gst_memory_map (memory, &info, GST_MAP_READ);
167   fail_if (ret == FALSE);
168
169   fail_unless (info.size == rtp_header_len[index]);
170
171   /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
172    * most likely be changed in gstrtpbin.
173    */
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));
179
180   gst_memory_unmap (memory, &info);
181   gst_memory_unref (memory);
182 }
183
184 static void
185 check_payload (GstBuffer * buffer, guint index)
186 {
187   GstMemory *memory;
188   GstMapInfo info;
189   gboolean ret;
190
191   fail_if (buffer == NULL);
192   fail_unless (index < 2);
193
194   memory = gst_buffer_get_memory (buffer, 1);
195   ret = gst_memory_map (memory, &info, GST_MAP_READ);
196   fail_if (ret == FALSE);
197
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]));
202
203   gst_memory_unmap (memory, &info);
204   gst_memory_unref (memory);
205 }
206
207 static void
208 check_packet (GstBufferList * list, guint list_index, guint packet_index)
209 {
210   GstBuffer *buffer;
211
212   fail_unless (list != NULL);
213
214   fail_unless ((buffer = gst_buffer_list_get (list, list_index)) != NULL);
215   fail_unless (gst_buffer_n_memory (buffer) == 2);
216
217   fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
218       GST_BUFFER_TIMESTAMP (original_buffer));
219
220   check_header (buffer, packet_index);
221   check_payload (buffer, packet_index);
222 }
223
224 /*
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.
230  */
231 static gboolean chain_list_func_called;
232 static guint chain_list_bytes_received;
233
234 /* Create two packets with different payloads. */
235 static GstBufferList *
236 create_buffer_list (void)
237 {
238   GstBufferList *list;
239   GstBuffer *orig_buffer;
240   GstBuffer *buffer;
241
242   orig_buffer = create_original_buffer ();
243   fail_if (orig_buffer == NULL);
244
245   list = gst_buffer_list_new ();
246   fail_if (list == NULL);
247
248   /*** First packet. **/
249   buffer =
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);
253
254   /***  Second packet. ***/
255   buffer =
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);
259
260   return list;
261 }
262
263 /* Check that the correct packets have been pushed out of the element. */
264 static GstFlowReturn
265 sink_chain_list (GstPad * pad, GstObject * parent, GstBufferList * list)
266 {
267   GstCaps *current_caps;
268   GstCaps *caps;
269
270   chain_list_func_called = TRUE;
271
272   current_caps = gst_pad_get_current_caps (pad);
273   fail_unless (current_caps != NULL);
274
275   caps = gst_caps_from_string (TEST_CAPS);
276   fail_unless (caps != NULL);
277
278   fail_unless (gst_caps_is_equal (caps, current_caps));
279   gst_caps_unref (caps);
280   gst_caps_unref (current_caps);
281
282   fail_unless (GST_IS_BUFFER_LIST (list));
283   fail_unless (gst_buffer_list_length (list) == 2);
284
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;
288
289   fail_unless (gst_buffer_list_get (list, 0));
290   check_packet (list, 0, 0);
291
292   fail_unless (gst_buffer_list_get (list, 1));
293   check_packet (list, 1, 1);
294
295   gst_buffer_list_unref (list);
296
297   return GST_FLOW_OK;
298 }
299
300 /* Get the stats of the **first** source of the given type (get_sender) */
301 static void
302 get_source_stats (GstElement * rtpsession,
303     gboolean get_sender, GstStructure ** source_stats)
304 {
305   GstStructure *stats;
306   GValueArray *stats_arr;
307   guint i;
308
309   g_object_get (rtpsession, "stats", &stats, NULL);
310   stats_arr =
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);
314
315   *source_stats = NULL;
316   for (i = 0; i < stats_arr->n_values; i++) {
317     GstStructure *tmp_source_stats;
318     gboolean is_sender;
319
320     tmp_source_stats = g_value_dup_boxed (&stats_arr->values[i]);
321     gst_structure_get (tmp_source_stats, "is-sender", G_TYPE_BOOLEAN,
322         &is_sender, NULL);
323
324     /* Return the stats of the **first** source of the given type. */
325     if (is_sender == get_sender) {
326       *source_stats = tmp_source_stats;
327       break;
328     }
329     gst_structure_free (tmp_source_stats);
330   }
331
332   gst_structure_free (stats);
333 }
334
335 /* Get the source stats given a session and a source type (get_sender) */
336 static void
337 get_session_source_stats (GstElement * rtpbin, guint session,
338     gboolean get_sender, GstStructure ** source_stats)
339 {
340   GstElement *rtpsession;
341
342   g_signal_emit_by_name (rtpbin, "get-session", session, &rtpsession);
343   fail_if (rtpsession == NULL);
344
345   get_source_stats (rtpsession, get_sender, source_stats);
346
347   gst_object_unref (rtpsession);
348 }
349
350 GST_START_TEST (test_bufferlist)
351 {
352   GstElement *rtpbin;
353   GstPad *srcpad;
354   GstPad *sinkpad;
355   GstCaps *caps;
356   GstBufferList *list;
357   GstStructure *stats;
358   guint64 packets_sent;
359   guint64 packets_received;
360
361   list = create_buffer_list ();
362   fail_unless (list != NULL);
363
364   rtpbin = gst_check_setup_element ("rtpbin");
365
366   srcpad =
367       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
368   fail_if (srcpad == NULL);
369   sinkpad =
370       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
371       "send_rtp_src_0");
372   fail_if (sinkpad == NULL);
373
374   gst_pad_set_chain_list_function (sinkpad,
375       GST_DEBUG_FUNCPTR (sink_chain_list));
376
377   gst_pad_set_active (srcpad, TRUE);
378   gst_pad_set_active (sinkpad, TRUE);
379
380   caps = gst_caps_from_string (TEST_CAPS);
381   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
382   gst_caps_unref (caps);
383
384   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
385
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);
389
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);
393
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);
400
401   gst_pad_set_active (sinkpad, FALSE);
402   gst_pad_set_active (srcpad, FALSE);
403
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);
407 }
408
409 GST_END_TEST;
410
411
412 GST_START_TEST (test_bufferlist_recv)
413 {
414   GstElement *rtpbin;
415   GstPad *srcpad;
416   GstPad *sinkpad;
417   GstCaps *caps;
418   GstBufferList *list;
419   GstStructure *stats;
420   guint64 bytes_received;
421   guint64 packets_received;
422
423   list = create_buffer_list ();
424   fail_unless (list != NULL);
425
426   rtpbin = gst_check_setup_element ("rtpsession");
427
428   srcpad =
429       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "recv_rtp_sink");
430   fail_if (srcpad == NULL);
431
432   sinkpad =
433       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate, "recv_rtp_src");
434   fail_if (sinkpad == NULL);
435
436   gst_pad_set_chain_list_function (sinkpad,
437       GST_DEBUG_FUNCPTR (sink_chain_list));
438
439   gst_pad_set_active (srcpad, TRUE);
440   gst_pad_set_active (sinkpad, TRUE);
441
442   caps = gst_caps_from_string (TEST_CAPS);
443   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
444   gst_caps_unref (caps);
445
446   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
447
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);
451
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);
456
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);
463
464
465   gst_pad_set_active (sinkpad, FALSE);
466   gst_pad_set_active (srcpad, FALSE);
467
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);
471 }
472
473 GST_END_TEST;
474
475 static Suite *
476 bufferlist_suite (void)
477 {
478   Suite *s = suite_create ("BufferList");
479
480   TCase *tc_chain = tcase_create ("general");
481
482   /* time out after 30s. */
483   tcase_set_timeout (tc_chain, 10);
484
485   suite_add_tcase (s, tc_chain);
486   tcase_add_test (tc_chain, test_bufferlist);
487   tcase_add_test (tc_chain, test_bufferlist_recv);
488
489   return s;
490 }
491
492 GST_CHECK_MAIN (bufferlist);