summaryrefslogtreecommitdiff
path: root/tests/check/elements/rtpssrcdemux.c
blob: 831d757b8b7529efcedeb166539a9093b15b2492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* GStreamer
 *
 * Copyright (C) 2018 Collabora Ltd.
 *               Author: Nicolas Dufresne <nicolas.dufresne@collabora.com>
 * Copyright (C) 2019 Pexip
 *               Author: Havard Graff <havard@pexip.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/rtp/gstrtpbuffer.h>
#include <gst/rtp/gstrtcpbuffer.h>

#include <gst/check/gstcheck.h>
#include <gst/check/gstharness.h>

#ifdef HAVE_VALGRIND
# include <valgrind/valgrind.h>
#else
# define RUNNING_ON_VALGRIND 0
#endif

#define TEST_BUF_CLOCK_RATE 8000
#define TEST_BUF_PT 0
#define TEST_BUF_SSRC 0x01BADBAD
#define TEST_BUF_MS  20
#define TEST_BUF_DURATION (TEST_BUF_MS * GST_MSECOND)
#define TEST_BUF_SIZE (64000 * TEST_BUF_MS / 1000)
#define TEST_RTP_TS_DURATION (TEST_BUF_CLOCK_RATE * TEST_BUF_MS / 1000)

static GstCaps *
generate_caps (void)
{
  return gst_caps_new_simple ("application/x-rtp",
      "media", G_TYPE_STRING, "audio",
      "clock-rate", G_TYPE_INT, TEST_BUF_CLOCK_RATE, NULL);
}

static GstBuffer *
create_buffer (guint seq_num, guint32 ssrc)
{
  GstBuffer *buf;
  guint8 *payload;
  guint i;
  GstClockTime dts = seq_num * TEST_BUF_DURATION;
  guint32 rtp_ts = seq_num * TEST_RTP_TS_DURATION;
  GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;

  buf = gst_rtp_buffer_new_allocate (TEST_BUF_SIZE, 0, 0);
  GST_BUFFER_DTS (buf) = dts;

  gst_rtp_buffer_map (buf, GST_MAP_READWRITE, &rtp);
  gst_rtp_buffer_set_payload_type (&rtp, TEST_BUF_PT);
  gst_rtp_buffer_set_seq (&rtp, seq_num);
  gst_rtp_buffer_set_timestamp (&rtp, rtp_ts);
  gst_rtp_buffer_set_ssrc (&rtp, ssrc);

  payload = gst_rtp_buffer_get_payload (&rtp);
  for (i = 0; i < TEST_BUF_SIZE; i++)
    payload[i] = 0xff;

  gst_rtp_buffer_unmap (&rtp);

  return buf;
}


typedef struct
{
  GstHarness *rtp_sink;
  GstHarness *rtcp_sink;
  GstHarness *rtp_src;
  GstHarness *rtcp_src;
} TestContext;

static void
rtpssrcdemux_pad_added (G_GNUC_UNUSED GstElement * demux, GstPad * src_pad,
    TestContext * ctx)
{
  GstHarness *h;

  h = gst_harness_new_with_element (ctx->rtp_sink->element, NULL,
      GST_PAD_NAME (src_pad));

  /* FIXME We should also check that pads have current caps, but this is not
   * currently the case as both pads are created when the first pad receive a
   * buffer. If the other pad is not linked, you'll get a pad without caps.
   * Changing this implies not having both pads on 'on-new-ssrc' which would
   * break rtpbin assumption. */

  if (g_str_has_prefix (GST_PAD_NAME (src_pad), "src_")) {
    g_assert (ctx->rtp_src == NULL);
    ctx->rtp_src = h;
  } else if (g_str_has_prefix (GST_PAD_NAME (src_pad), "rtcp_src_")) {
    g_assert (ctx->rtcp_src == NULL);
    ctx->rtcp_src = h;
  } else {
    g_assert_not_reached ();
  }
}

GST_START_TEST (test_event_forwarding)
{
  TestContext ctx = { NULL, NULL, NULL, NULL };
  GstHarness *h;
  GstEvent *event;
  GstCaps *caps;
  GstStructure *s;
  guint ssrc;

  ctx.rtp_sink = h = gst_harness_new_with_padnames ("rtpssrcdemux", "sink",
      NULL);
  g_signal_connect (h->element, "pad_added",
      G_CALLBACK (rtpssrcdemux_pad_added), &ctx);

  ctx.rtcp_sink = gst_harness_new_with_element (h->element, "rtcp_sink", NULL);

  gst_harness_set_src_caps (h, generate_caps ());
  gst_harness_push (h, create_buffer (0, TEST_BUF_SSRC));

  g_assert (ctx.rtp_src);
  g_assert (ctx.rtcp_src);

  gst_harness_push_event (h, gst_event_new_eos ());

  /* We expect stream-start/caps/segment/eos */
  g_assert_cmpint (gst_harness_events_in_queue (ctx.rtp_src), ==, 4);

  event = gst_harness_pull_event (ctx.rtp_src);
  g_assert_cmpint (event->type, ==, GST_EVENT_STREAM_START);
  gst_event_unref (event);

  event = gst_harness_pull_event (ctx.rtp_src);
  g_assert_cmpint (event->type, ==, GST_EVENT_CAPS);
  gst_event_parse_caps (event, &caps);
  s = gst_caps_get_structure (caps, 0);
  g_assert (gst_structure_has_field (s, "ssrc"));
  g_assert (gst_structure_get_uint (s, "ssrc", &ssrc));
  g_assert_cmpuint (ssrc, ==, TEST_BUF_SSRC);
  gst_event_unref (event);

  event = gst_harness_pull_event (ctx.rtp_src);
  g_assert_cmpint (event->type, ==, GST_EVENT_SEGMENT);
  gst_event_unref (event);

  event = gst_harness_pull_event (ctx.rtp_src);
  g_assert_cmpint (event->type, ==, GST_EVENT_EOS);
  gst_event_unref (event);

  /* We pushed on the RTP pad, no events should have reached the RTCP pad */
  g_assert_cmpint (gst_harness_events_in_queue (ctx.rtcp_src), ==, 0);

  /* push EOS on the rtcp sink pad, to make sure it EOS properly, the harness
   * will create the missing stream-start */
  gst_harness_push_event (ctx.rtcp_sink, gst_event_new_eos ());

  g_assert_cmpint (gst_harness_events_in_queue (ctx.rtp_src), ==, 0);
  g_assert_cmpint (gst_harness_events_in_queue (ctx.rtcp_src), ==, 1);

  event = gst_harness_pull_event (ctx.rtcp_src);
  g_assert_cmpint (event->type, ==, GST_EVENT_EOS);
  gst_event_unref (event);

  gst_harness_teardown (ctx.rtp_src);
  gst_harness_teardown (ctx.rtcp_src);
  gst_harness_teardown (ctx.rtcp_sink);
  gst_harness_teardown (ctx.rtp_sink);
}

GST_END_TEST;

typedef struct
{
  gint ready;
  GMutex mutex;
  GCond cond;
} LockTestContext;

static void
new_ssrc_pad_cb (G_GNUC_UNUSED GstElement * element, G_GNUC_UNUSED guint ssrc,
    G_GNUC_UNUSED GstPad * pad, LockTestContext * ctx)
{
  g_message ("Signalling ready");
  g_atomic_int_set (&ctx->ready, 1);

  g_message ("Waiting no more ready");
  while (g_atomic_int_get (&ctx->ready))
    g_usleep (G_USEC_PER_SEC / 100);

  g_mutex_lock (&ctx->mutex);
  g_mutex_unlock (&ctx->mutex);
}

static gpointer
push_buffer_func (gpointer user_data)
{
  GstHarness *h = user_data;
  gst_harness_push (h, create_buffer (0, 0xdeadbeef));
  return NULL;
}

GST_START_TEST (test_oob_event_locking)
{
  GstHarness *h = gst_harness_new_with_padnames ("rtpssrcdemux", "sink", NULL);
  LockTestContext ctx;
  GThread *thread;

  memset (&ctx, 0, sizeof (LockTestContext));
  g_mutex_init (&ctx.mutex);
  g_cond_init (&ctx.cond);

  gst_harness_set_src_caps_str (h, "application/x-rtp");
  g_signal_connect (h->element,
      "new-ssrc-pad", G_CALLBACK (new_ssrc_pad_cb), &ctx);

  thread = g_thread_new ("streaming-thread", push_buffer_func, h);

  g_mutex_lock (&ctx.mutex);

  g_message ("Waiting for ready");
  while (!g_atomic_int_get (&ctx.ready))
    g_usleep (G_USEC_PER_SEC / 100);
  g_message ("Signal no more ready");
  g_atomic_int_set (&ctx.ready, 0);

  gst_harness_push_event (h,
      gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_OOB, NULL));

  g_mutex_unlock (&ctx.mutex);

  g_thread_join (thread);
  g_mutex_clear (&ctx.mutex);
  g_cond_clear (&ctx.cond);
  gst_harness_teardown (h);
}

GST_END_TEST;


static void
new_ssrc_pad_found (GstElement * element, G_GNUC_UNUSED guint ssrc,
    GstPad * pad, GSList ** src_h)
{
  GstHarness *h = gst_harness_new_with_element (element, NULL, NULL);
  gst_harness_add_element_src_pad (h, pad);
  *src_h = g_slist_prepend (*src_h, h);
}

GST_START_TEST (test_rtpssrcdemux_max_streams)
{
  GstHarness *h = gst_harness_new_with_padnames ("rtpssrcdemux", "sink", NULL);
  GSList *src_h = NULL;
  gint i;

  g_object_set (h->element, "max-streams", 64, NULL);
  gst_harness_set_src_caps_str (h, "application/x-rtp");
  g_signal_connect (h->element,
      "new-ssrc-pad", (GCallback) new_ssrc_pad_found, &src_h);
  gst_harness_play (h);

  for (i = 0; i < 128; ++i) {
    fail_unless_equals_int (GST_FLOW_OK,
        gst_harness_push (h, create_buffer (0, i)));
  }

  fail_unless_equals_int (g_slist_length (src_h), 64);
  g_slist_free_full (src_h, (GDestroyNotify) gst_harness_teardown);
  gst_harness_teardown (h);
}

GST_END_TEST;

static void
new_rtcp_ssrc_pad_found (GstElement * element, guint ssrc,
    G_GNUC_UNUSED GstPad * rtp_pad, GSList ** src_h)
{
  GstHarness *h;
  gchar *name;

  name = g_strdup_printf ("rtcp_src_%u", ssrc);
  h = gst_harness_new_with_element (element, NULL, name);
  g_free (name);
  *src_h = g_slist_prepend (*src_h, h);
}

GST_START_TEST (test_rtpssrcdemux_rtcp_app)
{
  GstHarness *h =
      gst_harness_new_with_padnames ("rtpssrcdemux", "rtcp_sink", NULL);
  GSList *src_h = NULL;
  guint8 rtcp_app_pkt[] = { 0x81, 0xcc, 0x00, 0x05, 0x00, 0x00, 0x5d, 0xaf,
    0x20, 0x20, 0x20, 0x20, 0x21, 0x02, 0x00, 0x0a,
    0x00, 0x00, 0x5d, 0xaf, 0x00, 0x00, 0x16, 0x03
  };

  gst_harness_set_src_caps_str (h, "application/x-rtcp");
  g_signal_connect (h->element,
      "new-ssrc-pad", (GCallback) new_rtcp_ssrc_pad_found, &src_h);
  gst_harness_play (h);

  fail_unless_equals_int (GST_FLOW_OK,
      gst_harness_push (h, gst_buffer_new_wrapped_full (0, rtcp_app_pkt,
              sizeof rtcp_app_pkt, 0, sizeof rtcp_app_pkt, NULL, NULL)));

  fail_unless_equals_int (g_slist_length (src_h), 1);
  g_slist_free_full (src_h, (GDestroyNotify) gst_harness_teardown);
  gst_harness_teardown (h);
}

GST_END_TEST;

GST_START_TEST (test_rtpssrcdemux_invalid_rtp)
{
  GstHarness *h = gst_harness_new_with_padnames ("rtpssrcdemux", "sink", NULL);
  guint8 bad_pkt[] = {
    0x01, 0x02, 0x03
  };

  gst_harness_set_src_caps_str (h, "application/x-rtp");
  gst_harness_play (h);

  fail_unless_equals_int (GST_FLOW_OK,
      gst_harness_push (h, gst_buffer_new_wrapped_full (0, bad_pkt,
              sizeof bad_pkt, 0, sizeof bad_pkt, NULL, NULL)));

  gst_harness_teardown (h);
}

GST_END_TEST;

GST_START_TEST (test_rtpssrcdemux_invalid_rtcp)
{
  GstHarness *h =
      gst_harness_new_with_padnames ("rtpssrcdemux", "rtcp_sink", NULL);
  guint8 bad_pkt[] = {
    0x01, 0x02, 0x03
  };

  gst_harness_set_src_caps_str (h, "application/x-rtcp");
  gst_harness_play (h);

  fail_unless_equals_int (GST_FLOW_OK,
      gst_harness_push (h, gst_buffer_new_wrapped_full (0, bad_pkt,
              sizeof bad_pkt, 0, sizeof bad_pkt, NULL, NULL)));

  gst_harness_teardown (h);
}

GST_END_TEST;

static GstBuffer *
generate_rtcp_sr_buffer (guint ssrc)
{
  GstBuffer *buf;
  GstRTCPBuffer rtcp = GST_RTCP_BUFFER_INIT;
  GstRTCPPacket packet;

  buf = gst_rtcp_buffer_new (1000);
  fail_unless (gst_rtcp_buffer_map (buf, GST_MAP_READWRITE, &rtcp));
  fail_unless (gst_rtcp_buffer_add_packet (&rtcp, GST_RTCP_TYPE_SR, &packet));
  gst_rtcp_packet_sr_set_sender_info (&packet, ssrc, 0, 0, 1, 1);
  gst_rtcp_buffer_unmap (&rtcp);
  return buf;
}

typedef struct
{
  GstHarness *rtp_h;
  GstHarness *rtcp_h;
} SimulCtx;

static void
_simul_ctx_new_ssrc_pad_cb (GstElement * element, guint ssrc,
    GstPad * rtp_pad, SimulCtx * ctx)
{
  GstPad *rtcp_pad;
  gchar *name;

  gst_harness_add_element_src_pad (ctx->rtp_h, rtp_pad);

  name = g_strdup_printf ("rtcp_src_%u", ssrc);
  rtcp_pad = gst_element_get_static_pad (element, name);
  gst_harness_add_element_src_pad (ctx->rtcp_h, rtcp_pad);
  gst_object_unref (rtcp_pad);
  g_free (name);
}

static gpointer
_simul_ctx_push_rtp_buffers (gpointer user_data)
{
  SimulCtx *ctx = user_data;

  gst_harness_set_src_caps_str (ctx->rtp_h, "application/x-rtp");
  gst_harness_push (ctx->rtp_h, create_buffer (0, 1111));
  return NULL;
}

static gpointer
_simul_ctx_push_rtcp_buffers (gpointer user_data)
{
  SimulCtx *ctx = user_data;

  g_usleep (10);
  gst_harness_set_src_caps_str (ctx->rtcp_h, "application/x-rtcp");
  gst_harness_push (ctx->rtcp_h, generate_rtcp_sr_buffer (1111));
  return NULL;
}

GST_START_TEST (test_rtp_and_rtcp_arrives_simultaneously)
{
  guint r;
  guint repeats = 1000;
  if (RUNNING_ON_VALGRIND)
    repeats = 2;

  for (r = 0; r < repeats; r++) {
    SimulCtx ctx;
    GThread *t0, *t1;

    ctx.rtp_h = gst_harness_new_with_padnames ("rtpssrcdemux", "sink", NULL);
    ctx.rtcp_h =
        gst_harness_new_with_element (ctx.rtp_h->element, "rtcp_sink", NULL);

    g_signal_connect (ctx.rtp_h->element,
        "new-ssrc-pad", (GCallback) _simul_ctx_new_ssrc_pad_cb, &ctx);

    t0 = g_thread_new ("push rtp", _simul_ctx_push_rtp_buffers, &ctx);
    t1 = g_thread_new ("push rtcp", _simul_ctx_push_rtcp_buffers, &ctx);

    g_thread_join (t0);
    g_thread_join (t1);

    gst_harness_teardown (ctx.rtp_h);
    gst_harness_teardown (ctx.rtcp_h);
  }
}

GST_END_TEST;

static Suite *
rtpssrcdemux_suite (void)
{
  Suite *s = suite_create ("rtpssrcdemux");
  TCase *tc_chain = tcase_create ("general");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_event_forwarding);
  tcase_add_test (tc_chain, test_oob_event_locking);
  tcase_add_test (tc_chain, test_rtpssrcdemux_max_streams);
  tcase_add_test (tc_chain, test_rtpssrcdemux_rtcp_app);
  tcase_add_test (tc_chain, test_rtpssrcdemux_invalid_rtp);
  tcase_add_test (tc_chain, test_rtpssrcdemux_invalid_rtcp);
  tcase_add_test (tc_chain, test_rtp_and_rtcp_arrives_simultaneously);

  return s;
}

GST_CHECK_MAIN (rtpssrcdemux);