test: rtpbin_buffer_list: make check_packet more flexible
[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 GstBufferList *
133 create_buffer_list (void)
134 {
135   GstBufferList *list;
136   GstBuffer *orig_buffer;
137   GstBuffer *buffer;
138
139   orig_buffer = create_original_buffer ();
140   fail_if (orig_buffer == NULL);
141
142   list = gst_buffer_list_new ();
143   fail_if (list == NULL);
144
145   /*** First packet. **/
146   buffer =
147       create_rtp_packet_buffer (&rtp_header[0], rtp_header_len[0], orig_buffer,
148       payload_offset[0], payload_len[0]);
149   gst_buffer_list_add (list, buffer);
150
151   /***  Second packet. ***/
152   buffer =
153       create_rtp_packet_buffer (&rtp_header[1], rtp_header_len[1], orig_buffer,
154       payload_offset[1], payload_len[1]);
155   gst_buffer_list_add (list, buffer);
156
157   return list;
158 }
159
160 static void
161 check_header (GstBuffer * buffer, guint index)
162 {
163   GstMemory *memory;
164   GstMapInfo info;
165   gboolean ret;
166
167   fail_if (buffer == NULL);
168   fail_unless (index < 2);
169
170   memory = gst_buffer_get_memory (buffer, 0);
171   ret = gst_memory_map (memory, &info, GST_MAP_READ);
172   fail_if (ret == FALSE);
173
174   fail_unless (info.size == rtp_header_len[index]);
175
176   /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
177    * most likely be changed in gstrtpbin.
178    */
179   fail_unless (info.data != NULL);
180   fail_unless_equals_uint64 (*(guint64 *) info.data,
181       *(guint64 *) rtp_header[index]);
182   fail_unless (*(guint16 *) (info.data + 12) ==
183       *(guint16 *) (rtp_header[index] + 12));
184
185   gst_memory_unmap (memory, &info);
186   gst_memory_unref (memory);
187 }
188
189 static void
190 check_payload (GstBuffer * buffer, guint index)
191 {
192   GstMemory *memory;
193   GstMapInfo info;
194   gboolean ret;
195
196   fail_if (buffer == NULL);
197   fail_unless (index < 2);
198
199   memory = gst_buffer_get_memory (buffer, 1);
200   ret = gst_memory_map (memory, &info, GST_MAP_READ);
201   fail_if (ret == FALSE);
202
203   fail_unless (info.size == payload_len[index]);
204   fail_if (info.data != (gpointer) (payload + payload_offset[index]));
205   fail_if (memcmp (info.data, payload + payload_offset[index],
206           payload_len[index]));
207
208   gst_memory_unmap (memory, &info);
209   gst_memory_unref (memory);
210 }
211
212 static void
213 check_packet (GstBufferList * list, guint list_index, guint packet_index)
214 {
215   GstBuffer *buffer;
216
217   fail_unless (list != NULL);
218
219   fail_unless ((buffer = gst_buffer_list_get (list, list_index)) != NULL);
220   fail_unless (gst_buffer_n_memory (buffer) == 2);
221
222   fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
223       GST_BUFFER_TIMESTAMP (original_buffer));
224
225   check_header (buffer, packet_index);
226   check_payload (buffer, packet_index);
227 }
228
229 /*
230  * Used to verify that the chain_list function is actually implemented by the
231  * element and called when executing the pipeline. This is needed because pads
232  * always have a default chain_list handler which handle buffers in a buffer
233  * list individually, and pushing a list to a pad can succeed even if no
234  * chain_list handler has been set.
235  */
236 static gboolean chain_list_func_called;
237
238 static GstFlowReturn
239 sink_chain_list (GstPad * pad, GstObject * parent, GstBufferList * list)
240 {
241   GstCaps *current_caps;
242   GstCaps *caps;
243
244   chain_list_func_called = TRUE;
245
246   current_caps = gst_pad_get_current_caps (pad);
247   fail_unless (current_caps != NULL);
248
249   caps = gst_caps_from_string (TEST_CAPS);
250   fail_unless (caps != NULL);
251
252   fail_unless (gst_caps_is_equal (caps, current_caps));
253   gst_caps_unref (caps);
254   gst_caps_unref (current_caps);
255
256   fail_unless (GST_IS_BUFFER_LIST (list));
257   fail_unless (gst_buffer_list_length (list) == 2);
258
259   fail_unless (gst_buffer_list_get (list, 0));
260   check_packet (list, 0, 0);
261
262   fail_unless (gst_buffer_list_get (list, 1));
263   check_packet (list, 1, 1);
264
265   gst_buffer_list_unref (list);
266
267   return GST_FLOW_OK;
268 }
269
270 static void
271 set_chain_function (GstPad * pad)
272 {
273   gst_pad_set_chain_list_function (pad, GST_DEBUG_FUNCPTR (sink_chain_list));
274 }
275
276
277 GST_START_TEST (test_bufferlist)
278 {
279   GstElement *rtpbin;
280   GstPad *srcpad;
281   GstPad *sinkpad;
282   GstCaps *caps;
283   GstBufferList *list;
284
285   list = create_buffer_list ();
286   fail_unless (list != NULL);
287
288   rtpbin = gst_check_setup_element ("rtpbin");
289
290   srcpad =
291       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
292   fail_if (srcpad == NULL);
293   sinkpad =
294       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
295       "send_rtp_src_0");
296   fail_if (sinkpad == NULL);
297
298   set_chain_function (sinkpad);
299
300   gst_pad_set_active (srcpad, TRUE);
301   gst_pad_set_active (sinkpad, TRUE);
302
303   caps = gst_caps_from_string (TEST_CAPS);
304   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
305   gst_caps_unref (caps);
306
307   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
308
309   chain_list_func_called = FALSE;
310   fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
311   fail_if (chain_list_func_called == FALSE);
312
313   gst_pad_set_active (sinkpad, FALSE);
314   gst_pad_set_active (srcpad, FALSE);
315
316   gst_check_teardown_pad_by_name (rtpbin, "send_rtp_src_0");
317   gst_check_teardown_pad_by_name (rtpbin, "send_rtp_sink_0");
318   gst_check_teardown_element (rtpbin);
319 }
320
321 GST_END_TEST;
322
323 static Suite *
324 bufferlist_suite (void)
325 {
326   Suite *s = suite_create ("BufferList");
327
328   TCase *tc_chain = tcase_create ("general");
329
330   /* time out after 30s. */
331   tcase_set_timeout (tc_chain, 10);
332
333   suite_add_tcase (s, tc_chain);
334   tcase_add_test (tc_chain, test_bufferlist);
335
336   return s;
337 }
338
339 GST_CHECK_MAIN (bufferlist);