• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <2018> Marc Leeman <marc.leeman@gmail.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 /**
21  * SECTION: gstrtsinkp
22  * @title: GstRtpSink
23  * @short description: element with Uri interface to stream RTP data to
24  * the network.
25  *
26  * RTP (RFC 3550) is a protocol to stream media over the network while
27  * retaining the timing information and providing enough information to
28  * reconstruct the correct timing domain by the receiver.
29  *
30  * The RTP data port should be even, while the RTCP port should be
31  * odd. The URI that is entered defines the data port, the RTCP port will
32  * be allocated to the next port.
33  *
34  * This element hooks up the correct sockets to support both RTP as the
35  * accompanying RTCP layer.
36  *
37  * This Bin handles streaming RTP payloaded data on the network.
38  *
39  * This element also implements the URI scheme `rtp://` allowing to send
40  * data on the network by bins that allow use the URI to determine the sink.
41  * The RTP URI handler also allows setting properties through the URI query.
42  */
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46 
47 #include <gio/gio.h>
48 
49 #include "gstrtpsink.h"
50 #include "gstrtp-utils.h"
51 
52 GST_DEBUG_CATEGORY_STATIC (gst_rtp_sink_debug);
53 #define GST_CAT_DEFAULT gst_rtp_sink_debug
54 
55 #define DEFAULT_PROP_TTL              64
56 #define DEFAULT_PROP_TTL_MC           1
57 
58 #define DEFAULT_PROP_ADDRESS          "0.0.0.0"
59 #define DEFAULT_PROP_PORT             5004
60 #define DEFAULT_PROP_URI              "rtp://"DEFAULT_PROP_ADDRESS":"G_STRINGIFY(DEFAULT_PROP_PORT)
61 #define DEFAULT_PROP_MULTICAST_IFACE  NULL
62 
63 enum
64 {
65   PROP_0,
66 
67   PROP_URI,
68   PROP_ADDRESS,
69   PROP_PORT,
70   PROP_TTL,
71   PROP_TTL_MC,
72   PROP_MULTICAST_IFACE,
73 
74   PROP_LAST
75 };
76 
77 static void gst_rtp_sink_uri_handler_init (gpointer g_iface,
78     gpointer iface_data);
79 
80 #define gst_rtp_sink_parent_class parent_class
81 G_DEFINE_TYPE_WITH_CODE (GstRtpSink, gst_rtp_sink, GST_TYPE_BIN,
82     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtp_sink_uri_handler_init);
83     GST_DEBUG_CATEGORY_INIT (gst_rtp_sink_debug, "rtpsink", 0, "RTP Sink"));
84 GST_ELEMENT_REGISTER_DEFINE (rtpsink, "rtpsink", GST_RANK_PRIMARY + 1,
85     GST_TYPE_RTP_SINK);
86 
87 #define GST_RTP_SINK_GET_LOCK(obj) (&((GstRtpSink*)(obj))->lock)
88 #define GST_RTP_SINK_LOCK(obj) (g_mutex_lock (GST_RTP_SINK_GET_LOCK(obj)))
89 #define GST_RTP_SINK_UNLOCK(obj) (g_mutex_unlock (GST_RTP_SINK_GET_LOCK(obj)))
90 
91 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink_%u",
92     GST_PAD_SINK,
93     GST_PAD_REQUEST,
94     GST_STATIC_CAPS ("application/x-rtp"));
95 
96 static GstStateChangeReturn
97 gst_rtp_sink_change_state (GstElement * element, GstStateChange transition);
98 
99 static void
gst_rtp_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)100 gst_rtp_sink_set_property (GObject * object, guint prop_id,
101     const GValue * value, GParamSpec * pspec)
102 {
103   GstRtpSink *self = GST_RTP_SINK (object);
104 
105   switch (prop_id) {
106     case PROP_URI:{
107       GstUri *uri = NULL;
108 
109       GST_RTP_SINK_LOCK (object);
110       uri = gst_uri_from_string (g_value_get_string (value));
111       if (uri == NULL)
112         break;
113 
114       if (self->uri)
115         gst_uri_unref (self->uri);
116       self->uri = uri;
117 
118       gst_rtp_utils_set_properties_from_uri_query (G_OBJECT (self), self->uri);
119 
120       g_object_set (self, "address", gst_uri_get_host (self->uri), NULL);
121       g_object_set (self, "port", gst_uri_get_port (self->uri), NULL);
122 
123       GST_RTP_SINK_UNLOCK (object);
124       break;
125     }
126     case PROP_ADDRESS:
127       gst_uri_set_host (self->uri, g_value_get_string (value));
128       g_object_set_property (G_OBJECT (self->rtp_sink), "host", value);
129       g_object_set_property (G_OBJECT (self->rtcp_sink), "host", value);
130       break;
131 
132     case PROP_PORT:{
133       guint port = g_value_get_uint (value);
134 
135       /* According to RFC 3550, 11, RTCP receiver port should be even
136        * number and RTCP port should be the RTP port + 1 */
137       if (port & 0x1)
138         GST_WARNING_OBJECT (self,
139             "Port %u is odd, this is not standard (see RFC 3550).", port);
140 
141       gst_uri_set_port (self->uri, port);
142       g_object_set (self->rtp_sink, "port", port, NULL);
143       g_object_set (self->rtcp_sink, "port", port + 1, NULL);
144       break;
145     }
146     case PROP_TTL:
147       self->ttl = g_value_get_int (value);
148       g_object_set (self->rtp_sink, "ttl", self->ttl, NULL);
149       g_object_set (self->rtcp_sink, "ttl", self->ttl, NULL);
150       break;
151     case PROP_TTL_MC:
152       self->ttl_mc = g_value_get_int (value);
153       g_object_set (self->rtp_sink, "ttl-mc", self->ttl_mc, NULL);
154       g_object_set (self->rtcp_sink, "ttl-mc", self->ttl_mc, NULL);
155       break;
156     case PROP_MULTICAST_IFACE:
157       g_free (self->multi_iface);
158 
159       if (g_value_get_string (value) == NULL)
160         self->multi_iface = g_strdup (DEFAULT_PROP_MULTICAST_IFACE);
161       else
162         self->multi_iface = g_value_dup_string (value);
163       break;
164     default:
165       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166       break;
167   }
168 }
169 
170 static void
gst_rtp_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)171 gst_rtp_sink_get_property (GObject * object, guint prop_id,
172     GValue * value, GParamSpec * pspec)
173 {
174   GstRtpSink *self = GST_RTP_SINK (object);
175 
176   switch (prop_id) {
177     case PROP_URI:
178       GST_RTP_SINK_LOCK (object);
179       if (self->uri)
180         g_value_take_string (value, gst_uri_to_string (self->uri));
181       else
182         g_value_set_string (value, NULL);
183       GST_RTP_SINK_UNLOCK (object);
184       break;
185     case PROP_ADDRESS:
186       g_value_set_string (value, gst_uri_get_host (self->uri));
187       break;
188     case PROP_PORT:
189       g_value_set_uint (value, gst_uri_get_port (self->uri));
190       break;
191     case PROP_TTL:
192       g_value_set_int (value, self->ttl);
193       break;
194     case PROP_TTL_MC:
195       g_value_set_int (value, self->ttl_mc);
196       break;
197     case PROP_MULTICAST_IFACE:
198       g_value_set_string (value, self->multi_iface);
199       break;
200     default:
201       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202       break;
203   }
204 }
205 
206 static void
gst_rtp_sink_finalize(GObject * gobject)207 gst_rtp_sink_finalize (GObject * gobject)
208 {
209   GstRtpSink *self = GST_RTP_SINK (gobject);
210 
211   if (self->uri)
212     gst_uri_unref (self->uri);
213 
214   g_free (self->multi_iface);
215 
216   g_mutex_clear (&self->lock);
217   G_OBJECT_CLASS (parent_class)->finalize (gobject);
218 }
219 
220 static gboolean
gst_rtp_sink_setup_elements(GstRtpSink * self)221 gst_rtp_sink_setup_elements (GstRtpSink * self)
222 {
223   /*GstPad *pad; */
224   gchar name[48];
225 
226   /* pads are all named */
227   g_snprintf (name, 48, "send_rtp_src_%u", GST_ELEMENT (self)->numpads);
228   gst_element_link_pads (self->rtpbin, name, self->funnel_rtp, "sink_%u");
229 
230   g_snprintf (name, 48, "send_rtcp_src_%u", GST_ELEMENT (self)->numpads);
231   gst_element_link_pads (self->rtpbin, name, self->funnel_rtcp, "sink_%u");
232 
233   g_snprintf (name, 48, "recv_rtcp_sink_%u", GST_ELEMENT (self)->numpads);
234   gst_element_link_pads (self->rtcp_src, "src", self->rtpbin, name);
235 
236   return TRUE;
237 }
238 
239 static GstPad *
gst_rtp_sink_request_new_pad(GstElement * element,GstPadTemplate * templ,const gchar * name,const GstCaps * caps)240 gst_rtp_sink_request_new_pad (GstElement * element,
241     GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
242 {
243   GstRtpSink *self = GST_RTP_SINK (element);
244   GstPad *rpad, *pad = NULL;
245 
246   if (self->rtpbin == NULL) {
247     GST_ELEMENT_ERROR (self, CORE, MISSING_PLUGIN, (NULL),
248         ("%s", "rtpbin element is not available"));
249     return NULL;
250   }
251 
252   if (gst_rtp_sink_setup_elements (self) == FALSE)
253     return NULL;
254 
255   GST_RTP_SINK_LOCK (self);
256   rpad = gst_element_request_pad_simple (self->rtpbin, "send_rtp_sink_%u");
257   if (rpad) {
258     pad = gst_ghost_pad_new (GST_PAD_NAME (rpad), rpad);
259     gst_element_add_pad (element, pad);
260     gst_clear_object (&rpad);
261   }
262   GST_RTP_SINK_UNLOCK (self);
263 
264   g_return_val_if_fail (pad != NULL, NULL);
265 
266 
267   return pad;
268 }
269 
270 static void
gst_rtp_sink_release_pad(GstElement * element,GstPad * pad)271 gst_rtp_sink_release_pad (GstElement * element, GstPad * pad)
272 {
273   GstRtpSink *self = GST_RTP_SINK (element);
274   GstPad *rpad = gst_ghost_pad_get_target (GST_GHOST_PAD (pad));
275 
276   GST_RTP_SINK_LOCK (self);
277   gst_element_release_request_pad (self->rtpbin, rpad);
278   gst_object_unref (rpad);
279 
280   gst_pad_set_active (pad, FALSE);
281   gst_element_remove_pad (GST_ELEMENT (self), pad);
282 
283   GST_RTP_SINK_UNLOCK (self);
284 }
285 
286 static void
gst_rtp_sink_class_init(GstRtpSinkClass * klass)287 gst_rtp_sink_class_init (GstRtpSinkClass * klass)
288 {
289   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
290   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
291 
292   gobject_class->set_property = gst_rtp_sink_set_property;
293   gobject_class->get_property = gst_rtp_sink_get_property;
294   gobject_class->finalize = gst_rtp_sink_finalize;
295   gstelement_class->change_state = gst_rtp_sink_change_state;
296 
297   gstelement_class->request_new_pad =
298       GST_DEBUG_FUNCPTR (gst_rtp_sink_request_new_pad);
299   gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_rtp_sink_release_pad);
300 
301   /**
302    * GstRtpSink:uri:
303    *
304    * uri to stream RTP to. All GStreamer parameters can be
305    * encoded in the URI, this URI format is RFC compliant.
306    */
307   g_object_class_install_property (gobject_class, PROP_URI,
308       g_param_spec_string ("uri", "URI",
309           "URI in the form of rtp://host:port?query", DEFAULT_PROP_URI,
310           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311 
312   /**
313    * GstRtpSink:address:
314    *
315    * Address to receive packets from (can be IPv4 or IPv6).
316    */
317   g_object_class_install_property (gobject_class, PROP_ADDRESS,
318       g_param_spec_string ("address", "Address",
319           "Address to send packets to (can be IPv4 or IPv6).",
320           DEFAULT_PROP_ADDRESS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 
322   /**
323    * GstRtpSink:port:
324    *
325    * The port to listen to RTP packets, the RTCP port is this value
326    * +1. This port must be an even number.
327    */
328   g_object_class_install_property (gobject_class, PROP_PORT,
329       g_param_spec_uint ("port", "Port", "The port RTP packets will be sent, "
330           "the RTCP port is this value + 1. This port must be an even number.",
331           2, 65534, DEFAULT_PROP_PORT,
332           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
333 
334   /**
335    * GstRtpSink:ttl:
336    *
337    * Set the unicast TTL parameter.
338    */
339   g_object_class_install_property (gobject_class, PROP_TTL,
340       g_param_spec_int ("ttl", "Unicast TTL",
341           "Used for setting the unicast TTL parameter",
342           0, 255, DEFAULT_PROP_TTL,
343           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 
345   /**
346    * GstRtpSink:ttl-mc:
347    *
348    * Set the multicast TTL parameter.
349    */
350   g_object_class_install_property (gobject_class, PROP_TTL_MC,
351       g_param_spec_int ("ttl-mc", "Multicast TTL",
352           "Used for setting the multicast TTL parameter", 0, 255,
353           DEFAULT_PROP_TTL_MC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 
355   /* GstRtpSink:multicast-iface:
356    *
357    * The networkinterface on which to join the multicast group
358    */
359   g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
360       g_param_spec_string ("multicast-iface", "Multicast Interface",
361           "The network interface on which to join the multicast group."
362           "This allows multiple interfaces separated by comma. (\"eth0,eth1\")",
363           DEFAULT_PROP_MULTICAST_IFACE,
364           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
365 
366   gst_element_class_add_pad_template (gstelement_class,
367       gst_static_pad_template_get (&sink_template));
368 
369   gst_element_class_set_static_metadata (gstelement_class,
370       "RTP Sink element",
371       "Generic/Bin/Sink",
372       "Simple RTP sink", "Marc Leeman <marc.leeman@gmail.com>");
373 }
374 
375 static void
gst_rtp_sink_rtpbin_element_added_cb(GstBin * element,GstElement * new_element,gpointer data)376 gst_rtp_sink_rtpbin_element_added_cb (GstBin * element,
377     GstElement * new_element, gpointer data)
378 {
379   GstRtpSink *self = GST_RTP_SINK (data);
380   GST_INFO_OBJECT (self,
381       "Element %" GST_PTR_FORMAT " added element %" GST_PTR_FORMAT ".", element,
382       new_element);
383 }
384 
385 static void
gst_rtp_sink_rtpbin_pad_added_cb(GstElement * element,GstPad * pad,gpointer data)386 gst_rtp_sink_rtpbin_pad_added_cb (GstElement * element, GstPad * pad,
387     gpointer data)
388 {
389   GstRtpSink *self = GST_RTP_SINK (data);
390   GstCaps *caps = gst_pad_query_caps (pad, NULL);
391   GstPad *upad;
392 
393   /* Expose RTP data pad only */
394   GST_INFO_OBJECT (self,
395       "Element %" GST_PTR_FORMAT " added pad %" GST_PTR_FORMAT "with caps %"
396       GST_PTR_FORMAT ".", element, pad, caps);
397 
398   /* Sanity checks */
399   if (GST_PAD_DIRECTION (pad) == GST_PAD_SINK) {
400     /* Src pad, do not expose */
401     gst_caps_unref (caps);
402     return;
403   }
404 
405   if (G_LIKELY (caps)) {
406     GstCaps *ref_caps = gst_caps_new_empty_simple ("application/x-rtcp");
407 
408     if (gst_caps_can_intersect (caps, ref_caps)) {
409       /* SRC RTCP caps, do not expose */
410       gst_caps_unref (ref_caps);
411       gst_caps_unref (caps);
412 
413       return;
414     }
415     gst_caps_unref (ref_caps);
416   } else {
417     GST_ERROR_OBJECT (self, "Pad with no caps detected.");
418     gst_caps_unref (caps);
419 
420     return;
421   }
422   gst_caps_unref (caps);
423 
424   upad = gst_element_get_compatible_pad (self->funnel_rtp, pad, NULL);
425   if (upad == NULL) {
426     GST_ERROR_OBJECT (self, "No compatible pad found to link pad.");
427     gst_caps_unref (caps);
428 
429     return;
430   }
431   GST_INFO_OBJECT (self, "Linking with pad %" GST_PTR_FORMAT ".", upad);
432   gst_pad_link (pad, upad);
433   gst_object_unref (upad);
434 }
435 
436 static void
gst_rtp_sink_rtpbin_pad_removed_cb(GstElement * element,GstPad * pad,gpointer data)437 gst_rtp_sink_rtpbin_pad_removed_cb (GstElement * element, GstPad * pad,
438     gpointer data)
439 {
440   GstRtpSink *self = GST_RTP_SINK (data);
441   GST_INFO_OBJECT (self,
442       "Element %" GST_PTR_FORMAT " removed pad %" GST_PTR_FORMAT ".", element,
443       pad);
444 }
445 
446 static gboolean
gst_rtp_sink_reuse_socket(GstRtpSink * self)447 gst_rtp_sink_reuse_socket (GstRtpSink * self)
448 {
449   GSocket *socket = NULL;
450 
451   gst_element_set_locked_state (self->rtcp_src, FALSE);
452   gst_element_sync_state_with_parent (self->rtcp_src);
453 
454   /* share the socket created by the sink */
455   g_object_get (self->rtcp_src, "used-socket", &socket, NULL);
456   g_object_set (self->rtcp_sink, "socket", socket, "auto-multicast", FALSE,
457       "close-socket", FALSE, NULL);
458   g_object_unref (socket);
459 
460   g_object_set (self->rtcp_sink, "sync", FALSE, "async", FALSE, NULL);
461   gst_element_set_locked_state (self->rtcp_sink, FALSE);
462   gst_element_sync_state_with_parent (self->rtcp_sink);
463 
464   return TRUE;
465 
466 }
467 
468 static gboolean
gst_rtp_sink_start(GstRtpSink * self)469 gst_rtp_sink_start (GstRtpSink * self)
470 {
471   GInetAddress *iaddr = NULL;
472   gchar *remote_addr = NULL;
473   GError *error = NULL;
474 
475   /* Should not be NULL */
476   g_return_val_if_fail (self->uri != NULL, FALSE);
477 
478   iaddr = g_inet_address_new_from_string (gst_uri_get_host (self->uri));
479   if (!iaddr) {
480     GList *results;
481     GResolver *resolver = NULL;
482 
483     resolver = g_resolver_get_default ();
484     results =
485         g_resolver_lookup_by_name (resolver, gst_uri_get_host (self->uri), NULL,
486         &error);
487 
488     if (!results) {
489       g_object_unref (resolver);
490       goto dns_resolve_failed;
491     }
492 
493     iaddr = G_INET_ADDRESS (g_object_ref (results->data));
494 
495     g_resolver_free_addresses (results);
496     g_object_unref (resolver);
497   }
498   remote_addr = g_inet_address_to_string (iaddr);
499 
500   if (g_inet_address_get_is_multicast (iaddr)) {
501     g_object_set (self->rtcp_src, "address", remote_addr, "port",
502         gst_uri_get_port (self->uri) + 1, NULL);
503 
504     /* set multicast-iface on the udpsrc and udpsink elements */
505     g_object_set (self->rtcp_src, "multicast-iface", self->multi_iface, NULL);
506     g_object_set (self->rtcp_sink, "multicast-iface", self->multi_iface, NULL);
507     g_object_set (self->rtp_sink, "multicast-iface", self->multi_iface, NULL);
508   } else {
509     const gchar *any_addr;
510 
511     if (g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV6)
512       any_addr = "::";
513     else
514       any_addr = "0.0.0.0";
515 
516     g_object_set (self->rtcp_src, "address", any_addr, "port", 0, NULL);
517   }
518   g_free (remote_addr);
519   g_object_unref (iaddr);
520 
521   return TRUE;
522 
523 dns_resolve_failed:
524   GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND,
525       ("Could not resolve hostname '%s'", gst_uri_get_host (self->uri)),
526       ("DNS resolver reported: %s", error->message));
527   g_error_free (error);
528   return FALSE;
529 }
530 
531 static GstStateChangeReturn
gst_rtp_sink_change_state(GstElement * element,GstStateChange transition)532 gst_rtp_sink_change_state (GstElement * element, GstStateChange transition)
533 {
534   GstRtpSink *self = GST_RTP_SINK (element);
535   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
536 
537   GST_DEBUG_OBJECT (self, "changing state: %s => %s",
538       gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
539       gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
540 
541   switch (transition) {
542     case GST_STATE_CHANGE_NULL_TO_READY:
543       /* Set the properties to the child elements to avoid binding to
544        * a NULL interface on a network without a default gateway */
545       if (gst_rtp_sink_start (self) == FALSE)
546         return GST_STATE_CHANGE_FAILURE;
547       break;
548     case GST_STATE_CHANGE_READY_TO_PAUSED:
549       break;
550     default:
551       break;
552   }
553 
554   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
555   if (ret == GST_STATE_CHANGE_FAILURE)
556     return ret;
557 
558   switch (transition) {
559     case GST_STATE_CHANGE_NULL_TO_READY:
560       /* re-use the sockets after they have been initialised */
561       if (gst_rtp_sink_reuse_socket (self) == FALSE)
562         return GST_STATE_CHANGE_FAILURE;
563       break;
564     case GST_STATE_CHANGE_READY_TO_PAUSED:
565       break;
566     case GST_STATE_CHANGE_PAUSED_TO_READY:
567       break;
568     default:
569       break;
570   }
571 
572   return ret;
573 }
574 
575 
576 static void
gst_rtp_sink_init(GstRtpSink * self)577 gst_rtp_sink_init (GstRtpSink * self)
578 {
579   const gchar *missing_plugin = NULL;
580   GstCaps *caps;
581 
582   self->rtpbin = NULL;
583   self->funnel_rtp = NULL;
584   self->funnel_rtcp = NULL;
585   self->rtp_sink = NULL;
586   self->rtcp_src = NULL;
587   self->rtcp_sink = NULL;
588 
589   self->uri = gst_uri_from_string (DEFAULT_PROP_URI);
590   self->ttl = DEFAULT_PROP_TTL;
591   self->ttl_mc = DEFAULT_PROP_TTL_MC;
592   self->multi_iface = g_strdup (DEFAULT_PROP_MULTICAST_IFACE);
593 
594   g_mutex_init (&self->lock);
595 
596   /* Construct the RTP sender pipeline.
597    *
598    *           *-> [send_rtp_sink_%u]   --------  [send_rtp_src_%u]  -> udpsink
599    *                                   | rtpbin |
600    * udpsrc     -> [recv_rtcp_sink_%u]  --------  [send_rtcp_src_%u] -> * udpsink
601    */
602   self->rtpbin = gst_element_factory_make ("rtpbin", "rtp_send_rtpbin0");
603   if (self->rtpbin == NULL) {
604     missing_plugin = "rtpmanager";
605     goto missing_plugin;
606   }
607 
608   gst_bin_add (GST_BIN (self), self->rtpbin);
609 
610   /* Add rtpbin callbacks to monitor the operation of rtpbin */
611   g_signal_connect_object (self->rtpbin, "element-added",
612       G_CALLBACK (gst_rtp_sink_rtpbin_element_added_cb), self, 0);
613   g_signal_connect_object (self->rtpbin, "pad-added",
614       G_CALLBACK (gst_rtp_sink_rtpbin_pad_added_cb), self, 0);
615   g_signal_connect_object (self->rtpbin, "pad-removed",
616       G_CALLBACK (gst_rtp_sink_rtpbin_pad_removed_cb), self, 0);
617 
618   GST_OBJECT_FLAG_SET (GST_OBJECT (self), GST_ELEMENT_FLAG_SINK);
619   gst_bin_set_suppressed_flags (GST_BIN (self),
620       GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
621 
622   self->funnel_rtp = gst_element_factory_make ("funnel", "rtp_rtp_funnel0");
623   if (self->funnel_rtp == NULL) {
624     missing_plugin = "funnel";
625     goto missing_plugin;
626   }
627 
628   self->funnel_rtcp = gst_element_factory_make ("funnel", "rtp_rtcp_funnel0");
629   if (self->funnel_rtcp == NULL) {
630     missing_plugin = "funnel";
631     goto missing_plugin;
632   }
633 
634   self->rtp_sink = gst_element_factory_make ("udpsink", "rtp_rtp_udpsink0");
635   if (self->rtp_sink == NULL) {
636     missing_plugin = "udp";
637     goto missing_plugin;
638   }
639 
640   self->rtcp_src = gst_element_factory_make ("udpsrc", "rtp_rtcp_udpsrc0");
641   if (self->rtcp_src == NULL) {
642     missing_plugin = "udp";
643     goto missing_plugin;
644   }
645 
646   self->rtcp_sink = gst_element_factory_make ("udpsink", "rtp_rtcp_udpsink0");
647   if (self->rtcp_sink == NULL) {
648     missing_plugin = "udp";
649     goto missing_plugin;
650   }
651 
652   gst_bin_add (GST_BIN (self), self->funnel_rtp);
653   gst_bin_add (GST_BIN (self), self->funnel_rtcp);
654 
655   gst_bin_add (GST_BIN (self), self->rtp_sink);
656   gst_bin_add (GST_BIN (self), self->rtcp_src);
657   gst_bin_add (GST_BIN (self), self->rtcp_sink);
658 
659   gst_element_set_locked_state (self->rtcp_src, TRUE);
660   gst_element_set_locked_state (self->rtcp_sink, TRUE);
661 
662   /* no need to set address if unicast */
663   caps = gst_caps_new_empty_simple ("application/x-rtcp");
664   g_object_set (self->rtcp_src, "caps", caps, NULL);
665   gst_caps_unref (caps);
666 
667   gst_element_link (self->funnel_rtp, self->rtp_sink);
668   gst_element_link (self->funnel_rtcp, self->rtcp_sink);
669 
670   if (missing_plugin == NULL)
671     return;
672 
673 missing_plugin:
674   {
675     GST_ERROR_OBJECT (self, "'%s' plugin is missing.", missing_plugin);
676     /* Just make our element valid, so we fail cleanly */
677     gst_element_add_pad (GST_ELEMENT (self),
678         gst_pad_new_from_static_template (&sink_template, "sink_%u"));
679   }
680 }
681 
682 static GstURIType
gst_rtp_sink_uri_get_type(GType type)683 gst_rtp_sink_uri_get_type (GType type)
684 {
685   return GST_URI_SINK;
686 }
687 
688 static const gchar *const *
gst_rtp_sink_uri_get_protocols(GType type)689 gst_rtp_sink_uri_get_protocols (GType type)
690 {
691   static const gchar *protocols[] = { (char *) "rtp", NULL };
692 
693   return protocols;
694 }
695 
696 static gchar *
gst_rtp_sink_uri_get_uri(GstURIHandler * handler)697 gst_rtp_sink_uri_get_uri (GstURIHandler * handler)
698 {
699   GstRtpSink *self = (GstRtpSink *) handler;
700 
701   return gst_uri_to_string (self->uri);
702 }
703 
704 static gboolean
gst_rtp_sink_uri_set_uri(GstURIHandler * handler,const gchar * uri,GError ** error)705 gst_rtp_sink_uri_set_uri (GstURIHandler * handler, const gchar * uri,
706     GError ** error)
707 {
708   GstRtpSink *self = (GstRtpSink *) handler;
709 
710   g_object_set (G_OBJECT (self), "uri", uri, NULL);
711 
712   return TRUE;
713 }
714 
715 static void
gst_rtp_sink_uri_handler_init(gpointer g_iface,gpointer iface_data)716 gst_rtp_sink_uri_handler_init (gpointer g_iface, gpointer iface_data)
717 {
718   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
719 
720   iface->get_type = gst_rtp_sink_uri_get_type;
721   iface->get_protocols = gst_rtp_sink_uri_get_protocols;
722   iface->get_uri = gst_rtp_sink_uri_get_uri;
723   iface->set_uri = gst_rtp_sink_uri_set_uri;
724 }
725 
726 /* ex: set tabstop=2 shiftwidth=2 expandtab: */
727