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))
61 struct _GstRTSPStreamPrivate
66 GstElement *payloader;
71 GstRTSPLowerTrans protocols;
73 /* pads on the rtpbin */
74 GstPad *send_rtp_sink;
78 /* the RTPSession object */
81 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
83 GstElement *udpsrc_v4[2];
85 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
87 GstElement *udpsrc_v6[2];
89 GstElement *udpsink[2];
91 /* for TCP transport */
92 GstElement *appsrc[2];
93 GstElement *appqueue[2];
94 GstElement *appsink[2];
97 GstElement *funnel[2];
99 /* server ports for sending/receiving over ipv4 */
100 GstRTSPRange server_port_v4;
101 GstRTSPAddress *server_addr_v4;
104 /* server ports for sending/receiving over ipv6 */
105 GstRTSPRange server_port_v6;
106 GstRTSPAddress *server_addr_v6;
109 /* multicast addresses */
110 GstRTSPAddressPool *pool;
111 GstRTSPAddress *addr_v4;
112 GstRTSPAddress *addr_v6;
114 /* the caps of the stream */
118 /* transports we stream to */
124 /* stream blocking */
129 #define DEFAULT_CONTROL NULL
130 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
131 GST_RTSP_LOWER_TRANS_TCP
141 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
142 #define GST_CAT_DEFAULT rtsp_stream_debug
144 static GQuark ssrc_stream_map_key;
146 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
147 GValue * value, GParamSpec * pspec);
148 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
149 const GValue * value, GParamSpec * pspec);
151 static void gst_rtsp_stream_finalize (GObject * obj);
153 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
156 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
158 GObjectClass *gobject_class;
160 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
162 gobject_class = G_OBJECT_CLASS (klass);
164 gobject_class->get_property = gst_rtsp_stream_get_property;
165 gobject_class->set_property = gst_rtsp_stream_set_property;
166 gobject_class->finalize = gst_rtsp_stream_finalize;
168 g_object_class_install_property (gobject_class, PROP_CONTROL,
169 g_param_spec_string ("control", "Control",
170 "The control string for this stream", DEFAULT_CONTROL,
171 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
173 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
174 g_param_spec_flags ("protocols", "Protocols",
175 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
176 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
180 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
184 gst_rtsp_stream_init (GstRTSPStream * stream)
186 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
188 GST_DEBUG ("new stream %p", stream);
193 priv->control = g_strdup (DEFAULT_CONTROL);
194 priv->protocols = DEFAULT_PROTOCOLS;
196 g_mutex_init (&priv->lock);
200 gst_rtsp_stream_finalize (GObject * obj)
202 GstRTSPStream *stream;
203 GstRTSPStreamPrivate *priv;
205 stream = GST_RTSP_STREAM (obj);
208 GST_DEBUG ("finalize stream %p", stream);
210 /* we really need to be unjoined now */
211 g_return_if_fail (!priv->is_joined);
214 gst_rtsp_address_free (priv->addr_v4);
216 gst_rtsp_address_free (priv->addr_v6);
217 if (priv->server_addr_v4)
218 gst_rtsp_address_free (priv->server_addr_v4);
219 if (priv->server_addr_v6)
220 gst_rtsp_address_free (priv->server_addr_v6);
222 g_object_unref (priv->pool);
223 gst_object_unref (priv->payloader);
224 gst_object_unref (priv->srcpad);
225 g_free (priv->control);
226 g_mutex_clear (&priv->lock);
228 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
232 gst_rtsp_stream_get_property (GObject * object, guint propid,
233 GValue * value, GParamSpec * pspec)
235 GstRTSPStream *stream = GST_RTSP_STREAM (object);
239 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
242 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
250 gst_rtsp_stream_set_property (GObject * object, guint propid,
251 const GValue * value, GParamSpec * pspec)
253 GstRTSPStream *stream = GST_RTSP_STREAM (object);
257 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
260 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
268 * gst_rtsp_stream_new:
271 * @payloader: a #GstElement
273 * Create a new media stream with index @idx that handles RTP data on
274 * @srcpad and has a payloader element @payloader.
276 * Returns: a new #GstRTSPStream
279 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
281 GstRTSPStreamPrivate *priv;
282 GstRTSPStream *stream;
284 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
285 g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
286 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
288 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
291 priv->payloader = gst_object_ref (payloader);
292 priv->srcpad = gst_object_ref (srcpad);
298 * gst_rtsp_stream_get_index:
299 * @stream: a #GstRTSPStream
301 * Get the stream index.
303 * Return: the stream index.
306 gst_rtsp_stream_get_index (GstRTSPStream * stream)
308 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
310 return stream->priv->idx;
314 * gst_rtsp_stream_get_pt:
315 * @stream: a #GstRTSPStream
317 * Get the stream payload type.
319 * Return: the stream payload type.
322 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
324 GstRTSPStreamPrivate *priv;
327 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
331 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
337 * gst_rtsp_stream_get_srcpad:
338 * @stream: a #GstRTSPStream
340 * Get the srcpad associated with @stream.
342 * Returns: (transfer full): the srcpad. Unref after usage.
345 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
347 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
349 return gst_object_ref (stream->priv->srcpad);
353 * gst_rtsp_stream_get_control:
354 * @stream: a #GstRTSPStream
356 * Get the control string to identify this stream.
358 * Returns: (transfer full): the control string. free after usage.
361 gst_rtsp_stream_get_control (GstRTSPStream * stream)
363 GstRTSPStreamPrivate *priv;
366 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
370 g_mutex_lock (&priv->lock);
371 if ((result = g_strdup (priv->control)) == NULL)
372 result = g_strdup_printf ("stream=%u", priv->idx);
373 g_mutex_unlock (&priv->lock);
379 * gst_rtsp_stream_set_control:
380 * @stream: a #GstRTSPStream
381 * @control: a control string
383 * Set the control string in @stream.
386 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
388 GstRTSPStreamPrivate *priv;
390 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
394 g_mutex_lock (&priv->lock);
395 g_free (priv->control);
396 priv->control = g_strdup (control);
397 g_mutex_unlock (&priv->lock);
401 * gst_rtsp_stream_has_control:
402 * @stream: a #GstRTSPStream
403 * @control: a control string
405 * Check if @stream has the control string @control.
407 * Returns: %TRUE is @stream has @control as the control string
410 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
412 GstRTSPStreamPrivate *priv;
415 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
419 g_mutex_lock (&priv->lock);
421 res = (g_strcmp0 (priv->control, control) == 0);
424 sscanf (control, "stream=%u", &streamid);
425 res = (streamid == priv->idx);
427 g_mutex_unlock (&priv->lock);
433 * gst_rtsp_stream_set_mtu:
434 * @stream: a #GstRTSPStream
437 * Configure the mtu in the payloader of @stream to @mtu.
440 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
442 GstRTSPStreamPrivate *priv;
444 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
448 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
450 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
454 * gst_rtsp_stream_get_mtu:
455 * @stream: a #GstRTSPStream
457 * Get the configured MTU in the payloader of @stream.
459 * Returns: the MTU of the payloader.
462 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
464 GstRTSPStreamPrivate *priv;
467 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
471 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
476 /* Update the dscp qos property on the udp sinks */
478 update_dscp_qos (GstRTSPStream * stream)
480 GstRTSPStreamPrivate *priv;
482 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
486 if (priv->udpsink[0]) {
487 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
491 if (priv->udpsink[1]) {
492 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
498 * gst_rtsp_stream_set_dscp_qos:
499 * @stream: a #GstRTSPStream
500 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
502 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
505 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
507 GstRTSPStreamPrivate *priv;
509 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
513 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
515 if (dscp_qos < -1 || dscp_qos > 63) {
516 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
520 priv->dscp_qos = dscp_qos;
522 update_dscp_qos (stream);
526 * gst_rtsp_stream_get_dscp_qos:
527 * @stream: a #GstRTSPStream
529 * Get the configured DSCP QoS in of the outgoing sockets.
531 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
534 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
536 GstRTSPStreamPrivate *priv;
538 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
542 return priv->dscp_qos;
546 * gst_rtsp_stream_set_protocols:
547 * @stream: a #GstRTSPStream
548 * @protocols: the new flags
550 * Configure the allowed lower transport for @stream.
553 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
554 GstRTSPLowerTrans protocols)
556 GstRTSPStreamPrivate *priv;
558 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
562 g_mutex_lock (&priv->lock);
563 priv->protocols = protocols;
564 g_mutex_unlock (&priv->lock);
568 * gst_rtsp_stream_get_protocols:
569 * @stream: a #GstRTSPStream
571 * Get the allowed protocols of @stream.
573 * Returns: a #GstRTSPLowerTrans
576 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
578 GstRTSPStreamPrivate *priv;
579 GstRTSPLowerTrans res;
581 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
582 GST_RTSP_LOWER_TRANS_UNKNOWN);
586 g_mutex_lock (&priv->lock);
587 res = priv->protocols;
588 g_mutex_unlock (&priv->lock);
594 * gst_rtsp_stream_set_address_pool:
595 * @stream: a #GstRTSPStream
596 * @pool: a #GstRTSPAddressPool
598 * configure @pool to be used as the address pool of @stream.
601 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
602 GstRTSPAddressPool * pool)
604 GstRTSPStreamPrivate *priv;
605 GstRTSPAddressPool *old;
607 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
611 GST_LOG_OBJECT (stream, "set address pool %p", pool);
613 g_mutex_lock (&priv->lock);
614 if ((old = priv->pool) != pool)
615 priv->pool = pool ? g_object_ref (pool) : NULL;
618 g_mutex_unlock (&priv->lock);
621 g_object_unref (old);
625 * gst_rtsp_stream_get_address_pool:
626 * @stream: a #GstRTSPStream
628 * Get the #GstRTSPAddressPool used as the address pool of @stream.
630 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
634 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
636 GstRTSPStreamPrivate *priv;
637 GstRTSPAddressPool *result;
639 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
643 g_mutex_lock (&priv->lock);
644 if ((result = priv->pool))
645 g_object_ref (result);
646 g_mutex_unlock (&priv->lock);
652 * gst_rtsp_stream_get_multicast_address:
653 * @stream: a #GstRTSPStream
654 * @family: the #GSocketFamily
656 * Get the multicast address of @stream for @family.
658 * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
659 * allocated. gst_rtsp_address_free() after usage.
662 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
663 GSocketFamily family)
665 GstRTSPStreamPrivate *priv;
666 GstRTSPAddress *result;
667 GstRTSPAddress **addrp;
668 GstRTSPAddressFlags flags;
670 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
674 if (family == G_SOCKET_FAMILY_IPV6) {
675 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
676 addrp = &priv->addr_v4;
678 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
679 addrp = &priv->addr_v6;
682 g_mutex_lock (&priv->lock);
683 if (*addrp == NULL) {
684 if (priv->pool == NULL)
687 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
689 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
693 result = gst_rtsp_address_copy (*addrp);
694 g_mutex_unlock (&priv->lock);
701 GST_ERROR_OBJECT (stream, "no address pool specified");
702 g_mutex_unlock (&priv->lock);
707 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
708 g_mutex_unlock (&priv->lock);
714 * gst_rtsp_stream_reserve_address:
715 * @stream: a #GstRTSPStream
716 * @address: an address
721 * Reserve @address and @port as the address and port of @stream.
723 * Returns: the #GstRTSPAddress of @stream or %NULL when the address could be
724 * reserved. gst_rtsp_address_free() after usage.
727 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
728 const gchar * address, guint port, guint n_ports, guint ttl)
730 GstRTSPStreamPrivate *priv;
731 GstRTSPAddress *result;
733 GSocketFamily family;
734 GstRTSPAddress **addrp;
736 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
737 g_return_val_if_fail (address != NULL, NULL);
738 g_return_val_if_fail (port > 0, NULL);
739 g_return_val_if_fail (n_ports > 0, NULL);
740 g_return_val_if_fail (ttl > 0, NULL);
744 addr = g_inet_address_new_from_string (address);
746 GST_ERROR ("failed to get inet addr from %s", address);
747 family = G_SOCKET_FAMILY_IPV4;
749 family = g_inet_address_get_family (addr);
750 g_object_unref (addr);
753 if (family == G_SOCKET_FAMILY_IPV6)
754 addrp = &priv->addr_v4;
756 addrp = &priv->addr_v6;
758 g_mutex_lock (&priv->lock);
759 if (*addrp == NULL) {
760 GstRTSPAddressPoolResult res;
762 if (priv->pool == NULL)
765 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
766 port, n_ports, ttl, addrp);
767 if (res != GST_RTSP_ADDRESS_POOL_OK)
770 if (strcmp ((*addrp)->address, address) ||
771 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
772 (*addrp)->ttl != ttl)
773 goto different_address;
775 result = gst_rtsp_address_copy (*addrp);
776 g_mutex_unlock (&priv->lock);
783 GST_ERROR_OBJECT (stream, "no address pool specified");
784 g_mutex_unlock (&priv->lock);
789 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
791 g_mutex_unlock (&priv->lock);
796 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
797 " reserved", address);
798 g_mutex_unlock (&priv->lock);
804 alloc_ports_one_family (GstRTSPAddressPool * pool, gint buffer_size,
805 GSocketFamily family, GstElement * udpsrc_out[2],
806 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
807 GstRTSPAddress ** server_addr_out)
809 GstStateChangeReturn ret;
810 GstElement *udpsrc0, *udpsrc1;
811 GstElement *udpsink0, *udpsink1;
812 GSocket *rtp_socket = NULL;
813 GSocket *rtcp_socket;
814 gint tmp_rtp, tmp_rtcp;
816 gint rtpport, rtcpport;
817 GList *rejected_addresses = NULL;
818 GstRTSPAddress *addr = NULL;
819 GInetAddress *inetaddr = NULL;
820 GSocketAddress *rtp_sockaddr = NULL;
821 GSocketAddress *rtcp_sockaddr = NULL;
822 const gchar *multisink_socket;
824 if (family == G_SOCKET_FAMILY_IPV6)
825 multisink_socket = "socket-v6";
827 multisink_socket = "socket";
835 /* Start with random port */
838 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
839 G_SOCKET_PROTOCOL_UDP, NULL);
841 goto no_udp_protocol;
843 if (*server_addr_out)
844 gst_rtsp_address_free (*server_addr_out);
846 /* try to allocate 2 UDP ports, the RTP port should be an even
847 * number and the RTCP port should be the next (uneven) port */
850 if (rtp_socket == NULL) {
851 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
852 G_SOCKET_PROTOCOL_UDP, NULL);
854 goto no_udp_protocol;
857 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
858 GstRTSPAddressFlags flags;
861 rejected_addresses = g_list_prepend (rejected_addresses, addr);
863 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
864 if (family == G_SOCKET_FAMILY_IPV6)
865 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
867 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
869 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
874 tmp_rtp = addr->port;
876 g_clear_object (&inetaddr);
877 inetaddr = g_inet_address_new_from_string (addr->address);
885 if (inetaddr == NULL)
886 inetaddr = g_inet_address_new_any (family);
889 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
890 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
891 g_object_unref (rtp_sockaddr);
894 g_object_unref (rtp_sockaddr);
896 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
897 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
898 g_clear_object (&rtp_sockaddr);
903 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
904 g_object_unref (rtp_sockaddr);
906 /* check if port is even */
907 if ((tmp_rtp & 1) != 0) {
908 /* port not even, close and allocate another */
910 g_clear_object (&rtp_socket);
915 tmp_rtcp = tmp_rtp + 1;
917 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
918 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
919 g_object_unref (rtcp_sockaddr);
920 g_clear_object (&rtp_socket);
923 g_object_unref (rtcp_sockaddr);
925 g_clear_object (&inetaddr);
927 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
928 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
930 if (udpsrc0 == NULL || udpsrc1 == NULL)
931 goto no_udp_protocol;
933 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
934 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
936 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
937 if (ret == GST_STATE_CHANGE_FAILURE)
939 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
940 if (ret == GST_STATE_CHANGE_FAILURE)
943 /* all fine, do port check */
944 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
945 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
947 /* this should not happen... */
948 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
952 udpsink0 = udpsink_out[0];
954 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
957 goto no_udp_protocol;
959 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
960 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
963 udpsink1 = udpsink_out[1];
965 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
968 goto no_udp_protocol;
970 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
971 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
972 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
974 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
975 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
976 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
977 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
978 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
979 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
980 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
981 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
983 /* we keep these elements, we will further configure them when the
984 * client told us to really use the UDP ports. */
985 udpsrc_out[0] = udpsrc0;
986 udpsrc_out[1] = udpsrc1;
987 udpsink_out[0] = udpsink0;
988 udpsink_out[1] = udpsink1;
989 server_port_out->min = rtpport;
990 server_port_out->max = rtcpport;
992 *server_addr_out = addr;
993 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
995 g_object_unref (rtp_socket);
996 g_object_unref (rtcp_socket);
1024 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1025 gst_object_unref (udpsrc0);
1028 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1029 gst_object_unref (udpsrc1);
1032 gst_element_set_state (udpsink0, GST_STATE_NULL);
1033 gst_object_unref (udpsink0);
1036 gst_element_set_state (udpsink1, GST_STATE_NULL);
1037 gst_object_unref (udpsink1);
1040 g_object_unref (inetaddr);
1041 g_list_free_full (rejected_addresses,
1042 (GDestroyNotify) gst_rtsp_address_free);
1044 gst_rtsp_address_free (addr);
1046 g_object_unref (rtp_socket);
1048 g_object_unref (rtcp_socket);
1053 /* must be called with lock */
1055 alloc_ports (GstRTSPStream * stream)
1057 GstRTSPStreamPrivate *priv = stream->priv;
1059 priv->have_ipv4 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1060 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1061 &priv->server_port_v4, &priv->server_addr_v4);
1063 priv->have_ipv6 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1064 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1065 &priv->server_port_v6, &priv->server_addr_v6);
1067 return priv->have_ipv4 || priv->have_ipv6;
1071 * gst_rtsp_stream_get_server_port:
1072 * @stream: a #GstRTSPStream
1073 * @server_port: (out): result server port
1074 * @family: the port family to get
1076 * Fill @server_port with the port pair used by the server. This function can
1077 * only be called when @stream has been joined.
1080 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1081 GstRTSPRange * server_port, GSocketFamily family)
1083 GstRTSPStreamPrivate *priv;
1085 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1086 priv = stream->priv;
1087 g_return_if_fail (priv->is_joined);
1089 g_mutex_lock (&priv->lock);
1090 if (family == G_SOCKET_FAMILY_IPV4) {
1092 *server_port = priv->server_port_v4;
1095 *server_port = priv->server_port_v6;
1097 g_mutex_unlock (&priv->lock);
1101 * gst_rtsp_stream_get_rtpsession:
1102 * @stream: a #GstRTSPStream
1104 * Get the RTP session of this stream.
1106 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1109 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1111 GstRTSPStreamPrivate *priv;
1114 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1116 priv = stream->priv;
1118 g_mutex_lock (&priv->lock);
1119 if ((session = priv->session))
1120 g_object_ref (session);
1121 g_mutex_unlock (&priv->lock);
1127 * gst_rtsp_stream_get_ssrc:
1128 * @stream: a #GstRTSPStream
1129 * @ssrc: (out): result ssrc
1131 * Get the SSRC used by the RTP session of this stream. This function can only
1132 * be called when @stream has been joined.
1135 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1137 GstRTSPStreamPrivate *priv;
1139 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1140 priv = stream->priv;
1141 g_return_if_fail (priv->is_joined);
1143 g_mutex_lock (&priv->lock);
1144 if (ssrc && priv->session)
1145 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1146 g_mutex_unlock (&priv->lock);
1149 /* executed from streaming thread */
1151 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1153 GstRTSPStreamPrivate *priv = stream->priv;
1154 GstCaps *newcaps, *oldcaps;
1156 newcaps = gst_pad_get_current_caps (pad);
1158 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1161 g_mutex_lock (&priv->lock);
1162 oldcaps = priv->caps;
1163 priv->caps = newcaps;
1164 g_mutex_unlock (&priv->lock);
1167 gst_caps_unref (oldcaps);
1171 dump_structure (const GstStructure * s)
1175 sstr = gst_structure_to_string (s);
1176 GST_INFO ("structure: %s", sstr);
1180 static GstRTSPStreamTransport *
1181 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1183 GstRTSPStreamPrivate *priv = stream->priv;
1185 GstRTSPStreamTransport *result = NULL;
1190 if (rtcp_from == NULL)
1193 tmp = g_strrstr (rtcp_from, ":");
1197 port = atoi (tmp + 1);
1198 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1200 g_mutex_lock (&priv->lock);
1201 GST_INFO ("finding %s:%d in %d transports", dest, port,
1202 g_list_length (priv->transports));
1204 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1205 GstRTSPStreamTransport *trans = walk->data;
1206 const GstRTSPTransport *tr;
1209 tr = gst_rtsp_stream_transport_get_transport (trans);
1211 min = tr->client_port.min;
1212 max = tr->client_port.max;
1214 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1220 g_object_ref (result);
1221 g_mutex_unlock (&priv->lock);
1228 static GstRTSPStreamTransport *
1229 check_transport (GObject * source, GstRTSPStream * stream)
1231 GstStructure *stats;
1232 GstRTSPStreamTransport *trans;
1234 /* see if we have a stream to match with the origin of the RTCP packet */
1235 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1236 if (trans == NULL) {
1237 g_object_get (source, "stats", &stats, NULL);
1239 const gchar *rtcp_from;
1241 dump_structure (stats);
1243 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1244 if ((trans = find_transport (stream, rtcp_from))) {
1245 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1247 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1250 gst_structure_free (stats);
1258 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1260 GstRTSPStreamTransport *trans;
1262 GST_INFO ("%p: new source %p", stream, source);
1264 trans = check_transport (source, stream);
1267 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1271 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1273 GST_INFO ("%p: new SDES %p", stream, source);
1277 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1279 GstRTSPStreamTransport *trans;
1281 trans = check_transport (source, stream);
1284 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1285 gst_rtsp_stream_transport_keep_alive (trans);
1289 GstStructure *stats;
1290 g_object_get (source, "stats", &stats, NULL);
1292 dump_structure (stats);
1293 gst_structure_free (stats);
1300 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1302 GST_INFO ("%p: source %p bye", stream, source);
1306 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1308 GstRTSPStreamTransport *trans;
1310 GST_INFO ("%p: source %p bye timeout", stream, source);
1312 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1313 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1314 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1319 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1321 GstRTSPStreamTransport *trans;
1323 GST_INFO ("%p: source %p timeout", stream, source);
1325 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1326 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1327 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1331 static GstFlowReturn
1332 handle_new_sample (GstAppSink * sink, gpointer user_data)
1334 GstRTSPStreamPrivate *priv;
1338 GstRTSPStream *stream;
1340 sample = gst_app_sink_pull_sample (sink);
1344 stream = (GstRTSPStream *) user_data;
1345 priv = stream->priv;
1346 buffer = gst_sample_get_buffer (sample);
1348 g_mutex_lock (&priv->lock);
1349 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1350 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1352 if (GST_ELEMENT_CAST (sink) == priv->appsink[0]) {
1353 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1355 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1358 g_mutex_unlock (&priv->lock);
1360 gst_sample_unref (sample);
1365 static GstAppSinkCallbacks sink_cb = {
1366 NULL, /* not interested in EOS */
1367 NULL, /* not interested in preroll samples */
1372 * gst_rtsp_stream_join_bin:
1373 * @stream: a #GstRTSPStream
1374 * @bin: a #GstBin to join
1375 * @rtpbin: a rtpbin element in @bin
1376 * @state: the target state of the new elements
1378 * Join the #GstBin @bin that contains the element @rtpbin.
1380 * @stream will link to @rtpbin, which must be inside @bin. The elements
1381 * added to @bin will be set to the state given in @state.
1383 * Returns: %TRUE on success.
1386 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1387 GstElement * rtpbin, GstState state)
1389 GstRTSPStreamPrivate *priv;
1393 GstPad *pad, *sinkpad, *selpad;
1394 GstPadLinkReturn ret;
1396 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1397 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1398 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1400 priv = stream->priv;
1402 g_mutex_lock (&priv->lock);
1403 if (priv->is_joined)
1406 /* create a session with the same index as the stream */
1409 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1411 if (!alloc_ports (stream))
1414 /* update the dscp qos field in the sinks */
1415 update_dscp_qos (stream);
1417 /* get a pad for sending RTP */
1418 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1419 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
1421 /* link the RTP pad to the session manager, it should not really fail unless
1422 * this is not really an RTP pad */
1423 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
1424 if (ret != GST_PAD_LINK_OK)
1427 /* get pads from the RTP session element for sending and receiving
1429 name = g_strdup_printf ("send_rtp_src_%u", idx);
1430 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
1432 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1433 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
1435 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1436 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
1438 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1439 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
1442 /* get the session */
1443 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
1445 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1447 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1449 g_signal_connect (priv->session, "on-ssrc-active",
1450 (GCallback) on_ssrc_active, stream);
1451 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1453 g_signal_connect (priv->session, "on-bye-timeout",
1454 (GCallback) on_bye_timeout, stream);
1455 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1458 for (i = 0; i < 2; i++) {
1459 GstPad *teepad, *queuepad;
1460 /* For the sender we create this bit of pipeline for both
1461 * RTP and RTCP. Sync and preroll are enabled on udpsink so
1462 * we need to add a queue before appsink to make the pipeline
1463 * not block. For the TCP case, we want to pump data to the
1464 * client as fast as possible anyway.
1466 * .--------. .-----. .---------.
1467 * | rtpbin | | tee | | udpsink |
1468 * | send->sink src->sink |
1469 * '--------' | | '---------'
1470 * | | .---------. .---------.
1471 * | | | queue | | appsink |
1472 * | src->sink src->sink |
1473 * '-----' '---------' '---------'
1475 * When only UDP is allowed, we skip the tee, queue and appsink and link the
1476 * udpsink directly to the session.
1479 gst_bin_add (bin, priv->udpsink[i]);
1480 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1482 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1483 /* make tee for RTP/RTCP */
1484 priv->tee[i] = gst_element_factory_make ("tee", NULL);
1485 gst_bin_add (bin, priv->tee[i]);
1487 /* and link to rtpbin send pad */
1488 pad = gst_element_get_static_pad (priv->tee[i], "sink");
1489 gst_pad_link (priv->send_src[i], pad);
1490 gst_object_unref (pad);
1492 /* link tee to udpsink */
1493 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1494 gst_pad_link (teepad, sinkpad);
1495 gst_object_unref (teepad);
1498 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1499 gst_bin_add (bin, priv->appqueue[i]);
1500 /* and link to tee */
1501 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1502 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1503 gst_pad_link (teepad, pad);
1504 gst_object_unref (pad);
1505 gst_object_unref (teepad);
1508 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1509 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1510 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1511 gst_bin_add (bin, priv->appsink[i]);
1512 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1513 &sink_cb, stream, NULL);
1514 /* and link to queue */
1515 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1516 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1517 gst_pad_link (queuepad, pad);
1518 gst_object_unref (pad);
1519 gst_object_unref (queuepad);
1521 /* else only udpsink needed, link it to the session */
1522 gst_pad_link (priv->send_src[i], sinkpad);
1524 gst_object_unref (sinkpad);
1526 /* For the receiver we create this bit of pipeline for both
1527 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1528 * and it is all funneled into the rtpbin receive pad.
1530 * .--------. .--------. .--------.
1531 * | udpsrc | | funnel | | rtpbin |
1532 * | src->sink src->sink |
1533 * '--------' | | '--------'
1537 * '--------' '--------'
1539 /* make funnel for the RTP/RTCP receivers */
1540 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1541 gst_bin_add (bin, priv->funnel[i]);
1543 pad = gst_element_get_static_pad (priv->funnel[i], "src");
1544 gst_pad_link (pad, priv->recv_sink[i]);
1545 gst_object_unref (pad);
1547 if (priv->udpsrc_v4[i]) {
1548 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1550 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
1551 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
1553 gst_bin_add (bin, priv->udpsrc_v4[i]);
1555 /* and link to the funnel v4 */
1556 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1557 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
1558 gst_pad_link (pad, selpad);
1559 gst_object_unref (pad);
1560 gst_object_unref (selpad);
1563 if (priv->udpsrc_v6[i]) {
1564 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
1565 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
1566 gst_bin_add (bin, priv->udpsrc_v6[i]);
1568 /* and link to the funnel v6 */
1569 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1570 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
1571 gst_pad_link (pad, selpad);
1572 gst_object_unref (pad);
1573 gst_object_unref (selpad);
1576 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1577 /* make and add appsrc */
1578 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1579 gst_bin_add (bin, priv->appsrc[i]);
1580 /* and link to the funnel */
1581 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1582 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
1583 gst_pad_link (pad, selpad);
1584 gst_object_unref (pad);
1585 gst_object_unref (selpad);
1588 /* check if we need to set to a special state */
1589 if (state != GST_STATE_NULL) {
1590 if (priv->udpsink[i])
1591 gst_element_set_state (priv->udpsink[i], state);
1592 if (priv->appsink[i])
1593 gst_element_set_state (priv->appsink[i], state);
1594 if (priv->appqueue[i])
1595 gst_element_set_state (priv->appqueue[i], state);
1597 gst_element_set_state (priv->tee[i], state);
1598 if (priv->funnel[i])
1599 gst_element_set_state (priv->funnel[i], state);
1600 if (priv->appsrc[i])
1601 gst_element_set_state (priv->appsrc[i], state);
1605 /* be notified of caps changes */
1606 priv->caps_sig = g_signal_connect (priv->send_rtp_sink, "notify::caps",
1607 (GCallback) caps_notify, stream);
1609 priv->is_joined = TRUE;
1610 g_mutex_unlock (&priv->lock);
1617 g_mutex_unlock (&priv->lock);
1622 g_mutex_unlock (&priv->lock);
1623 GST_WARNING ("failed to allocate ports %u", idx);
1628 GST_WARNING ("failed to link stream %u", idx);
1629 gst_object_unref (priv->send_rtp_sink);
1630 priv->send_rtp_sink = NULL;
1631 g_mutex_unlock (&priv->lock);
1637 * gst_rtsp_stream_leave_bin:
1638 * @stream: a #GstRTSPStream
1640 * @rtpbin: a rtpbin #GstElement
1642 * Remove the elements of @stream from @bin.
1644 * Return: %TRUE on success.
1647 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1648 GstElement * rtpbin)
1650 GstRTSPStreamPrivate *priv;
1653 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1654 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1655 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1657 priv = stream->priv;
1659 g_mutex_lock (&priv->lock);
1660 if (!priv->is_joined)
1661 goto was_not_joined;
1663 /* all transports must be removed by now */
1664 g_return_val_if_fail (priv->transports == NULL, FALSE);
1666 GST_INFO ("stream %p leaving bin", stream);
1668 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
1669 g_signal_handler_disconnect (priv->send_rtp_sink, priv->caps_sig);
1670 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
1671 gst_object_unref (priv->send_rtp_sink);
1672 priv->send_rtp_sink = NULL;
1674 for (i = 0; i < 2; i++) {
1675 if (priv->udpsink[i])
1676 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
1677 if (priv->appsink[i])
1678 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
1679 if (priv->appqueue[i])
1680 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
1682 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
1683 if (priv->funnel[i])
1684 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
1685 if (priv->appsrc[i])
1686 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
1687 if (priv->udpsrc_v4[i]) {
1688 /* and set udpsrc to NULL now before removing */
1689 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
1690 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
1691 /* removing them should also nicely release the request
1692 * pads when they finalize */
1693 gst_bin_remove (bin, priv->udpsrc_v4[i]);
1695 if (priv->udpsrc_v6[i]) {
1696 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
1697 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
1698 gst_bin_remove (bin, priv->udpsrc_v6[i]);
1700 if (priv->udpsink[i])
1701 gst_bin_remove (bin, priv->udpsink[i]);
1702 if (priv->appsrc[i])
1703 gst_bin_remove (bin, priv->appsrc[i]);
1704 if (priv->appsink[i])
1705 gst_bin_remove (bin, priv->appsink[i]);
1706 if (priv->appqueue[i])
1707 gst_bin_remove (bin, priv->appqueue[i]);
1709 gst_bin_remove (bin, priv->tee[i]);
1710 if (priv->funnel[i])
1711 gst_bin_remove (bin, priv->funnel[i]);
1713 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
1714 gst_object_unref (priv->recv_sink[i]);
1715 priv->recv_sink[i] = NULL;
1717 priv->udpsrc_v4[i] = NULL;
1718 priv->udpsrc_v6[i] = NULL;
1719 priv->udpsink[i] = NULL;
1720 priv->appsrc[i] = NULL;
1721 priv->appsink[i] = NULL;
1722 priv->appqueue[i] = NULL;
1723 priv->tee[i] = NULL;
1724 priv->funnel[i] = NULL;
1726 gst_object_unref (priv->send_src[0]);
1727 priv->send_src[0] = NULL;
1729 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
1730 gst_object_unref (priv->send_src[1]);
1731 priv->send_src[1] = NULL;
1733 g_object_unref (priv->session);
1734 priv->session = NULL;
1736 gst_caps_unref (priv->caps);
1739 priv->is_joined = FALSE;
1740 g_mutex_unlock (&priv->lock);
1751 * gst_rtsp_stream_get_rtpinfo:
1752 * @stream: a #GstRTSPStream
1753 * @rtptime: result RTP timestamp
1754 * @seq: result RTP seqnum
1756 * Retrieve the current rtptime and seq. This is used to
1757 * construct a RTPInfo reply header.
1759 * Returns: %TRUE when rtptime and seq could be determined.
1762 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
1763 guint * rtptime, guint * seq)
1765 GstRTSPStreamPrivate *priv;
1766 GObjectClass *payobjclass;
1768 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1769 g_return_val_if_fail (rtptime != NULL, FALSE);
1770 g_return_val_if_fail (seq != NULL, FALSE);
1772 priv = stream->priv;
1774 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
1776 if (!g_object_class_find_property (payobjclass, "seqnum") ||
1777 !g_object_class_find_property (payobjclass, "timestamp"))
1780 g_object_get (priv->payloader, "seqnum", seq, "timestamp", rtptime, NULL);
1786 * gst_rtsp_stream_get_caps:
1787 * @stream: a #GstRTSPStream
1789 * Retrieve the current caps of @stream.
1791 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
1795 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
1797 GstRTSPStreamPrivate *priv;
1800 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1802 priv = stream->priv;
1804 g_mutex_lock (&priv->lock);
1805 if ((result = priv->caps))
1806 gst_caps_ref (result);
1807 g_mutex_unlock (&priv->lock);
1813 * gst_rtsp_stream_recv_rtp:
1814 * @stream: a #GstRTSPStream
1815 * @buffer: (transfer full): a #GstBuffer
1817 * Handle an RTP buffer for the stream. This method is usually called when a
1818 * message has been received from a client using the TCP transport.
1820 * This function takes ownership of @buffer.
1822 * Returns: a GstFlowReturn.
1825 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
1827 GstRTSPStreamPrivate *priv;
1829 GstElement *element;
1831 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1832 priv = stream->priv;
1833 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1834 g_return_val_if_fail (priv->is_joined, FALSE);
1836 g_mutex_lock (&priv->lock);
1837 if (priv->appsrc[0])
1838 element = gst_object_ref (priv->appsrc[0]);
1841 g_mutex_unlock (&priv->lock);
1844 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1845 gst_object_unref (element);
1853 * gst_rtsp_stream_recv_rtcp:
1854 * @stream: a #GstRTSPStream
1855 * @buffer: (transfer full): a #GstBuffer
1857 * Handle an RTCP buffer for the stream. This method is usually called when a
1858 * message has been received from a client using the TCP transport.
1860 * This function takes ownership of @buffer.
1862 * Returns: a GstFlowReturn.
1865 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
1867 GstRTSPStreamPrivate *priv;
1869 GstElement *element;
1871 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1872 priv = stream->priv;
1873 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1874 g_return_val_if_fail (priv->is_joined, FALSE);
1876 g_mutex_lock (&priv->lock);
1877 if (priv->appsrc[1])
1878 element = gst_object_ref (priv->appsrc[1]);
1881 g_mutex_unlock (&priv->lock);
1884 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1885 gst_object_unref (element);
1892 /* must be called with lock */
1894 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
1897 GstRTSPStreamPrivate *priv = stream->priv;
1898 const GstRTSPTransport *tr;
1900 tr = gst_rtsp_stream_transport_get_transport (trans);
1902 switch (tr->lower_transport) {
1903 case GST_RTSP_LOWER_TRANS_UDP:
1904 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1910 dest = tr->destination;
1911 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1916 min = tr->client_port.min;
1917 max = tr->client_port.max;
1921 GST_INFO ("adding %s:%d-%d", dest, min, max);
1922 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
1923 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
1925 GST_INFO ("setting ttl-mc %d", ttl);
1926 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
1927 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
1929 priv->transports = g_list_prepend (priv->transports, trans);
1931 GST_INFO ("removing %s:%d-%d", dest, min, max);
1932 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
1933 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
1934 priv->transports = g_list_remove (priv->transports, trans);
1938 case GST_RTSP_LOWER_TRANS_TCP:
1940 GST_INFO ("adding TCP %s", tr->destination);
1941 priv->transports = g_list_prepend (priv->transports, trans);
1943 GST_INFO ("removing TCP %s", tr->destination);
1944 priv->transports = g_list_remove (priv->transports, trans);
1948 goto unknown_transport;
1955 GST_INFO ("Unknown transport %d", tr->lower_transport);
1962 * gst_rtsp_stream_add_transport:
1963 * @stream: a #GstRTSPStream
1964 * @trans: a #GstRTSPStreamTransport
1966 * Add the transport in @trans to @stream. The media of @stream will
1967 * then also be send to the values configured in @trans.
1969 * @stream must be joined to a bin.
1971 * @trans must contain a valid #GstRTSPTransport.
1973 * Returns: %TRUE if @trans was added
1976 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
1977 GstRTSPStreamTransport * trans)
1979 GstRTSPStreamPrivate *priv;
1982 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1983 priv = stream->priv;
1984 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1985 g_return_val_if_fail (priv->is_joined, FALSE);
1987 g_mutex_lock (&priv->lock);
1988 res = update_transport (stream, trans, TRUE);
1989 g_mutex_unlock (&priv->lock);
1995 * gst_rtsp_stream_remove_transport:
1996 * @stream: a #GstRTSPStream
1997 * @trans: a #GstRTSPStreamTransport
1999 * Remove the transport in @trans from @stream. The media of @stream will
2000 * not be sent to the values configured in @trans.
2002 * @stream must be joined to a bin.
2004 * @trans must contain a valid #GstRTSPTransport.
2006 * Returns: %TRUE if @trans was removed
2009 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2010 GstRTSPStreamTransport * trans)
2012 GstRTSPStreamPrivate *priv;
2015 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2016 priv = stream->priv;
2017 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2018 g_return_val_if_fail (priv->is_joined, FALSE);
2020 g_mutex_lock (&priv->lock);
2021 res = update_transport (stream, trans, FALSE);
2022 g_mutex_unlock (&priv->lock);
2028 * gst_rtsp_stream_get_rtp_socket:
2029 * @stream: a #GstRTSPStream
2030 * @family: the socket family
2032 * Get the RTP socket from @stream for a @family.
2034 * @stream must be joined to a bin.
2036 * Returns: the RTP socket or %NULL if no socket could be allocated for @family.
2040 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2042 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2046 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2047 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2048 family == G_SOCKET_FAMILY_IPV6, NULL);
2049 g_return_val_if_fail (priv->udpsink[0], NULL);
2051 if (family == G_SOCKET_FAMILY_IPV6)
2056 g_object_get (priv->udpsink[0], name, &socket, NULL);
2062 * gst_rtsp_stream_get_rtcp_socket:
2063 * @stream: a #GstRTSPStream
2064 * @family: the socket family
2066 * Get the RTCP socket from @stream for a @family.
2068 * @stream must be joined to a bin.
2070 * Returns: the RTCP socket or %NULL if no socket could be allocated for
2071 * @family. Unref after usage
2074 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
2076 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2080 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2081 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2082 family == G_SOCKET_FAMILY_IPV6, NULL);
2083 g_return_val_if_fail (priv->udpsink[1], NULL);
2085 if (family == G_SOCKET_FAMILY_IPV6)
2090 g_object_get (priv->udpsink[1], name, &socket, NULL);
2096 * gst_rtsp_stream_transport_filter:
2097 * @stream: a #GstRTSPStream
2098 * @func: (scope call) (allow-none): a callback
2099 * @user_data: user data passed to @func
2101 * Call @func for each transport managed by @stream. The result value of @func
2102 * determines what happens to the transport. @func will be called with @stream
2103 * locked so no further actions on @stream can be performed from @func.
2105 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
2108 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
2110 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
2111 * will also be added with an additional ref to the result #GList of this
2114 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
2116 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
2117 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2118 * element in the #GList should be unreffed before the list is freed.
2121 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
2122 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
2124 GstRTSPStreamPrivate *priv;
2125 GList *result, *walk, *next;
2127 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2129 priv = stream->priv;
2133 g_mutex_lock (&priv->lock);
2134 for (walk = priv->transports; walk; walk = next) {
2135 GstRTSPStreamTransport *trans = walk->data;
2136 GstRTSPFilterResult res;
2138 next = g_list_next (walk);
2141 res = func (stream, trans, user_data);
2143 res = GST_RTSP_FILTER_REF;
2146 case GST_RTSP_FILTER_REMOVE:
2147 update_transport (stream, trans, FALSE);
2149 case GST_RTSP_FILTER_REF:
2150 result = g_list_prepend (result, g_object_ref (trans));
2152 case GST_RTSP_FILTER_KEEP:
2157 g_mutex_unlock (&priv->lock);
2162 static GstPadProbeReturn
2163 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2165 GstRTSPStreamPrivate *priv;
2166 GstRTSPStream *stream;
2169 priv = stream->priv;
2171 GST_DEBUG_OBJECT (pad, "now blocking");
2173 g_mutex_lock (&priv->lock);
2174 priv->blocking = TRUE;
2175 g_mutex_unlock (&priv->lock);
2177 gst_element_post_message (priv->payloader,
2178 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
2179 gst_structure_new_empty ("GstRTSPStreamBlocking")));
2181 return GST_PAD_PROBE_OK;
2185 * gst_rtsp_stream_set_blocked:
2186 * @stream: a #GstRTSPStream
2187 * @blocked: boolean indicating we should block or unblock
2189 * Blocks or unblocks the dataflow on @stream.
2191 * Returns: %TRUE on success
2194 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
2196 GstRTSPStreamPrivate *priv;
2198 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2200 priv = stream->priv;
2202 g_mutex_lock (&priv->lock);
2204 priv->blocking = FALSE;
2205 if (priv->blocked_id == 0) {
2206 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
2207 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
2208 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
2209 g_object_ref (stream), g_object_unref);
2212 if (priv->blocked_id != 0) {
2213 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
2214 priv->blocked_id = 0;
2215 priv->blocking = FALSE;
2218 g_mutex_unlock (&priv->lock);
2224 * gst_rtsp_stream_is_blocking:
2225 * @stream: a #GstRTSPStream
2227 * Check if @stream is blocking on a #GstBuffer.
2229 * Returns: %TRUE if @stream is blocking
2232 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
2234 GstRTSPStreamPrivate *priv;
2237 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2239 priv = stream->priv;
2241 g_mutex_lock (&priv->lock);
2242 result = priv->blocking;
2243 g_mutex_unlock (&priv->lock);