2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3 * Copyright (C) 2015 Centricular Ltd
4 * Author: Sebastian Dröge <sebastian@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * @short_description: A media stream
24 * @see_also: #GstRTSPMedia
26 * The #GstRTSPStream object manages the data transport for one stream. It
27 * is created from a payloader element and a source pad that produce the RTP
28 * packets for the stream.
30 * With gst_rtsp_stream_join_bin() the streaming elements are added to the bin
31 * and rtpbin. gst_rtsp_stream_leave_bin() removes the elements again.
33 * The #GstRTSPStream will use the configured addresspool, as set with
34 * gst_rtsp_stream_set_address_pool(), to allocate multicast addresses for the
35 * stream. With gst_rtsp_stream_get_multicast_address() you can get the
38 * With gst_rtsp_stream_get_server_port () you can get the port that the server
39 * will use to receive RTCP. This is the part that the clients will use to send
42 * With gst_rtsp_stream_add_transport() destinations can be added where the
43 * stream should be sent to. Use gst_rtsp_stream_remove_transport() to remove
44 * the destination again.
46 * Last reviewed on 2013-07-16 (1.0.0)
55 #include <gst/app/gstappsrc.h>
56 #include <gst/app/gstappsink.h>
58 #include <gst/rtp/gstrtpbuffer.h>
60 #include "rtsp-stream.h"
62 #define GST_RTSP_STREAM_GET_PRIVATE(obj) \
63 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
65 struct _GstRTSPStreamPrivate
69 /* Only one pad is ever set */
70 GstPad *srcpad, *sinkpad;
71 GstElement *payloader;
75 /* TRUE if this stream is running on
76 * the client side of an RTSP link (for RECORD) */
80 GstRTSPProfile profiles;
81 GstRTSPLowerTrans protocols;
83 /* pads on the rtpbin */
84 GstPad *send_rtp_sink;
89 /* the RTPSession object */
92 /* SRTP encoder/decoder */
98 GstElement *udpsrc_v4[2];
99 GstElement *udpsrc_v6[2];
100 GstElement *udpqueue[2];
101 GstElement *udpsink[2];
103 /* for UDP multicast */
104 GstElement *mcast_udpsrc_v4[2];
105 GstElement *mcast_udpsrc_v6[2];
106 GstElement *mcast_udpqueue[2];
107 GstElement *mcast_udpsink[2];
109 /* for TCP transport */
110 GstElement *appsrc[2];
111 GstClockTime appsrc_base_time[2];
112 GstElement *appqueue[2];
113 GstElement *appsink[2];
116 GstElement *funnel[2];
121 GstClockTime rtx_time;
123 /* pool used to manage unicast and multicast addresses */
124 GstRTSPAddressPool *pool;
126 /* unicast server addr/port */
127 GstRTSPAddress *server_addr_v4;
128 GstRTSPAddress *server_addr_v6;
130 /* multicast addresses */
131 GstRTSPAddress *mcast_addr_v4;
132 GstRTSPAddress *mcast_addr_v6;
134 gchar *multicast_iface;
136 /* the caps of the stream */
140 /* transports we stream to */
143 guint transports_cookie;
145 GList *tr_cache_rtcp;
146 guint tr_cache_cookie_rtp;
147 guint tr_cache_cookie_rtcp;
151 /* stream blocking */
155 /* pt->caps map for RECORD streams */
158 GstRTSPPublishClockMode publish_clock_mode;
161 #define DEFAULT_CONTROL NULL
162 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
163 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
164 GST_RTSP_LOWER_TRANS_TCP
177 SIGNAL_NEW_RTP_ENCODER,
178 SIGNAL_NEW_RTCP_ENCODER,
182 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
183 #define GST_CAT_DEFAULT rtsp_stream_debug
185 static GQuark ssrc_stream_map_key;
187 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
188 GValue * value, GParamSpec * pspec);
189 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
190 const GValue * value, GParamSpec * pspec);
192 static void gst_rtsp_stream_finalize (GObject * obj);
194 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
196 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
199 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
201 GObjectClass *gobject_class;
203 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
205 gobject_class = G_OBJECT_CLASS (klass);
207 gobject_class->get_property = gst_rtsp_stream_get_property;
208 gobject_class->set_property = gst_rtsp_stream_set_property;
209 gobject_class->finalize = gst_rtsp_stream_finalize;
211 g_object_class_install_property (gobject_class, PROP_CONTROL,
212 g_param_spec_string ("control", "Control",
213 "The control string for this stream", DEFAULT_CONTROL,
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_PROFILES,
217 g_param_spec_flags ("profiles", "Profiles",
218 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
219 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
222 g_param_spec_flags ("protocols", "Protocols",
223 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
224 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
227 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
228 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
229 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
231 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
232 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
233 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
234 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
236 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
238 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
242 gst_rtsp_stream_init (GstRTSPStream * stream)
244 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
246 GST_DEBUG ("new stream %p", stream);
251 priv->control = g_strdup (DEFAULT_CONTROL);
252 priv->profiles = DEFAULT_PROFILES;
253 priv->protocols = DEFAULT_PROTOCOLS;
254 priv->publish_clock_mode = GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK;
256 g_mutex_init (&priv->lock);
258 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
259 NULL, (GDestroyNotify) gst_caps_unref);
260 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
261 (GDestroyNotify) gst_caps_unref);
265 gst_rtsp_stream_finalize (GObject * obj)
267 GstRTSPStream *stream;
268 GstRTSPStreamPrivate *priv;
270 stream = GST_RTSP_STREAM (obj);
273 GST_DEBUG ("finalize stream %p", stream);
275 /* we really need to be unjoined now */
276 g_return_if_fail (priv->joined_bin == NULL);
278 if (priv->mcast_addr_v4)
279 gst_rtsp_address_free (priv->mcast_addr_v4);
280 if (priv->mcast_addr_v6)
281 gst_rtsp_address_free (priv->mcast_addr_v6);
282 if (priv->server_addr_v4)
283 gst_rtsp_address_free (priv->server_addr_v4);
284 if (priv->server_addr_v6)
285 gst_rtsp_address_free (priv->server_addr_v6);
287 g_object_unref (priv->pool);
289 g_object_unref (priv->rtxsend);
291 g_free (priv->multicast_iface);
293 gst_object_unref (priv->payloader);
295 gst_object_unref (priv->srcpad);
297 gst_object_unref (priv->sinkpad);
298 g_free (priv->control);
299 g_mutex_clear (&priv->lock);
301 g_hash_table_unref (priv->keys);
302 g_hash_table_destroy (priv->ptmap);
304 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
308 gst_rtsp_stream_get_property (GObject * object, guint propid,
309 GValue * value, GParamSpec * pspec)
311 GstRTSPStream *stream = GST_RTSP_STREAM (object);
315 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
318 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
321 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
329 gst_rtsp_stream_set_property (GObject * object, guint propid,
330 const GValue * value, GParamSpec * pspec)
332 GstRTSPStream *stream = GST_RTSP_STREAM (object);
336 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
339 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
342 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
350 * gst_rtsp_stream_new:
353 * @payloader: a #GstElement
355 * Create a new media stream with index @idx that handles RTP data on
356 * @pad and has a payloader element @payloader if @pad is a source pad
357 * or a depayloader element @payloader if @pad is a sink pad.
359 * Returns: (transfer full): a new #GstRTSPStream
362 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
364 GstRTSPStreamPrivate *priv;
365 GstRTSPStream *stream;
367 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
368 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
370 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
373 priv->payloader = gst_object_ref (payloader);
374 if (GST_PAD_IS_SRC (pad))
375 priv->srcpad = gst_object_ref (pad);
377 priv->sinkpad = gst_object_ref (pad);
383 * gst_rtsp_stream_get_index:
384 * @stream: a #GstRTSPStream
386 * Get the stream index.
388 * Return: the stream index.
391 gst_rtsp_stream_get_index (GstRTSPStream * stream)
393 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
395 return stream->priv->idx;
399 * gst_rtsp_stream_get_pt:
400 * @stream: a #GstRTSPStream
402 * Get the stream payload type.
404 * Return: the stream payload type.
407 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
409 GstRTSPStreamPrivate *priv;
412 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
416 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
422 * gst_rtsp_stream_get_srcpad:
423 * @stream: a #GstRTSPStream
425 * Get the srcpad associated with @stream.
427 * Returns: (transfer full): the srcpad. Unref after usage.
430 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
432 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
434 if (!stream->priv->srcpad)
437 return gst_object_ref (stream->priv->srcpad);
441 * gst_rtsp_stream_get_sinkpad:
442 * @stream: a #GstRTSPStream
444 * Get the sinkpad associated with @stream.
446 * Returns: (transfer full): the sinkpad. Unref after usage.
449 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
451 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
453 if (!stream->priv->sinkpad)
456 return gst_object_ref (stream->priv->sinkpad);
460 * gst_rtsp_stream_get_control:
461 * @stream: a #GstRTSPStream
463 * Get the control string to identify this stream.
465 * Returns: (transfer full): the control string. g_free() after usage.
468 gst_rtsp_stream_get_control (GstRTSPStream * stream)
470 GstRTSPStreamPrivate *priv;
473 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
477 g_mutex_lock (&priv->lock);
478 if ((result = g_strdup (priv->control)) == NULL)
479 result = g_strdup_printf ("stream=%u", priv->idx);
480 g_mutex_unlock (&priv->lock);
486 * gst_rtsp_stream_set_control:
487 * @stream: a #GstRTSPStream
488 * @control: a control string
490 * Set the control string in @stream.
493 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
495 GstRTSPStreamPrivate *priv;
497 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
501 g_mutex_lock (&priv->lock);
502 g_free (priv->control);
503 priv->control = g_strdup (control);
504 g_mutex_unlock (&priv->lock);
508 * gst_rtsp_stream_has_control:
509 * @stream: a #GstRTSPStream
510 * @control: a control string
512 * Check if @stream has the control string @control.
514 * Returns: %TRUE is @stream has @control as the control string
517 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
519 GstRTSPStreamPrivate *priv;
522 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
526 g_mutex_lock (&priv->lock);
528 res = (g_strcmp0 (priv->control, control) == 0);
532 if (sscanf (control, "stream=%u", &streamid) > 0)
533 res = (streamid == priv->idx);
537 g_mutex_unlock (&priv->lock);
543 * gst_rtsp_stream_set_mtu:
544 * @stream: a #GstRTSPStream
547 * Configure the mtu in the payloader of @stream to @mtu.
550 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
552 GstRTSPStreamPrivate *priv;
554 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
558 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
560 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
564 * gst_rtsp_stream_get_mtu:
565 * @stream: a #GstRTSPStream
567 * Get the configured MTU in the payloader of @stream.
569 * Returns: the MTU of the payloader.
572 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
574 GstRTSPStreamPrivate *priv;
577 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
581 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
586 /* Update the dscp qos property on the udp sinks */
588 update_dscp_qos (GstRTSPStream * stream, GstElement * udpsink[2])
590 GstRTSPStreamPrivate *priv;
595 g_object_set (G_OBJECT (udpsink[0]), "qos-dscp", priv->dscp_qos, NULL);
599 g_object_set (G_OBJECT (udpsink[1]), "qos-dscp", priv->dscp_qos, NULL);
604 * gst_rtsp_stream_set_dscp_qos:
605 * @stream: a #GstRTSPStream
606 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
608 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
611 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
613 GstRTSPStreamPrivate *priv;
615 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
619 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
621 if (dscp_qos < -1 || dscp_qos > 63) {
622 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
626 priv->dscp_qos = dscp_qos;
628 update_dscp_qos (stream, priv->udpsink);
632 * gst_rtsp_stream_get_dscp_qos:
633 * @stream: a #GstRTSPStream
635 * Get the configured DSCP QoS in of the outgoing sockets.
637 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
640 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
642 GstRTSPStreamPrivate *priv;
644 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
648 return priv->dscp_qos;
652 * gst_rtsp_stream_is_transport_supported:
653 * @stream: a #GstRTSPStream
654 * @transport: (transfer none): a #GstRTSPTransport
656 * Check if @transport can be handled by stream
658 * Returns: %TRUE if @transport can be handled by @stream.
661 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
662 GstRTSPTransport * transport)
664 GstRTSPStreamPrivate *priv;
666 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
670 g_mutex_lock (&priv->lock);
671 if (transport->trans != GST_RTSP_TRANS_RTP)
672 goto unsupported_transmode;
674 if (!(transport->profile & priv->profiles))
675 goto unsupported_profile;
677 if (!(transport->lower_transport & priv->protocols))
678 goto unsupported_ltrans;
680 g_mutex_unlock (&priv->lock);
685 unsupported_transmode:
687 GST_DEBUG ("unsupported transport mode %d", transport->trans);
688 g_mutex_unlock (&priv->lock);
693 GST_DEBUG ("unsupported profile %d", transport->profile);
694 g_mutex_unlock (&priv->lock);
699 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
700 g_mutex_unlock (&priv->lock);
706 * gst_rtsp_stream_set_profiles:
707 * @stream: a #GstRTSPStream
708 * @profiles: the new profiles
710 * Configure the allowed profiles for @stream.
713 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
715 GstRTSPStreamPrivate *priv;
717 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
721 g_mutex_lock (&priv->lock);
722 priv->profiles = profiles;
723 g_mutex_unlock (&priv->lock);
727 * gst_rtsp_stream_get_profiles:
728 * @stream: a #GstRTSPStream
730 * Get the allowed profiles of @stream.
732 * Returns: a #GstRTSPProfile
735 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
737 GstRTSPStreamPrivate *priv;
740 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
744 g_mutex_lock (&priv->lock);
745 res = priv->profiles;
746 g_mutex_unlock (&priv->lock);
752 * gst_rtsp_stream_set_protocols:
753 * @stream: a #GstRTSPStream
754 * @protocols: the new flags
756 * Configure the allowed lower transport for @stream.
759 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
760 GstRTSPLowerTrans protocols)
762 GstRTSPStreamPrivate *priv;
764 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
768 g_mutex_lock (&priv->lock);
769 priv->protocols = protocols;
770 g_mutex_unlock (&priv->lock);
774 * gst_rtsp_stream_get_protocols:
775 * @stream: a #GstRTSPStream
777 * Get the allowed protocols of @stream.
779 * Returns: a #GstRTSPLowerTrans
782 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
784 GstRTSPStreamPrivate *priv;
785 GstRTSPLowerTrans res;
787 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
788 GST_RTSP_LOWER_TRANS_UNKNOWN);
792 g_mutex_lock (&priv->lock);
793 res = priv->protocols;
794 g_mutex_unlock (&priv->lock);
800 * gst_rtsp_stream_set_address_pool:
801 * @stream: a #GstRTSPStream
802 * @pool: (transfer none): a #GstRTSPAddressPool
804 * configure @pool to be used as the address pool of @stream.
807 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
808 GstRTSPAddressPool * pool)
810 GstRTSPStreamPrivate *priv;
811 GstRTSPAddressPool *old;
813 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
817 GST_LOG_OBJECT (stream, "set address pool %p", pool);
819 g_mutex_lock (&priv->lock);
820 if ((old = priv->pool) != pool)
821 priv->pool = pool ? g_object_ref (pool) : NULL;
824 g_mutex_unlock (&priv->lock);
827 g_object_unref (old);
831 * gst_rtsp_stream_get_address_pool:
832 * @stream: a #GstRTSPStream
834 * Get the #GstRTSPAddressPool used as the address pool of @stream.
836 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
840 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
842 GstRTSPStreamPrivate *priv;
843 GstRTSPAddressPool *result;
845 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
849 g_mutex_lock (&priv->lock);
850 if ((result = priv->pool))
851 g_object_ref (result);
852 g_mutex_unlock (&priv->lock);
858 * gst_rtsp_stream_set_multicast_iface:
859 * @stream: a #GstRTSPStream
860 * @multicast_iface: (transfer none): a multicast interface
862 * configure @multicast_iface to be used for @stream.
865 gst_rtsp_stream_set_multicast_iface (GstRTSPStream * stream,
866 const gchar * multicast_iface)
868 GstRTSPStreamPrivate *priv;
871 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
875 GST_LOG_OBJECT (stream, "set multicast iface %s",
876 GST_STR_NULL (multicast_iface));
878 g_mutex_lock (&priv->lock);
879 if ((old = priv->multicast_iface) != multicast_iface)
880 priv->multicast_iface = multicast_iface ? g_strdup (multicast_iface) : NULL;
883 g_mutex_unlock (&priv->lock);
890 * gst_rtsp_stream_get_multicast_iface:
891 * @stream: a #GstRTSPStream
893 * Get the multicast interface used for @stream.
895 * Returns: (transfer full): the multicast interface for @stream. g_free() after
899 gst_rtsp_stream_get_multicast_iface (GstRTSPStream * stream)
901 GstRTSPStreamPrivate *priv;
904 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
908 g_mutex_lock (&priv->lock);
909 if ((result = priv->multicast_iface))
910 result = g_strdup (result);
911 g_mutex_unlock (&priv->lock);
917 static GstRTSPAddress *
918 gst_rtsp_stream_get_multicast_address_locked (GstRTSPStream * stream,
919 GSocketFamily family)
921 GstRTSPStreamPrivate *priv;
922 GstRTSPAddress *result;
923 GstRTSPAddress **addrp;
924 GstRTSPAddressFlags flags;
928 if (family == G_SOCKET_FAMILY_IPV6) {
929 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
930 addrp = &priv->mcast_addr_v6;
932 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
933 addrp = &priv->mcast_addr_v4;
936 if (*addrp == NULL) {
937 if (priv->pool == NULL)
940 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
942 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
946 /* FIXME: Also reserve the same port with unicast ANY address, since that's
947 * where we are going to bind our socket. Probably loop until we find a port
948 * available in both mcast and unicast pools. Maybe GstRTSPAddressPool
949 * should do it for us when both GST_RTSP_ADDRESS_FLAG_MULTICAST and
950 * GST_RTSP_ADDRESS_FLAG_UNICAST are givent. */
952 result = gst_rtsp_address_copy (*addrp);
959 GST_ERROR_OBJECT (stream, "no address pool specified");
964 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
970 * gst_rtsp_stream_get_multicast_address:
971 * @stream: a #GstRTSPStream
972 * @family: the #GSocketFamily
974 * Get the multicast address of @stream for @family. The original
975 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
976 * won't release the address from the pool.
978 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
979 * or %NULL when no address could be allocated. gst_rtsp_address_free()
983 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
984 GSocketFamily family)
986 GstRTSPAddress *result;
988 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
990 g_mutex_lock (&stream->priv->lock);
991 result = gst_rtsp_stream_get_multicast_address_locked (stream, family);
992 g_mutex_unlock (&stream->priv->lock);
998 * gst_rtsp_stream_reserve_address:
999 * @stream: a #GstRTSPStream
1000 * @address: an address
1005 * Reserve @address and @port as the address and port of @stream. The original
1006 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
1007 * won't release the address from the pool.
1009 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
1010 * the address could be reserved. gst_rtsp_address_free() after usage.
1013 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
1014 const gchar * address, guint port, guint n_ports, guint ttl)
1016 GstRTSPStreamPrivate *priv;
1017 GstRTSPAddress *result;
1019 GSocketFamily family;
1020 GstRTSPAddress **addrp;
1022 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1023 g_return_val_if_fail (address != NULL, NULL);
1024 g_return_val_if_fail (port > 0, NULL);
1025 g_return_val_if_fail (n_ports > 0, NULL);
1026 g_return_val_if_fail (ttl > 0, NULL);
1028 priv = stream->priv;
1030 addr = g_inet_address_new_from_string (address);
1032 GST_ERROR ("failed to get inet addr from %s", address);
1033 family = G_SOCKET_FAMILY_IPV4;
1035 family = g_inet_address_get_family (addr);
1036 g_object_unref (addr);
1039 if (family == G_SOCKET_FAMILY_IPV6)
1040 addrp = &priv->mcast_addr_v6;
1042 addrp = &priv->mcast_addr_v4;
1044 g_mutex_lock (&priv->lock);
1045 if (*addrp == NULL) {
1046 GstRTSPAddressPoolResult res;
1048 if (priv->pool == NULL)
1051 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
1052 port, n_ports, ttl, addrp);
1053 if (res != GST_RTSP_ADDRESS_POOL_OK)
1056 /* FIXME: Also reserve the same port with unicast ANY address, since that's
1057 * where we are going to bind our socket. */
1059 if (g_ascii_strcasecmp ((*addrp)->address, address) ||
1060 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
1061 (*addrp)->ttl != ttl)
1062 goto different_address;
1064 result = gst_rtsp_address_copy (*addrp);
1065 g_mutex_unlock (&priv->lock);
1072 GST_ERROR_OBJECT (stream, "no address pool specified");
1073 g_mutex_unlock (&priv->lock);
1078 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1080 g_mutex_unlock (&priv->lock);
1085 GST_ERROR_OBJECT (stream,
1086 "address %s is not the same as %s that was already" " reserved",
1087 address, (*addrp)->address);
1088 g_mutex_unlock (&priv->lock);
1093 /* must be called with lock */
1095 set_sockets_for_udpsinks (GstElement * udpsink[2], GSocket * rtp_socket,
1096 GSocket * rtcp_socket, GSocketFamily family)
1098 const gchar *multisink_socket;
1100 if (family == G_SOCKET_FAMILY_IPV6)
1101 multisink_socket = "socket-v6";
1103 multisink_socket = "socket";
1105 g_object_set (G_OBJECT (udpsink[0]), multisink_socket, rtp_socket, NULL);
1106 g_object_set (G_OBJECT (udpsink[1]), multisink_socket, rtcp_socket, NULL);
1110 create_and_configure_udpsinks (GstRTSPStream * stream, GstElement * udpsink[2])
1112 GstRTSPStreamPrivate *priv = stream->priv;
1113 GstElement *udpsink0, *udpsink1;
1115 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1116 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1118 if (!udpsink0 || !udpsink1)
1119 goto no_udp_protocol;
1121 /* configure sinks */
1123 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1124 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1126 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1127 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1129 g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
1131 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1132 /* Needs to be async for RECORD streams, otherwise we will never go to
1133 * PLAYING because the sinks will wait for data while the udpsrc can't
1134 * provide data with timestamps in PAUSED. */
1136 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1137 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1139 /* join multicast group when adding clients, so we'll start receiving from it.
1140 * We cannot rely on the udpsrc to join the group since its socket is always a
1141 * local unicast one. */
1142 g_object_set (G_OBJECT (udpsink0), "auto-multicast", TRUE, NULL);
1143 g_object_set (G_OBJECT (udpsink1), "auto-multicast", TRUE, NULL);
1145 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1146 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1148 udpsink[0] = udpsink0;
1149 udpsink[1] = udpsink1;
1151 /* update the dscp qos field in the sinks */
1152 update_dscp_qos (stream, udpsink);
1163 /* must be called with lock */
1165 create_and_configure_udpsources (GstElement * udpsrc_out[2],
1166 GSocket * rtp_socket, GSocket * rtcp_socket)
1168 GstStateChangeReturn ret;
1170 udpsrc_out[0] = gst_element_factory_make ("udpsrc", NULL);
1171 udpsrc_out[1] = gst_element_factory_make ("udpsrc", NULL);
1173 if (udpsrc_out[0] == NULL || udpsrc_out[1] == NULL)
1176 g_object_set (G_OBJECT (udpsrc_out[0]), "socket", rtp_socket, NULL);
1177 g_object_set (G_OBJECT (udpsrc_out[1]), "socket", rtcp_socket, NULL);
1179 /* The udpsrc cannot do the join because its socket is always a local unicast
1180 * one. The udpsink sharing the same socket will do it for us. */
1181 g_object_set (G_OBJECT (udpsrc_out[0]), "auto-multicast", FALSE, NULL);
1182 g_object_set (G_OBJECT (udpsrc_out[1]), "auto-multicast", FALSE, NULL);
1184 g_object_set (G_OBJECT (udpsrc_out[0]), "loop", FALSE, NULL);
1185 g_object_set (G_OBJECT (udpsrc_out[1]), "loop", FALSE, NULL);
1187 ret = gst_element_set_state (udpsrc_out[0], GST_STATE_READY);
1188 if (ret == GST_STATE_CHANGE_FAILURE)
1190 ret = gst_element_set_state (udpsrc_out[1], GST_STATE_READY);
1191 if (ret == GST_STATE_CHANGE_FAILURE)
1199 if (udpsrc_out[0]) {
1200 gst_element_set_state (udpsrc_out[0], GST_STATE_NULL);
1201 g_clear_object (&udpsrc_out[0]);
1203 if (udpsrc_out[1]) {
1204 gst_element_set_state (udpsrc_out[1], GST_STATE_NULL);
1205 g_clear_object (&udpsrc_out[1]);
1212 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1213 GstElement * udpsrc_out[2], GstElement * udpsink_out[2],
1214 GstRTSPAddress ** server_addr_out, gboolean multicast)
1216 GstRTSPStreamPrivate *priv = stream->priv;
1217 GSocket *rtp_socket = NULL;
1218 GSocket *rtcp_socket;
1219 gint tmp_rtp, tmp_rtcp;
1221 gint rtpport, rtcpport;
1222 GList *rejected_addresses = NULL;
1223 GstRTSPAddress *addr = NULL;
1224 GInetAddress *inetaddr = NULL;
1226 GSocketAddress *rtp_sockaddr = NULL;
1227 GSocketAddress *rtcp_sockaddr = NULL;
1228 GstRTSPAddressPool *pool;
1230 g_assert (!udpsrc_out[0]);
1231 g_assert (!udpsrc_out[1]);
1232 g_assert ((!udpsink_out[0] && !udpsink_out[1]) ||
1233 (udpsink_out[0] && udpsink_out[1]));
1234 g_assert (*server_addr_out == NULL);
1239 /* Start with random port */
1242 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1243 G_SOCKET_PROTOCOL_UDP, NULL);
1245 goto no_udp_protocol;
1246 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1248 /* try to allocate 2 UDP ports, the RTP port should be an even
1249 * number and the RTCP port should be the next (uneven) port */
1252 if (rtp_socket == NULL) {
1253 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1254 G_SOCKET_PROTOCOL_UDP, NULL);
1256 goto no_udp_protocol;
1257 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1260 if ((pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) || multicast) {
1261 GstRTSPAddressFlags flags;
1264 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1269 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1271 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1273 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1275 if (family == G_SOCKET_FAMILY_IPV6)
1276 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1278 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1280 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1285 tmp_rtp = addr->port;
1287 g_clear_object (&inetaddr);
1289 inetaddr = g_inet_address_new_any (family);
1291 inetaddr = g_inet_address_new_from_string (addr->address);
1299 if (inetaddr == NULL)
1300 inetaddr = g_inet_address_new_any (family);
1303 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1304 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1305 g_object_unref (rtp_sockaddr);
1308 g_object_unref (rtp_sockaddr);
1310 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1311 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1312 g_clear_object (&rtp_sockaddr);
1317 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1318 g_object_unref (rtp_sockaddr);
1320 /* check if port is even */
1321 if ((tmp_rtp & 1) != 0) {
1322 /* port not even, close and allocate another */
1324 g_clear_object (&rtp_socket);
1329 tmp_rtcp = tmp_rtp + 1;
1331 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1332 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1333 g_object_unref (rtcp_sockaddr);
1334 g_clear_object (&rtp_socket);
1337 g_object_unref (rtcp_sockaddr);
1340 addr = g_slice_new0 (GstRTSPAddress);
1341 addr->address = g_inet_address_to_string (inetaddr);
1342 addr->port = tmp_rtp;
1346 addr_str = addr->address;
1347 g_clear_object (&inetaddr);
1349 if (!create_and_configure_udpsources (udpsrc_out, rtp_socket, rtcp_socket)) {
1350 goto no_udp_protocol;
1353 g_object_get (G_OBJECT (udpsrc_out[0]), "port", &rtpport, NULL);
1354 g_object_get (G_OBJECT (udpsrc_out[1]), "port", &rtcpport, NULL);
1356 /* this should not happen... */
1357 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1360 /* This function is called twice (for v4 and v6) but we create only one pair
1363 && !create_and_configure_udpsinks (stream, udpsink_out))
1364 goto no_udp_protocol;
1367 g_object_set (G_OBJECT (udpsink_out[0]), "multicast-iface",
1368 priv->multicast_iface, NULL);
1369 g_object_set (G_OBJECT (udpsink_out[1]), "multicast-iface",
1370 priv->multicast_iface, NULL);
1372 g_signal_emit_by_name (udpsink_out[0], "add", addr_str, rtpport, NULL);
1373 g_signal_emit_by_name (udpsink_out[1], "add", addr_str, rtcpport, NULL);
1376 set_sockets_for_udpsinks (udpsink_out, rtp_socket, rtcp_socket, family);
1378 *server_addr_out = addr;
1380 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1382 g_object_unref (rtp_socket);
1383 g_object_unref (rtcp_socket);
1407 g_object_unref (inetaddr);
1408 g_list_free_full (rejected_addresses,
1409 (GDestroyNotify) gst_rtsp_address_free);
1411 gst_rtsp_address_free (addr);
1413 g_object_unref (rtp_socket);
1415 g_object_unref (rtcp_socket);
1421 * gst_rtsp_stream_allocate_udp_sockets:
1422 * @stream: a #GstRTSPStream
1423 * @family: protocol family
1424 * @transport_method: transport method
1426 * Allocates RTP and RTCP ports.
1428 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1429 * Deprecated: This function shouldn't have been made public
1432 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1433 GSocketFamily family, GstRTSPTransport * ct, gboolean use_client_settings)
1435 g_warn_if_reached ();
1440 * gst_rtsp_stream_set_client_side:
1441 * @stream: a #GstRTSPStream
1442 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1443 * an RTSP connection.
1445 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1446 * streams to an RTSP server via RECORD. This has the practical effect
1447 * of changing which UDP port numbers are used when setting up the local
1448 * side of the stream sending to be either the 'server' or 'client' pair
1449 * of a configured UDP transport.
1452 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1454 GstRTSPStreamPrivate *priv;
1456 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1457 priv = stream->priv;
1458 g_mutex_lock (&priv->lock);
1459 priv->client_side = client_side;
1460 g_mutex_unlock (&priv->lock);
1464 * gst_rtsp_stream_is_client_side:
1465 * @stream: a #GstRTSPStream
1467 * See gst_rtsp_stream_set_client_side()
1469 * Returns: TRUE if this #GstRTSPStream is client-side.
1472 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1474 GstRTSPStreamPrivate *priv;
1477 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1479 priv = stream->priv;
1480 g_mutex_lock (&priv->lock);
1481 ret = priv->client_side;
1482 g_mutex_unlock (&priv->lock);
1487 /* must be called with lock */
1489 alloc_ports (GstRTSPStream * stream)
1491 GstRTSPStreamPrivate *priv = stream->priv;
1492 gboolean ret = TRUE;
1494 if (priv->protocols & GST_RTSP_LOWER_TRANS_UDP) {
1495 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1496 priv->udpsrc_v4, priv->udpsink, &priv->server_addr_v4, FALSE);
1498 ret |= alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1499 priv->udpsrc_v6, priv->udpsink, &priv->server_addr_v6, FALSE);
1502 /* FIXME: Maybe actually consider the return values? */
1503 if (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1504 ret |= alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1505 priv->mcast_udpsrc_v4, priv->mcast_udpsink, &priv->mcast_addr_v4, TRUE);
1507 ret |= alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1508 priv->mcast_udpsrc_v6, priv->mcast_udpsink, &priv->mcast_addr_v6, TRUE);
1515 * gst_rtsp_stream_get_server_port:
1516 * @stream: a #GstRTSPStream
1517 * @server_port: (out): result server port
1518 * @family: the port family to get
1520 * Fill @server_port with the port pair used by the server. This function can
1521 * only be called when @stream has been joined.
1524 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1525 GstRTSPRange * server_port, GSocketFamily family)
1527 GstRTSPStreamPrivate *priv;
1529 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1530 priv = stream->priv;
1531 g_return_if_fail (priv->joined_bin != NULL);
1533 g_mutex_lock (&priv->lock);
1534 if (family == G_SOCKET_FAMILY_IPV4) {
1536 server_port->min = priv->server_addr_v4->port;
1538 priv->server_addr_v4->port + priv->server_addr_v4->n_ports - 1;
1542 server_port->min = priv->server_addr_v6->port;
1544 priv->server_addr_v6->port + priv->server_addr_v6->n_ports - 1;
1547 g_mutex_unlock (&priv->lock);
1551 * gst_rtsp_stream_get_rtpsession:
1552 * @stream: a #GstRTSPStream
1554 * Get the RTP session of this stream.
1556 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1559 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1561 GstRTSPStreamPrivate *priv;
1564 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1566 priv = stream->priv;
1568 g_mutex_lock (&priv->lock);
1569 if ((session = priv->session))
1570 g_object_ref (session);
1571 g_mutex_unlock (&priv->lock);
1577 * gst_rtsp_stream_get_encoder:
1578 * @stream: a #GstRTSPStream
1580 * Get the SRTP encoder for this stream.
1582 * Returns: (transfer full): The SRTP encoder for this stream. Unref after usage.
1585 gst_rtsp_stream_get_srtp_encoder (GstRTSPStream * stream)
1587 GstRTSPStreamPrivate *priv;
1588 GstElement *encoder;
1590 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1592 priv = stream->priv;
1594 g_mutex_lock (&priv->lock);
1595 if ((encoder = priv->srtpenc))
1596 g_object_ref (encoder);
1597 g_mutex_unlock (&priv->lock);
1603 * gst_rtsp_stream_get_ssrc:
1604 * @stream: a #GstRTSPStream
1605 * @ssrc: (out): result ssrc
1607 * Get the SSRC used by the RTP session of this stream. This function can only
1608 * be called when @stream has been joined.
1611 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1613 GstRTSPStreamPrivate *priv;
1615 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1616 priv = stream->priv;
1617 g_return_if_fail (priv->joined_bin != NULL);
1619 g_mutex_lock (&priv->lock);
1620 if (ssrc && priv->session)
1621 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1622 g_mutex_unlock (&priv->lock);
1626 * gst_rtsp_stream_set_retransmission_time:
1627 * @stream: a #GstRTSPStream
1628 * @time: a #GstClockTime
1630 * Set the amount of time to store retransmission packets.
1633 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1636 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1638 g_mutex_lock (&stream->priv->lock);
1639 stream->priv->rtx_time = time;
1640 if (stream->priv->rtxsend)
1641 g_object_set (stream->priv->rtxsend, "max-size-time",
1642 GST_TIME_AS_MSECONDS (time), NULL);
1643 g_mutex_unlock (&stream->priv->lock);
1647 * gst_rtsp_stream_get_retransmission_time:
1648 * @stream: a #GstRTSPStream
1650 * Get the amount of time to store retransmission data.
1652 * Returns: the amount of time to store retransmission data.
1655 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1659 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1661 g_mutex_lock (&stream->priv->lock);
1662 ret = stream->priv->rtx_time;
1663 g_mutex_unlock (&stream->priv->lock);
1669 * gst_rtsp_stream_set_retransmission_pt:
1670 * @stream: a #GstRTSPStream
1673 * Set the payload type (pt) for retransmission of this stream.
1676 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1678 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1680 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1682 g_mutex_lock (&stream->priv->lock);
1683 stream->priv->rtx_pt = rtx_pt;
1684 if (stream->priv->rtxsend) {
1685 guint pt = gst_rtsp_stream_get_pt (stream);
1686 gchar *pt_s = g_strdup_printf ("%d", pt);
1687 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1688 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1689 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1691 gst_structure_free (rtx_pt_map);
1693 g_mutex_unlock (&stream->priv->lock);
1697 * gst_rtsp_stream_get_retransmission_pt:
1698 * @stream: a #GstRTSPStream
1700 * Get the payload-type used for retransmission of this stream
1702 * Returns: The retransmission PT.
1705 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1709 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1711 g_mutex_lock (&stream->priv->lock);
1712 rtx_pt = stream->priv->rtx_pt;
1713 g_mutex_unlock (&stream->priv->lock);
1719 * gst_rtsp_stream_set_buffer_size:
1720 * @stream: a #GstRTSPStream
1721 * @size: the buffer size
1723 * Set the size of the UDP transmission buffer (in bytes)
1724 * Needs to be set before the stream is joined to a bin.
1729 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1731 g_mutex_lock (&stream->priv->lock);
1732 stream->priv->buffer_size = size;
1733 g_mutex_unlock (&stream->priv->lock);
1737 * gst_rtsp_stream_get_buffer_size:
1738 * @stream: a #GstRTSPStream
1740 * Get the size of the UDP transmission buffer (in bytes)
1742 * Returns: the size of the UDP TX buffer
1747 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1751 g_mutex_lock (&stream->priv->lock);
1752 buffer_size = stream->priv->buffer_size;
1753 g_mutex_unlock (&stream->priv->lock);
1758 /* executed from streaming thread */
1760 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1762 GstRTSPStreamPrivate *priv = stream->priv;
1763 GstCaps *newcaps, *oldcaps;
1765 newcaps = gst_pad_get_current_caps (pad);
1767 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1770 g_mutex_lock (&priv->lock);
1771 oldcaps = priv->caps;
1772 priv->caps = newcaps;
1773 g_mutex_unlock (&priv->lock);
1776 gst_caps_unref (oldcaps);
1780 dump_structure (const GstStructure * s)
1784 sstr = gst_structure_to_string (s);
1785 GST_INFO ("structure: %s", sstr);
1789 static GstRTSPStreamTransport *
1790 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1792 GstRTSPStreamPrivate *priv = stream->priv;
1794 GstRTSPStreamTransport *result = NULL;
1799 if (rtcp_from == NULL)
1802 tmp = g_strrstr (rtcp_from, ":");
1806 port = atoi (tmp + 1);
1807 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1809 g_mutex_lock (&priv->lock);
1810 GST_INFO ("finding %s:%d in %d transports", dest, port,
1811 g_list_length (priv->transports));
1813 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1814 GstRTSPStreamTransport *trans = walk->data;
1815 const GstRTSPTransport *tr;
1818 tr = gst_rtsp_stream_transport_get_transport (trans);
1820 if (priv->client_side) {
1821 /* In client side mode the 'destination' is the RTSP server, so send
1823 min = tr->server_port.min;
1824 max = tr->server_port.max;
1826 min = tr->client_port.min;
1827 max = tr->client_port.max;
1830 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1836 g_object_ref (result);
1837 g_mutex_unlock (&priv->lock);
1844 static GstRTSPStreamTransport *
1845 check_transport (GObject * source, GstRTSPStream * stream)
1847 GstStructure *stats;
1848 GstRTSPStreamTransport *trans;
1850 /* see if we have a stream to match with the origin of the RTCP packet */
1851 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1852 if (trans == NULL) {
1853 g_object_get (source, "stats", &stats, NULL);
1855 const gchar *rtcp_from;
1857 dump_structure (stats);
1859 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1860 if ((trans = find_transport (stream, rtcp_from))) {
1861 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1863 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1866 gst_structure_free (stats);
1874 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1876 GstRTSPStreamTransport *trans;
1878 GST_INFO ("%p: new source %p", stream, source);
1880 trans = check_transport (source, stream);
1883 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1887 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1889 GST_INFO ("%p: new SDES %p", stream, source);
1893 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1895 GstRTSPStreamTransport *trans;
1897 trans = check_transport (source, stream);
1900 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1901 gst_rtsp_stream_transport_keep_alive (trans);
1905 GstStructure *stats;
1906 g_object_get (source, "stats", &stats, NULL);
1908 dump_structure (stats);
1909 gst_structure_free (stats);
1916 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1918 GST_INFO ("%p: source %p bye", stream, source);
1922 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1924 GstRTSPStreamTransport *trans;
1926 GST_INFO ("%p: source %p bye timeout", stream, source);
1928 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1929 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1930 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1935 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1937 GstRTSPStreamTransport *trans;
1939 GST_INFO ("%p: source %p timeout", stream, source);
1941 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1942 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1943 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1948 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1950 GST_INFO ("%p: new sender source %p", stream, source);
1953 GstStructure *stats;
1954 g_object_get (source, "stats", &stats, NULL);
1956 dump_structure (stats);
1957 gst_structure_free (stats);
1964 on_sender_ssrc_active (GObject * session, GObject * source,
1965 GstRTSPStream * stream)
1969 GstStructure *stats;
1970 g_object_get (source, "stats", &stats, NULL);
1972 dump_structure (stats);
1973 gst_structure_free (stats);
1980 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1983 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1984 g_list_free (priv->tr_cache_rtp);
1985 priv->tr_cache_rtp = NULL;
1987 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1988 g_list_free (priv->tr_cache_rtcp);
1989 priv->tr_cache_rtcp = NULL;
1993 static GstFlowReturn
1994 handle_new_sample (GstAppSink * sink, gpointer user_data)
1996 GstRTSPStreamPrivate *priv;
2000 GstRTSPStream *stream;
2003 sample = gst_app_sink_pull_sample (sink);
2007 stream = (GstRTSPStream *) user_data;
2008 priv = stream->priv;
2009 buffer = gst_sample_get_buffer (sample);
2011 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
2013 g_mutex_lock (&priv->lock);
2015 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
2016 clear_tr_cache (priv, is_rtp);
2017 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2018 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2019 priv->tr_cache_rtp =
2020 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
2022 priv->tr_cache_cookie_rtp = priv->transports_cookie;
2025 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
2026 clear_tr_cache (priv, is_rtp);
2027 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2028 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2029 priv->tr_cache_rtcp =
2030 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
2032 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
2035 g_mutex_unlock (&priv->lock);
2038 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2039 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2040 gst_rtsp_stream_transport_send_rtp (tr, buffer);
2043 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2044 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2045 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
2048 gst_sample_unref (sample);
2053 static GstAppSinkCallbacks sink_cb = {
2054 NULL, /* not interested in EOS */
2055 NULL, /* not interested in preroll samples */
2060 get_rtp_encoder (GstRTSPStream * stream, guint session)
2062 GstRTSPStreamPrivate *priv = stream->priv;
2064 if (priv->srtpenc == NULL) {
2067 name = g_strdup_printf ("srtpenc_%u", session);
2068 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2071 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2073 return gst_object_ref (priv->srtpenc);
2077 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2079 GstRTSPStreamPrivate *priv = stream->priv;
2080 GstElement *oldenc, *enc;
2084 if (priv->idx != session)
2087 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2089 oldenc = priv->srtpenc;
2090 enc = get_rtp_encoder (stream, session);
2091 name = g_strdup_printf ("rtp_sink_%d", session);
2092 pad = gst_element_get_request_pad (enc, name);
2094 gst_object_unref (pad);
2097 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2104 request_rtcp_encoder (GstElement * rtpbin, guint session,
2105 GstRTSPStream * stream)
2107 GstRTSPStreamPrivate *priv = stream->priv;
2108 GstElement *oldenc, *enc;
2112 if (priv->idx != session)
2115 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2117 oldenc = priv->srtpenc;
2118 enc = get_rtp_encoder (stream, session);
2119 name = g_strdup_printf ("rtcp_sink_%d", session);
2120 pad = gst_element_get_request_pad (enc, name);
2122 gst_object_unref (pad);
2125 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2132 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2134 GstRTSPStreamPrivate *priv = stream->priv;
2137 GST_DEBUG ("request key %08x", ssrc);
2139 g_mutex_lock (&priv->lock);
2140 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2141 gst_caps_ref (caps);
2142 g_mutex_unlock (&priv->lock);
2148 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2149 GstRTSPStream * stream)
2151 GstRTSPStreamPrivate *priv = stream->priv;
2153 if (priv->idx != session)
2156 if (priv->srtpdec == NULL) {
2159 name = g_strdup_printf ("srtpdec_%u", session);
2160 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2163 g_signal_connect (priv->srtpdec, "request-key",
2164 (GCallback) request_key, stream);
2166 return gst_object_ref (priv->srtpdec);
2170 * gst_rtsp_stream_request_aux_sender:
2171 * @stream: a #GstRTSPStream
2172 * @sessid: the session id
2174 * Creating a rtxsend bin
2176 * Returns: (transfer full): a #GstElement.
2181 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2185 GstStructure *pt_map;
2190 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2192 pt = gst_rtsp_stream_get_pt (stream);
2193 pt_s = g_strdup_printf ("%u", pt);
2194 rtx_pt = stream->priv->rtx_pt;
2196 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2198 bin = gst_bin_new (NULL);
2199 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2200 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2201 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2202 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2203 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2205 gst_structure_free (pt_map);
2206 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2208 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2209 name = g_strdup_printf ("src_%u", sessid);
2210 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2212 gst_object_unref (pad);
2214 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2215 name = g_strdup_printf ("sink_%u", sessid);
2216 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2218 gst_object_unref (pad);
2224 * gst_rtsp_stream_set_pt_map:
2225 * @stream: a #GstRTSPStream
2229 * Configure a pt map between @pt and @caps.
2232 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2234 GstRTSPStreamPrivate *priv = stream->priv;
2236 g_mutex_lock (&priv->lock);
2237 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2238 g_mutex_unlock (&priv->lock);
2242 * gst_rtsp_stream_set_publish_clock_mode:
2243 * @stream: a #GstRTSPStream
2244 * @mode: the clock publish mode
2246 * Sets if and how the stream clock should be published according to RFC7273.
2251 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
2252 GstRTSPPublishClockMode mode)
2254 GstRTSPStreamPrivate *priv;
2256 priv = stream->priv;
2257 g_mutex_lock (&priv->lock);
2258 priv->publish_clock_mode = mode;
2259 g_mutex_unlock (&priv->lock);
2263 * gst_rtsp_stream_get_publish_clock_mode:
2264 * @factory: a #GstRTSPStream
2266 * Gets if and how the stream clock should be published according to RFC7273.
2268 * Returns: The GstRTSPPublishClockMode
2272 GstRTSPPublishClockMode
2273 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
2275 GstRTSPStreamPrivate *priv;
2276 GstRTSPPublishClockMode ret;
2278 priv = stream->priv;
2279 g_mutex_lock (&priv->lock);
2280 ret = priv->publish_clock_mode;
2281 g_mutex_unlock (&priv->lock);
2287 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2288 GstRTSPStream * stream)
2290 GstRTSPStreamPrivate *priv = stream->priv;
2291 GstCaps *caps = NULL;
2293 g_mutex_lock (&priv->lock);
2295 if (priv->idx == session) {
2296 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2298 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2299 gst_caps_ref (caps);
2301 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2305 g_mutex_unlock (&priv->lock);
2311 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2313 GstRTSPStreamPrivate *priv = stream->priv;
2315 GstPadLinkReturn ret;
2318 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2319 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2321 name = gst_pad_get_name (pad);
2322 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2328 if (priv->idx != sessid)
2331 if (gst_pad_is_linked (priv->sinkpad)) {
2332 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2333 GST_DEBUG_PAD_NAME (priv->sinkpad));
2337 /* link the RTP pad to the session manager, it should not really fail unless
2338 * this is not really an RTP pad */
2339 ret = gst_pad_link (pad, priv->sinkpad);
2340 if (ret != GST_PAD_LINK_OK)
2342 priv->recv_rtp_src = gst_object_ref (pad);
2349 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2350 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2355 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2356 GstRTSPStream * stream)
2358 /* TODO: What to do here other than this? */
2359 GST_DEBUG ("Stream %p: Got EOS", stream);
2360 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2364 plug_sink (GstBin * bin, GstElement * tee, GstElement * sink,
2365 GstElement ** queue_out)
2371 gst_bin_add (bin, sink);
2373 *queue_out = gst_element_factory_make ("queue", NULL);
2374 g_object_set (*queue_out, "max-size-buffers", 1, "max-size-bytes", 0,
2375 "max-size-time", G_GINT64_CONSTANT (0), NULL);
2376 gst_bin_add (bin, *queue_out);
2378 /* link tee to queue */
2379 teepad = gst_element_get_request_pad (tee, "src_%u");
2380 pad = gst_element_get_static_pad (*queue_out, "sink");
2381 gst_pad_link (teepad, pad);
2382 gst_object_unref (pad);
2383 gst_object_unref (teepad);
2385 /* link queue to sink */
2386 queuepad = gst_element_get_static_pad (*queue_out, "src");
2387 pad = gst_element_get_static_pad (sink, "sink");
2388 gst_pad_link (queuepad, pad);
2389 gst_object_unref (queuepad);
2390 gst_object_unref (pad);
2393 /* must be called with lock */
2395 create_sender_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2397 GstRTSPStreamPrivate *priv;
2399 gboolean is_tcp, is_udp;
2402 priv = stream->priv;
2404 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2405 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2406 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2408 for (i = 0; i < 2; i++) {
2409 /* For the sender we create this bit of pipeline for both
2410 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2411 * we need to add a queue before appsink and udpsink to make
2412 * the pipeline not block. For the TCP case, we want to pump
2413 * client as fast as possible anyway. This pipeline is used
2414 * when both TCP and UDP are present.
2416 * .--------. .-----. .---------. .---------.
2417 * | rtpbin | | tee | | queue | | udpsink |
2418 * | send->sink src->sink src->sink |
2419 * '--------' | | '---------' '---------'
2420 * | | .---------. .---------.
2421 * | | | queue | | appsink |
2422 * | src->sink src->sink |
2423 * '-----' '---------' '---------'
2425 * When only UDP or only TCP is allowed, we skip the tee and queue
2426 * and link the udpsink (for UDP) or appsink (for TCP) directly to
2430 /* Only link the RTP send src if we're going to send RTP, link
2431 * the RTCP send src always */
2432 if (!priv->srcpad && i == 0)
2437 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2438 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2439 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2440 &sink_cb, stream, NULL);
2443 /* If we have udp always use a tee because we could have mcast clients
2444 * requesting different ports, in which case we'll have to plug more
2447 /* make tee for RTP/RTCP */
2448 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2449 gst_bin_add (bin, priv->tee[i]);
2451 /* and link to rtpbin send pad */
2452 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2453 gst_pad_link (priv->send_src[i], pad);
2454 gst_object_unref (pad);
2456 if (priv->udpsink[i])
2457 plug_sink (bin, priv->tee[i], priv->udpsink[i], &priv->udpqueue[i]);
2459 if (priv->mcast_udpsink[i])
2460 plug_sink (bin, priv->tee[i], priv->mcast_udpsink[i],
2461 &priv->mcast_udpqueue[i]);
2464 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2465 plug_sink (bin, priv->tee[i], priv->appsink[i], &priv->appqueue[i]);
2467 } else if (is_tcp) {
2468 /* only appsink needed, link it to the session */
2469 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2470 gst_pad_link (priv->send_src[i], pad);
2471 gst_object_unref (pad);
2473 /* when its only TCP, we need to set sync and preroll to FALSE
2474 * for the sink to avoid deadlock. And this is only needed for
2475 * sink used for RTCP data, not the RTP data. */
2477 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2480 /* check if we need to set to a special state */
2481 if (state != GST_STATE_NULL) {
2482 if (priv->udpsink[i])
2483 gst_element_set_state (priv->udpsink[i], state);
2484 if (priv->mcast_udpsink[i])
2485 gst_element_set_state (priv->mcast_udpsink[i], state);
2486 if (priv->appsink[i])
2487 gst_element_set_state (priv->appsink[i], state);
2488 if (priv->appqueue[i])
2489 gst_element_set_state (priv->appqueue[i], state);
2490 if (priv->udpqueue[i])
2491 gst_element_set_state (priv->udpqueue[i], state);
2492 if (priv->mcast_udpqueue[i])
2493 gst_element_set_state (priv->mcast_udpqueue[i], state);
2495 gst_element_set_state (priv->tee[i], state);
2500 /* must be called with lock */
2502 plug_src (GstRTSPStream * stream, GstBin * bin, GstElement * src,
2503 GstElement * funnel)
2505 GstRTSPStreamPrivate *priv;
2506 GstPad *pad, *selpad;
2508 priv = stream->priv;
2511 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2512 * values. This is only relevant for PLAY pipelines */
2513 gst_element_set_state (src, GST_STATE_PLAYING);
2514 gst_element_set_locked_state (src, TRUE);
2518 gst_bin_add (bin, src);
2520 /* and link to the funnel */
2521 selpad = gst_element_get_request_pad (funnel, "sink_%u");
2522 pad = gst_element_get_static_pad (src, "src");
2523 gst_pad_link (pad, selpad);
2524 gst_object_unref (pad);
2525 gst_object_unref (selpad);
2528 /* must be called with lock */
2530 create_receiver_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2532 GstRTSPStreamPrivate *priv;
2537 priv = stream->priv;
2539 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2541 for (i = 0; i < 2; i++) {
2542 /* For the receiver we create this bit of pipeline for both
2543 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2544 * and it is all funneled into the rtpbin receive pad.
2546 * .--------. .--------. .--------.
2547 * | udpsrc | | funnel | | rtpbin |
2548 * | src->sink src->sink |
2549 * '--------' | | '--------'
2553 * '--------' '--------'
2556 if (!priv->sinkpad && i == 0) {
2557 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2558 * RTCP sink always */
2562 /* make funnel for the RTP/RTCP receivers */
2563 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2564 gst_bin_add (bin, priv->funnel[i]);
2566 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2567 gst_pad_link (pad, priv->recv_sink[i]);
2568 gst_object_unref (pad);
2570 if (priv->udpsrc_v4[i])
2571 plug_src (stream, bin, priv->udpsrc_v4[i], priv->funnel[i]);
2573 if (priv->udpsrc_v6[i])
2574 plug_src (stream, bin, priv->udpsrc_v6[i], priv->funnel[i]);
2576 if (priv->mcast_udpsrc_v4[i])
2577 plug_src (stream, bin, priv->mcast_udpsrc_v4[i], priv->funnel[i]);
2579 if (priv->mcast_udpsrc_v6[i])
2580 plug_src (stream, bin, priv->mcast_udpsrc_v6[i], priv->funnel[i]);
2583 /* make and add appsrc */
2584 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2585 priv->appsrc_base_time[i] = -1;
2586 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
2588 plug_src (stream, bin, priv->appsrc[i], priv->funnel[i]);
2591 /* check if we need to set to a special state */
2592 if (state != GST_STATE_NULL) {
2593 gst_element_set_state (priv->funnel[i], state);
2599 check_mcast_part_for_transport (GstRTSPStream * stream,
2600 const GstRTSPTransport * tr)
2602 GstRTSPStreamPrivate *priv = stream->priv;
2603 GInetAddress *inetaddr;
2604 GSocketFamily family;
2605 GstRTSPAddress *mcast_addr;
2607 /* Check if it's a ipv4 or ipv6 transport */
2608 inetaddr = g_inet_address_new_from_string (tr->destination);
2609 family = g_inet_address_get_family (inetaddr);
2610 g_object_unref (inetaddr);
2612 /* Select fields corresponding to the family */
2613 if (family == G_SOCKET_FAMILY_IPV4) {
2614 mcast_addr = priv->mcast_addr_v4;
2616 mcast_addr = priv->mcast_addr_v6;
2619 /* We support only one mcast group per family, make sure this transport
2624 if (!g_str_equal (tr->destination, mcast_addr->address) ||
2625 tr->port.min != mcast_addr->port ||
2626 tr->port.max != mcast_addr->port + mcast_addr->n_ports - 1 ||
2627 tr->ttl != mcast_addr->ttl)
2634 GST_WARNING_OBJECT (stream, "Adding mcast transport, but no mcast address "
2635 "has been reserved");
2640 GST_WARNING_OBJECT (stream, "Adding mcast transport, but it doesn't match "
2641 "the reserved address");
2647 * gst_rtsp_stream_join_bin:
2648 * @stream: a #GstRTSPStream
2649 * @bin: (transfer none): a #GstBin to join
2650 * @rtpbin: (transfer none): a rtpbin element in @bin
2651 * @state: the target state of the new elements
2653 * Join the #GstBin @bin that contains the element @rtpbin.
2655 * @stream will link to @rtpbin, which must be inside @bin. The elements
2656 * added to @bin will be set to the state given in @state.
2658 * Returns: %TRUE on success.
2661 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2662 GstElement * rtpbin, GstState state)
2664 GstRTSPStreamPrivate *priv;
2667 GstPadLinkReturn ret;
2669 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2670 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2671 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2673 priv = stream->priv;
2675 g_mutex_lock (&priv->lock);
2676 if (priv->joined_bin != NULL)
2679 /* create a session with the same index as the stream */
2682 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2684 if (!alloc_ports (stream))
2687 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2688 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2690 g_signal_connect (rtpbin, "request-rtp-encoder",
2691 (GCallback) request_rtp_encoder, stream);
2692 g_signal_connect (rtpbin, "request-rtcp-encoder",
2693 (GCallback) request_rtcp_encoder, stream);
2694 g_signal_connect (rtpbin, "request-rtp-decoder",
2695 (GCallback) request_rtp_rtcp_decoder, stream);
2696 g_signal_connect (rtpbin, "request-rtcp-decoder",
2697 (GCallback) request_rtp_rtcp_decoder, stream);
2700 if (priv->sinkpad) {
2701 g_signal_connect (rtpbin, "request-pt-map",
2702 (GCallback) request_pt_map, stream);
2705 /* get pads from the RTP session element for sending and receiving
2708 /* get a pad for sending RTP */
2709 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2710 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2713 /* link the RTP pad to the session manager, it should not really fail unless
2714 * this is not really an RTP pad */
2715 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2716 if (ret != GST_PAD_LINK_OK)
2719 name = g_strdup_printf ("send_rtp_src_%u", idx);
2720 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2723 /* Need to connect our sinkpad from here */
2724 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2726 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2728 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2729 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2733 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2734 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2736 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2737 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2740 /* get the session */
2741 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2743 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2745 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2747 g_signal_connect (priv->session, "on-ssrc-active",
2748 (GCallback) on_ssrc_active, stream);
2749 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2751 g_signal_connect (priv->session, "on-bye-timeout",
2752 (GCallback) on_bye_timeout, stream);
2753 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2756 /* signal for sender ssrc */
2757 g_signal_connect (priv->session, "on-new-sender-ssrc",
2758 (GCallback) on_new_sender_ssrc, stream);
2759 g_signal_connect (priv->session, "on-sender-ssrc-active",
2760 (GCallback) on_sender_ssrc_active, stream);
2762 create_sender_part (stream, bin, state);
2763 create_receiver_part (stream, bin, state);
2766 /* be notified of caps changes */
2767 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2768 (GCallback) caps_notify, stream);
2771 priv->joined_bin = bin;
2772 g_mutex_unlock (&priv->lock);
2779 g_mutex_unlock (&priv->lock);
2784 g_mutex_unlock (&priv->lock);
2785 GST_WARNING ("failed to allocate ports %u", idx);
2790 GST_WARNING ("failed to link stream %u", idx);
2791 gst_object_unref (priv->send_rtp_sink);
2792 priv->send_rtp_sink = NULL;
2793 g_mutex_unlock (&priv->lock);
2799 clear_element (GstBin * bin, GstElement ** elementptr)
2802 gst_element_set_locked_state (*elementptr, FALSE);
2803 gst_element_set_state (*elementptr, GST_STATE_NULL);
2804 if (GST_ELEMENT_PARENT (*elementptr))
2805 gst_bin_remove (bin, *elementptr);
2807 gst_object_unref (*elementptr);
2813 * gst_rtsp_stream_leave_bin:
2814 * @stream: a #GstRTSPStream
2815 * @bin: (transfer none): a #GstBin
2816 * @rtpbin: (transfer none): a rtpbin #GstElement
2818 * Remove the elements of @stream from @bin.
2820 * Return: %TRUE on success.
2823 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2824 GstElement * rtpbin)
2826 GstRTSPStreamPrivate *priv;
2829 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2830 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2831 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2833 priv = stream->priv;
2835 g_mutex_lock (&priv->lock);
2836 if (priv->joined_bin == NULL)
2837 goto was_not_joined;
2838 if (priv->joined_bin != bin)
2841 priv->joined_bin = NULL;
2843 /* all transports must be removed by now */
2844 if (priv->transports != NULL)
2845 goto transports_not_removed;
2847 clear_tr_cache (priv, TRUE);
2848 clear_tr_cache (priv, FALSE);
2850 GST_INFO ("stream %p leaving bin", stream);
2853 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2855 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2856 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2857 gst_object_unref (priv->send_rtp_sink);
2858 priv->send_rtp_sink = NULL;
2859 } else if (priv->recv_rtp_src) {
2860 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2861 gst_object_unref (priv->recv_rtp_src);
2862 priv->recv_rtp_src = NULL;
2865 for (i = 0; i < 2; i++) {
2866 clear_element (bin, &priv->udpsrc_v4[i]);
2867 clear_element (bin, &priv->udpsrc_v6[i]);
2868 clear_element (bin, &priv->udpqueue[i]);
2869 clear_element (bin, &priv->udpsink[i]);
2871 clear_element (bin, &priv->mcast_udpsrc_v4[i]);
2872 clear_element (bin, &priv->mcast_udpsrc_v6[i]);
2873 clear_element (bin, &priv->mcast_udpqueue[i]);
2874 clear_element (bin, &priv->mcast_udpsink[i]);
2876 clear_element (bin, &priv->appsrc[i]);
2877 clear_element (bin, &priv->appqueue[i]);
2878 clear_element (bin, &priv->appsink[i]);
2880 clear_element (bin, &priv->tee[i]);
2881 clear_element (bin, &priv->funnel[i]);
2883 if (priv->sinkpad || i == 1) {
2884 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2885 gst_object_unref (priv->recv_sink[i]);
2886 priv->recv_sink[i] = NULL;
2891 gst_object_unref (priv->send_src[0]);
2892 priv->send_src[0] = NULL;
2895 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2896 gst_object_unref (priv->send_src[1]);
2897 priv->send_src[1] = NULL;
2899 g_object_unref (priv->session);
2900 priv->session = NULL;
2902 gst_caps_unref (priv->caps);
2906 gst_object_unref (priv->srtpenc);
2908 gst_object_unref (priv->srtpdec);
2910 if (priv->mcast_addr_v4)
2911 gst_rtsp_address_free (priv->mcast_addr_v4);
2912 priv->mcast_addr_v4 = NULL;
2913 if (priv->mcast_addr_v6)
2914 gst_rtsp_address_free (priv->mcast_addr_v6);
2915 priv->mcast_addr_v6 = NULL;
2916 if (priv->server_addr_v4)
2917 gst_rtsp_address_free (priv->server_addr_v4);
2918 priv->server_addr_v4 = NULL;
2919 if (priv->server_addr_v6)
2920 gst_rtsp_address_free (priv->server_addr_v6);
2921 priv->server_addr_v6 = NULL;
2923 g_mutex_unlock (&priv->lock);
2929 g_mutex_unlock (&priv->lock);
2932 transports_not_removed:
2934 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2935 g_mutex_unlock (&priv->lock);
2940 GST_ERROR_OBJECT (stream, "leaving the wrong bin");
2941 g_mutex_unlock (&priv->lock);
2947 * gst_rtsp_stream_get_joined_bin:
2948 * @stream: a #GstRTSPStream
2950 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
2952 * Return: (transfer full): the joined bin or NULL.
2955 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
2957 GstRTSPStreamPrivate *priv;
2960 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2962 priv = stream->priv;
2964 g_mutex_lock (&priv->lock);
2965 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
2966 g_mutex_unlock (&priv->lock);
2972 * gst_rtsp_stream_get_rtpinfo:
2973 * @stream: a #GstRTSPStream
2974 * @rtptime: (allow-none): result RTP timestamp
2975 * @seq: (allow-none): result RTP seqnum
2976 * @clock_rate: (allow-none): the clock rate
2977 * @running_time: (allow-none): result running-time
2979 * Retrieve the current rtptime, seq and running-time. This is used to
2980 * construct a RTPInfo reply header.
2982 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2985 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2986 guint * rtptime, guint * seq, guint * clock_rate,
2987 GstClockTime * running_time)
2989 GstRTSPStreamPrivate *priv;
2990 GstStructure *stats;
2991 GObjectClass *payobjclass;
2993 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2995 priv = stream->priv;
2997 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2999 g_mutex_lock (&priv->lock);
3001 /* First try to extract the information from the last buffer on the sinks.
3002 * This will have a more accurate sequence number and timestamp, as between
3003 * the payloader and the sink there can be some queues
3005 if (priv->udpsink[0] || priv->appsink[0]) {
3006 GstSample *last_sample;
3008 if (priv->udpsink[0])
3009 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
3011 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
3016 GstSegment *segment;
3017 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
3019 caps = gst_sample_get_caps (last_sample);
3020 buffer = gst_sample_get_buffer (last_sample);
3021 segment = gst_sample_get_segment (last_sample);
3023 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3025 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3029 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3032 gst_rtp_buffer_unmap (&rtp_buffer);
3036 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3037 GST_BUFFER_TIMESTAMP (buffer));
3041 GstStructure *s = gst_caps_get_structure (caps, 0);
3043 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3045 if (*clock_rate == 0 && running_time)
3046 *running_time = GST_CLOCK_TIME_NONE;
3048 gst_sample_unref (last_sample);
3052 gst_sample_unref (last_sample);
3057 if (g_object_class_find_property (payobjclass, "stats")) {
3058 g_object_get (priv->payloader, "stats", &stats, NULL);
3063 gst_structure_get_uint (stats, "seqnum", seq);
3066 gst_structure_get_uint (stats, "timestamp", rtptime);
3069 gst_structure_get_clock_time (stats, "running-time", running_time);
3072 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3073 if (*clock_rate == 0 && running_time)
3074 *running_time = GST_CLOCK_TIME_NONE;
3076 gst_structure_free (stats);
3078 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3079 !g_object_class_find_property (payobjclass, "timestamp"))
3083 g_object_get (priv->payloader, "seqnum", seq, NULL);
3086 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3089 *running_time = GST_CLOCK_TIME_NONE;
3093 g_mutex_unlock (&priv->lock);
3100 GST_WARNING ("Could not get payloader stats");
3101 g_mutex_unlock (&priv->lock);
3107 * gst_rtsp_stream_get_caps:
3108 * @stream: a #GstRTSPStream
3110 * Retrieve the current caps of @stream.
3112 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
3116 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3118 GstRTSPStreamPrivate *priv;
3121 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3123 priv = stream->priv;
3125 g_mutex_lock (&priv->lock);
3126 if ((result = priv->caps))
3127 gst_caps_ref (result);
3128 g_mutex_unlock (&priv->lock);
3134 * gst_rtsp_stream_recv_rtp:
3135 * @stream: a #GstRTSPStream
3136 * @buffer: (transfer full): a #GstBuffer
3138 * Handle an RTP buffer for the stream. This method is usually called when a
3139 * message has been received from a client using the TCP transport.
3141 * This function takes ownership of @buffer.
3143 * Returns: a GstFlowReturn.
3146 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3148 GstRTSPStreamPrivate *priv;
3150 GstElement *element;
3152 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3153 priv = stream->priv;
3154 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3155 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3157 g_mutex_lock (&priv->lock);
3158 if (priv->appsrc[0])
3159 element = gst_object_ref (priv->appsrc[0]);
3162 g_mutex_unlock (&priv->lock);
3165 if (priv->appsrc_base_time[0] == -1) {
3166 /* Take current running_time. This timestamp will be put on
3167 * the first buffer of each stream because we are a live source and so we
3168 * timestamp with the running_time. When we are dealing with TCP, we also
3169 * only timestamp the first buffer (using the DISCONT flag) because a server
3170 * typically bursts data, for which we don't want to compensate by speeding
3171 * up the media. The other timestamps will be interpollated from this one
3172 * using the RTP timestamps. */
3173 GST_OBJECT_LOCK (element);
3174 if (GST_ELEMENT_CLOCK (element)) {
3176 GstClockTime base_time;
3178 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3179 base_time = GST_ELEMENT_CAST (element)->base_time;
3181 priv->appsrc_base_time[0] = now - base_time;
3182 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3183 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3184 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3185 GST_TIME_ARGS (base_time));
3187 GST_OBJECT_UNLOCK (element);
3190 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3191 gst_object_unref (element);
3199 * gst_rtsp_stream_recv_rtcp:
3200 * @stream: a #GstRTSPStream
3201 * @buffer: (transfer full): a #GstBuffer
3203 * Handle an RTCP buffer for the stream. This method is usually called when a
3204 * message has been received from a client using the TCP transport.
3206 * This function takes ownership of @buffer.
3208 * Returns: a GstFlowReturn.
3211 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3213 GstRTSPStreamPrivate *priv;
3215 GstElement *element;
3217 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3218 priv = stream->priv;
3219 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3221 if (priv->joined_bin == NULL) {
3222 gst_buffer_unref (buffer);
3223 return GST_FLOW_NOT_LINKED;
3225 g_mutex_lock (&priv->lock);
3226 if (priv->appsrc[1])
3227 element = gst_object_ref (priv->appsrc[1]);
3230 g_mutex_unlock (&priv->lock);
3233 if (priv->appsrc_base_time[1] == -1) {
3234 /* Take current running_time. This timestamp will be put on
3235 * the first buffer of each stream because we are a live source and so we
3236 * timestamp with the running_time. When we are dealing with TCP, we also
3237 * only timestamp the first buffer (using the DISCONT flag) because a server
3238 * typically bursts data, for which we don't want to compensate by speeding
3239 * up the media. The other timestamps will be interpollated from this one
3240 * using the RTP timestamps. */
3241 GST_OBJECT_LOCK (element);
3242 if (GST_ELEMENT_CLOCK (element)) {
3244 GstClockTime base_time;
3246 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3247 base_time = GST_ELEMENT_CAST (element)->base_time;
3249 priv->appsrc_base_time[1] = now - base_time;
3250 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3251 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3252 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3253 GST_TIME_ARGS (base_time));
3255 GST_OBJECT_UNLOCK (element);
3258 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3259 gst_object_unref (element);
3262 gst_buffer_unref (buffer);
3267 /* must be called with lock */
3269 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3272 GstRTSPStreamPrivate *priv = stream->priv;
3273 const GstRTSPTransport *tr;
3275 tr = gst_rtsp_stream_transport_get_transport (trans);
3277 switch (tr->lower_transport) {
3278 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3281 if (!check_mcast_part_for_transport (stream, tr))
3283 priv->transports = g_list_prepend (priv->transports, trans);
3285 priv->transports = g_list_remove (priv->transports, trans);
3289 case GST_RTSP_LOWER_TRANS_UDP:
3295 dest = tr->destination;
3296 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3300 } else if (priv->client_side) {
3301 /* In client side mode the 'destination' is the RTSP server, so send
3303 min = tr->server_port.min;
3304 max = tr->server_port.max;
3306 min = tr->client_port.min;
3307 max = tr->client_port.max;
3312 GST_INFO ("setting ttl-mc %d", ttl);
3313 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3314 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3316 GST_INFO ("adding %s:%d-%d", dest, min, max);
3317 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3318 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3319 priv->transports = g_list_prepend (priv->transports, trans);
3321 GST_INFO ("removing %s:%d-%d", dest, min, max);
3322 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3323 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3324 priv->transports = g_list_remove (priv->transports, trans);
3326 priv->transports_cookie++;
3329 case GST_RTSP_LOWER_TRANS_TCP:
3331 GST_INFO ("adding TCP %s", tr->destination);
3332 priv->transports = g_list_prepend (priv->transports, trans);
3334 GST_INFO ("removing TCP %s", tr->destination);
3335 priv->transports = g_list_remove (priv->transports, trans);
3337 priv->transports_cookie++;
3340 goto unknown_transport;
3347 GST_INFO ("Unknown transport %d", tr->lower_transport);
3358 * gst_rtsp_stream_add_transport:
3359 * @stream: a #GstRTSPStream
3360 * @trans: (transfer none): a #GstRTSPStreamTransport
3362 * Add the transport in @trans to @stream. The media of @stream will
3363 * then also be send to the values configured in @trans.
3365 * @stream must be joined to a bin.
3367 * @trans must contain a valid #GstRTSPTransport.
3369 * Returns: %TRUE if @trans was added
3372 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3373 GstRTSPStreamTransport * trans)
3375 GstRTSPStreamPrivate *priv;
3378 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3379 priv = stream->priv;
3380 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3381 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3383 g_mutex_lock (&priv->lock);
3384 res = update_transport (stream, trans, TRUE);
3385 g_mutex_unlock (&priv->lock);
3391 * gst_rtsp_stream_remove_transport:
3392 * @stream: a #GstRTSPStream
3393 * @trans: (transfer none): a #GstRTSPStreamTransport
3395 * Remove the transport in @trans from @stream. The media of @stream will
3396 * not be sent to the values configured in @trans.
3398 * @stream must be joined to a bin.
3400 * @trans must contain a valid #GstRTSPTransport.
3402 * Returns: %TRUE if @trans was removed
3405 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3406 GstRTSPStreamTransport * trans)
3408 GstRTSPStreamPrivate *priv;
3411 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3412 priv = stream->priv;
3413 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3414 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3416 g_mutex_lock (&priv->lock);
3417 res = update_transport (stream, trans, FALSE);
3418 g_mutex_unlock (&priv->lock);
3424 * gst_rtsp_stream_update_crypto:
3425 * @stream: a #GstRTSPStream
3427 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3429 * Update the new crypto information for @ssrc in @stream. If information
3430 * for @ssrc did not exist, it will be added. If information
3431 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3432 * be removed from @stream.
3434 * Returns: %TRUE if @crypto could be updated
3437 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3438 guint ssrc, GstCaps * crypto)
3440 GstRTSPStreamPrivate *priv;
3442 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3443 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3445 priv = stream->priv;
3447 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3449 g_mutex_lock (&priv->lock);
3451 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3452 gst_caps_ref (crypto));
3454 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3455 g_mutex_unlock (&priv->lock);
3461 * gst_rtsp_stream_get_rtp_socket:
3462 * @stream: a #GstRTSPStream
3463 * @family: the socket family
3465 * Get the RTP socket from @stream for a @family.
3467 * @stream must be joined to a bin.
3469 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3470 * socket could be allocated for @family. Unref after usage
3473 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3475 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3479 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3480 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3481 family == G_SOCKET_FAMILY_IPV6, NULL);
3482 g_return_val_if_fail (priv->udpsink[0], NULL);
3484 if (family == G_SOCKET_FAMILY_IPV6)
3489 g_object_get (priv->udpsink[0], name, &socket, NULL);
3495 * gst_rtsp_stream_get_rtcp_socket:
3496 * @stream: a #GstRTSPStream
3497 * @family: the socket family
3499 * Get the RTCP socket from @stream for a @family.
3501 * @stream must be joined to a bin.
3503 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3504 * socket could be allocated for @family. Unref after usage
3507 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3509 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3513 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3514 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3515 family == G_SOCKET_FAMILY_IPV6, NULL);
3516 g_return_val_if_fail (priv->udpsink[1], NULL);
3518 if (family == G_SOCKET_FAMILY_IPV6)
3523 g_object_get (priv->udpsink[1], name, &socket, NULL);
3529 * gst_rtsp_stream_set_seqnum:
3530 * @stream: a #GstRTSPStream
3531 * @seqnum: a new sequence number
3533 * Configure the sequence number in the payloader of @stream to @seqnum.
3536 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3538 GstRTSPStreamPrivate *priv;
3540 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3542 priv = stream->priv;
3544 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3548 * gst_rtsp_stream_get_seqnum:
3549 * @stream: a #GstRTSPStream
3551 * Get the configured sequence number in the payloader of @stream.
3553 * Returns: the sequence number of the payloader.
3556 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3558 GstRTSPStreamPrivate *priv;
3561 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3563 priv = stream->priv;
3565 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3571 * gst_rtsp_stream_transport_filter:
3572 * @stream: a #GstRTSPStream
3573 * @func: (scope call) (allow-none): a callback
3574 * @user_data: (closure): user data passed to @func
3576 * Call @func for each transport managed by @stream. The result value of @func
3577 * determines what happens to the transport. @func will be called with @stream
3578 * locked so no further actions on @stream can be performed from @func.
3580 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3583 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3585 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3586 * will also be added with an additional ref to the result #GList of this
3589 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3591 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3592 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3593 * element in the #GList should be unreffed before the list is freed.
3596 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3597 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3599 GstRTSPStreamPrivate *priv;
3600 GList *result, *walk, *next;
3601 GHashTable *visited = NULL;
3604 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3606 priv = stream->priv;
3610 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3612 g_mutex_lock (&priv->lock);
3614 cookie = priv->transports_cookie;
3615 for (walk = priv->transports; walk; walk = next) {
3616 GstRTSPStreamTransport *trans = walk->data;
3617 GstRTSPFilterResult res;
3620 next = g_list_next (walk);
3623 /* only visit each transport once */
3624 if (g_hash_table_contains (visited, trans))
3627 g_hash_table_add (visited, g_object_ref (trans));
3628 g_mutex_unlock (&priv->lock);
3630 res = func (stream, trans, user_data);
3632 g_mutex_lock (&priv->lock);
3634 res = GST_RTSP_FILTER_REF;
3636 changed = (cookie != priv->transports_cookie);
3639 case GST_RTSP_FILTER_REMOVE:
3640 update_transport (stream, trans, FALSE);
3642 case GST_RTSP_FILTER_REF:
3643 result = g_list_prepend (result, g_object_ref (trans));
3645 case GST_RTSP_FILTER_KEEP:
3652 g_mutex_unlock (&priv->lock);
3655 g_hash_table_unref (visited);
3660 static GstPadProbeReturn
3661 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3663 GstRTSPStreamPrivate *priv;
3664 GstRTSPStream *stream;
3667 priv = stream->priv;
3669 GST_DEBUG_OBJECT (pad, "now blocking");
3671 g_mutex_lock (&priv->lock);
3672 priv->blocking = TRUE;
3673 g_mutex_unlock (&priv->lock);
3675 gst_element_post_message (priv->payloader,
3676 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3677 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3679 return GST_PAD_PROBE_OK;
3683 * gst_rtsp_stream_set_blocked:
3684 * @stream: a #GstRTSPStream
3685 * @blocked: boolean indicating we should block or unblock
3687 * Blocks or unblocks the dataflow on @stream.
3689 * Returns: %TRUE on success
3692 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3694 GstRTSPStreamPrivate *priv;
3696 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3698 priv = stream->priv;
3700 g_mutex_lock (&priv->lock);
3702 priv->blocking = FALSE;
3703 if (priv->blocked_id == 0) {
3704 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3705 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3706 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3707 g_object_ref (stream), g_object_unref);
3710 if (priv->blocked_id != 0) {
3711 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3712 priv->blocked_id = 0;
3713 priv->blocking = FALSE;
3716 g_mutex_unlock (&priv->lock);
3722 * gst_rtsp_stream_is_blocking:
3723 * @stream: a #GstRTSPStream
3725 * Check if @stream is blocking on a #GstBuffer.
3727 * Returns: %TRUE if @stream is blocking
3730 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3732 GstRTSPStreamPrivate *priv;
3735 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3737 priv = stream->priv;
3739 g_mutex_lock (&priv->lock);
3740 result = priv->blocking;
3741 g_mutex_unlock (&priv->lock);
3747 * gst_rtsp_stream_query_position:
3748 * @stream: a #GstRTSPStream
3750 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3751 * the RTP parts of the pipeline and not the RTCP parts.
3753 * Returns: %TRUE if the position could be queried
3756 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3758 GstRTSPStreamPrivate *priv;
3762 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3764 priv = stream->priv;
3766 g_mutex_lock (&priv->lock);
3767 /* depending on the transport type, it should query corresponding sink */
3768 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3769 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3770 sink = priv->udpsink[0];
3772 sink = priv->appsink[0];
3775 gst_object_ref (sink);
3776 g_mutex_unlock (&priv->lock);
3781 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3782 gst_object_unref (sink);
3788 * gst_rtsp_stream_query_stop:
3789 * @stream: a #GstRTSPStream
3791 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3792 * the RTP parts of the pipeline and not the RTCP parts.
3794 * Returns: %TRUE if the stop could be queried
3797 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3799 GstRTSPStreamPrivate *priv;
3804 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3806 priv = stream->priv;
3808 g_mutex_lock (&priv->lock);
3809 /* depending on the transport type, it should query corresponding sink */
3810 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3811 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3812 sink = priv->udpsink[0];
3814 sink = priv->appsink[0];
3817 gst_object_ref (sink);
3818 g_mutex_unlock (&priv->lock);
3823 query = gst_query_new_segment (GST_FORMAT_TIME);
3824 if ((ret = gst_element_query (sink, query))) {
3827 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3828 if (format != GST_FORMAT_TIME)
3831 gst_query_unref (query);
3832 gst_object_unref (sink);