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 #ifdef GST_TYPE_RTSP_PROFILE
177 g_object_class_install_property (gobject_class, PROP_PROFILES,
178 g_param_spec_flags ("profiles", "Profiles",
179 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
180 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
184 g_param_spec_flags ("protocols", "Protocols",
185 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
186 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
190 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
194 gst_rtsp_stream_init (GstRTSPStream * stream)
196 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
198 GST_DEBUG ("new stream %p", stream);
203 priv->control = g_strdup (DEFAULT_CONTROL);
204 priv->profiles = DEFAULT_PROFILES;
205 priv->protocols = DEFAULT_PROTOCOLS;
207 g_mutex_init (&priv->lock);
211 gst_rtsp_stream_finalize (GObject * obj)
213 GstRTSPStream *stream;
214 GstRTSPStreamPrivate *priv;
216 stream = GST_RTSP_STREAM (obj);
219 GST_DEBUG ("finalize stream %p", stream);
221 /* we really need to be unjoined now */
222 g_return_if_fail (!priv->is_joined);
225 gst_rtsp_address_free (priv->addr_v4);
227 gst_rtsp_address_free (priv->addr_v6);
228 if (priv->server_addr_v4)
229 gst_rtsp_address_free (priv->server_addr_v4);
230 if (priv->server_addr_v6)
231 gst_rtsp_address_free (priv->server_addr_v6);
233 g_object_unref (priv->pool);
234 gst_object_unref (priv->payloader);
235 gst_object_unref (priv->srcpad);
236 g_free (priv->control);
237 g_mutex_clear (&priv->lock);
239 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
243 gst_rtsp_stream_get_property (GObject * object, guint propid,
244 GValue * value, GParamSpec * pspec)
246 GstRTSPStream *stream = GST_RTSP_STREAM (object);
250 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
253 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
256 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
264 gst_rtsp_stream_set_property (GObject * object, guint propid,
265 const GValue * value, GParamSpec * pspec)
267 GstRTSPStream *stream = GST_RTSP_STREAM (object);
271 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
274 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
277 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
280 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
285 * gst_rtsp_stream_new:
288 * @payloader: a #GstElement
290 * Create a new media stream with index @idx that handles RTP data on
291 * @srcpad and has a payloader element @payloader.
293 * Returns: a new #GstRTSPStream
296 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
298 GstRTSPStreamPrivate *priv;
299 GstRTSPStream *stream;
301 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
302 g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
303 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
305 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
308 priv->payloader = gst_object_ref (payloader);
309 priv->srcpad = gst_object_ref (srcpad);
315 * gst_rtsp_stream_get_index:
316 * @stream: a #GstRTSPStream
318 * Get the stream index.
320 * Return: the stream index.
323 gst_rtsp_stream_get_index (GstRTSPStream * stream)
325 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
327 return stream->priv->idx;
331 * gst_rtsp_stream_get_pt:
332 * @stream: a #GstRTSPStream
334 * Get the stream payload type.
336 * Return: the stream payload type.
339 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
341 GstRTSPStreamPrivate *priv;
344 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
348 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
354 * gst_rtsp_stream_get_srcpad:
355 * @stream: a #GstRTSPStream
357 * Get the srcpad associated with @stream.
359 * Returns: (transfer full): the srcpad. Unref after usage.
362 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
364 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
366 return gst_object_ref (stream->priv->srcpad);
370 * gst_rtsp_stream_get_control:
371 * @stream: a #GstRTSPStream
373 * Get the control string to identify this stream.
375 * Returns: (transfer full): the control string. free after usage.
378 gst_rtsp_stream_get_control (GstRTSPStream * stream)
380 GstRTSPStreamPrivate *priv;
383 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
387 g_mutex_lock (&priv->lock);
388 if ((result = g_strdup (priv->control)) == NULL)
389 result = g_strdup_printf ("stream=%u", priv->idx);
390 g_mutex_unlock (&priv->lock);
396 * gst_rtsp_stream_set_control:
397 * @stream: a #GstRTSPStream
398 * @control: a control string
400 * Set the control string in @stream.
403 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
405 GstRTSPStreamPrivate *priv;
407 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
411 g_mutex_lock (&priv->lock);
412 g_free (priv->control);
413 priv->control = g_strdup (control);
414 g_mutex_unlock (&priv->lock);
418 * gst_rtsp_stream_has_control:
419 * @stream: a #GstRTSPStream
420 * @control: a control string
422 * Check if @stream has the control string @control.
424 * Returns: %TRUE is @stream has @control as the control string
427 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
429 GstRTSPStreamPrivate *priv;
432 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
436 g_mutex_lock (&priv->lock);
438 res = (g_strcmp0 (priv->control, control) == 0);
442 if (sscanf (control, "stream=%u", &streamid) > 0)
443 res = (streamid == priv->idx);
447 g_mutex_unlock (&priv->lock);
453 * gst_rtsp_stream_set_mtu:
454 * @stream: a #GstRTSPStream
457 * Configure the mtu in the payloader of @stream to @mtu.
460 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
462 GstRTSPStreamPrivate *priv;
464 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
468 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
470 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
474 * gst_rtsp_stream_get_mtu:
475 * @stream: a #GstRTSPStream
477 * Get the configured MTU in the payloader of @stream.
479 * Returns: the MTU of the payloader.
482 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
484 GstRTSPStreamPrivate *priv;
487 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
491 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
496 /* Update the dscp qos property on the udp sinks */
498 update_dscp_qos (GstRTSPStream * stream)
500 GstRTSPStreamPrivate *priv;
502 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
506 if (priv->udpsink[0]) {
507 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
511 if (priv->udpsink[1]) {
512 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
518 * gst_rtsp_stream_set_dscp_qos:
519 * @stream: a #GstRTSPStream
520 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
522 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
525 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
527 GstRTSPStreamPrivate *priv;
529 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
533 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
535 if (dscp_qos < -1 || dscp_qos > 63) {
536 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
540 priv->dscp_qos = dscp_qos;
542 update_dscp_qos (stream);
546 * gst_rtsp_stream_get_dscp_qos:
547 * @stream: a #GstRTSPStream
549 * Get the configured DSCP QoS in of the outgoing sockets.
551 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
554 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
556 GstRTSPStreamPrivate *priv;
558 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
562 return priv->dscp_qos;
566 * gst_rtsp_stream_is_transport_supported:
567 * @stream: a #GstRTSPStream
568 * @transport: a #GstRTSPTransport
570 * Check if @transport can be handled by stream
572 * Returns: %TRUE if @transport can be handled by @stream.
575 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
576 GstRTSPTransport * transport)
578 GstRTSPStreamPrivate *priv;
580 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
584 g_mutex_lock (&priv->lock);
585 if (transport->trans != GST_RTSP_TRANS_RTP)
586 goto unsupported_transmode;
588 if (!(transport->profile & priv->profiles))
589 goto unsupported_profile;
591 if (!(transport->lower_transport & priv->protocols))
592 goto unsupported_ltrans;
594 g_mutex_unlock (&priv->lock);
599 unsupported_transmode:
601 GST_DEBUG ("unsupported transport mode %d", transport->trans);
606 GST_DEBUG ("unsupported profile %d", transport->profile);
611 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
617 * gst_rtsp_stream_set_profiles:
618 * @stream: a #GstRTSPStream
619 * @profiles: the new profiles
621 * Configure the allowed profiles for @stream.
624 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
626 GstRTSPStreamPrivate *priv;
628 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
632 g_mutex_lock (&priv->lock);
633 priv->profiles = profiles;
634 g_mutex_unlock (&priv->lock);
638 * gst_rtsp_stream_get_profiles:
639 * @stream: a #GstRTSPStream
641 * Get the allowed profiles of @stream.
643 * Returns: a #GstRTSPProfile
646 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
648 GstRTSPStreamPrivate *priv;
651 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
655 g_mutex_lock (&priv->lock);
656 res = priv->profiles;
657 g_mutex_unlock (&priv->lock);
663 * gst_rtsp_stream_set_protocols:
664 * @stream: a #GstRTSPStream
665 * @protocols: the new flags
667 * Configure the allowed lower transport for @stream.
670 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
671 GstRTSPLowerTrans protocols)
673 GstRTSPStreamPrivate *priv;
675 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
679 g_mutex_lock (&priv->lock);
680 priv->protocols = protocols;
681 g_mutex_unlock (&priv->lock);
685 * gst_rtsp_stream_get_protocols:
686 * @stream: a #GstRTSPStream
688 * Get the allowed protocols of @stream.
690 * Returns: a #GstRTSPLowerTrans
693 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
695 GstRTSPStreamPrivate *priv;
696 GstRTSPLowerTrans res;
698 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
699 GST_RTSP_LOWER_TRANS_UNKNOWN);
703 g_mutex_lock (&priv->lock);
704 res = priv->protocols;
705 g_mutex_unlock (&priv->lock);
711 * gst_rtsp_stream_set_address_pool:
712 * @stream: a #GstRTSPStream
713 * @pool: a #GstRTSPAddressPool
715 * configure @pool to be used as the address pool of @stream.
718 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
719 GstRTSPAddressPool * pool)
721 GstRTSPStreamPrivate *priv;
722 GstRTSPAddressPool *old;
724 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
728 GST_LOG_OBJECT (stream, "set address pool %p", pool);
730 g_mutex_lock (&priv->lock);
731 if ((old = priv->pool) != pool)
732 priv->pool = pool ? g_object_ref (pool) : NULL;
735 g_mutex_unlock (&priv->lock);
738 g_object_unref (old);
742 * gst_rtsp_stream_get_address_pool:
743 * @stream: a #GstRTSPStream
745 * Get the #GstRTSPAddressPool used as the address pool of @stream.
747 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
751 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
753 GstRTSPStreamPrivate *priv;
754 GstRTSPAddressPool *result;
756 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
760 g_mutex_lock (&priv->lock);
761 if ((result = priv->pool))
762 g_object_ref (result);
763 g_mutex_unlock (&priv->lock);
769 * gst_rtsp_stream_get_multicast_address:
770 * @stream: a #GstRTSPStream
771 * @family: the #GSocketFamily
773 * Get the multicast address of @stream for @family.
775 * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
776 * allocated. gst_rtsp_address_free() after usage.
779 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
780 GSocketFamily family)
782 GstRTSPStreamPrivate *priv;
783 GstRTSPAddress *result;
784 GstRTSPAddress **addrp;
785 GstRTSPAddressFlags flags;
787 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
791 if (family == G_SOCKET_FAMILY_IPV6) {
792 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
793 addrp = &priv->addr_v4;
795 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
796 addrp = &priv->addr_v6;
799 g_mutex_lock (&priv->lock);
800 if (*addrp == NULL) {
801 if (priv->pool == NULL)
804 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
806 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
810 result = gst_rtsp_address_copy (*addrp);
811 g_mutex_unlock (&priv->lock);
818 GST_ERROR_OBJECT (stream, "no address pool specified");
819 g_mutex_unlock (&priv->lock);
824 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
825 g_mutex_unlock (&priv->lock);
831 * gst_rtsp_stream_reserve_address:
832 * @stream: a #GstRTSPStream
833 * @address: an address
838 * Reserve @address and @port as the address and port of @stream.
840 * Returns: the #GstRTSPAddress of @stream or %NULL when the address could be
841 * reserved. gst_rtsp_address_free() after usage.
844 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
845 const gchar * address, guint port, guint n_ports, guint ttl)
847 GstRTSPStreamPrivate *priv;
848 GstRTSPAddress *result;
850 GSocketFamily family;
851 GstRTSPAddress **addrp;
853 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
854 g_return_val_if_fail (address != NULL, NULL);
855 g_return_val_if_fail (port > 0, NULL);
856 g_return_val_if_fail (n_ports > 0, NULL);
857 g_return_val_if_fail (ttl > 0, NULL);
861 addr = g_inet_address_new_from_string (address);
863 GST_ERROR ("failed to get inet addr from %s", address);
864 family = G_SOCKET_FAMILY_IPV4;
866 family = g_inet_address_get_family (addr);
867 g_object_unref (addr);
870 if (family == G_SOCKET_FAMILY_IPV6)
871 addrp = &priv->addr_v4;
873 addrp = &priv->addr_v6;
875 g_mutex_lock (&priv->lock);
876 if (*addrp == NULL) {
877 GstRTSPAddressPoolResult res;
879 if (priv->pool == NULL)
882 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
883 port, n_ports, ttl, addrp);
884 if (res != GST_RTSP_ADDRESS_POOL_OK)
887 if (strcmp ((*addrp)->address, address) ||
888 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
889 (*addrp)->ttl != ttl)
890 goto different_address;
892 result = gst_rtsp_address_copy (*addrp);
893 g_mutex_unlock (&priv->lock);
900 GST_ERROR_OBJECT (stream, "no address pool specified");
901 g_mutex_unlock (&priv->lock);
906 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
908 g_mutex_unlock (&priv->lock);
913 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
914 " reserved", address);
915 g_mutex_unlock (&priv->lock);
921 alloc_ports_one_family (GstRTSPAddressPool * pool, gint buffer_size,
922 GSocketFamily family, GstElement * udpsrc_out[2],
923 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
924 GstRTSPAddress ** server_addr_out)
926 GstStateChangeReturn ret;
927 GstElement *udpsrc0, *udpsrc1;
928 GstElement *udpsink0, *udpsink1;
929 GSocket *rtp_socket = NULL;
930 GSocket *rtcp_socket;
931 gint tmp_rtp, tmp_rtcp;
933 gint rtpport, rtcpport;
934 GList *rejected_addresses = NULL;
935 GstRTSPAddress *addr = NULL;
936 GInetAddress *inetaddr = NULL;
937 GSocketAddress *rtp_sockaddr = NULL;
938 GSocketAddress *rtcp_sockaddr = NULL;
939 const gchar *multisink_socket;
941 if (family == G_SOCKET_FAMILY_IPV6)
942 multisink_socket = "socket-v6";
944 multisink_socket = "socket";
952 /* Start with random port */
955 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
956 G_SOCKET_PROTOCOL_UDP, NULL);
958 goto no_udp_protocol;
960 if (*server_addr_out)
961 gst_rtsp_address_free (*server_addr_out);
963 /* try to allocate 2 UDP ports, the RTP port should be an even
964 * number and the RTCP port should be the next (uneven) port */
967 if (rtp_socket == NULL) {
968 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
969 G_SOCKET_PROTOCOL_UDP, NULL);
971 goto no_udp_protocol;
974 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
975 GstRTSPAddressFlags flags;
978 rejected_addresses = g_list_prepend (rejected_addresses, addr);
980 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
981 if (family == G_SOCKET_FAMILY_IPV6)
982 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
984 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
986 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
991 tmp_rtp = addr->port;
993 g_clear_object (&inetaddr);
994 inetaddr = g_inet_address_new_from_string (addr->address);
1002 if (inetaddr == NULL)
1003 inetaddr = g_inet_address_new_any (family);
1006 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1007 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1008 g_object_unref (rtp_sockaddr);
1011 g_object_unref (rtp_sockaddr);
1013 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1014 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1015 g_clear_object (&rtp_sockaddr);
1020 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1021 g_object_unref (rtp_sockaddr);
1023 /* check if port is even */
1024 if ((tmp_rtp & 1) != 0) {
1025 /* port not even, close and allocate another */
1027 g_clear_object (&rtp_socket);
1032 tmp_rtcp = tmp_rtp + 1;
1034 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1035 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1036 g_object_unref (rtcp_sockaddr);
1037 g_clear_object (&rtp_socket);
1040 g_object_unref (rtcp_sockaddr);
1042 g_clear_object (&inetaddr);
1044 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
1045 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
1047 if (udpsrc0 == NULL || udpsrc1 == NULL)
1048 goto no_udp_protocol;
1050 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
1051 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
1053 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1054 if (ret == GST_STATE_CHANGE_FAILURE)
1056 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1057 if (ret == GST_STATE_CHANGE_FAILURE)
1060 /* all fine, do port check */
1061 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
1062 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
1064 /* this should not happen... */
1065 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1069 udpsink0 = udpsink_out[0];
1071 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1074 goto no_udp_protocol;
1076 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1077 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
1080 udpsink1 = udpsink_out[1];
1082 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1085 goto no_udp_protocol;
1087 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1088 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1089 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
1091 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1092 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
1093 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1094 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1095 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1096 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1097 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1098 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1100 /* we keep these elements, we will further configure them when the
1101 * client told us to really use the UDP ports. */
1102 udpsrc_out[0] = udpsrc0;
1103 udpsrc_out[1] = udpsrc1;
1104 udpsink_out[0] = udpsink0;
1105 udpsink_out[1] = udpsink1;
1106 server_port_out->min = rtpport;
1107 server_port_out->max = rtcpport;
1109 *server_addr_out = addr;
1110 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1112 g_object_unref (rtp_socket);
1113 g_object_unref (rtcp_socket);
1141 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1142 gst_object_unref (udpsrc0);
1145 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1146 gst_object_unref (udpsrc1);
1149 gst_element_set_state (udpsink0, GST_STATE_NULL);
1150 gst_object_unref (udpsink0);
1153 g_object_unref (inetaddr);
1154 g_list_free_full (rejected_addresses,
1155 (GDestroyNotify) gst_rtsp_address_free);
1157 gst_rtsp_address_free (addr);
1159 g_object_unref (rtp_socket);
1161 g_object_unref (rtcp_socket);
1166 /* must be called with lock */
1168 alloc_ports (GstRTSPStream * stream)
1170 GstRTSPStreamPrivate *priv = stream->priv;
1172 priv->have_ipv4 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1173 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1174 &priv->server_port_v4, &priv->server_addr_v4);
1176 priv->have_ipv6 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1177 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1178 &priv->server_port_v6, &priv->server_addr_v6);
1180 return priv->have_ipv4 || priv->have_ipv6;
1184 * gst_rtsp_stream_get_server_port:
1185 * @stream: a #GstRTSPStream
1186 * @server_port: (out): result server port
1187 * @family: the port family to get
1189 * Fill @server_port with the port pair used by the server. This function can
1190 * only be called when @stream has been joined.
1193 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1194 GstRTSPRange * server_port, GSocketFamily family)
1196 GstRTSPStreamPrivate *priv;
1198 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1199 priv = stream->priv;
1200 g_return_if_fail (priv->is_joined);
1202 g_mutex_lock (&priv->lock);
1203 if (family == G_SOCKET_FAMILY_IPV4) {
1205 *server_port = priv->server_port_v4;
1208 *server_port = priv->server_port_v6;
1210 g_mutex_unlock (&priv->lock);
1214 * gst_rtsp_stream_get_rtpsession:
1215 * @stream: a #GstRTSPStream
1217 * Get the RTP session of this stream.
1219 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1222 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1224 GstRTSPStreamPrivate *priv;
1227 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1229 priv = stream->priv;
1231 g_mutex_lock (&priv->lock);
1232 if ((session = priv->session))
1233 g_object_ref (session);
1234 g_mutex_unlock (&priv->lock);
1240 * gst_rtsp_stream_get_ssrc:
1241 * @stream: a #GstRTSPStream
1242 * @ssrc: (out): result ssrc
1244 * Get the SSRC used by the RTP session of this stream. This function can only
1245 * be called when @stream has been joined.
1248 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1250 GstRTSPStreamPrivate *priv;
1252 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1253 priv = stream->priv;
1254 g_return_if_fail (priv->is_joined);
1256 g_mutex_lock (&priv->lock);
1257 if (ssrc && priv->session)
1258 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1259 g_mutex_unlock (&priv->lock);
1262 /* executed from streaming thread */
1264 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1266 GstRTSPStreamPrivate *priv = stream->priv;
1267 GstCaps *newcaps, *oldcaps;
1269 newcaps = gst_pad_get_current_caps (pad);
1271 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1274 g_mutex_lock (&priv->lock);
1275 oldcaps = priv->caps;
1276 priv->caps = newcaps;
1277 g_mutex_unlock (&priv->lock);
1280 gst_caps_unref (oldcaps);
1284 dump_structure (const GstStructure * s)
1288 sstr = gst_structure_to_string (s);
1289 GST_INFO ("structure: %s", sstr);
1293 static GstRTSPStreamTransport *
1294 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1296 GstRTSPStreamPrivate *priv = stream->priv;
1298 GstRTSPStreamTransport *result = NULL;
1303 if (rtcp_from == NULL)
1306 tmp = g_strrstr (rtcp_from, ":");
1310 port = atoi (tmp + 1);
1311 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1313 g_mutex_lock (&priv->lock);
1314 GST_INFO ("finding %s:%d in %d transports", dest, port,
1315 g_list_length (priv->transports));
1317 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1318 GstRTSPStreamTransport *trans = walk->data;
1319 const GstRTSPTransport *tr;
1322 tr = gst_rtsp_stream_transport_get_transport (trans);
1324 min = tr->client_port.min;
1325 max = tr->client_port.max;
1327 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1333 g_object_ref (result);
1334 g_mutex_unlock (&priv->lock);
1341 static GstRTSPStreamTransport *
1342 check_transport (GObject * source, GstRTSPStream * stream)
1344 GstStructure *stats;
1345 GstRTSPStreamTransport *trans;
1347 /* see if we have a stream to match with the origin of the RTCP packet */
1348 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1349 if (trans == NULL) {
1350 g_object_get (source, "stats", &stats, NULL);
1352 const gchar *rtcp_from;
1354 dump_structure (stats);
1356 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1357 if ((trans = find_transport (stream, rtcp_from))) {
1358 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1360 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1363 gst_structure_free (stats);
1371 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1373 GstRTSPStreamTransport *trans;
1375 GST_INFO ("%p: new source %p", stream, source);
1377 trans = check_transport (source, stream);
1380 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1384 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1386 GST_INFO ("%p: new SDES %p", stream, source);
1390 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1392 GstRTSPStreamTransport *trans;
1394 trans = check_transport (source, stream);
1397 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1398 gst_rtsp_stream_transport_keep_alive (trans);
1402 GstStructure *stats;
1403 g_object_get (source, "stats", &stats, NULL);
1405 dump_structure (stats);
1406 gst_structure_free (stats);
1413 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1415 GST_INFO ("%p: source %p bye", stream, source);
1419 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1421 GstRTSPStreamTransport *trans;
1423 GST_INFO ("%p: source %p bye timeout", stream, source);
1425 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1426 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1427 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1432 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1434 GstRTSPStreamTransport *trans;
1436 GST_INFO ("%p: source %p timeout", stream, source);
1438 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1439 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1440 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1444 static GstFlowReturn
1445 handle_new_sample (GstAppSink * sink, gpointer user_data)
1447 GstRTSPStreamPrivate *priv;
1451 GstRTSPStream *stream;
1453 sample = gst_app_sink_pull_sample (sink);
1457 stream = (GstRTSPStream *) user_data;
1458 priv = stream->priv;
1459 buffer = gst_sample_get_buffer (sample);
1461 g_mutex_lock (&priv->lock);
1462 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1463 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1465 if (GST_ELEMENT_CAST (sink) == priv->appsink[0]) {
1466 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1468 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1471 g_mutex_unlock (&priv->lock);
1473 gst_sample_unref (sample);
1478 static GstAppSinkCallbacks sink_cb = {
1479 NULL, /* not interested in EOS */
1480 NULL, /* not interested in preroll samples */
1485 * gst_rtsp_stream_join_bin:
1486 * @stream: a #GstRTSPStream
1487 * @bin: a #GstBin to join
1488 * @rtpbin: a rtpbin element in @bin
1489 * @state: the target state of the new elements
1491 * Join the #GstBin @bin that contains the element @rtpbin.
1493 * @stream will link to @rtpbin, which must be inside @bin. The elements
1494 * added to @bin will be set to the state given in @state.
1496 * Returns: %TRUE on success.
1499 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1500 GstElement * rtpbin, GstState state)
1502 GstRTSPStreamPrivate *priv;
1506 GstPad *pad, *sinkpad, *selpad;
1507 GstPadLinkReturn ret;
1509 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1510 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1511 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1513 priv = stream->priv;
1515 g_mutex_lock (&priv->lock);
1516 if (priv->is_joined)
1519 /* create a session with the same index as the stream */
1522 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1524 if (!alloc_ports (stream))
1527 /* update the dscp qos field in the sinks */
1528 update_dscp_qos (stream);
1530 /* get a pad for sending RTP */
1531 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1532 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
1534 /* link the RTP pad to the session manager, it should not really fail unless
1535 * this is not really an RTP pad */
1536 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
1537 if (ret != GST_PAD_LINK_OK)
1540 /* get pads from the RTP session element for sending and receiving
1542 name = g_strdup_printf ("send_rtp_src_%u", idx);
1543 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
1545 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1546 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
1548 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1549 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
1551 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1552 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
1555 /* get the session */
1556 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
1558 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1560 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1562 g_signal_connect (priv->session, "on-ssrc-active",
1563 (GCallback) on_ssrc_active, stream);
1564 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1566 g_signal_connect (priv->session, "on-bye-timeout",
1567 (GCallback) on_bye_timeout, stream);
1568 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1571 for (i = 0; i < 2; i++) {
1572 GstPad *teepad, *queuepad;
1573 /* For the sender we create this bit of pipeline for both
1574 * RTP and RTCP. Sync and preroll are enabled on udpsink so
1575 * we need to add a queue before appsink to make the pipeline
1576 * not block. For the TCP case, we want to pump data to the
1577 * client as fast as possible anyway.
1579 * .--------. .-----. .---------.
1580 * | rtpbin | | tee | | udpsink |
1581 * | send->sink src->sink |
1582 * '--------' | | '---------'
1583 * | | .---------. .---------.
1584 * | | | queue | | appsink |
1585 * | src->sink src->sink |
1586 * '-----' '---------' '---------'
1588 * When only UDP is allowed, we skip the tee, queue and appsink and link the
1589 * udpsink directly to the session.
1592 gst_bin_add (bin, priv->udpsink[i]);
1593 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1595 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1596 /* make tee for RTP/RTCP */
1597 priv->tee[i] = gst_element_factory_make ("tee", NULL);
1598 gst_bin_add (bin, priv->tee[i]);
1600 /* and link to rtpbin send pad */
1601 pad = gst_element_get_static_pad (priv->tee[i], "sink");
1602 gst_pad_link (priv->send_src[i], pad);
1603 gst_object_unref (pad);
1605 /* link tee to udpsink */
1606 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1607 gst_pad_link (teepad, sinkpad);
1608 gst_object_unref (teepad);
1611 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1612 gst_bin_add (bin, priv->appqueue[i]);
1613 /* and link to tee */
1614 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1615 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1616 gst_pad_link (teepad, pad);
1617 gst_object_unref (pad);
1618 gst_object_unref (teepad);
1621 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1622 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1623 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1624 gst_bin_add (bin, priv->appsink[i]);
1625 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1626 &sink_cb, stream, NULL);
1627 /* and link to queue */
1628 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1629 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1630 gst_pad_link (queuepad, pad);
1631 gst_object_unref (pad);
1632 gst_object_unref (queuepad);
1634 /* else only udpsink needed, link it to the session */
1635 gst_pad_link (priv->send_src[i], sinkpad);
1637 gst_object_unref (sinkpad);
1639 /* For the receiver we create this bit of pipeline for both
1640 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1641 * and it is all funneled into the rtpbin receive pad.
1643 * .--------. .--------. .--------.
1644 * | udpsrc | | funnel | | rtpbin |
1645 * | src->sink src->sink |
1646 * '--------' | | '--------'
1650 * '--------' '--------'
1652 /* make funnel for the RTP/RTCP receivers */
1653 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1654 gst_bin_add (bin, priv->funnel[i]);
1656 pad = gst_element_get_static_pad (priv->funnel[i], "src");
1657 gst_pad_link (pad, priv->recv_sink[i]);
1658 gst_object_unref (pad);
1660 if (priv->udpsrc_v4[i]) {
1661 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1663 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
1664 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
1666 gst_bin_add (bin, priv->udpsrc_v4[i]);
1668 /* and link to the funnel v4 */
1669 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1670 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
1671 gst_pad_link (pad, selpad);
1672 gst_object_unref (pad);
1673 gst_object_unref (selpad);
1676 if (priv->udpsrc_v6[i]) {
1677 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
1678 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
1679 gst_bin_add (bin, priv->udpsrc_v6[i]);
1681 /* and link to the funnel v6 */
1682 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1683 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
1684 gst_pad_link (pad, selpad);
1685 gst_object_unref (pad);
1686 gst_object_unref (selpad);
1689 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1690 /* make and add appsrc */
1691 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1692 gst_bin_add (bin, priv->appsrc[i]);
1693 /* and link to the funnel */
1694 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1695 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
1696 gst_pad_link (pad, selpad);
1697 gst_object_unref (pad);
1698 gst_object_unref (selpad);
1701 /* check if we need to set to a special state */
1702 if (state != GST_STATE_NULL) {
1703 if (priv->udpsink[i])
1704 gst_element_set_state (priv->udpsink[i], state);
1705 if (priv->appsink[i])
1706 gst_element_set_state (priv->appsink[i], state);
1707 if (priv->appqueue[i])
1708 gst_element_set_state (priv->appqueue[i], state);
1710 gst_element_set_state (priv->tee[i], state);
1711 if (priv->funnel[i])
1712 gst_element_set_state (priv->funnel[i], state);
1713 if (priv->appsrc[i])
1714 gst_element_set_state (priv->appsrc[i], state);
1718 /* be notified of caps changes */
1719 priv->caps_sig = g_signal_connect (priv->send_rtp_sink, "notify::caps",
1720 (GCallback) caps_notify, stream);
1722 priv->is_joined = TRUE;
1723 g_mutex_unlock (&priv->lock);
1730 g_mutex_unlock (&priv->lock);
1735 g_mutex_unlock (&priv->lock);
1736 GST_WARNING ("failed to allocate ports %u", idx);
1741 GST_WARNING ("failed to link stream %u", idx);
1742 gst_object_unref (priv->send_rtp_sink);
1743 priv->send_rtp_sink = NULL;
1744 g_mutex_unlock (&priv->lock);
1750 * gst_rtsp_stream_leave_bin:
1751 * @stream: a #GstRTSPStream
1753 * @rtpbin: a rtpbin #GstElement
1755 * Remove the elements of @stream from @bin.
1757 * Return: %TRUE on success.
1760 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1761 GstElement * rtpbin)
1763 GstRTSPStreamPrivate *priv;
1766 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1767 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1768 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1770 priv = stream->priv;
1772 g_mutex_lock (&priv->lock);
1773 if (!priv->is_joined)
1774 goto was_not_joined;
1776 /* all transports must be removed by now */
1777 g_return_val_if_fail (priv->transports == NULL, FALSE);
1779 GST_INFO ("stream %p leaving bin", stream);
1781 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
1782 g_signal_handler_disconnect (priv->send_rtp_sink, priv->caps_sig);
1783 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
1784 gst_object_unref (priv->send_rtp_sink);
1785 priv->send_rtp_sink = NULL;
1787 for (i = 0; i < 2; i++) {
1788 if (priv->udpsink[i])
1789 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
1790 if (priv->appsink[i])
1791 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
1792 if (priv->appqueue[i])
1793 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
1795 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
1796 if (priv->funnel[i])
1797 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
1798 if (priv->appsrc[i])
1799 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
1800 if (priv->udpsrc_v4[i]) {
1801 /* and set udpsrc to NULL now before removing */
1802 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
1803 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
1804 /* removing them should also nicely release the request
1805 * pads when they finalize */
1806 gst_bin_remove (bin, priv->udpsrc_v4[i]);
1808 if (priv->udpsrc_v6[i]) {
1809 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
1810 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
1811 gst_bin_remove (bin, priv->udpsrc_v6[i]);
1813 if (priv->udpsink[i])
1814 gst_bin_remove (bin, priv->udpsink[i]);
1815 if (priv->appsrc[i])
1816 gst_bin_remove (bin, priv->appsrc[i]);
1817 if (priv->appsink[i])
1818 gst_bin_remove (bin, priv->appsink[i]);
1819 if (priv->appqueue[i])
1820 gst_bin_remove (bin, priv->appqueue[i]);
1822 gst_bin_remove (bin, priv->tee[i]);
1823 if (priv->funnel[i])
1824 gst_bin_remove (bin, priv->funnel[i]);
1826 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
1827 gst_object_unref (priv->recv_sink[i]);
1828 priv->recv_sink[i] = NULL;
1830 priv->udpsrc_v4[i] = NULL;
1831 priv->udpsrc_v6[i] = NULL;
1832 priv->udpsink[i] = NULL;
1833 priv->appsrc[i] = NULL;
1834 priv->appsink[i] = NULL;
1835 priv->appqueue[i] = NULL;
1836 priv->tee[i] = NULL;
1837 priv->funnel[i] = NULL;
1839 gst_object_unref (priv->send_src[0]);
1840 priv->send_src[0] = NULL;
1842 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
1843 gst_object_unref (priv->send_src[1]);
1844 priv->send_src[1] = NULL;
1846 g_object_unref (priv->session);
1847 priv->session = NULL;
1849 gst_caps_unref (priv->caps);
1852 priv->is_joined = FALSE;
1853 g_mutex_unlock (&priv->lock);
1864 * gst_rtsp_stream_get_rtpinfo:
1865 * @stream: a #GstRTSPStream
1866 * @rtptime: (allow-none): result RTP timestamp
1867 * @seq: (allow-none): result RTP seqnum
1868 * @clock_rate: the clock rate
1869 * @running_time: (allow-none): result running-time
1871 * Retrieve the current rtptime, seq and running-time. This is used to
1872 * construct a RTPInfo reply header.
1874 * Returns: %TRUE when rtptime, seq and running-time could be determined.
1877 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
1878 guint * rtptime, guint * seq, guint * clock_rate,
1879 GstClockTime * running_time)
1881 GstRTSPStreamPrivate *priv;
1882 GstStructure *stats;
1883 GObjectClass *payobjclass;
1885 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1887 priv = stream->priv;
1889 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
1891 g_mutex_lock (&priv->lock);
1893 if (g_object_class_find_property (payobjclass, "stats")) {
1894 g_object_get (priv->payloader, "stats", &stats, NULL);
1899 gst_structure_get_uint (stats, "seqnum", seq);
1902 gst_structure_get_uint (stats, "timestamp", rtptime);
1905 gst_structure_get_clock_time (stats, "running-time", running_time);
1908 gst_structure_get_uint (stats, "clock-rate", clock_rate);
1909 if (*clock_rate == 0 && running_time)
1910 *running_time = GST_CLOCK_TIME_NONE;
1912 gst_structure_free (stats);
1914 if (!g_object_class_find_property (payobjclass, "seqnum") ||
1915 !g_object_class_find_property (payobjclass, "timestamp"))
1919 g_object_get (priv->payloader, "seqnum", seq, NULL);
1922 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
1925 *running_time = GST_CLOCK_TIME_NONE;
1927 g_mutex_unlock (&priv->lock);
1934 GST_WARNING ("Could not get payloader stats");
1935 g_mutex_unlock (&priv->lock);
1941 * gst_rtsp_stream_get_caps:
1942 * @stream: a #GstRTSPStream
1944 * Retrieve the current caps of @stream.
1946 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
1950 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
1952 GstRTSPStreamPrivate *priv;
1955 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1957 priv = stream->priv;
1959 g_mutex_lock (&priv->lock);
1960 if ((result = priv->caps))
1961 gst_caps_ref (result);
1962 g_mutex_unlock (&priv->lock);
1968 * gst_rtsp_stream_recv_rtp:
1969 * @stream: a #GstRTSPStream
1970 * @buffer: (transfer full): a #GstBuffer
1972 * Handle an RTP buffer for the stream. This method is usually called when a
1973 * message has been received from a client using the TCP transport.
1975 * This function takes ownership of @buffer.
1977 * Returns: a GstFlowReturn.
1980 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
1982 GstRTSPStreamPrivate *priv;
1984 GstElement *element;
1986 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1987 priv = stream->priv;
1988 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1989 g_return_val_if_fail (priv->is_joined, FALSE);
1991 g_mutex_lock (&priv->lock);
1992 if (priv->appsrc[0])
1993 element = gst_object_ref (priv->appsrc[0]);
1996 g_mutex_unlock (&priv->lock);
1999 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2000 gst_object_unref (element);
2008 * gst_rtsp_stream_recv_rtcp:
2009 * @stream: a #GstRTSPStream
2010 * @buffer: (transfer full): a #GstBuffer
2012 * Handle an RTCP buffer for the stream. This method is usually called when a
2013 * message has been received from a client using the TCP transport.
2015 * This function takes ownership of @buffer.
2017 * Returns: a GstFlowReturn.
2020 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
2022 GstRTSPStreamPrivate *priv;
2024 GstElement *element;
2026 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2027 priv = stream->priv;
2028 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2029 g_return_val_if_fail (priv->is_joined, FALSE);
2031 g_mutex_lock (&priv->lock);
2032 if (priv->appsrc[1])
2033 element = gst_object_ref (priv->appsrc[1]);
2036 g_mutex_unlock (&priv->lock);
2039 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2040 gst_object_unref (element);
2047 /* must be called with lock */
2049 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2052 GstRTSPStreamPrivate *priv = stream->priv;
2053 const GstRTSPTransport *tr;
2055 tr = gst_rtsp_stream_transport_get_transport (trans);
2057 switch (tr->lower_transport) {
2058 case GST_RTSP_LOWER_TRANS_UDP:
2059 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2065 dest = tr->destination;
2066 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2071 min = tr->client_port.min;
2072 max = tr->client_port.max;
2076 GST_INFO ("adding %s:%d-%d", dest, min, max);
2077 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
2078 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
2080 GST_INFO ("setting ttl-mc %d", ttl);
2081 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
2082 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
2084 priv->transports = g_list_prepend (priv->transports, trans);
2086 GST_INFO ("removing %s:%d-%d", dest, min, max);
2087 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
2088 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
2089 priv->transports = g_list_remove (priv->transports, trans);
2093 case GST_RTSP_LOWER_TRANS_TCP:
2095 GST_INFO ("adding TCP %s", tr->destination);
2096 priv->transports = g_list_prepend (priv->transports, trans);
2098 GST_INFO ("removing TCP %s", tr->destination);
2099 priv->transports = g_list_remove (priv->transports, trans);
2103 goto unknown_transport;
2110 GST_INFO ("Unknown transport %d", tr->lower_transport);
2117 * gst_rtsp_stream_add_transport:
2118 * @stream: a #GstRTSPStream
2119 * @trans: a #GstRTSPStreamTransport
2121 * Add the transport in @trans to @stream. The media of @stream will
2122 * then also be send to the values configured in @trans.
2124 * @stream must be joined to a bin.
2126 * @trans must contain a valid #GstRTSPTransport.
2128 * Returns: %TRUE if @trans was added
2131 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
2132 GstRTSPStreamTransport * trans)
2134 GstRTSPStreamPrivate *priv;
2137 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2138 priv = stream->priv;
2139 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2140 g_return_val_if_fail (priv->is_joined, FALSE);
2142 g_mutex_lock (&priv->lock);
2143 res = update_transport (stream, trans, TRUE);
2144 g_mutex_unlock (&priv->lock);
2150 * gst_rtsp_stream_remove_transport:
2151 * @stream: a #GstRTSPStream
2152 * @trans: a #GstRTSPStreamTransport
2154 * Remove the transport in @trans from @stream. The media of @stream will
2155 * not be sent to the values configured in @trans.
2157 * @stream must be joined to a bin.
2159 * @trans must contain a valid #GstRTSPTransport.
2161 * Returns: %TRUE if @trans was removed
2164 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2165 GstRTSPStreamTransport * trans)
2167 GstRTSPStreamPrivate *priv;
2170 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2171 priv = stream->priv;
2172 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2173 g_return_val_if_fail (priv->is_joined, FALSE);
2175 g_mutex_lock (&priv->lock);
2176 res = update_transport (stream, trans, FALSE);
2177 g_mutex_unlock (&priv->lock);
2183 * gst_rtsp_stream_get_rtp_socket:
2184 * @stream: a #GstRTSPStream
2185 * @family: the socket family
2187 * Get the RTP socket from @stream for a @family.
2189 * @stream must be joined to a bin.
2191 * Returns: the RTP socket or %NULL if no socket could be allocated for @family.
2195 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2197 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2201 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2202 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2203 family == G_SOCKET_FAMILY_IPV6, NULL);
2204 g_return_val_if_fail (priv->udpsink[0], NULL);
2206 if (family == G_SOCKET_FAMILY_IPV6)
2211 g_object_get (priv->udpsink[0], name, &socket, NULL);
2217 * gst_rtsp_stream_get_rtcp_socket:
2218 * @stream: a #GstRTSPStream
2219 * @family: the socket family
2221 * Get the RTCP socket from @stream for a @family.
2223 * @stream must be joined to a bin.
2225 * Returns: the RTCP socket or %NULL if no socket could be allocated for
2226 * @family. Unref after usage
2229 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
2231 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2235 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2236 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2237 family == G_SOCKET_FAMILY_IPV6, NULL);
2238 g_return_val_if_fail (priv->udpsink[1], NULL);
2240 if (family == G_SOCKET_FAMILY_IPV6)
2245 g_object_get (priv->udpsink[1], name, &socket, NULL);
2251 * gst_rtsp_stream_transport_filter:
2252 * @stream: a #GstRTSPStream
2253 * @func: (scope call) (allow-none): a callback
2254 * @user_data: user data passed to @func
2256 * Call @func for each transport managed by @stream. The result value of @func
2257 * determines what happens to the transport. @func will be called with @stream
2258 * locked so no further actions on @stream can be performed from @func.
2260 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
2263 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
2265 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
2266 * will also be added with an additional ref to the result #GList of this
2269 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
2271 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
2272 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2273 * element in the #GList should be unreffed before the list is freed.
2276 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
2277 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
2279 GstRTSPStreamPrivate *priv;
2280 GList *result, *walk, *next;
2282 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2284 priv = stream->priv;
2288 g_mutex_lock (&priv->lock);
2289 for (walk = priv->transports; walk; walk = next) {
2290 GstRTSPStreamTransport *trans = walk->data;
2291 GstRTSPFilterResult res;
2293 next = g_list_next (walk);
2296 res = func (stream, trans, user_data);
2298 res = GST_RTSP_FILTER_REF;
2301 case GST_RTSP_FILTER_REMOVE:
2302 update_transport (stream, trans, FALSE);
2304 case GST_RTSP_FILTER_REF:
2305 result = g_list_prepend (result, g_object_ref (trans));
2307 case GST_RTSP_FILTER_KEEP:
2312 g_mutex_unlock (&priv->lock);
2317 static GstPadProbeReturn
2318 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2320 GstRTSPStreamPrivate *priv;
2321 GstRTSPStream *stream;
2324 priv = stream->priv;
2326 GST_DEBUG_OBJECT (pad, "now blocking");
2328 g_mutex_lock (&priv->lock);
2329 priv->blocking = TRUE;
2330 g_mutex_unlock (&priv->lock);
2332 gst_element_post_message (priv->payloader,
2333 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
2334 gst_structure_new_empty ("GstRTSPStreamBlocking")));
2336 return GST_PAD_PROBE_OK;
2340 * gst_rtsp_stream_set_blocked:
2341 * @stream: a #GstRTSPStream
2342 * @blocked: boolean indicating we should block or unblock
2344 * Blocks or unblocks the dataflow on @stream.
2346 * Returns: %TRUE on success
2349 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
2351 GstRTSPStreamPrivate *priv;
2353 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2355 priv = stream->priv;
2357 g_mutex_lock (&priv->lock);
2359 priv->blocking = FALSE;
2360 if (priv->blocked_id == 0) {
2361 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
2362 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
2363 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
2364 g_object_ref (stream), g_object_unref);
2367 if (priv->blocked_id != 0) {
2368 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
2369 priv->blocked_id = 0;
2370 priv->blocking = FALSE;
2373 g_mutex_unlock (&priv->lock);
2379 * gst_rtsp_stream_is_blocking:
2380 * @stream: a #GstRTSPStream
2382 * Check if @stream is blocking on a #GstBuffer.
2384 * Returns: %TRUE if @stream is blocking
2387 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
2389 GstRTSPStreamPrivate *priv;
2392 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2394 priv = stream->priv;
2396 g_mutex_lock (&priv->lock);
2397 result = priv->blocking;
2398 g_mutex_unlock (&priv->lock);