2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3 * Copyright (C) 2015 Centricular Ltd
4 * Author: Sebastian Dröge <sebastian@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * @short_description: A media stream
24 * @see_also: #GstRTSPMedia
26 * The #GstRTSPStream object manages the data transport for one stream. It
27 * is created from a payloader element and a source pad that produce the RTP
28 * packets for the stream.
30 * With gst_rtsp_stream_join_bin() the streaming elements are added to the bin
31 * and rtpbin. gst_rtsp_stream_leave_bin() removes the elements again.
33 * The #GstRTSPStream will use the configured addresspool, as set with
34 * gst_rtsp_stream_set_address_pool(), to allocate multicast addresses for the
35 * stream. With gst_rtsp_stream_get_multicast_address() you can get the
38 * With gst_rtsp_stream_get_server_port () you can get the port that the server
39 * will use to receive RTCP. This is the part that the clients will use to send
42 * With gst_rtsp_stream_add_transport() destinations can be added where the
43 * stream should be sent to. Use gst_rtsp_stream_remove_transport() to remove
44 * the destination again.
46 * Last reviewed on 2013-07-16 (1.0.0)
55 #include <gst/app/gstappsrc.h>
56 #include <gst/app/gstappsink.h>
58 #include <gst/rtp/gstrtpbuffer.h>
60 #include "rtsp-stream.h"
62 #define GST_RTSP_STREAM_GET_PRIVATE(obj) \
63 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
67 GstRTSPStreamTransport *transport;
69 /* RTP and RTCP source */
70 GstElement *udpsrc[2];
72 } GstRTSPMulticastTransportSource;
74 struct _GstRTSPStreamPrivate
78 /* Only one pad is ever set */
79 GstPad *srcpad, *sinkpad;
80 GstElement *payloader;
85 GstRTSPProfile profiles;
86 GstRTSPLowerTrans protocols;
88 /* pads on the rtpbin */
89 GstPad *send_rtp_sink;
94 /* the RTPSession object */
97 /* SRTP encoder/decoder */
102 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
104 GstElement *udpsrc_v4[2];
106 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
108 GstElement *udpsrc_v6[2];
110 GstElement *udpqueue[2];
111 GstElement *udpsink[2];
113 /* for TCP transport */
114 GstElement *appsrc[2];
115 GstClockTime appsrc_base_time[2];
116 GstElement *appqueue[2];
117 GstElement *appsink[2];
120 GstElement *funnel[2];
125 GstClockTime rtx_time;
127 /* server ports for sending/receiving over ipv4 */
128 GstRTSPRange server_port_v4;
129 GstRTSPAddress *server_addr_v4;
132 /* server ports for sending/receiving over ipv6 */
133 GstRTSPRange server_port_v6;
134 GstRTSPAddress *server_addr_v6;
137 /* multicast addresses */
138 GstRTSPAddressPool *pool;
139 GstRTSPAddress *addr_v4;
140 GstRTSPAddress *addr_v6;
142 /* the caps of the stream */
146 /* transports we stream to */
149 guint transports_cookie;
151 GList *tr_cache_rtcp;
152 guint tr_cache_cookie_rtp;
153 guint tr_cache_cookie_rtcp;
156 /* UDP sources for UDP multicast transports */
157 GList *transport_sources;
161 /* stream blocking */
165 /* pt->caps map for RECORD streams */
169 #define DEFAULT_CONTROL NULL
170 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
171 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
172 GST_RTSP_LOWER_TRANS_TCP
185 SIGNAL_NEW_RTP_ENCODER,
186 SIGNAL_NEW_RTCP_ENCODER,
191 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
192 #define GST_CAT_DEFAULT rtsp_stream_debug
194 static GQuark ssrc_stream_map_key;
196 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
197 GValue * value, GParamSpec * pspec);
198 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
199 const GValue * value, GParamSpec * pspec);
201 static void gst_rtsp_stream_finalize (GObject * obj);
203 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
205 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
208 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
210 GObjectClass *gobject_class;
212 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
214 gobject_class = G_OBJECT_CLASS (klass);
216 gobject_class->get_property = gst_rtsp_stream_get_property;
217 gobject_class->set_property = gst_rtsp_stream_set_property;
218 gobject_class->finalize = gst_rtsp_stream_finalize;
220 g_object_class_install_property (gobject_class, PROP_CONTROL,
221 g_param_spec_string ("control", "Control",
222 "The control string for this stream", DEFAULT_CONTROL,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 g_object_class_install_property (gobject_class, PROP_PROFILES,
226 g_param_spec_flags ("profiles", "Profiles",
227 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
228 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
231 g_param_spec_flags ("protocols", "Protocols",
232 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
233 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
236 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
237 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
238 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
240 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
241 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
242 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
243 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
245 gst_rtsp_stream_signals[SIGNAL_RTCP_STATS] =
246 g_signal_new ("rtcp-statistics", G_TYPE_FROM_CLASS (klass),
247 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
248 G_TYPE_NONE, 1, GST_TYPE_STRUCTURE);
250 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
252 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
256 gst_rtsp_stream_init (GstRTSPStream * stream)
258 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
260 GST_DEBUG ("new stream %p", stream);
265 priv->control = g_strdup (DEFAULT_CONTROL);
266 priv->profiles = DEFAULT_PROFILES;
267 priv->protocols = DEFAULT_PROTOCOLS;
269 g_mutex_init (&priv->lock);
271 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
272 NULL, (GDestroyNotify) gst_caps_unref);
273 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
274 (GDestroyNotify) gst_caps_unref);
278 gst_rtsp_stream_finalize (GObject * obj)
280 GstRTSPStream *stream;
281 GstRTSPStreamPrivate *priv;
283 stream = GST_RTSP_STREAM (obj);
286 GST_DEBUG ("finalize stream %p", stream);
288 /* we really need to be unjoined now */
289 g_return_if_fail (!priv->is_joined);
292 gst_rtsp_address_free (priv->addr_v4);
294 gst_rtsp_address_free (priv->addr_v6);
295 if (priv->server_addr_v4)
296 gst_rtsp_address_free (priv->server_addr_v4);
297 if (priv->server_addr_v6)
298 gst_rtsp_address_free (priv->server_addr_v6);
300 g_object_unref (priv->pool);
302 g_object_unref (priv->rtxsend);
304 gst_object_unref (priv->payloader);
306 gst_object_unref (priv->srcpad);
308 gst_object_unref (priv->sinkpad);
309 g_free (priv->control);
310 g_mutex_clear (&priv->lock);
312 g_hash_table_unref (priv->keys);
313 g_hash_table_destroy (priv->ptmap);
315 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
319 gst_rtsp_stream_get_property (GObject * object, guint propid,
320 GValue * value, GParamSpec * pspec)
322 GstRTSPStream *stream = GST_RTSP_STREAM (object);
326 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
329 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
332 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
340 gst_rtsp_stream_set_property (GObject * object, guint propid,
341 const GValue * value, GParamSpec * pspec)
343 GstRTSPStream *stream = GST_RTSP_STREAM (object);
347 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
350 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
353 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
356 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
361 * gst_rtsp_stream_new:
364 * @payloader: a #GstElement
366 * Create a new media stream with index @idx that handles RTP data on
367 * @pad and has a payloader element @payloader if @pad is a source pad
368 * or a depayloader element @payloader if @pad is a sink pad.
370 * Returns: (transfer full): a new #GstRTSPStream
373 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
375 GstRTSPStreamPrivate *priv;
376 GstRTSPStream *stream;
378 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
379 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
381 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
384 priv->payloader = gst_object_ref (payloader);
385 if (GST_PAD_IS_SRC (pad))
386 priv->srcpad = gst_object_ref (pad);
388 priv->sinkpad = gst_object_ref (pad);
394 * gst_rtsp_stream_get_index:
395 * @stream: a #GstRTSPStream
397 * Get the stream index.
399 * Return: the stream index.
402 gst_rtsp_stream_get_index (GstRTSPStream * stream)
404 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
406 return stream->priv->idx;
410 * gst_rtsp_stream_get_pt:
411 * @stream: a #GstRTSPStream
413 * Get the stream payload type.
415 * Return: the stream payload type.
418 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
420 GstRTSPStreamPrivate *priv;
423 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
427 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
433 * gst_rtsp_stream_get_srcpad:
434 * @stream: a #GstRTSPStream
436 * Get the srcpad associated with @stream.
438 * Returns: (transfer full): the srcpad. Unref after usage.
441 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
443 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
445 if (!stream->priv->srcpad)
448 return gst_object_ref (stream->priv->srcpad);
452 * gst_rtsp_stream_get_sinkpad:
453 * @stream: a #GstRTSPStream
455 * Get the sinkpad associated with @stream.
457 * Returns: (transfer full): the sinkpad. Unref after usage.
460 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
462 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
464 if (!stream->priv->sinkpad)
467 return gst_object_ref (stream->priv->sinkpad);
471 * gst_rtsp_stream_get_control:
472 * @stream: a #GstRTSPStream
474 * Get the control string to identify this stream.
476 * Returns: (transfer full): the control string. g_free() after usage.
479 gst_rtsp_stream_get_control (GstRTSPStream * stream)
481 GstRTSPStreamPrivate *priv;
484 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
488 g_mutex_lock (&priv->lock);
489 if ((result = g_strdup (priv->control)) == NULL)
490 result = g_strdup_printf ("stream=%u", priv->idx);
491 g_mutex_unlock (&priv->lock);
497 * gst_rtsp_stream_set_control:
498 * @stream: a #GstRTSPStream
499 * @control: a control string
501 * Set the control string in @stream.
504 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
506 GstRTSPStreamPrivate *priv;
508 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
512 g_mutex_lock (&priv->lock);
513 g_free (priv->control);
514 priv->control = g_strdup (control);
515 g_mutex_unlock (&priv->lock);
519 * gst_rtsp_stream_has_control:
520 * @stream: a #GstRTSPStream
521 * @control: a control string
523 * Check if @stream has the control string @control.
525 * Returns: %TRUE is @stream has @control as the control string
528 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
530 GstRTSPStreamPrivate *priv;
533 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
537 g_mutex_lock (&priv->lock);
539 res = (g_strcmp0 (priv->control, control) == 0);
543 if (sscanf (control, "stream=%u", &streamid) > 0)
544 res = (streamid == priv->idx);
548 g_mutex_unlock (&priv->lock);
554 * gst_rtsp_stream_set_mtu:
555 * @stream: a #GstRTSPStream
558 * Configure the mtu in the payloader of @stream to @mtu.
561 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
563 GstRTSPStreamPrivate *priv;
565 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
569 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
571 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
575 * gst_rtsp_stream_get_mtu:
576 * @stream: a #GstRTSPStream
578 * Get the configured MTU in the payloader of @stream.
580 * Returns: the MTU of the payloader.
583 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
585 GstRTSPStreamPrivate *priv;
588 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
592 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
597 /* Update the dscp qos property on the udp sinks */
599 update_dscp_qos (GstRTSPStream * stream)
601 GstRTSPStreamPrivate *priv;
603 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
607 if (priv->udpsink[0]) {
608 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
612 if (priv->udpsink[1]) {
613 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
619 * gst_rtsp_stream_set_dscp_qos:
620 * @stream: a #GstRTSPStream
621 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
623 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
626 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
628 GstRTSPStreamPrivate *priv;
630 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
634 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
636 if (dscp_qos < -1 || dscp_qos > 63) {
637 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
641 priv->dscp_qos = dscp_qos;
643 update_dscp_qos (stream);
647 * gst_rtsp_stream_get_dscp_qos:
648 * @stream: a #GstRTSPStream
650 * Get the configured DSCP QoS in of the outgoing sockets.
652 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
655 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
657 GstRTSPStreamPrivate *priv;
659 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
663 return priv->dscp_qos;
667 * gst_rtsp_stream_is_transport_supported:
668 * @stream: a #GstRTSPStream
669 * @transport: (transfer none): a #GstRTSPTransport
671 * Check if @transport can be handled by stream
673 * Returns: %TRUE if @transport can be handled by @stream.
676 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
677 GstRTSPTransport * transport)
679 GstRTSPStreamPrivate *priv;
681 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
685 g_mutex_lock (&priv->lock);
686 if (transport->trans != GST_RTSP_TRANS_RTP)
687 goto unsupported_transmode;
689 if (!(transport->profile & priv->profiles))
690 goto unsupported_profile;
692 if (!(transport->lower_transport & priv->protocols))
693 goto unsupported_ltrans;
695 g_mutex_unlock (&priv->lock);
700 unsupported_transmode:
702 GST_DEBUG ("unsupported transport mode %d", transport->trans);
703 g_mutex_unlock (&priv->lock);
708 GST_DEBUG ("unsupported profile %d", transport->profile);
709 g_mutex_unlock (&priv->lock);
714 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
715 g_mutex_unlock (&priv->lock);
721 * gst_rtsp_stream_set_profiles:
722 * @stream: a #GstRTSPStream
723 * @profiles: the new profiles
725 * Configure the allowed profiles for @stream.
728 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
730 GstRTSPStreamPrivate *priv;
732 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
736 g_mutex_lock (&priv->lock);
737 priv->profiles = profiles;
738 g_mutex_unlock (&priv->lock);
742 * gst_rtsp_stream_get_profiles:
743 * @stream: a #GstRTSPStream
745 * Get the allowed profiles of @stream.
747 * Returns: a #GstRTSPProfile
750 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
752 GstRTSPStreamPrivate *priv;
755 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
759 g_mutex_lock (&priv->lock);
760 res = priv->profiles;
761 g_mutex_unlock (&priv->lock);
767 * gst_rtsp_stream_set_protocols:
768 * @stream: a #GstRTSPStream
769 * @protocols: the new flags
771 * Configure the allowed lower transport for @stream.
774 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
775 GstRTSPLowerTrans protocols)
777 GstRTSPStreamPrivate *priv;
779 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
783 g_mutex_lock (&priv->lock);
784 priv->protocols = protocols;
785 g_mutex_unlock (&priv->lock);
789 * gst_rtsp_stream_get_protocols:
790 * @stream: a #GstRTSPStream
792 * Get the allowed protocols of @stream.
794 * Returns: a #GstRTSPLowerTrans
797 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
799 GstRTSPStreamPrivate *priv;
800 GstRTSPLowerTrans res;
802 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
803 GST_RTSP_LOWER_TRANS_UNKNOWN);
807 g_mutex_lock (&priv->lock);
808 res = priv->protocols;
809 g_mutex_unlock (&priv->lock);
815 * gst_rtsp_stream_set_address_pool:
816 * @stream: a #GstRTSPStream
817 * @pool: (transfer none): a #GstRTSPAddressPool
819 * configure @pool to be used as the address pool of @stream.
822 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
823 GstRTSPAddressPool * pool)
825 GstRTSPStreamPrivate *priv;
826 GstRTSPAddressPool *old;
828 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
832 GST_LOG_OBJECT (stream, "set address pool %p", pool);
834 g_mutex_lock (&priv->lock);
835 if ((old = priv->pool) != pool)
836 priv->pool = pool ? g_object_ref (pool) : NULL;
839 g_mutex_unlock (&priv->lock);
842 g_object_unref (old);
846 * gst_rtsp_stream_get_address_pool:
847 * @stream: a #GstRTSPStream
849 * Get the #GstRTSPAddressPool used as the address pool of @stream.
851 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
855 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
857 GstRTSPStreamPrivate *priv;
858 GstRTSPAddressPool *result;
860 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
864 g_mutex_lock (&priv->lock);
865 if ((result = priv->pool))
866 g_object_ref (result);
867 g_mutex_unlock (&priv->lock);
873 * gst_rtsp_stream_get_multicast_address:
874 * @stream: a #GstRTSPStream
875 * @family: the #GSocketFamily
877 * Get the multicast address of @stream for @family.
879 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
880 * or %NULL when no address could be allocated. gst_rtsp_address_free()
884 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
885 GSocketFamily family)
887 GstRTSPStreamPrivate *priv;
888 GstRTSPAddress *result;
889 GstRTSPAddress **addrp;
890 GstRTSPAddressFlags flags;
892 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
896 if (family == G_SOCKET_FAMILY_IPV6) {
897 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
898 addrp = &priv->addr_v6;
900 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
901 addrp = &priv->addr_v4;
904 g_mutex_lock (&priv->lock);
905 if (*addrp == NULL) {
906 if (priv->pool == NULL)
909 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
911 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
915 result = gst_rtsp_address_copy (*addrp);
916 g_mutex_unlock (&priv->lock);
923 GST_ERROR_OBJECT (stream, "no address pool specified");
924 g_mutex_unlock (&priv->lock);
929 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
930 g_mutex_unlock (&priv->lock);
936 * gst_rtsp_stream_reserve_address:
937 * @stream: a #GstRTSPStream
938 * @address: an address
943 * Reserve @address and @port as the address and port of @stream.
945 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
946 * the address could be reserved. gst_rtsp_address_free() after usage.
949 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
950 const gchar * address, guint port, guint n_ports, guint ttl)
952 GstRTSPStreamPrivate *priv;
953 GstRTSPAddress *result;
955 GSocketFamily family;
956 GstRTSPAddress **addrp;
958 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
959 g_return_val_if_fail (address != NULL, NULL);
960 g_return_val_if_fail (port > 0, NULL);
961 g_return_val_if_fail (n_ports > 0, NULL);
962 g_return_val_if_fail (ttl > 0, NULL);
966 addr = g_inet_address_new_from_string (address);
968 GST_ERROR ("failed to get inet addr from %s", address);
969 family = G_SOCKET_FAMILY_IPV4;
971 family = g_inet_address_get_family (addr);
972 g_object_unref (addr);
975 if (family == G_SOCKET_FAMILY_IPV6)
976 addrp = &priv->addr_v6;
978 addrp = &priv->addr_v4;
980 g_mutex_lock (&priv->lock);
981 if (*addrp == NULL) {
982 GstRTSPAddressPoolResult res;
984 if (priv->pool == NULL)
987 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
988 port, n_ports, ttl, addrp);
989 if (res != GST_RTSP_ADDRESS_POOL_OK)
992 if (strcmp ((*addrp)->address, address) ||
993 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
994 (*addrp)->ttl != ttl)
995 goto different_address;
997 result = gst_rtsp_address_copy (*addrp);
998 g_mutex_unlock (&priv->lock);
1005 GST_ERROR_OBJECT (stream, "no address pool specified");
1006 g_mutex_unlock (&priv->lock);
1011 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1013 g_mutex_unlock (&priv->lock);
1018 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
1019 " reserved", address);
1020 g_mutex_unlock (&priv->lock);
1026 alloc_ports_one_family (GstRTSPStream * stream, GstRTSPAddressPool * pool,
1027 gint buffer_size, GSocketFamily family, GstElement * udpsrc_out[2],
1028 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
1029 GstRTSPAddress ** server_addr_out)
1031 GstRTSPStreamPrivate *priv = stream->priv;
1032 GstStateChangeReturn ret;
1033 GstElement *udpsrc0, *udpsrc1;
1034 GstElement *udpsink0, *udpsink1;
1035 GSocket *rtp_socket = NULL;
1036 GSocket *rtcp_socket;
1037 gint tmp_rtp, tmp_rtcp;
1039 gint rtpport, rtcpport;
1040 GList *rejected_addresses = NULL;
1041 GstRTSPAddress *addr = NULL;
1042 GInetAddress *inetaddr = NULL;
1043 GSocketAddress *rtp_sockaddr = NULL;
1044 GSocketAddress *rtcp_sockaddr = NULL;
1045 const gchar *multisink_socket;
1047 if (family == G_SOCKET_FAMILY_IPV6)
1048 multisink_socket = "socket-v6";
1050 multisink_socket = "socket";
1058 /* Start with random port */
1061 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1062 G_SOCKET_PROTOCOL_UDP, NULL);
1064 goto no_udp_protocol;
1066 if (*server_addr_out)
1067 gst_rtsp_address_free (*server_addr_out);
1069 /* try to allocate 2 UDP ports, the RTP port should be an even
1070 * number and the RTCP port should be the next (uneven) port */
1073 if (rtp_socket == NULL) {
1074 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1075 G_SOCKET_PROTOCOL_UDP, NULL);
1077 goto no_udp_protocol;
1080 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
1081 GstRTSPAddressFlags flags;
1084 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1086 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
1087 if (family == G_SOCKET_FAMILY_IPV6)
1088 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1090 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1092 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1097 tmp_rtp = addr->port;
1099 g_clear_object (&inetaddr);
1100 inetaddr = g_inet_address_new_from_string (addr->address);
1108 if (inetaddr == NULL)
1109 inetaddr = g_inet_address_new_any (family);
1112 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1113 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1114 g_object_unref (rtp_sockaddr);
1117 g_object_unref (rtp_sockaddr);
1119 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1120 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1121 g_clear_object (&rtp_sockaddr);
1126 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1127 g_object_unref (rtp_sockaddr);
1129 /* check if port is even */
1130 if ((tmp_rtp & 1) != 0) {
1131 /* port not even, close and allocate another */
1133 g_clear_object (&rtp_socket);
1138 tmp_rtcp = tmp_rtp + 1;
1140 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1141 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1142 g_object_unref (rtcp_sockaddr);
1143 g_clear_object (&rtp_socket);
1146 g_object_unref (rtcp_sockaddr);
1148 g_clear_object (&inetaddr);
1150 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
1151 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
1153 if (udpsrc0 == NULL || udpsrc1 == NULL)
1154 goto no_udp_protocol;
1156 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
1157 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
1159 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1160 if (ret == GST_STATE_CHANGE_FAILURE)
1162 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1163 if (ret == GST_STATE_CHANGE_FAILURE)
1166 /* all fine, do port check */
1167 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
1168 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
1170 /* this should not happen... */
1171 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1175 udpsink0 = udpsink_out[0];
1177 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1180 goto no_udp_protocol;
1182 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1183 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
1186 udpsink1 = udpsink_out[1];
1188 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1191 goto no_udp_protocol;
1193 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1194 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1195 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
1197 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1198 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
1199 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1200 /* Needs to be async for RECORD streams, otherwise we will never go to
1201 * PLAYING because the sinks will wait for data while the udpsrc can't
1202 * provide data with timestamps in PAUSED. */
1204 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1205 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1206 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1207 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1208 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1209 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1211 /* we keep these elements, we will further configure them when the
1212 * client told us to really use the UDP ports. */
1213 udpsrc_out[0] = udpsrc0;
1214 udpsrc_out[1] = udpsrc1;
1215 udpsink_out[0] = udpsink0;
1216 udpsink_out[1] = udpsink1;
1218 server_port_out->min = rtpport;
1219 server_port_out->max = rtcpport;
1221 *server_addr_out = addr;
1222 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1224 g_object_unref (rtp_socket);
1225 g_object_unref (rtcp_socket);
1253 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1254 gst_object_unref (udpsrc0);
1257 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1258 gst_object_unref (udpsrc1);
1261 gst_element_set_state (udpsink0, GST_STATE_NULL);
1262 gst_object_unref (udpsink0);
1265 g_object_unref (inetaddr);
1266 g_list_free_full (rejected_addresses,
1267 (GDestroyNotify) gst_rtsp_address_free);
1269 gst_rtsp_address_free (addr);
1271 g_object_unref (rtp_socket);
1273 g_object_unref (rtcp_socket);
1278 /* must be called with lock */
1280 alloc_ports (GstRTSPStream * stream)
1282 GstRTSPStreamPrivate *priv = stream->priv;
1285 alloc_ports_one_family (stream, priv->pool, priv->buffer_size,
1286 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1287 &priv->server_port_v4, &priv->server_addr_v4);
1290 alloc_ports_one_family (stream, priv->pool, priv->buffer_size,
1291 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1292 &priv->server_port_v6, &priv->server_addr_v6);
1294 return priv->have_ipv4 || priv->have_ipv6;
1298 * gst_rtsp_stream_get_server_port:
1299 * @stream: a #GstRTSPStream
1300 * @server_port: (out): result server port
1301 * @family: the port family to get
1303 * Fill @server_port with the port pair used by the server. This function can
1304 * only be called when @stream has been joined.
1307 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1308 GstRTSPRange * server_port, GSocketFamily family)
1310 GstRTSPStreamPrivate *priv;
1312 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1313 priv = stream->priv;
1314 g_return_if_fail (priv->is_joined);
1316 g_mutex_lock (&priv->lock);
1317 if (family == G_SOCKET_FAMILY_IPV4) {
1319 *server_port = priv->server_port_v4;
1322 *server_port = priv->server_port_v6;
1324 g_mutex_unlock (&priv->lock);
1328 * gst_rtsp_stream_get_rtpsession:
1329 * @stream: a #GstRTSPStream
1331 * Get the RTP session of this stream.
1333 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1336 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1338 GstRTSPStreamPrivate *priv;
1341 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1343 priv = stream->priv;
1345 g_mutex_lock (&priv->lock);
1346 if ((session = priv->session))
1347 g_object_ref (session);
1348 g_mutex_unlock (&priv->lock);
1354 * gst_rtsp_stream_get_ssrc:
1355 * @stream: a #GstRTSPStream
1356 * @ssrc: (out): result ssrc
1358 * Get the SSRC used by the RTP session of this stream. This function can only
1359 * be called when @stream has been joined.
1362 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1364 GstRTSPStreamPrivate *priv;
1366 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1367 priv = stream->priv;
1368 g_return_if_fail (priv->is_joined);
1370 g_mutex_lock (&priv->lock);
1371 if (ssrc && priv->session)
1372 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1373 g_mutex_unlock (&priv->lock);
1377 * gst_rtsp_stream_set_retransmission_time:
1378 * @stream: a #GstRTSPStream
1379 * @time: a #GstClockTime
1381 * Set the amount of time to store retransmission packets.
1384 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1387 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1389 g_mutex_lock (&stream->priv->lock);
1390 stream->priv->rtx_time = time;
1391 if (stream->priv->rtxsend)
1392 g_object_set (stream->priv->rtxsend, "max-size-time",
1393 GST_TIME_AS_MSECONDS (time), NULL);
1394 g_mutex_unlock (&stream->priv->lock);
1398 * gst_rtsp_stream_get_retransmission_time:
1399 * @stream: a #GstRTSPStream
1401 * Get the amount of time to store retransmission data.
1403 * Returns: the amount of time to store retransmission data.
1406 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1410 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1412 g_mutex_lock (&stream->priv->lock);
1413 ret = stream->priv->rtx_time;
1414 g_mutex_unlock (&stream->priv->lock);
1420 * gst_rtsp_stream_set_retransmission_pt:
1421 * @stream: a #GstRTSPStream
1424 * Set the payload type (pt) for retransmission of this stream.
1427 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1429 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1431 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1433 g_mutex_lock (&stream->priv->lock);
1434 stream->priv->rtx_pt = rtx_pt;
1435 if (stream->priv->rtxsend) {
1436 guint pt = gst_rtsp_stream_get_pt (stream);
1437 gchar *pt_s = g_strdup_printf ("%d", pt);
1438 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1439 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1440 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1442 gst_structure_free (rtx_pt_map);
1444 g_mutex_unlock (&stream->priv->lock);
1448 * gst_rtsp_stream_get_retransmission_pt:
1449 * @stream: a #GstRTSPStream
1451 * Get the payload-type used for retransmission of this stream
1453 * Returns: The retransmission PT.
1456 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1460 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1462 g_mutex_lock (&stream->priv->lock);
1463 rtx_pt = stream->priv->rtx_pt;
1464 g_mutex_unlock (&stream->priv->lock);
1470 * gst_rtsp_stream_set_buffer_size:
1471 * @stream: a #GstRTSPStream
1472 * @size: the buffer size
1474 * Set the size of the UDP transmission buffer (in bytes)
1475 * Needs to be set before the stream is joined to a bin.
1480 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1482 g_mutex_lock (&stream->priv->lock);
1483 stream->priv->buffer_size = size;
1484 g_mutex_unlock (&stream->priv->lock);
1488 * gst_rtsp_stream_get_buffer_size:
1489 * @stream: a #GstRTSPStream
1491 * Get the size of the UDP transmission buffer (in bytes)
1493 * Returns: the size of the UDP TX buffer
1498 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1502 g_mutex_lock (&stream->priv->lock);
1503 buffer_size = stream->priv->buffer_size;
1504 g_mutex_unlock (&stream->priv->lock);
1509 /* executed from streaming thread */
1511 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1513 GstRTSPStreamPrivate *priv = stream->priv;
1514 GstCaps *newcaps, *oldcaps;
1516 newcaps = gst_pad_get_current_caps (pad);
1518 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1521 g_mutex_lock (&priv->lock);
1522 oldcaps = priv->caps;
1523 priv->caps = newcaps;
1524 g_mutex_unlock (&priv->lock);
1527 gst_caps_unref (oldcaps);
1531 dump_structure (const GstStructure * s)
1535 sstr = gst_structure_to_string (s);
1536 GST_INFO ("structure: %s", sstr);
1540 static GstRTSPStreamTransport *
1541 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1543 GstRTSPStreamPrivate *priv = stream->priv;
1545 GstRTSPStreamTransport *result = NULL;
1550 if (rtcp_from == NULL)
1553 tmp = g_strrstr (rtcp_from, ":");
1557 port = atoi (tmp + 1);
1558 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1560 g_mutex_lock (&priv->lock);
1561 GST_INFO ("finding %s:%d in %d transports", dest, port,
1562 g_list_length (priv->transports));
1564 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1565 GstRTSPStreamTransport *trans = walk->data;
1566 const GstRTSPTransport *tr;
1569 tr = gst_rtsp_stream_transport_get_transport (trans);
1571 min = tr->client_port.min;
1572 max = tr->client_port.max;
1574 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1580 g_object_ref (result);
1581 g_mutex_unlock (&priv->lock);
1588 static GstRTSPStreamTransport *
1589 check_transport (GObject * source, GstRTSPStream * stream)
1591 GstStructure *stats;
1592 GstRTSPStreamTransport *trans;
1594 /* see if we have a stream to match with the origin of the RTCP packet */
1595 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1596 if (trans == NULL) {
1597 g_object_get (source, "stats", &stats, NULL);
1599 const gchar *rtcp_from;
1601 dump_structure (stats);
1603 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_RTCP_STATS], 0, stats);
1605 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1606 if ((trans = find_transport (stream, rtcp_from))) {
1607 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1609 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1612 gst_structure_free (stats);
1620 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1622 GstRTSPStreamTransport *trans;
1624 GST_INFO ("%p: new source %p", stream, source);
1626 trans = check_transport (source, stream);
1629 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1633 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1635 GST_INFO ("%p: new SDES %p", stream, source);
1639 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1641 GstRTSPStreamTransport *trans;
1643 trans = check_transport (source, stream);
1646 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1647 gst_rtsp_stream_transport_keep_alive (trans);
1651 GstStructure *stats;
1652 g_object_get (source, "stats", &stats, NULL);
1654 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_RTCP_STATS], 0, stats);
1656 dump_structure (stats);
1657 gst_structure_free (stats);
1664 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1666 GST_INFO ("%p: source %p bye", stream, source);
1670 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1672 GstRTSPStreamTransport *trans;
1674 GST_INFO ("%p: source %p bye timeout", stream, source);
1676 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1677 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1678 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1683 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1685 GstRTSPStreamTransport *trans;
1687 GST_INFO ("%p: source %p timeout", stream, source);
1689 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1690 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1691 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1696 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1699 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1700 g_list_free (priv->tr_cache_rtp);
1701 priv->tr_cache_rtp = NULL;
1703 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1704 g_list_free (priv->tr_cache_rtcp);
1705 priv->tr_cache_rtcp = NULL;
1709 static GstFlowReturn
1710 handle_new_sample (GstAppSink * sink, gpointer user_data)
1712 GstRTSPStreamPrivate *priv;
1716 GstRTSPStream *stream;
1719 sample = gst_app_sink_pull_sample (sink);
1723 stream = (GstRTSPStream *) user_data;
1724 priv = stream->priv;
1725 buffer = gst_sample_get_buffer (sample);
1727 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
1729 g_mutex_lock (&priv->lock);
1731 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
1732 clear_tr_cache (priv, is_rtp);
1733 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1734 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1735 priv->tr_cache_rtp =
1736 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
1738 priv->tr_cache_cookie_rtp = priv->transports_cookie;
1741 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
1742 clear_tr_cache (priv, is_rtp);
1743 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1744 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1745 priv->tr_cache_rtcp =
1746 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
1748 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
1751 g_mutex_unlock (&priv->lock);
1754 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
1755 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1756 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1759 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
1760 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1761 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1764 gst_sample_unref (sample);
1769 static GstAppSinkCallbacks sink_cb = {
1770 NULL, /* not interested in EOS */
1771 NULL, /* not interested in preroll samples */
1776 get_rtp_encoder (GstRTSPStream * stream, guint session)
1778 GstRTSPStreamPrivate *priv = stream->priv;
1780 if (priv->srtpenc == NULL) {
1783 name = g_strdup_printf ("srtpenc_%u", session);
1784 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
1787 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
1789 return gst_object_ref (priv->srtpenc);
1793 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
1795 GstRTSPStreamPrivate *priv = stream->priv;
1796 GstElement *oldenc, *enc;
1800 if (priv->idx != session)
1803 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
1805 oldenc = priv->srtpenc;
1806 enc = get_rtp_encoder (stream, session);
1807 name = g_strdup_printf ("rtp_sink_%d", session);
1808 pad = gst_element_get_request_pad (enc, name);
1810 gst_object_unref (pad);
1813 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
1820 request_rtcp_encoder (GstElement * rtpbin, guint session,
1821 GstRTSPStream * stream)
1823 GstRTSPStreamPrivate *priv = stream->priv;
1824 GstElement *oldenc, *enc;
1828 if (priv->idx != session)
1831 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
1833 oldenc = priv->srtpenc;
1834 enc = get_rtp_encoder (stream, session);
1835 name = g_strdup_printf ("rtcp_sink_%d", session);
1836 pad = gst_element_get_request_pad (enc, name);
1838 gst_object_unref (pad);
1841 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
1848 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
1850 GstRTSPStreamPrivate *priv = stream->priv;
1853 GST_DEBUG ("request key %08x", ssrc);
1855 g_mutex_lock (&priv->lock);
1856 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
1857 gst_caps_ref (caps);
1858 g_mutex_unlock (&priv->lock);
1864 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
1865 GstRTSPStream * stream)
1867 GstRTSPStreamPrivate *priv = stream->priv;
1869 if (priv->idx != session)
1872 if (priv->srtpdec == NULL) {
1875 name = g_strdup_printf ("srtpdec_%u", session);
1876 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
1879 g_signal_connect (priv->srtpdec, "request-key",
1880 (GCallback) request_key, stream);
1882 return gst_object_ref (priv->srtpdec);
1886 * gst_rtsp_stream_request_aux_sender:
1887 * @stream: a #GstRTSPStream
1888 * @sessid: the session id
1890 * Creating a rtxsend bin
1892 * Returns: (transfer full): a #GstElement.
1897 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
1901 GstStructure *pt_map;
1906 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1908 pt = gst_rtsp_stream_get_pt (stream);
1909 pt_s = g_strdup_printf ("%u", pt);
1910 rtx_pt = stream->priv->rtx_pt;
1912 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
1914 bin = gst_bin_new (NULL);
1915 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
1916 pt_map = gst_structure_new ("application/x-rtp-pt-map",
1917 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1918 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
1919 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
1921 gst_structure_free (pt_map);
1922 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
1924 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
1925 name = g_strdup_printf ("src_%u", sessid);
1926 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1928 gst_object_unref (pad);
1930 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
1931 name = g_strdup_printf ("sink_%u", sessid);
1932 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1934 gst_object_unref (pad);
1940 * gst_rtsp_stream_set_pt_map:
1941 * @stream: a #GstRTSPStream
1945 * Configure a pt map between @pt and @caps.
1948 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
1950 GstRTSPStreamPrivate *priv = stream->priv;
1952 g_mutex_lock (&priv->lock);
1953 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
1954 g_mutex_unlock (&priv->lock);
1958 request_pt_map (GstElement * rtpbin, guint session, guint pt,
1959 GstRTSPStream * stream)
1961 GstRTSPStreamPrivate *priv = stream->priv;
1962 GstCaps *caps = NULL;
1964 g_mutex_lock (&priv->lock);
1966 if (priv->idx == session) {
1967 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
1969 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
1970 gst_caps_ref (caps);
1972 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
1976 g_mutex_unlock (&priv->lock);
1982 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
1984 GstRTSPStreamPrivate *priv = stream->priv;
1986 GstPadLinkReturn ret;
1989 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
1990 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
1992 name = gst_pad_get_name (pad);
1993 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
1999 if (priv->idx != sessid)
2002 if (gst_pad_is_linked (priv->sinkpad)) {
2003 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2004 GST_DEBUG_PAD_NAME (priv->sinkpad));
2008 /* link the RTP pad to the session manager, it should not really fail unless
2009 * this is not really an RTP pad */
2010 ret = gst_pad_link (pad, priv->sinkpad);
2011 if (ret != GST_PAD_LINK_OK)
2013 priv->recv_rtp_src = gst_object_ref (pad);
2020 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2021 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2026 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2027 GstRTSPStream * stream)
2029 /* TODO: What to do here other than this? */
2030 GST_DEBUG ("Stream %p: Got EOS", stream);
2031 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2035 * gst_rtsp_stream_join_bin:
2036 * @stream: a #GstRTSPStream
2037 * @bin: (transfer none): a #GstBin to join
2038 * @rtpbin: (transfer none): a rtpbin element in @bin
2039 * @state: the target state of the new elements
2041 * Join the #GstBin @bin that contains the element @rtpbin.
2043 * @stream will link to @rtpbin, which must be inside @bin. The elements
2044 * added to @bin will be set to the state given in @state.
2046 * Returns: %TRUE on success.
2049 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2050 GstElement * rtpbin, GstState state)
2052 GstRTSPStreamPrivate *priv;
2056 GstPad *pad, *sinkpad, *selpad;
2057 GstPadLinkReturn ret;
2059 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2060 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2061 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2063 priv = stream->priv;
2065 g_mutex_lock (&priv->lock);
2066 if (priv->is_joined)
2069 /* create a session with the same index as the stream */
2072 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2074 if (!alloc_ports (stream))
2077 /* update the dscp qos field in the sinks */
2078 update_dscp_qos (stream);
2080 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2081 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2083 g_signal_connect (rtpbin, "request-rtp-encoder",
2084 (GCallback) request_rtp_encoder, stream);
2085 g_signal_connect (rtpbin, "request-rtcp-encoder",
2086 (GCallback) request_rtcp_encoder, stream);
2087 g_signal_connect (rtpbin, "request-rtp-decoder",
2088 (GCallback) request_rtp_rtcp_decoder, stream);
2089 g_signal_connect (rtpbin, "request-rtcp-decoder",
2090 (GCallback) request_rtp_rtcp_decoder, stream);
2093 if (priv->sinkpad) {
2094 g_signal_connect (rtpbin, "request-pt-map",
2095 (GCallback) request_pt_map, stream);
2098 /* get a pad for sending RTP */
2099 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2100 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2104 /* link the RTP pad to the session manager, it should not really fail unless
2105 * this is not really an RTP pad */
2106 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2107 if (ret != GST_PAD_LINK_OK)
2110 /* Need to connect our sinkpad from here */
2111 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2113 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2116 /* get pads from the RTP session element for sending and receiving
2118 name = g_strdup_printf ("send_rtp_src_%u", idx);
2119 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2121 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2122 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2125 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2126 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2128 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2129 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2132 /* get the session */
2133 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2135 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2137 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2139 g_signal_connect (priv->session, "on-ssrc-active",
2140 (GCallback) on_ssrc_active, stream);
2141 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2143 g_signal_connect (priv->session, "on-bye-timeout",
2144 (GCallback) on_bye_timeout, stream);
2145 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2148 for (i = 0; i < 2; i++) {
2149 GstPad *teepad, *queuepad;
2150 /* For the sender we create this bit of pipeline for both
2151 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2152 * we need to add a queue before appsink and udpsink to make
2153 * the pipeline not block. For the TCP case, we want to pump
2154 * data to the client as fast as possible.
2156 * .--------. .-----. .---------. .---------.
2157 * | rtpbin | | tee | | queue | | udpsink |
2158 * | send->sink src->sink src->sink |
2159 * '--------' | | '---------' '---------'
2160 * | | .---------. .---------.
2161 * | | | queue | | appsink |
2162 * | src->sink src->sink |
2163 * '-----' '---------' '---------'
2165 * When only UDP is allowed, we skip the tee, queue and appsink and link the
2166 * udpsink directly to the session.
2169 gst_bin_add (bin, priv->udpsink[i]);
2170 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
2172 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
2173 /* make tee for RTP/RTCP */
2174 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2175 gst_bin_add (bin, priv->tee[i]);
2177 /* and link to rtpbin send pad */
2178 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2179 gst_pad_link (priv->send_src[i], pad);
2180 gst_object_unref (pad);
2182 priv->udpqueue[i] = gst_element_factory_make ("queue", NULL);
2183 g_object_set (priv->udpqueue[i], "max-size-buffers",
2184 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0), NULL);
2185 gst_bin_add (bin, priv->udpqueue[i]);
2186 /* link tee to udpqueue */
2187 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2188 pad = gst_element_get_static_pad (priv->udpqueue[i], "sink");
2189 gst_pad_link (teepad, pad);
2190 gst_object_unref (pad);
2191 gst_object_unref (teepad);
2193 /* link udpqueue to udpsink */
2194 queuepad = gst_element_get_static_pad (priv->udpqueue[i], "src");
2195 gst_pad_link (queuepad, sinkpad);
2196 gst_object_unref (queuepad);
2199 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
2200 g_object_set (priv->appqueue[i], "max-size-buffers",
2201 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0), NULL);
2202 gst_bin_add (bin, priv->appqueue[i]);
2203 /* and link to tee */
2204 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2205 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
2206 gst_pad_link (teepad, pad);
2207 gst_object_unref (pad);
2208 gst_object_unref (teepad);
2211 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2212 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2213 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2214 gst_bin_add (bin, priv->appsink[i]);
2215 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2216 &sink_cb, stream, NULL);
2217 /* and link to queue */
2218 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
2219 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2220 gst_pad_link (queuepad, pad);
2221 gst_object_unref (pad);
2222 gst_object_unref (queuepad);
2224 /* else only udpsink needed, link it to the session */
2225 gst_pad_link (priv->send_src[i], sinkpad);
2227 gst_object_unref (sinkpad);
2229 /* For the receiver we create this bit of pipeline for both
2230 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2231 * and it is all funneled into the rtpbin receive pad.
2233 * .--------. .--------. .--------.
2234 * | udpsrc | | funnel | | rtpbin |
2235 * | src->sink src->sink |
2236 * '--------' | | '--------'
2240 * '--------' '--------'
2242 /* make funnel for the RTP/RTCP receivers */
2243 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2244 gst_bin_add (bin, priv->funnel[i]);
2246 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2247 gst_pad_link (pad, priv->recv_sink[i]);
2248 gst_object_unref (pad);
2250 if (priv->udpsrc_v4[i]) {
2252 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2253 * values. This is only relevant for PLAY pipelines */
2254 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
2255 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
2258 gst_bin_add (bin, priv->udpsrc_v4[i]);
2260 /* and link to the funnel v4 */
2261 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2262 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
2263 gst_pad_link (pad, selpad);
2264 gst_object_unref (pad);
2265 gst_object_unref (selpad);
2268 if (priv->udpsrc_v6[i]) {
2270 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
2271 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
2273 gst_bin_add (bin, priv->udpsrc_v6[i]);
2275 /* and link to the funnel v6 */
2276 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2277 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2278 gst_pad_link (pad, selpad);
2279 gst_object_unref (pad);
2280 gst_object_unref (selpad);
2283 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
2284 /* make and add appsrc */
2285 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2286 priv->appsrc_base_time[i] = -1;
2287 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, NULL);
2288 gst_bin_add (bin, priv->appsrc[i]);
2289 /* and link to the funnel */
2290 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2291 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2292 gst_pad_link (pad, selpad);
2293 gst_object_unref (pad);
2294 gst_object_unref (selpad);
2297 /* check if we need to set to a special state */
2298 if (state != GST_STATE_NULL) {
2299 if (priv->udpsink[i])
2300 gst_element_set_state (priv->udpsink[i], state);
2301 if (priv->appsink[i])
2302 gst_element_set_state (priv->appsink[i], state);
2303 if (priv->appqueue[i])
2304 gst_element_set_state (priv->appqueue[i], state);
2305 if (priv->udpqueue[i])
2306 gst_element_set_state (priv->udpqueue[i], state);
2308 gst_element_set_state (priv->tee[i], state);
2309 if (priv->funnel[i])
2310 gst_element_set_state (priv->funnel[i], state);
2311 if (priv->appsrc[i])
2312 gst_element_set_state (priv->appsrc[i], state);
2316 /* be notified of caps changes */
2317 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2318 (GCallback) caps_notify, stream);
2320 priv->is_joined = TRUE;
2321 g_mutex_unlock (&priv->lock);
2328 g_mutex_unlock (&priv->lock);
2333 g_mutex_unlock (&priv->lock);
2334 GST_WARNING ("failed to allocate ports %u", idx);
2339 GST_WARNING ("failed to link stream %u", idx);
2340 gst_object_unref (priv->send_rtp_sink);
2341 priv->send_rtp_sink = NULL;
2342 g_mutex_unlock (&priv->lock);
2348 * gst_rtsp_stream_leave_bin:
2349 * @stream: a #GstRTSPStream
2350 * @bin: (transfer none): a #GstBin
2351 * @rtpbin: (transfer none): a rtpbin #GstElement
2353 * Remove the elements of @stream from @bin.
2355 * Return: %TRUE on success.
2358 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2359 GstElement * rtpbin)
2361 GstRTSPStreamPrivate *priv;
2365 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2366 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2367 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2369 priv = stream->priv;
2371 g_mutex_lock (&priv->lock);
2372 if (!priv->is_joined)
2373 goto was_not_joined;
2375 /* all transports must be removed by now */
2376 if (priv->transports != NULL)
2377 goto transports_not_removed;
2379 clear_tr_cache (priv, TRUE);
2380 clear_tr_cache (priv, FALSE);
2382 GST_INFO ("stream %p leaving bin", stream);
2385 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2386 } else if (priv->recv_rtp_src) {
2387 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2388 gst_object_unref (priv->recv_rtp_src);
2389 priv->recv_rtp_src = NULL;
2391 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2392 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2393 gst_object_unref (priv->send_rtp_sink);
2394 priv->send_rtp_sink = NULL;
2396 for (i = 0; i < 2; i++) {
2397 if (priv->udpsink[i])
2398 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
2399 if (priv->appsink[i])
2400 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
2401 if (priv->appqueue[i])
2402 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
2403 if (priv->udpqueue[i])
2404 gst_element_set_state (priv->udpqueue[i], GST_STATE_NULL);
2406 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
2407 if (priv->funnel[i])
2408 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
2409 if (priv->appsrc[i])
2410 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2411 if (priv->udpsrc_v4[i]) {
2412 /* and set udpsrc to NULL now before removing */
2413 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
2414 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2415 /* removing them should also nicely release the request
2416 * pads when they finalize */
2417 gst_bin_remove (bin, priv->udpsrc_v4[i]);
2419 if (priv->udpsrc_v6[i]) {
2420 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
2421 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2422 gst_bin_remove (bin, priv->udpsrc_v6[i]);
2425 for (l = priv->transport_sources; l; l = l->next) {
2426 GstRTSPMulticastTransportSource *s = l->data;
2431 gst_element_set_locked_state (s->udpsrc[i], FALSE);
2432 gst_element_set_state (s->udpsrc[i], GST_STATE_NULL);
2433 gst_bin_remove (bin, s->udpsrc[i]);
2436 if (priv->udpsink[i])
2437 gst_bin_remove (bin, priv->udpsink[i]);
2438 if (priv->appsrc[i])
2439 gst_bin_remove (bin, priv->appsrc[i]);
2440 if (priv->appsink[i])
2441 gst_bin_remove (bin, priv->appsink[i]);
2442 if (priv->appqueue[i])
2443 gst_bin_remove (bin, priv->appqueue[i]);
2444 if (priv->udpqueue[i])
2445 gst_bin_remove (bin, priv->udpqueue[i]);
2447 gst_bin_remove (bin, priv->tee[i]);
2448 if (priv->funnel[i])
2449 gst_bin_remove (bin, priv->funnel[i]);
2451 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2452 gst_object_unref (priv->recv_sink[i]);
2453 priv->recv_sink[i] = NULL;
2455 priv->udpsrc_v4[i] = NULL;
2456 priv->udpsrc_v6[i] = NULL;
2457 priv->udpsink[i] = NULL;
2458 priv->appsrc[i] = NULL;
2459 priv->appsink[i] = NULL;
2460 priv->appqueue[i] = NULL;
2461 priv->udpqueue[i] = NULL;
2462 priv->tee[i] = NULL;
2463 priv->funnel[i] = NULL;
2466 for (l = priv->transport_sources; l; l = l->next) {
2467 GstRTSPMulticastTransportSource *s = l->data;
2468 g_slice_free (GstRTSPMulticastTransportSource, s);
2470 g_list_free (priv->transport_sources);
2471 priv->transport_sources = NULL;
2473 gst_object_unref (priv->send_src[0]);
2474 priv->send_src[0] = NULL;
2476 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2477 gst_object_unref (priv->send_src[1]);
2478 priv->send_src[1] = NULL;
2480 g_object_unref (priv->session);
2481 priv->session = NULL;
2483 gst_caps_unref (priv->caps);
2487 gst_object_unref (priv->srtpenc);
2489 gst_object_unref (priv->srtpdec);
2491 priv->is_joined = FALSE;
2492 g_mutex_unlock (&priv->lock);
2498 g_mutex_unlock (&priv->lock);
2501 transports_not_removed:
2503 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2504 g_mutex_unlock (&priv->lock);
2510 * gst_rtsp_stream_get_rtpinfo:
2511 * @stream: a #GstRTSPStream
2512 * @rtptime: (allow-none): result RTP timestamp
2513 * @seq: (allow-none): result RTP seqnum
2514 * @clock_rate: (allow-none): the clock rate
2515 * @running_time: (allow-none): result running-time
2517 * Retrieve the current rtptime, seq and running-time. This is used to
2518 * construct a RTPInfo reply header.
2520 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2523 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2524 guint * rtptime, guint * seq, guint * clock_rate,
2525 GstClockTime * running_time)
2527 GstRTSPStreamPrivate *priv;
2528 GstStructure *stats;
2529 GObjectClass *payobjclass;
2531 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2533 priv = stream->priv;
2535 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2537 g_mutex_lock (&priv->lock);
2539 /* First try to extract the information from the last buffer on the sinks.
2540 * This will have a more accurate sequence number and timestamp, as between
2541 * the payloader and the sink there can be some queues
2543 if (priv->udpsink[0] || priv->appsink[0]) {
2544 GstSample *last_sample;
2546 if (priv->udpsink[0])
2547 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
2549 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
2554 GstSegment *segment;
2555 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
2557 caps = gst_sample_get_caps (last_sample);
2558 buffer = gst_sample_get_buffer (last_sample);
2559 segment = gst_sample_get_segment (last_sample);
2561 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
2563 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
2567 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
2570 gst_rtp_buffer_unmap (&rtp_buffer);
2574 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
2575 GST_BUFFER_TIMESTAMP (buffer));
2579 GstStructure *s = gst_caps_get_structure (caps, 0);
2581 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
2583 if (*clock_rate == 0 && running_time)
2584 *running_time = GST_CLOCK_TIME_NONE;
2586 gst_sample_unref (last_sample);
2590 gst_sample_unref (last_sample);
2595 if (g_object_class_find_property (payobjclass, "stats")) {
2596 g_object_get (priv->payloader, "stats", &stats, NULL);
2601 gst_structure_get_uint (stats, "seqnum", seq);
2604 gst_structure_get_uint (stats, "timestamp", rtptime);
2607 gst_structure_get_clock_time (stats, "running-time", running_time);
2610 gst_structure_get_uint (stats, "clock-rate", clock_rate);
2611 if (*clock_rate == 0 && running_time)
2612 *running_time = GST_CLOCK_TIME_NONE;
2614 gst_structure_free (stats);
2616 if (!g_object_class_find_property (payobjclass, "seqnum") ||
2617 !g_object_class_find_property (payobjclass, "timestamp"))
2621 g_object_get (priv->payloader, "seqnum", seq, NULL);
2624 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
2627 *running_time = GST_CLOCK_TIME_NONE;
2631 g_mutex_unlock (&priv->lock);
2638 GST_WARNING ("Could not get payloader stats");
2639 g_mutex_unlock (&priv->lock);
2645 * gst_rtsp_stream_get_caps:
2646 * @stream: a #GstRTSPStream
2648 * Retrieve the current caps of @stream.
2650 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
2654 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
2656 GstRTSPStreamPrivate *priv;
2659 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2661 priv = stream->priv;
2663 g_mutex_lock (&priv->lock);
2664 if ((result = priv->caps))
2665 gst_caps_ref (result);
2666 g_mutex_unlock (&priv->lock);
2672 * gst_rtsp_stream_recv_rtp:
2673 * @stream: a #GstRTSPStream
2674 * @buffer: (transfer full): a #GstBuffer
2676 * Handle an RTP buffer for the stream. This method is usually called when a
2677 * message has been received from a client using the TCP transport.
2679 * This function takes ownership of @buffer.
2681 * Returns: a GstFlowReturn.
2684 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
2686 GstRTSPStreamPrivate *priv;
2688 GstElement *element;
2690 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2691 priv = stream->priv;
2692 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2693 g_return_val_if_fail (priv->is_joined, FALSE);
2695 g_mutex_lock (&priv->lock);
2696 if (priv->appsrc[0])
2697 element = gst_object_ref (priv->appsrc[0]);
2700 g_mutex_unlock (&priv->lock);
2703 if (priv->appsrc_base_time[0] == -1) {
2704 /* Take current running_time. This timestamp will be put on
2705 * the first buffer of each stream because we are a live source and so we
2706 * timestamp with the running_time. When we are dealing with TCP, we also
2707 * only timestamp the first buffer (using the DISCONT flag) because a server
2708 * typically bursts data, for which we don't want to compensate by speeding
2709 * up the media. The other timestamps will be interpollated from this one
2710 * using the RTP timestamps. */
2711 GST_OBJECT_LOCK (element);
2712 if (GST_ELEMENT_CLOCK (element)) {
2714 GstClockTime base_time;
2716 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
2717 base_time = GST_ELEMENT_CAST (element)->base_time;
2719 priv->appsrc_base_time[0] = now - base_time;
2720 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
2721 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
2722 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
2723 GST_TIME_ARGS (base_time));
2725 GST_OBJECT_UNLOCK (element);
2728 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2729 gst_object_unref (element);
2737 * gst_rtsp_stream_recv_rtcp:
2738 * @stream: a #GstRTSPStream
2739 * @buffer: (transfer full): a #GstBuffer
2741 * Handle an RTCP buffer for the stream. This method is usually called when a
2742 * message has been received from a client using the TCP transport.
2744 * This function takes ownership of @buffer.
2746 * Returns: a GstFlowReturn.
2749 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
2751 GstRTSPStreamPrivate *priv;
2753 GstElement *element;
2755 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2756 priv = stream->priv;
2757 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2759 if (!priv->is_joined) {
2760 gst_buffer_unref (buffer);
2761 return GST_FLOW_NOT_LINKED;
2763 g_mutex_lock (&priv->lock);
2764 if (priv->appsrc[1])
2765 element = gst_object_ref (priv->appsrc[1]);
2768 g_mutex_unlock (&priv->lock);
2771 if (priv->appsrc_base_time[1] == -1) {
2772 /* Take current running_time. This timestamp will be put on
2773 * the first buffer of each stream because we are a live source and so we
2774 * timestamp with the running_time. When we are dealing with TCP, we also
2775 * only timestamp the first buffer (using the DISCONT flag) because a server
2776 * typically bursts data, for which we don't want to compensate by speeding
2777 * up the media. The other timestamps will be interpollated from this one
2778 * using the RTP timestamps. */
2779 GST_OBJECT_LOCK (element);
2780 if (GST_ELEMENT_CLOCK (element)) {
2782 GstClockTime base_time;
2784 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
2785 base_time = GST_ELEMENT_CAST (element)->base_time;
2787 priv->appsrc_base_time[1] = now - base_time;
2788 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
2789 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
2790 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
2791 GST_TIME_ARGS (base_time));
2793 GST_OBJECT_UNLOCK (element);
2796 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2797 gst_object_unref (element);
2800 gst_buffer_unref (buffer);
2805 /* must be called with lock */
2807 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2810 GstRTSPStreamPrivate *priv = stream->priv;
2811 const GstRTSPTransport *tr;
2813 tr = gst_rtsp_stream_transport_get_transport (trans);
2815 switch (tr->lower_transport) {
2816 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2818 GstRTSPMulticastTransportSource *source;
2821 bin = GST_BIN (gst_object_get_parent (GST_OBJECT (priv->funnel[0])));
2826 GstPad *selpad, *pad;
2828 source = g_slice_new0 (GstRTSPMulticastTransportSource);
2829 source->transport = trans;
2831 for (i = 0; i < 2; i++) {
2833 g_strdup_printf ("udp://%s:%d", tr->destination,
2834 (i == 0) ? tr->port.min : tr->port.max);
2836 gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2840 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2841 * values. This is only relevant for PLAY pipelines */
2842 gst_element_set_state (source->udpsrc[i], GST_STATE_PLAYING);
2843 gst_element_set_locked_state (source->udpsrc[i], TRUE);
2846 gst_bin_add (bin, source->udpsrc[i]);
2848 /* and link to the funnel v4 */
2849 source->selpad[i] = selpad =
2850 gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2851 pad = gst_element_get_static_pad (source->udpsrc[i], "src");
2852 gst_pad_link (pad, selpad);
2853 gst_object_unref (pad);
2854 gst_object_unref (selpad);
2857 priv->transport_sources =
2858 g_list_prepend (priv->transport_sources, source);
2862 for (l = priv->transport_sources; l; l = l->next) {
2865 if (source->transport == trans) {
2866 priv->transport_sources =
2867 g_list_delete_link (priv->transport_sources, l);
2875 for (i = 0; i < 2; i++) {
2876 /* Will automatically unlink everything */
2877 gst_bin_remove (bin,
2878 GST_ELEMENT (gst_object_ref (source->udpsrc[i])));
2880 gst_element_set_state (source->udpsrc[i], GST_STATE_NULL);
2881 gst_object_unref (source->udpsrc[i]);
2883 gst_element_release_request_pad (priv->funnel[i],
2887 g_slice_free (GstRTSPMulticastTransportSource, source);
2891 gst_object_unref (bin);
2893 /* fall through for the generic case */
2895 case GST_RTSP_LOWER_TRANS_UDP:
2901 dest = tr->destination;
2902 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2907 min = tr->client_port.min;
2908 max = tr->client_port.max;
2913 GST_INFO ("setting ttl-mc %d", ttl);
2914 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
2915 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
2917 GST_INFO ("adding %s:%d-%d", dest, min, max);
2918 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
2919 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
2920 priv->transports = g_list_prepend (priv->transports, trans);
2922 GST_INFO ("removing %s:%d-%d", dest, min, max);
2923 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
2924 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
2925 priv->transports = g_list_remove (priv->transports, trans);
2927 priv->transports_cookie++;
2930 case GST_RTSP_LOWER_TRANS_TCP:
2932 GST_INFO ("adding TCP %s", tr->destination);
2933 priv->transports = g_list_prepend (priv->transports, trans);
2935 GST_INFO ("removing TCP %s", tr->destination);
2936 priv->transports = g_list_remove (priv->transports, trans);
2938 priv->transports_cookie++;
2941 goto unknown_transport;
2948 GST_INFO ("Unknown transport %d", tr->lower_transport);
2955 * gst_rtsp_stream_add_transport:
2956 * @stream: a #GstRTSPStream
2957 * @trans: (transfer none): a #GstRTSPStreamTransport
2959 * Add the transport in @trans to @stream. The media of @stream will
2960 * then also be send to the values configured in @trans.
2962 * @stream must be joined to a bin.
2964 * @trans must contain a valid #GstRTSPTransport.
2966 * Returns: %TRUE if @trans was added
2969 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
2970 GstRTSPStreamTransport * trans)
2972 GstRTSPStreamPrivate *priv;
2975 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2976 priv = stream->priv;
2977 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2978 g_return_val_if_fail (priv->is_joined, FALSE);
2980 g_mutex_lock (&priv->lock);
2981 res = update_transport (stream, trans, TRUE);
2982 g_mutex_unlock (&priv->lock);
2988 * gst_rtsp_stream_remove_transport:
2989 * @stream: a #GstRTSPStream
2990 * @trans: (transfer none): a #GstRTSPStreamTransport
2992 * Remove the transport in @trans from @stream. The media of @stream will
2993 * not be sent to the values configured in @trans.
2995 * @stream must be joined to a bin.
2997 * @trans must contain a valid #GstRTSPTransport.
2999 * Returns: %TRUE if @trans was removed
3002 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3003 GstRTSPStreamTransport * trans)
3005 GstRTSPStreamPrivate *priv;
3008 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3009 priv = stream->priv;
3010 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3011 g_return_val_if_fail (priv->is_joined, FALSE);
3013 g_mutex_lock (&priv->lock);
3014 res = update_transport (stream, trans, FALSE);
3015 g_mutex_unlock (&priv->lock);
3021 * gst_rtsp_stream_update_crypto:
3022 * @stream: a #GstRTSPStream
3024 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3026 * Update the new crypto information for @ssrc in @stream. If information
3027 * for @ssrc did not exist, it will be added. If information
3028 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3029 * be removed from @stream.
3031 * Returns: %TRUE if @crypto could be updated
3034 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3035 guint ssrc, GstCaps * crypto)
3037 GstRTSPStreamPrivate *priv;
3039 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3040 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3042 priv = stream->priv;
3044 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3046 g_mutex_lock (&priv->lock);
3048 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3049 gst_caps_ref (crypto));
3051 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3052 g_mutex_unlock (&priv->lock);
3058 * gst_rtsp_stream_get_rtp_socket:
3059 * @stream: a #GstRTSPStream
3060 * @family: the socket family
3062 * Get the RTP socket from @stream for a @family.
3064 * @stream must be joined to a bin.
3066 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3067 * socket could be allocated for @family. Unref after usage
3070 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3072 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3076 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3077 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3078 family == G_SOCKET_FAMILY_IPV6, NULL);
3079 g_return_val_if_fail (priv->udpsink[0], NULL);
3081 if (family == G_SOCKET_FAMILY_IPV6)
3086 g_object_get (priv->udpsink[0], name, &socket, NULL);
3092 * gst_rtsp_stream_get_rtcp_socket:
3093 * @stream: a #GstRTSPStream
3094 * @family: the socket family
3096 * Get the RTCP socket from @stream for a @family.
3098 * @stream must be joined to a bin.
3100 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3101 * socket could be allocated for @family. Unref after usage
3104 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3106 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3110 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3111 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3112 family == G_SOCKET_FAMILY_IPV6, NULL);
3113 g_return_val_if_fail (priv->udpsink[1], NULL);
3115 if (family == G_SOCKET_FAMILY_IPV6)
3120 g_object_get (priv->udpsink[1], name, &socket, NULL);
3126 * gst_rtsp_stream_set_seqnum:
3127 * @stream: a #GstRTSPStream
3128 * @seqnum: a new sequence number
3130 * Configure the sequence number in the payloader of @stream to @seqnum.
3133 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3135 GstRTSPStreamPrivate *priv;
3137 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3139 priv = stream->priv;
3141 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3145 * gst_rtsp_stream_get_seqnum:
3146 * @stream: a #GstRTSPStream
3148 * Get the configured sequence number in the payloader of @stream.
3150 * Returns: the sequence number of the payloader.
3153 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3155 GstRTSPStreamPrivate *priv;
3158 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3160 priv = stream->priv;
3162 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3168 gst_rtsp_stream_get_udp_sent_bytes (GstRTSPStream *stream)
3170 GstRTSPStreamPrivate *priv;
3173 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3175 priv = stream->priv;
3177 g_object_get (G_OBJECT (priv->udpsink[0]), "bytes-to-serve", &bytes, NULL);
3183 * gst_rtsp_stream_transport_filter:
3184 * @stream: a #GstRTSPStream
3185 * @func: (scope call) (allow-none): a callback
3186 * @user_data: (closure): user data passed to @func
3188 * Call @func for each transport managed by @stream. The result value of @func
3189 * determines what happens to the transport. @func will be called with @stream
3190 * locked so no further actions on @stream can be performed from @func.
3192 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3195 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3197 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3198 * will also be added with an additional ref to the result #GList of this
3201 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3203 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3204 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3205 * element in the #GList should be unreffed before the list is freed.
3208 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3209 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3211 GstRTSPStreamPrivate *priv;
3212 GList *result, *walk, *next;
3213 GHashTable *visited = NULL;
3216 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3218 priv = stream->priv;
3222 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3224 g_mutex_lock (&priv->lock);
3226 cookie = priv->transports_cookie;
3227 for (walk = priv->transports; walk; walk = next) {
3228 GstRTSPStreamTransport *trans = walk->data;
3229 GstRTSPFilterResult res;
3232 next = g_list_next (walk);
3235 /* only visit each transport once */
3236 if (g_hash_table_contains (visited, trans))
3239 g_hash_table_add (visited, g_object_ref (trans));
3240 g_mutex_unlock (&priv->lock);
3242 res = func (stream, trans, user_data);
3244 g_mutex_lock (&priv->lock);
3246 res = GST_RTSP_FILTER_REF;
3248 changed = (cookie != priv->transports_cookie);
3251 case GST_RTSP_FILTER_REMOVE:
3252 update_transport (stream, trans, FALSE);
3254 case GST_RTSP_FILTER_REF:
3255 result = g_list_prepend (result, g_object_ref (trans));
3257 case GST_RTSP_FILTER_KEEP:
3264 g_mutex_unlock (&priv->lock);
3267 g_hash_table_unref (visited);
3272 static GstPadProbeReturn
3273 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3275 GstRTSPStreamPrivate *priv;
3276 GstRTSPStream *stream;
3279 priv = stream->priv;
3281 GST_DEBUG_OBJECT (pad, "now blocking");
3283 g_mutex_lock (&priv->lock);
3284 priv->blocking = TRUE;
3285 g_mutex_unlock (&priv->lock);
3287 gst_element_post_message (priv->payloader,
3288 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3289 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3291 return GST_PAD_PROBE_OK;
3295 * gst_rtsp_stream_set_blocked:
3296 * @stream: a #GstRTSPStream
3297 * @blocked: boolean indicating we should block or unblock
3299 * Blocks or unblocks the dataflow on @stream.
3301 * Returns: %TRUE on success
3304 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3306 GstRTSPStreamPrivate *priv;
3308 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3310 priv = stream->priv;
3312 g_mutex_lock (&priv->lock);
3314 priv->blocking = FALSE;
3315 if (priv->blocked_id == 0) {
3316 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3317 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3318 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3319 g_object_ref (stream), g_object_unref);
3322 if (priv->blocked_id != 0) {
3323 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3324 priv->blocked_id = 0;
3325 priv->blocking = FALSE;
3328 g_mutex_unlock (&priv->lock);
3334 * gst_rtsp_stream_is_blocking:
3335 * @stream: a #GstRTSPStream
3337 * Check if @stream is blocking on a #GstBuffer.
3339 * Returns: %TRUE if @stream is blocking
3342 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3344 GstRTSPStreamPrivate *priv;
3347 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3349 priv = stream->priv;
3351 g_mutex_lock (&priv->lock);
3352 result = priv->blocking;
3353 g_mutex_unlock (&priv->lock);
3359 * gst_rtsp_stream_query_position:
3360 * @stream: a #GstRTSPStream
3362 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3363 * the RTP parts of the pipeline and not the RTCP parts.
3365 * Returns: %TRUE if the position could be queried
3368 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3370 GstRTSPStreamPrivate *priv;
3374 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3376 priv = stream->priv;
3378 g_mutex_lock (&priv->lock);
3379 if ((sink = priv->udpsink[0]))
3380 gst_object_ref (sink);
3381 g_mutex_unlock (&priv->lock);
3386 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3387 gst_object_unref (sink);
3393 * gst_rtsp_stream_query_stop:
3394 * @stream: a #GstRTSPStream
3396 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3397 * the RTP parts of the pipeline and not the RTCP parts.
3399 * Returns: %TRUE if the stop could be queried
3402 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3404 GstRTSPStreamPrivate *priv;
3409 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3411 priv = stream->priv;
3413 g_mutex_lock (&priv->lock);
3414 if ((sink = priv->udpsink[0]))
3415 gst_object_ref (sink);
3416 g_mutex_unlock (&priv->lock);
3421 query = gst_query_new_segment (GST_FORMAT_TIME);
3422 if ((ret = gst_element_query (sink, query))) {
3425 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3426 if (format != GST_FORMAT_TIME)
3429 gst_query_unref (query);
3430 gst_object_unref (sink);