test: rtpbin_buffer_list: add test to verify that receiving stats are correct
[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_header (GstBuffer * buffer, guint index)
136 {
137   GstMemory *memory;
138   GstMapInfo info;
139   gboolean ret;
140
141   fail_if (buffer == NULL);
142   fail_unless (index < 2);
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   fail_unless (info.size == rtp_header_len[index]);
149
150   /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
151    * most likely be changed in gstrtpbin.
152    */
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));
158
159   gst_memory_unmap (memory, &info);
160   gst_memory_unref (memory);
161 }
162
163 static void
164 check_payload (GstBuffer * buffer, guint index)
165 {
166   GstMemory *memory;
167   GstMapInfo info;
168   gboolean ret;
169
170   fail_if (buffer == NULL);
171   fail_unless (index < 2);
172
173   memory = gst_buffer_get_memory (buffer, 1);
174   ret = gst_memory_map (memory, &info, GST_MAP_READ);
175   fail_if (ret == FALSE);
176
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]));
181
182   gst_memory_unmap (memory, &info);
183   gst_memory_unref (memory);
184 }
185
186 static void
187 check_packet (GstBufferList * list, guint list_index, guint packet_index)
188 {
189   GstBuffer *buffer;
190
191   fail_unless (list != NULL);
192
193   fail_unless ((buffer = gst_buffer_list_get (list, list_index)) != NULL);
194   fail_unless (gst_buffer_n_memory (buffer) == 2);
195
196   fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
197       GST_BUFFER_TIMESTAMP (original_buffer));
198
199   check_header (buffer, packet_index);
200   check_payload (buffer, packet_index);
201 }
202
203 /*
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.
209  */
210 static gboolean chain_list_func_called;
211 static guint chain_list_bytes_received;
212
213 /* Create two packets with different payloads. */
214 static GstBufferList *
215 create_buffer_list (void)
216 {
217   GstBufferList *list;
218   GstBuffer *orig_buffer;
219   GstBuffer *buffer;
220
221   orig_buffer = create_original_buffer ();
222   fail_if (orig_buffer == NULL);
223
224   list = gst_buffer_list_new ();
225   fail_if (list == NULL);
226
227   /*** First packet. **/
228   buffer =
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);
232
233   /***  Second packet. ***/
234   buffer =
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);
238
239   return list;
240 }
241
242 /* Check that the correct packets have been pushed out of the element. */
243 static GstFlowReturn
244 sink_chain_list (GstPad * pad, GstObject * parent, GstBufferList * list)
245 {
246   GstCaps *current_caps;
247   GstCaps *caps;
248
249   chain_list_func_called = TRUE;
250
251   current_caps = gst_pad_get_current_caps (pad);
252   fail_unless (current_caps != NULL);
253
254   caps = gst_caps_from_string (TEST_CAPS);
255   fail_unless (caps != NULL);
256
257   fail_unless (gst_caps_is_equal (caps, current_caps));
258   gst_caps_unref (caps);
259   gst_caps_unref (current_caps);
260
261   fail_unless (GST_IS_BUFFER_LIST (list));
262   fail_unless (gst_buffer_list_length (list) == 2);
263
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;
267
268   fail_unless (gst_buffer_list_get (list, 0));
269   check_packet (list, 0, 0);
270
271   fail_unless (gst_buffer_list_get (list, 1));
272   check_packet (list, 1, 1);
273
274   gst_buffer_list_unref (list);
275
276   return GST_FLOW_OK;
277 }
278
279 /* Get the stats of the **first** source of the given type (get_sender) */
280 static void
281 get_source_stats (GstElement * rtpsession,
282     gboolean get_sender, GstStructure ** source_stats)
283 {
284   GstStructure *stats;
285   GValueArray *stats_arr;
286   guint i;
287
288   g_object_get (rtpsession, "stats", &stats, NULL);
289   stats_arr =
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);
293
294   *source_stats = NULL;
295   for (i = 0; i < stats_arr->n_values; i++) {
296     GstStructure *tmp_source_stats;
297     gboolean is_sender;
298
299     tmp_source_stats = g_value_dup_boxed (&stats_arr->values[i]);
300     gst_structure_get (tmp_source_stats, "is-sender", G_TYPE_BOOLEAN,
301         &is_sender, NULL);
302
303     /* Return the stats of the **first** source of the given type. */
304     if (is_sender == get_sender) {
305       *source_stats = tmp_source_stats;
306       break;
307     }
308     gst_structure_free (tmp_source_stats);
309   }
310
311   gst_structure_free (stats);
312 }
313
314 /* Get the source stats given a session and a source type (get_sender) */
315 static void
316 get_session_source_stats (GstElement * rtpbin, guint session,
317     gboolean get_sender, GstStructure ** source_stats)
318 {
319   GstElement *rtpsession;
320
321   g_signal_emit_by_name (rtpbin, "get-session", session, &rtpsession);
322   fail_if (rtpsession == NULL);
323
324   get_source_stats (rtpsession, get_sender, source_stats);
325
326   gst_object_unref (rtpsession);
327 }
328
329 GST_START_TEST (test_bufferlist)
330 {
331   GstElement *rtpbin;
332   GstPad *srcpad;
333   GstPad *sinkpad;
334   GstCaps *caps;
335   GstBufferList *list;
336   GstStructure *stats;
337   guint64 packets_sent;
338   guint64 packets_received;
339
340   list = create_buffer_list ();
341   fail_unless (list != NULL);
342
343   rtpbin = gst_check_setup_element ("rtpbin");
344
345   srcpad =
346       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
347   fail_if (srcpad == NULL);
348   sinkpad =
349       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
350       "send_rtp_src_0");
351   fail_if (sinkpad == NULL);
352
353   gst_pad_set_chain_list_function (sinkpad,
354       GST_DEBUG_FUNCPTR (sink_chain_list));
355
356   gst_pad_set_active (srcpad, TRUE);
357   gst_pad_set_active (sinkpad, TRUE);
358
359   caps = gst_caps_from_string (TEST_CAPS);
360   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
361   gst_caps_unref (caps);
362
363   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
364
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);
368
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);
372
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);
379
380   gst_pad_set_active (sinkpad, FALSE);
381   gst_pad_set_active (srcpad, FALSE);
382
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);
386 }
387
388 GST_END_TEST;
389
390
391 GST_START_TEST (test_bufferlist_recv)
392 {
393   GstElement *rtpbin;
394   GstPad *srcpad;
395   GstPad *sinkpad;
396   GstCaps *caps;
397   GstBufferList *list;
398   GstStructure *stats;
399   guint64 bytes_received;
400   guint64 packets_received;
401
402   list = create_buffer_list ();
403   fail_unless (list != NULL);
404
405   rtpbin = gst_check_setup_element ("rtpsession");
406
407   srcpad =
408       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "recv_rtp_sink");
409   fail_if (srcpad == NULL);
410
411   sinkpad =
412       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate, "recv_rtp_src");
413   fail_if (sinkpad == NULL);
414
415   gst_pad_set_chain_list_function (sinkpad,
416       GST_DEBUG_FUNCPTR (sink_chain_list));
417
418   gst_pad_set_active (srcpad, TRUE);
419   gst_pad_set_active (sinkpad, TRUE);
420
421   caps = gst_caps_from_string (TEST_CAPS);
422   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
423   gst_caps_unref (caps);
424
425   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
426
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);
430
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);
435
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);
442
443
444   gst_pad_set_active (sinkpad, FALSE);
445   gst_pad_set_active (srcpad, FALSE);
446
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);
450 }
451
452 GST_END_TEST;
453
454 static Suite *
455 bufferlist_suite (void)
456 {
457   Suite *s = suite_create ("BufferList");
458
459   TCase *tc_chain = tcase_create ("general");
460
461   /* time out after 30s. */
462   tcase_set_timeout (tc_chain, 10);
463
464   suite_add_tcase (s, tc_chain);
465   tcase_add_test (tc_chain, test_bufferlist);
466   tcase_add_test (tc_chain, test_bufferlist_recv);
467
468   return s;
469 }
470
471 GST_CHECK_MAIN (bufferlist);