2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3 * Copyright (C) 2015 Centricular Ltd
4 * Author: Sebastian Dröge <sebastian@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * @short_description: A media stream
24 * @see_also: #GstRTSPMedia
26 * The #GstRTSPStream object manages the data transport for one stream. It
27 * is created from a payloader element and a source pad that produce the RTP
28 * packets for the stream.
30 * With gst_rtsp_stream_join_bin() the streaming elements are added to the bin
31 * and rtpbin. gst_rtsp_stream_leave_bin() removes the elements again.
33 * The #GstRTSPStream will use the configured addresspool, as set with
34 * gst_rtsp_stream_set_address_pool(), to allocate multicast addresses for the
35 * stream. With gst_rtsp_stream_get_multicast_address() you can get the
38 * With gst_rtsp_stream_get_server_port () you can get the port that the server
39 * will use to receive RTCP. This is the part that the clients will use to send
42 * With gst_rtsp_stream_add_transport() destinations can be added where the
43 * stream should be sent to. Use gst_rtsp_stream_remove_transport() to remove
44 * the destination again.
46 * Last reviewed on 2013-07-16 (1.0.0)
55 #include <gst/app/gstappsrc.h>
56 #include <gst/app/gstappsink.h>
58 #include <gst/rtp/gstrtpbuffer.h>
60 #include "rtsp-stream.h"
62 #define GST_RTSP_STREAM_GET_PRIVATE(obj) \
63 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
65 struct _GstRTSPStreamPrivate
69 /* Only one pad is ever set */
70 GstPad *srcpad, *sinkpad;
71 GstElement *payloader;
75 /* TRUE if this stream is running on
76 * the client side of an RTSP link (for RECORD) */
80 GstRTSPProfile profiles;
81 GstRTSPLowerTrans protocols;
83 /* pads on the rtpbin */
84 GstPad *send_rtp_sink;
89 /* the RTPSession object */
92 /* SRTP encoder/decoder */
98 GstElement *udpsrc_v4[2];
99 GstElement *udpsrc_v6[2];
100 GstElement *udpqueue[2];
101 GstElement *udpsink[2];
103 /* for UDP multicast */
104 GstElement *mcast_udpsrc_v4[2];
105 GstElement *mcast_udpsrc_v6[2];
106 GstElement *mcast_udpqueue[2];
107 GstElement *mcast_udpsink[2];
109 /* for TCP transport */
110 GstElement *appsrc[2];
111 GstClockTime appsrc_base_time[2];
112 GstElement *appqueue[2];
113 GstElement *appsink[2];
116 GstElement *funnel[2];
121 GstClockTime rtx_time;
123 /* pool used to manage unicast and multicast addresses */
124 GstRTSPAddressPool *pool;
126 /* unicast server addr/port */
127 GstRTSPAddress *server_addr_v4;
128 GstRTSPAddress *server_addr_v6;
130 /* multicast addresses */
131 GstRTSPAddress *mcast_addr_v4;
132 GstRTSPAddress *mcast_addr_v6;
134 gchar *multicast_iface;
136 /* the caps of the stream */
140 /* transports we stream to */
143 guint transports_cookie;
145 GList *tr_cache_rtcp;
146 guint tr_cache_cookie_rtp;
147 guint tr_cache_cookie_rtcp;
151 /* stream blocking */
152 gulong blocked_id[2];
155 /* pt->caps map for RECORD streams */
158 GstRTSPPublishClockMode publish_clock_mode;
161 #define DEFAULT_CONTROL NULL
162 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
163 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
164 GST_RTSP_LOWER_TRANS_TCP
177 SIGNAL_NEW_RTP_ENCODER,
178 SIGNAL_NEW_RTCP_ENCODER,
182 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
183 #define GST_CAT_DEFAULT rtsp_stream_debug
185 static GQuark ssrc_stream_map_key;
187 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
188 GValue * value, GParamSpec * pspec);
189 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
190 const GValue * value, GParamSpec * pspec);
192 static void gst_rtsp_stream_finalize (GObject * obj);
194 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
196 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
199 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
201 GObjectClass *gobject_class;
203 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
205 gobject_class = G_OBJECT_CLASS (klass);
207 gobject_class->get_property = gst_rtsp_stream_get_property;
208 gobject_class->set_property = gst_rtsp_stream_set_property;
209 gobject_class->finalize = gst_rtsp_stream_finalize;
211 g_object_class_install_property (gobject_class, PROP_CONTROL,
212 g_param_spec_string ("control", "Control",
213 "The control string for this stream", DEFAULT_CONTROL,
214 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_PROFILES,
217 g_param_spec_flags ("profiles", "Profiles",
218 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
219 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
222 g_param_spec_flags ("protocols", "Protocols",
223 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
224 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
226 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
227 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
228 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
229 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
231 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
232 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
233 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
234 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
236 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
238 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
242 gst_rtsp_stream_init (GstRTSPStream * stream)
244 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
246 GST_DEBUG ("new stream %p", stream);
251 priv->control = g_strdup (DEFAULT_CONTROL);
252 priv->profiles = DEFAULT_PROFILES;
253 priv->protocols = DEFAULT_PROTOCOLS;
254 priv->publish_clock_mode = GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK;
256 g_mutex_init (&priv->lock);
258 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
259 NULL, (GDestroyNotify) gst_caps_unref);
260 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
261 (GDestroyNotify) gst_caps_unref);
265 gst_rtsp_stream_finalize (GObject * obj)
267 GstRTSPStream *stream;
268 GstRTSPStreamPrivate *priv;
270 stream = GST_RTSP_STREAM (obj);
273 GST_DEBUG ("finalize stream %p", stream);
275 /* we really need to be unjoined now */
276 g_return_if_fail (priv->joined_bin == NULL);
278 if (priv->mcast_addr_v4)
279 gst_rtsp_address_free (priv->mcast_addr_v4);
280 if (priv->mcast_addr_v6)
281 gst_rtsp_address_free (priv->mcast_addr_v6);
282 if (priv->server_addr_v4)
283 gst_rtsp_address_free (priv->server_addr_v4);
284 if (priv->server_addr_v6)
285 gst_rtsp_address_free (priv->server_addr_v6);
287 g_object_unref (priv->pool);
289 g_object_unref (priv->rtxsend);
291 g_free (priv->multicast_iface);
293 gst_object_unref (priv->payloader);
295 gst_object_unref (priv->srcpad);
297 gst_object_unref (priv->sinkpad);
298 g_free (priv->control);
299 g_mutex_clear (&priv->lock);
301 g_hash_table_unref (priv->keys);
302 g_hash_table_destroy (priv->ptmap);
304 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
308 gst_rtsp_stream_get_property (GObject * object, guint propid,
309 GValue * value, GParamSpec * pspec)
311 GstRTSPStream *stream = GST_RTSP_STREAM (object);
315 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
318 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
321 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
324 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
329 gst_rtsp_stream_set_property (GObject * object, guint propid,
330 const GValue * value, GParamSpec * pspec)
332 GstRTSPStream *stream = GST_RTSP_STREAM (object);
336 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
339 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
342 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
345 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
350 * gst_rtsp_stream_new:
353 * @payloader: a #GstElement
355 * Create a new media stream with index @idx that handles RTP data on
356 * @pad and has a payloader element @payloader if @pad is a source pad
357 * or a depayloader element @payloader if @pad is a sink pad.
359 * Returns: (transfer full): a new #GstRTSPStream
362 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
364 GstRTSPStreamPrivate *priv;
365 GstRTSPStream *stream;
367 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
368 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
370 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
373 priv->payloader = gst_object_ref (payloader);
374 if (GST_PAD_IS_SRC (pad))
375 priv->srcpad = gst_object_ref (pad);
377 priv->sinkpad = gst_object_ref (pad);
383 * gst_rtsp_stream_get_index:
384 * @stream: a #GstRTSPStream
386 * Get the stream index.
388 * Return: the stream index.
391 gst_rtsp_stream_get_index (GstRTSPStream * stream)
393 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
395 return stream->priv->idx;
399 * gst_rtsp_stream_get_pt:
400 * @stream: a #GstRTSPStream
402 * Get the stream payload type.
404 * Return: the stream payload type.
407 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
409 GstRTSPStreamPrivate *priv;
412 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
416 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
422 * gst_rtsp_stream_get_srcpad:
423 * @stream: a #GstRTSPStream
425 * Get the srcpad associated with @stream.
427 * Returns: (transfer full): the srcpad. Unref after usage.
430 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
432 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
434 if (!stream->priv->srcpad)
437 return gst_object_ref (stream->priv->srcpad);
441 * gst_rtsp_stream_get_sinkpad:
442 * @stream: a #GstRTSPStream
444 * Get the sinkpad associated with @stream.
446 * Returns: (transfer full): the sinkpad. Unref after usage.
449 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
451 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
453 if (!stream->priv->sinkpad)
456 return gst_object_ref (stream->priv->sinkpad);
460 * gst_rtsp_stream_get_control:
461 * @stream: a #GstRTSPStream
463 * Get the control string to identify this stream.
465 * Returns: (transfer full): the control string. g_free() after usage.
468 gst_rtsp_stream_get_control (GstRTSPStream * stream)
470 GstRTSPStreamPrivate *priv;
473 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
477 g_mutex_lock (&priv->lock);
478 if ((result = g_strdup (priv->control)) == NULL)
479 result = g_strdup_printf ("stream=%u", priv->idx);
480 g_mutex_unlock (&priv->lock);
486 * gst_rtsp_stream_set_control:
487 * @stream: a #GstRTSPStream
488 * @control: a control string
490 * Set the control string in @stream.
493 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
495 GstRTSPStreamPrivate *priv;
497 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
501 g_mutex_lock (&priv->lock);
502 g_free (priv->control);
503 priv->control = g_strdup (control);
504 g_mutex_unlock (&priv->lock);
508 * gst_rtsp_stream_has_control:
509 * @stream: a #GstRTSPStream
510 * @control: a control string
512 * Check if @stream has the control string @control.
514 * Returns: %TRUE is @stream has @control as the control string
517 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
519 GstRTSPStreamPrivate *priv;
522 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
526 g_mutex_lock (&priv->lock);
528 res = (g_strcmp0 (priv->control, control) == 0);
532 if (sscanf (control, "stream=%u", &streamid) > 0)
533 res = (streamid == priv->idx);
537 g_mutex_unlock (&priv->lock);
543 * gst_rtsp_stream_set_mtu:
544 * @stream: a #GstRTSPStream
547 * Configure the mtu in the payloader of @stream to @mtu.
550 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
552 GstRTSPStreamPrivate *priv;
554 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
558 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
560 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
564 * gst_rtsp_stream_get_mtu:
565 * @stream: a #GstRTSPStream
567 * Get the configured MTU in the payloader of @stream.
569 * Returns: the MTU of the payloader.
572 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
574 GstRTSPStreamPrivate *priv;
577 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
581 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
586 /* Update the dscp qos property on the udp sinks */
588 update_dscp_qos (GstRTSPStream * stream, GstElement * udpsink[2])
590 GstRTSPStreamPrivate *priv;
595 g_object_set (G_OBJECT (udpsink[0]), "qos-dscp", priv->dscp_qos, NULL);
599 g_object_set (G_OBJECT (udpsink[1]), "qos-dscp", priv->dscp_qos, NULL);
604 * gst_rtsp_stream_set_dscp_qos:
605 * @stream: a #GstRTSPStream
606 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
608 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
611 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
613 GstRTSPStreamPrivate *priv;
615 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
619 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
621 if (dscp_qos < -1 || dscp_qos > 63) {
622 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
626 priv->dscp_qos = dscp_qos;
628 update_dscp_qos (stream, priv->udpsink);
632 * gst_rtsp_stream_get_dscp_qos:
633 * @stream: a #GstRTSPStream
635 * Get the configured DSCP QoS in of the outgoing sockets.
637 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
640 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
642 GstRTSPStreamPrivate *priv;
644 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
648 return priv->dscp_qos;
652 * gst_rtsp_stream_is_transport_supported:
653 * @stream: a #GstRTSPStream
654 * @transport: (transfer none): a #GstRTSPTransport
656 * Check if @transport can be handled by stream
658 * Returns: %TRUE if @transport can be handled by @stream.
661 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
662 GstRTSPTransport * transport)
664 GstRTSPStreamPrivate *priv;
666 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
670 g_mutex_lock (&priv->lock);
671 if (transport->trans != GST_RTSP_TRANS_RTP)
672 goto unsupported_transmode;
674 if (!(transport->profile & priv->profiles))
675 goto unsupported_profile;
677 if (!(transport->lower_transport & priv->protocols))
678 goto unsupported_ltrans;
680 g_mutex_unlock (&priv->lock);
685 unsupported_transmode:
687 GST_DEBUG ("unsupported transport mode %d", transport->trans);
688 g_mutex_unlock (&priv->lock);
693 GST_DEBUG ("unsupported profile %d", transport->profile);
694 g_mutex_unlock (&priv->lock);
699 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
700 g_mutex_unlock (&priv->lock);
706 * gst_rtsp_stream_set_profiles:
707 * @stream: a #GstRTSPStream
708 * @profiles: the new profiles
710 * Configure the allowed profiles for @stream.
713 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
715 GstRTSPStreamPrivate *priv;
717 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
721 g_mutex_lock (&priv->lock);
722 priv->profiles = profiles;
723 g_mutex_unlock (&priv->lock);
727 * gst_rtsp_stream_get_profiles:
728 * @stream: a #GstRTSPStream
730 * Get the allowed profiles of @stream.
732 * Returns: a #GstRTSPProfile
735 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
737 GstRTSPStreamPrivate *priv;
740 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
744 g_mutex_lock (&priv->lock);
745 res = priv->profiles;
746 g_mutex_unlock (&priv->lock);
752 * gst_rtsp_stream_set_protocols:
753 * @stream: a #GstRTSPStream
754 * @protocols: the new flags
756 * Configure the allowed lower transport for @stream.
759 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
760 GstRTSPLowerTrans protocols)
762 GstRTSPStreamPrivate *priv;
764 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
768 g_mutex_lock (&priv->lock);
769 priv->protocols = protocols;
770 g_mutex_unlock (&priv->lock);
774 * gst_rtsp_stream_get_protocols:
775 * @stream: a #GstRTSPStream
777 * Get the allowed protocols of @stream.
779 * Returns: a #GstRTSPLowerTrans
782 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
784 GstRTSPStreamPrivate *priv;
785 GstRTSPLowerTrans res;
787 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
788 GST_RTSP_LOWER_TRANS_UNKNOWN);
792 g_mutex_lock (&priv->lock);
793 res = priv->protocols;
794 g_mutex_unlock (&priv->lock);
800 * gst_rtsp_stream_set_address_pool:
801 * @stream: a #GstRTSPStream
802 * @pool: (transfer none): a #GstRTSPAddressPool
804 * configure @pool to be used as the address pool of @stream.
807 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
808 GstRTSPAddressPool * pool)
810 GstRTSPStreamPrivate *priv;
811 GstRTSPAddressPool *old;
813 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
817 GST_LOG_OBJECT (stream, "set address pool %p", pool);
819 g_mutex_lock (&priv->lock);
820 if ((old = priv->pool) != pool)
821 priv->pool = pool ? g_object_ref (pool) : NULL;
824 g_mutex_unlock (&priv->lock);
827 g_object_unref (old);
831 * gst_rtsp_stream_get_address_pool:
832 * @stream: a #GstRTSPStream
834 * Get the #GstRTSPAddressPool used as the address pool of @stream.
836 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
840 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
842 GstRTSPStreamPrivate *priv;
843 GstRTSPAddressPool *result;
845 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
849 g_mutex_lock (&priv->lock);
850 if ((result = priv->pool))
851 g_object_ref (result);
852 g_mutex_unlock (&priv->lock);
858 * gst_rtsp_stream_set_multicast_iface:
859 * @stream: a #GstRTSPStream
860 * @multicast_iface: (transfer none): a multicast interface name
862 * configure @multicast_iface to be used for @stream.
865 gst_rtsp_stream_set_multicast_iface (GstRTSPStream * stream,
866 const gchar * multicast_iface)
868 GstRTSPStreamPrivate *priv;
871 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
875 GST_LOG_OBJECT (stream, "set multicast iface %s",
876 GST_STR_NULL (multicast_iface));
878 g_mutex_lock (&priv->lock);
879 if ((old = priv->multicast_iface) != multicast_iface)
880 priv->multicast_iface = multicast_iface ? g_strdup (multicast_iface) : NULL;
883 g_mutex_unlock (&priv->lock);
890 * gst_rtsp_stream_get_multicast_iface:
891 * @stream: a #GstRTSPStream
893 * Get the multicast interface used for @stream.
895 * Returns: (transfer full): the multicast interface for @stream. g_free() after
899 gst_rtsp_stream_get_multicast_iface (GstRTSPStream * stream)
901 GstRTSPStreamPrivate *priv;
904 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
908 g_mutex_lock (&priv->lock);
909 if ((result = priv->multicast_iface))
910 result = g_strdup (result);
911 g_mutex_unlock (&priv->lock);
917 * gst_rtsp_stream_get_multicast_address:
918 * @stream: a #GstRTSPStream
919 * @family: the #GSocketFamily
921 * Get the multicast address of @stream for @family. The original
922 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
923 * won't release the address from the pool.
925 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
926 * or %NULL when no address could be allocated. gst_rtsp_address_free()
930 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
931 GSocketFamily family)
933 GstRTSPStreamPrivate *priv;
934 GstRTSPAddress *result;
935 GstRTSPAddress **addrp;
936 GstRTSPAddressFlags flags;
938 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
942 g_mutex_lock (&stream->priv->lock);
944 if (family == G_SOCKET_FAMILY_IPV6) {
945 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
946 addrp = &priv->mcast_addr_v6;
948 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
949 addrp = &priv->mcast_addr_v4;
952 if (*addrp == NULL) {
953 if (priv->pool == NULL)
956 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
958 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
962 /* FIXME: Also reserve the same port with unicast ANY address, since that's
963 * where we are going to bind our socket. Probably loop until we find a port
964 * available in both mcast and unicast pools. Maybe GstRTSPAddressPool
965 * should do it for us when both GST_RTSP_ADDRESS_FLAG_MULTICAST and
966 * GST_RTSP_ADDRESS_FLAG_UNICAST are givent. */
968 result = gst_rtsp_address_copy (*addrp);
970 g_mutex_unlock (&stream->priv->lock);
977 GST_ERROR_OBJECT (stream, "no address pool specified");
978 g_mutex_unlock (&stream->priv->lock);
983 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
984 g_mutex_unlock (&stream->priv->lock);
990 * gst_rtsp_stream_reserve_address:
991 * @stream: a #GstRTSPStream
992 * @address: an address
997 * Reserve @address and @port as the address and port of @stream. The original
998 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
999 * won't release the address from the pool.
1001 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
1002 * the address could be reserved. gst_rtsp_address_free() after usage.
1005 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
1006 const gchar * address, guint port, guint n_ports, guint ttl)
1008 GstRTSPStreamPrivate *priv;
1009 GstRTSPAddress *result;
1011 GSocketFamily family;
1012 GstRTSPAddress **addrp;
1014 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1015 g_return_val_if_fail (address != NULL, NULL);
1016 g_return_val_if_fail (port > 0, NULL);
1017 g_return_val_if_fail (n_ports > 0, NULL);
1018 g_return_val_if_fail (ttl > 0, NULL);
1020 priv = stream->priv;
1022 addr = g_inet_address_new_from_string (address);
1024 GST_ERROR ("failed to get inet addr from %s", address);
1025 family = G_SOCKET_FAMILY_IPV4;
1027 family = g_inet_address_get_family (addr);
1028 g_object_unref (addr);
1031 if (family == G_SOCKET_FAMILY_IPV6)
1032 addrp = &priv->mcast_addr_v6;
1034 addrp = &priv->mcast_addr_v4;
1036 g_mutex_lock (&priv->lock);
1037 if (*addrp == NULL) {
1038 GstRTSPAddressPoolResult res;
1040 if (priv->pool == NULL)
1043 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
1044 port, n_ports, ttl, addrp);
1045 if (res != GST_RTSP_ADDRESS_POOL_OK)
1048 /* FIXME: Also reserve the same port with unicast ANY address, since that's
1049 * where we are going to bind our socket. */
1051 if (g_ascii_strcasecmp ((*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,
1078 "address %s is not the same as %s that was already reserved",
1079 address, (*addrp)->address);
1080 g_mutex_unlock (&priv->lock);
1085 /* must be called with lock */
1087 set_sockets_for_udpsinks (GstElement * udpsink[2], GSocket * rtp_socket,
1088 GSocket * rtcp_socket, GSocketFamily family)
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 (udpsink[0]), multisink_socket, rtp_socket, NULL);
1098 g_object_set (G_OBJECT (udpsink[1]), multisink_socket, rtcp_socket, NULL);
1102 create_and_configure_udpsinks (GstRTSPStream * stream, GstElement * udpsink[2])
1104 GstRTSPStreamPrivate *priv = stream->priv;
1105 GstElement *udpsink0, *udpsink1;
1107 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1108 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1110 if (!udpsink0 || !udpsink1)
1111 goto no_udp_protocol;
1113 /* configure sinks */
1115 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1116 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1118 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1119 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1121 g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
1123 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1124 /* Needs to be async for RECORD streams, otherwise we will never go to
1125 * PLAYING because the sinks will wait for data while the udpsrc can't
1126 * provide data with timestamps in PAUSED. */
1128 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1129 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1131 /* join multicast group when adding clients, so we'll start receiving from it.
1132 * We cannot rely on the udpsrc to join the group since its socket is always a
1133 * local unicast one. */
1134 g_object_set (G_OBJECT (udpsink0), "auto-multicast", TRUE, NULL);
1135 g_object_set (G_OBJECT (udpsink1), "auto-multicast", TRUE, NULL);
1137 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1138 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1140 udpsink[0] = udpsink0;
1141 udpsink[1] = udpsink1;
1143 /* update the dscp qos field in the sinks */
1144 update_dscp_qos (stream, udpsink);
1155 /* must be called with lock */
1157 create_and_configure_udpsources (GstElement * udpsrc_out[2],
1158 GSocket * rtp_socket, GSocket * rtcp_socket)
1160 GstStateChangeReturn ret;
1162 udpsrc_out[0] = gst_element_factory_make ("udpsrc", NULL);
1163 udpsrc_out[1] = gst_element_factory_make ("udpsrc", NULL);
1165 if (udpsrc_out[0] == NULL || udpsrc_out[1] == NULL)
1168 g_object_set (G_OBJECT (udpsrc_out[0]), "socket", rtp_socket, NULL);
1169 g_object_set (G_OBJECT (udpsrc_out[1]), "socket", rtcp_socket, NULL);
1171 /* The udpsrc cannot do the join because its socket is always a local unicast
1172 * one. The udpsink sharing the same socket will do it for us. */
1173 g_object_set (G_OBJECT (udpsrc_out[0]), "auto-multicast", FALSE, NULL);
1174 g_object_set (G_OBJECT (udpsrc_out[1]), "auto-multicast", FALSE, NULL);
1176 g_object_set (G_OBJECT (udpsrc_out[0]), "loop", FALSE, NULL);
1177 g_object_set (G_OBJECT (udpsrc_out[1]), "loop", FALSE, NULL);
1179 g_object_set (G_OBJECT (udpsrc_out[0]), "close-socket", FALSE, NULL);
1180 g_object_set (G_OBJECT (udpsrc_out[1]), "close-socket", FALSE, NULL);
1182 ret = gst_element_set_state (udpsrc_out[0], GST_STATE_READY);
1183 if (ret == GST_STATE_CHANGE_FAILURE)
1185 ret = gst_element_set_state (udpsrc_out[1], GST_STATE_READY);
1186 if (ret == GST_STATE_CHANGE_FAILURE)
1194 if (udpsrc_out[0]) {
1195 gst_element_set_state (udpsrc_out[0], GST_STATE_NULL);
1196 g_clear_object (&udpsrc_out[0]);
1198 if (udpsrc_out[1]) {
1199 gst_element_set_state (udpsrc_out[1], GST_STATE_NULL);
1200 g_clear_object (&udpsrc_out[1]);
1207 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1208 GstElement * udpsrc_out[2], GstElement * udpsink_out[2],
1209 GstRTSPAddress ** server_addr_out, gboolean multicast)
1211 GstRTSPStreamPrivate *priv = stream->priv;
1212 GSocket *rtp_socket = NULL;
1213 GSocket *rtcp_socket;
1214 gint tmp_rtp, tmp_rtcp;
1216 gint rtpport, rtcpport;
1217 GList *rejected_addresses = NULL;
1218 GstRTSPAddress *addr = NULL;
1219 GInetAddress *inetaddr = NULL;
1221 GSocketAddress *rtp_sockaddr = NULL;
1222 GSocketAddress *rtcp_sockaddr = NULL;
1223 GstRTSPAddressPool *pool;
1225 g_assert (!udpsrc_out[0]);
1226 g_assert (!udpsrc_out[1]);
1227 g_assert ((!udpsink_out[0] && !udpsink_out[1]) ||
1228 (udpsink_out[0] && udpsink_out[1]));
1229 g_assert (*server_addr_out == NULL);
1234 /* Start with random port */
1237 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1238 G_SOCKET_PROTOCOL_UDP, NULL);
1240 goto no_udp_protocol;
1241 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1243 /* try to allocate 2 UDP ports, the RTP port should be an even
1244 * number and the RTCP port should be the next (uneven) port */
1247 if (rtp_socket == NULL) {
1248 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1249 G_SOCKET_PROTOCOL_UDP, NULL);
1251 goto no_udp_protocol;
1252 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1255 if ((pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) || multicast) {
1256 GstRTSPAddressFlags flags;
1259 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1264 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1266 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1268 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1270 if (family == G_SOCKET_FAMILY_IPV6)
1271 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1273 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1275 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1280 tmp_rtp = addr->port;
1282 g_clear_object (&inetaddr);
1284 inetaddr = g_inet_address_new_any (family);
1286 inetaddr = g_inet_address_new_from_string (addr->address);
1294 if (inetaddr == NULL)
1295 inetaddr = g_inet_address_new_any (family);
1298 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1299 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1300 g_object_unref (rtp_sockaddr);
1303 g_object_unref (rtp_sockaddr);
1305 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1306 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1307 g_clear_object (&rtp_sockaddr);
1312 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1313 g_object_unref (rtp_sockaddr);
1315 /* check if port is even */
1316 if ((tmp_rtp & 1) != 0) {
1317 /* port not even, close and allocate another */
1319 g_clear_object (&rtp_socket);
1324 tmp_rtcp = tmp_rtp + 1;
1326 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1327 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1328 g_object_unref (rtcp_sockaddr);
1329 g_clear_object (&rtp_socket);
1332 g_object_unref (rtcp_sockaddr);
1335 addr = g_slice_new0 (GstRTSPAddress);
1336 addr->address = g_inet_address_to_string (inetaddr);
1337 addr->port = tmp_rtp;
1341 addr_str = addr->address;
1342 g_clear_object (&inetaddr);
1344 if (!create_and_configure_udpsources (udpsrc_out, rtp_socket, rtcp_socket)) {
1345 goto no_udp_protocol;
1348 g_object_get (G_OBJECT (udpsrc_out[0]), "port", &rtpport, NULL);
1349 g_object_get (G_OBJECT (udpsrc_out[1]), "port", &rtcpport, NULL);
1351 /* this should not happen... */
1352 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1355 /* This function is called twice (for v4 and v6) but we create only one pair
1358 && !create_and_configure_udpsinks (stream, udpsink_out))
1359 goto no_udp_protocol;
1362 g_object_set (G_OBJECT (udpsink_out[0]), "multicast-iface",
1363 priv->multicast_iface, NULL);
1364 g_object_set (G_OBJECT (udpsink_out[1]), "multicast-iface",
1365 priv->multicast_iface, NULL);
1367 g_signal_emit_by_name (udpsink_out[0], "add", addr_str, rtpport, NULL);
1368 g_signal_emit_by_name (udpsink_out[1], "add", addr_str, rtcpport, NULL);
1371 set_sockets_for_udpsinks (udpsink_out, rtp_socket, rtcp_socket, family);
1373 *server_addr_out = addr;
1375 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1377 g_object_unref (rtp_socket);
1378 g_object_unref (rtcp_socket);
1402 g_object_unref (inetaddr);
1403 g_list_free_full (rejected_addresses,
1404 (GDestroyNotify) gst_rtsp_address_free);
1406 gst_rtsp_address_free (addr);
1408 g_object_unref (rtp_socket);
1410 g_object_unref (rtcp_socket);
1416 * gst_rtsp_stream_allocate_udp_sockets:
1417 * @stream: a #GstRTSPStream
1418 * @family: protocol family
1419 * @transport: transport method
1420 * @use_client_setttings: Whether to use client settings or not
1422 * Allocates RTP and RTCP ports.
1424 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1425 * Deprecated: This function shouldn't have been made public
1428 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1429 GSocketFamily family, GstRTSPTransport * ct, gboolean use_client_settings)
1431 g_warn_if_reached ();
1436 * gst_rtsp_stream_set_client_side:
1437 * @stream: a #GstRTSPStream
1438 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1439 * an RTSP connection.
1441 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1442 * streams to an RTSP server via RECORD. This has the practical effect
1443 * of changing which UDP port numbers are used when setting up the local
1444 * side of the stream sending to be either the 'server' or 'client' pair
1445 * of a configured UDP transport.
1448 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1450 GstRTSPStreamPrivate *priv;
1452 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1453 priv = stream->priv;
1454 g_mutex_lock (&priv->lock);
1455 priv->client_side = client_side;
1456 g_mutex_unlock (&priv->lock);
1460 * gst_rtsp_stream_is_client_side:
1461 * @stream: a #GstRTSPStream
1463 * See gst_rtsp_stream_set_client_side()
1465 * Returns: TRUE if this #GstRTSPStream is client-side.
1468 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1470 GstRTSPStreamPrivate *priv;
1473 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1475 priv = stream->priv;
1476 g_mutex_lock (&priv->lock);
1477 ret = priv->client_side;
1478 g_mutex_unlock (&priv->lock);
1483 /* must be called with lock */
1485 alloc_ports (GstRTSPStream * stream)
1487 GstRTSPStreamPrivate *priv = stream->priv;
1488 gboolean ret = TRUE;
1490 if (priv->protocols & GST_RTSP_LOWER_TRANS_UDP) {
1491 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1492 priv->udpsrc_v4, priv->udpsink, &priv->server_addr_v4, FALSE);
1494 ret |= alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1495 priv->udpsrc_v6, priv->udpsink, &priv->server_addr_v6, FALSE);
1498 /* FIXME: Maybe actually consider the return values? */
1499 if (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1500 ret |= alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1501 priv->mcast_udpsrc_v4, priv->mcast_udpsink, &priv->mcast_addr_v4, TRUE);
1503 ret |= alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1504 priv->mcast_udpsrc_v6, priv->mcast_udpsink, &priv->mcast_addr_v6, TRUE);
1511 * gst_rtsp_stream_get_server_port:
1512 * @stream: a #GstRTSPStream
1513 * @server_port: (out): result server port
1514 * @family: the port family to get
1516 * Fill @server_port with the port pair used by the server. This function can
1517 * only be called when @stream has been joined.
1520 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1521 GstRTSPRange * server_port, GSocketFamily family)
1523 GstRTSPStreamPrivate *priv;
1525 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1526 priv = stream->priv;
1527 g_return_if_fail (priv->joined_bin != NULL);
1530 server_port->min = 0;
1531 server_port->max = 0;
1534 g_mutex_lock (&priv->lock);
1535 if (family == G_SOCKET_FAMILY_IPV4) {
1536 if (server_port && priv->server_addr_v4) {
1537 server_port->min = priv->server_addr_v4->port;
1539 priv->server_addr_v4->port + priv->server_addr_v4->n_ports - 1;
1542 if (server_port && priv->server_addr_v6) {
1543 server_port->min = priv->server_addr_v6->port;
1545 priv->server_addr_v6->port + priv->server_addr_v6->n_ports - 1;
1548 g_mutex_unlock (&priv->lock);
1552 * gst_rtsp_stream_get_rtpsession:
1553 * @stream: a #GstRTSPStream
1555 * Get the RTP session of this stream.
1557 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1560 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1562 GstRTSPStreamPrivate *priv;
1565 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1567 priv = stream->priv;
1569 g_mutex_lock (&priv->lock);
1570 if ((session = priv->session))
1571 g_object_ref (session);
1572 g_mutex_unlock (&priv->lock);
1578 * gst_rtsp_stream_get_srtp_encoder:
1579 * @stream: a #GstRTSPStream
1581 * Get the SRTP encoder for this stream.
1583 * Returns: (transfer full): The SRTP encoder for this stream. Unref after usage.
1586 gst_rtsp_stream_get_srtp_encoder (GstRTSPStream * stream)
1588 GstRTSPStreamPrivate *priv;
1589 GstElement *encoder;
1591 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1593 priv = stream->priv;
1595 g_mutex_lock (&priv->lock);
1596 if ((encoder = priv->srtpenc))
1597 g_object_ref (encoder);
1598 g_mutex_unlock (&priv->lock);
1604 * gst_rtsp_stream_get_ssrc:
1605 * @stream: a #GstRTSPStream
1606 * @ssrc: (out): result ssrc
1608 * Get the SSRC used by the RTP session of this stream. This function can only
1609 * be called when @stream has been joined.
1612 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1614 GstRTSPStreamPrivate *priv;
1616 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1617 priv = stream->priv;
1618 g_return_if_fail (priv->joined_bin != NULL);
1620 g_mutex_lock (&priv->lock);
1621 if (ssrc && priv->session)
1622 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1623 g_mutex_unlock (&priv->lock);
1627 * gst_rtsp_stream_set_retransmission_time:
1628 * @stream: a #GstRTSPStream
1629 * @time: a #GstClockTime
1631 * Set the amount of time to store retransmission packets.
1634 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1637 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1639 g_mutex_lock (&stream->priv->lock);
1640 stream->priv->rtx_time = time;
1641 if (stream->priv->rtxsend)
1642 g_object_set (stream->priv->rtxsend, "max-size-time",
1643 GST_TIME_AS_MSECONDS (time), NULL);
1644 g_mutex_unlock (&stream->priv->lock);
1648 * gst_rtsp_stream_get_retransmission_time:
1649 * @stream: a #GstRTSPStream
1651 * Get the amount of time to store retransmission data.
1653 * Returns: the amount of time to store retransmission data.
1656 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1660 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1662 g_mutex_lock (&stream->priv->lock);
1663 ret = stream->priv->rtx_time;
1664 g_mutex_unlock (&stream->priv->lock);
1670 * gst_rtsp_stream_set_retransmission_pt:
1671 * @stream: a #GstRTSPStream
1674 * Set the payload type (pt) for retransmission of this stream.
1677 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1679 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1681 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1683 g_mutex_lock (&stream->priv->lock);
1684 stream->priv->rtx_pt = rtx_pt;
1685 if (stream->priv->rtxsend) {
1686 guint pt = gst_rtsp_stream_get_pt (stream);
1687 gchar *pt_s = g_strdup_printf ("%d", pt);
1688 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1689 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1690 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1692 gst_structure_free (rtx_pt_map);
1694 g_mutex_unlock (&stream->priv->lock);
1698 * gst_rtsp_stream_get_retransmission_pt:
1699 * @stream: a #GstRTSPStream
1701 * Get the payload-type used for retransmission of this stream
1703 * Returns: The retransmission PT.
1706 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1710 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1712 g_mutex_lock (&stream->priv->lock);
1713 rtx_pt = stream->priv->rtx_pt;
1714 g_mutex_unlock (&stream->priv->lock);
1720 * gst_rtsp_stream_set_buffer_size:
1721 * @stream: a #GstRTSPStream
1722 * @size: the buffer size
1724 * Set the size of the UDP transmission buffer (in bytes)
1725 * Needs to be set before the stream is joined to a bin.
1730 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1732 g_mutex_lock (&stream->priv->lock);
1733 stream->priv->buffer_size = size;
1734 g_mutex_unlock (&stream->priv->lock);
1738 * gst_rtsp_stream_get_buffer_size:
1739 * @stream: a #GstRTSPStream
1741 * Get the size of the UDP transmission buffer (in bytes)
1743 * Returns: the size of the UDP TX buffer
1748 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1752 g_mutex_lock (&stream->priv->lock);
1753 buffer_size = stream->priv->buffer_size;
1754 g_mutex_unlock (&stream->priv->lock);
1759 /* executed from streaming thread */
1761 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1763 GstRTSPStreamPrivate *priv = stream->priv;
1764 GstCaps *newcaps, *oldcaps;
1766 newcaps = gst_pad_get_current_caps (pad);
1768 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1771 g_mutex_lock (&priv->lock);
1772 oldcaps = priv->caps;
1773 priv->caps = newcaps;
1774 g_mutex_unlock (&priv->lock);
1777 gst_caps_unref (oldcaps);
1781 dump_structure (const GstStructure * s)
1785 sstr = gst_structure_to_string (s);
1786 GST_INFO ("structure: %s", sstr);
1790 static GstRTSPStreamTransport *
1791 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1793 GstRTSPStreamPrivate *priv = stream->priv;
1795 GstRTSPStreamTransport *result = NULL;
1800 if (rtcp_from == NULL)
1803 tmp = g_strrstr (rtcp_from, ":");
1807 port = atoi (tmp + 1);
1808 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1810 g_mutex_lock (&priv->lock);
1811 GST_INFO ("finding %s:%d in %d transports", dest, port,
1812 g_list_length (priv->transports));
1814 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1815 GstRTSPStreamTransport *trans = walk->data;
1816 const GstRTSPTransport *tr;
1819 tr = gst_rtsp_stream_transport_get_transport (trans);
1821 if (priv->client_side) {
1822 /* In client side mode the 'destination' is the RTSP server, so send
1824 min = tr->server_port.min;
1825 max = tr->server_port.max;
1827 min = tr->client_port.min;
1828 max = tr->client_port.max;
1831 if ((g_ascii_strcasecmp (tr->destination, dest) == 0) &&
1832 (min == port || max == port)) {
1838 g_object_ref (result);
1839 g_mutex_unlock (&priv->lock);
1846 static GstRTSPStreamTransport *
1847 check_transport (GObject * source, GstRTSPStream * stream)
1849 GstStructure *stats;
1850 GstRTSPStreamTransport *trans;
1852 /* see if we have a stream to match with the origin of the RTCP packet */
1853 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1854 if (trans == NULL) {
1855 g_object_get (source, "stats", &stats, NULL);
1857 const gchar *rtcp_from;
1859 dump_structure (stats);
1861 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1862 if ((trans = find_transport (stream, rtcp_from))) {
1863 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1865 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1868 gst_structure_free (stats);
1876 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1878 GstRTSPStreamTransport *trans;
1880 GST_INFO ("%p: new source %p", stream, source);
1882 trans = check_transport (source, stream);
1885 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1889 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1891 GST_INFO ("%p: new SDES %p", stream, source);
1895 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1897 GstRTSPStreamTransport *trans;
1899 trans = check_transport (source, stream);
1902 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1903 gst_rtsp_stream_transport_keep_alive (trans);
1907 GstStructure *stats;
1908 g_object_get (source, "stats", &stats, NULL);
1910 dump_structure (stats);
1911 gst_structure_free (stats);
1918 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1920 GST_INFO ("%p: source %p bye", stream, source);
1924 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1926 GstRTSPStreamTransport *trans;
1928 GST_INFO ("%p: source %p bye timeout", stream, source);
1930 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1931 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1932 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1937 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1939 GstRTSPStreamTransport *trans;
1941 GST_INFO ("%p: source %p timeout", stream, source);
1943 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1944 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1945 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1950 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1952 GST_INFO ("%p: new sender source %p", stream, source);
1955 GstStructure *stats;
1956 g_object_get (source, "stats", &stats, NULL);
1958 dump_structure (stats);
1959 gst_structure_free (stats);
1966 on_sender_ssrc_active (GObject * session, GObject * source,
1967 GstRTSPStream * stream)
1971 GstStructure *stats;
1972 g_object_get (source, "stats", &stats, NULL);
1974 dump_structure (stats);
1975 gst_structure_free (stats);
1982 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1985 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1986 g_list_free (priv->tr_cache_rtp);
1987 priv->tr_cache_rtp = NULL;
1989 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1990 g_list_free (priv->tr_cache_rtcp);
1991 priv->tr_cache_rtcp = NULL;
1995 static GstFlowReturn
1996 handle_new_sample (GstAppSink * sink, gpointer user_data)
1998 GstRTSPStreamPrivate *priv;
2002 GstRTSPStream *stream;
2005 sample = gst_app_sink_pull_sample (sink);
2009 stream = (GstRTSPStream *) user_data;
2010 priv = stream->priv;
2011 buffer = gst_sample_get_buffer (sample);
2013 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
2015 g_mutex_lock (&priv->lock);
2017 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
2018 clear_tr_cache (priv, is_rtp);
2019 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2020 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2021 priv->tr_cache_rtp =
2022 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
2024 priv->tr_cache_cookie_rtp = priv->transports_cookie;
2027 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
2028 clear_tr_cache (priv, is_rtp);
2029 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2030 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2031 priv->tr_cache_rtcp =
2032 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
2034 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
2037 g_mutex_unlock (&priv->lock);
2040 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2041 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2042 gst_rtsp_stream_transport_send_rtp (tr, buffer);
2045 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2046 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2047 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
2050 gst_sample_unref (sample);
2055 static GstAppSinkCallbacks sink_cb = {
2056 NULL, /* not interested in EOS */
2057 NULL, /* not interested in preroll samples */
2062 get_rtp_encoder (GstRTSPStream * stream, guint session)
2064 GstRTSPStreamPrivate *priv = stream->priv;
2066 if (priv->srtpenc == NULL) {
2069 name = g_strdup_printf ("srtpenc_%u", session);
2070 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2073 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2075 return gst_object_ref (priv->srtpenc);
2079 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2081 GstRTSPStreamPrivate *priv = stream->priv;
2082 GstElement *oldenc, *enc;
2086 if (priv->idx != session)
2089 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2091 oldenc = priv->srtpenc;
2092 enc = get_rtp_encoder (stream, session);
2093 name = g_strdup_printf ("rtp_sink_%d", session);
2094 pad = gst_element_get_request_pad (enc, name);
2096 gst_object_unref (pad);
2099 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2106 request_rtcp_encoder (GstElement * rtpbin, guint session,
2107 GstRTSPStream * stream)
2109 GstRTSPStreamPrivate *priv = stream->priv;
2110 GstElement *oldenc, *enc;
2114 if (priv->idx != session)
2117 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2119 oldenc = priv->srtpenc;
2120 enc = get_rtp_encoder (stream, session);
2121 name = g_strdup_printf ("rtcp_sink_%d", session);
2122 pad = gst_element_get_request_pad (enc, name);
2124 gst_object_unref (pad);
2127 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2134 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2136 GstRTSPStreamPrivate *priv = stream->priv;
2139 GST_DEBUG ("request key %08x", ssrc);
2141 g_mutex_lock (&priv->lock);
2142 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2143 gst_caps_ref (caps);
2144 g_mutex_unlock (&priv->lock);
2150 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2151 GstRTSPStream * stream)
2153 GstRTSPStreamPrivate *priv = stream->priv;
2155 if (priv->idx != session)
2158 if (priv->srtpdec == NULL) {
2161 name = g_strdup_printf ("srtpdec_%u", session);
2162 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2165 g_signal_connect (priv->srtpdec, "request-key",
2166 (GCallback) request_key, stream);
2168 return gst_object_ref (priv->srtpdec);
2172 * gst_rtsp_stream_request_aux_sender:
2173 * @stream: a #GstRTSPStream
2174 * @sessid: the session id
2176 * Creating a rtxsend bin
2178 * Returns: (transfer full): a #GstElement.
2183 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2187 GstStructure *pt_map;
2192 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2194 pt = gst_rtsp_stream_get_pt (stream);
2195 pt_s = g_strdup_printf ("%u", pt);
2196 rtx_pt = stream->priv->rtx_pt;
2198 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2200 bin = gst_bin_new (NULL);
2201 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2202 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2203 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2204 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2205 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2207 gst_structure_free (pt_map);
2208 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2210 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2211 name = g_strdup_printf ("src_%u", sessid);
2212 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2214 gst_object_unref (pad);
2216 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2217 name = g_strdup_printf ("sink_%u", sessid);
2218 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2220 gst_object_unref (pad);
2226 * gst_rtsp_stream_set_pt_map:
2227 * @stream: a #GstRTSPStream
2231 * Configure a pt map between @pt and @caps.
2234 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2236 GstRTSPStreamPrivate *priv = stream->priv;
2238 g_mutex_lock (&priv->lock);
2239 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2240 g_mutex_unlock (&priv->lock);
2244 * gst_rtsp_stream_set_publish_clock_mode:
2245 * @stream: a #GstRTSPStream
2246 * @mode: the clock publish mode
2248 * Sets if and how the stream clock should be published according to RFC7273.
2253 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
2254 GstRTSPPublishClockMode mode)
2256 GstRTSPStreamPrivate *priv;
2258 priv = stream->priv;
2259 g_mutex_lock (&priv->lock);
2260 priv->publish_clock_mode = mode;
2261 g_mutex_unlock (&priv->lock);
2265 * gst_rtsp_stream_get_publish_clock_mode:
2266 * @stream: a #GstRTSPStream
2268 * Gets if and how the stream clock should be published according to RFC7273.
2270 * Returns: The GstRTSPPublishClockMode
2274 GstRTSPPublishClockMode
2275 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
2277 GstRTSPStreamPrivate *priv;
2278 GstRTSPPublishClockMode ret;
2280 priv = stream->priv;
2281 g_mutex_lock (&priv->lock);
2282 ret = priv->publish_clock_mode;
2283 g_mutex_unlock (&priv->lock);
2289 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2290 GstRTSPStream * stream)
2292 GstRTSPStreamPrivate *priv = stream->priv;
2293 GstCaps *caps = NULL;
2295 g_mutex_lock (&priv->lock);
2297 if (priv->idx == session) {
2298 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2300 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2301 gst_caps_ref (caps);
2303 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2307 g_mutex_unlock (&priv->lock);
2313 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2315 GstRTSPStreamPrivate *priv = stream->priv;
2317 GstPadLinkReturn ret;
2320 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2321 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2323 name = gst_pad_get_name (pad);
2324 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2330 if (priv->idx != sessid)
2333 if (gst_pad_is_linked (priv->sinkpad)) {
2334 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2335 GST_DEBUG_PAD_NAME (priv->sinkpad));
2339 /* link the RTP pad to the session manager, it should not really fail unless
2340 * this is not really an RTP pad */
2341 ret = gst_pad_link (pad, priv->sinkpad);
2342 if (ret != GST_PAD_LINK_OK)
2344 priv->recv_rtp_src = gst_object_ref (pad);
2351 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2352 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2357 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2358 GstRTSPStream * stream)
2360 /* TODO: What to do here other than this? */
2361 GST_DEBUG ("Stream %p: Got EOS", stream);
2362 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2366 plug_sink (GstBin * bin, GstElement * tee, GstElement * sink,
2367 GstElement ** queue_out)
2373 gst_bin_add (bin, sink);
2375 *queue_out = gst_element_factory_make ("queue", NULL);
2376 g_object_set (*queue_out, "max-size-buffers", 1, "max-size-bytes", 0,
2377 "max-size-time", G_GINT64_CONSTANT (0), NULL);
2378 gst_bin_add (bin, *queue_out);
2380 /* link tee to queue */
2381 teepad = gst_element_get_request_pad (tee, "src_%u");
2382 pad = gst_element_get_static_pad (*queue_out, "sink");
2383 gst_pad_link (teepad, pad);
2384 gst_object_unref (pad);
2385 gst_object_unref (teepad);
2387 /* link queue to sink */
2388 queuepad = gst_element_get_static_pad (*queue_out, "src");
2389 pad = gst_element_get_static_pad (sink, "sink");
2390 gst_pad_link (queuepad, pad);
2391 gst_object_unref (queuepad);
2392 gst_object_unref (pad);
2395 /* must be called with lock */
2397 create_sender_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2399 GstRTSPStreamPrivate *priv;
2401 gboolean is_tcp, is_udp;
2404 priv = stream->priv;
2406 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2407 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2408 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2410 for (i = 0; i < 2; i++) {
2411 /* For the sender we create this bit of pipeline for both
2412 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2413 * we need to add a queue before appsink and udpsink to make
2414 * the pipeline not block. For the TCP case, we want to pump
2415 * client as fast as possible anyway. This pipeline is used
2416 * when both TCP and UDP are present.
2418 * .--------. .-----. .---------. .---------.
2419 * | rtpbin | | tee | | queue | | udpsink |
2420 * | send->sink src->sink src->sink |
2421 * '--------' | | '---------' '---------'
2422 * | | .---------. .---------.
2423 * | | | queue | | appsink |
2424 * | src->sink src->sink |
2425 * '-----' '---------' '---------'
2427 * When only UDP or only TCP is allowed, we skip the tee and queue
2428 * and link the udpsink (for UDP) or appsink (for TCP) directly to
2432 /* Only link the RTP send src if we're going to send RTP, link
2433 * the RTCP send src always */
2434 if (!priv->srcpad && i == 0)
2439 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2440 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2441 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2442 &sink_cb, stream, NULL);
2445 /* If we have udp always use a tee because we could have mcast clients
2446 * requesting different ports, in which case we'll have to plug more
2449 /* make tee for RTP/RTCP */
2450 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2451 gst_bin_add (bin, priv->tee[i]);
2453 /* and link to rtpbin send pad */
2454 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2455 gst_pad_link (priv->send_src[i], pad);
2456 gst_object_unref (pad);
2458 if (priv->udpsink[i])
2459 plug_sink (bin, priv->tee[i], priv->udpsink[i], &priv->udpqueue[i]);
2461 if (priv->mcast_udpsink[i])
2462 plug_sink (bin, priv->tee[i], priv->mcast_udpsink[i],
2463 &priv->mcast_udpqueue[i]);
2466 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2467 plug_sink (bin, priv->tee[i], priv->appsink[i], &priv->appqueue[i]);
2469 } else if (is_tcp) {
2470 /* only appsink needed, link it to the session */
2471 gst_bin_add (bin, priv->appsink[i]);
2472 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2473 gst_pad_link (priv->send_src[i], pad);
2474 gst_object_unref (pad);
2476 /* when its only TCP, we need to set sync and preroll to FALSE
2477 * for the sink to avoid deadlock. And this is only needed for
2478 * sink used for RTCP data, not the RTP data. */
2480 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2483 /* check if we need to set to a special state */
2484 if (state != GST_STATE_NULL) {
2485 if (priv->udpsink[i])
2486 gst_element_set_state (priv->udpsink[i], state);
2487 if (priv->mcast_udpsink[i])
2488 gst_element_set_state (priv->mcast_udpsink[i], state);
2489 if (priv->appsink[i])
2490 gst_element_set_state (priv->appsink[i], state);
2491 if (priv->appqueue[i])
2492 gst_element_set_state (priv->appqueue[i], state);
2493 if (priv->udpqueue[i])
2494 gst_element_set_state (priv->udpqueue[i], state);
2495 if (priv->mcast_udpqueue[i])
2496 gst_element_set_state (priv->mcast_udpqueue[i], state);
2498 gst_element_set_state (priv->tee[i], state);
2503 /* must be called with lock */
2505 plug_src (GstRTSPStream * stream, GstBin * bin, GstElement * src,
2506 GstElement * funnel)
2508 GstRTSPStreamPrivate *priv;
2509 GstPad *pad, *selpad;
2511 priv = stream->priv;
2514 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2515 * values. This is only relevant for PLAY pipelines */
2516 gst_element_set_state (src, GST_STATE_PLAYING);
2517 gst_element_set_locked_state (src, TRUE);
2521 gst_bin_add (bin, src);
2523 /* and link to the funnel */
2524 selpad = gst_element_get_request_pad (funnel, "sink_%u");
2525 pad = gst_element_get_static_pad (src, "src");
2526 gst_pad_link (pad, selpad);
2527 gst_object_unref (pad);
2528 gst_object_unref (selpad);
2531 /* must be called with lock */
2533 create_receiver_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2535 GstRTSPStreamPrivate *priv;
2540 priv = stream->priv;
2542 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2544 for (i = 0; i < 2; i++) {
2545 /* For the receiver we create this bit of pipeline for both
2546 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2547 * and it is all funneled into the rtpbin receive pad.
2549 * .--------. .--------. .--------.
2550 * | udpsrc | | funnel | | rtpbin |
2551 * | src->sink src->sink |
2552 * '--------' | | '--------'
2556 * '--------' '--------'
2559 if (!priv->sinkpad && i == 0) {
2560 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2561 * RTCP sink always */
2565 /* make funnel for the RTP/RTCP receivers */
2566 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2567 gst_bin_add (bin, priv->funnel[i]);
2569 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2570 gst_pad_link (pad, priv->recv_sink[i]);
2571 gst_object_unref (pad);
2573 if (priv->udpsrc_v4[i])
2574 plug_src (stream, bin, priv->udpsrc_v4[i], priv->funnel[i]);
2576 if (priv->udpsrc_v6[i])
2577 plug_src (stream, bin, priv->udpsrc_v6[i], priv->funnel[i]);
2579 if (priv->mcast_udpsrc_v4[i])
2580 plug_src (stream, bin, priv->mcast_udpsrc_v4[i], priv->funnel[i]);
2582 if (priv->mcast_udpsrc_v6[i])
2583 plug_src (stream, bin, priv->mcast_udpsrc_v6[i], priv->funnel[i]);
2586 /* make and add appsrc */
2587 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2588 priv->appsrc_base_time[i] = -1;
2589 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
2591 plug_src (stream, bin, priv->appsrc[i], priv->funnel[i]);
2594 /* check if we need to set to a special state */
2595 if (state != GST_STATE_NULL) {
2596 gst_element_set_state (priv->funnel[i], state);
2602 check_mcast_part_for_transport (GstRTSPStream * stream,
2603 const GstRTSPTransport * tr)
2605 GstRTSPStreamPrivate *priv = stream->priv;
2606 GInetAddress *inetaddr;
2607 GSocketFamily family;
2608 GstRTSPAddress *mcast_addr;
2610 /* Check if it's a ipv4 or ipv6 transport */
2611 inetaddr = g_inet_address_new_from_string (tr->destination);
2612 family = g_inet_address_get_family (inetaddr);
2613 g_object_unref (inetaddr);
2615 /* Select fields corresponding to the family */
2616 if (family == G_SOCKET_FAMILY_IPV4) {
2617 mcast_addr = priv->mcast_addr_v4;
2619 mcast_addr = priv->mcast_addr_v6;
2622 /* We support only one mcast group per family, make sure this transport
2627 if (g_ascii_strcasecmp (tr->destination, mcast_addr->address) != 0 ||
2628 tr->port.min != mcast_addr->port ||
2629 tr->port.max != mcast_addr->port + mcast_addr->n_ports - 1 ||
2630 tr->ttl != mcast_addr->ttl)
2637 GST_WARNING_OBJECT (stream, "Adding mcast transport, but no mcast address "
2638 "has been reserved");
2643 GST_WARNING_OBJECT (stream, "Adding mcast transport, but it doesn't match "
2644 "the reserved address");
2650 * gst_rtsp_stream_join_bin:
2651 * @stream: a #GstRTSPStream
2652 * @bin: (transfer none): a #GstBin to join
2653 * @rtpbin: (transfer none): a rtpbin element in @bin
2654 * @state: the target state of the new elements
2656 * Join the #GstBin @bin that contains the element @rtpbin.
2658 * @stream will link to @rtpbin, which must be inside @bin. The elements
2659 * added to @bin will be set to the state given in @state.
2661 * Returns: %TRUE on success.
2664 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2665 GstElement * rtpbin, GstState state)
2667 GstRTSPStreamPrivate *priv;
2670 GstPadLinkReturn ret;
2672 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2673 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2674 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2676 priv = stream->priv;
2678 g_mutex_lock (&priv->lock);
2679 if (priv->joined_bin != NULL)
2682 /* create a session with the same index as the stream */
2685 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2687 if (!alloc_ports (stream))
2690 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2691 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2693 g_signal_connect (rtpbin, "request-rtp-encoder",
2694 (GCallback) request_rtp_encoder, stream);
2695 g_signal_connect (rtpbin, "request-rtcp-encoder",
2696 (GCallback) request_rtcp_encoder, stream);
2697 g_signal_connect (rtpbin, "request-rtp-decoder",
2698 (GCallback) request_rtp_rtcp_decoder, stream);
2699 g_signal_connect (rtpbin, "request-rtcp-decoder",
2700 (GCallback) request_rtp_rtcp_decoder, stream);
2703 if (priv->sinkpad) {
2704 g_signal_connect (rtpbin, "request-pt-map",
2705 (GCallback) request_pt_map, stream);
2708 /* get pads from the RTP session element for sending and receiving
2711 /* get a pad for sending RTP */
2712 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2713 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2716 /* link the RTP pad to the session manager, it should not really fail unless
2717 * this is not really an RTP pad */
2718 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2719 if (ret != GST_PAD_LINK_OK)
2722 name = g_strdup_printf ("send_rtp_src_%u", idx);
2723 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2726 /* Need to connect our sinkpad from here */
2727 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2729 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2731 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2732 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2736 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2737 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2739 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2740 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2743 /* get the session */
2744 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2746 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2748 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2750 g_signal_connect (priv->session, "on-ssrc-active",
2751 (GCallback) on_ssrc_active, stream);
2752 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2754 g_signal_connect (priv->session, "on-bye-timeout",
2755 (GCallback) on_bye_timeout, stream);
2756 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2759 /* signal for sender ssrc */
2760 g_signal_connect (priv->session, "on-new-sender-ssrc",
2761 (GCallback) on_new_sender_ssrc, stream);
2762 g_signal_connect (priv->session, "on-sender-ssrc-active",
2763 (GCallback) on_sender_ssrc_active, stream);
2765 create_sender_part (stream, bin, state);
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);
2772 priv->caps = gst_pad_get_current_caps (priv->send_src[0]);
2775 priv->joined_bin = bin;
2776 g_mutex_unlock (&priv->lock);
2783 g_mutex_unlock (&priv->lock);
2788 g_mutex_unlock (&priv->lock);
2789 GST_WARNING ("failed to allocate ports %u", idx);
2794 GST_WARNING ("failed to link stream %u", idx);
2795 gst_object_unref (priv->send_rtp_sink);
2796 priv->send_rtp_sink = NULL;
2797 g_mutex_unlock (&priv->lock);
2803 clear_element (GstBin * bin, GstElement ** elementptr)
2806 gst_element_set_locked_state (*elementptr, FALSE);
2807 gst_element_set_state (*elementptr, GST_STATE_NULL);
2808 if (GST_ELEMENT_PARENT (*elementptr))
2809 gst_bin_remove (bin, *elementptr);
2811 gst_object_unref (*elementptr);
2817 * gst_rtsp_stream_leave_bin:
2818 * @stream: a #GstRTSPStream
2819 * @bin: (transfer none): a #GstBin
2820 * @rtpbin: (transfer none): a rtpbin #GstElement
2822 * Remove the elements of @stream from @bin.
2824 * Return: %TRUE on success.
2827 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2828 GstElement * rtpbin)
2830 GstRTSPStreamPrivate *priv;
2833 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2834 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2835 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2837 priv = stream->priv;
2839 g_mutex_lock (&priv->lock);
2840 if (priv->joined_bin == NULL)
2841 goto was_not_joined;
2842 if (priv->joined_bin != bin)
2845 priv->joined_bin = NULL;
2847 /* all transports must be removed by now */
2848 if (priv->transports != NULL)
2849 goto transports_not_removed;
2851 clear_tr_cache (priv, TRUE);
2852 clear_tr_cache (priv, FALSE);
2854 GST_INFO ("stream %p leaving bin", stream);
2857 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2859 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2860 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2861 gst_object_unref (priv->send_rtp_sink);
2862 priv->send_rtp_sink = NULL;
2863 } else if (priv->recv_rtp_src) {
2864 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2865 gst_object_unref (priv->recv_rtp_src);
2866 priv->recv_rtp_src = NULL;
2869 for (i = 0; i < 2; i++) {
2870 clear_element (bin, &priv->udpsrc_v4[i]);
2871 clear_element (bin, &priv->udpsrc_v6[i]);
2872 clear_element (bin, &priv->udpqueue[i]);
2873 clear_element (bin, &priv->udpsink[i]);
2875 clear_element (bin, &priv->mcast_udpsrc_v4[i]);
2876 clear_element (bin, &priv->mcast_udpsrc_v6[i]);
2877 clear_element (bin, &priv->mcast_udpqueue[i]);
2878 clear_element (bin, &priv->mcast_udpsink[i]);
2880 clear_element (bin, &priv->appsrc[i]);
2881 clear_element (bin, &priv->appqueue[i]);
2882 clear_element (bin, &priv->appsink[i]);
2884 clear_element (bin, &priv->tee[i]);
2885 clear_element (bin, &priv->funnel[i]);
2887 if (priv->sinkpad || i == 1) {
2888 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2889 gst_object_unref (priv->recv_sink[i]);
2890 priv->recv_sink[i] = NULL;
2895 gst_object_unref (priv->send_src[0]);
2896 priv->send_src[0] = NULL;
2899 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2900 gst_object_unref (priv->send_src[1]);
2901 priv->send_src[1] = NULL;
2903 g_object_unref (priv->session);
2904 priv->session = NULL;
2906 gst_caps_unref (priv->caps);
2910 gst_object_unref (priv->srtpenc);
2912 gst_object_unref (priv->srtpdec);
2914 if (priv->mcast_addr_v4)
2915 gst_rtsp_address_free (priv->mcast_addr_v4);
2916 priv->mcast_addr_v4 = NULL;
2917 if (priv->mcast_addr_v6)
2918 gst_rtsp_address_free (priv->mcast_addr_v6);
2919 priv->mcast_addr_v6 = NULL;
2920 if (priv->server_addr_v4)
2921 gst_rtsp_address_free (priv->server_addr_v4);
2922 priv->server_addr_v4 = NULL;
2923 if (priv->server_addr_v6)
2924 gst_rtsp_address_free (priv->server_addr_v6);
2925 priv->server_addr_v6 = NULL;
2927 g_mutex_unlock (&priv->lock);
2933 g_mutex_unlock (&priv->lock);
2936 transports_not_removed:
2938 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2939 g_mutex_unlock (&priv->lock);
2944 GST_ERROR_OBJECT (stream, "leaving the wrong bin");
2945 g_mutex_unlock (&priv->lock);
2951 * gst_rtsp_stream_get_joined_bin:
2952 * @stream: a #GstRTSPStream
2954 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
2956 * Return: (transfer full): the joined bin or NULL.
2959 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
2961 GstRTSPStreamPrivate *priv;
2964 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2966 priv = stream->priv;
2968 g_mutex_lock (&priv->lock);
2969 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
2970 g_mutex_unlock (&priv->lock);
2976 * gst_rtsp_stream_get_rtpinfo:
2977 * @stream: a #GstRTSPStream
2978 * @rtptime: (allow-none): result RTP timestamp
2979 * @seq: (allow-none): result RTP seqnum
2980 * @clock_rate: (allow-none): the clock rate
2981 * @running_time: result running-time
2983 * Retrieve the current rtptime, seq and running-time. This is used to
2984 * construct a RTPInfo reply header.
2986 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2989 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2990 guint * rtptime, guint * seq, guint * clock_rate,
2991 GstClockTime * running_time)
2993 GstRTSPStreamPrivate *priv;
2994 GstStructure *stats;
2995 GObjectClass *payobjclass;
2997 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2999 priv = stream->priv;
3001 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
3003 g_mutex_lock (&priv->lock);
3005 /* First try to extract the information from the last buffer on the sinks.
3006 * This will have a more accurate sequence number and timestamp, as between
3007 * the payloader and the sink there can be some queues
3009 if (priv->udpsink[0] || priv->appsink[0]) {
3010 GstSample *last_sample;
3012 if (priv->udpsink[0])
3013 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
3015 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
3020 GstSegment *segment;
3021 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
3023 caps = gst_sample_get_caps (last_sample);
3024 buffer = gst_sample_get_buffer (last_sample);
3025 segment = gst_sample_get_segment (last_sample);
3027 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3029 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3033 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3036 gst_rtp_buffer_unmap (&rtp_buffer);
3040 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3041 GST_BUFFER_TIMESTAMP (buffer));
3045 GstStructure *s = gst_caps_get_structure (caps, 0);
3047 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3049 if (*clock_rate == 0 && running_time)
3050 *running_time = GST_CLOCK_TIME_NONE;
3052 gst_sample_unref (last_sample);
3056 gst_sample_unref (last_sample);
3061 if (g_object_class_find_property (payobjclass, "stats")) {
3062 g_object_get (priv->payloader, "stats", &stats, NULL);
3067 gst_structure_get_uint (stats, "seqnum", seq);
3070 gst_structure_get_uint (stats, "timestamp", rtptime);
3073 gst_structure_get_clock_time (stats, "running-time", running_time);
3076 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3077 if (*clock_rate == 0 && running_time)
3078 *running_time = GST_CLOCK_TIME_NONE;
3080 gst_structure_free (stats);
3082 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3083 !g_object_class_find_property (payobjclass, "timestamp"))
3087 g_object_get (priv->payloader, "seqnum", seq, NULL);
3090 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3093 *running_time = GST_CLOCK_TIME_NONE;
3097 g_mutex_unlock (&priv->lock);
3104 GST_WARNING ("Could not get payloader stats");
3105 g_mutex_unlock (&priv->lock);
3111 * gst_rtsp_stream_get_caps:
3112 * @stream: a #GstRTSPStream
3114 * Retrieve the current caps of @stream.
3116 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
3120 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3122 GstRTSPStreamPrivate *priv;
3125 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3127 priv = stream->priv;
3129 g_mutex_lock (&priv->lock);
3130 if ((result = priv->caps))
3131 gst_caps_ref (result);
3132 g_mutex_unlock (&priv->lock);
3138 * gst_rtsp_stream_recv_rtp:
3139 * @stream: a #GstRTSPStream
3140 * @buffer: (transfer full): a #GstBuffer
3142 * Handle an RTP buffer for the stream. This method is usually called when a
3143 * message has been received from a client using the TCP transport.
3145 * This function takes ownership of @buffer.
3147 * Returns: a GstFlowReturn.
3150 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3152 GstRTSPStreamPrivate *priv;
3154 GstElement *element;
3156 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3157 priv = stream->priv;
3158 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3159 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3161 g_mutex_lock (&priv->lock);
3162 if (priv->appsrc[0])
3163 element = gst_object_ref (priv->appsrc[0]);
3166 g_mutex_unlock (&priv->lock);
3169 if (priv->appsrc_base_time[0] == -1) {
3170 /* Take current running_time. This timestamp will be put on
3171 * the first buffer of each stream because we are a live source and so we
3172 * timestamp with the running_time. When we are dealing with TCP, we also
3173 * only timestamp the first buffer (using the DISCONT flag) because a server
3174 * typically bursts data, for which we don't want to compensate by speeding
3175 * up the media. The other timestamps will be interpollated from this one
3176 * using the RTP timestamps. */
3177 GST_OBJECT_LOCK (element);
3178 if (GST_ELEMENT_CLOCK (element)) {
3180 GstClockTime base_time;
3182 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3183 base_time = GST_ELEMENT_CAST (element)->base_time;
3185 priv->appsrc_base_time[0] = now - base_time;
3186 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3187 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3188 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3189 GST_TIME_ARGS (base_time));
3191 GST_OBJECT_UNLOCK (element);
3194 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3195 gst_object_unref (element);
3203 * gst_rtsp_stream_recv_rtcp:
3204 * @stream: a #GstRTSPStream
3205 * @buffer: (transfer full): a #GstBuffer
3207 * Handle an RTCP buffer for the stream. This method is usually called when a
3208 * message has been received from a client using the TCP transport.
3210 * This function takes ownership of @buffer.
3212 * Returns: a GstFlowReturn.
3215 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3217 GstRTSPStreamPrivate *priv;
3219 GstElement *element;
3221 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3222 priv = stream->priv;
3223 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3225 if (priv->joined_bin == NULL) {
3226 gst_buffer_unref (buffer);
3227 return GST_FLOW_NOT_LINKED;
3229 g_mutex_lock (&priv->lock);
3230 if (priv->appsrc[1])
3231 element = gst_object_ref (priv->appsrc[1]);
3234 g_mutex_unlock (&priv->lock);
3237 if (priv->appsrc_base_time[1] == -1) {
3238 /* Take current running_time. This timestamp will be put on
3239 * the first buffer of each stream because we are a live source and so we
3240 * timestamp with the running_time. When we are dealing with TCP, we also
3241 * only timestamp the first buffer (using the DISCONT flag) because a server
3242 * typically bursts data, for which we don't want to compensate by speeding
3243 * up the media. The other timestamps will be interpollated from this one
3244 * using the RTP timestamps. */
3245 GST_OBJECT_LOCK (element);
3246 if (GST_ELEMENT_CLOCK (element)) {
3248 GstClockTime base_time;
3250 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3251 base_time = GST_ELEMENT_CAST (element)->base_time;
3253 priv->appsrc_base_time[1] = now - base_time;
3254 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3255 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3256 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3257 GST_TIME_ARGS (base_time));
3259 GST_OBJECT_UNLOCK (element);
3262 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3263 gst_object_unref (element);
3266 gst_buffer_unref (buffer);
3271 /* must be called with lock */
3273 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3276 GstRTSPStreamPrivate *priv = stream->priv;
3277 const GstRTSPTransport *tr;
3279 tr = gst_rtsp_stream_transport_get_transport (trans);
3281 switch (tr->lower_transport) {
3282 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3285 if (!check_mcast_part_for_transport (stream, tr))
3287 priv->transports = g_list_prepend (priv->transports, trans);
3289 priv->transports = g_list_remove (priv->transports, trans);
3293 case GST_RTSP_LOWER_TRANS_UDP:
3299 dest = tr->destination;
3300 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3304 } else if (priv->client_side) {
3305 /* In client side mode the 'destination' is the RTSP server, so send
3307 min = tr->server_port.min;
3308 max = tr->server_port.max;
3310 min = tr->client_port.min;
3311 max = tr->client_port.max;
3316 GST_INFO ("setting ttl-mc %d", ttl);
3317 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3318 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3320 GST_INFO ("adding %s:%d-%d", dest, min, max);
3321 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3322 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3323 priv->transports = g_list_prepend (priv->transports, trans);
3325 GST_INFO ("removing %s:%d-%d", dest, min, max);
3326 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3327 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3328 priv->transports = g_list_remove (priv->transports, trans);
3330 priv->transports_cookie++;
3333 case GST_RTSP_LOWER_TRANS_TCP:
3335 GST_INFO ("adding TCP %s", tr->destination);
3336 priv->transports = g_list_prepend (priv->transports, trans);
3338 GST_INFO ("removing TCP %s", tr->destination);
3339 priv->transports = g_list_remove (priv->transports, trans);
3341 priv->transports_cookie++;
3344 goto unknown_transport;
3351 GST_INFO ("Unknown transport %d", tr->lower_transport);
3362 * gst_rtsp_stream_add_transport:
3363 * @stream: a #GstRTSPStream
3364 * @trans: (transfer none): a #GstRTSPStreamTransport
3366 * Add the transport in @trans to @stream. The media of @stream will
3367 * then also be send to the values configured in @trans.
3369 * @stream must be joined to a bin.
3371 * @trans must contain a valid #GstRTSPTransport.
3373 * Returns: %TRUE if @trans was added
3376 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3377 GstRTSPStreamTransport * trans)
3379 GstRTSPStreamPrivate *priv;
3382 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3383 priv = stream->priv;
3384 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3385 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3387 g_mutex_lock (&priv->lock);
3388 res = update_transport (stream, trans, TRUE);
3389 g_mutex_unlock (&priv->lock);
3395 * gst_rtsp_stream_remove_transport:
3396 * @stream: a #GstRTSPStream
3397 * @trans: (transfer none): a #GstRTSPStreamTransport
3399 * Remove the transport in @trans from @stream. The media of @stream will
3400 * not be sent to the values configured in @trans.
3402 * @stream must be joined to a bin.
3404 * @trans must contain a valid #GstRTSPTransport.
3406 * Returns: %TRUE if @trans was removed
3409 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3410 GstRTSPStreamTransport * trans)
3412 GstRTSPStreamPrivate *priv;
3415 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3416 priv = stream->priv;
3417 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3418 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3420 g_mutex_lock (&priv->lock);
3421 res = update_transport (stream, trans, FALSE);
3422 g_mutex_unlock (&priv->lock);
3428 * gst_rtsp_stream_update_crypto:
3429 * @stream: a #GstRTSPStream
3431 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3433 * Update the new crypto information for @ssrc in @stream. If information
3434 * for @ssrc did not exist, it will be added. If information
3435 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3436 * be removed from @stream.
3438 * Returns: %TRUE if @crypto could be updated
3441 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3442 guint ssrc, GstCaps * crypto)
3444 GstRTSPStreamPrivate *priv;
3446 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3447 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3449 priv = stream->priv;
3451 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3453 g_mutex_lock (&priv->lock);
3455 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3456 gst_caps_ref (crypto));
3458 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3459 g_mutex_unlock (&priv->lock);
3465 * gst_rtsp_stream_get_rtp_socket:
3466 * @stream: a #GstRTSPStream
3467 * @family: the socket family
3469 * Get the RTP socket from @stream for a @family.
3471 * @stream must be joined to a bin.
3473 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3474 * socket could be allocated for @family. Unref after usage
3477 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3479 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3483 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3484 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3485 family == G_SOCKET_FAMILY_IPV6, NULL);
3486 g_return_val_if_fail (priv->udpsink[0], NULL);
3488 if (family == G_SOCKET_FAMILY_IPV6)
3493 g_object_get (priv->udpsink[0], name, &socket, NULL);
3499 * gst_rtsp_stream_get_rtcp_socket:
3500 * @stream: a #GstRTSPStream
3501 * @family: the socket family
3503 * Get the RTCP socket from @stream for a @family.
3505 * @stream must be joined to a bin.
3507 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3508 * socket could be allocated for @family. Unref after usage
3511 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3513 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3517 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3518 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3519 family == G_SOCKET_FAMILY_IPV6, NULL);
3520 g_return_val_if_fail (priv->udpsink[1], NULL);
3522 if (family == G_SOCKET_FAMILY_IPV6)
3527 g_object_get (priv->udpsink[1], name, &socket, NULL);
3533 * gst_rtsp_stream_set_seqnum:
3534 * @stream: a #GstRTSPStream
3535 * @seqnum: a new sequence number
3537 * Configure the sequence number in the payloader of @stream to @seqnum.
3540 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3542 GstRTSPStreamPrivate *priv;
3544 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3546 priv = stream->priv;
3548 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3552 * gst_rtsp_stream_get_seqnum:
3553 * @stream: a #GstRTSPStream
3555 * Get the configured sequence number in the payloader of @stream.
3557 * Returns: the sequence number of the payloader.
3560 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3562 GstRTSPStreamPrivate *priv;
3565 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3567 priv = stream->priv;
3569 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3575 * gst_rtsp_stream_transport_filter:
3576 * @stream: a #GstRTSPStream
3577 * @func: (scope call) (allow-none): a callback
3578 * @user_data: (closure): user data passed to @func
3580 * Call @func for each transport managed by @stream. The result value of @func
3581 * determines what happens to the transport. @func will be called with @stream
3582 * locked so no further actions on @stream can be performed from @func.
3584 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3587 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3589 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3590 * will also be added with an additional ref to the result #GList of this
3593 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3595 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3596 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3597 * element in the #GList should be unreffed before the list is freed.
3600 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3601 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3603 GstRTSPStreamPrivate *priv;
3604 GList *result, *walk, *next;
3605 GHashTable *visited = NULL;
3608 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3610 priv = stream->priv;
3614 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3616 g_mutex_lock (&priv->lock);
3618 cookie = priv->transports_cookie;
3619 for (walk = priv->transports; walk; walk = next) {
3620 GstRTSPStreamTransport *trans = walk->data;
3621 GstRTSPFilterResult res;
3624 next = g_list_next (walk);
3627 /* only visit each transport once */
3628 if (g_hash_table_contains (visited, trans))
3631 g_hash_table_add (visited, g_object_ref (trans));
3632 g_mutex_unlock (&priv->lock);
3634 res = func (stream, trans, user_data);
3636 g_mutex_lock (&priv->lock);
3638 res = GST_RTSP_FILTER_REF;
3640 changed = (cookie != priv->transports_cookie);
3643 case GST_RTSP_FILTER_REMOVE:
3644 update_transport (stream, trans, FALSE);
3646 case GST_RTSP_FILTER_REF:
3647 result = g_list_prepend (result, g_object_ref (trans));
3649 case GST_RTSP_FILTER_KEEP:
3656 g_mutex_unlock (&priv->lock);
3659 g_hash_table_unref (visited);
3664 static GstPadProbeReturn
3665 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3667 GstRTSPStreamPrivate *priv;
3668 GstRTSPStream *stream;
3671 priv = stream->priv;
3673 GST_DEBUG_OBJECT (pad, "now blocking");
3675 g_mutex_lock (&priv->lock);
3676 priv->blocking = TRUE;
3677 g_mutex_unlock (&priv->lock);
3679 gst_element_post_message (priv->payloader,
3680 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3681 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3683 return GST_PAD_PROBE_OK;
3687 * gst_rtsp_stream_set_blocked:
3688 * @stream: a #GstRTSPStream
3689 * @blocked: boolean indicating we should block or unblock
3691 * Blocks or unblocks the dataflow on @stream.
3693 * Returns: %TRUE on success
3696 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3698 GstRTSPStreamPrivate *priv;
3701 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3703 priv = stream->priv;
3705 g_mutex_lock (&priv->lock);
3707 priv->blocking = FALSE;
3708 for (i = 0; i < 2; i++) {
3709 if (priv->blocked_id[i] == 0) {
3710 priv->blocked_id[i] = gst_pad_add_probe (priv->send_src[i],
3711 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3712 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3713 g_object_ref (stream), g_object_unref);
3717 for (i = 0; i < 2; i++) {
3718 if (priv->blocked_id[i] != 0) {
3719 gst_pad_remove_probe (priv->send_src[i], priv->blocked_id[i]);
3720 priv->blocked_id[i] = 0;
3723 priv->blocking = FALSE;
3725 g_mutex_unlock (&priv->lock);
3731 * gst_rtsp_stream_is_blocking:
3732 * @stream: a #GstRTSPStream
3734 * Check if @stream is blocking on a #GstBuffer.
3736 * Returns: %TRUE if @stream is blocking
3739 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3741 GstRTSPStreamPrivate *priv;
3744 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3746 priv = stream->priv;
3748 g_mutex_lock (&priv->lock);
3749 result = priv->blocking;
3750 g_mutex_unlock (&priv->lock);
3756 * gst_rtsp_stream_query_position:
3757 * @stream: a #GstRTSPStream
3759 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3760 * the RTP parts of the pipeline and not the RTCP parts.
3762 * Returns: %TRUE if the position could be queried
3765 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3767 GstRTSPStreamPrivate *priv;
3771 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3773 priv = stream->priv;
3775 g_mutex_lock (&priv->lock);
3776 /* depending on the transport type, it should query corresponding sink */
3777 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3778 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3779 sink = priv->udpsink[0];
3781 sink = priv->appsink[0];
3784 gst_object_ref (sink);
3785 g_mutex_unlock (&priv->lock);
3790 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3791 gst_object_unref (sink);
3797 * gst_rtsp_stream_query_stop:
3798 * @stream: a #GstRTSPStream
3800 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3801 * the RTP parts of the pipeline and not the RTCP parts.
3803 * Returns: %TRUE if the stop could be queried
3806 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3808 GstRTSPStreamPrivate *priv;
3813 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3815 priv = stream->priv;
3817 g_mutex_lock (&priv->lock);
3818 /* depending on the transport type, it should query corresponding sink */
3819 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3820 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3821 sink = priv->udpsink[0];
3823 sink = priv->appsink[0];
3826 gst_object_ref (sink);
3827 g_mutex_unlock (&priv->lock);
3832 query = gst_query_new_segment (GST_FORMAT_TIME);
3833 if ((ret = gst_element_query (sink, query))) {
3836 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3837 if (format != GST_FORMAT_TIME)
3840 gst_query_unref (query);
3841 gst_object_unref (sink);