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);
425 if (sscanf (control, "stream=%u", &streamid) > 0)
426 res = (streamid == priv->idx);
430 g_mutex_unlock (&priv->lock);
436 * gst_rtsp_stream_set_mtu:
437 * @stream: a #GstRTSPStream
440 * Configure the mtu in the payloader of @stream to @mtu.
443 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
445 GstRTSPStreamPrivate *priv;
447 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
451 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
453 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
457 * gst_rtsp_stream_get_mtu:
458 * @stream: a #GstRTSPStream
460 * Get the configured MTU in the payloader of @stream.
462 * Returns: the MTU of the payloader.
465 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
467 GstRTSPStreamPrivate *priv;
470 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
474 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
479 /* Update the dscp qos property on the udp sinks */
481 update_dscp_qos (GstRTSPStream * stream)
483 GstRTSPStreamPrivate *priv;
485 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
489 if (priv->udpsink[0]) {
490 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
494 if (priv->udpsink[1]) {
495 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
501 * gst_rtsp_stream_set_dscp_qos:
502 * @stream: a #GstRTSPStream
503 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
505 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
508 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
510 GstRTSPStreamPrivate *priv;
512 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
516 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
518 if (dscp_qos < -1 || dscp_qos > 63) {
519 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
523 priv->dscp_qos = dscp_qos;
525 update_dscp_qos (stream);
529 * gst_rtsp_stream_get_dscp_qos:
530 * @stream: a #GstRTSPStream
532 * Get the configured DSCP QoS in of the outgoing sockets.
534 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
537 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
539 GstRTSPStreamPrivate *priv;
541 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
545 return priv->dscp_qos;
549 * gst_rtsp_stream_is_transport_supported:
550 * @stream: a #GstRTSPStream
551 * @transport: a #GstRTSPTransport
553 * Check if @transport can be handled by stream
555 * Returns: %TRUE if @transport can be handled by @stream.
558 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
559 GstRTSPTransport * transport)
561 GstRTSPStreamPrivate *priv;
563 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
567 g_mutex_lock (&priv->lock);
568 if (transport->trans != GST_RTSP_TRANS_RTP)
569 goto unsupported_transmode;
571 if (transport->profile != GST_RTSP_PROFILE_AVP)
572 goto unsupported_profile;
574 if (!(transport->lower_transport & priv->protocols))
575 goto unsupported_ltrans;
577 g_mutex_unlock (&priv->lock);
582 unsupported_transmode:
584 GST_DEBUG ("unsupported transport mode %d", transport->trans);
589 GST_DEBUG ("unsupported profile %d", transport->profile);
594 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
600 * gst_rtsp_stream_set_protocols:
601 * @stream: a #GstRTSPStream
602 * @protocols: the new flags
604 * Configure the allowed lower transport for @stream.
607 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
608 GstRTSPLowerTrans protocols)
610 GstRTSPStreamPrivate *priv;
612 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
616 g_mutex_lock (&priv->lock);
617 priv->protocols = protocols;
618 g_mutex_unlock (&priv->lock);
622 * gst_rtsp_stream_get_protocols:
623 * @stream: a #GstRTSPStream
625 * Get the allowed protocols of @stream.
627 * Returns: a #GstRTSPLowerTrans
630 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
632 GstRTSPStreamPrivate *priv;
633 GstRTSPLowerTrans res;
635 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
636 GST_RTSP_LOWER_TRANS_UNKNOWN);
640 g_mutex_lock (&priv->lock);
641 res = priv->protocols;
642 g_mutex_unlock (&priv->lock);
648 * gst_rtsp_stream_set_address_pool:
649 * @stream: a #GstRTSPStream
650 * @pool: a #GstRTSPAddressPool
652 * configure @pool to be used as the address pool of @stream.
655 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
656 GstRTSPAddressPool * pool)
658 GstRTSPStreamPrivate *priv;
659 GstRTSPAddressPool *old;
661 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
665 GST_LOG_OBJECT (stream, "set address pool %p", pool);
667 g_mutex_lock (&priv->lock);
668 if ((old = priv->pool) != pool)
669 priv->pool = pool ? g_object_ref (pool) : NULL;
672 g_mutex_unlock (&priv->lock);
675 g_object_unref (old);
679 * gst_rtsp_stream_get_address_pool:
680 * @stream: a #GstRTSPStream
682 * Get the #GstRTSPAddressPool used as the address pool of @stream.
684 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
688 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
690 GstRTSPStreamPrivate *priv;
691 GstRTSPAddressPool *result;
693 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
697 g_mutex_lock (&priv->lock);
698 if ((result = priv->pool))
699 g_object_ref (result);
700 g_mutex_unlock (&priv->lock);
706 * gst_rtsp_stream_get_multicast_address:
707 * @stream: a #GstRTSPStream
708 * @family: the #GSocketFamily
710 * Get the multicast address of @stream for @family.
712 * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
713 * allocated. gst_rtsp_address_free() after usage.
716 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
717 GSocketFamily family)
719 GstRTSPStreamPrivate *priv;
720 GstRTSPAddress *result;
721 GstRTSPAddress **addrp;
722 GstRTSPAddressFlags flags;
724 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
728 if (family == G_SOCKET_FAMILY_IPV6) {
729 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
730 addrp = &priv->addr_v4;
732 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
733 addrp = &priv->addr_v6;
736 g_mutex_lock (&priv->lock);
737 if (*addrp == NULL) {
738 if (priv->pool == NULL)
741 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
743 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
747 result = gst_rtsp_address_copy (*addrp);
748 g_mutex_unlock (&priv->lock);
755 GST_ERROR_OBJECT (stream, "no address pool specified");
756 g_mutex_unlock (&priv->lock);
761 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
762 g_mutex_unlock (&priv->lock);
768 * gst_rtsp_stream_reserve_address:
769 * @stream: a #GstRTSPStream
770 * @address: an address
775 * Reserve @address and @port as the address and port of @stream.
777 * Returns: the #GstRTSPAddress of @stream or %NULL when the address could be
778 * reserved. gst_rtsp_address_free() after usage.
781 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
782 const gchar * address, guint port, guint n_ports, guint ttl)
784 GstRTSPStreamPrivate *priv;
785 GstRTSPAddress *result;
787 GSocketFamily family;
788 GstRTSPAddress **addrp;
790 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
791 g_return_val_if_fail (address != NULL, NULL);
792 g_return_val_if_fail (port > 0, NULL);
793 g_return_val_if_fail (n_ports > 0, NULL);
794 g_return_val_if_fail (ttl > 0, NULL);
798 addr = g_inet_address_new_from_string (address);
800 GST_ERROR ("failed to get inet addr from %s", address);
801 family = G_SOCKET_FAMILY_IPV4;
803 family = g_inet_address_get_family (addr);
804 g_object_unref (addr);
807 if (family == G_SOCKET_FAMILY_IPV6)
808 addrp = &priv->addr_v4;
810 addrp = &priv->addr_v6;
812 g_mutex_lock (&priv->lock);
813 if (*addrp == NULL) {
814 GstRTSPAddressPoolResult res;
816 if (priv->pool == NULL)
819 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
820 port, n_ports, ttl, addrp);
821 if (res != GST_RTSP_ADDRESS_POOL_OK)
824 if (strcmp ((*addrp)->address, address) ||
825 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
826 (*addrp)->ttl != ttl)
827 goto different_address;
829 result = gst_rtsp_address_copy (*addrp);
830 g_mutex_unlock (&priv->lock);
837 GST_ERROR_OBJECT (stream, "no address pool specified");
838 g_mutex_unlock (&priv->lock);
843 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
845 g_mutex_unlock (&priv->lock);
850 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
851 " reserved", address);
852 g_mutex_unlock (&priv->lock);
858 alloc_ports_one_family (GstRTSPAddressPool * pool, gint buffer_size,
859 GSocketFamily family, GstElement * udpsrc_out[2],
860 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
861 GstRTSPAddress ** server_addr_out)
863 GstStateChangeReturn ret;
864 GstElement *udpsrc0, *udpsrc1;
865 GstElement *udpsink0, *udpsink1;
866 GSocket *rtp_socket = NULL;
867 GSocket *rtcp_socket;
868 gint tmp_rtp, tmp_rtcp;
870 gint rtpport, rtcpport;
871 GList *rejected_addresses = NULL;
872 GstRTSPAddress *addr = NULL;
873 GInetAddress *inetaddr = NULL;
874 GSocketAddress *rtp_sockaddr = NULL;
875 GSocketAddress *rtcp_sockaddr = NULL;
876 const gchar *multisink_socket;
878 if (family == G_SOCKET_FAMILY_IPV6)
879 multisink_socket = "socket-v6";
881 multisink_socket = "socket";
889 /* Start with random port */
892 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
893 G_SOCKET_PROTOCOL_UDP, NULL);
895 goto no_udp_protocol;
897 if (*server_addr_out)
898 gst_rtsp_address_free (*server_addr_out);
900 /* try to allocate 2 UDP ports, the RTP port should be an even
901 * number and the RTCP port should be the next (uneven) port */
904 if (rtp_socket == NULL) {
905 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
906 G_SOCKET_PROTOCOL_UDP, NULL);
908 goto no_udp_protocol;
911 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
912 GstRTSPAddressFlags flags;
915 rejected_addresses = g_list_prepend (rejected_addresses, addr);
917 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
918 if (family == G_SOCKET_FAMILY_IPV6)
919 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
921 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
923 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
928 tmp_rtp = addr->port;
930 g_clear_object (&inetaddr);
931 inetaddr = g_inet_address_new_from_string (addr->address);
939 if (inetaddr == NULL)
940 inetaddr = g_inet_address_new_any (family);
943 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
944 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
945 g_object_unref (rtp_sockaddr);
948 g_object_unref (rtp_sockaddr);
950 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
951 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
952 g_clear_object (&rtp_sockaddr);
957 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
958 g_object_unref (rtp_sockaddr);
960 /* check if port is even */
961 if ((tmp_rtp & 1) != 0) {
962 /* port not even, close and allocate another */
964 g_clear_object (&rtp_socket);
969 tmp_rtcp = tmp_rtp + 1;
971 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
972 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
973 g_object_unref (rtcp_sockaddr);
974 g_clear_object (&rtp_socket);
977 g_object_unref (rtcp_sockaddr);
979 g_clear_object (&inetaddr);
981 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
982 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
984 if (udpsrc0 == NULL || udpsrc1 == NULL)
985 goto no_udp_protocol;
987 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
988 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
990 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
991 if (ret == GST_STATE_CHANGE_FAILURE)
993 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
994 if (ret == GST_STATE_CHANGE_FAILURE)
997 /* all fine, do port check */
998 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
999 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
1001 /* this should not happen... */
1002 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1006 udpsink0 = udpsink_out[0];
1008 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1011 goto no_udp_protocol;
1013 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1014 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
1017 udpsink1 = udpsink_out[1];
1019 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1022 goto no_udp_protocol;
1024 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1025 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1026 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
1028 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1029 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
1030 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1031 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1032 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1033 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1034 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1035 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1037 /* we keep these elements, we will further configure them when the
1038 * client told us to really use the UDP ports. */
1039 udpsrc_out[0] = udpsrc0;
1040 udpsrc_out[1] = udpsrc1;
1041 udpsink_out[0] = udpsink0;
1042 udpsink_out[1] = udpsink1;
1043 server_port_out->min = rtpport;
1044 server_port_out->max = rtcpport;
1046 *server_addr_out = addr;
1047 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1049 g_object_unref (rtp_socket);
1050 g_object_unref (rtcp_socket);
1078 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1079 gst_object_unref (udpsrc0);
1082 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1083 gst_object_unref (udpsrc1);
1086 gst_element_set_state (udpsink0, GST_STATE_NULL);
1087 gst_object_unref (udpsink0);
1090 g_object_unref (inetaddr);
1091 g_list_free_full (rejected_addresses,
1092 (GDestroyNotify) gst_rtsp_address_free);
1094 gst_rtsp_address_free (addr);
1096 g_object_unref (rtp_socket);
1098 g_object_unref (rtcp_socket);
1103 /* must be called with lock */
1105 alloc_ports (GstRTSPStream * stream)
1107 GstRTSPStreamPrivate *priv = stream->priv;
1109 priv->have_ipv4 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1110 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1111 &priv->server_port_v4, &priv->server_addr_v4);
1113 priv->have_ipv6 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1114 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1115 &priv->server_port_v6, &priv->server_addr_v6);
1117 return priv->have_ipv4 || priv->have_ipv6;
1121 * gst_rtsp_stream_get_server_port:
1122 * @stream: a #GstRTSPStream
1123 * @server_port: (out): result server port
1124 * @family: the port family to get
1126 * Fill @server_port with the port pair used by the server. This function can
1127 * only be called when @stream has been joined.
1130 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1131 GstRTSPRange * server_port, GSocketFamily family)
1133 GstRTSPStreamPrivate *priv;
1135 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1136 priv = stream->priv;
1137 g_return_if_fail (priv->is_joined);
1139 g_mutex_lock (&priv->lock);
1140 if (family == G_SOCKET_FAMILY_IPV4) {
1142 *server_port = priv->server_port_v4;
1145 *server_port = priv->server_port_v6;
1147 g_mutex_unlock (&priv->lock);
1151 * gst_rtsp_stream_get_rtpsession:
1152 * @stream: a #GstRTSPStream
1154 * Get the RTP session of this stream.
1156 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1159 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1161 GstRTSPStreamPrivate *priv;
1164 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1166 priv = stream->priv;
1168 g_mutex_lock (&priv->lock);
1169 if ((session = priv->session))
1170 g_object_ref (session);
1171 g_mutex_unlock (&priv->lock);
1177 * gst_rtsp_stream_get_ssrc:
1178 * @stream: a #GstRTSPStream
1179 * @ssrc: (out): result ssrc
1181 * Get the SSRC used by the RTP session of this stream. This function can only
1182 * be called when @stream has been joined.
1185 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1187 GstRTSPStreamPrivate *priv;
1189 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1190 priv = stream->priv;
1191 g_return_if_fail (priv->is_joined);
1193 g_mutex_lock (&priv->lock);
1194 if (ssrc && priv->session)
1195 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1196 g_mutex_unlock (&priv->lock);
1199 /* executed from streaming thread */
1201 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1203 GstRTSPStreamPrivate *priv = stream->priv;
1204 GstCaps *newcaps, *oldcaps;
1206 newcaps = gst_pad_get_current_caps (pad);
1208 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1211 g_mutex_lock (&priv->lock);
1212 oldcaps = priv->caps;
1213 priv->caps = newcaps;
1214 g_mutex_unlock (&priv->lock);
1217 gst_caps_unref (oldcaps);
1221 dump_structure (const GstStructure * s)
1225 sstr = gst_structure_to_string (s);
1226 GST_INFO ("structure: %s", sstr);
1230 static GstRTSPStreamTransport *
1231 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1233 GstRTSPStreamPrivate *priv = stream->priv;
1235 GstRTSPStreamTransport *result = NULL;
1240 if (rtcp_from == NULL)
1243 tmp = g_strrstr (rtcp_from, ":");
1247 port = atoi (tmp + 1);
1248 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1250 g_mutex_lock (&priv->lock);
1251 GST_INFO ("finding %s:%d in %d transports", dest, port,
1252 g_list_length (priv->transports));
1254 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1255 GstRTSPStreamTransport *trans = walk->data;
1256 const GstRTSPTransport *tr;
1259 tr = gst_rtsp_stream_transport_get_transport (trans);
1261 min = tr->client_port.min;
1262 max = tr->client_port.max;
1264 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1270 g_object_ref (result);
1271 g_mutex_unlock (&priv->lock);
1278 static GstRTSPStreamTransport *
1279 check_transport (GObject * source, GstRTSPStream * stream)
1281 GstStructure *stats;
1282 GstRTSPStreamTransport *trans;
1284 /* see if we have a stream to match with the origin of the RTCP packet */
1285 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1286 if (trans == NULL) {
1287 g_object_get (source, "stats", &stats, NULL);
1289 const gchar *rtcp_from;
1291 dump_structure (stats);
1293 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1294 if ((trans = find_transport (stream, rtcp_from))) {
1295 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1297 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1300 gst_structure_free (stats);
1308 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1310 GstRTSPStreamTransport *trans;
1312 GST_INFO ("%p: new source %p", stream, source);
1314 trans = check_transport (source, stream);
1317 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1321 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1323 GST_INFO ("%p: new SDES %p", stream, source);
1327 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1329 GstRTSPStreamTransport *trans;
1331 trans = check_transport (source, stream);
1334 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1335 gst_rtsp_stream_transport_keep_alive (trans);
1339 GstStructure *stats;
1340 g_object_get (source, "stats", &stats, NULL);
1342 dump_structure (stats);
1343 gst_structure_free (stats);
1350 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1352 GST_INFO ("%p: source %p bye", stream, source);
1356 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1358 GstRTSPStreamTransport *trans;
1360 GST_INFO ("%p: source %p bye timeout", stream, source);
1362 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1363 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1364 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1369 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1371 GstRTSPStreamTransport *trans;
1373 GST_INFO ("%p: source %p timeout", stream, source);
1375 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1376 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1377 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1381 static GstFlowReturn
1382 handle_new_sample (GstAppSink * sink, gpointer user_data)
1384 GstRTSPStreamPrivate *priv;
1388 GstRTSPStream *stream;
1390 sample = gst_app_sink_pull_sample (sink);
1394 stream = (GstRTSPStream *) user_data;
1395 priv = stream->priv;
1396 buffer = gst_sample_get_buffer (sample);
1398 g_mutex_lock (&priv->lock);
1399 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1400 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1402 if (GST_ELEMENT_CAST (sink) == priv->appsink[0]) {
1403 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1405 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1408 g_mutex_unlock (&priv->lock);
1410 gst_sample_unref (sample);
1415 static GstAppSinkCallbacks sink_cb = {
1416 NULL, /* not interested in EOS */
1417 NULL, /* not interested in preroll samples */
1422 * gst_rtsp_stream_join_bin:
1423 * @stream: a #GstRTSPStream
1424 * @bin: a #GstBin to join
1425 * @rtpbin: a rtpbin element in @bin
1426 * @state: the target state of the new elements
1428 * Join the #GstBin @bin that contains the element @rtpbin.
1430 * @stream will link to @rtpbin, which must be inside @bin. The elements
1431 * added to @bin will be set to the state given in @state.
1433 * Returns: %TRUE on success.
1436 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1437 GstElement * rtpbin, GstState state)
1439 GstRTSPStreamPrivate *priv;
1443 GstPad *pad, *sinkpad, *selpad;
1444 GstPadLinkReturn ret;
1446 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1447 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1448 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1450 priv = stream->priv;
1452 g_mutex_lock (&priv->lock);
1453 if (priv->is_joined)
1456 /* create a session with the same index as the stream */
1459 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1461 if (!alloc_ports (stream))
1464 /* update the dscp qos field in the sinks */
1465 update_dscp_qos (stream);
1467 /* get a pad for sending RTP */
1468 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1469 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
1471 /* link the RTP pad to the session manager, it should not really fail unless
1472 * this is not really an RTP pad */
1473 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
1474 if (ret != GST_PAD_LINK_OK)
1477 /* get pads from the RTP session element for sending and receiving
1479 name = g_strdup_printf ("send_rtp_src_%u", idx);
1480 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
1482 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1483 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
1485 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1486 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
1488 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1489 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
1492 /* get the session */
1493 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
1495 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1497 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1499 g_signal_connect (priv->session, "on-ssrc-active",
1500 (GCallback) on_ssrc_active, stream);
1501 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1503 g_signal_connect (priv->session, "on-bye-timeout",
1504 (GCallback) on_bye_timeout, stream);
1505 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1508 for (i = 0; i < 2; i++) {
1509 GstPad *teepad, *queuepad;
1510 /* For the sender we create this bit of pipeline for both
1511 * RTP and RTCP. Sync and preroll are enabled on udpsink so
1512 * we need to add a queue before appsink to make the pipeline
1513 * not block. For the TCP case, we want to pump data to the
1514 * client as fast as possible anyway.
1516 * .--------. .-----. .---------.
1517 * | rtpbin | | tee | | udpsink |
1518 * | send->sink src->sink |
1519 * '--------' | | '---------'
1520 * | | .---------. .---------.
1521 * | | | queue | | appsink |
1522 * | src->sink src->sink |
1523 * '-----' '---------' '---------'
1525 * When only UDP is allowed, we skip the tee, queue and appsink and link the
1526 * udpsink directly to the session.
1529 gst_bin_add (bin, priv->udpsink[i]);
1530 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1532 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1533 /* make tee for RTP/RTCP */
1534 priv->tee[i] = gst_element_factory_make ("tee", NULL);
1535 gst_bin_add (bin, priv->tee[i]);
1537 /* and link to rtpbin send pad */
1538 pad = gst_element_get_static_pad (priv->tee[i], "sink");
1539 gst_pad_link (priv->send_src[i], pad);
1540 gst_object_unref (pad);
1542 /* link tee to udpsink */
1543 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1544 gst_pad_link (teepad, sinkpad);
1545 gst_object_unref (teepad);
1548 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1549 gst_bin_add (bin, priv->appqueue[i]);
1550 /* and link to tee */
1551 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1552 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1553 gst_pad_link (teepad, pad);
1554 gst_object_unref (pad);
1555 gst_object_unref (teepad);
1558 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1559 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1560 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1561 gst_bin_add (bin, priv->appsink[i]);
1562 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1563 &sink_cb, stream, NULL);
1564 /* and link to queue */
1565 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1566 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1567 gst_pad_link (queuepad, pad);
1568 gst_object_unref (pad);
1569 gst_object_unref (queuepad);
1571 /* else only udpsink needed, link it to the session */
1572 gst_pad_link (priv->send_src[i], sinkpad);
1574 gst_object_unref (sinkpad);
1576 /* For the receiver we create this bit of pipeline for both
1577 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1578 * and it is all funneled into the rtpbin receive pad.
1580 * .--------. .--------. .--------.
1581 * | udpsrc | | funnel | | rtpbin |
1582 * | src->sink src->sink |
1583 * '--------' | | '--------'
1587 * '--------' '--------'
1589 /* make funnel for the RTP/RTCP receivers */
1590 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1591 gst_bin_add (bin, priv->funnel[i]);
1593 pad = gst_element_get_static_pad (priv->funnel[i], "src");
1594 gst_pad_link (pad, priv->recv_sink[i]);
1595 gst_object_unref (pad);
1597 if (priv->udpsrc_v4[i]) {
1598 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1600 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
1601 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
1603 gst_bin_add (bin, priv->udpsrc_v4[i]);
1605 /* and link to the funnel v4 */
1606 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1607 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
1608 gst_pad_link (pad, selpad);
1609 gst_object_unref (pad);
1610 gst_object_unref (selpad);
1613 if (priv->udpsrc_v6[i]) {
1614 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
1615 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
1616 gst_bin_add (bin, priv->udpsrc_v6[i]);
1618 /* and link to the funnel v6 */
1619 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1620 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
1621 gst_pad_link (pad, selpad);
1622 gst_object_unref (pad);
1623 gst_object_unref (selpad);
1626 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1627 /* make and add appsrc */
1628 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1629 gst_bin_add (bin, priv->appsrc[i]);
1630 /* and link to the funnel */
1631 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1632 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
1633 gst_pad_link (pad, selpad);
1634 gst_object_unref (pad);
1635 gst_object_unref (selpad);
1638 /* check if we need to set to a special state */
1639 if (state != GST_STATE_NULL) {
1640 if (priv->udpsink[i])
1641 gst_element_set_state (priv->udpsink[i], state);
1642 if (priv->appsink[i])
1643 gst_element_set_state (priv->appsink[i], state);
1644 if (priv->appqueue[i])
1645 gst_element_set_state (priv->appqueue[i], state);
1647 gst_element_set_state (priv->tee[i], state);
1648 if (priv->funnel[i])
1649 gst_element_set_state (priv->funnel[i], state);
1650 if (priv->appsrc[i])
1651 gst_element_set_state (priv->appsrc[i], state);
1655 /* be notified of caps changes */
1656 priv->caps_sig = g_signal_connect (priv->send_rtp_sink, "notify::caps",
1657 (GCallback) caps_notify, stream);
1659 priv->is_joined = TRUE;
1660 g_mutex_unlock (&priv->lock);
1667 g_mutex_unlock (&priv->lock);
1672 g_mutex_unlock (&priv->lock);
1673 GST_WARNING ("failed to allocate ports %u", idx);
1678 GST_WARNING ("failed to link stream %u", idx);
1679 gst_object_unref (priv->send_rtp_sink);
1680 priv->send_rtp_sink = NULL;
1681 g_mutex_unlock (&priv->lock);
1687 * gst_rtsp_stream_leave_bin:
1688 * @stream: a #GstRTSPStream
1690 * @rtpbin: a rtpbin #GstElement
1692 * Remove the elements of @stream from @bin.
1694 * Return: %TRUE on success.
1697 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1698 GstElement * rtpbin)
1700 GstRTSPStreamPrivate *priv;
1703 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1704 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1705 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1707 priv = stream->priv;
1709 g_mutex_lock (&priv->lock);
1710 if (!priv->is_joined)
1711 goto was_not_joined;
1713 /* all transports must be removed by now */
1714 g_return_val_if_fail (priv->transports == NULL, FALSE);
1716 GST_INFO ("stream %p leaving bin", stream);
1718 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
1719 g_signal_handler_disconnect (priv->send_rtp_sink, priv->caps_sig);
1720 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
1721 gst_object_unref (priv->send_rtp_sink);
1722 priv->send_rtp_sink = NULL;
1724 for (i = 0; i < 2; i++) {
1725 if (priv->udpsink[i])
1726 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
1727 if (priv->appsink[i])
1728 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
1729 if (priv->appqueue[i])
1730 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
1732 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
1733 if (priv->funnel[i])
1734 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
1735 if (priv->appsrc[i])
1736 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
1737 if (priv->udpsrc_v4[i]) {
1738 /* and set udpsrc to NULL now before removing */
1739 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
1740 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
1741 /* removing them should also nicely release the request
1742 * pads when they finalize */
1743 gst_bin_remove (bin, priv->udpsrc_v4[i]);
1745 if (priv->udpsrc_v6[i]) {
1746 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
1747 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
1748 gst_bin_remove (bin, priv->udpsrc_v6[i]);
1750 if (priv->udpsink[i])
1751 gst_bin_remove (bin, priv->udpsink[i]);
1752 if (priv->appsrc[i])
1753 gst_bin_remove (bin, priv->appsrc[i]);
1754 if (priv->appsink[i])
1755 gst_bin_remove (bin, priv->appsink[i]);
1756 if (priv->appqueue[i])
1757 gst_bin_remove (bin, priv->appqueue[i]);
1759 gst_bin_remove (bin, priv->tee[i]);
1760 if (priv->funnel[i])
1761 gst_bin_remove (bin, priv->funnel[i]);
1763 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
1764 gst_object_unref (priv->recv_sink[i]);
1765 priv->recv_sink[i] = NULL;
1767 priv->udpsrc_v4[i] = NULL;
1768 priv->udpsrc_v6[i] = NULL;
1769 priv->udpsink[i] = NULL;
1770 priv->appsrc[i] = NULL;
1771 priv->appsink[i] = NULL;
1772 priv->appqueue[i] = NULL;
1773 priv->tee[i] = NULL;
1774 priv->funnel[i] = NULL;
1776 gst_object_unref (priv->send_src[0]);
1777 priv->send_src[0] = NULL;
1779 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
1780 gst_object_unref (priv->send_src[1]);
1781 priv->send_src[1] = NULL;
1783 g_object_unref (priv->session);
1784 priv->session = NULL;
1786 gst_caps_unref (priv->caps);
1789 priv->is_joined = FALSE;
1790 g_mutex_unlock (&priv->lock);
1801 * gst_rtsp_stream_get_rtpinfo:
1802 * @stream: a #GstRTSPStream
1803 * @rtptime: (allow-none): result RTP timestamp
1804 * @seq: (allow-none): result RTP seqnum
1805 * @clock_rate: the clock rate
1806 * @running_time: (allow-none): result running-time
1808 * Retrieve the current rtptime, seq and running-time. This is used to
1809 * construct a RTPInfo reply header.
1811 * Returns: %TRUE when rtptime, seq and running-time could be determined.
1814 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
1815 guint * rtptime, guint * seq, guint * clock_rate,
1816 GstClockTime * running_time)
1818 GstRTSPStreamPrivate *priv;
1819 GObjectClass *payobjclass;
1821 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1823 priv = stream->priv;
1825 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
1827 g_mutex_lock (&priv->lock);
1828 if (seq && g_object_class_find_property (payobjclass, "seqnum"))
1829 g_object_get (priv->payloader, "seqnum", seq, NULL);
1831 if (rtptime && g_object_class_find_property (payobjclass, "timestamp"))
1832 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
1835 && g_object_class_find_property (payobjclass, "running-time"))
1836 g_object_get (priv->payloader, "running-time", running_time, NULL);
1838 if (clock_rate && priv->caps) {
1841 s = gst_caps_get_structure (priv->caps, 0);
1842 if (!gst_structure_get_int (s, "clock-rate", (gint *) clock_rate))
1844 *running_time = GST_CLOCK_TIME_NONE;
1846 g_mutex_unlock (&priv->lock);
1852 * gst_rtsp_stream_get_caps:
1853 * @stream: a #GstRTSPStream
1855 * Retrieve the current caps of @stream.
1857 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
1861 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
1863 GstRTSPStreamPrivate *priv;
1866 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1868 priv = stream->priv;
1870 g_mutex_lock (&priv->lock);
1871 if ((result = priv->caps))
1872 gst_caps_ref (result);
1873 g_mutex_unlock (&priv->lock);
1879 * gst_rtsp_stream_recv_rtp:
1880 * @stream: a #GstRTSPStream
1881 * @buffer: (transfer full): a #GstBuffer
1883 * Handle an RTP buffer for the stream. This method is usually called when a
1884 * message has been received from a client using the TCP transport.
1886 * This function takes ownership of @buffer.
1888 * Returns: a GstFlowReturn.
1891 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
1893 GstRTSPStreamPrivate *priv;
1895 GstElement *element;
1897 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1898 priv = stream->priv;
1899 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1900 g_return_val_if_fail (priv->is_joined, FALSE);
1902 g_mutex_lock (&priv->lock);
1903 if (priv->appsrc[0])
1904 element = gst_object_ref (priv->appsrc[0]);
1907 g_mutex_unlock (&priv->lock);
1910 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1911 gst_object_unref (element);
1919 * gst_rtsp_stream_recv_rtcp:
1920 * @stream: a #GstRTSPStream
1921 * @buffer: (transfer full): a #GstBuffer
1923 * Handle an RTCP buffer for the stream. This method is usually called when a
1924 * message has been received from a client using the TCP transport.
1926 * This function takes ownership of @buffer.
1928 * Returns: a GstFlowReturn.
1931 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
1933 GstRTSPStreamPrivate *priv;
1935 GstElement *element;
1937 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1938 priv = stream->priv;
1939 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1940 g_return_val_if_fail (priv->is_joined, FALSE);
1942 g_mutex_lock (&priv->lock);
1943 if (priv->appsrc[1])
1944 element = gst_object_ref (priv->appsrc[1]);
1947 g_mutex_unlock (&priv->lock);
1950 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1951 gst_object_unref (element);
1958 /* must be called with lock */
1960 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
1963 GstRTSPStreamPrivate *priv = stream->priv;
1964 const GstRTSPTransport *tr;
1966 tr = gst_rtsp_stream_transport_get_transport (trans);
1968 switch (tr->lower_transport) {
1969 case GST_RTSP_LOWER_TRANS_UDP:
1970 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1976 dest = tr->destination;
1977 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1982 min = tr->client_port.min;
1983 max = tr->client_port.max;
1987 GST_INFO ("adding %s:%d-%d", dest, min, max);
1988 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
1989 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
1991 GST_INFO ("setting ttl-mc %d", ttl);
1992 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
1993 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
1995 priv->transports = g_list_prepend (priv->transports, trans);
1997 GST_INFO ("removing %s:%d-%d", dest, min, max);
1998 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
1999 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
2000 priv->transports = g_list_remove (priv->transports, trans);
2004 case GST_RTSP_LOWER_TRANS_TCP:
2006 GST_INFO ("adding TCP %s", tr->destination);
2007 priv->transports = g_list_prepend (priv->transports, trans);
2009 GST_INFO ("removing TCP %s", tr->destination);
2010 priv->transports = g_list_remove (priv->transports, trans);
2014 goto unknown_transport;
2021 GST_INFO ("Unknown transport %d", tr->lower_transport);
2028 * gst_rtsp_stream_add_transport:
2029 * @stream: a #GstRTSPStream
2030 * @trans: a #GstRTSPStreamTransport
2032 * Add the transport in @trans to @stream. The media of @stream will
2033 * then also be send to the values configured in @trans.
2035 * @stream must be joined to a bin.
2037 * @trans must contain a valid #GstRTSPTransport.
2039 * Returns: %TRUE if @trans was added
2042 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
2043 GstRTSPStreamTransport * trans)
2045 GstRTSPStreamPrivate *priv;
2048 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2049 priv = stream->priv;
2050 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2051 g_return_val_if_fail (priv->is_joined, FALSE);
2053 g_mutex_lock (&priv->lock);
2054 res = update_transport (stream, trans, TRUE);
2055 g_mutex_unlock (&priv->lock);
2061 * gst_rtsp_stream_remove_transport:
2062 * @stream: a #GstRTSPStream
2063 * @trans: a #GstRTSPStreamTransport
2065 * Remove the transport in @trans from @stream. The media of @stream will
2066 * not be sent to the values configured in @trans.
2068 * @stream must be joined to a bin.
2070 * @trans must contain a valid #GstRTSPTransport.
2072 * Returns: %TRUE if @trans was removed
2075 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2076 GstRTSPStreamTransport * trans)
2078 GstRTSPStreamPrivate *priv;
2081 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2082 priv = stream->priv;
2083 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2084 g_return_val_if_fail (priv->is_joined, FALSE);
2086 g_mutex_lock (&priv->lock);
2087 res = update_transport (stream, trans, FALSE);
2088 g_mutex_unlock (&priv->lock);
2094 * gst_rtsp_stream_get_rtp_socket:
2095 * @stream: a #GstRTSPStream
2096 * @family: the socket family
2098 * Get the RTP socket from @stream for a @family.
2100 * @stream must be joined to a bin.
2102 * Returns: the RTP socket or %NULL if no socket could be allocated for @family.
2106 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2108 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2112 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2113 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2114 family == G_SOCKET_FAMILY_IPV6, NULL);
2115 g_return_val_if_fail (priv->udpsink[0], NULL);
2117 if (family == G_SOCKET_FAMILY_IPV6)
2122 g_object_get (priv->udpsink[0], name, &socket, NULL);
2128 * gst_rtsp_stream_get_rtcp_socket:
2129 * @stream: a #GstRTSPStream
2130 * @family: the socket family
2132 * Get the RTCP socket from @stream for a @family.
2134 * @stream must be joined to a bin.
2136 * Returns: the RTCP socket or %NULL if no socket could be allocated for
2137 * @family. Unref after usage
2140 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
2142 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2146 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2147 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2148 family == G_SOCKET_FAMILY_IPV6, NULL);
2149 g_return_val_if_fail (priv->udpsink[1], NULL);
2151 if (family == G_SOCKET_FAMILY_IPV6)
2156 g_object_get (priv->udpsink[1], name, &socket, NULL);
2162 * gst_rtsp_stream_transport_filter:
2163 * @stream: a #GstRTSPStream
2164 * @func: (scope call) (allow-none): a callback
2165 * @user_data: user data passed to @func
2167 * Call @func for each transport managed by @stream. The result value of @func
2168 * determines what happens to the transport. @func will be called with @stream
2169 * locked so no further actions on @stream can be performed from @func.
2171 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
2174 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
2176 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
2177 * will also be added with an additional ref to the result #GList of this
2180 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
2182 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
2183 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2184 * element in the #GList should be unreffed before the list is freed.
2187 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
2188 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
2190 GstRTSPStreamPrivate *priv;
2191 GList *result, *walk, *next;
2193 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2195 priv = stream->priv;
2199 g_mutex_lock (&priv->lock);
2200 for (walk = priv->transports; walk; walk = next) {
2201 GstRTSPStreamTransport *trans = walk->data;
2202 GstRTSPFilterResult res;
2204 next = g_list_next (walk);
2207 res = func (stream, trans, user_data);
2209 res = GST_RTSP_FILTER_REF;
2212 case GST_RTSP_FILTER_REMOVE:
2213 update_transport (stream, trans, FALSE);
2215 case GST_RTSP_FILTER_REF:
2216 result = g_list_prepend (result, g_object_ref (trans));
2218 case GST_RTSP_FILTER_KEEP:
2223 g_mutex_unlock (&priv->lock);
2228 static GstPadProbeReturn
2229 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2231 GstRTSPStreamPrivate *priv;
2232 GstRTSPStream *stream;
2235 priv = stream->priv;
2237 GST_DEBUG_OBJECT (pad, "now blocking");
2239 g_mutex_lock (&priv->lock);
2240 priv->blocking = TRUE;
2241 g_mutex_unlock (&priv->lock);
2243 gst_element_post_message (priv->payloader,
2244 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
2245 gst_structure_new_empty ("GstRTSPStreamBlocking")));
2247 return GST_PAD_PROBE_OK;
2251 * gst_rtsp_stream_set_blocked:
2252 * @stream: a #GstRTSPStream
2253 * @blocked: boolean indicating we should block or unblock
2255 * Blocks or unblocks the dataflow on @stream.
2257 * Returns: %TRUE on success
2260 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
2262 GstRTSPStreamPrivate *priv;
2264 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2266 priv = stream->priv;
2268 g_mutex_lock (&priv->lock);
2270 priv->blocking = FALSE;
2271 if (priv->blocked_id == 0) {
2272 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
2273 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
2274 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
2275 g_object_ref (stream), g_object_unref);
2278 if (priv->blocked_id != 0) {
2279 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
2280 priv->blocked_id = 0;
2281 priv->blocking = FALSE;
2284 g_mutex_unlock (&priv->lock);
2290 * gst_rtsp_stream_is_blocking:
2291 * @stream: a #GstRTSPStream
2293 * Check if @stream is blocking on a #GstBuffer.
2295 * Returns: %TRUE if @stream is blocking
2298 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
2300 GstRTSPStreamPrivate *priv;
2303 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2305 priv = stream->priv;
2307 g_mutex_lock (&priv->lock);
2308 result = priv->blocking;
2309 g_mutex_unlock (&priv->lock);