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 */
125 #define DEFAULT_CONTROL NULL
126 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
127 GST_RTSP_LOWER_TRANS_TCP
137 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
138 #define GST_CAT_DEFAULT rtsp_stream_debug
140 static GQuark ssrc_stream_map_key;
142 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
143 GValue * value, GParamSpec * pspec);
144 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
145 const GValue * value, GParamSpec * pspec);
147 static void gst_rtsp_stream_finalize (GObject * obj);
149 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
152 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
154 GObjectClass *gobject_class;
156 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
158 gobject_class = G_OBJECT_CLASS (klass);
160 gobject_class->get_property = gst_rtsp_stream_get_property;
161 gobject_class->set_property = gst_rtsp_stream_set_property;
162 gobject_class->finalize = gst_rtsp_stream_finalize;
164 g_object_class_install_property (gobject_class, PROP_CONTROL,
165 g_param_spec_string ("control", "Control",
166 "The control string for this stream", DEFAULT_CONTROL,
167 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
170 g_param_spec_flags ("protocols", "Protocols",
171 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
172 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
176 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
180 gst_rtsp_stream_init (GstRTSPStream * stream)
182 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
184 GST_DEBUG ("new stream %p", stream);
189 priv->control = g_strdup (DEFAULT_CONTROL);
190 priv->protocols = DEFAULT_PROTOCOLS;
192 g_mutex_init (&priv->lock);
196 gst_rtsp_stream_finalize (GObject * obj)
198 GstRTSPStream *stream;
199 GstRTSPStreamPrivate *priv;
201 stream = GST_RTSP_STREAM (obj);
204 GST_DEBUG ("finalize stream %p", stream);
206 /* we really need to be unjoined now */
207 g_return_if_fail (!priv->is_joined);
210 gst_rtsp_address_free (priv->addr_v4);
212 gst_rtsp_address_free (priv->addr_v6);
213 if (priv->server_addr_v4)
214 gst_rtsp_address_free (priv->server_addr_v4);
215 if (priv->server_addr_v6)
216 gst_rtsp_address_free (priv->server_addr_v6);
218 g_object_unref (priv->pool);
219 gst_object_unref (priv->payloader);
220 gst_object_unref (priv->srcpad);
221 g_free (priv->control);
222 g_mutex_clear (&priv->lock);
224 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
228 gst_rtsp_stream_get_property (GObject * object, guint propid,
229 GValue * value, GParamSpec * pspec)
231 GstRTSPStream *stream = GST_RTSP_STREAM (object);
235 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
238 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
246 gst_rtsp_stream_set_property (GObject * object, guint propid,
247 const GValue * value, GParamSpec * pspec)
249 GstRTSPStream *stream = GST_RTSP_STREAM (object);
253 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
256 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
264 * gst_rtsp_stream_new:
267 * @payloader: a #GstElement
269 * Create a new media stream with index @idx that handles RTP data on
270 * @srcpad and has a payloader element @payloader.
272 * Returns: a new #GstRTSPStream
275 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
277 GstRTSPStreamPrivate *priv;
278 GstRTSPStream *stream;
280 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
281 g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
282 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
284 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
287 priv->payloader = gst_object_ref (payloader);
288 priv->srcpad = gst_object_ref (srcpad);
294 * gst_rtsp_stream_get_index:
295 * @stream: a #GstRTSPStream
297 * Get the stream index.
299 * Return: the stream index.
302 gst_rtsp_stream_get_index (GstRTSPStream * stream)
304 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
306 return stream->priv->idx;
310 * gst_rtsp_stream_get_srcpad:
311 * @stream: a #GstRTSPStream
313 * Get the srcpad associated with @stream.
315 * Return: the srcpad. Unref after usage.
318 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
320 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
322 return gst_object_ref (stream->priv->srcpad);
326 * gst_rtsp_stream_get_control:
327 * @stream: a #GstRTSPStream
329 * Get the control string to identify this stream.
331 * Return: the control string. free after usage.
334 gst_rtsp_stream_get_control (GstRTSPStream * stream)
336 GstRTSPStreamPrivate *priv;
339 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
343 g_mutex_lock (&priv->lock);
344 if ((result = g_strdup (priv->control)) == NULL)
345 result = g_strdup_printf ("stream=%u", priv->idx);
346 g_mutex_unlock (&priv->lock);
352 * gst_rtsp_stream_set_control:
353 * @stream: a #GstRTSPStream
354 * @control: a control string
356 * Set the control string in @stream.
359 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
361 GstRTSPStreamPrivate *priv;
363 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
367 g_mutex_lock (&priv->lock);
368 g_free (priv->control);
369 priv->control = g_strdup (control);
370 g_mutex_unlock (&priv->lock);
374 * gst_rtsp_stream_has_control:
375 * @stream: a #GstRTSPStream
376 * @control: a control string
378 * Check if @stream has the control string @control.
380 * Returns: %TRUE is @stream has @control as the control string
383 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
385 GstRTSPStreamPrivate *priv;
388 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
392 g_mutex_lock (&priv->lock);
394 res = g_strcmp0 (priv->control, control);
397 sscanf (control, "stream=%u", &streamid);
398 res = (streamid == priv->idx);
400 g_mutex_unlock (&priv->lock);
406 * gst_rtsp_stream_set_mtu:
407 * @stream: a #GstRTSPStream
410 * Configure the mtu in the payloader of @stream to @mtu.
413 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
415 GstRTSPStreamPrivate *priv;
417 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
421 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
423 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
427 * gst_rtsp_stream_get_mtu:
428 * @stream: a #GstRTSPStream
430 * Get the configured MTU in the payloader of @stream.
432 * Returns: the MTU of the payloader.
435 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
437 GstRTSPStreamPrivate *priv;
440 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
444 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
449 /* Update the dscp qos property on the udp sinks */
451 update_dscp_qos (GstRTSPStream * stream)
453 GstRTSPStreamPrivate *priv;
455 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
459 if (priv->udpsink[0]) {
460 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
464 if (priv->udpsink[1]) {
465 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
471 * gst_rtsp_stream_set_dscp_qos:
472 * @stream: a #GstRTSPStream
473 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
475 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
478 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
480 GstRTSPStreamPrivate *priv;
482 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
486 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
488 if (dscp_qos < -1 || dscp_qos > 63) {
489 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
493 priv->dscp_qos = dscp_qos;
495 update_dscp_qos (stream);
499 * gst_rtsp_stream_get_dscp_qos:
500 * @stream: a #GstRTSPStream
502 * Get the configured DSCP QoS in of the outgoing sockets.
504 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
507 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
509 GstRTSPStreamPrivate *priv;
511 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
515 return priv->dscp_qos;
519 * gst_rtsp_stream_set_protocols:
520 * @stream: a #GstRTSPStream
521 * @protocols: the new flags
523 * Configure the allowed lower transport for @stream.
526 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
527 GstRTSPLowerTrans protocols)
529 GstRTSPStreamPrivate *priv;
531 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
535 g_mutex_lock (&priv->lock);
536 priv->protocols = protocols;
537 g_mutex_unlock (&priv->lock);
541 * gst_rtsp_stream_get_protocols:
542 * @stream: a #GstRTSPStream
544 * Get the allowed protocols of @stream.
546 * Returns: a #GstRTSPLowerTrans
549 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
551 GstRTSPStreamPrivate *priv;
552 GstRTSPLowerTrans res;
554 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
555 GST_RTSP_LOWER_TRANS_UNKNOWN);
559 g_mutex_lock (&priv->lock);
560 res = priv->protocols;
561 g_mutex_unlock (&priv->lock);
567 * gst_rtsp_stream_set_address_pool:
568 * @stream: a #GstRTSPStream
569 * @pool: a #GstRTSPAddressPool
571 * configure @pool to be used as the address pool of @stream.
574 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
575 GstRTSPAddressPool * pool)
577 GstRTSPStreamPrivate *priv;
578 GstRTSPAddressPool *old;
580 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
584 GST_LOG_OBJECT (stream, "set address pool %p", pool);
586 g_mutex_lock (&priv->lock);
587 if ((old = priv->pool) != pool)
588 priv->pool = pool ? g_object_ref (pool) : NULL;
591 g_mutex_unlock (&priv->lock);
594 g_object_unref (old);
598 * gst_rtsp_stream_get_address_pool:
599 * @stream: a #GstRTSPStream
601 * Get the #GstRTSPAddressPool used as the address pool of @stream.
603 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
607 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
609 GstRTSPStreamPrivate *priv;
610 GstRTSPAddressPool *result;
612 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
616 g_mutex_lock (&priv->lock);
617 if ((result = priv->pool))
618 g_object_ref (result);
619 g_mutex_unlock (&priv->lock);
625 * gst_rtsp_stream_get_multicast_address:
626 * @stream: a #GstRTSPStream
627 * @family: the #GSocketFamily
629 * Get the multicast address of @stream for @family.
631 * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
632 * allocated. gst_rtsp_address_free() after usage.
635 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
636 GSocketFamily family)
638 GstRTSPStreamPrivate *priv;
639 GstRTSPAddress *result;
640 GstRTSPAddress **addrp;
641 GstRTSPAddressFlags flags;
643 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
647 if (family == G_SOCKET_FAMILY_IPV6) {
648 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
649 addrp = &priv->addr_v4;
651 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
652 addrp = &priv->addr_v6;
655 g_mutex_lock (&priv->lock);
656 if (*addrp == NULL) {
657 if (priv->pool == NULL)
660 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
662 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
666 result = gst_rtsp_address_copy (*addrp);
667 g_mutex_unlock (&priv->lock);
674 GST_ERROR_OBJECT (stream, "no address pool specified");
675 g_mutex_unlock (&priv->lock);
680 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
681 g_mutex_unlock (&priv->lock);
687 * gst_rtsp_stream_reserve_address:
688 * @stream: a #GstRTSPStream
689 * @address: an address
694 * Reserve @address and @port as the address and port of @stream.
696 * Returns: the #GstRTSPAddress of @stream or %NULL when the address could be
697 * reserved. gst_rtsp_address_free() after usage.
700 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
701 const gchar * address, guint port, guint n_ports, guint ttl)
703 GstRTSPStreamPrivate *priv;
704 GstRTSPAddress *result;
706 GSocketFamily family;
707 GstRTSPAddress **addrp;
709 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
710 g_return_val_if_fail (address != NULL, NULL);
711 g_return_val_if_fail (port > 0, NULL);
712 g_return_val_if_fail (n_ports > 0, NULL);
713 g_return_val_if_fail (ttl > 0, NULL);
717 addr = g_inet_address_new_from_string (address);
719 GST_ERROR ("failed to get inet addr from %s", address);
720 family = G_SOCKET_FAMILY_IPV4;
722 family = g_inet_address_get_family (addr);
723 g_object_unref (addr);
726 if (family == G_SOCKET_FAMILY_IPV6)
727 addrp = &priv->addr_v4;
729 addrp = &priv->addr_v6;
731 g_mutex_lock (&priv->lock);
732 if (*addrp == NULL) {
733 GstRTSPAddressPoolResult res;
735 if (priv->pool == NULL)
738 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
739 port, n_ports, ttl, addrp);
740 if (res != GST_RTSP_ADDRESS_POOL_OK)
743 if (strcmp ((*addrp)->address, address) ||
744 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
745 (*addrp)->ttl != ttl)
746 goto different_address;
748 result = gst_rtsp_address_copy (*addrp);
749 g_mutex_unlock (&priv->lock);
756 GST_ERROR_OBJECT (stream, "no address pool specified");
757 g_mutex_unlock (&priv->lock);
762 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
764 g_mutex_unlock (&priv->lock);
769 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
770 " reserved", address);
771 g_mutex_unlock (&priv->lock);
777 alloc_ports_one_family (GstRTSPAddressPool * pool, gint buffer_size,
778 GSocketFamily family, GstElement * udpsrc_out[2],
779 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
780 GstRTSPAddress ** server_addr_out)
782 GstStateChangeReturn ret;
783 GstElement *udpsrc0, *udpsrc1;
784 GstElement *udpsink0, *udpsink1;
785 GSocket *rtp_socket = NULL;
786 GSocket *rtcp_socket;
787 gint tmp_rtp, tmp_rtcp;
789 gint rtpport, rtcpport;
790 GList *rejected_addresses = NULL;
791 GstRTSPAddress *addr = NULL;
792 GInetAddress *inetaddr = NULL;
793 GSocketAddress *rtp_sockaddr = NULL;
794 GSocketAddress *rtcp_sockaddr = NULL;
795 const gchar *multisink_socket;
797 if (family == G_SOCKET_FAMILY_IPV6)
798 multisink_socket = "socket-v6";
800 multisink_socket = "socket";
808 /* Start with random port */
811 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
812 G_SOCKET_PROTOCOL_UDP, NULL);
814 goto no_udp_protocol;
816 if (*server_addr_out)
817 gst_rtsp_address_free (*server_addr_out);
819 /* try to allocate 2 UDP ports, the RTP port should be an even
820 * number and the RTCP port should be the next (uneven) port */
823 if (rtp_socket == NULL) {
824 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
825 G_SOCKET_PROTOCOL_UDP, NULL);
827 goto no_udp_protocol;
830 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
831 GstRTSPAddressFlags flags;
834 rejected_addresses = g_list_prepend (rejected_addresses, addr);
836 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
837 if (family == G_SOCKET_FAMILY_IPV6)
838 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
840 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
842 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
847 tmp_rtp = addr->port;
849 g_clear_object (&inetaddr);
850 inetaddr = g_inet_address_new_from_string (addr->address);
858 if (inetaddr == NULL)
859 inetaddr = g_inet_address_new_any (family);
862 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
863 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
864 g_object_unref (rtp_sockaddr);
867 g_object_unref (rtp_sockaddr);
869 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
870 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
871 g_clear_object (&rtp_sockaddr);
876 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
877 g_object_unref (rtp_sockaddr);
879 /* check if port is even */
880 if ((tmp_rtp & 1) != 0) {
881 /* port not even, close and allocate another */
883 g_clear_object (&rtp_socket);
888 tmp_rtcp = tmp_rtp + 1;
890 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
891 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
892 g_object_unref (rtcp_sockaddr);
893 g_clear_object (&rtp_socket);
896 g_object_unref (rtcp_sockaddr);
898 g_clear_object (&inetaddr);
900 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
901 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
903 if (udpsrc0 == NULL || udpsrc1 == NULL)
904 goto no_udp_protocol;
906 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
907 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
909 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
910 if (ret == GST_STATE_CHANGE_FAILURE)
912 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
913 if (ret == GST_STATE_CHANGE_FAILURE)
916 /* all fine, do port check */
917 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
918 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
920 /* this should not happen... */
921 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
925 udpsink0 = udpsink_out[0];
927 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
930 goto no_udp_protocol;
932 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
933 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
936 udpsink1 = udpsink_out[1];
938 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
941 goto no_udp_protocol;
943 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
944 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
945 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
947 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
948 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
949 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
950 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
951 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
952 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
953 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
954 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
956 /* we keep these elements, we will further configure them when the
957 * client told us to really use the UDP ports. */
958 udpsrc_out[0] = udpsrc0;
959 udpsrc_out[1] = udpsrc1;
960 udpsink_out[0] = udpsink0;
961 udpsink_out[1] = udpsink1;
962 server_port_out->min = rtpport;
963 server_port_out->max = rtcpport;
965 *server_addr_out = addr;
966 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
968 g_object_unref (rtp_socket);
969 g_object_unref (rtcp_socket);
997 gst_element_set_state (udpsrc0, GST_STATE_NULL);
998 gst_object_unref (udpsrc0);
1001 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1002 gst_object_unref (udpsrc1);
1005 gst_element_set_state (udpsink0, GST_STATE_NULL);
1006 gst_object_unref (udpsink0);
1009 gst_element_set_state (udpsink1, GST_STATE_NULL);
1010 gst_object_unref (udpsink1);
1013 g_object_unref (inetaddr);
1014 g_list_free_full (rejected_addresses,
1015 (GDestroyNotify) gst_rtsp_address_free);
1017 gst_rtsp_address_free (addr);
1019 g_object_unref (rtp_socket);
1021 g_object_unref (rtcp_socket);
1026 /* must be called with lock */
1028 alloc_ports (GstRTSPStream * stream)
1030 GstRTSPStreamPrivate *priv = stream->priv;
1032 priv->have_ipv4 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1033 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1034 &priv->server_port_v4, &priv->server_addr_v4);
1036 priv->have_ipv6 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1037 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1038 &priv->server_port_v6, &priv->server_addr_v6);
1040 return priv->have_ipv4 || priv->have_ipv6;
1044 * gst_rtsp_stream_get_server_port:
1045 * @stream: a #GstRTSPStream
1046 * @server_port: (out): result server port
1047 * @family: the port family to get
1049 * Fill @server_port with the port pair used by the server. This function can
1050 * only be called when @stream has been joined.
1053 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1054 GstRTSPRange * server_port, GSocketFamily family)
1056 GstRTSPStreamPrivate *priv;
1058 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1059 priv = stream->priv;
1060 g_return_if_fail (priv->is_joined);
1062 g_mutex_lock (&priv->lock);
1063 if (family == G_SOCKET_FAMILY_IPV4) {
1065 *server_port = priv->server_port_v4;
1068 *server_port = priv->server_port_v6;
1070 g_mutex_unlock (&priv->lock);
1074 * gst_rtsp_stream_get_rtpsession:
1075 * @stream: a #GstRTSPStream
1077 * Get the RTP session of this stream.
1079 * Returns: The RTP session of this stream. Unref after usage.
1082 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1084 GstRTSPStreamPrivate *priv;
1087 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1089 priv = stream->priv;
1091 g_mutex_lock (&priv->lock);
1092 if ((session = priv->session))
1093 g_object_ref (session);
1094 g_mutex_unlock (&priv->lock);
1100 * gst_rtsp_stream_get_ssrc:
1101 * @stream: a #GstRTSPStream
1102 * @ssrc: (out): result ssrc
1104 * Get the SSRC used by the RTP session of this stream. This function can only
1105 * be called when @stream has been joined.
1108 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1110 GstRTSPStreamPrivate *priv;
1112 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1113 priv = stream->priv;
1114 g_return_if_fail (priv->is_joined);
1116 g_mutex_lock (&priv->lock);
1117 if (ssrc && priv->session)
1118 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1119 g_mutex_unlock (&priv->lock);
1122 /* executed from streaming thread */
1124 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1126 GstRTSPStreamPrivate *priv = stream->priv;
1127 GstCaps *newcaps, *oldcaps;
1129 newcaps = gst_pad_get_current_caps (pad);
1131 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1134 g_mutex_lock (&priv->lock);
1135 oldcaps = priv->caps;
1136 priv->caps = newcaps;
1137 g_mutex_unlock (&priv->lock);
1140 gst_caps_unref (oldcaps);
1144 dump_structure (const GstStructure * s)
1148 sstr = gst_structure_to_string (s);
1149 GST_INFO ("structure: %s", sstr);
1153 static GstRTSPStreamTransport *
1154 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1156 GstRTSPStreamPrivate *priv = stream->priv;
1158 GstRTSPStreamTransport *result = NULL;
1163 if (rtcp_from == NULL)
1166 tmp = g_strrstr (rtcp_from, ":");
1170 port = atoi (tmp + 1);
1171 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1173 g_mutex_lock (&priv->lock);
1174 GST_INFO ("finding %s:%d in %d transports", dest, port,
1175 g_list_length (priv->transports));
1177 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1178 GstRTSPStreamTransport *trans = walk->data;
1179 const GstRTSPTransport *tr;
1182 tr = gst_rtsp_stream_transport_get_transport (trans);
1184 min = tr->client_port.min;
1185 max = tr->client_port.max;
1187 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1193 g_object_ref (result);
1194 g_mutex_unlock (&priv->lock);
1201 static GstRTSPStreamTransport *
1202 check_transport (GObject * source, GstRTSPStream * stream)
1204 GstStructure *stats;
1205 GstRTSPStreamTransport *trans;
1207 /* see if we have a stream to match with the origin of the RTCP packet */
1208 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1209 if (trans == NULL) {
1210 g_object_get (source, "stats", &stats, NULL);
1212 const gchar *rtcp_from;
1214 dump_structure (stats);
1216 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1217 if ((trans = find_transport (stream, rtcp_from))) {
1218 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1220 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1223 gst_structure_free (stats);
1231 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1233 GstRTSPStreamTransport *trans;
1235 GST_INFO ("%p: new source %p", stream, source);
1237 trans = check_transport (source, stream);
1240 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1244 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1246 GST_INFO ("%p: new SDES %p", stream, source);
1250 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1252 GstRTSPStreamTransport *trans;
1254 trans = check_transport (source, stream);
1257 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1258 gst_rtsp_stream_transport_keep_alive (trans);
1262 GstStructure *stats;
1263 g_object_get (source, "stats", &stats, NULL);
1265 dump_structure (stats);
1266 gst_structure_free (stats);
1273 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1275 GST_INFO ("%p: source %p bye", stream, source);
1279 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1281 GstRTSPStreamTransport *trans;
1283 GST_INFO ("%p: source %p bye timeout", stream, source);
1285 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1286 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1287 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1292 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1294 GstRTSPStreamTransport *trans;
1296 GST_INFO ("%p: source %p timeout", stream, source);
1298 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1299 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1300 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1304 static GstFlowReturn
1305 handle_new_sample (GstAppSink * sink, gpointer user_data)
1307 GstRTSPStreamPrivate *priv;
1311 GstRTSPStream *stream;
1313 sample = gst_app_sink_pull_sample (sink);
1317 stream = (GstRTSPStream *) user_data;
1318 priv = stream->priv;
1319 buffer = gst_sample_get_buffer (sample);
1321 g_mutex_lock (&priv->lock);
1322 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1323 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1325 if (GST_ELEMENT_CAST (sink) == priv->appsink[0]) {
1326 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1328 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1331 g_mutex_unlock (&priv->lock);
1333 gst_sample_unref (sample);
1338 static GstAppSinkCallbacks sink_cb = {
1339 NULL, /* not interested in EOS */
1340 NULL, /* not interested in preroll samples */
1345 * gst_rtsp_stream_join_bin:
1346 * @stream: a #GstRTSPStream
1347 * @bin: a #GstBin to join
1348 * @rtpbin: a rtpbin element in @bin
1349 * @state: the target state of the new elements
1351 * Join the #Gstbin @bin that contains the element @rtpbin.
1353 * @stream will link to @rtpbin, which must be inside @bin. The elements
1354 * added to @bin will be set to the state given in @state.
1356 * Returns: %TRUE on success.
1359 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1360 GstElement * rtpbin, GstState state)
1362 GstRTSPStreamPrivate *priv;
1366 GstPad *pad, *sinkpad, *selpad;
1367 GstPadLinkReturn ret;
1369 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1370 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1371 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1373 priv = stream->priv;
1375 g_mutex_lock (&priv->lock);
1376 if (priv->is_joined)
1379 /* create a session with the same index as the stream */
1382 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1384 if (!alloc_ports (stream))
1387 /* update the dscp qos field in the sinks */
1388 update_dscp_qos (stream);
1390 /* get a pad for sending RTP */
1391 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1392 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
1394 /* link the RTP pad to the session manager, it should not really fail unless
1395 * this is not really an RTP pad */
1396 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
1397 if (ret != GST_PAD_LINK_OK)
1400 /* get pads from the RTP session element for sending and receiving
1402 name = g_strdup_printf ("send_rtp_src_%u", idx);
1403 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
1405 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1406 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
1408 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1409 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
1411 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1412 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
1415 /* get the session */
1416 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
1418 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1420 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1422 g_signal_connect (priv->session, "on-ssrc-active",
1423 (GCallback) on_ssrc_active, stream);
1424 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1426 g_signal_connect (priv->session, "on-bye-timeout",
1427 (GCallback) on_bye_timeout, stream);
1428 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1431 for (i = 0; i < 2; i++) {
1432 GstPad *teepad, *queuepad;
1433 /* For the sender we create this bit of pipeline for both
1434 * RTP and RTCP. Sync and preroll are enabled on udpsink so
1435 * we need to add a queue before appsink to make the pipeline
1436 * not block. For the TCP case, we want to pump data to the
1437 * client as fast as possible anyway.
1439 * .--------. .-----. .---------.
1440 * | rtpbin | | tee | | udpsink |
1441 * | send->sink src->sink |
1442 * '--------' | | '---------'
1443 * | | .---------. .---------.
1444 * | | | queue | | appsink |
1445 * | src->sink src->sink |
1446 * '-----' '---------' '---------'
1448 * When only UDP is allowed, we skip the tee, queue and appsink and link the
1449 * udpsink directly to the session.
1452 gst_bin_add (bin, priv->udpsink[i]);
1453 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1455 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1456 /* make tee for RTP/RTCP */
1457 priv->tee[i] = gst_element_factory_make ("tee", NULL);
1458 gst_bin_add (bin, priv->tee[i]);
1460 /* and link to rtpbin send pad */
1461 pad = gst_element_get_static_pad (priv->tee[i], "sink");
1462 gst_pad_link (priv->send_src[i], pad);
1463 gst_object_unref (pad);
1465 /* link tee to udpsink */
1466 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1467 gst_pad_link (teepad, sinkpad);
1468 gst_object_unref (teepad);
1471 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1472 gst_bin_add (bin, priv->appqueue[i]);
1473 /* and link to tee */
1474 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1475 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1476 gst_pad_link (teepad, pad);
1477 gst_object_unref (pad);
1478 gst_object_unref (teepad);
1481 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1482 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1483 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1484 gst_bin_add (bin, priv->appsink[i]);
1485 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1486 &sink_cb, stream, NULL);
1487 /* and link to queue */
1488 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1489 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1490 gst_pad_link (queuepad, pad);
1491 gst_object_unref (pad);
1492 gst_object_unref (queuepad);
1494 /* else only udpsink needed, link it to the session */
1495 gst_pad_link (priv->send_src[i], sinkpad);
1497 gst_object_unref (sinkpad);
1499 /* For the receiver we create this bit of pipeline for both
1500 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1501 * and it is all funneled into the rtpbin receive pad.
1503 * .--------. .--------. .--------.
1504 * | udpsrc | | funnel | | rtpbin |
1505 * | src->sink src->sink |
1506 * '--------' | | '--------'
1510 * '--------' '--------'
1512 /* make funnel for the RTP/RTCP receivers */
1513 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1514 gst_bin_add (bin, priv->funnel[i]);
1516 pad = gst_element_get_static_pad (priv->funnel[i], "src");
1517 gst_pad_link (pad, priv->recv_sink[i]);
1518 gst_object_unref (pad);
1520 if (priv->udpsrc_v4[i]) {
1521 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1523 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
1524 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
1526 gst_bin_add (bin, priv->udpsrc_v4[i]);
1528 /* and link to the funnel v4 */
1529 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1530 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
1531 gst_pad_link (pad, selpad);
1532 gst_object_unref (pad);
1533 gst_object_unref (selpad);
1536 if (priv->udpsrc_v6[i]) {
1537 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
1538 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
1539 gst_bin_add (bin, priv->udpsrc_v6[i]);
1541 /* and link to the funnel v6 */
1542 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1543 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
1544 gst_pad_link (pad, selpad);
1545 gst_object_unref (pad);
1546 gst_object_unref (selpad);
1549 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1550 /* make and add appsrc */
1551 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1552 gst_bin_add (bin, priv->appsrc[i]);
1553 /* and link to the funnel */
1554 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1555 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
1556 gst_pad_link (pad, selpad);
1557 gst_object_unref (pad);
1558 gst_object_unref (selpad);
1561 /* check if we need to set to a special state */
1562 if (state != GST_STATE_NULL) {
1563 if (priv->udpsink[i])
1564 gst_element_set_state (priv->udpsink[i], state);
1565 if (priv->appsink[i])
1566 gst_element_set_state (priv->appsink[i], state);
1567 if (priv->appqueue[i])
1568 gst_element_set_state (priv->appqueue[i], state);
1570 gst_element_set_state (priv->tee[i], state);
1571 if (priv->funnel[i])
1572 gst_element_set_state (priv->funnel[i], state);
1573 if (priv->appsrc[i])
1574 gst_element_set_state (priv->appsrc[i], state);
1578 /* be notified of caps changes */
1579 priv->caps_sig = g_signal_connect (priv->send_rtp_sink, "notify::caps",
1580 (GCallback) caps_notify, stream);
1582 priv->is_joined = TRUE;
1583 g_mutex_unlock (&priv->lock);
1590 g_mutex_unlock (&priv->lock);
1595 g_mutex_unlock (&priv->lock);
1596 GST_WARNING ("failed to allocate ports %u", idx);
1601 GST_WARNING ("failed to link stream %u", idx);
1602 gst_object_unref (priv->send_rtp_sink);
1603 priv->send_rtp_sink = NULL;
1604 g_mutex_unlock (&priv->lock);
1610 * gst_rtsp_stream_leave_bin:
1611 * @stream: a #GstRTSPStream
1613 * @rtpbin: a rtpbin #GstElement
1615 * Remove the elements of @stream from @bin.
1617 * Return: %TRUE on success.
1620 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1621 GstElement * rtpbin)
1623 GstRTSPStreamPrivate *priv;
1626 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1627 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1628 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1630 priv = stream->priv;
1632 g_mutex_lock (&priv->lock);
1633 if (!priv->is_joined)
1634 goto was_not_joined;
1636 /* all transports must be removed by now */
1637 g_return_val_if_fail (priv->transports == NULL, FALSE);
1639 GST_INFO ("stream %p leaving bin", stream);
1641 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
1642 g_signal_handler_disconnect (priv->send_rtp_sink, priv->caps_sig);
1643 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
1644 gst_object_unref (priv->send_rtp_sink);
1645 priv->send_rtp_sink = NULL;
1647 for (i = 0; i < 2; i++) {
1648 if (priv->udpsink[i])
1649 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
1650 if (priv->appsink[i])
1651 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
1652 if (priv->appqueue[i])
1653 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
1655 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
1656 if (priv->funnel[i])
1657 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
1658 if (priv->appsrc[i])
1659 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
1660 if (priv->udpsrc_v4[i]) {
1661 /* and set udpsrc to NULL now before removing */
1662 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
1663 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
1664 /* removing them should also nicely release the request
1665 * pads when they finalize */
1666 gst_bin_remove (bin, priv->udpsrc_v4[i]);
1668 if (priv->udpsrc_v6[i]) {
1669 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
1670 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
1671 gst_bin_remove (bin, priv->udpsrc_v6[i]);
1673 if (priv->udpsink[i])
1674 gst_bin_remove (bin, priv->udpsink[i]);
1675 if (priv->appsrc[i])
1676 gst_bin_remove (bin, priv->appsrc[i]);
1677 if (priv->appsink[i])
1678 gst_bin_remove (bin, priv->appsink[i]);
1679 if (priv->appqueue[i])
1680 gst_bin_remove (bin, priv->appqueue[i]);
1682 gst_bin_remove (bin, priv->tee[i]);
1683 if (priv->funnel[i])
1684 gst_bin_remove (bin, priv->funnel[i]);
1686 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
1687 gst_object_unref (priv->recv_sink[i]);
1688 priv->recv_sink[i] = NULL;
1690 priv->udpsrc_v4[i] = NULL;
1691 priv->udpsrc_v6[i] = NULL;
1692 priv->udpsink[i] = NULL;
1693 priv->appsrc[i] = NULL;
1694 priv->appsink[i] = NULL;
1695 priv->appqueue[i] = NULL;
1696 priv->tee[i] = NULL;
1697 priv->funnel[i] = NULL;
1699 gst_object_unref (priv->send_src[0]);
1700 priv->send_src[0] = NULL;
1702 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
1703 gst_object_unref (priv->send_src[1]);
1704 priv->send_src[1] = NULL;
1706 g_object_unref (priv->session);
1707 priv->session = NULL;
1709 gst_caps_unref (priv->caps);
1712 priv->is_joined = FALSE;
1713 g_mutex_unlock (&priv->lock);
1724 * gst_rtsp_stream_get_rtpinfo:
1725 * @stream: a #GstRTSPStream
1726 * @rtptime: result RTP timestamp
1727 * @seq: result RTP seqnum
1729 * Retrieve the current rtptime and seq. This is used to
1730 * construct a RTPInfo reply header.
1732 * Returns: %TRUE when rtptime and seq could be determined.
1735 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
1736 guint * rtptime, guint * seq)
1738 GstRTSPStreamPrivate *priv;
1739 GObjectClass *payobjclass;
1741 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1742 g_return_val_if_fail (rtptime != NULL, FALSE);
1743 g_return_val_if_fail (seq != NULL, FALSE);
1745 priv = stream->priv;
1747 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
1749 if (!g_object_class_find_property (payobjclass, "seqnum") ||
1750 !g_object_class_find_property (payobjclass, "timestamp"))
1753 g_object_get (priv->payloader, "seqnum", seq, "timestamp", rtptime, NULL);
1759 * gst_rtsp_stream_get_caps:
1760 * @stream: a #GstRTSPStream
1762 * Retrieve the current caps of @stream.
1764 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
1768 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
1770 GstRTSPStreamPrivate *priv;
1773 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1775 priv = stream->priv;
1777 g_mutex_lock (&priv->lock);
1778 if ((result = priv->caps))
1779 gst_caps_ref (result);
1780 g_mutex_unlock (&priv->lock);
1786 * gst_rtsp_stream_recv_rtp:
1787 * @stream: a #GstRTSPStream
1788 * @buffer: (transfer full): a #GstBuffer
1790 * Handle an RTP buffer for the stream. This method is usually called when a
1791 * message has been received from a client using the TCP transport.
1793 * This function takes ownership of @buffer.
1795 * Returns: a GstFlowReturn.
1798 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
1800 GstRTSPStreamPrivate *priv;
1802 GstElement *element;
1804 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1805 priv = stream->priv;
1806 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1807 g_return_val_if_fail (priv->is_joined, FALSE);
1809 g_mutex_lock (&priv->lock);
1810 if (priv->appsrc[0])
1811 element = gst_object_ref (priv->appsrc[0]);
1814 g_mutex_unlock (&priv->lock);
1817 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1818 gst_object_unref (element);
1826 * gst_rtsp_stream_recv_rtcp:
1827 * @stream: a #GstRTSPStream
1828 * @buffer: (transfer full): a #GstBuffer
1830 * Handle an RTCP buffer for the stream. This method is usually called when a
1831 * message has been received from a client using the TCP transport.
1833 * This function takes ownership of @buffer.
1835 * Returns: a GstFlowReturn.
1838 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
1840 GstRTSPStreamPrivate *priv;
1842 GstElement *element;
1844 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1845 priv = stream->priv;
1846 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1847 g_return_val_if_fail (priv->is_joined, FALSE);
1849 g_mutex_lock (&priv->lock);
1850 if (priv->appsrc[1])
1851 element = gst_object_ref (priv->appsrc[1]);
1854 g_mutex_unlock (&priv->lock);
1857 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1858 gst_object_unref (element);
1865 /* must be called with lock */
1867 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
1870 GstRTSPStreamPrivate *priv = stream->priv;
1871 const GstRTSPTransport *tr;
1873 tr = gst_rtsp_stream_transport_get_transport (trans);
1875 switch (tr->lower_transport) {
1876 case GST_RTSP_LOWER_TRANS_UDP:
1877 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1883 dest = tr->destination;
1884 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1889 min = tr->client_port.min;
1890 max = tr->client_port.max;
1894 GST_INFO ("adding %s:%d-%d", dest, min, max);
1895 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
1896 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
1898 GST_INFO ("setting ttl-mc %d", ttl);
1899 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
1900 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
1902 priv->transports = g_list_prepend (priv->transports, trans);
1904 GST_INFO ("removing %s:%d-%d", dest, min, max);
1905 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
1906 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
1907 priv->transports = g_list_remove (priv->transports, trans);
1911 case GST_RTSP_LOWER_TRANS_TCP:
1913 GST_INFO ("adding TCP %s", tr->destination);
1914 priv->transports = g_list_prepend (priv->transports, trans);
1916 GST_INFO ("removing TCP %s", tr->destination);
1917 priv->transports = g_list_remove (priv->transports, trans);
1921 goto unknown_transport;
1928 GST_INFO ("Unknown transport %d", tr->lower_transport);
1935 * gst_rtsp_stream_add_transport:
1936 * @stream: a #GstRTSPStream
1937 * @trans: a #GstRTSPStreamTransport
1939 * Add the transport in @trans to @stream. The media of @stream will
1940 * then also be send to the values configured in @trans.
1942 * @stream must be joined to a bin.
1944 * @trans must contain a valid #GstRTSPTransport.
1946 * Returns: %TRUE if @trans was added
1949 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
1950 GstRTSPStreamTransport * trans)
1952 GstRTSPStreamPrivate *priv;
1955 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1956 priv = stream->priv;
1957 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1958 g_return_val_if_fail (priv->is_joined, FALSE);
1960 g_mutex_lock (&priv->lock);
1961 res = update_transport (stream, trans, TRUE);
1962 g_mutex_unlock (&priv->lock);
1968 * gst_rtsp_stream_remove_transport:
1969 * @stream: a #GstRTSPStream
1970 * @trans: a #GstRTSPStreamTransport
1972 * Remove the transport in @trans from @stream. The media of @stream will
1973 * not be sent to the values configured in @trans.
1975 * @stream must be joined to a bin.
1977 * @trans must contain a valid #GstRTSPTransport.
1979 * Returns: %TRUE if @trans was removed
1982 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
1983 GstRTSPStreamTransport * trans)
1985 GstRTSPStreamPrivate *priv;
1988 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1989 priv = stream->priv;
1990 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1991 g_return_val_if_fail (priv->is_joined, FALSE);
1993 g_mutex_lock (&priv->lock);
1994 res = update_transport (stream, trans, FALSE);
1995 g_mutex_unlock (&priv->lock);