2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3 * Copyright (C) 2015 Centricular Ltd
4 * Author: Sebastian Dröge <sebastian@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * @short_description: A media stream
24 * @see_also: #GstRTSPMedia
26 * The #GstRTSPStream object manages the data transport for one stream. It
27 * is created from a payloader element and a source pad that produce the RTP
28 * packets for the stream.
30 * With gst_rtsp_stream_join_bin() the streaming elements are added to the bin
31 * and rtpbin. gst_rtsp_stream_leave_bin() removes the elements again.
33 * The #GstRTSPStream will use the configured addresspool, as set with
34 * gst_rtsp_stream_set_address_pool(), to allocate multicast addresses for the
35 * stream. With gst_rtsp_stream_get_multicast_address() you can get the
38 * With gst_rtsp_stream_get_server_port () you can get the port that the server
39 * will use to receive RTCP. This is the part that the clients will use to send
42 * With gst_rtsp_stream_add_transport() destinations can be added where the
43 * stream should be sent to. Use gst_rtsp_stream_remove_transport() to remove
44 * the destination again.
46 * Last reviewed on 2013-07-16 (1.0.0)
55 #include <gst/app/gstappsrc.h>
56 #include <gst/app/gstappsink.h>
58 #include <gst/rtp/gstrtpbuffer.h>
60 #include "rtsp-stream.h"
62 #define GST_RTSP_STREAM_GET_PRIVATE(obj) \
63 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
65 struct _GstRTSPStreamPrivate
69 /* Only one pad is ever set */
70 GstPad *srcpad, *sinkpad;
71 GstElement *payloader;
75 /* TRUE if this stream is running on
76 * the client side of an RTSP link (for RECORD) */
80 GstRTSPProfile profiles;
81 GstRTSPLowerTrans protocols;
83 /* pads on the rtpbin */
84 GstPad *send_rtp_sink;
89 /* the RTPSession object */
92 /* SRTP encoder/decoder */
98 GstElement *udpsrc_v4[2];
99 GstElement *udpsrc_v6[2];
100 GstElement *udpqueue[2];
101 GstElement *udpsink[2];
103 /* for UDP multicast */
104 GstElement *mcast_udpsrc_v4[2];
105 GstElement *mcast_udpsrc_v6[2];
106 GstElement *mcast_udpqueue[2];
107 GstElement *mcast_udpsink[2];
109 /* for TCP transport */
110 GstElement *appsrc[2];
111 GstClockTime appsrc_base_time[2];
112 GstElement *appqueue[2];
113 GstElement *appsink[2];
116 GstElement *funnel[2];
121 GstClockTime rtx_time;
123 /* pool used to manage unicast and multicast addresses */
124 GstRTSPAddressPool *pool;
126 /* unicast server addr/port */
127 GstRTSPRange server_port_v4;
128 GstRTSPRange server_port_v6;
129 GstRTSPAddress *server_addr_v4;
130 GstRTSPAddress *server_addr_v6;
132 /* multicast addresses */
133 GstRTSPAddress *mcast_addr_v4;
134 GstRTSPAddress *mcast_addr_v6;
136 gchar *multicast_iface;
138 /* the caps of the stream */
142 /* transports we stream to */
145 guint transports_cookie;
147 GList *tr_cache_rtcp;
148 guint tr_cache_cookie_rtp;
149 guint tr_cache_cookie_rtcp;
153 /* stream blocking */
157 /* pt->caps map for RECORD streams */
160 GstRTSPPublishClockMode publish_clock_mode;
163 #define DEFAULT_CONTROL NULL
164 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
165 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
166 GST_RTSP_LOWER_TRANS_TCP
179 SIGNAL_NEW_RTP_ENCODER,
180 SIGNAL_NEW_RTCP_ENCODER,
184 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
185 #define GST_CAT_DEFAULT rtsp_stream_debug
187 static GQuark ssrc_stream_map_key;
189 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
190 GValue * value, GParamSpec * pspec);
191 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
192 const GValue * value, GParamSpec * pspec);
194 static void gst_rtsp_stream_finalize (GObject * obj);
196 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
198 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
201 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
203 GObjectClass *gobject_class;
205 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
207 gobject_class = G_OBJECT_CLASS (klass);
209 gobject_class->get_property = gst_rtsp_stream_get_property;
210 gobject_class->set_property = gst_rtsp_stream_set_property;
211 gobject_class->finalize = gst_rtsp_stream_finalize;
213 g_object_class_install_property (gobject_class, PROP_CONTROL,
214 g_param_spec_string ("control", "Control",
215 "The control string for this stream", DEFAULT_CONTROL,
216 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
218 g_object_class_install_property (gobject_class, PROP_PROFILES,
219 g_param_spec_flags ("profiles", "Profiles",
220 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
221 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
224 g_param_spec_flags ("protocols", "Protocols",
225 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
226 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
229 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
230 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
231 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
233 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
234 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
235 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
236 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
238 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
240 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
244 gst_rtsp_stream_init (GstRTSPStream * stream)
246 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
248 GST_DEBUG ("new stream %p", stream);
253 priv->control = g_strdup (DEFAULT_CONTROL);
254 priv->profiles = DEFAULT_PROFILES;
255 priv->protocols = DEFAULT_PROTOCOLS;
256 priv->publish_clock_mode = GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK;
258 g_mutex_init (&priv->lock);
260 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
261 NULL, (GDestroyNotify) gst_caps_unref);
262 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
263 (GDestroyNotify) gst_caps_unref);
267 gst_rtsp_stream_finalize (GObject * obj)
269 GstRTSPStream *stream;
270 GstRTSPStreamPrivate *priv;
272 stream = GST_RTSP_STREAM (obj);
275 GST_DEBUG ("finalize stream %p", stream);
277 /* we really need to be unjoined now */
278 g_return_if_fail (priv->joined_bin == NULL);
280 if (priv->mcast_addr_v4)
281 gst_rtsp_address_free (priv->mcast_addr_v4);
282 if (priv->mcast_addr_v6)
283 gst_rtsp_address_free (priv->mcast_addr_v6);
284 if (priv->server_addr_v4)
285 gst_rtsp_address_free (priv->server_addr_v4);
286 if (priv->server_addr_v6)
287 gst_rtsp_address_free (priv->server_addr_v6);
289 g_object_unref (priv->pool);
291 g_object_unref (priv->rtxsend);
293 g_free (priv->multicast_iface);
295 gst_object_unref (priv->payloader);
297 gst_object_unref (priv->srcpad);
299 gst_object_unref (priv->sinkpad);
300 g_free (priv->control);
301 g_mutex_clear (&priv->lock);
303 g_hash_table_unref (priv->keys);
304 g_hash_table_destroy (priv->ptmap);
306 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
310 gst_rtsp_stream_get_property (GObject * object, guint propid,
311 GValue * value, GParamSpec * pspec)
313 GstRTSPStream *stream = GST_RTSP_STREAM (object);
317 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
320 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
323 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
331 gst_rtsp_stream_set_property (GObject * object, guint propid,
332 const GValue * value, GParamSpec * pspec)
334 GstRTSPStream *stream = GST_RTSP_STREAM (object);
338 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
341 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
344 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
347 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
352 * gst_rtsp_stream_new:
355 * @payloader: a #GstElement
357 * Create a new media stream with index @idx that handles RTP data on
358 * @pad and has a payloader element @payloader if @pad is a source pad
359 * or a depayloader element @payloader if @pad is a sink pad.
361 * Returns: (transfer full): a new #GstRTSPStream
364 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
366 GstRTSPStreamPrivate *priv;
367 GstRTSPStream *stream;
369 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
370 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
372 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
375 priv->payloader = gst_object_ref (payloader);
376 if (GST_PAD_IS_SRC (pad))
377 priv->srcpad = gst_object_ref (pad);
379 priv->sinkpad = gst_object_ref (pad);
385 * gst_rtsp_stream_get_index:
386 * @stream: a #GstRTSPStream
388 * Get the stream index.
390 * Return: the stream index.
393 gst_rtsp_stream_get_index (GstRTSPStream * stream)
395 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
397 return stream->priv->idx;
401 * gst_rtsp_stream_get_pt:
402 * @stream: a #GstRTSPStream
404 * Get the stream payload type.
406 * Return: the stream payload type.
409 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
411 GstRTSPStreamPrivate *priv;
414 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
418 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
424 * gst_rtsp_stream_get_srcpad:
425 * @stream: a #GstRTSPStream
427 * Get the srcpad associated with @stream.
429 * Returns: (transfer full): the srcpad. Unref after usage.
432 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
434 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
436 if (!stream->priv->srcpad)
439 return gst_object_ref (stream->priv->srcpad);
443 * gst_rtsp_stream_get_sinkpad:
444 * @stream: a #GstRTSPStream
446 * Get the sinkpad associated with @stream.
448 * Returns: (transfer full): the sinkpad. Unref after usage.
451 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
453 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
455 if (!stream->priv->sinkpad)
458 return gst_object_ref (stream->priv->sinkpad);
462 * gst_rtsp_stream_get_control:
463 * @stream: a #GstRTSPStream
465 * Get the control string to identify this stream.
467 * Returns: (transfer full): the control string. g_free() after usage.
470 gst_rtsp_stream_get_control (GstRTSPStream * stream)
472 GstRTSPStreamPrivate *priv;
475 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
479 g_mutex_lock (&priv->lock);
480 if ((result = g_strdup (priv->control)) == NULL)
481 result = g_strdup_printf ("stream=%u", priv->idx);
482 g_mutex_unlock (&priv->lock);
488 * gst_rtsp_stream_set_control:
489 * @stream: a #GstRTSPStream
490 * @control: a control string
492 * Set the control string in @stream.
495 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
497 GstRTSPStreamPrivate *priv;
499 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
503 g_mutex_lock (&priv->lock);
504 g_free (priv->control);
505 priv->control = g_strdup (control);
506 g_mutex_unlock (&priv->lock);
510 * gst_rtsp_stream_has_control:
511 * @stream: a #GstRTSPStream
512 * @control: a control string
514 * Check if @stream has the control string @control.
516 * Returns: %TRUE is @stream has @control as the control string
519 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
521 GstRTSPStreamPrivate *priv;
524 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
528 g_mutex_lock (&priv->lock);
530 res = (g_strcmp0 (priv->control, control) == 0);
534 if (sscanf (control, "stream=%u", &streamid) > 0)
535 res = (streamid == priv->idx);
539 g_mutex_unlock (&priv->lock);
545 * gst_rtsp_stream_set_mtu:
546 * @stream: a #GstRTSPStream
549 * Configure the mtu in the payloader of @stream to @mtu.
552 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
554 GstRTSPStreamPrivate *priv;
556 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
560 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
562 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
566 * gst_rtsp_stream_get_mtu:
567 * @stream: a #GstRTSPStream
569 * Get the configured MTU in the payloader of @stream.
571 * Returns: the MTU of the payloader.
574 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
576 GstRTSPStreamPrivate *priv;
579 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
583 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
588 /* Update the dscp qos property on the udp sinks */
590 update_dscp_qos (GstRTSPStream * stream, GstElement * udpsink[2])
592 GstRTSPStreamPrivate *priv;
597 g_object_set (G_OBJECT (udpsink[0]), "qos-dscp", priv->dscp_qos, NULL);
601 g_object_set (G_OBJECT (udpsink[1]), "qos-dscp", priv->dscp_qos, NULL);
606 * gst_rtsp_stream_set_dscp_qos:
607 * @stream: a #GstRTSPStream
608 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
610 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
613 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
615 GstRTSPStreamPrivate *priv;
617 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
621 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
623 if (dscp_qos < -1 || dscp_qos > 63) {
624 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
628 priv->dscp_qos = dscp_qos;
630 update_dscp_qos (stream, priv->udpsink);
634 * gst_rtsp_stream_get_dscp_qos:
635 * @stream: a #GstRTSPStream
637 * Get the configured DSCP QoS in of the outgoing sockets.
639 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
642 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
644 GstRTSPStreamPrivate *priv;
646 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
650 return priv->dscp_qos;
654 * gst_rtsp_stream_is_transport_supported:
655 * @stream: a #GstRTSPStream
656 * @transport: (transfer none): a #GstRTSPTransport
658 * Check if @transport can be handled by stream
660 * Returns: %TRUE if @transport can be handled by @stream.
663 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
664 GstRTSPTransport * transport)
666 GstRTSPStreamPrivate *priv;
668 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
672 g_mutex_lock (&priv->lock);
673 if (transport->trans != GST_RTSP_TRANS_RTP)
674 goto unsupported_transmode;
676 if (!(transport->profile & priv->profiles))
677 goto unsupported_profile;
679 if (!(transport->lower_transport & priv->protocols))
680 goto unsupported_ltrans;
682 g_mutex_unlock (&priv->lock);
687 unsupported_transmode:
689 GST_DEBUG ("unsupported transport mode %d", transport->trans);
690 g_mutex_unlock (&priv->lock);
695 GST_DEBUG ("unsupported profile %d", transport->profile);
696 g_mutex_unlock (&priv->lock);
701 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
702 g_mutex_unlock (&priv->lock);
708 * gst_rtsp_stream_set_profiles:
709 * @stream: a #GstRTSPStream
710 * @profiles: the new profiles
712 * Configure the allowed profiles for @stream.
715 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
717 GstRTSPStreamPrivate *priv;
719 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
723 g_mutex_lock (&priv->lock);
724 priv->profiles = profiles;
725 g_mutex_unlock (&priv->lock);
729 * gst_rtsp_stream_get_profiles:
730 * @stream: a #GstRTSPStream
732 * Get the allowed profiles of @stream.
734 * Returns: a #GstRTSPProfile
737 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
739 GstRTSPStreamPrivate *priv;
742 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
746 g_mutex_lock (&priv->lock);
747 res = priv->profiles;
748 g_mutex_unlock (&priv->lock);
754 * gst_rtsp_stream_set_protocols:
755 * @stream: a #GstRTSPStream
756 * @protocols: the new flags
758 * Configure the allowed lower transport for @stream.
761 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
762 GstRTSPLowerTrans protocols)
764 GstRTSPStreamPrivate *priv;
766 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
770 g_mutex_lock (&priv->lock);
771 priv->protocols = protocols;
772 g_mutex_unlock (&priv->lock);
776 * gst_rtsp_stream_get_protocols:
777 * @stream: a #GstRTSPStream
779 * Get the allowed protocols of @stream.
781 * Returns: a #GstRTSPLowerTrans
784 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
786 GstRTSPStreamPrivate *priv;
787 GstRTSPLowerTrans res;
789 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
790 GST_RTSP_LOWER_TRANS_UNKNOWN);
794 g_mutex_lock (&priv->lock);
795 res = priv->protocols;
796 g_mutex_unlock (&priv->lock);
802 * gst_rtsp_stream_set_address_pool:
803 * @stream: a #GstRTSPStream
804 * @pool: (transfer none): a #GstRTSPAddressPool
806 * configure @pool to be used as the address pool of @stream.
809 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
810 GstRTSPAddressPool * pool)
812 GstRTSPStreamPrivate *priv;
813 GstRTSPAddressPool *old;
815 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
819 GST_LOG_OBJECT (stream, "set address pool %p", pool);
821 g_mutex_lock (&priv->lock);
822 if ((old = priv->pool) != pool)
823 priv->pool = pool ? g_object_ref (pool) : NULL;
826 g_mutex_unlock (&priv->lock);
829 g_object_unref (old);
833 * gst_rtsp_stream_get_address_pool:
834 * @stream: a #GstRTSPStream
836 * Get the #GstRTSPAddressPool used as the address pool of @stream.
838 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
842 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
844 GstRTSPStreamPrivate *priv;
845 GstRTSPAddressPool *result;
847 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
851 g_mutex_lock (&priv->lock);
852 if ((result = priv->pool))
853 g_object_ref (result);
854 g_mutex_unlock (&priv->lock);
860 * gst_rtsp_stream_set_multicast_iface:
861 * @stream: a #GstRTSPStream
862 * @multicast_iface: (transfer none): a multicast interface
864 * configure @multicast_iface to be used for @stream.
867 gst_rtsp_stream_set_multicast_iface (GstRTSPStream * stream,
868 const gchar * multicast_iface)
870 GstRTSPStreamPrivate *priv;
873 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
877 GST_LOG_OBJECT (stream, "set multicast iface %s",
878 GST_STR_NULL (multicast_iface));
880 g_mutex_lock (&priv->lock);
881 if ((old = priv->multicast_iface) != multicast_iface)
882 priv->multicast_iface = multicast_iface ? g_strdup (multicast_iface) : NULL;
885 g_mutex_unlock (&priv->lock);
892 * gst_rtsp_stream_get_multicast_iface:
893 * @stream: a #GstRTSPStream
895 * Get the multicast interface used for @stream.
897 * Returns: (transfer full): the multicast interface for @stream. g_free() after
901 gst_rtsp_stream_get_multicast_iface (GstRTSPStream * stream)
903 GstRTSPStreamPrivate *priv;
906 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
910 g_mutex_lock (&priv->lock);
911 if ((result = priv->multicast_iface))
912 result = g_strdup (result);
913 g_mutex_unlock (&priv->lock);
919 * gst_rtsp_stream_get_multicast_address:
920 * @stream: a #GstRTSPStream
921 * @family: the #GSocketFamily
923 * Get the multicast address of @stream for @family. The original
924 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
925 * won't release the address from the pool.
927 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
928 * or %NULL when no address could be allocated. gst_rtsp_address_free()
932 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
933 GSocketFamily family)
935 GstRTSPStreamPrivate *priv;
936 GstRTSPAddress *result;
937 GstRTSPAddress **addrp;
938 GstRTSPAddressFlags flags;
940 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
944 if (family == G_SOCKET_FAMILY_IPV6) {
945 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
946 addrp = &priv->mcast_addr_v6;
948 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
949 addrp = &priv->mcast_addr_v4;
952 g_mutex_lock (&priv->lock);
953 if (*addrp == NULL) {
954 if (priv->pool == NULL)
957 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
959 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
963 /* FIXME: Also reserve the same port with unicast ANY address, since that's
964 * where we are going to bind our socket. Probably loop until we find a port
965 * available in both mcast and unicast pools. Maybe GstRTSPAddressPool
966 * should do it for us when both GST_RTSP_ADDRESS_FLAG_MULTICAST and
967 * GST_RTSP_ADDRESS_FLAG_UNICAST are givent. */
969 result = gst_rtsp_address_copy (*addrp);
970 g_mutex_unlock (&priv->lock);
977 GST_ERROR_OBJECT (stream, "no address pool specified");
978 g_mutex_unlock (&priv->lock);
983 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
984 g_mutex_unlock (&priv->lock);
990 * gst_rtsp_stream_reserve_address:
991 * @stream: a #GstRTSPStream
992 * @address: an address
997 * Reserve @address and @port as the address and port of @stream. The original
998 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
999 * won't release the address from the pool.
1001 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
1002 * the address could be reserved. gst_rtsp_address_free() after usage.
1005 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
1006 const gchar * address, guint port, guint n_ports, guint ttl)
1008 GstRTSPStreamPrivate *priv;
1009 GstRTSPAddress *result;
1011 GSocketFamily family;
1012 GstRTSPAddress **addrp;
1014 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1015 g_return_val_if_fail (address != NULL, NULL);
1016 g_return_val_if_fail (port > 0, NULL);
1017 g_return_val_if_fail (n_ports > 0, NULL);
1018 g_return_val_if_fail (ttl > 0, NULL);
1020 priv = stream->priv;
1022 addr = g_inet_address_new_from_string (address);
1024 GST_ERROR ("failed to get inet addr from %s", address);
1025 family = G_SOCKET_FAMILY_IPV4;
1027 family = g_inet_address_get_family (addr);
1028 g_object_unref (addr);
1031 if (family == G_SOCKET_FAMILY_IPV6)
1032 addrp = &priv->mcast_addr_v6;
1034 addrp = &priv->mcast_addr_v4;
1036 g_mutex_lock (&priv->lock);
1037 if (*addrp == NULL) {
1038 GstRTSPAddressPoolResult res;
1040 if (priv->pool == NULL)
1043 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
1044 port, n_ports, ttl, addrp);
1045 if (res != GST_RTSP_ADDRESS_POOL_OK)
1048 /* FIXME: Also reserve the same port with unicast ANY address, since that's
1049 * where we are going to bind our socket. */
1051 if (strcmp ((*addrp)->address, address) ||
1052 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
1053 (*addrp)->ttl != ttl)
1054 goto different_address;
1056 result = gst_rtsp_address_copy (*addrp);
1057 g_mutex_unlock (&priv->lock);
1064 GST_ERROR_OBJECT (stream, "no address pool specified");
1065 g_mutex_unlock (&priv->lock);
1070 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1072 g_mutex_unlock (&priv->lock);
1077 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
1078 " reserved", address);
1079 g_mutex_unlock (&priv->lock);
1084 /* must be called with lock */
1086 set_sockets_for_udpsinks (GstElement * udpsink[2], GSocket * rtp_socket,
1087 GSocket * rtcp_socket, GSocketFamily family)
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 (udpsink[0]), multisink_socket, rtp_socket, NULL);
1097 g_object_set (G_OBJECT (udpsink[1]), multisink_socket, rtcp_socket, NULL);
1101 create_and_configure_udpsinks (GstRTSPStream * stream, GstElement * udpsink[2])
1103 GstRTSPStreamPrivate *priv = stream->priv;
1104 GstElement *udpsink0, *udpsink1;
1106 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1107 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1109 if (!udpsink0 || !udpsink1)
1110 goto no_udp_protocol;
1112 /* configure sinks */
1114 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1115 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1117 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1118 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1120 g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
1122 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1123 /* Needs to be async for RECORD streams, otherwise we will never go to
1124 * PLAYING because the sinks will wait for data while the udpsrc can't
1125 * provide data with timestamps in PAUSED. */
1127 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1128 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1130 /* join multicast group when adding clients, so we'll start receiving from it.
1131 * We cannot rely on the udpsrc to join the group since its socket is always a
1132 * local unicast one. */
1133 g_object_set (G_OBJECT (udpsink0), "auto-multicast", TRUE, NULL);
1134 g_object_set (G_OBJECT (udpsink1), "auto-multicast", TRUE, NULL);
1136 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1137 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1139 udpsink[0] = udpsink0;
1140 udpsink[1] = udpsink1;
1142 /* update the dscp qos field in the sinks */
1143 update_dscp_qos (stream, udpsink);
1154 /* must be called with lock */
1156 create_and_configure_udpsources (GstElement * udpsrc_out[2],
1157 GSocket * rtp_socket, GSocket * rtcp_socket)
1159 GstStateChangeReturn ret;
1161 udpsrc_out[0] = gst_element_factory_make ("udpsrc", NULL);
1162 udpsrc_out[1] = gst_element_factory_make ("udpsrc", NULL);
1164 if (udpsrc_out[0] == NULL || udpsrc_out[1] == NULL)
1167 g_object_set (G_OBJECT (udpsrc_out[0]), "socket", rtp_socket, NULL);
1168 g_object_set (G_OBJECT (udpsrc_out[1]), "socket", rtcp_socket, NULL);
1170 /* The udpsrc cannot do the join because its socket is always a local unicast
1171 * one. The udpsink sharing the same socket will do it for us. */
1172 g_object_set (G_OBJECT (udpsrc_out[0]), "auto-multicast", FALSE, NULL);
1173 g_object_set (G_OBJECT (udpsrc_out[1]), "auto-multicast", FALSE, NULL);
1175 g_object_set (G_OBJECT (udpsrc_out[0]), "loop", FALSE, NULL);
1176 g_object_set (G_OBJECT (udpsrc_out[1]), "loop", FALSE, NULL);
1178 ret = gst_element_set_state (udpsrc_out[0], GST_STATE_READY);
1179 if (ret == GST_STATE_CHANGE_FAILURE)
1181 ret = gst_element_set_state (udpsrc_out[1], GST_STATE_READY);
1182 if (ret == GST_STATE_CHANGE_FAILURE)
1190 if (udpsrc_out[0]) {
1191 gst_element_set_state (udpsrc_out[0], GST_STATE_NULL);
1192 g_clear_object (&udpsrc_out[0]);
1194 if (udpsrc_out[1]) {
1195 gst_element_set_state (udpsrc_out[1], GST_STATE_NULL);
1196 g_clear_object (&udpsrc_out[1]);
1203 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1204 GstElement * udpsrc_out[2], GstElement * udpsink_out[2],
1205 GstRTSPRange * server_port_out, GstRTSPAddress ** server_addr_out)
1207 GstRTSPStreamPrivate *priv = stream->priv;
1208 GSocket *rtp_socket = NULL;
1209 GSocket *rtcp_socket;
1210 gint tmp_rtp, tmp_rtcp;
1212 gint rtpport, rtcpport;
1213 GList *rejected_addresses = NULL;
1214 GstRTSPAddress *addr = NULL;
1215 GInetAddress *inetaddr = NULL;
1217 GSocketAddress *rtp_sockaddr = NULL;
1218 GSocketAddress *rtcp_sockaddr = NULL;
1219 GstRTSPAddressPool *pool;
1221 g_assert (!udpsrc_out[0]);
1222 g_assert (!udpsrc_out[1]);
1223 g_assert ((!udpsink_out[0] && !udpsink_out[1]) ||
1224 (udpsink_out[0] && udpsink_out[1]));
1225 g_assert (*server_addr_out == NULL);
1230 /* Start with random port */
1233 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1234 G_SOCKET_PROTOCOL_UDP, NULL);
1236 goto no_udp_protocol;
1237 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1239 /* try to allocate 2 UDP ports, the RTP port should be an even
1240 * number and the RTCP port should be the next (uneven) port */
1243 if (rtp_socket == NULL) {
1244 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1245 G_SOCKET_PROTOCOL_UDP, NULL);
1247 goto no_udp_protocol;
1248 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1251 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
1252 GstRTSPAddressFlags flags;
1255 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1257 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
1258 if (family == G_SOCKET_FAMILY_IPV6)
1259 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1261 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1263 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1268 tmp_rtp = addr->port;
1270 g_clear_object (&inetaddr);
1271 inetaddr = g_inet_address_new_from_string (addr->address);
1279 if (inetaddr == NULL)
1280 inetaddr = g_inet_address_new_any (family);
1283 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1284 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1285 g_object_unref (rtp_sockaddr);
1288 g_object_unref (rtp_sockaddr);
1290 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1291 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1292 g_clear_object (&rtp_sockaddr);
1297 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1298 g_object_unref (rtp_sockaddr);
1300 /* check if port is even */
1301 if ((tmp_rtp & 1) != 0) {
1302 /* port not even, close and allocate another */
1304 g_clear_object (&rtp_socket);
1309 tmp_rtcp = tmp_rtp + 1;
1311 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1312 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1313 g_object_unref (rtcp_sockaddr);
1314 g_clear_object (&rtp_socket);
1317 g_object_unref (rtcp_sockaddr);
1320 addr_str = g_inet_address_to_string (inetaddr);
1322 addr_str = addr->address;
1323 g_clear_object (&inetaddr);
1325 if (!create_and_configure_udpsources (udpsrc_out, rtp_socket, rtcp_socket)) {
1328 goto no_udp_protocol;
1334 g_object_get (G_OBJECT (udpsrc_out[0]), "port", &rtpport, NULL);
1335 g_object_get (G_OBJECT (udpsrc_out[1]), "port", &rtcpport, NULL);
1337 /* this should not happen... */
1338 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1341 server_port_out->min = rtpport;
1342 server_port_out->max = rtcpport;
1344 /* This function is called twice (for v4 and v6) but we create only one pair
1347 && !create_and_configure_udpsinks (stream, udpsink_out))
1348 goto no_udp_protocol;
1350 set_sockets_for_udpsinks (udpsink_out, rtp_socket, rtcp_socket, family);
1352 *server_addr_out = addr;
1353 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1355 g_object_unref (rtp_socket);
1356 g_object_unref (rtcp_socket);
1380 g_object_unref (inetaddr);
1381 g_list_free_full (rejected_addresses,
1382 (GDestroyNotify) gst_rtsp_address_free);
1384 gst_rtsp_address_free (addr);
1386 g_object_unref (rtp_socket);
1388 g_object_unref (rtcp_socket);
1394 * gst_rtsp_stream_allocate_udp_sockets:
1395 * @stream: a #GstRTSPStream
1396 * @family: protocol family
1397 * @transport_method: transport method
1399 * Allocates RTP and RTCP ports.
1401 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1402 * Deprecated: This function shouldn't have been made public
1405 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1406 GSocketFamily family, GstRTSPTransport * ct, gboolean use_client_settings)
1408 g_warn_if_reached ();
1413 * gst_rtsp_stream_set_client_side:
1414 * @stream: a #GstRTSPStream
1415 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1416 * an RTSP connection.
1418 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1419 * streams to an RTSP server via RECORD. This has the practical effect
1420 * of changing which UDP port numbers are used when setting up the local
1421 * side of the stream sending to be either the 'server' or 'client' pair
1422 * of a configured UDP transport.
1425 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1427 GstRTSPStreamPrivate *priv;
1429 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1430 priv = stream->priv;
1431 g_mutex_lock (&priv->lock);
1432 priv->client_side = client_side;
1433 g_mutex_unlock (&priv->lock);
1437 * gst_rtsp_stream_is_client_side:
1438 * @stream: a #GstRTSPStream
1440 * See gst_rtsp_stream_set_client_side()
1442 * Returns: TRUE if this #GstRTSPStream is client-side.
1445 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1447 GstRTSPStreamPrivate *priv;
1450 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1452 priv = stream->priv;
1453 g_mutex_lock (&priv->lock);
1454 ret = priv->client_side;
1455 g_mutex_unlock (&priv->lock);
1460 /* must be called with lock */
1462 alloc_ports (GstRTSPStream * stream)
1464 GstRTSPStreamPrivate *priv = stream->priv;
1465 gboolean ret = TRUE;
1467 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
1468 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)) {
1469 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1470 priv->udpsrc_v4, priv->udpsink,
1471 &priv->server_port_v4, &priv->server_addr_v4);
1473 ret |= alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1474 priv->udpsrc_v6, priv->udpsink,
1475 &priv->server_port_v6, &priv->server_addr_v6);
1482 * gst_rtsp_stream_get_server_port:
1483 * @stream: a #GstRTSPStream
1484 * @server_port: (out): result server port
1485 * @family: the port family to get
1487 * Fill @server_port with the port pair used by the server. This function can
1488 * only be called when @stream has been joined.
1491 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1492 GstRTSPRange * server_port, GSocketFamily family)
1494 GstRTSPStreamPrivate *priv;
1496 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1497 priv = stream->priv;
1498 g_return_if_fail (priv->joined_bin != NULL);
1500 g_mutex_lock (&priv->lock);
1501 if (family == G_SOCKET_FAMILY_IPV4) {
1503 *server_port = priv->server_port_v4;
1506 *server_port = priv->server_port_v6;
1508 g_mutex_unlock (&priv->lock);
1512 * gst_rtsp_stream_get_rtpsession:
1513 * @stream: a #GstRTSPStream
1515 * Get the RTP session of this stream.
1517 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1520 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1522 GstRTSPStreamPrivate *priv;
1525 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1527 priv = stream->priv;
1529 g_mutex_lock (&priv->lock);
1530 if ((session = priv->session))
1531 g_object_ref (session);
1532 g_mutex_unlock (&priv->lock);
1538 * gst_rtsp_stream_get_encoder:
1539 * @stream: a #GstRTSPStream
1541 * Get the SRTP encoder for this stream.
1543 * Returns: (transfer full): The SRTP encoder for this stream. Unref after usage.
1546 gst_rtsp_stream_get_srtp_encoder (GstRTSPStream * stream)
1548 GstRTSPStreamPrivate *priv;
1549 GstElement *encoder;
1551 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1553 priv = stream->priv;
1555 g_mutex_lock (&priv->lock);
1556 if ((encoder = priv->srtpenc))
1557 g_object_ref (encoder);
1558 g_mutex_unlock (&priv->lock);
1564 * gst_rtsp_stream_get_ssrc:
1565 * @stream: a #GstRTSPStream
1566 * @ssrc: (out): result ssrc
1568 * Get the SSRC used by the RTP session of this stream. This function can only
1569 * be called when @stream has been joined.
1572 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1574 GstRTSPStreamPrivate *priv;
1576 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1577 priv = stream->priv;
1578 g_return_if_fail (priv->joined_bin != NULL);
1580 g_mutex_lock (&priv->lock);
1581 if (ssrc && priv->session)
1582 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1583 g_mutex_unlock (&priv->lock);
1587 * gst_rtsp_stream_set_retransmission_time:
1588 * @stream: a #GstRTSPStream
1589 * @time: a #GstClockTime
1591 * Set the amount of time to store retransmission packets.
1594 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1597 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1599 g_mutex_lock (&stream->priv->lock);
1600 stream->priv->rtx_time = time;
1601 if (stream->priv->rtxsend)
1602 g_object_set (stream->priv->rtxsend, "max-size-time",
1603 GST_TIME_AS_MSECONDS (time), NULL);
1604 g_mutex_unlock (&stream->priv->lock);
1608 * gst_rtsp_stream_get_retransmission_time:
1609 * @stream: a #GstRTSPStream
1611 * Get the amount of time to store retransmission data.
1613 * Returns: the amount of time to store retransmission data.
1616 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1620 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1622 g_mutex_lock (&stream->priv->lock);
1623 ret = stream->priv->rtx_time;
1624 g_mutex_unlock (&stream->priv->lock);
1630 * gst_rtsp_stream_set_retransmission_pt:
1631 * @stream: a #GstRTSPStream
1634 * Set the payload type (pt) for retransmission of this stream.
1637 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1639 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1641 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1643 g_mutex_lock (&stream->priv->lock);
1644 stream->priv->rtx_pt = rtx_pt;
1645 if (stream->priv->rtxsend) {
1646 guint pt = gst_rtsp_stream_get_pt (stream);
1647 gchar *pt_s = g_strdup_printf ("%d", pt);
1648 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1649 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1650 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1652 gst_structure_free (rtx_pt_map);
1654 g_mutex_unlock (&stream->priv->lock);
1658 * gst_rtsp_stream_get_retransmission_pt:
1659 * @stream: a #GstRTSPStream
1661 * Get the payload-type used for retransmission of this stream
1663 * Returns: The retransmission PT.
1666 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1670 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1672 g_mutex_lock (&stream->priv->lock);
1673 rtx_pt = stream->priv->rtx_pt;
1674 g_mutex_unlock (&stream->priv->lock);
1680 * gst_rtsp_stream_set_buffer_size:
1681 * @stream: a #GstRTSPStream
1682 * @size: the buffer size
1684 * Set the size of the UDP transmission buffer (in bytes)
1685 * Needs to be set before the stream is joined to a bin.
1690 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1692 g_mutex_lock (&stream->priv->lock);
1693 stream->priv->buffer_size = size;
1694 g_mutex_unlock (&stream->priv->lock);
1698 * gst_rtsp_stream_get_buffer_size:
1699 * @stream: a #GstRTSPStream
1701 * Get the size of the UDP transmission buffer (in bytes)
1703 * Returns: the size of the UDP TX buffer
1708 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1712 g_mutex_lock (&stream->priv->lock);
1713 buffer_size = stream->priv->buffer_size;
1714 g_mutex_unlock (&stream->priv->lock);
1719 /* executed from streaming thread */
1721 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1723 GstRTSPStreamPrivate *priv = stream->priv;
1724 GstCaps *newcaps, *oldcaps;
1726 newcaps = gst_pad_get_current_caps (pad);
1728 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1731 g_mutex_lock (&priv->lock);
1732 oldcaps = priv->caps;
1733 priv->caps = newcaps;
1734 g_mutex_unlock (&priv->lock);
1737 gst_caps_unref (oldcaps);
1741 dump_structure (const GstStructure * s)
1745 sstr = gst_structure_to_string (s);
1746 GST_INFO ("structure: %s", sstr);
1750 static GstRTSPStreamTransport *
1751 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1753 GstRTSPStreamPrivate *priv = stream->priv;
1755 GstRTSPStreamTransport *result = NULL;
1760 if (rtcp_from == NULL)
1763 tmp = g_strrstr (rtcp_from, ":");
1767 port = atoi (tmp + 1);
1768 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1770 g_mutex_lock (&priv->lock);
1771 GST_INFO ("finding %s:%d in %d transports", dest, port,
1772 g_list_length (priv->transports));
1774 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1775 GstRTSPStreamTransport *trans = walk->data;
1776 const GstRTSPTransport *tr;
1779 tr = gst_rtsp_stream_transport_get_transport (trans);
1781 if (priv->client_side) {
1782 /* In client side mode the 'destination' is the RTSP server, so send
1784 min = tr->server_port.min;
1785 max = tr->server_port.max;
1787 min = tr->client_port.min;
1788 max = tr->client_port.max;
1791 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1797 g_object_ref (result);
1798 g_mutex_unlock (&priv->lock);
1805 static GstRTSPStreamTransport *
1806 check_transport (GObject * source, GstRTSPStream * stream)
1808 GstStructure *stats;
1809 GstRTSPStreamTransport *trans;
1811 /* see if we have a stream to match with the origin of the RTCP packet */
1812 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1813 if (trans == NULL) {
1814 g_object_get (source, "stats", &stats, NULL);
1816 const gchar *rtcp_from;
1818 dump_structure (stats);
1820 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1821 if ((trans = find_transport (stream, rtcp_from))) {
1822 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1824 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1827 gst_structure_free (stats);
1835 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1837 GstRTSPStreamTransport *trans;
1839 GST_INFO ("%p: new source %p", stream, source);
1841 trans = check_transport (source, stream);
1844 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1848 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1850 GST_INFO ("%p: new SDES %p", stream, source);
1854 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1856 GstRTSPStreamTransport *trans;
1858 trans = check_transport (source, stream);
1861 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1862 gst_rtsp_stream_transport_keep_alive (trans);
1866 GstStructure *stats;
1867 g_object_get (source, "stats", &stats, NULL);
1869 dump_structure (stats);
1870 gst_structure_free (stats);
1877 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1879 GST_INFO ("%p: source %p bye", stream, source);
1883 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1885 GstRTSPStreamTransport *trans;
1887 GST_INFO ("%p: source %p bye timeout", stream, source);
1889 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1890 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1891 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1896 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1898 GstRTSPStreamTransport *trans;
1900 GST_INFO ("%p: source %p timeout", stream, source);
1902 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1903 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1904 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1909 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1911 GST_INFO ("%p: new sender source %p", stream, source);
1914 GstStructure *stats;
1915 g_object_get (source, "stats", &stats, NULL);
1917 dump_structure (stats);
1918 gst_structure_free (stats);
1925 on_sender_ssrc_active (GObject * session, GObject * source,
1926 GstRTSPStream * stream)
1930 GstStructure *stats;
1931 g_object_get (source, "stats", &stats, NULL);
1933 dump_structure (stats);
1934 gst_structure_free (stats);
1941 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1944 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1945 g_list_free (priv->tr_cache_rtp);
1946 priv->tr_cache_rtp = NULL;
1948 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1949 g_list_free (priv->tr_cache_rtcp);
1950 priv->tr_cache_rtcp = NULL;
1954 static GstFlowReturn
1955 handle_new_sample (GstAppSink * sink, gpointer user_data)
1957 GstRTSPStreamPrivate *priv;
1961 GstRTSPStream *stream;
1964 sample = gst_app_sink_pull_sample (sink);
1968 stream = (GstRTSPStream *) user_data;
1969 priv = stream->priv;
1970 buffer = gst_sample_get_buffer (sample);
1972 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
1974 g_mutex_lock (&priv->lock);
1976 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
1977 clear_tr_cache (priv, is_rtp);
1978 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1979 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1980 priv->tr_cache_rtp =
1981 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
1983 priv->tr_cache_cookie_rtp = priv->transports_cookie;
1986 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
1987 clear_tr_cache (priv, is_rtp);
1988 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1989 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1990 priv->tr_cache_rtcp =
1991 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
1993 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
1996 g_mutex_unlock (&priv->lock);
1999 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2000 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2001 gst_rtsp_stream_transport_send_rtp (tr, buffer);
2004 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2005 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2006 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
2009 gst_sample_unref (sample);
2014 static GstAppSinkCallbacks sink_cb = {
2015 NULL, /* not interested in EOS */
2016 NULL, /* not interested in preroll samples */
2021 get_rtp_encoder (GstRTSPStream * stream, guint session)
2023 GstRTSPStreamPrivate *priv = stream->priv;
2025 if (priv->srtpenc == NULL) {
2028 name = g_strdup_printf ("srtpenc_%u", session);
2029 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2032 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2034 return gst_object_ref (priv->srtpenc);
2038 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2040 GstRTSPStreamPrivate *priv = stream->priv;
2041 GstElement *oldenc, *enc;
2045 if (priv->idx != session)
2048 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2050 oldenc = priv->srtpenc;
2051 enc = get_rtp_encoder (stream, session);
2052 name = g_strdup_printf ("rtp_sink_%d", session);
2053 pad = gst_element_get_request_pad (enc, name);
2055 gst_object_unref (pad);
2058 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2065 request_rtcp_encoder (GstElement * rtpbin, guint session,
2066 GstRTSPStream * stream)
2068 GstRTSPStreamPrivate *priv = stream->priv;
2069 GstElement *oldenc, *enc;
2073 if (priv->idx != session)
2076 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2078 oldenc = priv->srtpenc;
2079 enc = get_rtp_encoder (stream, session);
2080 name = g_strdup_printf ("rtcp_sink_%d", session);
2081 pad = gst_element_get_request_pad (enc, name);
2083 gst_object_unref (pad);
2086 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2093 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2095 GstRTSPStreamPrivate *priv = stream->priv;
2098 GST_DEBUG ("request key %08x", ssrc);
2100 g_mutex_lock (&priv->lock);
2101 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2102 gst_caps_ref (caps);
2103 g_mutex_unlock (&priv->lock);
2109 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2110 GstRTSPStream * stream)
2112 GstRTSPStreamPrivate *priv = stream->priv;
2114 if (priv->idx != session)
2117 if (priv->srtpdec == NULL) {
2120 name = g_strdup_printf ("srtpdec_%u", session);
2121 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2124 g_signal_connect (priv->srtpdec, "request-key",
2125 (GCallback) request_key, stream);
2127 return gst_object_ref (priv->srtpdec);
2131 * gst_rtsp_stream_request_aux_sender:
2132 * @stream: a #GstRTSPStream
2133 * @sessid: the session id
2135 * Creating a rtxsend bin
2137 * Returns: (transfer full): a #GstElement.
2142 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2146 GstStructure *pt_map;
2151 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2153 pt = gst_rtsp_stream_get_pt (stream);
2154 pt_s = g_strdup_printf ("%u", pt);
2155 rtx_pt = stream->priv->rtx_pt;
2157 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2159 bin = gst_bin_new (NULL);
2160 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2161 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2162 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2163 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2164 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2166 gst_structure_free (pt_map);
2167 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2169 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2170 name = g_strdup_printf ("src_%u", sessid);
2171 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2173 gst_object_unref (pad);
2175 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2176 name = g_strdup_printf ("sink_%u", sessid);
2177 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2179 gst_object_unref (pad);
2185 * gst_rtsp_stream_set_pt_map:
2186 * @stream: a #GstRTSPStream
2190 * Configure a pt map between @pt and @caps.
2193 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2195 GstRTSPStreamPrivate *priv = stream->priv;
2197 g_mutex_lock (&priv->lock);
2198 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2199 g_mutex_unlock (&priv->lock);
2203 * gst_rtsp_stream_set_publish_clock_mode:
2204 * @stream: a #GstRTSPStream
2205 * @mode: the clock publish mode
2207 * Sets if and how the stream clock should be published according to RFC7273.
2212 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
2213 GstRTSPPublishClockMode mode)
2215 GstRTSPStreamPrivate *priv;
2217 priv = stream->priv;
2218 g_mutex_lock (&priv->lock);
2219 priv->publish_clock_mode = mode;
2220 g_mutex_unlock (&priv->lock);
2224 * gst_rtsp_stream_get_publish_clock_mode:
2225 * @factory: a #GstRTSPStream
2227 * Gets if and how the stream clock should be published according to RFC7273.
2229 * Returns: The GstRTSPPublishClockMode
2233 GstRTSPPublishClockMode
2234 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
2236 GstRTSPStreamPrivate *priv;
2237 GstRTSPPublishClockMode ret;
2239 priv = stream->priv;
2240 g_mutex_lock (&priv->lock);
2241 ret = priv->publish_clock_mode;
2242 g_mutex_unlock (&priv->lock);
2248 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2249 GstRTSPStream * stream)
2251 GstRTSPStreamPrivate *priv = stream->priv;
2252 GstCaps *caps = NULL;
2254 g_mutex_lock (&priv->lock);
2256 if (priv->idx == session) {
2257 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2259 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2260 gst_caps_ref (caps);
2262 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2266 g_mutex_unlock (&priv->lock);
2272 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2274 GstRTSPStreamPrivate *priv = stream->priv;
2276 GstPadLinkReturn ret;
2279 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2280 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2282 name = gst_pad_get_name (pad);
2283 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2289 if (priv->idx != sessid)
2292 if (gst_pad_is_linked (priv->sinkpad)) {
2293 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2294 GST_DEBUG_PAD_NAME (priv->sinkpad));
2298 /* link the RTP pad to the session manager, it should not really fail unless
2299 * this is not really an RTP pad */
2300 ret = gst_pad_link (pad, priv->sinkpad);
2301 if (ret != GST_PAD_LINK_OK)
2303 priv->recv_rtp_src = gst_object_ref (pad);
2310 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2311 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2316 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2317 GstRTSPStream * stream)
2319 /* TODO: What to do here other than this? */
2320 GST_DEBUG ("Stream %p: Got EOS", stream);
2321 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2325 plug_sink (GstBin * bin, GstElement * tee, GstElement * sink,
2326 GstElement ** queue_out)
2332 gst_bin_add (bin, sink);
2334 *queue_out = gst_element_factory_make ("queue", NULL);
2335 g_object_set (*queue_out, "max-size-buffers", 1, "max-size-bytes", 0,
2336 "max-size-time", G_GINT64_CONSTANT (0), NULL);
2337 gst_bin_add (bin, *queue_out);
2339 /* link tee to queue */
2340 teepad = gst_element_get_request_pad (tee, "src_%u");
2341 pad = gst_element_get_static_pad (*queue_out, "sink");
2342 gst_pad_link (teepad, pad);
2343 gst_object_unref (pad);
2344 gst_object_unref (teepad);
2346 /* link queue to sink */
2347 queuepad = gst_element_get_static_pad (*queue_out, "src");
2348 pad = gst_element_get_static_pad (sink, "sink");
2349 gst_pad_link (queuepad, pad);
2350 gst_object_unref (queuepad);
2351 gst_object_unref (pad);
2354 /* must be called with lock */
2356 create_sender_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2358 GstRTSPStreamPrivate *priv;
2360 gboolean is_tcp = FALSE, is_udp = FALSE;
2363 priv = stream->priv;
2365 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2366 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2367 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2369 for (i = 0; i < 2; i++) {
2370 /* For the sender we create this bit of pipeline for both
2371 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2372 * we need to add a queue before appsink and udpsink to make
2373 * the pipeline not block. For the TCP case, we want to pump
2374 * client as fast as possible anyway. This pipeline is used
2375 * when both TCP and UDP are present.
2377 * .--------. .-----. .---------. .---------.
2378 * | rtpbin | | tee | | queue | | udpsink |
2379 * | send->sink src->sink src->sink |
2380 * '--------' | | '---------' '---------'
2381 * | | .---------. .---------.
2382 * | | | queue | | appsink |
2383 * | src->sink src->sink |
2384 * '-----' '---------' '---------'
2386 * When only UDP or only TCP is allowed, we skip the tee and queue
2387 * and link the udpsink (for UDP) or appsink (for TCP) directly to
2391 /* Only link the RTP send src if we're going to send RTP, link
2392 * the RTCP send src always */
2393 if (!priv->srcpad && i == 0)
2398 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2399 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2400 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2401 &sink_cb, stream, NULL);
2404 /* If we have udp always use a tee because we could have mcast clients
2405 * requesting different ports, in which case we'll have to plug more
2408 /* make tee for RTP/RTCP */
2409 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2410 gst_bin_add (bin, priv->tee[i]);
2412 /* and link to rtpbin send pad */
2413 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2414 gst_pad_link (priv->send_src[i], pad);
2415 gst_object_unref (pad);
2417 plug_sink (bin, priv->tee[i], priv->udpsink[i], &priv->udpqueue[i]);
2420 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2421 plug_sink (bin, priv->tee[i], priv->appsink[i], &priv->appqueue[i]);
2423 } else if (is_tcp) {
2424 /* only appsink needed, link it to the session */
2425 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2426 gst_pad_link (priv->send_src[i], pad);
2427 gst_object_unref (pad);
2429 /* when its only TCP, we need to set sync and preroll to FALSE
2430 * for the sink to avoid deadlock. And this is only needed for
2431 * sink used for RTCP data, not the RTP data. */
2433 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2436 /* check if we need to set to a special state */
2437 if (state != GST_STATE_NULL) {
2438 if (priv->udpsink[i])
2439 gst_element_set_state (priv->udpsink[i], state);
2440 if (priv->appsink[i])
2441 gst_element_set_state (priv->appsink[i], state);
2442 if (priv->appqueue[i])
2443 gst_element_set_state (priv->appqueue[i], state);
2444 if (priv->udpqueue[i])
2445 gst_element_set_state (priv->udpqueue[i], state);
2447 gst_element_set_state (priv->tee[i], state);
2452 /* must be called with lock */
2454 plug_src (GstRTSPStream * stream, GstBin * bin, GstElement * src,
2455 GstElement * funnel)
2457 GstRTSPStreamPrivate *priv;
2458 GstPad *pad, *selpad;
2460 priv = stream->priv;
2463 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2464 * values. This is only relevant for PLAY pipelines */
2465 gst_element_set_state (src, GST_STATE_PLAYING);
2466 gst_element_set_locked_state (src, TRUE);
2470 gst_bin_add (bin, src);
2472 /* and link to the funnel */
2473 selpad = gst_element_get_request_pad (funnel, "sink_%u");
2474 pad = gst_element_get_static_pad (src, "src");
2475 gst_pad_link (pad, selpad);
2476 gst_object_unref (pad);
2477 gst_object_unref (selpad);
2480 /* must be called with lock */
2482 create_receiver_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2484 GstRTSPStreamPrivate *priv;
2489 priv = stream->priv;
2491 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2493 for (i = 0; i < 2; i++) {
2494 /* For the receiver we create this bit of pipeline for both
2495 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2496 * and it is all funneled into the rtpbin receive pad.
2498 * .--------. .--------. .--------.
2499 * | udpsrc | | funnel | | rtpbin |
2500 * | src->sink src->sink |
2501 * '--------' | | '--------'
2505 * '--------' '--------'
2508 if (!priv->sinkpad && i == 0) {
2509 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2510 * RTCP sink always */
2514 /* make funnel for the RTP/RTCP receivers */
2515 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2516 gst_bin_add (bin, priv->funnel[i]);
2518 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2519 gst_pad_link (pad, priv->recv_sink[i]);
2520 gst_object_unref (pad);
2522 if (priv->udpsrc_v4[i])
2523 plug_src (stream, bin, priv->udpsrc_v4[i], priv->funnel[i]);
2525 if (priv->udpsrc_v6[i])
2526 plug_src (stream, bin, priv->udpsrc_v6[i], priv->funnel[i]);
2529 /* make and add appsrc */
2530 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2531 priv->appsrc_base_time[i] = -1;
2532 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
2534 plug_src (stream, bin, priv->appsrc[i], priv->funnel[i]);
2537 /* check if we need to set to a special state */
2538 if (state != GST_STATE_NULL) {
2539 gst_element_set_state (priv->funnel[i], state);
2545 create_mcast_part_for_transport (GstRTSPStream * stream,
2546 const GstRTSPTransport * tr)
2548 GstRTSPStreamPrivate *priv = stream->priv;
2549 GInetAddress *inetaddr;
2550 GSocketFamily family;
2551 GstRTSPAddress *mcast_addr;
2552 GstElement **mcast_udpsrc;
2553 GSocket *rtp_socket = NULL;
2554 GSocket *rtcp_socket = NULL;
2555 GSocketAddress *rtp_sockaddr = NULL;
2556 GSocketAddress *rtcp_sockaddr = NULL;
2557 GError *error = NULL;
2558 const gchar *multicast_iface = priv->multicast_iface;
2560 /* Check if it's a ipv4 or ipv6 transport */
2561 inetaddr = g_inet_address_new_from_string (tr->destination);
2562 family = g_inet_address_get_family (inetaddr);
2563 g_object_unref (inetaddr);
2565 /* Select fields corresponding to the family */
2566 if (family == G_SOCKET_FAMILY_IPV4) {
2567 mcast_addr = priv->mcast_addr_v4;
2568 mcast_udpsrc = priv->mcast_udpsrc_v4;
2570 mcast_addr = priv->mcast_addr_v6;
2571 mcast_udpsrc = priv->mcast_udpsrc_v6;
2574 /* We support only one mcast group per family, make sure this transport
2579 if (!g_str_equal (tr->destination, mcast_addr->address) ||
2580 tr->port.min != mcast_addr->port ||
2581 tr->port.max != mcast_addr->port + mcast_addr->n_ports - 1 ||
2582 tr->ttl != mcast_addr->ttl)
2585 if (mcast_udpsrc[0]) {
2586 /* We already created elements for this family. Since we support only one
2587 * mcast group per family, there is nothing more to do here. */
2588 g_assert (mcast_udpsrc[1]);
2589 g_assert (priv->mcast_udpqueue[0]);
2590 g_assert (priv->mcast_udpqueue[1]);
2591 g_assert (priv->mcast_udpsink[0]);
2592 g_assert (priv->mcast_udpsink[1]);
2596 g_assert (!mcast_udpsrc[1]);
2598 /* Create RTP/RTCP sockets and bind them on ANY with mcast ports */
2599 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
2600 G_SOCKET_PROTOCOL_UDP, &error);
2603 g_socket_set_multicast_loopback (rtp_socket, FALSE);
2605 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
2606 G_SOCKET_PROTOCOL_UDP, &error);
2609 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
2611 inetaddr = g_inet_address_new_any (family);
2612 rtp_sockaddr = g_inet_socket_address_new (inetaddr, mcast_addr->port);
2613 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, mcast_addr->port + 1);
2614 g_object_unref (inetaddr);
2616 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, &error))
2619 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, &error))
2622 g_object_unref (rtp_sockaddr);
2623 g_object_unref (rtcp_sockaddr);
2625 /* Add receiver part */
2626 create_and_configure_udpsources (mcast_udpsrc, rtp_socket, rtcp_socket);
2627 if (priv->sinkpad) {
2628 plug_src (stream, priv->joined_bin, mcast_udpsrc[0], priv->funnel[0]);
2629 gst_element_sync_state_with_parent (mcast_udpsrc[0]);
2631 plug_src (stream, priv->joined_bin, mcast_udpsrc[1], priv->funnel[1]);
2632 gst_element_sync_state_with_parent (mcast_udpsrc[1]);
2634 /* Add sender part, could already have been created for the other family. */
2635 if (!priv->mcast_udpsink[0]) {
2636 g_assert (!priv->mcast_udpsink[1]);
2637 g_assert (!priv->mcast_udpqueue[0]);
2638 g_assert (!priv->mcast_udpqueue[1]);
2640 create_and_configure_udpsinks (stream, priv->mcast_udpsink);
2642 g_object_set (G_OBJECT (priv->mcast_udpsink[0]), "multicast-iface",
2643 multicast_iface, NULL);
2644 g_object_set (G_OBJECT (priv->mcast_udpsink[1]), "multicast-iface",
2645 multicast_iface, NULL);
2647 g_signal_emit_by_name (priv->mcast_udpsink[0], "add", mcast_addr->address,
2648 mcast_addr->port, NULL);
2649 g_signal_emit_by_name (priv->mcast_udpsink[1], "add", mcast_addr->address,
2650 mcast_addr->port + 1, NULL);
2652 set_sockets_for_udpsinks (priv->mcast_udpsink, rtp_socket, rtcp_socket,
2656 plug_sink (priv->joined_bin, priv->tee[0], priv->mcast_udpsink[0],
2657 &priv->mcast_udpqueue[0]);
2658 gst_element_sync_state_with_parent (priv->mcast_udpsink[0]);
2659 gst_element_sync_state_with_parent (priv->mcast_udpqueue[0]);
2661 plug_sink (priv->joined_bin, priv->tee[1], priv->mcast_udpsink[1],
2662 &priv->mcast_udpqueue[1]);
2663 gst_element_sync_state_with_parent (priv->mcast_udpsink[1]);
2664 gst_element_sync_state_with_parent (priv->mcast_udpqueue[1]);
2666 g_assert (priv->mcast_udpsink[1]);
2667 g_assert (priv->mcast_udpqueue[0]);
2668 g_assert (priv->mcast_udpqueue[1]);
2670 set_sockets_for_udpsinks (priv->mcast_udpsink, rtp_socket, rtcp_socket,
2678 GST_WARNING_OBJECT (stream, "Adding mcast transport, but no mcast address "
2679 "has been reserved");
2684 GST_WARNING_OBJECT (stream, "Adding mcast transport, but it doesn't match "
2685 "the reserved address");
2690 GST_ERROR_OBJECT (stream, "Error creating and binding mcast socket: %s",
2692 g_clear_object (&rtp_socket);
2693 g_clear_object (&rtcp_socket);
2694 g_clear_object (&rtp_sockaddr);
2695 g_clear_object (&rtcp_sockaddr);
2696 g_clear_error (&error);
2702 * gst_rtsp_stream_join_bin:
2703 * @stream: a #GstRTSPStream
2704 * @bin: (transfer none): a #GstBin to join
2705 * @rtpbin: (transfer none): a rtpbin element in @bin
2706 * @state: the target state of the new elements
2708 * Join the #GstBin @bin that contains the element @rtpbin.
2710 * @stream will link to @rtpbin, which must be inside @bin. The elements
2711 * added to @bin will be set to the state given in @state.
2713 * Returns: %TRUE on success.
2716 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2717 GstElement * rtpbin, GstState state)
2719 GstRTSPStreamPrivate *priv;
2722 GstPadLinkReturn ret;
2724 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2725 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2726 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2728 priv = stream->priv;
2730 g_mutex_lock (&priv->lock);
2731 if (priv->joined_bin != NULL)
2734 /* create a session with the same index as the stream */
2737 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2739 if (!alloc_ports (stream))
2742 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2743 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2745 g_signal_connect (rtpbin, "request-rtp-encoder",
2746 (GCallback) request_rtp_encoder, stream);
2747 g_signal_connect (rtpbin, "request-rtcp-encoder",
2748 (GCallback) request_rtcp_encoder, stream);
2749 g_signal_connect (rtpbin, "request-rtp-decoder",
2750 (GCallback) request_rtp_rtcp_decoder, stream);
2751 g_signal_connect (rtpbin, "request-rtcp-decoder",
2752 (GCallback) request_rtp_rtcp_decoder, stream);
2755 if (priv->sinkpad) {
2756 g_signal_connect (rtpbin, "request-pt-map",
2757 (GCallback) request_pt_map, stream);
2760 /* get pads from the RTP session element for sending and receiving
2763 /* get a pad for sending RTP */
2764 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2765 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2768 /* link the RTP pad to the session manager, it should not really fail unless
2769 * this is not really an RTP pad */
2770 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2771 if (ret != GST_PAD_LINK_OK)
2774 name = g_strdup_printf ("send_rtp_src_%u", idx);
2775 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2778 /* Need to connect our sinkpad from here */
2779 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2781 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2783 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2784 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2788 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2789 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2791 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2792 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2795 /* get the session */
2796 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2798 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2800 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2802 g_signal_connect (priv->session, "on-ssrc-active",
2803 (GCallback) on_ssrc_active, stream);
2804 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2806 g_signal_connect (priv->session, "on-bye-timeout",
2807 (GCallback) on_bye_timeout, stream);
2808 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2811 /* signal for sender ssrc */
2812 g_signal_connect (priv->session, "on-new-sender-ssrc",
2813 (GCallback) on_new_sender_ssrc, stream);
2814 g_signal_connect (priv->session, "on-sender-ssrc-active",
2815 (GCallback) on_sender_ssrc_active, stream);
2817 create_sender_part (stream, bin, state);
2818 create_receiver_part (stream, bin, state);
2821 /* be notified of caps changes */
2822 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2823 (GCallback) caps_notify, stream);
2826 priv->joined_bin = gst_object_ref (bin);
2827 g_mutex_unlock (&priv->lock);
2834 g_mutex_unlock (&priv->lock);
2839 g_mutex_unlock (&priv->lock);
2840 GST_WARNING ("failed to allocate ports %u", idx);
2845 GST_WARNING ("failed to link stream %u", idx);
2846 gst_object_unref (priv->send_rtp_sink);
2847 priv->send_rtp_sink = NULL;
2848 g_mutex_unlock (&priv->lock);
2854 clear_element (GstBin * bin, GstElement ** elementptr)
2857 gst_element_set_locked_state (*elementptr, FALSE);
2858 gst_element_set_state (*elementptr, GST_STATE_NULL);
2859 if (GST_ELEMENT_PARENT (*elementptr))
2860 gst_bin_remove (bin, *elementptr);
2862 gst_object_unref (*elementptr);
2868 * gst_rtsp_stream_leave_bin:
2869 * @stream: a #GstRTSPStream
2870 * @bin: (transfer none): a #GstBin
2871 * @rtpbin: (transfer none): a rtpbin #GstElement
2873 * Remove the elements of @stream from @bin.
2875 * Return: %TRUE on success.
2878 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2879 GstElement * rtpbin)
2881 GstRTSPStreamPrivate *priv;
2884 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2885 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2886 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2888 priv = stream->priv;
2890 g_mutex_lock (&priv->lock);
2891 if (priv->joined_bin == NULL)
2892 goto was_not_joined;
2893 if (priv->joined_bin != bin)
2896 priv->joined_bin = NULL;
2898 /* all transports must be removed by now */
2899 if (priv->transports != NULL)
2900 goto transports_not_removed;
2902 clear_tr_cache (priv, TRUE);
2903 clear_tr_cache (priv, FALSE);
2905 GST_INFO ("stream %p leaving bin", stream);
2908 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2910 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2911 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2912 gst_object_unref (priv->send_rtp_sink);
2913 priv->send_rtp_sink = NULL;
2914 } else if (priv->recv_rtp_src) {
2915 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2916 gst_object_unref (priv->recv_rtp_src);
2917 priv->recv_rtp_src = NULL;
2920 for (i = 0; i < 2; i++) {
2921 clear_element (bin, &priv->udpsrc_v4[i]);
2922 clear_element (bin, &priv->udpsrc_v6[i]);
2923 clear_element (bin, &priv->udpqueue[i]);
2924 clear_element (bin, &priv->udpsink[i]);
2926 clear_element (bin, &priv->mcast_udpsrc_v4[i]);
2927 clear_element (bin, &priv->mcast_udpsrc_v6[i]);
2928 clear_element (bin, &priv->mcast_udpqueue[i]);
2929 clear_element (bin, &priv->mcast_udpsink[i]);
2931 clear_element (bin, &priv->appsrc[i]);
2932 clear_element (bin, &priv->appqueue[i]);
2933 clear_element (bin, &priv->appsink[i]);
2935 clear_element (bin, &priv->tee[i]);
2936 clear_element (bin, &priv->funnel[i]);
2938 if (priv->sinkpad || i == 1) {
2939 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2940 gst_object_unref (priv->recv_sink[i]);
2941 priv->recv_sink[i] = NULL;
2946 gst_object_unref (priv->send_src[0]);
2947 priv->send_src[0] = NULL;
2950 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2951 gst_object_unref (priv->send_src[1]);
2952 priv->send_src[1] = NULL;
2954 g_object_unref (priv->session);
2955 priv->session = NULL;
2957 gst_caps_unref (priv->caps);
2961 gst_object_unref (priv->srtpenc);
2963 gst_object_unref (priv->srtpdec);
2965 g_clear_object (&priv->joined_bin);
2966 g_mutex_unlock (&priv->lock);
2972 g_mutex_unlock (&priv->lock);
2975 transports_not_removed:
2977 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2978 g_mutex_unlock (&priv->lock);
2983 GST_ERROR_OBJECT (stream, "leaving the wrong bin");
2984 g_mutex_unlock (&priv->lock);
2990 * gst_rtsp_stream_get_joined_bin:
2991 * @stream: a #GstRTSPStream
2993 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
2995 * Return: (transfer full): the joined bin or NULL.
2998 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
3000 GstRTSPStreamPrivate *priv;
3003 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3005 priv = stream->priv;
3007 g_mutex_lock (&priv->lock);
3008 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
3009 g_mutex_unlock (&priv->lock);
3015 * gst_rtsp_stream_get_rtpinfo:
3016 * @stream: a #GstRTSPStream
3017 * @rtptime: (allow-none): result RTP timestamp
3018 * @seq: (allow-none): result RTP seqnum
3019 * @clock_rate: (allow-none): the clock rate
3020 * @running_time: (allow-none): result running-time
3022 * Retrieve the current rtptime, seq and running-time. This is used to
3023 * construct a RTPInfo reply header.
3025 * Returns: %TRUE when rtptime, seq and running-time could be determined.
3028 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
3029 guint * rtptime, guint * seq, guint * clock_rate,
3030 GstClockTime * running_time)
3032 GstRTSPStreamPrivate *priv;
3033 GstStructure *stats;
3034 GObjectClass *payobjclass;
3036 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3038 priv = stream->priv;
3040 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
3042 g_mutex_lock (&priv->lock);
3044 /* First try to extract the information from the last buffer on the sinks.
3045 * This will have a more accurate sequence number and timestamp, as between
3046 * the payloader and the sink there can be some queues
3048 if (priv->udpsink[0] || priv->appsink[0]) {
3049 GstSample *last_sample;
3051 if (priv->udpsink[0])
3052 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
3054 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
3059 GstSegment *segment;
3060 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
3062 caps = gst_sample_get_caps (last_sample);
3063 buffer = gst_sample_get_buffer (last_sample);
3064 segment = gst_sample_get_segment (last_sample);
3066 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3068 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3072 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3075 gst_rtp_buffer_unmap (&rtp_buffer);
3079 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3080 GST_BUFFER_TIMESTAMP (buffer));
3084 GstStructure *s = gst_caps_get_structure (caps, 0);
3086 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3088 if (*clock_rate == 0 && running_time)
3089 *running_time = GST_CLOCK_TIME_NONE;
3091 gst_sample_unref (last_sample);
3095 gst_sample_unref (last_sample);
3100 if (g_object_class_find_property (payobjclass, "stats")) {
3101 g_object_get (priv->payloader, "stats", &stats, NULL);
3106 gst_structure_get_uint (stats, "seqnum", seq);
3109 gst_structure_get_uint (stats, "timestamp", rtptime);
3112 gst_structure_get_clock_time (stats, "running-time", running_time);
3115 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3116 if (*clock_rate == 0 && running_time)
3117 *running_time = GST_CLOCK_TIME_NONE;
3119 gst_structure_free (stats);
3121 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3122 !g_object_class_find_property (payobjclass, "timestamp"))
3126 g_object_get (priv->payloader, "seqnum", seq, NULL);
3129 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3132 *running_time = GST_CLOCK_TIME_NONE;
3136 g_mutex_unlock (&priv->lock);
3143 GST_WARNING ("Could not get payloader stats");
3144 g_mutex_unlock (&priv->lock);
3150 * gst_rtsp_stream_get_caps:
3151 * @stream: a #GstRTSPStream
3153 * Retrieve the current caps of @stream.
3155 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
3159 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3161 GstRTSPStreamPrivate *priv;
3164 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3166 priv = stream->priv;
3168 g_mutex_lock (&priv->lock);
3169 if ((result = priv->caps))
3170 gst_caps_ref (result);
3171 g_mutex_unlock (&priv->lock);
3177 * gst_rtsp_stream_recv_rtp:
3178 * @stream: a #GstRTSPStream
3179 * @buffer: (transfer full): a #GstBuffer
3181 * Handle an RTP buffer for the stream. This method is usually called when a
3182 * message has been received from a client using the TCP transport.
3184 * This function takes ownership of @buffer.
3186 * Returns: a GstFlowReturn.
3189 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3191 GstRTSPStreamPrivate *priv;
3193 GstElement *element;
3195 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3196 priv = stream->priv;
3197 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3198 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3200 g_mutex_lock (&priv->lock);
3201 if (priv->appsrc[0])
3202 element = gst_object_ref (priv->appsrc[0]);
3205 g_mutex_unlock (&priv->lock);
3208 if (priv->appsrc_base_time[0] == -1) {
3209 /* Take current running_time. This timestamp will be put on
3210 * the first buffer of each stream because we are a live source and so we
3211 * timestamp with the running_time. When we are dealing with TCP, we also
3212 * only timestamp the first buffer (using the DISCONT flag) because a server
3213 * typically bursts data, for which we don't want to compensate by speeding
3214 * up the media. The other timestamps will be interpollated from this one
3215 * using the RTP timestamps. */
3216 GST_OBJECT_LOCK (element);
3217 if (GST_ELEMENT_CLOCK (element)) {
3219 GstClockTime base_time;
3221 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3222 base_time = GST_ELEMENT_CAST (element)->base_time;
3224 priv->appsrc_base_time[0] = now - base_time;
3225 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3226 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3227 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3228 GST_TIME_ARGS (base_time));
3230 GST_OBJECT_UNLOCK (element);
3233 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3234 gst_object_unref (element);
3242 * gst_rtsp_stream_recv_rtcp:
3243 * @stream: a #GstRTSPStream
3244 * @buffer: (transfer full): a #GstBuffer
3246 * Handle an RTCP buffer for the stream. This method is usually called when a
3247 * message has been received from a client using the TCP transport.
3249 * This function takes ownership of @buffer.
3251 * Returns: a GstFlowReturn.
3254 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3256 GstRTSPStreamPrivate *priv;
3258 GstElement *element;
3260 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3261 priv = stream->priv;
3262 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3264 if (priv->joined_bin == NULL) {
3265 gst_buffer_unref (buffer);
3266 return GST_FLOW_NOT_LINKED;
3268 g_mutex_lock (&priv->lock);
3269 if (priv->appsrc[1])
3270 element = gst_object_ref (priv->appsrc[1]);
3273 g_mutex_unlock (&priv->lock);
3276 if (priv->appsrc_base_time[1] == -1) {
3277 /* Take current running_time. This timestamp will be put on
3278 * the first buffer of each stream because we are a live source and so we
3279 * timestamp with the running_time. When we are dealing with TCP, we also
3280 * only timestamp the first buffer (using the DISCONT flag) because a server
3281 * typically bursts data, for which we don't want to compensate by speeding
3282 * up the media. The other timestamps will be interpollated from this one
3283 * using the RTP timestamps. */
3284 GST_OBJECT_LOCK (element);
3285 if (GST_ELEMENT_CLOCK (element)) {
3287 GstClockTime base_time;
3289 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3290 base_time = GST_ELEMENT_CAST (element)->base_time;
3292 priv->appsrc_base_time[1] = now - base_time;
3293 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3294 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3295 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3296 GST_TIME_ARGS (base_time));
3298 GST_OBJECT_UNLOCK (element);
3301 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3302 gst_object_unref (element);
3305 gst_buffer_unref (buffer);
3310 /* must be called with lock */
3312 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3315 GstRTSPStreamPrivate *priv = stream->priv;
3316 const GstRTSPTransport *tr;
3318 tr = gst_rtsp_stream_transport_get_transport (trans);
3320 switch (tr->lower_transport) {
3321 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3324 if (!create_mcast_part_for_transport (stream, tr))
3326 priv->transports = g_list_prepend (priv->transports, trans);
3328 priv->transports = g_list_remove (priv->transports, trans);
3329 /* FIXME: Check if there are remaining mcast transports, and destroy
3330 * mcast part if its now unused */
3334 case GST_RTSP_LOWER_TRANS_UDP:
3340 dest = tr->destination;
3341 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3345 } else if (priv->client_side) {
3346 /* In client side mode the 'destination' is the RTSP server, so send
3348 min = tr->server_port.min;
3349 max = tr->server_port.max;
3351 min = tr->client_port.min;
3352 max = tr->client_port.max;
3357 GST_INFO ("setting ttl-mc %d", ttl);
3358 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3359 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3361 GST_INFO ("adding %s:%d-%d", dest, min, max);
3362 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3363 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3364 priv->transports = g_list_prepend (priv->transports, trans);
3366 GST_INFO ("removing %s:%d-%d", dest, min, max);
3367 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3368 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3369 priv->transports = g_list_remove (priv->transports, trans);
3371 priv->transports_cookie++;
3374 case GST_RTSP_LOWER_TRANS_TCP:
3376 GST_INFO ("adding TCP %s", tr->destination);
3377 priv->transports = g_list_prepend (priv->transports, trans);
3379 GST_INFO ("removing TCP %s", tr->destination);
3380 priv->transports = g_list_remove (priv->transports, trans);
3382 priv->transports_cookie++;
3385 goto unknown_transport;
3392 GST_INFO ("Unknown transport %d", tr->lower_transport);
3403 * gst_rtsp_stream_add_transport:
3404 * @stream: a #GstRTSPStream
3405 * @trans: (transfer none): a #GstRTSPStreamTransport
3407 * Add the transport in @trans to @stream. The media of @stream will
3408 * then also be send to the values configured in @trans.
3410 * @stream must be joined to a bin.
3412 * @trans must contain a valid #GstRTSPTransport.
3414 * Returns: %TRUE if @trans was added
3417 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3418 GstRTSPStreamTransport * trans)
3420 GstRTSPStreamPrivate *priv;
3423 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3424 priv = stream->priv;
3425 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3426 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3428 g_mutex_lock (&priv->lock);
3429 res = update_transport (stream, trans, TRUE);
3430 g_mutex_unlock (&priv->lock);
3436 * gst_rtsp_stream_remove_transport:
3437 * @stream: a #GstRTSPStream
3438 * @trans: (transfer none): a #GstRTSPStreamTransport
3440 * Remove the transport in @trans from @stream. The media of @stream will
3441 * not be sent to the values configured in @trans.
3443 * @stream must be joined to a bin.
3445 * @trans must contain a valid #GstRTSPTransport.
3447 * Returns: %TRUE if @trans was removed
3450 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3451 GstRTSPStreamTransport * trans)
3453 GstRTSPStreamPrivate *priv;
3456 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3457 priv = stream->priv;
3458 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3459 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3461 g_mutex_lock (&priv->lock);
3462 res = update_transport (stream, trans, FALSE);
3463 g_mutex_unlock (&priv->lock);
3469 * gst_rtsp_stream_update_crypto:
3470 * @stream: a #GstRTSPStream
3472 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3474 * Update the new crypto information for @ssrc in @stream. If information
3475 * for @ssrc did not exist, it will be added. If information
3476 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3477 * be removed from @stream.
3479 * Returns: %TRUE if @crypto could be updated
3482 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3483 guint ssrc, GstCaps * crypto)
3485 GstRTSPStreamPrivate *priv;
3487 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3488 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3490 priv = stream->priv;
3492 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3494 g_mutex_lock (&priv->lock);
3496 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3497 gst_caps_ref (crypto));
3499 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3500 g_mutex_unlock (&priv->lock);
3506 * gst_rtsp_stream_get_rtp_socket:
3507 * @stream: a #GstRTSPStream
3508 * @family: the socket family
3510 * Get the RTP socket from @stream for a @family.
3512 * @stream must be joined to a bin.
3514 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3515 * socket could be allocated for @family. Unref after usage
3518 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3520 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3524 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3525 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3526 family == G_SOCKET_FAMILY_IPV6, NULL);
3527 g_return_val_if_fail (priv->udpsink[0], NULL);
3529 if (family == G_SOCKET_FAMILY_IPV6)
3534 g_object_get (priv->udpsink[0], name, &socket, NULL);
3540 * gst_rtsp_stream_get_rtcp_socket:
3541 * @stream: a #GstRTSPStream
3542 * @family: the socket family
3544 * Get the RTCP socket from @stream for a @family.
3546 * @stream must be joined to a bin.
3548 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3549 * socket could be allocated for @family. Unref after usage
3552 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3554 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3558 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3559 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3560 family == G_SOCKET_FAMILY_IPV6, NULL);
3561 g_return_val_if_fail (priv->udpsink[1], NULL);
3563 if (family == G_SOCKET_FAMILY_IPV6)
3568 g_object_get (priv->udpsink[1], name, &socket, NULL);
3574 * gst_rtsp_stream_set_seqnum:
3575 * @stream: a #GstRTSPStream
3576 * @seqnum: a new sequence number
3578 * Configure the sequence number in the payloader of @stream to @seqnum.
3581 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3583 GstRTSPStreamPrivate *priv;
3585 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3587 priv = stream->priv;
3589 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3593 * gst_rtsp_stream_get_seqnum:
3594 * @stream: a #GstRTSPStream
3596 * Get the configured sequence number in the payloader of @stream.
3598 * Returns: the sequence number of the payloader.
3601 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3603 GstRTSPStreamPrivate *priv;
3606 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3608 priv = stream->priv;
3610 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3616 * gst_rtsp_stream_transport_filter:
3617 * @stream: a #GstRTSPStream
3618 * @func: (scope call) (allow-none): a callback
3619 * @user_data: (closure): user data passed to @func
3621 * Call @func for each transport managed by @stream. The result value of @func
3622 * determines what happens to the transport. @func will be called with @stream
3623 * locked so no further actions on @stream can be performed from @func.
3625 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3628 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3630 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3631 * will also be added with an additional ref to the result #GList of this
3634 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3636 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3637 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3638 * element in the #GList should be unreffed before the list is freed.
3641 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3642 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3644 GstRTSPStreamPrivate *priv;
3645 GList *result, *walk, *next;
3646 GHashTable *visited = NULL;
3649 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3651 priv = stream->priv;
3655 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3657 g_mutex_lock (&priv->lock);
3659 cookie = priv->transports_cookie;
3660 for (walk = priv->transports; walk; walk = next) {
3661 GstRTSPStreamTransport *trans = walk->data;
3662 GstRTSPFilterResult res;
3665 next = g_list_next (walk);
3668 /* only visit each transport once */
3669 if (g_hash_table_contains (visited, trans))
3672 g_hash_table_add (visited, g_object_ref (trans));
3673 g_mutex_unlock (&priv->lock);
3675 res = func (stream, trans, user_data);
3677 g_mutex_lock (&priv->lock);
3679 res = GST_RTSP_FILTER_REF;
3681 changed = (cookie != priv->transports_cookie);
3684 case GST_RTSP_FILTER_REMOVE:
3685 update_transport (stream, trans, FALSE);
3687 case GST_RTSP_FILTER_REF:
3688 result = g_list_prepend (result, g_object_ref (trans));
3690 case GST_RTSP_FILTER_KEEP:
3697 g_mutex_unlock (&priv->lock);
3700 g_hash_table_unref (visited);
3705 static GstPadProbeReturn
3706 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3708 GstRTSPStreamPrivate *priv;
3709 GstRTSPStream *stream;
3712 priv = stream->priv;
3714 GST_DEBUG_OBJECT (pad, "now blocking");
3716 g_mutex_lock (&priv->lock);
3717 priv->blocking = TRUE;
3718 g_mutex_unlock (&priv->lock);
3720 gst_element_post_message (priv->payloader,
3721 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3722 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3724 return GST_PAD_PROBE_OK;
3728 * gst_rtsp_stream_set_blocked:
3729 * @stream: a #GstRTSPStream
3730 * @blocked: boolean indicating we should block or unblock
3732 * Blocks or unblocks the dataflow on @stream.
3734 * Returns: %TRUE on success
3737 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3739 GstRTSPStreamPrivate *priv;
3741 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3743 priv = stream->priv;
3745 g_mutex_lock (&priv->lock);
3747 priv->blocking = FALSE;
3748 if (priv->blocked_id == 0) {
3749 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3750 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3751 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3752 g_object_ref (stream), g_object_unref);
3755 if (priv->blocked_id != 0) {
3756 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3757 priv->blocked_id = 0;
3758 priv->blocking = FALSE;
3761 g_mutex_unlock (&priv->lock);
3767 * gst_rtsp_stream_is_blocking:
3768 * @stream: a #GstRTSPStream
3770 * Check if @stream is blocking on a #GstBuffer.
3772 * Returns: %TRUE if @stream is blocking
3775 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3777 GstRTSPStreamPrivate *priv;
3780 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3782 priv = stream->priv;
3784 g_mutex_lock (&priv->lock);
3785 result = priv->blocking;
3786 g_mutex_unlock (&priv->lock);
3792 * gst_rtsp_stream_query_position:
3793 * @stream: a #GstRTSPStream
3795 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3796 * the RTP parts of the pipeline and not the RTCP parts.
3798 * Returns: %TRUE if the position could be queried
3801 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3803 GstRTSPStreamPrivate *priv;
3807 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3809 priv = stream->priv;
3811 g_mutex_lock (&priv->lock);
3812 /* depending on the transport type, it should query corresponding sink */
3813 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3814 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3815 sink = priv->udpsink[0];
3817 sink = priv->appsink[0];
3820 gst_object_ref (sink);
3821 g_mutex_unlock (&priv->lock);
3826 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3827 gst_object_unref (sink);
3833 * gst_rtsp_stream_query_stop:
3834 * @stream: a #GstRTSPStream
3836 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3837 * the RTP parts of the pipeline and not the RTCP parts.
3839 * Returns: %TRUE if the stop could be queried
3842 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3844 GstRTSPStreamPrivate *priv;
3849 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3851 priv = stream->priv;
3853 g_mutex_lock (&priv->lock);
3854 /* depending on the transport type, it should query corresponding sink */
3855 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3856 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3857 sink = priv->udpsink[0];
3859 sink = priv->appsink[0];
3862 gst_object_ref (sink);
3863 g_mutex_unlock (&priv->lock);
3868 query = gst_query_new_segment (GST_FORMAT_TIME);
3869 if ((ret = gst_element_query (sink, query))) {
3872 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3873 if (format != GST_FORMAT_TIME)
3876 gst_query_unref (query);
3877 gst_object_unref (sink);