• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * unit test for rtpfunnel
4  *
5  * Copyright (C) <2017> Pexip.
6  *   Contact: Havard Graff <havard@pexip.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 #include <gst/check/gstcheck.h>
24 #include <gst/check/gstharness.h>
25 #include <gst/rtp/gstrtpbuffer.h>
26 
GST_START_TEST(rtpfunnel_ssrc_demuxing)27 GST_START_TEST (rtpfunnel_ssrc_demuxing)
28 {
29   GstHarness *h = gst_harness_new_with_padnames ("rtpfunnel", NULL, "src");
30   GstHarness *h0 = gst_harness_new_with_element (h->element, "sink_0", NULL);
31   GstHarness *h1 = gst_harness_new_with_element (h->element, "sink_1", NULL);
32 
33   gst_harness_set_src_caps_str (h0, "application/x-rtp, ssrc=(uint)123");
34   gst_harness_set_src_caps_str (h1, "application/x-rtp, ssrc=(uint)321");
35 
36   /* unref latency events */
37   gst_event_unref (gst_harness_pull_upstream_event (h0));
38   gst_event_unref (gst_harness_pull_upstream_event (h1));
39   fail_unless_equals_int (1, gst_harness_upstream_events_received (h0));
40   fail_unless_equals_int (1, gst_harness_upstream_events_received (h1));
41 
42   /* send to pad 0 */
43   gst_harness_push_upstream_event (h,
44       gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
45           gst_structure_new ("GstForceKeyUnit",
46               "ssrc", G_TYPE_UINT, 123, NULL)));
47   fail_unless_equals_int (2, gst_harness_upstream_events_received (h0));
48   fail_unless_equals_int (1, gst_harness_upstream_events_received (h1));
49 
50   /* send to pad 1 */
51   gst_harness_push_upstream_event (h,
52       gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
53           gst_structure_new ("GstForceKeyUnit",
54               "ssrc", G_TYPE_UINT, 321, NULL)));
55   fail_unless_equals_int (2, gst_harness_upstream_events_received (h0));
56   fail_unless_equals_int (2, gst_harness_upstream_events_received (h1));
57 
58   /* unknown ssrc, we drop it */
59   gst_harness_push_upstream_event (h,
60       gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
61           gst_structure_new ("GstForceKeyUnit",
62               "ssrc", G_TYPE_UINT, 666, NULL)));
63   fail_unless_equals_int (2, gst_harness_upstream_events_received (h0));
64   fail_unless_equals_int (2, gst_harness_upstream_events_received (h1));
65 
66   /* no ssrc, we send to all */
67   gst_harness_push_upstream_event (h,
68       gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
69           gst_structure_new_empty ("GstForceKeyUnit")));
70   fail_unless_equals_int (3, gst_harness_upstream_events_received (h0));
71   fail_unless_equals_int (3, gst_harness_upstream_events_received (h1));
72 
73   /* remove pad 0, and send an event referencing the now dead ssrc */
74   gst_harness_teardown (h0);
75   gst_harness_push_upstream_event (h,
76       gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM,
77           gst_structure_new ("GstForceKeyUnit",
78               "ssrc", G_TYPE_UINT, 123, NULL)));
79   fail_unless_equals_int (3, gst_harness_upstream_events_received (h1));
80 
81   gst_harness_teardown (h);
82   gst_harness_teardown (h1);
83 }
84 
85 GST_END_TEST;
86 
GST_START_TEST(rtpfunnel_ssrc_downstream_not_leaking_through)87 GST_START_TEST (rtpfunnel_ssrc_downstream_not_leaking_through)
88 {
89   GstHarness *h = gst_harness_new_with_padnames ("rtpfunnel",
90       "sink_0", "src");
91   GstCaps *caps;
92   const GstStructure *s;
93 
94   gst_harness_set_sink_caps_str (h, "application/x-rtp, ssrc=(uint)123");
95 
96   caps = gst_pad_peer_query_caps (h->srcpad, NULL);
97   s = gst_caps_get_structure (caps, 0);
98 
99   fail_unless (!gst_structure_has_field (s, "ssrc"));
100 
101   gst_caps_unref (caps);
102   gst_harness_teardown (h);
103 }
104 
105 GST_END_TEST;
106 
GST_START_TEST(rtpfunnel_common_ts_offset)107 GST_START_TEST (rtpfunnel_common_ts_offset)
108 {
109   GstHarness *h = gst_harness_new_with_padnames ("rtpfunnel",
110       "sink_0", "src");
111   GstCaps *caps;
112   const GstStructure *s;
113   const guint expected_ts_offset = 12345;
114   guint ts_offset;
115 
116   g_object_set (h->element, "common-ts-offset", expected_ts_offset, NULL);
117 
118   caps = gst_pad_peer_query_caps (h->srcpad, NULL);
119   s = gst_caps_get_structure (caps, 0);
120 
121   fail_unless (gst_structure_get_uint (s, "timestamp-offset", &ts_offset));
122   fail_unless_equals_int (expected_ts_offset, ts_offset);
123 
124   gst_caps_unref (caps);
125   gst_harness_teardown (h);
126 }
127 
128 GST_END_TEST;
129 
130 static GstBuffer *
generate_test_buffer(guint seqnum,guint ssrc,guint8 twcc_ext_id)131 generate_test_buffer (guint seqnum, guint ssrc, guint8 twcc_ext_id)
132 {
133   GstBuffer *buf;
134   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
135 
136   buf = gst_rtp_buffer_new_allocate (0, 0, 0);
137   GST_BUFFER_PTS (buf) = seqnum * 20 * GST_MSECOND;
138   GST_BUFFER_DTS (buf) = GST_BUFFER_PTS (buf);
139 
140   gst_rtp_buffer_map (buf, GST_MAP_READWRITE, &rtp);
141   gst_rtp_buffer_set_payload_type (&rtp, 100);
142   gst_rtp_buffer_set_seq (&rtp, seqnum);
143   gst_rtp_buffer_set_timestamp (&rtp, seqnum * 160);
144   gst_rtp_buffer_set_ssrc (&rtp, ssrc);
145 
146   if (twcc_ext_id > 0) {
147     guint16 data;
148     GST_WRITE_UINT16_BE (&data, seqnum);
149     gst_rtp_buffer_add_extension_onebyte_header (&rtp, twcc_ext_id,
150         &data, sizeof (guint16));
151   }
152 
153   gst_rtp_buffer_unmap (&rtp);
154 
155   return buf;
156 }
157 
GST_START_TEST(rtpfunnel_custom_sticky)158 GST_START_TEST (rtpfunnel_custom_sticky)
159 {
160   GstHarness *h, *h0, *h1;
161   GstEvent *event;
162   const GstStructure *s;
163   const gchar *value = NULL;
164 
165   h = gst_harness_new_with_padnames ("rtpfunnel", NULL, "src");
166 
167   /* request a sinkpad, with some caps */
168   h0 = gst_harness_new_with_element (h->element, "sink_0", NULL);
169   gst_harness_set_src_caps_str (h0, "application/x-rtp, " "ssrc=(uint)123");
170 
171   /* request a second sinkpad, also with caps */
172   h1 = gst_harness_new_with_element (h->element, "sink_1", NULL);
173   gst_harness_set_src_caps_str (h1, "application/x-rtp, " "ssrc=(uint)456");
174 
175   while ((event = gst_harness_try_pull_event (h)))
176     gst_event_unref (event);
177 
178   fail_unless (gst_harness_push_event (h0,
179           gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY,
180               gst_structure_new ("test", "key", G_TYPE_STRING, "value0",
181                   NULL))));
182 
183   fail_unless (gst_harness_push_event (h1,
184           gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY,
185               gst_structure_new ("test", "key", G_TYPE_STRING, "value1",
186                   NULL))));
187 
188   /* Send a buffer through first pad, expect the event to be the first one */
189   fail_unless_equals_int (GST_FLOW_OK,
190       gst_harness_push (h0, generate_test_buffer (500, 123, 0)));
191   for (;;) {
192     event = gst_harness_pull_event (h);
193     if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_STICKY)
194       break;
195     gst_event_unref (event);
196   }
197   s = gst_event_get_structure (event);
198   fail_unless (s);
199   fail_unless (gst_structure_has_name (s, "test"));
200   value = gst_structure_get_string (s, "key");
201   fail_unless_equals_string (value, "value0");
202   gst_event_unref (event);
203   gst_buffer_unref (gst_harness_pull (h));
204 
205   /* Send a buffer through second pad, expect the event to be the second one
206    */
207   fail_unless_equals_int (GST_FLOW_OK,
208       gst_harness_push (h1, generate_test_buffer (500, 123, 0)));
209   for (;;) {
210     event = gst_harness_pull_event (h);
211     if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_STICKY)
212       break;
213     gst_event_unref (event);
214   }
215   s = gst_event_get_structure (event);
216   fail_unless (s);
217   fail_unless (gst_structure_has_name (s, "test"));
218   value = gst_structure_get_string (s, "key");
219   fail_unless_equals_string (value, "value1");
220   gst_event_unref (event);
221   gst_buffer_unref (gst_harness_pull (h));
222 
223   /* Send a buffer through first pad, expect the event to again be the first
224    * one
225    */
226   fail_unless_equals_int (GST_FLOW_OK,
227       gst_harness_push (h0, generate_test_buffer (500, 123, 5)));
228   for (;;) {
229     event = gst_harness_pull_event (h);
230     if (GST_EVENT_TYPE (event) == GST_EVENT_CUSTOM_DOWNSTREAM_STICKY)
231       break;
232     gst_event_unref (event);
233   }
234   s = gst_event_get_structure (event);
235   fail_unless (s);
236   fail_unless (gst_structure_has_name (s, "test"));
237   value = gst_structure_get_string (s, "key");
238   fail_unless_equals_string (value, "value0");
239   gst_event_unref (event);
240   gst_buffer_unref (gst_harness_pull (h));
241 
242   gst_harness_teardown (h);
243   gst_harness_teardown (h0);
244   gst_harness_teardown (h1);
245 }
246 
247 GST_END_TEST;
248 
GST_START_TEST(rtpfunnel_stress)249 GST_START_TEST (rtpfunnel_stress)
250 {
251   GstHarness *h = gst_harness_new_with_padnames ("rtpfunnel",
252       "sink_0", "src");
253   GstHarness *h1 = gst_harness_new_with_element (h->element, "sink_1", NULL);
254 
255   GstPadTemplate *templ =
256       gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS (h->element),
257       "sink_%u");
258   GstCaps *caps = gst_caps_from_string ("application/x-rtp, ssrc=(uint)123");
259   GstBuffer *buf = gst_buffer_new_allocate (NULL, 0, NULL);
260   GstSegment segment;
261   GstHarnessThread *statechange, *push, *req, *push1;
262 
263   gst_check_add_log_filter ("GStreamer", G_LOG_LEVEL_WARNING,
264       g_regex_new ("Got data flow before (stream-start|segment) event",
265           (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL),
266       NULL, NULL, NULL);
267   gst_check_add_log_filter ("GStreamer", G_LOG_LEVEL_WARNING,
268       g_regex_new ("Sticky event misordering",
269           (GRegexCompileFlags) 0, (GRegexMatchFlags) 0, NULL),
270       NULL, NULL, NULL);
271 
272 
273   gst_segment_init (&segment, GST_FORMAT_TIME);
274 
275   statechange = gst_harness_stress_statechange_start (h);
276   push = gst_harness_stress_push_buffer_start (h, caps, &segment, buf);
277   req = gst_harness_stress_requestpad_start (h, templ, NULL, NULL, TRUE);
278   push1 = gst_harness_stress_push_buffer_start (h1, caps, &segment, buf);
279 
280   gst_caps_unref (caps);
281   gst_buffer_unref (buf);
282 
283   /* test-length */
284   g_usleep (G_USEC_PER_SEC * 1);
285 
286   gst_harness_stress_thread_stop (push1);
287   gst_harness_stress_thread_stop (req);
288   gst_harness_stress_thread_stop (push);
289   gst_harness_stress_thread_stop (statechange);
290 
291   gst_harness_teardown (h1);
292   gst_harness_teardown (h);
293 
294   gst_check_clear_log_filter ();
295 }
296 
297 GST_END_TEST;
298 
299 #define TWCC_EXTMAP_STR "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
300 
301 #define BOGUS_EXTMAP_STR "http://www.ietf.org/id/bogus"
302 
GST_START_TEST(rtpfunnel_twcc_caps)303 GST_START_TEST (rtpfunnel_twcc_caps)
304 {
305   GstHarness *h, *h0, *h1;
306   GstCaps *caps, *expected_caps;
307 
308   h = gst_harness_new_with_padnames ("rtpfunnel", NULL, "src");
309 
310   /* request a sinkpad, set caps with twcc extmap */
311   h0 = gst_harness_new_with_element (h->element, "sink_0", NULL);
312   gst_harness_set_src_caps_str (h0, "application/x-rtp, "
313       "ssrc=(uint)123, extmap-5=" TWCC_EXTMAP_STR "");
314 
315   /* request a second sinkpad, the extmap should not be
316      present in the caps when doing a caps-query downstream,
317      as we don't want to force upstream (typically a payloader)
318      to use the extension */
319   h1 = gst_harness_new_with_element (h->element, "sink_1", NULL);
320   caps = gst_pad_query_caps (GST_PAD_PEER (h1->srcpad), NULL);
321   expected_caps = gst_caps_new_empty_simple ("application/x-rtp");
322   fail_unless (gst_caps_is_equal (expected_caps, caps));
323   gst_caps_unref (caps);
324   gst_caps_unref (expected_caps);
325 
326   /* now try and set a different extmap for the same id on the other
327    * sinkpad, and verify this does not work */
328   gst_harness_set_src_caps_str (h1, "application/x-rtp, "
329       "ssrc=(uint)456, extmap-5=" BOGUS_EXTMAP_STR "");
330   caps = gst_pad_get_current_caps (GST_PAD_PEER (h1->srcpad));
331   fail_unless (caps == NULL);
332 
333   /* ...but setting the right extmap (5) will work just fine */
334   expected_caps = gst_caps_from_string ("application/x-rtp, "
335       "ssrc=(uint)456, extmap-5=" TWCC_EXTMAP_STR "");
336   gst_harness_set_src_caps (h1, gst_caps_ref (expected_caps));
337   caps = gst_pad_get_current_caps (GST_PAD_PEER (h1->srcpad));
338   fail_unless (gst_caps_is_equal (expected_caps, caps));
339   gst_caps_unref (caps);
340   gst_caps_unref (expected_caps);
341 
342   gst_harness_teardown (h);
343   gst_harness_teardown (h0);
344   gst_harness_teardown (h1);
345 }
346 
347 GST_END_TEST;
348 
349 static gint32
get_twcc_seqnum(GstBuffer * buf,guint8 ext_id)350 get_twcc_seqnum (GstBuffer * buf, guint8 ext_id)
351 {
352   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
353   gint32 val = -1;
354   gpointer data;
355 
356   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
357   if (gst_rtp_buffer_get_extension_onebyte_header (&rtp, ext_id,
358           0, &data, NULL)) {
359     val = GST_READ_UINT16_BE (data);
360   }
361   gst_rtp_buffer_unmap (&rtp);
362 
363   return val;
364 }
365 
366 static guint32
get_ssrc(GstBuffer * buf)367 get_ssrc (GstBuffer * buf)
368 {
369   guint32 ssrc;
370   GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
371   gst_rtp_buffer_map (buf, GST_MAP_READ, &rtp);
372   ssrc = gst_rtp_buffer_get_ssrc (&rtp);
373   gst_rtp_buffer_unmap (&rtp);
374   return ssrc;
375 }
376 
GST_START_TEST(rtpfunnel_twcc_passthrough)377 GST_START_TEST (rtpfunnel_twcc_passthrough)
378 {
379   GstHarness *h, *h0;
380   GstBuffer *buf;
381   guint16 offset = 65530;
382   guint packets = 40;
383   guint i;
384 
385   h = gst_harness_new_with_padnames ("rtpfunnel", NULL, "src");
386   h0 = gst_harness_new_with_element (h->element, "sink_0", NULL);
387   gst_harness_set_src_caps_str (h0, "application/x-rtp, "
388       "ssrc=(uint)123, extmap-5=" TWCC_EXTMAP_STR "");
389 
390   /* push some packets with twcc seqnum */
391   for (i = 0; i < packets; i++) {
392     guint16 seqnum = i + offset;
393     fail_unless_equals_int (GST_FLOW_OK,
394         gst_harness_push (h0, generate_test_buffer (seqnum, 123, 5)));
395   }
396 
397   /* and verify the seqnums stays unchanged through the funnel */
398   for (i = 0; i < packets; i++) {
399     guint16 seqnum = i + offset;
400     buf = gst_harness_pull (h);
401     fail_unless_equals_int (seqnum, get_twcc_seqnum (buf, 5));
402     gst_buffer_unref (buf);
403   }
404 
405   gst_harness_teardown (h);
406   gst_harness_teardown (h0);
407 }
408 
409 GST_END_TEST;
410 
GST_START_TEST(rtpfunnel_twcc_mux)411 GST_START_TEST (rtpfunnel_twcc_mux)
412 {
413   GstHarness *h, *h0, *h1;
414   GstBuffer *buf;
415 
416   h = gst_harness_new_with_padnames ("rtpfunnel", NULL, "src");
417   h0 = gst_harness_new_with_element (h->element, "sink_0", NULL);
418   h1 = gst_harness_new_with_element (h->element, "sink_1", NULL);
419   gst_harness_set_src_caps_str (h0, "application/x-rtp, "
420       "ssrc=(uint)123, extmap-5=" TWCC_EXTMAP_STR "");
421   gst_harness_set_src_caps_str (h1, "application/x-rtp, "
422       "ssrc=(uint)456, extmap-5=" TWCC_EXTMAP_STR "");
423 
424   /* push buffers on both pads with different twcc-seqnums on them (500 and 60000) */
425   fail_unless_equals_int (GST_FLOW_OK,
426       gst_harness_push (h0, generate_test_buffer (500, 123, 5)));
427   fail_unless_equals_int (GST_FLOW_OK,
428       gst_harness_push (h1, generate_test_buffer (60000, 321, 5)));
429 
430   /* verify they are muxed continuously (0 -> 1) */
431   buf = gst_harness_pull (h);
432   fail_unless_equals_int (123, get_ssrc (buf));
433   fail_unless_equals_int (0, get_twcc_seqnum (buf, 5));
434   gst_buffer_unref (buf);
435 
436   buf = gst_harness_pull (h);
437   fail_unless_equals_int (321, get_ssrc (buf));
438   fail_unless_equals_int (1, get_twcc_seqnum (buf, 5));
439   gst_buffer_unref (buf);
440 
441   gst_harness_teardown (h);
442   gst_harness_teardown (h0);
443   gst_harness_teardown (h1);
444 }
445 
446 GST_END_TEST;
447 
448 
GST_START_TEST(rtpfunnel_twcc_passthrough_then_mux)449 GST_START_TEST (rtpfunnel_twcc_passthrough_then_mux)
450 {
451   GstHarness *h, *h0, *h1;
452   GstBuffer *buf;
453   guint offset0 = 500;
454   guint offset1 = 45678;
455   guint i;
456 
457   h = gst_harness_new_with_padnames ("rtpfunnel", NULL, "src");
458   h0 = gst_harness_new_with_element (h->element, "sink_0", NULL);
459   gst_harness_set_src_caps_str (h0, "application/x-rtp, "
460       "ssrc=(uint)123, extmap-5=" TWCC_EXTMAP_STR "");
461 
462   /* push one packet with twcc seqnum 100 on pad0 */
463   fail_unless_equals_int (GST_FLOW_OK,
464       gst_harness_push (h0, generate_test_buffer (offset0, 123, 5)));
465 
466   /* add pad1 to the funnel, also with twcc */
467   h1 = gst_harness_new_with_element (h->element, "sink_1", NULL);
468   gst_harness_set_src_caps_str (h1, "application/x-rtp, "
469       "ssrc=(uint)456, extmap-5=" TWCC_EXTMAP_STR "");
470 
471   /* push one buffer on both pads, with pad1 starting at a different
472      offset */
473   fail_unless_equals_int (GST_FLOW_OK,
474       gst_harness_push (h0, generate_test_buffer (offset0 + 1, 123, 5)));
475   fail_unless_equals_int (GST_FLOW_OK,
476       gst_harness_push (h1, generate_test_buffer (offset1, 321, 5)));
477 
478   /* and verify the seqnums are continuous for all 3 packets, using
479      the inital offset from pad0 */
480   for (i = 0; i < 3; i++) {
481     guint16 seqnum = i + offset0;
482     buf = gst_harness_pull (h);
483     fail_unless_equals_int (seqnum, get_twcc_seqnum (buf, 5));
484     gst_buffer_unref (buf);
485   }
486 
487   gst_harness_teardown (h);
488   gst_harness_teardown (h0);
489   gst_harness_teardown (h1);
490 }
491 
492 GST_END_TEST;
493 
494 static Suite *
rtpfunnel_suite(void)495 rtpfunnel_suite (void)
496 {
497   Suite *s = suite_create ("rtpfunnel");
498   TCase *tc_chain = tcase_create ("general");
499 
500   suite_add_tcase (s, tc_chain);
501 
502   tcase_add_test (tc_chain, rtpfunnel_ssrc_demuxing);
503   tcase_add_test (tc_chain, rtpfunnel_ssrc_downstream_not_leaking_through);
504   tcase_add_test (tc_chain, rtpfunnel_common_ts_offset);
505   tcase_add_test (tc_chain, rtpfunnel_custom_sticky);
506 
507   tcase_add_test (tc_chain, rtpfunnel_stress);
508 
509   tcase_add_test (tc_chain, rtpfunnel_twcc_caps);
510   tcase_add_test (tc_chain, rtpfunnel_twcc_passthrough);
511   tcase_add_test (tc_chain, rtpfunnel_twcc_mux);
512   tcase_add_test (tc_chain, rtpfunnel_twcc_passthrough_then_mux);
513 
514   return s;
515 }
516 
517 GST_CHECK_MAIN (rtpfunnel)
518