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;
76 /* TRUE if this stream is running on
77 * the client side of an RTSP link (for RECORD) */
81 GstRTSPProfile profiles;
82 GstRTSPLowerTrans protocols;
84 /* pads on the rtpbin */
85 GstPad *send_rtp_sink;
90 /* the RTPSession object */
93 /* SRTP encoder/decoder */
98 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
100 GstElement *udpsrc_v4[2];
101 /* UDP sources for UDP multicast transports */
102 GstElement *udpsrc_mcast_v4[2];
104 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
106 GstElement *udpsrc_v6[2];
107 /* UDP sources for UDP multicast transports */
108 GstElement *udpsrc_mcast_v6[2];
110 GstElement *udpqueue[2];
111 GstElement *udpsink[2];
113 /* for TCP transport */
114 GstElement *appsrc[2];
115 GstClockTime appsrc_base_time[2];
116 GstElement *appqueue[2];
117 GstElement *appsink[2];
120 GstElement *funnel[2];
125 GstClockTime rtx_time;
127 /* server ports for sending/receiving over ipv4 */
128 GstRTSPRange server_port_v4;
129 GstRTSPAddress *server_addr_v4;
132 /* server ports for sending/receiving over ipv6 */
133 GstRTSPRange server_port_v6;
134 GstRTSPAddress *server_addr_v6;
137 /* multicast addresses */
138 GstRTSPAddressPool *pool;
139 GstRTSPAddress *addr_v4;
140 GstRTSPAddress *addr_v6;
141 gboolean have_ipv4_mcast;
142 gboolean have_ipv6_mcast;
144 gchar *multicast_iface;
146 /* the caps of the stream */
150 /* transports we stream to */
153 guint transports_cookie;
155 GList *tr_cache_rtcp;
156 guint tr_cache_cookie_rtp;
157 guint tr_cache_cookie_rtcp;
162 /* stream blocking */
166 /* pt->caps map for RECORD streams */
169 GstRTSPPublishClockMode publish_clock_mode;
172 #define DEFAULT_CONTROL NULL
173 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
174 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
175 GST_RTSP_LOWER_TRANS_TCP
188 SIGNAL_NEW_RTP_ENCODER,
189 SIGNAL_NEW_RTCP_ENCODER,
193 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
194 #define GST_CAT_DEFAULT rtsp_stream_debug
196 static GQuark ssrc_stream_map_key;
198 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
199 GValue * value, GParamSpec * pspec);
200 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
201 const GValue * value, GParamSpec * pspec);
203 static void gst_rtsp_stream_finalize (GObject * obj);
205 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
207 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
210 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
212 GObjectClass *gobject_class;
214 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
216 gobject_class = G_OBJECT_CLASS (klass);
218 gobject_class->get_property = gst_rtsp_stream_get_property;
219 gobject_class->set_property = gst_rtsp_stream_set_property;
220 gobject_class->finalize = gst_rtsp_stream_finalize;
222 g_object_class_install_property (gobject_class, PROP_CONTROL,
223 g_param_spec_string ("control", "Control",
224 "The control string for this stream", DEFAULT_CONTROL,
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (gobject_class, PROP_PROFILES,
228 g_param_spec_flags ("profiles", "Profiles",
229 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
230 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
233 g_param_spec_flags ("protocols", "Protocols",
234 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
235 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
238 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
239 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
240 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
242 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
243 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
244 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
245 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
247 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
249 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
253 gst_rtsp_stream_init (GstRTSPStream * stream)
255 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
257 GST_DEBUG ("new stream %p", stream);
262 priv->control = g_strdup (DEFAULT_CONTROL);
263 priv->profiles = DEFAULT_PROFILES;
264 priv->protocols = DEFAULT_PROTOCOLS;
265 priv->publish_clock_mode = GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK;
267 g_mutex_init (&priv->lock);
269 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
270 NULL, (GDestroyNotify) gst_caps_unref);
271 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
272 (GDestroyNotify) gst_caps_unref);
276 gst_rtsp_stream_finalize (GObject * obj)
278 GstRTSPStream *stream;
279 GstRTSPStreamPrivate *priv;
281 stream = GST_RTSP_STREAM (obj);
284 GST_DEBUG ("finalize stream %p", stream);
286 /* we really need to be unjoined now */
287 g_return_if_fail (!priv->is_joined);
290 gst_rtsp_address_free (priv->addr_v4);
292 gst_rtsp_address_free (priv->addr_v6);
293 if (priv->server_addr_v4)
294 gst_rtsp_address_free (priv->server_addr_v4);
295 if (priv->server_addr_v6)
296 gst_rtsp_address_free (priv->server_addr_v6);
298 g_object_unref (priv->pool);
300 g_object_unref (priv->rtxsend);
302 g_free (priv->multicast_iface);
304 gst_object_unref (priv->payloader);
306 gst_object_unref (priv->srcpad);
308 gst_object_unref (priv->sinkpad);
309 g_free (priv->control);
310 g_mutex_clear (&priv->lock);
312 g_hash_table_unref (priv->keys);
313 g_hash_table_destroy (priv->ptmap);
315 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
319 gst_rtsp_stream_get_property (GObject * object, guint propid,
320 GValue * value, GParamSpec * pspec)
322 GstRTSPStream *stream = GST_RTSP_STREAM (object);
326 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
329 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
332 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
340 gst_rtsp_stream_set_property (GObject * object, guint propid,
341 const GValue * value, GParamSpec * pspec)
343 GstRTSPStream *stream = GST_RTSP_STREAM (object);
347 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
350 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
353 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
356 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
361 * gst_rtsp_stream_new:
364 * @payloader: a #GstElement
366 * Create a new media stream with index @idx that handles RTP data on
367 * @pad and has a payloader element @payloader if @pad is a source pad
368 * or a depayloader element @payloader if @pad is a sink pad.
370 * Returns: (transfer full): a new #GstRTSPStream
373 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
375 GstRTSPStreamPrivate *priv;
376 GstRTSPStream *stream;
378 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
379 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
381 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
384 priv->payloader = gst_object_ref (payloader);
385 if (GST_PAD_IS_SRC (pad))
386 priv->srcpad = gst_object_ref (pad);
388 priv->sinkpad = gst_object_ref (pad);
394 * gst_rtsp_stream_get_index:
395 * @stream: a #GstRTSPStream
397 * Get the stream index.
399 * Return: the stream index.
402 gst_rtsp_stream_get_index (GstRTSPStream * stream)
404 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
406 return stream->priv->idx;
410 * gst_rtsp_stream_get_pt:
411 * @stream: a #GstRTSPStream
413 * Get the stream payload type.
415 * Return: the stream payload type.
418 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
420 GstRTSPStreamPrivate *priv;
423 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
427 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
433 * gst_rtsp_stream_get_srcpad:
434 * @stream: a #GstRTSPStream
436 * Get the srcpad associated with @stream.
438 * Returns: (transfer full): the srcpad. Unref after usage.
441 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
443 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
445 if (!stream->priv->srcpad)
448 return gst_object_ref (stream->priv->srcpad);
452 * gst_rtsp_stream_get_sinkpad:
453 * @stream: a #GstRTSPStream
455 * Get the sinkpad associated with @stream.
457 * Returns: (transfer full): the sinkpad. Unref after usage.
460 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
462 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
464 if (!stream->priv->sinkpad)
467 return gst_object_ref (stream->priv->sinkpad);
471 * gst_rtsp_stream_get_control:
472 * @stream: a #GstRTSPStream
474 * Get the control string to identify this stream.
476 * Returns: (transfer full): the control string. g_free() after usage.
479 gst_rtsp_stream_get_control (GstRTSPStream * stream)
481 GstRTSPStreamPrivate *priv;
484 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
488 g_mutex_lock (&priv->lock);
489 if ((result = g_strdup (priv->control)) == NULL)
490 result = g_strdup_printf ("stream=%u", priv->idx);
491 g_mutex_unlock (&priv->lock);
497 * gst_rtsp_stream_set_control:
498 * @stream: a #GstRTSPStream
499 * @control: a control string
501 * Set the control string in @stream.
504 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
506 GstRTSPStreamPrivate *priv;
508 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
512 g_mutex_lock (&priv->lock);
513 g_free (priv->control);
514 priv->control = g_strdup (control);
515 g_mutex_unlock (&priv->lock);
519 * gst_rtsp_stream_has_control:
520 * @stream: a #GstRTSPStream
521 * @control: a control string
523 * Check if @stream has the control string @control.
525 * Returns: %TRUE is @stream has @control as the control string
528 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
530 GstRTSPStreamPrivate *priv;
533 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
537 g_mutex_lock (&priv->lock);
539 res = (g_strcmp0 (priv->control, control) == 0);
543 if (sscanf (control, "stream=%u", &streamid) > 0)
544 res = (streamid == priv->idx);
548 g_mutex_unlock (&priv->lock);
554 * gst_rtsp_stream_set_mtu:
555 * @stream: a #GstRTSPStream
558 * Configure the mtu in the payloader of @stream to @mtu.
561 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
563 GstRTSPStreamPrivate *priv;
565 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
569 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
571 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
575 * gst_rtsp_stream_get_mtu:
576 * @stream: a #GstRTSPStream
578 * Get the configured MTU in the payloader of @stream.
580 * Returns: the MTU of the payloader.
583 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
585 GstRTSPStreamPrivate *priv;
588 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
592 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
597 /* Update the dscp qos property on the udp sinks */
599 update_dscp_qos (GstRTSPStream * stream)
601 GstRTSPStreamPrivate *priv;
603 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
607 if (priv->udpsink[0]) {
608 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
612 if (priv->udpsink[1]) {
613 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
619 * gst_rtsp_stream_set_dscp_qos:
620 * @stream: a #GstRTSPStream
621 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
623 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
626 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
628 GstRTSPStreamPrivate *priv;
630 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
634 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
636 if (dscp_qos < -1 || dscp_qos > 63) {
637 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
641 priv->dscp_qos = dscp_qos;
643 update_dscp_qos (stream);
647 * gst_rtsp_stream_get_dscp_qos:
648 * @stream: a #GstRTSPStream
650 * Get the configured DSCP QoS in of the outgoing sockets.
652 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
655 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
657 GstRTSPStreamPrivate *priv;
659 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
663 return priv->dscp_qos;
667 * gst_rtsp_stream_is_transport_supported:
668 * @stream: a #GstRTSPStream
669 * @transport: (transfer none): a #GstRTSPTransport
671 * Check if @transport can be handled by stream
673 * Returns: %TRUE if @transport can be handled by @stream.
676 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
677 GstRTSPTransport * transport)
679 GstRTSPStreamPrivate *priv;
681 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
685 g_mutex_lock (&priv->lock);
686 if (transport->trans != GST_RTSP_TRANS_RTP)
687 goto unsupported_transmode;
689 if (!(transport->profile & priv->profiles))
690 goto unsupported_profile;
692 if (!(transport->lower_transport & priv->protocols))
693 goto unsupported_ltrans;
695 g_mutex_unlock (&priv->lock);
700 unsupported_transmode:
702 GST_DEBUG ("unsupported transport mode %d", transport->trans);
703 g_mutex_unlock (&priv->lock);
708 GST_DEBUG ("unsupported profile %d", transport->profile);
709 g_mutex_unlock (&priv->lock);
714 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
715 g_mutex_unlock (&priv->lock);
721 * gst_rtsp_stream_set_profiles:
722 * @stream: a #GstRTSPStream
723 * @profiles: the new profiles
725 * Configure the allowed profiles for @stream.
728 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
730 GstRTSPStreamPrivate *priv;
732 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
736 g_mutex_lock (&priv->lock);
737 priv->profiles = profiles;
738 g_mutex_unlock (&priv->lock);
742 * gst_rtsp_stream_get_profiles:
743 * @stream: a #GstRTSPStream
745 * Get the allowed profiles of @stream.
747 * Returns: a #GstRTSPProfile
750 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
752 GstRTSPStreamPrivate *priv;
755 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
759 g_mutex_lock (&priv->lock);
760 res = priv->profiles;
761 g_mutex_unlock (&priv->lock);
767 * gst_rtsp_stream_set_protocols:
768 * @stream: a #GstRTSPStream
769 * @protocols: the new flags
771 * Configure the allowed lower transport for @stream.
774 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
775 GstRTSPLowerTrans protocols)
777 GstRTSPStreamPrivate *priv;
779 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
783 g_mutex_lock (&priv->lock);
784 priv->protocols = protocols;
785 g_mutex_unlock (&priv->lock);
789 * gst_rtsp_stream_get_protocols:
790 * @stream: a #GstRTSPStream
792 * Get the allowed protocols of @stream.
794 * Returns: a #GstRTSPLowerTrans
797 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
799 GstRTSPStreamPrivate *priv;
800 GstRTSPLowerTrans res;
802 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
803 GST_RTSP_LOWER_TRANS_UNKNOWN);
807 g_mutex_lock (&priv->lock);
808 res = priv->protocols;
809 g_mutex_unlock (&priv->lock);
815 * gst_rtsp_stream_set_address_pool:
816 * @stream: a #GstRTSPStream
817 * @pool: (transfer none): a #GstRTSPAddressPool
819 * configure @pool to be used as the address pool of @stream.
822 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
823 GstRTSPAddressPool * pool)
825 GstRTSPStreamPrivate *priv;
826 GstRTSPAddressPool *old;
828 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
832 GST_LOG_OBJECT (stream, "set address pool %p", pool);
834 g_mutex_lock (&priv->lock);
835 if ((old = priv->pool) != pool)
836 priv->pool = pool ? g_object_ref (pool) : NULL;
839 g_mutex_unlock (&priv->lock);
842 g_object_unref (old);
846 * gst_rtsp_stream_get_address_pool:
847 * @stream: a #GstRTSPStream
849 * Get the #GstRTSPAddressPool used as the address pool of @stream.
851 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
855 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
857 GstRTSPStreamPrivate *priv;
858 GstRTSPAddressPool *result;
860 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
864 g_mutex_lock (&priv->lock);
865 if ((result = priv->pool))
866 g_object_ref (result);
867 g_mutex_unlock (&priv->lock);
873 * gst_rtsp_stream_set_multicast_iface:
874 * @stream: a #GstRTSPStream
875 * @multicast_iface: (transfer none): a multicast interface
877 * configure @multicast_iface to be used for @stream.
880 gst_rtsp_stream_set_multicast_iface (GstRTSPStream * stream,
881 const gchar * multicast_iface)
883 GstRTSPStreamPrivate *priv;
886 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
890 GST_LOG_OBJECT (stream, "set multicast iface %s",
891 GST_STR_NULL (multicast_iface));
893 g_mutex_lock (&priv->lock);
894 if ((old = priv->multicast_iface) != multicast_iface)
895 priv->multicast_iface = multicast_iface ? g_strdup (multicast_iface) : NULL;
898 g_mutex_unlock (&priv->lock);
905 * gst_rtsp_stream_get_multicast_iface:
906 * @stream: a #GstRTSPStream
908 * Get the multicast interface used for @stream.
910 * Returns: (transfer full): the multicast interface for @stream. g_free() after
914 gst_rtsp_stream_get_multicast_iface (GstRTSPStream * stream)
916 GstRTSPStreamPrivate *priv;
919 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
923 g_mutex_lock (&priv->lock);
924 if ((result = priv->multicast_iface))
925 result = g_strdup (result);
926 g_mutex_unlock (&priv->lock);
932 * gst_rtsp_stream_get_multicast_address:
933 * @stream: a #GstRTSPStream
934 * @family: the #GSocketFamily
936 * Get the multicast address of @stream for @family.
938 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
939 * or %NULL when no address could be allocated. gst_rtsp_address_free()
943 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
944 GSocketFamily family)
946 GstRTSPStreamPrivate *priv;
947 GstRTSPAddress *result;
948 GstRTSPAddress **addrp;
949 GstRTSPAddressFlags flags;
951 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
955 if (family == G_SOCKET_FAMILY_IPV6) {
956 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
957 addrp = &priv->addr_v6;
959 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
960 addrp = &priv->addr_v4;
963 g_mutex_lock (&priv->lock);
964 if (*addrp == NULL) {
965 if (priv->pool == NULL)
968 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
970 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
974 result = gst_rtsp_address_copy (*addrp);
975 g_mutex_unlock (&priv->lock);
982 GST_ERROR_OBJECT (stream, "no address pool specified");
983 g_mutex_unlock (&priv->lock);
988 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
989 g_mutex_unlock (&priv->lock);
995 * gst_rtsp_stream_reserve_address:
996 * @stream: a #GstRTSPStream
997 * @address: an address
1002 * Reserve @address and @port as the address and port of @stream.
1004 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
1005 * the address could be reserved. gst_rtsp_address_free() after usage.
1008 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
1009 const gchar * address, guint port, guint n_ports, guint ttl)
1011 GstRTSPStreamPrivate *priv;
1012 GstRTSPAddress *result;
1014 GSocketFamily family;
1015 GstRTSPAddress **addrp;
1017 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1018 g_return_val_if_fail (address != NULL, NULL);
1019 g_return_val_if_fail (port > 0, NULL);
1020 g_return_val_if_fail (n_ports > 0, NULL);
1021 g_return_val_if_fail (ttl > 0, NULL);
1023 priv = stream->priv;
1025 addr = g_inet_address_new_from_string (address);
1027 GST_ERROR ("failed to get inet addr from %s", address);
1028 family = G_SOCKET_FAMILY_IPV4;
1030 family = g_inet_address_get_family (addr);
1031 g_object_unref (addr);
1034 if (family == G_SOCKET_FAMILY_IPV6)
1035 addrp = &priv->addr_v6;
1037 addrp = &priv->addr_v4;
1039 g_mutex_lock (&priv->lock);
1040 if (*addrp == NULL) {
1041 GstRTSPAddressPoolResult res;
1043 if (priv->pool == NULL)
1046 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
1047 port, n_ports, ttl, addrp);
1048 if (res != GST_RTSP_ADDRESS_POOL_OK)
1051 if (strcmp ((*addrp)->address, address) ||
1052 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
1053 (*addrp)->ttl != ttl)
1054 goto different_address;
1056 result = gst_rtsp_address_copy (*addrp);
1057 g_mutex_unlock (&priv->lock);
1064 GST_ERROR_OBJECT (stream, "no address pool specified");
1065 g_mutex_unlock (&priv->lock);
1070 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1072 g_mutex_unlock (&priv->lock);
1077 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
1078 " reserved", address);
1079 g_mutex_unlock (&priv->lock);
1084 /* must be called with lock */
1086 set_sockets_for_udpsinks (GstRTSPStream * stream, GSocket * rtp_socket,
1087 GSocket * rtcp_socket, GSocketFamily family)
1089 GstRTSPStreamPrivate *priv = stream->priv;
1090 const gchar *multisink_socket;
1092 if (family == G_SOCKET_FAMILY_IPV6)
1093 multisink_socket = "socket-v6";
1095 multisink_socket = "socket";
1097 g_object_set (G_OBJECT (priv->udpsink[0]), multisink_socket, rtp_socket,
1099 g_object_set (G_OBJECT (priv->udpsink[1]), multisink_socket, rtcp_socket,
1103 /* must be called with lock */
1105 create_and_configure_udpsinks (GstRTSPStream * stream)
1107 GstRTSPStreamPrivate *priv = stream->priv;
1108 GstElement *udpsink0, *udpsink1;
1113 if (priv->udpsink[0])
1114 udpsink0 = priv->udpsink[0];
1116 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1119 goto no_udp_protocol;
1121 if (priv->udpsink[1])
1122 udpsink1 = priv->udpsink[1];
1124 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1127 goto no_udp_protocol;
1129 /* configure sinks */
1131 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1132 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1134 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1135 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1137 g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
1139 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1140 /* Needs to be async for RECORD streams, otherwise we will never go to
1141 * PLAYING because the sinks will wait for data while the udpsrc can't
1142 * provide data with timestamps in PAUSED. */
1144 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1145 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1147 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1148 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1150 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1151 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1153 /* update the dscp qos field in the sinks */
1154 update_dscp_qos (stream);
1156 priv->udpsink[0] = udpsink0;
1157 priv->udpsink[1] = udpsink1;
1168 /* must be called with lock */
1170 play_udpsources_one_family (GstRTSPStream * stream, GstElement * udpsrc_out[2],
1171 GSocketFamily family)
1173 GstRTSPStreamPrivate *priv;
1174 GstPad *pad, *selpad;
1178 priv = stream->priv;
1179 bin = GST_BIN (gst_object_get_parent (GST_OBJECT (priv->funnel[1])));
1181 for (i = 0; i < 2; i++) {
1182 if (priv->sinkpad || i == 1) {
1184 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1185 * values. This is only relevant for PLAY pipelines */
1186 gst_element_set_state (udpsrc_out[i], GST_STATE_PLAYING);
1187 gst_element_set_locked_state (udpsrc_out[i], TRUE);
1190 gst_bin_add (bin, udpsrc_out[i]);
1192 /* and link to the funnel */
1193 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1194 pad = gst_element_get_static_pad (udpsrc_out[i], "src");
1195 gst_pad_link (pad, selpad);
1196 gst_object_unref (pad);
1197 gst_object_unref (selpad);
1199 /* otherwise sync state with parent in case it's running already
1201 if (!priv->srcpad) {
1202 gst_element_sync_state_with_parent (udpsrc_out[i]);
1207 gst_object_unref (bin);
1210 /* must be called with lock */
1212 create_and_configure_udpsources_one_family (GstElement * udpsrc_out[2],
1213 GSocket * rtp_socket, GSocket * rtcp_socket, GSocketFamily family,
1214 const gchar * address, gint rtpport, gint rtcpport,
1215 const gchar * multicast_iface, GstRTSPLowerTrans transport)
1217 GstStateChangeReturn ret;
1219 udpsrc_out[0] = gst_element_factory_make ("udpsrc", NULL);
1220 udpsrc_out[1] = gst_element_factory_make ("udpsrc", NULL);
1222 if (udpsrc_out[0] == NULL || udpsrc_out[1] == NULL)
1225 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1226 g_object_set (G_OBJECT (udpsrc_out[0]), "address", address, NULL);
1227 g_object_set (G_OBJECT (udpsrc_out[1]), "address", address, NULL);
1228 g_object_set (G_OBJECT (udpsrc_out[0]), "port", rtpport, NULL);
1229 g_object_set (G_OBJECT (udpsrc_out[1]), "port", rtcpport, NULL);
1230 g_object_set (G_OBJECT (udpsrc_out[0]), "multicast-iface", multicast_iface,
1232 g_object_set (G_OBJECT (udpsrc_out[1]), "multicast-iface", multicast_iface,
1234 g_object_set (G_OBJECT (udpsrc_out[0]), "loop", FALSE, NULL);
1235 g_object_set (G_OBJECT (udpsrc_out[1]), "loop", FALSE, NULL);
1238 g_object_set (G_OBJECT (udpsrc_out[0]), "socket", rtp_socket, NULL);
1239 g_object_set (G_OBJECT (udpsrc_out[1]), "socket", rtcp_socket, NULL);
1241 ret = gst_element_set_state (udpsrc_out[0], GST_STATE_READY);
1242 if (ret == GST_STATE_CHANGE_FAILURE)
1244 ret = gst_element_set_state (udpsrc_out[1], GST_STATE_READY);
1245 if (ret == GST_STATE_CHANGE_FAILURE)
1255 gst_object_unref (udpsrc_out[0]);
1257 gst_object_unref (udpsrc_out[1]);
1263 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1264 GstElement * udpsrc_out[2], GstRTSPRange * server_port_out,
1265 GstRTSPTransport * ct, GstRTSPAddress ** server_addr_out,
1266 gboolean use_client_settings)
1268 GstRTSPStreamPrivate *priv = stream->priv;
1269 GSocket *rtp_socket = NULL;
1270 GSocket *rtcp_socket;
1271 gint tmp_rtp, tmp_rtcp;
1273 gint rtpport, rtcpport;
1274 GList *rejected_addresses = NULL;
1275 GstRTSPAddress *addr = NULL;
1276 GInetAddress *inetaddr = NULL;
1278 GSocketAddress *rtp_sockaddr = NULL;
1279 GSocketAddress *rtcp_sockaddr = NULL;
1280 GstRTSPAddressPool *pool;
1281 GstRTSPLowerTrans transport;
1282 const gchar *multicast_iface = priv->multicast_iface;
1286 transport = ct->lower_transport;
1288 /* Start with random port */
1291 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1292 G_SOCKET_PROTOCOL_UDP, NULL);
1294 goto no_udp_protocol;
1295 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1297 if (*server_addr_out)
1298 gst_rtsp_address_free (*server_addr_out);
1300 /* try to allocate 2 UDP ports, the RTP port should be an even
1301 * number and the RTCP port should be the next (uneven) port */
1304 if (rtp_socket == NULL) {
1305 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1306 G_SOCKET_PROTOCOL_UDP, NULL);
1308 goto no_udp_protocol;
1309 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1312 if (pool && ((transport == GST_RTSP_LOWER_TRANS_UDP &&
1313 gst_rtsp_address_pool_has_unicast_addresses (pool))
1314 || transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)) {
1315 GstRTSPAddressFlags flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1317 if (transport == GST_RTSP_LOWER_TRANS_UDP)
1318 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1320 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1323 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1325 if (family == G_SOCKET_FAMILY_IPV6)
1326 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1328 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1330 if (ct->destination && transport == GST_RTSP_LOWER_TRANS_UDP_MCAST
1331 && use_client_settings)
1332 gst_rtsp_address_pool_reserve_address (pool, ct->destination,
1333 ct->port.min, 2, ct->ttl, &addr);
1335 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1340 tmp_rtp = addr->port;
1342 g_clear_object (&inetaddr);
1343 inetaddr = g_inet_address_new_from_string (addr->address);
1345 /* If we're supposed to bind to a multicast address, instead bind
1346 * to ANY and let udpsrc later join the relevant multicast group
1348 if (g_inet_address_get_is_multicast (inetaddr)) {
1349 g_object_unref (inetaddr);
1350 inetaddr = g_inet_address_new_any (family);
1359 if (inetaddr == NULL)
1360 inetaddr = g_inet_address_new_any (family);
1363 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1364 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1365 g_object_unref (rtp_sockaddr);
1368 g_object_unref (rtp_sockaddr);
1370 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1371 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1372 g_clear_object (&rtp_sockaddr);
1377 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1378 g_object_unref (rtp_sockaddr);
1380 /* check if port is even */
1381 if ((tmp_rtp & 1) != 0) {
1382 /* port not even, close and allocate another */
1384 g_clear_object (&rtp_socket);
1389 tmp_rtcp = tmp_rtp + 1;
1391 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1392 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1393 g_object_unref (rtcp_sockaddr);
1394 g_clear_object (&rtp_socket);
1397 g_object_unref (rtcp_sockaddr);
1400 addr_str = g_inet_address_to_string (inetaddr);
1402 addr_str = addr->address;
1403 g_clear_object (&inetaddr);
1405 if (!create_and_configure_udpsources_one_family (udpsrc_out, rtp_socket,
1406 rtcp_socket, family, addr_str, tmp_rtp, tmp_rtcp, multicast_iface,
1410 goto no_udp_protocol;
1416 play_udpsources_one_family (stream, udpsrc_out, family);
1418 g_object_get (G_OBJECT (udpsrc_out[0]), "port", &rtpport, NULL);
1419 g_object_get (G_OBJECT (udpsrc_out[1]), "port", &rtcpport, NULL);
1421 /* this should not happen... */
1422 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1425 /* set RTP and RTCP sockets */
1426 set_sockets_for_udpsinks (stream, rtp_socket, rtcp_socket, family);
1428 server_port_out->min = rtpport;
1429 server_port_out->max = rtcpport;
1431 *server_addr_out = addr;
1432 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1434 g_object_unref (rtp_socket);
1435 g_object_unref (rtcp_socket);
1459 g_object_unref (inetaddr);
1460 g_list_free_full (rejected_addresses,
1461 (GDestroyNotify) gst_rtsp_address_free);
1463 gst_rtsp_address_free (addr);
1465 g_object_unref (rtp_socket);
1467 g_object_unref (rtcp_socket);
1473 * gst_rtsp_stream_allocate_udp_sockets:
1474 * @stream: a #GstRTSPStream
1475 * @family: protocol family
1476 * @transport_method: transport method
1478 * Allocates RTP and RTCP ports.
1480 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1483 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1484 GSocketFamily family, GstRTSPTransport * ct, gboolean use_client_settings)
1486 GstRTSPStreamPrivate *priv;
1487 gboolean result = FALSE;
1488 GstRTSPLowerTrans transport = ct->lower_transport;
1490 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1491 priv = stream->priv;
1492 g_return_val_if_fail (priv->is_joined, FALSE);
1494 g_mutex_lock (&priv->lock);
1496 if (family == G_SOCKET_FAMILY_IPV4) {
1497 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1498 if (priv->have_ipv4_mcast)
1500 priv->have_ipv4_mcast =
1501 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1502 priv->udpsrc_mcast_v4, &priv->server_port_v4, ct, &priv->addr_v4,
1503 use_client_settings);
1506 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4,
1507 &priv->server_port_v4, ct, &priv->server_addr_v4,
1508 use_client_settings);
1511 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1512 if (priv->have_ipv6_mcast)
1514 priv->have_ipv6_mcast =
1515 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1516 priv->udpsrc_mcast_v6, &priv->server_port_v6, ct, &priv->addr_v6,
1517 use_client_settings);
1519 if (priv->have_ipv6)
1522 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6,
1523 &priv->server_port_v6, ct, &priv->server_addr_v6,
1524 use_client_settings);
1529 result = priv->have_ipv4 || priv->have_ipv4_mcast || priv->have_ipv6 ||
1530 priv->have_ipv6_mcast;
1532 g_mutex_unlock (&priv->lock);
1538 * gst_rtsp_stream_set_client_side:
1539 * @stream: a #GstRTSPStream
1540 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1541 * an RTSP connection.
1543 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1544 * streams to an RTSP server via RECORD. This has the practical effect
1545 * of changing which UDP port numbers are used when setting up the local
1546 * side of the stream sending to be either the 'server' or 'client' pair
1547 * of a configured UDP transport.
1550 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1552 GstRTSPStreamPrivate *priv;
1554 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1555 priv = stream->priv;
1556 g_mutex_lock (&priv->lock);
1557 priv->client_side = client_side;
1558 g_mutex_unlock (&priv->lock);
1562 * gst_rtsp_stream_is_client_side:
1563 * @stream: a #GstRTSPStream
1565 * See gst_rtsp_stream_set_client_side()
1567 * Returns: TRUE if this #GstRTSPStream is client-side.
1570 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1572 GstRTSPStreamPrivate *priv;
1575 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1577 priv = stream->priv;
1578 g_mutex_lock (&priv->lock);
1579 ret = priv->client_side;
1580 g_mutex_unlock (&priv->lock);
1586 * gst_rtsp_stream_get_server_port:
1587 * @stream: a #GstRTSPStream
1588 * @server_port: (out): result server port
1589 * @family: the port family to get
1591 * Fill @server_port with the port pair used by the server. This function can
1592 * only be called when @stream has been joined.
1595 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1596 GstRTSPRange * server_port, GSocketFamily family)
1598 GstRTSPStreamPrivate *priv;
1600 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1601 priv = stream->priv;
1602 g_return_if_fail (priv->is_joined);
1604 g_mutex_lock (&priv->lock);
1605 if (family == G_SOCKET_FAMILY_IPV4) {
1607 *server_port = priv->server_port_v4;
1610 *server_port = priv->server_port_v6;
1612 g_mutex_unlock (&priv->lock);
1616 * gst_rtsp_stream_get_rtpsession:
1617 * @stream: a #GstRTSPStream
1619 * Get the RTP session of this stream.
1621 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1624 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1626 GstRTSPStreamPrivate *priv;
1629 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1631 priv = stream->priv;
1633 g_mutex_lock (&priv->lock);
1634 if ((session = priv->session))
1635 g_object_ref (session);
1636 g_mutex_unlock (&priv->lock);
1642 * gst_rtsp_stream_get_ssrc:
1643 * @stream: a #GstRTSPStream
1644 * @ssrc: (out): result ssrc
1646 * Get the SSRC used by the RTP session of this stream. This function can only
1647 * be called when @stream has been joined.
1650 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1652 GstRTSPStreamPrivate *priv;
1654 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1655 priv = stream->priv;
1656 g_return_if_fail (priv->is_joined);
1658 g_mutex_lock (&priv->lock);
1659 if (ssrc && priv->session)
1660 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1661 g_mutex_unlock (&priv->lock);
1665 * gst_rtsp_stream_set_retransmission_time:
1666 * @stream: a #GstRTSPStream
1667 * @time: a #GstClockTime
1669 * Set the amount of time to store retransmission packets.
1672 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1675 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1677 g_mutex_lock (&stream->priv->lock);
1678 stream->priv->rtx_time = time;
1679 if (stream->priv->rtxsend)
1680 g_object_set (stream->priv->rtxsend, "max-size-time",
1681 GST_TIME_AS_MSECONDS (time), NULL);
1682 g_mutex_unlock (&stream->priv->lock);
1686 * gst_rtsp_stream_get_retransmission_time:
1687 * @stream: a #GstRTSPStream
1689 * Get the amount of time to store retransmission data.
1691 * Returns: the amount of time to store retransmission data.
1694 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1698 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1700 g_mutex_lock (&stream->priv->lock);
1701 ret = stream->priv->rtx_time;
1702 g_mutex_unlock (&stream->priv->lock);
1708 * gst_rtsp_stream_set_retransmission_pt:
1709 * @stream: a #GstRTSPStream
1712 * Set the payload type (pt) for retransmission of this stream.
1715 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1717 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1719 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1721 g_mutex_lock (&stream->priv->lock);
1722 stream->priv->rtx_pt = rtx_pt;
1723 if (stream->priv->rtxsend) {
1724 guint pt = gst_rtsp_stream_get_pt (stream);
1725 gchar *pt_s = g_strdup_printf ("%d", pt);
1726 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1727 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1728 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1730 gst_structure_free (rtx_pt_map);
1732 g_mutex_unlock (&stream->priv->lock);
1736 * gst_rtsp_stream_get_retransmission_pt:
1737 * @stream: a #GstRTSPStream
1739 * Get the payload-type used for retransmission of this stream
1741 * Returns: The retransmission PT.
1744 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1748 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1750 g_mutex_lock (&stream->priv->lock);
1751 rtx_pt = stream->priv->rtx_pt;
1752 g_mutex_unlock (&stream->priv->lock);
1758 * gst_rtsp_stream_set_buffer_size:
1759 * @stream: a #GstRTSPStream
1760 * @size: the buffer size
1762 * Set the size of the UDP transmission buffer (in bytes)
1763 * Needs to be set before the stream is joined to a bin.
1768 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1770 g_mutex_lock (&stream->priv->lock);
1771 stream->priv->buffer_size = size;
1772 g_mutex_unlock (&stream->priv->lock);
1776 * gst_rtsp_stream_get_buffer_size:
1777 * @stream: a #GstRTSPStream
1779 * Get the size of the UDP transmission buffer (in bytes)
1781 * Returns: the size of the UDP TX buffer
1786 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1790 g_mutex_lock (&stream->priv->lock);
1791 buffer_size = stream->priv->buffer_size;
1792 g_mutex_unlock (&stream->priv->lock);
1797 /* executed from streaming thread */
1799 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1801 GstRTSPStreamPrivate *priv = stream->priv;
1802 GstCaps *newcaps, *oldcaps;
1804 newcaps = gst_pad_get_current_caps (pad);
1806 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1809 g_mutex_lock (&priv->lock);
1810 oldcaps = priv->caps;
1811 priv->caps = newcaps;
1812 g_mutex_unlock (&priv->lock);
1815 gst_caps_unref (oldcaps);
1819 dump_structure (const GstStructure * s)
1823 sstr = gst_structure_to_string (s);
1824 GST_INFO ("structure: %s", sstr);
1828 static GstRTSPStreamTransport *
1829 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1831 GstRTSPStreamPrivate *priv = stream->priv;
1833 GstRTSPStreamTransport *result = NULL;
1838 if (rtcp_from == NULL)
1841 tmp = g_strrstr (rtcp_from, ":");
1845 port = atoi (tmp + 1);
1846 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1848 g_mutex_lock (&priv->lock);
1849 GST_INFO ("finding %s:%d in %d transports", dest, port,
1850 g_list_length (priv->transports));
1852 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1853 GstRTSPStreamTransport *trans = walk->data;
1854 const GstRTSPTransport *tr;
1857 tr = gst_rtsp_stream_transport_get_transport (trans);
1859 if (priv->client_side) {
1860 /* In client side mode the 'destination' is the RTSP server, so send
1862 min = tr->server_port.min;
1863 max = tr->server_port.max;
1865 min = tr->client_port.min;
1866 max = tr->client_port.max;
1869 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1875 g_object_ref (result);
1876 g_mutex_unlock (&priv->lock);
1883 static GstRTSPStreamTransport *
1884 check_transport (GObject * source, GstRTSPStream * stream)
1886 GstStructure *stats;
1887 GstRTSPStreamTransport *trans;
1889 /* see if we have a stream to match with the origin of the RTCP packet */
1890 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1891 if (trans == NULL) {
1892 g_object_get (source, "stats", &stats, NULL);
1894 const gchar *rtcp_from;
1896 dump_structure (stats);
1898 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1899 if ((trans = find_transport (stream, rtcp_from))) {
1900 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1902 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1905 gst_structure_free (stats);
1913 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1915 GstRTSPStreamTransport *trans;
1917 GST_INFO ("%p: new source %p", stream, source);
1919 trans = check_transport (source, stream);
1922 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1926 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1928 GST_INFO ("%p: new SDES %p", stream, source);
1932 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1934 GstRTSPStreamTransport *trans;
1936 trans = check_transport (source, stream);
1939 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1940 gst_rtsp_stream_transport_keep_alive (trans);
1944 GstStructure *stats;
1945 g_object_get (source, "stats", &stats, NULL);
1947 dump_structure (stats);
1948 gst_structure_free (stats);
1955 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1957 GST_INFO ("%p: source %p bye", stream, source);
1961 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1963 GstRTSPStreamTransport *trans;
1965 GST_INFO ("%p: source %p bye timeout", stream, source);
1967 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1968 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1969 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1974 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1976 GstRTSPStreamTransport *trans;
1978 GST_INFO ("%p: source %p timeout", stream, source);
1980 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1981 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1982 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1987 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1989 GST_INFO ("%p: new sender source %p", stream, source);
1992 GstStructure *stats;
1993 g_object_get (source, "stats", &stats, NULL);
1995 dump_structure (stats);
1996 gst_structure_free (stats);
2003 on_sender_ssrc_active (GObject * session, GObject * source,
2004 GstRTSPStream * stream)
2008 GstStructure *stats;
2009 g_object_get (source, "stats", &stats, NULL);
2011 dump_structure (stats);
2012 gst_structure_free (stats);
2019 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
2022 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
2023 g_list_free (priv->tr_cache_rtp);
2024 priv->tr_cache_rtp = NULL;
2026 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
2027 g_list_free (priv->tr_cache_rtcp);
2028 priv->tr_cache_rtcp = NULL;
2032 static GstFlowReturn
2033 handle_new_sample (GstAppSink * sink, gpointer user_data)
2035 GstRTSPStreamPrivate *priv;
2039 GstRTSPStream *stream;
2042 sample = gst_app_sink_pull_sample (sink);
2046 stream = (GstRTSPStream *) user_data;
2047 priv = stream->priv;
2048 buffer = gst_sample_get_buffer (sample);
2050 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
2052 g_mutex_lock (&priv->lock);
2054 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
2055 clear_tr_cache (priv, is_rtp);
2056 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2057 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2058 priv->tr_cache_rtp =
2059 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
2061 priv->tr_cache_cookie_rtp = priv->transports_cookie;
2064 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
2065 clear_tr_cache (priv, is_rtp);
2066 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2067 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2068 priv->tr_cache_rtcp =
2069 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
2071 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
2074 g_mutex_unlock (&priv->lock);
2077 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2078 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2079 gst_rtsp_stream_transport_send_rtp (tr, buffer);
2082 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2083 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2084 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
2087 gst_sample_unref (sample);
2092 static GstAppSinkCallbacks sink_cb = {
2093 NULL, /* not interested in EOS */
2094 NULL, /* not interested in preroll samples */
2099 get_rtp_encoder (GstRTSPStream * stream, guint session)
2101 GstRTSPStreamPrivate *priv = stream->priv;
2103 if (priv->srtpenc == NULL) {
2106 name = g_strdup_printf ("srtpenc_%u", session);
2107 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2110 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2112 return gst_object_ref (priv->srtpenc);
2116 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2118 GstRTSPStreamPrivate *priv = stream->priv;
2119 GstElement *oldenc, *enc;
2123 if (priv->idx != session)
2126 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2128 oldenc = priv->srtpenc;
2129 enc = get_rtp_encoder (stream, session);
2130 name = g_strdup_printf ("rtp_sink_%d", session);
2131 pad = gst_element_get_request_pad (enc, name);
2133 gst_object_unref (pad);
2136 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2143 request_rtcp_encoder (GstElement * rtpbin, guint session,
2144 GstRTSPStream * stream)
2146 GstRTSPStreamPrivate *priv = stream->priv;
2147 GstElement *oldenc, *enc;
2151 if (priv->idx != session)
2154 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2156 oldenc = priv->srtpenc;
2157 enc = get_rtp_encoder (stream, session);
2158 name = g_strdup_printf ("rtcp_sink_%d", session);
2159 pad = gst_element_get_request_pad (enc, name);
2161 gst_object_unref (pad);
2164 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2171 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2173 GstRTSPStreamPrivate *priv = stream->priv;
2176 GST_DEBUG ("request key %08x", ssrc);
2178 g_mutex_lock (&priv->lock);
2179 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2180 gst_caps_ref (caps);
2181 g_mutex_unlock (&priv->lock);
2187 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2188 GstRTSPStream * stream)
2190 GstRTSPStreamPrivate *priv = stream->priv;
2192 if (priv->idx != session)
2195 if (priv->srtpdec == NULL) {
2198 name = g_strdup_printf ("srtpdec_%u", session);
2199 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2202 g_signal_connect (priv->srtpdec, "request-key",
2203 (GCallback) request_key, stream);
2205 return gst_object_ref (priv->srtpdec);
2209 * gst_rtsp_stream_request_aux_sender:
2210 * @stream: a #GstRTSPStream
2211 * @sessid: the session id
2213 * Creating a rtxsend bin
2215 * Returns: (transfer full): a #GstElement.
2220 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2224 GstStructure *pt_map;
2229 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2231 pt = gst_rtsp_stream_get_pt (stream);
2232 pt_s = g_strdup_printf ("%u", pt);
2233 rtx_pt = stream->priv->rtx_pt;
2235 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2237 bin = gst_bin_new (NULL);
2238 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2239 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2240 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2241 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2242 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2244 gst_structure_free (pt_map);
2245 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2247 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2248 name = g_strdup_printf ("src_%u", sessid);
2249 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2251 gst_object_unref (pad);
2253 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2254 name = g_strdup_printf ("sink_%u", sessid);
2255 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2257 gst_object_unref (pad);
2263 * gst_rtsp_stream_set_pt_map:
2264 * @stream: a #GstRTSPStream
2268 * Configure a pt map between @pt and @caps.
2271 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2273 GstRTSPStreamPrivate *priv = stream->priv;
2275 g_mutex_lock (&priv->lock);
2276 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2277 g_mutex_unlock (&priv->lock);
2281 * gst_rtsp_stream_set_publish_clock_mode:
2282 * @stream: a #GstRTSPStream
2283 * @mode: the clock publish mode
2285 * Sets if and how the stream clock should be published according to RFC7273.
2290 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
2291 GstRTSPPublishClockMode mode)
2293 GstRTSPStreamPrivate *priv;
2295 priv = stream->priv;
2296 g_mutex_lock (&priv->lock);
2297 priv->publish_clock_mode = mode;
2298 g_mutex_unlock (&priv->lock);
2302 * gst_rtsp_stream_get_publish_clock_mode:
2303 * @factory: a #GstRTSPStream
2305 * Gets if and how the stream clock should be published according to RFC7273.
2307 * Returns: The GstRTSPPublishClockMode
2311 GstRTSPPublishClockMode
2312 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
2314 GstRTSPStreamPrivate *priv;
2315 GstRTSPPublishClockMode ret;
2317 priv = stream->priv;
2318 g_mutex_lock (&priv->lock);
2319 ret = priv->publish_clock_mode;
2320 g_mutex_unlock (&priv->lock);
2326 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2327 GstRTSPStream * stream)
2329 GstRTSPStreamPrivate *priv = stream->priv;
2330 GstCaps *caps = NULL;
2332 g_mutex_lock (&priv->lock);
2334 if (priv->idx == session) {
2335 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2337 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2338 gst_caps_ref (caps);
2340 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2344 g_mutex_unlock (&priv->lock);
2350 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2352 GstRTSPStreamPrivate *priv = stream->priv;
2354 GstPadLinkReturn ret;
2357 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2358 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2360 name = gst_pad_get_name (pad);
2361 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2367 if (priv->idx != sessid)
2370 if (gst_pad_is_linked (priv->sinkpad)) {
2371 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2372 GST_DEBUG_PAD_NAME (priv->sinkpad));
2376 /* link the RTP pad to the session manager, it should not really fail unless
2377 * this is not really an RTP pad */
2378 ret = gst_pad_link (pad, priv->sinkpad);
2379 if (ret != GST_PAD_LINK_OK)
2381 priv->recv_rtp_src = gst_object_ref (pad);
2388 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2389 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2394 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2395 GstRTSPStream * stream)
2397 /* TODO: What to do here other than this? */
2398 GST_DEBUG ("Stream %p: Got EOS", stream);
2399 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2402 /* must be called with lock */
2404 create_sender_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2406 GstRTSPStreamPrivate *priv;
2407 GstPad *pad, *sinkpad = NULL;
2408 gboolean is_tcp = FALSE, is_udp = FALSE;
2411 priv = stream->priv;
2413 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2414 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2415 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2417 if (is_udp && !create_and_configure_udpsinks (stream))
2418 goto no_udp_protocol;
2420 for (i = 0; i < 2; i++) {
2421 GstPad *teepad, *queuepad;
2422 /* For the sender we create this bit of pipeline for both
2423 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2424 * we need to add a queue before appsink and udpsink to make
2425 * the pipeline not block. For the TCP case, we want to pump
2426 * client as fast as possible anyway. This pipeline is used
2427 * when both TCP and UDP are present.
2429 * .--------. .-----. .---------. .---------.
2430 * | rtpbin | | tee | | queue | | udpsink |
2431 * | send->sink src->sink src->sink |
2432 * '--------' | | '---------' '---------'
2433 * | | .---------. .---------.
2434 * | | | queue | | appsink |
2435 * | src->sink src->sink |
2436 * '-----' '---------' '---------'
2438 * When only UDP or only TCP is allowed, we skip the tee and queue
2439 * and link the udpsink (for UDP) or appsink (for TCP) directly to
2442 /* Only link the RTP send src if we're going to send RTP, link
2443 * the RTCP send src always */
2444 if (priv->srcpad || i == 1) {
2447 gst_bin_add (bin, priv->udpsink[i]);
2448 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
2453 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2454 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2455 gst_bin_add (bin, priv->appsink[i]);
2456 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2457 &sink_cb, stream, NULL);
2460 if (is_udp && is_tcp) {
2461 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2463 /* make tee for RTP/RTCP */
2464 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2465 gst_bin_add (bin, priv->tee[i]);
2467 /* and link to rtpbin send pad */
2468 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2469 gst_pad_link (priv->send_src[i], pad);
2470 gst_object_unref (pad);
2472 priv->udpqueue[i] = gst_element_factory_make ("queue", NULL);
2473 g_object_set (priv->udpqueue[i], "max-size-buffers",
2474 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0),
2476 gst_bin_add (bin, priv->udpqueue[i]);
2477 /* link tee to udpqueue */
2478 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2479 pad = gst_element_get_static_pad (priv->udpqueue[i], "sink");
2480 gst_pad_link (teepad, pad);
2481 gst_object_unref (pad);
2482 gst_object_unref (teepad);
2484 /* link udpqueue to udpsink */
2485 queuepad = gst_element_get_static_pad (priv->udpqueue[i], "src");
2486 gst_pad_link (queuepad, sinkpad);
2487 gst_object_unref (queuepad);
2488 gst_object_unref (sinkpad);
2491 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
2492 g_object_set (priv->appqueue[i], "max-size-buffers",
2493 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0),
2495 gst_bin_add (bin, priv->appqueue[i]);
2496 /* and link tee to appqueue */
2497 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2498 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
2499 gst_pad_link (teepad, pad);
2500 gst_object_unref (pad);
2501 gst_object_unref (teepad);
2503 /* and link appqueue to appsink */
2504 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
2505 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2506 gst_pad_link (queuepad, pad);
2507 gst_object_unref (pad);
2508 gst_object_unref (queuepad);
2509 } else if (is_tcp) {
2510 /* only appsink needed, link it to the session */
2511 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2512 gst_pad_link (priv->send_src[i], pad);
2513 gst_object_unref (pad);
2515 /* when its only TCP, we need to set sync and preroll to FALSE
2516 * for the sink to avoid deadlock. And this is only needed for
2517 * sink used for RTCP data, not the RTP data. */
2519 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2521 /* else only udpsink needed, link it to the session */
2522 gst_pad_link (priv->send_src[i], sinkpad);
2523 gst_object_unref (sinkpad);
2527 /* check if we need to set to a special state */
2528 if (state != GST_STATE_NULL) {
2529 if (priv->udpsink[i] && (priv->srcpad || i == 1))
2530 gst_element_set_state (priv->udpsink[i], state);
2531 if (priv->appsink[i] && (priv->srcpad || i == 1))
2532 gst_element_set_state (priv->appsink[i], state);
2533 if (priv->appqueue[i] && (priv->srcpad || i == 1))
2534 gst_element_set_state (priv->appqueue[i], state);
2535 if (priv->udpqueue[i] && (priv->srcpad || i == 1))
2536 gst_element_set_state (priv->udpqueue[i], state);
2537 if (priv->tee[i] && (priv->srcpad || i == 1))
2538 gst_element_set_state (priv->tee[i], state);
2551 /* must be called with lock */
2553 create_receiver_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2555 GstRTSPStreamPrivate *priv;
2556 GstPad *pad, *selpad;
2560 priv = stream->priv;
2562 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2564 for (i = 0; i < 2; i++) {
2565 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2566 * RTCP sink always */
2567 if (priv->sinkpad || i == 1) {
2568 /* For the receiver we create this bit of pipeline for both
2569 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2570 * and it is all funneled into the rtpbin receive pad.
2572 * .--------. .--------. .--------.
2573 * | udpsrc | | funnel | | rtpbin |
2574 * | src->sink src->sink |
2575 * '--------' | | '--------'
2579 * '--------' '--------'
2581 /* make funnel for the RTP/RTCP receivers */
2582 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2583 gst_bin_add (bin, priv->funnel[i]);
2585 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2586 gst_pad_link (pad, priv->recv_sink[i]);
2587 gst_object_unref (pad);
2589 if (priv->udpsrc_v4[i]) {
2591 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2592 * values. This is only relevant for PLAY pipelines */
2593 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
2594 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
2597 gst_bin_add (bin, priv->udpsrc_v4[i]);
2599 /* and link to the funnel v4 */
2600 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2601 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
2602 gst_pad_link (pad, selpad);
2603 gst_object_unref (pad);
2604 gst_object_unref (selpad);
2607 if (priv->udpsrc_v6[i]) {
2609 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
2610 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
2612 gst_bin_add (bin, priv->udpsrc_v6[i]);
2614 /* and link to the funnel v6 */
2615 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2616 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2617 gst_pad_link (pad, selpad);
2618 gst_object_unref (pad);
2619 gst_object_unref (selpad);
2623 /* make and add appsrc */
2624 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2625 priv->appsrc_base_time[i] = -1;
2627 gst_element_set_state (priv->appsrc[i], GST_STATE_PLAYING);
2628 gst_element_set_locked_state (priv->appsrc[i], TRUE);
2630 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
2632 gst_bin_add (bin, priv->appsrc[i]);
2633 /* and link to the funnel */
2634 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2635 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2636 gst_pad_link (pad, selpad);
2637 gst_object_unref (pad);
2638 gst_object_unref (selpad);
2642 /* check if we need to set to a special state */
2643 if (state != GST_STATE_NULL) {
2644 if (priv->funnel[i] && (priv->sinkpad || i == 1))
2645 gst_element_set_state (priv->funnel[i], state);
2651 * gst_rtsp_stream_join_bin:
2652 * @stream: a #GstRTSPStream
2653 * @bin: (transfer none): a #GstBin to join
2654 * @rtpbin: (transfer none): a rtpbin element in @bin
2655 * @state: the target state of the new elements
2657 * Join the #GstBin @bin that contains the element @rtpbin.
2659 * @stream will link to @rtpbin, which must be inside @bin. The elements
2660 * added to @bin will be set to the state given in @state.
2662 * Returns: %TRUE on success.
2665 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2666 GstElement * rtpbin, GstState state)
2668 GstRTSPStreamPrivate *priv;
2671 GstPadLinkReturn ret;
2673 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2674 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2675 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2677 priv = stream->priv;
2679 g_mutex_lock (&priv->lock);
2680 if (priv->is_joined)
2683 /* create a session with the same index as the stream */
2686 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2688 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2689 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2691 g_signal_connect (rtpbin, "request-rtp-encoder",
2692 (GCallback) request_rtp_encoder, stream);
2693 g_signal_connect (rtpbin, "request-rtcp-encoder",
2694 (GCallback) request_rtcp_encoder, stream);
2695 g_signal_connect (rtpbin, "request-rtp-decoder",
2696 (GCallback) request_rtp_rtcp_decoder, stream);
2697 g_signal_connect (rtpbin, "request-rtcp-decoder",
2698 (GCallback) request_rtp_rtcp_decoder, stream);
2701 if (priv->sinkpad) {
2702 g_signal_connect (rtpbin, "request-pt-map",
2703 (GCallback) request_pt_map, stream);
2706 /* get pads from the RTP session element for sending and receiving
2709 /* get a pad for sending RTP */
2710 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2711 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2714 /* link the RTP pad to the session manager, it should not really fail unless
2715 * this is not really an RTP pad */
2716 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2717 if (ret != GST_PAD_LINK_OK)
2720 name = g_strdup_printf ("send_rtp_src_%u", idx);
2721 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2724 /* Need to connect our sinkpad from here */
2725 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2727 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2729 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2730 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2734 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2735 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2737 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2738 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2741 /* get the session */
2742 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2744 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2746 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2748 g_signal_connect (priv->session, "on-ssrc-active",
2749 (GCallback) on_ssrc_active, stream);
2750 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2752 g_signal_connect (priv->session, "on-bye-timeout",
2753 (GCallback) on_bye_timeout, stream);
2754 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2757 /* signal for sender ssrc */
2758 g_signal_connect (priv->session, "on-new-sender-ssrc",
2759 (GCallback) on_new_sender_ssrc, stream);
2760 g_signal_connect (priv->session, "on-sender-ssrc-active",
2761 (GCallback) on_sender_ssrc_active, stream);
2763 if (!create_sender_part (stream, bin, state))
2764 goto no_udp_protocol;
2766 create_receiver_part (stream, bin, state);
2769 /* be notified of caps changes */
2770 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2771 (GCallback) caps_notify, stream);
2774 priv->joined_bin = bin;
2775 priv->is_joined = TRUE;
2776 g_mutex_unlock (&priv->lock);
2783 g_mutex_unlock (&priv->lock);
2788 GST_WARNING ("failed to link stream %u", idx);
2789 gst_object_unref (priv->send_rtp_sink);
2790 priv->send_rtp_sink = NULL;
2791 g_mutex_unlock (&priv->lock);
2796 GST_WARNING ("failed to allocate ports %u", idx);
2797 gst_object_unref (priv->send_rtp_sink);
2798 priv->send_rtp_sink = NULL;
2799 gst_object_unref (priv->send_src[0]);
2800 priv->send_src[0] = NULL;
2801 gst_object_unref (priv->send_src[1]);
2802 priv->send_src[1] = NULL;
2803 gst_object_unref (priv->recv_sink[0]);
2804 priv->recv_sink[0] = NULL;
2805 gst_object_unref (priv->recv_sink[1]);
2806 priv->recv_sink[1] = NULL;
2807 if (priv->udpsink[0])
2808 gst_element_set_state (priv->udpsink[0], GST_STATE_NULL);
2809 if (priv->udpsink[1])
2810 gst_element_set_state (priv->udpsink[1], GST_STATE_NULL);
2811 if (priv->udpsrc_v4[0]) {
2812 gst_element_set_state (priv->udpsrc_v4[0], GST_STATE_NULL);
2813 gst_object_unref (priv->udpsrc_v4[0]);
2814 priv->udpsrc_v4[0] = NULL;
2816 if (priv->udpsrc_v4[1]) {
2817 gst_element_set_state (priv->udpsrc_v4[1], GST_STATE_NULL);
2818 gst_object_unref (priv->udpsrc_v4[1]);
2819 priv->udpsrc_v4[1] = NULL;
2821 if (priv->udpsrc_mcast_v4[0]) {
2822 gst_element_set_state (priv->udpsrc_mcast_v4[0], GST_STATE_NULL);
2823 gst_object_unref (priv->udpsrc_mcast_v4[0]);
2824 priv->udpsrc_mcast_v4[0] = NULL;
2826 if (priv->udpsrc_mcast_v4[1]) {
2827 gst_element_set_state (priv->udpsrc_mcast_v4[1], GST_STATE_NULL);
2828 gst_object_unref (priv->udpsrc_mcast_v4[1]);
2829 priv->udpsrc_mcast_v4[1] = NULL;
2831 if (priv->udpsrc_v6[0]) {
2832 gst_element_set_state (priv->udpsrc_v6[0], GST_STATE_NULL);
2833 gst_object_unref (priv->udpsrc_v6[0]);
2834 priv->udpsrc_v6[0] = NULL;
2836 if (priv->udpsrc_v6[1]) {
2837 gst_element_set_state (priv->udpsrc_v6[1], GST_STATE_NULL);
2838 gst_object_unref (priv->udpsrc_v6[1]);
2839 priv->udpsrc_v6[1] = NULL;
2841 if (priv->udpsrc_mcast_v6[0]) {
2842 gst_element_set_state (priv->udpsrc_mcast_v6[0], GST_STATE_NULL);
2843 gst_object_unref (priv->udpsrc_mcast_v6[0]);
2844 priv->udpsrc_mcast_v6[0] = NULL;
2846 if (priv->udpsrc_mcast_v6[1]) {
2847 gst_element_set_state (priv->udpsrc_mcast_v6[1], GST_STATE_NULL);
2848 gst_object_unref (priv->udpsrc_mcast_v6[1]);
2849 priv->udpsrc_mcast_v6[1] = NULL;
2851 g_mutex_unlock (&priv->lock);
2857 * gst_rtsp_stream_leave_bin:
2858 * @stream: a #GstRTSPStream
2859 * @bin: (transfer none): a #GstBin
2860 * @rtpbin: (transfer none): a rtpbin #GstElement
2862 * Remove the elements of @stream from @bin.
2864 * Return: %TRUE on success.
2867 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2868 GstElement * rtpbin)
2870 GstRTSPStreamPrivate *priv;
2872 gboolean is_tcp, is_udp;
2874 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2875 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2876 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2878 priv = stream->priv;
2880 g_mutex_lock (&priv->lock);
2881 if (!priv->is_joined)
2882 goto was_not_joined;
2884 priv->joined_bin = NULL;
2886 /* all transports must be removed by now */
2887 if (priv->transports != NULL)
2888 goto transports_not_removed;
2890 clear_tr_cache (priv, TRUE);
2891 clear_tr_cache (priv, FALSE);
2893 GST_INFO ("stream %p leaving bin", stream);
2896 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2898 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2899 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2900 gst_object_unref (priv->send_rtp_sink);
2901 priv->send_rtp_sink = NULL;
2902 } else if (priv->recv_rtp_src) {
2903 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2904 gst_object_unref (priv->recv_rtp_src);
2905 priv->recv_rtp_src = NULL;
2908 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2910 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2911 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2914 for (i = 0; i < 2; i++) {
2915 if (priv->udpsink[i])
2916 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
2917 if (priv->appsink[i])
2918 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
2919 if (priv->appqueue[i])
2920 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
2921 if (priv->udpqueue[i])
2922 gst_element_set_state (priv->udpqueue[i], GST_STATE_NULL);
2924 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
2925 if (priv->funnel[i])
2926 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
2927 if (priv->appsrc[i])
2928 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2930 if (priv->udpsrc_v4[i]) {
2931 if (priv->sinkpad || i == 1) {
2932 /* and set udpsrc to NULL now before removing */
2933 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
2934 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2935 /* removing them should also nicely release the request
2936 * pads when they finalize */
2937 gst_bin_remove (bin, priv->udpsrc_v4[i]);
2939 /* we need to set the state to NULL before unref */
2940 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2941 gst_object_unref (priv->udpsrc_v4[i]);
2945 if (priv->udpsrc_mcast_v4[i]) {
2946 if (priv->sinkpad || i == 1) {
2947 /* and set udpsrc to NULL now before removing */
2948 gst_element_set_locked_state (priv->udpsrc_mcast_v4[i], FALSE);
2949 gst_element_set_state (priv->udpsrc_mcast_v4[i], GST_STATE_NULL);
2950 /* removing them should also nicely release the request
2951 * pads when they finalize */
2952 gst_bin_remove (bin, priv->udpsrc_mcast_v4[i]);
2954 gst_element_set_state (priv->udpsrc_mcast_v4[i], GST_STATE_NULL);
2955 gst_object_unref (priv->udpsrc_mcast_v4[i]);
2959 if (priv->udpsrc_v6[i]) {
2960 if (priv->sinkpad || i == 1) {
2961 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
2962 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2963 gst_bin_remove (bin, priv->udpsrc_v6[i]);
2965 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2966 gst_object_unref (priv->udpsrc_v6[i]);
2969 if (priv->udpsrc_mcast_v6[i]) {
2970 if (priv->sinkpad || i == 1) {
2971 gst_element_set_locked_state (priv->udpsrc_mcast_v6[i], FALSE);
2972 gst_element_set_state (priv->udpsrc_mcast_v6[i], GST_STATE_NULL);
2973 gst_bin_remove (bin, priv->udpsrc_mcast_v6[i]);
2975 gst_element_set_state (priv->udpsrc_mcast_v6[i], GST_STATE_NULL);
2976 gst_object_unref (priv->udpsrc_mcast_v6[i]);
2980 if (priv->udpsink[i] && is_udp && (priv->srcpad || i == 1))
2981 gst_bin_remove (bin, priv->udpsink[i]);
2982 if (priv->appsrc[i]) {
2983 if (priv->sinkpad || i == 1) {
2984 gst_element_set_locked_state (priv->appsrc[i], FALSE);
2985 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2986 gst_bin_remove (bin, priv->appsrc[i]);
2988 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2989 gst_object_unref (priv->appsrc[i]);
2992 if (priv->appsink[i] && is_tcp && (priv->srcpad || i == 1))
2993 gst_bin_remove (bin, priv->appsink[i]);
2994 if (priv->appqueue[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2995 gst_bin_remove (bin, priv->appqueue[i]);
2996 if (priv->udpqueue[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2997 gst_bin_remove (bin, priv->udpqueue[i]);
2998 if (priv->tee[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2999 gst_bin_remove (bin, priv->tee[i]);
3000 if (priv->funnel[i] && (priv->sinkpad || i == 1))
3001 gst_bin_remove (bin, priv->funnel[i]);
3003 if (priv->sinkpad || i == 1) {
3004 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
3005 gst_object_unref (priv->recv_sink[i]);
3006 priv->recv_sink[i] = NULL;
3009 priv->udpsrc_v4[i] = NULL;
3010 priv->udpsrc_v6[i] = NULL;
3011 priv->udpsrc_mcast_v4[i] = NULL;
3012 priv->udpsrc_mcast_v6[i] = NULL;
3013 priv->udpsink[i] = NULL;
3014 priv->appsrc[i] = NULL;
3015 priv->appsink[i] = NULL;
3016 priv->appqueue[i] = NULL;
3017 priv->udpqueue[i] = NULL;
3018 priv->tee[i] = NULL;
3019 priv->funnel[i] = NULL;
3023 gst_object_unref (priv->send_src[0]);
3024 priv->send_src[0] = NULL;
3027 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
3028 gst_object_unref (priv->send_src[1]);
3029 priv->send_src[1] = NULL;
3031 g_object_unref (priv->session);
3032 priv->session = NULL;
3034 gst_caps_unref (priv->caps);
3038 gst_object_unref (priv->srtpenc);
3040 gst_object_unref (priv->srtpdec);
3042 priv->is_joined = FALSE;
3043 g_mutex_unlock (&priv->lock);
3049 g_mutex_unlock (&priv->lock);
3052 transports_not_removed:
3054 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
3055 g_mutex_unlock (&priv->lock);
3061 * gst_rtsp_stream_get_joined_bin:
3062 * @stream: a #GstRTSPStream
3064 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
3066 * Return: (transfer full): the joined bin or NULL.
3069 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
3071 GstRTSPStreamPrivate *priv;
3074 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3076 priv = stream->priv;
3078 g_mutex_lock (&priv->lock);
3079 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
3080 g_mutex_unlock (&priv->lock);
3086 * gst_rtsp_stream_get_rtpinfo:
3087 * @stream: a #GstRTSPStream
3088 * @rtptime: (allow-none): result RTP timestamp
3089 * @seq: (allow-none): result RTP seqnum
3090 * @clock_rate: (allow-none): the clock rate
3091 * @running_time: (allow-none): result running-time
3093 * Retrieve the current rtptime, seq and running-time. This is used to
3094 * construct a RTPInfo reply header.
3096 * Returns: %TRUE when rtptime, seq and running-time could be determined.
3099 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
3100 guint * rtptime, guint * seq, guint * clock_rate,
3101 GstClockTime * running_time)
3103 GstRTSPStreamPrivate *priv;
3104 GstStructure *stats;
3105 GObjectClass *payobjclass;
3107 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3109 priv = stream->priv;
3111 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
3113 g_mutex_lock (&priv->lock);
3115 /* First try to extract the information from the last buffer on the sinks.
3116 * This will have a more accurate sequence number and timestamp, as between
3117 * the payloader and the sink there can be some queues
3119 if (priv->udpsink[0] || priv->appsink[0]) {
3120 GstSample *last_sample;
3122 if (priv->udpsink[0])
3123 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
3125 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
3130 GstSegment *segment;
3131 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
3133 caps = gst_sample_get_caps (last_sample);
3134 buffer = gst_sample_get_buffer (last_sample);
3135 segment = gst_sample_get_segment (last_sample);
3137 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3139 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3143 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3146 gst_rtp_buffer_unmap (&rtp_buffer);
3150 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3151 GST_BUFFER_TIMESTAMP (buffer));
3155 GstStructure *s = gst_caps_get_structure (caps, 0);
3157 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3159 if (*clock_rate == 0 && running_time)
3160 *running_time = GST_CLOCK_TIME_NONE;
3162 gst_sample_unref (last_sample);
3166 gst_sample_unref (last_sample);
3171 if (g_object_class_find_property (payobjclass, "stats")) {
3172 g_object_get (priv->payloader, "stats", &stats, NULL);
3177 gst_structure_get_uint (stats, "seqnum", seq);
3180 gst_structure_get_uint (stats, "timestamp", rtptime);
3183 gst_structure_get_clock_time (stats, "running-time", running_time);
3186 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3187 if (*clock_rate == 0 && running_time)
3188 *running_time = GST_CLOCK_TIME_NONE;
3190 gst_structure_free (stats);
3192 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3193 !g_object_class_find_property (payobjclass, "timestamp"))
3197 g_object_get (priv->payloader, "seqnum", seq, NULL);
3200 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3203 *running_time = GST_CLOCK_TIME_NONE;
3207 g_mutex_unlock (&priv->lock);
3214 GST_WARNING ("Could not get payloader stats");
3215 g_mutex_unlock (&priv->lock);
3221 * gst_rtsp_stream_get_caps:
3222 * @stream: a #GstRTSPStream
3224 * Retrieve the current caps of @stream.
3226 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
3230 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3232 GstRTSPStreamPrivate *priv;
3235 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3237 priv = stream->priv;
3239 g_mutex_lock (&priv->lock);
3240 if ((result = priv->caps))
3241 gst_caps_ref (result);
3242 g_mutex_unlock (&priv->lock);
3248 * gst_rtsp_stream_recv_rtp:
3249 * @stream: a #GstRTSPStream
3250 * @buffer: (transfer full): a #GstBuffer
3252 * Handle an RTP buffer for the stream. This method is usually called when a
3253 * message has been received from a client using the TCP transport.
3255 * This function takes ownership of @buffer.
3257 * Returns: a GstFlowReturn.
3260 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3262 GstRTSPStreamPrivate *priv;
3264 GstElement *element;
3266 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3267 priv = stream->priv;
3268 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3269 g_return_val_if_fail (priv->is_joined, FALSE);
3271 g_mutex_lock (&priv->lock);
3272 if (priv->appsrc[0])
3273 element = gst_object_ref (priv->appsrc[0]);
3276 g_mutex_unlock (&priv->lock);
3279 if (priv->appsrc_base_time[0] == -1) {
3280 /* Take current running_time. This timestamp will be put on
3281 * the first buffer of each stream because we are a live source and so we
3282 * timestamp with the running_time. When we are dealing with TCP, we also
3283 * only timestamp the first buffer (using the DISCONT flag) because a server
3284 * typically bursts data, for which we don't want to compensate by speeding
3285 * up the media. The other timestamps will be interpollated from this one
3286 * using the RTP timestamps. */
3287 GST_OBJECT_LOCK (element);
3288 if (GST_ELEMENT_CLOCK (element)) {
3290 GstClockTime base_time;
3292 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3293 base_time = GST_ELEMENT_CAST (element)->base_time;
3295 priv->appsrc_base_time[0] = now - base_time;
3296 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3297 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3298 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3299 GST_TIME_ARGS (base_time));
3301 GST_OBJECT_UNLOCK (element);
3304 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3305 gst_object_unref (element);
3313 * gst_rtsp_stream_recv_rtcp:
3314 * @stream: a #GstRTSPStream
3315 * @buffer: (transfer full): a #GstBuffer
3317 * Handle an RTCP buffer for the stream. This method is usually called when a
3318 * message has been received from a client using the TCP transport.
3320 * This function takes ownership of @buffer.
3322 * Returns: a GstFlowReturn.
3325 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3327 GstRTSPStreamPrivate *priv;
3329 GstElement *element;
3331 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3332 priv = stream->priv;
3333 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3335 if (!priv->is_joined) {
3336 gst_buffer_unref (buffer);
3337 return GST_FLOW_NOT_LINKED;
3339 g_mutex_lock (&priv->lock);
3340 if (priv->appsrc[1])
3341 element = gst_object_ref (priv->appsrc[1]);
3344 g_mutex_unlock (&priv->lock);
3347 if (priv->appsrc_base_time[1] == -1) {
3348 /* Take current running_time. This timestamp will be put on
3349 * the first buffer of each stream because we are a live source and so we
3350 * timestamp with the running_time. When we are dealing with TCP, we also
3351 * only timestamp the first buffer (using the DISCONT flag) because a server
3352 * typically bursts data, for which we don't want to compensate by speeding
3353 * up the media. The other timestamps will be interpollated from this one
3354 * using the RTP timestamps. */
3355 GST_OBJECT_LOCK (element);
3356 if (GST_ELEMENT_CLOCK (element)) {
3358 GstClockTime base_time;
3360 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3361 base_time = GST_ELEMENT_CAST (element)->base_time;
3363 priv->appsrc_base_time[1] = now - base_time;
3364 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3365 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3366 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3367 GST_TIME_ARGS (base_time));
3369 GST_OBJECT_UNLOCK (element);
3372 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3373 gst_object_unref (element);
3376 gst_buffer_unref (buffer);
3381 /* must be called with lock */
3383 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3386 GstRTSPStreamPrivate *priv = stream->priv;
3387 const GstRTSPTransport *tr;
3389 tr = gst_rtsp_stream_transport_get_transport (trans);
3391 switch (tr->lower_transport) {
3392 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3393 case GST_RTSP_LOWER_TRANS_UDP:
3399 dest = tr->destination;
3400 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3404 } else if (priv->client_side) {
3405 /* In client side mode the 'destination' is the RTSP server, so send
3407 min = tr->server_port.min;
3408 max = tr->server_port.max;
3410 min = tr->client_port.min;
3411 max = tr->client_port.max;
3416 GST_INFO ("setting ttl-mc %d", ttl);
3417 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3418 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3420 GST_INFO ("adding %s:%d-%d", dest, min, max);
3421 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3422 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3423 priv->transports = g_list_prepend (priv->transports, trans);
3425 GST_INFO ("removing %s:%d-%d", dest, min, max);
3426 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3427 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3428 priv->transports = g_list_remove (priv->transports, trans);
3430 priv->transports_cookie++;
3433 case GST_RTSP_LOWER_TRANS_TCP:
3435 GST_INFO ("adding TCP %s", tr->destination);
3436 priv->transports = g_list_prepend (priv->transports, trans);
3438 GST_INFO ("removing TCP %s", tr->destination);
3439 priv->transports = g_list_remove (priv->transports, trans);
3441 priv->transports_cookie++;
3444 goto unknown_transport;
3451 GST_INFO ("Unknown transport %d", tr->lower_transport);
3458 * gst_rtsp_stream_add_transport:
3459 * @stream: a #GstRTSPStream
3460 * @trans: (transfer none): a #GstRTSPStreamTransport
3462 * Add the transport in @trans to @stream. The media of @stream will
3463 * then also be send to the values configured in @trans.
3465 * @stream must be joined to a bin.
3467 * @trans must contain a valid #GstRTSPTransport.
3469 * Returns: %TRUE if @trans was added
3472 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3473 GstRTSPStreamTransport * trans)
3475 GstRTSPStreamPrivate *priv;
3478 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3479 priv = stream->priv;
3480 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3481 g_return_val_if_fail (priv->is_joined, FALSE);
3483 g_mutex_lock (&priv->lock);
3484 res = update_transport (stream, trans, TRUE);
3485 g_mutex_unlock (&priv->lock);
3491 * gst_rtsp_stream_remove_transport:
3492 * @stream: a #GstRTSPStream
3493 * @trans: (transfer none): a #GstRTSPStreamTransport
3495 * Remove the transport in @trans from @stream. The media of @stream will
3496 * not be sent to the values configured in @trans.
3498 * @stream must be joined to a bin.
3500 * @trans must contain a valid #GstRTSPTransport.
3502 * Returns: %TRUE if @trans was removed
3505 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3506 GstRTSPStreamTransport * trans)
3508 GstRTSPStreamPrivate *priv;
3511 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3512 priv = stream->priv;
3513 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3514 g_return_val_if_fail (priv->is_joined, FALSE);
3516 g_mutex_lock (&priv->lock);
3517 res = update_transport (stream, trans, FALSE);
3518 g_mutex_unlock (&priv->lock);
3524 * gst_rtsp_stream_update_crypto:
3525 * @stream: a #GstRTSPStream
3527 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3529 * Update the new crypto information for @ssrc in @stream. If information
3530 * for @ssrc did not exist, it will be added. If information
3531 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3532 * be removed from @stream.
3534 * Returns: %TRUE if @crypto could be updated
3537 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3538 guint ssrc, GstCaps * crypto)
3540 GstRTSPStreamPrivate *priv;
3542 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3543 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3545 priv = stream->priv;
3547 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3549 g_mutex_lock (&priv->lock);
3551 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3552 gst_caps_ref (crypto));
3554 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3555 g_mutex_unlock (&priv->lock);
3561 * gst_rtsp_stream_get_rtp_socket:
3562 * @stream: a #GstRTSPStream
3563 * @family: the socket family
3565 * Get the RTP socket from @stream for a @family.
3567 * @stream must be joined to a bin.
3569 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3570 * socket could be allocated for @family. Unref after usage
3573 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3575 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3579 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3580 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3581 family == G_SOCKET_FAMILY_IPV6, NULL);
3582 g_return_val_if_fail (priv->udpsink[0], NULL);
3584 if (family == G_SOCKET_FAMILY_IPV6)
3589 g_object_get (priv->udpsink[0], name, &socket, NULL);
3595 * gst_rtsp_stream_get_rtcp_socket:
3596 * @stream: a #GstRTSPStream
3597 * @family: the socket family
3599 * Get the RTCP socket from @stream for a @family.
3601 * @stream must be joined to a bin.
3603 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3604 * socket could be allocated for @family. Unref after usage
3607 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3609 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3613 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3614 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3615 family == G_SOCKET_FAMILY_IPV6, NULL);
3616 g_return_val_if_fail (priv->udpsink[1], NULL);
3618 if (family == G_SOCKET_FAMILY_IPV6)
3623 g_object_get (priv->udpsink[1], name, &socket, NULL);
3629 * gst_rtsp_stream_set_seqnum:
3630 * @stream: a #GstRTSPStream
3631 * @seqnum: a new sequence number
3633 * Configure the sequence number in the payloader of @stream to @seqnum.
3636 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3638 GstRTSPStreamPrivate *priv;
3640 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3642 priv = stream->priv;
3644 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3648 * gst_rtsp_stream_get_seqnum:
3649 * @stream: a #GstRTSPStream
3651 * Get the configured sequence number in the payloader of @stream.
3653 * Returns: the sequence number of the payloader.
3656 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3658 GstRTSPStreamPrivate *priv;
3661 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3663 priv = stream->priv;
3665 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3671 * gst_rtsp_stream_transport_filter:
3672 * @stream: a #GstRTSPStream
3673 * @func: (scope call) (allow-none): a callback
3674 * @user_data: (closure): user data passed to @func
3676 * Call @func for each transport managed by @stream. The result value of @func
3677 * determines what happens to the transport. @func will be called with @stream
3678 * locked so no further actions on @stream can be performed from @func.
3680 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3683 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3685 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3686 * will also be added with an additional ref to the result #GList of this
3689 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3691 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3692 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3693 * element in the #GList should be unreffed before the list is freed.
3696 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3697 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3699 GstRTSPStreamPrivate *priv;
3700 GList *result, *walk, *next;
3701 GHashTable *visited = NULL;
3704 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3706 priv = stream->priv;
3710 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3712 g_mutex_lock (&priv->lock);
3714 cookie = priv->transports_cookie;
3715 for (walk = priv->transports; walk; walk = next) {
3716 GstRTSPStreamTransport *trans = walk->data;
3717 GstRTSPFilterResult res;
3720 next = g_list_next (walk);
3723 /* only visit each transport once */
3724 if (g_hash_table_contains (visited, trans))
3727 g_hash_table_add (visited, g_object_ref (trans));
3728 g_mutex_unlock (&priv->lock);
3730 res = func (stream, trans, user_data);
3732 g_mutex_lock (&priv->lock);
3734 res = GST_RTSP_FILTER_REF;
3736 changed = (cookie != priv->transports_cookie);
3739 case GST_RTSP_FILTER_REMOVE:
3740 update_transport (stream, trans, FALSE);
3742 case GST_RTSP_FILTER_REF:
3743 result = g_list_prepend (result, g_object_ref (trans));
3745 case GST_RTSP_FILTER_KEEP:
3752 g_mutex_unlock (&priv->lock);
3755 g_hash_table_unref (visited);
3760 static GstPadProbeReturn
3761 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3763 GstRTSPStreamPrivate *priv;
3764 GstRTSPStream *stream;
3767 priv = stream->priv;
3769 GST_DEBUG_OBJECT (pad, "now blocking");
3771 g_mutex_lock (&priv->lock);
3772 priv->blocking = TRUE;
3773 g_mutex_unlock (&priv->lock);
3775 gst_element_post_message (priv->payloader,
3776 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3777 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3779 return GST_PAD_PROBE_OK;
3783 * gst_rtsp_stream_set_blocked:
3784 * @stream: a #GstRTSPStream
3785 * @blocked: boolean indicating we should block or unblock
3787 * Blocks or unblocks the dataflow on @stream.
3789 * Returns: %TRUE on success
3792 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3794 GstRTSPStreamPrivate *priv;
3796 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3798 priv = stream->priv;
3800 g_mutex_lock (&priv->lock);
3802 priv->blocking = FALSE;
3803 if (priv->blocked_id == 0) {
3804 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3805 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3806 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3807 g_object_ref (stream), g_object_unref);
3810 if (priv->blocked_id != 0) {
3811 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3812 priv->blocked_id = 0;
3813 priv->blocking = FALSE;
3816 g_mutex_unlock (&priv->lock);
3822 * gst_rtsp_stream_is_blocking:
3823 * @stream: a #GstRTSPStream
3825 * Check if @stream is blocking on a #GstBuffer.
3827 * Returns: %TRUE if @stream is blocking
3830 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3832 GstRTSPStreamPrivate *priv;
3835 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3837 priv = stream->priv;
3839 g_mutex_lock (&priv->lock);
3840 result = priv->blocking;
3841 g_mutex_unlock (&priv->lock);
3847 * gst_rtsp_stream_query_position:
3848 * @stream: a #GstRTSPStream
3850 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3851 * the RTP parts of the pipeline and not the RTCP parts.
3853 * Returns: %TRUE if the position could be queried
3856 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3858 GstRTSPStreamPrivate *priv;
3862 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3864 priv = stream->priv;
3866 g_mutex_lock (&priv->lock);
3867 /* depending on the transport type, it should query corresponding sink */
3868 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3869 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3870 sink = priv->udpsink[0];
3872 sink = priv->appsink[0];
3875 gst_object_ref (sink);
3876 g_mutex_unlock (&priv->lock);
3881 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3882 gst_object_unref (sink);
3888 * gst_rtsp_stream_query_stop:
3889 * @stream: a #GstRTSPStream
3891 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3892 * the RTP parts of the pipeline and not the RTCP parts.
3894 * Returns: %TRUE if the stop could be queried
3897 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3899 GstRTSPStreamPrivate *priv;
3904 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3906 priv = stream->priv;
3908 g_mutex_lock (&priv->lock);
3909 /* depending on the transport type, it should query corresponding sink */
3910 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3911 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3912 sink = priv->udpsink[0];
3914 sink = priv->appsink[0];
3917 gst_object_ref (sink);
3918 g_mutex_unlock (&priv->lock);
3923 query = gst_query_new_segment (GST_FORMAT_TIME);
3924 if ((ret = gst_element_query (sink, query))) {
3927 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3928 if (format != GST_FORMAT_TIME)
3931 gst_query_unref (query);
3932 gst_object_unref (sink);