2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3 * Copyright (C) 2015 Centricular Ltd
4 * Author: Sebastian Dröge <sebastian@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * @short_description: A media stream
24 * @see_also: #GstRTSPMedia
26 * The #GstRTSPStream object manages the data transport for one stream. It
27 * is created from a payloader element and a source pad that produce the RTP
28 * packets for the stream.
30 * With gst_rtsp_stream_join_bin() the streaming elements are added to the bin
31 * and rtpbin. gst_rtsp_stream_leave_bin() removes the elements again.
33 * The #GstRTSPStream will use the configured addresspool, as set with
34 * gst_rtsp_stream_set_address_pool(), to allocate multicast addresses for the
35 * stream. With gst_rtsp_stream_get_multicast_address() you can get the
38 * With gst_rtsp_stream_get_server_port () you can get the port that the server
39 * will use to receive RTCP. This is the part that the clients will use to send
42 * With gst_rtsp_stream_add_transport() destinations can be added where the
43 * stream should be sent to. Use gst_rtsp_stream_remove_transport() to remove
44 * the destination again.
46 * Last reviewed on 2013-07-16 (1.0.0)
55 #include <gst/app/gstappsrc.h>
56 #include <gst/app/gstappsink.h>
58 #include <gst/rtp/gstrtpbuffer.h>
60 #include "rtsp-stream.h"
62 #define GST_RTSP_STREAM_GET_PRIVATE(obj) \
63 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
65 struct _GstRTSPStreamPrivate
69 /* Only one pad is ever set */
70 GstPad *srcpad, *sinkpad;
71 GstElement *payloader;
75 /* TRUE if this stream is running on
76 * the client side of an RTSP link (for RECORD) */
80 GstRTSPProfile profiles;
81 GstRTSPLowerTrans protocols;
83 /* pads on the rtpbin */
84 GstPad *send_rtp_sink;
89 /* the RTPSession object */
92 /* SRTP encoder/decoder */
97 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
99 GstElement *udpsrc_v4[2];
100 /* UDP sources for UDP multicast transports */
101 GstElement *udpsrc_mcast_v4[2];
103 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
105 GstElement *udpsrc_v6[2];
106 /* UDP sources for UDP multicast transports */
107 GstElement *udpsrc_mcast_v6[2];
109 GstElement *udpqueue[2];
110 GstElement *udpsink[2];
112 /* for TCP transport */
113 GstElement *appsrc[2];
114 GstClockTime appsrc_base_time[2];
115 GstElement *appqueue[2];
116 GstElement *appsink[2];
119 GstElement *funnel[2];
124 GstClockTime rtx_time;
126 /* server ports for sending/receiving over ipv4 */
127 GstRTSPRange server_port_v4;
128 GstRTSPAddress *server_addr_v4;
131 /* server ports for sending/receiving over ipv6 */
132 GstRTSPRange server_port_v6;
133 GstRTSPAddress *server_addr_v6;
136 /* multicast addresses */
137 GstRTSPAddressPool *pool;
138 GstRTSPAddress *mcast_addr_v4;
139 GstRTSPAddress *mcast_addr_v6;
140 gboolean have_ipv4_mcast;
141 gboolean have_ipv6_mcast;
143 gchar *multicast_iface;
145 /* the caps of the stream */
149 /* transports we stream to */
152 guint transports_cookie;
154 GList *tr_cache_rtcp;
155 guint tr_cache_cookie_rtp;
156 guint tr_cache_cookie_rtcp;
161 /* stream blocking */
165 /* pt->caps map for RECORD streams */
168 GstRTSPPublishClockMode publish_clock_mode;
171 #define DEFAULT_CONTROL NULL
172 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
173 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
174 GST_RTSP_LOWER_TRANS_TCP
187 SIGNAL_NEW_RTP_ENCODER,
188 SIGNAL_NEW_RTCP_ENCODER,
192 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
193 #define GST_CAT_DEFAULT rtsp_stream_debug
195 static GQuark ssrc_stream_map_key;
197 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
198 GValue * value, GParamSpec * pspec);
199 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
200 const GValue * value, GParamSpec * pspec);
202 static void gst_rtsp_stream_finalize (GObject * obj);
204 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
206 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
209 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
211 GObjectClass *gobject_class;
213 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
215 gobject_class = G_OBJECT_CLASS (klass);
217 gobject_class->get_property = gst_rtsp_stream_get_property;
218 gobject_class->set_property = gst_rtsp_stream_set_property;
219 gobject_class->finalize = gst_rtsp_stream_finalize;
221 g_object_class_install_property (gobject_class, PROP_CONTROL,
222 g_param_spec_string ("control", "Control",
223 "The control string for this stream", DEFAULT_CONTROL,
224 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 g_object_class_install_property (gobject_class, PROP_PROFILES,
227 g_param_spec_flags ("profiles", "Profiles",
228 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
229 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
231 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
232 g_param_spec_flags ("protocols", "Protocols",
233 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
234 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
237 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
238 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
239 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
241 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
242 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
243 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
244 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
246 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
248 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
252 gst_rtsp_stream_init (GstRTSPStream * stream)
254 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
256 GST_DEBUG ("new stream %p", stream);
261 priv->control = g_strdup (DEFAULT_CONTROL);
262 priv->profiles = DEFAULT_PROFILES;
263 priv->protocols = DEFAULT_PROTOCOLS;
264 priv->publish_clock_mode = GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK;
266 g_mutex_init (&priv->lock);
268 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
269 NULL, (GDestroyNotify) gst_caps_unref);
270 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
271 (GDestroyNotify) gst_caps_unref);
275 gst_rtsp_stream_finalize (GObject * obj)
277 GstRTSPStream *stream;
278 GstRTSPStreamPrivate *priv;
280 stream = GST_RTSP_STREAM (obj);
283 GST_DEBUG ("finalize stream %p", stream);
285 /* we really need to be unjoined now */
286 g_return_if_fail (priv->joined_bin == NULL);
288 if (priv->mcast_addr_v4)
289 gst_rtsp_address_free (priv->mcast_addr_v4);
290 if (priv->mcast_addr_v6)
291 gst_rtsp_address_free (priv->mcast_addr_v6);
292 if (priv->server_addr_v4)
293 gst_rtsp_address_free (priv->server_addr_v4);
294 if (priv->server_addr_v6)
295 gst_rtsp_address_free (priv->server_addr_v6);
297 g_object_unref (priv->pool);
299 g_object_unref (priv->rtxsend);
301 g_free (priv->multicast_iface);
303 gst_object_unref (priv->payloader);
305 gst_object_unref (priv->srcpad);
307 gst_object_unref (priv->sinkpad);
308 g_free (priv->control);
309 g_mutex_clear (&priv->lock);
311 g_hash_table_unref (priv->keys);
312 g_hash_table_destroy (priv->ptmap);
314 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
318 gst_rtsp_stream_get_property (GObject * object, guint propid,
319 GValue * value, GParamSpec * pspec)
321 GstRTSPStream *stream = GST_RTSP_STREAM (object);
325 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
328 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
331 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
334 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
339 gst_rtsp_stream_set_property (GObject * object, guint propid,
340 const GValue * value, GParamSpec * pspec)
342 GstRTSPStream *stream = GST_RTSP_STREAM (object);
346 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
349 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
352 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
355 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
360 * gst_rtsp_stream_new:
363 * @payloader: a #GstElement
365 * Create a new media stream with index @idx that handles RTP data on
366 * @pad and has a payloader element @payloader if @pad is a source pad
367 * or a depayloader element @payloader if @pad is a sink pad.
369 * Returns: (transfer full): a new #GstRTSPStream
372 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
374 GstRTSPStreamPrivate *priv;
375 GstRTSPStream *stream;
377 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
378 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
380 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
383 priv->payloader = gst_object_ref (payloader);
384 if (GST_PAD_IS_SRC (pad))
385 priv->srcpad = gst_object_ref (pad);
387 priv->sinkpad = gst_object_ref (pad);
393 * gst_rtsp_stream_get_index:
394 * @stream: a #GstRTSPStream
396 * Get the stream index.
398 * Return: the stream index.
401 gst_rtsp_stream_get_index (GstRTSPStream * stream)
403 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
405 return stream->priv->idx;
409 * gst_rtsp_stream_get_pt:
410 * @stream: a #GstRTSPStream
412 * Get the stream payload type.
414 * Return: the stream payload type.
417 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
419 GstRTSPStreamPrivate *priv;
422 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
426 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
432 * gst_rtsp_stream_get_srcpad:
433 * @stream: a #GstRTSPStream
435 * Get the srcpad associated with @stream.
437 * Returns: (transfer full): the srcpad. Unref after usage.
440 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
442 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
444 if (!stream->priv->srcpad)
447 return gst_object_ref (stream->priv->srcpad);
451 * gst_rtsp_stream_get_sinkpad:
452 * @stream: a #GstRTSPStream
454 * Get the sinkpad associated with @stream.
456 * Returns: (transfer full): the sinkpad. Unref after usage.
459 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
461 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
463 if (!stream->priv->sinkpad)
466 return gst_object_ref (stream->priv->sinkpad);
470 * gst_rtsp_stream_get_control:
471 * @stream: a #GstRTSPStream
473 * Get the control string to identify this stream.
475 * Returns: (transfer full): the control string. g_free() after usage.
478 gst_rtsp_stream_get_control (GstRTSPStream * stream)
480 GstRTSPStreamPrivate *priv;
483 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
487 g_mutex_lock (&priv->lock);
488 if ((result = g_strdup (priv->control)) == NULL)
489 result = g_strdup_printf ("stream=%u", priv->idx);
490 g_mutex_unlock (&priv->lock);
496 * gst_rtsp_stream_set_control:
497 * @stream: a #GstRTSPStream
498 * @control: a control string
500 * Set the control string in @stream.
503 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
505 GstRTSPStreamPrivate *priv;
507 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
511 g_mutex_lock (&priv->lock);
512 g_free (priv->control);
513 priv->control = g_strdup (control);
514 g_mutex_unlock (&priv->lock);
518 * gst_rtsp_stream_has_control:
519 * @stream: a #GstRTSPStream
520 * @control: a control string
522 * Check if @stream has the control string @control.
524 * Returns: %TRUE is @stream has @control as the control string
527 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
529 GstRTSPStreamPrivate *priv;
532 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
536 g_mutex_lock (&priv->lock);
538 res = (g_strcmp0 (priv->control, control) == 0);
542 if (sscanf (control, "stream=%u", &streamid) > 0)
543 res = (streamid == priv->idx);
547 g_mutex_unlock (&priv->lock);
553 * gst_rtsp_stream_set_mtu:
554 * @stream: a #GstRTSPStream
557 * Configure the mtu in the payloader of @stream to @mtu.
560 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
562 GstRTSPStreamPrivate *priv;
564 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
568 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
570 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
574 * gst_rtsp_stream_get_mtu:
575 * @stream: a #GstRTSPStream
577 * Get the configured MTU in the payloader of @stream.
579 * Returns: the MTU of the payloader.
582 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
584 GstRTSPStreamPrivate *priv;
587 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
591 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
596 /* Update the dscp qos property on the udp sinks */
598 update_dscp_qos (GstRTSPStream * stream)
600 GstRTSPStreamPrivate *priv;
602 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
606 if (priv->udpsink[0]) {
607 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
611 if (priv->udpsink[1]) {
612 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
618 * gst_rtsp_stream_set_dscp_qos:
619 * @stream: a #GstRTSPStream
620 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
622 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
625 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
627 GstRTSPStreamPrivate *priv;
629 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
633 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
635 if (dscp_qos < -1 || dscp_qos > 63) {
636 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
640 priv->dscp_qos = dscp_qos;
642 update_dscp_qos (stream);
646 * gst_rtsp_stream_get_dscp_qos:
647 * @stream: a #GstRTSPStream
649 * Get the configured DSCP QoS in of the outgoing sockets.
651 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
654 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
656 GstRTSPStreamPrivate *priv;
658 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
662 return priv->dscp_qos;
666 * gst_rtsp_stream_is_transport_supported:
667 * @stream: a #GstRTSPStream
668 * @transport: (transfer none): a #GstRTSPTransport
670 * Check if @transport can be handled by stream
672 * Returns: %TRUE if @transport can be handled by @stream.
675 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
676 GstRTSPTransport * transport)
678 GstRTSPStreamPrivate *priv;
680 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
684 g_mutex_lock (&priv->lock);
685 if (transport->trans != GST_RTSP_TRANS_RTP)
686 goto unsupported_transmode;
688 if (!(transport->profile & priv->profiles))
689 goto unsupported_profile;
691 if (!(transport->lower_transport & priv->protocols))
692 goto unsupported_ltrans;
694 g_mutex_unlock (&priv->lock);
699 unsupported_transmode:
701 GST_DEBUG ("unsupported transport mode %d", transport->trans);
702 g_mutex_unlock (&priv->lock);
707 GST_DEBUG ("unsupported profile %d", transport->profile);
708 g_mutex_unlock (&priv->lock);
713 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
714 g_mutex_unlock (&priv->lock);
720 * gst_rtsp_stream_set_profiles:
721 * @stream: a #GstRTSPStream
722 * @profiles: the new profiles
724 * Configure the allowed profiles for @stream.
727 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
729 GstRTSPStreamPrivate *priv;
731 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
735 g_mutex_lock (&priv->lock);
736 priv->profiles = profiles;
737 g_mutex_unlock (&priv->lock);
741 * gst_rtsp_stream_get_profiles:
742 * @stream: a #GstRTSPStream
744 * Get the allowed profiles of @stream.
746 * Returns: a #GstRTSPProfile
749 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
751 GstRTSPStreamPrivate *priv;
754 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
758 g_mutex_lock (&priv->lock);
759 res = priv->profiles;
760 g_mutex_unlock (&priv->lock);
766 * gst_rtsp_stream_set_protocols:
767 * @stream: a #GstRTSPStream
768 * @protocols: the new flags
770 * Configure the allowed lower transport for @stream.
773 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
774 GstRTSPLowerTrans protocols)
776 GstRTSPStreamPrivate *priv;
778 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
782 g_mutex_lock (&priv->lock);
783 priv->protocols = protocols;
784 g_mutex_unlock (&priv->lock);
788 * gst_rtsp_stream_get_protocols:
789 * @stream: a #GstRTSPStream
791 * Get the allowed protocols of @stream.
793 * Returns: a #GstRTSPLowerTrans
796 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
798 GstRTSPStreamPrivate *priv;
799 GstRTSPLowerTrans res;
801 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
802 GST_RTSP_LOWER_TRANS_UNKNOWN);
806 g_mutex_lock (&priv->lock);
807 res = priv->protocols;
808 g_mutex_unlock (&priv->lock);
814 * gst_rtsp_stream_set_address_pool:
815 * @stream: a #GstRTSPStream
816 * @pool: (transfer none): a #GstRTSPAddressPool
818 * configure @pool to be used as the address pool of @stream.
821 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
822 GstRTSPAddressPool * pool)
824 GstRTSPStreamPrivate *priv;
825 GstRTSPAddressPool *old;
827 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
831 GST_LOG_OBJECT (stream, "set address pool %p", pool);
833 g_mutex_lock (&priv->lock);
834 if ((old = priv->pool) != pool)
835 priv->pool = pool ? g_object_ref (pool) : NULL;
838 g_mutex_unlock (&priv->lock);
841 g_object_unref (old);
845 * gst_rtsp_stream_get_address_pool:
846 * @stream: a #GstRTSPStream
848 * Get the #GstRTSPAddressPool used as the address pool of @stream.
850 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
854 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
856 GstRTSPStreamPrivate *priv;
857 GstRTSPAddressPool *result;
859 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
863 g_mutex_lock (&priv->lock);
864 if ((result = priv->pool))
865 g_object_ref (result);
866 g_mutex_unlock (&priv->lock);
872 * gst_rtsp_stream_set_multicast_iface:
873 * @stream: a #GstRTSPStream
874 * @multicast_iface: (transfer none): a multicast interface
876 * configure @multicast_iface to be used for @stream.
879 gst_rtsp_stream_set_multicast_iface (GstRTSPStream * stream,
880 const gchar * multicast_iface)
882 GstRTSPStreamPrivate *priv;
885 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
889 GST_LOG_OBJECT (stream, "set multicast iface %s",
890 GST_STR_NULL (multicast_iface));
892 g_mutex_lock (&priv->lock);
893 if ((old = priv->multicast_iface) != multicast_iface)
894 priv->multicast_iface = multicast_iface ? g_strdup (multicast_iface) : NULL;
897 g_mutex_unlock (&priv->lock);
904 * gst_rtsp_stream_get_multicast_iface:
905 * @stream: a #GstRTSPStream
907 * Get the multicast interface used for @stream.
909 * Returns: (transfer full): the multicast interface for @stream. g_free() after
913 gst_rtsp_stream_get_multicast_iface (GstRTSPStream * stream)
915 GstRTSPStreamPrivate *priv;
918 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
922 g_mutex_lock (&priv->lock);
923 if ((result = priv->multicast_iface))
924 result = g_strdup (result);
925 g_mutex_unlock (&priv->lock);
931 * gst_rtsp_stream_get_multicast_address:
932 * @stream: a #GstRTSPStream
933 * @family: the #GSocketFamily
935 * Get the multicast address of @stream for @family. The original
936 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
937 * won't release the address from the pool.
939 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
940 * or %NULL when no address could be allocated. gst_rtsp_address_free()
944 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
945 GSocketFamily family)
947 GstRTSPStreamPrivate *priv;
948 GstRTSPAddress *result;
949 GstRTSPAddress **addrp;
950 GstRTSPAddressFlags flags;
952 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
956 if (family == G_SOCKET_FAMILY_IPV6) {
957 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
958 addrp = &priv->mcast_addr_v6;
960 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
961 addrp = &priv->mcast_addr_v4;
964 g_mutex_lock (&priv->lock);
965 if (*addrp == NULL) {
966 if (priv->pool == NULL)
969 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
971 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
975 result = gst_rtsp_address_copy (*addrp);
976 g_mutex_unlock (&priv->lock);
983 GST_ERROR_OBJECT (stream, "no address pool specified");
984 g_mutex_unlock (&priv->lock);
989 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
990 g_mutex_unlock (&priv->lock);
996 * gst_rtsp_stream_reserve_address:
997 * @stream: a #GstRTSPStream
998 * @address: an address
1003 * Reserve @address and @port as the address and port of @stream. The original
1004 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
1005 * won't release the address from the pool.
1007 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
1008 * the address could be reserved. gst_rtsp_address_free() after usage.
1011 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
1012 const gchar * address, guint port, guint n_ports, guint ttl)
1014 GstRTSPStreamPrivate *priv;
1015 GstRTSPAddress *result;
1017 GSocketFamily family;
1018 GstRTSPAddress **addrp;
1020 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1021 g_return_val_if_fail (address != NULL, NULL);
1022 g_return_val_if_fail (port > 0, NULL);
1023 g_return_val_if_fail (n_ports > 0, NULL);
1024 g_return_val_if_fail (ttl > 0, NULL);
1026 priv = stream->priv;
1028 addr = g_inet_address_new_from_string (address);
1030 GST_ERROR ("failed to get inet addr from %s", address);
1031 family = G_SOCKET_FAMILY_IPV4;
1033 family = g_inet_address_get_family (addr);
1034 g_object_unref (addr);
1037 if (family == G_SOCKET_FAMILY_IPV6)
1038 addrp = &priv->mcast_addr_v6;
1040 addrp = &priv->mcast_addr_v4;
1042 g_mutex_lock (&priv->lock);
1043 if (*addrp == NULL) {
1044 GstRTSPAddressPoolResult res;
1046 if (priv->pool == NULL)
1049 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
1050 port, n_ports, ttl, addrp);
1051 if (res != GST_RTSP_ADDRESS_POOL_OK)
1054 if (strcmp ((*addrp)->address, address) ||
1055 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
1056 (*addrp)->ttl != ttl)
1057 goto different_address;
1059 result = gst_rtsp_address_copy (*addrp);
1060 g_mutex_unlock (&priv->lock);
1067 GST_ERROR_OBJECT (stream, "no address pool specified");
1068 g_mutex_unlock (&priv->lock);
1073 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1075 g_mutex_unlock (&priv->lock);
1080 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
1081 " reserved", address);
1082 g_mutex_unlock (&priv->lock);
1087 /* must be called with lock */
1089 set_sockets_for_udpsinks (GstRTSPStream * stream, GSocket * rtp_socket,
1090 GSocket * rtcp_socket, GSocketFamily family)
1092 GstRTSPStreamPrivate *priv = stream->priv;
1093 const gchar *multisink_socket;
1095 if (family == G_SOCKET_FAMILY_IPV6)
1096 multisink_socket = "socket-v6";
1098 multisink_socket = "socket";
1100 g_object_set (G_OBJECT (priv->udpsink[0]), multisink_socket, rtp_socket,
1102 g_object_set (G_OBJECT (priv->udpsink[1]), multisink_socket, rtcp_socket,
1106 /* must be called with lock */
1108 create_and_configure_udpsinks (GstRTSPStream * stream)
1110 GstRTSPStreamPrivate *priv = stream->priv;
1111 GstElement *udpsink0, *udpsink1;
1116 if (priv->udpsink[0])
1117 udpsink0 = priv->udpsink[0];
1119 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1122 goto no_udp_protocol;
1124 if (priv->udpsink[1])
1125 udpsink1 = priv->udpsink[1];
1127 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1130 goto no_udp_protocol;
1132 /* configure sinks */
1134 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1135 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1137 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1138 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1140 g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
1142 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1143 /* Needs to be async for RECORD streams, otherwise we will never go to
1144 * PLAYING because the sinks will wait for data while the udpsrc can't
1145 * provide data with timestamps in PAUSED. */
1147 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1148 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1150 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1151 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1153 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1154 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1156 /* update the dscp qos field in the sinks */
1157 update_dscp_qos (stream);
1159 priv->udpsink[0] = udpsink0;
1160 priv->udpsink[1] = udpsink1;
1171 /* must be called with lock */
1173 play_udpsources_one_family (GstRTSPStream * stream, GstElement * udpsrc_out[2],
1174 GSocketFamily family)
1176 GstRTSPStreamPrivate *priv;
1177 GstPad *pad, *selpad;
1180 priv = stream->priv;
1182 for (i = 0; i < 2; i++) {
1183 if (!priv->sinkpad && i == 0) {
1184 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
1185 * RTCP sink always */
1190 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1191 * values. This is only relevant for PLAY pipelines */
1192 gst_element_set_state (udpsrc_out[i], GST_STATE_PLAYING);
1193 gst_element_set_locked_state (udpsrc_out[i], TRUE);
1197 gst_bin_add (priv->joined_bin, udpsrc_out[i]);
1199 /* and link to the funnel */
1200 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1201 pad = gst_element_get_static_pad (udpsrc_out[i], "src");
1202 gst_pad_link (pad, selpad);
1203 gst_object_unref (pad);
1204 gst_object_unref (selpad);
1206 /* otherwise sync state with parent in case it's running already
1208 if (!priv->srcpad) {
1209 gst_element_sync_state_with_parent (udpsrc_out[i]);
1214 /* must be called with lock */
1216 create_and_configure_udpsources_one_family (GstElement * udpsrc_out[2],
1217 GSocket * rtp_socket, GSocket * rtcp_socket, GSocketFamily family,
1218 const gchar * address, gint rtpport, gint rtcpport,
1219 const gchar * multicast_iface, GstRTSPLowerTrans transport)
1221 GstStateChangeReturn ret;
1223 udpsrc_out[0] = gst_element_factory_make ("udpsrc", NULL);
1224 udpsrc_out[1] = gst_element_factory_make ("udpsrc", NULL);
1226 if (udpsrc_out[0] == NULL || udpsrc_out[1] == NULL)
1229 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1230 g_object_set (G_OBJECT (udpsrc_out[0]), "address", address, NULL);
1231 g_object_set (G_OBJECT (udpsrc_out[1]), "address", address, NULL);
1232 g_object_set (G_OBJECT (udpsrc_out[0]), "port", rtpport, NULL);
1233 g_object_set (G_OBJECT (udpsrc_out[1]), "port", rtcpport, NULL);
1234 g_object_set (G_OBJECT (udpsrc_out[0]), "multicast-iface", multicast_iface,
1236 g_object_set (G_OBJECT (udpsrc_out[1]), "multicast-iface", multicast_iface,
1238 g_object_set (G_OBJECT (udpsrc_out[0]), "loop", FALSE, NULL);
1239 g_object_set (G_OBJECT (udpsrc_out[1]), "loop", FALSE, NULL);
1242 g_object_set (G_OBJECT (udpsrc_out[0]), "socket", rtp_socket, NULL);
1243 g_object_set (G_OBJECT (udpsrc_out[1]), "socket", rtcp_socket, NULL);
1245 ret = gst_element_set_state (udpsrc_out[0], GST_STATE_READY);
1246 if (ret == GST_STATE_CHANGE_FAILURE)
1248 ret = gst_element_set_state (udpsrc_out[1], GST_STATE_READY);
1249 if (ret == GST_STATE_CHANGE_FAILURE)
1257 if (udpsrc_out[0]) {
1258 gst_element_set_state (udpsrc_out[0], GST_STATE_NULL);
1259 g_clear_object (&udpsrc_out[0]);
1261 if (udpsrc_out[1]) {
1262 gst_element_set_state (udpsrc_out[1], GST_STATE_NULL);
1263 g_clear_object (&udpsrc_out[1]);
1270 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1271 GstElement * udpsrc_out[2], GstRTSPRange * server_port_out,
1272 GstRTSPTransport * ct, GstRTSPAddress ** server_addr_out,
1273 gboolean use_client_settings)
1275 GstRTSPStreamPrivate *priv = stream->priv;
1276 GSocket *rtp_socket = NULL;
1277 GSocket *rtcp_socket;
1278 gint tmp_rtp, tmp_rtcp;
1280 gint rtpport, rtcpport;
1281 GList *rejected_addresses = NULL;
1282 GstRTSPAddress *addr = NULL;
1283 GInetAddress *inetaddr = NULL;
1285 GSocketAddress *rtp_sockaddr = NULL;
1286 GSocketAddress *rtcp_sockaddr = NULL;
1287 GstRTSPAddressPool *pool;
1288 GstRTSPLowerTrans transport;
1289 const gchar *multicast_iface = priv->multicast_iface;
1293 transport = ct->lower_transport;
1295 /* Start with random port */
1298 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1299 G_SOCKET_PROTOCOL_UDP, NULL);
1301 goto no_udp_protocol;
1302 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1304 if (*server_addr_out)
1305 gst_rtsp_address_free (*server_addr_out);
1307 /* try to allocate 2 UDP ports, the RTP port should be an even
1308 * number and the RTCP port should be the next (uneven) port */
1311 if (rtp_socket == NULL) {
1312 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1313 G_SOCKET_PROTOCOL_UDP, NULL);
1315 goto no_udp_protocol;
1316 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1319 if (pool && ((transport == GST_RTSP_LOWER_TRANS_UDP &&
1320 gst_rtsp_address_pool_has_unicast_addresses (pool))
1321 || transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)) {
1322 GstRTSPAddressFlags flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1324 if (transport == GST_RTSP_LOWER_TRANS_UDP)
1325 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1327 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1330 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1332 if (family == G_SOCKET_FAMILY_IPV6)
1333 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1335 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1337 if (ct->destination && transport == GST_RTSP_LOWER_TRANS_UDP_MCAST
1338 && use_client_settings)
1339 gst_rtsp_address_pool_reserve_address (pool, ct->destination,
1340 ct->port.min, 2, ct->ttl, &addr);
1342 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1347 tmp_rtp = addr->port;
1349 g_clear_object (&inetaddr);
1350 inetaddr = g_inet_address_new_from_string (addr->address);
1352 /* If we're supposed to bind to a multicast address, instead bind
1353 * to ANY and let udpsrc later join the relevant multicast group
1355 if (g_inet_address_get_is_multicast (inetaddr)) {
1356 g_object_unref (inetaddr);
1357 inetaddr = g_inet_address_new_any (family);
1366 if (inetaddr == NULL)
1367 inetaddr = g_inet_address_new_any (family);
1370 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1371 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1372 g_object_unref (rtp_sockaddr);
1375 g_object_unref (rtp_sockaddr);
1377 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1378 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1379 g_clear_object (&rtp_sockaddr);
1384 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1385 g_object_unref (rtp_sockaddr);
1387 /* check if port is even */
1388 if ((tmp_rtp & 1) != 0) {
1389 /* port not even, close and allocate another */
1391 g_clear_object (&rtp_socket);
1396 tmp_rtcp = tmp_rtp + 1;
1398 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1399 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1400 g_object_unref (rtcp_sockaddr);
1401 g_clear_object (&rtp_socket);
1404 g_object_unref (rtcp_sockaddr);
1407 addr_str = g_inet_address_to_string (inetaddr);
1409 addr_str = addr->address;
1410 g_clear_object (&inetaddr);
1412 if (!create_and_configure_udpsources_one_family (udpsrc_out, rtp_socket,
1413 rtcp_socket, family, addr_str, tmp_rtp, tmp_rtcp, multicast_iface,
1417 goto no_udp_protocol;
1423 play_udpsources_one_family (stream, udpsrc_out, family);
1425 g_object_get (G_OBJECT (udpsrc_out[0]), "port", &rtpport, NULL);
1426 g_object_get (G_OBJECT (udpsrc_out[1]), "port", &rtcpport, NULL);
1428 /* this should not happen... */
1429 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1432 /* set RTP and RTCP sockets */
1433 set_sockets_for_udpsinks (stream, rtp_socket, rtcp_socket, family);
1435 server_port_out->min = rtpport;
1436 server_port_out->max = rtcpport;
1438 *server_addr_out = addr;
1439 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1441 g_object_unref (rtp_socket);
1442 g_object_unref (rtcp_socket);
1466 g_object_unref (inetaddr);
1467 g_list_free_full (rejected_addresses,
1468 (GDestroyNotify) gst_rtsp_address_free);
1470 gst_rtsp_address_free (addr);
1472 g_object_unref (rtp_socket);
1474 g_object_unref (rtcp_socket);
1480 * gst_rtsp_stream_allocate_udp_sockets:
1481 * @stream: a #GstRTSPStream
1482 * @family: protocol family
1483 * @transport_method: transport method
1485 * Allocates RTP and RTCP ports.
1487 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1490 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1491 GSocketFamily family, GstRTSPTransport * ct, gboolean use_client_settings)
1493 GstRTSPStreamPrivate *priv;
1494 gboolean result = FALSE;
1495 GstRTSPLowerTrans transport = ct->lower_transport;
1497 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1498 priv = stream->priv;
1499 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
1501 g_mutex_lock (&priv->lock);
1503 if (family == G_SOCKET_FAMILY_IPV4) {
1504 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1505 if (priv->have_ipv4_mcast)
1507 priv->have_ipv4_mcast =
1508 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1509 priv->udpsrc_mcast_v4, &priv->server_port_v4, ct,
1510 &priv->mcast_addr_v4, use_client_settings);
1513 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4,
1514 &priv->server_port_v4, ct, &priv->server_addr_v4,
1515 use_client_settings);
1518 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1519 if (priv->have_ipv6_mcast)
1521 priv->have_ipv6_mcast =
1522 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1523 priv->udpsrc_mcast_v6, &priv->server_port_v6, ct,
1524 &priv->mcast_addr_v6, use_client_settings);
1526 if (priv->have_ipv6)
1529 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6,
1530 &priv->server_port_v6, ct, &priv->server_addr_v6,
1531 use_client_settings);
1536 result = priv->have_ipv4 || priv->have_ipv4_mcast || priv->have_ipv6 ||
1537 priv->have_ipv6_mcast;
1539 g_mutex_unlock (&priv->lock);
1545 * gst_rtsp_stream_set_client_side:
1546 * @stream: a #GstRTSPStream
1547 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1548 * an RTSP connection.
1550 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1551 * streams to an RTSP server via RECORD. This has the practical effect
1552 * of changing which UDP port numbers are used when setting up the local
1553 * side of the stream sending to be either the 'server' or 'client' pair
1554 * of a configured UDP transport.
1557 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1559 GstRTSPStreamPrivate *priv;
1561 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1562 priv = stream->priv;
1563 g_mutex_lock (&priv->lock);
1564 priv->client_side = client_side;
1565 g_mutex_unlock (&priv->lock);
1569 * gst_rtsp_stream_is_client_side:
1570 * @stream: a #GstRTSPStream
1572 * See gst_rtsp_stream_set_client_side()
1574 * Returns: TRUE if this #GstRTSPStream is client-side.
1577 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1579 GstRTSPStreamPrivate *priv;
1582 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1584 priv = stream->priv;
1585 g_mutex_lock (&priv->lock);
1586 ret = priv->client_side;
1587 g_mutex_unlock (&priv->lock);
1593 * gst_rtsp_stream_get_server_port:
1594 * @stream: a #GstRTSPStream
1595 * @server_port: (out): result server port
1596 * @family: the port family to get
1598 * Fill @server_port with the port pair used by the server. This function can
1599 * only be called when @stream has been joined.
1602 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1603 GstRTSPRange * server_port, GSocketFamily family)
1605 GstRTSPStreamPrivate *priv;
1607 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1608 priv = stream->priv;
1609 g_return_if_fail (priv->joined_bin != NULL);
1611 g_mutex_lock (&priv->lock);
1612 if (family == G_SOCKET_FAMILY_IPV4) {
1614 *server_port = priv->server_port_v4;
1617 *server_port = priv->server_port_v6;
1619 g_mutex_unlock (&priv->lock);
1623 * gst_rtsp_stream_get_rtpsession:
1624 * @stream: a #GstRTSPStream
1626 * Get the RTP session of this stream.
1628 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1631 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1633 GstRTSPStreamPrivate *priv;
1636 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1638 priv = stream->priv;
1640 g_mutex_lock (&priv->lock);
1641 if ((session = priv->session))
1642 g_object_ref (session);
1643 g_mutex_unlock (&priv->lock);
1649 * gst_rtsp_stream_get_encoder:
1650 * @stream: a #GstRTSPStream
1652 * Get the SRTP encoder for this stream.
1654 * Returns: (transfer full): The SRTP encoder for this stream. Unref after usage.
1657 gst_rtsp_stream_get_srtp_encoder (GstRTSPStream * stream)
1659 GstRTSPStreamPrivate *priv;
1660 GstElement *encoder;
1662 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1664 priv = stream->priv;
1666 g_mutex_lock (&priv->lock);
1667 if ((encoder = priv->srtpenc))
1668 g_object_ref (encoder);
1669 g_mutex_unlock (&priv->lock);
1675 * gst_rtsp_stream_get_ssrc:
1676 * @stream: a #GstRTSPStream
1677 * @ssrc: (out): result ssrc
1679 * Get the SSRC used by the RTP session of this stream. This function can only
1680 * be called when @stream has been joined.
1683 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1685 GstRTSPStreamPrivate *priv;
1687 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1688 priv = stream->priv;
1689 g_return_if_fail (priv->joined_bin != NULL);
1691 g_mutex_lock (&priv->lock);
1692 if (ssrc && priv->session)
1693 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1694 g_mutex_unlock (&priv->lock);
1698 * gst_rtsp_stream_set_retransmission_time:
1699 * @stream: a #GstRTSPStream
1700 * @time: a #GstClockTime
1702 * Set the amount of time to store retransmission packets.
1705 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1708 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1710 g_mutex_lock (&stream->priv->lock);
1711 stream->priv->rtx_time = time;
1712 if (stream->priv->rtxsend)
1713 g_object_set (stream->priv->rtxsend, "max-size-time",
1714 GST_TIME_AS_MSECONDS (time), NULL);
1715 g_mutex_unlock (&stream->priv->lock);
1719 * gst_rtsp_stream_get_retransmission_time:
1720 * @stream: a #GstRTSPStream
1722 * Get the amount of time to store retransmission data.
1724 * Returns: the amount of time to store retransmission data.
1727 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1731 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1733 g_mutex_lock (&stream->priv->lock);
1734 ret = stream->priv->rtx_time;
1735 g_mutex_unlock (&stream->priv->lock);
1741 * gst_rtsp_stream_set_retransmission_pt:
1742 * @stream: a #GstRTSPStream
1745 * Set the payload type (pt) for retransmission of this stream.
1748 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1750 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1752 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1754 g_mutex_lock (&stream->priv->lock);
1755 stream->priv->rtx_pt = rtx_pt;
1756 if (stream->priv->rtxsend) {
1757 guint pt = gst_rtsp_stream_get_pt (stream);
1758 gchar *pt_s = g_strdup_printf ("%d", pt);
1759 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1760 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1761 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1763 gst_structure_free (rtx_pt_map);
1765 g_mutex_unlock (&stream->priv->lock);
1769 * gst_rtsp_stream_get_retransmission_pt:
1770 * @stream: a #GstRTSPStream
1772 * Get the payload-type used for retransmission of this stream
1774 * Returns: The retransmission PT.
1777 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1781 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1783 g_mutex_lock (&stream->priv->lock);
1784 rtx_pt = stream->priv->rtx_pt;
1785 g_mutex_unlock (&stream->priv->lock);
1791 * gst_rtsp_stream_set_buffer_size:
1792 * @stream: a #GstRTSPStream
1793 * @size: the buffer size
1795 * Set the size of the UDP transmission buffer (in bytes)
1796 * Needs to be set before the stream is joined to a bin.
1801 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1803 g_mutex_lock (&stream->priv->lock);
1804 stream->priv->buffer_size = size;
1805 g_mutex_unlock (&stream->priv->lock);
1809 * gst_rtsp_stream_get_buffer_size:
1810 * @stream: a #GstRTSPStream
1812 * Get the size of the UDP transmission buffer (in bytes)
1814 * Returns: the size of the UDP TX buffer
1819 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1823 g_mutex_lock (&stream->priv->lock);
1824 buffer_size = stream->priv->buffer_size;
1825 g_mutex_unlock (&stream->priv->lock);
1830 /* executed from streaming thread */
1832 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1834 GstRTSPStreamPrivate *priv = stream->priv;
1835 GstCaps *newcaps, *oldcaps;
1837 newcaps = gst_pad_get_current_caps (pad);
1839 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1842 g_mutex_lock (&priv->lock);
1843 oldcaps = priv->caps;
1844 priv->caps = newcaps;
1845 g_mutex_unlock (&priv->lock);
1848 gst_caps_unref (oldcaps);
1852 dump_structure (const GstStructure * s)
1856 sstr = gst_structure_to_string (s);
1857 GST_INFO ("structure: %s", sstr);
1861 static GstRTSPStreamTransport *
1862 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1864 GstRTSPStreamPrivate *priv = stream->priv;
1866 GstRTSPStreamTransport *result = NULL;
1871 if (rtcp_from == NULL)
1874 tmp = g_strrstr (rtcp_from, ":");
1878 port = atoi (tmp + 1);
1879 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1881 g_mutex_lock (&priv->lock);
1882 GST_INFO ("finding %s:%d in %d transports", dest, port,
1883 g_list_length (priv->transports));
1885 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1886 GstRTSPStreamTransport *trans = walk->data;
1887 const GstRTSPTransport *tr;
1890 tr = gst_rtsp_stream_transport_get_transport (trans);
1892 if (priv->client_side) {
1893 /* In client side mode the 'destination' is the RTSP server, so send
1895 min = tr->server_port.min;
1896 max = tr->server_port.max;
1898 min = tr->client_port.min;
1899 max = tr->client_port.max;
1902 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1908 g_object_ref (result);
1909 g_mutex_unlock (&priv->lock);
1916 static GstRTSPStreamTransport *
1917 check_transport (GObject * source, GstRTSPStream * stream)
1919 GstStructure *stats;
1920 GstRTSPStreamTransport *trans;
1922 /* see if we have a stream to match with the origin of the RTCP packet */
1923 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1924 if (trans == NULL) {
1925 g_object_get (source, "stats", &stats, NULL);
1927 const gchar *rtcp_from;
1929 dump_structure (stats);
1931 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1932 if ((trans = find_transport (stream, rtcp_from))) {
1933 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1935 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1938 gst_structure_free (stats);
1946 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1948 GstRTSPStreamTransport *trans;
1950 GST_INFO ("%p: new source %p", stream, source);
1952 trans = check_transport (source, stream);
1955 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1959 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1961 GST_INFO ("%p: new SDES %p", stream, source);
1965 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1967 GstRTSPStreamTransport *trans;
1969 trans = check_transport (source, stream);
1972 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1973 gst_rtsp_stream_transport_keep_alive (trans);
1977 GstStructure *stats;
1978 g_object_get (source, "stats", &stats, NULL);
1980 dump_structure (stats);
1981 gst_structure_free (stats);
1988 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1990 GST_INFO ("%p: source %p bye", stream, source);
1994 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1996 GstRTSPStreamTransport *trans;
1998 GST_INFO ("%p: source %p bye timeout", stream, source);
2000 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2001 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2002 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2007 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2009 GstRTSPStreamTransport *trans;
2011 GST_INFO ("%p: source %p timeout", stream, source);
2013 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2014 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2015 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2020 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2022 GST_INFO ("%p: new sender source %p", stream, source);
2025 GstStructure *stats;
2026 g_object_get (source, "stats", &stats, NULL);
2028 dump_structure (stats);
2029 gst_structure_free (stats);
2036 on_sender_ssrc_active (GObject * session, GObject * source,
2037 GstRTSPStream * stream)
2041 GstStructure *stats;
2042 g_object_get (source, "stats", &stats, NULL);
2044 dump_structure (stats);
2045 gst_structure_free (stats);
2052 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
2055 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
2056 g_list_free (priv->tr_cache_rtp);
2057 priv->tr_cache_rtp = NULL;
2059 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
2060 g_list_free (priv->tr_cache_rtcp);
2061 priv->tr_cache_rtcp = NULL;
2065 static GstFlowReturn
2066 handle_new_sample (GstAppSink * sink, gpointer user_data)
2068 GstRTSPStreamPrivate *priv;
2072 GstRTSPStream *stream;
2075 sample = gst_app_sink_pull_sample (sink);
2079 stream = (GstRTSPStream *) user_data;
2080 priv = stream->priv;
2081 buffer = gst_sample_get_buffer (sample);
2083 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
2085 g_mutex_lock (&priv->lock);
2087 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
2088 clear_tr_cache (priv, is_rtp);
2089 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2090 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2091 priv->tr_cache_rtp =
2092 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
2094 priv->tr_cache_cookie_rtp = priv->transports_cookie;
2097 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
2098 clear_tr_cache (priv, is_rtp);
2099 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2100 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2101 priv->tr_cache_rtcp =
2102 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
2104 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
2107 g_mutex_unlock (&priv->lock);
2110 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2111 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2112 gst_rtsp_stream_transport_send_rtp (tr, buffer);
2115 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2116 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2117 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
2120 gst_sample_unref (sample);
2125 static GstAppSinkCallbacks sink_cb = {
2126 NULL, /* not interested in EOS */
2127 NULL, /* not interested in preroll samples */
2132 get_rtp_encoder (GstRTSPStream * stream, guint session)
2134 GstRTSPStreamPrivate *priv = stream->priv;
2136 if (priv->srtpenc == NULL) {
2139 name = g_strdup_printf ("srtpenc_%u", session);
2140 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2143 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2145 return gst_object_ref (priv->srtpenc);
2149 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2151 GstRTSPStreamPrivate *priv = stream->priv;
2152 GstElement *oldenc, *enc;
2156 if (priv->idx != session)
2159 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2161 oldenc = priv->srtpenc;
2162 enc = get_rtp_encoder (stream, session);
2163 name = g_strdup_printf ("rtp_sink_%d", session);
2164 pad = gst_element_get_request_pad (enc, name);
2166 gst_object_unref (pad);
2169 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2176 request_rtcp_encoder (GstElement * rtpbin, guint session,
2177 GstRTSPStream * stream)
2179 GstRTSPStreamPrivate *priv = stream->priv;
2180 GstElement *oldenc, *enc;
2184 if (priv->idx != session)
2187 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2189 oldenc = priv->srtpenc;
2190 enc = get_rtp_encoder (stream, session);
2191 name = g_strdup_printf ("rtcp_sink_%d", session);
2192 pad = gst_element_get_request_pad (enc, name);
2194 gst_object_unref (pad);
2197 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2204 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2206 GstRTSPStreamPrivate *priv = stream->priv;
2209 GST_DEBUG ("request key %08x", ssrc);
2211 g_mutex_lock (&priv->lock);
2212 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2213 gst_caps_ref (caps);
2214 g_mutex_unlock (&priv->lock);
2220 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2221 GstRTSPStream * stream)
2223 GstRTSPStreamPrivate *priv = stream->priv;
2225 if (priv->idx != session)
2228 if (priv->srtpdec == NULL) {
2231 name = g_strdup_printf ("srtpdec_%u", session);
2232 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2235 g_signal_connect (priv->srtpdec, "request-key",
2236 (GCallback) request_key, stream);
2238 return gst_object_ref (priv->srtpdec);
2242 * gst_rtsp_stream_request_aux_sender:
2243 * @stream: a #GstRTSPStream
2244 * @sessid: the session id
2246 * Creating a rtxsend bin
2248 * Returns: (transfer full): a #GstElement.
2253 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2257 GstStructure *pt_map;
2262 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2264 pt = gst_rtsp_stream_get_pt (stream);
2265 pt_s = g_strdup_printf ("%u", pt);
2266 rtx_pt = stream->priv->rtx_pt;
2268 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2270 bin = gst_bin_new (NULL);
2271 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2272 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2273 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2274 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2275 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2277 gst_structure_free (pt_map);
2278 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2280 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2281 name = g_strdup_printf ("src_%u", sessid);
2282 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2284 gst_object_unref (pad);
2286 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2287 name = g_strdup_printf ("sink_%u", sessid);
2288 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2290 gst_object_unref (pad);
2296 * gst_rtsp_stream_set_pt_map:
2297 * @stream: a #GstRTSPStream
2301 * Configure a pt map between @pt and @caps.
2304 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2306 GstRTSPStreamPrivate *priv = stream->priv;
2308 g_mutex_lock (&priv->lock);
2309 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2310 g_mutex_unlock (&priv->lock);
2314 * gst_rtsp_stream_set_publish_clock_mode:
2315 * @stream: a #GstRTSPStream
2316 * @mode: the clock publish mode
2318 * Sets if and how the stream clock should be published according to RFC7273.
2323 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
2324 GstRTSPPublishClockMode mode)
2326 GstRTSPStreamPrivate *priv;
2328 priv = stream->priv;
2329 g_mutex_lock (&priv->lock);
2330 priv->publish_clock_mode = mode;
2331 g_mutex_unlock (&priv->lock);
2335 * gst_rtsp_stream_get_publish_clock_mode:
2336 * @factory: a #GstRTSPStream
2338 * Gets if and how the stream clock should be published according to RFC7273.
2340 * Returns: The GstRTSPPublishClockMode
2344 GstRTSPPublishClockMode
2345 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
2347 GstRTSPStreamPrivate *priv;
2348 GstRTSPPublishClockMode ret;
2350 priv = stream->priv;
2351 g_mutex_lock (&priv->lock);
2352 ret = priv->publish_clock_mode;
2353 g_mutex_unlock (&priv->lock);
2359 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2360 GstRTSPStream * stream)
2362 GstRTSPStreamPrivate *priv = stream->priv;
2363 GstCaps *caps = NULL;
2365 g_mutex_lock (&priv->lock);
2367 if (priv->idx == session) {
2368 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2370 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2371 gst_caps_ref (caps);
2373 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2377 g_mutex_unlock (&priv->lock);
2383 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2385 GstRTSPStreamPrivate *priv = stream->priv;
2387 GstPadLinkReturn ret;
2390 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2391 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2393 name = gst_pad_get_name (pad);
2394 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2400 if (priv->idx != sessid)
2403 if (gst_pad_is_linked (priv->sinkpad)) {
2404 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2405 GST_DEBUG_PAD_NAME (priv->sinkpad));
2409 /* link the RTP pad to the session manager, it should not really fail unless
2410 * this is not really an RTP pad */
2411 ret = gst_pad_link (pad, priv->sinkpad);
2412 if (ret != GST_PAD_LINK_OK)
2414 priv->recv_rtp_src = gst_object_ref (pad);
2421 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2422 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2427 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2428 GstRTSPStream * stream)
2430 /* TODO: What to do here other than this? */
2431 GST_DEBUG ("Stream %p: Got EOS", stream);
2432 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2436 plug_sink (GstBin * bin, GstElement * tee, GstElement * sink,
2437 GstElement ** queue_out)
2443 gst_bin_add (bin, sink);
2445 *queue_out = gst_element_factory_make ("queue", NULL);
2446 g_object_set (*queue_out, "max-size-buffers", 1, "max-size-bytes", 0,
2447 "max-size-time", G_GINT64_CONSTANT (0), NULL);
2448 gst_bin_add (bin, *queue_out);
2450 /* link tee to queue */
2451 teepad = gst_element_get_request_pad (tee, "src_%u");
2452 pad = gst_element_get_static_pad (*queue_out, "sink");
2453 gst_pad_link (teepad, pad);
2454 gst_object_unref (pad);
2455 gst_object_unref (teepad);
2457 /* link queue to sink */
2458 queuepad = gst_element_get_static_pad (*queue_out, "src");
2459 pad = gst_element_get_static_pad (sink, "sink");
2460 gst_pad_link (queuepad, pad);
2461 gst_object_unref (queuepad);
2462 gst_object_unref (pad);
2465 /* must be called with lock */
2467 create_sender_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2469 GstRTSPStreamPrivate *priv;
2471 gboolean is_tcp = FALSE, is_udp = FALSE;
2474 priv = stream->priv;
2476 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2477 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2478 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2480 if (is_udp && !create_and_configure_udpsinks (stream))
2481 goto no_udp_protocol;
2483 for (i = 0; i < 2; i++) {
2484 /* For the sender we create this bit of pipeline for both
2485 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2486 * we need to add a queue before appsink and udpsink to make
2487 * the pipeline not block. For the TCP case, we want to pump
2488 * client as fast as possible anyway. This pipeline is used
2489 * when both TCP and UDP are present.
2491 * .--------. .-----. .---------. .---------.
2492 * | rtpbin | | tee | | queue | | udpsink |
2493 * | send->sink src->sink src->sink |
2494 * '--------' | | '---------' '---------'
2495 * | | .---------. .---------.
2496 * | | | queue | | appsink |
2497 * | src->sink src->sink |
2498 * '-----' '---------' '---------'
2500 * When only UDP or only TCP is allowed, we skip the tee and queue
2501 * and link the udpsink (for UDP) or appsink (for TCP) directly to
2505 /* Only link the RTP send src if we're going to send RTP, link
2506 * the RTCP send src always */
2507 if (!priv->srcpad && i == 0)
2512 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2513 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2514 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2515 &sink_cb, stream, NULL);
2518 if (is_udp && is_tcp) {
2519 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2521 /* make tee for RTP/RTCP */
2522 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2523 gst_bin_add (bin, priv->tee[i]);
2525 /* and link to rtpbin send pad */
2526 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2527 gst_pad_link (priv->send_src[i], pad);
2528 gst_object_unref (pad);
2530 plug_sink (bin, priv->tee[i], priv->udpsink[i], &priv->udpqueue[i]);
2531 plug_sink (bin, priv->tee[i], priv->appsink[i], &priv->appqueue[i]);
2532 } else if (is_tcp) {
2533 /* only appsink needed, link it to the session */
2534 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2535 gst_pad_link (priv->send_src[i], pad);
2536 gst_object_unref (pad);
2538 /* when its only TCP, we need to set sync and preroll to FALSE
2539 * for the sink to avoid deadlock. And this is only needed for
2540 * sink used for RTCP data, not the RTP data. */
2542 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2544 /* else only udpsink needed, link it to the session */
2545 pad = gst_element_get_static_pad (priv->udpsink[i], "sink");
2546 gst_pad_link (priv->send_src[i], pad);
2547 gst_object_unref (pad);
2550 /* check if we need to set to a special state */
2551 if (state != GST_STATE_NULL) {
2552 if (priv->udpsink[i])
2553 gst_element_set_state (priv->udpsink[i], state);
2554 if (priv->appsink[i])
2555 gst_element_set_state (priv->appsink[i], state);
2556 if (priv->appqueue[i])
2557 gst_element_set_state (priv->appqueue[i], state);
2558 if (priv->udpqueue[i])
2559 gst_element_set_state (priv->udpqueue[i], state);
2561 gst_element_set_state (priv->tee[i], state);
2574 /* must be called with lock */
2576 create_receiver_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2578 GstRTSPStreamPrivate *priv;
2579 GstPad *pad, *selpad;
2583 priv = stream->priv;
2585 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2587 for (i = 0; i < 2; i++) {
2588 /* For the receiver we create this bit of pipeline for both
2589 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2590 * and it is all funneled into the rtpbin receive pad.
2592 * .--------. .--------. .--------.
2593 * | udpsrc | | funnel | | rtpbin |
2594 * | src->sink src->sink |
2595 * '--------' | | '--------'
2599 * '--------' '--------'
2602 if (!priv->sinkpad && i == 0) {
2603 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2604 * RTCP sink always */
2608 /* make funnel for the RTP/RTCP receivers */
2609 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2610 gst_bin_add (bin, priv->funnel[i]);
2612 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2613 gst_pad_link (pad, priv->recv_sink[i]);
2614 gst_object_unref (pad);
2616 if (priv->udpsrc_v4[i]) {
2618 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2619 * values. This is only relevant for PLAY pipelines */
2620 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
2621 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
2624 gst_bin_add (bin, priv->udpsrc_v4[i]);
2626 /* and link to the funnel v4 */
2627 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2628 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
2629 gst_pad_link (pad, selpad);
2630 gst_object_unref (pad);
2631 gst_object_unref (selpad);
2634 if (priv->udpsrc_v6[i]) {
2636 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
2637 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
2639 gst_bin_add (bin, priv->udpsrc_v6[i]);
2641 /* and link to the funnel v6 */
2642 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2643 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2644 gst_pad_link (pad, selpad);
2645 gst_object_unref (pad);
2646 gst_object_unref (selpad);
2650 /* make and add appsrc */
2651 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2652 priv->appsrc_base_time[i] = -1;
2654 gst_element_set_state (priv->appsrc[i], GST_STATE_PLAYING);
2655 gst_element_set_locked_state (priv->appsrc[i], TRUE);
2657 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
2659 gst_bin_add (bin, priv->appsrc[i]);
2660 /* and link to the funnel */
2661 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2662 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2663 gst_pad_link (pad, selpad);
2664 gst_object_unref (pad);
2665 gst_object_unref (selpad);
2668 /* check if we need to set to a special state */
2669 if (state != GST_STATE_NULL) {
2670 gst_element_set_state (priv->funnel[i], state);
2676 * gst_rtsp_stream_join_bin:
2677 * @stream: a #GstRTSPStream
2678 * @bin: (transfer none): a #GstBin to join
2679 * @rtpbin: (transfer none): a rtpbin element in @bin
2680 * @state: the target state of the new elements
2682 * Join the #GstBin @bin that contains the element @rtpbin.
2684 * @stream will link to @rtpbin, which must be inside @bin. The elements
2685 * added to @bin will be set to the state given in @state.
2687 * Returns: %TRUE on success.
2690 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2691 GstElement * rtpbin, GstState state)
2693 GstRTSPStreamPrivate *priv;
2696 GstPadLinkReturn ret;
2698 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2699 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2700 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2702 priv = stream->priv;
2704 g_mutex_lock (&priv->lock);
2705 if (priv->joined_bin != NULL)
2708 /* create a session with the same index as the stream */
2711 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2713 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2714 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2716 g_signal_connect (rtpbin, "request-rtp-encoder",
2717 (GCallback) request_rtp_encoder, stream);
2718 g_signal_connect (rtpbin, "request-rtcp-encoder",
2719 (GCallback) request_rtcp_encoder, stream);
2720 g_signal_connect (rtpbin, "request-rtp-decoder",
2721 (GCallback) request_rtp_rtcp_decoder, stream);
2722 g_signal_connect (rtpbin, "request-rtcp-decoder",
2723 (GCallback) request_rtp_rtcp_decoder, stream);
2726 if (priv->sinkpad) {
2727 g_signal_connect (rtpbin, "request-pt-map",
2728 (GCallback) request_pt_map, stream);
2731 /* get pads from the RTP session element for sending and receiving
2734 /* get a pad for sending RTP */
2735 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2736 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2739 /* link the RTP pad to the session manager, it should not really fail unless
2740 * this is not really an RTP pad */
2741 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2742 if (ret != GST_PAD_LINK_OK)
2745 name = g_strdup_printf ("send_rtp_src_%u", idx);
2746 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2749 /* Need to connect our sinkpad from here */
2750 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2752 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2754 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2755 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2759 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2760 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2762 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2763 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2766 /* get the session */
2767 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2769 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2771 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2773 g_signal_connect (priv->session, "on-ssrc-active",
2774 (GCallback) on_ssrc_active, stream);
2775 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2777 g_signal_connect (priv->session, "on-bye-timeout",
2778 (GCallback) on_bye_timeout, stream);
2779 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2782 /* signal for sender ssrc */
2783 g_signal_connect (priv->session, "on-new-sender-ssrc",
2784 (GCallback) on_new_sender_ssrc, stream);
2785 g_signal_connect (priv->session, "on-sender-ssrc-active",
2786 (GCallback) on_sender_ssrc_active, stream);
2788 if (!create_sender_part (stream, bin, state))
2789 goto no_udp_protocol;
2791 create_receiver_part (stream, bin, state);
2794 /* be notified of caps changes */
2795 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2796 (GCallback) caps_notify, stream);
2799 priv->joined_bin = gst_object_ref (bin);
2800 g_mutex_unlock (&priv->lock);
2807 g_mutex_unlock (&priv->lock);
2812 GST_WARNING ("failed to link stream %u", idx);
2813 gst_object_unref (priv->send_rtp_sink);
2814 priv->send_rtp_sink = NULL;
2815 g_mutex_unlock (&priv->lock);
2820 GST_WARNING ("failed to allocate ports %u", idx);
2821 gst_object_unref (priv->send_rtp_sink);
2822 priv->send_rtp_sink = NULL;
2823 gst_object_unref (priv->send_src[0]);
2824 priv->send_src[0] = NULL;
2825 gst_object_unref (priv->send_src[1]);
2826 priv->send_src[1] = NULL;
2827 gst_object_unref (priv->recv_sink[0]);
2828 priv->recv_sink[0] = NULL;
2829 gst_object_unref (priv->recv_sink[1]);
2830 priv->recv_sink[1] = NULL;
2831 if (priv->udpsink[0])
2832 gst_element_set_state (priv->udpsink[0], GST_STATE_NULL);
2833 if (priv->udpsink[1])
2834 gst_element_set_state (priv->udpsink[1], GST_STATE_NULL);
2835 if (priv->udpsrc_v4[0]) {
2836 gst_element_set_state (priv->udpsrc_v4[0], GST_STATE_NULL);
2837 gst_object_unref (priv->udpsrc_v4[0]);
2838 priv->udpsrc_v4[0] = NULL;
2840 if (priv->udpsrc_v4[1]) {
2841 gst_element_set_state (priv->udpsrc_v4[1], GST_STATE_NULL);
2842 gst_object_unref (priv->udpsrc_v4[1]);
2843 priv->udpsrc_v4[1] = NULL;
2845 if (priv->udpsrc_mcast_v4[0]) {
2846 gst_element_set_state (priv->udpsrc_mcast_v4[0], GST_STATE_NULL);
2847 gst_object_unref (priv->udpsrc_mcast_v4[0]);
2848 priv->udpsrc_mcast_v4[0] = NULL;
2850 if (priv->udpsrc_mcast_v4[1]) {
2851 gst_element_set_state (priv->udpsrc_mcast_v4[1], GST_STATE_NULL);
2852 gst_object_unref (priv->udpsrc_mcast_v4[1]);
2853 priv->udpsrc_mcast_v4[1] = NULL;
2855 if (priv->udpsrc_v6[0]) {
2856 gst_element_set_state (priv->udpsrc_v6[0], GST_STATE_NULL);
2857 gst_object_unref (priv->udpsrc_v6[0]);
2858 priv->udpsrc_v6[0] = NULL;
2860 if (priv->udpsrc_v6[1]) {
2861 gst_element_set_state (priv->udpsrc_v6[1], GST_STATE_NULL);
2862 gst_object_unref (priv->udpsrc_v6[1]);
2863 priv->udpsrc_v6[1] = NULL;
2865 if (priv->udpsrc_mcast_v6[0]) {
2866 gst_element_set_state (priv->udpsrc_mcast_v6[0], GST_STATE_NULL);
2867 gst_object_unref (priv->udpsrc_mcast_v6[0]);
2868 priv->udpsrc_mcast_v6[0] = NULL;
2870 if (priv->udpsrc_mcast_v6[1]) {
2871 gst_element_set_state (priv->udpsrc_mcast_v6[1], GST_STATE_NULL);
2872 gst_object_unref (priv->udpsrc_mcast_v6[1]);
2873 priv->udpsrc_mcast_v6[1] = NULL;
2875 g_mutex_unlock (&priv->lock);
2881 clear_element (GstBin * bin, GstElement ** elementptr)
2884 gst_element_set_locked_state (*elementptr, FALSE);
2885 gst_element_set_state (*elementptr, GST_STATE_NULL);
2886 if (GST_ELEMENT_PARENT (*elementptr))
2887 gst_bin_remove (bin, *elementptr);
2889 gst_object_unref (*elementptr);
2895 * gst_rtsp_stream_leave_bin:
2896 * @stream: a #GstRTSPStream
2897 * @bin: (transfer none): a #GstBin
2898 * @rtpbin: (transfer none): a rtpbin #GstElement
2900 * Remove the elements of @stream from @bin.
2902 * Return: %TRUE on success.
2905 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2906 GstElement * rtpbin)
2908 GstRTSPStreamPrivate *priv;
2911 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2912 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2913 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2915 priv = stream->priv;
2917 g_mutex_lock (&priv->lock);
2918 if (priv->joined_bin == NULL)
2919 goto was_not_joined;
2920 if (priv->joined_bin != bin)
2923 priv->joined_bin = NULL;
2925 /* all transports must be removed by now */
2926 if (priv->transports != NULL)
2927 goto transports_not_removed;
2929 clear_tr_cache (priv, TRUE);
2930 clear_tr_cache (priv, FALSE);
2932 GST_INFO ("stream %p leaving bin", stream);
2935 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2937 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2938 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2939 gst_object_unref (priv->send_rtp_sink);
2940 priv->send_rtp_sink = NULL;
2941 } else if (priv->recv_rtp_src) {
2942 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2943 gst_object_unref (priv->recv_rtp_src);
2944 priv->recv_rtp_src = NULL;
2947 for (i = 0; i < 2; i++) {
2948 clear_element (bin, &priv->udpsink[i]);
2949 clear_element (bin, &priv->appsink[i]);
2950 clear_element (bin, &priv->appqueue[i]);
2951 clear_element (bin, &priv->udpqueue[i]);
2952 clear_element (bin, &priv->tee[i]);
2953 clear_element (bin, &priv->funnel[i]);
2954 clear_element (bin, &priv->appsrc[i]);
2955 clear_element (bin, &priv->udpsrc_v4[i]);
2956 clear_element (bin, &priv->udpsrc_v6[i]);
2957 clear_element (bin, &priv->udpsrc_mcast_v4[i]);
2958 clear_element (bin, &priv->udpsrc_mcast_v6[i]);
2960 if (priv->sinkpad || i == 1) {
2961 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2962 gst_object_unref (priv->recv_sink[i]);
2963 priv->recv_sink[i] = NULL;
2968 gst_object_unref (priv->send_src[0]);
2969 priv->send_src[0] = NULL;
2972 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2973 gst_object_unref (priv->send_src[1]);
2974 priv->send_src[1] = NULL;
2976 g_object_unref (priv->session);
2977 priv->session = NULL;
2979 gst_caps_unref (priv->caps);
2983 gst_object_unref (priv->srtpenc);
2985 gst_object_unref (priv->srtpdec);
2987 g_clear_object (&priv->joined_bin);
2988 g_mutex_unlock (&priv->lock);
2994 g_mutex_unlock (&priv->lock);
2997 transports_not_removed:
2999 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
3000 g_mutex_unlock (&priv->lock);
3005 GST_ERROR_OBJECT (stream, "leaving the wrong bin");
3006 g_mutex_unlock (&priv->lock);
3012 * gst_rtsp_stream_get_joined_bin:
3013 * @stream: a #GstRTSPStream
3015 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
3017 * Return: (transfer full): the joined bin or NULL.
3020 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
3022 GstRTSPStreamPrivate *priv;
3025 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3027 priv = stream->priv;
3029 g_mutex_lock (&priv->lock);
3030 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
3031 g_mutex_unlock (&priv->lock);
3037 * gst_rtsp_stream_get_rtpinfo:
3038 * @stream: a #GstRTSPStream
3039 * @rtptime: (allow-none): result RTP timestamp
3040 * @seq: (allow-none): result RTP seqnum
3041 * @clock_rate: (allow-none): the clock rate
3042 * @running_time: (allow-none): result running-time
3044 * Retrieve the current rtptime, seq and running-time. This is used to
3045 * construct a RTPInfo reply header.
3047 * Returns: %TRUE when rtptime, seq and running-time could be determined.
3050 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
3051 guint * rtptime, guint * seq, guint * clock_rate,
3052 GstClockTime * running_time)
3054 GstRTSPStreamPrivate *priv;
3055 GstStructure *stats;
3056 GObjectClass *payobjclass;
3058 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3060 priv = stream->priv;
3062 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
3064 g_mutex_lock (&priv->lock);
3066 /* First try to extract the information from the last buffer on the sinks.
3067 * This will have a more accurate sequence number and timestamp, as between
3068 * the payloader and the sink there can be some queues
3070 if (priv->udpsink[0] || priv->appsink[0]) {
3071 GstSample *last_sample;
3073 if (priv->udpsink[0])
3074 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
3076 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
3081 GstSegment *segment;
3082 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
3084 caps = gst_sample_get_caps (last_sample);
3085 buffer = gst_sample_get_buffer (last_sample);
3086 segment = gst_sample_get_segment (last_sample);
3088 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3090 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3094 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3097 gst_rtp_buffer_unmap (&rtp_buffer);
3101 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3102 GST_BUFFER_TIMESTAMP (buffer));
3106 GstStructure *s = gst_caps_get_structure (caps, 0);
3108 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3110 if (*clock_rate == 0 && running_time)
3111 *running_time = GST_CLOCK_TIME_NONE;
3113 gst_sample_unref (last_sample);
3117 gst_sample_unref (last_sample);
3122 if (g_object_class_find_property (payobjclass, "stats")) {
3123 g_object_get (priv->payloader, "stats", &stats, NULL);
3128 gst_structure_get_uint (stats, "seqnum", seq);
3131 gst_structure_get_uint (stats, "timestamp", rtptime);
3134 gst_structure_get_clock_time (stats, "running-time", running_time);
3137 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3138 if (*clock_rate == 0 && running_time)
3139 *running_time = GST_CLOCK_TIME_NONE;
3141 gst_structure_free (stats);
3143 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3144 !g_object_class_find_property (payobjclass, "timestamp"))
3148 g_object_get (priv->payloader, "seqnum", seq, NULL);
3151 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3154 *running_time = GST_CLOCK_TIME_NONE;
3158 g_mutex_unlock (&priv->lock);
3165 GST_WARNING ("Could not get payloader stats");
3166 g_mutex_unlock (&priv->lock);
3172 * gst_rtsp_stream_get_caps:
3173 * @stream: a #GstRTSPStream
3175 * Retrieve the current caps of @stream.
3177 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
3181 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3183 GstRTSPStreamPrivate *priv;
3186 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3188 priv = stream->priv;
3190 g_mutex_lock (&priv->lock);
3191 if ((result = priv->caps))
3192 gst_caps_ref (result);
3193 g_mutex_unlock (&priv->lock);
3199 * gst_rtsp_stream_recv_rtp:
3200 * @stream: a #GstRTSPStream
3201 * @buffer: (transfer full): a #GstBuffer
3203 * Handle an RTP buffer for the stream. This method is usually called when a
3204 * message has been received from a client using the TCP transport.
3206 * This function takes ownership of @buffer.
3208 * Returns: a GstFlowReturn.
3211 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3213 GstRTSPStreamPrivate *priv;
3215 GstElement *element;
3217 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3218 priv = stream->priv;
3219 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3220 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3222 g_mutex_lock (&priv->lock);
3223 if (priv->appsrc[0])
3224 element = gst_object_ref (priv->appsrc[0]);
3227 g_mutex_unlock (&priv->lock);
3230 if (priv->appsrc_base_time[0] == -1) {
3231 /* Take current running_time. This timestamp will be put on
3232 * the first buffer of each stream because we are a live source and so we
3233 * timestamp with the running_time. When we are dealing with TCP, we also
3234 * only timestamp the first buffer (using the DISCONT flag) because a server
3235 * typically bursts data, for which we don't want to compensate by speeding
3236 * up the media. The other timestamps will be interpollated from this one
3237 * using the RTP timestamps. */
3238 GST_OBJECT_LOCK (element);
3239 if (GST_ELEMENT_CLOCK (element)) {
3241 GstClockTime base_time;
3243 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3244 base_time = GST_ELEMENT_CAST (element)->base_time;
3246 priv->appsrc_base_time[0] = now - base_time;
3247 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3248 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3249 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3250 GST_TIME_ARGS (base_time));
3252 GST_OBJECT_UNLOCK (element);
3255 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3256 gst_object_unref (element);
3264 * gst_rtsp_stream_recv_rtcp:
3265 * @stream: a #GstRTSPStream
3266 * @buffer: (transfer full): a #GstBuffer
3268 * Handle an RTCP buffer for the stream. This method is usually called when a
3269 * message has been received from a client using the TCP transport.
3271 * This function takes ownership of @buffer.
3273 * Returns: a GstFlowReturn.
3276 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3278 GstRTSPStreamPrivate *priv;
3280 GstElement *element;
3282 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3283 priv = stream->priv;
3284 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3286 if (priv->joined_bin == NULL) {
3287 gst_buffer_unref (buffer);
3288 return GST_FLOW_NOT_LINKED;
3290 g_mutex_lock (&priv->lock);
3291 if (priv->appsrc[1])
3292 element = gst_object_ref (priv->appsrc[1]);
3295 g_mutex_unlock (&priv->lock);
3298 if (priv->appsrc_base_time[1] == -1) {
3299 /* Take current running_time. This timestamp will be put on
3300 * the first buffer of each stream because we are a live source and so we
3301 * timestamp with the running_time. When we are dealing with TCP, we also
3302 * only timestamp the first buffer (using the DISCONT flag) because a server
3303 * typically bursts data, for which we don't want to compensate by speeding
3304 * up the media. The other timestamps will be interpollated from this one
3305 * using the RTP timestamps. */
3306 GST_OBJECT_LOCK (element);
3307 if (GST_ELEMENT_CLOCK (element)) {
3309 GstClockTime base_time;
3311 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3312 base_time = GST_ELEMENT_CAST (element)->base_time;
3314 priv->appsrc_base_time[1] = now - base_time;
3315 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3316 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3317 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3318 GST_TIME_ARGS (base_time));
3320 GST_OBJECT_UNLOCK (element);
3323 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3324 gst_object_unref (element);
3327 gst_buffer_unref (buffer);
3332 /* must be called with lock */
3334 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3337 GstRTSPStreamPrivate *priv = stream->priv;
3338 const GstRTSPTransport *tr;
3340 tr = gst_rtsp_stream_transport_get_transport (trans);
3342 switch (tr->lower_transport) {
3343 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3344 case GST_RTSP_LOWER_TRANS_UDP:
3350 dest = tr->destination;
3351 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3355 } else if (priv->client_side) {
3356 /* In client side mode the 'destination' is the RTSP server, so send
3358 min = tr->server_port.min;
3359 max = tr->server_port.max;
3361 min = tr->client_port.min;
3362 max = tr->client_port.max;
3367 GST_INFO ("setting ttl-mc %d", ttl);
3368 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3369 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3371 GST_INFO ("adding %s:%d-%d", dest, min, max);
3372 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3373 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3374 priv->transports = g_list_prepend (priv->transports, trans);
3376 GST_INFO ("removing %s:%d-%d", dest, min, max);
3377 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3378 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3379 priv->transports = g_list_remove (priv->transports, trans);
3381 priv->transports_cookie++;
3384 case GST_RTSP_LOWER_TRANS_TCP:
3386 GST_INFO ("adding TCP %s", tr->destination);
3387 priv->transports = g_list_prepend (priv->transports, trans);
3389 GST_INFO ("removing TCP %s", tr->destination);
3390 priv->transports = g_list_remove (priv->transports, trans);
3392 priv->transports_cookie++;
3395 goto unknown_transport;
3402 GST_INFO ("Unknown transport %d", tr->lower_transport);
3409 * gst_rtsp_stream_add_transport:
3410 * @stream: a #GstRTSPStream
3411 * @trans: (transfer none): a #GstRTSPStreamTransport
3413 * Add the transport in @trans to @stream. The media of @stream will
3414 * then also be send to the values configured in @trans.
3416 * @stream must be joined to a bin.
3418 * @trans must contain a valid #GstRTSPTransport.
3420 * Returns: %TRUE if @trans was added
3423 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3424 GstRTSPStreamTransport * trans)
3426 GstRTSPStreamPrivate *priv;
3429 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3430 priv = stream->priv;
3431 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3432 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3434 g_mutex_lock (&priv->lock);
3435 res = update_transport (stream, trans, TRUE);
3436 g_mutex_unlock (&priv->lock);
3442 * gst_rtsp_stream_remove_transport:
3443 * @stream: a #GstRTSPStream
3444 * @trans: (transfer none): a #GstRTSPStreamTransport
3446 * Remove the transport in @trans from @stream. The media of @stream will
3447 * not be sent to the values configured in @trans.
3449 * @stream must be joined to a bin.
3451 * @trans must contain a valid #GstRTSPTransport.
3453 * Returns: %TRUE if @trans was removed
3456 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3457 GstRTSPStreamTransport * trans)
3459 GstRTSPStreamPrivate *priv;
3462 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3463 priv = stream->priv;
3464 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3465 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3467 g_mutex_lock (&priv->lock);
3468 res = update_transport (stream, trans, FALSE);
3469 g_mutex_unlock (&priv->lock);
3475 * gst_rtsp_stream_update_crypto:
3476 * @stream: a #GstRTSPStream
3478 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3480 * Update the new crypto information for @ssrc in @stream. If information
3481 * for @ssrc did not exist, it will be added. If information
3482 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3483 * be removed from @stream.
3485 * Returns: %TRUE if @crypto could be updated
3488 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3489 guint ssrc, GstCaps * crypto)
3491 GstRTSPStreamPrivate *priv;
3493 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3494 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3496 priv = stream->priv;
3498 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3500 g_mutex_lock (&priv->lock);
3502 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3503 gst_caps_ref (crypto));
3505 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3506 g_mutex_unlock (&priv->lock);
3512 * gst_rtsp_stream_get_rtp_socket:
3513 * @stream: a #GstRTSPStream
3514 * @family: the socket family
3516 * Get the RTP socket from @stream for a @family.
3518 * @stream must be joined to a bin.
3520 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3521 * socket could be allocated for @family. Unref after usage
3524 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3526 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3530 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3531 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3532 family == G_SOCKET_FAMILY_IPV6, NULL);
3533 g_return_val_if_fail (priv->udpsink[0], NULL);
3535 if (family == G_SOCKET_FAMILY_IPV6)
3540 g_object_get (priv->udpsink[0], name, &socket, NULL);
3546 * gst_rtsp_stream_get_rtcp_socket:
3547 * @stream: a #GstRTSPStream
3548 * @family: the socket family
3550 * Get the RTCP socket from @stream for a @family.
3552 * @stream must be joined to a bin.
3554 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3555 * socket could be allocated for @family. Unref after usage
3558 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3560 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3564 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3565 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3566 family == G_SOCKET_FAMILY_IPV6, NULL);
3567 g_return_val_if_fail (priv->udpsink[1], NULL);
3569 if (family == G_SOCKET_FAMILY_IPV6)
3574 g_object_get (priv->udpsink[1], name, &socket, NULL);
3580 * gst_rtsp_stream_set_seqnum:
3581 * @stream: a #GstRTSPStream
3582 * @seqnum: a new sequence number
3584 * Configure the sequence number in the payloader of @stream to @seqnum.
3587 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3589 GstRTSPStreamPrivate *priv;
3591 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3593 priv = stream->priv;
3595 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3599 * gst_rtsp_stream_get_seqnum:
3600 * @stream: a #GstRTSPStream
3602 * Get the configured sequence number in the payloader of @stream.
3604 * Returns: the sequence number of the payloader.
3607 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3609 GstRTSPStreamPrivate *priv;
3612 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3614 priv = stream->priv;
3616 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3622 * gst_rtsp_stream_transport_filter:
3623 * @stream: a #GstRTSPStream
3624 * @func: (scope call) (allow-none): a callback
3625 * @user_data: (closure): user data passed to @func
3627 * Call @func for each transport managed by @stream. The result value of @func
3628 * determines what happens to the transport. @func will be called with @stream
3629 * locked so no further actions on @stream can be performed from @func.
3631 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3634 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3636 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3637 * will also be added with an additional ref to the result #GList of this
3640 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3642 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3643 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3644 * element in the #GList should be unreffed before the list is freed.
3647 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3648 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3650 GstRTSPStreamPrivate *priv;
3651 GList *result, *walk, *next;
3652 GHashTable *visited = NULL;
3655 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3657 priv = stream->priv;
3661 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3663 g_mutex_lock (&priv->lock);
3665 cookie = priv->transports_cookie;
3666 for (walk = priv->transports; walk; walk = next) {
3667 GstRTSPStreamTransport *trans = walk->data;
3668 GstRTSPFilterResult res;
3671 next = g_list_next (walk);
3674 /* only visit each transport once */
3675 if (g_hash_table_contains (visited, trans))
3678 g_hash_table_add (visited, g_object_ref (trans));
3679 g_mutex_unlock (&priv->lock);
3681 res = func (stream, trans, user_data);
3683 g_mutex_lock (&priv->lock);
3685 res = GST_RTSP_FILTER_REF;
3687 changed = (cookie != priv->transports_cookie);
3690 case GST_RTSP_FILTER_REMOVE:
3691 update_transport (stream, trans, FALSE);
3693 case GST_RTSP_FILTER_REF:
3694 result = g_list_prepend (result, g_object_ref (trans));
3696 case GST_RTSP_FILTER_KEEP:
3703 g_mutex_unlock (&priv->lock);
3706 g_hash_table_unref (visited);
3711 static GstPadProbeReturn
3712 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3714 GstRTSPStreamPrivate *priv;
3715 GstRTSPStream *stream;
3718 priv = stream->priv;
3720 GST_DEBUG_OBJECT (pad, "now blocking");
3722 g_mutex_lock (&priv->lock);
3723 priv->blocking = TRUE;
3724 g_mutex_unlock (&priv->lock);
3726 gst_element_post_message (priv->payloader,
3727 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3728 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3730 return GST_PAD_PROBE_OK;
3734 * gst_rtsp_stream_set_blocked:
3735 * @stream: a #GstRTSPStream
3736 * @blocked: boolean indicating we should block or unblock
3738 * Blocks or unblocks the dataflow on @stream.
3740 * Returns: %TRUE on success
3743 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3745 GstRTSPStreamPrivate *priv;
3747 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3749 priv = stream->priv;
3751 g_mutex_lock (&priv->lock);
3753 priv->blocking = FALSE;
3754 if (priv->blocked_id == 0) {
3755 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3756 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3757 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3758 g_object_ref (stream), g_object_unref);
3761 if (priv->blocked_id != 0) {
3762 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3763 priv->blocked_id = 0;
3764 priv->blocking = FALSE;
3767 g_mutex_unlock (&priv->lock);
3773 * gst_rtsp_stream_is_blocking:
3774 * @stream: a #GstRTSPStream
3776 * Check if @stream is blocking on a #GstBuffer.
3778 * Returns: %TRUE if @stream is blocking
3781 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3783 GstRTSPStreamPrivate *priv;
3786 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3788 priv = stream->priv;
3790 g_mutex_lock (&priv->lock);
3791 result = priv->blocking;
3792 g_mutex_unlock (&priv->lock);
3798 * gst_rtsp_stream_query_position:
3799 * @stream: a #GstRTSPStream
3801 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3802 * the RTP parts of the pipeline and not the RTCP parts.
3804 * Returns: %TRUE if the position could be queried
3807 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3809 GstRTSPStreamPrivate *priv;
3813 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3815 priv = stream->priv;
3817 g_mutex_lock (&priv->lock);
3818 /* depending on the transport type, it should query corresponding sink */
3819 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3820 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3821 sink = priv->udpsink[0];
3823 sink = priv->appsink[0];
3826 gst_object_ref (sink);
3827 g_mutex_unlock (&priv->lock);
3832 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3833 gst_object_unref (sink);
3839 * gst_rtsp_stream_query_stop:
3840 * @stream: a #GstRTSPStream
3842 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3843 * the RTP parts of the pipeline and not the RTCP parts.
3845 * Returns: %TRUE if the stop could be queried
3848 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3850 GstRTSPStreamPrivate *priv;
3855 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3857 priv = stream->priv;
3859 g_mutex_lock (&priv->lock);
3860 /* depending on the transport type, it should query corresponding sink */
3861 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3862 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3863 sink = priv->udpsink[0];
3865 sink = priv->appsink[0];
3868 gst_object_ref (sink);
3869 g_mutex_unlock (&priv->lock);
3874 query = gst_query_new_segment (GST_FORMAT_TIME);
3875 if ((ret = gst_element_query (sink, query))) {
3878 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3879 if (format != GST_FORMAT_TIME)
3882 gst_query_unref (query);
3883 gst_object_unref (sink);