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_set_protocols:
550 * @stream: a #GstRTSPStream
551 * @protocols: the new flags
553 * Configure the allowed lower transport for @stream.
556 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
557 GstRTSPLowerTrans protocols)
559 GstRTSPStreamPrivate *priv;
561 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
565 g_mutex_lock (&priv->lock);
566 priv->protocols = protocols;
567 g_mutex_unlock (&priv->lock);
571 * gst_rtsp_stream_get_protocols:
572 * @stream: a #GstRTSPStream
574 * Get the allowed protocols of @stream.
576 * Returns: a #GstRTSPLowerTrans
579 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
581 GstRTSPStreamPrivate *priv;
582 GstRTSPLowerTrans res;
584 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
585 GST_RTSP_LOWER_TRANS_UNKNOWN);
589 g_mutex_lock (&priv->lock);
590 res = priv->protocols;
591 g_mutex_unlock (&priv->lock);
597 * gst_rtsp_stream_set_address_pool:
598 * @stream: a #GstRTSPStream
599 * @pool: a #GstRTSPAddressPool
601 * configure @pool to be used as the address pool of @stream.
604 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
605 GstRTSPAddressPool * pool)
607 GstRTSPStreamPrivate *priv;
608 GstRTSPAddressPool *old;
610 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
614 GST_LOG_OBJECT (stream, "set address pool %p", pool);
616 g_mutex_lock (&priv->lock);
617 if ((old = priv->pool) != pool)
618 priv->pool = pool ? g_object_ref (pool) : NULL;
621 g_mutex_unlock (&priv->lock);
624 g_object_unref (old);
628 * gst_rtsp_stream_get_address_pool:
629 * @stream: a #GstRTSPStream
631 * Get the #GstRTSPAddressPool used as the address pool of @stream.
633 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
637 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
639 GstRTSPStreamPrivate *priv;
640 GstRTSPAddressPool *result;
642 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
646 g_mutex_lock (&priv->lock);
647 if ((result = priv->pool))
648 g_object_ref (result);
649 g_mutex_unlock (&priv->lock);
655 * gst_rtsp_stream_get_multicast_address:
656 * @stream: a #GstRTSPStream
657 * @family: the #GSocketFamily
659 * Get the multicast address of @stream for @family.
661 * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
662 * allocated. gst_rtsp_address_free() after usage.
665 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
666 GSocketFamily family)
668 GstRTSPStreamPrivate *priv;
669 GstRTSPAddress *result;
670 GstRTSPAddress **addrp;
671 GstRTSPAddressFlags flags;
673 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
677 if (family == G_SOCKET_FAMILY_IPV6) {
678 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
679 addrp = &priv->addr_v4;
681 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
682 addrp = &priv->addr_v6;
685 g_mutex_lock (&priv->lock);
686 if (*addrp == NULL) {
687 if (priv->pool == NULL)
690 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
692 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
696 result = gst_rtsp_address_copy (*addrp);
697 g_mutex_unlock (&priv->lock);
704 GST_ERROR_OBJECT (stream, "no address pool specified");
705 g_mutex_unlock (&priv->lock);
710 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
711 g_mutex_unlock (&priv->lock);
717 * gst_rtsp_stream_reserve_address:
718 * @stream: a #GstRTSPStream
719 * @address: an address
724 * Reserve @address and @port as the address and port of @stream.
726 * Returns: the #GstRTSPAddress of @stream or %NULL when the address could be
727 * reserved. gst_rtsp_address_free() after usage.
730 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
731 const gchar * address, guint port, guint n_ports, guint ttl)
733 GstRTSPStreamPrivate *priv;
734 GstRTSPAddress *result;
736 GSocketFamily family;
737 GstRTSPAddress **addrp;
739 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
740 g_return_val_if_fail (address != NULL, NULL);
741 g_return_val_if_fail (port > 0, NULL);
742 g_return_val_if_fail (n_ports > 0, NULL);
743 g_return_val_if_fail (ttl > 0, NULL);
747 addr = g_inet_address_new_from_string (address);
749 GST_ERROR ("failed to get inet addr from %s", address);
750 family = G_SOCKET_FAMILY_IPV4;
752 family = g_inet_address_get_family (addr);
753 g_object_unref (addr);
756 if (family == G_SOCKET_FAMILY_IPV6)
757 addrp = &priv->addr_v4;
759 addrp = &priv->addr_v6;
761 g_mutex_lock (&priv->lock);
762 if (*addrp == NULL) {
763 GstRTSPAddressPoolResult res;
765 if (priv->pool == NULL)
768 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
769 port, n_ports, ttl, addrp);
770 if (res != GST_RTSP_ADDRESS_POOL_OK)
773 if (strcmp ((*addrp)->address, address) ||
774 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
775 (*addrp)->ttl != ttl)
776 goto different_address;
778 result = gst_rtsp_address_copy (*addrp);
779 g_mutex_unlock (&priv->lock);
786 GST_ERROR_OBJECT (stream, "no address pool specified");
787 g_mutex_unlock (&priv->lock);
792 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
794 g_mutex_unlock (&priv->lock);
799 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
800 " reserved", address);
801 g_mutex_unlock (&priv->lock);
807 alloc_ports_one_family (GstRTSPAddressPool * pool, gint buffer_size,
808 GSocketFamily family, GstElement * udpsrc_out[2],
809 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
810 GstRTSPAddress ** server_addr_out)
812 GstStateChangeReturn ret;
813 GstElement *udpsrc0, *udpsrc1;
814 GstElement *udpsink0, *udpsink1;
815 GSocket *rtp_socket = NULL;
816 GSocket *rtcp_socket;
817 gint tmp_rtp, tmp_rtcp;
819 gint rtpport, rtcpport;
820 GList *rejected_addresses = NULL;
821 GstRTSPAddress *addr = NULL;
822 GInetAddress *inetaddr = NULL;
823 GSocketAddress *rtp_sockaddr = NULL;
824 GSocketAddress *rtcp_sockaddr = NULL;
825 const gchar *multisink_socket;
827 if (family == G_SOCKET_FAMILY_IPV6)
828 multisink_socket = "socket-v6";
830 multisink_socket = "socket";
838 /* Start with random port */
841 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
842 G_SOCKET_PROTOCOL_UDP, NULL);
844 goto no_udp_protocol;
846 if (*server_addr_out)
847 gst_rtsp_address_free (*server_addr_out);
849 /* try to allocate 2 UDP ports, the RTP port should be an even
850 * number and the RTCP port should be the next (uneven) port */
853 if (rtp_socket == NULL) {
854 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
855 G_SOCKET_PROTOCOL_UDP, NULL);
857 goto no_udp_protocol;
860 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
861 GstRTSPAddressFlags flags;
864 rejected_addresses = g_list_prepend (rejected_addresses, addr);
866 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
867 if (family == G_SOCKET_FAMILY_IPV6)
868 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
870 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
872 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
877 tmp_rtp = addr->port;
879 g_clear_object (&inetaddr);
880 inetaddr = g_inet_address_new_from_string (addr->address);
888 if (inetaddr == NULL)
889 inetaddr = g_inet_address_new_any (family);
892 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
893 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
894 g_object_unref (rtp_sockaddr);
897 g_object_unref (rtp_sockaddr);
899 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
900 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
901 g_clear_object (&rtp_sockaddr);
906 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
907 g_object_unref (rtp_sockaddr);
909 /* check if port is even */
910 if ((tmp_rtp & 1) != 0) {
911 /* port not even, close and allocate another */
913 g_clear_object (&rtp_socket);
918 tmp_rtcp = tmp_rtp + 1;
920 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
921 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
922 g_object_unref (rtcp_sockaddr);
923 g_clear_object (&rtp_socket);
926 g_object_unref (rtcp_sockaddr);
928 g_clear_object (&inetaddr);
930 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
931 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
933 if (udpsrc0 == NULL || udpsrc1 == NULL)
934 goto no_udp_protocol;
936 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
937 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
939 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
940 if (ret == GST_STATE_CHANGE_FAILURE)
942 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
943 if (ret == GST_STATE_CHANGE_FAILURE)
946 /* all fine, do port check */
947 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
948 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
950 /* this should not happen... */
951 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
955 udpsink0 = udpsink_out[0];
957 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
960 goto no_udp_protocol;
962 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
963 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
966 udpsink1 = udpsink_out[1];
968 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
971 goto no_udp_protocol;
973 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
974 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
975 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
977 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
978 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
979 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
980 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
981 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
982 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
983 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
984 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
986 /* we keep these elements, we will further configure them when the
987 * client told us to really use the UDP ports. */
988 udpsrc_out[0] = udpsrc0;
989 udpsrc_out[1] = udpsrc1;
990 udpsink_out[0] = udpsink0;
991 udpsink_out[1] = udpsink1;
992 server_port_out->min = rtpport;
993 server_port_out->max = rtcpport;
995 *server_addr_out = addr;
996 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
998 g_object_unref (rtp_socket);
999 g_object_unref (rtcp_socket);
1027 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1028 gst_object_unref (udpsrc0);
1031 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1032 gst_object_unref (udpsrc1);
1035 gst_element_set_state (udpsink0, GST_STATE_NULL);
1036 gst_object_unref (udpsink0);
1039 g_object_unref (inetaddr);
1040 g_list_free_full (rejected_addresses,
1041 (GDestroyNotify) gst_rtsp_address_free);
1043 gst_rtsp_address_free (addr);
1045 g_object_unref (rtp_socket);
1047 g_object_unref (rtcp_socket);
1052 /* must be called with lock */
1054 alloc_ports (GstRTSPStream * stream)
1056 GstRTSPStreamPrivate *priv = stream->priv;
1058 priv->have_ipv4 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1059 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1060 &priv->server_port_v4, &priv->server_addr_v4);
1062 priv->have_ipv6 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1063 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1064 &priv->server_port_v6, &priv->server_addr_v6);
1066 return priv->have_ipv4 || priv->have_ipv6;
1070 * gst_rtsp_stream_get_server_port:
1071 * @stream: a #GstRTSPStream
1072 * @server_port: (out): result server port
1073 * @family: the port family to get
1075 * Fill @server_port with the port pair used by the server. This function can
1076 * only be called when @stream has been joined.
1079 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1080 GstRTSPRange * server_port, GSocketFamily family)
1082 GstRTSPStreamPrivate *priv;
1084 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1085 priv = stream->priv;
1086 g_return_if_fail (priv->is_joined);
1088 g_mutex_lock (&priv->lock);
1089 if (family == G_SOCKET_FAMILY_IPV4) {
1091 *server_port = priv->server_port_v4;
1094 *server_port = priv->server_port_v6;
1096 g_mutex_unlock (&priv->lock);
1100 * gst_rtsp_stream_get_rtpsession:
1101 * @stream: a #GstRTSPStream
1103 * Get the RTP session of this stream.
1105 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1108 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1110 GstRTSPStreamPrivate *priv;
1113 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1115 priv = stream->priv;
1117 g_mutex_lock (&priv->lock);
1118 if ((session = priv->session))
1119 g_object_ref (session);
1120 g_mutex_unlock (&priv->lock);
1126 * gst_rtsp_stream_get_ssrc:
1127 * @stream: a #GstRTSPStream
1128 * @ssrc: (out): result ssrc
1130 * Get the SSRC used by the RTP session of this stream. This function can only
1131 * be called when @stream has been joined.
1134 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1136 GstRTSPStreamPrivate *priv;
1138 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1139 priv = stream->priv;
1140 g_return_if_fail (priv->is_joined);
1142 g_mutex_lock (&priv->lock);
1143 if (ssrc && priv->session)
1144 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1145 g_mutex_unlock (&priv->lock);
1148 /* executed from streaming thread */
1150 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1152 GstRTSPStreamPrivate *priv = stream->priv;
1153 GstCaps *newcaps, *oldcaps;
1155 newcaps = gst_pad_get_current_caps (pad);
1157 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1160 g_mutex_lock (&priv->lock);
1161 oldcaps = priv->caps;
1162 priv->caps = newcaps;
1163 g_mutex_unlock (&priv->lock);
1166 gst_caps_unref (oldcaps);
1170 dump_structure (const GstStructure * s)
1174 sstr = gst_structure_to_string (s);
1175 GST_INFO ("structure: %s", sstr);
1179 static GstRTSPStreamTransport *
1180 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1182 GstRTSPStreamPrivate *priv = stream->priv;
1184 GstRTSPStreamTransport *result = NULL;
1189 if (rtcp_from == NULL)
1192 tmp = g_strrstr (rtcp_from, ":");
1196 port = atoi (tmp + 1);
1197 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1199 g_mutex_lock (&priv->lock);
1200 GST_INFO ("finding %s:%d in %d transports", dest, port,
1201 g_list_length (priv->transports));
1203 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1204 GstRTSPStreamTransport *trans = walk->data;
1205 const GstRTSPTransport *tr;
1208 tr = gst_rtsp_stream_transport_get_transport (trans);
1210 min = tr->client_port.min;
1211 max = tr->client_port.max;
1213 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1219 g_object_ref (result);
1220 g_mutex_unlock (&priv->lock);
1227 static GstRTSPStreamTransport *
1228 check_transport (GObject * source, GstRTSPStream * stream)
1230 GstStructure *stats;
1231 GstRTSPStreamTransport *trans;
1233 /* see if we have a stream to match with the origin of the RTCP packet */
1234 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1235 if (trans == NULL) {
1236 g_object_get (source, "stats", &stats, NULL);
1238 const gchar *rtcp_from;
1240 dump_structure (stats);
1242 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1243 if ((trans = find_transport (stream, rtcp_from))) {
1244 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1246 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1249 gst_structure_free (stats);
1257 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1259 GstRTSPStreamTransport *trans;
1261 GST_INFO ("%p: new source %p", stream, source);
1263 trans = check_transport (source, stream);
1266 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1270 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1272 GST_INFO ("%p: new SDES %p", stream, source);
1276 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1278 GstRTSPStreamTransport *trans;
1280 trans = check_transport (source, stream);
1283 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1284 gst_rtsp_stream_transport_keep_alive (trans);
1288 GstStructure *stats;
1289 g_object_get (source, "stats", &stats, NULL);
1291 dump_structure (stats);
1292 gst_structure_free (stats);
1299 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1301 GST_INFO ("%p: source %p bye", stream, source);
1305 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1307 GstRTSPStreamTransport *trans;
1309 GST_INFO ("%p: source %p bye timeout", stream, source);
1311 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1312 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1313 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1318 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1320 GstRTSPStreamTransport *trans;
1322 GST_INFO ("%p: source %p timeout", stream, source);
1324 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1325 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1326 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1330 static GstFlowReturn
1331 handle_new_sample (GstAppSink * sink, gpointer user_data)
1333 GstRTSPStreamPrivate *priv;
1337 GstRTSPStream *stream;
1339 sample = gst_app_sink_pull_sample (sink);
1343 stream = (GstRTSPStream *) user_data;
1344 priv = stream->priv;
1345 buffer = gst_sample_get_buffer (sample);
1347 g_mutex_lock (&priv->lock);
1348 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1349 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1351 if (GST_ELEMENT_CAST (sink) == priv->appsink[0]) {
1352 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1354 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1357 g_mutex_unlock (&priv->lock);
1359 gst_sample_unref (sample);
1364 static GstAppSinkCallbacks sink_cb = {
1365 NULL, /* not interested in EOS */
1366 NULL, /* not interested in preroll samples */
1371 * gst_rtsp_stream_join_bin:
1372 * @stream: a #GstRTSPStream
1373 * @bin: a #GstBin to join
1374 * @rtpbin: a rtpbin element in @bin
1375 * @state: the target state of the new elements
1377 * Join the #GstBin @bin that contains the element @rtpbin.
1379 * @stream will link to @rtpbin, which must be inside @bin. The elements
1380 * added to @bin will be set to the state given in @state.
1382 * Returns: %TRUE on success.
1385 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1386 GstElement * rtpbin, GstState state)
1388 GstRTSPStreamPrivate *priv;
1392 GstPad *pad, *sinkpad, *selpad;
1393 GstPadLinkReturn ret;
1395 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1396 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1397 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1399 priv = stream->priv;
1401 g_mutex_lock (&priv->lock);
1402 if (priv->is_joined)
1405 /* create a session with the same index as the stream */
1408 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1410 if (!alloc_ports (stream))
1413 /* update the dscp qos field in the sinks */
1414 update_dscp_qos (stream);
1416 /* get a pad for sending RTP */
1417 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1418 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
1420 /* link the RTP pad to the session manager, it should not really fail unless
1421 * this is not really an RTP pad */
1422 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
1423 if (ret != GST_PAD_LINK_OK)
1426 /* get pads from the RTP session element for sending and receiving
1428 name = g_strdup_printf ("send_rtp_src_%u", idx);
1429 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
1431 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1432 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
1434 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1435 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
1437 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1438 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
1441 /* get the session */
1442 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
1444 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1446 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1448 g_signal_connect (priv->session, "on-ssrc-active",
1449 (GCallback) on_ssrc_active, stream);
1450 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1452 g_signal_connect (priv->session, "on-bye-timeout",
1453 (GCallback) on_bye_timeout, stream);
1454 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1457 for (i = 0; i < 2; i++) {
1458 GstPad *teepad, *queuepad;
1459 /* For the sender we create this bit of pipeline for both
1460 * RTP and RTCP. Sync and preroll are enabled on udpsink so
1461 * we need to add a queue before appsink to make the pipeline
1462 * not block. For the TCP case, we want to pump data to the
1463 * client as fast as possible anyway.
1465 * .--------. .-----. .---------.
1466 * | rtpbin | | tee | | udpsink |
1467 * | send->sink src->sink |
1468 * '--------' | | '---------'
1469 * | | .---------. .---------.
1470 * | | | queue | | appsink |
1471 * | src->sink src->sink |
1472 * '-----' '---------' '---------'
1474 * When only UDP is allowed, we skip the tee, queue and appsink and link the
1475 * udpsink directly to the session.
1478 gst_bin_add (bin, priv->udpsink[i]);
1479 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1481 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1482 /* make tee for RTP/RTCP */
1483 priv->tee[i] = gst_element_factory_make ("tee", NULL);
1484 gst_bin_add (bin, priv->tee[i]);
1486 /* and link to rtpbin send pad */
1487 pad = gst_element_get_static_pad (priv->tee[i], "sink");
1488 gst_pad_link (priv->send_src[i], pad);
1489 gst_object_unref (pad);
1491 /* link tee to udpsink */
1492 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1493 gst_pad_link (teepad, sinkpad);
1494 gst_object_unref (teepad);
1497 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1498 gst_bin_add (bin, priv->appqueue[i]);
1499 /* and link to tee */
1500 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1501 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1502 gst_pad_link (teepad, pad);
1503 gst_object_unref (pad);
1504 gst_object_unref (teepad);
1507 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1508 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1509 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1510 gst_bin_add (bin, priv->appsink[i]);
1511 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1512 &sink_cb, stream, NULL);
1513 /* and link to queue */
1514 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1515 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1516 gst_pad_link (queuepad, pad);
1517 gst_object_unref (pad);
1518 gst_object_unref (queuepad);
1520 /* else only udpsink needed, link it to the session */
1521 gst_pad_link (priv->send_src[i], sinkpad);
1523 gst_object_unref (sinkpad);
1525 /* For the receiver we create this bit of pipeline for both
1526 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1527 * and it is all funneled into the rtpbin receive pad.
1529 * .--------. .--------. .--------.
1530 * | udpsrc | | funnel | | rtpbin |
1531 * | src->sink src->sink |
1532 * '--------' | | '--------'
1536 * '--------' '--------'
1538 /* make funnel for the RTP/RTCP receivers */
1539 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1540 gst_bin_add (bin, priv->funnel[i]);
1542 pad = gst_element_get_static_pad (priv->funnel[i], "src");
1543 gst_pad_link (pad, priv->recv_sink[i]);
1544 gst_object_unref (pad);
1546 if (priv->udpsrc_v4[i]) {
1547 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1549 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
1550 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
1552 gst_bin_add (bin, priv->udpsrc_v4[i]);
1554 /* and link to the funnel v4 */
1555 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1556 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
1557 gst_pad_link (pad, selpad);
1558 gst_object_unref (pad);
1559 gst_object_unref (selpad);
1562 if (priv->udpsrc_v6[i]) {
1563 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
1564 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
1565 gst_bin_add (bin, priv->udpsrc_v6[i]);
1567 /* and link to the funnel v6 */
1568 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1569 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
1570 gst_pad_link (pad, selpad);
1571 gst_object_unref (pad);
1572 gst_object_unref (selpad);
1575 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1576 /* make and add appsrc */
1577 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1578 gst_bin_add (bin, priv->appsrc[i]);
1579 /* and link to the funnel */
1580 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1581 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
1582 gst_pad_link (pad, selpad);
1583 gst_object_unref (pad);
1584 gst_object_unref (selpad);
1587 /* check if we need to set to a special state */
1588 if (state != GST_STATE_NULL) {
1589 if (priv->udpsink[i])
1590 gst_element_set_state (priv->udpsink[i], state);
1591 if (priv->appsink[i])
1592 gst_element_set_state (priv->appsink[i], state);
1593 if (priv->appqueue[i])
1594 gst_element_set_state (priv->appqueue[i], state);
1596 gst_element_set_state (priv->tee[i], state);
1597 if (priv->funnel[i])
1598 gst_element_set_state (priv->funnel[i], state);
1599 if (priv->appsrc[i])
1600 gst_element_set_state (priv->appsrc[i], state);
1604 /* be notified of caps changes */
1605 priv->caps_sig = g_signal_connect (priv->send_rtp_sink, "notify::caps",
1606 (GCallback) caps_notify, stream);
1608 priv->is_joined = TRUE;
1609 g_mutex_unlock (&priv->lock);
1616 g_mutex_unlock (&priv->lock);
1621 g_mutex_unlock (&priv->lock);
1622 GST_WARNING ("failed to allocate ports %u", idx);
1627 GST_WARNING ("failed to link stream %u", idx);
1628 gst_object_unref (priv->send_rtp_sink);
1629 priv->send_rtp_sink = NULL;
1630 g_mutex_unlock (&priv->lock);
1636 * gst_rtsp_stream_leave_bin:
1637 * @stream: a #GstRTSPStream
1639 * @rtpbin: a rtpbin #GstElement
1641 * Remove the elements of @stream from @bin.
1643 * Return: %TRUE on success.
1646 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1647 GstElement * rtpbin)
1649 GstRTSPStreamPrivate *priv;
1652 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1653 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1654 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1656 priv = stream->priv;
1658 g_mutex_lock (&priv->lock);
1659 if (!priv->is_joined)
1660 goto was_not_joined;
1662 /* all transports must be removed by now */
1663 g_return_val_if_fail (priv->transports == NULL, FALSE);
1665 GST_INFO ("stream %p leaving bin", stream);
1667 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
1668 g_signal_handler_disconnect (priv->send_rtp_sink, priv->caps_sig);
1669 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
1670 gst_object_unref (priv->send_rtp_sink);
1671 priv->send_rtp_sink = NULL;
1673 for (i = 0; i < 2; i++) {
1674 if (priv->udpsink[i])
1675 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
1676 if (priv->appsink[i])
1677 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
1678 if (priv->appqueue[i])
1679 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
1681 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
1682 if (priv->funnel[i])
1683 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
1684 if (priv->appsrc[i])
1685 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
1686 if (priv->udpsrc_v4[i]) {
1687 /* and set udpsrc to NULL now before removing */
1688 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
1689 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
1690 /* removing them should also nicely release the request
1691 * pads when they finalize */
1692 gst_bin_remove (bin, priv->udpsrc_v4[i]);
1694 if (priv->udpsrc_v6[i]) {
1695 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
1696 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
1697 gst_bin_remove (bin, priv->udpsrc_v6[i]);
1699 if (priv->udpsink[i])
1700 gst_bin_remove (bin, priv->udpsink[i]);
1701 if (priv->appsrc[i])
1702 gst_bin_remove (bin, priv->appsrc[i]);
1703 if (priv->appsink[i])
1704 gst_bin_remove (bin, priv->appsink[i]);
1705 if (priv->appqueue[i])
1706 gst_bin_remove (bin, priv->appqueue[i]);
1708 gst_bin_remove (bin, priv->tee[i]);
1709 if (priv->funnel[i])
1710 gst_bin_remove (bin, priv->funnel[i]);
1712 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
1713 gst_object_unref (priv->recv_sink[i]);
1714 priv->recv_sink[i] = NULL;
1716 priv->udpsrc_v4[i] = NULL;
1717 priv->udpsrc_v6[i] = NULL;
1718 priv->udpsink[i] = NULL;
1719 priv->appsrc[i] = NULL;
1720 priv->appsink[i] = NULL;
1721 priv->appqueue[i] = NULL;
1722 priv->tee[i] = NULL;
1723 priv->funnel[i] = NULL;
1725 gst_object_unref (priv->send_src[0]);
1726 priv->send_src[0] = NULL;
1728 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
1729 gst_object_unref (priv->send_src[1]);
1730 priv->send_src[1] = NULL;
1732 g_object_unref (priv->session);
1733 priv->session = NULL;
1735 gst_caps_unref (priv->caps);
1738 priv->is_joined = FALSE;
1739 g_mutex_unlock (&priv->lock);
1750 * gst_rtsp_stream_get_rtpinfo:
1751 * @stream: a #GstRTSPStream
1752 * @rtptime: result RTP timestamp
1753 * @seq: result RTP seqnum
1755 * Retrieve the current rtptime and seq. This is used to
1756 * construct a RTPInfo reply header.
1758 * Returns: %TRUE when rtptime and seq could be determined.
1761 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
1762 guint * rtptime, guint * seq)
1764 GstRTSPStreamPrivate *priv;
1765 GObjectClass *payobjclass;
1767 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1768 g_return_val_if_fail (rtptime != NULL, FALSE);
1769 g_return_val_if_fail (seq != NULL, FALSE);
1771 priv = stream->priv;
1773 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
1775 if (!g_object_class_find_property (payobjclass, "seqnum") ||
1776 !g_object_class_find_property (payobjclass, "timestamp"))
1779 g_object_get (priv->payloader, "seqnum", seq, "timestamp", rtptime, NULL);
1785 * gst_rtsp_stream_get_caps:
1786 * @stream: a #GstRTSPStream
1788 * Retrieve the current caps of @stream.
1790 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
1794 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
1796 GstRTSPStreamPrivate *priv;
1799 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1801 priv = stream->priv;
1803 g_mutex_lock (&priv->lock);
1804 if ((result = priv->caps))
1805 gst_caps_ref (result);
1806 g_mutex_unlock (&priv->lock);
1812 * gst_rtsp_stream_recv_rtp:
1813 * @stream: a #GstRTSPStream
1814 * @buffer: (transfer full): a #GstBuffer
1816 * Handle an RTP buffer for the stream. This method is usually called when a
1817 * message has been received from a client using the TCP transport.
1819 * This function takes ownership of @buffer.
1821 * Returns: a GstFlowReturn.
1824 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
1826 GstRTSPStreamPrivate *priv;
1828 GstElement *element;
1830 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1831 priv = stream->priv;
1832 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1833 g_return_val_if_fail (priv->is_joined, FALSE);
1835 g_mutex_lock (&priv->lock);
1836 if (priv->appsrc[0])
1837 element = gst_object_ref (priv->appsrc[0]);
1840 g_mutex_unlock (&priv->lock);
1843 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1844 gst_object_unref (element);
1852 * gst_rtsp_stream_recv_rtcp:
1853 * @stream: a #GstRTSPStream
1854 * @buffer: (transfer full): a #GstBuffer
1856 * Handle an RTCP buffer for the stream. This method is usually called when a
1857 * message has been received from a client using the TCP transport.
1859 * This function takes ownership of @buffer.
1861 * Returns: a GstFlowReturn.
1864 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
1866 GstRTSPStreamPrivate *priv;
1868 GstElement *element;
1870 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1871 priv = stream->priv;
1872 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1873 g_return_val_if_fail (priv->is_joined, FALSE);
1875 g_mutex_lock (&priv->lock);
1876 if (priv->appsrc[1])
1877 element = gst_object_ref (priv->appsrc[1]);
1880 g_mutex_unlock (&priv->lock);
1883 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1884 gst_object_unref (element);
1891 /* must be called with lock */
1893 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
1896 GstRTSPStreamPrivate *priv = stream->priv;
1897 const GstRTSPTransport *tr;
1899 tr = gst_rtsp_stream_transport_get_transport (trans);
1901 switch (tr->lower_transport) {
1902 case GST_RTSP_LOWER_TRANS_UDP:
1903 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1909 dest = tr->destination;
1910 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1915 min = tr->client_port.min;
1916 max = tr->client_port.max;
1920 GST_INFO ("adding %s:%d-%d", dest, min, max);
1921 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
1922 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
1924 GST_INFO ("setting ttl-mc %d", ttl);
1925 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
1926 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
1928 priv->transports = g_list_prepend (priv->transports, trans);
1930 GST_INFO ("removing %s:%d-%d", dest, min, max);
1931 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
1932 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
1933 priv->transports = g_list_remove (priv->transports, trans);
1937 case GST_RTSP_LOWER_TRANS_TCP:
1939 GST_INFO ("adding TCP %s", tr->destination);
1940 priv->transports = g_list_prepend (priv->transports, trans);
1942 GST_INFO ("removing TCP %s", tr->destination);
1943 priv->transports = g_list_remove (priv->transports, trans);
1947 goto unknown_transport;
1954 GST_INFO ("Unknown transport %d", tr->lower_transport);
1961 * gst_rtsp_stream_add_transport:
1962 * @stream: a #GstRTSPStream
1963 * @trans: a #GstRTSPStreamTransport
1965 * Add the transport in @trans to @stream. The media of @stream will
1966 * then also be send to the values configured in @trans.
1968 * @stream must be joined to a bin.
1970 * @trans must contain a valid #GstRTSPTransport.
1972 * Returns: %TRUE if @trans was added
1975 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
1976 GstRTSPStreamTransport * trans)
1978 GstRTSPStreamPrivate *priv;
1981 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1982 priv = stream->priv;
1983 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1984 g_return_val_if_fail (priv->is_joined, FALSE);
1986 g_mutex_lock (&priv->lock);
1987 res = update_transport (stream, trans, TRUE);
1988 g_mutex_unlock (&priv->lock);
1994 * gst_rtsp_stream_remove_transport:
1995 * @stream: a #GstRTSPStream
1996 * @trans: a #GstRTSPStreamTransport
1998 * Remove the transport in @trans from @stream. The media of @stream will
1999 * not be sent to the values configured in @trans.
2001 * @stream must be joined to a bin.
2003 * @trans must contain a valid #GstRTSPTransport.
2005 * Returns: %TRUE if @trans was removed
2008 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2009 GstRTSPStreamTransport * trans)
2011 GstRTSPStreamPrivate *priv;
2014 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2015 priv = stream->priv;
2016 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2017 g_return_val_if_fail (priv->is_joined, FALSE);
2019 g_mutex_lock (&priv->lock);
2020 res = update_transport (stream, trans, FALSE);
2021 g_mutex_unlock (&priv->lock);
2027 * gst_rtsp_stream_get_rtp_socket:
2028 * @stream: a #GstRTSPStream
2029 * @family: the socket family
2031 * Get the RTP socket from @stream for a @family.
2033 * @stream must be joined to a bin.
2035 * Returns: the RTP socket or %NULL if no socket could be allocated for @family.
2039 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2041 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2045 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2046 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2047 family == G_SOCKET_FAMILY_IPV6, NULL);
2048 g_return_val_if_fail (priv->udpsink[0], NULL);
2050 if (family == G_SOCKET_FAMILY_IPV6)
2055 g_object_get (priv->udpsink[0], name, &socket, NULL);
2061 * gst_rtsp_stream_get_rtcp_socket:
2062 * @stream: a #GstRTSPStream
2063 * @family: the socket family
2065 * Get the RTCP socket from @stream for a @family.
2067 * @stream must be joined to a bin.
2069 * Returns: the RTCP socket or %NULL if no socket could be allocated for
2070 * @family. Unref after usage
2073 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
2075 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2079 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2080 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2081 family == G_SOCKET_FAMILY_IPV6, NULL);
2082 g_return_val_if_fail (priv->udpsink[1], NULL);
2084 if (family == G_SOCKET_FAMILY_IPV6)
2089 g_object_get (priv->udpsink[1], name, &socket, NULL);
2095 * gst_rtsp_stream_transport_filter:
2096 * @stream: a #GstRTSPStream
2097 * @func: (scope call) (allow-none): a callback
2098 * @user_data: user data passed to @func
2100 * Call @func for each transport managed by @stream. The result value of @func
2101 * determines what happens to the transport. @func will be called with @stream
2102 * locked so no further actions on @stream can be performed from @func.
2104 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
2107 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
2109 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
2110 * will also be added with an additional ref to the result #GList of this
2113 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
2115 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
2116 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2117 * element in the #GList should be unreffed before the list is freed.
2120 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
2121 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
2123 GstRTSPStreamPrivate *priv;
2124 GList *result, *walk, *next;
2126 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2128 priv = stream->priv;
2132 g_mutex_lock (&priv->lock);
2133 for (walk = priv->transports; walk; walk = next) {
2134 GstRTSPStreamTransport *trans = walk->data;
2135 GstRTSPFilterResult res;
2137 next = g_list_next (walk);
2140 res = func (stream, trans, user_data);
2142 res = GST_RTSP_FILTER_REF;
2145 case GST_RTSP_FILTER_REMOVE:
2146 update_transport (stream, trans, FALSE);
2148 case GST_RTSP_FILTER_REF:
2149 result = g_list_prepend (result, g_object_ref (trans));
2151 case GST_RTSP_FILTER_KEEP:
2156 g_mutex_unlock (&priv->lock);
2161 static GstPadProbeReturn
2162 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2164 GstRTSPStreamPrivate *priv;
2165 GstRTSPStream *stream;
2168 priv = stream->priv;
2170 GST_DEBUG_OBJECT (pad, "now blocking");
2172 g_mutex_lock (&priv->lock);
2173 priv->blocking = TRUE;
2174 g_mutex_unlock (&priv->lock);
2176 gst_element_post_message (priv->payloader,
2177 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
2178 gst_structure_new_empty ("GstRTSPStreamBlocking")));
2180 return GST_PAD_PROBE_OK;
2184 * gst_rtsp_stream_set_blocked:
2185 * @stream: a #GstRTSPStream
2186 * @blocked: boolean indicating we should block or unblock
2188 * Blocks or unblocks the dataflow on @stream.
2190 * Returns: %TRUE on success
2193 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
2195 GstRTSPStreamPrivate *priv;
2197 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2199 priv = stream->priv;
2201 g_mutex_lock (&priv->lock);
2203 priv->blocking = FALSE;
2204 if (priv->blocked_id == 0) {
2205 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
2206 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
2207 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
2208 g_object_ref (stream), g_object_unref);
2211 if (priv->blocked_id != 0) {
2212 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
2213 priv->blocked_id = 0;
2214 priv->blocking = FALSE;
2217 g_mutex_unlock (&priv->lock);
2223 * gst_rtsp_stream_is_blocking:
2224 * @stream: a #GstRTSPStream
2226 * Check if @stream is blocking on a #GstBuffer.
2228 * Returns: %TRUE if @stream is blocking
2231 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
2233 GstRTSPStreamPrivate *priv;
2236 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2238 priv = stream->priv;
2240 g_mutex_lock (&priv->lock);
2241 result = priv->blocking;
2242 g_mutex_unlock (&priv->lock);