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 /* SRTP encoder/decoder */
87 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
89 GstElement *udpsrc_v4[2];
91 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
93 GstElement *udpsrc_v6[2];
95 GstElement *udpsink[2];
97 /* for TCP transport */
98 GstElement *appsrc[2];
99 GstElement *appqueue[2];
100 GstElement *appsink[2];
103 GstElement *funnel[2];
105 /* server ports for sending/receiving over ipv4 */
106 GstRTSPRange server_port_v4;
107 GstRTSPAddress *server_addr_v4;
110 /* server ports for sending/receiving over ipv6 */
111 GstRTSPRange server_port_v6;
112 GstRTSPAddress *server_addr_v6;
115 /* multicast addresses */
116 GstRTSPAddressPool *pool;
117 GstRTSPAddress *addr_v4;
118 GstRTSPAddress *addr_v6;
120 /* the caps of the stream */
124 /* transports we stream to */
127 guint transports_cookie;
129 guint tr_cache_cookie;
133 /* stream blocking */
138 #define DEFAULT_CONTROL NULL
139 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
140 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
141 GST_RTSP_LOWER_TRANS_TCP
154 SIGNAL_NEW_RTP_ENCODER,
155 SIGNAL_NEW_RTCP_ENCODER,
159 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
160 #define GST_CAT_DEFAULT rtsp_stream_debug
162 static GQuark ssrc_stream_map_key;
164 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
165 GValue * value, GParamSpec * pspec);
166 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
167 const GValue * value, GParamSpec * pspec);
169 static void gst_rtsp_stream_finalize (GObject * obj);
171 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
173 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
176 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
178 GObjectClass *gobject_class;
180 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
182 gobject_class = G_OBJECT_CLASS (klass);
184 gobject_class->get_property = gst_rtsp_stream_get_property;
185 gobject_class->set_property = gst_rtsp_stream_set_property;
186 gobject_class->finalize = gst_rtsp_stream_finalize;
188 g_object_class_install_property (gobject_class, PROP_CONTROL,
189 g_param_spec_string ("control", "Control",
190 "The control string for this stream", DEFAULT_CONTROL,
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
193 g_object_class_install_property (gobject_class, PROP_PROFILES,
194 g_param_spec_flags ("profiles", "Profiles",
195 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
196 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
199 g_param_spec_flags ("protocols", "Protocols",
200 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
201 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
204 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
205 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
206 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
208 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
209 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
210 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
211 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
213 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
215 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
219 gst_rtsp_stream_init (GstRTSPStream * stream)
221 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
223 GST_DEBUG ("new stream %p", stream);
228 priv->control = g_strdup (DEFAULT_CONTROL);
229 priv->profiles = DEFAULT_PROFILES;
230 priv->protocols = DEFAULT_PROTOCOLS;
232 g_mutex_init (&priv->lock);
234 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
235 NULL, (GDestroyNotify) gst_caps_unref);
239 gst_rtsp_stream_finalize (GObject * obj)
241 GstRTSPStream *stream;
242 GstRTSPStreamPrivate *priv;
244 stream = GST_RTSP_STREAM (obj);
247 GST_DEBUG ("finalize stream %p", stream);
249 /* we really need to be unjoined now */
250 g_return_if_fail (!priv->is_joined);
253 gst_rtsp_address_free (priv->addr_v4);
255 gst_rtsp_address_free (priv->addr_v6);
256 if (priv->server_addr_v4)
257 gst_rtsp_address_free (priv->server_addr_v4);
258 if (priv->server_addr_v6)
259 gst_rtsp_address_free (priv->server_addr_v6);
261 g_object_unref (priv->pool);
262 gst_object_unref (priv->payloader);
263 gst_object_unref (priv->srcpad);
264 g_free (priv->control);
265 g_mutex_clear (&priv->lock);
267 g_hash_table_unref (priv->keys);
269 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
273 gst_rtsp_stream_get_property (GObject * object, guint propid,
274 GValue * value, GParamSpec * pspec)
276 GstRTSPStream *stream = GST_RTSP_STREAM (object);
280 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
283 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
286 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
294 gst_rtsp_stream_set_property (GObject * object, guint propid,
295 const GValue * value, GParamSpec * pspec)
297 GstRTSPStream *stream = GST_RTSP_STREAM (object);
301 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
304 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
307 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
315 * gst_rtsp_stream_new:
318 * @payloader: a #GstElement
320 * Create a new media stream with index @idx that handles RTP data on
321 * @srcpad and has a payloader element @payloader.
323 * Returns: (transfer full): a new #GstRTSPStream
326 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
328 GstRTSPStreamPrivate *priv;
329 GstRTSPStream *stream;
331 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
332 g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
333 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
335 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
338 priv->payloader = gst_object_ref (payloader);
339 priv->srcpad = gst_object_ref (srcpad);
345 * gst_rtsp_stream_get_index:
346 * @stream: a #GstRTSPStream
348 * Get the stream index.
350 * Return: the stream index.
353 gst_rtsp_stream_get_index (GstRTSPStream * stream)
355 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
357 return stream->priv->idx;
361 * gst_rtsp_stream_get_pt:
362 * @stream: a #GstRTSPStream
364 * Get the stream payload type.
366 * Return: the stream payload type.
369 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
371 GstRTSPStreamPrivate *priv;
374 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
378 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
384 * gst_rtsp_stream_get_srcpad:
385 * @stream: a #GstRTSPStream
387 * Get the srcpad associated with @stream.
389 * Returns: (transfer full): the srcpad. Unref after usage.
392 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
394 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
396 return gst_object_ref (stream->priv->srcpad);
400 * gst_rtsp_stream_get_control:
401 * @stream: a #GstRTSPStream
403 * Get the control string to identify this stream.
405 * Returns: (transfer full): the control string. g_free() after usage.
408 gst_rtsp_stream_get_control (GstRTSPStream * stream)
410 GstRTSPStreamPrivate *priv;
413 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
417 g_mutex_lock (&priv->lock);
418 if ((result = g_strdup (priv->control)) == NULL)
419 result = g_strdup_printf ("stream=%u", priv->idx);
420 g_mutex_unlock (&priv->lock);
426 * gst_rtsp_stream_set_control:
427 * @stream: a #GstRTSPStream
428 * @control: a control string
430 * Set the control string in @stream.
433 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
435 GstRTSPStreamPrivate *priv;
437 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
441 g_mutex_lock (&priv->lock);
442 g_free (priv->control);
443 priv->control = g_strdup (control);
444 g_mutex_unlock (&priv->lock);
448 * gst_rtsp_stream_has_control:
449 * @stream: a #GstRTSPStream
450 * @control: a control string
452 * Check if @stream has the control string @control.
454 * Returns: %TRUE is @stream has @control as the control string
457 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
459 GstRTSPStreamPrivate *priv;
462 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
466 g_mutex_lock (&priv->lock);
468 res = (g_strcmp0 (priv->control, control) == 0);
472 if (sscanf (control, "stream=%u", &streamid) > 0)
473 res = (streamid == priv->idx);
477 g_mutex_unlock (&priv->lock);
483 * gst_rtsp_stream_set_mtu:
484 * @stream: a #GstRTSPStream
487 * Configure the mtu in the payloader of @stream to @mtu.
490 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
492 GstRTSPStreamPrivate *priv;
494 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
498 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
500 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
504 * gst_rtsp_stream_get_mtu:
505 * @stream: a #GstRTSPStream
507 * Get the configured MTU in the payloader of @stream.
509 * Returns: the MTU of the payloader.
512 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
514 GstRTSPStreamPrivate *priv;
517 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
521 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
526 /* Update the dscp qos property on the udp sinks */
528 update_dscp_qos (GstRTSPStream * stream)
530 GstRTSPStreamPrivate *priv;
532 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
536 if (priv->udpsink[0]) {
537 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
541 if (priv->udpsink[1]) {
542 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
548 * gst_rtsp_stream_set_dscp_qos:
549 * @stream: a #GstRTSPStream
550 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
552 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
555 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
557 GstRTSPStreamPrivate *priv;
559 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
563 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
565 if (dscp_qos < -1 || dscp_qos > 63) {
566 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
570 priv->dscp_qos = dscp_qos;
572 update_dscp_qos (stream);
576 * gst_rtsp_stream_get_dscp_qos:
577 * @stream: a #GstRTSPStream
579 * Get the configured DSCP QoS in of the outgoing sockets.
581 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
584 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
586 GstRTSPStreamPrivate *priv;
588 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
592 return priv->dscp_qos;
596 * gst_rtsp_stream_is_transport_supported:
597 * @stream: a #GstRTSPStream
598 * @transport: (transfer none): a #GstRTSPTransport
600 * Check if @transport can be handled by stream
602 * Returns: %TRUE if @transport can be handled by @stream.
605 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
606 GstRTSPTransport * transport)
608 GstRTSPStreamPrivate *priv;
610 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
614 g_mutex_lock (&priv->lock);
615 if (transport->trans != GST_RTSP_TRANS_RTP)
616 goto unsupported_transmode;
618 if (!(transport->profile & priv->profiles))
619 goto unsupported_profile;
621 if (!(transport->lower_transport & priv->protocols))
622 goto unsupported_ltrans;
624 g_mutex_unlock (&priv->lock);
629 unsupported_transmode:
631 GST_DEBUG ("unsupported transport mode %d", transport->trans);
632 g_mutex_unlock (&priv->lock);
637 GST_DEBUG ("unsupported profile %d", transport->profile);
638 g_mutex_unlock (&priv->lock);
643 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
644 g_mutex_unlock (&priv->lock);
650 * gst_rtsp_stream_set_profiles:
651 * @stream: a #GstRTSPStream
652 * @profiles: the new profiles
654 * Configure the allowed profiles for @stream.
657 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
659 GstRTSPStreamPrivate *priv;
661 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
665 g_mutex_lock (&priv->lock);
666 priv->profiles = profiles;
667 g_mutex_unlock (&priv->lock);
671 * gst_rtsp_stream_get_profiles:
672 * @stream: a #GstRTSPStream
674 * Get the allowed profiles of @stream.
676 * Returns: a #GstRTSPProfile
679 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
681 GstRTSPStreamPrivate *priv;
684 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
688 g_mutex_lock (&priv->lock);
689 res = priv->profiles;
690 g_mutex_unlock (&priv->lock);
696 * gst_rtsp_stream_set_protocols:
697 * @stream: a #GstRTSPStream
698 * @protocols: the new flags
700 * Configure the allowed lower transport for @stream.
703 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
704 GstRTSPLowerTrans protocols)
706 GstRTSPStreamPrivate *priv;
708 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
712 g_mutex_lock (&priv->lock);
713 priv->protocols = protocols;
714 g_mutex_unlock (&priv->lock);
718 * gst_rtsp_stream_get_protocols:
719 * @stream: a #GstRTSPStream
721 * Get the allowed protocols of @stream.
723 * Returns: a #GstRTSPLowerTrans
726 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
728 GstRTSPStreamPrivate *priv;
729 GstRTSPLowerTrans res;
731 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
732 GST_RTSP_LOWER_TRANS_UNKNOWN);
736 g_mutex_lock (&priv->lock);
737 res = priv->protocols;
738 g_mutex_unlock (&priv->lock);
744 * gst_rtsp_stream_set_address_pool:
745 * @stream: a #GstRTSPStream
746 * @pool: (transfer none): a #GstRTSPAddressPool
748 * configure @pool to be used as the address pool of @stream.
751 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
752 GstRTSPAddressPool * pool)
754 GstRTSPStreamPrivate *priv;
755 GstRTSPAddressPool *old;
757 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
761 GST_LOG_OBJECT (stream, "set address pool %p", pool);
763 g_mutex_lock (&priv->lock);
764 if ((old = priv->pool) != pool)
765 priv->pool = pool ? g_object_ref (pool) : NULL;
768 g_mutex_unlock (&priv->lock);
771 g_object_unref (old);
775 * gst_rtsp_stream_get_address_pool:
776 * @stream: a #GstRTSPStream
778 * Get the #GstRTSPAddressPool used as the address pool of @stream.
780 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
784 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
786 GstRTSPStreamPrivate *priv;
787 GstRTSPAddressPool *result;
789 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
793 g_mutex_lock (&priv->lock);
794 if ((result = priv->pool))
795 g_object_ref (result);
796 g_mutex_unlock (&priv->lock);
802 * gst_rtsp_stream_get_multicast_address:
803 * @stream: a #GstRTSPStream
804 * @family: the #GSocketFamily
806 * Get the multicast address of @stream for @family.
808 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
809 * or %NULL when no address could be allocated. gst_rtsp_address_free()
813 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
814 GSocketFamily family)
816 GstRTSPStreamPrivate *priv;
817 GstRTSPAddress *result;
818 GstRTSPAddress **addrp;
819 GstRTSPAddressFlags flags;
821 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
825 if (family == G_SOCKET_FAMILY_IPV6) {
826 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
827 addrp = &priv->addr_v6;
829 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
830 addrp = &priv->addr_v4;
833 g_mutex_lock (&priv->lock);
834 if (*addrp == NULL) {
835 if (priv->pool == NULL)
838 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
840 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
844 result = gst_rtsp_address_copy (*addrp);
845 g_mutex_unlock (&priv->lock);
852 GST_ERROR_OBJECT (stream, "no address pool specified");
853 g_mutex_unlock (&priv->lock);
858 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
859 g_mutex_unlock (&priv->lock);
865 * gst_rtsp_stream_reserve_address:
866 * @stream: a #GstRTSPStream
867 * @address: an address
872 * Reserve @address and @port as the address and port of @stream.
874 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
875 * the address could be reserved. gst_rtsp_address_free() after usage.
878 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
879 const gchar * address, guint port, guint n_ports, guint ttl)
881 GstRTSPStreamPrivate *priv;
882 GstRTSPAddress *result;
884 GSocketFamily family;
885 GstRTSPAddress **addrp;
887 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
888 g_return_val_if_fail (address != NULL, NULL);
889 g_return_val_if_fail (port > 0, NULL);
890 g_return_val_if_fail (n_ports > 0, NULL);
891 g_return_val_if_fail (ttl > 0, NULL);
895 addr = g_inet_address_new_from_string (address);
897 GST_ERROR ("failed to get inet addr from %s", address);
898 family = G_SOCKET_FAMILY_IPV4;
900 family = g_inet_address_get_family (addr);
901 g_object_unref (addr);
904 if (family == G_SOCKET_FAMILY_IPV6)
905 addrp = &priv->addr_v6;
907 addrp = &priv->addr_v4;
909 g_mutex_lock (&priv->lock);
910 if (*addrp == NULL) {
911 GstRTSPAddressPoolResult res;
913 if (priv->pool == NULL)
916 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
917 port, n_ports, ttl, addrp);
918 if (res != GST_RTSP_ADDRESS_POOL_OK)
921 if (strcmp ((*addrp)->address, address) ||
922 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
923 (*addrp)->ttl != ttl)
924 goto different_address;
926 result = gst_rtsp_address_copy (*addrp);
927 g_mutex_unlock (&priv->lock);
934 GST_ERROR_OBJECT (stream, "no address pool specified");
935 g_mutex_unlock (&priv->lock);
940 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
942 g_mutex_unlock (&priv->lock);
947 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
948 " reserved", address);
949 g_mutex_unlock (&priv->lock);
955 alloc_ports_one_family (GstRTSPAddressPool * pool, gint buffer_size,
956 GSocketFamily family, GstElement * udpsrc_out[2],
957 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
958 GstRTSPAddress ** server_addr_out)
960 GstStateChangeReturn ret;
961 GstElement *udpsrc0, *udpsrc1;
962 GstElement *udpsink0, *udpsink1;
963 GSocket *rtp_socket = NULL;
964 GSocket *rtcp_socket;
965 gint tmp_rtp, tmp_rtcp;
967 gint rtpport, rtcpport;
968 GList *rejected_addresses = NULL;
969 GstRTSPAddress *addr = NULL;
970 GInetAddress *inetaddr = NULL;
971 GSocketAddress *rtp_sockaddr = NULL;
972 GSocketAddress *rtcp_sockaddr = NULL;
973 const gchar *multisink_socket;
975 if (family == G_SOCKET_FAMILY_IPV6)
976 multisink_socket = "socket-v6";
978 multisink_socket = "socket";
986 /* Start with random port */
989 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
990 G_SOCKET_PROTOCOL_UDP, NULL);
992 goto no_udp_protocol;
994 if (*server_addr_out)
995 gst_rtsp_address_free (*server_addr_out);
997 /* try to allocate 2 UDP ports, the RTP port should be an even
998 * number and the RTCP port should be the next (uneven) port */
1001 if (rtp_socket == NULL) {
1002 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1003 G_SOCKET_PROTOCOL_UDP, NULL);
1005 goto no_udp_protocol;
1008 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
1009 GstRTSPAddressFlags flags;
1012 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1014 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
1015 if (family == G_SOCKET_FAMILY_IPV6)
1016 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1018 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1020 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1025 tmp_rtp = addr->port;
1027 g_clear_object (&inetaddr);
1028 inetaddr = g_inet_address_new_from_string (addr->address);
1036 if (inetaddr == NULL)
1037 inetaddr = g_inet_address_new_any (family);
1040 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1041 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1042 g_object_unref (rtp_sockaddr);
1045 g_object_unref (rtp_sockaddr);
1047 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1048 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1049 g_clear_object (&rtp_sockaddr);
1054 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1055 g_object_unref (rtp_sockaddr);
1057 /* check if port is even */
1058 if ((tmp_rtp & 1) != 0) {
1059 /* port not even, close and allocate another */
1061 g_clear_object (&rtp_socket);
1066 tmp_rtcp = tmp_rtp + 1;
1068 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1069 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1070 g_object_unref (rtcp_sockaddr);
1071 g_clear_object (&rtp_socket);
1074 g_object_unref (rtcp_sockaddr);
1076 g_clear_object (&inetaddr);
1078 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
1079 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
1081 if (udpsrc0 == NULL || udpsrc1 == NULL)
1082 goto no_udp_protocol;
1084 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
1085 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
1087 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1088 if (ret == GST_STATE_CHANGE_FAILURE)
1090 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1091 if (ret == GST_STATE_CHANGE_FAILURE)
1094 /* all fine, do port check */
1095 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
1096 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
1098 /* this should not happen... */
1099 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1103 udpsink0 = udpsink_out[0];
1105 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1108 goto no_udp_protocol;
1110 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1111 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
1114 udpsink1 = udpsink_out[1];
1116 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1119 goto no_udp_protocol;
1121 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1122 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1123 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
1125 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1126 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
1127 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1128 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1129 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1130 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1131 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1132 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1134 /* we keep these elements, we will further configure them when the
1135 * client told us to really use the UDP ports. */
1136 udpsrc_out[0] = udpsrc0;
1137 udpsrc_out[1] = udpsrc1;
1138 udpsink_out[0] = udpsink0;
1139 udpsink_out[1] = udpsink1;
1140 server_port_out->min = rtpport;
1141 server_port_out->max = rtcpport;
1143 *server_addr_out = addr;
1144 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1146 g_object_unref (rtp_socket);
1147 g_object_unref (rtcp_socket);
1175 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1176 gst_object_unref (udpsrc0);
1179 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1180 gst_object_unref (udpsrc1);
1183 gst_element_set_state (udpsink0, GST_STATE_NULL);
1184 gst_object_unref (udpsink0);
1187 g_object_unref (inetaddr);
1188 g_list_free_full (rejected_addresses,
1189 (GDestroyNotify) gst_rtsp_address_free);
1191 gst_rtsp_address_free (addr);
1193 g_object_unref (rtp_socket);
1195 g_object_unref (rtcp_socket);
1200 /* must be called with lock */
1202 alloc_ports (GstRTSPStream * stream)
1204 GstRTSPStreamPrivate *priv = stream->priv;
1206 priv->have_ipv4 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1207 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1208 &priv->server_port_v4, &priv->server_addr_v4);
1210 priv->have_ipv6 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1211 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1212 &priv->server_port_v6, &priv->server_addr_v6);
1214 return priv->have_ipv4 || priv->have_ipv6;
1218 * gst_rtsp_stream_get_server_port:
1219 * @stream: a #GstRTSPStream
1220 * @server_port: (out): result server port
1221 * @family: the port family to get
1223 * Fill @server_port with the port pair used by the server. This function can
1224 * only be called when @stream has been joined.
1227 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1228 GstRTSPRange * server_port, GSocketFamily family)
1230 GstRTSPStreamPrivate *priv;
1232 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1233 priv = stream->priv;
1234 g_return_if_fail (priv->is_joined);
1236 g_mutex_lock (&priv->lock);
1237 if (family == G_SOCKET_FAMILY_IPV4) {
1239 *server_port = priv->server_port_v4;
1242 *server_port = priv->server_port_v6;
1244 g_mutex_unlock (&priv->lock);
1248 * gst_rtsp_stream_get_rtpsession:
1249 * @stream: a #GstRTSPStream
1251 * Get the RTP session of this stream.
1253 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1256 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1258 GstRTSPStreamPrivate *priv;
1261 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1263 priv = stream->priv;
1265 g_mutex_lock (&priv->lock);
1266 if ((session = priv->session))
1267 g_object_ref (session);
1268 g_mutex_unlock (&priv->lock);
1274 * gst_rtsp_stream_get_ssrc:
1275 * @stream: a #GstRTSPStream
1276 * @ssrc: (out): result ssrc
1278 * Get the SSRC used by the RTP session of this stream. This function can only
1279 * be called when @stream has been joined.
1282 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1284 GstRTSPStreamPrivate *priv;
1286 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1287 priv = stream->priv;
1288 g_return_if_fail (priv->is_joined);
1290 g_mutex_lock (&priv->lock);
1291 if (ssrc && priv->session)
1292 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1293 g_mutex_unlock (&priv->lock);
1296 /* executed from streaming thread */
1298 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1300 GstRTSPStreamPrivate *priv = stream->priv;
1301 GstCaps *newcaps, *oldcaps;
1303 newcaps = gst_pad_get_current_caps (pad);
1305 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1308 g_mutex_lock (&priv->lock);
1309 oldcaps = priv->caps;
1310 priv->caps = newcaps;
1311 g_mutex_unlock (&priv->lock);
1314 gst_caps_unref (oldcaps);
1318 dump_structure (const GstStructure * s)
1322 sstr = gst_structure_to_string (s);
1323 GST_INFO ("structure: %s", sstr);
1327 static GstRTSPStreamTransport *
1328 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1330 GstRTSPStreamPrivate *priv = stream->priv;
1332 GstRTSPStreamTransport *result = NULL;
1337 if (rtcp_from == NULL)
1340 tmp = g_strrstr (rtcp_from, ":");
1344 port = atoi (tmp + 1);
1345 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1347 g_mutex_lock (&priv->lock);
1348 GST_INFO ("finding %s:%d in %d transports", dest, port,
1349 g_list_length (priv->transports));
1351 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1352 GstRTSPStreamTransport *trans = walk->data;
1353 const GstRTSPTransport *tr;
1356 tr = gst_rtsp_stream_transport_get_transport (trans);
1358 min = tr->client_port.min;
1359 max = tr->client_port.max;
1361 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1367 g_object_ref (result);
1368 g_mutex_unlock (&priv->lock);
1375 static GstRTSPStreamTransport *
1376 check_transport (GObject * source, GstRTSPStream * stream)
1378 GstStructure *stats;
1379 GstRTSPStreamTransport *trans;
1381 /* see if we have a stream to match with the origin of the RTCP packet */
1382 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1383 if (trans == NULL) {
1384 g_object_get (source, "stats", &stats, NULL);
1386 const gchar *rtcp_from;
1388 dump_structure (stats);
1390 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1391 if ((trans = find_transport (stream, rtcp_from))) {
1392 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1394 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1397 gst_structure_free (stats);
1405 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1407 GstRTSPStreamTransport *trans;
1409 GST_INFO ("%p: new source %p", stream, source);
1411 trans = check_transport (source, stream);
1414 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1418 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1420 GST_INFO ("%p: new SDES %p", stream, source);
1424 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1426 GstRTSPStreamTransport *trans;
1428 trans = check_transport (source, stream);
1431 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1432 gst_rtsp_stream_transport_keep_alive (trans);
1436 GstStructure *stats;
1437 g_object_get (source, "stats", &stats, NULL);
1439 dump_structure (stats);
1440 gst_structure_free (stats);
1447 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1449 GST_INFO ("%p: source %p bye", stream, source);
1453 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1455 GstRTSPStreamTransport *trans;
1457 GST_INFO ("%p: source %p bye timeout", stream, source);
1459 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1460 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1461 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1466 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1468 GstRTSPStreamTransport *trans;
1470 GST_INFO ("%p: source %p timeout", stream, source);
1472 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1473 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1474 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1479 clear_tr_cache (GstRTSPStreamPrivate * priv)
1481 g_list_foreach (priv->tr_cache, (GFunc) g_object_unref, NULL);
1482 g_list_free (priv->tr_cache);
1483 priv->tr_cache = NULL;
1486 static GstFlowReturn
1487 handle_new_sample (GstAppSink * sink, gpointer user_data)
1489 GstRTSPStreamPrivate *priv;
1493 GstRTSPStream *stream;
1496 sample = gst_app_sink_pull_sample (sink);
1500 stream = (GstRTSPStream *) user_data;
1501 priv = stream->priv;
1502 buffer = gst_sample_get_buffer (sample);
1504 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
1506 g_mutex_lock (&priv->lock);
1507 if (priv->tr_cache_cookie != priv->transports_cookie) {
1508 clear_tr_cache (priv);
1509 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1510 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1511 priv->tr_cache = g_list_prepend (priv->tr_cache, g_object_ref (tr));
1513 priv->tr_cache_cookie = priv->transports_cookie;
1515 g_mutex_unlock (&priv->lock);
1517 for (walk = priv->tr_cache; walk; walk = g_list_next (walk)) {
1518 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1521 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1523 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1526 gst_sample_unref (sample);
1531 static GstAppSinkCallbacks sink_cb = {
1532 NULL, /* not interested in EOS */
1533 NULL, /* not interested in preroll samples */
1538 get_rtp_encoder (GstRTSPStream * stream, guint session)
1540 GstRTSPStreamPrivate *priv = stream->priv;
1542 if (priv->srtpenc == NULL) {
1545 name = g_strdup_printf ("srtpenc_%u", session);
1546 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
1549 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
1551 return gst_object_ref (priv->srtpenc);
1555 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
1557 GstRTSPStreamPrivate *priv = stream->priv;
1558 GstElement *oldenc, *enc;
1562 if (priv->idx != session)
1565 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
1567 oldenc = priv->srtpenc;
1568 enc = get_rtp_encoder (stream, session);
1569 name = g_strdup_printf ("rtp_sink_%d", session);
1570 pad = gst_element_get_request_pad (enc, name);
1572 gst_object_unref (pad);
1575 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
1582 request_rtcp_encoder (GstElement * rtpbin, guint session,
1583 GstRTSPStream * stream)
1585 GstRTSPStreamPrivate *priv = stream->priv;
1586 GstElement *oldenc, *enc;
1590 if (priv->idx != session)
1593 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
1595 oldenc = priv->srtpenc;
1596 enc = get_rtp_encoder (stream, session);
1597 name = g_strdup_printf ("rtcp_sink_%d", session);
1598 pad = gst_element_get_request_pad (enc, name);
1600 gst_object_unref (pad);
1603 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
1610 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
1612 GstRTSPStreamPrivate *priv = stream->priv;
1615 GST_DEBUG ("request key %08x", ssrc);
1617 g_mutex_lock (&priv->lock);
1618 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
1619 gst_caps_ref (caps);
1620 g_mutex_unlock (&priv->lock);
1626 request_rtcp_decoder (GstElement * rtpbin, guint session,
1627 GstRTSPStream * stream)
1629 GstRTSPStreamPrivate *priv = stream->priv;
1631 if (priv->idx != session)
1634 if (priv->srtpdec == NULL) {
1637 name = g_strdup_printf ("srtpdec_%u", session);
1638 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
1641 g_signal_connect (priv->srtpdec, "request-key",
1642 (GCallback) request_key, stream);
1644 return gst_object_ref (priv->srtpdec);
1648 * gst_rtsp_stream_join_bin:
1649 * @stream: a #GstRTSPStream
1650 * @bin: (transfer none): a #GstBin to join
1651 * @rtpbin: (transfer none): a rtpbin element in @bin
1652 * @state: the target state of the new elements
1654 * Join the #GstBin @bin that contains the element @rtpbin.
1656 * @stream will link to @rtpbin, which must be inside @bin. The elements
1657 * added to @bin will be set to the state given in @state.
1659 * Returns: %TRUE on success.
1662 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1663 GstElement * rtpbin, GstState state)
1665 GstRTSPStreamPrivate *priv;
1669 GstPad *pad, *sinkpad, *selpad;
1670 GstPadLinkReturn ret;
1672 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1673 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1674 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1676 priv = stream->priv;
1678 g_mutex_lock (&priv->lock);
1679 if (priv->is_joined)
1682 /* create a session with the same index as the stream */
1685 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1687 if (!alloc_ports (stream))
1690 /* update the dscp qos field in the sinks */
1691 update_dscp_qos (stream);
1693 if (priv->profiles & GST_RTSP_PROFILE_SAVP
1694 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
1696 g_signal_connect (rtpbin, "request-rtp-encoder",
1697 (GCallback) request_rtp_encoder, stream);
1698 g_signal_connect (rtpbin, "request-rtcp-encoder",
1699 (GCallback) request_rtcp_encoder, stream);
1700 g_signal_connect (rtpbin, "request-rtcp-decoder",
1701 (GCallback) request_rtcp_decoder, stream);
1704 /* get a pad for sending RTP */
1705 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1706 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
1708 /* link the RTP pad to the session manager, it should not really fail unless
1709 * this is not really an RTP pad */
1710 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
1711 if (ret != GST_PAD_LINK_OK)
1714 /* get pads from the RTP session element for sending and receiving
1716 name = g_strdup_printf ("send_rtp_src_%u", idx);
1717 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
1719 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1720 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
1722 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1723 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
1725 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1726 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
1729 /* get the session */
1730 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
1732 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1734 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1736 g_signal_connect (priv->session, "on-ssrc-active",
1737 (GCallback) on_ssrc_active, stream);
1738 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1740 g_signal_connect (priv->session, "on-bye-timeout",
1741 (GCallback) on_bye_timeout, stream);
1742 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1745 for (i = 0; i < 2; i++) {
1746 GstPad *teepad, *queuepad;
1747 /* For the sender we create this bit of pipeline for both
1748 * RTP and RTCP. Sync and preroll are enabled on udpsink so
1749 * we need to add a queue before appsink to make the pipeline
1750 * not block. For the TCP case, we want to pump data to the
1751 * client as fast as possible anyway.
1753 * .--------. .-----. .---------.
1754 * | rtpbin | | tee | | udpsink |
1755 * | send->sink src->sink |
1756 * '--------' | | '---------'
1757 * | | .---------. .---------.
1758 * | | | queue | | appsink |
1759 * | src->sink src->sink |
1760 * '-----' '---------' '---------'
1762 * When only UDP is allowed, we skip the tee, queue and appsink and link the
1763 * udpsink directly to the session.
1766 gst_bin_add (bin, priv->udpsink[i]);
1767 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1769 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1770 /* make tee for RTP/RTCP */
1771 priv->tee[i] = gst_element_factory_make ("tee", NULL);
1772 gst_bin_add (bin, priv->tee[i]);
1774 /* and link to rtpbin send pad */
1775 pad = gst_element_get_static_pad (priv->tee[i], "sink");
1776 gst_pad_link (priv->send_src[i], pad);
1777 gst_object_unref (pad);
1779 /* link tee to udpsink */
1780 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1781 gst_pad_link (teepad, sinkpad);
1782 gst_object_unref (teepad);
1785 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1786 gst_bin_add (bin, priv->appqueue[i]);
1787 /* and link to tee */
1788 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1789 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1790 gst_pad_link (teepad, pad);
1791 gst_object_unref (pad);
1792 gst_object_unref (teepad);
1795 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1796 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1797 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1798 gst_bin_add (bin, priv->appsink[i]);
1799 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1800 &sink_cb, stream, NULL);
1801 /* and link to queue */
1802 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1803 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1804 gst_pad_link (queuepad, pad);
1805 gst_object_unref (pad);
1806 gst_object_unref (queuepad);
1808 /* else only udpsink needed, link it to the session */
1809 gst_pad_link (priv->send_src[i], sinkpad);
1811 gst_object_unref (sinkpad);
1813 /* For the receiver we create this bit of pipeline for both
1814 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1815 * and it is all funneled into the rtpbin receive pad.
1817 * .--------. .--------. .--------.
1818 * | udpsrc | | funnel | | rtpbin |
1819 * | src->sink src->sink |
1820 * '--------' | | '--------'
1824 * '--------' '--------'
1826 /* make funnel for the RTP/RTCP receivers */
1827 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1828 gst_bin_add (bin, priv->funnel[i]);
1830 pad = gst_element_get_static_pad (priv->funnel[i], "src");
1831 gst_pad_link (pad, priv->recv_sink[i]);
1832 gst_object_unref (pad);
1834 if (priv->udpsrc_v4[i]) {
1835 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1837 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
1838 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
1840 gst_bin_add (bin, priv->udpsrc_v4[i]);
1842 /* and link to the funnel v4 */
1843 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1844 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
1845 gst_pad_link (pad, selpad);
1846 gst_object_unref (pad);
1847 gst_object_unref (selpad);
1850 if (priv->udpsrc_v6[i]) {
1851 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
1852 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
1853 gst_bin_add (bin, priv->udpsrc_v6[i]);
1855 /* and link to the funnel v6 */
1856 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1857 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
1858 gst_pad_link (pad, selpad);
1859 gst_object_unref (pad);
1860 gst_object_unref (selpad);
1863 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1864 /* make and add appsrc */
1865 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1866 gst_bin_add (bin, priv->appsrc[i]);
1867 /* and link to the funnel */
1868 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1869 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
1870 gst_pad_link (pad, selpad);
1871 gst_object_unref (pad);
1872 gst_object_unref (selpad);
1875 /* check if we need to set to a special state */
1876 if (state != GST_STATE_NULL) {
1877 if (priv->udpsink[i])
1878 gst_element_set_state (priv->udpsink[i], state);
1879 if (priv->appsink[i])
1880 gst_element_set_state (priv->appsink[i], state);
1881 if (priv->appqueue[i])
1882 gst_element_set_state (priv->appqueue[i], state);
1884 gst_element_set_state (priv->tee[i], state);
1885 if (priv->funnel[i])
1886 gst_element_set_state (priv->funnel[i], state);
1887 if (priv->appsrc[i])
1888 gst_element_set_state (priv->appsrc[i], state);
1892 /* be notified of caps changes */
1893 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
1894 (GCallback) caps_notify, stream);
1896 priv->is_joined = TRUE;
1897 g_mutex_unlock (&priv->lock);
1904 g_mutex_unlock (&priv->lock);
1909 g_mutex_unlock (&priv->lock);
1910 GST_WARNING ("failed to allocate ports %u", idx);
1915 GST_WARNING ("failed to link stream %u", idx);
1916 gst_object_unref (priv->send_rtp_sink);
1917 priv->send_rtp_sink = NULL;
1918 g_mutex_unlock (&priv->lock);
1924 * gst_rtsp_stream_leave_bin:
1925 * @stream: a #GstRTSPStream
1926 * @bin: (transfer none): a #GstBin
1927 * @rtpbin: (transfer none): a rtpbin #GstElement
1929 * Remove the elements of @stream from @bin.
1931 * Return: %TRUE on success.
1934 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1935 GstElement * rtpbin)
1937 GstRTSPStreamPrivate *priv;
1940 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1941 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1942 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1944 priv = stream->priv;
1946 g_mutex_lock (&priv->lock);
1947 if (!priv->is_joined)
1948 goto was_not_joined;
1950 /* all transports must be removed by now */
1951 g_return_val_if_fail (priv->transports == NULL, FALSE);
1953 clear_tr_cache (priv);
1955 GST_INFO ("stream %p leaving bin", stream);
1957 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
1958 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
1959 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
1960 gst_object_unref (priv->send_rtp_sink);
1961 priv->send_rtp_sink = NULL;
1963 for (i = 0; i < 2; i++) {
1964 if (priv->udpsink[i])
1965 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
1966 if (priv->appsink[i])
1967 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
1968 if (priv->appqueue[i])
1969 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
1971 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
1972 if (priv->funnel[i])
1973 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
1974 if (priv->appsrc[i])
1975 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
1976 if (priv->udpsrc_v4[i]) {
1977 /* and set udpsrc to NULL now before removing */
1978 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
1979 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
1980 /* removing them should also nicely release the request
1981 * pads when they finalize */
1982 gst_bin_remove (bin, priv->udpsrc_v4[i]);
1984 if (priv->udpsrc_v6[i]) {
1985 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
1986 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
1987 gst_bin_remove (bin, priv->udpsrc_v6[i]);
1989 if (priv->udpsink[i])
1990 gst_bin_remove (bin, priv->udpsink[i]);
1991 if (priv->appsrc[i])
1992 gst_bin_remove (bin, priv->appsrc[i]);
1993 if (priv->appsink[i])
1994 gst_bin_remove (bin, priv->appsink[i]);
1995 if (priv->appqueue[i])
1996 gst_bin_remove (bin, priv->appqueue[i]);
1998 gst_bin_remove (bin, priv->tee[i]);
1999 if (priv->funnel[i])
2000 gst_bin_remove (bin, priv->funnel[i]);
2002 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2003 gst_object_unref (priv->recv_sink[i]);
2004 priv->recv_sink[i] = NULL;
2006 priv->udpsrc_v4[i] = NULL;
2007 priv->udpsrc_v6[i] = NULL;
2008 priv->udpsink[i] = NULL;
2009 priv->appsrc[i] = NULL;
2010 priv->appsink[i] = NULL;
2011 priv->appqueue[i] = NULL;
2012 priv->tee[i] = NULL;
2013 priv->funnel[i] = NULL;
2015 gst_object_unref (priv->send_src[0]);
2016 priv->send_src[0] = NULL;
2018 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2019 gst_object_unref (priv->send_src[1]);
2020 priv->send_src[1] = NULL;
2022 g_object_unref (priv->session);
2023 priv->session = NULL;
2025 gst_caps_unref (priv->caps);
2029 gst_object_unref (priv->srtpenc);
2031 priv->is_joined = FALSE;
2032 g_mutex_unlock (&priv->lock);
2038 g_mutex_unlock (&priv->lock);
2044 * gst_rtsp_stream_get_rtpinfo:
2045 * @stream: a #GstRTSPStream
2046 * @rtptime: (allow-none): result RTP timestamp
2047 * @seq: (allow-none): result RTP seqnum
2048 * @clock_rate: (allow-none): the clock rate
2049 * @running_time: (allow-none): result running-time
2051 * Retrieve the current rtptime, seq and running-time. This is used to
2052 * construct a RTPInfo reply header.
2054 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2057 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2058 guint * rtptime, guint * seq, guint * clock_rate,
2059 GstClockTime * running_time)
2061 GstRTSPStreamPrivate *priv;
2062 GstStructure *stats;
2063 GObjectClass *payobjclass;
2065 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2067 priv = stream->priv;
2069 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2071 g_mutex_lock (&priv->lock);
2073 if (g_object_class_find_property (payobjclass, "stats")) {
2074 g_object_get (priv->payloader, "stats", &stats, NULL);
2079 gst_structure_get_uint (stats, "seqnum", seq);
2082 gst_structure_get_uint (stats, "timestamp", rtptime);
2085 gst_structure_get_clock_time (stats, "running-time", running_time);
2088 gst_structure_get_uint (stats, "clock-rate", clock_rate);
2089 if (*clock_rate == 0 && running_time)
2090 *running_time = GST_CLOCK_TIME_NONE;
2092 gst_structure_free (stats);
2094 if (!g_object_class_find_property (payobjclass, "seqnum") ||
2095 !g_object_class_find_property (payobjclass, "timestamp"))
2099 g_object_get (priv->payloader, "seqnum", seq, NULL);
2102 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
2105 *running_time = GST_CLOCK_TIME_NONE;
2107 g_mutex_unlock (&priv->lock);
2114 GST_WARNING ("Could not get payloader stats");
2115 g_mutex_unlock (&priv->lock);
2121 * gst_rtsp_stream_get_caps:
2122 * @stream: a #GstRTSPStream
2124 * Retrieve the current caps of @stream.
2126 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
2130 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
2132 GstRTSPStreamPrivate *priv;
2135 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2137 priv = stream->priv;
2139 g_mutex_lock (&priv->lock);
2140 if ((result = priv->caps))
2141 gst_caps_ref (result);
2142 g_mutex_unlock (&priv->lock);
2148 * gst_rtsp_stream_recv_rtp:
2149 * @stream: a #GstRTSPStream
2150 * @buffer: (transfer full): a #GstBuffer
2152 * Handle an RTP buffer for the stream. This method is usually called when a
2153 * message has been received from a client using the TCP transport.
2155 * This function takes ownership of @buffer.
2157 * Returns: a GstFlowReturn.
2160 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
2162 GstRTSPStreamPrivate *priv;
2164 GstElement *element;
2166 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2167 priv = stream->priv;
2168 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2169 g_return_val_if_fail (priv->is_joined, FALSE);
2171 g_mutex_lock (&priv->lock);
2172 if (priv->appsrc[0])
2173 element = gst_object_ref (priv->appsrc[0]);
2176 g_mutex_unlock (&priv->lock);
2179 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2180 gst_object_unref (element);
2188 * gst_rtsp_stream_recv_rtcp:
2189 * @stream: a #GstRTSPStream
2190 * @buffer: (transfer full): a #GstBuffer
2192 * Handle an RTCP buffer for the stream. This method is usually called when a
2193 * message has been received from a client using the TCP transport.
2195 * This function takes ownership of @buffer.
2197 * Returns: a GstFlowReturn.
2200 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
2202 GstRTSPStreamPrivate *priv;
2204 GstElement *element;
2206 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2207 priv = stream->priv;
2208 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2209 g_return_val_if_fail (priv->is_joined, FALSE);
2211 g_mutex_lock (&priv->lock);
2212 if (priv->appsrc[1])
2213 element = gst_object_ref (priv->appsrc[1]);
2216 g_mutex_unlock (&priv->lock);
2219 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2220 gst_object_unref (element);
2223 gst_buffer_unref (buffer);
2228 /* must be called with lock */
2230 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2233 GstRTSPStreamPrivate *priv = stream->priv;
2234 const GstRTSPTransport *tr;
2236 tr = gst_rtsp_stream_transport_get_transport (trans);
2238 switch (tr->lower_transport) {
2239 case GST_RTSP_LOWER_TRANS_UDP:
2240 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2246 dest = tr->destination;
2247 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2252 min = tr->client_port.min;
2253 max = tr->client_port.max;
2258 GST_INFO ("setting ttl-mc %d", ttl);
2259 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
2260 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
2262 GST_INFO ("adding %s:%d-%d", dest, min, max);
2263 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
2264 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
2265 priv->transports = g_list_prepend (priv->transports, trans);
2267 GST_INFO ("removing %s:%d-%d", dest, min, max);
2268 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
2269 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
2270 priv->transports = g_list_remove (priv->transports, trans);
2272 priv->transports_cookie++;
2275 case GST_RTSP_LOWER_TRANS_TCP:
2277 GST_INFO ("adding TCP %s", tr->destination);
2278 priv->transports = g_list_prepend (priv->transports, trans);
2280 GST_INFO ("removing TCP %s", tr->destination);
2281 priv->transports = g_list_remove (priv->transports, trans);
2283 priv->transports_cookie++;
2286 goto unknown_transport;
2293 GST_INFO ("Unknown transport %d", tr->lower_transport);
2300 * gst_rtsp_stream_add_transport:
2301 * @stream: a #GstRTSPStream
2302 * @trans: (transfer none): a #GstRTSPStreamTransport
2304 * Add the transport in @trans to @stream. The media of @stream will
2305 * then also be send to the values configured in @trans.
2307 * @stream must be joined to a bin.
2309 * @trans must contain a valid #GstRTSPTransport.
2311 * Returns: %TRUE if @trans was added
2314 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
2315 GstRTSPStreamTransport * trans)
2317 GstRTSPStreamPrivate *priv;
2320 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2321 priv = stream->priv;
2322 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2323 g_return_val_if_fail (priv->is_joined, FALSE);
2325 g_mutex_lock (&priv->lock);
2326 res = update_transport (stream, trans, TRUE);
2327 g_mutex_unlock (&priv->lock);
2333 * gst_rtsp_stream_remove_transport:
2334 * @stream: a #GstRTSPStream
2335 * @trans: (transfer none): a #GstRTSPStreamTransport
2337 * Remove the transport in @trans from @stream. The media of @stream will
2338 * not be sent to the values configured in @trans.
2340 * @stream must be joined to a bin.
2342 * @trans must contain a valid #GstRTSPTransport.
2344 * Returns: %TRUE if @trans was removed
2347 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2348 GstRTSPStreamTransport * trans)
2350 GstRTSPStreamPrivate *priv;
2353 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2354 priv = stream->priv;
2355 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2356 g_return_val_if_fail (priv->is_joined, FALSE);
2358 g_mutex_lock (&priv->lock);
2359 res = update_transport (stream, trans, FALSE);
2360 g_mutex_unlock (&priv->lock);
2366 * gst_rtsp_stream_update_crypto:
2367 * @stream: a #GstRTSPStream
2369 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
2371 * Update the new crypto information for @ssrc in @stream. If information
2372 * for @ssrc did not exist, it will be added. If information
2373 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
2374 * be removed from @stream.
2376 * Returns: %TRUE if @crypto could be updated
2379 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
2380 guint ssrc, GstCaps * crypto)
2382 GstRTSPStreamPrivate *priv;
2384 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2385 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
2387 priv = stream->priv;
2389 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
2391 g_mutex_lock (&priv->lock);
2393 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
2394 gst_caps_ref (crypto));
2396 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
2397 g_mutex_unlock (&priv->lock);
2403 * gst_rtsp_stream_get_rtp_socket:
2404 * @stream: a #GstRTSPStream
2405 * @family: the socket family
2407 * Get the RTP socket from @stream for a @family.
2409 * @stream must be joined to a bin.
2411 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
2412 * socket could be allocated for @family. Unref after usage
2415 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2417 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2421 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2422 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2423 family == G_SOCKET_FAMILY_IPV6, NULL);
2424 g_return_val_if_fail (priv->udpsink[0], NULL);
2426 if (family == G_SOCKET_FAMILY_IPV6)
2431 g_object_get (priv->udpsink[0], name, &socket, NULL);
2437 * gst_rtsp_stream_get_rtcp_socket:
2438 * @stream: a #GstRTSPStream
2439 * @family: the socket family
2441 * Get the RTCP socket from @stream for a @family.
2443 * @stream must be joined to a bin.
2445 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
2446 * socket could be allocated for @family. Unref after usage
2449 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
2451 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2455 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2456 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2457 family == G_SOCKET_FAMILY_IPV6, NULL);
2458 g_return_val_if_fail (priv->udpsink[1], NULL);
2460 if (family == G_SOCKET_FAMILY_IPV6)
2465 g_object_get (priv->udpsink[1], name, &socket, NULL);
2471 * gst_rtsp_stream_transport_filter:
2472 * @stream: a #GstRTSPStream
2473 * @func: (scope call) (allow-none): a callback
2474 * @user_data: (closure): user data passed to @func
2476 * Call @func for each transport managed by @stream. The result value of @func
2477 * determines what happens to the transport. @func will be called with @stream
2478 * locked so no further actions on @stream can be performed from @func.
2480 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
2483 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
2485 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
2486 * will also be added with an additional ref to the result #GList of this
2489 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
2491 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
2492 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2493 * element in the #GList should be unreffed before the list is freed.
2496 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
2497 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
2499 GstRTSPStreamPrivate *priv;
2500 GList *result, *walk, *next;
2501 GHashTable *visited;
2504 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2506 priv = stream->priv;
2510 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
2512 g_mutex_lock (&priv->lock);
2514 cookie = priv->transports_cookie;
2515 for (walk = priv->transports; walk; walk = next) {
2516 GstRTSPStreamTransport *trans = walk->data;
2517 GstRTSPFilterResult res;
2520 next = g_list_next (walk);
2523 /* only visit each transport once */
2524 if (g_hash_table_contains (visited, trans))
2527 g_hash_table_add (visited, g_object_ref (trans));
2528 g_mutex_unlock (&priv->lock);
2530 res = func (stream, trans, user_data);
2532 g_mutex_lock (&priv->lock);
2534 res = GST_RTSP_FILTER_REF;
2536 changed = (cookie != priv->transports_cookie);
2539 case GST_RTSP_FILTER_REMOVE:
2540 update_transport (stream, trans, FALSE);
2542 case GST_RTSP_FILTER_REF:
2543 result = g_list_prepend (result, g_object_ref (trans));
2545 case GST_RTSP_FILTER_KEEP:
2552 g_mutex_unlock (&priv->lock);
2555 g_hash_table_unref (visited);
2560 static GstPadProbeReturn
2561 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2563 GstRTSPStreamPrivate *priv;
2564 GstRTSPStream *stream;
2567 priv = stream->priv;
2569 GST_DEBUG_OBJECT (pad, "now blocking");
2571 g_mutex_lock (&priv->lock);
2572 priv->blocking = TRUE;
2573 g_mutex_unlock (&priv->lock);
2575 gst_element_post_message (priv->payloader,
2576 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
2577 gst_structure_new_empty ("GstRTSPStreamBlocking")));
2579 return GST_PAD_PROBE_OK;
2583 * gst_rtsp_stream_set_blocked:
2584 * @stream: a #GstRTSPStream
2585 * @blocked: boolean indicating we should block or unblock
2587 * Blocks or unblocks the dataflow on @stream.
2589 * Returns: %TRUE on success
2592 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
2594 GstRTSPStreamPrivate *priv;
2596 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2598 priv = stream->priv;
2600 g_mutex_lock (&priv->lock);
2602 priv->blocking = FALSE;
2603 if (priv->blocked_id == 0) {
2604 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
2605 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
2606 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
2607 g_object_ref (stream), g_object_unref);
2610 if (priv->blocked_id != 0) {
2611 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
2612 priv->blocked_id = 0;
2613 priv->blocking = FALSE;
2616 g_mutex_unlock (&priv->lock);
2622 * gst_rtsp_stream_is_blocking:
2623 * @stream: a #GstRTSPStream
2625 * Check if @stream is blocking on a #GstBuffer.
2627 * Returns: %TRUE if @stream is blocking
2630 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
2632 GstRTSPStreamPrivate *priv;
2635 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2637 priv = stream->priv;
2639 g_mutex_lock (&priv->lock);
2640 result = priv->blocking;
2641 g_mutex_unlock (&priv->lock);