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_media_get_retransmission_time:
1393 * @media: a #GstRTSPMedia
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 (GstRTSPStream * stream, guint rtx_pt)
1416 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1418 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1420 g_mutex_lock (&stream->priv->lock);
1421 stream->priv->rtx_pt = rtx_pt;
1422 if (stream->priv->rtxsend) {
1423 guint pt = gst_rtsp_stream_get_pt (stream);
1424 gchar *pt_s = g_strdup_printf ("%d", pt);
1425 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1426 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1427 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1429 gst_structure_free (rtx_pt_map);
1431 g_mutex_unlock (&stream->priv->lock);
1435 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1439 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1441 g_mutex_lock (&stream->priv->lock);
1442 rtx_pt = stream->priv->rtx_pt;
1443 g_mutex_unlock (&stream->priv->lock);
1448 /* executed from streaming thread */
1450 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1452 GstRTSPStreamPrivate *priv = stream->priv;
1453 GstCaps *newcaps, *oldcaps;
1455 newcaps = gst_pad_get_current_caps (pad);
1457 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1460 g_mutex_lock (&priv->lock);
1461 oldcaps = priv->caps;
1462 priv->caps = newcaps;
1463 g_mutex_unlock (&priv->lock);
1466 gst_caps_unref (oldcaps);
1470 dump_structure (const GstStructure * s)
1474 sstr = gst_structure_to_string (s);
1475 GST_INFO ("structure: %s", sstr);
1479 static GstRTSPStreamTransport *
1480 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1482 GstRTSPStreamPrivate *priv = stream->priv;
1484 GstRTSPStreamTransport *result = NULL;
1489 if (rtcp_from == NULL)
1492 tmp = g_strrstr (rtcp_from, ":");
1496 port = atoi (tmp + 1);
1497 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1499 g_mutex_lock (&priv->lock);
1500 GST_INFO ("finding %s:%d in %d transports", dest, port,
1501 g_list_length (priv->transports));
1503 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1504 GstRTSPStreamTransport *trans = walk->data;
1505 const GstRTSPTransport *tr;
1508 tr = gst_rtsp_stream_transport_get_transport (trans);
1510 min = tr->client_port.min;
1511 max = tr->client_port.max;
1513 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1519 g_object_ref (result);
1520 g_mutex_unlock (&priv->lock);
1527 static GstRTSPStreamTransport *
1528 check_transport (GObject * source, GstRTSPStream * stream)
1530 GstStructure *stats;
1531 GstRTSPStreamTransport *trans;
1533 /* see if we have a stream to match with the origin of the RTCP packet */
1534 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1535 if (trans == NULL) {
1536 g_object_get (source, "stats", &stats, NULL);
1538 const gchar *rtcp_from;
1540 dump_structure (stats);
1542 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1543 if ((trans = find_transport (stream, rtcp_from))) {
1544 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1546 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1549 gst_structure_free (stats);
1557 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1559 GstRTSPStreamTransport *trans;
1561 GST_INFO ("%p: new source %p", stream, source);
1563 trans = check_transport (source, stream);
1566 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1570 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1572 GST_INFO ("%p: new SDES %p", stream, source);
1576 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1578 GstRTSPStreamTransport *trans;
1580 trans = check_transport (source, stream);
1583 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1584 gst_rtsp_stream_transport_keep_alive (trans);
1588 GstStructure *stats;
1589 g_object_get (source, "stats", &stats, NULL);
1591 dump_structure (stats);
1592 gst_structure_free (stats);
1599 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1601 GST_INFO ("%p: source %p bye", stream, source);
1605 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1607 GstRTSPStreamTransport *trans;
1609 GST_INFO ("%p: source %p bye timeout", stream, source);
1611 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1612 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1613 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1618 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1620 GstRTSPStreamTransport *trans;
1622 GST_INFO ("%p: source %p timeout", stream, source);
1624 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1625 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1626 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1631 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1634 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1635 g_list_free (priv->tr_cache_rtp);
1636 priv->tr_cache_rtp = NULL;
1638 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1639 g_list_free (priv->tr_cache_rtcp);
1640 priv->tr_cache_rtcp = NULL;
1644 static GstFlowReturn
1645 handle_new_sample (GstAppSink * sink, gpointer user_data)
1647 GstRTSPStreamPrivate *priv;
1651 GstRTSPStream *stream;
1654 sample = gst_app_sink_pull_sample (sink);
1658 stream = (GstRTSPStream *) user_data;
1659 priv = stream->priv;
1660 buffer = gst_sample_get_buffer (sample);
1662 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
1664 g_mutex_lock (&priv->lock);
1666 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
1667 clear_tr_cache (priv, is_rtp);
1668 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1669 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1670 priv->tr_cache_rtp =
1671 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
1673 priv->tr_cache_cookie_rtp = priv->transports_cookie;
1676 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
1677 clear_tr_cache (priv, is_rtp);
1678 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1679 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1680 priv->tr_cache_rtcp =
1681 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
1683 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
1686 g_mutex_unlock (&priv->lock);
1689 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
1690 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1691 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1694 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
1695 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1696 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1699 gst_sample_unref (sample);
1704 static GstAppSinkCallbacks sink_cb = {
1705 NULL, /* not interested in EOS */
1706 NULL, /* not interested in preroll samples */
1711 get_rtp_encoder (GstRTSPStream * stream, guint session)
1713 GstRTSPStreamPrivate *priv = stream->priv;
1715 if (priv->srtpenc == NULL) {
1718 name = g_strdup_printf ("srtpenc_%u", session);
1719 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
1722 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
1724 return gst_object_ref (priv->srtpenc);
1728 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
1730 GstRTSPStreamPrivate *priv = stream->priv;
1731 GstElement *oldenc, *enc;
1735 if (priv->idx != session)
1738 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
1740 oldenc = priv->srtpenc;
1741 enc = get_rtp_encoder (stream, session);
1742 name = g_strdup_printf ("rtp_sink_%d", session);
1743 pad = gst_element_get_request_pad (enc, name);
1745 gst_object_unref (pad);
1748 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
1755 request_rtcp_encoder (GstElement * rtpbin, guint session,
1756 GstRTSPStream * stream)
1758 GstRTSPStreamPrivate *priv = stream->priv;
1759 GstElement *oldenc, *enc;
1763 if (priv->idx != session)
1766 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
1768 oldenc = priv->srtpenc;
1769 enc = get_rtp_encoder (stream, session);
1770 name = g_strdup_printf ("rtcp_sink_%d", session);
1771 pad = gst_element_get_request_pad (enc, name);
1773 gst_object_unref (pad);
1776 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
1783 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
1785 GstRTSPStreamPrivate *priv = stream->priv;
1788 GST_DEBUG ("request key %08x", ssrc);
1790 g_mutex_lock (&priv->lock);
1791 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
1792 gst_caps_ref (caps);
1793 g_mutex_unlock (&priv->lock);
1799 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
1800 GstRTSPStream * stream)
1802 GstRTSPStreamPrivate *priv = stream->priv;
1804 if (priv->idx != session)
1807 if (priv->srtpdec == NULL) {
1810 name = g_strdup_printf ("srtpdec_%u", session);
1811 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
1814 g_signal_connect (priv->srtpdec, "request-key",
1815 (GCallback) request_key, stream);
1817 return gst_object_ref (priv->srtpdec);
1821 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPStream * stream)
1825 GstStructure *pt_map;
1830 pt = gst_rtsp_stream_get_pt (stream);
1831 pt_s = g_strdup_printf ("%u", pt);
1832 rtx_pt = stream->priv->rtx_pt;
1834 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
1836 bin = gst_bin_new (NULL);
1837 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
1838 pt_map = gst_structure_new ("application/x-rtp-pt-map",
1839 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1840 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
1841 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
1843 gst_structure_free (pt_map);
1844 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
1846 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
1847 name = g_strdup_printf ("src_%u", sessid);
1848 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1850 gst_object_unref (pad);
1852 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
1853 name = g_strdup_printf ("sink_%u", sessid);
1854 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1856 gst_object_unref (pad);
1862 * gst_rtsp_stream_set_pt_map:
1863 * @stream: a #GstRTSPStream
1867 * Configure a pt map between @pt and @caps.
1870 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
1872 GstRTSPStreamPrivate *priv = stream->priv;
1874 g_mutex_lock (&priv->lock);
1875 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
1876 g_mutex_unlock (&priv->lock);
1880 request_pt_map (GstElement * rtpbin, guint session, guint pt,
1881 GstRTSPStream * stream)
1883 GstRTSPStreamPrivate *priv = stream->priv;
1884 GstCaps *caps = NULL;
1886 g_mutex_lock (&priv->lock);
1888 if (priv->idx == session) {
1889 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
1891 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
1892 gst_caps_ref (caps);
1894 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
1898 g_mutex_unlock (&priv->lock);
1904 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
1906 GstRTSPStreamPrivate *priv = stream->priv;
1908 GstPadLinkReturn ret;
1911 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
1912 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
1914 name = gst_pad_get_name (pad);
1915 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
1921 if (priv->idx != sessid)
1924 if (gst_pad_is_linked (priv->sinkpad)) {
1925 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
1926 GST_DEBUG_PAD_NAME (priv->sinkpad));
1930 /* link the RTP pad to the session manager, it should not really fail unless
1931 * this is not really an RTP pad */
1932 ret = gst_pad_link (pad, priv->sinkpad);
1933 if (ret != GST_PAD_LINK_OK)
1935 priv->recv_rtp_src = gst_object_ref (pad);
1942 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
1943 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
1948 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
1949 GstRTSPStream * stream)
1951 /* TODO: What to do here other than this? */
1952 GST_DEBUG ("Stream %p: Got EOS", stream);
1953 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
1957 * gst_rtsp_stream_join_bin:
1958 * @stream: a #GstRTSPStream
1959 * @bin: (transfer none): a #GstBin to join
1960 * @rtpbin: (transfer none): a rtpbin element in @bin
1961 * @state: the target state of the new elements
1963 * Join the #GstBin @bin that contains the element @rtpbin.
1965 * @stream will link to @rtpbin, which must be inside @bin. The elements
1966 * added to @bin will be set to the state given in @state.
1968 * Returns: %TRUE on success.
1971 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1972 GstElement * rtpbin, GstState state)
1974 GstRTSPStreamPrivate *priv;
1978 GstPad *pad, *sinkpad, *selpad;
1979 GstPadLinkReturn ret;
1981 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1982 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1983 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1985 priv = stream->priv;
1987 g_mutex_lock (&priv->lock);
1988 if (priv->is_joined)
1991 /* create a session with the same index as the stream */
1994 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1996 if (!alloc_ports (stream))
1999 /* update the dscp qos field in the sinks */
2000 update_dscp_qos (stream);
2002 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2003 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2005 g_signal_connect (rtpbin, "request-rtp-encoder",
2006 (GCallback) request_rtp_encoder, stream);
2007 g_signal_connect (rtpbin, "request-rtcp-encoder",
2008 (GCallback) request_rtcp_encoder, stream);
2009 g_signal_connect (rtpbin, "request-rtp-decoder",
2010 (GCallback) request_rtp_rtcp_decoder, stream);
2011 g_signal_connect (rtpbin, "request-rtcp-decoder",
2012 (GCallback) request_rtp_rtcp_decoder, stream);
2015 if (priv->rtx_time > 0 && priv->srcpad) {
2016 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
2017 g_signal_connect (rtpbin, "request-aux-sender",
2018 (GCallback) request_aux_sender, stream);
2020 if (priv->sinkpad) {
2021 g_signal_connect (rtpbin, "request-pt-map",
2022 (GCallback) request_pt_map, stream);
2025 /* get a pad for sending RTP */
2026 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2027 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2031 /* link the RTP pad to the session manager, it should not really fail unless
2032 * this is not really an RTP pad */
2033 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2034 if (ret != GST_PAD_LINK_OK)
2037 /* Need to connect our sinkpad from here */
2038 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2040 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2043 /* get pads from the RTP session element for sending and receiving
2045 name = g_strdup_printf ("send_rtp_src_%u", idx);
2046 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2048 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2049 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2052 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2053 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2055 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2056 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2059 /* get the session */
2060 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2062 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2064 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2066 g_signal_connect (priv->session, "on-ssrc-active",
2067 (GCallback) on_ssrc_active, stream);
2068 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2070 g_signal_connect (priv->session, "on-bye-timeout",
2071 (GCallback) on_bye_timeout, stream);
2072 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2075 for (i = 0; i < 2; i++) {
2076 GstPad *teepad, *queuepad;
2077 /* For the sender we create this bit of pipeline for both
2078 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2079 * we need to add a queue before appsink to make the pipeline
2080 * not block. For the TCP case, we want to pump data to the
2081 * client as fast as possible anyway.
2083 * .--------. .-----. .---------.
2084 * | rtpbin | | tee | | udpsink |
2085 * | send->sink src->sink |
2086 * '--------' | | '---------'
2087 * | | .---------. .---------.
2088 * | | | queue | | appsink |
2089 * | src->sink src->sink |
2090 * '-----' '---------' '---------'
2092 * When only UDP is allowed, we skip the tee, queue and appsink and link the
2093 * udpsink directly to the session.
2096 gst_bin_add (bin, priv->udpsink[i]);
2097 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
2099 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
2100 /* make tee for RTP/RTCP */
2101 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2102 gst_bin_add (bin, priv->tee[i]);
2104 /* and link to rtpbin send pad */
2105 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2106 gst_pad_link (priv->send_src[i], pad);
2107 gst_object_unref (pad);
2109 priv->udpqueue[i] = gst_element_factory_make ("queue", NULL);
2110 gst_bin_add (bin, priv->udpqueue[i]);
2111 /* link tee to udpqueue */
2112 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2113 pad = gst_element_get_static_pad (priv->udpqueue[i], "sink");
2114 gst_pad_link (teepad, pad);
2115 gst_object_unref (pad);
2116 gst_object_unref (teepad);
2118 /* link udpqueue to udpsink */
2119 queuepad = gst_element_get_static_pad (priv->udpqueue[i], "src");
2120 gst_pad_link (queuepad, sinkpad);
2121 gst_object_unref (queuepad);
2124 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
2125 gst_bin_add (bin, priv->appqueue[i]);
2126 /* and link to tee */
2127 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2128 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
2129 gst_pad_link (teepad, pad);
2130 gst_object_unref (pad);
2131 gst_object_unref (teepad);
2134 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2135 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2136 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2137 gst_bin_add (bin, priv->appsink[i]);
2138 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2139 &sink_cb, stream, NULL);
2140 /* and link to queue */
2141 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
2142 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2143 gst_pad_link (queuepad, pad);
2144 gst_object_unref (pad);
2145 gst_object_unref (queuepad);
2147 /* else only udpsink needed, link it to the session */
2148 gst_pad_link (priv->send_src[i], sinkpad);
2150 gst_object_unref (sinkpad);
2152 /* For the receiver we create this bit of pipeline for both
2153 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2154 * and it is all funneled into the rtpbin receive pad.
2156 * .--------. .--------. .--------.
2157 * | udpsrc | | funnel | | rtpbin |
2158 * | src->sink src->sink |
2159 * '--------' | | '--------'
2163 * '--------' '--------'
2165 /* make funnel for the RTP/RTCP receivers */
2166 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2167 gst_bin_add (bin, priv->funnel[i]);
2169 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2170 gst_pad_link (pad, priv->recv_sink[i]);
2171 gst_object_unref (pad);
2173 if (priv->udpsrc_v4[i]) {
2175 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2176 * values. This is only relevant for PLAY pipelines */
2177 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
2178 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
2181 gst_bin_add (bin, priv->udpsrc_v4[i]);
2183 /* and link to the funnel v4 */
2184 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2185 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
2186 gst_pad_link (pad, selpad);
2187 gst_object_unref (pad);
2188 gst_object_unref (selpad);
2191 if (priv->udpsrc_v6[i]) {
2193 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
2194 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
2196 gst_bin_add (bin, priv->udpsrc_v6[i]);
2198 /* and link to the funnel v6 */
2199 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2200 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2201 gst_pad_link (pad, selpad);
2202 gst_object_unref (pad);
2203 gst_object_unref (selpad);
2206 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
2207 /* make and add appsrc */
2208 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2209 priv->appsrc_base_time[i] = -1;
2210 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, NULL);
2211 gst_bin_add (bin, priv->appsrc[i]);
2212 /* and link to the funnel */
2213 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2214 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2215 gst_pad_link (pad, selpad);
2216 gst_object_unref (pad);
2217 gst_object_unref (selpad);
2220 /* check if we need to set to a special state */
2221 if (state != GST_STATE_NULL) {
2222 if (priv->udpsink[i])
2223 gst_element_set_state (priv->udpsink[i], state);
2224 if (priv->appsink[i])
2225 gst_element_set_state (priv->appsink[i], state);
2226 if (priv->appqueue[i])
2227 gst_element_set_state (priv->appqueue[i], state);
2228 if (priv->udpqueue[i])
2229 gst_element_set_state (priv->udpqueue[i], state);
2231 gst_element_set_state (priv->tee[i], state);
2232 if (priv->funnel[i])
2233 gst_element_set_state (priv->funnel[i], state);
2234 if (priv->appsrc[i])
2235 gst_element_set_state (priv->appsrc[i], state);
2239 /* be notified of caps changes */
2240 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2241 (GCallback) caps_notify, stream);
2243 priv->is_joined = TRUE;
2244 g_mutex_unlock (&priv->lock);
2251 g_mutex_unlock (&priv->lock);
2256 g_mutex_unlock (&priv->lock);
2257 GST_WARNING ("failed to allocate ports %u", idx);
2262 GST_WARNING ("failed to link stream %u", idx);
2263 gst_object_unref (priv->send_rtp_sink);
2264 priv->send_rtp_sink = NULL;
2265 g_mutex_unlock (&priv->lock);
2271 * gst_rtsp_stream_leave_bin:
2272 * @stream: a #GstRTSPStream
2273 * @bin: (transfer none): a #GstBin
2274 * @rtpbin: (transfer none): a rtpbin #GstElement
2276 * Remove the elements of @stream from @bin.
2278 * Return: %TRUE on success.
2281 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2282 GstElement * rtpbin)
2284 GstRTSPStreamPrivate *priv;
2288 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2289 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2290 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2292 priv = stream->priv;
2294 g_mutex_lock (&priv->lock);
2295 if (!priv->is_joined)
2296 goto was_not_joined;
2298 /* all transports must be removed by now */
2299 if (priv->transports != NULL)
2300 goto transports_not_removed;
2302 clear_tr_cache (priv, TRUE);
2303 clear_tr_cache (priv, FALSE);
2305 GST_INFO ("stream %p leaving bin", stream);
2308 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2309 } else if (priv->recv_rtp_src) {
2310 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2311 gst_object_unref (priv->recv_rtp_src);
2312 priv->recv_rtp_src = NULL;
2314 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2315 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2316 gst_object_unref (priv->send_rtp_sink);
2317 priv->send_rtp_sink = NULL;
2319 for (i = 0; i < 2; i++) {
2320 if (priv->udpsink[i])
2321 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
2322 if (priv->appsink[i])
2323 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
2324 if (priv->appqueue[i])
2325 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
2326 if (priv->udpqueue[i])
2327 gst_element_set_state (priv->udpqueue[i], GST_STATE_NULL);
2329 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
2330 if (priv->funnel[i])
2331 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
2332 if (priv->appsrc[i])
2333 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2334 if (priv->udpsrc_v4[i]) {
2335 /* and set udpsrc to NULL now before removing */
2336 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
2337 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2338 /* removing them should also nicely release the request
2339 * pads when they finalize */
2340 gst_bin_remove (bin, priv->udpsrc_v4[i]);
2342 if (priv->udpsrc_v6[i]) {
2343 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
2344 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2345 gst_bin_remove (bin, priv->udpsrc_v6[i]);
2348 for (l = priv->transport_sources; l; l = l->next) {
2349 GstRTSPMulticastTransportSource *s = l->data;
2354 gst_element_set_locked_state (s->udpsrc[i], FALSE);
2355 gst_element_set_state (s->udpsrc[i], GST_STATE_NULL);
2356 gst_bin_remove (bin, s->udpsrc[i]);
2359 if (priv->udpsink[i])
2360 gst_bin_remove (bin, priv->udpsink[i]);
2361 if (priv->appsrc[i])
2362 gst_bin_remove (bin, priv->appsrc[i]);
2363 if (priv->appsink[i])
2364 gst_bin_remove (bin, priv->appsink[i]);
2365 if (priv->appqueue[i])
2366 gst_bin_remove (bin, priv->appqueue[i]);
2367 if (priv->udpqueue[i])
2368 gst_bin_remove (bin, priv->udpqueue[i]);
2370 gst_bin_remove (bin, priv->tee[i]);
2371 if (priv->funnel[i])
2372 gst_bin_remove (bin, priv->funnel[i]);
2374 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2375 gst_object_unref (priv->recv_sink[i]);
2376 priv->recv_sink[i] = NULL;
2378 priv->udpsrc_v4[i] = NULL;
2379 priv->udpsrc_v6[i] = NULL;
2380 priv->udpsink[i] = NULL;
2381 priv->appsrc[i] = NULL;
2382 priv->appsink[i] = NULL;
2383 priv->appqueue[i] = NULL;
2384 priv->udpqueue[i] = NULL;
2385 priv->tee[i] = NULL;
2386 priv->funnel[i] = NULL;
2389 for (l = priv->transport_sources; l; l = l->next) {
2390 GstRTSPMulticastTransportSource *s = l->data;
2391 g_slice_free (GstRTSPMulticastTransportSource, s);
2393 g_list_free (priv->transport_sources);
2394 priv->transport_sources = NULL;
2396 gst_object_unref (priv->send_src[0]);
2397 priv->send_src[0] = NULL;
2399 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2400 gst_object_unref (priv->send_src[1]);
2401 priv->send_src[1] = NULL;
2403 g_object_unref (priv->session);
2404 priv->session = NULL;
2406 gst_caps_unref (priv->caps);
2410 gst_object_unref (priv->srtpenc);
2412 gst_object_unref (priv->srtpdec);
2414 priv->is_joined = FALSE;
2415 g_mutex_unlock (&priv->lock);
2421 g_mutex_unlock (&priv->lock);
2424 transports_not_removed:
2426 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2427 g_mutex_unlock (&priv->lock);
2433 * gst_rtsp_stream_get_rtpinfo:
2434 * @stream: a #GstRTSPStream
2435 * @rtptime: (allow-none): result RTP timestamp
2436 * @seq: (allow-none): result RTP seqnum
2437 * @clock_rate: (allow-none): the clock rate
2438 * @running_time: (allow-none): result running-time
2440 * Retrieve the current rtptime, seq and running-time. This is used to
2441 * construct a RTPInfo reply header.
2443 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2446 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2447 guint * rtptime, guint * seq, guint * clock_rate,
2448 GstClockTime * running_time)
2450 GstRTSPStreamPrivate *priv;
2451 GstStructure *stats;
2452 GObjectClass *payobjclass;
2454 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2456 priv = stream->priv;
2458 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2460 g_mutex_lock (&priv->lock);
2462 /* First try to extract the information from the last buffer on the sinks.
2463 * This will have a more accurate sequence number and timestamp, as between
2464 * the payloader and the sink there can be some queues
2466 if (priv->udpsink[0] || priv->appsink[0]) {
2467 GstSample *last_sample;
2469 if (priv->udpsink[0])
2470 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
2472 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
2477 GstSegment *segment;
2478 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
2480 caps = gst_sample_get_caps (last_sample);
2481 buffer = gst_sample_get_buffer (last_sample);
2482 segment = gst_sample_get_segment (last_sample);
2484 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
2486 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
2490 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
2493 gst_rtp_buffer_unmap (&rtp_buffer);
2497 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
2498 GST_BUFFER_TIMESTAMP (buffer));
2502 GstStructure *s = gst_caps_get_structure (caps, 0);
2504 gst_structure_get_uint (s, "clock-rate", clock_rate);
2505 if (*clock_rate == 0 && running_time)
2506 *running_time = GST_CLOCK_TIME_NONE;
2508 gst_sample_unref (last_sample);
2512 gst_sample_unref (last_sample);
2517 if (g_object_class_find_property (payobjclass, "stats")) {
2518 g_object_get (priv->payloader, "stats", &stats, NULL);
2523 gst_structure_get_uint (stats, "seqnum", seq);
2526 gst_structure_get_uint (stats, "timestamp", rtptime);
2529 gst_structure_get_clock_time (stats, "running-time", running_time);
2532 gst_structure_get_uint (stats, "clock-rate", clock_rate);
2533 if (*clock_rate == 0 && running_time)
2534 *running_time = GST_CLOCK_TIME_NONE;
2536 gst_structure_free (stats);
2538 if (!g_object_class_find_property (payobjclass, "seqnum") ||
2539 !g_object_class_find_property (payobjclass, "timestamp"))
2543 g_object_get (priv->payloader, "seqnum", seq, NULL);
2546 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
2549 *running_time = GST_CLOCK_TIME_NONE;
2553 g_mutex_unlock (&priv->lock);
2560 GST_WARNING ("Could not get payloader stats");
2561 g_mutex_unlock (&priv->lock);
2567 * gst_rtsp_stream_get_caps:
2568 * @stream: a #GstRTSPStream
2570 * Retrieve the current caps of @stream.
2572 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
2576 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
2578 GstRTSPStreamPrivate *priv;
2581 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2583 priv = stream->priv;
2585 g_mutex_lock (&priv->lock);
2586 if ((result = priv->caps))
2587 gst_caps_ref (result);
2588 g_mutex_unlock (&priv->lock);
2594 * gst_rtsp_stream_recv_rtp:
2595 * @stream: a #GstRTSPStream
2596 * @buffer: (transfer full): a #GstBuffer
2598 * Handle an RTP buffer for the stream. This method is usually called when a
2599 * message has been received from a client using the TCP transport.
2601 * This function takes ownership of @buffer.
2603 * Returns: a GstFlowReturn.
2606 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
2608 GstRTSPStreamPrivate *priv;
2610 GstElement *element;
2612 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2613 priv = stream->priv;
2614 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2615 g_return_val_if_fail (priv->is_joined, FALSE);
2617 g_mutex_lock (&priv->lock);
2618 if (priv->appsrc[0])
2619 element = gst_object_ref (priv->appsrc[0]);
2622 g_mutex_unlock (&priv->lock);
2625 if (priv->appsrc_base_time[0] == -1) {
2626 /* Take current running_time. This timestamp will be put on
2627 * the first buffer of each stream because we are a live source and so we
2628 * timestamp with the running_time. When we are dealing with TCP, we also
2629 * only timestamp the first buffer (using the DISCONT flag) because a server
2630 * typically bursts data, for which we don't want to compensate by speeding
2631 * up the media. The other timestamps will be interpollated from this one
2632 * using the RTP timestamps. */
2633 GST_OBJECT_LOCK (element);
2634 if (GST_ELEMENT_CLOCK (element)) {
2636 GstClockTime base_time;
2638 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
2639 base_time = GST_ELEMENT_CAST (element)->base_time;
2641 priv->appsrc_base_time[0] = now - base_time;
2642 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
2643 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
2644 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
2645 GST_TIME_ARGS (base_time));
2647 GST_OBJECT_UNLOCK (element);
2650 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2651 gst_object_unref (element);
2659 * gst_rtsp_stream_recv_rtcp:
2660 * @stream: a #GstRTSPStream
2661 * @buffer: (transfer full): a #GstBuffer
2663 * Handle an RTCP buffer for the stream. This method is usually called when a
2664 * message has been received from a client using the TCP transport.
2666 * This function takes ownership of @buffer.
2668 * Returns: a GstFlowReturn.
2671 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
2673 GstRTSPStreamPrivate *priv;
2675 GstElement *element;
2677 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2678 priv = stream->priv;
2679 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2681 if (!priv->is_joined) {
2682 gst_buffer_unref (buffer);
2683 return GST_FLOW_NOT_LINKED;
2685 g_mutex_lock (&priv->lock);
2686 if (priv->appsrc[1])
2687 element = gst_object_ref (priv->appsrc[1]);
2690 g_mutex_unlock (&priv->lock);
2693 if (priv->appsrc_base_time[1] == -1) {
2694 /* Take current running_time. This timestamp will be put on
2695 * the first buffer of each stream because we are a live source and so we
2696 * timestamp with the running_time. When we are dealing with TCP, we also
2697 * only timestamp the first buffer (using the DISCONT flag) because a server
2698 * typically bursts data, for which we don't want to compensate by speeding
2699 * up the media. The other timestamps will be interpollated from this one
2700 * using the RTP timestamps. */
2701 GST_OBJECT_LOCK (element);
2702 if (GST_ELEMENT_CLOCK (element)) {
2704 GstClockTime base_time;
2706 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
2707 base_time = GST_ELEMENT_CAST (element)->base_time;
2709 priv->appsrc_base_time[1] = now - base_time;
2710 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
2711 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
2712 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
2713 GST_TIME_ARGS (base_time));
2715 GST_OBJECT_UNLOCK (element);
2718 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2719 gst_object_unref (element);
2722 gst_buffer_unref (buffer);
2727 /* must be called with lock */
2729 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2732 GstRTSPStreamPrivate *priv = stream->priv;
2733 const GstRTSPTransport *tr;
2735 tr = gst_rtsp_stream_transport_get_transport (trans);
2737 switch (tr->lower_transport) {
2738 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2740 GstRTSPMulticastTransportSource *source;
2743 bin = GST_BIN (gst_object_get_parent (GST_OBJECT (priv->funnel[0])));
2748 GstPad *selpad, *pad;
2750 source = g_slice_new0 (GstRTSPMulticastTransportSource);
2751 source->transport = trans;
2753 for (i = 0; i < 2; i++) {
2755 g_strdup_printf ("udp://%s:%d", tr->destination,
2756 (i == 0) ? tr->port.min : tr->port.max);
2758 gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2762 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2763 * values. This is only relevant for PLAY pipelines */
2764 gst_element_set_state (source->udpsrc[i], GST_STATE_PLAYING);
2765 gst_element_set_locked_state (source->udpsrc[i], TRUE);
2768 gst_bin_add (bin, source->udpsrc[i]);
2770 /* and link to the funnel v4 */
2771 source->selpad[i] = selpad =
2772 gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2773 pad = gst_element_get_static_pad (source->udpsrc[i], "src");
2774 gst_pad_link (pad, selpad);
2775 gst_object_unref (pad);
2776 gst_object_unref (selpad);
2778 gst_object_unref (bin);
2780 priv->transport_sources =
2781 g_list_prepend (priv->transport_sources, source);
2785 for (l = priv->transport_sources; l; l = l->next) {
2788 if (source->transport == trans) {
2789 priv->transport_sources =
2790 g_list_delete_link (priv->transport_sources, l);
2798 for (i = 0; i < 2; i++) {
2799 /* Will automatically unlink everything */
2800 gst_bin_remove (bin,
2801 GST_ELEMENT (gst_object_ref (source->udpsrc[i])));
2803 gst_element_set_state (source->udpsrc[i], GST_STATE_NULL);
2804 gst_object_unref (source->udpsrc[i]);
2806 gst_element_release_request_pad (priv->funnel[i],
2810 g_slice_free (GstRTSPMulticastTransportSource, source);
2814 /* fall through for the generic case */
2816 case GST_RTSP_LOWER_TRANS_UDP:
2822 dest = tr->destination;
2823 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2828 min = tr->client_port.min;
2829 max = tr->client_port.max;
2834 GST_INFO ("setting ttl-mc %d", ttl);
2835 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
2836 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
2838 GST_INFO ("adding %s:%d-%d", dest, min, max);
2839 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
2840 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
2841 priv->transports = g_list_prepend (priv->transports, trans);
2843 GST_INFO ("removing %s:%d-%d", dest, min, max);
2844 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
2845 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
2846 priv->transports = g_list_remove (priv->transports, trans);
2848 priv->transports_cookie++;
2851 case GST_RTSP_LOWER_TRANS_TCP:
2853 GST_INFO ("adding TCP %s", tr->destination);
2854 priv->transports = g_list_prepend (priv->transports, trans);
2856 GST_INFO ("removing TCP %s", tr->destination);
2857 priv->transports = g_list_remove (priv->transports, trans);
2859 priv->transports_cookie++;
2862 goto unknown_transport;
2869 GST_INFO ("Unknown transport %d", tr->lower_transport);
2876 * gst_rtsp_stream_add_transport:
2877 * @stream: a #GstRTSPStream
2878 * @trans: (transfer none): a #GstRTSPStreamTransport
2880 * Add the transport in @trans to @stream. The media of @stream will
2881 * then also be send to the values configured in @trans.
2883 * @stream must be joined to a bin.
2885 * @trans must contain a valid #GstRTSPTransport.
2887 * Returns: %TRUE if @trans was added
2890 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
2891 GstRTSPStreamTransport * trans)
2893 GstRTSPStreamPrivate *priv;
2896 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2897 priv = stream->priv;
2898 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2899 g_return_val_if_fail (priv->is_joined, FALSE);
2901 g_mutex_lock (&priv->lock);
2902 res = update_transport (stream, trans, TRUE);
2903 g_mutex_unlock (&priv->lock);
2909 * gst_rtsp_stream_remove_transport:
2910 * @stream: a #GstRTSPStream
2911 * @trans: (transfer none): a #GstRTSPStreamTransport
2913 * Remove the transport in @trans from @stream. The media of @stream will
2914 * not be sent to the values configured in @trans.
2916 * @stream must be joined to a bin.
2918 * @trans must contain a valid #GstRTSPTransport.
2920 * Returns: %TRUE if @trans was removed
2923 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2924 GstRTSPStreamTransport * trans)
2926 GstRTSPStreamPrivate *priv;
2929 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2930 priv = stream->priv;
2931 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2932 g_return_val_if_fail (priv->is_joined, FALSE);
2934 g_mutex_lock (&priv->lock);
2935 res = update_transport (stream, trans, FALSE);
2936 g_mutex_unlock (&priv->lock);
2942 * gst_rtsp_stream_update_crypto:
2943 * @stream: a #GstRTSPStream
2945 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
2947 * Update the new crypto information for @ssrc in @stream. If information
2948 * for @ssrc did not exist, it will be added. If information
2949 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
2950 * be removed from @stream.
2952 * Returns: %TRUE if @crypto could be updated
2955 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
2956 guint ssrc, GstCaps * crypto)
2958 GstRTSPStreamPrivate *priv;
2960 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2961 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
2963 priv = stream->priv;
2965 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
2967 g_mutex_lock (&priv->lock);
2969 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
2970 gst_caps_ref (crypto));
2972 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
2973 g_mutex_unlock (&priv->lock);
2979 * gst_rtsp_stream_get_rtp_socket:
2980 * @stream: a #GstRTSPStream
2981 * @family: the socket family
2983 * Get the RTP socket from @stream for a @family.
2985 * @stream must be joined to a bin.
2987 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
2988 * socket could be allocated for @family. Unref after usage
2991 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2993 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2997 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2998 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2999 family == G_SOCKET_FAMILY_IPV6, NULL);
3000 g_return_val_if_fail (priv->udpsink[0], NULL);
3002 if (family == G_SOCKET_FAMILY_IPV6)
3007 g_object_get (priv->udpsink[0], name, &socket, NULL);
3013 * gst_rtsp_stream_get_rtcp_socket:
3014 * @stream: a #GstRTSPStream
3015 * @family: the socket family
3017 * Get the RTCP socket from @stream for a @family.
3019 * @stream must be joined to a bin.
3021 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3022 * socket could be allocated for @family. Unref after usage
3025 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3027 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3031 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3032 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3033 family == G_SOCKET_FAMILY_IPV6, NULL);
3034 g_return_val_if_fail (priv->udpsink[1], NULL);
3036 if (family == G_SOCKET_FAMILY_IPV6)
3041 g_object_get (priv->udpsink[1], name, &socket, NULL);
3047 * gst_rtsp_stream_set_seqnum:
3048 * @stream: a #GstRTSPStream
3049 * @seqnum: a new sequence number
3051 * Configure the sequence number in the payloader of @stream to @seqnum.
3054 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3056 GstRTSPStreamPrivate *priv;
3058 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3060 priv = stream->priv;
3062 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3066 * gst_rtsp_stream_get_seqnum:
3067 * @stream: a #GstRTSPStream
3069 * Get the configured sequence number in the payloader of @stream.
3071 * Returns: the sequence number of the payloader.
3074 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3076 GstRTSPStreamPrivate *priv;
3079 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3081 priv = stream->priv;
3083 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3089 * gst_rtsp_stream_transport_filter:
3090 * @stream: a #GstRTSPStream
3091 * @func: (scope call) (allow-none): a callback
3092 * @user_data: (closure): user data passed to @func
3094 * Call @func for each transport managed by @stream. The result value of @func
3095 * determines what happens to the transport. @func will be called with @stream
3096 * locked so no further actions on @stream can be performed from @func.
3098 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3101 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3103 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3104 * will also be added with an additional ref to the result #GList of this
3107 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3109 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3110 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3111 * element in the #GList should be unreffed before the list is freed.
3114 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3115 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3117 GstRTSPStreamPrivate *priv;
3118 GList *result, *walk, *next;
3119 GHashTable *visited = NULL;
3122 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3124 priv = stream->priv;
3128 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3130 g_mutex_lock (&priv->lock);
3132 cookie = priv->transports_cookie;
3133 for (walk = priv->transports; walk; walk = next) {
3134 GstRTSPStreamTransport *trans = walk->data;
3135 GstRTSPFilterResult res;
3138 next = g_list_next (walk);
3141 /* only visit each transport once */
3142 if (g_hash_table_contains (visited, trans))
3145 g_hash_table_add (visited, g_object_ref (trans));
3146 g_mutex_unlock (&priv->lock);
3148 res = func (stream, trans, user_data);
3150 g_mutex_lock (&priv->lock);
3152 res = GST_RTSP_FILTER_REF;
3154 changed = (cookie != priv->transports_cookie);
3157 case GST_RTSP_FILTER_REMOVE:
3158 update_transport (stream, trans, FALSE);
3160 case GST_RTSP_FILTER_REF:
3161 result = g_list_prepend (result, g_object_ref (trans));
3163 case GST_RTSP_FILTER_KEEP:
3170 g_mutex_unlock (&priv->lock);
3173 g_hash_table_unref (visited);
3178 static GstPadProbeReturn
3179 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3181 GstRTSPStreamPrivate *priv;
3182 GstRTSPStream *stream;
3185 priv = stream->priv;
3187 GST_DEBUG_OBJECT (pad, "now blocking");
3189 g_mutex_lock (&priv->lock);
3190 priv->blocking = TRUE;
3191 g_mutex_unlock (&priv->lock);
3193 gst_element_post_message (priv->payloader,
3194 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3195 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3197 return GST_PAD_PROBE_OK;
3201 * gst_rtsp_stream_set_blocked:
3202 * @stream: a #GstRTSPStream
3203 * @blocked: boolean indicating we should block or unblock
3205 * Blocks or unblocks the dataflow on @stream.
3207 * Returns: %TRUE on success
3210 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3212 GstRTSPStreamPrivate *priv;
3214 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3216 priv = stream->priv;
3218 g_mutex_lock (&priv->lock);
3220 priv->blocking = FALSE;
3221 if (priv->blocked_id == 0) {
3222 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3223 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3224 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3225 g_object_ref (stream), g_object_unref);
3228 if (priv->blocked_id != 0) {
3229 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3230 priv->blocked_id = 0;
3231 priv->blocking = FALSE;
3234 g_mutex_unlock (&priv->lock);
3240 * gst_rtsp_stream_is_blocking:
3241 * @stream: a #GstRTSPStream
3243 * Check if @stream is blocking on a #GstBuffer.
3245 * Returns: %TRUE if @stream is blocking
3248 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3250 GstRTSPStreamPrivate *priv;
3253 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3255 priv = stream->priv;
3257 g_mutex_lock (&priv->lock);
3258 result = priv->blocking;
3259 g_mutex_unlock (&priv->lock);
3265 * gst_rtsp_stream_query_position:
3266 * @stream: a #GstRTSPStream
3268 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3269 * the RTP parts of the pipeline and not the RTCP parts.
3271 * Returns: %TRUE if the position could be queried
3274 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3276 GstRTSPStreamPrivate *priv;
3280 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3282 priv = stream->priv;
3284 g_mutex_lock (&priv->lock);
3285 if ((sink = priv->udpsink[0]))
3286 gst_object_ref (sink);
3287 g_mutex_unlock (&priv->lock);
3292 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3293 gst_object_unref (sink);
3299 * gst_rtsp_stream_query_stop:
3300 * @stream: a #GstRTSPStream
3302 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3303 * the RTP parts of the pipeline and not the RTCP parts.
3305 * Returns: %TRUE if the stop could be queried
3308 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3310 GstRTSPStreamPrivate *priv;
3315 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3317 priv = stream->priv;
3319 g_mutex_lock (&priv->lock);
3320 if ((sink = priv->udpsink[0]))
3321 gst_object_ref (sink);
3322 g_mutex_unlock (&priv->lock);
3327 query = gst_query_new_segment (GST_FORMAT_TIME);
3328 if ((ret = gst_element_query (sink, query))) {
3331 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3332 if (format != GST_FORMAT_TIME)
3335 gst_query_unref (query);
3336 gst_object_unref (sink);