• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2017 Matthew Waters <matthew@centricular.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23 
24 #include "transportreceivebin.h"
25 #include "utils.h"
26 #include "gst/webrtc/webrtc-priv.h"
27 
28 /*
29  * ,-----------------------transport_receive_%u------------------,
30  * ;                                                             ;
31  * ;  ,-nicesrc-, ,-capsfilter-, ,---queue---, ,-dtlssrtpdec-,   ;
32  * ;  ;     src o-o sink   src o-o sink  src o-osink  rtp_srco---o rtp_src
33  * ;  '---------' '------------' '-----------' ;             ;   ;
34  * ;                                           ;     rtcp_srco---o rtcp_src
35  * ;                                           ;             ;   ;
36  * ;                                           ;     data_srco---o data_src
37  * ;                                           '-------------'   ;
38  * '-------------------------------------------------------------'
39  *
40  * Do we really wnat to be *that* permissive in what we accept?
41  *
42  * FIXME: When and how do we want to clear the possibly stored buffers?
43  */
44 
45 #define GST_CAT_DEFAULT gst_webrtc_transport_receive_bin_debug
46 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
47 
48 #define transport_receive_bin_parent_class parent_class
49 G_DEFINE_TYPE_WITH_CODE (TransportReceiveBin, transport_receive_bin,
50     GST_TYPE_BIN,
51     GST_DEBUG_CATEGORY_INIT (gst_webrtc_transport_receive_bin_debug,
52         "webrtctransportreceivebin", 0, "webrtctransportreceivebin");
53     );
54 
55 static GstStaticPadTemplate rtp_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("rtp_src",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS ("application/x-rtp"));
60 
61 static GstStaticPadTemplate rtcp_sink_template =
62 GST_STATIC_PAD_TEMPLATE ("rtcp_src",
63     GST_PAD_SINK,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("application/x-rtp"));
66 
67 static GstStaticPadTemplate data_sink_template =
68 GST_STATIC_PAD_TEMPLATE ("data_src",
69     GST_PAD_SINK,
70     GST_PAD_ALWAYS,
71     GST_STATIC_CAPS_ANY);
72 
73 enum
74 {
75   PROP_0,
76   PROP_STREAM,
77 };
78 
79 static const gchar *
_receive_state_to_string(ReceiveState state)80 _receive_state_to_string (ReceiveState state)
81 {
82   switch (state) {
83     case RECEIVE_STATE_BLOCK:
84       return "block";
85     case RECEIVE_STATE_PASS:
86       return "pass";
87     default:
88       return "Unknown";
89   }
90 }
91 
92 static GstPadProbeReturn
pad_block(GstPad * pad,GstPadProbeInfo * info,TransportReceiveBin * receive)93 pad_block (GstPad * pad, GstPadProbeInfo * info, TransportReceiveBin * receive)
94 {
95   /* Drop all events: we don't care about them and don't want to block on
96    * them. Sticky events would be forwarded again later once we unblock
97    * and we don't want to forward them here already because that might
98    * cause a spurious GST_FLOW_FLUSHING */
99   if (GST_IS_EVENT (info->data) || GST_IS_QUERY (info->data))
100     return GST_PAD_PROBE_DROP;
101 
102   /* But block on any actual data-flow so we don't accidentally send that
103    * to a pad that is not ready yet, causing GST_FLOW_FLUSHING and everything
104    * to silently stop.
105    */
106   GST_LOG_OBJECT (pad, "blocking pad with data %" GST_PTR_FORMAT, info->data);
107 
108   return GST_PAD_PROBE_OK;
109 }
110 
111 void
transport_receive_bin_set_receive_state(TransportReceiveBin * receive,ReceiveState state)112 transport_receive_bin_set_receive_state (TransportReceiveBin * receive,
113     ReceiveState state)
114 {
115   GstWebRTCICEConnectionState icestate;
116 
117   g_mutex_lock (&receive->pad_block_lock);
118   if (receive->receive_state != state) {
119     GST_DEBUG_OBJECT (receive, "Requested change of receive state to %s",
120         _receive_state_to_string (state));
121   }
122 
123   receive->receive_state = state;
124 
125   g_object_get (receive->stream->transport->transport, "state", &icestate,
126       NULL);
127   if (state == RECEIVE_STATE_PASS) {
128     if (icestate == GST_WEBRTC_ICE_CONNECTION_STATE_CONNECTED ||
129         icestate == GST_WEBRTC_ICE_CONNECTION_STATE_COMPLETED) {
130       GST_LOG_OBJECT (receive, "Unblocking nicesrc because ICE is connected.");
131     } else {
132       GST_LOG_OBJECT (receive, "Can't unblock nicesrc yet because ICE "
133           "is not connected, it is %d", icestate);
134       state = RECEIVE_STATE_BLOCK;
135     }
136   }
137 
138   if (state == RECEIVE_STATE_PASS) {
139     g_object_set (receive->queue, "leaky", 0, NULL);
140 
141     if (receive->rtp_block)
142       _free_pad_block (receive->rtp_block);
143     receive->rtp_block = NULL;
144 
145     if (receive->rtcp_block)
146       _free_pad_block (receive->rtcp_block);
147     receive->rtcp_block = NULL;
148   } else {
149     g_assert (state == RECEIVE_STATE_BLOCK);
150     g_object_set (receive->queue, "leaky", 2, NULL);
151     if (receive->rtp_block == NULL) {
152       GstWebRTCDTLSTransport *transport;
153       GstElement *dtlssrtpdec;
154       GstPad *pad, *peer_pad;
155 
156       if (receive->stream) {
157         transport = receive->stream->transport;
158         dtlssrtpdec = transport->dtlssrtpdec;
159         pad = gst_element_get_static_pad (dtlssrtpdec, "sink");
160         peer_pad = gst_pad_get_peer (pad);
161         receive->rtp_block =
162             _create_pad_block (GST_ELEMENT (receive), peer_pad, 0, NULL, NULL);
163         receive->rtp_block->block_id =
164             gst_pad_add_probe (peer_pad,
165             GST_PAD_PROBE_TYPE_BLOCK |
166             GST_PAD_PROBE_TYPE_DATA_DOWNSTREAM,
167             (GstPadProbeCallback) pad_block, receive, NULL);
168         gst_object_unref (peer_pad);
169         gst_object_unref (pad);
170       }
171     }
172   }
173   g_mutex_unlock (&receive->pad_block_lock);
174 }
175 
176 static void
_on_notify_ice_connection_state(GstWebRTCICETransport * transport,GParamSpec * pspec,TransportReceiveBin * receive)177 _on_notify_ice_connection_state (GstWebRTCICETransport * transport,
178     GParamSpec * pspec, TransportReceiveBin * receive)
179 {
180   transport_receive_bin_set_receive_state (receive, receive->receive_state);
181 }
182 
183 static void
transport_receive_bin_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)184 transport_receive_bin_set_property (GObject * object, guint prop_id,
185     const GValue * value, GParamSpec * pspec)
186 {
187   TransportReceiveBin *receive = TRANSPORT_RECEIVE_BIN (object);
188 
189   GST_OBJECT_LOCK (receive);
190   switch (prop_id) {
191     case PROP_STREAM:
192       /* XXX: weak-ref this? */
193       receive->stream = TRANSPORT_STREAM (g_value_get_object (value));
194       break;
195     default:
196       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
197       break;
198   }
199   GST_OBJECT_UNLOCK (receive);
200 }
201 
202 static void
transport_receive_bin_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)203 transport_receive_bin_get_property (GObject * object, guint prop_id,
204     GValue * value, GParamSpec * pspec)
205 {
206   TransportReceiveBin *receive = TRANSPORT_RECEIVE_BIN (object);
207 
208   GST_OBJECT_LOCK (receive);
209   switch (prop_id) {
210     case PROP_STREAM:
211       g_value_set_object (value, receive->stream);
212       break;
213     default:
214       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215       break;
216   }
217   GST_OBJECT_UNLOCK (receive);
218 }
219 
220 static void
transport_receive_bin_finalize(GObject * object)221 transport_receive_bin_finalize (GObject * object)
222 {
223   TransportReceiveBin *receive = TRANSPORT_RECEIVE_BIN (object);
224 
225   g_mutex_clear (&receive->pad_block_lock);
226 
227   G_OBJECT_CLASS (parent_class)->finalize (object);
228 }
229 
230 static GstStateChangeReturn
transport_receive_bin_change_state(GstElement * element,GstStateChange transition)231 transport_receive_bin_change_state (GstElement * element,
232     GstStateChange transition)
233 {
234   TransportReceiveBin *receive = TRANSPORT_RECEIVE_BIN (element);
235   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
236 
237   GST_DEBUG ("changing state: %s => %s",
238       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
239       gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
240 
241   switch (transition) {
242     case GST_STATE_CHANGE_NULL_TO_READY:{
243       GstElement *elem;
244 
245       /* We want to start blocked, unless someone already switched us
246        * to PASS mode. receive_state is set to BLOCKED in _init(),
247        * so set up blocks with whatever the mode is now. */
248       transport_receive_bin_set_receive_state (receive, receive->receive_state);
249 
250       /* XXX: because nice needs the nicesrc internal main loop running in order
251        * correctly STUN... */
252       /* FIXME: this races with the pad exposure later and may get not-linked */
253       elem = receive->stream->transport->transport->src;
254       gst_element_set_locked_state (elem, TRUE);
255       gst_element_set_state (elem, GST_STATE_PLAYING);
256       break;
257     }
258     default:
259       break;
260   }
261 
262   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
263   if (ret == GST_STATE_CHANGE_FAILURE)
264     return ret;
265 
266   switch (transition) {
267     case GST_STATE_CHANGE_READY_TO_NULL:{
268       GstElement *elem;
269 
270       elem = receive->stream->transport->transport->src;
271       gst_element_set_locked_state (elem, FALSE);
272       gst_element_set_state (elem, GST_STATE_NULL);
273 
274       if (receive->rtp_block)
275         _free_pad_block (receive->rtp_block);
276       receive->rtp_block = NULL;
277 
278       if (receive->rtcp_block)
279         _free_pad_block (receive->rtcp_block);
280       receive->rtcp_block = NULL;
281 
282       break;
283     }
284     default:
285       break;
286   }
287 
288   return ret;
289 }
290 
291 static void
rtp_queue_overrun(GstElement * queue,TransportReceiveBin * receive)292 rtp_queue_overrun (GstElement * queue, TransportReceiveBin * receive)
293 {
294   GST_WARNING_OBJECT (receive, "Internal receive queue overrun. Dropping data");
295 }
296 
297 static GstPadProbeReturn
drop_serialized_queries(GstPad * pad,GstPadProbeInfo * info,TransportReceiveBin * receive)298 drop_serialized_queries (GstPad * pad, GstPadProbeInfo * info,
299     TransportReceiveBin * receive)
300 {
301   GstQuery *query = GST_PAD_PROBE_INFO_QUERY (info);
302 
303   if (GST_QUERY_IS_SERIALIZED (query))
304     return GST_PAD_PROBE_DROP;
305   else
306     return GST_PAD_PROBE_PASS;
307 }
308 
309 static void
transport_receive_bin_constructed(GObject * object)310 transport_receive_bin_constructed (GObject * object)
311 {
312   TransportReceiveBin *receive = TRANSPORT_RECEIVE_BIN (object);
313   GstWebRTCDTLSTransport *transport;
314   GstPad *ghost, *pad;
315   GstElement *capsfilter;
316   GstCaps *caps;
317 
318   g_return_if_fail (receive->stream);
319 
320   /* link ice src, dtlsrtp together for rtp */
321   transport = receive->stream->transport;
322   gst_bin_add (GST_BIN (receive), GST_ELEMENT (transport->dtlssrtpdec));
323 
324   capsfilter = gst_element_factory_make ("capsfilter", NULL);
325   caps = gst_caps_new_empty_simple ("application/x-rtp");
326   g_object_set (capsfilter, "caps", caps, NULL);
327   gst_caps_unref (caps);
328 
329   receive->queue = gst_element_factory_make ("queue", NULL);
330   /* FIXME: make this configurable? */
331   g_object_set (receive->queue, "leaky", 2, "max-size-time", (guint64) 0,
332       "max-size-buffers", 0, "max-size-bytes", 5 * 1024 * 1024, NULL);
333   g_signal_connect (receive->queue, "overrun", G_CALLBACK (rtp_queue_overrun),
334       receive);
335 
336   pad = gst_element_get_static_pad (receive->queue, "sink");
337   gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_QUERY_DOWNSTREAM,
338       (GstPadProbeCallback) drop_serialized_queries, receive, NULL);
339   gst_object_unref (pad);
340 
341   gst_bin_add (GST_BIN (receive), GST_ELEMENT (receive->queue));
342   gst_bin_add (GST_BIN (receive), GST_ELEMENT (capsfilter));
343   if (!gst_element_link_pads (capsfilter, "src", receive->queue, "sink"))
344     g_warn_if_reached ();
345 
346   if (!gst_element_link_pads (receive->queue, "src", transport->dtlssrtpdec,
347           "sink"))
348     g_warn_if_reached ();
349 
350   gst_bin_add (GST_BIN (receive), GST_ELEMENT (transport->transport->src));
351   if (!gst_element_link_pads (GST_ELEMENT (transport->transport->src), "src",
352           GST_ELEMENT (capsfilter), "sink"))
353     g_warn_if_reached ();
354 
355   /* expose rtp_src */
356   pad =
357       gst_element_get_static_pad (receive->stream->transport->dtlssrtpdec,
358       "rtp_src");
359   receive->rtp_src = gst_ghost_pad_new ("rtp_src", pad);
360 
361   gst_element_add_pad (GST_ELEMENT (receive), receive->rtp_src);
362   gst_object_unref (pad);
363 
364   /* expose rtcp_rtc */
365   pad = gst_element_get_static_pad (receive->stream->transport->dtlssrtpdec,
366       "rtcp_src");
367   receive->rtcp_src = gst_ghost_pad_new ("rtcp_src", pad);
368   gst_element_add_pad (GST_ELEMENT (receive), receive->rtcp_src);
369   gst_object_unref (pad);
370 
371   /* expose data_src */
372   pad = gst_element_request_pad_simple (receive->stream->transport->dtlssrtpdec,
373       "data_src");
374   ghost = gst_ghost_pad_new ("data_src", pad);
375   gst_element_add_pad (GST_ELEMENT (receive), ghost);
376   gst_object_unref (pad);
377 
378   g_signal_connect_after (receive->stream->transport->transport,
379       "notify::state", G_CALLBACK (_on_notify_ice_connection_state), receive);
380 
381   G_OBJECT_CLASS (parent_class)->constructed (object);
382 }
383 
384 static void
transport_receive_bin_class_init(TransportReceiveBinClass * klass)385 transport_receive_bin_class_init (TransportReceiveBinClass * klass)
386 {
387   GObjectClass *gobject_class = (GObjectClass *) klass;
388   GstElementClass *element_class = (GstElementClass *) klass;
389 
390   element_class->change_state = transport_receive_bin_change_state;
391 
392   gst_element_class_add_static_pad_template (element_class, &rtp_sink_template);
393   gst_element_class_add_static_pad_template (element_class,
394       &rtcp_sink_template);
395   gst_element_class_add_static_pad_template (element_class,
396       &data_sink_template);
397 
398   gst_element_class_set_metadata (element_class, "WebRTC Transport Receive Bin",
399       "Filter/Network/WebRTC", "A bin for webrtc connections",
400       "Matthew Waters <matthew@centricular.com>");
401 
402   gobject_class->constructed = transport_receive_bin_constructed;
403   gobject_class->get_property = transport_receive_bin_get_property;
404   gobject_class->set_property = transport_receive_bin_set_property;
405   gobject_class->finalize = transport_receive_bin_finalize;
406 
407   g_object_class_install_property (gobject_class,
408       PROP_STREAM,
409       g_param_spec_object ("stream", "Stream",
410           "The TransportStream for this receiving bin",
411           transport_stream_get_type (),
412           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
413 }
414 
415 static void
transport_receive_bin_init(TransportReceiveBin * receive)416 transport_receive_bin_init (TransportReceiveBin * receive)
417 {
418   receive->receive_state = RECEIVE_STATE_BLOCK;
419   g_mutex_init (&receive->pad_block_lock);
420 }
421