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];
102 GSocket *socket_v4[2];
103 GSocket *socket_v6[2];
105 /* for UDP multicast */
106 GstElement *mcast_udpsrc_v4[2];
107 GstElement *mcast_udpsrc_v6[2];
108 GstElement *mcast_udpqueue[2];
109 GstElement *mcast_udpsink[2];
110 GSocket *mcast_socket_v4[2];
111 GSocket *mcast_socket_v6[2];
113 /* for TCP transport */
114 GstElement *appsrc[2];
115 GstClockTime appsrc_base_time[2];
116 GstElement *appqueue[2];
117 GstElement *appsink[2];
120 GstElement *funnel[2];
125 GstClockTime rtx_time;
127 /* pool used to manage unicast and multicast addresses */
128 GstRTSPAddressPool *pool;
130 /* unicast server addr/port */
131 GstRTSPAddress *server_addr_v4;
132 GstRTSPAddress *server_addr_v6;
134 /* multicast addresses */
135 GstRTSPAddress *mcast_addr_v4;
136 GstRTSPAddress *mcast_addr_v6;
138 gchar *multicast_iface;
140 /* the caps of the stream */
144 /* transports we stream to */
147 guint transports_cookie;
149 GList *tr_cache_rtcp;
150 guint tr_cache_cookie_rtp;
151 guint tr_cache_cookie_rtcp;
155 /* stream blocking */
156 gulong blocked_id[2];
159 /* current stream postion */
160 GstClockTime position;
162 /* pt->caps map for RECORD streams */
165 GstRTSPPublishClockMode publish_clock_mode;
168 #define DEFAULT_CONTROL NULL
169 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
170 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
171 GST_RTSP_LOWER_TRANS_TCP
184 SIGNAL_NEW_RTP_ENCODER,
185 SIGNAL_NEW_RTCP_ENCODER,
189 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
190 #define GST_CAT_DEFAULT rtsp_stream_debug
192 static GQuark ssrc_stream_map_key;
194 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
195 GValue * value, GParamSpec * pspec);
196 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
197 const GValue * value, GParamSpec * pspec);
199 static void gst_rtsp_stream_finalize (GObject * obj);
201 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
203 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
206 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
208 GObjectClass *gobject_class;
210 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
212 gobject_class = G_OBJECT_CLASS (klass);
214 gobject_class->get_property = gst_rtsp_stream_get_property;
215 gobject_class->set_property = gst_rtsp_stream_set_property;
216 gobject_class->finalize = gst_rtsp_stream_finalize;
218 g_object_class_install_property (gobject_class, PROP_CONTROL,
219 g_param_spec_string ("control", "Control",
220 "The control string for this stream", DEFAULT_CONTROL,
221 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 g_object_class_install_property (gobject_class, PROP_PROFILES,
224 g_param_spec_flags ("profiles", "Profiles",
225 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
226 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
229 g_param_spec_flags ("protocols", "Protocols",
230 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
231 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
234 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
236 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
238 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
239 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
240 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
241 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
243 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
245 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
249 gst_rtsp_stream_init (GstRTSPStream * stream)
251 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
253 GST_DEBUG ("new stream %p", stream);
258 priv->control = g_strdup (DEFAULT_CONTROL);
259 priv->profiles = DEFAULT_PROFILES;
260 priv->protocols = DEFAULT_PROTOCOLS;
261 priv->publish_clock_mode = GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK;
263 g_mutex_init (&priv->lock);
265 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
266 NULL, (GDestroyNotify) gst_caps_unref);
267 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
268 (GDestroyNotify) gst_caps_unref);
272 gst_rtsp_stream_finalize (GObject * obj)
274 GstRTSPStream *stream;
275 GstRTSPStreamPrivate *priv;
277 stream = GST_RTSP_STREAM (obj);
280 GST_DEBUG ("finalize stream %p", stream);
282 /* we really need to be unjoined now */
283 g_return_if_fail (priv->joined_bin == NULL);
285 if (priv->mcast_addr_v4)
286 gst_rtsp_address_free (priv->mcast_addr_v4);
287 if (priv->mcast_addr_v6)
288 gst_rtsp_address_free (priv->mcast_addr_v6);
289 if (priv->server_addr_v4)
290 gst_rtsp_address_free (priv->server_addr_v4);
291 if (priv->server_addr_v6)
292 gst_rtsp_address_free (priv->server_addr_v6);
294 g_object_unref (priv->pool);
296 g_object_unref (priv->rtxsend);
298 if (priv->socket_v4[0])
299 g_object_unref (priv->socket_v4[0]);
300 if (priv->socket_v4[1])
301 g_object_unref (priv->socket_v4[1]);
302 if (priv->socket_v6[0])
303 g_object_unref (priv->socket_v6[0]);
304 if (priv->socket_v6[1])
305 g_object_unref (priv->socket_v6[1]);
306 if (priv->mcast_socket_v4[0])
307 g_object_unref (priv->mcast_socket_v4[0]);
308 if (priv->mcast_socket_v4[1])
309 g_object_unref (priv->mcast_socket_v4[1]);
310 if (priv->mcast_socket_v6[0])
311 g_object_unref (priv->mcast_socket_v6[0]);
312 if (priv->mcast_socket_v6[1])
313 g_object_unref (priv->mcast_socket_v6[1]);
315 g_free (priv->multicast_iface);
317 gst_object_unref (priv->payloader);
319 gst_object_unref (priv->srcpad);
321 gst_object_unref (priv->sinkpad);
322 g_free (priv->control);
323 g_mutex_clear (&priv->lock);
325 g_hash_table_unref (priv->keys);
326 g_hash_table_destroy (priv->ptmap);
328 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
332 gst_rtsp_stream_get_property (GObject * object, guint propid,
333 GValue * value, GParamSpec * pspec)
335 GstRTSPStream *stream = GST_RTSP_STREAM (object);
339 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
342 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
345 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
353 gst_rtsp_stream_set_property (GObject * object, guint propid,
354 const GValue * value, GParamSpec * pspec)
356 GstRTSPStream *stream = GST_RTSP_STREAM (object);
360 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
363 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
366 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
374 * gst_rtsp_stream_new:
377 * @payloader: a #GstElement
379 * Create a new media stream with index @idx that handles RTP data on
380 * @pad and has a payloader element @payloader if @pad is a source pad
381 * or a depayloader element @payloader if @pad is a sink pad.
383 * Returns: (transfer full): a new #GstRTSPStream
386 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
388 GstRTSPStreamPrivate *priv;
389 GstRTSPStream *stream;
391 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
392 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
394 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
397 priv->payloader = gst_object_ref (payloader);
398 if (GST_PAD_IS_SRC (pad))
399 priv->srcpad = gst_object_ref (pad);
401 priv->sinkpad = gst_object_ref (pad);
407 * gst_rtsp_stream_get_index:
408 * @stream: a #GstRTSPStream
410 * Get the stream index.
412 * Return: the stream index.
415 gst_rtsp_stream_get_index (GstRTSPStream * stream)
417 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
419 return stream->priv->idx;
423 * gst_rtsp_stream_get_pt:
424 * @stream: a #GstRTSPStream
426 * Get the stream payload type.
428 * Return: the stream payload type.
431 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
433 GstRTSPStreamPrivate *priv;
436 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
440 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
446 * gst_rtsp_stream_get_srcpad:
447 * @stream: a #GstRTSPStream
449 * Get the srcpad associated with @stream.
451 * Returns: (transfer full): the srcpad. Unref after usage.
454 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
456 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
458 if (!stream->priv->srcpad)
461 return gst_object_ref (stream->priv->srcpad);
465 * gst_rtsp_stream_get_sinkpad:
466 * @stream: a #GstRTSPStream
468 * Get the sinkpad associated with @stream.
470 * Returns: (transfer full): the sinkpad. Unref after usage.
473 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
475 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
477 if (!stream->priv->sinkpad)
480 return gst_object_ref (stream->priv->sinkpad);
484 * gst_rtsp_stream_get_control:
485 * @stream: a #GstRTSPStream
487 * Get the control string to identify this stream.
489 * Returns: (transfer full): the control string. g_free() after usage.
492 gst_rtsp_stream_get_control (GstRTSPStream * stream)
494 GstRTSPStreamPrivate *priv;
497 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
501 g_mutex_lock (&priv->lock);
502 if ((result = g_strdup (priv->control)) == NULL)
503 result = g_strdup_printf ("stream=%u", priv->idx);
504 g_mutex_unlock (&priv->lock);
510 * gst_rtsp_stream_set_control:
511 * @stream: a #GstRTSPStream
512 * @control: a control string
514 * Set the control string in @stream.
517 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
519 GstRTSPStreamPrivate *priv;
521 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
525 g_mutex_lock (&priv->lock);
526 g_free (priv->control);
527 priv->control = g_strdup (control);
528 g_mutex_unlock (&priv->lock);
532 * gst_rtsp_stream_has_control:
533 * @stream: a #GstRTSPStream
534 * @control: a control string
536 * Check if @stream has the control string @control.
538 * Returns: %TRUE is @stream has @control as the control string
541 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
543 GstRTSPStreamPrivate *priv;
546 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
550 g_mutex_lock (&priv->lock);
552 res = (g_strcmp0 (priv->control, control) == 0);
556 if (sscanf (control, "stream=%u", &streamid) > 0)
557 res = (streamid == priv->idx);
561 g_mutex_unlock (&priv->lock);
567 * gst_rtsp_stream_set_mtu:
568 * @stream: a #GstRTSPStream
571 * Configure the mtu in the payloader of @stream to @mtu.
574 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
576 GstRTSPStreamPrivate *priv;
578 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
582 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
584 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
588 * gst_rtsp_stream_get_mtu:
589 * @stream: a #GstRTSPStream
591 * Get the configured MTU in the payloader of @stream.
593 * Returns: the MTU of the payloader.
596 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
598 GstRTSPStreamPrivate *priv;
601 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
605 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
610 /* Update the dscp qos property on the udp sinks */
612 update_dscp_qos (GstRTSPStream * stream, GstElement ** udpsink)
614 GstRTSPStreamPrivate *priv;
619 g_object_set (G_OBJECT (*udpsink), "qos-dscp", priv->dscp_qos, NULL);
624 * gst_rtsp_stream_set_dscp_qos:
625 * @stream: a #GstRTSPStream
626 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
628 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
631 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
633 GstRTSPStreamPrivate *priv;
635 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
639 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
641 if (dscp_qos < -1 || dscp_qos > 63) {
642 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
646 priv->dscp_qos = dscp_qos;
648 update_dscp_qos (stream, priv->udpsink);
652 * gst_rtsp_stream_get_dscp_qos:
653 * @stream: a #GstRTSPStream
655 * Get the configured DSCP QoS in of the outgoing sockets.
657 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
660 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
662 GstRTSPStreamPrivate *priv;
664 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
668 return priv->dscp_qos;
672 * gst_rtsp_stream_is_transport_supported:
673 * @stream: a #GstRTSPStream
674 * @transport: (transfer none): a #GstRTSPTransport
676 * Check if @transport can be handled by stream
678 * Returns: %TRUE if @transport can be handled by @stream.
681 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
682 GstRTSPTransport * transport)
684 GstRTSPStreamPrivate *priv;
686 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
690 g_mutex_lock (&priv->lock);
691 if (transport->trans != GST_RTSP_TRANS_RTP)
692 goto unsupported_transmode;
694 if (!(transport->profile & priv->profiles))
695 goto unsupported_profile;
697 if (!(transport->lower_transport & priv->protocols))
698 goto unsupported_ltrans;
700 g_mutex_unlock (&priv->lock);
705 unsupported_transmode:
707 GST_DEBUG ("unsupported transport mode %d", transport->trans);
708 g_mutex_unlock (&priv->lock);
713 GST_DEBUG ("unsupported profile %d", transport->profile);
714 g_mutex_unlock (&priv->lock);
719 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
720 g_mutex_unlock (&priv->lock);
726 * gst_rtsp_stream_set_profiles:
727 * @stream: a #GstRTSPStream
728 * @profiles: the new profiles
730 * Configure the allowed profiles for @stream.
733 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
735 GstRTSPStreamPrivate *priv;
737 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
741 g_mutex_lock (&priv->lock);
742 priv->profiles = profiles;
743 g_mutex_unlock (&priv->lock);
747 * gst_rtsp_stream_get_profiles:
748 * @stream: a #GstRTSPStream
750 * Get the allowed profiles of @stream.
752 * Returns: a #GstRTSPProfile
755 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
757 GstRTSPStreamPrivate *priv;
760 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
764 g_mutex_lock (&priv->lock);
765 res = priv->profiles;
766 g_mutex_unlock (&priv->lock);
772 * gst_rtsp_stream_set_protocols:
773 * @stream: a #GstRTSPStream
774 * @protocols: the new flags
776 * Configure the allowed lower transport for @stream.
779 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
780 GstRTSPLowerTrans protocols)
782 GstRTSPStreamPrivate *priv;
784 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
788 g_mutex_lock (&priv->lock);
789 priv->protocols = protocols;
790 g_mutex_unlock (&priv->lock);
794 * gst_rtsp_stream_get_protocols:
795 * @stream: a #GstRTSPStream
797 * Get the allowed protocols of @stream.
799 * Returns: a #GstRTSPLowerTrans
802 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
804 GstRTSPStreamPrivate *priv;
805 GstRTSPLowerTrans res;
807 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
808 GST_RTSP_LOWER_TRANS_UNKNOWN);
812 g_mutex_lock (&priv->lock);
813 res = priv->protocols;
814 g_mutex_unlock (&priv->lock);
820 * gst_rtsp_stream_set_address_pool:
821 * @stream: a #GstRTSPStream
822 * @pool: (transfer none): a #GstRTSPAddressPool
824 * configure @pool to be used as the address pool of @stream.
827 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
828 GstRTSPAddressPool * pool)
830 GstRTSPStreamPrivate *priv;
831 GstRTSPAddressPool *old;
833 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
837 GST_LOG_OBJECT (stream, "set address pool %p", pool);
839 g_mutex_lock (&priv->lock);
840 if ((old = priv->pool) != pool)
841 priv->pool = pool ? g_object_ref (pool) : NULL;
844 g_mutex_unlock (&priv->lock);
847 g_object_unref (old);
851 * gst_rtsp_stream_get_address_pool:
852 * @stream: a #GstRTSPStream
854 * Get the #GstRTSPAddressPool used as the address pool of @stream.
856 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
860 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
862 GstRTSPStreamPrivate *priv;
863 GstRTSPAddressPool *result;
865 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
869 g_mutex_lock (&priv->lock);
870 if ((result = priv->pool))
871 g_object_ref (result);
872 g_mutex_unlock (&priv->lock);
878 * gst_rtsp_stream_set_multicast_iface:
879 * @stream: a #GstRTSPStream
880 * @multicast_iface: (transfer none): a multicast interface name
882 * configure @multicast_iface to be used for @stream.
885 gst_rtsp_stream_set_multicast_iface (GstRTSPStream * stream,
886 const gchar * multicast_iface)
888 GstRTSPStreamPrivate *priv;
891 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
895 GST_LOG_OBJECT (stream, "set multicast iface %s",
896 GST_STR_NULL (multicast_iface));
898 g_mutex_lock (&priv->lock);
899 if ((old = priv->multicast_iface) != multicast_iface)
900 priv->multicast_iface = multicast_iface ? g_strdup (multicast_iface) : NULL;
903 g_mutex_unlock (&priv->lock);
910 * gst_rtsp_stream_get_multicast_iface:
911 * @stream: a #GstRTSPStream
913 * Get the multicast interface used for @stream.
915 * Returns: (transfer full): the multicast interface for @stream. g_free() after
919 gst_rtsp_stream_get_multicast_iface (GstRTSPStream * stream)
921 GstRTSPStreamPrivate *priv;
924 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
928 g_mutex_lock (&priv->lock);
929 if ((result = priv->multicast_iface))
930 result = g_strdup (result);
931 g_mutex_unlock (&priv->lock);
937 * gst_rtsp_stream_get_multicast_address:
938 * @stream: a #GstRTSPStream
939 * @family: the #GSocketFamily
941 * Get the multicast address of @stream for @family. The original
942 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
943 * won't release the address from the pool.
945 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
946 * or %NULL when no address could be allocated. gst_rtsp_address_free()
950 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
951 GSocketFamily family)
953 GstRTSPStreamPrivate *priv;
954 GstRTSPAddress *result;
955 GstRTSPAddress **addrp;
956 GstRTSPAddressFlags flags;
958 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
962 g_mutex_lock (&stream->priv->lock);
964 if (family == G_SOCKET_FAMILY_IPV6) {
965 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
966 addrp = &priv->mcast_addr_v6;
968 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
969 addrp = &priv->mcast_addr_v4;
972 if (*addrp == NULL) {
973 if (priv->pool == NULL)
976 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
978 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
982 /* FIXME: Also reserve the same port with unicast ANY address, since that's
983 * where we are going to bind our socket. Probably loop until we find a port
984 * available in both mcast and unicast pools. Maybe GstRTSPAddressPool
985 * should do it for us when both GST_RTSP_ADDRESS_FLAG_MULTICAST and
986 * GST_RTSP_ADDRESS_FLAG_UNICAST are givent. */
988 result = gst_rtsp_address_copy (*addrp);
990 g_mutex_unlock (&stream->priv->lock);
997 GST_ERROR_OBJECT (stream, "no address pool specified");
998 g_mutex_unlock (&stream->priv->lock);
1003 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
1004 g_mutex_unlock (&stream->priv->lock);
1010 * gst_rtsp_stream_reserve_address:
1011 * @stream: a #GstRTSPStream
1012 * @address: an address
1017 * Reserve @address and @port as the address and port of @stream. The original
1018 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
1019 * won't release the address from the pool.
1021 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
1022 * the address could be reserved. gst_rtsp_address_free() after usage.
1025 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
1026 const gchar * address, guint port, guint n_ports, guint ttl)
1028 GstRTSPStreamPrivate *priv;
1029 GstRTSPAddress *result;
1031 GSocketFamily family;
1032 GstRTSPAddress **addrp;
1034 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1035 g_return_val_if_fail (address != NULL, NULL);
1036 g_return_val_if_fail (port > 0, NULL);
1037 g_return_val_if_fail (n_ports > 0, NULL);
1038 g_return_val_if_fail (ttl > 0, NULL);
1040 priv = stream->priv;
1042 addr = g_inet_address_new_from_string (address);
1044 GST_ERROR ("failed to get inet addr from %s", address);
1045 family = G_SOCKET_FAMILY_IPV4;
1047 family = g_inet_address_get_family (addr);
1048 g_object_unref (addr);
1051 if (family == G_SOCKET_FAMILY_IPV6)
1052 addrp = &priv->mcast_addr_v6;
1054 addrp = &priv->mcast_addr_v4;
1056 g_mutex_lock (&priv->lock);
1057 if (*addrp == NULL) {
1058 GstRTSPAddressPoolResult res;
1060 if (priv->pool == NULL)
1063 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
1064 port, n_ports, ttl, addrp);
1065 if (res != GST_RTSP_ADDRESS_POOL_OK)
1068 /* FIXME: Also reserve the same port with unicast ANY address, since that's
1069 * where we are going to bind our socket. */
1071 if (g_ascii_strcasecmp ((*addrp)->address, address) ||
1072 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
1073 (*addrp)->ttl != ttl)
1074 goto different_address;
1076 result = gst_rtsp_address_copy (*addrp);
1077 g_mutex_unlock (&priv->lock);
1084 GST_ERROR_OBJECT (stream, "no address pool specified");
1085 g_mutex_unlock (&priv->lock);
1090 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1092 g_mutex_unlock (&priv->lock);
1097 GST_ERROR_OBJECT (stream,
1098 "address %s is not the same as %s that was already reserved",
1099 address, (*addrp)->address);
1100 g_mutex_unlock (&priv->lock);
1105 /* must be called with lock */
1107 set_socket_for_udpsink (GstElement * udpsink, GSocket * socket,
1108 GSocketFamily family)
1110 const gchar *multisink_socket;
1112 if (family == G_SOCKET_FAMILY_IPV6)
1113 multisink_socket = "socket-v6";
1115 multisink_socket = "socket";
1117 g_object_set (G_OBJECT (udpsink), multisink_socket, socket, NULL);
1120 /* must be called with lock */
1122 set_multicast_socket_for_udpsink (GstElement * udpsink, GSocket * socket,
1123 GSocketFamily family, const gchar * multicast_iface,
1124 const gchar * addr_str, gint port)
1126 set_socket_for_udpsink (udpsink, socket, family);
1128 if (multicast_iface) {
1129 g_object_set (G_OBJECT (udpsink), "multicast-iface",
1130 multicast_iface, NULL);
1133 g_signal_emit_by_name (udpsink, "add", addr_str, port, NULL);
1137 /* must be called with lock */
1139 set_unicast_socket_for_udpsink (GstElement * udpsink, GSocket * socket,
1140 GSocketFamily family)
1142 set_socket_for_udpsink (udpsink, socket, family);
1146 get_port_from_socket (GSocket * socket)
1149 GSocketAddress *sockaddr;
1152 GST_DEBUG ("socket: %p", socket);
1153 sockaddr = g_socket_get_local_address (socket, &err);
1154 if (sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (sockaddr)) {
1155 g_clear_object (&sockaddr);
1156 GST_ERROR ("failed to get sockaddr: %s", err->message);
1161 port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
1162 g_object_unref (sockaddr);
1169 create_and_configure_udpsink (GstRTSPStream * stream, GstElement ** udpsink,
1170 GSocket *socket_v4, GSocket *socket_v6, gboolean multicast, gboolean is_rtp)
1172 GstRTSPStreamPrivate *priv = stream->priv;
1174 *udpsink = gst_element_factory_make ("multiudpsink", NULL);
1177 goto no_udp_protocol;
1179 /* configure sinks */
1181 g_object_set (G_OBJECT (*udpsink), "close-socket", FALSE, NULL);
1183 g_object_set (G_OBJECT (*udpsink), "send-duplicates", FALSE, NULL);
1186 g_object_set (G_OBJECT (*udpsink), "buffer-size", priv->buffer_size, NULL);
1188 g_object_set (G_OBJECT (*udpsink), "sync", FALSE, NULL);
1190 /* Needs to be async for RECORD streams, otherwise we will never go to
1191 * PLAYING because the sinks will wait for data while the udpsrc can't
1192 * provide data with timestamps in PAUSED. */
1193 if (!is_rtp || priv->sinkpad)
1194 g_object_set (G_OBJECT (*udpsink), "async", FALSE, NULL);
1197 /* join multicast group when adding clients, so we'll start receiving from it.
1198 * We cannot rely on the udpsrc to join the group since its socket is always a
1199 * local unicast one. */
1200 g_object_set (G_OBJECT (*udpsink), "auto-multicast", TRUE, NULL);
1202 g_object_set (G_OBJECT (*udpsink), "loop", FALSE, NULL);
1205 /* update the dscp qos field in the sinks */
1206 update_dscp_qos (stream, udpsink);
1208 if (priv->server_addr_v4) {
1209 GST_DEBUG_OBJECT (stream,
1210 "udp IPv4, configure udpsinks");
1211 set_unicast_socket_for_udpsink (*udpsink, socket_v4,
1212 G_SOCKET_FAMILY_IPV4);
1215 if (priv->server_addr_v6) {
1216 GST_DEBUG_OBJECT (stream,
1217 "udp IPv6, configure udpsinks");
1218 set_unicast_socket_for_udpsink (*udpsink, socket_v6,
1219 G_SOCKET_FAMILY_IPV6);
1224 if (priv->mcast_addr_v4) {
1225 GST_DEBUG_OBJECT (stream, "mcast IPv4, configure udpsinks");
1226 port = get_port_from_socket (socket_v4);
1228 goto get_port_failed;
1229 set_multicast_socket_for_udpsink (*udpsink, socket_v4,
1230 G_SOCKET_FAMILY_IPV4, priv->multicast_iface, priv->mcast_addr_v4->address, port);
1233 if (priv->mcast_addr_v6) {
1234 GST_DEBUG_OBJECT (stream, "mcast IPv6, configure udpsinks");
1235 port = get_port_from_socket (socket_v6);
1237 goto get_port_failed;
1238 set_multicast_socket_for_udpsink (*udpsink, socket_v6,
1239 G_SOCKET_FAMILY_IPV6, priv->multicast_iface, priv->mcast_addr_v6->address, port);
1249 GST_ERROR_OBJECT (stream, "failed to create udpsink element");
1254 GST_ERROR_OBJECT (stream, "failed to get udp port");
1259 /* must be called with lock */
1261 create_and_configure_udpsource (GstElement ** udpsrc,
1264 GstStateChangeReturn ret;
1266 g_assert (socket != NULL);
1268 *udpsrc = gst_element_factory_make ("udpsrc", NULL);
1269 if (*udpsrc == NULL)
1272 g_object_set (G_OBJECT (*udpsrc), "socket", socket, NULL);
1274 /* The udpsrc cannot do the join because its socket is always a local unicast
1275 * one. The udpsink sharing the same socket will do it for us. */
1276 g_object_set (G_OBJECT (*udpsrc), "auto-multicast", FALSE, NULL);
1278 g_object_set (G_OBJECT (*udpsrc), "loop", FALSE, NULL);
1280 g_object_set (G_OBJECT (*udpsrc), "close-socket", FALSE, NULL);
1282 ret = gst_element_set_state (*udpsrc, GST_STATE_READY);
1283 if (ret == GST_STATE_CHANGE_FAILURE)
1292 gst_element_set_state (*udpsrc, GST_STATE_NULL);
1293 g_clear_object (udpsrc);
1300 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1301 GSocket *socket_out[2], GstRTSPAddress ** server_addr_out,
1302 gboolean multicast, GstRTSPTransport * ct)
1304 GstRTSPStreamPrivate *priv = stream->priv;
1305 GSocket *rtp_socket = NULL;
1306 GSocket *rtcp_socket;
1307 gint tmp_rtp, tmp_rtcp;
1309 GList *rejected_addresses = NULL;
1310 GstRTSPAddress *addr = NULL;
1311 GInetAddress *inetaddr = NULL;
1312 GSocketAddress *rtp_sockaddr = NULL;
1313 GSocketAddress *rtcp_sockaddr = NULL;
1314 GstRTSPAddressPool *pool;
1319 /* Start with random port */
1322 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1323 G_SOCKET_PROTOCOL_UDP, NULL);
1325 goto no_udp_protocol;
1326 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1328 /* try to allocate 2 UDP ports, the RTP port should be an even
1329 * number and the RTCP port should be the next (uneven) port */
1332 if (rtp_socket == NULL) {
1333 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1334 G_SOCKET_PROTOCOL_UDP, NULL);
1336 goto no_udp_protocol;
1337 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1340 if ((pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) || multicast) {
1341 GstRTSPAddressFlags flags;
1344 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1349 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1351 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1353 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1355 if (family == G_SOCKET_FAMILY_IPV6)
1356 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1358 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1360 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1365 tmp_rtp = addr->port;
1367 g_clear_object (&inetaddr);
1368 /* FIXME: Does it really work with the IP_MULTICAST_ALL socket option and
1369 * socket control message set in udpsrc? */
1371 inetaddr = g_inet_address_new_any (family);
1373 inetaddr = g_inet_address_new_from_string (addr->address);
1381 if (inetaddr == NULL)
1382 inetaddr = g_inet_address_new_any (family);
1385 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1386 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1387 GST_DEBUG_OBJECT (stream, "rtp bind() failed, will try again");
1388 g_object_unref (rtp_sockaddr);
1391 g_object_unref (rtp_sockaddr);
1393 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1394 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1395 g_clear_object (&rtp_sockaddr);
1400 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1401 g_object_unref (rtp_sockaddr);
1403 /* check if port is even */
1404 if ((tmp_rtp & 1) != 0) {
1405 /* port not even, close and allocate another */
1407 g_clear_object (&rtp_socket);
1412 tmp_rtcp = tmp_rtp + 1;
1414 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1415 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1416 GST_DEBUG_OBJECT (stream, "rctp bind() failed, will try again");
1417 g_object_unref (rtcp_sockaddr);
1418 g_clear_object (&rtp_socket);
1421 g_object_unref (rtcp_sockaddr);
1424 addr = g_slice_new0 (GstRTSPAddress);
1425 addr->address = g_inet_address_to_string (inetaddr);
1426 addr->port = tmp_rtp;
1430 g_clear_object (&inetaddr);
1432 socket_out[0] = rtp_socket;
1433 socket_out[1] = rtcp_socket;
1434 *server_addr_out = addr;
1436 GST_DEBUG_OBJECT (stream, "allocated address: %s and ports: %d, %d", addr->address, tmp_rtp, tmp_rtcp);
1438 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1445 GST_ERROR_OBJECT (stream, "failed to allocate UDP ports: protocol error");
1450 GST_ERROR_OBJECT (stream, "failed to allocate UDP ports: no address pool specified");
1455 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
1460 GST_ERROR_OBJECT (stream, "failed to allocate UDP ports: no ports");
1465 GST_ERROR_OBJECT (stream, "failed to allocate UDP ports: socket error");
1471 g_object_unref (inetaddr);
1472 g_list_free_full (rejected_addresses,
1473 (GDestroyNotify) gst_rtsp_address_free);
1475 gst_rtsp_address_free (addr);
1477 g_object_unref (rtp_socket);
1479 g_object_unref (rtcp_socket);
1485 * gst_rtsp_stream_allocate_udp_sockets:
1486 * @stream: a #GstRTSPStream
1487 * @family: protocol family
1488 * @transport: transport method
1489 * @use_client_setttings: Whether to use client settings or not
1491 * Allocates RTP and RTCP ports.
1493 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1496 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1497 GSocketFamily family, GstRTSPTransport * ct,
1498 gboolean use_transport_settings)
1500 GstRTSPStreamPrivate *priv;
1501 gboolean ret = FALSE;
1502 GstRTSPLowerTrans transport;
1503 gboolean allocated = FALSE;
1505 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1506 g_return_val_if_fail (ct != NULL, FALSE);
1507 priv = stream->priv;
1509 transport = ct->lower_transport;
1511 g_mutex_lock (&priv->lock);
1513 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1514 if (family == G_SOCKET_FAMILY_IPV4 && priv->mcast_addr_v4)
1516 else if (family == G_SOCKET_FAMILY_IPV6 && priv->mcast_addr_v6)
1518 } else if (transport == GST_RTSP_LOWER_TRANS_UDP) {
1519 if (family == G_SOCKET_FAMILY_IPV4 && priv->server_addr_v4)
1521 else if (family == G_SOCKET_FAMILY_IPV6 && priv->server_addr_v6)
1526 g_mutex_unlock (&priv->lock);
1530 if (family == G_SOCKET_FAMILY_IPV4) {
1532 if (transport == GST_RTSP_LOWER_TRANS_UDP) {
1534 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_UDP, ipv4");
1535 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1536 priv->socket_v4, &priv->server_addr_v4, FALSE, ct);
1539 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_MCAST_UDP, ipv4");
1540 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1541 priv->mcast_socket_v4, &priv->mcast_addr_v4, TRUE, ct);
1545 if (transport == GST_RTSP_LOWER_TRANS_UDP) {
1547 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_UDP, ipv6");
1548 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1549 priv->socket_v6, &priv->server_addr_v6, FALSE, ct);
1553 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_MCAST_UDP, ipv6");
1554 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1555 priv->mcast_socket_v6, &priv->mcast_addr_v6, TRUE, ct);
1558 g_mutex_unlock (&priv->lock);
1564 * gst_rtsp_stream_set_client_side:
1565 * @stream: a #GstRTSPStream
1566 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1567 * an RTSP connection.
1569 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1570 * streams to an RTSP server via RECORD. This has the practical effect
1571 * of changing which UDP port numbers are used when setting up the local
1572 * side of the stream sending to be either the 'server' or 'client' pair
1573 * of a configured UDP transport.
1576 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1578 GstRTSPStreamPrivate *priv;
1580 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1581 priv = stream->priv;
1582 g_mutex_lock (&priv->lock);
1583 priv->client_side = client_side;
1584 g_mutex_unlock (&priv->lock);
1588 * gst_rtsp_stream_is_client_side:
1589 * @stream: a #GstRTSPStream
1591 * See gst_rtsp_stream_set_client_side()
1593 * Returns: TRUE if this #GstRTSPStream is client-side.
1596 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1598 GstRTSPStreamPrivate *priv;
1601 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1603 priv = stream->priv;
1604 g_mutex_lock (&priv->lock);
1605 ret = priv->client_side;
1606 g_mutex_unlock (&priv->lock);
1612 * gst_rtsp_stream_get_server_port:
1613 * @stream: a #GstRTSPStream
1614 * @server_port: (out): result server port
1615 * @family: the port family to get
1617 * Fill @server_port with the port pair used by the server. This function can
1618 * only be called when @stream has been joined.
1621 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1622 GstRTSPRange * server_port, GSocketFamily family)
1624 GstRTSPStreamPrivate *priv;
1626 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1627 priv = stream->priv;
1628 g_return_if_fail (priv->joined_bin != NULL);
1631 server_port->min = 0;
1632 server_port->max = 0;
1635 g_mutex_lock (&priv->lock);
1636 if (family == G_SOCKET_FAMILY_IPV4) {
1637 if (server_port && priv->server_addr_v4) {
1638 server_port->min = priv->server_addr_v4->port;
1640 priv->server_addr_v4->port + priv->server_addr_v4->n_ports - 1;
1643 if (server_port && priv->server_addr_v6) {
1644 server_port->min = priv->server_addr_v6->port;
1646 priv->server_addr_v6->port + priv->server_addr_v6->n_ports - 1;
1649 g_mutex_unlock (&priv->lock);
1653 * gst_rtsp_stream_get_rtpsession:
1654 * @stream: a #GstRTSPStream
1656 * Get the RTP session of this stream.
1658 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1661 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1663 GstRTSPStreamPrivate *priv;
1666 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1668 priv = stream->priv;
1670 g_mutex_lock (&priv->lock);
1671 if ((session = priv->session))
1672 g_object_ref (session);
1673 g_mutex_unlock (&priv->lock);
1679 * gst_rtsp_stream_get_srtp_encoder:
1680 * @stream: a #GstRTSPStream
1682 * Get the SRTP encoder for this stream.
1684 * Returns: (transfer full): The SRTP encoder for this stream. Unref after usage.
1687 gst_rtsp_stream_get_srtp_encoder (GstRTSPStream * stream)
1689 GstRTSPStreamPrivate *priv;
1690 GstElement *encoder;
1692 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1694 priv = stream->priv;
1696 g_mutex_lock (&priv->lock);
1697 if ((encoder = priv->srtpenc))
1698 g_object_ref (encoder);
1699 g_mutex_unlock (&priv->lock);
1705 * gst_rtsp_stream_get_ssrc:
1706 * @stream: a #GstRTSPStream
1707 * @ssrc: (out): result ssrc
1709 * Get the SSRC used by the RTP session of this stream. This function can only
1710 * be called when @stream has been joined.
1713 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1715 GstRTSPStreamPrivate *priv;
1717 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1718 priv = stream->priv;
1719 g_return_if_fail (priv->joined_bin != NULL);
1721 g_mutex_lock (&priv->lock);
1722 if (ssrc && priv->session)
1723 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1724 g_mutex_unlock (&priv->lock);
1728 * gst_rtsp_stream_set_retransmission_time:
1729 * @stream: a #GstRTSPStream
1730 * @time: a #GstClockTime
1732 * Set the amount of time to store retransmission packets.
1735 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1738 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1740 g_mutex_lock (&stream->priv->lock);
1741 stream->priv->rtx_time = time;
1742 if (stream->priv->rtxsend)
1743 g_object_set (stream->priv->rtxsend, "max-size-time",
1744 GST_TIME_AS_MSECONDS (time), NULL);
1745 g_mutex_unlock (&stream->priv->lock);
1749 * gst_rtsp_stream_get_retransmission_time:
1750 * @stream: a #GstRTSPStream
1752 * Get the amount of time to store retransmission data.
1754 * Returns: the amount of time to store retransmission data.
1757 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1761 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1763 g_mutex_lock (&stream->priv->lock);
1764 ret = stream->priv->rtx_time;
1765 g_mutex_unlock (&stream->priv->lock);
1771 * gst_rtsp_stream_set_retransmission_pt:
1772 * @stream: a #GstRTSPStream
1775 * Set the payload type (pt) for retransmission of this stream.
1778 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1780 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1782 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1784 g_mutex_lock (&stream->priv->lock);
1785 stream->priv->rtx_pt = rtx_pt;
1786 if (stream->priv->rtxsend) {
1787 guint pt = gst_rtsp_stream_get_pt (stream);
1788 gchar *pt_s = g_strdup_printf ("%d", pt);
1789 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1790 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1791 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1793 gst_structure_free (rtx_pt_map);
1795 g_mutex_unlock (&stream->priv->lock);
1799 * gst_rtsp_stream_get_retransmission_pt:
1800 * @stream: a #GstRTSPStream
1802 * Get the payload-type used for retransmission of this stream
1804 * Returns: The retransmission PT.
1807 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1811 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1813 g_mutex_lock (&stream->priv->lock);
1814 rtx_pt = stream->priv->rtx_pt;
1815 g_mutex_unlock (&stream->priv->lock);
1821 * gst_rtsp_stream_set_buffer_size:
1822 * @stream: a #GstRTSPStream
1823 * @size: the buffer size
1825 * Set the size of the UDP transmission buffer (in bytes)
1826 * Needs to be set before the stream is joined to a bin.
1831 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1833 g_mutex_lock (&stream->priv->lock);
1834 stream->priv->buffer_size = size;
1835 g_mutex_unlock (&stream->priv->lock);
1839 * gst_rtsp_stream_get_buffer_size:
1840 * @stream: a #GstRTSPStream
1842 * Get the size of the UDP transmission buffer (in bytes)
1844 * Returns: the size of the UDP TX buffer
1849 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1853 g_mutex_lock (&stream->priv->lock);
1854 buffer_size = stream->priv->buffer_size;
1855 g_mutex_unlock (&stream->priv->lock);
1860 /* executed from streaming thread */
1862 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1864 GstRTSPStreamPrivate *priv = stream->priv;
1865 GstCaps *newcaps, *oldcaps;
1867 newcaps = gst_pad_get_current_caps (pad);
1869 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1872 g_mutex_lock (&priv->lock);
1873 oldcaps = priv->caps;
1874 priv->caps = newcaps;
1875 g_mutex_unlock (&priv->lock);
1878 gst_caps_unref (oldcaps);
1882 dump_structure (const GstStructure * s)
1886 sstr = gst_structure_to_string (s);
1887 GST_INFO ("structure: %s", sstr);
1891 static GstRTSPStreamTransport *
1892 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1894 GstRTSPStreamPrivate *priv = stream->priv;
1896 GstRTSPStreamTransport *result = NULL;
1901 if (rtcp_from == NULL)
1904 tmp = g_strrstr (rtcp_from, ":");
1908 port = atoi (tmp + 1);
1909 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1911 g_mutex_lock (&priv->lock);
1912 GST_INFO ("finding %s:%d in %d transports", dest, port,
1913 g_list_length (priv->transports));
1915 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1916 GstRTSPStreamTransport *trans = walk->data;
1917 const GstRTSPTransport *tr;
1920 tr = gst_rtsp_stream_transport_get_transport (trans);
1922 if (priv->client_side) {
1923 /* In client side mode the 'destination' is the RTSP server, so send
1925 min = tr->server_port.min;
1926 max = tr->server_port.max;
1928 min = tr->client_port.min;
1929 max = tr->client_port.max;
1932 if ((g_ascii_strcasecmp (tr->destination, dest) == 0) &&
1933 (min == port || max == port)) {
1939 g_object_ref (result);
1940 g_mutex_unlock (&priv->lock);
1947 static GstRTSPStreamTransport *
1948 check_transport (GObject * source, GstRTSPStream * stream)
1950 GstStructure *stats;
1951 GstRTSPStreamTransport *trans;
1953 /* see if we have a stream to match with the origin of the RTCP packet */
1954 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1955 if (trans == NULL) {
1956 g_object_get (source, "stats", &stats, NULL);
1958 const gchar *rtcp_from;
1960 dump_structure (stats);
1962 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1963 if ((trans = find_transport (stream, rtcp_from))) {
1964 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1966 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1969 gst_structure_free (stats);
1977 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1979 GstRTSPStreamTransport *trans;
1981 GST_INFO ("%p: new source %p", stream, source);
1983 trans = check_transport (source, stream);
1986 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1990 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1992 GST_INFO ("%p: new SDES %p", stream, source);
1996 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1998 GstRTSPStreamTransport *trans;
2000 trans = check_transport (source, stream);
2003 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
2004 gst_rtsp_stream_transport_keep_alive (trans);
2008 GstStructure *stats;
2009 g_object_get (source, "stats", &stats, NULL);
2011 dump_structure (stats);
2012 gst_structure_free (stats);
2019 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2021 GST_INFO ("%p: source %p bye", stream, source);
2025 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2027 GstRTSPStreamTransport *trans;
2029 GST_INFO ("%p: source %p bye timeout", stream, source);
2031 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2032 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2033 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2038 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2040 GstRTSPStreamTransport *trans;
2042 GST_INFO ("%p: source %p timeout", stream, source);
2044 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2045 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2046 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2051 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2053 GST_INFO ("%p: new sender source %p", stream, source);
2056 GstStructure *stats;
2057 g_object_get (source, "stats", &stats, NULL);
2059 dump_structure (stats);
2060 gst_structure_free (stats);
2067 on_sender_ssrc_active (GObject * session, GObject * source,
2068 GstRTSPStream * stream)
2072 GstStructure *stats;
2073 g_object_get (source, "stats", &stats, NULL);
2075 dump_structure (stats);
2076 gst_structure_free (stats);
2083 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
2086 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
2087 g_list_free (priv->tr_cache_rtp);
2088 priv->tr_cache_rtp = NULL;
2090 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
2091 g_list_free (priv->tr_cache_rtcp);
2092 priv->tr_cache_rtcp = NULL;
2096 static GstFlowReturn
2097 handle_new_sample (GstAppSink * sink, gpointer user_data)
2099 GstRTSPStreamPrivate *priv;
2103 GstRTSPStream *stream;
2106 sample = gst_app_sink_pull_sample (sink);
2110 stream = (GstRTSPStream *) user_data;
2111 priv = stream->priv;
2112 buffer = gst_sample_get_buffer (sample);
2114 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
2116 g_mutex_lock (&priv->lock);
2118 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
2119 clear_tr_cache (priv, is_rtp);
2120 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2121 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2122 priv->tr_cache_rtp =
2123 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
2125 priv->tr_cache_cookie_rtp = priv->transports_cookie;
2128 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
2129 clear_tr_cache (priv, is_rtp);
2130 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2131 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2132 priv->tr_cache_rtcp =
2133 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
2135 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
2138 g_mutex_unlock (&priv->lock);
2141 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2142 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2143 gst_rtsp_stream_transport_send_rtp (tr, buffer);
2146 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2147 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2148 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
2151 gst_sample_unref (sample);
2156 static GstAppSinkCallbacks sink_cb = {
2157 NULL, /* not interested in EOS */
2158 NULL, /* not interested in preroll samples */
2163 get_rtp_encoder (GstRTSPStream * stream, guint session)
2165 GstRTSPStreamPrivate *priv = stream->priv;
2167 if (priv->srtpenc == NULL) {
2170 name = g_strdup_printf ("srtpenc_%u", session);
2171 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2174 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2176 return gst_object_ref (priv->srtpenc);
2180 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2182 GstRTSPStreamPrivate *priv = stream->priv;
2183 GstElement *oldenc, *enc;
2187 if (priv->idx != session)
2190 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2192 oldenc = priv->srtpenc;
2193 enc = get_rtp_encoder (stream, session);
2194 name = g_strdup_printf ("rtp_sink_%d", session);
2195 pad = gst_element_get_request_pad (enc, name);
2197 gst_object_unref (pad);
2200 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2207 request_rtcp_encoder (GstElement * rtpbin, guint session,
2208 GstRTSPStream * stream)
2210 GstRTSPStreamPrivate *priv = stream->priv;
2211 GstElement *oldenc, *enc;
2215 if (priv->idx != session)
2218 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2220 oldenc = priv->srtpenc;
2221 enc = get_rtp_encoder (stream, session);
2222 name = g_strdup_printf ("rtcp_sink_%d", session);
2223 pad = gst_element_get_request_pad (enc, name);
2225 gst_object_unref (pad);
2228 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2235 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2237 GstRTSPStreamPrivate *priv = stream->priv;
2240 GST_DEBUG ("request key %08x", ssrc);
2242 g_mutex_lock (&priv->lock);
2243 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2244 gst_caps_ref (caps);
2245 g_mutex_unlock (&priv->lock);
2251 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2252 GstRTSPStream * stream)
2254 GstRTSPStreamPrivate *priv = stream->priv;
2256 if (priv->idx != session)
2259 if (priv->srtpdec == NULL) {
2262 name = g_strdup_printf ("srtpdec_%u", session);
2263 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2266 g_signal_connect (priv->srtpdec, "request-key",
2267 (GCallback) request_key, stream);
2269 return gst_object_ref (priv->srtpdec);
2273 * gst_rtsp_stream_request_aux_sender:
2274 * @stream: a #GstRTSPStream
2275 * @sessid: the session id
2277 * Creating a rtxsend bin
2279 * Returns: (transfer full): a #GstElement.
2284 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2288 GstStructure *pt_map;
2293 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2295 pt = gst_rtsp_stream_get_pt (stream);
2296 pt_s = g_strdup_printf ("%u", pt);
2297 rtx_pt = stream->priv->rtx_pt;
2299 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2301 bin = gst_bin_new (NULL);
2302 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2303 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2304 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2305 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2306 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2308 gst_structure_free (pt_map);
2309 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2311 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2312 name = g_strdup_printf ("src_%u", sessid);
2313 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2315 gst_object_unref (pad);
2317 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2318 name = g_strdup_printf ("sink_%u", sessid);
2319 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2321 gst_object_unref (pad);
2327 * gst_rtsp_stream_set_pt_map:
2328 * @stream: a #GstRTSPStream
2332 * Configure a pt map between @pt and @caps.
2335 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2337 GstRTSPStreamPrivate *priv = stream->priv;
2339 g_mutex_lock (&priv->lock);
2340 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2341 g_mutex_unlock (&priv->lock);
2345 * gst_rtsp_stream_set_publish_clock_mode:
2346 * @stream: a #GstRTSPStream
2347 * @mode: the clock publish mode
2349 * Sets if and how the stream clock should be published according to RFC7273.
2354 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
2355 GstRTSPPublishClockMode mode)
2357 GstRTSPStreamPrivate *priv;
2359 priv = stream->priv;
2360 g_mutex_lock (&priv->lock);
2361 priv->publish_clock_mode = mode;
2362 g_mutex_unlock (&priv->lock);
2366 * gst_rtsp_stream_get_publish_clock_mode:
2367 * @stream: a #GstRTSPStream
2369 * Gets if and how the stream clock should be published according to RFC7273.
2371 * Returns: The GstRTSPPublishClockMode
2375 GstRTSPPublishClockMode
2376 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
2378 GstRTSPStreamPrivate *priv;
2379 GstRTSPPublishClockMode ret;
2381 priv = stream->priv;
2382 g_mutex_lock (&priv->lock);
2383 ret = priv->publish_clock_mode;
2384 g_mutex_unlock (&priv->lock);
2390 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2391 GstRTSPStream * stream)
2393 GstRTSPStreamPrivate *priv = stream->priv;
2394 GstCaps *caps = NULL;
2396 g_mutex_lock (&priv->lock);
2398 if (priv->idx == session) {
2399 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2401 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2402 gst_caps_ref (caps);
2404 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2408 g_mutex_unlock (&priv->lock);
2414 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2416 GstRTSPStreamPrivate *priv = stream->priv;
2418 GstPadLinkReturn ret;
2421 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2422 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2424 name = gst_pad_get_name (pad);
2425 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2431 if (priv->idx != sessid)
2434 if (gst_pad_is_linked (priv->sinkpad)) {
2435 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2436 GST_DEBUG_PAD_NAME (priv->sinkpad));
2440 /* link the RTP pad to the session manager, it should not really fail unless
2441 * this is not really an RTP pad */
2442 ret = gst_pad_link (pad, priv->sinkpad);
2443 if (ret != GST_PAD_LINK_OK)
2445 priv->recv_rtp_src = gst_object_ref (pad);
2452 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2453 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2458 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2459 GstRTSPStream * stream)
2461 /* TODO: What to do here other than this? */
2462 GST_DEBUG ("Stream %p: Got EOS", stream);
2463 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2466 typedef struct _ProbeData ProbeData;
2470 GstRTSPStream *stream;
2471 /* existing sink, already linked to tee */
2473 /* new sink, about to be linked */
2475 /* new queue element, that will be linked to tee and sink1 */
2476 GstElement **queue1;
2477 /* new queue element, that will be linked to tee and sink2 */
2478 GstElement **queue2;
2485 free_cb_data (gpointer user_data)
2487 ProbeData *data = user_data;
2489 gst_object_unref (data->stream);
2490 gst_object_unref (data->sink1);
2491 gst_object_unref (data->sink2);
2492 gst_object_unref (data->sink_pad);
2493 gst_object_unref (data->tee_pad);
2499 create_and_plug_queue_to_unlinked_stream (GstRTSPStream * stream, GstElement *tee,
2500 GstElement *sink, GstElement ** queue)
2502 GstRTSPStreamPrivate *priv = stream->priv;
2507 /* create queue for the new stream */
2508 *queue = gst_element_factory_make ("queue", NULL);
2509 g_object_set (*queue, "max-size-buffers", 1, "max-size-bytes", 0,
2510 "max-size-time", G_GINT64_CONSTANT (0), NULL);
2511 gst_bin_add (priv->joined_bin, *queue);
2513 /* link tee to queue */
2514 tee_pad = gst_element_get_request_pad (tee, "src_%u");
2515 queue_pad = gst_element_get_static_pad (*queue, "sink");
2516 gst_pad_link (tee_pad, queue_pad);
2517 gst_object_unref (queue_pad);
2518 gst_object_unref (tee_pad);
2520 /* link queue to sink */
2521 queue_pad = gst_element_get_static_pad (*queue, "src");
2522 sink_pad = gst_element_get_static_pad (sink, "sink");
2523 gst_pad_link (queue_pad, sink_pad);
2524 gst_object_unref (queue_pad);
2525 gst_object_unref (sink_pad);
2527 gst_element_sync_state_with_parent (sink);
2528 gst_element_sync_state_with_parent (*queue);
2531 static GstPadProbeReturn
2532 create_and_plug_queue_to_linked_stream_probe_cb (GstPad * inpad,
2533 GstPadProbeInfo * info, gpointer user_data)
2535 GstRTSPStreamPrivate *priv;
2536 ProbeData *data = user_data;
2537 GstRTSPStream *stream;
2538 GstElement **queue1;
2539 GstElement **queue2;
2545 stream = data->stream;
2546 priv = stream->priv;
2547 queue1 = data->queue1;
2548 queue2 = data->queue2;
2549 sink_pad = data->sink_pad;
2550 tee_pad = data->tee_pad;
2551 index = data->index;
2553 /* unlink tee and the existing sink:
2554 * .-----. .---------.
2557 * '-----' '---------'
2559 g_assert (gst_pad_unlink (tee_pad, sink_pad));
2561 /* add queue to the already existing stream */
2562 *queue1 = gst_element_factory_make ("queue", NULL);
2563 g_object_set (*queue1, "max-size-buffers", 1, "max-size-bytes", 0,
2564 "max-size-time", G_GINT64_CONSTANT (0), NULL);
2565 gst_bin_add (priv->joined_bin, *queue1);
2567 /* link tee, queue and sink:
2568 * .-----. .---------. .---------.
2569 * | tee | | queue1 | | sink1 |
2570 * sink src->sink src->sink |
2571 * '-----' '---------' '---------'
2573 queue_pad = gst_element_get_static_pad (*queue1, "sink");
2574 gst_pad_link (tee_pad, queue_pad);
2575 gst_object_unref (queue_pad);
2576 queue_pad = gst_element_get_static_pad (*queue1, "src");
2577 gst_pad_link (queue_pad, sink_pad);
2578 gst_object_unref (queue_pad);
2580 gst_element_sync_state_with_parent (*queue1);
2582 /* create queue and link it to tee and the new sink */
2583 create_and_plug_queue_to_unlinked_stream (stream,
2584 priv->tee[index], data->sink2, queue2);
2586 /* the final stream:
2588 * .-----. .---------. .---------.
2589 * | tee | | queue1 | | sink1 |
2590 * sink src->sink src->sink |
2591 * | | '---------' '---------'
2592 * | | .---------. .---------.
2593 * | | | queue2 | | sink2 |
2594 * | src->sink src->sink |
2595 * '-----' '---------' '---------'
2598 return GST_PAD_PROBE_REMOVE;
2602 create_and_plug_queue_to_linked_stream (GstRTSPStream * stream, GstElement * sink1,
2603 GstElement * sink2, guint index, GstElement ** queue1,
2604 GstElement ** queue2)
2608 data = g_new0 (ProbeData, 1);
2609 data->stream = gst_object_ref (stream);
2610 data->sink1 = gst_object_ref (sink1);
2611 data->sink2 = gst_object_ref (sink2);
2612 data->queue1 = queue1;
2613 data->queue2 = queue2;
2614 data->index = index;
2616 data->sink_pad = gst_element_get_static_pad (sink1, "sink");
2617 g_assert (data->sink_pad);
2618 data->tee_pad = gst_pad_get_peer (data->sink_pad);
2619 g_assert (data->tee_pad);
2621 gst_pad_add_probe (data->tee_pad, GST_PAD_PROBE_TYPE_IDLE,
2622 create_and_plug_queue_to_linked_stream_probe_cb, data, free_cb_data);
2626 plug_udp_sink (GstRTSPStream * stream, GstElement * sink_to_plug,
2627 GstElement ** queue_to_plug, guint index, gboolean is_mcast)
2629 GstRTSPStreamPrivate *priv = stream->priv;
2630 GstElement *existing_sink;
2633 existing_sink = priv->udpsink[index];
2635 existing_sink = priv->mcast_udpsink[index];
2637 GST_DEBUG_OBJECT (stream, "plug %s sink", is_mcast ? "mcast" : "udp");
2639 /* add sink to the bin */
2640 gst_bin_add (priv->joined_bin, sink_to_plug);
2642 if (priv->appsink[index] && existing_sink) {
2644 /* queues are already added for the existing stream, add one for
2645 the newly added udp stream */
2646 create_and_plug_queue_to_unlinked_stream (stream, priv->tee[index],
2647 sink_to_plug, queue_to_plug);
2649 } else if (priv->appsink[index] || existing_sink) {
2651 GstElement *element;
2653 /* add queue to the already existing stream plus the newly created udp
2655 if (priv->appsink[index]) {
2656 element = priv->appsink[index];
2657 queue = &priv->appqueue[index];
2659 element = existing_sink;
2661 queue = &priv->udpqueue[index];
2663 queue = &priv->mcast_udpqueue[index];
2666 create_and_plug_queue_to_linked_stream (stream, element, sink_to_plug, index,
2667 queue, queue_to_plug);
2673 GST_DEBUG_OBJECT (stream, "creating first stream");
2675 /* no need to add queues */
2676 tee_pad = gst_element_get_request_pad (priv->tee[index], "src_%u");
2677 sink_pad = gst_element_get_static_pad (sink_to_plug, "sink");
2678 gst_pad_link (tee_pad, sink_pad);
2679 gst_object_unref (tee_pad);
2680 gst_object_unref (sink_pad);
2683 gst_element_sync_state_with_parent (sink_to_plug);
2687 plug_tcp_sink (GstRTSPStream * stream, guint index)
2689 GstRTSPStreamPrivate *priv = stream->priv;
2691 GST_DEBUG_OBJECT (stream, "plug tcp sink");
2693 /* add sink to the bin */
2694 gst_bin_add (priv->joined_bin, priv->appsink[index]);
2696 if (priv->mcast_udpsink[index] && priv->udpsink[index]) {
2698 /* queues are already added for the existing stream, add one for
2699 the newly added tcp stream */
2700 create_and_plug_queue_to_unlinked_stream (stream,
2701 priv->tee[index], priv->appsink[index], &priv->appqueue[index]);
2703 } else if (priv->mcast_udpsink[index] || priv->udpsink[index]) {
2705 GstElement *element;
2707 /* add queue to the already existing stream plus the newly created tcp
2709 if (priv->mcast_udpsink[index]) {
2710 element = priv->mcast_udpsink[index];
2711 queue = &priv->mcast_udpqueue[index];
2713 element = priv->udpsink[index];
2714 queue = &priv->udpqueue[index];
2717 create_and_plug_queue_to_linked_stream (stream, element, priv->appsink[index], index,
2718 queue, &priv->appqueue[index]);
2724 /* no need to add queues */
2725 tee_pad = gst_element_get_request_pad (priv->tee[index], "src_%u");
2726 sink_pad = gst_element_get_static_pad (priv->appsink[index], "sink");
2727 gst_pad_link (tee_pad, sink_pad);
2728 gst_object_unref (tee_pad);
2729 gst_object_unref (sink_pad);
2732 gst_element_sync_state_with_parent (priv->appsink[index]);
2736 plug_sink (GstRTSPStream * stream, const GstRTSPTransport * transport,
2739 GstRTSPStreamPrivate *priv;
2740 gboolean is_tcp, is_udp, is_mcast;
2741 priv = stream->priv;
2743 is_tcp = transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP;
2744 is_udp = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP;
2745 is_mcast = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST;
2748 plug_udp_sink (stream, priv->udpsink[index],
2749 &priv->udpqueue[index], index, FALSE);
2752 plug_udp_sink (stream, priv->mcast_udpsink[index],
2753 &priv->mcast_udpqueue[index], index, TRUE);
2756 plug_tcp_sink (stream, index);
2759 /* must be called with lock */
2761 create_sender_part (GstRTSPStream * stream, const GstRTSPTransport * transport)
2763 GstRTSPStreamPrivate *priv;
2766 gboolean is_tcp, is_udp, is_mcast;
2769 GST_DEBUG_OBJECT (stream, "create sender part");
2770 priv = stream->priv;
2771 bin = priv->joined_bin;
2773 is_tcp = transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP;
2774 is_udp = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP;
2775 is_mcast = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST;
2777 GST_DEBUG_OBJECT (stream, "tcp: %d, udp: %d, mcast: %d", is_tcp, is_udp,
2780 if (is_udp && !priv->server_addr_v4 && !priv->server_addr_v6) {
2781 GST_WARNING_OBJECT (stream, "no sockets assigned for UDP");
2785 if (is_mcast && !priv->mcast_addr_v4 && !priv->mcast_addr_v6) {
2786 GST_WARNING_OBJECT (stream, "no sockets assigned for UDP multicast");
2790 for (i = 0; i < 2; i++) {
2791 gboolean link_tee = FALSE;
2792 /* For the sender we create this bit of pipeline for both
2794 * Initially there will be only one active transport for
2795 * the stream, so the pipeline will look like this:
2797 * .--------. .-----. .---------.
2798 * | rtpbin | | tee | | sink |
2799 * | send->sink src->sink |
2800 * '--------' '-----' '---------'
2802 * For each new transport, the already existing branch will
2803 * be reconfigured by adding a queue element:
2805 * .--------. .-----. .---------. .---------.
2806 * | rtpbin | | tee | | queue | | udpsink |
2807 * | send->sink src->sink src->sink |
2808 * '--------' | | '---------' '---------'
2809 * | | .---------. .---------.
2810 * | | | queue | | udpsink |
2811 * | src->sink src->sink |
2812 * | | '---------' '---------'
2813 * | | .---------. .---------.
2814 * | | | queue | | appsink |
2815 * | src->sink src->sink |
2816 * '-----' '---------' '---------'
2819 /* Only link the RTP send src if we're going to send RTP, link
2820 * the RTCP send src always */
2821 if (!priv->srcpad && i == 0)
2824 if (!priv->tee[i]) {
2825 /* make tee for RTP/RTCP */
2826 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2827 gst_bin_add (bin, priv->tee[i]);
2831 if (is_udp && !priv->udpsink[i]) {
2832 /* we create only one pair of udpsinks for IPv4 and IPv6 */
2833 create_and_configure_udpsink (stream, &priv->udpsink[i], priv->socket_v4[i],
2834 priv->socket_v6[i], FALSE, (i == 0));
2835 plug_sink (stream, transport, i);
2836 } else if (is_mcast && !priv->mcast_udpsink[i]) {
2837 /* we create only one pair of mcast-udpsinks for IPv4 and IPv6 */
2838 create_and_configure_udpsink (stream, &priv->mcast_udpsink[i],
2839 priv->mcast_socket_v4[i], priv->mcast_socket_v6[i], TRUE, (i == 0));
2840 plug_sink (stream, transport, i);
2841 } else if (is_tcp && !priv->appsink[i]) {
2843 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2844 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2846 /* we need to set sync and preroll to FALSE for the sink to avoid
2847 * deadlock. This is only needed for sink sending RTCP data. */
2849 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE,
2852 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2853 &sink_cb, stream, NULL);
2854 plug_sink (stream, transport, i);
2858 /* and link to rtpbin send pad */
2859 gst_element_sync_state_with_parent (priv->tee[i]);
2860 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2861 gst_pad_link (priv->send_src[i], pad);
2862 gst_object_unref (pad);
2869 /* must be called with lock */
2871 plug_src (GstRTSPStream * stream, GstBin * bin, GstElement * src,
2872 GstElement * funnel)
2874 GstRTSPStreamPrivate *priv;
2875 GstPad *pad, *selpad;
2877 priv = stream->priv;
2880 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2881 * values. This is only relevant for PLAY pipelines */
2882 gst_element_set_state (src, GST_STATE_PLAYING);
2883 gst_element_set_locked_state (src, TRUE);
2887 gst_bin_add (bin, src);
2889 /* and link to the funnel */
2890 selpad = gst_element_get_request_pad (funnel, "sink_%u");
2891 pad = gst_element_get_static_pad (src, "src");
2892 gst_pad_link (pad, selpad);
2893 gst_object_unref (pad);
2894 gst_object_unref (selpad);
2897 /* must be called with lock */
2899 create_receiver_part (GstRTSPStream * stream, const GstRTSPTransport *
2902 GstRTSPStreamPrivate *priv;
2910 GST_DEBUG_OBJECT (stream, "create receiver part");
2911 priv = stream->priv;
2912 bin = priv->joined_bin;
2914 tcp = transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP;
2915 udp = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP;
2916 mcast = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST;
2918 for (i = 0; i < 2; i++) {
2919 /* For the receiver we create this bit of pipeline for both
2920 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2921 * and it is all funneled into the rtpbin receive pad.
2924 * .--------. .--------. .--------.
2925 * | udpsrc | | funnel | | rtpbin |
2926 * | RTP src->sink src->sink |
2927 * '--------' | | | |
2928 * .--------. | | | |
2929 * | appsrc | | | | |
2930 * | RTP src->sink | | |
2931 * '--------' '--------' | |
2933 * .--------. .--------. | |
2934 * | udpsrc | | funnel | | |
2935 * | RTCP src->sink src->sink |
2936 * '--------' | | '--------'
2939 * | RTCP src->sink |
2940 * '--------' '--------'
2943 if (!priv->sinkpad && i == 0) {
2944 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2945 * RTCP sink always */
2949 /* make funnel for the RTP/RTCP receivers */
2950 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2951 gst_bin_add (bin, priv->funnel[i]);
2953 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2954 gst_pad_link (pad, priv->recv_sink[i]);
2955 gst_object_unref (pad);
2957 if (udp && !priv->udpsrc_v4[i] && priv->server_addr_v4) {
2958 GST_DEBUG_OBJECT (stream, "udp IPv4, create and configure udpsources");
2959 if (!create_and_configure_udpsource (&priv->udpsrc_v4[i],
2960 priv->socket_v4[i]))
2963 plug_src (stream, bin, priv->udpsrc_v4[i], priv->funnel[i]);
2966 if (udp && !priv->udpsrc_v6[i] && priv->server_addr_v6) {
2967 GST_DEBUG_OBJECT (stream, "udp IPv6, create and configure udpsources");
2968 if (!create_and_configure_udpsource (&priv->udpsrc_v6[i],
2969 priv->socket_v6[i]))
2972 plug_src (stream, bin, priv->udpsrc_v6[i], priv->funnel[i]);
2975 if (mcast && !priv->mcast_udpsrc_v4[i] && priv->mcast_addr_v4) {
2976 GST_DEBUG_OBJECT (stream, "mcast IPv4, create and configure udpsources");
2977 if (!create_and_configure_udpsource (&priv->mcast_udpsrc_v4[i],
2978 priv->mcast_socket_v4[i]))
2979 goto mcast_udpsrc_error;
2980 plug_src (stream, bin, priv->mcast_udpsrc_v4[i], priv->funnel[i]);
2983 if (mcast && !priv->mcast_udpsrc_v6[i] && priv->mcast_addr_v6) {
2984 GST_DEBUG_OBJECT (stream, "mcast IPv6, create and configure udpsources");
2985 if (!create_and_configure_udpsource (&priv->mcast_udpsrc_v6[i],
2986 priv->mcast_socket_v6[i]))
2987 goto mcast_udpsrc_error;
2988 plug_src (stream, bin, priv->mcast_udpsrc_v6[i], priv->funnel[i]);
2991 if (tcp && !priv->appsrc[i]) {
2992 /* make and add appsrc */
2993 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2994 priv->appsrc_base_time[i] = -1;
2995 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
2997 plug_src (stream, bin, priv->appsrc[i], priv->funnel[i]);
3000 gst_element_sync_state_with_parent (priv->funnel[i]);
3011 check_mcast_part_for_transport (GstRTSPStream * stream,
3012 const GstRTSPTransport * tr)
3014 GstRTSPStreamPrivate *priv = stream->priv;
3015 GInetAddress *inetaddr;
3016 GSocketFamily family;
3017 GstRTSPAddress *mcast_addr;
3019 /* Check if it's a ipv4 or ipv6 transport */
3020 inetaddr = g_inet_address_new_from_string (tr->destination);
3021 family = g_inet_address_get_family (inetaddr);
3022 g_object_unref (inetaddr);
3024 /* Select fields corresponding to the family */
3025 if (family == G_SOCKET_FAMILY_IPV4) {
3026 mcast_addr = priv->mcast_addr_v4;
3028 mcast_addr = priv->mcast_addr_v6;
3031 /* We support only one mcast group per family, make sure this transport
3036 if (g_ascii_strcasecmp (tr->destination, mcast_addr->address) != 0 ||
3037 tr->port.min != mcast_addr->port ||
3038 tr->port.max != mcast_addr->port + mcast_addr->n_ports - 1 ||
3039 tr->ttl != mcast_addr->ttl)
3046 GST_WARNING_OBJECT (stream, "Adding mcast transport, but no mcast address "
3047 "has been reserved");
3052 GST_WARNING_OBJECT (stream, "Adding mcast transport, but it doesn't match "
3053 "the reserved address");
3059 * gst_rtsp_stream_join_bin:
3060 * @stream: a #GstRTSPStream
3061 * @bin: (transfer none): a #GstBin to join
3062 * @rtpbin: (transfer none): a rtpbin element in @bin
3063 * @state: the target state of the new elements
3065 * Join the #GstBin @bin that contains the element @rtpbin.
3067 * @stream will link to @rtpbin, which must be inside @bin. The elements
3068 * added to @bin will be set to the state given in @state.
3070 * Returns: %TRUE on success.
3073 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
3074 GstElement * rtpbin, GstState state)
3076 GstRTSPStreamPrivate *priv;
3079 GstPadLinkReturn ret;
3081 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3082 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
3083 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
3085 priv = stream->priv;
3087 g_mutex_lock (&priv->lock);
3088 if (priv->joined_bin != NULL)
3091 /* create a session with the same index as the stream */
3094 GST_INFO ("stream %p joining bin as session %u", stream, idx);
3096 if (priv->profiles & GST_RTSP_PROFILE_SAVP
3097 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
3099 g_signal_connect (rtpbin, "request-rtp-encoder",
3100 (GCallback) request_rtp_encoder, stream);
3101 g_signal_connect (rtpbin, "request-rtcp-encoder",
3102 (GCallback) request_rtcp_encoder, stream);
3103 g_signal_connect (rtpbin, "request-rtp-decoder",
3104 (GCallback) request_rtp_rtcp_decoder, stream);
3105 g_signal_connect (rtpbin, "request-rtcp-decoder",
3106 (GCallback) request_rtp_rtcp_decoder, stream);
3109 if (priv->sinkpad) {
3110 g_signal_connect (rtpbin, "request-pt-map",
3111 (GCallback) request_pt_map, stream);
3114 /* get pads from the RTP session element for sending and receiving
3117 /* get a pad for sending RTP */
3118 name = g_strdup_printf ("send_rtp_sink_%u", idx);
3119 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
3122 /* link the RTP pad to the session manager, it should not really fail unless
3123 * this is not really an RTP pad */
3124 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
3125 if (ret != GST_PAD_LINK_OK)
3128 name = g_strdup_printf ("send_rtp_src_%u", idx);
3129 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
3132 /* RECORD case: need to connect our sinkpad from here */
3133 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
3135 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
3137 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
3138 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
3142 name = g_strdup_printf ("send_rtcp_src_%u", idx);
3143 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
3145 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
3146 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
3149 /* get the session */
3150 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
3152 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
3154 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
3156 g_signal_connect (priv->session, "on-ssrc-active",
3157 (GCallback) on_ssrc_active, stream);
3158 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3160 g_signal_connect (priv->session, "on-bye-timeout",
3161 (GCallback) on_bye_timeout, stream);
3162 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
3165 /* signal for sender ssrc */
3166 g_signal_connect (priv->session, "on-new-sender-ssrc",
3167 (GCallback) on_new_sender_ssrc, stream);
3168 g_signal_connect (priv->session, "on-sender-ssrc-active",
3169 (GCallback) on_sender_ssrc_active, stream);
3172 /* be notified of caps changes */
3173 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
3174 (GCallback) caps_notify, stream);
3175 priv->caps = gst_pad_get_current_caps (priv->send_src[0]);
3178 priv->joined_bin = bin;
3179 GST_DEBUG_OBJECT (stream, "successfully joined bin");
3180 g_mutex_unlock (&priv->lock);
3187 g_mutex_unlock (&priv->lock);
3192 GST_WARNING ("failed to link stream %u", idx);
3193 gst_object_unref (priv->send_rtp_sink);
3194 priv->send_rtp_sink = NULL;
3195 g_mutex_unlock (&priv->lock);
3201 clear_element (GstBin * bin, GstElement ** elementptr)
3204 gst_element_set_locked_state (*elementptr, FALSE);
3205 gst_element_set_state (*elementptr, GST_STATE_NULL);
3206 if (GST_ELEMENT_PARENT (*elementptr))
3207 gst_bin_remove (bin, *elementptr);
3209 gst_object_unref (*elementptr);
3215 * gst_rtsp_stream_leave_bin:
3216 * @stream: a #GstRTSPStream
3217 * @bin: (transfer none): a #GstBin
3218 * @rtpbin: (transfer none): a rtpbin #GstElement
3220 * Remove the elements of @stream from @bin.
3222 * Return: %TRUE on success.
3225 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
3226 GstElement * rtpbin)
3228 GstRTSPStreamPrivate *priv;
3231 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3232 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
3233 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
3235 priv = stream->priv;
3237 g_mutex_lock (&priv->lock);
3238 if (priv->joined_bin == NULL)
3239 goto was_not_joined;
3240 if (priv->joined_bin != bin)
3243 priv->joined_bin = NULL;
3245 /* all transports must be removed by now */
3246 if (priv->transports != NULL)
3247 goto transports_not_removed;
3249 clear_tr_cache (priv, TRUE);
3250 clear_tr_cache (priv, FALSE);
3252 GST_INFO ("stream %p leaving bin", stream);
3255 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
3257 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
3258 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
3259 gst_object_unref (priv->send_rtp_sink);
3260 priv->send_rtp_sink = NULL;
3261 } else if (priv->recv_rtp_src) {
3262 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
3263 gst_object_unref (priv->recv_rtp_src);
3264 priv->recv_rtp_src = NULL;
3267 for (i = 0; i < 2; i++) {
3268 clear_element (bin, &priv->udpsrc_v4[i]);
3269 clear_element (bin, &priv->udpsrc_v6[i]);
3270 clear_element (bin, &priv->udpqueue[i]);
3271 clear_element (bin, &priv->udpsink[i]);
3273 clear_element (bin, &priv->mcast_udpsrc_v4[i]);
3274 clear_element (bin, &priv->mcast_udpsrc_v6[i]);
3275 clear_element (bin, &priv->mcast_udpqueue[i]);
3276 clear_element (bin, &priv->mcast_udpsink[i]);
3278 clear_element (bin, &priv->appsrc[i]);
3279 clear_element (bin, &priv->appqueue[i]);
3280 clear_element (bin, &priv->appsink[i]);
3282 clear_element (bin, &priv->tee[i]);
3283 clear_element (bin, &priv->funnel[i]);
3285 if (priv->sinkpad || i == 1) {
3286 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
3287 gst_object_unref (priv->recv_sink[i]);
3288 priv->recv_sink[i] = NULL;
3293 gst_object_unref (priv->send_src[0]);
3294 priv->send_src[0] = NULL;
3297 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
3298 gst_object_unref (priv->send_src[1]);
3299 priv->send_src[1] = NULL;
3301 g_object_unref (priv->session);
3302 priv->session = NULL;
3304 gst_caps_unref (priv->caps);
3308 gst_object_unref (priv->srtpenc);
3310 gst_object_unref (priv->srtpdec);
3312 if (priv->mcast_addr_v4)
3313 gst_rtsp_address_free (priv->mcast_addr_v4);
3314 priv->mcast_addr_v4 = NULL;
3315 if (priv->mcast_addr_v6)
3316 gst_rtsp_address_free (priv->mcast_addr_v6);
3317 priv->mcast_addr_v6 = NULL;
3318 if (priv->server_addr_v4)
3319 gst_rtsp_address_free (priv->server_addr_v4);
3320 priv->server_addr_v4 = NULL;
3321 if (priv->server_addr_v6)
3322 gst_rtsp_address_free (priv->server_addr_v6);
3323 priv->server_addr_v6 = NULL;
3325 g_mutex_unlock (&priv->lock);
3331 g_mutex_unlock (&priv->lock);
3334 transports_not_removed:
3336 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
3337 g_mutex_unlock (&priv->lock);
3342 GST_ERROR_OBJECT (stream, "leaving the wrong bin");
3343 g_mutex_unlock (&priv->lock);
3349 * gst_rtsp_stream_get_joined_bin:
3350 * @stream: a #GstRTSPStream
3352 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
3354 * Return: (transfer full): the joined bin or NULL.
3357 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
3359 GstRTSPStreamPrivate *priv;
3362 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3364 priv = stream->priv;
3366 g_mutex_lock (&priv->lock);
3367 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
3368 g_mutex_unlock (&priv->lock);
3374 * gst_rtsp_stream_get_rtpinfo:
3375 * @stream: a #GstRTSPStream
3376 * @rtptime: (allow-none): result RTP timestamp
3377 * @seq: (allow-none): result RTP seqnum
3378 * @clock_rate: (allow-none): the clock rate
3379 * @running_time: result running-time
3381 * Retrieve the current rtptime, seq and running-time. This is used to
3382 * construct a RTPInfo reply header.
3384 * Returns: %TRUE when rtptime, seq and running-time could be determined.
3387 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
3388 guint * rtptime, guint * seq, guint * clock_rate,
3389 GstClockTime * running_time)
3391 GstRTSPStreamPrivate *priv;
3392 GstStructure *stats;
3393 GObjectClass *payobjclass;
3395 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3397 priv = stream->priv;
3399 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
3401 g_mutex_lock (&priv->lock);
3403 /* First try to extract the information from the last buffer on the sinks.
3404 * This will have a more accurate sequence number and timestamp, as between
3405 * the payloader and the sink there can be some queues
3407 if (priv->udpsink[0] || priv->appsink[0]) {
3408 GstSample *last_sample;
3410 if (priv->udpsink[0])
3411 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
3413 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
3418 GstSegment *segment;
3420 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
3422 caps = gst_sample_get_caps (last_sample);
3423 buffer = gst_sample_get_buffer (last_sample);
3424 segment = gst_sample_get_segment (last_sample);
3425 s = gst_caps_get_structure (caps, 0);
3427 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3428 guint ssrc_buf = gst_rtp_buffer_get_ssrc (&rtp_buffer);
3429 guint ssrc_stream = 0;
3430 if (gst_structure_has_field_typed (s, "ssrc", G_TYPE_UINT) &&
3431 gst_structure_get_uint (s, "ssrc", &ssrc_stream) &&
3432 ssrc_buf != ssrc_stream) {
3433 /* Skip buffers from auxiliary streams. */
3434 GST_DEBUG_OBJECT (stream,
3435 "not a buffer from the payloader, SSRC: %08x", ssrc_buf);
3437 gst_rtp_buffer_unmap (&rtp_buffer);
3438 gst_sample_unref (last_sample);
3443 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3447 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3450 gst_rtp_buffer_unmap (&rtp_buffer);
3454 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3455 GST_BUFFER_TIMESTAMP (buffer));
3459 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3461 if (*clock_rate == 0 && running_time)
3462 *running_time = GST_CLOCK_TIME_NONE;
3464 gst_sample_unref (last_sample);
3468 gst_sample_unref (last_sample);
3474 if (g_object_class_find_property (payobjclass, "stats")) {
3475 g_object_get (priv->payloader, "stats", &stats, NULL);
3480 gst_structure_get_uint (stats, "seqnum", seq);
3483 gst_structure_get_uint (stats, "timestamp", rtptime);
3486 gst_structure_get_clock_time (stats, "running-time", running_time);
3489 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3490 if (*clock_rate == 0 && running_time)
3491 *running_time = GST_CLOCK_TIME_NONE;
3493 gst_structure_free (stats);
3495 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3496 !g_object_class_find_property (payobjclass, "timestamp"))
3500 g_object_get (priv->payloader, "seqnum", seq, NULL);
3503 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3506 *running_time = GST_CLOCK_TIME_NONE;
3510 g_mutex_unlock (&priv->lock);
3517 GST_WARNING ("Could not get payloader stats");
3518 g_mutex_unlock (&priv->lock);
3524 * gst_rtsp_stream_get_caps:
3525 * @stream: a #GstRTSPStream
3527 * Retrieve the current caps of @stream.
3529 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
3533 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3535 GstRTSPStreamPrivate *priv;
3538 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3540 priv = stream->priv;
3542 g_mutex_lock (&priv->lock);
3543 if ((result = priv->caps))
3544 gst_caps_ref (result);
3545 g_mutex_unlock (&priv->lock);
3551 * gst_rtsp_stream_recv_rtp:
3552 * @stream: a #GstRTSPStream
3553 * @buffer: (transfer full): a #GstBuffer
3555 * Handle an RTP buffer for the stream. This method is usually called when a
3556 * message has been received from a client using the TCP transport.
3558 * This function takes ownership of @buffer.
3560 * Returns: a GstFlowReturn.
3563 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3565 GstRTSPStreamPrivate *priv;
3567 GstElement *element;
3569 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3570 priv = stream->priv;
3571 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3572 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3574 g_mutex_lock (&priv->lock);
3575 if (priv->appsrc[0])
3576 element = gst_object_ref (priv->appsrc[0]);
3579 g_mutex_unlock (&priv->lock);
3582 if (priv->appsrc_base_time[0] == -1) {
3583 /* Take current running_time. This timestamp will be put on
3584 * the first buffer of each stream because we are a live source and so we
3585 * timestamp with the running_time. When we are dealing with TCP, we also
3586 * only timestamp the first buffer (using the DISCONT flag) because a server
3587 * typically bursts data, for which we don't want to compensate by speeding
3588 * up the media. The other timestamps will be interpollated from this one
3589 * using the RTP timestamps. */
3590 GST_OBJECT_LOCK (element);
3591 if (GST_ELEMENT_CLOCK (element)) {
3593 GstClockTime base_time;
3595 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3596 base_time = GST_ELEMENT_CAST (element)->base_time;
3598 priv->appsrc_base_time[0] = now - base_time;
3599 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3600 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3601 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3602 GST_TIME_ARGS (base_time));
3604 GST_OBJECT_UNLOCK (element);
3607 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3608 gst_object_unref (element);
3616 * gst_rtsp_stream_recv_rtcp:
3617 * @stream: a #GstRTSPStream
3618 * @buffer: (transfer full): a #GstBuffer
3620 * Handle an RTCP buffer for the stream. This method is usually called when a
3621 * message has been received from a client using the TCP transport.
3623 * This function takes ownership of @buffer.
3625 * Returns: a GstFlowReturn.
3628 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3630 GstRTSPStreamPrivate *priv;
3632 GstElement *element;
3634 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3635 priv = stream->priv;
3636 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3638 if (priv->joined_bin == NULL) {
3639 gst_buffer_unref (buffer);
3640 return GST_FLOW_NOT_LINKED;
3642 g_mutex_lock (&priv->lock);
3643 if (priv->appsrc[1])
3644 element = gst_object_ref (priv->appsrc[1]);
3647 g_mutex_unlock (&priv->lock);
3650 if (priv->appsrc_base_time[1] == -1) {
3651 /* Take current running_time. This timestamp will be put on
3652 * the first buffer of each stream because we are a live source and so we
3653 * timestamp with the running_time. When we are dealing with TCP, we also
3654 * only timestamp the first buffer (using the DISCONT flag) because a server
3655 * typically bursts data, for which we don't want to compensate by speeding
3656 * up the media. The other timestamps will be interpollated from this one
3657 * using the RTP timestamps. */
3658 GST_OBJECT_LOCK (element);
3659 if (GST_ELEMENT_CLOCK (element)) {
3661 GstClockTime base_time;
3663 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3664 base_time = GST_ELEMENT_CAST (element)->base_time;
3666 priv->appsrc_base_time[1] = now - base_time;
3667 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3668 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3669 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3670 GST_TIME_ARGS (base_time));
3672 GST_OBJECT_UNLOCK (element);
3675 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3676 gst_object_unref (element);
3679 gst_buffer_unref (buffer);
3684 /* must be called with lock */
3686 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3689 GstRTSPStreamPrivate *priv = stream->priv;
3690 const GstRTSPTransport *tr;
3692 tr = gst_rtsp_stream_transport_get_transport (trans);
3694 switch (tr->lower_transport) {
3695 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3698 if (!check_mcast_part_for_transport (stream, tr))
3700 priv->transports = g_list_prepend (priv->transports, trans);
3702 priv->transports = g_list_remove (priv->transports, trans);
3706 case GST_RTSP_LOWER_TRANS_UDP:
3712 dest = tr->destination;
3713 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3717 } else if (priv->client_side) {
3718 /* In client side mode the 'destination' is the RTSP server, so send
3720 min = tr->server_port.min;
3721 max = tr->server_port.max;
3723 min = tr->client_port.min;
3724 max = tr->client_port.max;
3729 GST_INFO ("setting ttl-mc %d", ttl);
3730 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3731 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3733 GST_INFO ("adding %s:%d-%d", dest, min, max);
3734 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3735 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3736 priv->transports = g_list_prepend (priv->transports, trans);
3738 GST_INFO ("removing %s:%d-%d", dest, min, max);
3739 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3740 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3741 priv->transports = g_list_remove (priv->transports, trans);
3743 priv->transports_cookie++;
3746 case GST_RTSP_LOWER_TRANS_TCP:
3748 GST_INFO ("adding TCP %s", tr->destination);
3749 priv->transports = g_list_prepend (priv->transports, trans);
3751 GST_INFO ("removing TCP %s", tr->destination);
3752 priv->transports = g_list_remove (priv->transports, trans);
3754 priv->transports_cookie++;
3757 goto unknown_transport;
3764 GST_INFO ("Unknown transport %d", tr->lower_transport);
3775 * gst_rtsp_stream_add_transport:
3776 * @stream: a #GstRTSPStream
3777 * @trans: (transfer none): a #GstRTSPStreamTransport
3779 * Add the transport in @trans to @stream. The media of @stream will
3780 * then also be send to the values configured in @trans.
3782 * @stream must be joined to a bin.
3784 * @trans must contain a valid #GstRTSPTransport.
3786 * Returns: %TRUE if @trans was added
3789 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3790 GstRTSPStreamTransport * trans)
3792 GstRTSPStreamPrivate *priv;
3795 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3796 priv = stream->priv;
3797 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3798 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3800 g_mutex_lock (&priv->lock);
3801 res = update_transport (stream, trans, TRUE);
3802 g_mutex_unlock (&priv->lock);
3808 * gst_rtsp_stream_remove_transport:
3809 * @stream: a #GstRTSPStream
3810 * @trans: (transfer none): a #GstRTSPStreamTransport
3812 * Remove the transport in @trans from @stream. The media of @stream will
3813 * not be sent to the values configured in @trans.
3815 * @stream must be joined to a bin.
3817 * @trans must contain a valid #GstRTSPTransport.
3819 * Returns: %TRUE if @trans was removed
3822 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3823 GstRTSPStreamTransport * trans)
3825 GstRTSPStreamPrivate *priv;
3828 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3829 priv = stream->priv;
3830 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3831 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3833 g_mutex_lock (&priv->lock);
3834 res = update_transport (stream, trans, FALSE);
3835 g_mutex_unlock (&priv->lock);
3841 * gst_rtsp_stream_update_crypto:
3842 * @stream: a #GstRTSPStream
3844 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3846 * Update the new crypto information for @ssrc in @stream. If information
3847 * for @ssrc did not exist, it will be added. If information
3848 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3849 * be removed from @stream.
3851 * Returns: %TRUE if @crypto could be updated
3854 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3855 guint ssrc, GstCaps * crypto)
3857 GstRTSPStreamPrivate *priv;
3859 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3860 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3862 priv = stream->priv;
3864 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3866 g_mutex_lock (&priv->lock);
3868 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3869 gst_caps_ref (crypto));
3871 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3872 g_mutex_unlock (&priv->lock);
3878 * gst_rtsp_stream_get_rtp_socket:
3879 * @stream: a #GstRTSPStream
3880 * @family: the socket family
3882 * Get the RTP socket from @stream for a @family.
3884 * @stream must be joined to a bin.
3886 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3887 * socket could be allocated for @family. Unref after usage
3890 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3892 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3896 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3897 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3898 family == G_SOCKET_FAMILY_IPV6, NULL);
3899 g_return_val_if_fail (priv->udpsink[0], NULL);
3901 if (family == G_SOCKET_FAMILY_IPV6)
3906 g_object_get (priv->udpsink[0], name, &socket, NULL);
3912 * gst_rtsp_stream_get_rtcp_socket:
3913 * @stream: a #GstRTSPStream
3914 * @family: the socket family
3916 * Get the RTCP socket from @stream for a @family.
3918 * @stream must be joined to a bin.
3920 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3921 * socket could be allocated for @family. Unref after usage
3924 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3926 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3930 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3931 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3932 family == G_SOCKET_FAMILY_IPV6, NULL);
3933 g_return_val_if_fail (priv->udpsink[1], NULL);
3935 if (family == G_SOCKET_FAMILY_IPV6)
3940 g_object_get (priv->udpsink[1], name, &socket, NULL);
3946 * gst_rtsp_stream_get_rtp_multicast_socket:
3947 * @stream: a #GstRTSPStream
3948 * @family: the socket family
3950 * Get the multicast RTP socket from @stream for a @family.
3952 * Returns: (transfer full) (nullable): the multicast RTP socket or %NULL if no
3953 * socket could be allocated for @family. Unref after usage
3956 gst_rtsp_stream_get_rtp_multicast_socket (GstRTSPStream * stream, GSocketFamily family)
3958 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3962 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3963 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3964 family == G_SOCKET_FAMILY_IPV6, NULL);
3965 g_return_val_if_fail (priv->mcast_udpsink[0], NULL);
3967 if (family == G_SOCKET_FAMILY_IPV6)
3972 g_object_get (priv->mcast_udpsink[0], name, &socket, NULL);
3978 * gst_rtsp_stream_get_rtcp_multicast_socket:
3979 * @stream: a #GstRTSPStream
3980 * @family: the socket family
3982 * Get the multicast RTCP socket from @stream for a @family.
3984 * Returns: (transfer full) (nullable): the multicast RTCP socket or %NULL if no
3985 * socket could be allocated for @family. Unref after usage
3988 gst_rtsp_stream_get_rtcp_multicast_socket (GstRTSPStream * stream, GSocketFamily family)
3990 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3994 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3995 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3996 family == G_SOCKET_FAMILY_IPV6, NULL);
3997 g_return_val_if_fail (priv->mcast_udpsink[1], NULL);
3999 if (family == G_SOCKET_FAMILY_IPV6)
4004 g_object_get (priv->mcast_udpsink[1], name, &socket, NULL);
4010 * gst_rtsp_stream_set_seqnum:
4011 * @stream: a #GstRTSPStream
4012 * @seqnum: a new sequence number
4014 * Configure the sequence number in the payloader of @stream to @seqnum.
4017 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
4019 GstRTSPStreamPrivate *priv;
4021 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
4023 priv = stream->priv;
4025 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
4029 * gst_rtsp_stream_get_seqnum:
4030 * @stream: a #GstRTSPStream
4032 * Get the configured sequence number in the payloader of @stream.
4034 * Returns: the sequence number of the payloader.
4037 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
4039 GstRTSPStreamPrivate *priv;
4042 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
4044 priv = stream->priv;
4046 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
4052 * gst_rtsp_stream_transport_filter:
4053 * @stream: a #GstRTSPStream
4054 * @func: (scope call) (allow-none): a callback
4055 * @user_data: (closure): user data passed to @func
4057 * Call @func for each transport managed by @stream. The result value of @func
4058 * determines what happens to the transport. @func will be called with @stream
4059 * locked so no further actions on @stream can be performed from @func.
4061 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
4064 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
4066 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
4067 * will also be added with an additional ref to the result #GList of this
4070 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
4072 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
4073 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
4074 * element in the #GList should be unreffed before the list is freed.
4077 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
4078 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
4080 GstRTSPStreamPrivate *priv;
4081 GList *result, *walk, *next;
4082 GHashTable *visited = NULL;
4085 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
4087 priv = stream->priv;
4091 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
4093 g_mutex_lock (&priv->lock);
4095 cookie = priv->transports_cookie;
4096 for (walk = priv->transports; walk; walk = next) {
4097 GstRTSPStreamTransport *trans = walk->data;
4098 GstRTSPFilterResult res;
4101 next = g_list_next (walk);
4104 /* only visit each transport once */
4105 if (g_hash_table_contains (visited, trans))
4108 g_hash_table_add (visited, g_object_ref (trans));
4109 g_mutex_unlock (&priv->lock);
4111 res = func (stream, trans, user_data);
4113 g_mutex_lock (&priv->lock);
4115 res = GST_RTSP_FILTER_REF;
4117 changed = (cookie != priv->transports_cookie);
4120 case GST_RTSP_FILTER_REMOVE:
4121 update_transport (stream, trans, FALSE);
4123 case GST_RTSP_FILTER_REF:
4124 result = g_list_prepend (result, g_object_ref (trans));
4126 case GST_RTSP_FILTER_KEEP:
4133 g_mutex_unlock (&priv->lock);
4136 g_hash_table_unref (visited);
4141 static GstPadProbeReturn
4142 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
4144 GstRTSPStreamPrivate *priv;
4145 GstRTSPStream *stream;
4146 GstBuffer *buffer = NULL;
4149 priv = stream->priv;
4151 GST_DEBUG_OBJECT (pad, "now blocking");
4153 g_mutex_lock (&priv->lock);
4154 priv->blocking = TRUE;
4156 if ((info->type & GST_PAD_PROBE_TYPE_BUFFER)) {
4157 buffer = gst_pad_probe_info_get_buffer (info);
4158 } else if ((info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST)) {
4159 GstBufferList *list = gst_pad_probe_info_get_buffer_list (info);
4160 buffer = gst_buffer_list_get (list, 0);
4162 g_assert_not_reached ();
4166 priv->position = GST_BUFFER_TIMESTAMP (buffer);
4167 GST_DEBUG_OBJECT (stream, "buffer position: %" GST_TIME_FORMAT,
4168 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
4169 g_mutex_unlock (&priv->lock);
4171 gst_element_post_message (priv->payloader,
4172 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
4173 gst_structure_new_empty ("GstRTSPStreamBlocking")));
4175 return GST_PAD_PROBE_OK;
4179 set_blocked (GstRTSPStream * stream, gboolean blocked)
4181 GstRTSPStreamPrivate *priv;
4184 GST_DEBUG_OBJECT (stream, "blocked: %d", blocked);
4186 priv = stream->priv;
4189 priv->blocking = FALSE;
4190 for (i = 0; i < 2; i++) {
4191 if (priv->blocked_id[i] != 0)
4193 if (priv->send_src[i]) {
4194 priv->blocked_id[i] = gst_pad_add_probe (priv->send_src[i],
4195 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
4196 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
4197 g_object_ref (stream), g_object_unref);
4201 for (i = 0; i < 2; i++) {
4202 if (priv->blocked_id[i] != 0) {
4203 gst_pad_remove_probe (priv->send_src[i], priv->blocked_id[i]);
4204 priv->blocked_id[i] = 0;
4207 priv->blocking = FALSE;
4212 * gst_rtsp_stream_set_blocked:
4213 * @stream: a #GstRTSPStream
4214 * @blocked: boolean indicating we should block or unblock
4216 * Blocks or unblocks the dataflow on @stream.
4218 * Returns: %TRUE on success
4221 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
4223 GstRTSPStreamPrivate *priv;
4225 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4227 priv = stream->priv;
4228 g_mutex_lock (&priv->lock);
4229 set_blocked (stream, blocked);
4230 g_mutex_unlock (&priv->lock);
4236 * gst_rtsp_stream_ublock_linked:
4237 * @stream: a #GstRTSPStream
4239 * Unblocks the dataflow on @stream if it is linked.
4241 * Returns: %TRUE on success
4244 gst_rtsp_stream_unblock_linked (GstRTSPStream * stream)
4246 GstRTSPStreamPrivate *priv;
4248 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4250 priv = stream->priv;
4251 g_mutex_lock (&priv->lock);
4252 if (priv->send_src[0] && gst_pad_is_linked (priv->send_src[0]))
4253 set_blocked (stream, FALSE);
4254 g_mutex_unlock (&priv->lock);
4260 * gst_rtsp_stream_is_blocking:
4261 * @stream: a #GstRTSPStream
4263 * Check if @stream is blocking on a #GstBuffer.
4265 * Returns: %TRUE if @stream is blocking
4268 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
4270 GstRTSPStreamPrivate *priv;
4273 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4275 priv = stream->priv;
4277 g_mutex_lock (&priv->lock);
4278 result = priv->blocking;
4279 g_mutex_unlock (&priv->lock);
4285 * gst_rtsp_stream_query_position:
4286 * @stream: a #GstRTSPStream
4287 * @position: current position of a #GstRTSPStream
4289 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
4290 * the RTP parts of the pipeline and not the RTCP parts.
4292 * Returns: %TRUE if the position could be queried
4295 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
4297 GstRTSPStreamPrivate *priv;
4301 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4303 /* query position: if no sinks have been added yet,
4304 * we obtain the position from the pad otherwise we query the sinks */
4306 priv = stream->priv;
4308 g_mutex_lock (&priv->lock);
4309 /* depending on the transport type, it should query corresponding sink */
4310 if (priv->protocols & GST_RTSP_LOWER_TRANS_UDP)
4311 sink = priv->udpsink[0];
4312 else if (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
4313 sink = priv->mcast_udpsink[0];
4315 sink = priv->appsink[0];
4318 gst_object_ref (sink);
4319 } else if (priv->send_src[0]) {
4320 pad = gst_object_ref (priv->send_src[0]);
4322 g_mutex_unlock (&priv->lock);
4323 GST_WARNING_OBJECT (stream, "Couldn't obtain postion: erroneous pipeline");
4326 g_mutex_unlock (&priv->lock);
4329 if (!gst_element_query_position (sink , GST_FORMAT_TIME, position)) {
4330 GST_WARNING_OBJECT (stream, "Couldn't obtain postion: position query failed");
4331 gst_object_unref (sink);
4334 gst_object_unref (sink);
4337 const GstSegment *segment;
4339 event = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
4341 GST_WARNING_OBJECT (stream, "Couldn't obtain postion: no segment event");
4342 gst_object_unref (pad);
4346 gst_event_parse_segment (event, &segment);
4347 if (segment->format != GST_FORMAT_TIME) {
4350 g_mutex_lock (&priv->lock);
4351 *position = priv->position;
4352 g_mutex_unlock (&priv->lock);
4354 gst_segment_to_stream_time (segment, GST_FORMAT_TIME, *position);
4356 gst_event_unref (event);
4357 gst_object_unref (pad);
4364 * gst_rtsp_stream_query_stop:
4365 * @stream: a #GstRTSPStream
4366 * @stop: current stop of a #GstRTSPStream
4368 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
4369 * the RTP parts of the pipeline and not the RTCP parts.
4371 * Returns: %TRUE if the stop could be queried
4374 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
4376 GstRTSPStreamPrivate *priv;
4380 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4382 /* query stop position: if no sinks have been added yet,
4383 * we obtain the stop position from the pad otherwise we query the sinks */
4385 priv = stream->priv;
4387 g_mutex_lock (&priv->lock);
4388 /* depending on the transport type, it should query corresponding sink */
4389 if (priv->protocols & GST_RTSP_LOWER_TRANS_UDP)
4390 sink = priv->udpsink[0];
4391 else if (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
4392 sink = priv->mcast_udpsink[0];
4394 sink = priv->appsink[0];
4397 gst_object_ref (sink);
4398 } else if (priv->send_src[0]) {
4399 pad = gst_object_ref (priv->send_src[0]);
4401 g_mutex_unlock (&priv->lock);
4402 GST_WARNING_OBJECT (stream, "Couldn't obtain stop: erroneous pipeline");
4405 g_mutex_unlock (&priv->lock);
4411 query = gst_query_new_segment (GST_FORMAT_TIME);
4412 if (!gst_element_query (sink, query)) {
4413 GST_WARNING_OBJECT (stream, "Couldn't obtain stop: element query failed");
4414 gst_query_unref (query);
4415 gst_object_unref (sink);
4418 gst_query_parse_segment (query, NULL, &format, NULL, stop);
4419 if (format != GST_FORMAT_TIME)
4421 gst_query_unref (query);
4422 gst_object_unref (sink);
4425 const GstSegment *segment;
4427 event = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
4429 GST_WARNING_OBJECT (stream, "Couldn't obtain stop: no segment event");
4430 gst_object_unref (pad);
4433 gst_event_parse_segment (event, &segment);
4434 if (segment->format != GST_FORMAT_TIME) {
4437 *stop = segment->stop;
4439 *stop = segment->duration;
4441 *stop = gst_segment_to_stream_time (segment, GST_FORMAT_TIME, *stop);
4443 gst_event_unref (event);
4444 gst_object_unref (pad);
4451 * gst_rtsp_stream_complete_stream:
4452 * @stream: a #GstRTSPStream
4453 * @transport: a #GstRTSPTransport
4455 * Add a receiver and sender part to the pipeline based on the transport from
4458 * Returns: %TRUE if the pipeline has been sucessfully updated.
4461 gst_rtsp_stream_complete_stream (GstRTSPStream * stream,
4462 const GstRTSPTransport * transport)
4464 GstRTSPStreamPrivate *priv;
4466 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4468 priv = stream->priv;
4469 GST_DEBUG_OBJECT (stream, "complete stream");
4471 g_mutex_lock (&priv->lock);
4473 if (!(priv->protocols & transport->lower_transport))
4474 goto unallowed_transport;
4476 if (!create_receiver_part (stream, transport))
4477 goto create_receiver_error;
4479 /* in the RECORD case, we only add RTCP sender part */
4480 if (!create_sender_part (stream, transport))
4481 goto create_sender_error;
4483 g_mutex_unlock (&priv->lock);
4485 GST_DEBUG_OBJECT (stream, "pipeline sucsessfully updated");
4488 create_receiver_error:
4489 create_sender_error:
4490 unallowed_transport:
4492 g_mutex_unlock (&priv->lock);