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 (strcmp ((*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, "address %s is not the same that was already"
1086 " reserved", address);
1087 g_mutex_unlock (&priv->lock);
1092 /* must be called with lock */
1094 set_sockets_for_udpsinks (GstElement * udpsink[2], GSocket * rtp_socket,
1095 GSocket * rtcp_socket, GSocketFamily family)
1097 const gchar *multisink_socket;
1099 if (family == G_SOCKET_FAMILY_IPV6)
1100 multisink_socket = "socket-v6";
1102 multisink_socket = "socket";
1104 g_object_set (G_OBJECT (udpsink[0]), multisink_socket, rtp_socket, NULL);
1105 g_object_set (G_OBJECT (udpsink[1]), multisink_socket, rtcp_socket, NULL);
1109 create_and_configure_udpsinks (GstRTSPStream * stream, GstElement * udpsink[2])
1111 GstRTSPStreamPrivate *priv = stream->priv;
1112 GstElement *udpsink0, *udpsink1;
1114 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1115 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1117 if (!udpsink0 || !udpsink1)
1118 goto no_udp_protocol;
1120 /* configure sinks */
1122 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1123 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1125 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1126 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1128 g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
1130 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1131 /* Needs to be async for RECORD streams, otherwise we will never go to
1132 * PLAYING because the sinks will wait for data while the udpsrc can't
1133 * provide data with timestamps in PAUSED. */
1135 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1136 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1138 /* join multicast group when adding clients, so we'll start receiving from it.
1139 * We cannot rely on the udpsrc to join the group since its socket is always a
1140 * local unicast one. */
1141 g_object_set (G_OBJECT (udpsink0), "auto-multicast", TRUE, NULL);
1142 g_object_set (G_OBJECT (udpsink1), "auto-multicast", TRUE, NULL);
1144 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1145 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1147 udpsink[0] = udpsink0;
1148 udpsink[1] = udpsink1;
1150 /* update the dscp qos field in the sinks */
1151 update_dscp_qos (stream, udpsink);
1162 /* must be called with lock */
1164 create_and_configure_udpsources (GstElement * udpsrc_out[2],
1165 GSocket * rtp_socket, GSocket * rtcp_socket)
1167 GstStateChangeReturn ret;
1169 udpsrc_out[0] = gst_element_factory_make ("udpsrc", NULL);
1170 udpsrc_out[1] = gst_element_factory_make ("udpsrc", NULL);
1172 if (udpsrc_out[0] == NULL || udpsrc_out[1] == NULL)
1175 g_object_set (G_OBJECT (udpsrc_out[0]), "socket", rtp_socket, NULL);
1176 g_object_set (G_OBJECT (udpsrc_out[1]), "socket", rtcp_socket, NULL);
1178 /* The udpsrc cannot do the join because its socket is always a local unicast
1179 * one. The udpsink sharing the same socket will do it for us. */
1180 g_object_set (G_OBJECT (udpsrc_out[0]), "auto-multicast", FALSE, NULL);
1181 g_object_set (G_OBJECT (udpsrc_out[1]), "auto-multicast", FALSE, NULL);
1183 g_object_set (G_OBJECT (udpsrc_out[0]), "loop", FALSE, NULL);
1184 g_object_set (G_OBJECT (udpsrc_out[1]), "loop", FALSE, NULL);
1186 ret = gst_element_set_state (udpsrc_out[0], GST_STATE_READY);
1187 if (ret == GST_STATE_CHANGE_FAILURE)
1189 ret = gst_element_set_state (udpsrc_out[1], GST_STATE_READY);
1190 if (ret == GST_STATE_CHANGE_FAILURE)
1198 if (udpsrc_out[0]) {
1199 gst_element_set_state (udpsrc_out[0], GST_STATE_NULL);
1200 g_clear_object (&udpsrc_out[0]);
1202 if (udpsrc_out[1]) {
1203 gst_element_set_state (udpsrc_out[1], GST_STATE_NULL);
1204 g_clear_object (&udpsrc_out[1]);
1211 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1212 GstElement * udpsrc_out[2], GstElement * udpsink_out[2],
1213 GstRTSPAddress ** server_addr_out, gboolean multicast)
1215 GstRTSPStreamPrivate *priv = stream->priv;
1216 GSocket *rtp_socket = NULL;
1217 GSocket *rtcp_socket;
1218 gint tmp_rtp, tmp_rtcp;
1220 gint rtpport, rtcpport;
1221 GList *rejected_addresses = NULL;
1222 GstRTSPAddress *addr = NULL;
1223 GInetAddress *inetaddr = NULL;
1225 GSocketAddress *rtp_sockaddr = NULL;
1226 GSocketAddress *rtcp_sockaddr = NULL;
1227 GstRTSPAddressPool *pool;
1229 g_assert (!udpsrc_out[0]);
1230 g_assert (!udpsrc_out[1]);
1231 g_assert ((!udpsink_out[0] && !udpsink_out[1]) ||
1232 (udpsink_out[0] && udpsink_out[1]));
1233 g_assert (*server_addr_out == NULL);
1238 /* Start with random port */
1241 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1242 G_SOCKET_PROTOCOL_UDP, NULL);
1244 goto no_udp_protocol;
1245 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1247 /* try to allocate 2 UDP ports, the RTP port should be an even
1248 * number and the RTCP port should be the next (uneven) port */
1251 if (rtp_socket == NULL) {
1252 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1253 G_SOCKET_PROTOCOL_UDP, NULL);
1255 goto no_udp_protocol;
1256 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1259 if ((pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) || multicast) {
1260 GstRTSPAddressFlags flags;
1263 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1268 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1270 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1272 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1274 if (family == G_SOCKET_FAMILY_IPV6)
1275 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1277 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1279 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1284 tmp_rtp = addr->port;
1286 g_clear_object (&inetaddr);
1287 inetaddr = g_inet_address_new_from_string (addr->address);
1295 if (inetaddr == NULL)
1296 inetaddr = g_inet_address_new_any (family);
1299 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1300 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1301 g_object_unref (rtp_sockaddr);
1304 g_object_unref (rtp_sockaddr);
1306 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1307 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1308 g_clear_object (&rtp_sockaddr);
1313 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1314 g_object_unref (rtp_sockaddr);
1316 /* check if port is even */
1317 if ((tmp_rtp & 1) != 0) {
1318 /* port not even, close and allocate another */
1320 g_clear_object (&rtp_socket);
1325 tmp_rtcp = tmp_rtp + 1;
1327 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1328 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1329 g_object_unref (rtcp_sockaddr);
1330 g_clear_object (&rtp_socket);
1333 g_object_unref (rtcp_sockaddr);
1336 addr = g_slice_new0 (GstRTSPAddress);
1337 addr->address = g_inet_address_to_string (inetaddr);
1338 addr->port = tmp_rtp;
1342 addr_str = addr->address;
1343 g_clear_object (&inetaddr);
1345 if (!create_and_configure_udpsources (udpsrc_out, rtp_socket, rtcp_socket)) {
1346 goto no_udp_protocol;
1349 g_object_get (G_OBJECT (udpsrc_out[0]), "port", &rtpport, NULL);
1350 g_object_get (G_OBJECT (udpsrc_out[1]), "port", &rtcpport, NULL);
1352 /* this should not happen... */
1353 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1356 /* This function is called twice (for v4 and v6) but we create only one pair
1359 && !create_and_configure_udpsinks (stream, udpsink_out))
1360 goto no_udp_protocol;
1363 g_object_set (G_OBJECT (udpsink_out[0]), "multicast-iface",
1364 priv->multicast_iface, NULL);
1365 g_object_set (G_OBJECT (udpsink_out[1]), "multicast-iface",
1366 priv->multicast_iface, NULL);
1368 g_signal_emit_by_name (udpsink_out[0], "add", addr_str, rtpport, NULL);
1369 g_signal_emit_by_name (udpsink_out[1], "add", addr_str, rtcpport, NULL);
1372 set_sockets_for_udpsinks (udpsink_out, rtp_socket, rtcp_socket, family);
1374 *server_addr_out = addr;
1376 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1378 g_object_unref (rtp_socket);
1379 g_object_unref (rtcp_socket);
1403 g_object_unref (inetaddr);
1404 g_list_free_full (rejected_addresses,
1405 (GDestroyNotify) gst_rtsp_address_free);
1407 gst_rtsp_address_free (addr);
1409 g_object_unref (rtp_socket);
1411 g_object_unref (rtcp_socket);
1417 * gst_rtsp_stream_allocate_udp_sockets:
1418 * @stream: a #GstRTSPStream
1419 * @family: protocol family
1420 * @transport_method: transport method
1422 * Allocates RTP and RTCP ports.
1424 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1425 * Deprecated: This function shouldn't have been made public
1428 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1429 GSocketFamily family, GstRTSPTransport * ct, gboolean use_client_settings)
1431 g_warn_if_reached ();
1436 * gst_rtsp_stream_set_client_side:
1437 * @stream: a #GstRTSPStream
1438 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1439 * an RTSP connection.
1441 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1442 * streams to an RTSP server via RECORD. This has the practical effect
1443 * of changing which UDP port numbers are used when setting up the local
1444 * side of the stream sending to be either the 'server' or 'client' pair
1445 * of a configured UDP transport.
1448 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1450 GstRTSPStreamPrivate *priv;
1452 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1453 priv = stream->priv;
1454 g_mutex_lock (&priv->lock);
1455 priv->client_side = client_side;
1456 g_mutex_unlock (&priv->lock);
1460 * gst_rtsp_stream_is_client_side:
1461 * @stream: a #GstRTSPStream
1463 * See gst_rtsp_stream_set_client_side()
1465 * Returns: TRUE if this #GstRTSPStream is client-side.
1468 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1470 GstRTSPStreamPrivate *priv;
1473 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1475 priv = stream->priv;
1476 g_mutex_lock (&priv->lock);
1477 ret = priv->client_side;
1478 g_mutex_unlock (&priv->lock);
1483 /* must be called with lock */
1485 alloc_ports (GstRTSPStream * stream)
1487 GstRTSPStreamPrivate *priv = stream->priv;
1488 gboolean ret = TRUE;
1490 if (priv->protocols & GST_RTSP_LOWER_TRANS_UDP) {
1491 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1492 priv->udpsrc_v4, priv->udpsink, &priv->server_addr_v4, FALSE);
1494 ret |= alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1495 priv->udpsrc_v6, priv->udpsink, &priv->server_addr_v6, FALSE);
1498 /* FIXME: Maybe actually consider the return values? */
1499 if (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1500 ret |= alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1501 priv->mcast_udpsrc_v4, priv->mcast_udpsink, &priv->mcast_addr_v4, TRUE);
1503 ret |= alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1504 priv->mcast_udpsrc_v6, priv->mcast_udpsink, &priv->mcast_addr_v6, TRUE);
1511 * gst_rtsp_stream_get_server_port:
1512 * @stream: a #GstRTSPStream
1513 * @server_port: (out): result server port
1514 * @family: the port family to get
1516 * Fill @server_port with the port pair used by the server. This function can
1517 * only be called when @stream has been joined.
1520 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1521 GstRTSPRange * server_port, GSocketFamily family)
1523 GstRTSPStreamPrivate *priv;
1525 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1526 priv = stream->priv;
1527 g_return_if_fail (priv->joined_bin != NULL);
1529 g_mutex_lock (&priv->lock);
1530 if (family == G_SOCKET_FAMILY_IPV4) {
1532 server_port->min = priv->server_addr_v4->port;
1534 priv->server_addr_v4->port + priv->server_addr_v4->n_ports - 1;
1538 server_port->min = priv->server_addr_v6->port;
1540 priv->server_addr_v6->port + priv->server_addr_v6->n_ports - 1;
1543 g_mutex_unlock (&priv->lock);
1547 * gst_rtsp_stream_get_rtpsession:
1548 * @stream: a #GstRTSPStream
1550 * Get the RTP session of this stream.
1552 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1555 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1557 GstRTSPStreamPrivate *priv;
1560 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1562 priv = stream->priv;
1564 g_mutex_lock (&priv->lock);
1565 if ((session = priv->session))
1566 g_object_ref (session);
1567 g_mutex_unlock (&priv->lock);
1573 * gst_rtsp_stream_get_encoder:
1574 * @stream: a #GstRTSPStream
1576 * Get the SRTP encoder for this stream.
1578 * Returns: (transfer full): The SRTP encoder for this stream. Unref after usage.
1581 gst_rtsp_stream_get_srtp_encoder (GstRTSPStream * stream)
1583 GstRTSPStreamPrivate *priv;
1584 GstElement *encoder;
1586 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1588 priv = stream->priv;
1590 g_mutex_lock (&priv->lock);
1591 if ((encoder = priv->srtpenc))
1592 g_object_ref (encoder);
1593 g_mutex_unlock (&priv->lock);
1599 * gst_rtsp_stream_get_ssrc:
1600 * @stream: a #GstRTSPStream
1601 * @ssrc: (out): result ssrc
1603 * Get the SSRC used by the RTP session of this stream. This function can only
1604 * be called when @stream has been joined.
1607 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1609 GstRTSPStreamPrivate *priv;
1611 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1612 priv = stream->priv;
1613 g_return_if_fail (priv->joined_bin != NULL);
1615 g_mutex_lock (&priv->lock);
1616 if (ssrc && priv->session)
1617 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1618 g_mutex_unlock (&priv->lock);
1622 * gst_rtsp_stream_set_retransmission_time:
1623 * @stream: a #GstRTSPStream
1624 * @time: a #GstClockTime
1626 * Set the amount of time to store retransmission packets.
1629 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1632 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1634 g_mutex_lock (&stream->priv->lock);
1635 stream->priv->rtx_time = time;
1636 if (stream->priv->rtxsend)
1637 g_object_set (stream->priv->rtxsend, "max-size-time",
1638 GST_TIME_AS_MSECONDS (time), NULL);
1639 g_mutex_unlock (&stream->priv->lock);
1643 * gst_rtsp_stream_get_retransmission_time:
1644 * @stream: a #GstRTSPStream
1646 * Get the amount of time to store retransmission data.
1648 * Returns: the amount of time to store retransmission data.
1651 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1655 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1657 g_mutex_lock (&stream->priv->lock);
1658 ret = stream->priv->rtx_time;
1659 g_mutex_unlock (&stream->priv->lock);
1665 * gst_rtsp_stream_set_retransmission_pt:
1666 * @stream: a #GstRTSPStream
1669 * Set the payload type (pt) for retransmission of this stream.
1672 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1674 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1676 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1678 g_mutex_lock (&stream->priv->lock);
1679 stream->priv->rtx_pt = rtx_pt;
1680 if (stream->priv->rtxsend) {
1681 guint pt = gst_rtsp_stream_get_pt (stream);
1682 gchar *pt_s = g_strdup_printf ("%d", pt);
1683 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1684 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1685 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1687 gst_structure_free (rtx_pt_map);
1689 g_mutex_unlock (&stream->priv->lock);
1693 * gst_rtsp_stream_get_retransmission_pt:
1694 * @stream: a #GstRTSPStream
1696 * Get the payload-type used for retransmission of this stream
1698 * Returns: The retransmission PT.
1701 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1705 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1707 g_mutex_lock (&stream->priv->lock);
1708 rtx_pt = stream->priv->rtx_pt;
1709 g_mutex_unlock (&stream->priv->lock);
1715 * gst_rtsp_stream_set_buffer_size:
1716 * @stream: a #GstRTSPStream
1717 * @size: the buffer size
1719 * Set the size of the UDP transmission buffer (in bytes)
1720 * Needs to be set before the stream is joined to a bin.
1725 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1727 g_mutex_lock (&stream->priv->lock);
1728 stream->priv->buffer_size = size;
1729 g_mutex_unlock (&stream->priv->lock);
1733 * gst_rtsp_stream_get_buffer_size:
1734 * @stream: a #GstRTSPStream
1736 * Get the size of the UDP transmission buffer (in bytes)
1738 * Returns: the size of the UDP TX buffer
1743 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1747 g_mutex_lock (&stream->priv->lock);
1748 buffer_size = stream->priv->buffer_size;
1749 g_mutex_unlock (&stream->priv->lock);
1754 /* executed from streaming thread */
1756 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1758 GstRTSPStreamPrivate *priv = stream->priv;
1759 GstCaps *newcaps, *oldcaps;
1761 newcaps = gst_pad_get_current_caps (pad);
1763 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1766 g_mutex_lock (&priv->lock);
1767 oldcaps = priv->caps;
1768 priv->caps = newcaps;
1769 g_mutex_unlock (&priv->lock);
1772 gst_caps_unref (oldcaps);
1776 dump_structure (const GstStructure * s)
1780 sstr = gst_structure_to_string (s);
1781 GST_INFO ("structure: %s", sstr);
1785 static GstRTSPStreamTransport *
1786 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1788 GstRTSPStreamPrivate *priv = stream->priv;
1790 GstRTSPStreamTransport *result = NULL;
1795 if (rtcp_from == NULL)
1798 tmp = g_strrstr (rtcp_from, ":");
1802 port = atoi (tmp + 1);
1803 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1805 g_mutex_lock (&priv->lock);
1806 GST_INFO ("finding %s:%d in %d transports", dest, port,
1807 g_list_length (priv->transports));
1809 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1810 GstRTSPStreamTransport *trans = walk->data;
1811 const GstRTSPTransport *tr;
1814 tr = gst_rtsp_stream_transport_get_transport (trans);
1816 if (priv->client_side) {
1817 /* In client side mode the 'destination' is the RTSP server, so send
1819 min = tr->server_port.min;
1820 max = tr->server_port.max;
1822 min = tr->client_port.min;
1823 max = tr->client_port.max;
1826 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1832 g_object_ref (result);
1833 g_mutex_unlock (&priv->lock);
1840 static GstRTSPStreamTransport *
1841 check_transport (GObject * source, GstRTSPStream * stream)
1843 GstStructure *stats;
1844 GstRTSPStreamTransport *trans;
1846 /* see if we have a stream to match with the origin of the RTCP packet */
1847 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1848 if (trans == NULL) {
1849 g_object_get (source, "stats", &stats, NULL);
1851 const gchar *rtcp_from;
1853 dump_structure (stats);
1855 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1856 if ((trans = find_transport (stream, rtcp_from))) {
1857 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1859 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1862 gst_structure_free (stats);
1870 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1872 GstRTSPStreamTransport *trans;
1874 GST_INFO ("%p: new source %p", stream, source);
1876 trans = check_transport (source, stream);
1879 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1883 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1885 GST_INFO ("%p: new SDES %p", stream, source);
1889 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1891 GstRTSPStreamTransport *trans;
1893 trans = check_transport (source, stream);
1896 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1897 gst_rtsp_stream_transport_keep_alive (trans);
1901 GstStructure *stats;
1902 g_object_get (source, "stats", &stats, NULL);
1904 dump_structure (stats);
1905 gst_structure_free (stats);
1912 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1914 GST_INFO ("%p: source %p bye", stream, source);
1918 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1920 GstRTSPStreamTransport *trans;
1922 GST_INFO ("%p: source %p bye timeout", stream, source);
1924 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1925 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1926 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1931 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1933 GstRTSPStreamTransport *trans;
1935 GST_INFO ("%p: source %p timeout", stream, source);
1937 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1938 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1939 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1944 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1946 GST_INFO ("%p: new sender source %p", stream, source);
1949 GstStructure *stats;
1950 g_object_get (source, "stats", &stats, NULL);
1952 dump_structure (stats);
1953 gst_structure_free (stats);
1960 on_sender_ssrc_active (GObject * session, GObject * source,
1961 GstRTSPStream * stream)
1965 GstStructure *stats;
1966 g_object_get (source, "stats", &stats, NULL);
1968 dump_structure (stats);
1969 gst_structure_free (stats);
1976 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1979 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1980 g_list_free (priv->tr_cache_rtp);
1981 priv->tr_cache_rtp = NULL;
1983 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1984 g_list_free (priv->tr_cache_rtcp);
1985 priv->tr_cache_rtcp = NULL;
1989 static GstFlowReturn
1990 handle_new_sample (GstAppSink * sink, gpointer user_data)
1992 GstRTSPStreamPrivate *priv;
1996 GstRTSPStream *stream;
1999 sample = gst_app_sink_pull_sample (sink);
2003 stream = (GstRTSPStream *) user_data;
2004 priv = stream->priv;
2005 buffer = gst_sample_get_buffer (sample);
2007 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
2009 g_mutex_lock (&priv->lock);
2011 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
2012 clear_tr_cache (priv, is_rtp);
2013 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2014 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2015 priv->tr_cache_rtp =
2016 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
2018 priv->tr_cache_cookie_rtp = priv->transports_cookie;
2021 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
2022 clear_tr_cache (priv, is_rtp);
2023 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2024 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2025 priv->tr_cache_rtcp =
2026 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
2028 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
2031 g_mutex_unlock (&priv->lock);
2034 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2035 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2036 gst_rtsp_stream_transport_send_rtp (tr, buffer);
2039 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2040 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2041 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
2044 gst_sample_unref (sample);
2049 static GstAppSinkCallbacks sink_cb = {
2050 NULL, /* not interested in EOS */
2051 NULL, /* not interested in preroll samples */
2056 get_rtp_encoder (GstRTSPStream * stream, guint session)
2058 GstRTSPStreamPrivate *priv = stream->priv;
2060 if (priv->srtpenc == NULL) {
2063 name = g_strdup_printf ("srtpenc_%u", session);
2064 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2067 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2069 return gst_object_ref (priv->srtpenc);
2073 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2075 GstRTSPStreamPrivate *priv = stream->priv;
2076 GstElement *oldenc, *enc;
2080 if (priv->idx != session)
2083 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2085 oldenc = priv->srtpenc;
2086 enc = get_rtp_encoder (stream, session);
2087 name = g_strdup_printf ("rtp_sink_%d", session);
2088 pad = gst_element_get_request_pad (enc, name);
2090 gst_object_unref (pad);
2093 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2100 request_rtcp_encoder (GstElement * rtpbin, guint session,
2101 GstRTSPStream * stream)
2103 GstRTSPStreamPrivate *priv = stream->priv;
2104 GstElement *oldenc, *enc;
2108 if (priv->idx != session)
2111 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2113 oldenc = priv->srtpenc;
2114 enc = get_rtp_encoder (stream, session);
2115 name = g_strdup_printf ("rtcp_sink_%d", session);
2116 pad = gst_element_get_request_pad (enc, name);
2118 gst_object_unref (pad);
2121 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2128 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2130 GstRTSPStreamPrivate *priv = stream->priv;
2133 GST_DEBUG ("request key %08x", ssrc);
2135 g_mutex_lock (&priv->lock);
2136 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2137 gst_caps_ref (caps);
2138 g_mutex_unlock (&priv->lock);
2144 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2145 GstRTSPStream * stream)
2147 GstRTSPStreamPrivate *priv = stream->priv;
2149 if (priv->idx != session)
2152 if (priv->srtpdec == NULL) {
2155 name = g_strdup_printf ("srtpdec_%u", session);
2156 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2159 g_signal_connect (priv->srtpdec, "request-key",
2160 (GCallback) request_key, stream);
2162 return gst_object_ref (priv->srtpdec);
2166 * gst_rtsp_stream_request_aux_sender:
2167 * @stream: a #GstRTSPStream
2168 * @sessid: the session id
2170 * Creating a rtxsend bin
2172 * Returns: (transfer full): a #GstElement.
2177 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2181 GstStructure *pt_map;
2186 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2188 pt = gst_rtsp_stream_get_pt (stream);
2189 pt_s = g_strdup_printf ("%u", pt);
2190 rtx_pt = stream->priv->rtx_pt;
2192 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2194 bin = gst_bin_new (NULL);
2195 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2196 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2197 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2198 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2199 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2201 gst_structure_free (pt_map);
2202 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2204 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2205 name = g_strdup_printf ("src_%u", sessid);
2206 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2208 gst_object_unref (pad);
2210 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2211 name = g_strdup_printf ("sink_%u", sessid);
2212 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2214 gst_object_unref (pad);
2220 * gst_rtsp_stream_set_pt_map:
2221 * @stream: a #GstRTSPStream
2225 * Configure a pt map between @pt and @caps.
2228 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2230 GstRTSPStreamPrivate *priv = stream->priv;
2232 g_mutex_lock (&priv->lock);
2233 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2234 g_mutex_unlock (&priv->lock);
2238 * gst_rtsp_stream_set_publish_clock_mode:
2239 * @stream: a #GstRTSPStream
2240 * @mode: the clock publish mode
2242 * Sets if and how the stream clock should be published according to RFC7273.
2247 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
2248 GstRTSPPublishClockMode mode)
2250 GstRTSPStreamPrivate *priv;
2252 priv = stream->priv;
2253 g_mutex_lock (&priv->lock);
2254 priv->publish_clock_mode = mode;
2255 g_mutex_unlock (&priv->lock);
2259 * gst_rtsp_stream_get_publish_clock_mode:
2260 * @factory: a #GstRTSPStream
2262 * Gets if and how the stream clock should be published according to RFC7273.
2264 * Returns: The GstRTSPPublishClockMode
2268 GstRTSPPublishClockMode
2269 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
2271 GstRTSPStreamPrivate *priv;
2272 GstRTSPPublishClockMode ret;
2274 priv = stream->priv;
2275 g_mutex_lock (&priv->lock);
2276 ret = priv->publish_clock_mode;
2277 g_mutex_unlock (&priv->lock);
2283 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2284 GstRTSPStream * stream)
2286 GstRTSPStreamPrivate *priv = stream->priv;
2287 GstCaps *caps = NULL;
2289 g_mutex_lock (&priv->lock);
2291 if (priv->idx == session) {
2292 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2294 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2295 gst_caps_ref (caps);
2297 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2301 g_mutex_unlock (&priv->lock);
2307 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2309 GstRTSPStreamPrivate *priv = stream->priv;
2311 GstPadLinkReturn ret;
2314 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2315 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2317 name = gst_pad_get_name (pad);
2318 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2324 if (priv->idx != sessid)
2327 if (gst_pad_is_linked (priv->sinkpad)) {
2328 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2329 GST_DEBUG_PAD_NAME (priv->sinkpad));
2333 /* link the RTP pad to the session manager, it should not really fail unless
2334 * this is not really an RTP pad */
2335 ret = gst_pad_link (pad, priv->sinkpad);
2336 if (ret != GST_PAD_LINK_OK)
2338 priv->recv_rtp_src = gst_object_ref (pad);
2345 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2346 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2351 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2352 GstRTSPStream * stream)
2354 /* TODO: What to do here other than this? */
2355 GST_DEBUG ("Stream %p: Got EOS", stream);
2356 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2360 plug_sink (GstBin * bin, GstElement * tee, GstElement * sink,
2361 GstElement ** queue_out)
2367 gst_bin_add (bin, sink);
2369 *queue_out = gst_element_factory_make ("queue", NULL);
2370 g_object_set (*queue_out, "max-size-buffers", 1, "max-size-bytes", 0,
2371 "max-size-time", G_GINT64_CONSTANT (0), NULL);
2372 gst_bin_add (bin, *queue_out);
2374 /* link tee to queue */
2375 teepad = gst_element_get_request_pad (tee, "src_%u");
2376 pad = gst_element_get_static_pad (*queue_out, "sink");
2377 gst_pad_link (teepad, pad);
2378 gst_object_unref (pad);
2379 gst_object_unref (teepad);
2381 /* link queue to sink */
2382 queuepad = gst_element_get_static_pad (*queue_out, "src");
2383 pad = gst_element_get_static_pad (sink, "sink");
2384 gst_pad_link (queuepad, pad);
2385 gst_object_unref (queuepad);
2386 gst_object_unref (pad);
2389 /* must be called with lock */
2391 create_sender_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2393 GstRTSPStreamPrivate *priv;
2395 gboolean is_tcp, is_udp;
2398 priv = stream->priv;
2400 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2401 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2402 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2404 for (i = 0; i < 2; i++) {
2405 /* For the sender we create this bit of pipeline for both
2406 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2407 * we need to add a queue before appsink and udpsink to make
2408 * the pipeline not block. For the TCP case, we want to pump
2409 * client as fast as possible anyway. This pipeline is used
2410 * when both TCP and UDP are present.
2412 * .--------. .-----. .---------. .---------.
2413 * | rtpbin | | tee | | queue | | udpsink |
2414 * | send->sink src->sink src->sink |
2415 * '--------' | | '---------' '---------'
2416 * | | .---------. .---------.
2417 * | | | queue | | appsink |
2418 * | src->sink src->sink |
2419 * '-----' '---------' '---------'
2421 * When only UDP or only TCP is allowed, we skip the tee and queue
2422 * and link the udpsink (for UDP) or appsink (for TCP) directly to
2426 /* Only link the RTP send src if we're going to send RTP, link
2427 * the RTCP send src always */
2428 if (!priv->srcpad && i == 0)
2433 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2434 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2435 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2436 &sink_cb, stream, NULL);
2439 /* If we have udp always use a tee because we could have mcast clients
2440 * requesting different ports, in which case we'll have to plug more
2443 /* make tee for RTP/RTCP */
2444 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2445 gst_bin_add (bin, priv->tee[i]);
2447 /* and link to rtpbin send pad */
2448 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2449 gst_pad_link (priv->send_src[i], pad);
2450 gst_object_unref (pad);
2452 if (priv->udpsink[i])
2453 plug_sink (bin, priv->tee[i], priv->udpsink[i], &priv->udpqueue[i]);
2455 if (priv->mcast_udpsink[i])
2456 plug_sink (bin, priv->tee[i], priv->mcast_udpsink[i],
2457 &priv->mcast_udpqueue[i]);
2460 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2461 plug_sink (bin, priv->tee[i], priv->appsink[i], &priv->appqueue[i]);
2463 } else if (is_tcp) {
2464 /* only appsink needed, link it to the session */
2465 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2466 gst_pad_link (priv->send_src[i], pad);
2467 gst_object_unref (pad);
2469 /* when its only TCP, we need to set sync and preroll to FALSE
2470 * for the sink to avoid deadlock. And this is only needed for
2471 * sink used for RTCP data, not the RTP data. */
2473 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2476 /* check if we need to set to a special state */
2477 if (state != GST_STATE_NULL) {
2478 if (priv->udpsink[i])
2479 gst_element_set_state (priv->udpsink[i], state);
2480 if (priv->mcast_udpsink[i])
2481 gst_element_set_state (priv->mcast_udpsink[i], state);
2482 if (priv->appsink[i])
2483 gst_element_set_state (priv->appsink[i], state);
2484 if (priv->appqueue[i])
2485 gst_element_set_state (priv->appqueue[i], state);
2486 if (priv->udpqueue[i])
2487 gst_element_set_state (priv->udpqueue[i], state);
2488 if (priv->mcast_udpqueue[i])
2489 gst_element_set_state (priv->mcast_udpqueue[i], state);
2491 gst_element_set_state (priv->tee[i], state);
2496 /* must be called with lock */
2498 plug_src (GstRTSPStream * stream, GstBin * bin, GstElement * src,
2499 GstElement * funnel)
2501 GstRTSPStreamPrivate *priv;
2502 GstPad *pad, *selpad;
2504 priv = stream->priv;
2507 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2508 * values. This is only relevant for PLAY pipelines */
2509 gst_element_set_state (src, GST_STATE_PLAYING);
2510 gst_element_set_locked_state (src, TRUE);
2514 gst_bin_add (bin, src);
2516 /* and link to the funnel */
2517 selpad = gst_element_get_request_pad (funnel, "sink_%u");
2518 pad = gst_element_get_static_pad (src, "src");
2519 gst_pad_link (pad, selpad);
2520 gst_object_unref (pad);
2521 gst_object_unref (selpad);
2524 /* must be called with lock */
2526 create_receiver_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2528 GstRTSPStreamPrivate *priv;
2533 priv = stream->priv;
2535 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2537 for (i = 0; i < 2; i++) {
2538 /* For the receiver we create this bit of pipeline for both
2539 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2540 * and it is all funneled into the rtpbin receive pad.
2542 * .--------. .--------. .--------.
2543 * | udpsrc | | funnel | | rtpbin |
2544 * | src->sink src->sink |
2545 * '--------' | | '--------'
2549 * '--------' '--------'
2552 if (!priv->sinkpad && i == 0) {
2553 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2554 * RTCP sink always */
2558 /* make funnel for the RTP/RTCP receivers */
2559 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2560 gst_bin_add (bin, priv->funnel[i]);
2562 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2563 gst_pad_link (pad, priv->recv_sink[i]);
2564 gst_object_unref (pad);
2566 if (priv->udpsrc_v4[i])
2567 plug_src (stream, bin, priv->udpsrc_v4[i], priv->funnel[i]);
2569 if (priv->udpsrc_v6[i])
2570 plug_src (stream, bin, priv->udpsrc_v6[i], priv->funnel[i]);
2572 if (priv->mcast_udpsrc_v4[i])
2573 plug_src (stream, bin, priv->mcast_udpsrc_v4[i], priv->funnel[i]);
2575 if (priv->mcast_udpsrc_v6[i])
2576 plug_src (stream, bin, priv->mcast_udpsrc_v6[i], priv->funnel[i]);
2579 /* make and add appsrc */
2580 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2581 priv->appsrc_base_time[i] = -1;
2582 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
2584 plug_src (stream, bin, priv->appsrc[i], priv->funnel[i]);
2587 /* check if we need to set to a special state */
2588 if (state != GST_STATE_NULL) {
2589 gst_element_set_state (priv->funnel[i], state);
2595 check_mcast_part_for_transport (GstRTSPStream * stream,
2596 const GstRTSPTransport * tr)
2598 GstRTSPStreamPrivate *priv = stream->priv;
2599 GInetAddress *inetaddr;
2600 GSocketFamily family;
2601 GstRTSPAddress *mcast_addr;
2603 /* Check if it's a ipv4 or ipv6 transport */
2604 inetaddr = g_inet_address_new_from_string (tr->destination);
2605 family = g_inet_address_get_family (inetaddr);
2606 g_object_unref (inetaddr);
2608 /* Select fields corresponding to the family */
2609 if (family == G_SOCKET_FAMILY_IPV4) {
2610 mcast_addr = priv->mcast_addr_v4;
2612 mcast_addr = priv->mcast_addr_v6;
2615 /* We support only one mcast group per family, make sure this transport
2620 if (!g_str_equal (tr->destination, mcast_addr->address) ||
2621 tr->port.min != mcast_addr->port ||
2622 tr->port.max != mcast_addr->port + mcast_addr->n_ports - 1 ||
2623 tr->ttl != mcast_addr->ttl)
2630 GST_WARNING_OBJECT (stream, "Adding mcast transport, but no mcast address "
2631 "has been reserved");
2636 GST_WARNING_OBJECT (stream, "Adding mcast transport, but it doesn't match "
2637 "the reserved address");
2643 * gst_rtsp_stream_join_bin:
2644 * @stream: a #GstRTSPStream
2645 * @bin: (transfer none): a #GstBin to join
2646 * @rtpbin: (transfer none): a rtpbin element in @bin
2647 * @state: the target state of the new elements
2649 * Join the #GstBin @bin that contains the element @rtpbin.
2651 * @stream will link to @rtpbin, which must be inside @bin. The elements
2652 * added to @bin will be set to the state given in @state.
2654 * Returns: %TRUE on success.
2657 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2658 GstElement * rtpbin, GstState state)
2660 GstRTSPStreamPrivate *priv;
2663 GstPadLinkReturn ret;
2665 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2666 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2667 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2669 priv = stream->priv;
2671 g_mutex_lock (&priv->lock);
2672 if (priv->joined_bin != NULL)
2675 /* create a session with the same index as the stream */
2678 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2680 if (!alloc_ports (stream))
2683 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2684 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2686 g_signal_connect (rtpbin, "request-rtp-encoder",
2687 (GCallback) request_rtp_encoder, stream);
2688 g_signal_connect (rtpbin, "request-rtcp-encoder",
2689 (GCallback) request_rtcp_encoder, stream);
2690 g_signal_connect (rtpbin, "request-rtp-decoder",
2691 (GCallback) request_rtp_rtcp_decoder, stream);
2692 g_signal_connect (rtpbin, "request-rtcp-decoder",
2693 (GCallback) request_rtp_rtcp_decoder, stream);
2696 if (priv->sinkpad) {
2697 g_signal_connect (rtpbin, "request-pt-map",
2698 (GCallback) request_pt_map, stream);
2701 /* get pads from the RTP session element for sending and receiving
2704 /* get a pad for sending RTP */
2705 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2706 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2709 /* link the RTP pad to the session manager, it should not really fail unless
2710 * this is not really an RTP pad */
2711 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2712 if (ret != GST_PAD_LINK_OK)
2715 name = g_strdup_printf ("send_rtp_src_%u", idx);
2716 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2719 /* Need to connect our sinkpad from here */
2720 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2722 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2724 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2725 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2729 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2730 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2732 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2733 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2736 /* get the session */
2737 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2739 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2741 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2743 g_signal_connect (priv->session, "on-ssrc-active",
2744 (GCallback) on_ssrc_active, stream);
2745 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2747 g_signal_connect (priv->session, "on-bye-timeout",
2748 (GCallback) on_bye_timeout, stream);
2749 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2752 /* signal for sender ssrc */
2753 g_signal_connect (priv->session, "on-new-sender-ssrc",
2754 (GCallback) on_new_sender_ssrc, stream);
2755 g_signal_connect (priv->session, "on-sender-ssrc-active",
2756 (GCallback) on_sender_ssrc_active, stream);
2758 create_sender_part (stream, bin, state);
2759 create_receiver_part (stream, bin, state);
2762 /* be notified of caps changes */
2763 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2764 (GCallback) caps_notify, stream);
2767 priv->joined_bin = gst_object_ref (bin);
2768 g_mutex_unlock (&priv->lock);
2775 g_mutex_unlock (&priv->lock);
2780 g_mutex_unlock (&priv->lock);
2781 GST_WARNING ("failed to allocate ports %u", idx);
2786 GST_WARNING ("failed to link stream %u", idx);
2787 gst_object_unref (priv->send_rtp_sink);
2788 priv->send_rtp_sink = NULL;
2789 g_mutex_unlock (&priv->lock);
2795 clear_element (GstBin * bin, GstElement ** elementptr)
2798 gst_element_set_locked_state (*elementptr, FALSE);
2799 gst_element_set_state (*elementptr, GST_STATE_NULL);
2800 if (GST_ELEMENT_PARENT (*elementptr))
2801 gst_bin_remove (bin, *elementptr);
2803 gst_object_unref (*elementptr);
2809 * gst_rtsp_stream_leave_bin:
2810 * @stream: a #GstRTSPStream
2811 * @bin: (transfer none): a #GstBin
2812 * @rtpbin: (transfer none): a rtpbin #GstElement
2814 * Remove the elements of @stream from @bin.
2816 * Return: %TRUE on success.
2819 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2820 GstElement * rtpbin)
2822 GstRTSPStreamPrivate *priv;
2825 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2826 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2827 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2829 priv = stream->priv;
2831 g_mutex_lock (&priv->lock);
2832 if (priv->joined_bin == NULL)
2833 goto was_not_joined;
2834 if (priv->joined_bin != bin)
2837 priv->joined_bin = NULL;
2839 /* all transports must be removed by now */
2840 if (priv->transports != NULL)
2841 goto transports_not_removed;
2843 clear_tr_cache (priv, TRUE);
2844 clear_tr_cache (priv, FALSE);
2846 GST_INFO ("stream %p leaving bin", stream);
2849 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2851 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2852 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2853 gst_object_unref (priv->send_rtp_sink);
2854 priv->send_rtp_sink = NULL;
2855 } else if (priv->recv_rtp_src) {
2856 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2857 gst_object_unref (priv->recv_rtp_src);
2858 priv->recv_rtp_src = NULL;
2861 for (i = 0; i < 2; i++) {
2862 clear_element (bin, &priv->udpsrc_v4[i]);
2863 clear_element (bin, &priv->udpsrc_v6[i]);
2864 clear_element (bin, &priv->udpqueue[i]);
2865 clear_element (bin, &priv->udpsink[i]);
2867 clear_element (bin, &priv->mcast_udpsrc_v4[i]);
2868 clear_element (bin, &priv->mcast_udpsrc_v6[i]);
2869 clear_element (bin, &priv->mcast_udpqueue[i]);
2870 clear_element (bin, &priv->mcast_udpsink[i]);
2872 clear_element (bin, &priv->appsrc[i]);
2873 clear_element (bin, &priv->appqueue[i]);
2874 clear_element (bin, &priv->appsink[i]);
2876 clear_element (bin, &priv->tee[i]);
2877 clear_element (bin, &priv->funnel[i]);
2879 if (priv->sinkpad || i == 1) {
2880 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2881 gst_object_unref (priv->recv_sink[i]);
2882 priv->recv_sink[i] = NULL;
2887 gst_object_unref (priv->send_src[0]);
2888 priv->send_src[0] = NULL;
2891 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2892 gst_object_unref (priv->send_src[1]);
2893 priv->send_src[1] = NULL;
2895 g_object_unref (priv->session);
2896 priv->session = NULL;
2898 gst_caps_unref (priv->caps);
2902 gst_object_unref (priv->srtpenc);
2904 gst_object_unref (priv->srtpdec);
2906 if (priv->mcast_addr_v4)
2907 gst_rtsp_address_free (priv->mcast_addr_v4);
2908 priv->mcast_addr_v4 = NULL;
2909 if (priv->mcast_addr_v6)
2910 gst_rtsp_address_free (priv->mcast_addr_v6);
2911 priv->mcast_addr_v6 = NULL;
2912 if (priv->server_addr_v4)
2913 gst_rtsp_address_free (priv->server_addr_v4);
2914 priv->server_addr_v4 = NULL;
2915 if (priv->server_addr_v6)
2916 gst_rtsp_address_free (priv->server_addr_v6);
2917 priv->server_addr_v6 = NULL;
2919 g_clear_object (&priv->joined_bin);
2920 g_mutex_unlock (&priv->lock);
2926 g_mutex_unlock (&priv->lock);
2929 transports_not_removed:
2931 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2932 g_mutex_unlock (&priv->lock);
2937 GST_ERROR_OBJECT (stream, "leaving the wrong bin");
2938 g_mutex_unlock (&priv->lock);
2944 * gst_rtsp_stream_get_joined_bin:
2945 * @stream: a #GstRTSPStream
2947 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
2949 * Return: (transfer full): the joined bin or NULL.
2952 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
2954 GstRTSPStreamPrivate *priv;
2957 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2959 priv = stream->priv;
2961 g_mutex_lock (&priv->lock);
2962 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
2963 g_mutex_unlock (&priv->lock);
2969 * gst_rtsp_stream_get_rtpinfo:
2970 * @stream: a #GstRTSPStream
2971 * @rtptime: (allow-none): result RTP timestamp
2972 * @seq: (allow-none): result RTP seqnum
2973 * @clock_rate: (allow-none): the clock rate
2974 * @running_time: (allow-none): result running-time
2976 * Retrieve the current rtptime, seq and running-time. This is used to
2977 * construct a RTPInfo reply header.
2979 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2982 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2983 guint * rtptime, guint * seq, guint * clock_rate,
2984 GstClockTime * running_time)
2986 GstRTSPStreamPrivate *priv;
2987 GstStructure *stats;
2988 GObjectClass *payobjclass;
2990 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2992 priv = stream->priv;
2994 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2996 g_mutex_lock (&priv->lock);
2998 /* First try to extract the information from the last buffer on the sinks.
2999 * This will have a more accurate sequence number and timestamp, as between
3000 * the payloader and the sink there can be some queues
3002 if (priv->udpsink[0] || priv->appsink[0]) {
3003 GstSample *last_sample;
3005 if (priv->udpsink[0])
3006 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
3008 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
3013 GstSegment *segment;
3014 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
3016 caps = gst_sample_get_caps (last_sample);
3017 buffer = gst_sample_get_buffer (last_sample);
3018 segment = gst_sample_get_segment (last_sample);
3020 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3022 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3026 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3029 gst_rtp_buffer_unmap (&rtp_buffer);
3033 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3034 GST_BUFFER_TIMESTAMP (buffer));
3038 GstStructure *s = gst_caps_get_structure (caps, 0);
3040 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3042 if (*clock_rate == 0 && running_time)
3043 *running_time = GST_CLOCK_TIME_NONE;
3045 gst_sample_unref (last_sample);
3049 gst_sample_unref (last_sample);
3054 if (g_object_class_find_property (payobjclass, "stats")) {
3055 g_object_get (priv->payloader, "stats", &stats, NULL);
3060 gst_structure_get_uint (stats, "seqnum", seq);
3063 gst_structure_get_uint (stats, "timestamp", rtptime);
3066 gst_structure_get_clock_time (stats, "running-time", running_time);
3069 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3070 if (*clock_rate == 0 && running_time)
3071 *running_time = GST_CLOCK_TIME_NONE;
3073 gst_structure_free (stats);
3075 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3076 !g_object_class_find_property (payobjclass, "timestamp"))
3080 g_object_get (priv->payloader, "seqnum", seq, NULL);
3083 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3086 *running_time = GST_CLOCK_TIME_NONE;
3090 g_mutex_unlock (&priv->lock);
3097 GST_WARNING ("Could not get payloader stats");
3098 g_mutex_unlock (&priv->lock);
3104 * gst_rtsp_stream_get_caps:
3105 * @stream: a #GstRTSPStream
3107 * Retrieve the current caps of @stream.
3109 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
3113 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3115 GstRTSPStreamPrivate *priv;
3118 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3120 priv = stream->priv;
3122 g_mutex_lock (&priv->lock);
3123 if ((result = priv->caps))
3124 gst_caps_ref (result);
3125 g_mutex_unlock (&priv->lock);
3131 * gst_rtsp_stream_recv_rtp:
3132 * @stream: a #GstRTSPStream
3133 * @buffer: (transfer full): a #GstBuffer
3135 * Handle an RTP buffer for the stream. This method is usually called when a
3136 * message has been received from a client using the TCP transport.
3138 * This function takes ownership of @buffer.
3140 * Returns: a GstFlowReturn.
3143 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3145 GstRTSPStreamPrivate *priv;
3147 GstElement *element;
3149 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3150 priv = stream->priv;
3151 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3152 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3154 g_mutex_lock (&priv->lock);
3155 if (priv->appsrc[0])
3156 element = gst_object_ref (priv->appsrc[0]);
3159 g_mutex_unlock (&priv->lock);
3162 if (priv->appsrc_base_time[0] == -1) {
3163 /* Take current running_time. This timestamp will be put on
3164 * the first buffer of each stream because we are a live source and so we
3165 * timestamp with the running_time. When we are dealing with TCP, we also
3166 * only timestamp the first buffer (using the DISCONT flag) because a server
3167 * typically bursts data, for which we don't want to compensate by speeding
3168 * up the media. The other timestamps will be interpollated from this one
3169 * using the RTP timestamps. */
3170 GST_OBJECT_LOCK (element);
3171 if (GST_ELEMENT_CLOCK (element)) {
3173 GstClockTime base_time;
3175 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3176 base_time = GST_ELEMENT_CAST (element)->base_time;
3178 priv->appsrc_base_time[0] = now - base_time;
3179 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3180 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3181 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3182 GST_TIME_ARGS (base_time));
3184 GST_OBJECT_UNLOCK (element);
3187 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3188 gst_object_unref (element);
3196 * gst_rtsp_stream_recv_rtcp:
3197 * @stream: a #GstRTSPStream
3198 * @buffer: (transfer full): a #GstBuffer
3200 * Handle an RTCP buffer for the stream. This method is usually called when a
3201 * message has been received from a client using the TCP transport.
3203 * This function takes ownership of @buffer.
3205 * Returns: a GstFlowReturn.
3208 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3210 GstRTSPStreamPrivate *priv;
3212 GstElement *element;
3214 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3215 priv = stream->priv;
3216 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3218 if (priv->joined_bin == NULL) {
3219 gst_buffer_unref (buffer);
3220 return GST_FLOW_NOT_LINKED;
3222 g_mutex_lock (&priv->lock);
3223 if (priv->appsrc[1])
3224 element = gst_object_ref (priv->appsrc[1]);
3227 g_mutex_unlock (&priv->lock);
3230 if (priv->appsrc_base_time[1] == -1) {
3231 /* Take current running_time. This timestamp will be put on
3232 * the first buffer of each stream because we are a live source and so we
3233 * timestamp with the running_time. When we are dealing with TCP, we also
3234 * only timestamp the first buffer (using the DISCONT flag) because a server
3235 * typically bursts data, for which we don't want to compensate by speeding
3236 * up the media. The other timestamps will be interpollated from this one
3237 * using the RTP timestamps. */
3238 GST_OBJECT_LOCK (element);
3239 if (GST_ELEMENT_CLOCK (element)) {
3241 GstClockTime base_time;
3243 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3244 base_time = GST_ELEMENT_CAST (element)->base_time;
3246 priv->appsrc_base_time[1] = now - base_time;
3247 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3248 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3249 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3250 GST_TIME_ARGS (base_time));
3252 GST_OBJECT_UNLOCK (element);
3255 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3256 gst_object_unref (element);
3259 gst_buffer_unref (buffer);
3264 /* must be called with lock */
3266 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3269 GstRTSPStreamPrivate *priv = stream->priv;
3270 const GstRTSPTransport *tr;
3272 tr = gst_rtsp_stream_transport_get_transport (trans);
3274 switch (tr->lower_transport) {
3275 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3278 if (!check_mcast_part_for_transport (stream, tr))
3280 priv->transports = g_list_prepend (priv->transports, trans);
3282 priv->transports = g_list_remove (priv->transports, trans);
3286 case GST_RTSP_LOWER_TRANS_UDP:
3292 dest = tr->destination;
3293 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3297 } else if (priv->client_side) {
3298 /* In client side mode the 'destination' is the RTSP server, so send
3300 min = tr->server_port.min;
3301 max = tr->server_port.max;
3303 min = tr->client_port.min;
3304 max = tr->client_port.max;
3309 GST_INFO ("setting ttl-mc %d", ttl);
3310 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3311 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3313 GST_INFO ("adding %s:%d-%d", dest, min, max);
3314 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3315 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3316 priv->transports = g_list_prepend (priv->transports, trans);
3318 GST_INFO ("removing %s:%d-%d", dest, min, max);
3319 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3320 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3321 priv->transports = g_list_remove (priv->transports, trans);
3323 priv->transports_cookie++;
3326 case GST_RTSP_LOWER_TRANS_TCP:
3328 GST_INFO ("adding TCP %s", tr->destination);
3329 priv->transports = g_list_prepend (priv->transports, trans);
3331 GST_INFO ("removing TCP %s", tr->destination);
3332 priv->transports = g_list_remove (priv->transports, trans);
3334 priv->transports_cookie++;
3337 goto unknown_transport;
3344 GST_INFO ("Unknown transport %d", tr->lower_transport);
3355 * gst_rtsp_stream_add_transport:
3356 * @stream: a #GstRTSPStream
3357 * @trans: (transfer none): a #GstRTSPStreamTransport
3359 * Add the transport in @trans to @stream. The media of @stream will
3360 * then also be send to the values configured in @trans.
3362 * @stream must be joined to a bin.
3364 * @trans must contain a valid #GstRTSPTransport.
3366 * Returns: %TRUE if @trans was added
3369 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3370 GstRTSPStreamTransport * trans)
3372 GstRTSPStreamPrivate *priv;
3375 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3376 priv = stream->priv;
3377 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3378 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3380 g_mutex_lock (&priv->lock);
3381 res = update_transport (stream, trans, TRUE);
3382 g_mutex_unlock (&priv->lock);
3388 * gst_rtsp_stream_remove_transport:
3389 * @stream: a #GstRTSPStream
3390 * @trans: (transfer none): a #GstRTSPStreamTransport
3392 * Remove the transport in @trans from @stream. The media of @stream will
3393 * not be sent to the values configured in @trans.
3395 * @stream must be joined to a bin.
3397 * @trans must contain a valid #GstRTSPTransport.
3399 * Returns: %TRUE if @trans was removed
3402 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3403 GstRTSPStreamTransport * trans)
3405 GstRTSPStreamPrivate *priv;
3408 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3409 priv = stream->priv;
3410 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3411 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3413 g_mutex_lock (&priv->lock);
3414 res = update_transport (stream, trans, FALSE);
3415 g_mutex_unlock (&priv->lock);
3421 * gst_rtsp_stream_update_crypto:
3422 * @stream: a #GstRTSPStream
3424 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3426 * Update the new crypto information for @ssrc in @stream. If information
3427 * for @ssrc did not exist, it will be added. If information
3428 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3429 * be removed from @stream.
3431 * Returns: %TRUE if @crypto could be updated
3434 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3435 guint ssrc, GstCaps * crypto)
3437 GstRTSPStreamPrivate *priv;
3439 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3440 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3442 priv = stream->priv;
3444 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3446 g_mutex_lock (&priv->lock);
3448 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3449 gst_caps_ref (crypto));
3451 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3452 g_mutex_unlock (&priv->lock);
3458 * gst_rtsp_stream_get_rtp_socket:
3459 * @stream: a #GstRTSPStream
3460 * @family: the socket family
3462 * Get the RTP socket from @stream for a @family.
3464 * @stream must be joined to a bin.
3466 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3467 * socket could be allocated for @family. Unref after usage
3470 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3472 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3476 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3477 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3478 family == G_SOCKET_FAMILY_IPV6, NULL);
3479 g_return_val_if_fail (priv->udpsink[0], NULL);
3481 if (family == G_SOCKET_FAMILY_IPV6)
3486 g_object_get (priv->udpsink[0], name, &socket, NULL);
3492 * gst_rtsp_stream_get_rtcp_socket:
3493 * @stream: a #GstRTSPStream
3494 * @family: the socket family
3496 * Get the RTCP socket from @stream for a @family.
3498 * @stream must be joined to a bin.
3500 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3501 * socket could be allocated for @family. Unref after usage
3504 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3506 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3510 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3511 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3512 family == G_SOCKET_FAMILY_IPV6, NULL);
3513 g_return_val_if_fail (priv->udpsink[1], NULL);
3515 if (family == G_SOCKET_FAMILY_IPV6)
3520 g_object_get (priv->udpsink[1], name, &socket, NULL);
3526 * gst_rtsp_stream_set_seqnum:
3527 * @stream: a #GstRTSPStream
3528 * @seqnum: a new sequence number
3530 * Configure the sequence number in the payloader of @stream to @seqnum.
3533 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3535 GstRTSPStreamPrivate *priv;
3537 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3539 priv = stream->priv;
3541 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3545 * gst_rtsp_stream_get_seqnum:
3546 * @stream: a #GstRTSPStream
3548 * Get the configured sequence number in the payloader of @stream.
3550 * Returns: the sequence number of the payloader.
3553 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3555 GstRTSPStreamPrivate *priv;
3558 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3560 priv = stream->priv;
3562 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3568 * gst_rtsp_stream_transport_filter:
3569 * @stream: a #GstRTSPStream
3570 * @func: (scope call) (allow-none): a callback
3571 * @user_data: (closure): user data passed to @func
3573 * Call @func for each transport managed by @stream. The result value of @func
3574 * determines what happens to the transport. @func will be called with @stream
3575 * locked so no further actions on @stream can be performed from @func.
3577 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3580 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3582 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3583 * will also be added with an additional ref to the result #GList of this
3586 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3588 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3589 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3590 * element in the #GList should be unreffed before the list is freed.
3593 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3594 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3596 GstRTSPStreamPrivate *priv;
3597 GList *result, *walk, *next;
3598 GHashTable *visited = NULL;
3601 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3603 priv = stream->priv;
3607 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3609 g_mutex_lock (&priv->lock);
3611 cookie = priv->transports_cookie;
3612 for (walk = priv->transports; walk; walk = next) {
3613 GstRTSPStreamTransport *trans = walk->data;
3614 GstRTSPFilterResult res;
3617 next = g_list_next (walk);
3620 /* only visit each transport once */
3621 if (g_hash_table_contains (visited, trans))
3624 g_hash_table_add (visited, g_object_ref (trans));
3625 g_mutex_unlock (&priv->lock);
3627 res = func (stream, trans, user_data);
3629 g_mutex_lock (&priv->lock);
3631 res = GST_RTSP_FILTER_REF;
3633 changed = (cookie != priv->transports_cookie);
3636 case GST_RTSP_FILTER_REMOVE:
3637 update_transport (stream, trans, FALSE);
3639 case GST_RTSP_FILTER_REF:
3640 result = g_list_prepend (result, g_object_ref (trans));
3642 case GST_RTSP_FILTER_KEEP:
3649 g_mutex_unlock (&priv->lock);
3652 g_hash_table_unref (visited);
3657 static GstPadProbeReturn
3658 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3660 GstRTSPStreamPrivate *priv;
3661 GstRTSPStream *stream;
3664 priv = stream->priv;
3666 GST_DEBUG_OBJECT (pad, "now blocking");
3668 g_mutex_lock (&priv->lock);
3669 priv->blocking = TRUE;
3670 g_mutex_unlock (&priv->lock);
3672 gst_element_post_message (priv->payloader,
3673 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3674 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3676 return GST_PAD_PROBE_OK;
3680 * gst_rtsp_stream_set_blocked:
3681 * @stream: a #GstRTSPStream
3682 * @blocked: boolean indicating we should block or unblock
3684 * Blocks or unblocks the dataflow on @stream.
3686 * Returns: %TRUE on success
3689 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3691 GstRTSPStreamPrivate *priv;
3693 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3695 priv = stream->priv;
3697 g_mutex_lock (&priv->lock);
3699 priv->blocking = FALSE;
3700 if (priv->blocked_id == 0) {
3701 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3702 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3703 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3704 g_object_ref (stream), g_object_unref);
3707 if (priv->blocked_id != 0) {
3708 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3709 priv->blocked_id = 0;
3710 priv->blocking = FALSE;
3713 g_mutex_unlock (&priv->lock);
3719 * gst_rtsp_stream_is_blocking:
3720 * @stream: a #GstRTSPStream
3722 * Check if @stream is blocking on a #GstBuffer.
3724 * Returns: %TRUE if @stream is blocking
3727 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3729 GstRTSPStreamPrivate *priv;
3732 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3734 priv = stream->priv;
3736 g_mutex_lock (&priv->lock);
3737 result = priv->blocking;
3738 g_mutex_unlock (&priv->lock);
3744 * gst_rtsp_stream_query_position:
3745 * @stream: a #GstRTSPStream
3747 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3748 * the RTP parts of the pipeline and not the RTCP parts.
3750 * Returns: %TRUE if the position could be queried
3753 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3755 GstRTSPStreamPrivate *priv;
3759 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3761 priv = stream->priv;
3763 g_mutex_lock (&priv->lock);
3764 /* depending on the transport type, it should query corresponding sink */
3765 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3766 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3767 sink = priv->udpsink[0];
3769 sink = priv->appsink[0];
3772 gst_object_ref (sink);
3773 g_mutex_unlock (&priv->lock);
3778 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3779 gst_object_unref (sink);
3785 * gst_rtsp_stream_query_stop:
3786 * @stream: a #GstRTSPStream
3788 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3789 * the RTP parts of the pipeline and not the RTCP parts.
3791 * Returns: %TRUE if the stop could be queried
3794 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3796 GstRTSPStreamPrivate *priv;
3801 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3803 priv = stream->priv;
3805 g_mutex_lock (&priv->lock);
3806 /* depending on the transport type, it should query corresponding sink */
3807 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3808 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3809 sink = priv->udpsink[0];
3811 sink = priv->appsink[0];
3814 gst_object_ref (sink);
3815 g_mutex_unlock (&priv->lock);
3820 query = gst_query_new_segment (GST_FORMAT_TIME);
3821 if ((ret = gst_element_query (sink, query))) {
3824 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3825 if (format != GST_FORMAT_TIME)
3828 gst_query_unref (query);
3829 gst_object_unref (sink);