test: rtpbin_buffer_list: port to GStreamer 1.0
[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 GstBufferList *
109 create_buffer_list (void)
110 {
111   GstBufferList *list;
112   GstBuffer *orig_buffer;
113   GstBuffer *buffer;
114   GstBuffer *sub_buffer;
115
116   orig_buffer = create_original_buffer ();
117   fail_if (orig_buffer == NULL);
118
119   list = gst_buffer_list_new ();
120   fail_if (list == NULL);
121
122   /*** First packet. **/
123
124   /* Create buffer with RTP header. */
125   buffer = gst_buffer_new_allocate (NULL, rtp_header_len[0], NULL);
126   gst_buffer_fill (buffer, 0, &rtp_header[0], rtp_header_len[0]);
127   gst_buffer_copy_into (buffer, orig_buffer, GST_BUFFER_COPY_METADATA, 0, -1);
128
129   /* Create the payload buffer and add it to the current buffer. */
130   sub_buffer =
131       gst_buffer_copy_region (orig_buffer, GST_BUFFER_COPY_MEMORY,
132       payload_offset[0], payload_len[0]);
133
134   buffer = gst_buffer_append (buffer, sub_buffer);
135   fail_if (buffer == NULL);
136   gst_buffer_list_add (list, buffer);
137
138   /***  Second packet. ***/
139
140   /* Create buffer with RTP header. */
141   buffer = gst_buffer_new_allocate (NULL, rtp_header_len[1], NULL);
142   gst_buffer_fill (buffer, 0, &rtp_header[1], rtp_header_len[1]);
143   gst_buffer_copy_into (buffer, orig_buffer, GST_BUFFER_COPY_METADATA, 0, -1);
144
145   /* Create the payload buffer and add it to the current buffer. */
146   sub_buffer =
147       gst_buffer_copy_region (orig_buffer, GST_BUFFER_COPY_MEMORY,
148       payload_offset[1], payload_len[1]);
149
150   buffer = gst_buffer_append (buffer, sub_buffer);
151   fail_if (buffer == NULL);
152   gst_buffer_list_add (list, buffer);
153
154   return list;
155 }
156
157 static void
158 check_header (GstBuffer * buffer, guint index)
159 {
160   GstMemory *memory;
161   GstMapInfo info;
162   gboolean ret;
163
164   fail_if (buffer == NULL);
165   fail_unless (index < 2);
166
167   memory = gst_buffer_get_memory (buffer, 0);
168   ret = gst_memory_map (memory, &info, GST_MAP_READ);
169   fail_if (ret == FALSE);
170
171   fail_unless (info.size == rtp_header_len[index]);
172
173   /* Can't do a memcmp() on the whole header, cause the SSRC (bytes 8-11) will
174    * most likely be changed in gstrtpbin.
175    */
176   fail_unless (info.data != NULL);
177   fail_unless_equals_uint64 (*(guint64 *) info.data,
178       *(guint64 *) rtp_header[index]);
179   fail_unless (*(guint16 *) (info.data + 12) ==
180       *(guint16 *) (rtp_header[index] + 12));
181
182   gst_memory_unmap (memory, &info);
183   gst_memory_unref (memory);
184 }
185
186 static void
187 check_payload (GstBuffer * buffer, guint index)
188 {
189   GstMemory *memory;
190   GstMapInfo info;
191   gboolean ret;
192
193   fail_if (buffer == NULL);
194   fail_unless (index < 2);
195
196   memory = gst_buffer_get_memory (buffer, 1);
197   ret = gst_memory_map (memory, &info, GST_MAP_READ);
198   fail_if (ret == FALSE);
199
200   fail_unless (info.size == payload_len[index]);
201   fail_if (info.data != (gpointer) (payload + payload_offset[index]));
202   fail_if (memcmp (info.data, payload + payload_offset[index],
203           payload_len[index]));
204
205   gst_memory_unmap (memory, &info);
206   gst_memory_unref (memory);
207 }
208
209 static void
210 check_packet (GstBufferList * list, guint index)
211 {
212   GstBuffer *buffer;
213
214   fail_unless (list != NULL);
215
216   fail_unless ((buffer = gst_buffer_list_get (list, index)) != NULL);
217   fail_unless (gst_buffer_n_memory (buffer) == 2);
218
219   fail_unless (GST_BUFFER_TIMESTAMP (buffer) ==
220       GST_BUFFER_TIMESTAMP (original_buffer));
221
222   check_header (buffer, index);
223   check_payload (buffer, index);
224 }
225
226 static GstFlowReturn
227 sink_chain_list (GstPad * pad, GstObject * parent, GstBufferList * list)
228 {
229   GstCaps *current_caps;
230   GstCaps *caps;
231
232   current_caps = gst_pad_get_current_caps (pad);
233   fail_unless (current_caps != NULL);
234
235   caps = gst_caps_from_string (TEST_CAPS);
236   fail_unless (caps != NULL);
237
238   fail_unless (gst_caps_is_equal (caps, current_caps));
239   gst_caps_unref (caps);
240   gst_caps_unref (current_caps);
241
242   fail_unless (GST_IS_BUFFER_LIST (list));
243   fail_unless (gst_buffer_list_length (list) == 2);
244
245   fail_unless (gst_buffer_list_get (list, 0));
246   check_packet (list, 0);
247
248   fail_unless (gst_buffer_list_get (list, 1));
249   check_packet (list, 1);
250
251   gst_buffer_list_unref (list);
252
253   return GST_FLOW_OK;
254 }
255
256 static void
257 set_chain_function (GstPad * pad)
258 {
259   gst_pad_set_chain_list_function (pad, GST_DEBUG_FUNCPTR (sink_chain_list));
260 }
261
262
263 GST_START_TEST (test_bufferlist)
264 {
265   GstElement *rtpbin;
266   GstPad *srcpad;
267   GstPad *sinkpad;
268   GstCaps *caps;
269   GstBufferList *list;
270
271   list = create_buffer_list ();
272   fail_unless (list != NULL);
273
274   rtpbin = gst_check_setup_element ("rtpbin");
275
276   srcpad =
277       gst_check_setup_src_pad_by_name (rtpbin, &srctemplate, "send_rtp_sink_0");
278   fail_if (srcpad == NULL);
279   sinkpad =
280       gst_check_setup_sink_pad_by_name (rtpbin, &sinktemplate,
281       "send_rtp_src_0");
282   fail_if (sinkpad == NULL);
283
284   set_chain_function (sinkpad);
285
286   gst_pad_set_active (srcpad, TRUE);
287   gst_pad_set_active (sinkpad, TRUE);
288
289   caps = gst_caps_from_string (TEST_CAPS);
290   gst_check_setup_events (srcpad, rtpbin, caps, GST_FORMAT_TIME);
291   gst_caps_unref (caps);
292
293   gst_element_set_state (rtpbin, GST_STATE_PLAYING);
294   fail_unless (gst_pad_push_list (srcpad, list) == GST_FLOW_OK);
295
296   gst_pad_set_active (sinkpad, FALSE);
297   gst_pad_set_active (srcpad, FALSE);
298
299   gst_check_teardown_pad_by_name (rtpbin, "send_rtp_src_0");
300   gst_check_teardown_pad_by_name (rtpbin, "send_rtp_sink_0");
301   gst_check_teardown_element (rtpbin);
302 }
303
304 GST_END_TEST;
305
306 static Suite *
307 bufferlist_suite (void)
308 {
309   Suite *s = suite_create ("BufferList");
310
311   TCase *tc_chain = tcase_create ("general");
312
313   /* time out after 30s. */
314   tcase_set_timeout (tc_chain, 10);
315
316   suite_add_tcase (s, tc_chain);
317   tcase_add_test (tc_chain, test_bufferlist);
318
319   return s;
320 }
321
322 GST_CHECK_MAIN (bufferlist);