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 */
97 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
99 GstElement *udpsrc_v4[2];
100 /* UDP sources for UDP multicast transports */
101 GstElement *udpsrc_mcast_v4[2];
103 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
105 GstElement *udpsrc_v6[2];
106 /* UDP sources for UDP multicast transports */
107 GstElement *udpsrc_mcast_v6[2];
109 GstElement *udpqueue[2];
110 GstElement *udpsink[2];
112 /* for TCP transport */
113 GstElement *appsrc[2];
114 GstClockTime appsrc_base_time[2];
115 GstElement *appqueue[2];
116 GstElement *appsink[2];
119 GstElement *funnel[2];
124 GstClockTime rtx_time;
126 /* server ports for sending/receiving over ipv4 */
127 GstRTSPRange server_port_v4;
128 GstRTSPAddress *server_addr_v4;
131 /* server ports for sending/receiving over ipv6 */
132 GstRTSPRange server_port_v6;
133 GstRTSPAddress *server_addr_v6;
136 /* multicast addresses */
137 GstRTSPAddressPool *pool;
138 GstRTSPAddress *addr_v4;
139 GstRTSPAddress *addr_v6;
140 gboolean have_ipv4_mcast;
141 gboolean have_ipv6_mcast;
143 gchar *multicast_iface;
145 /* the caps of the stream */
149 /* transports we stream to */
152 guint transports_cookie;
154 GList *tr_cache_rtcp;
155 guint tr_cache_cookie_rtp;
156 guint tr_cache_cookie_rtcp;
161 /* stream blocking */
165 /* pt->caps map for RECORD streams */
168 GstRTSPPublishClockMode publish_clock_mode;
171 #define DEFAULT_CONTROL NULL
172 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
173 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
174 GST_RTSP_LOWER_TRANS_TCP
187 SIGNAL_NEW_RTP_ENCODER,
188 SIGNAL_NEW_RTCP_ENCODER,
192 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
193 #define GST_CAT_DEFAULT rtsp_stream_debug
195 static GQuark ssrc_stream_map_key;
197 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
198 GValue * value, GParamSpec * pspec);
199 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
200 const GValue * value, GParamSpec * pspec);
202 static void gst_rtsp_stream_finalize (GObject * obj);
204 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
206 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
209 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
211 GObjectClass *gobject_class;
213 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
215 gobject_class = G_OBJECT_CLASS (klass);
217 gobject_class->get_property = gst_rtsp_stream_get_property;
218 gobject_class->set_property = gst_rtsp_stream_set_property;
219 gobject_class->finalize = gst_rtsp_stream_finalize;
221 g_object_class_install_property (gobject_class, PROP_CONTROL,
222 g_param_spec_string ("control", "Control",
223 "The control string for this stream", DEFAULT_CONTROL,
224 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 g_object_class_install_property (gobject_class, PROP_PROFILES,
227 g_param_spec_flags ("profiles", "Profiles",
228 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
229 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
232 g_param_spec_flags ("protocols", "Protocols",
233 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
234 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
237 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
238 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
239 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
241 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
242 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
243 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
244 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
246 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
248 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
252 gst_rtsp_stream_init (GstRTSPStream * stream)
254 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
256 GST_DEBUG ("new stream %p", stream);
261 priv->control = g_strdup (DEFAULT_CONTROL);
262 priv->profiles = DEFAULT_PROFILES;
263 priv->protocols = DEFAULT_PROTOCOLS;
264 priv->publish_clock_mode = GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK;
266 g_mutex_init (&priv->lock);
268 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
269 NULL, (GDestroyNotify) gst_caps_unref);
270 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
271 (GDestroyNotify) gst_caps_unref);
275 gst_rtsp_stream_finalize (GObject * obj)
277 GstRTSPStream *stream;
278 GstRTSPStreamPrivate *priv;
280 stream = GST_RTSP_STREAM (obj);
283 GST_DEBUG ("finalize stream %p", stream);
285 /* we really need to be unjoined now */
286 g_return_if_fail (priv->joined_bin == NULL);
289 gst_rtsp_address_free (priv->addr_v4);
291 gst_rtsp_address_free (priv->addr_v6);
292 if (priv->server_addr_v4)
293 gst_rtsp_address_free (priv->server_addr_v4);
294 if (priv->server_addr_v6)
295 gst_rtsp_address_free (priv->server_addr_v6);
297 g_object_unref (priv->pool);
299 g_object_unref (priv->rtxsend);
301 g_free (priv->multicast_iface);
303 gst_object_unref (priv->payloader);
305 gst_object_unref (priv->srcpad);
307 gst_object_unref (priv->sinkpad);
308 g_free (priv->control);
309 g_mutex_clear (&priv->lock);
311 g_hash_table_unref (priv->keys);
312 g_hash_table_destroy (priv->ptmap);
314 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
318 gst_rtsp_stream_get_property (GObject * object, guint propid,
319 GValue * value, GParamSpec * pspec)
321 GstRTSPStream *stream = GST_RTSP_STREAM (object);
325 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
328 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
331 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
339 gst_rtsp_stream_set_property (GObject * object, guint propid,
340 const GValue * value, GParamSpec * pspec)
342 GstRTSPStream *stream = GST_RTSP_STREAM (object);
346 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
349 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
352 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
360 * gst_rtsp_stream_new:
363 * @payloader: a #GstElement
365 * Create a new media stream with index @idx that handles RTP data on
366 * @pad and has a payloader element @payloader if @pad is a source pad
367 * or a depayloader element @payloader if @pad is a sink pad.
369 * Returns: (transfer full): a new #GstRTSPStream
372 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
374 GstRTSPStreamPrivate *priv;
375 GstRTSPStream *stream;
377 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
378 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
380 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
383 priv->payloader = gst_object_ref (payloader);
384 if (GST_PAD_IS_SRC (pad))
385 priv->srcpad = gst_object_ref (pad);
387 priv->sinkpad = gst_object_ref (pad);
393 * gst_rtsp_stream_get_index:
394 * @stream: a #GstRTSPStream
396 * Get the stream index.
398 * Return: the stream index.
401 gst_rtsp_stream_get_index (GstRTSPStream * stream)
403 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
405 return stream->priv->idx;
409 * gst_rtsp_stream_get_pt:
410 * @stream: a #GstRTSPStream
412 * Get the stream payload type.
414 * Return: the stream payload type.
417 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
419 GstRTSPStreamPrivate *priv;
422 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
426 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
432 * gst_rtsp_stream_get_srcpad:
433 * @stream: a #GstRTSPStream
435 * Get the srcpad associated with @stream.
437 * Returns: (transfer full): the srcpad. Unref after usage.
440 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
442 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
444 if (!stream->priv->srcpad)
447 return gst_object_ref (stream->priv->srcpad);
451 * gst_rtsp_stream_get_sinkpad:
452 * @stream: a #GstRTSPStream
454 * Get the sinkpad associated with @stream.
456 * Returns: (transfer full): the sinkpad. Unref after usage.
459 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
461 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
463 if (!stream->priv->sinkpad)
466 return gst_object_ref (stream->priv->sinkpad);
470 * gst_rtsp_stream_get_control:
471 * @stream: a #GstRTSPStream
473 * Get the control string to identify this stream.
475 * Returns: (transfer full): the control string. g_free() after usage.
478 gst_rtsp_stream_get_control (GstRTSPStream * stream)
480 GstRTSPStreamPrivate *priv;
483 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
487 g_mutex_lock (&priv->lock);
488 if ((result = g_strdup (priv->control)) == NULL)
489 result = g_strdup_printf ("stream=%u", priv->idx);
490 g_mutex_unlock (&priv->lock);
496 * gst_rtsp_stream_set_control:
497 * @stream: a #GstRTSPStream
498 * @control: a control string
500 * Set the control string in @stream.
503 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
505 GstRTSPStreamPrivate *priv;
507 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
511 g_mutex_lock (&priv->lock);
512 g_free (priv->control);
513 priv->control = g_strdup (control);
514 g_mutex_unlock (&priv->lock);
518 * gst_rtsp_stream_has_control:
519 * @stream: a #GstRTSPStream
520 * @control: a control string
522 * Check if @stream has the control string @control.
524 * Returns: %TRUE is @stream has @control as the control string
527 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
529 GstRTSPStreamPrivate *priv;
532 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
536 g_mutex_lock (&priv->lock);
538 res = (g_strcmp0 (priv->control, control) == 0);
542 if (sscanf (control, "stream=%u", &streamid) > 0)
543 res = (streamid == priv->idx);
547 g_mutex_unlock (&priv->lock);
553 * gst_rtsp_stream_set_mtu:
554 * @stream: a #GstRTSPStream
557 * Configure the mtu in the payloader of @stream to @mtu.
560 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
562 GstRTSPStreamPrivate *priv;
564 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
568 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
570 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
574 * gst_rtsp_stream_get_mtu:
575 * @stream: a #GstRTSPStream
577 * Get the configured MTU in the payloader of @stream.
579 * Returns: the MTU of the payloader.
582 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
584 GstRTSPStreamPrivate *priv;
587 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
591 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
596 /* Update the dscp qos property on the udp sinks */
598 update_dscp_qos (GstRTSPStream * stream)
600 GstRTSPStreamPrivate *priv;
602 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
606 if (priv->udpsink[0]) {
607 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
611 if (priv->udpsink[1]) {
612 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
618 * gst_rtsp_stream_set_dscp_qos:
619 * @stream: a #GstRTSPStream
620 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
622 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
625 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
627 GstRTSPStreamPrivate *priv;
629 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
633 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
635 if (dscp_qos < -1 || dscp_qos > 63) {
636 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
640 priv->dscp_qos = dscp_qos;
642 update_dscp_qos (stream);
646 * gst_rtsp_stream_get_dscp_qos:
647 * @stream: a #GstRTSPStream
649 * Get the configured DSCP QoS in of the outgoing sockets.
651 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
654 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
656 GstRTSPStreamPrivate *priv;
658 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
662 return priv->dscp_qos;
666 * gst_rtsp_stream_is_transport_supported:
667 * @stream: a #GstRTSPStream
668 * @transport: (transfer none): a #GstRTSPTransport
670 * Check if @transport can be handled by stream
672 * Returns: %TRUE if @transport can be handled by @stream.
675 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
676 GstRTSPTransport * transport)
678 GstRTSPStreamPrivate *priv;
680 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
684 g_mutex_lock (&priv->lock);
685 if (transport->trans != GST_RTSP_TRANS_RTP)
686 goto unsupported_transmode;
688 if (!(transport->profile & priv->profiles))
689 goto unsupported_profile;
691 if (!(transport->lower_transport & priv->protocols))
692 goto unsupported_ltrans;
694 g_mutex_unlock (&priv->lock);
699 unsupported_transmode:
701 GST_DEBUG ("unsupported transport mode %d", transport->trans);
702 g_mutex_unlock (&priv->lock);
707 GST_DEBUG ("unsupported profile %d", transport->profile);
708 g_mutex_unlock (&priv->lock);
713 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
714 g_mutex_unlock (&priv->lock);
720 * gst_rtsp_stream_set_profiles:
721 * @stream: a #GstRTSPStream
722 * @profiles: the new profiles
724 * Configure the allowed profiles for @stream.
727 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
729 GstRTSPStreamPrivate *priv;
731 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
735 g_mutex_lock (&priv->lock);
736 priv->profiles = profiles;
737 g_mutex_unlock (&priv->lock);
741 * gst_rtsp_stream_get_profiles:
742 * @stream: a #GstRTSPStream
744 * Get the allowed profiles of @stream.
746 * Returns: a #GstRTSPProfile
749 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
751 GstRTSPStreamPrivate *priv;
754 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
758 g_mutex_lock (&priv->lock);
759 res = priv->profiles;
760 g_mutex_unlock (&priv->lock);
766 * gst_rtsp_stream_set_protocols:
767 * @stream: a #GstRTSPStream
768 * @protocols: the new flags
770 * Configure the allowed lower transport for @stream.
773 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
774 GstRTSPLowerTrans protocols)
776 GstRTSPStreamPrivate *priv;
778 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
782 g_mutex_lock (&priv->lock);
783 priv->protocols = protocols;
784 g_mutex_unlock (&priv->lock);
788 * gst_rtsp_stream_get_protocols:
789 * @stream: a #GstRTSPStream
791 * Get the allowed protocols of @stream.
793 * Returns: a #GstRTSPLowerTrans
796 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
798 GstRTSPStreamPrivate *priv;
799 GstRTSPLowerTrans res;
801 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
802 GST_RTSP_LOWER_TRANS_UNKNOWN);
806 g_mutex_lock (&priv->lock);
807 res = priv->protocols;
808 g_mutex_unlock (&priv->lock);
814 * gst_rtsp_stream_set_address_pool:
815 * @stream: a #GstRTSPStream
816 * @pool: (transfer none): a #GstRTSPAddressPool
818 * configure @pool to be used as the address pool of @stream.
821 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
822 GstRTSPAddressPool * pool)
824 GstRTSPStreamPrivate *priv;
825 GstRTSPAddressPool *old;
827 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
831 GST_LOG_OBJECT (stream, "set address pool %p", pool);
833 g_mutex_lock (&priv->lock);
834 if ((old = priv->pool) != pool)
835 priv->pool = pool ? g_object_ref (pool) : NULL;
838 g_mutex_unlock (&priv->lock);
841 g_object_unref (old);
845 * gst_rtsp_stream_get_address_pool:
846 * @stream: a #GstRTSPStream
848 * Get the #GstRTSPAddressPool used as the address pool of @stream.
850 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
854 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
856 GstRTSPStreamPrivate *priv;
857 GstRTSPAddressPool *result;
859 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
863 g_mutex_lock (&priv->lock);
864 if ((result = priv->pool))
865 g_object_ref (result);
866 g_mutex_unlock (&priv->lock);
872 * gst_rtsp_stream_set_multicast_iface:
873 * @stream: a #GstRTSPStream
874 * @multicast_iface: (transfer none): a multicast interface
876 * configure @multicast_iface to be used for @stream.
879 gst_rtsp_stream_set_multicast_iface (GstRTSPStream * stream,
880 const gchar * multicast_iface)
882 GstRTSPStreamPrivate *priv;
885 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
889 GST_LOG_OBJECT (stream, "set multicast iface %s",
890 GST_STR_NULL (multicast_iface));
892 g_mutex_lock (&priv->lock);
893 if ((old = priv->multicast_iface) != multicast_iface)
894 priv->multicast_iface = multicast_iface ? g_strdup (multicast_iface) : NULL;
897 g_mutex_unlock (&priv->lock);
904 * gst_rtsp_stream_get_multicast_iface:
905 * @stream: a #GstRTSPStream
907 * Get the multicast interface used for @stream.
909 * Returns: (transfer full): the multicast interface for @stream. g_free() after
913 gst_rtsp_stream_get_multicast_iface (GstRTSPStream * stream)
915 GstRTSPStreamPrivate *priv;
918 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
922 g_mutex_lock (&priv->lock);
923 if ((result = priv->multicast_iface))
924 result = g_strdup (result);
925 g_mutex_unlock (&priv->lock);
931 * gst_rtsp_stream_get_multicast_address:
932 * @stream: a #GstRTSPStream
933 * @family: the #GSocketFamily
935 * Get the multicast address of @stream for @family.
937 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
938 * or %NULL when no address could be allocated. gst_rtsp_address_free()
942 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
943 GSocketFamily family)
945 GstRTSPStreamPrivate *priv;
946 GstRTSPAddress *result;
947 GstRTSPAddress **addrp;
948 GstRTSPAddressFlags flags;
950 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
954 if (family == G_SOCKET_FAMILY_IPV6) {
955 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
956 addrp = &priv->addr_v6;
958 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
959 addrp = &priv->addr_v4;
962 g_mutex_lock (&priv->lock);
963 if (*addrp == NULL) {
964 if (priv->pool == NULL)
967 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
969 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
973 result = gst_rtsp_address_copy (*addrp);
974 g_mutex_unlock (&priv->lock);
981 GST_ERROR_OBJECT (stream, "no address pool specified");
982 g_mutex_unlock (&priv->lock);
987 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
988 g_mutex_unlock (&priv->lock);
994 * gst_rtsp_stream_reserve_address:
995 * @stream: a #GstRTSPStream
996 * @address: an address
1001 * Reserve @address and @port as the address and port of @stream.
1003 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
1004 * the address could be reserved. gst_rtsp_address_free() after usage.
1007 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
1008 const gchar * address, guint port, guint n_ports, guint ttl)
1010 GstRTSPStreamPrivate *priv;
1011 GstRTSPAddress *result;
1013 GSocketFamily family;
1014 GstRTSPAddress **addrp;
1016 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1017 g_return_val_if_fail (address != NULL, NULL);
1018 g_return_val_if_fail (port > 0, NULL);
1019 g_return_val_if_fail (n_ports > 0, NULL);
1020 g_return_val_if_fail (ttl > 0, NULL);
1022 priv = stream->priv;
1024 addr = g_inet_address_new_from_string (address);
1026 GST_ERROR ("failed to get inet addr from %s", address);
1027 family = G_SOCKET_FAMILY_IPV4;
1029 family = g_inet_address_get_family (addr);
1030 g_object_unref (addr);
1033 if (family == G_SOCKET_FAMILY_IPV6)
1034 addrp = &priv->addr_v6;
1036 addrp = &priv->addr_v4;
1038 g_mutex_lock (&priv->lock);
1039 if (*addrp == NULL) {
1040 GstRTSPAddressPoolResult res;
1042 if (priv->pool == NULL)
1045 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
1046 port, n_ports, ttl, addrp);
1047 if (res != GST_RTSP_ADDRESS_POOL_OK)
1050 if (strcmp ((*addrp)->address, address) ||
1051 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
1052 (*addrp)->ttl != ttl)
1053 goto different_address;
1055 result = gst_rtsp_address_copy (*addrp);
1056 g_mutex_unlock (&priv->lock);
1063 GST_ERROR_OBJECT (stream, "no address pool specified");
1064 g_mutex_unlock (&priv->lock);
1069 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1071 g_mutex_unlock (&priv->lock);
1076 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
1077 " reserved", address);
1078 g_mutex_unlock (&priv->lock);
1083 /* must be called with lock */
1085 set_sockets_for_udpsinks (GstRTSPStream * stream, GSocket * rtp_socket,
1086 GSocket * rtcp_socket, GSocketFamily family)
1088 GstRTSPStreamPrivate *priv = stream->priv;
1089 const gchar *multisink_socket;
1091 if (family == G_SOCKET_FAMILY_IPV6)
1092 multisink_socket = "socket-v6";
1094 multisink_socket = "socket";
1096 g_object_set (G_OBJECT (priv->udpsink[0]), multisink_socket, rtp_socket,
1098 g_object_set (G_OBJECT (priv->udpsink[1]), multisink_socket, rtcp_socket,
1102 /* must be called with lock */
1104 create_and_configure_udpsinks (GstRTSPStream * stream)
1106 GstRTSPStreamPrivate *priv = stream->priv;
1107 GstElement *udpsink0, *udpsink1;
1112 if (priv->udpsink[0])
1113 udpsink0 = priv->udpsink[0];
1115 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1118 goto no_udp_protocol;
1120 if (priv->udpsink[1])
1121 udpsink1 = priv->udpsink[1];
1123 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1126 goto no_udp_protocol;
1128 /* configure sinks */
1130 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1131 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1133 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1134 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1136 g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
1138 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1139 /* Needs to be async for RECORD streams, otherwise we will never go to
1140 * PLAYING because the sinks will wait for data while the udpsrc can't
1141 * provide data with timestamps in PAUSED. */
1143 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1144 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1146 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1147 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1149 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1150 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1152 /* update the dscp qos field in the sinks */
1153 update_dscp_qos (stream);
1155 priv->udpsink[0] = udpsink0;
1156 priv->udpsink[1] = udpsink1;
1167 /* must be called with lock */
1169 play_udpsources_one_family (GstRTSPStream * stream, GstElement * udpsrc_out[2],
1170 GSocketFamily family)
1172 GstRTSPStreamPrivate *priv;
1173 GstPad *pad, *selpad;
1176 priv = stream->priv;
1178 for (i = 0; i < 2; i++) {
1179 if (!priv->sinkpad && i == 0) {
1180 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
1181 * RTCP sink always */
1186 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1187 * values. This is only relevant for PLAY pipelines */
1188 gst_element_set_state (udpsrc_out[i], GST_STATE_PLAYING);
1189 gst_element_set_locked_state (udpsrc_out[i], TRUE);
1193 gst_bin_add (priv->joined_bin, udpsrc_out[i]);
1195 /* and link to the funnel */
1196 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1197 pad = gst_element_get_static_pad (udpsrc_out[i], "src");
1198 gst_pad_link (pad, selpad);
1199 gst_object_unref (pad);
1200 gst_object_unref (selpad);
1202 /* otherwise sync state with parent in case it's running already
1204 if (!priv->srcpad) {
1205 gst_element_sync_state_with_parent (udpsrc_out[i]);
1210 /* must be called with lock */
1212 create_and_configure_udpsources_one_family (GstElement * udpsrc_out[2],
1213 GSocket * rtp_socket, GSocket * rtcp_socket, GSocketFamily family,
1214 const gchar * address, gint rtpport, gint rtcpport,
1215 const gchar * multicast_iface, GstRTSPLowerTrans transport)
1217 GstStateChangeReturn ret;
1219 udpsrc_out[0] = gst_element_factory_make ("udpsrc", NULL);
1220 udpsrc_out[1] = gst_element_factory_make ("udpsrc", NULL);
1222 if (udpsrc_out[0] == NULL || udpsrc_out[1] == NULL)
1225 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1226 g_object_set (G_OBJECT (udpsrc_out[0]), "address", address, NULL);
1227 g_object_set (G_OBJECT (udpsrc_out[1]), "address", address, NULL);
1228 g_object_set (G_OBJECT (udpsrc_out[0]), "port", rtpport, NULL);
1229 g_object_set (G_OBJECT (udpsrc_out[1]), "port", rtcpport, NULL);
1230 g_object_set (G_OBJECT (udpsrc_out[0]), "multicast-iface", multicast_iface,
1232 g_object_set (G_OBJECT (udpsrc_out[1]), "multicast-iface", multicast_iface,
1234 g_object_set (G_OBJECT (udpsrc_out[0]), "loop", FALSE, NULL);
1235 g_object_set (G_OBJECT (udpsrc_out[1]), "loop", FALSE, NULL);
1238 g_object_set (G_OBJECT (udpsrc_out[0]), "socket", rtp_socket, NULL);
1239 g_object_set (G_OBJECT (udpsrc_out[1]), "socket", rtcp_socket, NULL);
1241 ret = gst_element_set_state (udpsrc_out[0], GST_STATE_READY);
1242 if (ret == GST_STATE_CHANGE_FAILURE)
1244 ret = gst_element_set_state (udpsrc_out[1], GST_STATE_READY);
1245 if (ret == GST_STATE_CHANGE_FAILURE)
1253 if (udpsrc_out[0]) {
1254 gst_element_set_state (udpsrc_out[0], GST_STATE_NULL);
1255 g_clear_object (&udpsrc_out[0]);
1257 if (udpsrc_out[1]) {
1258 gst_element_set_state (udpsrc_out[1], GST_STATE_NULL);
1259 g_clear_object (&udpsrc_out[1]);
1266 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1267 GstElement * udpsrc_out[2], GstRTSPRange * server_port_out,
1268 GstRTSPTransport * ct, GstRTSPAddress ** server_addr_out,
1269 gboolean use_client_settings)
1271 GstRTSPStreamPrivate *priv = stream->priv;
1272 GSocket *rtp_socket = NULL;
1273 GSocket *rtcp_socket;
1274 gint tmp_rtp, tmp_rtcp;
1276 gint rtpport, rtcpport;
1277 GList *rejected_addresses = NULL;
1278 GstRTSPAddress *addr = NULL;
1279 GInetAddress *inetaddr = NULL;
1281 GSocketAddress *rtp_sockaddr = NULL;
1282 GSocketAddress *rtcp_sockaddr = NULL;
1283 GstRTSPAddressPool *pool;
1284 GstRTSPLowerTrans transport;
1285 const gchar *multicast_iface = priv->multicast_iface;
1289 transport = ct->lower_transport;
1291 /* Start with random port */
1294 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1295 G_SOCKET_PROTOCOL_UDP, NULL);
1297 goto no_udp_protocol;
1298 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1300 if (*server_addr_out)
1301 gst_rtsp_address_free (*server_addr_out);
1303 /* try to allocate 2 UDP ports, the RTP port should be an even
1304 * number and the RTCP port should be the next (uneven) port */
1307 if (rtp_socket == NULL) {
1308 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1309 G_SOCKET_PROTOCOL_UDP, NULL);
1311 goto no_udp_protocol;
1312 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1315 if (pool && ((transport == GST_RTSP_LOWER_TRANS_UDP &&
1316 gst_rtsp_address_pool_has_unicast_addresses (pool))
1317 || transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)) {
1318 GstRTSPAddressFlags flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1320 if (transport == GST_RTSP_LOWER_TRANS_UDP)
1321 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1323 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1326 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1328 if (family == G_SOCKET_FAMILY_IPV6)
1329 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1331 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1333 if (ct->destination && transport == GST_RTSP_LOWER_TRANS_UDP_MCAST
1334 && use_client_settings)
1335 gst_rtsp_address_pool_reserve_address (pool, ct->destination,
1336 ct->port.min, 2, ct->ttl, &addr);
1338 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1343 tmp_rtp = addr->port;
1345 g_clear_object (&inetaddr);
1346 inetaddr = g_inet_address_new_from_string (addr->address);
1348 /* If we're supposed to bind to a multicast address, instead bind
1349 * to ANY and let udpsrc later join the relevant multicast group
1351 if (g_inet_address_get_is_multicast (inetaddr)) {
1352 g_object_unref (inetaddr);
1353 inetaddr = g_inet_address_new_any (family);
1362 if (inetaddr == NULL)
1363 inetaddr = g_inet_address_new_any (family);
1366 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1367 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1368 g_object_unref (rtp_sockaddr);
1371 g_object_unref (rtp_sockaddr);
1373 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1374 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1375 g_clear_object (&rtp_sockaddr);
1380 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1381 g_object_unref (rtp_sockaddr);
1383 /* check if port is even */
1384 if ((tmp_rtp & 1) != 0) {
1385 /* port not even, close and allocate another */
1387 g_clear_object (&rtp_socket);
1392 tmp_rtcp = tmp_rtp + 1;
1394 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1395 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1396 g_object_unref (rtcp_sockaddr);
1397 g_clear_object (&rtp_socket);
1400 g_object_unref (rtcp_sockaddr);
1403 addr_str = g_inet_address_to_string (inetaddr);
1405 addr_str = addr->address;
1406 g_clear_object (&inetaddr);
1408 if (!create_and_configure_udpsources_one_family (udpsrc_out, rtp_socket,
1409 rtcp_socket, family, addr_str, tmp_rtp, tmp_rtcp, multicast_iface,
1413 goto no_udp_protocol;
1419 play_udpsources_one_family (stream, udpsrc_out, family);
1421 g_object_get (G_OBJECT (udpsrc_out[0]), "port", &rtpport, NULL);
1422 g_object_get (G_OBJECT (udpsrc_out[1]), "port", &rtcpport, NULL);
1424 /* this should not happen... */
1425 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1428 /* set RTP and RTCP sockets */
1429 set_sockets_for_udpsinks (stream, rtp_socket, rtcp_socket, family);
1431 server_port_out->min = rtpport;
1432 server_port_out->max = rtcpport;
1434 *server_addr_out = addr;
1435 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1437 g_object_unref (rtp_socket);
1438 g_object_unref (rtcp_socket);
1462 g_object_unref (inetaddr);
1463 g_list_free_full (rejected_addresses,
1464 (GDestroyNotify) gst_rtsp_address_free);
1466 gst_rtsp_address_free (addr);
1468 g_object_unref (rtp_socket);
1470 g_object_unref (rtcp_socket);
1476 * gst_rtsp_stream_allocate_udp_sockets:
1477 * @stream: a #GstRTSPStream
1478 * @family: protocol family
1479 * @transport_method: transport method
1481 * Allocates RTP and RTCP ports.
1483 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1486 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1487 GSocketFamily family, GstRTSPTransport * ct, gboolean use_client_settings)
1489 GstRTSPStreamPrivate *priv;
1490 gboolean result = FALSE;
1491 GstRTSPLowerTrans transport = ct->lower_transport;
1493 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1494 priv = stream->priv;
1495 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
1497 g_mutex_lock (&priv->lock);
1499 if (family == G_SOCKET_FAMILY_IPV4) {
1500 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1501 if (priv->have_ipv4_mcast)
1503 priv->have_ipv4_mcast =
1504 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1505 priv->udpsrc_mcast_v4, &priv->server_port_v4, ct, &priv->addr_v4,
1506 use_client_settings);
1509 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4,
1510 &priv->server_port_v4, ct, &priv->server_addr_v4,
1511 use_client_settings);
1514 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1515 if (priv->have_ipv6_mcast)
1517 priv->have_ipv6_mcast =
1518 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1519 priv->udpsrc_mcast_v6, &priv->server_port_v6, ct, &priv->addr_v6,
1520 use_client_settings);
1522 if (priv->have_ipv6)
1525 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6,
1526 &priv->server_port_v6, ct, &priv->server_addr_v6,
1527 use_client_settings);
1532 result = priv->have_ipv4 || priv->have_ipv4_mcast || priv->have_ipv6 ||
1533 priv->have_ipv6_mcast;
1535 g_mutex_unlock (&priv->lock);
1541 * gst_rtsp_stream_set_client_side:
1542 * @stream: a #GstRTSPStream
1543 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1544 * an RTSP connection.
1546 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1547 * streams to an RTSP server via RECORD. This has the practical effect
1548 * of changing which UDP port numbers are used when setting up the local
1549 * side of the stream sending to be either the 'server' or 'client' pair
1550 * of a configured UDP transport.
1553 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1555 GstRTSPStreamPrivate *priv;
1557 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1558 priv = stream->priv;
1559 g_mutex_lock (&priv->lock);
1560 priv->client_side = client_side;
1561 g_mutex_unlock (&priv->lock);
1565 * gst_rtsp_stream_is_client_side:
1566 * @stream: a #GstRTSPStream
1568 * See gst_rtsp_stream_set_client_side()
1570 * Returns: TRUE if this #GstRTSPStream is client-side.
1573 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1575 GstRTSPStreamPrivate *priv;
1578 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1580 priv = stream->priv;
1581 g_mutex_lock (&priv->lock);
1582 ret = priv->client_side;
1583 g_mutex_unlock (&priv->lock);
1589 * gst_rtsp_stream_get_server_port:
1590 * @stream: a #GstRTSPStream
1591 * @server_port: (out): result server port
1592 * @family: the port family to get
1594 * Fill @server_port with the port pair used by the server. This function can
1595 * only be called when @stream has been joined.
1598 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1599 GstRTSPRange * server_port, GSocketFamily family)
1601 GstRTSPStreamPrivate *priv;
1603 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1604 priv = stream->priv;
1605 g_return_if_fail (priv->joined_bin != NULL);
1607 g_mutex_lock (&priv->lock);
1608 if (family == G_SOCKET_FAMILY_IPV4) {
1610 *server_port = priv->server_port_v4;
1613 *server_port = priv->server_port_v6;
1615 g_mutex_unlock (&priv->lock);
1619 * gst_rtsp_stream_get_rtpsession:
1620 * @stream: a #GstRTSPStream
1622 * Get the RTP session of this stream.
1624 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1627 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1629 GstRTSPStreamPrivate *priv;
1632 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1634 priv = stream->priv;
1636 g_mutex_lock (&priv->lock);
1637 if ((session = priv->session))
1638 g_object_ref (session);
1639 g_mutex_unlock (&priv->lock);
1645 * gst_rtsp_stream_get_encoder:
1646 * @stream: a #GstRTSPStream
1648 * Get the SRTP encoder for this stream.
1650 * Returns: (transfer full): The SRTP encoder for this stream. Unref after usage.
1653 gst_rtsp_stream_get_srtp_encoder (GstRTSPStream * stream)
1655 GstRTSPStreamPrivate *priv;
1656 GstElement *encoder;
1658 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1660 priv = stream->priv;
1662 g_mutex_lock (&priv->lock);
1663 if ((encoder = priv->srtpenc))
1664 g_object_ref (encoder);
1665 g_mutex_unlock (&priv->lock);
1671 * gst_rtsp_stream_get_ssrc:
1672 * @stream: a #GstRTSPStream
1673 * @ssrc: (out): result ssrc
1675 * Get the SSRC used by the RTP session of this stream. This function can only
1676 * be called when @stream has been joined.
1679 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1681 GstRTSPStreamPrivate *priv;
1683 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1684 priv = stream->priv;
1685 g_return_if_fail (priv->joined_bin != NULL);
1687 g_mutex_lock (&priv->lock);
1688 if (ssrc && priv->session)
1689 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1690 g_mutex_unlock (&priv->lock);
1694 * gst_rtsp_stream_set_retransmission_time:
1695 * @stream: a #GstRTSPStream
1696 * @time: a #GstClockTime
1698 * Set the amount of time to store retransmission packets.
1701 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1704 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1706 g_mutex_lock (&stream->priv->lock);
1707 stream->priv->rtx_time = time;
1708 if (stream->priv->rtxsend)
1709 g_object_set (stream->priv->rtxsend, "max-size-time",
1710 GST_TIME_AS_MSECONDS (time), NULL);
1711 g_mutex_unlock (&stream->priv->lock);
1715 * gst_rtsp_stream_get_retransmission_time:
1716 * @stream: a #GstRTSPStream
1718 * Get the amount of time to store retransmission data.
1720 * Returns: the amount of time to store retransmission data.
1723 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1727 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1729 g_mutex_lock (&stream->priv->lock);
1730 ret = stream->priv->rtx_time;
1731 g_mutex_unlock (&stream->priv->lock);
1737 * gst_rtsp_stream_set_retransmission_pt:
1738 * @stream: a #GstRTSPStream
1741 * Set the payload type (pt) for retransmission of this stream.
1744 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1746 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1748 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1750 g_mutex_lock (&stream->priv->lock);
1751 stream->priv->rtx_pt = rtx_pt;
1752 if (stream->priv->rtxsend) {
1753 guint pt = gst_rtsp_stream_get_pt (stream);
1754 gchar *pt_s = g_strdup_printf ("%d", pt);
1755 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1756 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1757 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1759 gst_structure_free (rtx_pt_map);
1761 g_mutex_unlock (&stream->priv->lock);
1765 * gst_rtsp_stream_get_retransmission_pt:
1766 * @stream: a #GstRTSPStream
1768 * Get the payload-type used for retransmission of this stream
1770 * Returns: The retransmission PT.
1773 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1777 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1779 g_mutex_lock (&stream->priv->lock);
1780 rtx_pt = stream->priv->rtx_pt;
1781 g_mutex_unlock (&stream->priv->lock);
1787 * gst_rtsp_stream_set_buffer_size:
1788 * @stream: a #GstRTSPStream
1789 * @size: the buffer size
1791 * Set the size of the UDP transmission buffer (in bytes)
1792 * Needs to be set before the stream is joined to a bin.
1797 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1799 g_mutex_lock (&stream->priv->lock);
1800 stream->priv->buffer_size = size;
1801 g_mutex_unlock (&stream->priv->lock);
1805 * gst_rtsp_stream_get_buffer_size:
1806 * @stream: a #GstRTSPStream
1808 * Get the size of the UDP transmission buffer (in bytes)
1810 * Returns: the size of the UDP TX buffer
1815 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1819 g_mutex_lock (&stream->priv->lock);
1820 buffer_size = stream->priv->buffer_size;
1821 g_mutex_unlock (&stream->priv->lock);
1826 /* executed from streaming thread */
1828 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1830 GstRTSPStreamPrivate *priv = stream->priv;
1831 GstCaps *newcaps, *oldcaps;
1833 newcaps = gst_pad_get_current_caps (pad);
1835 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1838 g_mutex_lock (&priv->lock);
1839 oldcaps = priv->caps;
1840 priv->caps = newcaps;
1841 g_mutex_unlock (&priv->lock);
1844 gst_caps_unref (oldcaps);
1848 dump_structure (const GstStructure * s)
1852 sstr = gst_structure_to_string (s);
1853 GST_INFO ("structure: %s", sstr);
1857 static GstRTSPStreamTransport *
1858 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1860 GstRTSPStreamPrivate *priv = stream->priv;
1862 GstRTSPStreamTransport *result = NULL;
1867 if (rtcp_from == NULL)
1870 tmp = g_strrstr (rtcp_from, ":");
1874 port = atoi (tmp + 1);
1875 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1877 g_mutex_lock (&priv->lock);
1878 GST_INFO ("finding %s:%d in %d transports", dest, port,
1879 g_list_length (priv->transports));
1881 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1882 GstRTSPStreamTransport *trans = walk->data;
1883 const GstRTSPTransport *tr;
1886 tr = gst_rtsp_stream_transport_get_transport (trans);
1888 if (priv->client_side) {
1889 /* In client side mode the 'destination' is the RTSP server, so send
1891 min = tr->server_port.min;
1892 max = tr->server_port.max;
1894 min = tr->client_port.min;
1895 max = tr->client_port.max;
1898 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1904 g_object_ref (result);
1905 g_mutex_unlock (&priv->lock);
1912 static GstRTSPStreamTransport *
1913 check_transport (GObject * source, GstRTSPStream * stream)
1915 GstStructure *stats;
1916 GstRTSPStreamTransport *trans;
1918 /* see if we have a stream to match with the origin of the RTCP packet */
1919 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1920 if (trans == NULL) {
1921 g_object_get (source, "stats", &stats, NULL);
1923 const gchar *rtcp_from;
1925 dump_structure (stats);
1927 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1928 if ((trans = find_transport (stream, rtcp_from))) {
1929 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1931 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1934 gst_structure_free (stats);
1942 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1944 GstRTSPStreamTransport *trans;
1946 GST_INFO ("%p: new source %p", stream, source);
1948 trans = check_transport (source, stream);
1951 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1955 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1957 GST_INFO ("%p: new SDES %p", stream, source);
1961 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1963 GstRTSPStreamTransport *trans;
1965 trans = check_transport (source, stream);
1968 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1969 gst_rtsp_stream_transport_keep_alive (trans);
1973 GstStructure *stats;
1974 g_object_get (source, "stats", &stats, NULL);
1976 dump_structure (stats);
1977 gst_structure_free (stats);
1984 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1986 GST_INFO ("%p: source %p bye", stream, source);
1990 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1992 GstRTSPStreamTransport *trans;
1994 GST_INFO ("%p: source %p bye timeout", stream, source);
1996 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1997 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1998 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2003 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2005 GstRTSPStreamTransport *trans;
2007 GST_INFO ("%p: source %p timeout", stream, source);
2009 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2010 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2011 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2016 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2018 GST_INFO ("%p: new sender source %p", stream, source);
2021 GstStructure *stats;
2022 g_object_get (source, "stats", &stats, NULL);
2024 dump_structure (stats);
2025 gst_structure_free (stats);
2032 on_sender_ssrc_active (GObject * session, GObject * source,
2033 GstRTSPStream * stream)
2037 GstStructure *stats;
2038 g_object_get (source, "stats", &stats, NULL);
2040 dump_structure (stats);
2041 gst_structure_free (stats);
2048 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
2051 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
2052 g_list_free (priv->tr_cache_rtp);
2053 priv->tr_cache_rtp = NULL;
2055 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
2056 g_list_free (priv->tr_cache_rtcp);
2057 priv->tr_cache_rtcp = NULL;
2061 static GstFlowReturn
2062 handle_new_sample (GstAppSink * sink, gpointer user_data)
2064 GstRTSPStreamPrivate *priv;
2068 GstRTSPStream *stream;
2071 sample = gst_app_sink_pull_sample (sink);
2075 stream = (GstRTSPStream *) user_data;
2076 priv = stream->priv;
2077 buffer = gst_sample_get_buffer (sample);
2079 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
2081 g_mutex_lock (&priv->lock);
2083 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
2084 clear_tr_cache (priv, is_rtp);
2085 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2086 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2087 priv->tr_cache_rtp =
2088 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
2090 priv->tr_cache_cookie_rtp = priv->transports_cookie;
2093 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
2094 clear_tr_cache (priv, is_rtp);
2095 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2096 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2097 priv->tr_cache_rtcp =
2098 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
2100 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
2103 g_mutex_unlock (&priv->lock);
2106 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2107 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2108 gst_rtsp_stream_transport_send_rtp (tr, buffer);
2111 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2112 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2113 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
2116 gst_sample_unref (sample);
2121 static GstAppSinkCallbacks sink_cb = {
2122 NULL, /* not interested in EOS */
2123 NULL, /* not interested in preroll samples */
2128 get_rtp_encoder (GstRTSPStream * stream, guint session)
2130 GstRTSPStreamPrivate *priv = stream->priv;
2132 if (priv->srtpenc == NULL) {
2135 name = g_strdup_printf ("srtpenc_%u", session);
2136 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2139 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2141 return gst_object_ref (priv->srtpenc);
2145 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2147 GstRTSPStreamPrivate *priv = stream->priv;
2148 GstElement *oldenc, *enc;
2152 if (priv->idx != session)
2155 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2157 oldenc = priv->srtpenc;
2158 enc = get_rtp_encoder (stream, session);
2159 name = g_strdup_printf ("rtp_sink_%d", session);
2160 pad = gst_element_get_request_pad (enc, name);
2162 gst_object_unref (pad);
2165 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2172 request_rtcp_encoder (GstElement * rtpbin, guint session,
2173 GstRTSPStream * stream)
2175 GstRTSPStreamPrivate *priv = stream->priv;
2176 GstElement *oldenc, *enc;
2180 if (priv->idx != session)
2183 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2185 oldenc = priv->srtpenc;
2186 enc = get_rtp_encoder (stream, session);
2187 name = g_strdup_printf ("rtcp_sink_%d", session);
2188 pad = gst_element_get_request_pad (enc, name);
2190 gst_object_unref (pad);
2193 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2200 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2202 GstRTSPStreamPrivate *priv = stream->priv;
2205 GST_DEBUG ("request key %08x", ssrc);
2207 g_mutex_lock (&priv->lock);
2208 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2209 gst_caps_ref (caps);
2210 g_mutex_unlock (&priv->lock);
2216 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2217 GstRTSPStream * stream)
2219 GstRTSPStreamPrivate *priv = stream->priv;
2221 if (priv->idx != session)
2224 if (priv->srtpdec == NULL) {
2227 name = g_strdup_printf ("srtpdec_%u", session);
2228 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2231 g_signal_connect (priv->srtpdec, "request-key",
2232 (GCallback) request_key, stream);
2234 return gst_object_ref (priv->srtpdec);
2238 * gst_rtsp_stream_request_aux_sender:
2239 * @stream: a #GstRTSPStream
2240 * @sessid: the session id
2242 * Creating a rtxsend bin
2244 * Returns: (transfer full): a #GstElement.
2249 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2253 GstStructure *pt_map;
2258 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2260 pt = gst_rtsp_stream_get_pt (stream);
2261 pt_s = g_strdup_printf ("%u", pt);
2262 rtx_pt = stream->priv->rtx_pt;
2264 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2266 bin = gst_bin_new (NULL);
2267 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2268 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2269 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2270 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2271 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2273 gst_structure_free (pt_map);
2274 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2276 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2277 name = g_strdup_printf ("src_%u", sessid);
2278 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2280 gst_object_unref (pad);
2282 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2283 name = g_strdup_printf ("sink_%u", sessid);
2284 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2286 gst_object_unref (pad);
2292 * gst_rtsp_stream_set_pt_map:
2293 * @stream: a #GstRTSPStream
2297 * Configure a pt map between @pt and @caps.
2300 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2302 GstRTSPStreamPrivate *priv = stream->priv;
2304 g_mutex_lock (&priv->lock);
2305 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2306 g_mutex_unlock (&priv->lock);
2310 * gst_rtsp_stream_set_publish_clock_mode:
2311 * @stream: a #GstRTSPStream
2312 * @mode: the clock publish mode
2314 * Sets if and how the stream clock should be published according to RFC7273.
2319 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
2320 GstRTSPPublishClockMode mode)
2322 GstRTSPStreamPrivate *priv;
2324 priv = stream->priv;
2325 g_mutex_lock (&priv->lock);
2326 priv->publish_clock_mode = mode;
2327 g_mutex_unlock (&priv->lock);
2331 * gst_rtsp_stream_get_publish_clock_mode:
2332 * @factory: a #GstRTSPStream
2334 * Gets if and how the stream clock should be published according to RFC7273.
2336 * Returns: The GstRTSPPublishClockMode
2340 GstRTSPPublishClockMode
2341 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
2343 GstRTSPStreamPrivate *priv;
2344 GstRTSPPublishClockMode ret;
2346 priv = stream->priv;
2347 g_mutex_lock (&priv->lock);
2348 ret = priv->publish_clock_mode;
2349 g_mutex_unlock (&priv->lock);
2355 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2356 GstRTSPStream * stream)
2358 GstRTSPStreamPrivate *priv = stream->priv;
2359 GstCaps *caps = NULL;
2361 g_mutex_lock (&priv->lock);
2363 if (priv->idx == session) {
2364 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2366 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2367 gst_caps_ref (caps);
2369 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2373 g_mutex_unlock (&priv->lock);
2379 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2381 GstRTSPStreamPrivate *priv = stream->priv;
2383 GstPadLinkReturn ret;
2386 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2387 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2389 name = gst_pad_get_name (pad);
2390 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2396 if (priv->idx != sessid)
2399 if (gst_pad_is_linked (priv->sinkpad)) {
2400 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2401 GST_DEBUG_PAD_NAME (priv->sinkpad));
2405 /* link the RTP pad to the session manager, it should not really fail unless
2406 * this is not really an RTP pad */
2407 ret = gst_pad_link (pad, priv->sinkpad);
2408 if (ret != GST_PAD_LINK_OK)
2410 priv->recv_rtp_src = gst_object_ref (pad);
2417 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2418 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2423 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2424 GstRTSPStream * stream)
2426 /* TODO: What to do here other than this? */
2427 GST_DEBUG ("Stream %p: Got EOS", stream);
2428 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2431 /* must be called with lock */
2433 create_sender_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2435 GstRTSPStreamPrivate *priv;
2436 GstPad *pad, *sinkpad = NULL;
2437 gboolean is_tcp = FALSE, is_udp = FALSE;
2440 priv = stream->priv;
2442 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2443 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2444 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2446 if (is_udp && !create_and_configure_udpsinks (stream))
2447 goto no_udp_protocol;
2449 for (i = 0; i < 2; i++) {
2450 GstPad *teepad, *queuepad;
2451 /* For the sender we create this bit of pipeline for both
2452 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2453 * we need to add a queue before appsink and udpsink to make
2454 * the pipeline not block. For the TCP case, we want to pump
2455 * client as fast as possible anyway. This pipeline is used
2456 * when both TCP and UDP are present.
2458 * .--------. .-----. .---------. .---------.
2459 * | rtpbin | | tee | | queue | | udpsink |
2460 * | send->sink src->sink src->sink |
2461 * '--------' | | '---------' '---------'
2462 * | | .---------. .---------.
2463 * | | | queue | | appsink |
2464 * | src->sink src->sink |
2465 * '-----' '---------' '---------'
2467 * When only UDP or only TCP is allowed, we skip the tee and queue
2468 * and link the udpsink (for UDP) or appsink (for TCP) directly to
2472 /* Only link the RTP send src if we're going to send RTP, link
2473 * the RTCP send src always */
2474 if (!priv->srcpad && i == 0)
2479 gst_bin_add (bin, priv->udpsink[i]);
2480 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
2485 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2486 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2487 gst_bin_add (bin, priv->appsink[i]);
2488 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2489 &sink_cb, stream, NULL);
2492 if (is_udp && is_tcp) {
2493 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2495 /* make tee for RTP/RTCP */
2496 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2497 gst_bin_add (bin, priv->tee[i]);
2499 /* and link to rtpbin send pad */
2500 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2501 gst_pad_link (priv->send_src[i], pad);
2502 gst_object_unref (pad);
2504 priv->udpqueue[i] = gst_element_factory_make ("queue", NULL);
2505 g_object_set (priv->udpqueue[i], "max-size-buffers",
2506 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0), NULL);
2507 gst_bin_add (bin, priv->udpqueue[i]);
2508 /* link tee to udpqueue */
2509 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2510 pad = gst_element_get_static_pad (priv->udpqueue[i], "sink");
2511 gst_pad_link (teepad, pad);
2512 gst_object_unref (pad);
2513 gst_object_unref (teepad);
2515 /* link udpqueue to udpsink */
2516 queuepad = gst_element_get_static_pad (priv->udpqueue[i], "src");
2517 gst_pad_link (queuepad, sinkpad);
2518 gst_object_unref (queuepad);
2519 gst_object_unref (sinkpad);
2522 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
2523 g_object_set (priv->appqueue[i], "max-size-buffers",
2524 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0), NULL);
2525 gst_bin_add (bin, priv->appqueue[i]);
2526 /* and link tee to appqueue */
2527 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2528 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
2529 gst_pad_link (teepad, pad);
2530 gst_object_unref (pad);
2531 gst_object_unref (teepad);
2533 /* and link appqueue to appsink */
2534 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
2535 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2536 gst_pad_link (queuepad, pad);
2537 gst_object_unref (pad);
2538 gst_object_unref (queuepad);
2539 } else if (is_tcp) {
2540 /* only appsink needed, link it to the session */
2541 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2542 gst_pad_link (priv->send_src[i], pad);
2543 gst_object_unref (pad);
2545 /* when its only TCP, we need to set sync and preroll to FALSE
2546 * for the sink to avoid deadlock. And this is only needed for
2547 * sink used for RTCP data, not the RTP data. */
2549 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2551 /* else only udpsink needed, link it to the session */
2552 gst_pad_link (priv->send_src[i], sinkpad);
2553 gst_object_unref (sinkpad);
2556 /* check if we need to set to a special state */
2557 if (state != GST_STATE_NULL) {
2558 if (priv->udpsink[i])
2559 gst_element_set_state (priv->udpsink[i], state);
2560 if (priv->appsink[i])
2561 gst_element_set_state (priv->appsink[i], state);
2562 if (priv->appqueue[i])
2563 gst_element_set_state (priv->appqueue[i], state);
2564 if (priv->udpqueue[i])
2565 gst_element_set_state (priv->udpqueue[i], state);
2567 gst_element_set_state (priv->tee[i], state);
2580 /* must be called with lock */
2582 create_receiver_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2584 GstRTSPStreamPrivate *priv;
2585 GstPad *pad, *selpad;
2589 priv = stream->priv;
2591 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2593 for (i = 0; i < 2; i++) {
2594 /* For the receiver we create this bit of pipeline for both
2595 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2596 * and it is all funneled into the rtpbin receive pad.
2598 * .--------. .--------. .--------.
2599 * | udpsrc | | funnel | | rtpbin |
2600 * | src->sink src->sink |
2601 * '--------' | | '--------'
2605 * '--------' '--------'
2608 if (!priv->sinkpad && i == 0) {
2609 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2610 * RTCP sink always */
2614 /* make funnel for the RTP/RTCP receivers */
2615 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2616 gst_bin_add (bin, priv->funnel[i]);
2618 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2619 gst_pad_link (pad, priv->recv_sink[i]);
2620 gst_object_unref (pad);
2622 if (priv->udpsrc_v4[i]) {
2624 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2625 * values. This is only relevant for PLAY pipelines */
2626 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
2627 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
2630 gst_bin_add (bin, priv->udpsrc_v4[i]);
2632 /* and link to the funnel v4 */
2633 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2634 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
2635 gst_pad_link (pad, selpad);
2636 gst_object_unref (pad);
2637 gst_object_unref (selpad);
2640 if (priv->udpsrc_v6[i]) {
2642 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
2643 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
2645 gst_bin_add (bin, priv->udpsrc_v6[i]);
2647 /* and link to the funnel v6 */
2648 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2649 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2650 gst_pad_link (pad, selpad);
2651 gst_object_unref (pad);
2652 gst_object_unref (selpad);
2656 /* make and add appsrc */
2657 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2658 priv->appsrc_base_time[i] = -1;
2660 gst_element_set_state (priv->appsrc[i], GST_STATE_PLAYING);
2661 gst_element_set_locked_state (priv->appsrc[i], TRUE);
2663 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
2665 gst_bin_add (bin, priv->appsrc[i]);
2666 /* and link to the funnel */
2667 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2668 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2669 gst_pad_link (pad, selpad);
2670 gst_object_unref (pad);
2671 gst_object_unref (selpad);
2674 /* check if we need to set to a special state */
2675 if (state != GST_STATE_NULL) {
2676 gst_element_set_state (priv->funnel[i], state);
2682 * gst_rtsp_stream_join_bin:
2683 * @stream: a #GstRTSPStream
2684 * @bin: (transfer none): a #GstBin to join
2685 * @rtpbin: (transfer none): a rtpbin element in @bin
2686 * @state: the target state of the new elements
2688 * Join the #GstBin @bin that contains the element @rtpbin.
2690 * @stream will link to @rtpbin, which must be inside @bin. The elements
2691 * added to @bin will be set to the state given in @state.
2693 * Returns: %TRUE on success.
2696 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2697 GstElement * rtpbin, GstState state)
2699 GstRTSPStreamPrivate *priv;
2702 GstPadLinkReturn ret;
2704 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2705 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2706 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2708 priv = stream->priv;
2710 g_mutex_lock (&priv->lock);
2711 if (priv->joined_bin != NULL)
2714 /* create a session with the same index as the stream */
2717 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2719 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2720 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2722 g_signal_connect (rtpbin, "request-rtp-encoder",
2723 (GCallback) request_rtp_encoder, stream);
2724 g_signal_connect (rtpbin, "request-rtcp-encoder",
2725 (GCallback) request_rtcp_encoder, stream);
2726 g_signal_connect (rtpbin, "request-rtp-decoder",
2727 (GCallback) request_rtp_rtcp_decoder, stream);
2728 g_signal_connect (rtpbin, "request-rtcp-decoder",
2729 (GCallback) request_rtp_rtcp_decoder, stream);
2732 if (priv->sinkpad) {
2733 g_signal_connect (rtpbin, "request-pt-map",
2734 (GCallback) request_pt_map, stream);
2737 /* get pads from the RTP session element for sending and receiving
2740 /* get a pad for sending RTP */
2741 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2742 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2745 /* link the RTP pad to the session manager, it should not really fail unless
2746 * this is not really an RTP pad */
2747 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2748 if (ret != GST_PAD_LINK_OK)
2751 name = g_strdup_printf ("send_rtp_src_%u", idx);
2752 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2755 /* Need to connect our sinkpad from here */
2756 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2758 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2760 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2761 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2765 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2766 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2768 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2769 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2772 /* get the session */
2773 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2775 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2777 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2779 g_signal_connect (priv->session, "on-ssrc-active",
2780 (GCallback) on_ssrc_active, stream);
2781 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2783 g_signal_connect (priv->session, "on-bye-timeout",
2784 (GCallback) on_bye_timeout, stream);
2785 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2788 /* signal for sender ssrc */
2789 g_signal_connect (priv->session, "on-new-sender-ssrc",
2790 (GCallback) on_new_sender_ssrc, stream);
2791 g_signal_connect (priv->session, "on-sender-ssrc-active",
2792 (GCallback) on_sender_ssrc_active, stream);
2794 if (!create_sender_part (stream, bin, state))
2795 goto no_udp_protocol;
2797 create_receiver_part (stream, bin, state);
2800 /* be notified of caps changes */
2801 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2802 (GCallback) caps_notify, stream);
2805 priv->joined_bin = gst_object_ref (bin);
2806 g_mutex_unlock (&priv->lock);
2813 g_mutex_unlock (&priv->lock);
2818 GST_WARNING ("failed to link stream %u", idx);
2819 gst_object_unref (priv->send_rtp_sink);
2820 priv->send_rtp_sink = NULL;
2821 g_mutex_unlock (&priv->lock);
2826 GST_WARNING ("failed to allocate ports %u", idx);
2827 gst_object_unref (priv->send_rtp_sink);
2828 priv->send_rtp_sink = NULL;
2829 gst_object_unref (priv->send_src[0]);
2830 priv->send_src[0] = NULL;
2831 gst_object_unref (priv->send_src[1]);
2832 priv->send_src[1] = NULL;
2833 gst_object_unref (priv->recv_sink[0]);
2834 priv->recv_sink[0] = NULL;
2835 gst_object_unref (priv->recv_sink[1]);
2836 priv->recv_sink[1] = NULL;
2837 if (priv->udpsink[0])
2838 gst_element_set_state (priv->udpsink[0], GST_STATE_NULL);
2839 if (priv->udpsink[1])
2840 gst_element_set_state (priv->udpsink[1], GST_STATE_NULL);
2841 if (priv->udpsrc_v4[0]) {
2842 gst_element_set_state (priv->udpsrc_v4[0], GST_STATE_NULL);
2843 gst_object_unref (priv->udpsrc_v4[0]);
2844 priv->udpsrc_v4[0] = NULL;
2846 if (priv->udpsrc_v4[1]) {
2847 gst_element_set_state (priv->udpsrc_v4[1], GST_STATE_NULL);
2848 gst_object_unref (priv->udpsrc_v4[1]);
2849 priv->udpsrc_v4[1] = NULL;
2851 if (priv->udpsrc_mcast_v4[0]) {
2852 gst_element_set_state (priv->udpsrc_mcast_v4[0], GST_STATE_NULL);
2853 gst_object_unref (priv->udpsrc_mcast_v4[0]);
2854 priv->udpsrc_mcast_v4[0] = NULL;
2856 if (priv->udpsrc_mcast_v4[1]) {
2857 gst_element_set_state (priv->udpsrc_mcast_v4[1], GST_STATE_NULL);
2858 gst_object_unref (priv->udpsrc_mcast_v4[1]);
2859 priv->udpsrc_mcast_v4[1] = NULL;
2861 if (priv->udpsrc_v6[0]) {
2862 gst_element_set_state (priv->udpsrc_v6[0], GST_STATE_NULL);
2863 gst_object_unref (priv->udpsrc_v6[0]);
2864 priv->udpsrc_v6[0] = NULL;
2866 if (priv->udpsrc_v6[1]) {
2867 gst_element_set_state (priv->udpsrc_v6[1], GST_STATE_NULL);
2868 gst_object_unref (priv->udpsrc_v6[1]);
2869 priv->udpsrc_v6[1] = NULL;
2871 if (priv->udpsrc_mcast_v6[0]) {
2872 gst_element_set_state (priv->udpsrc_mcast_v6[0], GST_STATE_NULL);
2873 gst_object_unref (priv->udpsrc_mcast_v6[0]);
2874 priv->udpsrc_mcast_v6[0] = NULL;
2876 if (priv->udpsrc_mcast_v6[1]) {
2877 gst_element_set_state (priv->udpsrc_mcast_v6[1], GST_STATE_NULL);
2878 gst_object_unref (priv->udpsrc_mcast_v6[1]);
2879 priv->udpsrc_mcast_v6[1] = NULL;
2881 g_mutex_unlock (&priv->lock);
2887 clear_element (GstBin * bin, GstElement ** elementptr)
2890 gst_element_set_locked_state (*elementptr, FALSE);
2891 gst_element_set_state (*elementptr, GST_STATE_NULL);
2892 if (GST_ELEMENT_PARENT (*elementptr))
2893 gst_bin_remove (bin, *elementptr);
2895 gst_object_unref (*elementptr);
2901 * gst_rtsp_stream_leave_bin:
2902 * @stream: a #GstRTSPStream
2903 * @bin: (transfer none): a #GstBin
2904 * @rtpbin: (transfer none): a rtpbin #GstElement
2906 * Remove the elements of @stream from @bin.
2908 * Return: %TRUE on success.
2911 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2912 GstElement * rtpbin)
2914 GstRTSPStreamPrivate *priv;
2917 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2918 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2919 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2921 priv = stream->priv;
2923 g_mutex_lock (&priv->lock);
2924 if (priv->joined_bin == NULL)
2925 goto was_not_joined;
2926 if (priv->joined_bin != bin)
2929 priv->joined_bin = NULL;
2931 /* all transports must be removed by now */
2932 if (priv->transports != NULL)
2933 goto transports_not_removed;
2935 clear_tr_cache (priv, TRUE);
2936 clear_tr_cache (priv, FALSE);
2938 GST_INFO ("stream %p leaving bin", stream);
2941 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2943 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2944 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2945 gst_object_unref (priv->send_rtp_sink);
2946 priv->send_rtp_sink = NULL;
2947 } else if (priv->recv_rtp_src) {
2948 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2949 gst_object_unref (priv->recv_rtp_src);
2950 priv->recv_rtp_src = NULL;
2953 for (i = 0; i < 2; i++) {
2954 clear_element (bin, &priv->udpsink[i]);
2955 clear_element (bin, &priv->appsink[i]);
2956 clear_element (bin, &priv->appqueue[i]);
2957 clear_element (bin, &priv->udpqueue[i]);
2958 clear_element (bin, &priv->tee[i]);
2959 clear_element (bin, &priv->funnel[i]);
2960 clear_element (bin, &priv->appsrc[i]);
2961 clear_element (bin, &priv->udpsrc_v4[i]);
2962 clear_element (bin, &priv->udpsrc_v6[i]);
2963 clear_element (bin, &priv->udpsrc_mcast_v4[i]);
2964 clear_element (bin, &priv->udpsrc_mcast_v6[i]);
2966 if (priv->sinkpad || i == 1) {
2967 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2968 gst_object_unref (priv->recv_sink[i]);
2969 priv->recv_sink[i] = NULL;
2974 gst_object_unref (priv->send_src[0]);
2975 priv->send_src[0] = NULL;
2978 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2979 gst_object_unref (priv->send_src[1]);
2980 priv->send_src[1] = NULL;
2982 g_object_unref (priv->session);
2983 priv->session = NULL;
2985 gst_caps_unref (priv->caps);
2989 gst_object_unref (priv->srtpenc);
2991 gst_object_unref (priv->srtpdec);
2993 g_clear_object (&priv->joined_bin);
2994 g_mutex_unlock (&priv->lock);
3000 g_mutex_unlock (&priv->lock);
3003 transports_not_removed:
3005 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
3006 g_mutex_unlock (&priv->lock);
3011 GST_ERROR_OBJECT (stream, "leaving the wrong bin");
3012 g_mutex_unlock (&priv->lock);
3018 * gst_rtsp_stream_get_joined_bin:
3019 * @stream: a #GstRTSPStream
3021 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
3023 * Return: (transfer full): the joined bin or NULL.
3026 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
3028 GstRTSPStreamPrivate *priv;
3031 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3033 priv = stream->priv;
3035 g_mutex_lock (&priv->lock);
3036 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
3037 g_mutex_unlock (&priv->lock);
3043 * gst_rtsp_stream_get_rtpinfo:
3044 * @stream: a #GstRTSPStream
3045 * @rtptime: (allow-none): result RTP timestamp
3046 * @seq: (allow-none): result RTP seqnum
3047 * @clock_rate: (allow-none): the clock rate
3048 * @running_time: (allow-none): result running-time
3050 * Retrieve the current rtptime, seq and running-time. This is used to
3051 * construct a RTPInfo reply header.
3053 * Returns: %TRUE when rtptime, seq and running-time could be determined.
3056 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
3057 guint * rtptime, guint * seq, guint * clock_rate,
3058 GstClockTime * running_time)
3060 GstRTSPStreamPrivate *priv;
3061 GstStructure *stats;
3062 GObjectClass *payobjclass;
3064 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3066 priv = stream->priv;
3068 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
3070 g_mutex_lock (&priv->lock);
3072 /* First try to extract the information from the last buffer on the sinks.
3073 * This will have a more accurate sequence number and timestamp, as between
3074 * the payloader and the sink there can be some queues
3076 if (priv->udpsink[0] || priv->appsink[0]) {
3077 GstSample *last_sample;
3079 if (priv->udpsink[0])
3080 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
3082 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
3087 GstSegment *segment;
3088 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
3090 caps = gst_sample_get_caps (last_sample);
3091 buffer = gst_sample_get_buffer (last_sample);
3092 segment = gst_sample_get_segment (last_sample);
3094 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3096 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3100 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3103 gst_rtp_buffer_unmap (&rtp_buffer);
3107 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3108 GST_BUFFER_TIMESTAMP (buffer));
3112 GstStructure *s = gst_caps_get_structure (caps, 0);
3114 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3116 if (*clock_rate == 0 && running_time)
3117 *running_time = GST_CLOCK_TIME_NONE;
3119 gst_sample_unref (last_sample);
3123 gst_sample_unref (last_sample);
3128 if (g_object_class_find_property (payobjclass, "stats")) {
3129 g_object_get (priv->payloader, "stats", &stats, NULL);
3134 gst_structure_get_uint (stats, "seqnum", seq);
3137 gst_structure_get_uint (stats, "timestamp", rtptime);
3140 gst_structure_get_clock_time (stats, "running-time", running_time);
3143 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3144 if (*clock_rate == 0 && running_time)
3145 *running_time = GST_CLOCK_TIME_NONE;
3147 gst_structure_free (stats);
3149 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3150 !g_object_class_find_property (payobjclass, "timestamp"))
3154 g_object_get (priv->payloader, "seqnum", seq, NULL);
3157 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3160 *running_time = GST_CLOCK_TIME_NONE;
3164 g_mutex_unlock (&priv->lock);
3171 GST_WARNING ("Could not get payloader stats");
3172 g_mutex_unlock (&priv->lock);
3178 * gst_rtsp_stream_get_caps:
3179 * @stream: a #GstRTSPStream
3181 * Retrieve the current caps of @stream.
3183 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
3187 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3189 GstRTSPStreamPrivate *priv;
3192 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3194 priv = stream->priv;
3196 g_mutex_lock (&priv->lock);
3197 if ((result = priv->caps))
3198 gst_caps_ref (result);
3199 g_mutex_unlock (&priv->lock);
3205 * gst_rtsp_stream_recv_rtp:
3206 * @stream: a #GstRTSPStream
3207 * @buffer: (transfer full): a #GstBuffer
3209 * Handle an RTP buffer for the stream. This method is usually called when a
3210 * message has been received from a client using the TCP transport.
3212 * This function takes ownership of @buffer.
3214 * Returns: a GstFlowReturn.
3217 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3219 GstRTSPStreamPrivate *priv;
3221 GstElement *element;
3223 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3224 priv = stream->priv;
3225 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3226 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3228 g_mutex_lock (&priv->lock);
3229 if (priv->appsrc[0])
3230 element = gst_object_ref (priv->appsrc[0]);
3233 g_mutex_unlock (&priv->lock);
3236 if (priv->appsrc_base_time[0] == -1) {
3237 /* Take current running_time. This timestamp will be put on
3238 * the first buffer of each stream because we are a live source and so we
3239 * timestamp with the running_time. When we are dealing with TCP, we also
3240 * only timestamp the first buffer (using the DISCONT flag) because a server
3241 * typically bursts data, for which we don't want to compensate by speeding
3242 * up the media. The other timestamps will be interpollated from this one
3243 * using the RTP timestamps. */
3244 GST_OBJECT_LOCK (element);
3245 if (GST_ELEMENT_CLOCK (element)) {
3247 GstClockTime base_time;
3249 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3250 base_time = GST_ELEMENT_CAST (element)->base_time;
3252 priv->appsrc_base_time[0] = now - base_time;
3253 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3254 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3255 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3256 GST_TIME_ARGS (base_time));
3258 GST_OBJECT_UNLOCK (element);
3261 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3262 gst_object_unref (element);
3270 * gst_rtsp_stream_recv_rtcp:
3271 * @stream: a #GstRTSPStream
3272 * @buffer: (transfer full): a #GstBuffer
3274 * Handle an RTCP buffer for the stream. This method is usually called when a
3275 * message has been received from a client using the TCP transport.
3277 * This function takes ownership of @buffer.
3279 * Returns: a GstFlowReturn.
3282 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3284 GstRTSPStreamPrivate *priv;
3286 GstElement *element;
3288 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3289 priv = stream->priv;
3290 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3292 if (priv->joined_bin == NULL) {
3293 gst_buffer_unref (buffer);
3294 return GST_FLOW_NOT_LINKED;
3296 g_mutex_lock (&priv->lock);
3297 if (priv->appsrc[1])
3298 element = gst_object_ref (priv->appsrc[1]);
3301 g_mutex_unlock (&priv->lock);
3304 if (priv->appsrc_base_time[1] == -1) {
3305 /* Take current running_time. This timestamp will be put on
3306 * the first buffer of each stream because we are a live source and so we
3307 * timestamp with the running_time. When we are dealing with TCP, we also
3308 * only timestamp the first buffer (using the DISCONT flag) because a server
3309 * typically bursts data, for which we don't want to compensate by speeding
3310 * up the media. The other timestamps will be interpollated from this one
3311 * using the RTP timestamps. */
3312 GST_OBJECT_LOCK (element);
3313 if (GST_ELEMENT_CLOCK (element)) {
3315 GstClockTime base_time;
3317 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3318 base_time = GST_ELEMENT_CAST (element)->base_time;
3320 priv->appsrc_base_time[1] = now - base_time;
3321 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3322 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3323 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3324 GST_TIME_ARGS (base_time));
3326 GST_OBJECT_UNLOCK (element);
3329 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3330 gst_object_unref (element);
3333 gst_buffer_unref (buffer);
3338 /* must be called with lock */
3340 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3343 GstRTSPStreamPrivate *priv = stream->priv;
3344 const GstRTSPTransport *tr;
3346 tr = gst_rtsp_stream_transport_get_transport (trans);
3348 switch (tr->lower_transport) {
3349 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3350 case GST_RTSP_LOWER_TRANS_UDP:
3356 dest = tr->destination;
3357 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3361 } else if (priv->client_side) {
3362 /* In client side mode the 'destination' is the RTSP server, so send
3364 min = tr->server_port.min;
3365 max = tr->server_port.max;
3367 min = tr->client_port.min;
3368 max = tr->client_port.max;
3373 GST_INFO ("setting ttl-mc %d", ttl);
3374 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3375 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3377 GST_INFO ("adding %s:%d-%d", dest, min, max);
3378 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3379 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3380 priv->transports = g_list_prepend (priv->transports, trans);
3382 GST_INFO ("removing %s:%d-%d", dest, min, max);
3383 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3384 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3385 priv->transports = g_list_remove (priv->transports, trans);
3387 priv->transports_cookie++;
3390 case GST_RTSP_LOWER_TRANS_TCP:
3392 GST_INFO ("adding TCP %s", tr->destination);
3393 priv->transports = g_list_prepend (priv->transports, trans);
3395 GST_INFO ("removing TCP %s", tr->destination);
3396 priv->transports = g_list_remove (priv->transports, trans);
3398 priv->transports_cookie++;
3401 goto unknown_transport;
3408 GST_INFO ("Unknown transport %d", tr->lower_transport);
3415 * gst_rtsp_stream_add_transport:
3416 * @stream: a #GstRTSPStream
3417 * @trans: (transfer none): a #GstRTSPStreamTransport
3419 * Add the transport in @trans to @stream. The media of @stream will
3420 * then also be send to the values configured in @trans.
3422 * @stream must be joined to a bin.
3424 * @trans must contain a valid #GstRTSPTransport.
3426 * Returns: %TRUE if @trans was added
3429 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3430 GstRTSPStreamTransport * trans)
3432 GstRTSPStreamPrivate *priv;
3435 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3436 priv = stream->priv;
3437 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3438 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3440 g_mutex_lock (&priv->lock);
3441 res = update_transport (stream, trans, TRUE);
3442 g_mutex_unlock (&priv->lock);
3448 * gst_rtsp_stream_remove_transport:
3449 * @stream: a #GstRTSPStream
3450 * @trans: (transfer none): a #GstRTSPStreamTransport
3452 * Remove the transport in @trans from @stream. The media of @stream will
3453 * not be sent to the values configured in @trans.
3455 * @stream must be joined to a bin.
3457 * @trans must contain a valid #GstRTSPTransport.
3459 * Returns: %TRUE if @trans was removed
3462 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3463 GstRTSPStreamTransport * trans)
3465 GstRTSPStreamPrivate *priv;
3468 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3469 priv = stream->priv;
3470 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3471 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3473 g_mutex_lock (&priv->lock);
3474 res = update_transport (stream, trans, FALSE);
3475 g_mutex_unlock (&priv->lock);
3481 * gst_rtsp_stream_update_crypto:
3482 * @stream: a #GstRTSPStream
3484 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3486 * Update the new crypto information for @ssrc in @stream. If information
3487 * for @ssrc did not exist, it will be added. If information
3488 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3489 * be removed from @stream.
3491 * Returns: %TRUE if @crypto could be updated
3494 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3495 guint ssrc, GstCaps * crypto)
3497 GstRTSPStreamPrivate *priv;
3499 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3500 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3502 priv = stream->priv;
3504 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3506 g_mutex_lock (&priv->lock);
3508 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3509 gst_caps_ref (crypto));
3511 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3512 g_mutex_unlock (&priv->lock);
3518 * gst_rtsp_stream_get_rtp_socket:
3519 * @stream: a #GstRTSPStream
3520 * @family: the socket family
3522 * Get the RTP socket from @stream for a @family.
3524 * @stream must be joined to a bin.
3526 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3527 * socket could be allocated for @family. Unref after usage
3530 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3532 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3536 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3537 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3538 family == G_SOCKET_FAMILY_IPV6, NULL);
3539 g_return_val_if_fail (priv->udpsink[0], NULL);
3541 if (family == G_SOCKET_FAMILY_IPV6)
3546 g_object_get (priv->udpsink[0], name, &socket, NULL);
3552 * gst_rtsp_stream_get_rtcp_socket:
3553 * @stream: a #GstRTSPStream
3554 * @family: the socket family
3556 * Get the RTCP socket from @stream for a @family.
3558 * @stream must be joined to a bin.
3560 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3561 * socket could be allocated for @family. Unref after usage
3564 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3566 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3570 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3571 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3572 family == G_SOCKET_FAMILY_IPV6, NULL);
3573 g_return_val_if_fail (priv->udpsink[1], NULL);
3575 if (family == G_SOCKET_FAMILY_IPV6)
3580 g_object_get (priv->udpsink[1], name, &socket, NULL);
3586 * gst_rtsp_stream_set_seqnum:
3587 * @stream: a #GstRTSPStream
3588 * @seqnum: a new sequence number
3590 * Configure the sequence number in the payloader of @stream to @seqnum.
3593 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3595 GstRTSPStreamPrivate *priv;
3597 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3599 priv = stream->priv;
3601 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3605 * gst_rtsp_stream_get_seqnum:
3606 * @stream: a #GstRTSPStream
3608 * Get the configured sequence number in the payloader of @stream.
3610 * Returns: the sequence number of the payloader.
3613 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3615 GstRTSPStreamPrivate *priv;
3618 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3620 priv = stream->priv;
3622 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3628 * gst_rtsp_stream_transport_filter:
3629 * @stream: a #GstRTSPStream
3630 * @func: (scope call) (allow-none): a callback
3631 * @user_data: (closure): user data passed to @func
3633 * Call @func for each transport managed by @stream. The result value of @func
3634 * determines what happens to the transport. @func will be called with @stream
3635 * locked so no further actions on @stream can be performed from @func.
3637 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3640 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3642 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3643 * will also be added with an additional ref to the result #GList of this
3646 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3648 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3649 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3650 * element in the #GList should be unreffed before the list is freed.
3653 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3654 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3656 GstRTSPStreamPrivate *priv;
3657 GList *result, *walk, *next;
3658 GHashTable *visited = NULL;
3661 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3663 priv = stream->priv;
3667 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3669 g_mutex_lock (&priv->lock);
3671 cookie = priv->transports_cookie;
3672 for (walk = priv->transports; walk; walk = next) {
3673 GstRTSPStreamTransport *trans = walk->data;
3674 GstRTSPFilterResult res;
3677 next = g_list_next (walk);
3680 /* only visit each transport once */
3681 if (g_hash_table_contains (visited, trans))
3684 g_hash_table_add (visited, g_object_ref (trans));
3685 g_mutex_unlock (&priv->lock);
3687 res = func (stream, trans, user_data);
3689 g_mutex_lock (&priv->lock);
3691 res = GST_RTSP_FILTER_REF;
3693 changed = (cookie != priv->transports_cookie);
3696 case GST_RTSP_FILTER_REMOVE:
3697 update_transport (stream, trans, FALSE);
3699 case GST_RTSP_FILTER_REF:
3700 result = g_list_prepend (result, g_object_ref (trans));
3702 case GST_RTSP_FILTER_KEEP:
3709 g_mutex_unlock (&priv->lock);
3712 g_hash_table_unref (visited);
3717 static GstPadProbeReturn
3718 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3720 GstRTSPStreamPrivate *priv;
3721 GstRTSPStream *stream;
3724 priv = stream->priv;
3726 GST_DEBUG_OBJECT (pad, "now blocking");
3728 g_mutex_lock (&priv->lock);
3729 priv->blocking = TRUE;
3730 g_mutex_unlock (&priv->lock);
3732 gst_element_post_message (priv->payloader,
3733 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3734 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3736 return GST_PAD_PROBE_OK;
3740 * gst_rtsp_stream_set_blocked:
3741 * @stream: a #GstRTSPStream
3742 * @blocked: boolean indicating we should block or unblock
3744 * Blocks or unblocks the dataflow on @stream.
3746 * Returns: %TRUE on success
3749 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3751 GstRTSPStreamPrivate *priv;
3753 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3755 priv = stream->priv;
3757 g_mutex_lock (&priv->lock);
3759 priv->blocking = FALSE;
3760 if (priv->blocked_id == 0) {
3761 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3762 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3763 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3764 g_object_ref (stream), g_object_unref);
3767 if (priv->blocked_id != 0) {
3768 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3769 priv->blocked_id = 0;
3770 priv->blocking = FALSE;
3773 g_mutex_unlock (&priv->lock);
3779 * gst_rtsp_stream_is_blocking:
3780 * @stream: a #GstRTSPStream
3782 * Check if @stream is blocking on a #GstBuffer.
3784 * Returns: %TRUE if @stream is blocking
3787 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3789 GstRTSPStreamPrivate *priv;
3792 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3794 priv = stream->priv;
3796 g_mutex_lock (&priv->lock);
3797 result = priv->blocking;
3798 g_mutex_unlock (&priv->lock);
3804 * gst_rtsp_stream_query_position:
3805 * @stream: a #GstRTSPStream
3807 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3808 * the RTP parts of the pipeline and not the RTCP parts.
3810 * Returns: %TRUE if the position could be queried
3813 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3815 GstRTSPStreamPrivate *priv;
3819 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3821 priv = stream->priv;
3823 g_mutex_lock (&priv->lock);
3824 /* depending on the transport type, it should query corresponding sink */
3825 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3826 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3827 sink = priv->udpsink[0];
3829 sink = priv->appsink[0];
3832 gst_object_ref (sink);
3833 g_mutex_unlock (&priv->lock);
3838 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3839 gst_object_unref (sink);
3845 * gst_rtsp_stream_query_stop:
3846 * @stream: a #GstRTSPStream
3848 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3849 * the RTP parts of the pipeline and not the RTCP parts.
3851 * Returns: %TRUE if the stop could be queried
3854 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3856 GstRTSPStreamPrivate *priv;
3861 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3863 priv = stream->priv;
3865 g_mutex_lock (&priv->lock);
3866 /* depending on the transport type, it should query corresponding sink */
3867 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3868 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3869 sink = priv->udpsink[0];
3871 sink = priv->appsink[0];
3874 gst_object_ref (sink);
3875 g_mutex_unlock (&priv->lock);
3880 query = gst_query_new_segment (GST_FORMAT_TIME);
3881 if ((ret = gst_element_query (sink, query))) {
3884 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3885 if (format != GST_FORMAT_TIME)
3888 gst_query_unref (query);
3889 gst_object_unref (sink);