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))
65 struct _GstRTSPStreamPrivate
69 /* Only one pad is ever set */
70 GstPad *srcpad, *sinkpad;
71 GstElement *payloader;
75 /* TRUE if this stream is running on
76 * the client side of an RTSP link (for RECORD) */
80 GstRTSPProfile profiles;
81 GstRTSPLowerTrans protocols;
83 /* pads on the rtpbin */
84 GstPad *send_rtp_sink;
89 /* the RTPSession object */
92 /* SRTP encoder/decoder */
97 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
99 GstElement *udpsrc_v4[2];
100 /* UDP sources for UDP multicast transports */
101 GstElement *udpsrc_mcast_v4[2];
103 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
105 GstElement *udpsrc_v6[2];
106 /* UDP sources for UDP multicast transports */
107 GstElement *udpsrc_mcast_v6[2];
109 GstElement *udpqueue[2];
110 GstElement *udpsink[2];
112 /* for TCP transport */
113 GstElement *appsrc[2];
114 GstClockTime appsrc_base_time[2];
115 GstElement *appqueue[2];
116 GstElement *appsink[2];
119 GstElement *funnel[2];
124 GstClockTime rtx_time;
126 /* server ports for sending/receiving over ipv4 */
127 GstRTSPRange server_port_v4;
128 GstRTSPAddress *server_addr_v4;
131 /* server ports for sending/receiving over ipv6 */
132 GstRTSPRange server_port_v6;
133 GstRTSPAddress *server_addr_v6;
136 /* multicast addresses */
137 GstRTSPAddressPool *pool;
138 GstRTSPAddress *addr_v4;
139 GstRTSPAddress *addr_v6;
140 gboolean have_ipv4_mcast;
141 gboolean have_ipv6_mcast;
143 /* the caps of the stream */
147 /* transports we stream to */
150 guint transports_cookie;
152 GList *tr_cache_rtcp;
153 guint tr_cache_cookie_rtp;
154 guint tr_cache_cookie_rtcp;
159 /* stream blocking */
163 /* pt->caps map for RECORD streams */
167 #define DEFAULT_CONTROL NULL
168 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
169 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
170 GST_RTSP_LOWER_TRANS_TCP
183 SIGNAL_NEW_RTP_ENCODER,
184 SIGNAL_NEW_RTCP_ENCODER,
188 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
189 #define GST_CAT_DEFAULT rtsp_stream_debug
191 static GQuark ssrc_stream_map_key;
193 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
194 GValue * value, GParamSpec * pspec);
195 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
196 const GValue * value, GParamSpec * pspec);
198 static void gst_rtsp_stream_finalize (GObject * obj);
200 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
202 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
205 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
207 GObjectClass *gobject_class;
209 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
211 gobject_class = G_OBJECT_CLASS (klass);
213 gobject_class->get_property = gst_rtsp_stream_get_property;
214 gobject_class->set_property = gst_rtsp_stream_set_property;
215 gobject_class->finalize = gst_rtsp_stream_finalize;
217 g_object_class_install_property (gobject_class, PROP_CONTROL,
218 g_param_spec_string ("control", "Control",
219 "The control string for this stream", DEFAULT_CONTROL,
220 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
222 g_object_class_install_property (gobject_class, PROP_PROFILES,
223 g_param_spec_flags ("profiles", "Profiles",
224 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
225 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
228 g_param_spec_flags ("protocols", "Protocols",
229 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
230 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
233 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
234 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
235 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
237 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
238 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
239 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
240 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
242 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
244 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
248 gst_rtsp_stream_init (GstRTSPStream * stream)
250 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
252 GST_DEBUG ("new stream %p", stream);
257 priv->control = g_strdup (DEFAULT_CONTROL);
258 priv->profiles = DEFAULT_PROFILES;
259 priv->protocols = DEFAULT_PROTOCOLS;
261 g_mutex_init (&priv->lock);
263 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
264 NULL, (GDestroyNotify) gst_caps_unref);
265 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
266 (GDestroyNotify) gst_caps_unref);
270 gst_rtsp_stream_finalize (GObject * obj)
272 GstRTSPStream *stream;
273 GstRTSPStreamPrivate *priv;
275 stream = GST_RTSP_STREAM (obj);
278 GST_DEBUG ("finalize stream %p", stream);
280 /* we really need to be unjoined now */
281 g_return_if_fail (!priv->is_joined);
284 gst_rtsp_address_free (priv->addr_v4);
286 gst_rtsp_address_free (priv->addr_v6);
287 if (priv->server_addr_v4)
288 gst_rtsp_address_free (priv->server_addr_v4);
289 if (priv->server_addr_v6)
290 gst_rtsp_address_free (priv->server_addr_v6);
292 g_object_unref (priv->pool);
294 g_object_unref (priv->rtxsend);
296 gst_object_unref (priv->payloader);
298 gst_object_unref (priv->srcpad);
300 gst_object_unref (priv->sinkpad);
301 g_free (priv->control);
302 g_mutex_clear (&priv->lock);
304 g_hash_table_unref (priv->keys);
305 g_hash_table_destroy (priv->ptmap);
307 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
311 gst_rtsp_stream_get_property (GObject * object, guint propid,
312 GValue * value, GParamSpec * pspec)
314 GstRTSPStream *stream = GST_RTSP_STREAM (object);
318 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
321 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
324 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
327 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
332 gst_rtsp_stream_set_property (GObject * object, guint propid,
333 const GValue * value, GParamSpec * pspec)
335 GstRTSPStream *stream = GST_RTSP_STREAM (object);
339 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
342 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
345 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
353 * gst_rtsp_stream_new:
356 * @payloader: a #GstElement
358 * Create a new media stream with index @idx that handles RTP data on
359 * @pad and has a payloader element @payloader if @pad is a source pad
360 * or a depayloader element @payloader if @pad is a sink pad.
362 * Returns: (transfer full): a new #GstRTSPStream
365 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
367 GstRTSPStreamPrivate *priv;
368 GstRTSPStream *stream;
370 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
371 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
373 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
376 priv->payloader = gst_object_ref (payloader);
377 if (GST_PAD_IS_SRC (pad))
378 priv->srcpad = gst_object_ref (pad);
380 priv->sinkpad = gst_object_ref (pad);
386 * gst_rtsp_stream_get_index:
387 * @stream: a #GstRTSPStream
389 * Get the stream index.
391 * Return: the stream index.
394 gst_rtsp_stream_get_index (GstRTSPStream * stream)
396 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
398 return stream->priv->idx;
402 * gst_rtsp_stream_get_pt:
403 * @stream: a #GstRTSPStream
405 * Get the stream payload type.
407 * Return: the stream payload type.
410 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
412 GstRTSPStreamPrivate *priv;
415 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
419 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
425 * gst_rtsp_stream_get_srcpad:
426 * @stream: a #GstRTSPStream
428 * Get the srcpad associated with @stream.
430 * Returns: (transfer full): the srcpad. Unref after usage.
433 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
435 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
437 if (!stream->priv->srcpad)
440 return gst_object_ref (stream->priv->srcpad);
444 * gst_rtsp_stream_get_sinkpad:
445 * @stream: a #GstRTSPStream
447 * Get the sinkpad associated with @stream.
449 * Returns: (transfer full): the sinkpad. Unref after usage.
452 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
454 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
456 if (!stream->priv->sinkpad)
459 return gst_object_ref (stream->priv->sinkpad);
463 * gst_rtsp_stream_get_control:
464 * @stream: a #GstRTSPStream
466 * Get the control string to identify this stream.
468 * Returns: (transfer full): the control string. g_free() after usage.
471 gst_rtsp_stream_get_control (GstRTSPStream * stream)
473 GstRTSPStreamPrivate *priv;
476 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
480 g_mutex_lock (&priv->lock);
481 if ((result = g_strdup (priv->control)) == NULL)
482 result = g_strdup_printf ("stream=%u", priv->idx);
483 g_mutex_unlock (&priv->lock);
489 * gst_rtsp_stream_set_control:
490 * @stream: a #GstRTSPStream
491 * @control: a control string
493 * Set the control string in @stream.
496 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
498 GstRTSPStreamPrivate *priv;
500 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
504 g_mutex_lock (&priv->lock);
505 g_free (priv->control);
506 priv->control = g_strdup (control);
507 g_mutex_unlock (&priv->lock);
511 * gst_rtsp_stream_has_control:
512 * @stream: a #GstRTSPStream
513 * @control: a control string
515 * Check if @stream has the control string @control.
517 * Returns: %TRUE is @stream has @control as the control string
520 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
522 GstRTSPStreamPrivate *priv;
525 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
529 g_mutex_lock (&priv->lock);
531 res = (g_strcmp0 (priv->control, control) == 0);
535 if (sscanf (control, "stream=%u", &streamid) > 0)
536 res = (streamid == priv->idx);
540 g_mutex_unlock (&priv->lock);
546 * gst_rtsp_stream_set_mtu:
547 * @stream: a #GstRTSPStream
550 * Configure the mtu in the payloader of @stream to @mtu.
553 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
555 GstRTSPStreamPrivate *priv;
557 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
561 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
563 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
567 * gst_rtsp_stream_get_mtu:
568 * @stream: a #GstRTSPStream
570 * Get the configured MTU in the payloader of @stream.
572 * Returns: the MTU of the payloader.
575 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
577 GstRTSPStreamPrivate *priv;
580 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
584 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
589 /* Update the dscp qos property on the udp sinks */
591 update_dscp_qos (GstRTSPStream * stream)
593 GstRTSPStreamPrivate *priv;
595 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
599 if (priv->udpsink[0]) {
600 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
604 if (priv->udpsink[1]) {
605 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
611 * gst_rtsp_stream_set_dscp_qos:
612 * @stream: a #GstRTSPStream
613 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
615 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
618 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
620 GstRTSPStreamPrivate *priv;
622 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
626 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
628 if (dscp_qos < -1 || dscp_qos > 63) {
629 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
633 priv->dscp_qos = dscp_qos;
635 update_dscp_qos (stream);
639 * gst_rtsp_stream_get_dscp_qos:
640 * @stream: a #GstRTSPStream
642 * Get the configured DSCP QoS in of the outgoing sockets.
644 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
647 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
649 GstRTSPStreamPrivate *priv;
651 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
655 return priv->dscp_qos;
659 * gst_rtsp_stream_is_transport_supported:
660 * @stream: a #GstRTSPStream
661 * @transport: (transfer none): a #GstRTSPTransport
663 * Check if @transport can be handled by stream
665 * Returns: %TRUE if @transport can be handled by @stream.
668 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
669 GstRTSPTransport * transport)
671 GstRTSPStreamPrivate *priv;
673 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
677 g_mutex_lock (&priv->lock);
678 if (transport->trans != GST_RTSP_TRANS_RTP)
679 goto unsupported_transmode;
681 if (!(transport->profile & priv->profiles))
682 goto unsupported_profile;
684 if (!(transport->lower_transport & priv->protocols))
685 goto unsupported_ltrans;
687 g_mutex_unlock (&priv->lock);
692 unsupported_transmode:
694 GST_DEBUG ("unsupported transport mode %d", transport->trans);
695 g_mutex_unlock (&priv->lock);
700 GST_DEBUG ("unsupported profile %d", transport->profile);
701 g_mutex_unlock (&priv->lock);
706 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
707 g_mutex_unlock (&priv->lock);
713 * gst_rtsp_stream_set_profiles:
714 * @stream: a #GstRTSPStream
715 * @profiles: the new profiles
717 * Configure the allowed profiles for @stream.
720 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
722 GstRTSPStreamPrivate *priv;
724 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
728 g_mutex_lock (&priv->lock);
729 priv->profiles = profiles;
730 g_mutex_unlock (&priv->lock);
734 * gst_rtsp_stream_get_profiles:
735 * @stream: a #GstRTSPStream
737 * Get the allowed profiles of @stream.
739 * Returns: a #GstRTSPProfile
742 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
744 GstRTSPStreamPrivate *priv;
747 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
751 g_mutex_lock (&priv->lock);
752 res = priv->profiles;
753 g_mutex_unlock (&priv->lock);
759 * gst_rtsp_stream_set_protocols:
760 * @stream: a #GstRTSPStream
761 * @protocols: the new flags
763 * Configure the allowed lower transport for @stream.
766 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
767 GstRTSPLowerTrans protocols)
769 GstRTSPStreamPrivate *priv;
771 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
775 g_mutex_lock (&priv->lock);
776 priv->protocols = protocols;
777 g_mutex_unlock (&priv->lock);
781 * gst_rtsp_stream_get_protocols:
782 * @stream: a #GstRTSPStream
784 * Get the allowed protocols of @stream.
786 * Returns: a #GstRTSPLowerTrans
789 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
791 GstRTSPStreamPrivate *priv;
792 GstRTSPLowerTrans res;
794 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
795 GST_RTSP_LOWER_TRANS_UNKNOWN);
799 g_mutex_lock (&priv->lock);
800 res = priv->protocols;
801 g_mutex_unlock (&priv->lock);
807 * gst_rtsp_stream_set_address_pool:
808 * @stream: a #GstRTSPStream
809 * @pool: (transfer none): a #GstRTSPAddressPool
811 * configure @pool to be used as the address pool of @stream.
814 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
815 GstRTSPAddressPool * pool)
817 GstRTSPStreamPrivate *priv;
818 GstRTSPAddressPool *old;
820 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
824 GST_LOG_OBJECT (stream, "set address pool %p", pool);
826 g_mutex_lock (&priv->lock);
827 if ((old = priv->pool) != pool)
828 priv->pool = pool ? g_object_ref (pool) : NULL;
831 g_mutex_unlock (&priv->lock);
834 g_object_unref (old);
838 * gst_rtsp_stream_get_address_pool:
839 * @stream: a #GstRTSPStream
841 * Get the #GstRTSPAddressPool used as the address pool of @stream.
843 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
847 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
849 GstRTSPStreamPrivate *priv;
850 GstRTSPAddressPool *result;
852 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
856 g_mutex_lock (&priv->lock);
857 if ((result = priv->pool))
858 g_object_ref (result);
859 g_mutex_unlock (&priv->lock);
865 * gst_rtsp_stream_get_multicast_address:
866 * @stream: a #GstRTSPStream
867 * @family: the #GSocketFamily
869 * Get the multicast address of @stream for @family.
871 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
872 * or %NULL when no address could be allocated. gst_rtsp_address_free()
876 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
877 GSocketFamily family)
879 GstRTSPStreamPrivate *priv;
880 GstRTSPAddress *result;
881 GstRTSPAddress **addrp;
882 GstRTSPAddressFlags flags;
884 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
888 if (family == G_SOCKET_FAMILY_IPV6) {
889 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
890 addrp = &priv->addr_v6;
892 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
893 addrp = &priv->addr_v4;
896 g_mutex_lock (&priv->lock);
897 if (*addrp == NULL) {
898 if (priv->pool == NULL)
901 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
903 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
907 result = gst_rtsp_address_copy (*addrp);
908 g_mutex_unlock (&priv->lock);
915 GST_ERROR_OBJECT (stream, "no address pool specified");
916 g_mutex_unlock (&priv->lock);
921 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
922 g_mutex_unlock (&priv->lock);
928 * gst_rtsp_stream_reserve_address:
929 * @stream: a #GstRTSPStream
930 * @address: an address
935 * Reserve @address and @port as the address and port of @stream.
937 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
938 * the address could be reserved. gst_rtsp_address_free() after usage.
941 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
942 const gchar * address, guint port, guint n_ports, guint ttl)
944 GstRTSPStreamPrivate *priv;
945 GstRTSPAddress *result;
947 GSocketFamily family;
948 GstRTSPAddress **addrp;
950 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
951 g_return_val_if_fail (address != NULL, NULL);
952 g_return_val_if_fail (port > 0, NULL);
953 g_return_val_if_fail (n_ports > 0, NULL);
954 g_return_val_if_fail (ttl > 0, NULL);
958 addr = g_inet_address_new_from_string (address);
960 GST_ERROR ("failed to get inet addr from %s", address);
961 family = G_SOCKET_FAMILY_IPV4;
963 family = g_inet_address_get_family (addr);
964 g_object_unref (addr);
967 if (family == G_SOCKET_FAMILY_IPV6)
968 addrp = &priv->addr_v6;
970 addrp = &priv->addr_v4;
972 g_mutex_lock (&priv->lock);
973 if (*addrp == NULL) {
974 GstRTSPAddressPoolResult res;
976 if (priv->pool == NULL)
979 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
980 port, n_ports, ttl, addrp);
981 if (res != GST_RTSP_ADDRESS_POOL_OK)
984 if (strcmp ((*addrp)->address, address) ||
985 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
986 (*addrp)->ttl != ttl)
987 goto different_address;
989 result = gst_rtsp_address_copy (*addrp);
990 g_mutex_unlock (&priv->lock);
997 GST_ERROR_OBJECT (stream, "no address pool specified");
998 g_mutex_unlock (&priv->lock);
1003 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1005 g_mutex_unlock (&priv->lock);
1010 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
1011 " reserved", address);
1012 g_mutex_unlock (&priv->lock);
1017 /* must be called with lock */
1019 set_sockets_for_udpsinks (GstRTSPStream * stream, GSocket * rtp_socket,
1020 GSocket * rtcp_socket, GSocketFamily family)
1022 GstRTSPStreamPrivate *priv = stream->priv;
1023 const gchar *multisink_socket;
1025 if (family == G_SOCKET_FAMILY_IPV6)
1026 multisink_socket = "socket-v6";
1028 multisink_socket = "socket";
1030 g_object_set (G_OBJECT (priv->udpsink[0]), multisink_socket, rtp_socket,
1032 g_object_set (G_OBJECT (priv->udpsink[1]), multisink_socket, rtcp_socket,
1036 /* must be called with lock */
1038 create_and_configure_udpsinks (GstRTSPStream * stream)
1040 GstRTSPStreamPrivate *priv = stream->priv;
1041 GstElement *udpsink0, *udpsink1;
1046 if (priv->udpsink[0])
1047 udpsink0 = priv->udpsink[0];
1049 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1052 goto no_udp_protocol;
1054 if (priv->udpsink[1])
1055 udpsink1 = priv->udpsink[1];
1057 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1060 goto no_udp_protocol;
1062 /* configure sinks */
1064 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1065 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1067 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1068 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1070 g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
1072 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1073 /* Needs to be async for RECORD streams, otherwise we will never go to
1074 * PLAYING because the sinks will wait for data while the udpsrc can't
1075 * provide data with timestamps in PAUSED. */
1077 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1078 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1080 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1081 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1083 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1084 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1086 /* update the dscp qos field in the sinks */
1087 update_dscp_qos (stream);
1089 priv->udpsink[0] = udpsink0;
1090 priv->udpsink[1] = udpsink1;
1101 /* must be called with lock */
1103 play_udpsources_one_family (GstRTSPStream * stream, GstElement * udpsrc_out[2],
1104 GSocketFamily family)
1106 GstRTSPStreamPrivate *priv;
1107 GstPad *pad, *selpad;
1111 priv = stream->priv;
1112 bin = GST_BIN (gst_object_get_parent (GST_OBJECT (priv->funnel[1])));
1114 for (i = 0; i < 2; i++) {
1115 if (priv->sinkpad || i == 1) {
1117 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1118 * values. This is only relevant for PLAY pipelines */
1119 gst_element_set_state (udpsrc_out[i], GST_STATE_PLAYING);
1120 gst_element_set_locked_state (udpsrc_out[i], TRUE);
1123 gst_bin_add (bin, udpsrc_out[i]);
1125 /* and link to the funnel */
1126 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1127 pad = gst_element_get_static_pad (udpsrc_out[i], "src");
1128 gst_pad_link (pad, selpad);
1129 gst_object_unref (pad);
1130 gst_object_unref (selpad);
1132 /* otherwise sync state with parent in case it's running already
1134 if (!priv->srcpad) {
1135 gst_element_sync_state_with_parent (udpsrc_out[i]);
1140 gst_object_unref (bin);
1143 /* must be called with lock */
1145 create_and_configure_udpsources_one_family (GstElement * udpsrc_out[2],
1146 GSocket * rtp_socket, GSocket * rtcp_socket, GSocketFamily family,
1147 const gchar * address, gint rtpport, gint rtcpport,
1148 GstRTSPLowerTrans transport)
1150 GstStateChangeReturn ret;
1152 udpsrc_out[0] = gst_element_factory_make ("udpsrc", NULL);
1153 udpsrc_out[1] = gst_element_factory_make ("udpsrc", NULL);
1155 if (udpsrc_out[0] == NULL || udpsrc_out[1] == NULL)
1158 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1159 g_object_set (G_OBJECT (udpsrc_out[0]), "address", address, NULL);
1160 g_object_set (G_OBJECT (udpsrc_out[1]), "address", address, NULL);
1161 g_object_set (G_OBJECT (udpsrc_out[0]), "port", rtpport, NULL);
1162 g_object_set (G_OBJECT (udpsrc_out[1]), "port", rtcpport, NULL);
1163 g_object_set (G_OBJECT (udpsrc_out[0]), "loop", FALSE, NULL);
1164 g_object_set (G_OBJECT (udpsrc_out[1]), "loop", FALSE, NULL);
1167 g_object_set (G_OBJECT (udpsrc_out[0]), "socket", rtp_socket, NULL);
1168 g_object_set (G_OBJECT (udpsrc_out[1]), "socket", rtcp_socket, NULL);
1170 ret = gst_element_set_state (udpsrc_out[0], GST_STATE_READY);
1171 if (ret == GST_STATE_CHANGE_FAILURE)
1173 ret = gst_element_set_state (udpsrc_out[1], GST_STATE_READY);
1174 if (ret == GST_STATE_CHANGE_FAILURE)
1184 gst_object_unref (udpsrc_out[0]);
1186 gst_object_unref (udpsrc_out[1]);
1192 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1193 GstElement * udpsrc_out[2], GstRTSPRange * server_port_out,
1194 GstRTSPTransport * ct, GstRTSPAddress ** server_addr_out,
1195 gboolean use_client_settings)
1197 GstRTSPStreamPrivate *priv = stream->priv;
1198 GSocket *rtp_socket = NULL;
1199 GSocket *rtcp_socket;
1200 gint tmp_rtp, tmp_rtcp;
1202 gint rtpport, rtcpport;
1203 GList *rejected_addresses = NULL;
1204 GstRTSPAddress *addr = NULL;
1205 GInetAddress *inetaddr = NULL;
1207 GSocketAddress *rtp_sockaddr = NULL;
1208 GSocketAddress *rtcp_sockaddr = NULL;
1209 GstRTSPAddressPool *pool;
1210 GstRTSPLowerTrans transport;
1214 transport = ct->lower_transport;
1216 /* Start with random port */
1219 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1220 G_SOCKET_PROTOCOL_UDP, NULL);
1222 goto no_udp_protocol;
1223 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1225 if (*server_addr_out)
1226 gst_rtsp_address_free (*server_addr_out);
1228 /* try to allocate 2 UDP ports, the RTP port should be an even
1229 * number and the RTCP port should be the next (uneven) port */
1232 if (rtp_socket == NULL) {
1233 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1234 G_SOCKET_PROTOCOL_UDP, NULL);
1236 goto no_udp_protocol;
1237 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1240 if (pool && ((transport == GST_RTSP_LOWER_TRANS_UDP &&
1241 gst_rtsp_address_pool_has_unicast_addresses (pool))
1242 || transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)) {
1243 GstRTSPAddressFlags flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1245 if (transport == GST_RTSP_LOWER_TRANS_UDP)
1246 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1248 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1251 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1253 if (family == G_SOCKET_FAMILY_IPV6)
1254 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1256 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1258 if (ct->destination && transport == GST_RTSP_LOWER_TRANS_UDP_MCAST
1259 && use_client_settings)
1260 gst_rtsp_address_pool_reserve_address (pool, ct->destination,
1261 ct->port.min, 2, ct->ttl, &addr);
1263 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1268 tmp_rtp = addr->port;
1270 g_clear_object (&inetaddr);
1271 inetaddr = g_inet_address_new_from_string (addr->address);
1273 /* On Windows it's not possible to bind to a multicast address
1274 * but the OS will make sure to filter out all packets that
1275 * arrive not for the multicast address the socket joined.
1277 * On Linux and others it is necessary to bind to a multicast
1278 * address to let the OS filter out all packets that are received
1279 * on the same port but for different addresses than the multicast
1283 if (g_inet_address_get_is_multicast (inetaddr)) {
1284 g_object_unref (inetaddr);
1285 inetaddr = g_inet_address_new_any (family);
1295 if (inetaddr == NULL)
1296 inetaddr = g_inet_address_new_any (family);
1299 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1300 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1301 g_object_unref (rtp_sockaddr);
1304 g_object_unref (rtp_sockaddr);
1306 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1307 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1308 g_clear_object (&rtp_sockaddr);
1313 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1314 g_object_unref (rtp_sockaddr);
1316 /* check if port is even */
1317 if ((tmp_rtp & 1) != 0) {
1318 /* port not even, close and allocate another */
1320 g_clear_object (&rtp_socket);
1325 tmp_rtcp = tmp_rtp + 1;
1327 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1328 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1329 g_object_unref (rtcp_sockaddr);
1330 g_clear_object (&rtp_socket);
1333 g_object_unref (rtcp_sockaddr);
1336 addr_str = g_inet_address_to_string (inetaddr);
1338 addr_str = addr->address;
1339 g_clear_object (&inetaddr);
1341 if (!create_and_configure_udpsources_one_family (udpsrc_out, rtp_socket,
1342 rtcp_socket, family, addr_str, tmp_rtp, tmp_rtcp, transport)) {
1345 goto no_udp_protocol;
1351 play_udpsources_one_family (stream, udpsrc_out, family);
1353 g_object_get (G_OBJECT (udpsrc_out[0]), "port", &rtpport, NULL);
1354 g_object_get (G_OBJECT (udpsrc_out[1]), "port", &rtcpport, NULL);
1356 /* this should not happen... */
1357 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1360 /* set RTP and RTCP sockets */
1361 set_sockets_for_udpsinks (stream, rtp_socket, rtcp_socket, family);
1363 server_port_out->min = rtpport;
1364 server_port_out->max = rtcpport;
1366 *server_addr_out = addr;
1367 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1369 g_object_unref (rtp_socket);
1370 g_object_unref (rtcp_socket);
1394 g_object_unref (inetaddr);
1395 g_list_free_full (rejected_addresses,
1396 (GDestroyNotify) gst_rtsp_address_free);
1398 gst_rtsp_address_free (addr);
1400 g_object_unref (rtp_socket);
1402 g_object_unref (rtcp_socket);
1408 * gst_rtsp_stream_allocate_udp_sockets:
1409 * @stream: a #GstRTSPStream
1410 * @family: protocol family
1411 * @transport_method: transport method
1413 * Allocates RTP and RTCP ports.
1415 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1418 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1419 GSocketFamily family, GstRTSPTransport * ct, gboolean use_client_settings)
1421 GstRTSPStreamPrivate *priv;
1422 gboolean result = FALSE;
1423 GstRTSPLowerTrans transport = ct->lower_transport;
1425 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1426 priv = stream->priv;
1427 g_return_val_if_fail (priv->is_joined, FALSE);
1429 g_mutex_lock (&priv->lock);
1431 if (family == G_SOCKET_FAMILY_IPV4) {
1432 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1433 if (priv->have_ipv4_mcast)
1435 priv->have_ipv4_mcast =
1436 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1437 priv->udpsrc_mcast_v4, &priv->server_port_v4, ct, &priv->addr_v4,
1438 use_client_settings);
1441 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4,
1442 &priv->server_port_v4, ct, &priv->server_addr_v4,
1443 use_client_settings);
1446 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1447 if (priv->have_ipv6_mcast)
1449 priv->have_ipv6_mcast =
1450 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1451 priv->udpsrc_mcast_v6, &priv->server_port_v6, ct, &priv->addr_v6,
1452 use_client_settings);
1454 if (priv->have_ipv6)
1457 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6,
1458 &priv->server_port_v6, ct, &priv->server_addr_v6,
1459 use_client_settings);
1464 result = priv->have_ipv4 || priv->have_ipv4_mcast || priv->have_ipv6 ||
1465 priv->have_ipv6_mcast;
1467 g_mutex_unlock (&priv->lock);
1473 * gst_rtsp_stream_set_client_side:
1474 * @stream: a #GstRTSPStream
1475 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1476 * an RTSP connection.
1478 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1479 * streams to an RTSP server via RECORD. This has the practical effect
1480 * of changing which UDP port numbers are used when setting up the local
1481 * side of the stream sending to be either the 'server' or 'client' pair
1482 * of a configured UDP transport.
1485 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1487 GstRTSPStreamPrivate *priv;
1489 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1490 priv = stream->priv;
1491 g_mutex_lock (&priv->lock);
1492 priv->client_side = client_side;
1493 g_mutex_unlock (&priv->lock);
1497 * gst_rtsp_stream_is_client_side:
1498 * @stream: a #GstRTSPStream
1500 * See gst_rtsp_stream_set_client_side()
1502 * Returns: TRUE if this #GstRTSPStream is client-side.
1505 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1507 GstRTSPStreamPrivate *priv;
1510 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1512 priv = stream->priv;
1513 g_mutex_lock (&priv->lock);
1514 ret = priv->client_side;
1515 g_mutex_unlock (&priv->lock);
1521 * gst_rtsp_stream_get_server_port:
1522 * @stream: a #GstRTSPStream
1523 * @server_port: (out): result server port
1524 * @family: the port family to get
1526 * Fill @server_port with the port pair used by the server. This function can
1527 * only be called when @stream has been joined.
1530 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1531 GstRTSPRange * server_port, GSocketFamily family)
1533 GstRTSPStreamPrivate *priv;
1535 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1536 priv = stream->priv;
1537 g_return_if_fail (priv->is_joined);
1539 g_mutex_lock (&priv->lock);
1540 if (family == G_SOCKET_FAMILY_IPV4) {
1542 *server_port = priv->server_port_v4;
1545 *server_port = priv->server_port_v6;
1547 g_mutex_unlock (&priv->lock);
1551 * gst_rtsp_stream_get_rtpsession:
1552 * @stream: a #GstRTSPStream
1554 * Get the RTP session of this stream.
1556 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1559 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1561 GstRTSPStreamPrivate *priv;
1564 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1566 priv = stream->priv;
1568 g_mutex_lock (&priv->lock);
1569 if ((session = priv->session))
1570 g_object_ref (session);
1571 g_mutex_unlock (&priv->lock);
1577 * gst_rtsp_stream_get_ssrc:
1578 * @stream: a #GstRTSPStream
1579 * @ssrc: (out): result ssrc
1581 * Get the SSRC used by the RTP session of this stream. This function can only
1582 * be called when @stream has been joined.
1585 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1587 GstRTSPStreamPrivate *priv;
1589 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1590 priv = stream->priv;
1591 g_return_if_fail (priv->is_joined);
1593 g_mutex_lock (&priv->lock);
1594 if (ssrc && priv->session)
1595 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1596 g_mutex_unlock (&priv->lock);
1600 * gst_rtsp_stream_set_retransmission_time:
1601 * @stream: a #GstRTSPStream
1602 * @time: a #GstClockTime
1604 * Set the amount of time to store retransmission packets.
1607 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1610 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1612 g_mutex_lock (&stream->priv->lock);
1613 stream->priv->rtx_time = time;
1614 if (stream->priv->rtxsend)
1615 g_object_set (stream->priv->rtxsend, "max-size-time",
1616 GST_TIME_AS_MSECONDS (time), NULL);
1617 g_mutex_unlock (&stream->priv->lock);
1621 * gst_rtsp_stream_get_retransmission_time:
1622 * @stream: a #GstRTSPStream
1624 * Get the amount of time to store retransmission data.
1626 * Returns: the amount of time to store retransmission data.
1629 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1633 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1635 g_mutex_lock (&stream->priv->lock);
1636 ret = stream->priv->rtx_time;
1637 g_mutex_unlock (&stream->priv->lock);
1643 * gst_rtsp_stream_set_retransmission_pt:
1644 * @stream: a #GstRTSPStream
1647 * Set the payload type (pt) for retransmission of this stream.
1650 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1652 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1654 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1656 g_mutex_lock (&stream->priv->lock);
1657 stream->priv->rtx_pt = rtx_pt;
1658 if (stream->priv->rtxsend) {
1659 guint pt = gst_rtsp_stream_get_pt (stream);
1660 gchar *pt_s = g_strdup_printf ("%d", pt);
1661 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1662 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1663 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1665 gst_structure_free (rtx_pt_map);
1667 g_mutex_unlock (&stream->priv->lock);
1671 * gst_rtsp_stream_get_retransmission_pt:
1672 * @stream: a #GstRTSPStream
1674 * Get the payload-type used for retransmission of this stream
1676 * Returns: The retransmission PT.
1679 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1683 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1685 g_mutex_lock (&stream->priv->lock);
1686 rtx_pt = stream->priv->rtx_pt;
1687 g_mutex_unlock (&stream->priv->lock);
1693 * gst_rtsp_stream_set_buffer_size:
1694 * @stream: a #GstRTSPStream
1695 * @size: the buffer size
1697 * Set the size of the UDP transmission buffer (in bytes)
1698 * Needs to be set before the stream is joined to a bin.
1703 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1705 g_mutex_lock (&stream->priv->lock);
1706 stream->priv->buffer_size = size;
1707 g_mutex_unlock (&stream->priv->lock);
1711 * gst_rtsp_stream_get_buffer_size:
1712 * @stream: a #GstRTSPStream
1714 * Get the size of the UDP transmission buffer (in bytes)
1716 * Returns: the size of the UDP TX buffer
1721 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1725 g_mutex_lock (&stream->priv->lock);
1726 buffer_size = stream->priv->buffer_size;
1727 g_mutex_unlock (&stream->priv->lock);
1732 /* executed from streaming thread */
1734 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1736 GstRTSPStreamPrivate *priv = stream->priv;
1737 GstCaps *newcaps, *oldcaps;
1739 newcaps = gst_pad_get_current_caps (pad);
1741 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1744 g_mutex_lock (&priv->lock);
1745 oldcaps = priv->caps;
1746 priv->caps = newcaps;
1747 g_mutex_unlock (&priv->lock);
1750 gst_caps_unref (oldcaps);
1754 dump_structure (const GstStructure * s)
1758 sstr = gst_structure_to_string (s);
1759 GST_INFO ("structure: %s", sstr);
1763 static GstRTSPStreamTransport *
1764 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1766 GstRTSPStreamPrivate *priv = stream->priv;
1768 GstRTSPStreamTransport *result = NULL;
1773 if (rtcp_from == NULL)
1776 tmp = g_strrstr (rtcp_from, ":");
1780 port = atoi (tmp + 1);
1781 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1783 g_mutex_lock (&priv->lock);
1784 GST_INFO ("finding %s:%d in %d transports", dest, port,
1785 g_list_length (priv->transports));
1787 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1788 GstRTSPStreamTransport *trans = walk->data;
1789 const GstRTSPTransport *tr;
1792 tr = gst_rtsp_stream_transport_get_transport (trans);
1794 if (priv->client_side) {
1795 /* In client side mode the 'destination' is the RTSP server, so send
1797 min = tr->server_port.min;
1798 max = tr->server_port.max;
1800 min = tr->client_port.min;
1801 max = tr->client_port.max;
1804 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1810 g_object_ref (result);
1811 g_mutex_unlock (&priv->lock);
1818 static GstRTSPStreamTransport *
1819 check_transport (GObject * source, GstRTSPStream * stream)
1821 GstStructure *stats;
1822 GstRTSPStreamTransport *trans;
1824 /* see if we have a stream to match with the origin of the RTCP packet */
1825 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1826 if (trans == NULL) {
1827 g_object_get (source, "stats", &stats, NULL);
1829 const gchar *rtcp_from;
1831 dump_structure (stats);
1833 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1834 if ((trans = find_transport (stream, rtcp_from))) {
1835 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1837 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1840 gst_structure_free (stats);
1848 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1850 GstRTSPStreamTransport *trans;
1852 GST_INFO ("%p: new source %p", stream, source);
1854 trans = check_transport (source, stream);
1857 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1861 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1863 GST_INFO ("%p: new SDES %p", stream, source);
1867 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1869 GstRTSPStreamTransport *trans;
1871 trans = check_transport (source, stream);
1874 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1875 gst_rtsp_stream_transport_keep_alive (trans);
1879 GstStructure *stats;
1880 g_object_get (source, "stats", &stats, NULL);
1882 dump_structure (stats);
1883 gst_structure_free (stats);
1890 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1892 GST_INFO ("%p: source %p bye", stream, source);
1896 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1898 GstRTSPStreamTransport *trans;
1900 GST_INFO ("%p: source %p bye timeout", stream, source);
1902 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1903 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1904 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1909 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1911 GstRTSPStreamTransport *trans;
1913 GST_INFO ("%p: source %p timeout", stream, source);
1915 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1916 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1917 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1922 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1924 GST_INFO ("%p: new sender source %p", stream, source);
1927 GstStructure *stats;
1928 g_object_get (source, "stats", &stats, NULL);
1930 dump_structure (stats);
1931 gst_structure_free (stats);
1938 on_sender_ssrc_active (GObject * session, GObject * source,
1939 GstRTSPStream * stream)
1943 GstStructure *stats;
1944 g_object_get (source, "stats", &stats, NULL);
1946 dump_structure (stats);
1947 gst_structure_free (stats);
1954 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1957 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1958 g_list_free (priv->tr_cache_rtp);
1959 priv->tr_cache_rtp = NULL;
1961 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1962 g_list_free (priv->tr_cache_rtcp);
1963 priv->tr_cache_rtcp = NULL;
1967 static GstFlowReturn
1968 handle_new_sample (GstAppSink * sink, gpointer user_data)
1970 GstRTSPStreamPrivate *priv;
1974 GstRTSPStream *stream;
1977 sample = gst_app_sink_pull_sample (sink);
1981 stream = (GstRTSPStream *) user_data;
1982 priv = stream->priv;
1983 buffer = gst_sample_get_buffer (sample);
1985 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
1987 g_mutex_lock (&priv->lock);
1989 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
1990 clear_tr_cache (priv, is_rtp);
1991 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1992 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1993 priv->tr_cache_rtp =
1994 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
1996 priv->tr_cache_cookie_rtp = priv->transports_cookie;
1999 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
2000 clear_tr_cache (priv, is_rtp);
2001 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2002 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2003 priv->tr_cache_rtcp =
2004 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
2006 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
2009 g_mutex_unlock (&priv->lock);
2012 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2013 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2014 gst_rtsp_stream_transport_send_rtp (tr, buffer);
2017 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2018 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2019 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
2022 gst_sample_unref (sample);
2027 static GstAppSinkCallbacks sink_cb = {
2028 NULL, /* not interested in EOS */
2029 NULL, /* not interested in preroll samples */
2034 get_rtp_encoder (GstRTSPStream * stream, guint session)
2036 GstRTSPStreamPrivate *priv = stream->priv;
2038 if (priv->srtpenc == NULL) {
2041 name = g_strdup_printf ("srtpenc_%u", session);
2042 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2045 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2047 return gst_object_ref (priv->srtpenc);
2051 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2053 GstRTSPStreamPrivate *priv = stream->priv;
2054 GstElement *oldenc, *enc;
2058 if (priv->idx != session)
2061 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2063 oldenc = priv->srtpenc;
2064 enc = get_rtp_encoder (stream, session);
2065 name = g_strdup_printf ("rtp_sink_%d", session);
2066 pad = gst_element_get_request_pad (enc, name);
2068 gst_object_unref (pad);
2071 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2078 request_rtcp_encoder (GstElement * rtpbin, guint session,
2079 GstRTSPStream * stream)
2081 GstRTSPStreamPrivate *priv = stream->priv;
2082 GstElement *oldenc, *enc;
2086 if (priv->idx != session)
2089 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2091 oldenc = priv->srtpenc;
2092 enc = get_rtp_encoder (stream, session);
2093 name = g_strdup_printf ("rtcp_sink_%d", session);
2094 pad = gst_element_get_request_pad (enc, name);
2096 gst_object_unref (pad);
2099 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2106 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2108 GstRTSPStreamPrivate *priv = stream->priv;
2111 GST_DEBUG ("request key %08x", ssrc);
2113 g_mutex_lock (&priv->lock);
2114 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2115 gst_caps_ref (caps);
2116 g_mutex_unlock (&priv->lock);
2122 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2123 GstRTSPStream * stream)
2125 GstRTSPStreamPrivate *priv = stream->priv;
2127 if (priv->idx != session)
2130 if (priv->srtpdec == NULL) {
2133 name = g_strdup_printf ("srtpdec_%u", session);
2134 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2137 g_signal_connect (priv->srtpdec, "request-key",
2138 (GCallback) request_key, stream);
2140 return gst_object_ref (priv->srtpdec);
2144 * gst_rtsp_stream_request_aux_sender:
2145 * @stream: a #GstRTSPStream
2146 * @sessid: the session id
2148 * Creating a rtxsend bin
2150 * Returns: (transfer full): a #GstElement.
2155 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2159 GstStructure *pt_map;
2164 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2166 pt = gst_rtsp_stream_get_pt (stream);
2167 pt_s = g_strdup_printf ("%u", pt);
2168 rtx_pt = stream->priv->rtx_pt;
2170 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2172 bin = gst_bin_new (NULL);
2173 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2174 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2175 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2176 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2177 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2179 gst_structure_free (pt_map);
2180 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2182 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2183 name = g_strdup_printf ("src_%u", sessid);
2184 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2186 gst_object_unref (pad);
2188 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2189 name = g_strdup_printf ("sink_%u", sessid);
2190 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2192 gst_object_unref (pad);
2198 * gst_rtsp_stream_set_pt_map:
2199 * @stream: a #GstRTSPStream
2203 * Configure a pt map between @pt and @caps.
2206 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2208 GstRTSPStreamPrivate *priv = stream->priv;
2210 g_mutex_lock (&priv->lock);
2211 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2212 g_mutex_unlock (&priv->lock);
2216 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2217 GstRTSPStream * stream)
2219 GstRTSPStreamPrivate *priv = stream->priv;
2220 GstCaps *caps = NULL;
2222 g_mutex_lock (&priv->lock);
2224 if (priv->idx == session) {
2225 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2227 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2228 gst_caps_ref (caps);
2230 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2234 g_mutex_unlock (&priv->lock);
2240 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2242 GstRTSPStreamPrivate *priv = stream->priv;
2244 GstPadLinkReturn ret;
2247 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2248 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2250 name = gst_pad_get_name (pad);
2251 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2257 if (priv->idx != sessid)
2260 if (gst_pad_is_linked (priv->sinkpad)) {
2261 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2262 GST_DEBUG_PAD_NAME (priv->sinkpad));
2266 /* link the RTP pad to the session manager, it should not really fail unless
2267 * this is not really an RTP pad */
2268 ret = gst_pad_link (pad, priv->sinkpad);
2269 if (ret != GST_PAD_LINK_OK)
2271 priv->recv_rtp_src = gst_object_ref (pad);
2278 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2279 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2284 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2285 GstRTSPStream * stream)
2287 /* TODO: What to do here other than this? */
2288 GST_DEBUG ("Stream %p: Got EOS", stream);
2289 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2292 /* must be called with lock */
2294 create_sender_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2296 GstRTSPStreamPrivate *priv;
2297 GstPad *pad, *sinkpad = NULL;
2298 gboolean is_tcp = FALSE, is_udp = FALSE;
2301 priv = stream->priv;
2303 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2304 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2305 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2307 if (is_udp && !create_and_configure_udpsinks (stream))
2308 goto no_udp_protocol;
2310 for (i = 0; i < 2; i++) {
2311 GstPad *teepad, *queuepad;
2312 /* For the sender we create this bit of pipeline for both
2313 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2314 * we need to add a queue before appsink and udpsink to make
2315 * the pipeline not block. For the TCP case, we want to pump
2316 * client as fast as possible anyway. This pipeline is used
2317 * when both TCP and UDP are present.
2319 * .--------. .-----. .---------. .---------.
2320 * | rtpbin | | tee | | queue | | udpsink |
2321 * | send->sink src->sink src->sink |
2322 * '--------' | | '---------' '---------'
2323 * | | .---------. .---------.
2324 * | | | queue | | appsink |
2325 * | src->sink src->sink |
2326 * '-----' '---------' '---------'
2328 * When only UDP or only TCP is allowed, we skip the tee and queue
2329 * and link the udpsink (for UDP) or appsink (for TCP) directly to
2332 /* Only link the RTP send src if we're going to send RTP, link
2333 * the RTCP send src always */
2334 if (priv->srcpad || i == 1) {
2337 gst_bin_add (bin, priv->udpsink[i]);
2338 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
2343 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2344 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2345 gst_bin_add (bin, priv->appsink[i]);
2346 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2347 &sink_cb, stream, NULL);
2350 if (is_udp && is_tcp) {
2351 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2353 /* make tee for RTP/RTCP */
2354 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2355 gst_bin_add (bin, priv->tee[i]);
2357 /* and link to rtpbin send pad */
2358 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2359 gst_pad_link (priv->send_src[i], pad);
2360 gst_object_unref (pad);
2362 priv->udpqueue[i] = gst_element_factory_make ("queue", NULL);
2363 g_object_set (priv->udpqueue[i], "max-size-buffers",
2364 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0),
2366 gst_bin_add (bin, priv->udpqueue[i]);
2367 /* link tee to udpqueue */
2368 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2369 pad = gst_element_get_static_pad (priv->udpqueue[i], "sink");
2370 gst_pad_link (teepad, pad);
2371 gst_object_unref (pad);
2372 gst_object_unref (teepad);
2374 /* link udpqueue to udpsink */
2375 queuepad = gst_element_get_static_pad (priv->udpqueue[i], "src");
2376 gst_pad_link (queuepad, sinkpad);
2377 gst_object_unref (queuepad);
2378 gst_object_unref (sinkpad);
2381 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
2382 g_object_set (priv->appqueue[i], "max-size-buffers",
2383 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0),
2385 gst_bin_add (bin, priv->appqueue[i]);
2386 /* and link tee to appqueue */
2387 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2388 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
2389 gst_pad_link (teepad, pad);
2390 gst_object_unref (pad);
2391 gst_object_unref (teepad);
2393 /* and link appqueue to appsink */
2394 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
2395 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2396 gst_pad_link (queuepad, pad);
2397 gst_object_unref (pad);
2398 gst_object_unref (queuepad);
2399 } else if (is_tcp) {
2400 /* only appsink needed, link it to the session */
2401 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2402 gst_pad_link (priv->send_src[i], pad);
2403 gst_object_unref (pad);
2405 /* when its only TCP, we need to set sync and preroll to FALSE
2406 * for the sink to avoid deadlock. And this is only needed for
2407 * sink used for RTCP data, not the RTP data. */
2409 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2411 /* else only udpsink needed, link it to the session */
2412 gst_pad_link (priv->send_src[i], sinkpad);
2413 gst_object_unref (sinkpad);
2417 /* check if we need to set to a special state */
2418 if (state != GST_STATE_NULL) {
2419 if (priv->udpsink[i] && (priv->srcpad || i == 1))
2420 gst_element_set_state (priv->udpsink[i], state);
2421 if (priv->appsink[i] && (priv->srcpad || i == 1))
2422 gst_element_set_state (priv->appsink[i], state);
2423 if (priv->appqueue[i] && (priv->srcpad || i == 1))
2424 gst_element_set_state (priv->appqueue[i], state);
2425 if (priv->udpqueue[i] && (priv->srcpad || i == 1))
2426 gst_element_set_state (priv->udpqueue[i], state);
2427 if (priv->tee[i] && (priv->srcpad || i == 1))
2428 gst_element_set_state (priv->tee[i], state);
2441 /* must be called with lock */
2443 create_receiver_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2445 GstRTSPStreamPrivate *priv;
2446 GstPad *pad, *selpad;
2450 priv = stream->priv;
2452 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2454 for (i = 0; i < 2; i++) {
2455 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2456 * RTCP sink always */
2457 if (priv->sinkpad || i == 1) {
2458 /* For the receiver we create this bit of pipeline for both
2459 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2460 * and it is all funneled into the rtpbin receive pad.
2462 * .--------. .--------. .--------.
2463 * | udpsrc | | funnel | | rtpbin |
2464 * | src->sink src->sink |
2465 * '--------' | | '--------'
2469 * '--------' '--------'
2471 /* make funnel for the RTP/RTCP receivers */
2472 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2473 gst_bin_add (bin, priv->funnel[i]);
2475 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2476 gst_pad_link (pad, priv->recv_sink[i]);
2477 gst_object_unref (pad);
2479 if (priv->udpsrc_v4[i]) {
2481 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2482 * values. This is only relevant for PLAY pipelines */
2483 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
2484 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
2487 gst_bin_add (bin, priv->udpsrc_v4[i]);
2489 /* and link to the funnel v4 */
2490 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2491 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
2492 gst_pad_link (pad, selpad);
2493 gst_object_unref (pad);
2494 gst_object_unref (selpad);
2497 if (priv->udpsrc_v6[i]) {
2499 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
2500 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
2502 gst_bin_add (bin, priv->udpsrc_v6[i]);
2504 /* and link to the funnel v6 */
2505 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2506 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2507 gst_pad_link (pad, selpad);
2508 gst_object_unref (pad);
2509 gst_object_unref (selpad);
2513 /* make and add appsrc */
2514 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2515 priv->appsrc_base_time[i] = -1;
2517 gst_element_set_state (priv->appsrc[i], GST_STATE_PLAYING);
2518 gst_element_set_locked_state (priv->appsrc[i], TRUE);
2520 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
2522 gst_bin_add (bin, priv->appsrc[i]);
2523 /* and link to the funnel */
2524 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2525 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2526 gst_pad_link (pad, selpad);
2527 gst_object_unref (pad);
2528 gst_object_unref (selpad);
2532 /* check if we need to set to a special state */
2533 if (state != GST_STATE_NULL) {
2534 if (priv->funnel[i] && (priv->sinkpad || i == 1))
2535 gst_element_set_state (priv->funnel[i], state);
2541 * gst_rtsp_stream_join_bin:
2542 * @stream: a #GstRTSPStream
2543 * @bin: (transfer none): a #GstBin to join
2544 * @rtpbin: (transfer none): a rtpbin element in @bin
2545 * @state: the target state of the new elements
2547 * Join the #GstBin @bin that contains the element @rtpbin.
2549 * @stream will link to @rtpbin, which must be inside @bin. The elements
2550 * added to @bin will be set to the state given in @state.
2552 * Returns: %TRUE on success.
2555 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2556 GstElement * rtpbin, GstState state)
2558 GstRTSPStreamPrivate *priv;
2561 GstPadLinkReturn ret;
2563 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2564 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2565 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2567 priv = stream->priv;
2569 g_mutex_lock (&priv->lock);
2570 if (priv->is_joined)
2573 /* create a session with the same index as the stream */
2576 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2578 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2579 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2581 g_signal_connect (rtpbin, "request-rtp-encoder",
2582 (GCallback) request_rtp_encoder, stream);
2583 g_signal_connect (rtpbin, "request-rtcp-encoder",
2584 (GCallback) request_rtcp_encoder, stream);
2585 g_signal_connect (rtpbin, "request-rtp-decoder",
2586 (GCallback) request_rtp_rtcp_decoder, stream);
2587 g_signal_connect (rtpbin, "request-rtcp-decoder",
2588 (GCallback) request_rtp_rtcp_decoder, stream);
2591 if (priv->sinkpad) {
2592 g_signal_connect (rtpbin, "request-pt-map",
2593 (GCallback) request_pt_map, stream);
2596 /* get pads from the RTP session element for sending and receiving
2599 /* get a pad for sending RTP */
2600 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2601 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2604 /* link the RTP pad to the session manager, it should not really fail unless
2605 * this is not really an RTP pad */
2606 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2607 if (ret != GST_PAD_LINK_OK)
2610 name = g_strdup_printf ("send_rtp_src_%u", idx);
2611 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2614 /* Need to connect our sinkpad from here */
2615 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2617 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2619 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2620 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2624 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2625 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2627 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2628 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2631 /* get the session */
2632 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2634 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2636 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2638 g_signal_connect (priv->session, "on-ssrc-active",
2639 (GCallback) on_ssrc_active, stream);
2640 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2642 g_signal_connect (priv->session, "on-bye-timeout",
2643 (GCallback) on_bye_timeout, stream);
2644 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2647 /* signal for sender ssrc */
2648 g_signal_connect (priv->session, "on-new-sender-ssrc",
2649 (GCallback) on_new_sender_ssrc, stream);
2650 g_signal_connect (priv->session, "on-sender-ssrc-active",
2651 (GCallback) on_sender_ssrc_active, stream);
2653 if (!create_sender_part (stream, bin, state))
2654 goto no_udp_protocol;
2656 create_receiver_part (stream, bin, state);
2659 /* be notified of caps changes */
2660 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2661 (GCallback) caps_notify, stream);
2664 priv->is_joined = TRUE;
2665 g_mutex_unlock (&priv->lock);
2672 g_mutex_unlock (&priv->lock);
2677 GST_WARNING ("failed to link stream %u", idx);
2678 gst_object_unref (priv->send_rtp_sink);
2679 priv->send_rtp_sink = NULL;
2680 g_mutex_unlock (&priv->lock);
2685 GST_WARNING ("failed to allocate ports %u", idx);
2686 gst_object_unref (priv->send_rtp_sink);
2687 priv->send_rtp_sink = NULL;
2688 gst_object_unref (priv->send_src[0]);
2689 priv->send_src[0] = NULL;
2690 gst_object_unref (priv->send_src[1]);
2691 priv->send_src[1] = NULL;
2692 gst_object_unref (priv->recv_sink[0]);
2693 priv->recv_sink[0] = NULL;
2694 gst_object_unref (priv->recv_sink[1]);
2695 priv->recv_sink[1] = NULL;
2696 if (priv->udpsink[0])
2697 gst_element_set_state (priv->udpsink[0], GST_STATE_NULL);
2698 if (priv->udpsink[1])
2699 gst_element_set_state (priv->udpsink[1], GST_STATE_NULL);
2700 if (priv->udpsrc_v4[0]) {
2701 gst_element_set_state (priv->udpsrc_v4[0], GST_STATE_NULL);
2702 gst_object_unref (priv->udpsrc_v4[0]);
2703 priv->udpsrc_v4[0] = NULL;
2705 if (priv->udpsrc_v4[1]) {
2706 gst_element_set_state (priv->udpsrc_v4[1], GST_STATE_NULL);
2707 gst_object_unref (priv->udpsrc_v4[1]);
2708 priv->udpsrc_v4[1] = NULL;
2710 if (priv->udpsrc_mcast_v4[0]) {
2711 gst_element_set_state (priv->udpsrc_mcast_v4[0], GST_STATE_NULL);
2712 gst_object_unref (priv->udpsrc_mcast_v4[0]);
2713 priv->udpsrc_mcast_v4[0] = NULL;
2715 if (priv->udpsrc_mcast_v4[1]) {
2716 gst_element_set_state (priv->udpsrc_mcast_v4[1], GST_STATE_NULL);
2717 gst_object_unref (priv->udpsrc_mcast_v4[1]);
2718 priv->udpsrc_mcast_v4[1] = NULL;
2720 if (priv->udpsrc_v6[0]) {
2721 gst_element_set_state (priv->udpsrc_v6[0], GST_STATE_NULL);
2722 gst_object_unref (priv->udpsrc_v6[0]);
2723 priv->udpsrc_v6[0] = NULL;
2725 if (priv->udpsrc_v6[1]) {
2726 gst_element_set_state (priv->udpsrc_v6[1], GST_STATE_NULL);
2727 gst_object_unref (priv->udpsrc_v6[1]);
2728 priv->udpsrc_v6[1] = NULL;
2730 if (priv->udpsrc_mcast_v6[0]) {
2731 gst_element_set_state (priv->udpsrc_mcast_v6[0], GST_STATE_NULL);
2732 gst_object_unref (priv->udpsrc_mcast_v6[0]);
2733 priv->udpsrc_mcast_v6[0] = NULL;
2735 if (priv->udpsrc_mcast_v6[1]) {
2736 gst_element_set_state (priv->udpsrc_mcast_v6[1], GST_STATE_NULL);
2737 gst_object_unref (priv->udpsrc_mcast_v6[1]);
2738 priv->udpsrc_mcast_v6[1] = NULL;
2740 g_mutex_unlock (&priv->lock);
2746 * gst_rtsp_stream_leave_bin:
2747 * @stream: a #GstRTSPStream
2748 * @bin: (transfer none): a #GstBin
2749 * @rtpbin: (transfer none): a rtpbin #GstElement
2751 * Remove the elements of @stream from @bin.
2753 * Return: %TRUE on success.
2756 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2757 GstElement * rtpbin)
2759 GstRTSPStreamPrivate *priv;
2761 gboolean is_tcp, is_udp;
2763 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2764 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2765 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2767 priv = stream->priv;
2769 g_mutex_lock (&priv->lock);
2770 if (!priv->is_joined)
2771 goto was_not_joined;
2773 /* all transports must be removed by now */
2774 if (priv->transports != NULL)
2775 goto transports_not_removed;
2777 clear_tr_cache (priv, TRUE);
2778 clear_tr_cache (priv, FALSE);
2780 GST_INFO ("stream %p leaving bin", stream);
2783 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2785 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2786 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2787 gst_object_unref (priv->send_rtp_sink);
2788 priv->send_rtp_sink = NULL;
2789 } else if (priv->recv_rtp_src) {
2790 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2791 gst_object_unref (priv->recv_rtp_src);
2792 priv->recv_rtp_src = NULL;
2795 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2797 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2798 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2801 for (i = 0; i < 2; i++) {
2802 if (priv->udpsink[i])
2803 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
2804 if (priv->appsink[i])
2805 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
2806 if (priv->appqueue[i])
2807 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
2808 if (priv->udpqueue[i])
2809 gst_element_set_state (priv->udpqueue[i], GST_STATE_NULL);
2811 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
2812 if (priv->funnel[i])
2813 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
2814 if (priv->appsrc[i])
2815 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2817 if (priv->udpsrc_v4[i]) {
2818 if (priv->sinkpad || i == 1) {
2819 /* and set udpsrc to NULL now before removing */
2820 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
2821 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2822 /* removing them should also nicely release the request
2823 * pads when they finalize */
2824 gst_bin_remove (bin, priv->udpsrc_v4[i]);
2826 /* we need to set the state to NULL before unref */
2827 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2828 gst_object_unref (priv->udpsrc_v4[i]);
2832 if (priv->udpsrc_mcast_v4[i]) {
2833 if (priv->sinkpad || i == 1) {
2834 /* and set udpsrc to NULL now before removing */
2835 gst_element_set_locked_state (priv->udpsrc_mcast_v4[i], FALSE);
2836 gst_element_set_state (priv->udpsrc_mcast_v4[i], GST_STATE_NULL);
2837 /* removing them should also nicely release the request
2838 * pads when they finalize */
2839 gst_bin_remove (bin, priv->udpsrc_mcast_v4[i]);
2841 gst_element_set_state (priv->udpsrc_mcast_v4[i], GST_STATE_NULL);
2842 gst_object_unref (priv->udpsrc_mcast_v4[i]);
2846 if (priv->udpsrc_v6[i]) {
2847 if (priv->sinkpad || i == 1) {
2848 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
2849 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2850 gst_bin_remove (bin, priv->udpsrc_v6[i]);
2852 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2853 gst_object_unref (priv->udpsrc_v6[i]);
2856 if (priv->udpsrc_mcast_v6[i]) {
2857 if (priv->sinkpad || i == 1) {
2858 gst_element_set_locked_state (priv->udpsrc_mcast_v6[i], FALSE);
2859 gst_element_set_state (priv->udpsrc_mcast_v6[i], GST_STATE_NULL);
2860 gst_bin_remove (bin, priv->udpsrc_mcast_v6[i]);
2862 gst_element_set_state (priv->udpsrc_mcast_v6[i], GST_STATE_NULL);
2863 gst_object_unref (priv->udpsrc_mcast_v6[i]);
2867 if (priv->udpsink[i] && is_udp && (priv->srcpad || i == 1))
2868 gst_bin_remove (bin, priv->udpsink[i]);
2869 if (priv->appsrc[i]) {
2870 if (priv->sinkpad || i == 1) {
2871 gst_element_set_locked_state (priv->appsrc[i], FALSE);
2872 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2873 gst_bin_remove (bin, priv->appsrc[i]);
2875 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2876 gst_object_unref (priv->appsrc[i]);
2879 if (priv->appsink[i] && is_tcp && (priv->srcpad || i == 1))
2880 gst_bin_remove (bin, priv->appsink[i]);
2881 if (priv->appqueue[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2882 gst_bin_remove (bin, priv->appqueue[i]);
2883 if (priv->udpqueue[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2884 gst_bin_remove (bin, priv->udpqueue[i]);
2885 if (priv->tee[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2886 gst_bin_remove (bin, priv->tee[i]);
2887 if (priv->funnel[i] && (priv->sinkpad || i == 1))
2888 gst_bin_remove (bin, priv->funnel[i]);
2890 if (priv->sinkpad || i == 1) {
2891 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2892 gst_object_unref (priv->recv_sink[i]);
2893 priv->recv_sink[i] = NULL;
2896 priv->udpsrc_v4[i] = NULL;
2897 priv->udpsrc_v6[i] = NULL;
2898 priv->udpsrc_mcast_v4[i] = NULL;
2899 priv->udpsrc_mcast_v6[i] = NULL;
2900 priv->udpsink[i] = NULL;
2901 priv->appsrc[i] = NULL;
2902 priv->appsink[i] = NULL;
2903 priv->appqueue[i] = NULL;
2904 priv->udpqueue[i] = NULL;
2905 priv->tee[i] = NULL;
2906 priv->funnel[i] = NULL;
2910 gst_object_unref (priv->send_src[0]);
2911 priv->send_src[0] = NULL;
2914 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2915 gst_object_unref (priv->send_src[1]);
2916 priv->send_src[1] = NULL;
2918 g_object_unref (priv->session);
2919 priv->session = NULL;
2921 gst_caps_unref (priv->caps);
2925 gst_object_unref (priv->srtpenc);
2927 gst_object_unref (priv->srtpdec);
2929 priv->is_joined = FALSE;
2930 g_mutex_unlock (&priv->lock);
2936 g_mutex_unlock (&priv->lock);
2939 transports_not_removed:
2941 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2942 g_mutex_unlock (&priv->lock);
2948 * gst_rtsp_stream_get_rtpinfo:
2949 * @stream: a #GstRTSPStream
2950 * @rtptime: (allow-none): result RTP timestamp
2951 * @seq: (allow-none): result RTP seqnum
2952 * @clock_rate: (allow-none): the clock rate
2953 * @running_time: (allow-none): result running-time
2955 * Retrieve the current rtptime, seq and running-time. This is used to
2956 * construct a RTPInfo reply header.
2958 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2961 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2962 guint * rtptime, guint * seq, guint * clock_rate,
2963 GstClockTime * running_time)
2965 GstRTSPStreamPrivate *priv;
2966 GstStructure *stats;
2967 GObjectClass *payobjclass;
2969 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2971 priv = stream->priv;
2973 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2975 g_mutex_lock (&priv->lock);
2977 /* First try to extract the information from the last buffer on the sinks.
2978 * This will have a more accurate sequence number and timestamp, as between
2979 * the payloader and the sink there can be some queues
2981 if (priv->udpsink[0] || priv->appsink[0]) {
2982 GstSample *last_sample;
2984 if (priv->udpsink[0])
2985 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
2987 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
2992 GstSegment *segment;
2993 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
2995 caps = gst_sample_get_caps (last_sample);
2996 buffer = gst_sample_get_buffer (last_sample);
2997 segment = gst_sample_get_segment (last_sample);
2999 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3001 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3005 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3008 gst_rtp_buffer_unmap (&rtp_buffer);
3012 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3013 GST_BUFFER_TIMESTAMP (buffer));
3017 GstStructure *s = gst_caps_get_structure (caps, 0);
3019 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3021 if (*clock_rate == 0 && running_time)
3022 *running_time = GST_CLOCK_TIME_NONE;
3024 gst_sample_unref (last_sample);
3028 gst_sample_unref (last_sample);
3033 if (g_object_class_find_property (payobjclass, "stats")) {
3034 g_object_get (priv->payloader, "stats", &stats, NULL);
3039 gst_structure_get_uint (stats, "seqnum", seq);
3042 gst_structure_get_uint (stats, "timestamp", rtptime);
3045 gst_structure_get_clock_time (stats, "running-time", running_time);
3048 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3049 if (*clock_rate == 0 && running_time)
3050 *running_time = GST_CLOCK_TIME_NONE;
3052 gst_structure_free (stats);
3054 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3055 !g_object_class_find_property (payobjclass, "timestamp"))
3059 g_object_get (priv->payloader, "seqnum", seq, NULL);
3062 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3065 *running_time = GST_CLOCK_TIME_NONE;
3069 g_mutex_unlock (&priv->lock);
3076 GST_WARNING ("Could not get payloader stats");
3077 g_mutex_unlock (&priv->lock);
3083 * gst_rtsp_stream_get_caps:
3084 * @stream: a #GstRTSPStream
3086 * Retrieve the current caps of @stream.
3088 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
3092 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3094 GstRTSPStreamPrivate *priv;
3097 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3099 priv = stream->priv;
3101 g_mutex_lock (&priv->lock);
3102 if ((result = priv->caps))
3103 gst_caps_ref (result);
3104 g_mutex_unlock (&priv->lock);
3110 * gst_rtsp_stream_recv_rtp:
3111 * @stream: a #GstRTSPStream
3112 * @buffer: (transfer full): a #GstBuffer
3114 * Handle an RTP buffer for the stream. This method is usually called when a
3115 * message has been received from a client using the TCP transport.
3117 * This function takes ownership of @buffer.
3119 * Returns: a GstFlowReturn.
3122 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3124 GstRTSPStreamPrivate *priv;
3126 GstElement *element;
3128 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3129 priv = stream->priv;
3130 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3131 g_return_val_if_fail (priv->is_joined, FALSE);
3133 g_mutex_lock (&priv->lock);
3134 if (priv->appsrc[0])
3135 element = gst_object_ref (priv->appsrc[0]);
3138 g_mutex_unlock (&priv->lock);
3141 if (priv->appsrc_base_time[0] == -1) {
3142 /* Take current running_time. This timestamp will be put on
3143 * the first buffer of each stream because we are a live source and so we
3144 * timestamp with the running_time. When we are dealing with TCP, we also
3145 * only timestamp the first buffer (using the DISCONT flag) because a server
3146 * typically bursts data, for which we don't want to compensate by speeding
3147 * up the media. The other timestamps will be interpollated from this one
3148 * using the RTP timestamps. */
3149 GST_OBJECT_LOCK (element);
3150 if (GST_ELEMENT_CLOCK (element)) {
3152 GstClockTime base_time;
3154 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3155 base_time = GST_ELEMENT_CAST (element)->base_time;
3157 priv->appsrc_base_time[0] = now - base_time;
3158 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3159 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3160 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3161 GST_TIME_ARGS (base_time));
3163 GST_OBJECT_UNLOCK (element);
3166 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3167 gst_object_unref (element);
3175 * gst_rtsp_stream_recv_rtcp:
3176 * @stream: a #GstRTSPStream
3177 * @buffer: (transfer full): a #GstBuffer
3179 * Handle an RTCP buffer for the stream. This method is usually called when a
3180 * message has been received from a client using the TCP transport.
3182 * This function takes ownership of @buffer.
3184 * Returns: a GstFlowReturn.
3187 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3189 GstRTSPStreamPrivate *priv;
3191 GstElement *element;
3193 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3194 priv = stream->priv;
3195 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3197 if (!priv->is_joined) {
3198 gst_buffer_unref (buffer);
3199 return GST_FLOW_NOT_LINKED;
3201 g_mutex_lock (&priv->lock);
3202 if (priv->appsrc[1])
3203 element = gst_object_ref (priv->appsrc[1]);
3206 g_mutex_unlock (&priv->lock);
3209 if (priv->appsrc_base_time[1] == -1) {
3210 /* Take current running_time. This timestamp will be put on
3211 * the first buffer of each stream because we are a live source and so we
3212 * timestamp with the running_time. When we are dealing with TCP, we also
3213 * only timestamp the first buffer (using the DISCONT flag) because a server
3214 * typically bursts data, for which we don't want to compensate by speeding
3215 * up the media. The other timestamps will be interpollated from this one
3216 * using the RTP timestamps. */
3217 GST_OBJECT_LOCK (element);
3218 if (GST_ELEMENT_CLOCK (element)) {
3220 GstClockTime base_time;
3222 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3223 base_time = GST_ELEMENT_CAST (element)->base_time;
3225 priv->appsrc_base_time[1] = now - base_time;
3226 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3227 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3228 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3229 GST_TIME_ARGS (base_time));
3231 GST_OBJECT_UNLOCK (element);
3234 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3235 gst_object_unref (element);
3238 gst_buffer_unref (buffer);
3243 /* must be called with lock */
3245 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3248 GstRTSPStreamPrivate *priv = stream->priv;
3249 const GstRTSPTransport *tr;
3251 tr = gst_rtsp_stream_transport_get_transport (trans);
3253 switch (tr->lower_transport) {
3254 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3255 case GST_RTSP_LOWER_TRANS_UDP:
3261 dest = tr->destination;
3262 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3266 } else if (priv->client_side) {
3267 /* In client side mode the 'destination' is the RTSP server, so send
3269 min = tr->server_port.min;
3270 max = tr->server_port.max;
3272 min = tr->client_port.min;
3273 max = tr->client_port.max;
3278 GST_INFO ("setting ttl-mc %d", ttl);
3279 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3280 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3282 GST_INFO ("adding %s:%d-%d", dest, min, max);
3283 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3284 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3285 priv->transports = g_list_prepend (priv->transports, trans);
3287 GST_INFO ("removing %s:%d-%d", dest, min, max);
3288 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3289 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3290 priv->transports = g_list_remove (priv->transports, trans);
3292 priv->transports_cookie++;
3295 case GST_RTSP_LOWER_TRANS_TCP:
3297 GST_INFO ("adding TCP %s", tr->destination);
3298 priv->transports = g_list_prepend (priv->transports, trans);
3300 GST_INFO ("removing TCP %s", tr->destination);
3301 priv->transports = g_list_remove (priv->transports, trans);
3303 priv->transports_cookie++;
3306 goto unknown_transport;
3313 GST_INFO ("Unknown transport %d", tr->lower_transport);
3320 * gst_rtsp_stream_add_transport:
3321 * @stream: a #GstRTSPStream
3322 * @trans: (transfer none): a #GstRTSPStreamTransport
3324 * Add the transport in @trans to @stream. The media of @stream will
3325 * then also be send to the values configured in @trans.
3327 * @stream must be joined to a bin.
3329 * @trans must contain a valid #GstRTSPTransport.
3331 * Returns: %TRUE if @trans was added
3334 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3335 GstRTSPStreamTransport * trans)
3337 GstRTSPStreamPrivate *priv;
3340 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3341 priv = stream->priv;
3342 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3343 g_return_val_if_fail (priv->is_joined, FALSE);
3345 g_mutex_lock (&priv->lock);
3346 res = update_transport (stream, trans, TRUE);
3347 g_mutex_unlock (&priv->lock);
3353 * gst_rtsp_stream_remove_transport:
3354 * @stream: a #GstRTSPStream
3355 * @trans: (transfer none): a #GstRTSPStreamTransport
3357 * Remove the transport in @trans from @stream. The media of @stream will
3358 * not be sent to the values configured in @trans.
3360 * @stream must be joined to a bin.
3362 * @trans must contain a valid #GstRTSPTransport.
3364 * Returns: %TRUE if @trans was removed
3367 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3368 GstRTSPStreamTransport * trans)
3370 GstRTSPStreamPrivate *priv;
3373 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3374 priv = stream->priv;
3375 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3376 g_return_val_if_fail (priv->is_joined, FALSE);
3378 g_mutex_lock (&priv->lock);
3379 res = update_transport (stream, trans, FALSE);
3380 g_mutex_unlock (&priv->lock);
3386 * gst_rtsp_stream_update_crypto:
3387 * @stream: a #GstRTSPStream
3389 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3391 * Update the new crypto information for @ssrc in @stream. If information
3392 * for @ssrc did not exist, it will be added. If information
3393 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3394 * be removed from @stream.
3396 * Returns: %TRUE if @crypto could be updated
3399 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3400 guint ssrc, GstCaps * crypto)
3402 GstRTSPStreamPrivate *priv;
3404 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3405 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3407 priv = stream->priv;
3409 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3411 g_mutex_lock (&priv->lock);
3413 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3414 gst_caps_ref (crypto));
3416 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3417 g_mutex_unlock (&priv->lock);
3423 * gst_rtsp_stream_get_rtp_socket:
3424 * @stream: a #GstRTSPStream
3425 * @family: the socket family
3427 * Get the RTP socket from @stream for a @family.
3429 * @stream must be joined to a bin.
3431 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3432 * socket could be allocated for @family. Unref after usage
3435 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3437 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3441 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3442 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3443 family == G_SOCKET_FAMILY_IPV6, NULL);
3444 g_return_val_if_fail (priv->udpsink[0], NULL);
3446 if (family == G_SOCKET_FAMILY_IPV6)
3451 g_object_get (priv->udpsink[0], name, &socket, NULL);
3457 * gst_rtsp_stream_get_rtcp_socket:
3458 * @stream: a #GstRTSPStream
3459 * @family: the socket family
3461 * Get the RTCP socket from @stream for a @family.
3463 * @stream must be joined to a bin.
3465 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3466 * socket could be allocated for @family. Unref after usage
3469 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3471 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3475 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3476 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3477 family == G_SOCKET_FAMILY_IPV6, NULL);
3478 g_return_val_if_fail (priv->udpsink[1], NULL);
3480 if (family == G_SOCKET_FAMILY_IPV6)
3485 g_object_get (priv->udpsink[1], name, &socket, NULL);
3491 * gst_rtsp_stream_set_seqnum:
3492 * @stream: a #GstRTSPStream
3493 * @seqnum: a new sequence number
3495 * Configure the sequence number in the payloader of @stream to @seqnum.
3498 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3500 GstRTSPStreamPrivate *priv;
3502 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3504 priv = stream->priv;
3506 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3510 * gst_rtsp_stream_get_seqnum:
3511 * @stream: a #GstRTSPStream
3513 * Get the configured sequence number in the payloader of @stream.
3515 * Returns: the sequence number of the payloader.
3518 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3520 GstRTSPStreamPrivate *priv;
3523 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3525 priv = stream->priv;
3527 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3533 * gst_rtsp_stream_transport_filter:
3534 * @stream: a #GstRTSPStream
3535 * @func: (scope call) (allow-none): a callback
3536 * @user_data: (closure): user data passed to @func
3538 * Call @func for each transport managed by @stream. The result value of @func
3539 * determines what happens to the transport. @func will be called with @stream
3540 * locked so no further actions on @stream can be performed from @func.
3542 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3545 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3547 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3548 * will also be added with an additional ref to the result #GList of this
3551 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3553 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3554 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3555 * element in the #GList should be unreffed before the list is freed.
3558 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3559 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3561 GstRTSPStreamPrivate *priv;
3562 GList *result, *walk, *next;
3563 GHashTable *visited = NULL;
3566 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3568 priv = stream->priv;
3572 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3574 g_mutex_lock (&priv->lock);
3576 cookie = priv->transports_cookie;
3577 for (walk = priv->transports; walk; walk = next) {
3578 GstRTSPStreamTransport *trans = walk->data;
3579 GstRTSPFilterResult res;
3582 next = g_list_next (walk);
3585 /* only visit each transport once */
3586 if (g_hash_table_contains (visited, trans))
3589 g_hash_table_add (visited, g_object_ref (trans));
3590 g_mutex_unlock (&priv->lock);
3592 res = func (stream, trans, user_data);
3594 g_mutex_lock (&priv->lock);
3596 res = GST_RTSP_FILTER_REF;
3598 changed = (cookie != priv->transports_cookie);
3601 case GST_RTSP_FILTER_REMOVE:
3602 update_transport (stream, trans, FALSE);
3604 case GST_RTSP_FILTER_REF:
3605 result = g_list_prepend (result, g_object_ref (trans));
3607 case GST_RTSP_FILTER_KEEP:
3614 g_mutex_unlock (&priv->lock);
3617 g_hash_table_unref (visited);
3622 static GstPadProbeReturn
3623 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3625 GstRTSPStreamPrivate *priv;
3626 GstRTSPStream *stream;
3629 priv = stream->priv;
3631 GST_DEBUG_OBJECT (pad, "now blocking");
3633 g_mutex_lock (&priv->lock);
3634 priv->blocking = TRUE;
3635 g_mutex_unlock (&priv->lock);
3637 gst_element_post_message (priv->payloader,
3638 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3639 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3641 return GST_PAD_PROBE_OK;
3645 * gst_rtsp_stream_set_blocked:
3646 * @stream: a #GstRTSPStream
3647 * @blocked: boolean indicating we should block or unblock
3649 * Blocks or unblocks the dataflow on @stream.
3651 * Returns: %TRUE on success
3654 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3656 GstRTSPStreamPrivate *priv;
3658 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3660 priv = stream->priv;
3662 g_mutex_lock (&priv->lock);
3664 priv->blocking = FALSE;
3665 if (priv->blocked_id == 0) {
3666 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3667 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3668 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3669 g_object_ref (stream), g_object_unref);
3672 if (priv->blocked_id != 0) {
3673 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3674 priv->blocked_id = 0;
3675 priv->blocking = FALSE;
3678 g_mutex_unlock (&priv->lock);
3684 * gst_rtsp_stream_is_blocking:
3685 * @stream: a #GstRTSPStream
3687 * Check if @stream is blocking on a #GstBuffer.
3689 * Returns: %TRUE if @stream is blocking
3692 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3694 GstRTSPStreamPrivate *priv;
3697 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3699 priv = stream->priv;
3701 g_mutex_lock (&priv->lock);
3702 result = priv->blocking;
3703 g_mutex_unlock (&priv->lock);
3709 * gst_rtsp_stream_query_position:
3710 * @stream: a #GstRTSPStream
3712 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3713 * the RTP parts of the pipeline and not the RTCP parts.
3715 * Returns: %TRUE if the position could be queried
3718 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3720 GstRTSPStreamPrivate *priv;
3724 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3726 priv = stream->priv;
3728 g_mutex_lock (&priv->lock);
3729 /* depending on the transport type, it should query corresponding sink */
3730 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3731 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3732 sink = priv->udpsink[0];
3734 sink = priv->appsink[0];
3737 gst_object_ref (sink);
3738 g_mutex_unlock (&priv->lock);
3743 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3744 gst_object_unref (sink);
3750 * gst_rtsp_stream_query_stop:
3751 * @stream: a #GstRTSPStream
3753 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3754 * the RTP parts of the pipeline and not the RTCP parts.
3756 * Returns: %TRUE if the stop could be queried
3759 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3761 GstRTSPStreamPrivate *priv;
3766 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3768 priv = stream->priv;
3770 g_mutex_lock (&priv->lock);
3771 /* depending on the transport type, it should query corresponding sink */
3772 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3773 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3774 sink = priv->udpsink[0];
3776 sink = priv->appsink[0];
3779 gst_object_ref (sink);
3780 g_mutex_unlock (&priv->lock);
3785 query = gst_query_new_segment (GST_FORMAT_TIME);
3786 if ((ret = gst_element_query (sink, query))) {
3789 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3790 if (format != GST_FORMAT_TIME)
3793 gst_query_unref (query);
3794 gst_object_unref (sink);