summaryrefslogtreecommitdiff
path: root/ext/webrtc/transportreceivebin.c
blob: 786b0c2ba6aad4c0f027ddbc65aea0fb1d489a37 (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
/* GStreamer
 * Copyright (C) 2017 Matthew Waters <matthew@centricular.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 "transportreceivebin.h"
#include "utils.h"
#include "gst/webrtc/webrtc-priv.h"

/*
 * ,-----------------------transport_receive_%u------------------,
 * ;                                                             ;
 * ;  ,-nicesrc-, ,-capsfilter-, ,---queue---, ,-dtlssrtpdec-,   ;
 * ;  ;     src o-o sink   src o-o sink  src o-osink  rtp_srco---o rtp_src
 * ;  '---------' '------------' '-----------' ;             ;   ; 
 * ;                                           ;     rtcp_srco---o rtcp_src
 * ;                                           ;             ;   ;
 * ;                                           ;     data_srco---o data_src
 * ;                                           '-------------'   ;
 * '-------------------------------------------------------------'
 *
 * Do we really wnat to be *that* permissive in what we accept?
 *
 * FIXME: When and how do we want to clear the possibly stored buffers?
 */

#define GST_CAT_DEFAULT gst_webrtc_transport_receive_bin_debug
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);

#define transport_receive_bin_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (TransportReceiveBin, transport_receive_bin,
    GST_TYPE_BIN,
    GST_DEBUG_CATEGORY_INIT (gst_webrtc_transport_receive_bin_debug,
        "webrtctransportreceivebin", 0, "webrtctransportreceivebin");
    );

static GstStaticPadTemplate rtp_sink_template =
GST_STATIC_PAD_TEMPLATE ("rtp_src",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp"));

static GstStaticPadTemplate rtcp_sink_template =
GST_STATIC_PAD_TEMPLATE ("rtcp_src",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/x-rtp"));

static GstStaticPadTemplate data_sink_template =
GST_STATIC_PAD_TEMPLATE ("data_src",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS_ANY);

enum
{
  PROP_0,
  PROP_STREAM,
};

static const gchar *
_receive_state_to_string (ReceiveState state)
{
  switch (state) {
    case RECEIVE_STATE_BLOCK:
      return "block";
    case RECEIVE_STATE_PASS:
      return "pass";
    default:
      return "Unknown";
  }
}

static GstPadProbeReturn
pad_block (GstPad * pad, GstPadProbeInfo * info, TransportReceiveBin * receive)
{
  /* Drop all events: we don't care about them and don't want to block on
   * them. Sticky events would be forwarded again later once we unblock
   * and we don't want to forward them here already because that might
   * cause a spurious GST_FLOW_FLUSHING */
  if (GST_IS_EVENT (info->data))
    return GST_PAD_PROBE_DROP;

  /* But block on any actual data-flow so we don't accidentally send that
   * to a pad that is not ready yet, causing GST_FLOW_FLUSHING and everything
   * to silently stop.
   */
  GST_LOG_OBJECT (pad, "blocking pad with data %" GST_PTR_FORMAT, info->data);

  return GST_PAD_PROBE_OK;
}

void
transport_receive_bin_set_receive_state (TransportReceiveBin * receive,
    ReceiveState state)
{

  g_mutex_lock (&receive->pad_block_lock);
  if (receive->receive_state != state) {
    GST_DEBUG_OBJECT (receive, "changing receive state to %s",
        _receive_state_to_string (state));
  }

  if (state == RECEIVE_STATE_PASS) {
    if (receive->rtp_block)
      _free_pad_block (receive->rtp_block);
    receive->rtp_block = NULL;

    if (receive->rtcp_block)
      _free_pad_block (receive->rtcp_block);
    receive->rtcp_block = NULL;
  } else {
    g_assert (state == RECEIVE_STATE_BLOCK);
    if (receive->rtp_block == NULL) {
      GstWebRTCDTLSTransport *transport;
      GstElement *dtlssrtpdec;
      GstPad *pad, *peer_pad;

      if (receive->stream) {
        transport = receive->stream->transport;
        dtlssrtpdec = transport->dtlssrtpdec;
        pad = gst_element_get_static_pad (dtlssrtpdec, "sink");
        peer_pad = gst_pad_get_peer (pad);
        receive->rtp_block =
            _create_pad_block (GST_ELEMENT (receive), peer_pad, 0, NULL, NULL);
        receive->rtp_block->block_id =
            gst_pad_add_probe (peer_pad,
            GST_PAD_PROBE_TYPE_BLOCK |
            GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
            (GstPadProbeCallback) pad_block, receive, NULL);
        gst_object_unref (peer_pad);
        gst_object_unref (pad);
      }
    }
  }
  receive->receive_state = state;
  g_mutex_unlock (&receive->pad_block_lock);
}

static void
transport_receive_bin_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  TransportReceiveBin *receive = TRANSPORT_RECEIVE_BIN (object);

  GST_OBJECT_LOCK (receive);
  switch (prop_id) {
    case PROP_STREAM:
      /* XXX: weak-ref this? */
      receive->stream = TRANSPORT_STREAM (g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
  GST_OBJECT_UNLOCK (receive);
}

static void
transport_receive_bin_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  TransportReceiveBin *receive = TRANSPORT_RECEIVE_BIN (object);

  GST_OBJECT_LOCK (receive);
  switch (prop_id) {
    case PROP_STREAM:
      g_value_set_object (value, receive->stream);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
  GST_OBJECT_UNLOCK (receive);
}

static void
transport_receive_bin_finalize (GObject * object)
{
  TransportReceiveBin *receive = TRANSPORT_RECEIVE_BIN (object);

  g_mutex_clear (&receive->pad_block_lock);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static GstStateChangeReturn
transport_receive_bin_change_state (GstElement * element,
    GstStateChange transition)
{
  TransportReceiveBin *receive = TRANSPORT_RECEIVE_BIN (element);
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;

  GST_DEBUG ("changing state: %s => %s",
      gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
      gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:{
      GstElement *elem;

      /* We want to start blocked, unless someone already switched us
       * to PASS mode. receive_state is set to BLOCKED in _init(),
       * so set up blocks with whatever the mode is now. */
      transport_receive_bin_set_receive_state (receive, receive->receive_state);

      /* XXX: because nice needs the nicesrc internal main loop running in order
       * correctly STUN... */
      /* FIXME: this races with the pad exposure later and may get not-linked */
      elem = receive->stream->transport->transport->src;
      gst_element_set_locked_state (elem, TRUE);
      gst_element_set_state (elem, GST_STATE_PLAYING);
      break;
    }
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  if (ret == GST_STATE_CHANGE_FAILURE)
    return ret;

  switch (transition) {
    case GST_STATE_CHANGE_READY_TO_NULL:{
      GstElement *elem;

      elem = receive->stream->transport->transport->src;
      gst_element_set_locked_state (elem, FALSE);
      gst_element_set_state (elem, GST_STATE_NULL);

      if (receive->rtp_block)
        _free_pad_block (receive->rtp_block);
      receive->rtp_block = NULL;

      if (receive->rtcp_block)
        _free_pad_block (receive->rtcp_block);
      receive->rtcp_block = NULL;

      break;
    }
    default:
      break;
  }

  return ret;
}

static void
rtp_queue_overrun (GstElement * queue, TransportReceiveBin * receive)
{
  GST_WARNING_OBJECT (receive, "Internal receive queue overrun. Dropping data");
}

static void
transport_receive_bin_constructed (GObject * object)
{
  TransportReceiveBin *receive = TRANSPORT_RECEIVE_BIN (object);
  GstWebRTCDTLSTransport *transport;
  GstPad *ghost, *pad;
  GstElement *capsfilter, *queue;
  GstCaps *caps;

  g_return_if_fail (receive->stream);

  /* link ice src, dtlsrtp together for rtp */
  transport = receive->stream->transport;
  gst_bin_add (GST_BIN (receive), GST_ELEMENT (transport->dtlssrtpdec));

  capsfilter = gst_element_factory_make ("capsfilter", NULL);
  caps = gst_caps_new_empty_simple ("application/x-rtp");
  g_object_set (capsfilter, "caps", caps, NULL);
  gst_caps_unref (caps);

  queue = gst_element_factory_make ("queue", NULL);
  /* FIXME: make this configurable? */
  g_object_set (queue, "leaky", 2, "max-size-time", (guint64) 0,
      "max-size-buffers", 0, "max-size-bytes", 5 * 1024 * 1024, NULL);
  g_signal_connect (queue, "overrun", G_CALLBACK (rtp_queue_overrun), receive);

  gst_bin_add (GST_BIN (receive), GST_ELEMENT (queue));
  gst_bin_add (GST_BIN (receive), GST_ELEMENT (capsfilter));
  if (!gst_element_link_pads (capsfilter, "src", queue, "sink"))
    g_warn_if_reached ();

  if (!gst_element_link_pads (queue, "src", transport->dtlssrtpdec, "sink"))
    g_warn_if_reached ();

  gst_bin_add (GST_BIN (receive), GST_ELEMENT (transport->transport->src));
  if (!gst_element_link_pads (GST_ELEMENT (transport->transport->src), "src",
          GST_ELEMENT (capsfilter), "sink"))
    g_warn_if_reached ();

  /* expose rtp_src */
  pad =
      gst_element_get_static_pad (receive->stream->transport->dtlssrtpdec,
      "rtp_src");
  receive->rtp_src = gst_ghost_pad_new ("rtp_src", pad);

  gst_element_add_pad (GST_ELEMENT (receive), receive->rtp_src);
  gst_object_unref (pad);

  /* expose rtcp_rtc */
  pad = gst_element_get_static_pad (receive->stream->transport->dtlssrtpdec,
      "rtcp_src");
  receive->rtcp_src = gst_ghost_pad_new ("rtcp_src", pad);
  gst_element_add_pad (GST_ELEMENT (receive), receive->rtcp_src);
  gst_object_unref (pad);

  /* expose data_src */
  pad = gst_element_request_pad_simple (receive->stream->transport->dtlssrtpdec,
      "data_src");
  ghost = gst_ghost_pad_new ("data_src", pad);
  gst_element_add_pad (GST_ELEMENT (receive), ghost);
  gst_object_unref (pad);

  G_OBJECT_CLASS (parent_class)->constructed (object);
}

static void
transport_receive_bin_class_init (TransportReceiveBinClass * klass)
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstElementClass *element_class = (GstElementClass *) klass;

  element_class->change_state = transport_receive_bin_change_state;

  gst_element_class_add_static_pad_template (element_class, &rtp_sink_template);
  gst_element_class_add_static_pad_template (element_class,
      &rtcp_sink_template);
  gst_element_class_add_static_pad_template (element_class,
      &data_sink_template);

  gst_element_class_set_metadata (element_class, "WebRTC Transport Receive Bin",
      "Filter/Network/WebRTC", "A bin for webrtc connections",
      "Matthew Waters <matthew@centricular.com>");

  gobject_class->constructed = transport_receive_bin_constructed;
  gobject_class->get_property = transport_receive_bin_get_property;
  gobject_class->set_property = transport_receive_bin_set_property;
  gobject_class->finalize = transport_receive_bin_finalize;

  g_object_class_install_property (gobject_class,
      PROP_STREAM,
      g_param_spec_object ("stream", "Stream",
          "The TransportStream for this receiving bin",
          transport_stream_get_type (),
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}

static void
transport_receive_bin_init (TransportReceiveBin * receive)
{
  receive->receive_state = RECEIVE_STATE_BLOCK;
  g_mutex_init (&receive->pad_block_lock);
}