test: rtpbin_buffer_list: add a test for buffer lists on the recv path
[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
28 /* This test makes sure that RTP packets sent as buffer lists are sent through
29  * the rtpbin as they are supposed to, and not corrupted in any way.
30  */
31
32
33 #define TEST_CAPS \
34   "application/x-rtp, "                \
35   "media=(string)video, "              \
36   "clock-rate=(int)90000, "            \
37   "encoding-name=(string)H264, "       \
38   "profile-level-id=(string)4d4015, "  \
39   "payload=(int)96, "                  \
40   "ssrc=(guint)2633237432, "           \
41   "clock-base=(guint)1868267015, "     \
42   "seqnum-base=(guint)54229"
43
44
45 /* RTP headers and the first 2 bytes of the payload (FU indicator and FU header)
46  */
47 static const guint8 rtp_header[2][14] = {
48   {0x80, 0x60, 0xbb, 0xb7, 0x5c, 0xe9, 0x09,
49       0x0d, 0xf5, 0x9c, 0x43, 0x55, 0x1c, 0x86},
50   {0x80, 0x60, 0xbb, 0xb8, 0x5c, 0xe9, 0x09,
51       0x0d, 0xf5, 0x9c, 0x43, 0x55, 0x1c, 0x46}
52 };
53
54 static const guint rtp_header_len[] = {
55   sizeof rtp_header[0],
56   sizeof rtp_header[1]
57 };
58
59 /* Some payload.
60  */
61 static const char *payload =
62     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
63     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
64     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
65     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
66     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
67     "0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF0123456789ABSDEF"
68     "0123456789ABSDEF0123456";
69
70 static const guint payload_offset[] = {
71   0, 498
72 };
73
74 static const guint payload_len[] = {
75   498, 5
76 };
77
78
79 static GstBuffer *original_buffer = NULL;
80
81 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
82     GST_PAD_SINK,
83     GST_PAD_ALWAYS,
84     GST_STATIC_CAPS ("application/x-rtp"));
85
86 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
87     GST_PAD_SRC,
88     GST_PAD_ALWAYS,
89     GST_STATIC_CAPS ("application/x-rtp"));
90
91
92 static GstBuffer *
93 create_original_buffer (void)
94 {
95   if (original_buffer != NULL)
96     return original_buffer;
97
98   original_buffer =
99       gst_buffer_new_wrapped ((guint8 *) payload, strlen (payload));
100   fail_unless (original_buffer != NULL);
101
102   GST_BUFFER_TIMESTAMP (original_buffer) =
103       gst_clock_get_internal_time (gst_system_clock_obtain ());
104
105   return original_buffer;
106 }
107
108 static GstBuffer *
109 create_rtp_packet_buffer (gconstpointer header, gint header_size,
110     GstBuffer * payload_buffer, gint payload_offset, gint payload_size)
111 {
112   GstBuffer *buffer;
113   GstBuffer *sub_buffer;
114
115   /* Create buffer with RTP header. */
116   buffer = gst_buffer_new_allocate (NULL, header_size, NULL);
117   gst_buffer_fill (buffer, 0, header, header_size);
118   gst_buffer_copy_into (buffer, payload_buffer, GST_BUFFER_COPY_METADATA, 0,
119       -1);
120
121   /* Create the payload buffer and add it to the current buffer. */
122   sub_buffer =
123       gst_buffer_copy_region (payload_buffer, GST_BUFFER_COPY_MEMORY,
124       payload_offset, payload_size);
125
126   buffer = gst_buffer_append (buffer, sub_buffer);
127   fail_if (buffer == NULL);
128
129   return buffer;
130 }
131
132 static void
133 check_header (GstBuffer * buffer, guint index)
134 {
135   GstMemory *memory;
136   GstMapInfo info;
137   gboolean ret;
138
139   fail_if (buffer == NULL);
140   fail_unless (index < 2);
141
142   memory = gst_buffer_get_memory (buffer, 0);
143   ret = gst_memory_map (memory, &info, GST_MAP_READ);
144   fail_if (ret == FALSE);
145
146   fail_unless (info.size == rtp_header_len[index]);
147
148   /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
149    * most likely be changed in gstrtpbin.
150    */
151   fail_unless (info.data != NULL);
152   fail_unless_equals_uint64 (*(guint64 *) info.data,
153       *(guint64 *) rtp_header[index]);
154   fail_unless (*(guint16 *) (info.data + 12) ==
155       *(guint16 *) (rtp_header[index] + 12));
156
157   gst_memory_unmap (memory, &info);
158   gst_memory_unref (memory);
159 }
160
161 static void
162 check_payload (GstBuffer * buffer, guint index)
163 {
164   GstMemory *memory;
165   GstMapInfo info;
166   gboolean ret;
167
168   fail_if (buffer == NULL);
169   fail_unless (index < 2);
170
171   memory = gst_buffer_get_memory (buffer, 1);
172   ret = gst_memory_map (memory, &info, GST_MAP_READ);
173   fail_if (ret == FALSE);
174
175   fail_unless (info.size == payload_len[index]);
176   fail_if (info.data != (gpointer) (payload + payload_offset[index]));
177   fail_if (memcmp (info.data, payload + payload_offset[index],
178           payload_len[index]));
179
180   gst_memory_unmap (memory, &info);
181   gst_memory_unref (memory);
182 }
183
184 static void
185 check_packet (GstBufferList * list, guint list_index, guint packet_index)
186 {
187   GstBuffer *buffer;
188
189   fail_unless (list != NULL);
190
191   fail_unless ((buffer = gst_buffer_list_get (list, list_index)) != NULL);
192   fail_unless (gst_buffer_n_memory (buffer) == 2);
193
194   fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
195       GST_BUFFER_TIMESTAMP (original_buffer));
196
197   check_header (buffer, packet_index);
198   check_payload (buffer, packet_index);
199 }
200
201 /*
202  * Used to verify that the chain_list function is actually implemented by the
203  * element and called when executing the pipeline. This is needed because pads
204  * always have a default chain_list handler which handle buffers in a buffer
205  * list individually, and pushing a list to a pad can succeed even if no
206  * chain_list handler has been set.
207  */
208 static gboolean chain_list_func_called;
209
210 /* Create two packets with different payloads. */
211 static GstBufferList *
212 create_buffer_list (void)
213 {
214   GstBufferList *list;
215   GstBuffer *orig_buffer;
216   GstBuffer *buffer;
217
218   orig_buffer = create_original_buffer ();
219   fail_if (orig_buffer == NULL);
220
221   list = gst_buffer_list_new ();
222   fail_if (list == NULL);
223
224   /*** First packet. **/
225   buffer =
226       create_rtp_packet_buffer (&rtp_header[0], rtp_header_len[0], orig_buffer,
227       payload_offset[0], payload_len[0]);
228   gst_buffer_list_add (list, buffer);
229
230   /***  Second packet. ***/
231   buffer =
232       create_rtp_packet_buffer (&rtp_header[1], rtp_header_len[1], orig_buffer,
233       payload_offset[1], payload_len[1]);
234   gst_buffer_list_add (list, buffer);
235
236   return list;
237 }
238
239 /* Check that the correct packets have been pushed out of the element. */
240 static GstFlowReturn
241 sink_chain_list (GstPad * pad, GstObject * parent, GstBufferList * list)
242 {
243   GstCaps *current_caps;
244   GstCaps *caps;
245
246   chain_list_func_called = TRUE;
247
248   current_caps = gst_pad_get_current_caps (pad);
249   fail_unless (current_caps != NULL);
250
251   caps = gst_caps_from_string (TEST_CAPS);
252   fail_unless (caps != NULL);
253
254   fail_unless (gst_caps_is_equal (caps, current_caps));
255   gst_caps_unref (caps);
256   gst_caps_unref (current_caps);
257
258   fail_unless (GST_IS_BUFFER_LIST (list));
259   fail_unless (gst_buffer_list_length (list) == 2);
260
261   fail_unless (gst_buffer_list_get (list, 0));
262   check_packet (list, 0, 0);
263
264   fail_unless (gst_buffer_list_get (list, 1));
265   check_packet (list, 1, 1);
266
267   gst_buffer_list_unref (list);
268
269   return GST_FLOW_OK;
270 }
271
272 /* Get the stats of the **first** source of the given type (get_sender) */
273 static void
274 get_session_source_stats (GstElement * rtpbin, guint session,
275     gboolean get_sender, GstStructure ** source_stats)
276 {
277   GstElement *rtpsession;
278   GstStructure *stats;
279   GValueArray *stats_arr;
280   guint i;
281
282   g_signal_emit_by_name (rtpbin, "get-session", session, &rtpsession);
283   fail_if (rtpsession == NULL);
284
285   g_object_get (rtpsession, "stats", &stats, NULL);
286   stats_arr =
287       g_value_get_boxed (gst_structure_get_value (stats, "source-stats"));
288   g_assert (stats_arr != NULL);
289   fail_unless (stats_arr->n_values >= 1);
290
291   *source_stats = NULL;
292   for (i = 0; i < stats_arr->n_values; i++) {
293     GstStructure *tmp_source_stats;
294     gboolean is_sender;
295
296     tmp_source_stats = g_value_dup_boxed (&stats_arr->values[i]);
297     gst_structure_get (tmp_source_stats, "is-sender", G_TYPE_BOOLEAN,
298         &is_sender, NULL);
299
300     /* Return the stats of the **first** source of the given type. */
301     if (is_sender == get_sender) {
302       *source_stats = tmp_source_stats;
303       break;
304     }
305     gst_structure_free (tmp_source_stats);
306   }
307
308   gst_structure_free (stats);
309   gst_object_unref (rtpsession);
310 }
311
312 GST_START_TEST (test_bufferlist)
313 {
314   GstElement *rtpbin;
315   GstPad *srcpad;
316   GstPad *sinkpad;
317   GstCaps *caps;
318   GstBufferList *list;
319   GstStructure *stats;
320   guint64 packets_sent;
321   guint64 packets_received;
322
323   list = create_buffer_list ();
324   fail_unless (list != NULL);
325
326   rtpbin = gst_check_setup_element ("rtpbin");
327
328   srcpad =
329       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
330   fail_if (srcpad == NULL);
331   sinkpad =
332       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
333       "send_rtp_src_0");
334   fail_if (sinkpad == NULL);
335
336   gst_pad_set_chain_list_function (sinkpad,
337       GST_DEBUG_FUNCPTR (sink_chain_list));
338
339   gst_pad_set_active (srcpad, TRUE);
340   gst_pad_set_active (sinkpad, TRUE);
341
342   caps = gst_caps_from_string (TEST_CAPS);
343   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
344   gst_caps_unref (caps);
345
346   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
347
348   chain_list_func_called = FALSE;
349   fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
350   fail_if (chain_list_func_called == FALSE);
351
352   /* make sure that stats about the number of sent packets are OK too */
353   get_session_source_stats (rtpbin, 0, TRUE, &stats);
354   fail_if (stats == NULL);
355
356   gst_structure_get (stats,
357       "packets-sent", G_TYPE_UINT64, &packets_sent,
358       "packets-received", G_TYPE_UINT64, &packets_received, NULL);
359   fail_unless (packets_sent == 2);
360   fail_unless (packets_received == 2);
361   gst_structure_free (stats);
362
363   gst_pad_set_active (sinkpad, FALSE);
364   gst_pad_set_active (srcpad, FALSE);
365
366   gst_check_teardown_pad_by_name (rtpbin, "send_rtp_src_0");
367   gst_check_teardown_pad_by_name (rtpbin, "send_rtp_sink_0");
368   gst_check_teardown_element (rtpbin);
369 }
370
371 GST_END_TEST;
372
373
374 GST_START_TEST (test_bufferlist_recv)
375 {
376   GstElement *rtpbin;
377   GstPad *srcpad;
378   GstPad *sinkpad;
379   GstCaps *caps;
380   GstBufferList *list;
381
382   list = create_buffer_list ();
383   fail_unless (list != NULL);
384
385   rtpbin = gst_check_setup_element ("rtpsession");
386
387   srcpad =
388       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "recv_rtp_sink");
389   fail_if (srcpad == NULL);
390
391   sinkpad =
392       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate, "recv_rtp_src");
393   fail_if (sinkpad == NULL);
394
395   gst_pad_set_chain_list_function (sinkpad,
396       GST_DEBUG_FUNCPTR (sink_chain_list));
397
398   gst_pad_set_active (srcpad, TRUE);
399   gst_pad_set_active (sinkpad, TRUE);
400
401   caps = gst_caps_from_string (TEST_CAPS);
402   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
403   gst_caps_unref (caps);
404
405   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
406
407   chain_list_func_called = FALSE;
408   fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
409   fail_if (chain_list_func_called == FALSE);
410
411   gst_pad_set_active (sinkpad, FALSE);
412   gst_pad_set_active (srcpad, FALSE);
413
414   gst_check_teardown_pad_by_name (rtpbin, "recv_rtp_src");
415   gst_check_teardown_pad_by_name (rtpbin, "recv_rtp_sink");
416   gst_check_teardown_element (rtpbin);
417 }
418
419 GST_END_TEST;
420
421 static Suite *
422 bufferlist_suite (void)
423 {
424   Suite *s = suite_create ("BufferList");
425
426   TCase *tc_chain = tcase_create ("general");
427
428   /* time out after 30s. */
429   tcase_set_timeout (tc_chain, 10);
430
431   suite_add_tcase (s, tc_chain);
432   tcase_add_test (tc_chain, test_bufferlist);
433   tcase_add_test (tc_chain, test_bufferlist_recv);
434
435   return s;
436 }
437
438 GST_CHECK_MAIN (bufferlist);