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,
190 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
191 #define GST_CAT_DEFAULT rtsp_stream_debug
193 static GQuark ssrc_stream_map_key;
195 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
196 GValue * value, GParamSpec * pspec);
197 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
198 const GValue * value, GParamSpec * pspec);
200 static void gst_rtsp_stream_finalize (GObject * obj);
202 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
204 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
207 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
209 GObjectClass *gobject_class;
211 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
213 gobject_class = G_OBJECT_CLASS (klass);
215 gobject_class->get_property = gst_rtsp_stream_get_property;
216 gobject_class->set_property = gst_rtsp_stream_set_property;
217 gobject_class->finalize = gst_rtsp_stream_finalize;
219 g_object_class_install_property (gobject_class, PROP_CONTROL,
220 g_param_spec_string ("control", "Control",
221 "The control string for this stream", DEFAULT_CONTROL,
222 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
224 g_object_class_install_property (gobject_class, PROP_PROFILES,
225 g_param_spec_flags ("profiles", "Profiles",
226 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
227 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
230 g_param_spec_flags ("protocols", "Protocols",
231 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
232 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
234 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
235 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
236 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
237 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
239 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
240 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
241 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
242 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
244 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
246 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
250 gst_rtsp_stream_init (GstRTSPStream * stream)
252 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
254 GST_DEBUG ("new stream %p", stream);
259 priv->control = g_strdup (DEFAULT_CONTROL);
260 priv->profiles = DEFAULT_PROFILES;
261 priv->protocols = DEFAULT_PROTOCOLS;
263 g_mutex_init (&priv->lock);
265 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
266 NULL, (GDestroyNotify) gst_caps_unref);
267 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
268 (GDestroyNotify) gst_caps_unref);
272 gst_rtsp_stream_finalize (GObject * obj)
274 GstRTSPStream *stream;
275 GstRTSPStreamPrivate *priv;
277 stream = GST_RTSP_STREAM (obj);
280 GST_DEBUG ("finalize stream %p", stream);
282 /* we really need to be unjoined now */
283 g_return_if_fail (!priv->is_joined);
286 gst_rtsp_address_free (priv->addr_v4);
288 gst_rtsp_address_free (priv->addr_v6);
289 if (priv->server_addr_v4)
290 gst_rtsp_address_free (priv->server_addr_v4);
291 if (priv->server_addr_v6)
292 gst_rtsp_address_free (priv->server_addr_v6);
294 g_object_unref (priv->pool);
296 g_object_unref (priv->rtxsend);
298 gst_object_unref (priv->payloader);
300 gst_object_unref (priv->srcpad);
302 gst_object_unref (priv->sinkpad);
303 g_free (priv->control);
304 g_mutex_clear (&priv->lock);
306 g_hash_table_unref (priv->keys);
307 g_hash_table_destroy (priv->ptmap);
309 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
313 gst_rtsp_stream_get_property (GObject * object, guint propid,
314 GValue * value, GParamSpec * pspec)
316 GstRTSPStream *stream = GST_RTSP_STREAM (object);
320 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
323 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
326 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
329 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
334 gst_rtsp_stream_set_property (GObject * object, guint propid,
335 const GValue * value, GParamSpec * pspec)
337 GstRTSPStream *stream = GST_RTSP_STREAM (object);
341 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
344 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
347 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
350 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
355 * gst_rtsp_stream_new:
358 * @payloader: a #GstElement
360 * Create a new media stream with index @idx that handles RTP data on
361 * @pad and has a payloader element @payloader if @pad is a source pad
362 * or a depayloader element @payloader if @pad is a sink pad.
364 * Returns: (transfer full): a new #GstRTSPStream
367 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
369 GstRTSPStreamPrivate *priv;
370 GstRTSPStream *stream;
372 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
373 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
375 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
378 priv->payloader = gst_object_ref (payloader);
379 if (GST_PAD_IS_SRC (pad))
380 priv->srcpad = gst_object_ref (pad);
382 priv->sinkpad = gst_object_ref (pad);
388 * gst_rtsp_stream_get_index:
389 * @stream: a #GstRTSPStream
391 * Get the stream index.
393 * Return: the stream index.
396 gst_rtsp_stream_get_index (GstRTSPStream * stream)
398 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
400 return stream->priv->idx;
404 * gst_rtsp_stream_get_pt:
405 * @stream: a #GstRTSPStream
407 * Get the stream payload type.
409 * Return: the stream payload type.
412 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
414 GstRTSPStreamPrivate *priv;
417 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
421 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
427 * gst_rtsp_stream_get_srcpad:
428 * @stream: a #GstRTSPStream
430 * Get the srcpad associated with @stream.
432 * Returns: (transfer full): the srcpad. Unref after usage.
435 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
437 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
439 if (!stream->priv->srcpad)
442 return gst_object_ref (stream->priv->srcpad);
446 * gst_rtsp_stream_get_sinkpad:
447 * @stream: a #GstRTSPStream
449 * Get the sinkpad associated with @stream.
451 * Returns: (transfer full): the sinkpad. Unref after usage.
454 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
456 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
458 if (!stream->priv->sinkpad)
461 return gst_object_ref (stream->priv->sinkpad);
465 * gst_rtsp_stream_get_control:
466 * @stream: a #GstRTSPStream
468 * Get the control string to identify this stream.
470 * Returns: (transfer full): the control string. g_free() after usage.
473 gst_rtsp_stream_get_control (GstRTSPStream * stream)
475 GstRTSPStreamPrivate *priv;
478 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
482 g_mutex_lock (&priv->lock);
483 if ((result = g_strdup (priv->control)) == NULL)
484 result = g_strdup_printf ("stream=%u", priv->idx);
485 g_mutex_unlock (&priv->lock);
491 * gst_rtsp_stream_set_control:
492 * @stream: a #GstRTSPStream
493 * @control: a control string
495 * Set the control string in @stream.
498 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
500 GstRTSPStreamPrivate *priv;
502 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
506 g_mutex_lock (&priv->lock);
507 g_free (priv->control);
508 priv->control = g_strdup (control);
509 g_mutex_unlock (&priv->lock);
513 * gst_rtsp_stream_has_control:
514 * @stream: a #GstRTSPStream
515 * @control: a control string
517 * Check if @stream has the control string @control.
519 * Returns: %TRUE is @stream has @control as the control string
522 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
524 GstRTSPStreamPrivate *priv;
527 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
531 g_mutex_lock (&priv->lock);
533 res = (g_strcmp0 (priv->control, control) == 0);
537 if (sscanf (control, "stream=%u", &streamid) > 0)
538 res = (streamid == priv->idx);
542 g_mutex_unlock (&priv->lock);
548 * gst_rtsp_stream_set_mtu:
549 * @stream: a #GstRTSPStream
552 * Configure the mtu in the payloader of @stream to @mtu.
555 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
557 GstRTSPStreamPrivate *priv;
559 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
563 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
565 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
569 * gst_rtsp_stream_get_mtu:
570 * @stream: a #GstRTSPStream
572 * Get the configured MTU in the payloader of @stream.
574 * Returns: the MTU of the payloader.
577 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
579 GstRTSPStreamPrivate *priv;
582 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
586 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
591 /* Update the dscp qos property on the udp sinks */
593 update_dscp_qos (GstRTSPStream * stream)
595 GstRTSPStreamPrivate *priv;
597 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
601 if (priv->udpsink[0]) {
602 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
606 if (priv->udpsink[1]) {
607 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
613 * gst_rtsp_stream_set_dscp_qos:
614 * @stream: a #GstRTSPStream
615 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
617 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
620 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
622 GstRTSPStreamPrivate *priv;
624 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
628 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
630 if (dscp_qos < -1 || dscp_qos > 63) {
631 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
635 priv->dscp_qos = dscp_qos;
637 update_dscp_qos (stream);
641 * gst_rtsp_stream_get_dscp_qos:
642 * @stream: a #GstRTSPStream
644 * Get the configured DSCP QoS in of the outgoing sockets.
646 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
649 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
651 GstRTSPStreamPrivate *priv;
653 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
657 return priv->dscp_qos;
661 * gst_rtsp_stream_is_transport_supported:
662 * @stream: a #GstRTSPStream
663 * @transport: (transfer none): a #GstRTSPTransport
665 * Check if @transport can be handled by stream
667 * Returns: %TRUE if @transport can be handled by @stream.
670 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
671 GstRTSPTransport * transport)
673 GstRTSPStreamPrivate *priv;
675 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
679 g_mutex_lock (&priv->lock);
680 if (transport->trans != GST_RTSP_TRANS_RTP)
681 goto unsupported_transmode;
683 if (!(transport->profile & priv->profiles))
684 goto unsupported_profile;
686 if (!(transport->lower_transport & priv->protocols))
687 goto unsupported_ltrans;
689 g_mutex_unlock (&priv->lock);
694 unsupported_transmode:
696 GST_DEBUG ("unsupported transport mode %d", transport->trans);
697 g_mutex_unlock (&priv->lock);
702 GST_DEBUG ("unsupported profile %d", transport->profile);
703 g_mutex_unlock (&priv->lock);
708 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
709 g_mutex_unlock (&priv->lock);
715 * gst_rtsp_stream_set_profiles:
716 * @stream: a #GstRTSPStream
717 * @profiles: the new profiles
719 * Configure the allowed profiles for @stream.
722 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
724 GstRTSPStreamPrivate *priv;
726 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
730 g_mutex_lock (&priv->lock);
731 priv->profiles = profiles;
732 g_mutex_unlock (&priv->lock);
736 * gst_rtsp_stream_get_profiles:
737 * @stream: a #GstRTSPStream
739 * Get the allowed profiles of @stream.
741 * Returns: a #GstRTSPProfile
744 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
746 GstRTSPStreamPrivate *priv;
749 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
753 g_mutex_lock (&priv->lock);
754 res = priv->profiles;
755 g_mutex_unlock (&priv->lock);
761 * gst_rtsp_stream_set_protocols:
762 * @stream: a #GstRTSPStream
763 * @protocols: the new flags
765 * Configure the allowed lower transport for @stream.
768 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
769 GstRTSPLowerTrans protocols)
771 GstRTSPStreamPrivate *priv;
773 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
777 g_mutex_lock (&priv->lock);
778 priv->protocols = protocols;
779 g_mutex_unlock (&priv->lock);
783 * gst_rtsp_stream_get_protocols:
784 * @stream: a #GstRTSPStream
786 * Get the allowed protocols of @stream.
788 * Returns: a #GstRTSPLowerTrans
791 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
793 GstRTSPStreamPrivate *priv;
794 GstRTSPLowerTrans res;
796 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
797 GST_RTSP_LOWER_TRANS_UNKNOWN);
801 g_mutex_lock (&priv->lock);
802 res = priv->protocols;
803 g_mutex_unlock (&priv->lock);
809 * gst_rtsp_stream_set_address_pool:
810 * @stream: a #GstRTSPStream
811 * @pool: (transfer none): a #GstRTSPAddressPool
813 * configure @pool to be used as the address pool of @stream.
816 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
817 GstRTSPAddressPool * pool)
819 GstRTSPStreamPrivate *priv;
820 GstRTSPAddressPool *old;
822 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
826 GST_LOG_OBJECT (stream, "set address pool %p", pool);
828 g_mutex_lock (&priv->lock);
829 if ((old = priv->pool) != pool)
830 priv->pool = pool ? g_object_ref (pool) : NULL;
833 g_mutex_unlock (&priv->lock);
836 g_object_unref (old);
840 * gst_rtsp_stream_get_address_pool:
841 * @stream: a #GstRTSPStream
843 * Get the #GstRTSPAddressPool used as the address pool of @stream.
845 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
849 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
851 GstRTSPStreamPrivate *priv;
852 GstRTSPAddressPool *result;
854 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
858 g_mutex_lock (&priv->lock);
859 if ((result = priv->pool))
860 g_object_ref (result);
861 g_mutex_unlock (&priv->lock);
867 * gst_rtsp_stream_get_multicast_address:
868 * @stream: a #GstRTSPStream
869 * @family: the #GSocketFamily
871 * Get the multicast address of @stream for @family.
873 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
874 * or %NULL when no address could be allocated. gst_rtsp_address_free()
878 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
879 GSocketFamily family)
881 GstRTSPStreamPrivate *priv;
882 GstRTSPAddress *result;
883 GstRTSPAddress **addrp;
884 GstRTSPAddressFlags flags;
886 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
890 if (family == G_SOCKET_FAMILY_IPV6) {
891 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
892 addrp = &priv->addr_v6;
894 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
895 addrp = &priv->addr_v4;
898 g_mutex_lock (&priv->lock);
899 if (*addrp == NULL) {
900 if (priv->pool == NULL)
903 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
905 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
909 result = gst_rtsp_address_copy (*addrp);
910 g_mutex_unlock (&priv->lock);
917 GST_ERROR_OBJECT (stream, "no address pool specified");
918 g_mutex_unlock (&priv->lock);
923 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
924 g_mutex_unlock (&priv->lock);
930 * gst_rtsp_stream_reserve_address:
931 * @stream: a #GstRTSPStream
932 * @address: an address
937 * Reserve @address and @port as the address and port of @stream.
939 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
940 * the address could be reserved. gst_rtsp_address_free() after usage.
943 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
944 const gchar * address, guint port, guint n_ports, guint ttl)
946 GstRTSPStreamPrivate *priv;
947 GstRTSPAddress *result;
949 GSocketFamily family;
950 GstRTSPAddress **addrp;
952 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
953 g_return_val_if_fail (address != NULL, NULL);
954 g_return_val_if_fail (port > 0, NULL);
955 g_return_val_if_fail (n_ports > 0, NULL);
956 g_return_val_if_fail (ttl > 0, NULL);
960 addr = g_inet_address_new_from_string (address);
962 GST_ERROR ("failed to get inet addr from %s", address);
963 family = G_SOCKET_FAMILY_IPV4;
965 family = g_inet_address_get_family (addr);
966 g_object_unref (addr);
969 if (family == G_SOCKET_FAMILY_IPV6)
970 addrp = &priv->addr_v6;
972 addrp = &priv->addr_v4;
974 g_mutex_lock (&priv->lock);
975 if (*addrp == NULL) {
976 GstRTSPAddressPoolResult res;
978 if (priv->pool == NULL)
981 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
982 port, n_ports, ttl, addrp);
983 if (res != GST_RTSP_ADDRESS_POOL_OK)
986 if (strcmp ((*addrp)->address, address) ||
987 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
988 (*addrp)->ttl != ttl)
989 goto different_address;
991 result = gst_rtsp_address_copy (*addrp);
992 g_mutex_unlock (&priv->lock);
999 GST_ERROR_OBJECT (stream, "no address pool specified");
1000 g_mutex_unlock (&priv->lock);
1005 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1007 g_mutex_unlock (&priv->lock);
1012 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
1013 " reserved", address);
1014 g_mutex_unlock (&priv->lock);
1020 alloc_ports_one_family (GstRTSPStream * stream, GstRTSPAddressPool * pool,
1021 gint buffer_size, GSocketFamily family, GstElement * udpsrc_out[2],
1022 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
1023 GstRTSPAddress ** server_addr_out)
1025 GstRTSPStreamPrivate *priv = stream->priv;
1026 GstStateChangeReturn ret;
1027 GstElement *udpsrc0, *udpsrc1;
1028 GstElement *udpsink0, *udpsink1;
1029 GSocket *rtp_socket = NULL;
1030 GSocket *rtcp_socket;
1031 gint tmp_rtp, tmp_rtcp;
1033 gint rtpport, rtcpport;
1034 GList *rejected_addresses = NULL;
1035 GstRTSPAddress *addr = NULL;
1036 GInetAddress *inetaddr = NULL;
1037 GSocketAddress *rtp_sockaddr = NULL;
1038 GSocketAddress *rtcp_sockaddr = NULL;
1039 const gchar *multisink_socket;
1041 if (family == G_SOCKET_FAMILY_IPV6)
1042 multisink_socket = "socket-v6";
1044 multisink_socket = "socket";
1052 /* Start with random port */
1055 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1056 G_SOCKET_PROTOCOL_UDP, NULL);
1058 goto no_udp_protocol;
1060 if (*server_addr_out)
1061 gst_rtsp_address_free (*server_addr_out);
1063 /* try to allocate 2 UDP ports, the RTP port should be an even
1064 * number and the RTCP port should be the next (uneven) port */
1067 if (rtp_socket == NULL) {
1068 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1069 G_SOCKET_PROTOCOL_UDP, NULL);
1071 goto no_udp_protocol;
1074 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
1075 GstRTSPAddressFlags flags;
1078 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1080 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
1081 if (family == G_SOCKET_FAMILY_IPV6)
1082 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1084 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1086 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1091 tmp_rtp = addr->port;
1093 g_clear_object (&inetaddr);
1094 inetaddr = g_inet_address_new_from_string (addr->address);
1102 if (inetaddr == NULL)
1103 inetaddr = g_inet_address_new_any (family);
1106 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1107 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1108 g_object_unref (rtp_sockaddr);
1111 g_object_unref (rtp_sockaddr);
1113 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1114 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1115 g_clear_object (&rtp_sockaddr);
1120 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1121 g_object_unref (rtp_sockaddr);
1123 /* check if port is even */
1124 if ((tmp_rtp & 1) != 0) {
1125 /* port not even, close and allocate another */
1127 g_clear_object (&rtp_socket);
1132 tmp_rtcp = tmp_rtp + 1;
1134 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1135 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1136 g_object_unref (rtcp_sockaddr);
1137 g_clear_object (&rtp_socket);
1140 g_object_unref (rtcp_sockaddr);
1142 g_clear_object (&inetaddr);
1144 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
1145 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
1147 if (udpsrc0 == NULL || udpsrc1 == NULL)
1148 goto no_udp_protocol;
1150 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
1151 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
1153 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1154 if (ret == GST_STATE_CHANGE_FAILURE)
1156 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1157 if (ret == GST_STATE_CHANGE_FAILURE)
1160 /* all fine, do port check */
1161 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
1162 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
1164 /* this should not happen... */
1165 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1169 udpsink0 = udpsink_out[0];
1171 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1174 goto no_udp_protocol;
1176 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1177 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
1180 udpsink1 = udpsink_out[1];
1182 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1185 goto no_udp_protocol;
1187 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1188 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1189 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
1191 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1192 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
1193 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1194 /* Needs to be async for RECORD streams, otherwise we will never go to
1195 * PLAYING because the sinks will wait for data while the udpsrc can't
1196 * provide data with timestamps in PAUSED. */
1198 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1199 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1200 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1201 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1202 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1203 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1205 /* we keep these elements, we will further configure them when the
1206 * client told us to really use the UDP ports. */
1207 udpsrc_out[0] = udpsrc0;
1208 udpsrc_out[1] = udpsrc1;
1209 udpsink_out[0] = udpsink0;
1210 udpsink_out[1] = udpsink1;
1212 server_port_out->min = rtpport;
1213 server_port_out->max = rtcpport;
1215 *server_addr_out = addr;
1216 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1218 g_object_unref (rtp_socket);
1219 g_object_unref (rtcp_socket);
1247 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1248 gst_object_unref (udpsrc0);
1251 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1252 gst_object_unref (udpsrc1);
1255 gst_element_set_state (udpsink0, GST_STATE_NULL);
1256 gst_object_unref (udpsink0);
1259 g_object_unref (inetaddr);
1260 g_list_free_full (rejected_addresses,
1261 (GDestroyNotify) gst_rtsp_address_free);
1263 gst_rtsp_address_free (addr);
1265 g_object_unref (rtp_socket);
1267 g_object_unref (rtcp_socket);
1272 /* must be called with lock */
1274 alloc_ports (GstRTSPStream * stream)
1276 GstRTSPStreamPrivate *priv = stream->priv;
1279 alloc_ports_one_family (stream, priv->pool, priv->buffer_size,
1280 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1281 &priv->server_port_v4, &priv->server_addr_v4);
1284 alloc_ports_one_family (stream, priv->pool, priv->buffer_size,
1285 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1286 &priv->server_port_v6, &priv->server_addr_v6);
1288 return priv->have_ipv4 || priv->have_ipv6;
1292 * gst_rtsp_stream_get_server_port:
1293 * @stream: a #GstRTSPStream
1294 * @server_port: (out): result server port
1295 * @family: the port family to get
1297 * Fill @server_port with the port pair used by the server. This function can
1298 * only be called when @stream has been joined.
1301 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1302 GstRTSPRange * server_port, GSocketFamily family)
1304 GstRTSPStreamPrivate *priv;
1306 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1307 priv = stream->priv;
1308 g_return_if_fail (priv->is_joined);
1310 g_mutex_lock (&priv->lock);
1311 if (family == G_SOCKET_FAMILY_IPV4) {
1313 *server_port = priv->server_port_v4;
1316 *server_port = priv->server_port_v6;
1318 g_mutex_unlock (&priv->lock);
1322 * gst_rtsp_stream_get_rtpsession:
1323 * @stream: a #GstRTSPStream
1325 * Get the RTP session of this stream.
1327 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1330 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1332 GstRTSPStreamPrivate *priv;
1335 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1337 priv = stream->priv;
1339 g_mutex_lock (&priv->lock);
1340 if ((session = priv->session))
1341 g_object_ref (session);
1342 g_mutex_unlock (&priv->lock);
1348 * gst_rtsp_stream_get_ssrc:
1349 * @stream: a #GstRTSPStream
1350 * @ssrc: (out): result ssrc
1352 * Get the SSRC used by the RTP session of this stream. This function can only
1353 * be called when @stream has been joined.
1356 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1358 GstRTSPStreamPrivate *priv;
1360 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1361 priv = stream->priv;
1362 g_return_if_fail (priv->is_joined);
1364 g_mutex_lock (&priv->lock);
1365 if (ssrc && priv->session)
1366 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1367 g_mutex_unlock (&priv->lock);
1371 * gst_rtsp_stream_set_retransmission_time:
1372 * @stream: a #GstRTSPStream
1373 * @time: a #GstClockTime
1375 * Set the amount of time to store retransmission packets.
1378 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1381 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1383 g_mutex_lock (&stream->priv->lock);
1384 stream->priv->rtx_time = time;
1385 if (stream->priv->rtxsend)
1386 g_object_set (stream->priv->rtxsend, "max-size-time",
1387 GST_TIME_AS_MSECONDS (time), NULL);
1388 g_mutex_unlock (&stream->priv->lock);
1392 * gst_rtsp_stream_get_retransmission_time:
1393 * @stream: a #GstRTSPStream
1395 * Get the amount of time to store retransmission data.
1397 * Returns: the amount of time to store retransmission data.
1400 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1404 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1406 g_mutex_lock (&stream->priv->lock);
1407 ret = stream->priv->rtx_time;
1408 g_mutex_unlock (&stream->priv->lock);
1414 * gst_rtsp_stream_set_retransmission_pt:
1415 * @stream: a #GstRTSPStream
1418 * Set the payload type (pt) for retransmission of this stream.
1421 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1423 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1425 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1427 g_mutex_lock (&stream->priv->lock);
1428 stream->priv->rtx_pt = rtx_pt;
1429 if (stream->priv->rtxsend) {
1430 guint pt = gst_rtsp_stream_get_pt (stream);
1431 gchar *pt_s = g_strdup_printf ("%d", pt);
1432 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1433 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1434 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1436 gst_structure_free (rtx_pt_map);
1438 g_mutex_unlock (&stream->priv->lock);
1442 * gst_rtsp_stream_get_retransmission_pt:
1443 * @stream: a #GstRTSPStream
1445 * Get the payload-type used for retransmission of this stream
1447 * Returns: The retransmission PT.
1450 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1454 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1456 g_mutex_lock (&stream->priv->lock);
1457 rtx_pt = stream->priv->rtx_pt;
1458 g_mutex_unlock (&stream->priv->lock);
1464 * gst_rtsp_stream_set_buffer_size:
1465 * @stream: a #GstRTSPStream
1466 * @size: the buffer size
1468 * Set the size of the UDP transmission buffer (in bytes)
1469 * Needs to be set before the stream is joined to a bin.
1474 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1476 g_mutex_lock (&stream->priv->lock);
1477 stream->priv->buffer_size = size;
1478 g_mutex_unlock (&stream->priv->lock);
1482 * gst_rtsp_stream_get_buffer_size:
1483 * @stream: a #GstRTSPStream
1485 * Get the size of the UDP transmission buffer (in bytes)
1487 * Returns: the size of the UDP TX buffer
1492 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1496 g_mutex_lock (&stream->priv->lock);
1497 buffer_size = stream->priv->buffer_size;
1498 g_mutex_unlock (&stream->priv->lock);
1503 /* executed from streaming thread */
1505 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1507 GstRTSPStreamPrivate *priv = stream->priv;
1508 GstCaps *newcaps, *oldcaps;
1510 newcaps = gst_pad_get_current_caps (pad);
1512 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1515 g_mutex_lock (&priv->lock);
1516 oldcaps = priv->caps;
1517 priv->caps = newcaps;
1518 g_mutex_unlock (&priv->lock);
1521 gst_caps_unref (oldcaps);
1525 dump_structure (const GstStructure * s)
1529 sstr = gst_structure_to_string (s);
1530 GST_INFO ("structure: %s", sstr);
1534 static GstRTSPStreamTransport *
1535 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1537 GstRTSPStreamPrivate *priv = stream->priv;
1539 GstRTSPStreamTransport *result = NULL;
1544 if (rtcp_from == NULL)
1547 tmp = g_strrstr (rtcp_from, ":");
1551 port = atoi (tmp + 1);
1552 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1554 g_mutex_lock (&priv->lock);
1555 GST_INFO ("finding %s:%d in %d transports", dest, port,
1556 g_list_length (priv->transports));
1558 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1559 GstRTSPStreamTransport *trans = walk->data;
1560 const GstRTSPTransport *tr;
1563 tr = gst_rtsp_stream_transport_get_transport (trans);
1565 min = tr->client_port.min;
1566 max = tr->client_port.max;
1568 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1574 g_object_ref (result);
1575 g_mutex_unlock (&priv->lock);
1582 static GstRTSPStreamTransport *
1583 check_transport (GObject * source, GstRTSPStream * stream)
1585 GstStructure *stats;
1586 GstRTSPStreamTransport *trans;
1588 /* see if we have a stream to match with the origin of the RTCP packet */
1589 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1590 if (trans == NULL) {
1591 g_object_get (source, "stats", &stats, NULL);
1593 const gchar *rtcp_from;
1595 dump_structure (stats);
1597 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1598 if ((trans = find_transport (stream, rtcp_from))) {
1599 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1601 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1604 gst_structure_free (stats);
1612 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1614 GstRTSPStreamTransport *trans;
1616 GST_INFO ("%p: new source %p", stream, source);
1618 trans = check_transport (source, stream);
1621 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1625 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1627 GST_INFO ("%p: new SDES %p", stream, source);
1631 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1633 GstRTSPStreamTransport *trans;
1635 trans = check_transport (source, stream);
1638 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1639 gst_rtsp_stream_transport_keep_alive (trans);
1643 GstStructure *stats;
1644 g_object_get (source, "stats", &stats, NULL);
1646 dump_structure (stats);
1647 gst_structure_free (stats);
1654 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1656 GST_INFO ("%p: source %p bye", stream, source);
1660 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1662 GstRTSPStreamTransport *trans;
1664 GST_INFO ("%p: source %p bye timeout", stream, source);
1666 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1667 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1668 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1673 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1675 GstRTSPStreamTransport *trans;
1677 GST_INFO ("%p: source %p timeout", stream, source);
1679 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1680 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1681 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1686 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1688 GST_INFO ("%p: new sender source %p", stream, source);
1691 GstStructure *stats;
1692 g_object_get (source, "stats", &stats, NULL);
1694 dump_structure (stats);
1695 gst_structure_free (stats);
1702 on_sender_ssrc_active (GObject * session, GObject * source,
1703 GstRTSPStream * stream)
1707 GstStructure *stats;
1708 g_object_get (source, "stats", &stats, NULL);
1710 dump_structure (stats);
1711 gst_structure_free (stats);
1718 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1721 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1722 g_list_free (priv->tr_cache_rtp);
1723 priv->tr_cache_rtp = NULL;
1725 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1726 g_list_free (priv->tr_cache_rtcp);
1727 priv->tr_cache_rtcp = NULL;
1731 static GstFlowReturn
1732 handle_new_sample (GstAppSink * sink, gpointer user_data)
1734 GstRTSPStreamPrivate *priv;
1738 GstRTSPStream *stream;
1741 sample = gst_app_sink_pull_sample (sink);
1745 stream = (GstRTSPStream *) user_data;
1746 priv = stream->priv;
1747 buffer = gst_sample_get_buffer (sample);
1749 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
1751 g_mutex_lock (&priv->lock);
1753 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
1754 clear_tr_cache (priv, is_rtp);
1755 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1756 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1757 priv->tr_cache_rtp =
1758 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
1760 priv->tr_cache_cookie_rtp = priv->transports_cookie;
1763 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
1764 clear_tr_cache (priv, is_rtp);
1765 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1766 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1767 priv->tr_cache_rtcp =
1768 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
1770 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
1773 g_mutex_unlock (&priv->lock);
1776 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
1777 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1778 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1781 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
1782 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1783 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1786 gst_sample_unref (sample);
1791 static GstAppSinkCallbacks sink_cb = {
1792 NULL, /* not interested in EOS */
1793 NULL, /* not interested in preroll samples */
1798 get_rtp_encoder (GstRTSPStream * stream, guint session)
1800 GstRTSPStreamPrivate *priv = stream->priv;
1802 if (priv->srtpenc == NULL) {
1805 name = g_strdup_printf ("srtpenc_%u", session);
1806 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
1809 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
1811 return gst_object_ref (priv->srtpenc);
1815 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
1817 GstRTSPStreamPrivate *priv = stream->priv;
1818 GstElement *oldenc, *enc;
1822 if (priv->idx != session)
1825 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
1827 oldenc = priv->srtpenc;
1828 enc = get_rtp_encoder (stream, session);
1829 name = g_strdup_printf ("rtp_sink_%d", session);
1830 pad = gst_element_get_request_pad (enc, name);
1832 gst_object_unref (pad);
1835 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
1842 request_rtcp_encoder (GstElement * rtpbin, guint session,
1843 GstRTSPStream * stream)
1845 GstRTSPStreamPrivate *priv = stream->priv;
1846 GstElement *oldenc, *enc;
1850 if (priv->idx != session)
1853 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
1855 oldenc = priv->srtpenc;
1856 enc = get_rtp_encoder (stream, session);
1857 name = g_strdup_printf ("rtcp_sink_%d", session);
1858 pad = gst_element_get_request_pad (enc, name);
1860 gst_object_unref (pad);
1863 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
1870 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
1872 GstRTSPStreamPrivate *priv = stream->priv;
1875 GST_DEBUG ("request key %08x", ssrc);
1877 g_mutex_lock (&priv->lock);
1878 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
1879 gst_caps_ref (caps);
1880 g_mutex_unlock (&priv->lock);
1886 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
1887 GstRTSPStream * stream)
1889 GstRTSPStreamPrivate *priv = stream->priv;
1891 if (priv->idx != session)
1894 if (priv->srtpdec == NULL) {
1897 name = g_strdup_printf ("srtpdec_%u", session);
1898 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
1901 g_signal_connect (priv->srtpdec, "request-key",
1902 (GCallback) request_key, stream);
1904 return gst_object_ref (priv->srtpdec);
1908 * gst_rtsp_stream_request_aux_sender:
1909 * @stream: a #GstRTSPStream
1910 * @sessid: the session id
1912 * Creating a rtxsend bin
1914 * Returns: (transfer full): a #GstElement.
1919 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
1923 GstStructure *pt_map;
1928 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1930 pt = gst_rtsp_stream_get_pt (stream);
1931 pt_s = g_strdup_printf ("%u", pt);
1932 rtx_pt = stream->priv->rtx_pt;
1934 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
1936 bin = gst_bin_new (NULL);
1937 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
1938 pt_map = gst_structure_new ("application/x-rtp-pt-map",
1939 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1940 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
1941 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
1943 gst_structure_free (pt_map);
1944 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
1946 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
1947 name = g_strdup_printf ("src_%u", sessid);
1948 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1950 gst_object_unref (pad);
1952 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
1953 name = g_strdup_printf ("sink_%u", sessid);
1954 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1956 gst_object_unref (pad);
1962 * gst_rtsp_stream_set_pt_map:
1963 * @stream: a #GstRTSPStream
1967 * Configure a pt map between @pt and @caps.
1970 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
1972 GstRTSPStreamPrivate *priv = stream->priv;
1974 g_mutex_lock (&priv->lock);
1975 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
1976 g_mutex_unlock (&priv->lock);
1980 request_pt_map (GstElement * rtpbin, guint session, guint pt,
1981 GstRTSPStream * stream)
1983 GstRTSPStreamPrivate *priv = stream->priv;
1984 GstCaps *caps = NULL;
1986 g_mutex_lock (&priv->lock);
1988 if (priv->idx == session) {
1989 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
1991 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
1992 gst_caps_ref (caps);
1994 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
1998 g_mutex_unlock (&priv->lock);
2004 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2006 GstRTSPStreamPrivate *priv = stream->priv;
2008 GstPadLinkReturn ret;
2011 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2012 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2014 name = gst_pad_get_name (pad);
2015 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2021 if (priv->idx != sessid)
2024 if (gst_pad_is_linked (priv->sinkpad)) {
2025 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2026 GST_DEBUG_PAD_NAME (priv->sinkpad));
2030 /* link the RTP pad to the session manager, it should not really fail unless
2031 * this is not really an RTP pad */
2032 ret = gst_pad_link (pad, priv->sinkpad);
2033 if (ret != GST_PAD_LINK_OK)
2035 priv->recv_rtp_src = gst_object_ref (pad);
2042 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2043 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2048 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2049 GstRTSPStream * stream)
2051 /* TODO: What to do here other than this? */
2052 GST_DEBUG ("Stream %p: Got EOS", stream);
2053 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2057 * gst_rtsp_stream_join_bin:
2058 * @stream: a #GstRTSPStream
2059 * @bin: (transfer none): a #GstBin to join
2060 * @rtpbin: (transfer none): a rtpbin element in @bin
2061 * @state: the target state of the new elements
2063 * Join the #GstBin @bin that contains the element @rtpbin.
2065 * @stream will link to @rtpbin, which must be inside @bin. The elements
2066 * added to @bin will be set to the state given in @state.
2068 * Returns: %TRUE on success.
2071 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2072 GstElement * rtpbin, GstState state)
2074 GstRTSPStreamPrivate *priv;
2078 GstPad *pad, *sinkpad = NULL, *selpad;
2079 GstPadLinkReturn ret;
2080 gboolean is_tcp = FALSE, is_udp = FALSE;
2082 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2083 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2084 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2086 priv = stream->priv;
2088 g_mutex_lock (&priv->lock);
2089 if (priv->is_joined)
2092 /* create a session with the same index as the stream */
2095 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2097 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2099 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2100 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2102 if (is_udp && !alloc_ports (stream))
2105 /* update the dscp qos field in the sinks */
2106 update_dscp_qos (stream);
2108 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2109 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2111 g_signal_connect (rtpbin, "request-rtp-encoder",
2112 (GCallback) request_rtp_encoder, stream);
2113 g_signal_connect (rtpbin, "request-rtcp-encoder",
2114 (GCallback) request_rtcp_encoder, stream);
2115 g_signal_connect (rtpbin, "request-rtp-decoder",
2116 (GCallback) request_rtp_rtcp_decoder, stream);
2117 g_signal_connect (rtpbin, "request-rtcp-decoder",
2118 (GCallback) request_rtp_rtcp_decoder, stream);
2121 if (priv->sinkpad) {
2122 g_signal_connect (rtpbin, "request-pt-map",
2123 (GCallback) request_pt_map, stream);
2126 /* get pads from the RTP session element for sending and receiving
2129 /* get a pad for sending RTP */
2130 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2131 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2134 /* link the RTP pad to the session manager, it should not really fail unless
2135 * this is not really an RTP pad */
2136 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2137 if (ret != GST_PAD_LINK_OK)
2140 name = g_strdup_printf ("send_rtp_src_%u", idx);
2141 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2144 /* Need to connect our sinkpad from here */
2145 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2147 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2149 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2150 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2154 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2155 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2157 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2158 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2161 /* get the session */
2162 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2164 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2166 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2168 g_signal_connect (priv->session, "on-ssrc-active",
2169 (GCallback) on_ssrc_active, stream);
2170 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2172 g_signal_connect (priv->session, "on-bye-timeout",
2173 (GCallback) on_bye_timeout, stream);
2174 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2177 /* signal for sender ssrc */
2178 g_signal_connect (priv->session, "on-new-sender-ssrc",
2179 (GCallback) on_new_sender_ssrc, stream);
2180 g_signal_connect (priv->session, "on-sender-ssrc-active",
2181 (GCallback) on_sender_ssrc_active, stream);
2183 for (i = 0; i < 2; i++) {
2184 GstPad *teepad, *queuepad;
2185 /* For the sender we create this bit of pipeline for both
2186 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2187 * we need to add a queue before appsink and udpsink to make
2188 * the pipeline not block. For the TCP case, we want to pump
2189 * client as fast as possible anyway. This pipeline is used
2190 * when both TCP and UDP are present.
2192 * .--------. .-----. .---------. .---------.
2193 * | rtpbin | | tee | | queue | | udpsink |
2194 * | send->sink src->sink src->sink |
2195 * '--------' | | '---------' '---------'
2196 * | | .---------. .---------.
2197 * | | | queue | | appsink |
2198 * | src->sink src->sink |
2199 * '-----' '---------' '---------'
2201 * When only UDP or only TCP is allowed, we skip the tee and queue
2202 * and link the udpsink (for UDP) or appsink (for TCP) directly to
2206 /* Only link the RTP send src if we're going to send RTP, link
2207 * the RTCP send src always */
2208 if (priv->srcpad || i == 1) {
2211 gst_bin_add (bin, priv->udpsink[i]);
2212 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
2217 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2218 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2219 gst_bin_add (bin, priv->appsink[i]);
2220 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2221 &sink_cb, stream, NULL);
2224 if (is_udp && is_tcp) {
2225 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2227 /* make tee for RTP/RTCP */
2228 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2229 gst_bin_add (bin, priv->tee[i]);
2231 /* and link to rtpbin send pad */
2232 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2233 gst_pad_link (priv->send_src[i], pad);
2234 gst_object_unref (pad);
2236 priv->udpqueue[i] = gst_element_factory_make ("queue", NULL);
2237 g_object_set (priv->udpqueue[i], "max-size-buffers",
2238 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0),
2240 gst_bin_add (bin, priv->udpqueue[i]);
2241 /* link tee to udpqueue */
2242 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2243 pad = gst_element_get_static_pad (priv->udpqueue[i], "sink");
2244 gst_pad_link (teepad, pad);
2245 gst_object_unref (pad);
2246 gst_object_unref (teepad);
2248 /* link udpqueue to udpsink */
2249 queuepad = gst_element_get_static_pad (priv->udpqueue[i], "src");
2250 gst_pad_link (queuepad, sinkpad);
2251 gst_object_unref (queuepad);
2252 gst_object_unref (sinkpad);
2255 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
2256 g_object_set (priv->appqueue[i], "max-size-buffers",
2257 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0),
2259 gst_bin_add (bin, priv->appqueue[i]);
2260 /* and link tee to appqueue */
2261 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2262 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
2263 gst_pad_link (teepad, pad);
2264 gst_object_unref (pad);
2265 gst_object_unref (teepad);
2267 /* and link appqueue to appsink */
2268 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
2269 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2270 gst_pad_link (queuepad, pad);
2271 gst_object_unref (pad);
2272 gst_object_unref (queuepad);
2273 } else if (is_tcp) {
2274 /* only appsink needed, link it to the session */
2275 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2276 gst_pad_link (priv->send_src[i], pad);
2277 gst_object_unref (pad);
2279 /* when its only TCP, we need to set sync and preroll to FALSE
2280 * for the sink to avoid deadlock. And this is only needed for
2281 * sink used for RTCP data, not the RTP data. */
2283 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2285 /* else only udpsink needed, link it to the session */
2286 gst_pad_link (priv->send_src[i], sinkpad);
2287 gst_object_unref (sinkpad);
2291 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2292 * RTCP sink always */
2293 if (priv->sinkpad || i == 1) {
2294 /* For the receiver we create this bit of pipeline for both
2295 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2296 * and it is all funneled into the rtpbin receive pad.
2298 * .--------. .--------. .--------.
2299 * | udpsrc | | funnel | | rtpbin |
2300 * | src->sink src->sink |
2301 * '--------' | | '--------'
2305 * '--------' '--------'
2307 /* make funnel for the RTP/RTCP receivers */
2308 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2309 gst_bin_add (bin, priv->funnel[i]);
2311 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2312 gst_pad_link (pad, priv->recv_sink[i]);
2313 gst_object_unref (pad);
2315 if (priv->udpsrc_v4[i]) {
2317 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2318 * values. This is only relevant for PLAY pipelines */
2319 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
2320 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
2323 gst_bin_add (bin, priv->udpsrc_v4[i]);
2325 /* and link to the funnel v4 */
2326 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2327 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
2328 gst_pad_link (pad, selpad);
2329 gst_object_unref (pad);
2330 gst_object_unref (selpad);
2333 if (priv->udpsrc_v6[i]) {
2335 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
2336 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
2338 gst_bin_add (bin, priv->udpsrc_v6[i]);
2340 /* and link to the funnel v6 */
2341 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2342 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2343 gst_pad_link (pad, selpad);
2344 gst_object_unref (pad);
2345 gst_object_unref (selpad);
2349 /* make and add appsrc */
2350 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2351 priv->appsrc_base_time[i] = -1;
2352 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, NULL);
2353 gst_bin_add (bin, priv->appsrc[i]);
2354 /* and link to the funnel */
2355 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2356 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2357 gst_pad_link (pad, selpad);
2358 gst_object_unref (pad);
2359 gst_object_unref (selpad);
2363 /* check if we need to set to a special state */
2364 if (state != GST_STATE_NULL) {
2365 if (priv->udpsink[i] && (priv->srcpad || i == 1))
2366 gst_element_set_state (priv->udpsink[i], state);
2367 if (priv->appsink[i] && (priv->srcpad || i == 1))
2368 gst_element_set_state (priv->appsink[i], state);
2369 if (priv->appqueue[i] && (priv->srcpad || i == 1))
2370 gst_element_set_state (priv->appqueue[i], state);
2371 if (priv->udpqueue[i] && (priv->srcpad || i == 1))
2372 gst_element_set_state (priv->udpqueue[i], state);
2373 if (priv->tee[i] && (priv->srcpad || i == 1))
2374 gst_element_set_state (priv->tee[i], state);
2375 if (priv->funnel[i] && (priv->sinkpad || i == 1))
2376 gst_element_set_state (priv->funnel[i], state);
2377 if (priv->appsrc[i] && (priv->sinkpad || i == 1))
2378 gst_element_set_state (priv->appsrc[i], state);
2383 /* be notified of caps changes */
2384 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2385 (GCallback) caps_notify, stream);
2388 priv->is_joined = TRUE;
2389 g_mutex_unlock (&priv->lock);
2396 g_mutex_unlock (&priv->lock);
2401 g_mutex_unlock (&priv->lock);
2402 GST_WARNING ("failed to allocate ports %u", idx);
2407 GST_WARNING ("failed to link stream %u", idx);
2408 gst_object_unref (priv->send_rtp_sink);
2409 priv->send_rtp_sink = NULL;
2410 g_mutex_unlock (&priv->lock);
2416 * gst_rtsp_stream_leave_bin:
2417 * @stream: a #GstRTSPStream
2418 * @bin: (transfer none): a #GstBin
2419 * @rtpbin: (transfer none): a rtpbin #GstElement
2421 * Remove the elements of @stream from @bin.
2423 * Return: %TRUE on success.
2426 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2427 GstElement * rtpbin)
2429 GstRTSPStreamPrivate *priv;
2432 gboolean is_tcp, is_udp;
2434 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2435 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2436 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2438 priv = stream->priv;
2440 g_mutex_lock (&priv->lock);
2441 if (!priv->is_joined)
2442 goto was_not_joined;
2444 /* all transports must be removed by now */
2445 if (priv->transports != NULL)
2446 goto transports_not_removed;
2448 clear_tr_cache (priv, TRUE);
2449 clear_tr_cache (priv, FALSE);
2451 GST_INFO ("stream %p leaving bin", stream);
2454 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2456 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2457 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2458 gst_object_unref (priv->send_rtp_sink);
2459 priv->send_rtp_sink = NULL;
2460 } else if (priv->recv_rtp_src) {
2461 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2462 gst_object_unref (priv->recv_rtp_src);
2463 priv->recv_rtp_src = NULL;
2466 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2468 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2469 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2472 for (i = 0; i < 2; i++) {
2473 if (priv->udpsink[i])
2474 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
2475 if (priv->appsink[i])
2476 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
2477 if (priv->appqueue[i])
2478 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
2479 if (priv->udpqueue[i])
2480 gst_element_set_state (priv->udpqueue[i], GST_STATE_NULL);
2482 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
2483 if (priv->funnel[i])
2484 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
2485 if (priv->appsrc[i])
2486 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2488 if (priv->udpsrc_v4[i]) {
2489 if (priv->sinkpad || i == 1) {
2490 /* and set udpsrc to NULL now before removing */
2491 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
2492 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2493 /* removing them should also nicely release the request
2494 * pads when they finalize */
2495 gst_bin_remove (bin, priv->udpsrc_v4[i]);
2497 /* we need to set the state to NULL before unref */
2498 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2499 gst_object_unref (priv->udpsrc_v4[i]);
2503 if (priv->udpsrc_v6[i]) {
2504 if (priv->sinkpad || i == 1) {
2505 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
2506 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2507 gst_bin_remove (bin, priv->udpsrc_v6[i]);
2509 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2510 gst_object_unref (priv->udpsrc_v6[i]);
2514 for (l = priv->transport_sources; l; l = l->next) {
2515 GstRTSPMulticastTransportSource *s = l->data;
2520 gst_element_set_locked_state (s->udpsrc[i], FALSE);
2521 gst_element_set_state (s->udpsrc[i], GST_STATE_NULL);
2522 gst_bin_remove (bin, s->udpsrc[i]);
2525 if (priv->udpsink[i] && is_udp && (priv->srcpad || i == 1))
2526 gst_bin_remove (bin, priv->udpsink[i]);
2527 if (priv->appsrc[i] && (priv->sinkpad || i == 1))
2528 gst_bin_remove (bin, priv->appsrc[i]);
2529 if (priv->appsink[i] && is_tcp && (priv->srcpad || i == 1))
2530 gst_bin_remove (bin, priv->appsink[i]);
2531 if (priv->appqueue[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2532 gst_bin_remove (bin, priv->appqueue[i]);
2533 if (priv->udpqueue[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2534 gst_bin_remove (bin, priv->udpqueue[i]);
2535 if (priv->tee[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2536 gst_bin_remove (bin, priv->tee[i]);
2537 if (priv->funnel[i] && (priv->sinkpad || i == 1))
2538 gst_bin_remove (bin, priv->funnel[i]);
2540 if (priv->sinkpad || i == 1) {
2541 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2542 gst_object_unref (priv->recv_sink[i]);
2543 priv->recv_sink[i] = NULL;
2546 priv->udpsrc_v4[i] = NULL;
2547 priv->udpsrc_v6[i] = NULL;
2548 priv->udpsink[i] = NULL;
2549 priv->appsrc[i] = NULL;
2550 priv->appsink[i] = NULL;
2551 priv->appqueue[i] = NULL;
2552 priv->udpqueue[i] = NULL;
2553 priv->tee[i] = NULL;
2554 priv->funnel[i] = NULL;
2557 for (l = priv->transport_sources; l; l = l->next) {
2558 GstRTSPMulticastTransportSource *s = l->data;
2559 g_slice_free (GstRTSPMulticastTransportSource, s);
2561 g_list_free (priv->transport_sources);
2562 priv->transport_sources = NULL;
2565 gst_object_unref (priv->send_src[0]);
2566 priv->send_src[0] = NULL;
2569 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2570 gst_object_unref (priv->send_src[1]);
2571 priv->send_src[1] = NULL;
2573 g_object_unref (priv->session);
2574 priv->session = NULL;
2576 gst_caps_unref (priv->caps);
2580 gst_object_unref (priv->srtpenc);
2582 gst_object_unref (priv->srtpdec);
2584 priv->is_joined = FALSE;
2585 g_mutex_unlock (&priv->lock);
2591 g_mutex_unlock (&priv->lock);
2594 transports_not_removed:
2596 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2597 g_mutex_unlock (&priv->lock);
2603 * gst_rtsp_stream_get_rtpinfo:
2604 * @stream: a #GstRTSPStream
2605 * @rtptime: (allow-none): result RTP timestamp
2606 * @seq: (allow-none): result RTP seqnum
2607 * @clock_rate: (allow-none): the clock rate
2608 * @running_time: (allow-none): result running-time
2610 * Retrieve the current rtptime, seq and running-time. This is used to
2611 * construct a RTPInfo reply header.
2613 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2616 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2617 guint * rtptime, guint * seq, guint * clock_rate,
2618 GstClockTime * running_time)
2620 GstRTSPStreamPrivate *priv;
2621 GstStructure *stats;
2622 GObjectClass *payobjclass;
2624 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2626 priv = stream->priv;
2628 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2630 g_mutex_lock (&priv->lock);
2632 /* First try to extract the information from the last buffer on the sinks.
2633 * This will have a more accurate sequence number and timestamp, as between
2634 * the payloader and the sink there can be some queues
2636 if (priv->udpsink[0] || priv->appsink[0]) {
2637 GstSample *last_sample;
2639 if (priv->udpsink[0])
2640 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
2642 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
2647 GstSegment *segment;
2648 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
2650 caps = gst_sample_get_caps (last_sample);
2651 buffer = gst_sample_get_buffer (last_sample);
2652 segment = gst_sample_get_segment (last_sample);
2654 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
2656 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
2660 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
2663 gst_rtp_buffer_unmap (&rtp_buffer);
2667 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
2668 GST_BUFFER_TIMESTAMP (buffer));
2672 GstStructure *s = gst_caps_get_structure (caps, 0);
2674 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
2676 if (*clock_rate == 0 && running_time)
2677 *running_time = GST_CLOCK_TIME_NONE;
2679 gst_sample_unref (last_sample);
2683 gst_sample_unref (last_sample);
2688 if (g_object_class_find_property (payobjclass, "stats")) {
2689 g_object_get (priv->payloader, "stats", &stats, NULL);
2694 gst_structure_get_uint (stats, "seqnum", seq);
2697 gst_structure_get_uint (stats, "timestamp", rtptime);
2700 gst_structure_get_clock_time (stats, "running-time", running_time);
2703 gst_structure_get_uint (stats, "clock-rate", clock_rate);
2704 if (*clock_rate == 0 && running_time)
2705 *running_time = GST_CLOCK_TIME_NONE;
2707 gst_structure_free (stats);
2709 if (!g_object_class_find_property (payobjclass, "seqnum") ||
2710 !g_object_class_find_property (payobjclass, "timestamp"))
2714 g_object_get (priv->payloader, "seqnum", seq, NULL);
2717 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
2720 *running_time = GST_CLOCK_TIME_NONE;
2724 g_mutex_unlock (&priv->lock);
2731 GST_WARNING ("Could not get payloader stats");
2732 g_mutex_unlock (&priv->lock);
2738 * gst_rtsp_stream_get_caps:
2739 * @stream: a #GstRTSPStream
2741 * Retrieve the current caps of @stream.
2743 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
2747 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
2749 GstRTSPStreamPrivate *priv;
2752 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2754 priv = stream->priv;
2756 g_mutex_lock (&priv->lock);
2757 if ((result = priv->caps))
2758 gst_caps_ref (result);
2759 g_mutex_unlock (&priv->lock);
2765 * gst_rtsp_stream_recv_rtp:
2766 * @stream: a #GstRTSPStream
2767 * @buffer: (transfer full): a #GstBuffer
2769 * Handle an RTP buffer for the stream. This method is usually called when a
2770 * message has been received from a client using the TCP transport.
2772 * This function takes ownership of @buffer.
2774 * Returns: a GstFlowReturn.
2777 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
2779 GstRTSPStreamPrivate *priv;
2781 GstElement *element;
2783 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2784 priv = stream->priv;
2785 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2786 g_return_val_if_fail (priv->is_joined, FALSE);
2788 g_mutex_lock (&priv->lock);
2789 if (priv->appsrc[0])
2790 element = gst_object_ref (priv->appsrc[0]);
2793 g_mutex_unlock (&priv->lock);
2796 if (priv->appsrc_base_time[0] == -1) {
2797 /* Take current running_time. This timestamp will be put on
2798 * the first buffer of each stream because we are a live source and so we
2799 * timestamp with the running_time. When we are dealing with TCP, we also
2800 * only timestamp the first buffer (using the DISCONT flag) because a server
2801 * typically bursts data, for which we don't want to compensate by speeding
2802 * up the media. The other timestamps will be interpollated from this one
2803 * using the RTP timestamps. */
2804 GST_OBJECT_LOCK (element);
2805 if (GST_ELEMENT_CLOCK (element)) {
2807 GstClockTime base_time;
2809 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
2810 base_time = GST_ELEMENT_CAST (element)->base_time;
2812 priv->appsrc_base_time[0] = now - base_time;
2813 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
2814 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
2815 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
2816 GST_TIME_ARGS (base_time));
2818 GST_OBJECT_UNLOCK (element);
2821 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2822 gst_object_unref (element);
2830 * gst_rtsp_stream_recv_rtcp:
2831 * @stream: a #GstRTSPStream
2832 * @buffer: (transfer full): a #GstBuffer
2834 * Handle an RTCP buffer for the stream. This method is usually called when a
2835 * message has been received from a client using the TCP transport.
2837 * This function takes ownership of @buffer.
2839 * Returns: a GstFlowReturn.
2842 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
2844 GstRTSPStreamPrivate *priv;
2846 GstElement *element;
2848 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2849 priv = stream->priv;
2850 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2852 if (!priv->is_joined) {
2853 gst_buffer_unref (buffer);
2854 return GST_FLOW_NOT_LINKED;
2856 g_mutex_lock (&priv->lock);
2857 if (priv->appsrc[1])
2858 element = gst_object_ref (priv->appsrc[1]);
2861 g_mutex_unlock (&priv->lock);
2864 if (priv->appsrc_base_time[1] == -1) {
2865 /* Take current running_time. This timestamp will be put on
2866 * the first buffer of each stream because we are a live source and so we
2867 * timestamp with the running_time. When we are dealing with TCP, we also
2868 * only timestamp the first buffer (using the DISCONT flag) because a server
2869 * typically bursts data, for which we don't want to compensate by speeding
2870 * up the media. The other timestamps will be interpollated from this one
2871 * using the RTP timestamps. */
2872 GST_OBJECT_LOCK (element);
2873 if (GST_ELEMENT_CLOCK (element)) {
2875 GstClockTime base_time;
2877 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
2878 base_time = GST_ELEMENT_CAST (element)->base_time;
2880 priv->appsrc_base_time[1] = now - base_time;
2881 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
2882 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
2883 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
2884 GST_TIME_ARGS (base_time));
2886 GST_OBJECT_UNLOCK (element);
2889 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2890 gst_object_unref (element);
2893 gst_buffer_unref (buffer);
2898 /* must be called with lock */
2900 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2903 GstRTSPStreamPrivate *priv = stream->priv;
2904 const GstRTSPTransport *tr;
2906 tr = gst_rtsp_stream_transport_get_transport (trans);
2908 switch (tr->lower_transport) {
2909 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2911 GstRTSPMulticastTransportSource *source;
2914 bin = GST_BIN (gst_object_get_parent (GST_OBJECT (priv->funnel[1])));
2919 GstPad *selpad, *pad;
2921 source = g_slice_new0 (GstRTSPMulticastTransportSource);
2922 source->transport = trans;
2924 for (i = 0; i < 2; i++) {
2926 g_strdup_printf ("udp://%s:%d", tr->destination,
2927 (i == 0) ? tr->port.min : tr->port.max);
2929 gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2931 g_object_set (source->udpsrc[i], "loop", FALSE, NULL);
2934 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2935 * values. This is only relevant for PLAY pipelines */
2936 gst_element_set_state (source->udpsrc[i], GST_STATE_PLAYING);
2937 gst_element_set_locked_state (source->udpsrc[i], TRUE);
2940 gst_bin_add (bin, source->udpsrc[i]);
2942 /* and link to the funnel v4 */
2943 if (priv->sinkpad || i == 1) {
2944 source->selpad[i] = selpad =
2945 gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2946 pad = gst_element_get_static_pad (source->udpsrc[i], "src");
2947 gst_pad_link (pad, selpad);
2948 gst_object_unref (pad);
2949 gst_object_unref (selpad);
2953 priv->transport_sources =
2954 g_list_prepend (priv->transport_sources, source);
2958 for (l = priv->transport_sources; l; l = l->next) {
2961 if (source->transport == trans) {
2962 priv->transport_sources =
2963 g_list_delete_link (priv->transport_sources, l);
2971 for (i = 0; i < 2; i++) {
2972 /* Will automatically unlink everything */
2973 gst_bin_remove (bin,
2974 GST_ELEMENT (gst_object_ref (source->udpsrc[i])));
2976 gst_element_set_state (source->udpsrc[i], GST_STATE_NULL);
2977 gst_object_unref (source->udpsrc[i]);
2979 if (priv->sinkpad || i == 1) {
2980 gst_element_release_request_pad (priv->funnel[i],
2985 g_slice_free (GstRTSPMulticastTransportSource, source);
2989 gst_object_unref (bin);
2991 /* fall through for the generic case */
2993 case GST_RTSP_LOWER_TRANS_UDP:
2999 dest = tr->destination;
3000 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3005 min = tr->client_port.min;
3006 max = tr->client_port.max;
3011 GST_INFO ("setting ttl-mc %d", ttl);
3012 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3013 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3015 GST_INFO ("adding %s:%d-%d", dest, min, max);
3016 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3017 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3018 priv->transports = g_list_prepend (priv->transports, trans);
3020 GST_INFO ("removing %s:%d-%d", dest, min, max);
3021 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3022 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3023 priv->transports = g_list_remove (priv->transports, trans);
3025 priv->transports_cookie++;
3028 case GST_RTSP_LOWER_TRANS_TCP:
3030 GST_INFO ("adding TCP %s", tr->destination);
3031 priv->transports = g_list_prepend (priv->transports, trans);
3033 GST_INFO ("removing TCP %s", tr->destination);
3034 priv->transports = g_list_remove (priv->transports, trans);
3036 priv->transports_cookie++;
3039 goto unknown_transport;
3046 GST_INFO ("Unknown transport %d", tr->lower_transport);
3053 * gst_rtsp_stream_add_transport:
3054 * @stream: a #GstRTSPStream
3055 * @trans: (transfer none): a #GstRTSPStreamTransport
3057 * Add the transport in @trans to @stream. The media of @stream will
3058 * then also be send to the values configured in @trans.
3060 * @stream must be joined to a bin.
3062 * @trans must contain a valid #GstRTSPTransport.
3064 * Returns: %TRUE if @trans was added
3067 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3068 GstRTSPStreamTransport * trans)
3070 GstRTSPStreamPrivate *priv;
3073 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3074 priv = stream->priv;
3075 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3076 g_return_val_if_fail (priv->is_joined, FALSE);
3078 g_mutex_lock (&priv->lock);
3079 res = update_transport (stream, trans, TRUE);
3080 g_mutex_unlock (&priv->lock);
3086 * gst_rtsp_stream_remove_transport:
3087 * @stream: a #GstRTSPStream
3088 * @trans: (transfer none): a #GstRTSPStreamTransport
3090 * Remove the transport in @trans from @stream. The media of @stream will
3091 * not be sent to the values configured in @trans.
3093 * @stream must be joined to a bin.
3095 * @trans must contain a valid #GstRTSPTransport.
3097 * Returns: %TRUE if @trans was removed
3100 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3101 GstRTSPStreamTransport * trans)
3103 GstRTSPStreamPrivate *priv;
3106 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3107 priv = stream->priv;
3108 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3109 g_return_val_if_fail (priv->is_joined, FALSE);
3111 g_mutex_lock (&priv->lock);
3112 res = update_transport (stream, trans, FALSE);
3113 g_mutex_unlock (&priv->lock);
3119 * gst_rtsp_stream_update_crypto:
3120 * @stream: a #GstRTSPStream
3122 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3124 * Update the new crypto information for @ssrc in @stream. If information
3125 * for @ssrc did not exist, it will be added. If information
3126 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3127 * be removed from @stream.
3129 * Returns: %TRUE if @crypto could be updated
3132 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3133 guint ssrc, GstCaps * crypto)
3135 GstRTSPStreamPrivate *priv;
3137 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3138 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3140 priv = stream->priv;
3142 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3144 g_mutex_lock (&priv->lock);
3146 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3147 gst_caps_ref (crypto));
3149 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3150 g_mutex_unlock (&priv->lock);
3156 * gst_rtsp_stream_get_rtp_socket:
3157 * @stream: a #GstRTSPStream
3158 * @family: the socket family
3160 * Get the RTP socket from @stream for a @family.
3162 * @stream must be joined to a bin.
3164 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3165 * socket could be allocated for @family. Unref after usage
3168 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3170 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3174 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3175 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3176 family == G_SOCKET_FAMILY_IPV6, NULL);
3177 g_return_val_if_fail (priv->udpsink[0], NULL);
3179 if (family == G_SOCKET_FAMILY_IPV6)
3184 g_object_get (priv->udpsink[0], name, &socket, NULL);
3190 * gst_rtsp_stream_get_rtcp_socket:
3191 * @stream: a #GstRTSPStream
3192 * @family: the socket family
3194 * Get the RTCP socket from @stream for a @family.
3196 * @stream must be joined to a bin.
3198 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3199 * socket could be allocated for @family. Unref after usage
3202 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3204 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3208 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3209 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3210 family == G_SOCKET_FAMILY_IPV6, NULL);
3211 g_return_val_if_fail (priv->udpsink[1], NULL);
3213 if (family == G_SOCKET_FAMILY_IPV6)
3218 g_object_get (priv->udpsink[1], name, &socket, NULL);
3224 * gst_rtsp_stream_set_seqnum:
3225 * @stream: a #GstRTSPStream
3226 * @seqnum: a new sequence number
3228 * Configure the sequence number in the payloader of @stream to @seqnum.
3231 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3233 GstRTSPStreamPrivate *priv;
3235 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3237 priv = stream->priv;
3239 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3243 * gst_rtsp_stream_get_seqnum:
3244 * @stream: a #GstRTSPStream
3246 * Get the configured sequence number in the payloader of @stream.
3248 * Returns: the sequence number of the payloader.
3251 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3253 GstRTSPStreamPrivate *priv;
3256 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3258 priv = stream->priv;
3260 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3266 * gst_rtsp_stream_transport_filter:
3267 * @stream: a #GstRTSPStream
3268 * @func: (scope call) (allow-none): a callback
3269 * @user_data: (closure): user data passed to @func
3271 * Call @func for each transport managed by @stream. The result value of @func
3272 * determines what happens to the transport. @func will be called with @stream
3273 * locked so no further actions on @stream can be performed from @func.
3275 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3278 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3280 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3281 * will also be added with an additional ref to the result #GList of this
3284 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3286 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3287 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3288 * element in the #GList should be unreffed before the list is freed.
3291 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3292 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3294 GstRTSPStreamPrivate *priv;
3295 GList *result, *walk, *next;
3296 GHashTable *visited = NULL;
3299 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3301 priv = stream->priv;
3305 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3307 g_mutex_lock (&priv->lock);
3309 cookie = priv->transports_cookie;
3310 for (walk = priv->transports; walk; walk = next) {
3311 GstRTSPStreamTransport *trans = walk->data;
3312 GstRTSPFilterResult res;
3315 next = g_list_next (walk);
3318 /* only visit each transport once */
3319 if (g_hash_table_contains (visited, trans))
3322 g_hash_table_add (visited, g_object_ref (trans));
3323 g_mutex_unlock (&priv->lock);
3325 res = func (stream, trans, user_data);
3327 g_mutex_lock (&priv->lock);
3329 res = GST_RTSP_FILTER_REF;
3331 changed = (cookie != priv->transports_cookie);
3334 case GST_RTSP_FILTER_REMOVE:
3335 update_transport (stream, trans, FALSE);
3337 case GST_RTSP_FILTER_REF:
3338 result = g_list_prepend (result, g_object_ref (trans));
3340 case GST_RTSP_FILTER_KEEP:
3347 g_mutex_unlock (&priv->lock);
3350 g_hash_table_unref (visited);
3355 static GstPadProbeReturn
3356 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3358 GstRTSPStreamPrivate *priv;
3359 GstRTSPStream *stream;
3362 priv = stream->priv;
3364 GST_DEBUG_OBJECT (pad, "now blocking");
3366 g_mutex_lock (&priv->lock);
3367 priv->blocking = TRUE;
3368 g_mutex_unlock (&priv->lock);
3370 gst_element_post_message (priv->payloader,
3371 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3372 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3374 return GST_PAD_PROBE_OK;
3378 * gst_rtsp_stream_set_blocked:
3379 * @stream: a #GstRTSPStream
3380 * @blocked: boolean indicating we should block or unblock
3382 * Blocks or unblocks the dataflow on @stream.
3384 * Returns: %TRUE on success
3387 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3389 GstRTSPStreamPrivate *priv;
3391 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3393 priv = stream->priv;
3395 g_mutex_lock (&priv->lock);
3397 priv->blocking = FALSE;
3398 if (priv->blocked_id == 0) {
3399 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3400 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3401 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3402 g_object_ref (stream), g_object_unref);
3405 if (priv->blocked_id != 0) {
3406 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3407 priv->blocked_id = 0;
3408 priv->blocking = FALSE;
3411 g_mutex_unlock (&priv->lock);
3417 * gst_rtsp_stream_is_blocking:
3418 * @stream: a #GstRTSPStream
3420 * Check if @stream is blocking on a #GstBuffer.
3422 * Returns: %TRUE if @stream is blocking
3425 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3427 GstRTSPStreamPrivate *priv;
3430 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3432 priv = stream->priv;
3434 g_mutex_lock (&priv->lock);
3435 result = priv->blocking;
3436 g_mutex_unlock (&priv->lock);
3442 * gst_rtsp_stream_query_position:
3443 * @stream: a #GstRTSPStream
3445 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3446 * the RTP parts of the pipeline and not the RTCP parts.
3448 * Returns: %TRUE if the position could be queried
3451 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3453 GstRTSPStreamPrivate *priv;
3457 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3459 priv = stream->priv;
3461 g_mutex_lock (&priv->lock);
3462 /* depending on the transport type, it should query corresponding sink */
3463 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3464 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3465 sink = priv->udpsink[0];
3467 sink = priv->appsink[0];
3470 gst_object_ref (sink);
3471 g_mutex_unlock (&priv->lock);
3476 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3477 gst_object_unref (sink);
3483 * gst_rtsp_stream_query_stop:
3484 * @stream: a #GstRTSPStream
3486 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3487 * the RTP parts of the pipeline and not the RTCP parts.
3489 * Returns: %TRUE if the stop could be queried
3492 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3494 GstRTSPStreamPrivate *priv;
3499 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3501 priv = stream->priv;
3503 g_mutex_lock (&priv->lock);
3504 /* depending on the transport type, it should query corresponding sink */
3505 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3506 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3507 sink = priv->udpsink[0];
3509 sink = priv->appsink[0];
3512 gst_object_ref (sink);
3513 g_mutex_unlock (&priv->lock);
3518 query = gst_query_new_segment (GST_FORMAT_TIME);
3519 if ((ret = gst_element_query (sink, query))) {
3522 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3523 if (format != GST_FORMAT_TIME)
3526 gst_query_unref (query);
3527 gst_object_unref (sink);