2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: A media stream
22 * @see_also: #GstRTSPMedia
24 * The #GstRTSPStream object manages the data transport for one stream. It
25 * is created from a payloader element and a source pad that produce the RTP
26 * packets for the stream.
28 * With gst_rtsp_stream_join_bin() the streaming elements are added to the bin
29 * and rtpbin. gst_rtsp_stream_leave_bin() removes the elements again.
31 * The #GstRTSPStream will use the configured addresspool, as set with
32 * gst_rtsp_stream_set_address_pool(), to allocate multicast addresses for the
33 * stream. With gst_rtsp_stream_get_multicast_address() you can get the
36 * With gst_rtsp_stream_get_server_port () you can get the port that the server
37 * will use to receive RTCP. This is the part that the clients will use to send
40 * With gst_rtsp_stream_add_transport() destinations can be added where the
41 * stream should be sent to. Use gst_rtsp_stream_remove_transport() to remove
42 * the destination again.
44 * Last reviewed on 2013-07-16 (1.0.0)
53 #include <gst/app/gstappsrc.h>
54 #include <gst/app/gstappsink.h>
56 #include "rtsp-stream.h"
58 #define GST_RTSP_STREAM_GET_PRIVATE(obj) \
59 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
63 GstRTSPStreamTransport *transport;
65 /* RTP and RTCP source */
66 GstElement *udpsrc[2];
68 } GstRTSPMulticastTransportSource;
70 struct _GstRTSPStreamPrivate
75 GstElement *payloader;
80 GstRTSPProfile profiles;
81 GstRTSPLowerTrans protocols;
83 /* pads on the rtpbin */
84 GstPad *send_rtp_sink;
88 /* the RTPSession object */
91 /* SRTP encoder/decoder */
96 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
98 GstElement *udpsrc_v4[2];
100 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
102 GstElement *udpsrc_v6[2];
104 GstElement *udpsink[2];
106 /* for TCP transport */
107 GstElement *appsrc[2];
108 GstElement *appqueue[2];
109 GstElement *appsink[2];
112 GstElement *funnel[2];
117 GstClockTime rtx_time;
119 /* server ports for sending/receiving over ipv4 */
120 GstRTSPRange server_port_v4;
121 GstRTSPAddress *server_addr_v4;
124 /* server ports for sending/receiving over ipv6 */
125 GstRTSPRange server_port_v6;
126 GstRTSPAddress *server_addr_v6;
129 /* multicast addresses */
130 GstRTSPAddressPool *pool;
131 GstRTSPAddress *addr_v4;
132 GstRTSPAddress *addr_v6;
134 /* the caps of the stream */
138 /* transports we stream to */
141 guint transports_cookie;
143 guint tr_cache_cookie;
145 /* UDP sources for UDP multicast transports */
146 GList *transport_sources;
150 /* stream blocking */
155 #define DEFAULT_CONTROL NULL
156 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
157 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
158 GST_RTSP_LOWER_TRANS_TCP
171 SIGNAL_NEW_RTP_ENCODER,
172 SIGNAL_NEW_RTCP_ENCODER,
176 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
177 #define GST_CAT_DEFAULT rtsp_stream_debug
179 static GQuark ssrc_stream_map_key;
181 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
182 GValue * value, GParamSpec * pspec);
183 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
184 const GValue * value, GParamSpec * pspec);
186 static void gst_rtsp_stream_finalize (GObject * obj);
188 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
190 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
193 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
195 GObjectClass *gobject_class;
197 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
199 gobject_class = G_OBJECT_CLASS (klass);
201 gobject_class->get_property = gst_rtsp_stream_get_property;
202 gobject_class->set_property = gst_rtsp_stream_set_property;
203 gobject_class->finalize = gst_rtsp_stream_finalize;
205 g_object_class_install_property (gobject_class, PROP_CONTROL,
206 g_param_spec_string ("control", "Control",
207 "The control string for this stream", DEFAULT_CONTROL,
208 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 g_object_class_install_property (gobject_class, PROP_PROFILES,
211 g_param_spec_flags ("profiles", "Profiles",
212 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
213 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
216 g_param_spec_flags ("protocols", "Protocols",
217 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
218 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
221 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
222 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
223 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
225 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
226 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
227 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
228 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
230 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
232 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
236 gst_rtsp_stream_init (GstRTSPStream * stream)
238 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
240 GST_DEBUG ("new stream %p", stream);
245 priv->control = g_strdup (DEFAULT_CONTROL);
246 priv->profiles = DEFAULT_PROFILES;
247 priv->protocols = DEFAULT_PROTOCOLS;
249 g_mutex_init (&priv->lock);
251 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
252 NULL, (GDestroyNotify) gst_caps_unref);
256 gst_rtsp_stream_finalize (GObject * obj)
258 GstRTSPStream *stream;
259 GstRTSPStreamPrivate *priv;
261 stream = GST_RTSP_STREAM (obj);
264 GST_DEBUG ("finalize stream %p", stream);
266 /* we really need to be unjoined now */
267 g_return_if_fail (!priv->is_joined);
270 gst_rtsp_address_free (priv->addr_v4);
272 gst_rtsp_address_free (priv->addr_v6);
273 if (priv->server_addr_v4)
274 gst_rtsp_address_free (priv->server_addr_v4);
275 if (priv->server_addr_v6)
276 gst_rtsp_address_free (priv->server_addr_v6);
278 g_object_unref (priv->pool);
280 g_object_unref (priv->rtxsend);
282 gst_object_unref (priv->payloader);
283 gst_object_unref (priv->srcpad);
284 g_free (priv->control);
285 g_mutex_clear (&priv->lock);
287 g_hash_table_unref (priv->keys);
289 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
293 gst_rtsp_stream_get_property (GObject * object, guint propid,
294 GValue * value, GParamSpec * pspec)
296 GstRTSPStream *stream = GST_RTSP_STREAM (object);
300 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
303 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
306 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
314 gst_rtsp_stream_set_property (GObject * object, guint propid,
315 const GValue * value, GParamSpec * pspec)
317 GstRTSPStream *stream = GST_RTSP_STREAM (object);
321 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
324 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
327 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
330 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
335 * gst_rtsp_stream_new:
338 * @payloader: a #GstElement
340 * Create a new media stream with index @idx that handles RTP data on
341 * @srcpad and has a payloader element @payloader.
343 * Returns: (transfer full): a new #GstRTSPStream
346 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
348 GstRTSPStreamPrivate *priv;
349 GstRTSPStream *stream;
351 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
352 g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
353 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
355 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
358 priv->payloader = gst_object_ref (payloader);
359 priv->srcpad = gst_object_ref (srcpad);
365 * gst_rtsp_stream_get_index:
366 * @stream: a #GstRTSPStream
368 * Get the stream index.
370 * Return: the stream index.
373 gst_rtsp_stream_get_index (GstRTSPStream * stream)
375 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
377 return stream->priv->idx;
381 * gst_rtsp_stream_get_pt:
382 * @stream: a #GstRTSPStream
384 * Get the stream payload type.
386 * Return: the stream payload type.
389 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
391 GstRTSPStreamPrivate *priv;
394 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
398 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
404 * gst_rtsp_stream_get_srcpad:
405 * @stream: a #GstRTSPStream
407 * Get the srcpad associated with @stream.
409 * Returns: (transfer full): the srcpad. Unref after usage.
412 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
414 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
416 return gst_object_ref (stream->priv->srcpad);
420 * gst_rtsp_stream_get_control:
421 * @stream: a #GstRTSPStream
423 * Get the control string to identify this stream.
425 * Returns: (transfer full): the control string. g_free() after usage.
428 gst_rtsp_stream_get_control (GstRTSPStream * stream)
430 GstRTSPStreamPrivate *priv;
433 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
437 g_mutex_lock (&priv->lock);
438 if ((result = g_strdup (priv->control)) == NULL)
439 result = g_strdup_printf ("stream=%u", priv->idx);
440 g_mutex_unlock (&priv->lock);
446 * gst_rtsp_stream_set_control:
447 * @stream: a #GstRTSPStream
448 * @control: a control string
450 * Set the control string in @stream.
453 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
455 GstRTSPStreamPrivate *priv;
457 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
461 g_mutex_lock (&priv->lock);
462 g_free (priv->control);
463 priv->control = g_strdup (control);
464 g_mutex_unlock (&priv->lock);
468 * gst_rtsp_stream_has_control:
469 * @stream: a #GstRTSPStream
470 * @control: a control string
472 * Check if @stream has the control string @control.
474 * Returns: %TRUE is @stream has @control as the control string
477 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
479 GstRTSPStreamPrivate *priv;
482 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
486 g_mutex_lock (&priv->lock);
488 res = (g_strcmp0 (priv->control, control) == 0);
492 if (sscanf (control, "stream=%u", &streamid) > 0)
493 res = (streamid == priv->idx);
497 g_mutex_unlock (&priv->lock);
503 * gst_rtsp_stream_set_mtu:
504 * @stream: a #GstRTSPStream
507 * Configure the mtu in the payloader of @stream to @mtu.
510 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
512 GstRTSPStreamPrivate *priv;
514 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
518 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
520 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
524 * gst_rtsp_stream_get_mtu:
525 * @stream: a #GstRTSPStream
527 * Get the configured MTU in the payloader of @stream.
529 * Returns: the MTU of the payloader.
532 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
534 GstRTSPStreamPrivate *priv;
537 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
541 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
546 /* Update the dscp qos property on the udp sinks */
548 update_dscp_qos (GstRTSPStream * stream)
550 GstRTSPStreamPrivate *priv;
552 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
556 if (priv->udpsink[0]) {
557 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
561 if (priv->udpsink[1]) {
562 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
568 * gst_rtsp_stream_set_dscp_qos:
569 * @stream: a #GstRTSPStream
570 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
572 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
575 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
577 GstRTSPStreamPrivate *priv;
579 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
583 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
585 if (dscp_qos < -1 || dscp_qos > 63) {
586 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
590 priv->dscp_qos = dscp_qos;
592 update_dscp_qos (stream);
596 * gst_rtsp_stream_get_dscp_qos:
597 * @stream: a #GstRTSPStream
599 * Get the configured DSCP QoS in of the outgoing sockets.
601 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
604 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
606 GstRTSPStreamPrivate *priv;
608 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
612 return priv->dscp_qos;
616 * gst_rtsp_stream_is_transport_supported:
617 * @stream: a #GstRTSPStream
618 * @transport: (transfer none): a #GstRTSPTransport
620 * Check if @transport can be handled by stream
622 * Returns: %TRUE if @transport can be handled by @stream.
625 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
626 GstRTSPTransport * transport)
628 GstRTSPStreamPrivate *priv;
630 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
634 g_mutex_lock (&priv->lock);
635 if (transport->trans != GST_RTSP_TRANS_RTP)
636 goto unsupported_transmode;
638 if (!(transport->profile & priv->profiles))
639 goto unsupported_profile;
641 if (!(transport->lower_transport & priv->protocols))
642 goto unsupported_ltrans;
644 g_mutex_unlock (&priv->lock);
649 unsupported_transmode:
651 GST_DEBUG ("unsupported transport mode %d", transport->trans);
652 g_mutex_unlock (&priv->lock);
657 GST_DEBUG ("unsupported profile %d", transport->profile);
658 g_mutex_unlock (&priv->lock);
663 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
664 g_mutex_unlock (&priv->lock);
670 * gst_rtsp_stream_set_profiles:
671 * @stream: a #GstRTSPStream
672 * @profiles: the new profiles
674 * Configure the allowed profiles for @stream.
677 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
679 GstRTSPStreamPrivate *priv;
681 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
685 g_mutex_lock (&priv->lock);
686 priv->profiles = profiles;
687 g_mutex_unlock (&priv->lock);
691 * gst_rtsp_stream_get_profiles:
692 * @stream: a #GstRTSPStream
694 * Get the allowed profiles of @stream.
696 * Returns: a #GstRTSPProfile
699 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
701 GstRTSPStreamPrivate *priv;
704 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
708 g_mutex_lock (&priv->lock);
709 res = priv->profiles;
710 g_mutex_unlock (&priv->lock);
716 * gst_rtsp_stream_set_protocols:
717 * @stream: a #GstRTSPStream
718 * @protocols: the new flags
720 * Configure the allowed lower transport for @stream.
723 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
724 GstRTSPLowerTrans protocols)
726 GstRTSPStreamPrivate *priv;
728 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
732 g_mutex_lock (&priv->lock);
733 priv->protocols = protocols;
734 g_mutex_unlock (&priv->lock);
738 * gst_rtsp_stream_get_protocols:
739 * @stream: a #GstRTSPStream
741 * Get the allowed protocols of @stream.
743 * Returns: a #GstRTSPLowerTrans
746 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
748 GstRTSPStreamPrivate *priv;
749 GstRTSPLowerTrans res;
751 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
752 GST_RTSP_LOWER_TRANS_UNKNOWN);
756 g_mutex_lock (&priv->lock);
757 res = priv->protocols;
758 g_mutex_unlock (&priv->lock);
764 * gst_rtsp_stream_set_address_pool:
765 * @stream: a #GstRTSPStream
766 * @pool: (transfer none): a #GstRTSPAddressPool
768 * configure @pool to be used as the address pool of @stream.
771 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
772 GstRTSPAddressPool * pool)
774 GstRTSPStreamPrivate *priv;
775 GstRTSPAddressPool *old;
777 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
781 GST_LOG_OBJECT (stream, "set address pool %p", pool);
783 g_mutex_lock (&priv->lock);
784 if ((old = priv->pool) != pool)
785 priv->pool = pool ? g_object_ref (pool) : NULL;
788 g_mutex_unlock (&priv->lock);
791 g_object_unref (old);
795 * gst_rtsp_stream_get_address_pool:
796 * @stream: a #GstRTSPStream
798 * Get the #GstRTSPAddressPool used as the address pool of @stream.
800 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
804 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
806 GstRTSPStreamPrivate *priv;
807 GstRTSPAddressPool *result;
809 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
813 g_mutex_lock (&priv->lock);
814 if ((result = priv->pool))
815 g_object_ref (result);
816 g_mutex_unlock (&priv->lock);
822 * gst_rtsp_stream_get_multicast_address:
823 * @stream: a #GstRTSPStream
824 * @family: the #GSocketFamily
826 * Get the multicast address of @stream for @family.
828 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
829 * or %NULL when no address could be allocated. gst_rtsp_address_free()
833 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
834 GSocketFamily family)
836 GstRTSPStreamPrivate *priv;
837 GstRTSPAddress *result;
838 GstRTSPAddress **addrp;
839 GstRTSPAddressFlags flags;
841 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
845 if (family == G_SOCKET_FAMILY_IPV6) {
846 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
847 addrp = &priv->addr_v6;
849 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
850 addrp = &priv->addr_v4;
853 g_mutex_lock (&priv->lock);
854 if (*addrp == NULL) {
855 if (priv->pool == NULL)
858 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
860 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
864 result = gst_rtsp_address_copy (*addrp);
865 g_mutex_unlock (&priv->lock);
872 GST_ERROR_OBJECT (stream, "no address pool specified");
873 g_mutex_unlock (&priv->lock);
878 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
879 g_mutex_unlock (&priv->lock);
885 * gst_rtsp_stream_reserve_address:
886 * @stream: a #GstRTSPStream
887 * @address: an address
892 * Reserve @address and @port as the address and port of @stream.
894 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
895 * the address could be reserved. gst_rtsp_address_free() after usage.
898 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
899 const gchar * address, guint port, guint n_ports, guint ttl)
901 GstRTSPStreamPrivate *priv;
902 GstRTSPAddress *result;
904 GSocketFamily family;
905 GstRTSPAddress **addrp;
907 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
908 g_return_val_if_fail (address != NULL, NULL);
909 g_return_val_if_fail (port > 0, NULL);
910 g_return_val_if_fail (n_ports > 0, NULL);
911 g_return_val_if_fail (ttl > 0, NULL);
915 addr = g_inet_address_new_from_string (address);
917 GST_ERROR ("failed to get inet addr from %s", address);
918 family = G_SOCKET_FAMILY_IPV4;
920 family = g_inet_address_get_family (addr);
921 g_object_unref (addr);
924 if (family == G_SOCKET_FAMILY_IPV6)
925 addrp = &priv->addr_v6;
927 addrp = &priv->addr_v4;
929 g_mutex_lock (&priv->lock);
930 if (*addrp == NULL) {
931 GstRTSPAddressPoolResult res;
933 if (priv->pool == NULL)
936 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
937 port, n_ports, ttl, addrp);
938 if (res != GST_RTSP_ADDRESS_POOL_OK)
941 if (strcmp ((*addrp)->address, address) ||
942 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
943 (*addrp)->ttl != ttl)
944 goto different_address;
946 result = gst_rtsp_address_copy (*addrp);
947 g_mutex_unlock (&priv->lock);
954 GST_ERROR_OBJECT (stream, "no address pool specified");
955 g_mutex_unlock (&priv->lock);
960 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
962 g_mutex_unlock (&priv->lock);
967 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
968 " reserved", address);
969 g_mutex_unlock (&priv->lock);
975 alloc_ports_one_family (GstRTSPAddressPool * pool, gint buffer_size,
976 GSocketFamily family, GstElement * udpsrc_out[2],
977 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
978 GstRTSPAddress ** server_addr_out)
980 GstStateChangeReturn ret;
981 GstElement *udpsrc0, *udpsrc1;
982 GstElement *udpsink0, *udpsink1;
983 GSocket *rtp_socket = NULL;
984 GSocket *rtcp_socket;
985 gint tmp_rtp, tmp_rtcp;
987 gint rtpport, rtcpport;
988 GList *rejected_addresses = NULL;
989 GstRTSPAddress *addr = NULL;
990 GInetAddress *inetaddr = NULL;
991 GSocketAddress *rtp_sockaddr = NULL;
992 GSocketAddress *rtcp_sockaddr = NULL;
993 const gchar *multisink_socket;
995 if (family == G_SOCKET_FAMILY_IPV6)
996 multisink_socket = "socket-v6";
998 multisink_socket = "socket";
1006 /* Start with random port */
1009 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1010 G_SOCKET_PROTOCOL_UDP, NULL);
1012 goto no_udp_protocol;
1014 if (*server_addr_out)
1015 gst_rtsp_address_free (*server_addr_out);
1017 /* try to allocate 2 UDP ports, the RTP port should be an even
1018 * number and the RTCP port should be the next (uneven) port */
1021 if (rtp_socket == NULL) {
1022 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1023 G_SOCKET_PROTOCOL_UDP, NULL);
1025 goto no_udp_protocol;
1028 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
1029 GstRTSPAddressFlags flags;
1032 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1034 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
1035 if (family == G_SOCKET_FAMILY_IPV6)
1036 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1038 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1040 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1045 tmp_rtp = addr->port;
1047 g_clear_object (&inetaddr);
1048 inetaddr = g_inet_address_new_from_string (addr->address);
1056 if (inetaddr == NULL)
1057 inetaddr = g_inet_address_new_any (family);
1060 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1061 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1062 g_object_unref (rtp_sockaddr);
1065 g_object_unref (rtp_sockaddr);
1067 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1068 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1069 g_clear_object (&rtp_sockaddr);
1074 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1075 g_object_unref (rtp_sockaddr);
1077 /* check if port is even */
1078 if ((tmp_rtp & 1) != 0) {
1079 /* port not even, close and allocate another */
1081 g_clear_object (&rtp_socket);
1086 tmp_rtcp = tmp_rtp + 1;
1088 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1089 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1090 g_object_unref (rtcp_sockaddr);
1091 g_clear_object (&rtp_socket);
1094 g_object_unref (rtcp_sockaddr);
1096 g_clear_object (&inetaddr);
1098 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
1099 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
1101 if (udpsrc0 == NULL || udpsrc1 == NULL)
1102 goto no_udp_protocol;
1104 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
1105 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
1107 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1108 if (ret == GST_STATE_CHANGE_FAILURE)
1110 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1111 if (ret == GST_STATE_CHANGE_FAILURE)
1114 /* all fine, do port check */
1115 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
1116 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
1118 /* this should not happen... */
1119 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1123 udpsink0 = udpsink_out[0];
1125 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1128 goto no_udp_protocol;
1130 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1131 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
1134 udpsink1 = udpsink_out[1];
1136 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1139 goto no_udp_protocol;
1141 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1142 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1143 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
1145 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1146 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
1147 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1148 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1149 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1150 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1151 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1152 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1154 /* we keep these elements, we will further configure them when the
1155 * client told us to really use the UDP ports. */
1156 udpsrc_out[0] = udpsrc0;
1157 udpsrc_out[1] = udpsrc1;
1158 udpsink_out[0] = udpsink0;
1159 udpsink_out[1] = udpsink1;
1161 server_port_out->min = rtpport;
1162 server_port_out->max = rtcpport;
1164 *server_addr_out = addr;
1165 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1167 g_object_unref (rtp_socket);
1168 g_object_unref (rtcp_socket);
1196 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1197 gst_object_unref (udpsrc0);
1200 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1201 gst_object_unref (udpsrc1);
1204 gst_element_set_state (udpsink0, GST_STATE_NULL);
1205 gst_object_unref (udpsink0);
1208 g_object_unref (inetaddr);
1209 g_list_free_full (rejected_addresses,
1210 (GDestroyNotify) gst_rtsp_address_free);
1212 gst_rtsp_address_free (addr);
1214 g_object_unref (rtp_socket);
1216 g_object_unref (rtcp_socket);
1221 /* must be called with lock */
1223 alloc_ports (GstRTSPStream * stream)
1225 GstRTSPStreamPrivate *priv = stream->priv;
1227 priv->have_ipv4 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1228 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1229 &priv->server_port_v4, &priv->server_addr_v4);
1231 priv->have_ipv6 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1232 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1233 &priv->server_port_v6, &priv->server_addr_v6);
1235 return priv->have_ipv4 || priv->have_ipv6;
1239 * gst_rtsp_stream_get_server_port:
1240 * @stream: a #GstRTSPStream
1241 * @server_port: (out): result server port
1242 * @family: the port family to get
1244 * Fill @server_port with the port pair used by the server. This function can
1245 * only be called when @stream has been joined.
1248 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1249 GstRTSPRange * server_port, GSocketFamily family)
1251 GstRTSPStreamPrivate *priv;
1253 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1254 priv = stream->priv;
1255 g_return_if_fail (priv->is_joined);
1257 g_mutex_lock (&priv->lock);
1258 if (family == G_SOCKET_FAMILY_IPV4) {
1260 *server_port = priv->server_port_v4;
1263 *server_port = priv->server_port_v6;
1265 g_mutex_unlock (&priv->lock);
1269 * gst_rtsp_stream_get_rtpsession:
1270 * @stream: a #GstRTSPStream
1272 * Get the RTP session of this stream.
1274 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1277 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1279 GstRTSPStreamPrivate *priv;
1282 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1284 priv = stream->priv;
1286 g_mutex_lock (&priv->lock);
1287 if ((session = priv->session))
1288 g_object_ref (session);
1289 g_mutex_unlock (&priv->lock);
1295 * gst_rtsp_stream_get_ssrc:
1296 * @stream: a #GstRTSPStream
1297 * @ssrc: (out): result ssrc
1299 * Get the SSRC used by the RTP session of this stream. This function can only
1300 * be called when @stream has been joined.
1303 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1305 GstRTSPStreamPrivate *priv;
1307 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1308 priv = stream->priv;
1309 g_return_if_fail (priv->is_joined);
1311 g_mutex_lock (&priv->lock);
1312 if (ssrc && priv->session)
1313 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1314 g_mutex_unlock (&priv->lock);
1318 * gst_rtsp_stream_set_retransmission_time:
1319 * @stream: a #GstRTSPStream
1320 * @time: a #GstClockTime
1322 * Set the amount of time to store retransmission packets.
1325 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1328 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1330 g_mutex_lock (&stream->priv->lock);
1331 stream->priv->rtx_time = time;
1332 if (stream->priv->rtxsend)
1333 g_object_set (stream->priv->rtxsend, "max-size-time",
1334 GST_TIME_AS_MSECONDS (time), NULL);
1335 g_mutex_unlock (&stream->priv->lock);
1339 * gst_rtsp_media_get_retransmission_time:
1340 * @media: a #GstRTSPMedia
1342 * Get the amount of time to store retransmission data.
1344 * Returns: the amount of time to store retransmission data.
1347 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1351 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1353 g_mutex_lock (&stream->priv->lock);
1354 ret = stream->priv->rtx_time;
1355 g_mutex_unlock (&stream->priv->lock);
1361 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1363 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1365 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1367 g_mutex_lock (&stream->priv->lock);
1368 stream->priv->rtx_pt = rtx_pt;
1369 if (stream->priv->rtxsend) {
1370 guint pt = gst_rtsp_stream_get_pt (stream);
1371 gchar *pt_s = g_strdup_printf ("%d", pt);
1372 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1373 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1374 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1376 g_mutex_unlock (&stream->priv->lock);
1380 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1384 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1386 g_mutex_lock (&stream->priv->lock);
1387 rtx_pt = stream->priv->rtx_pt;
1388 g_mutex_unlock (&stream->priv->lock);
1393 /* executed from streaming thread */
1395 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1397 GstRTSPStreamPrivate *priv = stream->priv;
1398 GstCaps *newcaps, *oldcaps;
1400 newcaps = gst_pad_get_current_caps (pad);
1402 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1405 g_mutex_lock (&priv->lock);
1406 oldcaps = priv->caps;
1407 priv->caps = newcaps;
1408 g_mutex_unlock (&priv->lock);
1411 gst_caps_unref (oldcaps);
1415 dump_structure (const GstStructure * s)
1419 sstr = gst_structure_to_string (s);
1420 GST_INFO ("structure: %s", sstr);
1424 static GstRTSPStreamTransport *
1425 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1427 GstRTSPStreamPrivate *priv = stream->priv;
1429 GstRTSPStreamTransport *result = NULL;
1434 if (rtcp_from == NULL)
1437 tmp = g_strrstr (rtcp_from, ":");
1441 port = atoi (tmp + 1);
1442 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1444 g_mutex_lock (&priv->lock);
1445 GST_INFO ("finding %s:%d in %d transports", dest, port,
1446 g_list_length (priv->transports));
1448 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1449 GstRTSPStreamTransport *trans = walk->data;
1450 const GstRTSPTransport *tr;
1453 tr = gst_rtsp_stream_transport_get_transport (trans);
1455 min = tr->client_port.min;
1456 max = tr->client_port.max;
1458 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1464 g_object_ref (result);
1465 g_mutex_unlock (&priv->lock);
1472 static GstRTSPStreamTransport *
1473 check_transport (GObject * source, GstRTSPStream * stream)
1475 GstStructure *stats;
1476 GstRTSPStreamTransport *trans;
1478 /* see if we have a stream to match with the origin of the RTCP packet */
1479 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1480 if (trans == NULL) {
1481 g_object_get (source, "stats", &stats, NULL);
1483 const gchar *rtcp_from;
1485 dump_structure (stats);
1487 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1488 if ((trans = find_transport (stream, rtcp_from))) {
1489 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1491 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1494 gst_structure_free (stats);
1502 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1504 GstRTSPStreamTransport *trans;
1506 GST_INFO ("%p: new source %p", stream, source);
1508 trans = check_transport (source, stream);
1511 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1515 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1517 GST_INFO ("%p: new SDES %p", stream, source);
1521 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1523 GstRTSPStreamTransport *trans;
1525 trans = check_transport (source, stream);
1528 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1529 gst_rtsp_stream_transport_keep_alive (trans);
1533 GstStructure *stats;
1534 g_object_get (source, "stats", &stats, NULL);
1536 dump_structure (stats);
1537 gst_structure_free (stats);
1544 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1546 GST_INFO ("%p: source %p bye", stream, source);
1550 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1552 GstRTSPStreamTransport *trans;
1554 GST_INFO ("%p: source %p bye timeout", stream, source);
1556 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1557 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1558 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1563 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1565 GstRTSPStreamTransport *trans;
1567 GST_INFO ("%p: source %p timeout", stream, source);
1569 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1570 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1571 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1576 clear_tr_cache (GstRTSPStreamPrivate * priv)
1578 g_list_foreach (priv->tr_cache, (GFunc) g_object_unref, NULL);
1579 g_list_free (priv->tr_cache);
1580 priv->tr_cache = NULL;
1583 static GstFlowReturn
1584 handle_new_sample (GstAppSink * sink, gpointer user_data)
1586 GstRTSPStreamPrivate *priv;
1590 GstRTSPStream *stream;
1593 sample = gst_app_sink_pull_sample (sink);
1597 stream = (GstRTSPStream *) user_data;
1598 priv = stream->priv;
1599 buffer = gst_sample_get_buffer (sample);
1601 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
1603 g_mutex_lock (&priv->lock);
1604 if (priv->tr_cache_cookie != priv->transports_cookie) {
1605 clear_tr_cache (priv);
1606 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1607 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1608 priv->tr_cache = g_list_prepend (priv->tr_cache, g_object_ref (tr));
1610 priv->tr_cache_cookie = priv->transports_cookie;
1612 g_mutex_unlock (&priv->lock);
1614 for (walk = priv->tr_cache; walk; walk = g_list_next (walk)) {
1615 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1618 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1620 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1623 gst_sample_unref (sample);
1628 static GstAppSinkCallbacks sink_cb = {
1629 NULL, /* not interested in EOS */
1630 NULL, /* not interested in preroll samples */
1635 get_rtp_encoder (GstRTSPStream * stream, guint session)
1637 GstRTSPStreamPrivate *priv = stream->priv;
1639 if (priv->srtpenc == NULL) {
1642 name = g_strdup_printf ("srtpenc_%u", session);
1643 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
1646 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
1648 return gst_object_ref (priv->srtpenc);
1652 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
1654 GstRTSPStreamPrivate *priv = stream->priv;
1655 GstElement *oldenc, *enc;
1659 if (priv->idx != session)
1662 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
1664 oldenc = priv->srtpenc;
1665 enc = get_rtp_encoder (stream, session);
1666 name = g_strdup_printf ("rtp_sink_%d", session);
1667 pad = gst_element_get_request_pad (enc, name);
1669 gst_object_unref (pad);
1672 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
1679 request_rtcp_encoder (GstElement * rtpbin, guint session,
1680 GstRTSPStream * stream)
1682 GstRTSPStreamPrivate *priv = stream->priv;
1683 GstElement *oldenc, *enc;
1687 if (priv->idx != session)
1690 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
1692 oldenc = priv->srtpenc;
1693 enc = get_rtp_encoder (stream, session);
1694 name = g_strdup_printf ("rtcp_sink_%d", session);
1695 pad = gst_element_get_request_pad (enc, name);
1697 gst_object_unref (pad);
1700 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
1707 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
1709 GstRTSPStreamPrivate *priv = stream->priv;
1712 GST_DEBUG ("request key %08x", ssrc);
1714 g_mutex_lock (&priv->lock);
1715 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
1716 gst_caps_ref (caps);
1717 g_mutex_unlock (&priv->lock);
1723 request_rtcp_decoder (GstElement * rtpbin, guint session,
1724 GstRTSPStream * stream)
1726 GstRTSPStreamPrivate *priv = stream->priv;
1728 if (priv->idx != session)
1731 if (priv->srtpdec == NULL) {
1734 name = g_strdup_printf ("srtpdec_%u", session);
1735 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
1738 g_signal_connect (priv->srtpdec, "request-key",
1739 (GCallback) request_key, stream);
1741 return gst_object_ref (priv->srtpdec);
1745 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPStream * stream)
1749 GstStructure *pt_map;
1754 pt = gst_rtsp_stream_get_pt (stream);
1755 pt_s = g_strdup_printf ("%u", pt);
1756 rtx_pt = stream->priv->rtx_pt;
1758 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
1760 bin = gst_bin_new (NULL);
1761 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
1762 pt_map = gst_structure_new ("application/x-rtp-pt-map",
1763 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1764 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
1765 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
1766 gst_structure_free (pt_map);
1767 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
1769 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
1770 name = g_strdup_printf ("src_%u", sessid);
1771 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1773 gst_object_unref (pad);
1775 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
1776 name = g_strdup_printf ("sink_%u", sessid);
1777 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1779 gst_object_unref (pad);
1785 * gst_rtsp_stream_join_bin:
1786 * @stream: a #GstRTSPStream
1787 * @bin: (transfer none): a #GstBin to join
1788 * @rtpbin: (transfer none): a rtpbin element in @bin
1789 * @state: the target state of the new elements
1791 * Join the #GstBin @bin that contains the element @rtpbin.
1793 * @stream will link to @rtpbin, which must be inside @bin. The elements
1794 * added to @bin will be set to the state given in @state.
1796 * Returns: %TRUE on success.
1799 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1800 GstElement * rtpbin, GstState state)
1802 GstRTSPStreamPrivate *priv;
1806 GstPad *pad, *sinkpad, *selpad;
1807 GstPadLinkReturn ret;
1809 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1810 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1811 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1813 priv = stream->priv;
1815 g_mutex_lock (&priv->lock);
1816 if (priv->is_joined)
1819 /* create a session with the same index as the stream */
1822 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1824 if (!alloc_ports (stream))
1827 /* update the dscp qos field in the sinks */
1828 update_dscp_qos (stream);
1830 if (priv->profiles & GST_RTSP_PROFILE_SAVP
1831 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
1833 g_signal_connect (rtpbin, "request-rtp-encoder",
1834 (GCallback) request_rtp_encoder, stream);
1835 g_signal_connect (rtpbin, "request-rtcp-encoder",
1836 (GCallback) request_rtcp_encoder, stream);
1837 g_signal_connect (rtpbin, "request-rtcp-decoder",
1838 (GCallback) request_rtcp_decoder, stream);
1841 if (priv->rtx_time > 0) {
1842 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
1843 g_signal_connect (rtpbin, "request-aux-sender",
1844 (GCallback) request_aux_sender, stream);
1847 /* get a pad for sending RTP */
1848 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1849 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
1851 /* link the RTP pad to the session manager, it should not really fail unless
1852 * this is not really an RTP pad */
1853 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
1854 if (ret != GST_PAD_LINK_OK)
1857 /* get pads from the RTP session element for sending and receiving
1859 name = g_strdup_printf ("send_rtp_src_%u", idx);
1860 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
1862 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1863 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
1865 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1866 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
1868 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1869 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
1872 /* get the session */
1873 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
1875 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1877 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1879 g_signal_connect (priv->session, "on-ssrc-active",
1880 (GCallback) on_ssrc_active, stream);
1881 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1883 g_signal_connect (priv->session, "on-bye-timeout",
1884 (GCallback) on_bye_timeout, stream);
1885 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1888 for (i = 0; i < 2; i++) {
1889 GstPad *teepad, *queuepad;
1890 /* For the sender we create this bit of pipeline for both
1891 * RTP and RTCP. Sync and preroll are enabled on udpsink so
1892 * we need to add a queue before appsink to make the pipeline
1893 * not block. For the TCP case, we want to pump data to the
1894 * client as fast as possible anyway.
1896 * .--------. .-----. .---------.
1897 * | rtpbin | | tee | | udpsink |
1898 * | send->sink src->sink |
1899 * '--------' | | '---------'
1900 * | | .---------. .---------.
1901 * | | | queue | | appsink |
1902 * | src->sink src->sink |
1903 * '-----' '---------' '---------'
1905 * When only UDP is allowed, we skip the tee, queue and appsink and link the
1906 * udpsink directly to the session.
1909 gst_bin_add (bin, priv->udpsink[i]);
1910 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1912 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1913 /* make tee for RTP/RTCP */
1914 priv->tee[i] = gst_element_factory_make ("tee", NULL);
1915 gst_bin_add (bin, priv->tee[i]);
1917 /* and link to rtpbin send pad */
1918 pad = gst_element_get_static_pad (priv->tee[i], "sink");
1919 gst_pad_link (priv->send_src[i], pad);
1920 gst_object_unref (pad);
1922 /* link tee to udpsink */
1923 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1924 gst_pad_link (teepad, sinkpad);
1925 gst_object_unref (teepad);
1928 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1929 gst_bin_add (bin, priv->appqueue[i]);
1930 /* and link to tee */
1931 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1932 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1933 gst_pad_link (teepad, pad);
1934 gst_object_unref (pad);
1935 gst_object_unref (teepad);
1938 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1939 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1940 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1941 gst_bin_add (bin, priv->appsink[i]);
1942 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1943 &sink_cb, stream, NULL);
1944 /* and link to queue */
1945 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1946 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1947 gst_pad_link (queuepad, pad);
1948 gst_object_unref (pad);
1949 gst_object_unref (queuepad);
1951 /* else only udpsink needed, link it to the session */
1952 gst_pad_link (priv->send_src[i], sinkpad);
1954 gst_object_unref (sinkpad);
1956 /* For the receiver we create this bit of pipeline for both
1957 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1958 * and it is all funneled into the rtpbin receive pad.
1960 * .--------. .--------. .--------.
1961 * | udpsrc | | funnel | | rtpbin |
1962 * | src->sink src->sink |
1963 * '--------' | | '--------'
1967 * '--------' '--------'
1969 /* make funnel for the RTP/RTCP receivers */
1970 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1971 gst_bin_add (bin, priv->funnel[i]);
1973 pad = gst_element_get_static_pad (priv->funnel[i], "src");
1974 gst_pad_link (pad, priv->recv_sink[i]);
1975 gst_object_unref (pad);
1977 if (priv->udpsrc_v4[i]) {
1978 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1980 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
1981 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
1983 gst_bin_add (bin, priv->udpsrc_v4[i]);
1985 /* and link to the funnel v4 */
1986 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1987 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
1988 gst_pad_link (pad, selpad);
1989 gst_object_unref (pad);
1990 gst_object_unref (selpad);
1993 if (priv->udpsrc_v6[i]) {
1994 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
1995 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
1996 gst_bin_add (bin, priv->udpsrc_v6[i]);
1998 /* and link to the funnel v6 */
1999 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2000 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2001 gst_pad_link (pad, selpad);
2002 gst_object_unref (pad);
2003 gst_object_unref (selpad);
2006 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
2007 /* make and add appsrc */
2008 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2009 gst_bin_add (bin, priv->appsrc[i]);
2010 /* and link to the funnel */
2011 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2012 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2013 gst_pad_link (pad, selpad);
2014 gst_object_unref (pad);
2015 gst_object_unref (selpad);
2018 /* check if we need to set to a special state */
2019 if (state != GST_STATE_NULL) {
2020 if (priv->udpsink[i])
2021 gst_element_set_state (priv->udpsink[i], state);
2022 if (priv->appsink[i])
2023 gst_element_set_state (priv->appsink[i], state);
2024 if (priv->appqueue[i])
2025 gst_element_set_state (priv->appqueue[i], state);
2027 gst_element_set_state (priv->tee[i], state);
2028 if (priv->funnel[i])
2029 gst_element_set_state (priv->funnel[i], state);
2030 if (priv->appsrc[i])
2031 gst_element_set_state (priv->appsrc[i], state);
2035 /* be notified of caps changes */
2036 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2037 (GCallback) caps_notify, stream);
2039 priv->is_joined = TRUE;
2040 g_mutex_unlock (&priv->lock);
2047 g_mutex_unlock (&priv->lock);
2052 g_mutex_unlock (&priv->lock);
2053 GST_WARNING ("failed to allocate ports %u", idx);
2058 GST_WARNING ("failed to link stream %u", idx);
2059 gst_object_unref (priv->send_rtp_sink);
2060 priv->send_rtp_sink = NULL;
2061 g_mutex_unlock (&priv->lock);
2067 * gst_rtsp_stream_leave_bin:
2068 * @stream: a #GstRTSPStream
2069 * @bin: (transfer none): a #GstBin
2070 * @rtpbin: (transfer none): a rtpbin #GstElement
2072 * Remove the elements of @stream from @bin.
2074 * Return: %TRUE on success.
2077 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2078 GstElement * rtpbin)
2080 GstRTSPStreamPrivate *priv;
2084 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2085 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2086 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2088 priv = stream->priv;
2090 g_mutex_lock (&priv->lock);
2091 if (!priv->is_joined)
2092 goto was_not_joined;
2094 /* all transports must be removed by now */
2095 if (priv->transports != NULL)
2096 goto transports_not_removed;
2098 clear_tr_cache (priv);
2100 GST_INFO ("stream %p leaving bin", stream);
2102 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2103 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2104 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2105 gst_object_unref (priv->send_rtp_sink);
2106 priv->send_rtp_sink = NULL;
2108 for (i = 0; i < 2; i++) {
2109 if (priv->udpsink[i])
2110 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
2111 if (priv->appsink[i])
2112 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
2113 if (priv->appqueue[i])
2114 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
2116 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
2117 if (priv->funnel[i])
2118 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
2119 if (priv->appsrc[i])
2120 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2121 if (priv->udpsrc_v4[i]) {
2122 /* and set udpsrc to NULL now before removing */
2123 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
2124 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2125 /* removing them should also nicely release the request
2126 * pads when they finalize */
2127 gst_bin_remove (bin, priv->udpsrc_v4[i]);
2129 if (priv->udpsrc_v6[i]) {
2130 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
2131 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2132 gst_bin_remove (bin, priv->udpsrc_v6[i]);
2135 for (l = priv->transport_sources; l; l = l->next) {
2136 GstRTSPMulticastTransportSource *s = l->data;
2141 gst_element_set_locked_state (s->udpsrc[i], FALSE);
2142 gst_element_set_state (s->udpsrc[i], GST_STATE_NULL);
2143 gst_bin_remove (bin, s->udpsrc[i]);
2146 if (priv->udpsink[i])
2147 gst_bin_remove (bin, priv->udpsink[i]);
2148 if (priv->appsrc[i])
2149 gst_bin_remove (bin, priv->appsrc[i]);
2150 if (priv->appsink[i])
2151 gst_bin_remove (bin, priv->appsink[i]);
2152 if (priv->appqueue[i])
2153 gst_bin_remove (bin, priv->appqueue[i]);
2155 gst_bin_remove (bin, priv->tee[i]);
2156 if (priv->funnel[i])
2157 gst_bin_remove (bin, priv->funnel[i]);
2159 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2160 gst_object_unref (priv->recv_sink[i]);
2161 priv->recv_sink[i] = NULL;
2163 priv->udpsrc_v4[i] = NULL;
2164 priv->udpsrc_v6[i] = NULL;
2165 priv->udpsink[i] = NULL;
2166 priv->appsrc[i] = NULL;
2167 priv->appsink[i] = NULL;
2168 priv->appqueue[i] = NULL;
2169 priv->tee[i] = NULL;
2170 priv->funnel[i] = NULL;
2173 for (l = priv->transport_sources; l; l = l->next) {
2174 GstRTSPMulticastTransportSource *s = l->data;
2175 g_slice_free (GstRTSPMulticastTransportSource, s);
2177 g_list_free (priv->transport_sources);
2178 priv->transport_sources = NULL;
2180 gst_object_unref (priv->send_src[0]);
2181 priv->send_src[0] = NULL;
2183 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2184 gst_object_unref (priv->send_src[1]);
2185 priv->send_src[1] = NULL;
2187 g_object_unref (priv->session);
2188 priv->session = NULL;
2190 gst_caps_unref (priv->caps);
2194 gst_object_unref (priv->srtpenc);
2196 gst_object_unref (priv->srtpdec);
2198 priv->is_joined = FALSE;
2199 g_mutex_unlock (&priv->lock);
2205 g_mutex_unlock (&priv->lock);
2208 transports_not_removed:
2210 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2211 g_mutex_unlock (&priv->lock);
2217 * gst_rtsp_stream_get_rtpinfo:
2218 * @stream: a #GstRTSPStream
2219 * @rtptime: (allow-none): result RTP timestamp
2220 * @seq: (allow-none): result RTP seqnum
2221 * @clock_rate: (allow-none): the clock rate
2222 * @running_time: (allow-none): result running-time
2224 * Retrieve the current rtptime, seq and running-time. This is used to
2225 * construct a RTPInfo reply header.
2227 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2230 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2231 guint * rtptime, guint * seq, guint * clock_rate,
2232 GstClockTime * running_time)
2234 GstRTSPStreamPrivate *priv;
2235 GstStructure *stats;
2236 GObjectClass *payobjclass;
2238 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2240 priv = stream->priv;
2242 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2244 g_mutex_lock (&priv->lock);
2246 if (g_object_class_find_property (payobjclass, "stats")) {
2247 g_object_get (priv->payloader, "stats", &stats, NULL);
2252 gst_structure_get_uint (stats, "seqnum", seq);
2255 gst_structure_get_uint (stats, "timestamp", rtptime);
2258 gst_structure_get_clock_time (stats, "running-time", running_time);
2261 gst_structure_get_uint (stats, "clock-rate", clock_rate);
2262 if (*clock_rate == 0 && running_time)
2263 *running_time = GST_CLOCK_TIME_NONE;
2265 gst_structure_free (stats);
2267 if (!g_object_class_find_property (payobjclass, "seqnum") ||
2268 !g_object_class_find_property (payobjclass, "timestamp"))
2272 g_object_get (priv->payloader, "seqnum", seq, NULL);
2275 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
2278 *running_time = GST_CLOCK_TIME_NONE;
2280 g_mutex_unlock (&priv->lock);
2287 GST_WARNING ("Could not get payloader stats");
2288 g_mutex_unlock (&priv->lock);
2294 * gst_rtsp_stream_get_caps:
2295 * @stream: a #GstRTSPStream
2297 * Retrieve the current caps of @stream.
2299 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
2303 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
2305 GstRTSPStreamPrivate *priv;
2308 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2310 priv = stream->priv;
2312 g_mutex_lock (&priv->lock);
2313 if ((result = priv->caps))
2314 gst_caps_ref (result);
2315 g_mutex_unlock (&priv->lock);
2321 * gst_rtsp_stream_recv_rtp:
2322 * @stream: a #GstRTSPStream
2323 * @buffer: (transfer full): a #GstBuffer
2325 * Handle an RTP buffer for the stream. This method is usually called when a
2326 * message has been received from a client using the TCP transport.
2328 * This function takes ownership of @buffer.
2330 * Returns: a GstFlowReturn.
2333 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
2335 GstRTSPStreamPrivate *priv;
2337 GstElement *element;
2339 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2340 priv = stream->priv;
2341 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2342 g_return_val_if_fail (priv->is_joined, FALSE);
2344 g_mutex_lock (&priv->lock);
2345 if (priv->appsrc[0])
2346 element = gst_object_ref (priv->appsrc[0]);
2349 g_mutex_unlock (&priv->lock);
2352 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2353 gst_object_unref (element);
2361 * gst_rtsp_stream_recv_rtcp:
2362 * @stream: a #GstRTSPStream
2363 * @buffer: (transfer full): a #GstBuffer
2365 * Handle an RTCP buffer for the stream. This method is usually called when a
2366 * message has been received from a client using the TCP transport.
2368 * This function takes ownership of @buffer.
2370 * Returns: a GstFlowReturn.
2373 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
2375 GstRTSPStreamPrivate *priv;
2377 GstElement *element;
2379 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2380 priv = stream->priv;
2381 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2383 if (!priv->is_joined) {
2384 gst_buffer_unref (buffer);
2385 return GST_FLOW_NOT_LINKED;
2387 g_mutex_lock (&priv->lock);
2388 if (priv->appsrc[1])
2389 element = gst_object_ref (priv->appsrc[1]);
2392 g_mutex_unlock (&priv->lock);
2395 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2396 gst_object_unref (element);
2399 gst_buffer_unref (buffer);
2404 /* must be called with lock */
2406 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2409 GstRTSPStreamPrivate *priv = stream->priv;
2410 const GstRTSPTransport *tr;
2412 tr = gst_rtsp_stream_transport_get_transport (trans);
2414 switch (tr->lower_transport) {
2415 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2417 GstRTSPMulticastTransportSource *source;
2420 bin = GST_BIN (gst_object_get_parent (GST_OBJECT (priv->funnel[0])));
2425 GstPad *selpad, *pad;
2427 source = g_slice_new0 (GstRTSPMulticastTransportSource);
2428 source->transport = trans;
2430 for (i = 0; i < 2; i++) {
2432 g_strdup_printf ("udp://%s:%d", tr->destination,
2433 (i == 0) ? tr->port.min : tr->port.max);
2435 gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2438 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2440 gst_element_set_state (source->udpsrc[i], GST_STATE_PLAYING);
2441 gst_element_set_locked_state (source->udpsrc[i], TRUE);
2443 gst_bin_add (bin, source->udpsrc[i]);
2445 /* and link to the funnel v4 */
2446 source->selpad[i] = selpad =
2447 gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2448 pad = gst_element_get_static_pad (source->udpsrc[i], "src");
2449 gst_pad_link (pad, selpad);
2450 gst_object_unref (pad);
2451 gst_object_unref (selpad);
2453 gst_object_unref (bin);
2455 priv->transport_sources =
2456 g_list_prepend (priv->transport_sources, source);
2460 for (l = priv->transport_sources; l; l = l->next) {
2463 if (source->transport == trans) {
2464 priv->transport_sources =
2465 g_list_delete_link (priv->transport_sources, l);
2473 for (i = 0; i < 2; i++) {
2474 /* Will automatically unlink everything */
2475 gst_bin_remove (bin,
2476 GST_ELEMENT (gst_object_ref (source->udpsrc[i])));
2478 gst_element_set_state (source->udpsrc[i], GST_STATE_NULL);
2479 gst_object_unref (source->udpsrc[i]);
2481 gst_element_release_request_pad (priv->funnel[i],
2485 g_slice_free (GstRTSPMulticastTransportSource, source);
2489 /* fall through for the generic case */
2491 case GST_RTSP_LOWER_TRANS_UDP:
2497 dest = tr->destination;
2498 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2503 min = tr->client_port.min;
2504 max = tr->client_port.max;
2509 GST_INFO ("setting ttl-mc %d", ttl);
2510 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
2511 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
2513 GST_INFO ("adding %s:%d-%d", dest, min, max);
2514 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
2515 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
2516 priv->transports = g_list_prepend (priv->transports, trans);
2518 GST_INFO ("removing %s:%d-%d", dest, min, max);
2519 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
2520 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
2521 priv->transports = g_list_remove (priv->transports, trans);
2523 priv->transports_cookie++;
2526 case GST_RTSP_LOWER_TRANS_TCP:
2528 GST_INFO ("adding TCP %s", tr->destination);
2529 priv->transports = g_list_prepend (priv->transports, trans);
2531 GST_INFO ("removing TCP %s", tr->destination);
2532 priv->transports = g_list_remove (priv->transports, trans);
2534 priv->transports_cookie++;
2537 goto unknown_transport;
2544 GST_INFO ("Unknown transport %d", tr->lower_transport);
2551 * gst_rtsp_stream_add_transport:
2552 * @stream: a #GstRTSPStream
2553 * @trans: (transfer none): a #GstRTSPStreamTransport
2555 * Add the transport in @trans to @stream. The media of @stream will
2556 * then also be send to the values configured in @trans.
2558 * @stream must be joined to a bin.
2560 * @trans must contain a valid #GstRTSPTransport.
2562 * Returns: %TRUE if @trans was added
2565 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
2566 GstRTSPStreamTransport * trans)
2568 GstRTSPStreamPrivate *priv;
2571 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2572 priv = stream->priv;
2573 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2574 g_return_val_if_fail (priv->is_joined, FALSE);
2576 g_mutex_lock (&priv->lock);
2577 res = update_transport (stream, trans, TRUE);
2578 g_mutex_unlock (&priv->lock);
2584 * gst_rtsp_stream_remove_transport:
2585 * @stream: a #GstRTSPStream
2586 * @trans: (transfer none): a #GstRTSPStreamTransport
2588 * Remove the transport in @trans from @stream. The media of @stream will
2589 * not be sent to the values configured in @trans.
2591 * @stream must be joined to a bin.
2593 * @trans must contain a valid #GstRTSPTransport.
2595 * Returns: %TRUE if @trans was removed
2598 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2599 GstRTSPStreamTransport * trans)
2601 GstRTSPStreamPrivate *priv;
2604 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2605 priv = stream->priv;
2606 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2607 g_return_val_if_fail (priv->is_joined, FALSE);
2609 g_mutex_lock (&priv->lock);
2610 res = update_transport (stream, trans, FALSE);
2611 g_mutex_unlock (&priv->lock);
2617 * gst_rtsp_stream_update_crypto:
2618 * @stream: a #GstRTSPStream
2620 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
2622 * Update the new crypto information for @ssrc in @stream. If information
2623 * for @ssrc did not exist, it will be added. If information
2624 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
2625 * be removed from @stream.
2627 * Returns: %TRUE if @crypto could be updated
2630 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
2631 guint ssrc, GstCaps * crypto)
2633 GstRTSPStreamPrivate *priv;
2635 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2636 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
2638 priv = stream->priv;
2640 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
2642 g_mutex_lock (&priv->lock);
2644 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
2645 gst_caps_ref (crypto));
2647 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
2648 g_mutex_unlock (&priv->lock);
2654 * gst_rtsp_stream_get_rtp_socket:
2655 * @stream: a #GstRTSPStream
2656 * @family: the socket family
2658 * Get the RTP socket from @stream for a @family.
2660 * @stream must be joined to a bin.
2662 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
2663 * socket could be allocated for @family. Unref after usage
2666 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2668 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2672 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2673 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2674 family == G_SOCKET_FAMILY_IPV6, NULL);
2675 g_return_val_if_fail (priv->udpsink[0], NULL);
2677 if (family == G_SOCKET_FAMILY_IPV6)
2682 g_object_get (priv->udpsink[0], name, &socket, NULL);
2688 * gst_rtsp_stream_get_rtcp_socket:
2689 * @stream: a #GstRTSPStream
2690 * @family: the socket family
2692 * Get the RTCP socket from @stream for a @family.
2694 * @stream must be joined to a bin.
2696 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
2697 * socket could be allocated for @family. Unref after usage
2700 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
2702 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2706 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2707 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2708 family == G_SOCKET_FAMILY_IPV6, NULL);
2709 g_return_val_if_fail (priv->udpsink[1], NULL);
2711 if (family == G_SOCKET_FAMILY_IPV6)
2716 g_object_get (priv->udpsink[1], name, &socket, NULL);
2722 * gst_rtsp_stream_set_seqnum:
2723 * @stream: a #GstRTSPStream
2724 * @seqnum: a new sequence number
2726 * Configure the sequence number in the payloader of @stream to @seqnum.
2729 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
2731 GstRTSPStreamPrivate *priv;
2733 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
2735 priv = stream->priv;
2737 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
2741 * gst_rtsp_stream_get_seqnum:
2742 * @stream: a #GstRTSPStream
2744 * Get the configured sequence number in the payloader of @stream.
2746 * Returns: the sequence number of the payloader.
2749 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
2751 GstRTSPStreamPrivate *priv;
2754 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
2756 priv = stream->priv;
2758 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
2764 * gst_rtsp_stream_transport_filter:
2765 * @stream: a #GstRTSPStream
2766 * @func: (scope call) (allow-none): a callback
2767 * @user_data: (closure): user data passed to @func
2769 * Call @func for each transport managed by @stream. The result value of @func
2770 * determines what happens to the transport. @func will be called with @stream
2771 * locked so no further actions on @stream can be performed from @func.
2773 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
2776 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
2778 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
2779 * will also be added with an additional ref to the result #GList of this
2782 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
2784 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
2785 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2786 * element in the #GList should be unreffed before the list is freed.
2789 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
2790 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
2792 GstRTSPStreamPrivate *priv;
2793 GList *result, *walk, *next;
2794 GHashTable *visited;
2797 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2799 priv = stream->priv;
2803 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
2805 g_mutex_lock (&priv->lock);
2807 cookie = priv->transports_cookie;
2808 for (walk = priv->transports; walk; walk = next) {
2809 GstRTSPStreamTransport *trans = walk->data;
2810 GstRTSPFilterResult res;
2813 next = g_list_next (walk);
2816 /* only visit each transport once */
2817 if (g_hash_table_contains (visited, trans))
2820 g_hash_table_add (visited, g_object_ref (trans));
2821 g_mutex_unlock (&priv->lock);
2823 res = func (stream, trans, user_data);
2825 g_mutex_lock (&priv->lock);
2827 res = GST_RTSP_FILTER_REF;
2829 changed = (cookie != priv->transports_cookie);
2832 case GST_RTSP_FILTER_REMOVE:
2833 update_transport (stream, trans, FALSE);
2835 case GST_RTSP_FILTER_REF:
2836 result = g_list_prepend (result, g_object_ref (trans));
2838 case GST_RTSP_FILTER_KEEP:
2845 g_mutex_unlock (&priv->lock);
2848 g_hash_table_unref (visited);
2853 static GstPadProbeReturn
2854 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2856 GstRTSPStreamPrivate *priv;
2857 GstRTSPStream *stream;
2860 priv = stream->priv;
2862 GST_DEBUG_OBJECT (pad, "now blocking");
2864 g_mutex_lock (&priv->lock);
2865 priv->blocking = TRUE;
2866 g_mutex_unlock (&priv->lock);
2868 gst_element_post_message (priv->payloader,
2869 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
2870 gst_structure_new_empty ("GstRTSPStreamBlocking")));
2872 return GST_PAD_PROBE_OK;
2876 * gst_rtsp_stream_set_blocked:
2877 * @stream: a #GstRTSPStream
2878 * @blocked: boolean indicating we should block or unblock
2880 * Blocks or unblocks the dataflow on @stream.
2882 * Returns: %TRUE on success
2885 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
2887 GstRTSPStreamPrivate *priv;
2889 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2891 priv = stream->priv;
2893 g_mutex_lock (&priv->lock);
2895 priv->blocking = FALSE;
2896 if (priv->blocked_id == 0) {
2897 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
2898 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
2899 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
2900 g_object_ref (stream), g_object_unref);
2903 if (priv->blocked_id != 0) {
2904 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
2905 priv->blocked_id = 0;
2906 priv->blocking = FALSE;
2909 g_mutex_unlock (&priv->lock);
2915 * gst_rtsp_stream_is_blocking:
2916 * @stream: a #GstRTSPStream
2918 * Check if @stream is blocking on a #GstBuffer.
2920 * Returns: %TRUE if @stream is blocking
2923 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
2925 GstRTSPStreamPrivate *priv;
2928 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2930 priv = stream->priv;
2932 g_mutex_lock (&priv->lock);
2933 result = priv->blocking;
2934 g_mutex_unlock (&priv->lock);
2940 * gst_rtsp_stream_query_position:
2941 * @stream: a #GstRTSPStream
2943 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
2944 * the RTP parts of the pipeline and not the RTCP parts.
2946 * Returns: %TRUE if the position could be queried
2949 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
2951 GstRTSPStreamPrivate *priv;
2955 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2957 priv = stream->priv;
2959 g_mutex_lock (&priv->lock);
2960 if ((sink = priv->udpsink[0]))
2961 gst_object_ref (sink);
2962 g_mutex_unlock (&priv->lock);
2967 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
2968 gst_object_unref (sink);
2974 * gst_rtsp_stream_query_stop:
2975 * @stream: a #GstRTSPStream
2977 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
2978 * the RTP parts of the pipeline and not the RTCP parts.
2980 * Returns: %TRUE if the stop could be queried
2983 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
2985 GstRTSPStreamPrivate *priv;
2990 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2992 priv = stream->priv;
2994 g_mutex_lock (&priv->lock);
2995 if ((sink = priv->udpsink[0]))
2996 gst_object_ref (sink);
2997 g_mutex_unlock (&priv->lock);
3002 query = gst_query_new_segment (GST_FORMAT_TIME);
3003 if ((ret = gst_element_query (sink, query))) {
3006 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3007 if (format != GST_FORMAT_TIME)
3010 gst_query_unref (query);
3011 gst_object_unref (sink);