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 /* Container for udpsrc elements created for a specific RTSPTransport. */
68 GstElement *udpsrc[2];
69 } GstRTSPStreamUDPSrcs;
72 destroy_udp_srcs_func (gpointer data)
74 g_slice_free (GstRTSPStreamUDPSrcs, (GstRTSPStreamUDPSrcs *) data);
77 struct _GstRTSPStreamPrivate
81 /* Only one pad is ever set */
82 GstPad *srcpad, *sinkpad;
83 GstElement *payloader;
88 /* TRUE if this stream is running on
89 * the client side of an RTSP link (for RECORD) */
93 GstRTSPProfile profiles;
94 GstRTSPLowerTrans protocols;
96 /* pads on the rtpbin */
97 GstPad *send_rtp_sink;
102 /* the RTPSession object */
105 /* SRTP encoder/decoder */
110 /* Unicast UDP sources associated with RTSPTransports */
113 /* Only allow one set of IPV4 and IPV6 multicast udpsrcs */
114 GstElement *udpsrc_mcast_v4[2];
115 GstElement *udpsrc_mcast_v6[2];
117 GstElement *udpqueue[2];
118 GstElement *udpsink[2];
120 /* for TCP transport */
121 GstElement *appsrc[2];
122 GstClockTime appsrc_base_time[2];
123 GstElement *appqueue[2];
124 GstElement *appsink[2];
127 GstElement *funnel[2];
132 GstClockTime rtx_time;
134 /* server ports for sending/receiving over ipv4 */
135 GstRTSPRange server_port_v4;
136 GstRTSPAddress *server_addr_v4;
138 /* server ports for sending/receiving over ipv6 */
139 GstRTSPRange server_port_v6;
140 GstRTSPAddress *server_addr_v6;
142 /* multicast addresses */
143 GstRTSPAddressPool *pool;
144 GstRTSPAddress *addr_v4;
145 GstRTSPAddress *addr_v6;
146 gboolean have_ipv4_mcast;
147 gboolean have_ipv6_mcast;
149 gchar *multicast_iface;
151 /* the caps of the stream */
155 /* transports we stream to */
158 guint transports_cookie;
160 GList *tr_cache_rtcp;
161 guint tr_cache_cookie_rtp;
162 guint tr_cache_cookie_rtcp;
167 /* stream blocking */
171 /* pt->caps map for RECORD streams */
174 GstRTSPPublishClockMode publish_clock_mode;
177 #define DEFAULT_CONTROL NULL
178 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
179 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
180 GST_RTSP_LOWER_TRANS_TCP
193 SIGNAL_NEW_RTP_ENCODER,
194 SIGNAL_NEW_RTCP_ENCODER,
198 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
199 #define GST_CAT_DEFAULT rtsp_stream_debug
201 static GQuark ssrc_stream_map_key;
203 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
204 GValue * value, GParamSpec * pspec);
205 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
206 const GValue * value, GParamSpec * pspec);
208 static void gst_rtsp_stream_finalize (GObject * obj);
210 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
212 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
215 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
217 GObjectClass *gobject_class;
219 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
221 gobject_class = G_OBJECT_CLASS (klass);
223 gobject_class->get_property = gst_rtsp_stream_get_property;
224 gobject_class->set_property = gst_rtsp_stream_set_property;
225 gobject_class->finalize = gst_rtsp_stream_finalize;
227 g_object_class_install_property (gobject_class, PROP_CONTROL,
228 g_param_spec_string ("control", "Control",
229 "The control string for this stream", DEFAULT_CONTROL,
230 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
232 g_object_class_install_property (gobject_class, PROP_PROFILES,
233 g_param_spec_flags ("profiles", "Profiles",
234 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
235 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
238 g_param_spec_flags ("protocols", "Protocols",
239 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
240 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
242 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
243 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
244 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
245 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
247 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
248 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
249 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
250 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
252 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
254 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
258 gst_rtsp_stream_init (GstRTSPStream * stream)
260 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
262 GST_DEBUG ("new stream %p", stream);
267 priv->control = g_strdup (DEFAULT_CONTROL);
268 priv->profiles = DEFAULT_PROFILES;
269 priv->protocols = DEFAULT_PROTOCOLS;
270 priv->publish_clock_mode = GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK;
272 g_mutex_init (&priv->lock);
274 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
275 NULL, (GDestroyNotify) gst_caps_unref);
276 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
277 (GDestroyNotify) gst_caps_unref);
278 priv->udpsrcs = g_hash_table_new_full (g_direct_hash, g_direct_equal,
279 NULL, (GDestroyNotify) destroy_udp_srcs_func);
283 gst_rtsp_stream_finalize (GObject * obj)
285 GstRTSPStream *stream;
286 GstRTSPStreamPrivate *priv;
288 stream = GST_RTSP_STREAM (obj);
291 GST_DEBUG ("finalize stream %p", stream);
293 /* we really need to be unjoined now */
294 g_return_if_fail (!priv->is_joined);
297 gst_rtsp_address_free (priv->addr_v4);
299 gst_rtsp_address_free (priv->addr_v6);
300 if (priv->server_addr_v4)
301 gst_rtsp_address_free (priv->server_addr_v4);
302 if (priv->server_addr_v6)
303 gst_rtsp_address_free (priv->server_addr_v6);
305 g_object_unref (priv->pool);
307 g_object_unref (priv->rtxsend);
309 g_free (priv->multicast_iface);
311 gst_object_unref (priv->payloader);
313 gst_object_unref (priv->srcpad);
315 gst_object_unref (priv->sinkpad);
316 g_free (priv->control);
317 g_mutex_clear (&priv->lock);
319 g_hash_table_unref (priv->keys);
320 g_hash_table_destroy (priv->ptmap);
322 /* We expect all udpsrcs to be cleaned up by this point. */
323 if (g_hash_table_size (priv->udpsrcs) > 0)
324 g_critical ("Unreffing udpsrcs hash table that contains elements.");
325 g_hash_table_unref (priv->udpsrcs);
327 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
331 gst_rtsp_stream_get_property (GObject * object, guint propid,
332 GValue * value, GParamSpec * pspec)
334 GstRTSPStream *stream = GST_RTSP_STREAM (object);
338 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
341 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
344 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
347 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
352 gst_rtsp_stream_set_property (GObject * object, guint propid,
353 const GValue * value, GParamSpec * pspec)
355 GstRTSPStream *stream = GST_RTSP_STREAM (object);
359 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
362 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
365 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
368 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
373 * gst_rtsp_stream_new:
376 * @payloader: a #GstElement
378 * Create a new media stream with index @idx that handles RTP data on
379 * @pad and has a payloader element @payloader if @pad is a source pad
380 * or a depayloader element @payloader if @pad is a sink pad.
382 * Returns: (transfer full): a new #GstRTSPStream
385 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
387 GstRTSPStreamPrivate *priv;
388 GstRTSPStream *stream;
390 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
391 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
393 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
396 priv->payloader = gst_object_ref (payloader);
397 if (GST_PAD_IS_SRC (pad))
398 priv->srcpad = gst_object_ref (pad);
400 priv->sinkpad = gst_object_ref (pad);
406 * gst_rtsp_stream_get_index:
407 * @stream: a #GstRTSPStream
409 * Get the stream index.
411 * Return: the stream index.
414 gst_rtsp_stream_get_index (GstRTSPStream * stream)
416 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
418 return stream->priv->idx;
422 * gst_rtsp_stream_get_pt:
423 * @stream: a #GstRTSPStream
425 * Get the stream payload type.
427 * Return: the stream payload type.
430 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
432 GstRTSPStreamPrivate *priv;
435 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
439 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
445 * gst_rtsp_stream_get_srcpad:
446 * @stream: a #GstRTSPStream
448 * Get the srcpad associated with @stream.
450 * Returns: (transfer full): the srcpad. Unref after usage.
453 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
455 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
457 if (!stream->priv->srcpad)
460 return gst_object_ref (stream->priv->srcpad);
464 * gst_rtsp_stream_get_sinkpad:
465 * @stream: a #GstRTSPStream
467 * Get the sinkpad associated with @stream.
469 * Returns: (transfer full): the sinkpad. Unref after usage.
472 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
474 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
476 if (!stream->priv->sinkpad)
479 return gst_object_ref (stream->priv->sinkpad);
483 * gst_rtsp_stream_get_control:
484 * @stream: a #GstRTSPStream
486 * Get the control string to identify this stream.
488 * Returns: (transfer full): the control string. g_free() after usage.
491 gst_rtsp_stream_get_control (GstRTSPStream * stream)
493 GstRTSPStreamPrivate *priv;
496 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
500 g_mutex_lock (&priv->lock);
501 if ((result = g_strdup (priv->control)) == NULL)
502 result = g_strdup_printf ("stream=%u", priv->idx);
503 g_mutex_unlock (&priv->lock);
509 * gst_rtsp_stream_set_control:
510 * @stream: a #GstRTSPStream
511 * @control: a control string
513 * Set the control string in @stream.
516 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
518 GstRTSPStreamPrivate *priv;
520 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
524 g_mutex_lock (&priv->lock);
525 g_free (priv->control);
526 priv->control = g_strdup (control);
527 g_mutex_unlock (&priv->lock);
531 * gst_rtsp_stream_has_control:
532 * @stream: a #GstRTSPStream
533 * @control: a control string
535 * Check if @stream has the control string @control.
537 * Returns: %TRUE is @stream has @control as the control string
540 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
542 GstRTSPStreamPrivate *priv;
545 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
549 g_mutex_lock (&priv->lock);
551 res = (g_strcmp0 (priv->control, control) == 0);
555 if (sscanf (control, "stream=%u", &streamid) > 0)
556 res = (streamid == priv->idx);
560 g_mutex_unlock (&priv->lock);
566 * gst_rtsp_stream_set_mtu:
567 * @stream: a #GstRTSPStream
570 * Configure the mtu in the payloader of @stream to @mtu.
573 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
575 GstRTSPStreamPrivate *priv;
577 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
581 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
583 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
587 * gst_rtsp_stream_get_mtu:
588 * @stream: a #GstRTSPStream
590 * Get the configured MTU in the payloader of @stream.
592 * Returns: the MTU of the payloader.
595 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
597 GstRTSPStreamPrivate *priv;
600 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
604 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
609 /* Update the dscp qos property on the udp sinks */
611 update_dscp_qos (GstRTSPStream * stream)
613 GstRTSPStreamPrivate *priv;
615 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
619 if (priv->udpsink[0]) {
620 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
624 if (priv->udpsink[1]) {
625 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
631 * gst_rtsp_stream_set_dscp_qos:
632 * @stream: a #GstRTSPStream
633 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
635 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
638 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
640 GstRTSPStreamPrivate *priv;
642 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
646 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
648 if (dscp_qos < -1 || dscp_qos > 63) {
649 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
653 priv->dscp_qos = dscp_qos;
655 update_dscp_qos (stream);
659 * gst_rtsp_stream_get_dscp_qos:
660 * @stream: a #GstRTSPStream
662 * Get the configured DSCP QoS in of the outgoing sockets.
664 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
667 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
669 GstRTSPStreamPrivate *priv;
671 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
675 return priv->dscp_qos;
679 * gst_rtsp_stream_is_transport_supported:
680 * @stream: a #GstRTSPStream
681 * @transport: (transfer none): a #GstRTSPTransport
683 * Check if @transport can be handled by stream
685 * Returns: %TRUE if @transport can be handled by @stream.
688 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
689 GstRTSPTransport * transport)
691 GstRTSPStreamPrivate *priv;
693 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
697 g_mutex_lock (&priv->lock);
698 if (transport->trans != GST_RTSP_TRANS_RTP)
699 goto unsupported_transmode;
701 if (!(transport->profile & priv->profiles))
702 goto unsupported_profile;
704 if (!(transport->lower_transport & priv->protocols))
705 goto unsupported_ltrans;
707 g_mutex_unlock (&priv->lock);
712 unsupported_transmode:
714 GST_DEBUG ("unsupported transport mode %d", transport->trans);
715 g_mutex_unlock (&priv->lock);
720 GST_DEBUG ("unsupported profile %d", transport->profile);
721 g_mutex_unlock (&priv->lock);
726 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
727 g_mutex_unlock (&priv->lock);
733 * gst_rtsp_stream_set_profiles:
734 * @stream: a #GstRTSPStream
735 * @profiles: the new profiles
737 * Configure the allowed profiles for @stream.
740 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
742 GstRTSPStreamPrivate *priv;
744 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
748 g_mutex_lock (&priv->lock);
749 priv->profiles = profiles;
750 g_mutex_unlock (&priv->lock);
754 * gst_rtsp_stream_get_profiles:
755 * @stream: a #GstRTSPStream
757 * Get the allowed profiles of @stream.
759 * Returns: a #GstRTSPProfile
762 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
764 GstRTSPStreamPrivate *priv;
767 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
771 g_mutex_lock (&priv->lock);
772 res = priv->profiles;
773 g_mutex_unlock (&priv->lock);
779 * gst_rtsp_stream_set_protocols:
780 * @stream: a #GstRTSPStream
781 * @protocols: the new flags
783 * Configure the allowed lower transport for @stream.
786 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
787 GstRTSPLowerTrans protocols)
789 GstRTSPStreamPrivate *priv;
791 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
795 g_mutex_lock (&priv->lock);
796 priv->protocols = protocols;
797 g_mutex_unlock (&priv->lock);
801 * gst_rtsp_stream_get_protocols:
802 * @stream: a #GstRTSPStream
804 * Get the allowed protocols of @stream.
806 * Returns: a #GstRTSPLowerTrans
809 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
811 GstRTSPStreamPrivate *priv;
812 GstRTSPLowerTrans res;
814 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
815 GST_RTSP_LOWER_TRANS_UNKNOWN);
819 g_mutex_lock (&priv->lock);
820 res = priv->protocols;
821 g_mutex_unlock (&priv->lock);
827 * gst_rtsp_stream_set_address_pool:
828 * @stream: a #GstRTSPStream
829 * @pool: (transfer none): a #GstRTSPAddressPool
831 * configure @pool to be used as the address pool of @stream.
834 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
835 GstRTSPAddressPool * pool)
837 GstRTSPStreamPrivate *priv;
838 GstRTSPAddressPool *old;
840 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
844 GST_LOG_OBJECT (stream, "set address pool %p", pool);
846 g_mutex_lock (&priv->lock);
847 if ((old = priv->pool) != pool)
848 priv->pool = pool ? g_object_ref (pool) : NULL;
851 g_mutex_unlock (&priv->lock);
854 g_object_unref (old);
858 * gst_rtsp_stream_get_address_pool:
859 * @stream: a #GstRTSPStream
861 * Get the #GstRTSPAddressPool used as the address pool of @stream.
863 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
867 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
869 GstRTSPStreamPrivate *priv;
870 GstRTSPAddressPool *result;
872 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
876 g_mutex_lock (&priv->lock);
877 if ((result = priv->pool))
878 g_object_ref (result);
879 g_mutex_unlock (&priv->lock);
885 * gst_rtsp_stream_set_multicast_iface:
886 * @stream: a #GstRTSPStream
887 * @multicast_iface: (transfer none): a multicast interface
889 * configure @multicast_iface to be used for @stream.
892 gst_rtsp_stream_set_multicast_iface (GstRTSPStream * stream,
893 const gchar * multicast_iface)
895 GstRTSPStreamPrivate *priv;
898 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
902 GST_LOG_OBJECT (stream, "set multicast iface %s",
903 GST_STR_NULL (multicast_iface));
905 g_mutex_lock (&priv->lock);
906 if ((old = priv->multicast_iface) != multicast_iface)
907 priv->multicast_iface = multicast_iface ? g_strdup (multicast_iface) : NULL;
910 g_mutex_unlock (&priv->lock);
917 * gst_rtsp_stream_get_multicast_iface:
918 * @stream: a #GstRTSPStream
920 * Get the multicast interface used for @stream.
922 * Returns: (transfer full): the multicast interface for @stream. g_free() after
926 gst_rtsp_stream_get_multicast_iface (GstRTSPStream * stream)
928 GstRTSPStreamPrivate *priv;
931 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
935 g_mutex_lock (&priv->lock);
936 if ((result = priv->multicast_iface))
937 result = g_strdup (result);
938 g_mutex_unlock (&priv->lock);
944 * gst_rtsp_stream_get_multicast_address:
945 * @stream: a #GstRTSPStream
946 * @family: the #GSocketFamily
948 * Get the multicast address of @stream for @family.
950 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
951 * or %NULL when no address could be allocated. gst_rtsp_address_free()
955 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
956 GSocketFamily family)
958 GstRTSPStreamPrivate *priv;
959 GstRTSPAddress *result;
960 GstRTSPAddress **addrp;
961 GstRTSPAddressFlags flags;
963 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
967 if (family == G_SOCKET_FAMILY_IPV6) {
968 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
969 addrp = &priv->addr_v6;
971 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
972 addrp = &priv->addr_v4;
975 g_mutex_lock (&priv->lock);
976 if (*addrp == NULL) {
977 if (priv->pool == NULL)
980 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
982 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
986 result = gst_rtsp_address_copy (*addrp);
987 g_mutex_unlock (&priv->lock);
994 GST_ERROR_OBJECT (stream, "no address pool specified");
995 g_mutex_unlock (&priv->lock);
1000 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
1001 g_mutex_unlock (&priv->lock);
1007 * gst_rtsp_stream_reserve_address:
1008 * @stream: a #GstRTSPStream
1009 * @address: an address
1014 * Reserve @address and @port as the address and port of @stream.
1016 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
1017 * the address could be reserved. gst_rtsp_address_free() after usage.
1020 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
1021 const gchar * address, guint port, guint n_ports, guint ttl)
1023 GstRTSPStreamPrivate *priv;
1024 GstRTSPAddress *result;
1026 GSocketFamily family;
1027 GstRTSPAddress **addrp;
1029 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1030 g_return_val_if_fail (address != NULL, NULL);
1031 g_return_val_if_fail (port > 0, NULL);
1032 g_return_val_if_fail (n_ports > 0, NULL);
1033 g_return_val_if_fail (ttl > 0, NULL);
1035 priv = stream->priv;
1037 addr = g_inet_address_new_from_string (address);
1039 GST_ERROR ("failed to get inet addr from %s", address);
1040 family = G_SOCKET_FAMILY_IPV4;
1042 family = g_inet_address_get_family (addr);
1043 g_object_unref (addr);
1046 if (family == G_SOCKET_FAMILY_IPV6)
1047 addrp = &priv->addr_v6;
1049 addrp = &priv->addr_v4;
1051 g_mutex_lock (&priv->lock);
1052 if (*addrp == NULL) {
1053 GstRTSPAddressPoolResult res;
1055 if (priv->pool == NULL)
1058 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
1059 port, n_ports, ttl, addrp);
1060 if (res != GST_RTSP_ADDRESS_POOL_OK)
1063 if (strcmp ((*addrp)->address, address) ||
1064 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
1065 (*addrp)->ttl != ttl)
1066 goto different_address;
1068 result = gst_rtsp_address_copy (*addrp);
1069 g_mutex_unlock (&priv->lock);
1076 GST_ERROR_OBJECT (stream, "no address pool specified");
1077 g_mutex_unlock (&priv->lock);
1082 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1084 g_mutex_unlock (&priv->lock);
1089 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
1090 " reserved", address);
1091 g_mutex_unlock (&priv->lock);
1096 /* must be called with lock */
1098 set_sockets_for_udpsinks (GstRTSPStream * stream, GSocket * rtp_socket,
1099 GSocket * rtcp_socket, GSocketFamily family)
1101 GstRTSPStreamPrivate *priv = stream->priv;
1102 const gchar *multisink_socket;
1104 if (family == G_SOCKET_FAMILY_IPV6)
1105 multisink_socket = "socket-v6";
1107 multisink_socket = "socket";
1109 g_object_set (G_OBJECT (priv->udpsink[0]), multisink_socket, rtp_socket,
1111 g_object_set (G_OBJECT (priv->udpsink[1]), multisink_socket, rtcp_socket,
1115 /* must be called with lock */
1117 create_and_configure_udpsinks (GstRTSPStream * stream)
1119 GstRTSPStreamPrivate *priv = stream->priv;
1120 GstElement *udpsink0, *udpsink1;
1125 if (priv->udpsink[0])
1126 udpsink0 = priv->udpsink[0];
1128 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1131 goto no_udp_protocol;
1133 if (priv->udpsink[1])
1134 udpsink1 = priv->udpsink[1];
1136 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1139 goto no_udp_protocol;
1141 /* configure sinks */
1143 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1144 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1146 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1147 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1149 g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
1151 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1152 /* Needs to be async for RECORD streams, otherwise we will never go to
1153 * PLAYING because the sinks will wait for data while the udpsrc can't
1154 * provide data with timestamps in PAUSED. */
1156 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1157 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1159 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1160 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1162 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1163 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1165 /* update the dscp qos field in the sinks */
1166 update_dscp_qos (stream);
1168 priv->udpsink[0] = udpsink0;
1169 priv->udpsink[1] = udpsink1;
1180 /* must be called with lock */
1182 play_udpsources_one_family (GstRTSPStream * stream, GstElement * udpsrc_out[2],
1183 GSocketFamily family)
1185 GstRTSPStreamPrivate *priv;
1186 GstPad *pad, *selpad;
1190 priv = stream->priv;
1191 bin = GST_BIN (gst_object_get_parent (GST_OBJECT (priv->funnel[1])));
1193 for (i = 0; i < 2; i++) {
1194 if (priv->sinkpad || i == 1) {
1196 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1197 * values. This is only relevant for PLAY pipelines */
1198 gst_element_set_state (udpsrc_out[i], GST_STATE_PLAYING);
1199 gst_element_set_locked_state (udpsrc_out[i], TRUE);
1202 gst_bin_add (bin, udpsrc_out[i]);
1204 /* and link to the funnel */
1205 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1206 pad = gst_element_get_static_pad (udpsrc_out[i], "src");
1207 gst_pad_link (pad, selpad);
1208 gst_object_unref (pad);
1209 gst_object_unref (selpad);
1211 /* otherwise sync state with parent in case it's running already
1213 if (!priv->srcpad) {
1214 gst_element_sync_state_with_parent (udpsrc_out[i]);
1219 gst_object_unref (bin);
1222 /* must be called with lock */
1224 create_and_configure_udpsources_one_family (GstElement * udpsrc_out[2],
1225 GSocket * rtp_socket, GSocket * rtcp_socket, GSocketFamily family,
1226 const gchar * address, gint rtpport, gint rtcpport,
1227 const gchar * multicast_iface, GstRTSPLowerTrans transport)
1229 GstStateChangeReturn ret;
1231 udpsrc_out[0] = gst_element_factory_make ("udpsrc", NULL);
1232 udpsrc_out[1] = gst_element_factory_make ("udpsrc", NULL);
1234 if (udpsrc_out[0] == NULL || udpsrc_out[1] == NULL)
1237 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1238 g_object_set (G_OBJECT (udpsrc_out[0]), "address", address, NULL);
1239 g_object_set (G_OBJECT (udpsrc_out[1]), "address", address, NULL);
1240 g_object_set (G_OBJECT (udpsrc_out[0]), "port", rtpport, NULL);
1241 g_object_set (G_OBJECT (udpsrc_out[1]), "port", rtcpport, NULL);
1242 g_object_set (G_OBJECT (udpsrc_out[0]), "multicast-iface", multicast_iface,
1244 g_object_set (G_OBJECT (udpsrc_out[1]), "multicast-iface", multicast_iface,
1246 g_object_set (G_OBJECT (udpsrc_out[0]), "loop", FALSE, NULL);
1247 g_object_set (G_OBJECT (udpsrc_out[1]), "loop", FALSE, NULL);
1250 g_object_set (G_OBJECT (udpsrc_out[0]), "socket", rtp_socket, NULL);
1251 g_object_set (G_OBJECT (udpsrc_out[1]), "socket", rtcp_socket, NULL);
1253 ret = gst_element_set_state (udpsrc_out[0], GST_STATE_READY);
1254 if (ret == GST_STATE_CHANGE_FAILURE)
1256 ret = gst_element_set_state (udpsrc_out[1], GST_STATE_READY);
1257 if (ret == GST_STATE_CHANGE_FAILURE)
1267 gst_object_unref (udpsrc_out[0]);
1269 gst_object_unref (udpsrc_out[1]);
1275 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1276 GstElement * udpsrc_out[2], GstRTSPRange * server_port_out,
1277 GstRTSPTransport * ct, GstRTSPAddress ** server_addr_out,
1278 gboolean use_client_settings)
1280 GstRTSPStreamPrivate *priv = stream->priv;
1281 GSocket *rtp_socket = NULL;
1282 GSocket *rtcp_socket;
1283 gint tmp_rtp, tmp_rtcp;
1285 gint rtpport, rtcpport;
1286 GList *rejected_addresses = NULL;
1287 GstRTSPAddress *addr = NULL;
1288 GInetAddress *inetaddr = NULL;
1290 GSocketAddress *rtp_sockaddr = NULL;
1291 GSocketAddress *rtcp_sockaddr = NULL;
1292 GstRTSPAddressPool *pool;
1293 GstRTSPLowerTrans transport;
1294 const gchar *multicast_iface = priv->multicast_iface;
1298 transport = ct->lower_transport;
1300 /* Start with random port */
1303 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1304 G_SOCKET_PROTOCOL_UDP, NULL);
1306 goto no_udp_protocol;
1307 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1309 if (*server_addr_out)
1310 gst_rtsp_address_free (*server_addr_out);
1312 /* try to allocate 2 UDP ports, the RTP port should be an even
1313 * number and the RTCP port should be the next (uneven) port */
1316 if (rtp_socket == NULL) {
1317 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1318 G_SOCKET_PROTOCOL_UDP, NULL);
1320 goto no_udp_protocol;
1321 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1324 if (pool && ((transport == GST_RTSP_LOWER_TRANS_UDP &&
1325 gst_rtsp_address_pool_has_unicast_addresses (pool))
1326 || transport == GST_RTSP_LOWER_TRANS_UDP_MCAST)) {
1327 GstRTSPAddressFlags flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1329 if (transport == GST_RTSP_LOWER_TRANS_UDP)
1330 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1332 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1335 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1337 if (family == G_SOCKET_FAMILY_IPV6)
1338 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1340 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1342 if (ct->destination && transport == GST_RTSP_LOWER_TRANS_UDP_MCAST
1343 && use_client_settings)
1344 gst_rtsp_address_pool_reserve_address (pool, ct->destination,
1345 ct->port.min, 2, ct->ttl, &addr);
1347 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1352 tmp_rtp = addr->port;
1354 g_clear_object (&inetaddr);
1355 inetaddr = g_inet_address_new_from_string (addr->address);
1357 /* If we're supposed to bind to a multicast address, instead bind
1358 * to ANY and let udpsrc later join the relevant multicast group
1360 if (g_inet_address_get_is_multicast (inetaddr)) {
1361 g_object_unref (inetaddr);
1362 inetaddr = g_inet_address_new_any (family);
1371 if (inetaddr == NULL)
1372 inetaddr = g_inet_address_new_any (family);
1375 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1376 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1377 g_object_unref (rtp_sockaddr);
1380 g_object_unref (rtp_sockaddr);
1382 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1383 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1384 g_clear_object (&rtp_sockaddr);
1389 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1390 g_object_unref (rtp_sockaddr);
1392 /* check if port is even */
1393 if ((tmp_rtp & 1) != 0) {
1394 /* port not even, close and allocate another */
1396 g_clear_object (&rtp_socket);
1401 tmp_rtcp = tmp_rtp + 1;
1403 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1404 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1405 g_object_unref (rtcp_sockaddr);
1406 g_clear_object (&rtp_socket);
1409 g_object_unref (rtcp_sockaddr);
1412 addr_str = g_inet_address_to_string (inetaddr);
1414 addr_str = addr->address;
1415 g_clear_object (&inetaddr);
1417 if (!create_and_configure_udpsources_one_family (udpsrc_out, rtp_socket,
1418 rtcp_socket, family, addr_str, tmp_rtp, tmp_rtcp, multicast_iface,
1422 goto no_udp_protocol;
1428 play_udpsources_one_family (stream, udpsrc_out, family);
1430 g_object_get (G_OBJECT (udpsrc_out[0]), "port", &rtpport, NULL);
1431 g_object_get (G_OBJECT (udpsrc_out[1]), "port", &rtcpport, NULL);
1433 /* this should not happen... */
1434 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1437 /* set RTP and RTCP sockets */
1438 set_sockets_for_udpsinks (stream, rtp_socket, rtcp_socket, family);
1440 server_port_out->min = rtpport;
1441 server_port_out->max = rtcpport;
1443 *server_addr_out = addr;
1444 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1446 g_object_unref (rtp_socket);
1447 g_object_unref (rtcp_socket);
1471 g_object_unref (inetaddr);
1472 g_list_free_full (rejected_addresses,
1473 (GDestroyNotify) gst_rtsp_address_free);
1475 gst_rtsp_address_free (addr);
1477 g_object_unref (rtp_socket);
1479 g_object_unref (rtcp_socket);
1485 * gst_rtsp_stream_allocate_udp_sockets:
1486 * @stream: a #GstRTSPStream
1487 * @family: protocol family
1488 * @transport_method: transport method
1490 * Allocates RTP and RTCP ports.
1492 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1495 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1496 GSocketFamily family, GstRTSPTransport * ct, gboolean use_client_settings)
1498 GstRTSPStreamPrivate *priv;
1499 gboolean result = FALSE;
1500 GstRTSPLowerTrans transport = ct->lower_transport;
1502 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1503 priv = stream->priv;
1504 g_return_val_if_fail (priv->is_joined, FALSE);
1506 g_mutex_lock (&priv->lock);
1508 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1509 if (family == G_SOCKET_FAMILY_IPV4) {
1510 /* Multicast IPV4 */
1511 if (priv->have_ipv4_mcast) {
1516 priv->have_ipv4_mcast =
1517 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1518 priv->udpsrc_mcast_v4, &priv->server_port_v4, ct, &priv->addr_v4,
1519 use_client_settings);
1520 result = priv->have_ipv4_mcast;
1523 /* Multicast IPV6 */
1524 if (priv->have_ipv6_mcast) {
1529 priv->have_ipv6_mcast =
1530 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1531 priv->udpsrc_mcast_v6, &priv->server_port_v6, ct, &priv->addr_v6,
1532 use_client_settings);
1533 result = priv->have_ipv6_mcast;
1536 /* We allow multiple unicast transports, so we must maintain a table of the
1537 * udpsrcs created for them. */
1538 GstRTSPStreamUDPSrcs *transport_udpsrcs =
1539 g_slice_new0 (GstRTSPStreamUDPSrcs);
1541 if (family == G_SOCKET_FAMILY_IPV4) {
1544 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1545 transport_udpsrcs->udpsrc, &priv->server_port_v4, ct,
1546 &priv->server_addr_v4, use_client_settings);
1550 alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1551 transport_udpsrcs->udpsrc, &priv->server_port_v6, ct,
1552 &priv->server_addr_v6, use_client_settings);
1555 /* If we didn't create any unicast udpsrcs, free the transport_udpsrcs struct.
1556 * Otherwise, add it to the hash table */
1557 if (transport_udpsrcs->udpsrc[0] == NULL
1558 && transport_udpsrcs->udpsrc[1] == NULL)
1559 g_slice_free (GstRTSPStreamUDPSrcs, transport_udpsrcs);
1561 g_hash_table_insert (priv->udpsrcs, ct, transport_udpsrcs);
1565 g_mutex_unlock (&priv->lock);
1571 * gst_rtsp_stream_set_client_side:
1572 * @stream: a #GstRTSPStream
1573 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1574 * an RTSP connection.
1576 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1577 * streams to an RTSP server via RECORD. This has the practical effect
1578 * of changing which UDP port numbers are used when setting up the local
1579 * side of the stream sending to be either the 'server' or 'client' pair
1580 * of a configured UDP transport.
1583 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1585 GstRTSPStreamPrivate *priv;
1587 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1588 priv = stream->priv;
1589 g_mutex_lock (&priv->lock);
1590 priv->client_side = client_side;
1591 g_mutex_unlock (&priv->lock);
1595 * gst_rtsp_stream_is_client_side:
1596 * @stream: a #GstRTSPStream
1598 * See gst_rtsp_stream_set_client_side()
1600 * Returns: TRUE if this #GstRTSPStream is client-side.
1603 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1605 GstRTSPStreamPrivate *priv;
1608 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1610 priv = stream->priv;
1611 g_mutex_lock (&priv->lock);
1612 ret = priv->client_side;
1613 g_mutex_unlock (&priv->lock);
1619 * gst_rtsp_stream_get_server_port:
1620 * @stream: a #GstRTSPStream
1621 * @server_port: (out): result server port
1622 * @family: the port family to get
1624 * Fill @server_port with the port pair used by the server. This function can
1625 * only be called when @stream has been joined.
1628 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1629 GstRTSPRange * server_port, GSocketFamily family)
1631 GstRTSPStreamPrivate *priv;
1633 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1634 priv = stream->priv;
1635 g_return_if_fail (priv->is_joined);
1637 g_mutex_lock (&priv->lock);
1638 if (family == G_SOCKET_FAMILY_IPV4) {
1640 *server_port = priv->server_port_v4;
1643 *server_port = priv->server_port_v6;
1645 g_mutex_unlock (&priv->lock);
1649 * gst_rtsp_stream_get_rtpsession:
1650 * @stream: a #GstRTSPStream
1652 * Get the RTP session of this stream.
1654 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1657 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1659 GstRTSPStreamPrivate *priv;
1662 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1664 priv = stream->priv;
1666 g_mutex_lock (&priv->lock);
1667 if ((session = priv->session))
1668 g_object_ref (session);
1669 g_mutex_unlock (&priv->lock);
1675 * gst_rtsp_stream_get_ssrc:
1676 * @stream: a #GstRTSPStream
1677 * @ssrc: (out): result ssrc
1679 * Get the SSRC used by the RTP session of this stream. This function can only
1680 * be called when @stream has been joined.
1683 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1685 GstRTSPStreamPrivate *priv;
1687 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1688 priv = stream->priv;
1689 g_return_if_fail (priv->is_joined);
1691 g_mutex_lock (&priv->lock);
1692 if (ssrc && priv->session)
1693 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1694 g_mutex_unlock (&priv->lock);
1698 * gst_rtsp_stream_set_retransmission_time:
1699 * @stream: a #GstRTSPStream
1700 * @time: a #GstClockTime
1702 * Set the amount of time to store retransmission packets.
1705 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1708 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1710 g_mutex_lock (&stream->priv->lock);
1711 stream->priv->rtx_time = time;
1712 if (stream->priv->rtxsend)
1713 g_object_set (stream->priv->rtxsend, "max-size-time",
1714 GST_TIME_AS_MSECONDS (time), NULL);
1715 g_mutex_unlock (&stream->priv->lock);
1719 * gst_rtsp_stream_get_retransmission_time:
1720 * @stream: a #GstRTSPStream
1722 * Get the amount of time to store retransmission data.
1724 * Returns: the amount of time to store retransmission data.
1727 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1731 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1733 g_mutex_lock (&stream->priv->lock);
1734 ret = stream->priv->rtx_time;
1735 g_mutex_unlock (&stream->priv->lock);
1741 * gst_rtsp_stream_set_retransmission_pt:
1742 * @stream: a #GstRTSPStream
1745 * Set the payload type (pt) for retransmission of this stream.
1748 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1750 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1752 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1754 g_mutex_lock (&stream->priv->lock);
1755 stream->priv->rtx_pt = rtx_pt;
1756 if (stream->priv->rtxsend) {
1757 guint pt = gst_rtsp_stream_get_pt (stream);
1758 gchar *pt_s = g_strdup_printf ("%d", pt);
1759 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1760 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1761 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1763 gst_structure_free (rtx_pt_map);
1765 g_mutex_unlock (&stream->priv->lock);
1769 * gst_rtsp_stream_get_retransmission_pt:
1770 * @stream: a #GstRTSPStream
1772 * Get the payload-type used for retransmission of this stream
1774 * Returns: The retransmission PT.
1777 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1781 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1783 g_mutex_lock (&stream->priv->lock);
1784 rtx_pt = stream->priv->rtx_pt;
1785 g_mutex_unlock (&stream->priv->lock);
1791 * gst_rtsp_stream_set_buffer_size:
1792 * @stream: a #GstRTSPStream
1793 * @size: the buffer size
1795 * Set the size of the UDP transmission buffer (in bytes)
1796 * Needs to be set before the stream is joined to a bin.
1801 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1803 g_mutex_lock (&stream->priv->lock);
1804 stream->priv->buffer_size = size;
1805 g_mutex_unlock (&stream->priv->lock);
1809 * gst_rtsp_stream_get_buffer_size:
1810 * @stream: a #GstRTSPStream
1812 * Get the size of the UDP transmission buffer (in bytes)
1814 * Returns: the size of the UDP TX buffer
1819 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1823 g_mutex_lock (&stream->priv->lock);
1824 buffer_size = stream->priv->buffer_size;
1825 g_mutex_unlock (&stream->priv->lock);
1830 /* executed from streaming thread */
1832 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1834 GstRTSPStreamPrivate *priv = stream->priv;
1835 GstCaps *newcaps, *oldcaps;
1837 newcaps = gst_pad_get_current_caps (pad);
1839 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1842 g_mutex_lock (&priv->lock);
1843 oldcaps = priv->caps;
1844 priv->caps = newcaps;
1845 g_mutex_unlock (&priv->lock);
1848 gst_caps_unref (oldcaps);
1852 dump_structure (const GstStructure * s)
1856 sstr = gst_structure_to_string (s);
1857 GST_INFO ("structure: %s", sstr);
1861 static GstRTSPStreamTransport *
1862 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1864 GstRTSPStreamPrivate *priv = stream->priv;
1866 GstRTSPStreamTransport *result = NULL;
1871 if (rtcp_from == NULL)
1874 tmp = g_strrstr (rtcp_from, ":");
1878 port = atoi (tmp + 1);
1879 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1881 g_mutex_lock (&priv->lock);
1882 GST_INFO ("finding %s:%d in %d transports", dest, port,
1883 g_list_length (priv->transports));
1885 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1886 GstRTSPStreamTransport *trans = walk->data;
1887 const GstRTSPTransport *tr;
1890 tr = gst_rtsp_stream_transport_get_transport (trans);
1892 if (priv->client_side) {
1893 /* In client side mode the 'destination' is the RTSP server, so send
1895 min = tr->server_port.min;
1896 max = tr->server_port.max;
1898 min = tr->client_port.min;
1899 max = tr->client_port.max;
1902 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1908 g_object_ref (result);
1909 g_mutex_unlock (&priv->lock);
1916 static GstRTSPStreamTransport *
1917 check_transport (GObject * source, GstRTSPStream * stream)
1919 GstStructure *stats;
1920 GstRTSPStreamTransport *trans;
1922 /* see if we have a stream to match with the origin of the RTCP packet */
1923 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1924 if (trans == NULL) {
1925 g_object_get (source, "stats", &stats, NULL);
1927 const gchar *rtcp_from;
1929 dump_structure (stats);
1931 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1932 if ((trans = find_transport (stream, rtcp_from))) {
1933 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1935 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1938 gst_structure_free (stats);
1946 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1948 GstRTSPStreamTransport *trans;
1950 GST_INFO ("%p: new source %p", stream, source);
1952 trans = check_transport (source, stream);
1955 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1959 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1961 GST_INFO ("%p: new SDES %p", stream, source);
1965 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1967 GstRTSPStreamTransport *trans;
1969 trans = check_transport (source, stream);
1972 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1973 gst_rtsp_stream_transport_keep_alive (trans);
1977 GstStructure *stats;
1978 g_object_get (source, "stats", &stats, NULL);
1980 dump_structure (stats);
1981 gst_structure_free (stats);
1988 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1990 GST_INFO ("%p: source %p bye", stream, source);
1994 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1996 GstRTSPStreamTransport *trans;
1998 GST_INFO ("%p: source %p bye timeout", stream, source);
2000 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2001 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2002 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2007 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2009 GstRTSPStreamTransport *trans;
2011 GST_INFO ("%p: source %p timeout", stream, source);
2013 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2014 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2015 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2020 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2022 GST_INFO ("%p: new sender source %p", stream, source);
2025 GstStructure *stats;
2026 g_object_get (source, "stats", &stats, NULL);
2028 dump_structure (stats);
2029 gst_structure_free (stats);
2036 on_sender_ssrc_active (GObject * session, GObject * source,
2037 GstRTSPStream * stream)
2041 GstStructure *stats;
2042 g_object_get (source, "stats", &stats, NULL);
2044 dump_structure (stats);
2045 gst_structure_free (stats);
2052 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
2055 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
2056 g_list_free (priv->tr_cache_rtp);
2057 priv->tr_cache_rtp = NULL;
2059 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
2060 g_list_free (priv->tr_cache_rtcp);
2061 priv->tr_cache_rtcp = NULL;
2065 static GstFlowReturn
2066 handle_new_sample (GstAppSink * sink, gpointer user_data)
2068 GstRTSPStreamPrivate *priv;
2072 GstRTSPStream *stream;
2075 sample = gst_app_sink_pull_sample (sink);
2079 stream = (GstRTSPStream *) user_data;
2080 priv = stream->priv;
2081 buffer = gst_sample_get_buffer (sample);
2083 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
2085 g_mutex_lock (&priv->lock);
2087 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
2088 clear_tr_cache (priv, is_rtp);
2089 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2090 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2091 priv->tr_cache_rtp =
2092 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
2094 priv->tr_cache_cookie_rtp = priv->transports_cookie;
2097 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
2098 clear_tr_cache (priv, is_rtp);
2099 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2100 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2101 priv->tr_cache_rtcp =
2102 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
2104 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
2107 g_mutex_unlock (&priv->lock);
2110 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2111 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2112 gst_rtsp_stream_transport_send_rtp (tr, buffer);
2115 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2116 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2117 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
2120 gst_sample_unref (sample);
2125 static GstAppSinkCallbacks sink_cb = {
2126 NULL, /* not interested in EOS */
2127 NULL, /* not interested in preroll samples */
2132 get_rtp_encoder (GstRTSPStream * stream, guint session)
2134 GstRTSPStreamPrivate *priv = stream->priv;
2136 if (priv->srtpenc == NULL) {
2139 name = g_strdup_printf ("srtpenc_%u", session);
2140 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2143 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2145 return gst_object_ref (priv->srtpenc);
2149 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2151 GstRTSPStreamPrivate *priv = stream->priv;
2152 GstElement *oldenc, *enc;
2156 if (priv->idx != session)
2159 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2161 oldenc = priv->srtpenc;
2162 enc = get_rtp_encoder (stream, session);
2163 name = g_strdup_printf ("rtp_sink_%d", session);
2164 pad = gst_element_get_request_pad (enc, name);
2166 gst_object_unref (pad);
2169 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2176 request_rtcp_encoder (GstElement * rtpbin, guint session,
2177 GstRTSPStream * stream)
2179 GstRTSPStreamPrivate *priv = stream->priv;
2180 GstElement *oldenc, *enc;
2184 if (priv->idx != session)
2187 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2189 oldenc = priv->srtpenc;
2190 enc = get_rtp_encoder (stream, session);
2191 name = g_strdup_printf ("rtcp_sink_%d", session);
2192 pad = gst_element_get_request_pad (enc, name);
2194 gst_object_unref (pad);
2197 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2204 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2206 GstRTSPStreamPrivate *priv = stream->priv;
2209 GST_DEBUG ("request key %08x", ssrc);
2211 g_mutex_lock (&priv->lock);
2212 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2213 gst_caps_ref (caps);
2214 g_mutex_unlock (&priv->lock);
2220 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2221 GstRTSPStream * stream)
2223 GstRTSPStreamPrivate *priv = stream->priv;
2225 if (priv->idx != session)
2228 if (priv->srtpdec == NULL) {
2231 name = g_strdup_printf ("srtpdec_%u", session);
2232 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2235 g_signal_connect (priv->srtpdec, "request-key",
2236 (GCallback) request_key, stream);
2238 return gst_object_ref (priv->srtpdec);
2242 * gst_rtsp_stream_request_aux_sender:
2243 * @stream: a #GstRTSPStream
2244 * @sessid: the session id
2246 * Creating a rtxsend bin
2248 * Returns: (transfer full): a #GstElement.
2253 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2257 GstStructure *pt_map;
2262 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2264 pt = gst_rtsp_stream_get_pt (stream);
2265 pt_s = g_strdup_printf ("%u", pt);
2266 rtx_pt = stream->priv->rtx_pt;
2268 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2270 bin = gst_bin_new (NULL);
2271 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2272 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2273 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2274 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2275 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2277 gst_structure_free (pt_map);
2278 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2280 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2281 name = g_strdup_printf ("src_%u", sessid);
2282 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2284 gst_object_unref (pad);
2286 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2287 name = g_strdup_printf ("sink_%u", sessid);
2288 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2290 gst_object_unref (pad);
2296 * gst_rtsp_stream_set_pt_map:
2297 * @stream: a #GstRTSPStream
2301 * Configure a pt map between @pt and @caps.
2304 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2306 GstRTSPStreamPrivate *priv = stream->priv;
2308 g_mutex_lock (&priv->lock);
2309 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2310 g_mutex_unlock (&priv->lock);
2314 * gst_rtsp_stream_set_publish_clock_mode:
2315 * @stream: a #GstRTSPStream
2316 * @mode: the clock publish mode
2318 * Sets if and how the stream clock should be published according to RFC7273.
2323 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
2324 GstRTSPPublishClockMode mode)
2326 GstRTSPStreamPrivate *priv;
2328 priv = stream->priv;
2329 g_mutex_lock (&priv->lock);
2330 priv->publish_clock_mode = mode;
2331 g_mutex_unlock (&priv->lock);
2335 * gst_rtsp_stream_get_publish_clock_mode:
2336 * @factory: a #GstRTSPStream
2338 * Gets if and how the stream clock should be published according to RFC7273.
2340 * Returns: The GstRTSPPublishClockMode
2344 GstRTSPPublishClockMode
2345 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
2347 GstRTSPStreamPrivate *priv;
2348 GstRTSPPublishClockMode ret;
2350 priv = stream->priv;
2351 g_mutex_lock (&priv->lock);
2352 ret = priv->publish_clock_mode;
2353 g_mutex_unlock (&priv->lock);
2359 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2360 GstRTSPStream * stream)
2362 GstRTSPStreamPrivate *priv = stream->priv;
2363 GstCaps *caps = NULL;
2365 g_mutex_lock (&priv->lock);
2367 if (priv->idx == session) {
2368 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2370 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2371 gst_caps_ref (caps);
2373 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2377 g_mutex_unlock (&priv->lock);
2383 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2385 GstRTSPStreamPrivate *priv = stream->priv;
2387 GstPadLinkReturn ret;
2390 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2391 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2393 name = gst_pad_get_name (pad);
2394 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2400 if (priv->idx != sessid)
2403 if (gst_pad_is_linked (priv->sinkpad)) {
2404 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2405 GST_DEBUG_PAD_NAME (priv->sinkpad));
2409 /* link the RTP pad to the session manager, it should not really fail unless
2410 * this is not really an RTP pad */
2411 ret = gst_pad_link (pad, priv->sinkpad);
2412 if (ret != GST_PAD_LINK_OK)
2414 priv->recv_rtp_src = gst_object_ref (pad);
2421 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2422 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2427 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2428 GstRTSPStream * stream)
2430 /* TODO: What to do here other than this? */
2431 GST_DEBUG ("Stream %p: Got EOS", stream);
2432 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2435 /* must be called with lock */
2437 create_sender_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2439 GstRTSPStreamPrivate *priv;
2440 GstPad *pad, *sinkpad = NULL;
2441 gboolean is_tcp = FALSE, is_udp = FALSE;
2444 priv = stream->priv;
2446 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2447 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2448 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2450 if (is_udp && !create_and_configure_udpsinks (stream))
2451 goto no_udp_protocol;
2453 for (i = 0; i < 2; i++) {
2454 GstPad *teepad, *queuepad;
2455 /* For the sender we create this bit of pipeline for both
2456 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2457 * we need to add a queue before appsink and udpsink to make
2458 * the pipeline not block. For the TCP case, we want to pump
2459 * client as fast as possible anyway. This pipeline is used
2460 * when both TCP and UDP are present.
2462 * .--------. .-----. .---------. .---------.
2463 * | rtpbin | | tee | | queue | | udpsink |
2464 * | send->sink src->sink src->sink |
2465 * '--------' | | '---------' '---------'
2466 * | | .---------. .---------.
2467 * | | | queue | | appsink |
2468 * | src->sink src->sink |
2469 * '-----' '---------' '---------'
2471 * When only UDP or only TCP is allowed, we skip the tee and queue
2472 * and link the udpsink (for UDP) or appsink (for TCP) directly to
2475 /* Only link the RTP send src if we're going to send RTP, link
2476 * the RTCP send src always */
2477 if (priv->srcpad || i == 1) {
2480 gst_bin_add (bin, priv->udpsink[i]);
2481 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
2486 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2487 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2488 gst_bin_add (bin, priv->appsink[i]);
2489 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2490 &sink_cb, stream, NULL);
2493 if (is_udp && is_tcp) {
2494 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2496 /* make tee for RTP/RTCP */
2497 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2498 gst_bin_add (bin, priv->tee[i]);
2500 /* and link to rtpbin send pad */
2501 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2502 gst_pad_link (priv->send_src[i], pad);
2503 gst_object_unref (pad);
2505 priv->udpqueue[i] = gst_element_factory_make ("queue", NULL);
2506 g_object_set (priv->udpqueue[i], "max-size-buffers",
2507 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0),
2509 gst_bin_add (bin, priv->udpqueue[i]);
2510 /* link tee to udpqueue */
2511 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2512 pad = gst_element_get_static_pad (priv->udpqueue[i], "sink");
2513 gst_pad_link (teepad, pad);
2514 gst_object_unref (pad);
2515 gst_object_unref (teepad);
2517 /* link udpqueue to udpsink */
2518 queuepad = gst_element_get_static_pad (priv->udpqueue[i], "src");
2519 gst_pad_link (queuepad, sinkpad);
2520 gst_object_unref (queuepad);
2521 gst_object_unref (sinkpad);
2524 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
2525 g_object_set (priv->appqueue[i], "max-size-buffers",
2526 1, "max-size-bytes", 0, "max-size-time", G_GINT64_CONSTANT (0),
2528 gst_bin_add (bin, priv->appqueue[i]);
2529 /* and link tee to appqueue */
2530 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2531 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
2532 gst_pad_link (teepad, pad);
2533 gst_object_unref (pad);
2534 gst_object_unref (teepad);
2536 /* and link appqueue to appsink */
2537 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
2538 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2539 gst_pad_link (queuepad, pad);
2540 gst_object_unref (pad);
2541 gst_object_unref (queuepad);
2542 } else if (is_tcp) {
2543 /* only appsink needed, link it to the session */
2544 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2545 gst_pad_link (priv->send_src[i], pad);
2546 gst_object_unref (pad);
2548 /* when its only TCP, we need to set sync and preroll to FALSE
2549 * for the sink to avoid deadlock. And this is only needed for
2550 * sink used for RTCP data, not the RTP data. */
2552 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2554 /* else only udpsink needed, link it to the session */
2555 gst_pad_link (priv->send_src[i], sinkpad);
2556 gst_object_unref (sinkpad);
2560 /* check if we need to set to a special state */
2561 if (state != GST_STATE_NULL) {
2562 if (priv->udpsink[i] && (priv->srcpad || i == 1))
2563 gst_element_set_state (priv->udpsink[i], state);
2564 if (priv->appsink[i] && (priv->srcpad || i == 1))
2565 gst_element_set_state (priv->appsink[i], state);
2566 if (priv->appqueue[i] && (priv->srcpad || i == 1))
2567 gst_element_set_state (priv->appqueue[i], state);
2568 if (priv->udpqueue[i] && (priv->srcpad || i == 1))
2569 gst_element_set_state (priv->udpqueue[i], state);
2570 if (priv->tee[i] && (priv->srcpad || i == 1))
2571 gst_element_set_state (priv->tee[i], state);
2584 /* must be called with lock */
2586 create_receiver_part (GstRTSPStream * stream, GstBin * bin, GstState state)
2588 GstRTSPStreamPrivate *priv;
2589 GstPad *pad, *selpad;
2593 priv = stream->priv;
2595 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2597 for (i = 0; i < 2; i++) {
2598 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
2599 * RTCP sink always */
2600 if (priv->sinkpad || i == 1) {
2601 /* For the receiver we create this bit of pipeline for both
2602 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2603 * and it is all funneled into the rtpbin receive pad.
2605 * .--------. .--------. .--------.
2606 * | udpsrc | | funnel | | rtpbin |
2607 * | src->sink src->sink |
2608 * '--------' | | '--------'
2612 * '--------' '--------'
2614 /* make funnel for the RTP/RTCP receivers */
2615 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2616 gst_bin_add (bin, priv->funnel[i]);
2618 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2619 gst_pad_link (pad, priv->recv_sink[i]);
2620 gst_object_unref (pad);
2623 /* make and add appsrc */
2624 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2625 priv->appsrc_base_time[i] = -1;
2627 gst_element_set_state (priv->appsrc[i], GST_STATE_PLAYING);
2628 gst_element_set_locked_state (priv->appsrc[i], TRUE);
2630 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
2632 gst_bin_add (bin, priv->appsrc[i]);
2633 /* and link to the funnel */
2634 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2635 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2636 gst_pad_link (pad, selpad);
2637 gst_object_unref (pad);
2638 gst_object_unref (selpad);
2642 /* check if we need to set to a special state */
2643 if (state != GST_STATE_NULL) {
2644 if (priv->funnel[i] && (priv->sinkpad || i == 1))
2645 gst_element_set_state (priv->funnel[i], state);
2651 * gst_rtsp_stream_join_bin:
2652 * @stream: a #GstRTSPStream
2653 * @bin: (transfer none): a #GstBin to join
2654 * @rtpbin: (transfer none): a rtpbin element in @bin
2655 * @state: the target state of the new elements
2657 * Join the #GstBin @bin that contains the element @rtpbin.
2659 * @stream will link to @rtpbin, which must be inside @bin. The elements
2660 * added to @bin will be set to the state given in @state.
2662 * Returns: %TRUE on success.
2665 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
2666 GstElement * rtpbin, GstState state)
2668 GstRTSPStreamPrivate *priv;
2671 GstPadLinkReturn ret;
2673 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2674 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2675 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2677 priv = stream->priv;
2679 g_mutex_lock (&priv->lock);
2680 if (priv->is_joined)
2683 /* create a session with the same index as the stream */
2686 GST_INFO ("stream %p joining bin as session %u", stream, idx);
2688 if (priv->profiles & GST_RTSP_PROFILE_SAVP
2689 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2691 g_signal_connect (rtpbin, "request-rtp-encoder",
2692 (GCallback) request_rtp_encoder, stream);
2693 g_signal_connect (rtpbin, "request-rtcp-encoder",
2694 (GCallback) request_rtcp_encoder, stream);
2695 g_signal_connect (rtpbin, "request-rtp-decoder",
2696 (GCallback) request_rtp_rtcp_decoder, stream);
2697 g_signal_connect (rtpbin, "request-rtcp-decoder",
2698 (GCallback) request_rtp_rtcp_decoder, stream);
2701 if (priv->sinkpad) {
2702 g_signal_connect (rtpbin, "request-pt-map",
2703 (GCallback) request_pt_map, stream);
2706 /* get pads from the RTP session element for sending and receiving
2709 /* get a pad for sending RTP */
2710 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2711 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2714 /* link the RTP pad to the session manager, it should not really fail unless
2715 * this is not really an RTP pad */
2716 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2717 if (ret != GST_PAD_LINK_OK)
2720 name = g_strdup_printf ("send_rtp_src_%u", idx);
2721 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2724 /* Need to connect our sinkpad from here */
2725 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2727 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2729 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2730 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2734 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2735 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2737 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2738 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2741 /* get the session */
2742 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2744 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2746 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2748 g_signal_connect (priv->session, "on-ssrc-active",
2749 (GCallback) on_ssrc_active, stream);
2750 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2752 g_signal_connect (priv->session, "on-bye-timeout",
2753 (GCallback) on_bye_timeout, stream);
2754 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2757 /* signal for sender ssrc */
2758 g_signal_connect (priv->session, "on-new-sender-ssrc",
2759 (GCallback) on_new_sender_ssrc, stream);
2760 g_signal_connect (priv->session, "on-sender-ssrc-active",
2761 (GCallback) on_sender_ssrc_active, stream);
2763 if (!create_sender_part (stream, bin, state))
2764 goto no_udp_protocol;
2766 create_receiver_part (stream, bin, state);
2769 /* be notified of caps changes */
2770 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2771 (GCallback) caps_notify, stream);
2774 priv->joined_bin = bin;
2775 priv->is_joined = TRUE;
2776 g_mutex_unlock (&priv->lock);
2783 g_mutex_unlock (&priv->lock);
2788 GST_WARNING ("failed to link stream %u", idx);
2789 gst_object_unref (priv->send_rtp_sink);
2790 priv->send_rtp_sink = NULL;
2791 g_mutex_unlock (&priv->lock);
2796 GST_WARNING ("failed to allocate ports %u", idx);
2797 gst_object_unref (priv->send_rtp_sink);
2798 priv->send_rtp_sink = NULL;
2799 gst_object_unref (priv->send_src[0]);
2800 priv->send_src[0] = NULL;
2801 gst_object_unref (priv->send_src[1]);
2802 priv->send_src[1] = NULL;
2803 gst_object_unref (priv->recv_sink[0]);
2804 priv->recv_sink[0] = NULL;
2805 gst_object_unref (priv->recv_sink[1]);
2806 priv->recv_sink[1] = NULL;
2807 if (priv->udpsink[0])
2808 gst_element_set_state (priv->udpsink[0], GST_STATE_NULL);
2809 if (priv->udpsink[1])
2810 gst_element_set_state (priv->udpsink[1], GST_STATE_NULL);
2812 g_mutex_unlock (&priv->lock);
2817 /* Must be called with priv->lock. */
2819 remove_all_unicast_udpsrcs (GstRTSPStream * stream, GstBin * bin)
2821 GstRTSPStreamPrivate *priv;
2822 GHashTableIter iter;
2823 gpointer iter_key, iter_value;
2825 priv = stream->priv;
2827 /* Remove all of the unicast udpsrcs */
2828 g_hash_table_iter_init (&iter, priv->udpsrcs);
2829 while (g_hash_table_iter_next (&iter, &iter_key, &iter_value)) {
2830 GstRTSPStreamUDPSrcs *transport_udpsrcs =
2831 (GstRTSPStreamUDPSrcs *) iter_value;
2833 for (int i = 0; i < 2; i++) {
2834 if (transport_udpsrcs->udpsrc[i]) {
2835 if (priv->sinkpad || i == 1) {
2836 /* Set udpsrc to NULL now before removing */
2837 gst_element_set_locked_state (transport_udpsrcs->udpsrc[i], FALSE);
2838 gst_element_set_state (transport_udpsrcs->udpsrc[i], GST_STATE_NULL);
2840 /* removing them should also nicely release the request
2841 * pads when they finalize */
2842 gst_bin_remove (bin, transport_udpsrcs->udpsrc[i]);
2844 /* we need to set the state to NULL before unref */
2845 gst_element_set_state (transport_udpsrcs->udpsrc[i], GST_STATE_NULL);
2846 gst_object_unref (transport_udpsrcs->udpsrc[i]);
2852 g_hash_table_remove_all (priv->udpsrcs);
2856 * gst_rtsp_stream_leave_bin:
2857 * @stream: a #GstRTSPStream
2858 * @bin: (transfer none): a #GstBin
2859 * @rtpbin: (transfer none): a rtpbin #GstElement
2861 * Remove the elements of @stream from @bin.
2863 * Return: %TRUE on success.
2866 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2867 GstElement * rtpbin)
2869 GstRTSPStreamPrivate *priv;
2871 gboolean is_tcp, is_udp;
2873 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2874 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2875 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2877 priv = stream->priv;
2879 g_mutex_lock (&priv->lock);
2880 if (!priv->is_joined)
2881 goto was_not_joined;
2883 priv->joined_bin = NULL;
2885 /* all transports must be removed by now */
2886 if (priv->transports != NULL)
2887 goto transports_not_removed;
2889 clear_tr_cache (priv, TRUE);
2890 clear_tr_cache (priv, FALSE);
2892 GST_INFO ("stream %p leaving bin", stream);
2895 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2897 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2898 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2899 gst_object_unref (priv->send_rtp_sink);
2900 priv->send_rtp_sink = NULL;
2901 } else if (priv->recv_rtp_src) {
2902 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2903 gst_object_unref (priv->recv_rtp_src);
2904 priv->recv_rtp_src = NULL;
2907 is_tcp = priv->protocols & GST_RTSP_LOWER_TRANS_TCP;
2909 is_udp = ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
2910 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST));
2912 remove_all_unicast_udpsrcs (stream, bin);
2914 for (i = 0; i < 2; i++) {
2915 if (priv->udpsink[i])
2916 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
2917 if (priv->appsink[i])
2918 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
2919 if (priv->appqueue[i])
2920 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
2921 if (priv->udpqueue[i])
2922 gst_element_set_state (priv->udpqueue[i], GST_STATE_NULL);
2924 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
2925 if (priv->funnel[i])
2926 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
2927 if (priv->appsrc[i])
2928 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2930 if (priv->udpsrc_mcast_v4[i]) {
2931 if (priv->sinkpad || i == 1) {
2932 /* and set udpsrc to NULL now before removing */
2933 gst_element_set_locked_state (priv->udpsrc_mcast_v4[i], FALSE);
2934 gst_element_set_state (priv->udpsrc_mcast_v4[i], GST_STATE_NULL);
2935 /* removing them should also nicely release the request
2936 * pads when they finalize */
2937 gst_bin_remove (bin, priv->udpsrc_mcast_v4[i]);
2939 gst_element_set_state (priv->udpsrc_mcast_v4[i], GST_STATE_NULL);
2940 gst_object_unref (priv->udpsrc_mcast_v4[i]);
2944 if (priv->udpsrc_mcast_v6[i]) {
2945 if (priv->sinkpad || i == 1) {
2946 gst_element_set_locked_state (priv->udpsrc_mcast_v6[i], FALSE);
2947 gst_element_set_state (priv->udpsrc_mcast_v6[i], GST_STATE_NULL);
2948 gst_bin_remove (bin, priv->udpsrc_mcast_v6[i]);
2950 gst_element_set_state (priv->udpsrc_mcast_v6[i], GST_STATE_NULL);
2951 gst_object_unref (priv->udpsrc_mcast_v6[i]);
2955 if (priv->udpsink[i] && is_udp && (priv->srcpad || i == 1))
2956 gst_bin_remove (bin, priv->udpsink[i]);
2957 if (priv->appsrc[i]) {
2958 if (priv->sinkpad || i == 1) {
2959 gst_element_set_locked_state (priv->appsrc[i], FALSE);
2960 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2961 gst_bin_remove (bin, priv->appsrc[i]);
2963 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2964 gst_object_unref (priv->appsrc[i]);
2967 if (priv->appsink[i] && is_tcp && (priv->srcpad || i == 1))
2968 gst_bin_remove (bin, priv->appsink[i]);
2969 if (priv->appqueue[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2970 gst_bin_remove (bin, priv->appqueue[i]);
2971 if (priv->udpqueue[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2972 gst_bin_remove (bin, priv->udpqueue[i]);
2973 if (priv->tee[i] && is_tcp && is_udp && (priv->srcpad || i == 1))
2974 gst_bin_remove (bin, priv->tee[i]);
2975 if (priv->funnel[i] && (priv->sinkpad || i == 1))
2976 gst_bin_remove (bin, priv->funnel[i]);
2978 if (priv->sinkpad || i == 1) {
2979 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2980 gst_object_unref (priv->recv_sink[i]);
2981 priv->recv_sink[i] = NULL;
2984 priv->udpsrc_mcast_v4[i] = NULL;
2985 priv->udpsrc_mcast_v6[i] = NULL;
2986 priv->udpsink[i] = NULL;
2987 priv->appsrc[i] = NULL;
2988 priv->appsink[i] = NULL;
2989 priv->appqueue[i] = NULL;
2990 priv->udpqueue[i] = NULL;
2991 priv->tee[i] = NULL;
2992 priv->funnel[i] = NULL;
2996 gst_object_unref (priv->send_src[0]);
2997 priv->send_src[0] = NULL;
3000 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
3001 gst_object_unref (priv->send_src[1]);
3002 priv->send_src[1] = NULL;
3004 g_object_unref (priv->session);
3005 priv->session = NULL;
3007 gst_caps_unref (priv->caps);
3011 gst_object_unref (priv->srtpenc);
3013 gst_object_unref (priv->srtpdec);
3015 priv->is_joined = FALSE;
3016 g_mutex_unlock (&priv->lock);
3022 g_mutex_unlock (&priv->lock);
3025 transports_not_removed:
3027 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
3028 g_mutex_unlock (&priv->lock);
3034 * gst_rtsp_stream_get_joined_bin:
3035 * @stream: a #GstRTSPStream
3037 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
3039 * Return: (transfer full): the joined bin or NULL.
3042 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
3044 GstRTSPStreamPrivate *priv;
3047 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3049 priv = stream->priv;
3051 g_mutex_lock (&priv->lock);
3052 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
3053 g_mutex_unlock (&priv->lock);
3059 * gst_rtsp_stream_get_rtpinfo:
3060 * @stream: a #GstRTSPStream
3061 * @rtptime: (allow-none): result RTP timestamp
3062 * @seq: (allow-none): result RTP seqnum
3063 * @clock_rate: (allow-none): the clock rate
3064 * @running_time: (allow-none): result running-time
3066 * Retrieve the current rtptime, seq and running-time. This is used to
3067 * construct a RTPInfo reply header.
3069 * Returns: %TRUE when rtptime, seq and running-time could be determined.
3072 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
3073 guint * rtptime, guint * seq, guint * clock_rate,
3074 GstClockTime * running_time)
3076 GstRTSPStreamPrivate *priv;
3077 GstStructure *stats;
3078 GObjectClass *payobjclass;
3080 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3082 priv = stream->priv;
3084 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
3086 g_mutex_lock (&priv->lock);
3088 /* First try to extract the information from the last buffer on the sinks.
3089 * This will have a more accurate sequence number and timestamp, as between
3090 * the payloader and the sink there can be some queues
3092 if (priv->udpsink[0] || priv->appsink[0]) {
3093 GstSample *last_sample;
3095 if (priv->udpsink[0])
3096 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
3098 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
3103 GstSegment *segment;
3104 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
3106 caps = gst_sample_get_caps (last_sample);
3107 buffer = gst_sample_get_buffer (last_sample);
3108 segment = gst_sample_get_segment (last_sample);
3110 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3112 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3116 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3119 gst_rtp_buffer_unmap (&rtp_buffer);
3123 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3124 GST_BUFFER_TIMESTAMP (buffer));
3128 GstStructure *s = gst_caps_get_structure (caps, 0);
3130 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3132 if (*clock_rate == 0 && running_time)
3133 *running_time = GST_CLOCK_TIME_NONE;
3135 gst_sample_unref (last_sample);
3139 gst_sample_unref (last_sample);
3144 if (g_object_class_find_property (payobjclass, "stats")) {
3145 g_object_get (priv->payloader, "stats", &stats, NULL);
3150 gst_structure_get_uint (stats, "seqnum", seq);
3153 gst_structure_get_uint (stats, "timestamp", rtptime);
3156 gst_structure_get_clock_time (stats, "running-time", running_time);
3159 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3160 if (*clock_rate == 0 && running_time)
3161 *running_time = GST_CLOCK_TIME_NONE;
3163 gst_structure_free (stats);
3165 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3166 !g_object_class_find_property (payobjclass, "timestamp"))
3170 g_object_get (priv->payloader, "seqnum", seq, NULL);
3173 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3176 *running_time = GST_CLOCK_TIME_NONE;
3180 g_mutex_unlock (&priv->lock);
3187 GST_WARNING ("Could not get payloader stats");
3188 g_mutex_unlock (&priv->lock);
3194 * gst_rtsp_stream_get_caps:
3195 * @stream: a #GstRTSPStream
3197 * Retrieve the current caps of @stream.
3199 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
3203 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3205 GstRTSPStreamPrivate *priv;
3208 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3210 priv = stream->priv;
3212 g_mutex_lock (&priv->lock);
3213 if ((result = priv->caps))
3214 gst_caps_ref (result);
3215 g_mutex_unlock (&priv->lock);
3221 * gst_rtsp_stream_recv_rtp:
3222 * @stream: a #GstRTSPStream
3223 * @buffer: (transfer full): a #GstBuffer
3225 * Handle an RTP buffer for the stream. This method is usually called when a
3226 * message has been received from a client using the TCP transport.
3228 * This function takes ownership of @buffer.
3230 * Returns: a GstFlowReturn.
3233 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3235 GstRTSPStreamPrivate *priv;
3237 GstElement *element;
3239 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3240 priv = stream->priv;
3241 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3242 g_return_val_if_fail (priv->is_joined, FALSE);
3244 g_mutex_lock (&priv->lock);
3245 if (priv->appsrc[0])
3246 element = gst_object_ref (priv->appsrc[0]);
3249 g_mutex_unlock (&priv->lock);
3252 if (priv->appsrc_base_time[0] == -1) {
3253 /* Take current running_time. This timestamp will be put on
3254 * the first buffer of each stream because we are a live source and so we
3255 * timestamp with the running_time. When we are dealing with TCP, we also
3256 * only timestamp the first buffer (using the DISCONT flag) because a server
3257 * typically bursts data, for which we don't want to compensate by speeding
3258 * up the media. The other timestamps will be interpollated from this one
3259 * using the RTP timestamps. */
3260 GST_OBJECT_LOCK (element);
3261 if (GST_ELEMENT_CLOCK (element)) {
3263 GstClockTime base_time;
3265 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3266 base_time = GST_ELEMENT_CAST (element)->base_time;
3268 priv->appsrc_base_time[0] = now - base_time;
3269 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3270 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3271 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3272 GST_TIME_ARGS (base_time));
3274 GST_OBJECT_UNLOCK (element);
3277 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3278 gst_object_unref (element);
3286 * gst_rtsp_stream_recv_rtcp:
3287 * @stream: a #GstRTSPStream
3288 * @buffer: (transfer full): a #GstBuffer
3290 * Handle an RTCP buffer for the stream. This method is usually called when a
3291 * message has been received from a client using the TCP transport.
3293 * This function takes ownership of @buffer.
3295 * Returns: a GstFlowReturn.
3298 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3300 GstRTSPStreamPrivate *priv;
3302 GstElement *element;
3304 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3305 priv = stream->priv;
3306 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3308 if (!priv->is_joined) {
3309 gst_buffer_unref (buffer);
3310 return GST_FLOW_NOT_LINKED;
3312 g_mutex_lock (&priv->lock);
3313 if (priv->appsrc[1])
3314 element = gst_object_ref (priv->appsrc[1]);
3317 g_mutex_unlock (&priv->lock);
3320 if (priv->appsrc_base_time[1] == -1) {
3321 /* Take current running_time. This timestamp will be put on
3322 * the first buffer of each stream because we are a live source and so we
3323 * timestamp with the running_time. When we are dealing with TCP, we also
3324 * only timestamp the first buffer (using the DISCONT flag) because a server
3325 * typically bursts data, for which we don't want to compensate by speeding
3326 * up the media. The other timestamps will be interpollated from this one
3327 * using the RTP timestamps. */
3328 GST_OBJECT_LOCK (element);
3329 if (GST_ELEMENT_CLOCK (element)) {
3331 GstClockTime base_time;
3333 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3334 base_time = GST_ELEMENT_CAST (element)->base_time;
3336 priv->appsrc_base_time[1] = now - base_time;
3337 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3338 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3339 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3340 GST_TIME_ARGS (base_time));
3342 GST_OBJECT_UNLOCK (element);
3345 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3346 gst_object_unref (element);
3349 gst_buffer_unref (buffer);
3354 /* Properly dispose udpsrcs that were created for a given transport. */
3355 /* Must be called with priv->lock. */
3357 remove_transport_udpsrcs (GstRTSPStreamPrivate * priv,
3358 const GstRTSPTransport * tr)
3360 /* Remove the udpsrcs associated with this transport. */
3361 GstRTSPStreamUDPSrcs *transport_udpsrcs =
3362 g_hash_table_lookup (priv->udpsrcs, tr);
3363 if (transport_udpsrcs != NULL) {
3364 for (int i = 0; i < 2; i++) {
3365 if (transport_udpsrcs->udpsrc[i]) {
3366 if (priv->sinkpad || i == 1) {
3368 GstPad *udpsrc_srcpad, *funnel_sinkpad;
3370 /* We know these udpsrcs are all linked to funnels. Explicitely
3371 * get the funnel src pads so we can properly release them. */
3373 gst_element_get_static_pad (transport_udpsrcs->udpsrc[i], "src");
3374 funnel_sinkpad = gst_pad_get_peer (udpsrc_srcpad);
3376 if (funnel_sinkpad != NULL) {
3377 /* Unlink pads and release funnel's request pad. */
3378 gst_pad_unlink (udpsrc_srcpad, funnel_sinkpad);
3379 gst_element_release_request_pad (priv->funnel[i], funnel_sinkpad);
3380 gst_object_unref (funnel_sinkpad);
3382 gst_object_unref (udpsrc_srcpad);
3384 /* Set udpsrc to NULL now before removing */
3385 gst_element_set_locked_state (transport_udpsrcs->udpsrc[i], FALSE);
3386 gst_element_set_state (transport_udpsrcs->udpsrc[i], GST_STATE_NULL);
3388 /* This udpsrc is expected to be owned by a bin. Get the bin and
3389 * remove our element. */
3390 bin = GST_BIN (gst_element_get_parent (transport_udpsrcs->udpsrc[i]));
3392 gst_bin_remove (bin, transport_udpsrcs->udpsrc[i]);
3393 gst_object_unref (bin);
3395 GST_ERROR ("Expected this udpsrc element to be part of a bin.");
3396 gst_object_unref (transport_udpsrcs->udpsrc[i]);
3400 /* we need to set the state to NULL before unref */
3401 gst_element_set_state (transport_udpsrcs->udpsrc[i], GST_STATE_NULL);
3402 gst_object_unref (transport_udpsrcs->udpsrc[i]);
3407 /* The udpsrcs are now properly cleaned up. Remove them from the table */
3408 g_hash_table_remove (priv->udpsrcs, tr);
3411 /* This can happen if we're dealing with a multicast transport. */
3412 GST_INFO ("Could not find udpsrcs associated with this transport.");
3416 /* must be called with lock */
3418 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3421 GstRTSPStreamPrivate *priv = stream->priv;
3422 const GstRTSPTransport *tr;
3424 tr = gst_rtsp_stream_transport_get_transport (trans);
3426 switch (tr->lower_transport) {
3427 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3428 case GST_RTSP_LOWER_TRANS_UDP:
3434 dest = tr->destination;
3435 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3439 } else if (priv->client_side) {
3440 /* In client side mode the 'destination' is the RTSP server, so send
3442 min = tr->server_port.min;
3443 max = tr->server_port.max;
3445 min = tr->client_port.min;
3446 max = tr->client_port.max;
3451 GST_INFO ("setting ttl-mc %d", ttl);
3452 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
3453 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
3455 GST_INFO ("adding %s:%d-%d", dest, min, max);
3456 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
3457 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
3458 priv->transports = g_list_prepend (priv->transports, trans);
3460 GST_INFO ("removing %s:%d-%d", dest, min, max);
3461 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
3462 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
3463 priv->transports = g_list_remove (priv->transports, trans);
3465 remove_transport_udpsrcs (priv, tr);
3467 priv->transports_cookie++;
3470 case GST_RTSP_LOWER_TRANS_TCP:
3472 GST_INFO ("adding TCP %s", tr->destination);
3473 priv->transports = g_list_prepend (priv->transports, trans);
3475 GST_INFO ("removing TCP %s", tr->destination);
3476 priv->transports = g_list_remove (priv->transports, trans);
3478 priv->transports_cookie++;
3481 goto unknown_transport;
3488 GST_INFO ("Unknown transport %d", tr->lower_transport);
3495 * gst_rtsp_stream_add_transport:
3496 * @stream: a #GstRTSPStream
3497 * @trans: (transfer none): a #GstRTSPStreamTransport
3499 * Add the transport in @trans to @stream. The media of @stream will
3500 * then also be send to the values configured in @trans.
3502 * @stream must be joined to a bin.
3504 * @trans must contain a valid #GstRTSPTransport.
3506 * Returns: %TRUE if @trans was added
3509 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
3510 GstRTSPStreamTransport * trans)
3512 GstRTSPStreamPrivate *priv;
3515 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3516 priv = stream->priv;
3517 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3518 g_return_val_if_fail (priv->is_joined, FALSE);
3520 g_mutex_lock (&priv->lock);
3521 res = update_transport (stream, trans, TRUE);
3522 g_mutex_unlock (&priv->lock);
3528 * gst_rtsp_stream_remove_transport:
3529 * @stream: a #GstRTSPStream
3530 * @trans: (transfer none): a #GstRTSPStreamTransport
3532 * Remove the transport in @trans from @stream. The media of @stream will
3533 * not be sent to the values configured in @trans.
3535 * @stream must be joined to a bin.
3537 * @trans must contain a valid #GstRTSPTransport.
3539 * Returns: %TRUE if @trans was removed
3542 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
3543 GstRTSPStreamTransport * trans)
3545 GstRTSPStreamPrivate *priv;
3548 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3549 priv = stream->priv;
3550 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
3551 g_return_val_if_fail (priv->is_joined, FALSE);
3553 g_mutex_lock (&priv->lock);
3554 res = update_transport (stream, trans, FALSE);
3555 g_mutex_unlock (&priv->lock);
3561 * gst_rtsp_stream_update_crypto:
3562 * @stream: a #GstRTSPStream
3564 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
3566 * Update the new crypto information for @ssrc in @stream. If information
3567 * for @ssrc did not exist, it will be added. If information
3568 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
3569 * be removed from @stream.
3571 * Returns: %TRUE if @crypto could be updated
3574 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
3575 guint ssrc, GstCaps * crypto)
3577 GstRTSPStreamPrivate *priv;
3579 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3580 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
3582 priv = stream->priv;
3584 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
3586 g_mutex_lock (&priv->lock);
3588 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
3589 gst_caps_ref (crypto));
3591 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
3592 g_mutex_unlock (&priv->lock);
3598 * gst_rtsp_stream_get_rtp_socket:
3599 * @stream: a #GstRTSPStream
3600 * @family: the socket family
3602 * Get the RTP socket from @stream for a @family.
3604 * @stream must be joined to a bin.
3606 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
3607 * socket could be allocated for @family. Unref after usage
3610 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
3612 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3616 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3617 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3618 family == G_SOCKET_FAMILY_IPV6, NULL);
3619 g_return_val_if_fail (priv->udpsink[0], NULL);
3621 if (family == G_SOCKET_FAMILY_IPV6)
3626 g_object_get (priv->udpsink[0], name, &socket, NULL);
3632 * gst_rtsp_stream_get_rtcp_socket:
3633 * @stream: a #GstRTSPStream
3634 * @family: the socket family
3636 * Get the RTCP socket from @stream for a @family.
3638 * @stream must be joined to a bin.
3640 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
3641 * socket could be allocated for @family. Unref after usage
3644 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
3646 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
3650 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3651 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
3652 family == G_SOCKET_FAMILY_IPV6, NULL);
3653 g_return_val_if_fail (priv->udpsink[1], NULL);
3655 if (family == G_SOCKET_FAMILY_IPV6)
3660 g_object_get (priv->udpsink[1], name, &socket, NULL);
3666 * gst_rtsp_stream_set_seqnum:
3667 * @stream: a #GstRTSPStream
3668 * @seqnum: a new sequence number
3670 * Configure the sequence number in the payloader of @stream to @seqnum.
3673 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
3675 GstRTSPStreamPrivate *priv;
3677 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
3679 priv = stream->priv;
3681 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
3685 * gst_rtsp_stream_get_seqnum:
3686 * @stream: a #GstRTSPStream
3688 * Get the configured sequence number in the payloader of @stream.
3690 * Returns: the sequence number of the payloader.
3693 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
3695 GstRTSPStreamPrivate *priv;
3698 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
3700 priv = stream->priv;
3702 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
3708 * gst_rtsp_stream_transport_filter:
3709 * @stream: a #GstRTSPStream
3710 * @func: (scope call) (allow-none): a callback
3711 * @user_data: (closure): user data passed to @func
3713 * Call @func for each transport managed by @stream. The result value of @func
3714 * determines what happens to the transport. @func will be called with @stream
3715 * locked so no further actions on @stream can be performed from @func.
3717 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
3720 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
3722 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
3723 * will also be added with an additional ref to the result #GList of this
3726 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
3728 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
3729 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
3730 * element in the #GList should be unreffed before the list is freed.
3733 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
3734 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
3736 GstRTSPStreamPrivate *priv;
3737 GList *result, *walk, *next;
3738 GHashTable *visited = NULL;
3741 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3743 priv = stream->priv;
3747 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3749 g_mutex_lock (&priv->lock);
3751 cookie = priv->transports_cookie;
3752 for (walk = priv->transports; walk; walk = next) {
3753 GstRTSPStreamTransport *trans = walk->data;
3754 GstRTSPFilterResult res;
3757 next = g_list_next (walk);
3760 /* only visit each transport once */
3761 if (g_hash_table_contains (visited, trans))
3764 g_hash_table_add (visited, g_object_ref (trans));
3765 g_mutex_unlock (&priv->lock);
3767 res = func (stream, trans, user_data);
3769 g_mutex_lock (&priv->lock);
3771 res = GST_RTSP_FILTER_REF;
3773 changed = (cookie != priv->transports_cookie);
3776 case GST_RTSP_FILTER_REMOVE:
3777 update_transport (stream, trans, FALSE);
3779 case GST_RTSP_FILTER_REF:
3780 result = g_list_prepend (result, g_object_ref (trans));
3782 case GST_RTSP_FILTER_KEEP:
3789 g_mutex_unlock (&priv->lock);
3792 g_hash_table_unref (visited);
3797 static GstPadProbeReturn
3798 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3800 GstRTSPStreamPrivate *priv;
3801 GstRTSPStream *stream;
3804 priv = stream->priv;
3806 GST_DEBUG_OBJECT (pad, "now blocking");
3808 g_mutex_lock (&priv->lock);
3809 priv->blocking = TRUE;
3810 g_mutex_unlock (&priv->lock);
3812 gst_element_post_message (priv->payloader,
3813 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3814 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3816 return GST_PAD_PROBE_OK;
3820 * gst_rtsp_stream_set_blocked:
3821 * @stream: a #GstRTSPStream
3822 * @blocked: boolean indicating we should block or unblock
3824 * Blocks or unblocks the dataflow on @stream.
3826 * Returns: %TRUE on success
3829 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3831 GstRTSPStreamPrivate *priv;
3833 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3835 priv = stream->priv;
3837 g_mutex_lock (&priv->lock);
3839 priv->blocking = FALSE;
3840 if (priv->blocked_id == 0) {
3841 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3842 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3843 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3844 g_object_ref (stream), g_object_unref);
3847 if (priv->blocked_id != 0) {
3848 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3849 priv->blocked_id = 0;
3850 priv->blocking = FALSE;
3853 g_mutex_unlock (&priv->lock);
3859 * gst_rtsp_stream_is_blocking:
3860 * @stream: a #GstRTSPStream
3862 * Check if @stream is blocking on a #GstBuffer.
3864 * Returns: %TRUE if @stream is blocking
3867 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3869 GstRTSPStreamPrivate *priv;
3872 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3874 priv = stream->priv;
3876 g_mutex_lock (&priv->lock);
3877 result = priv->blocking;
3878 g_mutex_unlock (&priv->lock);
3884 * gst_rtsp_stream_query_position:
3885 * @stream: a #GstRTSPStream
3887 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3888 * the RTP parts of the pipeline and not the RTCP parts.
3890 * Returns: %TRUE if the position could be queried
3893 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3895 GstRTSPStreamPrivate *priv;
3899 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3901 priv = stream->priv;
3903 g_mutex_lock (&priv->lock);
3904 /* depending on the transport type, it should query corresponding sink */
3905 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3906 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3907 sink = priv->udpsink[0];
3909 sink = priv->appsink[0];
3912 gst_object_ref (sink);
3913 g_mutex_unlock (&priv->lock);
3918 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3919 gst_object_unref (sink);
3925 * gst_rtsp_stream_query_stop:
3926 * @stream: a #GstRTSPStream
3928 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3929 * the RTP parts of the pipeline and not the RTCP parts.
3931 * Returns: %TRUE if the stop could be queried
3934 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3936 GstRTSPStreamPrivate *priv;
3941 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3943 priv = stream->priv;
3945 g_mutex_lock (&priv->lock);
3946 /* depending on the transport type, it should query corresponding sink */
3947 if ((priv->protocols & GST_RTSP_LOWER_TRANS_UDP) ||
3948 (priv->protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST))
3949 sink = priv->udpsink[0];
3951 sink = priv->appsink[0];
3954 gst_object_ref (sink);
3955 g_mutex_unlock (&priv->lock);
3960 query = gst_query_new_segment (GST_FORMAT_TIME);
3961 if ((ret = gst_element_query (sink, query))) {
3964 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3965 if (format != GST_FORMAT_TIME)
3968 gst_query_unref (query);
3969 gst_object_unref (sink);