1 /* GStreamer
2 *
3 * Copyright (C) 2013 Collabora Ltd.
4 * @author Julien Isorce <julien.isorce@collabora.co.uk>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #include <gst/check/gstcheck.h>
23 #include <gst/net/gstnetaddressmeta.h>
24 #include <gst/rtp/gstrtpbuffer.h>
25 #include <gst/rtp/gstrtcpbuffer.h>
26
27 static GMainLoop *main_loop;
28 static GstPad *srcpad;
29
30 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
31 GST_PAD_SINK,
32 GST_PAD_ALWAYS,
33 GST_STATIC_CAPS ("application/x-rtcp")
34 );
35
36 static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
37 GST_PAD_SRC,
38 GST_PAD_ALWAYS,
39 GST_STATIC_CAPS ("application/x-rtcp")
40 );
41
42 static void
message_received(GstBus * bus,GstMessage * message,GstPipeline * bin)43 message_received (GstBus * bus, GstMessage * message, GstPipeline * bin)
44 {
45 GST_INFO ("bus message from \"%" GST_PTR_FORMAT "\": %" GST_PTR_FORMAT,
46 GST_MESSAGE_SRC (message), message);
47
48 switch (message->type) {
49 case GST_MESSAGE_EOS:
50 g_main_loop_quit (main_loop);
51 break;
52 case GST_MESSAGE_WARNING:{
53 GError *gerror;
54 gchar *debug;
55
56 gst_message_parse_warning (message, &gerror, &debug);
57 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
58 g_error_free (gerror);
59 g_free (debug);
60 break;
61 }
62 case GST_MESSAGE_ERROR:{
63 GError *gerror;
64 gchar *debug;
65
66 gst_message_parse_error (message, &gerror, &debug);
67 gst_object_default_error (GST_MESSAGE_SRC (message), gerror, debug);
68 g_error_free (gerror);
69 g_free (debug);
70 g_main_loop_quit (main_loop);
71 break;
72 }
73 default:
74 break;
75 }
76 }
77
78 static GstBuffer *
create_rtcp_app(guint32 ssrc,guint count)79 create_rtcp_app (guint32 ssrc, guint count)
80 {
81 GInetAddress *inet_addr_0;
82 guint16 port = 5678 + count;
83 GSocketAddress *socket_addr_0;
84 GstBuffer *rtcp_buffer;
85 GstRTCPPacket *rtcp_packet = NULL;
86 GstRTCPBuffer rtcp = GST_RTCP_BUFFER_INIT;
87
88 inet_addr_0 = g_inet_address_new_from_string ("192.168.1.1");
89 socket_addr_0 = g_inet_socket_address_new (inet_addr_0, port);
90 g_object_unref (inet_addr_0);
91
92 rtcp_buffer = gst_rtcp_buffer_new (1400);
93 gst_buffer_add_net_address_meta (rtcp_buffer, socket_addr_0);
94 g_object_unref (socket_addr_0);
95
96 /* need to begin with rr */
97 gst_rtcp_buffer_map (rtcp_buffer, GST_MAP_READWRITE, &rtcp);
98 rtcp_packet = g_slice_new0 (GstRTCPPacket);
99 gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_RR, rtcp_packet);
100 gst_rtcp_packet_rr_set_ssrc (rtcp_packet, ssrc);
101 g_slice_free (GstRTCPPacket, rtcp_packet);
102
103 /* useful to make the rtcp buffer valid */
104 rtcp_packet = g_slice_new0 (GstRTCPPacket);
105 gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_APP, rtcp_packet);
106 g_slice_free (GstRTCPPacket, rtcp_packet);
107 gst_rtcp_buffer_unmap (&rtcp);
108
109 return rtcp_buffer;
110 }
111
112 static guint nb_ssrc_changes;
113 static guint ssrc_prev;
114
115 static GstPadProbeReturn
rtpsession_sinkpad_probe(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)116 rtpsession_sinkpad_probe (GstPad * pad, GstPadProbeInfo * info,
117 gpointer user_data)
118 {
119 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
120
121 if (info->type == (GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH)) {
122 GstBuffer *buffer = GST_BUFFER (info->data);
123 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
124 GstBuffer *rtcp_buffer = 0;
125 guint ssrc = 0;
126
127 /* retrieve current ssrc */
128 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
129 ssrc = gst_rtp_buffer_get_ssrc (&rtp);
130 gst_rtp_buffer_unmap (&rtp);
131
132 /* if not first buffer, check that our ssrc has changed */
133 if (ssrc_prev != -1 && ssrc != ssrc_prev)
134 ++nb_ssrc_changes;
135
136 /* update prev ssrc */
137 ssrc_prev = ssrc;
138
139 /* feint a collision on recv_rtcp_sink pad of gstrtpsession
140 * (note that after being marked as collied the rtpsession ignores
141 * all non bye packets)
142 */
143 rtcp_buffer = create_rtcp_app (ssrc, nb_ssrc_changes);
144
145 /* push collied packet on recv_rtcp_sink */
146 gst_pad_push (srcpad, rtcp_buffer);
147 }
148
149 return ret;
150 }
151
152 static GstFlowReturn
fake_udp_sink_chain_func(GstPad * pad,GstObject * parent,GstBuffer * buffer)153 fake_udp_sink_chain_func (GstPad * pad, GstObject * parent, GstBuffer * buffer)
154 {
155 gst_buffer_unref (buffer);
156 return GST_FLOW_OK;
157 }
158
159 /* This test build the pipeline audiotestsrc ! alawenc ! rtppcmapay ! \
160 * rtpsession ! fakesink
161 * It manually pushs buffer into rtpsession with same ssrc but different
162 * ip so that collision can be detected
163 * The test checks that the payloader change their ssrc
164 */
GST_START_TEST(test_master_ssrc_collision)165 GST_START_TEST (test_master_ssrc_collision)
166 {
167 GstElement *bin, *src, *encoder, *rtppayloader, *rtpsession, *sink;
168 GstBus *bus = NULL;
169 gboolean res = FALSE;
170 GstSegment segment;
171 GstPad *sinkpad = NULL;
172 GstPad *rtcp_sinkpad = NULL;
173 GstPad *fake_udp_sinkpad = NULL;
174 GstPad *rtcp_srcpad = NULL;
175 GstStateChangeReturn state_res = GST_STATE_CHANGE_FAILURE;
176
177 GST_INFO ("preparing test");
178
179 nb_ssrc_changes = 0;
180 ssrc_prev = -1;
181
182 /* build pipeline */
183 bin = gst_pipeline_new ("pipeline");
184 bus = gst_element_get_bus (bin);
185 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
186
187 src = gst_element_factory_make ("audiotestsrc", "src");
188 g_object_set (src, "num-buffers", 5, NULL);
189 encoder = gst_element_factory_make ("alawenc", NULL);
190 rtppayloader = gst_element_factory_make ("rtppcmapay", NULL);
191 g_object_set (rtppayloader, "pt", 8, NULL);
192 rtpsession = gst_element_factory_make ("rtpsession", NULL);
193 sink = gst_element_factory_make ("fakesink", "sink");
194 gst_bin_add_many (GST_BIN (bin), src, encoder, rtppayloader,
195 rtpsession, sink, NULL);
196
197 /* link elements */
198 res = gst_element_link (src, encoder);
199 fail_unless (res == TRUE, NULL);
200 res = gst_element_link (encoder, rtppayloader);
201 fail_unless (res == TRUE, NULL);
202 res = gst_element_link_pads_full (rtppayloader, "src",
203 rtpsession, "send_rtp_sink", GST_PAD_LINK_CHECK_NOTHING);
204 fail_unless (res == TRUE, NULL);
205 res = gst_element_link_pads_full (rtpsession, "send_rtp_src",
206 sink, "sink", GST_PAD_LINK_CHECK_NOTHING);
207 fail_unless (res == TRUE, NULL);
208
209 /* add probe on rtpsession sink pad to induce collision */
210 sinkpad = gst_element_get_static_pad (rtpsession, "send_rtp_sink");
211 gst_pad_add_probe (sinkpad,
212 (GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH),
213 (GstPadProbeCallback) rtpsession_sinkpad_probe, NULL, NULL);
214 gst_object_unref (sinkpad);
215
216 /* setup rtcp link */
217 srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
218 rtcp_sinkpad = gst_element_get_request_pad (rtpsession, "recv_rtcp_sink");
219 fail_unless (gst_pad_link (srcpad, rtcp_sinkpad) == GST_PAD_LINK_OK, NULL);
220 gst_object_unref (rtcp_sinkpad);
221 res = gst_pad_set_active (srcpad, TRUE);
222 fail_if (res == FALSE);
223 res =
224 gst_pad_push_event (srcpad,
225 gst_event_new_stream_start ("my_rtcp_stream_id"));
226 fail_if (res == FALSE);
227 gst_segment_init (&segment, GST_FORMAT_TIME);
228 res = gst_pad_push_event (srcpad, gst_event_new_segment (&segment));
229 fail_if (res == FALSE);
230
231 fake_udp_sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
232 gst_pad_set_chain_function (fake_udp_sinkpad, fake_udp_sink_chain_func);
233 rtcp_srcpad = gst_element_get_request_pad (rtpsession, "send_rtcp_src");
234 fail_unless (gst_pad_link (rtcp_srcpad, fake_udp_sinkpad) == GST_PAD_LINK_OK,
235 NULL);
236 gst_object_unref (rtcp_srcpad);
237 res = gst_pad_set_active (fake_udp_sinkpad, TRUE);
238 fail_if (res == FALSE);
239
240 /* connect messages */
241 main_loop = g_main_loop_new (NULL, FALSE);
242 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
243 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
244 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
245
246 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
247 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
248
249 GST_INFO ("running main loop");
250 g_main_loop_run (main_loop);
251
252 state_res = gst_element_set_state (bin, GST_STATE_NULL);
253 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
254
255 /* cleanup */
256 gst_object_unref (srcpad);
257 gst_object_unref (fake_udp_sinkpad);
258 g_main_loop_unref (main_loop);
259 gst_bus_remove_signal_watch (bus);
260 gst_object_unref (bus);
261 gst_object_unref (bin);
262
263 /* check results */
264 fail_unless_equals_int (nb_ssrc_changes, 4);
265 }
266
267 GST_END_TEST;
268
269 static guint ssrc_before;
270 static guint ssrc_after;
271 static guint rtx_ssrc_before;
272 static guint rtx_ssrc_after;
273
274 static GstPadProbeReturn
rtpsession_sinkpad_probe2(GstPad * pad,GstPadProbeInfo * info,gpointer user_data)275 rtpsession_sinkpad_probe2 (GstPad * pad, GstPadProbeInfo * info,
276 gpointer user_data)
277 {
278 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
279
280 if (info->type == (GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH)) {
281 GstBuffer *buffer = GST_BUFFER (info->data);
282 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
283 guint payload_type = 0;
284
285 static gint i = 0;
286
287 /* retrieve current ssrc for retransmission stream only */
288 gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp);
289 payload_type = gst_rtp_buffer_get_payload_type (&rtp);
290 if (payload_type == 99) {
291 if (i < 3)
292 rtx_ssrc_before = gst_rtp_buffer_get_ssrc (&rtp);
293 else
294 rtx_ssrc_after = gst_rtp_buffer_get_ssrc (&rtp);
295 } else {
296 /* ask to retransmit every packet */
297 GstEvent *event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
298 gst_structure_new ("GstRTPRetransmissionRequest",
299 "seqnum", G_TYPE_UINT, gst_rtp_buffer_get_seq (&rtp),
300 "ssrc", G_TYPE_UINT, gst_rtp_buffer_get_ssrc (&rtp),
301 NULL));
302 gst_pad_push_event (pad, event);
303
304 if (i < 3)
305 ssrc_before = gst_rtp_buffer_get_ssrc (&rtp);
306 else
307 ssrc_after = gst_rtp_buffer_get_ssrc (&rtp);
308 }
309 gst_rtp_buffer_unmap (&rtp);
310
311 /* feint a collision on recv_rtcp_sink pad of gstrtpsession
312 * (note that after being marked as collied the rtpsession ignores
313 * all non bye packets)
314 */
315 if (i == 2) {
316 GstBuffer *rtcp_buffer = create_rtcp_app (rtx_ssrc_before, 0);
317
318 /* push collied packet on recv_rtcp_sink */
319 gst_pad_push (srcpad, rtcp_buffer);
320 }
321
322 ++i;
323 }
324
325 return ret;
326 }
327
328 /* This test build the pipeline audiotestsrc ! alawenc ! rtppcmapay ! \
329 * rtprtxsend ! rtpsession ! fakesink
330 * It manually pushs buffer into rtpsession with same ssrc than rtx stream
331 * but different ip so that collision can be detected
332 * The test checks that the rtx elements changes its ssrc whereas
333 * the payloader keeps its master ssrc
334 */
GST_START_TEST(test_rtx_ssrc_collision)335 GST_START_TEST (test_rtx_ssrc_collision)
336 {
337 GstElement *bin, *src, *encoder, *rtppayloader, *rtprtxsend, *rtpsession,
338 *sink;
339 GstBus *bus = NULL;
340 gboolean res = FALSE;
341 GstSegment segment;
342 GstPad *sinkpad = NULL;
343 GstPad *rtcp_sinkpad = NULL;
344 GstPad *fake_udp_sinkpad = NULL;
345 GstPad *rtcp_srcpad = NULL;
346 GstStateChangeReturn state_res = GST_STATE_CHANGE_FAILURE;
347 GstStructure *pt_map;
348
349 GST_INFO ("preparing test");
350
351 /* build pipeline */
352 bin = gst_pipeline_new ("pipeline");
353 bus = gst_element_get_bus (bin);
354 gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
355
356 src = gst_element_factory_make ("audiotestsrc", "src");
357 g_object_set (src, "num-buffers", 5, NULL);
358 encoder = gst_element_factory_make ("alawenc", NULL);
359 rtppayloader = gst_element_factory_make ("rtppcmapay", NULL);
360 g_object_set (rtppayloader, "pt", 8, NULL);
361 rtprtxsend = gst_element_factory_make ("rtprtxsend", NULL);
362 pt_map = gst_structure_new ("application/x-rtp-pt-map",
363 "8", G_TYPE_UINT, 99, NULL);
364 g_object_set (rtprtxsend, "payload-type-map", pt_map, NULL);
365 gst_structure_free (pt_map);
366 rtpsession = gst_element_factory_make ("rtpsession", NULL);
367 sink = gst_element_factory_make ("fakesink", "sink");
368 gst_bin_add_many (GST_BIN (bin), src, encoder, rtppayloader, rtprtxsend,
369 rtpsession, sink, NULL);
370
371 /* link elements */
372 res = gst_element_link (src, encoder);
373 fail_unless (res == TRUE, NULL);
374 res = gst_element_link (encoder, rtppayloader);
375 fail_unless (res == TRUE, NULL);
376 res = gst_element_link (rtppayloader, rtprtxsend);
377 fail_unless (res == TRUE, NULL);
378 res = gst_element_link_pads_full (rtprtxsend, "src",
379 rtpsession, "send_rtp_sink", GST_PAD_LINK_CHECK_NOTHING);
380 fail_unless (res == TRUE, NULL);
381 res = gst_element_link_pads_full (rtpsession, "send_rtp_src",
382 sink, "sink", GST_PAD_LINK_CHECK_NOTHING);
383 fail_unless (res == TRUE, NULL);
384
385 /* add probe on rtpsession sink pad to induce collision */
386 sinkpad = gst_element_get_static_pad (rtpsession, "send_rtp_sink");
387 gst_pad_add_probe (sinkpad,
388 (GST_PAD_PROBE_TYPE_BUFFER | GST_PAD_PROBE_TYPE_PUSH),
389 (GstPadProbeCallback) rtpsession_sinkpad_probe2, NULL, NULL);
390 gst_object_unref (sinkpad);
391
392 /* setup rtcp link */
393 srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
394 rtcp_sinkpad = gst_element_get_request_pad (rtpsession, "recv_rtcp_sink");
395 fail_unless (gst_pad_link (srcpad, rtcp_sinkpad) == GST_PAD_LINK_OK, NULL);
396 gst_object_unref (rtcp_sinkpad);
397 res = gst_pad_set_active (srcpad, TRUE);
398 fail_if (res == FALSE);
399 res =
400 gst_pad_push_event (srcpad,
401 gst_event_new_stream_start ("my_rtcp_stream_id"));
402 fail_if (res == FALSE);
403 gst_segment_init (&segment, GST_FORMAT_TIME);
404 res = gst_pad_push_event (srcpad, gst_event_new_segment (&segment));
405 fail_if (res == FALSE);
406
407 fake_udp_sinkpad = gst_pad_new_from_static_template (&sinktemplate, "sink");
408 gst_pad_set_chain_function (fake_udp_sinkpad, fake_udp_sink_chain_func);
409 rtcp_srcpad = gst_element_get_request_pad (rtpsession, "send_rtcp_src");
410 fail_unless (gst_pad_link (rtcp_srcpad, fake_udp_sinkpad) == GST_PAD_LINK_OK,
411 NULL);
412 gst_object_unref (rtcp_srcpad);
413 res = gst_pad_set_active (fake_udp_sinkpad, TRUE);
414 fail_if (res == FALSE);
415
416 /* connect messages */
417 main_loop = g_main_loop_new (NULL, FALSE);
418 g_signal_connect (bus, "message::error", (GCallback) message_received, bin);
419 g_signal_connect (bus, "message::warning", (GCallback) message_received, bin);
420 g_signal_connect (bus, "message::eos", (GCallback) message_received, bin);
421
422 state_res = gst_element_set_state (bin, GST_STATE_PLAYING);
423 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
424
425 GST_INFO ("running main loop");
426 g_main_loop_run (main_loop);
427
428 state_res = gst_element_set_state (bin, GST_STATE_NULL);
429 ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
430
431 /* cleanup */
432 gst_object_unref (srcpad);
433 gst_object_unref (fake_udp_sinkpad);
434 g_main_loop_unref (main_loop);
435 gst_bus_remove_signal_watch (bus);
436 gst_object_unref (bus);
437 gst_object_unref (bin);
438
439 /* check results */
440 fail_if (rtx_ssrc_before == rtx_ssrc_after);
441 fail_if (ssrc_before != ssrc_after);
442 }
443
444 GST_END_TEST;
445
446 static Suite *
rtpcollision_suite(void)447 rtpcollision_suite (void)
448 {
449 Suite *s = suite_create ("rtpcollision");
450 TCase *tc_chain = tcase_create ("general");
451
452 tcase_set_timeout (tc_chain, 10);
453
454 suite_add_tcase (s, tc_chain);
455
456 tcase_add_test (tc_chain, test_master_ssrc_collision);
457 tcase_add_test (tc_chain, test_rtx_ssrc_collision);
458
459 return s;
460 }
461
462 GST_CHECK_MAIN (rtpcollision);
463