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 GstRTSPProfile profiles;
72 GstRTSPLowerTrans protocols;
74 /* pads on the rtpbin */
75 GstPad *send_rtp_sink;
79 /* the RTPSession object */
82 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
84 GstElement *udpsrc_v4[2];
86 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
88 GstElement *udpsrc_v6[2];
90 GstElement *udpsink[2];
92 /* for TCP transport */
93 GstElement *appsrc[2];
94 GstElement *appqueue[2];
95 GstElement *appsink[2];
98 GstElement *funnel[2];
100 /* server ports for sending/receiving over ipv4 */
101 GstRTSPRange server_port_v4;
102 GstRTSPAddress *server_addr_v4;
105 /* server ports for sending/receiving over ipv6 */
106 GstRTSPRange server_port_v6;
107 GstRTSPAddress *server_addr_v6;
110 /* multicast addresses */
111 GstRTSPAddressPool *pool;
112 GstRTSPAddress *addr_v4;
113 GstRTSPAddress *addr_v6;
115 /* the caps of the stream */
119 /* transports we stream to */
125 /* stream blocking */
130 #define DEFAULT_CONTROL NULL
131 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
132 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
133 GST_RTSP_LOWER_TRANS_TCP
144 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
145 #define GST_CAT_DEFAULT rtsp_stream_debug
147 static GQuark ssrc_stream_map_key;
149 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
150 GValue * value, GParamSpec * pspec);
151 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
152 const GValue * value, GParamSpec * pspec);
154 static void gst_rtsp_stream_finalize (GObject * obj);
156 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
159 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
161 GObjectClass *gobject_class;
163 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
165 gobject_class = G_OBJECT_CLASS (klass);
167 gobject_class->get_property = gst_rtsp_stream_get_property;
168 gobject_class->set_property = gst_rtsp_stream_set_property;
169 gobject_class->finalize = gst_rtsp_stream_finalize;
171 g_object_class_install_property (gobject_class, PROP_CONTROL,
172 g_param_spec_string ("control", "Control",
173 "The control string for this stream", DEFAULT_CONTROL,
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
176 g_object_class_install_property (gobject_class, PROP_PROFILES,
177 g_param_spec_flags ("profiles", "Profiles",
178 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
179 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
182 g_param_spec_flags ("protocols", "Protocols",
183 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
184 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
188 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
192 gst_rtsp_stream_init (GstRTSPStream * stream)
194 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
196 GST_DEBUG ("new stream %p", stream);
201 priv->control = g_strdup (DEFAULT_CONTROL);
202 priv->profiles = DEFAULT_PROFILES;
203 priv->protocols = DEFAULT_PROTOCOLS;
205 g_mutex_init (&priv->lock);
209 gst_rtsp_stream_finalize (GObject * obj)
211 GstRTSPStream *stream;
212 GstRTSPStreamPrivate *priv;
214 stream = GST_RTSP_STREAM (obj);
217 GST_DEBUG ("finalize stream %p", stream);
219 /* we really need to be unjoined now */
220 g_return_if_fail (!priv->is_joined);
223 gst_rtsp_address_free (priv->addr_v4);
225 gst_rtsp_address_free (priv->addr_v6);
226 if (priv->server_addr_v4)
227 gst_rtsp_address_free (priv->server_addr_v4);
228 if (priv->server_addr_v6)
229 gst_rtsp_address_free (priv->server_addr_v6);
231 g_object_unref (priv->pool);
232 gst_object_unref (priv->payloader);
233 gst_object_unref (priv->srcpad);
234 g_free (priv->control);
235 g_mutex_clear (&priv->lock);
237 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
241 gst_rtsp_stream_get_property (GObject * object, guint propid,
242 GValue * value, GParamSpec * pspec)
244 GstRTSPStream *stream = GST_RTSP_STREAM (object);
248 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
251 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
254 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
262 gst_rtsp_stream_set_property (GObject * object, guint propid,
263 const GValue * value, GParamSpec * pspec)
265 GstRTSPStream *stream = GST_RTSP_STREAM (object);
269 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
272 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
275 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
278 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
283 * gst_rtsp_stream_new:
286 * @payloader: a #GstElement
288 * Create a new media stream with index @idx that handles RTP data on
289 * @srcpad and has a payloader element @payloader.
291 * Returns: a new #GstRTSPStream
294 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
296 GstRTSPStreamPrivate *priv;
297 GstRTSPStream *stream;
299 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
300 g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
301 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
303 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
306 priv->payloader = gst_object_ref (payloader);
307 priv->srcpad = gst_object_ref (srcpad);
313 * gst_rtsp_stream_get_index:
314 * @stream: a #GstRTSPStream
316 * Get the stream index.
318 * Return: the stream index.
321 gst_rtsp_stream_get_index (GstRTSPStream * stream)
323 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
325 return stream->priv->idx;
329 * gst_rtsp_stream_get_pt:
330 * @stream: a #GstRTSPStream
332 * Get the stream payload type.
334 * Return: the stream payload type.
337 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
339 GstRTSPStreamPrivate *priv;
342 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
346 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
352 * gst_rtsp_stream_get_srcpad:
353 * @stream: a #GstRTSPStream
355 * Get the srcpad associated with @stream.
357 * Returns: (transfer full): the srcpad. Unref after usage.
360 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
362 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
364 return gst_object_ref (stream->priv->srcpad);
368 * gst_rtsp_stream_get_control:
369 * @stream: a #GstRTSPStream
371 * Get the control string to identify this stream.
373 * Returns: (transfer full): the control string. free after usage.
376 gst_rtsp_stream_get_control (GstRTSPStream * stream)
378 GstRTSPStreamPrivate *priv;
381 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
385 g_mutex_lock (&priv->lock);
386 if ((result = g_strdup (priv->control)) == NULL)
387 result = g_strdup_printf ("stream=%u", priv->idx);
388 g_mutex_unlock (&priv->lock);
394 * gst_rtsp_stream_set_control:
395 * @stream: a #GstRTSPStream
396 * @control: a control string
398 * Set the control string in @stream.
401 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
403 GstRTSPStreamPrivate *priv;
405 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
409 g_mutex_lock (&priv->lock);
410 g_free (priv->control);
411 priv->control = g_strdup (control);
412 g_mutex_unlock (&priv->lock);
416 * gst_rtsp_stream_has_control:
417 * @stream: a #GstRTSPStream
418 * @control: a control string
420 * Check if @stream has the control string @control.
422 * Returns: %TRUE is @stream has @control as the control string
425 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
427 GstRTSPStreamPrivate *priv;
430 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
434 g_mutex_lock (&priv->lock);
436 res = (g_strcmp0 (priv->control, control) == 0);
440 if (sscanf (control, "stream=%u", &streamid) > 0)
441 res = (streamid == priv->idx);
445 g_mutex_unlock (&priv->lock);
451 * gst_rtsp_stream_set_mtu:
452 * @stream: a #GstRTSPStream
455 * Configure the mtu in the payloader of @stream to @mtu.
458 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
460 GstRTSPStreamPrivate *priv;
462 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
466 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
468 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
472 * gst_rtsp_stream_get_mtu:
473 * @stream: a #GstRTSPStream
475 * Get the configured MTU in the payloader of @stream.
477 * Returns: the MTU of the payloader.
480 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
482 GstRTSPStreamPrivate *priv;
485 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
489 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
494 /* Update the dscp qos property on the udp sinks */
496 update_dscp_qos (GstRTSPStream * stream)
498 GstRTSPStreamPrivate *priv;
500 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
504 if (priv->udpsink[0]) {
505 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
509 if (priv->udpsink[1]) {
510 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
516 * gst_rtsp_stream_set_dscp_qos:
517 * @stream: a #GstRTSPStream
518 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
520 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
523 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
525 GstRTSPStreamPrivate *priv;
527 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
531 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
533 if (dscp_qos < -1 || dscp_qos > 63) {
534 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
538 priv->dscp_qos = dscp_qos;
540 update_dscp_qos (stream);
544 * gst_rtsp_stream_get_dscp_qos:
545 * @stream: a #GstRTSPStream
547 * Get the configured DSCP QoS in of the outgoing sockets.
549 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
552 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
554 GstRTSPStreamPrivate *priv;
556 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
560 return priv->dscp_qos;
564 * gst_rtsp_stream_is_transport_supported:
565 * @stream: a #GstRTSPStream
566 * @transport: a #GstRTSPTransport
568 * Check if @transport can be handled by stream
570 * Returns: %TRUE if @transport can be handled by @stream.
573 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
574 GstRTSPTransport * transport)
576 GstRTSPStreamPrivate *priv;
578 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
582 g_mutex_lock (&priv->lock);
583 if (transport->trans != GST_RTSP_TRANS_RTP)
584 goto unsupported_transmode;
586 if (!(transport->profile & priv->profiles))
587 goto unsupported_profile;
589 if (!(transport->lower_transport & priv->protocols))
590 goto unsupported_ltrans;
592 g_mutex_unlock (&priv->lock);
597 unsupported_transmode:
599 GST_DEBUG ("unsupported transport mode %d", transport->trans);
604 GST_DEBUG ("unsupported profile %d", transport->profile);
609 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
615 * gst_rtsp_stream_set_profiles:
616 * @stream: a #GstRTSPStream
617 * @profiles: the new profiles
619 * Configure the allowed profiles for @stream.
622 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
624 GstRTSPStreamPrivate *priv;
626 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
630 g_mutex_lock (&priv->lock);
631 priv->profiles = profiles;
632 g_mutex_unlock (&priv->lock);
636 * gst_rtsp_stream_get_profiles:
637 * @stream: a #GstRTSPStream
639 * Get the allowed profiles of @stream.
641 * Returns: a #GstRTSPProfile
644 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
646 GstRTSPStreamPrivate *priv;
649 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
653 g_mutex_lock (&priv->lock);
654 res = priv->profiles;
655 g_mutex_unlock (&priv->lock);
661 * gst_rtsp_stream_set_protocols:
662 * @stream: a #GstRTSPStream
663 * @protocols: the new flags
665 * Configure the allowed lower transport for @stream.
668 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
669 GstRTSPLowerTrans protocols)
671 GstRTSPStreamPrivate *priv;
673 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
677 g_mutex_lock (&priv->lock);
678 priv->protocols = protocols;
679 g_mutex_unlock (&priv->lock);
683 * gst_rtsp_stream_get_protocols:
684 * @stream: a #GstRTSPStream
686 * Get the allowed protocols of @stream.
688 * Returns: a #GstRTSPLowerTrans
691 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
693 GstRTSPStreamPrivate *priv;
694 GstRTSPLowerTrans res;
696 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
697 GST_RTSP_LOWER_TRANS_UNKNOWN);
701 g_mutex_lock (&priv->lock);
702 res = priv->protocols;
703 g_mutex_unlock (&priv->lock);
709 * gst_rtsp_stream_set_address_pool:
710 * @stream: a #GstRTSPStream
711 * @pool: a #GstRTSPAddressPool
713 * configure @pool to be used as the address pool of @stream.
716 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
717 GstRTSPAddressPool * pool)
719 GstRTSPStreamPrivate *priv;
720 GstRTSPAddressPool *old;
722 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
726 GST_LOG_OBJECT (stream, "set address pool %p", pool);
728 g_mutex_lock (&priv->lock);
729 if ((old = priv->pool) != pool)
730 priv->pool = pool ? g_object_ref (pool) : NULL;
733 g_mutex_unlock (&priv->lock);
736 g_object_unref (old);
740 * gst_rtsp_stream_get_address_pool:
741 * @stream: a #GstRTSPStream
743 * Get the #GstRTSPAddressPool used as the address pool of @stream.
745 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
749 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
751 GstRTSPStreamPrivate *priv;
752 GstRTSPAddressPool *result;
754 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
758 g_mutex_lock (&priv->lock);
759 if ((result = priv->pool))
760 g_object_ref (result);
761 g_mutex_unlock (&priv->lock);
767 * gst_rtsp_stream_get_multicast_address:
768 * @stream: a #GstRTSPStream
769 * @family: the #GSocketFamily
771 * Get the multicast address of @stream for @family.
773 * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
774 * allocated. gst_rtsp_address_free() after usage.
777 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
778 GSocketFamily family)
780 GstRTSPStreamPrivate *priv;
781 GstRTSPAddress *result;
782 GstRTSPAddress **addrp;
783 GstRTSPAddressFlags flags;
785 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
789 if (family == G_SOCKET_FAMILY_IPV6) {
790 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
791 addrp = &priv->addr_v4;
793 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
794 addrp = &priv->addr_v6;
797 g_mutex_lock (&priv->lock);
798 if (*addrp == NULL) {
799 if (priv->pool == NULL)
802 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
804 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
808 result = gst_rtsp_address_copy (*addrp);
809 g_mutex_unlock (&priv->lock);
816 GST_ERROR_OBJECT (stream, "no address pool specified");
817 g_mutex_unlock (&priv->lock);
822 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
823 g_mutex_unlock (&priv->lock);
829 * gst_rtsp_stream_reserve_address:
830 * @stream: a #GstRTSPStream
831 * @address: an address
836 * Reserve @address and @port as the address and port of @stream.
838 * Returns: the #GstRTSPAddress of @stream or %NULL when the address could be
839 * reserved. gst_rtsp_address_free() after usage.
842 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
843 const gchar * address, guint port, guint n_ports, guint ttl)
845 GstRTSPStreamPrivate *priv;
846 GstRTSPAddress *result;
848 GSocketFamily family;
849 GstRTSPAddress **addrp;
851 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
852 g_return_val_if_fail (address != NULL, NULL);
853 g_return_val_if_fail (port > 0, NULL);
854 g_return_val_if_fail (n_ports > 0, NULL);
855 g_return_val_if_fail (ttl > 0, NULL);
859 addr = g_inet_address_new_from_string (address);
861 GST_ERROR ("failed to get inet addr from %s", address);
862 family = G_SOCKET_FAMILY_IPV4;
864 family = g_inet_address_get_family (addr);
865 g_object_unref (addr);
868 if (family == G_SOCKET_FAMILY_IPV6)
869 addrp = &priv->addr_v4;
871 addrp = &priv->addr_v6;
873 g_mutex_lock (&priv->lock);
874 if (*addrp == NULL) {
875 GstRTSPAddressPoolResult res;
877 if (priv->pool == NULL)
880 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
881 port, n_ports, ttl, addrp);
882 if (res != GST_RTSP_ADDRESS_POOL_OK)
885 if (strcmp ((*addrp)->address, address) ||
886 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
887 (*addrp)->ttl != ttl)
888 goto different_address;
890 result = gst_rtsp_address_copy (*addrp);
891 g_mutex_unlock (&priv->lock);
898 GST_ERROR_OBJECT (stream, "no address pool specified");
899 g_mutex_unlock (&priv->lock);
904 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
906 g_mutex_unlock (&priv->lock);
911 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
912 " reserved", address);
913 g_mutex_unlock (&priv->lock);
919 alloc_ports_one_family (GstRTSPAddressPool * pool, gint buffer_size,
920 GSocketFamily family, GstElement * udpsrc_out[2],
921 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
922 GstRTSPAddress ** server_addr_out)
924 GstStateChangeReturn ret;
925 GstElement *udpsrc0, *udpsrc1;
926 GstElement *udpsink0, *udpsink1;
927 GSocket *rtp_socket = NULL;
928 GSocket *rtcp_socket;
929 gint tmp_rtp, tmp_rtcp;
931 gint rtpport, rtcpport;
932 GList *rejected_addresses = NULL;
933 GstRTSPAddress *addr = NULL;
934 GInetAddress *inetaddr = NULL;
935 GSocketAddress *rtp_sockaddr = NULL;
936 GSocketAddress *rtcp_sockaddr = NULL;
937 const gchar *multisink_socket;
939 if (family == G_SOCKET_FAMILY_IPV6)
940 multisink_socket = "socket-v6";
942 multisink_socket = "socket";
950 /* Start with random port */
953 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
954 G_SOCKET_PROTOCOL_UDP, NULL);
956 goto no_udp_protocol;
958 if (*server_addr_out)
959 gst_rtsp_address_free (*server_addr_out);
961 /* try to allocate 2 UDP ports, the RTP port should be an even
962 * number and the RTCP port should be the next (uneven) port */
965 if (rtp_socket == NULL) {
966 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
967 G_SOCKET_PROTOCOL_UDP, NULL);
969 goto no_udp_protocol;
972 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
973 GstRTSPAddressFlags flags;
976 rejected_addresses = g_list_prepend (rejected_addresses, addr);
978 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
979 if (family == G_SOCKET_FAMILY_IPV6)
980 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
982 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
984 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
989 tmp_rtp = addr->port;
991 g_clear_object (&inetaddr);
992 inetaddr = g_inet_address_new_from_string (addr->address);
1000 if (inetaddr == NULL)
1001 inetaddr = g_inet_address_new_any (family);
1004 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1005 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1006 g_object_unref (rtp_sockaddr);
1009 g_object_unref (rtp_sockaddr);
1011 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1012 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1013 g_clear_object (&rtp_sockaddr);
1018 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1019 g_object_unref (rtp_sockaddr);
1021 /* check if port is even */
1022 if ((tmp_rtp & 1) != 0) {
1023 /* port not even, close and allocate another */
1025 g_clear_object (&rtp_socket);
1030 tmp_rtcp = tmp_rtp + 1;
1032 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1033 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1034 g_object_unref (rtcp_sockaddr);
1035 g_clear_object (&rtp_socket);
1038 g_object_unref (rtcp_sockaddr);
1040 g_clear_object (&inetaddr);
1042 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
1043 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
1045 if (udpsrc0 == NULL || udpsrc1 == NULL)
1046 goto no_udp_protocol;
1048 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
1049 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
1051 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1052 if (ret == GST_STATE_CHANGE_FAILURE)
1054 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1055 if (ret == GST_STATE_CHANGE_FAILURE)
1058 /* all fine, do port check */
1059 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
1060 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
1062 /* this should not happen... */
1063 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1067 udpsink0 = udpsink_out[0];
1069 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1072 goto no_udp_protocol;
1074 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1075 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
1078 udpsink1 = udpsink_out[1];
1080 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1083 goto no_udp_protocol;
1085 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1086 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1087 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
1089 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1090 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
1091 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1092 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1093 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1094 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1095 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1096 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1098 /* we keep these elements, we will further configure them when the
1099 * client told us to really use the UDP ports. */
1100 udpsrc_out[0] = udpsrc0;
1101 udpsrc_out[1] = udpsrc1;
1102 udpsink_out[0] = udpsink0;
1103 udpsink_out[1] = udpsink1;
1104 server_port_out->min = rtpport;
1105 server_port_out->max = rtcpport;
1107 *server_addr_out = addr;
1108 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1110 g_object_unref (rtp_socket);
1111 g_object_unref (rtcp_socket);
1139 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1140 gst_object_unref (udpsrc0);
1143 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1144 gst_object_unref (udpsrc1);
1147 gst_element_set_state (udpsink0, GST_STATE_NULL);
1148 gst_object_unref (udpsink0);
1151 g_object_unref (inetaddr);
1152 g_list_free_full (rejected_addresses,
1153 (GDestroyNotify) gst_rtsp_address_free);
1155 gst_rtsp_address_free (addr);
1157 g_object_unref (rtp_socket);
1159 g_object_unref (rtcp_socket);
1164 /* must be called with lock */
1166 alloc_ports (GstRTSPStream * stream)
1168 GstRTSPStreamPrivate *priv = stream->priv;
1170 priv->have_ipv4 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1171 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1172 &priv->server_port_v4, &priv->server_addr_v4);
1174 priv->have_ipv6 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1175 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1176 &priv->server_port_v6, &priv->server_addr_v6);
1178 return priv->have_ipv4 || priv->have_ipv6;
1182 * gst_rtsp_stream_get_server_port:
1183 * @stream: a #GstRTSPStream
1184 * @server_port: (out): result server port
1185 * @family: the port family to get
1187 * Fill @server_port with the port pair used by the server. This function can
1188 * only be called when @stream has been joined.
1191 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1192 GstRTSPRange * server_port, GSocketFamily family)
1194 GstRTSPStreamPrivate *priv;
1196 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1197 priv = stream->priv;
1198 g_return_if_fail (priv->is_joined);
1200 g_mutex_lock (&priv->lock);
1201 if (family == G_SOCKET_FAMILY_IPV4) {
1203 *server_port = priv->server_port_v4;
1206 *server_port = priv->server_port_v6;
1208 g_mutex_unlock (&priv->lock);
1212 * gst_rtsp_stream_get_rtpsession:
1213 * @stream: a #GstRTSPStream
1215 * Get the RTP session of this stream.
1217 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1220 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1222 GstRTSPStreamPrivate *priv;
1225 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1227 priv = stream->priv;
1229 g_mutex_lock (&priv->lock);
1230 if ((session = priv->session))
1231 g_object_ref (session);
1232 g_mutex_unlock (&priv->lock);
1238 * gst_rtsp_stream_get_ssrc:
1239 * @stream: a #GstRTSPStream
1240 * @ssrc: (out): result ssrc
1242 * Get the SSRC used by the RTP session of this stream. This function can only
1243 * be called when @stream has been joined.
1246 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1248 GstRTSPStreamPrivate *priv;
1250 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1251 priv = stream->priv;
1252 g_return_if_fail (priv->is_joined);
1254 g_mutex_lock (&priv->lock);
1255 if (ssrc && priv->session)
1256 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1257 g_mutex_unlock (&priv->lock);
1260 /* executed from streaming thread */
1262 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1264 GstRTSPStreamPrivate *priv = stream->priv;
1265 GstCaps *newcaps, *oldcaps;
1267 newcaps = gst_pad_get_current_caps (pad);
1269 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1272 g_mutex_lock (&priv->lock);
1273 oldcaps = priv->caps;
1274 priv->caps = newcaps;
1275 g_mutex_unlock (&priv->lock);
1278 gst_caps_unref (oldcaps);
1282 dump_structure (const GstStructure * s)
1286 sstr = gst_structure_to_string (s);
1287 GST_INFO ("structure: %s", sstr);
1291 static GstRTSPStreamTransport *
1292 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1294 GstRTSPStreamPrivate *priv = stream->priv;
1296 GstRTSPStreamTransport *result = NULL;
1301 if (rtcp_from == NULL)
1304 tmp = g_strrstr (rtcp_from, ":");
1308 port = atoi (tmp + 1);
1309 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1311 g_mutex_lock (&priv->lock);
1312 GST_INFO ("finding %s:%d in %d transports", dest, port,
1313 g_list_length (priv->transports));
1315 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1316 GstRTSPStreamTransport *trans = walk->data;
1317 const GstRTSPTransport *tr;
1320 tr = gst_rtsp_stream_transport_get_transport (trans);
1322 min = tr->client_port.min;
1323 max = tr->client_port.max;
1325 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1331 g_object_ref (result);
1332 g_mutex_unlock (&priv->lock);
1339 static GstRTSPStreamTransport *
1340 check_transport (GObject * source, GstRTSPStream * stream)
1342 GstStructure *stats;
1343 GstRTSPStreamTransport *trans;
1345 /* see if we have a stream to match with the origin of the RTCP packet */
1346 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1347 if (trans == NULL) {
1348 g_object_get (source, "stats", &stats, NULL);
1350 const gchar *rtcp_from;
1352 dump_structure (stats);
1354 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1355 if ((trans = find_transport (stream, rtcp_from))) {
1356 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1358 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1361 gst_structure_free (stats);
1369 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1371 GstRTSPStreamTransport *trans;
1373 GST_INFO ("%p: new source %p", stream, source);
1375 trans = check_transport (source, stream);
1378 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1382 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1384 GST_INFO ("%p: new SDES %p", stream, source);
1388 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1390 GstRTSPStreamTransport *trans;
1392 trans = check_transport (source, stream);
1395 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1396 gst_rtsp_stream_transport_keep_alive (trans);
1400 GstStructure *stats;
1401 g_object_get (source, "stats", &stats, NULL);
1403 dump_structure (stats);
1404 gst_structure_free (stats);
1411 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1413 GST_INFO ("%p: source %p bye", stream, source);
1417 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1419 GstRTSPStreamTransport *trans;
1421 GST_INFO ("%p: source %p bye timeout", stream, source);
1423 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1424 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1425 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1430 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1432 GstRTSPStreamTransport *trans;
1434 GST_INFO ("%p: source %p timeout", stream, source);
1436 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1437 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1438 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1442 static GstFlowReturn
1443 handle_new_sample (GstAppSink * sink, gpointer user_data)
1445 GstRTSPStreamPrivate *priv;
1449 GstRTSPStream *stream;
1451 sample = gst_app_sink_pull_sample (sink);
1455 stream = (GstRTSPStream *) user_data;
1456 priv = stream->priv;
1457 buffer = gst_sample_get_buffer (sample);
1459 g_mutex_lock (&priv->lock);
1460 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1461 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1463 if (GST_ELEMENT_CAST (sink) == priv->appsink[0]) {
1464 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1466 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1469 g_mutex_unlock (&priv->lock);
1471 gst_sample_unref (sample);
1476 static GstAppSinkCallbacks sink_cb = {
1477 NULL, /* not interested in EOS */
1478 NULL, /* not interested in preroll samples */
1483 * gst_rtsp_stream_join_bin:
1484 * @stream: a #GstRTSPStream
1485 * @bin: a #GstBin to join
1486 * @rtpbin: a rtpbin element in @bin
1487 * @state: the target state of the new elements
1489 * Join the #GstBin @bin that contains the element @rtpbin.
1491 * @stream will link to @rtpbin, which must be inside @bin. The elements
1492 * added to @bin will be set to the state given in @state.
1494 * Returns: %TRUE on success.
1497 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1498 GstElement * rtpbin, GstState state)
1500 GstRTSPStreamPrivate *priv;
1504 GstPad *pad, *sinkpad, *selpad;
1505 GstPadLinkReturn ret;
1507 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1508 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1509 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1511 priv = stream->priv;
1513 g_mutex_lock (&priv->lock);
1514 if (priv->is_joined)
1517 /* create a session with the same index as the stream */
1520 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1522 if (!alloc_ports (stream))
1525 /* update the dscp qos field in the sinks */
1526 update_dscp_qos (stream);
1528 /* get a pad for sending RTP */
1529 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1530 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
1532 /* link the RTP pad to the session manager, it should not really fail unless
1533 * this is not really an RTP pad */
1534 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
1535 if (ret != GST_PAD_LINK_OK)
1538 /* get pads from the RTP session element for sending and receiving
1540 name = g_strdup_printf ("send_rtp_src_%u", idx);
1541 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
1543 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1544 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
1546 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1547 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
1549 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1550 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
1553 /* get the session */
1554 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
1556 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1558 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1560 g_signal_connect (priv->session, "on-ssrc-active",
1561 (GCallback) on_ssrc_active, stream);
1562 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1564 g_signal_connect (priv->session, "on-bye-timeout",
1565 (GCallback) on_bye_timeout, stream);
1566 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1569 for (i = 0; i < 2; i++) {
1570 GstPad *teepad, *queuepad;
1571 /* For the sender we create this bit of pipeline for both
1572 * RTP and RTCP. Sync and preroll are enabled on udpsink so
1573 * we need to add a queue before appsink to make the pipeline
1574 * not block. For the TCP case, we want to pump data to the
1575 * client as fast as possible anyway.
1577 * .--------. .-----. .---------.
1578 * | rtpbin | | tee | | udpsink |
1579 * | send->sink src->sink |
1580 * '--------' | | '---------'
1581 * | | .---------. .---------.
1582 * | | | queue | | appsink |
1583 * | src->sink src->sink |
1584 * '-----' '---------' '---------'
1586 * When only UDP is allowed, we skip the tee, queue and appsink and link the
1587 * udpsink directly to the session.
1590 gst_bin_add (bin, priv->udpsink[i]);
1591 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1593 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1594 /* make tee for RTP/RTCP */
1595 priv->tee[i] = gst_element_factory_make ("tee", NULL);
1596 gst_bin_add (bin, priv->tee[i]);
1598 /* and link to rtpbin send pad */
1599 pad = gst_element_get_static_pad (priv->tee[i], "sink");
1600 gst_pad_link (priv->send_src[i], pad);
1601 gst_object_unref (pad);
1603 /* link tee to udpsink */
1604 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1605 gst_pad_link (teepad, sinkpad);
1606 gst_object_unref (teepad);
1609 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1610 gst_bin_add (bin, priv->appqueue[i]);
1611 /* and link to tee */
1612 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1613 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1614 gst_pad_link (teepad, pad);
1615 gst_object_unref (pad);
1616 gst_object_unref (teepad);
1619 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1620 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1621 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1622 gst_bin_add (bin, priv->appsink[i]);
1623 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1624 &sink_cb, stream, NULL);
1625 /* and link to queue */
1626 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1627 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1628 gst_pad_link (queuepad, pad);
1629 gst_object_unref (pad);
1630 gst_object_unref (queuepad);
1632 /* else only udpsink needed, link it to the session */
1633 gst_pad_link (priv->send_src[i], sinkpad);
1635 gst_object_unref (sinkpad);
1637 /* For the receiver we create this bit of pipeline for both
1638 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1639 * and it is all funneled into the rtpbin receive pad.
1641 * .--------. .--------. .--------.
1642 * | udpsrc | | funnel | | rtpbin |
1643 * | src->sink src->sink |
1644 * '--------' | | '--------'
1648 * '--------' '--------'
1650 /* make funnel for the RTP/RTCP receivers */
1651 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1652 gst_bin_add (bin, priv->funnel[i]);
1654 pad = gst_element_get_static_pad (priv->funnel[i], "src");
1655 gst_pad_link (pad, priv->recv_sink[i]);
1656 gst_object_unref (pad);
1658 if (priv->udpsrc_v4[i]) {
1659 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1661 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
1662 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
1664 gst_bin_add (bin, priv->udpsrc_v4[i]);
1666 /* and link to the funnel v4 */
1667 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1668 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
1669 gst_pad_link (pad, selpad);
1670 gst_object_unref (pad);
1671 gst_object_unref (selpad);
1674 if (priv->udpsrc_v6[i]) {
1675 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
1676 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
1677 gst_bin_add (bin, priv->udpsrc_v6[i]);
1679 /* and link to the funnel v6 */
1680 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1681 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
1682 gst_pad_link (pad, selpad);
1683 gst_object_unref (pad);
1684 gst_object_unref (selpad);
1687 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1688 /* make and add appsrc */
1689 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1690 gst_bin_add (bin, priv->appsrc[i]);
1691 /* and link to the funnel */
1692 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1693 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
1694 gst_pad_link (pad, selpad);
1695 gst_object_unref (pad);
1696 gst_object_unref (selpad);
1699 /* check if we need to set to a special state */
1700 if (state != GST_STATE_NULL) {
1701 if (priv->udpsink[i])
1702 gst_element_set_state (priv->udpsink[i], state);
1703 if (priv->appsink[i])
1704 gst_element_set_state (priv->appsink[i], state);
1705 if (priv->appqueue[i])
1706 gst_element_set_state (priv->appqueue[i], state);
1708 gst_element_set_state (priv->tee[i], state);
1709 if (priv->funnel[i])
1710 gst_element_set_state (priv->funnel[i], state);
1711 if (priv->appsrc[i])
1712 gst_element_set_state (priv->appsrc[i], state);
1716 /* be notified of caps changes */
1717 priv->caps_sig = g_signal_connect (priv->send_rtp_sink, "notify::caps",
1718 (GCallback) caps_notify, stream);
1720 priv->is_joined = TRUE;
1721 g_mutex_unlock (&priv->lock);
1728 g_mutex_unlock (&priv->lock);
1733 g_mutex_unlock (&priv->lock);
1734 GST_WARNING ("failed to allocate ports %u", idx);
1739 GST_WARNING ("failed to link stream %u", idx);
1740 gst_object_unref (priv->send_rtp_sink);
1741 priv->send_rtp_sink = NULL;
1742 g_mutex_unlock (&priv->lock);
1748 * gst_rtsp_stream_leave_bin:
1749 * @stream: a #GstRTSPStream
1751 * @rtpbin: a rtpbin #GstElement
1753 * Remove the elements of @stream from @bin.
1755 * Return: %TRUE on success.
1758 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1759 GstElement * rtpbin)
1761 GstRTSPStreamPrivate *priv;
1764 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1765 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1766 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1768 priv = stream->priv;
1770 g_mutex_lock (&priv->lock);
1771 if (!priv->is_joined)
1772 goto was_not_joined;
1774 /* all transports must be removed by now */
1775 g_return_val_if_fail (priv->transports == NULL, FALSE);
1777 GST_INFO ("stream %p leaving bin", stream);
1779 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
1780 g_signal_handler_disconnect (priv->send_rtp_sink, priv->caps_sig);
1781 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
1782 gst_object_unref (priv->send_rtp_sink);
1783 priv->send_rtp_sink = NULL;
1785 for (i = 0; i < 2; i++) {
1786 if (priv->udpsink[i])
1787 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
1788 if (priv->appsink[i])
1789 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
1790 if (priv->appqueue[i])
1791 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
1793 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
1794 if (priv->funnel[i])
1795 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
1796 if (priv->appsrc[i])
1797 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
1798 if (priv->udpsrc_v4[i]) {
1799 /* and set udpsrc to NULL now before removing */
1800 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
1801 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
1802 /* removing them should also nicely release the request
1803 * pads when they finalize */
1804 gst_bin_remove (bin, priv->udpsrc_v4[i]);
1806 if (priv->udpsrc_v6[i]) {
1807 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
1808 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
1809 gst_bin_remove (bin, priv->udpsrc_v6[i]);
1811 if (priv->udpsink[i])
1812 gst_bin_remove (bin, priv->udpsink[i]);
1813 if (priv->appsrc[i])
1814 gst_bin_remove (bin, priv->appsrc[i]);
1815 if (priv->appsink[i])
1816 gst_bin_remove (bin, priv->appsink[i]);
1817 if (priv->appqueue[i])
1818 gst_bin_remove (bin, priv->appqueue[i]);
1820 gst_bin_remove (bin, priv->tee[i]);
1821 if (priv->funnel[i])
1822 gst_bin_remove (bin, priv->funnel[i]);
1824 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
1825 gst_object_unref (priv->recv_sink[i]);
1826 priv->recv_sink[i] = NULL;
1828 priv->udpsrc_v4[i] = NULL;
1829 priv->udpsrc_v6[i] = NULL;
1830 priv->udpsink[i] = NULL;
1831 priv->appsrc[i] = NULL;
1832 priv->appsink[i] = NULL;
1833 priv->appqueue[i] = NULL;
1834 priv->tee[i] = NULL;
1835 priv->funnel[i] = NULL;
1837 gst_object_unref (priv->send_src[0]);
1838 priv->send_src[0] = NULL;
1840 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
1841 gst_object_unref (priv->send_src[1]);
1842 priv->send_src[1] = NULL;
1844 g_object_unref (priv->session);
1845 priv->session = NULL;
1847 gst_caps_unref (priv->caps);
1850 priv->is_joined = FALSE;
1851 g_mutex_unlock (&priv->lock);
1862 * gst_rtsp_stream_get_rtpinfo:
1863 * @stream: a #GstRTSPStream
1864 * @rtptime: (allow-none): result RTP timestamp
1865 * @seq: (allow-none): result RTP seqnum
1866 * @clock_rate: the clock rate
1867 * @running_time: (allow-none): result running-time
1869 * Retrieve the current rtptime, seq and running-time. This is used to
1870 * construct a RTPInfo reply header.
1872 * Returns: %TRUE when rtptime, seq and running-time could be determined.
1875 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
1876 guint * rtptime, guint * seq, guint * clock_rate,
1877 GstClockTime * running_time)
1879 GstRTSPStreamPrivate *priv;
1880 GstStructure *stats;
1881 GObjectClass *payobjclass;
1883 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1885 priv = stream->priv;
1887 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
1889 g_mutex_lock (&priv->lock);
1891 if (g_object_class_find_property (payobjclass, "stats")) {
1892 g_object_get (priv->payloader, "stats", &stats, NULL);
1897 gst_structure_get_uint (stats, "seqnum", seq);
1900 gst_structure_get_uint (stats, "timestamp", rtptime);
1903 gst_structure_get_clock_time (stats, "running-time", running_time);
1906 gst_structure_get_uint (stats, "clock-rate", clock_rate);
1907 if (*clock_rate == 0 && running_time)
1908 *running_time = GST_CLOCK_TIME_NONE;
1910 gst_structure_free (stats);
1912 if (!g_object_class_find_property (payobjclass, "seqnum") ||
1913 !g_object_class_find_property (payobjclass, "timestamp"))
1917 g_object_get (priv->payloader, "seqnum", seq, NULL);
1920 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
1923 *running_time = GST_CLOCK_TIME_NONE;
1925 g_mutex_unlock (&priv->lock);
1932 GST_WARNING ("Could not get payloader stats");
1933 g_mutex_unlock (&priv->lock);
1939 * gst_rtsp_stream_get_caps:
1940 * @stream: a #GstRTSPStream
1942 * Retrieve the current caps of @stream.
1944 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
1948 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
1950 GstRTSPStreamPrivate *priv;
1953 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1955 priv = stream->priv;
1957 g_mutex_lock (&priv->lock);
1958 if ((result = priv->caps))
1959 gst_caps_ref (result);
1960 g_mutex_unlock (&priv->lock);
1966 * gst_rtsp_stream_recv_rtp:
1967 * @stream: a #GstRTSPStream
1968 * @buffer: (transfer full): a #GstBuffer
1970 * Handle an RTP buffer for the stream. This method is usually called when a
1971 * message has been received from a client using the TCP transport.
1973 * This function takes ownership of @buffer.
1975 * Returns: a GstFlowReturn.
1978 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
1980 GstRTSPStreamPrivate *priv;
1982 GstElement *element;
1984 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1985 priv = stream->priv;
1986 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1987 g_return_val_if_fail (priv->is_joined, FALSE);
1989 g_mutex_lock (&priv->lock);
1990 if (priv->appsrc[0])
1991 element = gst_object_ref (priv->appsrc[0]);
1994 g_mutex_unlock (&priv->lock);
1997 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1998 gst_object_unref (element);
2006 * gst_rtsp_stream_recv_rtcp:
2007 * @stream: a #GstRTSPStream
2008 * @buffer: (transfer full): a #GstBuffer
2010 * Handle an RTCP buffer for the stream. This method is usually called when a
2011 * message has been received from a client using the TCP transport.
2013 * This function takes ownership of @buffer.
2015 * Returns: a GstFlowReturn.
2018 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
2020 GstRTSPStreamPrivate *priv;
2022 GstElement *element;
2024 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2025 priv = stream->priv;
2026 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2027 g_return_val_if_fail (priv->is_joined, FALSE);
2029 g_mutex_lock (&priv->lock);
2030 if (priv->appsrc[1])
2031 element = gst_object_ref (priv->appsrc[1]);
2034 g_mutex_unlock (&priv->lock);
2037 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2038 gst_object_unref (element);
2045 /* must be called with lock */
2047 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2050 GstRTSPStreamPrivate *priv = stream->priv;
2051 const GstRTSPTransport *tr;
2053 tr = gst_rtsp_stream_transport_get_transport (trans);
2055 switch (tr->lower_transport) {
2056 case GST_RTSP_LOWER_TRANS_UDP:
2057 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2063 dest = tr->destination;
2064 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2069 min = tr->client_port.min;
2070 max = tr->client_port.max;
2075 GST_INFO ("setting ttl-mc %d", ttl);
2076 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
2077 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
2079 GST_INFO ("adding %s:%d-%d", dest, min, max);
2080 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
2081 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
2082 priv->transports = g_list_prepend (priv->transports, trans);
2084 GST_INFO ("removing %s:%d-%d", dest, min, max);
2085 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
2086 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
2087 priv->transports = g_list_remove (priv->transports, trans);
2091 case GST_RTSP_LOWER_TRANS_TCP:
2093 GST_INFO ("adding TCP %s", tr->destination);
2094 priv->transports = g_list_prepend (priv->transports, trans);
2096 GST_INFO ("removing TCP %s", tr->destination);
2097 priv->transports = g_list_remove (priv->transports, trans);
2101 goto unknown_transport;
2108 GST_INFO ("Unknown transport %d", tr->lower_transport);
2115 * gst_rtsp_stream_add_transport:
2116 * @stream: a #GstRTSPStream
2117 * @trans: a #GstRTSPStreamTransport
2119 * Add the transport in @trans to @stream. The media of @stream will
2120 * then also be send to the values configured in @trans.
2122 * @stream must be joined to a bin.
2124 * @trans must contain a valid #GstRTSPTransport.
2126 * Returns: %TRUE if @trans was added
2129 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
2130 GstRTSPStreamTransport * trans)
2132 GstRTSPStreamPrivate *priv;
2135 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2136 priv = stream->priv;
2137 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2138 g_return_val_if_fail (priv->is_joined, FALSE);
2140 g_mutex_lock (&priv->lock);
2141 res = update_transport (stream, trans, TRUE);
2142 g_mutex_unlock (&priv->lock);
2148 * gst_rtsp_stream_remove_transport:
2149 * @stream: a #GstRTSPStream
2150 * @trans: a #GstRTSPStreamTransport
2152 * Remove the transport in @trans from @stream. The media of @stream will
2153 * not be sent to the values configured in @trans.
2155 * @stream must be joined to a bin.
2157 * @trans must contain a valid #GstRTSPTransport.
2159 * Returns: %TRUE if @trans was removed
2162 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2163 GstRTSPStreamTransport * trans)
2165 GstRTSPStreamPrivate *priv;
2168 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2169 priv = stream->priv;
2170 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2171 g_return_val_if_fail (priv->is_joined, FALSE);
2173 g_mutex_lock (&priv->lock);
2174 res = update_transport (stream, trans, FALSE);
2175 g_mutex_unlock (&priv->lock);
2181 * gst_rtsp_stream_get_rtp_socket:
2182 * @stream: a #GstRTSPStream
2183 * @family: the socket family
2185 * Get the RTP socket from @stream for a @family.
2187 * @stream must be joined to a bin.
2189 * Returns: (transfer full): the RTP socket or %NULL if no socket could be
2190 * allocated for @family. Unref after usage
2193 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2195 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2199 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2200 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2201 family == G_SOCKET_FAMILY_IPV6, NULL);
2202 g_return_val_if_fail (priv->udpsink[0], NULL);
2204 if (family == G_SOCKET_FAMILY_IPV6)
2209 g_object_get (priv->udpsink[0], name, &socket, NULL);
2215 * gst_rtsp_stream_get_rtcp_socket:
2216 * @stream: a #GstRTSPStream
2217 * @family: the socket family
2219 * Get the RTCP socket from @stream for a @family.
2221 * @stream must be joined to a bin.
2223 * Returns: (transfer full): the RTCP socket or %NULL if no socket could be
2224 * allocated for @family. Unref after usage
2227 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
2229 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2233 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2234 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2235 family == G_SOCKET_FAMILY_IPV6, NULL);
2236 g_return_val_if_fail (priv->udpsink[1], NULL);
2238 if (family == G_SOCKET_FAMILY_IPV6)
2243 g_object_get (priv->udpsink[1], name, &socket, NULL);
2249 * gst_rtsp_stream_transport_filter:
2250 * @stream: a #GstRTSPStream
2251 * @func: (scope call) (allow-none): a callback
2252 * @user_data: user data passed to @func
2254 * Call @func for each transport managed by @stream. The result value of @func
2255 * determines what happens to the transport. @func will be called with @stream
2256 * locked so no further actions on @stream can be performed from @func.
2258 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
2261 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
2263 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
2264 * will also be added with an additional ref to the result #GList of this
2267 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
2269 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
2270 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2271 * element in the #GList should be unreffed before the list is freed.
2274 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
2275 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
2277 GstRTSPStreamPrivate *priv;
2278 GList *result, *walk, *next;
2280 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2282 priv = stream->priv;
2286 g_mutex_lock (&priv->lock);
2287 for (walk = priv->transports; walk; walk = next) {
2288 GstRTSPStreamTransport *trans = walk->data;
2289 GstRTSPFilterResult res;
2291 next = g_list_next (walk);
2294 res = func (stream, trans, user_data);
2296 res = GST_RTSP_FILTER_REF;
2299 case GST_RTSP_FILTER_REMOVE:
2300 update_transport (stream, trans, FALSE);
2302 case GST_RTSP_FILTER_REF:
2303 result = g_list_prepend (result, g_object_ref (trans));
2305 case GST_RTSP_FILTER_KEEP:
2310 g_mutex_unlock (&priv->lock);
2315 static GstPadProbeReturn
2316 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2318 GstRTSPStreamPrivate *priv;
2319 GstRTSPStream *stream;
2322 priv = stream->priv;
2324 GST_DEBUG_OBJECT (pad, "now blocking");
2326 g_mutex_lock (&priv->lock);
2327 priv->blocking = TRUE;
2328 g_mutex_unlock (&priv->lock);
2330 gst_element_post_message (priv->payloader,
2331 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
2332 gst_structure_new_empty ("GstRTSPStreamBlocking")));
2334 return GST_PAD_PROBE_OK;
2338 * gst_rtsp_stream_set_blocked:
2339 * @stream: a #GstRTSPStream
2340 * @blocked: boolean indicating we should block or unblock
2342 * Blocks or unblocks the dataflow on @stream.
2344 * Returns: %TRUE on success
2347 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
2349 GstRTSPStreamPrivate *priv;
2351 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2353 priv = stream->priv;
2355 g_mutex_lock (&priv->lock);
2357 priv->blocking = FALSE;
2358 if (priv->blocked_id == 0) {
2359 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
2360 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
2361 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
2362 g_object_ref (stream), g_object_unref);
2365 if (priv->blocked_id != 0) {
2366 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
2367 priv->blocked_id = 0;
2368 priv->blocking = FALSE;
2371 g_mutex_unlock (&priv->lock);
2377 * gst_rtsp_stream_is_blocking:
2378 * @stream: a #GstRTSPStream
2380 * Check if @stream is blocking on a #GstBuffer.
2382 * Returns: %TRUE if @stream is blocking
2385 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
2387 GstRTSPStreamPrivate *priv;
2390 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2392 priv = stream->priv;
2394 g_mutex_lock (&priv->lock);
2395 result = priv->blocking;
2396 g_mutex_unlock (&priv->lock);