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 struct _GstRTSPStreamPrivate
66 /* Only one pad is ever set */
67 GstPad *srcpad, *sinkpad;
68 GstElement *payloader;
72 /* TRUE if this stream is running on
73 * the client side of an RTSP link (for RECORD) */
77 /* TRUE if stream is complete. This means that the receiver and the sender
78 * parts are present in the stream. */
80 GstRTSPProfile profiles;
81 GstRTSPLowerTrans allowed_protocols;
82 GstRTSPLowerTrans configured_protocols;
84 /* pads on the rtpbin */
85 GstPad *send_rtp_sink;
90 /* the RTPSession object */
93 /* SRTP encoder/decoder */
99 GstElement *udpsrc_v4[2];
100 GstElement *udpsrc_v6[2];
101 GstElement *udpqueue[2];
102 GstElement *udpsink[2];
103 GSocket *socket_v4[2];
104 GSocket *socket_v6[2];
106 /* for UDP multicast */
107 GstElement *mcast_udpsrc_v4[2];
108 GstElement *mcast_udpsrc_v6[2];
109 GstElement *mcast_udpqueue[2];
110 GstElement *mcast_udpsink[2];
111 GSocket *mcast_socket_v4[2];
112 GSocket *mcast_socket_v6[2];
114 /* for TCP transport */
115 GstElement *appsrc[2];
116 GstClockTime appsrc_base_time[2];
117 GstElement *appqueue[2];
118 GstElement *appsink[2];
121 GstElement *funnel[2];
125 GstElement *rtxreceive;
127 GstClockTime rtx_time;
129 /* Forward Error Correction with RFC 5109 */
130 GstElement *ulpfec_decoder;
131 GstElement *ulpfec_encoder;
133 gboolean ulpfec_enabled;
134 guint ulpfec_percentage;
136 /* pool used to manage unicast and multicast addresses */
137 GstRTSPAddressPool *pool;
139 /* unicast server addr/port */
140 GstRTSPAddress *server_addr_v4;
141 GstRTSPAddress *server_addr_v6;
143 /* multicast addresses */
144 GstRTSPAddress *mcast_addr_v4;
145 GstRTSPAddress *mcast_addr_v6;
147 gchar *multicast_iface;
149 /* the caps of the stream */
153 /* transports we stream to */
156 guint transports_cookie;
158 GList *tr_cache_rtcp;
159 guint tr_cache_cookie_rtp;
160 guint tr_cache_cookie_rtcp;
161 guint n_tcp_transports;
162 gboolean have_buffer[2];
167 /* stream blocking */
168 gulong blocked_id[2];
171 /* current stream postion */
172 GstClockTime position;
174 /* pt->caps map for RECORD streams */
177 GstRTSPPublishClockMode publish_clock_mode;
180 #define DEFAULT_CONTROL NULL
181 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
182 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
183 GST_RTSP_LOWER_TRANS_TCP
196 SIGNAL_NEW_RTP_ENCODER,
197 SIGNAL_NEW_RTCP_ENCODER,
198 SIGNAL_NEW_RTP_RTCP_DECODER,
202 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
203 #define GST_CAT_DEFAULT rtsp_stream_debug
205 static GQuark ssrc_stream_map_key;
207 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
208 GValue * value, GParamSpec * pspec);
209 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
210 const GValue * value, GParamSpec * pspec);
212 static void gst_rtsp_stream_finalize (GObject * obj);
215 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
218 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
220 G_DEFINE_TYPE_WITH_PRIVATE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
223 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
225 GObjectClass *gobject_class;
227 gobject_class = G_OBJECT_CLASS (klass);
229 gobject_class->get_property = gst_rtsp_stream_get_property;
230 gobject_class->set_property = gst_rtsp_stream_set_property;
231 gobject_class->finalize = gst_rtsp_stream_finalize;
233 g_object_class_install_property (gobject_class, PROP_CONTROL,
234 g_param_spec_string ("control", "Control",
235 "The control string for this stream", DEFAULT_CONTROL,
236 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (gobject_class, PROP_PROFILES,
239 g_param_spec_flags ("profiles", "Profiles",
240 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
241 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
243 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
244 g_param_spec_flags ("protocols", "Protocols",
245 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
246 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
248 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
249 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
250 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
251 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
253 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
254 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
255 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
256 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
258 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_RTCP_DECODER] =
259 g_signal_new ("new-rtp-rtcp-decoder", G_TYPE_FROM_CLASS (klass),
260 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
261 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
263 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
265 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
269 gst_rtsp_stream_init (GstRTSPStream * stream)
271 GstRTSPStreamPrivate *priv = gst_rtsp_stream_get_instance_private (stream);
273 GST_DEBUG ("new stream %p", stream);
278 priv->control = g_strdup (DEFAULT_CONTROL);
279 priv->profiles = DEFAULT_PROFILES;
280 priv->allowed_protocols = DEFAULT_PROTOCOLS;
281 priv->configured_protocols = 0;
282 priv->publish_clock_mode = GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK;
284 g_mutex_init (&priv->lock);
286 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
287 NULL, (GDestroyNotify) gst_caps_unref);
288 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
289 (GDestroyNotify) gst_caps_unref);
293 gst_rtsp_stream_finalize (GObject * obj)
295 GstRTSPStream *stream;
296 GstRTSPStreamPrivate *priv;
299 stream = GST_RTSP_STREAM (obj);
302 GST_DEBUG ("finalize stream %p", stream);
304 /* we really need to be unjoined now */
305 g_return_if_fail (priv->joined_bin == NULL);
307 if (priv->mcast_addr_v4)
308 gst_rtsp_address_free (priv->mcast_addr_v4);
309 if (priv->mcast_addr_v6)
310 gst_rtsp_address_free (priv->mcast_addr_v6);
311 if (priv->server_addr_v4)
312 gst_rtsp_address_free (priv->server_addr_v4);
313 if (priv->server_addr_v6)
314 gst_rtsp_address_free (priv->server_addr_v6);
316 g_object_unref (priv->pool);
318 g_object_unref (priv->rtxsend);
319 if (priv->rtxreceive)
320 g_object_unref (priv->rtxreceive);
321 if (priv->ulpfec_encoder)
322 gst_object_unref (priv->ulpfec_encoder);
323 if (priv->ulpfec_decoder)
324 gst_object_unref (priv->ulpfec_decoder);
326 for (i = 0; i < 2; i++) {
327 if (priv->socket_v4[i])
328 g_object_unref (priv->socket_v4[i]);
329 if (priv->socket_v6[i])
330 g_object_unref (priv->socket_v6[i]);
331 if (priv->mcast_socket_v4[i])
332 g_object_unref (priv->mcast_socket_v4[i]);
333 if (priv->mcast_socket_v6[i])
334 g_object_unref (priv->mcast_socket_v6[i]);
337 g_free (priv->multicast_iface);
339 gst_object_unref (priv->payloader);
341 gst_object_unref (priv->srcpad);
343 gst_object_unref (priv->sinkpad);
344 g_free (priv->control);
345 g_mutex_clear (&priv->lock);
347 g_hash_table_unref (priv->keys);
348 g_hash_table_destroy (priv->ptmap);
350 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
354 gst_rtsp_stream_get_property (GObject * object, guint propid,
355 GValue * value, GParamSpec * pspec)
357 GstRTSPStream *stream = GST_RTSP_STREAM (object);
361 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
364 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
367 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
370 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
375 gst_rtsp_stream_set_property (GObject * object, guint propid,
376 const GValue * value, GParamSpec * pspec)
378 GstRTSPStream *stream = GST_RTSP_STREAM (object);
382 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
385 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
388 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
396 * gst_rtsp_stream_new:
399 * @payloader: a #GstElement
401 * Create a new media stream with index @idx that handles RTP data on
402 * @pad and has a payloader element @payloader if @pad is a source pad
403 * or a depayloader element @payloader if @pad is a sink pad.
405 * Returns: (transfer full): a new #GstRTSPStream
408 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
410 GstRTSPStreamPrivate *priv;
411 GstRTSPStream *stream;
413 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
414 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
416 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
419 priv->payloader = gst_object_ref (payloader);
420 if (GST_PAD_IS_SRC (pad))
421 priv->srcpad = gst_object_ref (pad);
423 priv->sinkpad = gst_object_ref (pad);
429 * gst_rtsp_stream_get_index:
430 * @stream: a #GstRTSPStream
432 * Get the stream index.
434 * Return: the stream index.
437 gst_rtsp_stream_get_index (GstRTSPStream * stream)
439 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
441 return stream->priv->idx;
445 * gst_rtsp_stream_get_pt:
446 * @stream: a #GstRTSPStream
448 * Get the stream payload type.
450 * Return: the stream payload type.
453 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
455 GstRTSPStreamPrivate *priv;
458 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
462 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
468 * gst_rtsp_stream_get_srcpad:
469 * @stream: a #GstRTSPStream
471 * Get the srcpad associated with @stream.
473 * Returns: (transfer full) (nullable): the srcpad. Unref after usage.
476 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
478 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
480 if (!stream->priv->srcpad)
483 return gst_object_ref (stream->priv->srcpad);
487 * gst_rtsp_stream_get_sinkpad:
488 * @stream: a #GstRTSPStream
490 * Get the sinkpad associated with @stream.
492 * Returns: (transfer full) (nullable): the sinkpad. Unref after usage.
495 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
497 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
499 if (!stream->priv->sinkpad)
502 return gst_object_ref (stream->priv->sinkpad);
506 * gst_rtsp_stream_get_control:
507 * @stream: a #GstRTSPStream
509 * Get the control string to identify this stream.
511 * Returns: (transfer full) (nullable): the control string. g_free() after usage.
514 gst_rtsp_stream_get_control (GstRTSPStream * stream)
516 GstRTSPStreamPrivate *priv;
519 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
523 g_mutex_lock (&priv->lock);
524 if ((result = g_strdup (priv->control)) == NULL)
525 result = g_strdup_printf ("stream=%u", priv->idx);
526 g_mutex_unlock (&priv->lock);
532 * gst_rtsp_stream_set_control:
533 * @stream: a #GstRTSPStream
534 * @control: (nullable): a control string
536 * Set the control string in @stream.
539 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
541 GstRTSPStreamPrivate *priv;
543 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
547 g_mutex_lock (&priv->lock);
548 g_free (priv->control);
549 priv->control = g_strdup (control);
550 g_mutex_unlock (&priv->lock);
554 * gst_rtsp_stream_has_control:
555 * @stream: a #GstRTSPStream
556 * @control: (nullable): a control string
558 * Check if @stream has the control string @control.
560 * Returns: %TRUE is @stream has @control as the control string
563 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
565 GstRTSPStreamPrivate *priv;
568 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
572 g_mutex_lock (&priv->lock);
574 res = (g_strcmp0 (priv->control, control) == 0);
578 if (sscanf (control, "stream=%u", &streamid) > 0)
579 res = (streamid == priv->idx);
583 g_mutex_unlock (&priv->lock);
589 * gst_rtsp_stream_set_mtu:
590 * @stream: a #GstRTSPStream
593 * Configure the mtu in the payloader of @stream to @mtu.
596 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
598 GstRTSPStreamPrivate *priv;
600 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
604 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
606 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
610 * gst_rtsp_stream_get_mtu:
611 * @stream: a #GstRTSPStream
613 * Get the configured MTU in the payloader of @stream.
615 * Returns: the MTU of the payloader.
618 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
620 GstRTSPStreamPrivate *priv;
623 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
627 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
632 /* Update the dscp qos property on the udp sinks */
634 update_dscp_qos (GstRTSPStream * stream, GstElement ** udpsink)
636 GstRTSPStreamPrivate *priv;
641 g_object_set (G_OBJECT (*udpsink), "qos-dscp", priv->dscp_qos, NULL);
646 * gst_rtsp_stream_set_dscp_qos:
647 * @stream: a #GstRTSPStream
648 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
650 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
653 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
655 GstRTSPStreamPrivate *priv;
657 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
661 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
663 if (dscp_qos < -1 || dscp_qos > 63) {
664 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
668 priv->dscp_qos = dscp_qos;
670 update_dscp_qos (stream, priv->udpsink);
674 * gst_rtsp_stream_get_dscp_qos:
675 * @stream: a #GstRTSPStream
677 * Get the configured DSCP QoS in of the outgoing sockets.
679 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
682 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
684 GstRTSPStreamPrivate *priv;
686 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
690 return priv->dscp_qos;
694 * gst_rtsp_stream_is_transport_supported:
695 * @stream: a #GstRTSPStream
696 * @transport: (transfer none): a #GstRTSPTransport
698 * Check if @transport can be handled by stream
700 * Returns: %TRUE if @transport can be handled by @stream.
703 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
704 GstRTSPTransport * transport)
706 GstRTSPStreamPrivate *priv;
708 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
709 g_return_val_if_fail (transport != NULL, FALSE);
713 g_mutex_lock (&priv->lock);
714 if (transport->trans != GST_RTSP_TRANS_RTP)
715 goto unsupported_transmode;
717 if (!(transport->profile & priv->profiles))
718 goto unsupported_profile;
720 if (!(transport->lower_transport & priv->allowed_protocols))
721 goto unsupported_ltrans;
723 g_mutex_unlock (&priv->lock);
728 unsupported_transmode:
730 GST_DEBUG ("unsupported transport mode %d", transport->trans);
731 g_mutex_unlock (&priv->lock);
736 GST_DEBUG ("unsupported profile %d", transport->profile);
737 g_mutex_unlock (&priv->lock);
742 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
743 g_mutex_unlock (&priv->lock);
749 * gst_rtsp_stream_set_profiles:
750 * @stream: a #GstRTSPStream
751 * @profiles: the new profiles
753 * Configure the allowed profiles for @stream.
756 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
758 GstRTSPStreamPrivate *priv;
760 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
764 g_mutex_lock (&priv->lock);
765 priv->profiles = profiles;
766 g_mutex_unlock (&priv->lock);
770 * gst_rtsp_stream_get_profiles:
771 * @stream: a #GstRTSPStream
773 * Get the allowed profiles of @stream.
775 * Returns: a #GstRTSPProfile
778 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
780 GstRTSPStreamPrivate *priv;
783 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
787 g_mutex_lock (&priv->lock);
788 res = priv->profiles;
789 g_mutex_unlock (&priv->lock);
795 * gst_rtsp_stream_set_protocols:
796 * @stream: a #GstRTSPStream
797 * @protocols: the new flags
799 * Configure the allowed lower transport for @stream.
802 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
803 GstRTSPLowerTrans protocols)
805 GstRTSPStreamPrivate *priv;
807 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
811 g_mutex_lock (&priv->lock);
812 priv->allowed_protocols = protocols;
813 g_mutex_unlock (&priv->lock);
817 * gst_rtsp_stream_get_protocols:
818 * @stream: a #GstRTSPStream
820 * Get the allowed protocols of @stream.
822 * Returns: a #GstRTSPLowerTrans
825 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
827 GstRTSPStreamPrivate *priv;
828 GstRTSPLowerTrans res;
830 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
831 GST_RTSP_LOWER_TRANS_UNKNOWN);
835 g_mutex_lock (&priv->lock);
836 res = priv->allowed_protocols;
837 g_mutex_unlock (&priv->lock);
843 * gst_rtsp_stream_set_address_pool:
844 * @stream: a #GstRTSPStream
845 * @pool: (transfer none) (nullable): a #GstRTSPAddressPool
847 * configure @pool to be used as the address pool of @stream.
850 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
851 GstRTSPAddressPool * pool)
853 GstRTSPStreamPrivate *priv;
854 GstRTSPAddressPool *old;
856 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
860 GST_LOG_OBJECT (stream, "set address pool %p", pool);
862 g_mutex_lock (&priv->lock);
863 if ((old = priv->pool) != pool)
864 priv->pool = pool ? g_object_ref (pool) : NULL;
867 g_mutex_unlock (&priv->lock);
870 g_object_unref (old);
874 * gst_rtsp_stream_get_address_pool:
875 * @stream: a #GstRTSPStream
877 * Get the #GstRTSPAddressPool used as the address pool of @stream.
879 * Returns: (transfer full) (nullable): the #GstRTSPAddressPool of @stream.
880 * g_object_unref() after usage.
883 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
885 GstRTSPStreamPrivate *priv;
886 GstRTSPAddressPool *result;
888 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
892 g_mutex_lock (&priv->lock);
893 if ((result = priv->pool))
894 g_object_ref (result);
895 g_mutex_unlock (&priv->lock);
901 * gst_rtsp_stream_set_multicast_iface:
902 * @stream: a #GstRTSPStream
903 * @multicast_iface: (transfer none) (nullable): a multicast interface name
905 * configure @multicast_iface to be used for @stream.
908 gst_rtsp_stream_set_multicast_iface (GstRTSPStream * stream,
909 const gchar * multicast_iface)
911 GstRTSPStreamPrivate *priv;
914 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
918 GST_LOG_OBJECT (stream, "set multicast iface %s",
919 GST_STR_NULL (multicast_iface));
921 g_mutex_lock (&priv->lock);
922 if ((old = priv->multicast_iface) != multicast_iface)
923 priv->multicast_iface = multicast_iface ? g_strdup (multicast_iface) : NULL;
926 g_mutex_unlock (&priv->lock);
933 * gst_rtsp_stream_get_multicast_iface:
934 * @stream: a #GstRTSPStream
936 * Get the multicast interface used for @stream.
938 * Returns: (transfer full) (nullable): the multicast interface for @stream.
939 * g_free() after usage.
942 gst_rtsp_stream_get_multicast_iface (GstRTSPStream * stream)
944 GstRTSPStreamPrivate *priv;
947 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
951 g_mutex_lock (&priv->lock);
952 if ((result = priv->multicast_iface))
953 result = g_strdup (result);
954 g_mutex_unlock (&priv->lock);
960 * gst_rtsp_stream_get_multicast_address:
961 * @stream: a #GstRTSPStream
962 * @family: the #GSocketFamily
964 * Get the multicast address of @stream for @family. The original
965 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
966 * won't release the address from the pool.
968 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
969 * or %NULL when no address could be allocated. gst_rtsp_address_free()
973 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
974 GSocketFamily family)
976 GstRTSPStreamPrivate *priv;
977 GstRTSPAddress *result;
978 GstRTSPAddress **addrp;
979 GstRTSPAddressFlags flags;
981 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
985 g_mutex_lock (&stream->priv->lock);
987 if (family == G_SOCKET_FAMILY_IPV6) {
988 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
989 addrp = &priv->mcast_addr_v6;
991 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
992 addrp = &priv->mcast_addr_v4;
995 if (*addrp == NULL) {
996 if (priv->pool == NULL)
999 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
1001 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
1005 /* FIXME: Also reserve the same port with unicast ANY address, since that's
1006 * where we are going to bind our socket. Probably loop until we find a port
1007 * available in both mcast and unicast pools. Maybe GstRTSPAddressPool
1008 * should do it for us when both GST_RTSP_ADDRESS_FLAG_MULTICAST and
1009 * GST_RTSP_ADDRESS_FLAG_UNICAST are givent. */
1011 result = gst_rtsp_address_copy (*addrp);
1013 g_mutex_unlock (&stream->priv->lock);
1020 GST_ERROR_OBJECT (stream, "no address pool specified");
1021 g_mutex_unlock (&stream->priv->lock);
1026 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
1027 g_mutex_unlock (&stream->priv->lock);
1033 * gst_rtsp_stream_reserve_address:
1034 * @stream: a #GstRTSPStream
1035 * @address: an address
1040 * Reserve @address and @port as the address and port of @stream. The original
1041 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
1042 * won't release the address from the pool.
1044 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
1045 * the address could be reserved. gst_rtsp_address_free() after usage.
1048 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
1049 const gchar * address, guint port, guint n_ports, guint ttl)
1051 GstRTSPStreamPrivate *priv;
1052 GstRTSPAddress *result;
1054 GSocketFamily family;
1055 GstRTSPAddress **addrp;
1057 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1058 g_return_val_if_fail (address != NULL, NULL);
1059 g_return_val_if_fail (port > 0, NULL);
1060 g_return_val_if_fail (n_ports > 0, NULL);
1061 g_return_val_if_fail (ttl > 0, NULL);
1063 priv = stream->priv;
1065 addr = g_inet_address_new_from_string (address);
1067 GST_ERROR ("failed to get inet addr from %s", address);
1068 family = G_SOCKET_FAMILY_IPV4;
1070 family = g_inet_address_get_family (addr);
1071 g_object_unref (addr);
1074 if (family == G_SOCKET_FAMILY_IPV6)
1075 addrp = &priv->mcast_addr_v6;
1077 addrp = &priv->mcast_addr_v4;
1079 g_mutex_lock (&priv->lock);
1080 if (*addrp == NULL) {
1081 GstRTSPAddressPoolResult res;
1083 if (priv->pool == NULL)
1086 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
1087 port, n_ports, ttl, addrp);
1088 if (res != GST_RTSP_ADDRESS_POOL_OK)
1091 /* FIXME: Also reserve the same port with unicast ANY address, since that's
1092 * where we are going to bind our socket. */
1094 if (g_ascii_strcasecmp ((*addrp)->address, address) ||
1095 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
1096 (*addrp)->ttl != ttl)
1097 goto different_address;
1099 result = gst_rtsp_address_copy (*addrp);
1100 g_mutex_unlock (&priv->lock);
1107 GST_ERROR_OBJECT (stream, "no address pool specified");
1108 g_mutex_unlock (&priv->lock);
1113 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1115 g_mutex_unlock (&priv->lock);
1120 GST_ERROR_OBJECT (stream,
1121 "address %s is not the same as %s that was already reserved",
1122 address, (*addrp)->address);
1123 g_mutex_unlock (&priv->lock);
1128 /* must be called with lock */
1130 set_socket_for_udpsink (GstElement * udpsink, GSocket * socket,
1131 GSocketFamily family)
1133 const gchar *multisink_socket;
1135 if (family == G_SOCKET_FAMILY_IPV6)
1136 multisink_socket = "socket-v6";
1138 multisink_socket = "socket";
1140 g_object_set (G_OBJECT (udpsink), multisink_socket, socket, NULL);
1143 /* must be called with lock */
1145 set_multicast_socket_for_udpsink (GstElement * udpsink, GSocket * socket,
1146 GSocketFamily family, const gchar * multicast_iface,
1147 const gchar * addr_str, gint port, gint mcast_ttl)
1149 set_socket_for_udpsink (udpsink, socket, family);
1151 if (multicast_iface) {
1152 GST_INFO ("setting multicast-iface %s", multicast_iface);
1153 g_object_set (G_OBJECT (udpsink), "multicast-iface", multicast_iface, NULL);
1156 if (mcast_ttl > 0) {
1157 GST_INFO ("setting ttl-mc %d", mcast_ttl);
1158 g_object_set (G_OBJECT (udpsink), "ttl-mc", mcast_ttl, NULL);
1163 /* must be called with lock */
1165 set_unicast_socket_for_udpsink (GstElement * udpsink, GSocket * socket,
1166 GSocketFamily family)
1168 set_socket_for_udpsink (udpsink, socket, family);
1172 get_port_from_socket (GSocket * socket)
1175 GSocketAddress *sockaddr;
1178 GST_DEBUG ("socket: %p", socket);
1179 sockaddr = g_socket_get_local_address (socket, &err);
1180 if (sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (sockaddr)) {
1181 g_clear_object (&sockaddr);
1182 GST_ERROR ("failed to get sockaddr: %s", err->message);
1187 port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
1188 g_object_unref (sockaddr);
1195 create_and_configure_udpsink (GstRTSPStream * stream, GstElement ** udpsink,
1196 GSocket * socket_v4, GSocket * socket_v6, gboolean multicast,
1197 gboolean is_rtp, gint mcast_ttl)
1199 GstRTSPStreamPrivate *priv = stream->priv;
1201 *udpsink = gst_element_factory_make ("multiudpsink", NULL);
1204 goto no_udp_protocol;
1206 /* configure sinks */
1208 g_object_set (G_OBJECT (*udpsink), "close-socket", FALSE, NULL);
1210 g_object_set (G_OBJECT (*udpsink), "send-duplicates", FALSE, NULL);
1213 g_object_set (G_OBJECT (*udpsink), "buffer-size", priv->buffer_size, NULL);
1215 g_object_set (G_OBJECT (*udpsink), "sync", FALSE, NULL);
1217 /* Needs to be async for RECORD streams, otherwise we will never go to
1218 * PLAYING because the sinks will wait for data while the udpsrc can't
1219 * provide data with timestamps in PAUSED. */
1220 if (!is_rtp || priv->sinkpad)
1221 g_object_set (G_OBJECT (*udpsink), "async", FALSE, NULL);
1224 /* join multicast group when adding clients, so we'll start receiving from it.
1225 * We cannot rely on the udpsrc to join the group since its socket is always a
1226 * local unicast one. */
1227 g_object_set (G_OBJECT (*udpsink), "auto-multicast", TRUE, NULL);
1229 g_object_set (G_OBJECT (*udpsink), "loop", FALSE, NULL);
1232 /* update the dscp qos field in the sinks */
1233 update_dscp_qos (stream, udpsink);
1235 if (priv->server_addr_v4) {
1236 GST_DEBUG_OBJECT (stream, "udp IPv4, configure udpsinks");
1237 set_unicast_socket_for_udpsink (*udpsink, socket_v4, G_SOCKET_FAMILY_IPV4);
1240 if (priv->server_addr_v6) {
1241 GST_DEBUG_OBJECT (stream, "udp IPv6, configure udpsinks");
1242 set_unicast_socket_for_udpsink (*udpsink, socket_v6, G_SOCKET_FAMILY_IPV6);
1247 if (priv->mcast_addr_v4) {
1248 GST_DEBUG_OBJECT (stream, "mcast IPv4, configure udpsinks");
1249 port = get_port_from_socket (socket_v4);
1251 goto get_port_failed;
1252 set_multicast_socket_for_udpsink (*udpsink, socket_v4,
1253 G_SOCKET_FAMILY_IPV4, priv->multicast_iface,
1254 priv->mcast_addr_v4->address, port, mcast_ttl);
1257 if (priv->mcast_addr_v6) {
1258 GST_DEBUG_OBJECT (stream, "mcast IPv6, configure udpsinks");
1259 port = get_port_from_socket (socket_v6);
1261 goto get_port_failed;
1262 set_multicast_socket_for_udpsink (*udpsink, socket_v6,
1263 G_SOCKET_FAMILY_IPV6, priv->multicast_iface,
1264 priv->mcast_addr_v6->address, port, mcast_ttl);
1274 GST_ERROR_OBJECT (stream, "failed to create udpsink element");
1279 GST_ERROR_OBJECT (stream, "failed to get udp port");
1284 /* must be called with lock */
1286 create_and_configure_udpsource (GstElement ** udpsrc, GSocket * socket)
1288 GstStateChangeReturn ret;
1290 g_assert (socket != NULL);
1292 *udpsrc = gst_element_factory_make ("udpsrc", NULL);
1293 if (*udpsrc == NULL)
1296 g_object_set (G_OBJECT (*udpsrc), "socket", socket, NULL);
1298 /* The udpsrc cannot do the join because its socket is always a local unicast
1299 * one. The udpsink sharing the same socket will do it for us. */
1300 g_object_set (G_OBJECT (*udpsrc), "auto-multicast", FALSE, NULL);
1302 g_object_set (G_OBJECT (*udpsrc), "loop", FALSE, NULL);
1304 g_object_set (G_OBJECT (*udpsrc), "close-socket", FALSE, NULL);
1306 ret = gst_element_set_state (*udpsrc, GST_STATE_READY);
1307 if (ret == GST_STATE_CHANGE_FAILURE)
1316 gst_element_set_state (*udpsrc, GST_STATE_NULL);
1317 g_clear_object (udpsrc);
1324 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1325 GSocket * socket_out[2], GstRTSPAddress ** server_addr_out,
1326 gboolean multicast, GstRTSPTransport * ct)
1328 GstRTSPStreamPrivate *priv = stream->priv;
1329 GSocket *rtp_socket = NULL;
1330 GSocket *rtcp_socket;
1331 gint tmp_rtp, tmp_rtcp;
1333 GList *rejected_addresses = NULL;
1334 GstRTSPAddress *addr = NULL;
1335 GInetAddress *inetaddr = NULL;
1336 GSocketAddress *rtp_sockaddr = NULL;
1337 GSocketAddress *rtcp_sockaddr = NULL;
1338 GstRTSPAddressPool *pool;
1343 /* Start with random port */
1346 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1347 G_SOCKET_PROTOCOL_UDP, NULL);
1349 goto no_udp_protocol;
1350 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1352 /* try to allocate 2 UDP ports, the RTP port should be an even
1353 * number and the RTCP port should be the next (uneven) port */
1356 if (rtp_socket == NULL) {
1357 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1358 G_SOCKET_PROTOCOL_UDP, NULL);
1360 goto no_udp_protocol;
1361 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1364 if ((pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) || multicast) {
1365 GstRTSPAddressFlags flags;
1368 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1373 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1375 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1377 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1379 if (family == G_SOCKET_FAMILY_IPV6)
1380 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1382 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1384 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1389 tmp_rtp = addr->port;
1391 g_clear_object (&inetaddr);
1392 /* FIXME: Does it really work with the IP_MULTICAST_ALL socket option and
1393 * socket control message set in udpsrc? */
1395 inetaddr = g_inet_address_new_any (family);
1397 inetaddr = g_inet_address_new_from_string (addr->address);
1405 if (inetaddr == NULL)
1406 inetaddr = g_inet_address_new_any (family);
1409 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1410 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1411 GST_DEBUG_OBJECT (stream, "rtp bind() failed, will try again");
1412 g_object_unref (rtp_sockaddr);
1415 g_object_unref (rtp_sockaddr);
1417 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1418 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1419 g_clear_object (&rtp_sockaddr);
1424 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1425 g_object_unref (rtp_sockaddr);
1427 /* check if port is even */
1428 if ((tmp_rtp & 1) != 0) {
1429 /* port not even, close and allocate another */
1431 g_clear_object (&rtp_socket);
1436 tmp_rtcp = tmp_rtp + 1;
1438 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1439 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1440 GST_DEBUG_OBJECT (stream, "rctp bind() failed, will try again");
1441 g_object_unref (rtcp_sockaddr);
1442 g_clear_object (&rtp_socket);
1445 g_object_unref (rtcp_sockaddr);
1448 addr = g_slice_new0 (GstRTSPAddress);
1449 addr->address = g_inet_address_to_string (inetaddr);
1450 addr->port = tmp_rtp;
1454 g_clear_object (&inetaddr);
1456 socket_out[0] = rtp_socket;
1457 socket_out[1] = rtcp_socket;
1458 *server_addr_out = addr;
1460 GST_DEBUG_OBJECT (stream, "allocated address: %s and ports: %d, %d",
1461 addr->address, tmp_rtp, tmp_rtcp);
1463 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1470 GST_WARNING_OBJECT (stream, "failed to allocate UDP ports: protocol error");
1475 GST_WARNING_OBJECT (stream,
1476 "failed to allocate UDP ports: no address pool specified");
1481 GST_WARNING_OBJECT (stream, "failed to acquire address from pool");
1486 GST_WARNING_OBJECT (stream, "failed to allocate UDP ports: no ports");
1491 GST_WARNING_OBJECT (stream, "failed to allocate UDP ports: socket error");
1497 g_object_unref (inetaddr);
1498 g_list_free_full (rejected_addresses,
1499 (GDestroyNotify) gst_rtsp_address_free);
1501 gst_rtsp_address_free (addr);
1503 g_object_unref (rtp_socket);
1505 g_object_unref (rtcp_socket);
1511 * gst_rtsp_stream_allocate_udp_sockets:
1512 * @stream: a #GstRTSPStream
1513 * @family: protocol family
1514 * @transport: transport method
1515 * @use_client_settings: Whether to use client settings or not
1517 * Allocates RTP and RTCP ports.
1519 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1522 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1523 GSocketFamily family, GstRTSPTransport * ct,
1524 gboolean use_transport_settings)
1526 GstRTSPStreamPrivate *priv;
1527 gboolean ret = FALSE;
1528 GstRTSPLowerTrans transport;
1529 gboolean allocated = FALSE;
1531 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1532 g_return_val_if_fail (ct != NULL, FALSE);
1533 priv = stream->priv;
1535 transport = ct->lower_transport;
1537 g_mutex_lock (&priv->lock);
1539 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1540 if (family == G_SOCKET_FAMILY_IPV4 && priv->mcast_socket_v4[0])
1542 else if (family == G_SOCKET_FAMILY_IPV6 && priv->mcast_socket_v6[0])
1544 } else if (transport == GST_RTSP_LOWER_TRANS_UDP) {
1545 if (family == G_SOCKET_FAMILY_IPV4 && priv->socket_v4[0])
1547 else if (family == G_SOCKET_FAMILY_IPV6 && priv->socket_v6[0])
1552 GST_DEBUG_OBJECT (stream, "Allocated already");
1553 g_mutex_unlock (&priv->lock);
1557 if (family == G_SOCKET_FAMILY_IPV4) {
1559 if (transport == GST_RTSP_LOWER_TRANS_UDP) {
1561 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_UDP, ipv4");
1562 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1563 priv->socket_v4, &priv->server_addr_v4, FALSE, ct);
1566 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_MCAST_UDP, ipv4");
1567 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1568 priv->mcast_socket_v4, &priv->mcast_addr_v4, TRUE, ct);
1572 if (transport == GST_RTSP_LOWER_TRANS_UDP) {
1574 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_UDP, ipv6");
1575 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1576 priv->socket_v6, &priv->server_addr_v6, FALSE, ct);
1580 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_MCAST_UDP, ipv6");
1581 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1582 priv->mcast_socket_v6, &priv->mcast_addr_v6, TRUE, ct);
1585 g_mutex_unlock (&priv->lock);
1591 * gst_rtsp_stream_set_client_side:
1592 * @stream: a #GstRTSPStream
1593 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1594 * an RTSP connection.
1596 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1597 * streams to an RTSP server via RECORD. This has the practical effect
1598 * of changing which UDP port numbers are used when setting up the local
1599 * side of the stream sending to be either the 'server' or 'client' pair
1600 * of a configured UDP transport.
1603 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1605 GstRTSPStreamPrivate *priv;
1607 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1608 priv = stream->priv;
1609 g_mutex_lock (&priv->lock);
1610 priv->client_side = client_side;
1611 g_mutex_unlock (&priv->lock);
1615 * gst_rtsp_stream_is_client_side:
1616 * @stream: a #GstRTSPStream
1618 * See gst_rtsp_stream_set_client_side()
1620 * Returns: TRUE if this #GstRTSPStream is client-side.
1623 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1625 GstRTSPStreamPrivate *priv;
1628 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1630 priv = stream->priv;
1631 g_mutex_lock (&priv->lock);
1632 ret = priv->client_side;
1633 g_mutex_unlock (&priv->lock);
1639 * gst_rtsp_stream_get_server_port:
1640 * @stream: a #GstRTSPStream
1641 * @server_port: (out): result server port
1642 * @family: the port family to get
1644 * Fill @server_port with the port pair used by the server. This function can
1645 * only be called when @stream has been joined.
1648 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1649 GstRTSPRange * server_port, GSocketFamily family)
1651 GstRTSPStreamPrivate *priv;
1653 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1654 priv = stream->priv;
1655 g_return_if_fail (priv->joined_bin != NULL);
1658 server_port->min = 0;
1659 server_port->max = 0;
1662 g_mutex_lock (&priv->lock);
1663 if (family == G_SOCKET_FAMILY_IPV4) {
1664 if (server_port && priv->server_addr_v4) {
1665 server_port->min = priv->server_addr_v4->port;
1667 priv->server_addr_v4->port + priv->server_addr_v4->n_ports - 1;
1670 if (server_port && priv->server_addr_v6) {
1671 server_port->min = priv->server_addr_v6->port;
1673 priv->server_addr_v6->port + priv->server_addr_v6->n_ports - 1;
1676 g_mutex_unlock (&priv->lock);
1680 * gst_rtsp_stream_get_rtpsession:
1681 * @stream: a #GstRTSPStream
1683 * Get the RTP session of this stream.
1685 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1688 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1690 GstRTSPStreamPrivate *priv;
1693 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1695 priv = stream->priv;
1697 g_mutex_lock (&priv->lock);
1698 if ((session = priv->session))
1699 g_object_ref (session);
1700 g_mutex_unlock (&priv->lock);
1706 * gst_rtsp_stream_get_srtp_encoder:
1707 * @stream: a #GstRTSPStream
1709 * Get the SRTP encoder for this stream.
1711 * Returns: (transfer full): The SRTP encoder for this stream. Unref after usage.
1714 gst_rtsp_stream_get_srtp_encoder (GstRTSPStream * stream)
1716 GstRTSPStreamPrivate *priv;
1717 GstElement *encoder;
1719 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1721 priv = stream->priv;
1723 g_mutex_lock (&priv->lock);
1724 if ((encoder = priv->srtpenc))
1725 g_object_ref (encoder);
1726 g_mutex_unlock (&priv->lock);
1732 * gst_rtsp_stream_get_ssrc:
1733 * @stream: a #GstRTSPStream
1734 * @ssrc: (out): result ssrc
1736 * Get the SSRC used by the RTP session of this stream. This function can only
1737 * be called when @stream has been joined.
1740 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1742 GstRTSPStreamPrivate *priv;
1744 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1745 priv = stream->priv;
1746 g_return_if_fail (priv->joined_bin != NULL);
1748 g_mutex_lock (&priv->lock);
1749 if (ssrc && priv->session)
1750 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1751 g_mutex_unlock (&priv->lock);
1755 * gst_rtsp_stream_set_retransmission_time:
1756 * @stream: a #GstRTSPStream
1757 * @time: a #GstClockTime
1759 * Set the amount of time to store retransmission packets.
1762 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1765 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1767 g_mutex_lock (&stream->priv->lock);
1768 stream->priv->rtx_time = time;
1769 if (stream->priv->rtxsend)
1770 g_object_set (stream->priv->rtxsend, "max-size-time",
1771 GST_TIME_AS_MSECONDS (time), NULL);
1772 g_mutex_unlock (&stream->priv->lock);
1776 * gst_rtsp_stream_get_retransmission_time:
1777 * @stream: a #GstRTSPStream
1779 * Get the amount of time to store retransmission data.
1781 * Returns: the amount of time to store retransmission data.
1784 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1788 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1790 g_mutex_lock (&stream->priv->lock);
1791 ret = stream->priv->rtx_time;
1792 g_mutex_unlock (&stream->priv->lock);
1798 * gst_rtsp_stream_set_retransmission_pt:
1799 * @stream: a #GstRTSPStream
1802 * Set the payload type (pt) for retransmission of this stream.
1805 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1807 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1809 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1811 g_mutex_lock (&stream->priv->lock);
1812 stream->priv->rtx_pt = rtx_pt;
1813 if (stream->priv->rtxsend) {
1814 guint pt = gst_rtsp_stream_get_pt (stream);
1815 gchar *pt_s = g_strdup_printf ("%d", pt);
1816 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1817 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1818 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1820 gst_structure_free (rtx_pt_map);
1822 g_mutex_unlock (&stream->priv->lock);
1826 * gst_rtsp_stream_get_retransmission_pt:
1827 * @stream: a #GstRTSPStream
1829 * Get the payload-type used for retransmission of this stream
1831 * Returns: The retransmission PT.
1834 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1838 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1840 g_mutex_lock (&stream->priv->lock);
1841 rtx_pt = stream->priv->rtx_pt;
1842 g_mutex_unlock (&stream->priv->lock);
1848 * gst_rtsp_stream_set_buffer_size:
1849 * @stream: a #GstRTSPStream
1850 * @size: the buffer size
1852 * Set the size of the UDP transmission buffer (in bytes)
1853 * Needs to be set before the stream is joined to a bin.
1858 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
1860 g_mutex_lock (&stream->priv->lock);
1861 stream->priv->buffer_size = size;
1862 g_mutex_unlock (&stream->priv->lock);
1866 * gst_rtsp_stream_get_buffer_size:
1867 * @stream: a #GstRTSPStream
1869 * Get the size of the UDP transmission buffer (in bytes)
1871 * Returns: the size of the UDP TX buffer
1876 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
1880 g_mutex_lock (&stream->priv->lock);
1881 buffer_size = stream->priv->buffer_size;
1882 g_mutex_unlock (&stream->priv->lock);
1887 /* executed from streaming thread */
1889 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1891 GstRTSPStreamPrivate *priv = stream->priv;
1892 GstCaps *newcaps, *oldcaps;
1894 newcaps = gst_pad_get_current_caps (pad);
1896 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1899 g_mutex_lock (&priv->lock);
1900 oldcaps = priv->caps;
1901 priv->caps = newcaps;
1902 g_mutex_unlock (&priv->lock);
1905 gst_caps_unref (oldcaps);
1909 dump_structure (const GstStructure * s)
1913 sstr = gst_structure_to_string (s);
1914 GST_INFO ("structure: %s", sstr);
1918 static GstRTSPStreamTransport *
1919 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1921 GstRTSPStreamPrivate *priv = stream->priv;
1923 GstRTSPStreamTransport *result = NULL;
1928 if (rtcp_from == NULL)
1931 tmp = g_strrstr (rtcp_from, ":");
1935 port = atoi (tmp + 1);
1936 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1938 g_mutex_lock (&priv->lock);
1939 GST_INFO ("finding %s:%d in %d transports", dest, port,
1940 g_list_length (priv->transports));
1942 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1943 GstRTSPStreamTransport *trans = walk->data;
1944 const GstRTSPTransport *tr;
1947 tr = gst_rtsp_stream_transport_get_transport (trans);
1949 if (priv->client_side) {
1950 /* In client side mode the 'destination' is the RTSP server, so send
1952 min = tr->server_port.min;
1953 max = tr->server_port.max;
1955 min = tr->client_port.min;
1956 max = tr->client_port.max;
1959 if ((g_ascii_strcasecmp (tr->destination, dest) == 0) &&
1960 (min == port || max == port)) {
1966 g_object_ref (result);
1967 g_mutex_unlock (&priv->lock);
1974 static GstRTSPStreamTransport *
1975 check_transport (GObject * source, GstRTSPStream * stream)
1977 GstStructure *stats;
1978 GstRTSPStreamTransport *trans;
1980 /* see if we have a stream to match with the origin of the RTCP packet */
1981 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1982 if (trans == NULL) {
1983 g_object_get (source, "stats", &stats, NULL);
1985 const gchar *rtcp_from;
1987 dump_structure (stats);
1989 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1990 if ((trans = find_transport (stream, rtcp_from))) {
1991 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1993 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1996 gst_structure_free (stats);
2004 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2006 GstRTSPStreamTransport *trans;
2008 GST_INFO ("%p: new source %p", stream, source);
2010 trans = check_transport (source, stream);
2013 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
2017 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
2019 GST_INFO ("%p: new SDES %p", stream, source);
2023 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2025 GstRTSPStreamTransport *trans;
2027 trans = check_transport (source, stream);
2030 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
2031 gst_rtsp_stream_transport_keep_alive (trans);
2035 GstStructure *stats;
2036 g_object_get (source, "stats", &stats, NULL);
2038 dump_structure (stats);
2039 gst_structure_free (stats);
2046 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2048 GST_INFO ("%p: source %p bye", stream, source);
2052 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2054 GstRTSPStreamTransport *trans;
2056 GST_INFO ("%p: source %p bye timeout", stream, source);
2058 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2059 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2060 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2065 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2067 GstRTSPStreamTransport *trans;
2069 GST_INFO ("%p: source %p timeout", stream, source);
2071 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2072 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2073 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2078 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2080 GST_INFO ("%p: new sender source %p", stream, source);
2083 GstStructure *stats;
2084 g_object_get (source, "stats", &stats, NULL);
2086 dump_structure (stats);
2087 gst_structure_free (stats);
2094 on_sender_ssrc_active (GObject * session, GObject * source,
2095 GstRTSPStream * stream)
2099 GstStructure *stats;
2100 g_object_get (source, "stats", &stats, NULL);
2102 dump_structure (stats);
2103 gst_structure_free (stats);
2110 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
2113 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
2114 g_list_free (priv->tr_cache_rtp);
2115 priv->tr_cache_rtp = NULL;
2117 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
2118 g_list_free (priv->tr_cache_rtcp);
2119 priv->tr_cache_rtcp = NULL;
2123 /* Must be called with priv->lock */
2125 send_tcp_message (GstRTSPStream * stream, gint idx)
2127 GstRTSPStreamPrivate *priv = stream->priv;
2134 if (priv->n_outstanding > 0 || !priv->have_buffer[idx]) {
2138 priv->have_buffer[idx] = FALSE;
2140 if (priv->appsink[idx] == NULL) {
2141 /* session expired */
2145 sink = GST_APP_SINK (priv->appsink[idx]);
2146 sample = gst_app_sink_pull_sample (sink);
2151 buffer = gst_sample_get_buffer (sample);
2153 is_rtp = (idx == 0);
2156 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
2157 clear_tr_cache (priv, is_rtp);
2158 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2159 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2160 const GstRTSPTransport *t =
2161 gst_rtsp_stream_transport_get_transport (tr);
2163 if (t->lower_transport != GST_RTSP_LOWER_TRANS_TCP)
2166 priv->tr_cache_rtp =
2167 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
2169 priv->tr_cache_cookie_rtp = priv->transports_cookie;
2172 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
2173 clear_tr_cache (priv, is_rtp);
2174 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2175 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2176 const GstRTSPTransport *t =
2177 gst_rtsp_stream_transport_get_transport (tr);
2179 if (t->lower_transport != GST_RTSP_LOWER_TRANS_TCP)
2182 priv->tr_cache_rtcp =
2183 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
2185 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
2189 priv->n_outstanding += priv->n_tcp_transports;
2191 g_mutex_unlock (&priv->lock);
2194 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
2195 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2196 if (!gst_rtsp_stream_transport_send_rtp (tr, buffer)) {
2197 /* remove transport on send error */
2198 g_mutex_lock (&priv->lock);
2199 priv->n_outstanding--;
2200 update_transport (stream, tr, FALSE);
2201 g_mutex_unlock (&priv->lock);
2205 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
2206 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2207 if (!gst_rtsp_stream_transport_send_rtcp (tr, buffer)) {
2208 /* remove transport on send error */
2209 g_mutex_lock (&priv->lock);
2210 priv->n_outstanding--;
2211 update_transport (stream, tr, FALSE);
2212 g_mutex_unlock (&priv->lock);
2216 gst_sample_unref (sample);
2218 g_mutex_lock (&priv->lock);
2221 static GstFlowReturn
2222 handle_new_sample (GstAppSink * sink, gpointer user_data)
2224 GstRTSPStream *stream = user_data;
2225 GstRTSPStreamPrivate *priv = stream->priv;
2229 g_mutex_lock (&priv->lock);
2231 for (i = 0; i < 2; i++)
2232 if (GST_ELEMENT_CAST (sink) == priv->appsink[i]) {
2233 priv->have_buffer[i] = TRUE;
2234 if (priv->n_outstanding == 0) {
2242 send_tcp_message (stream, idx);
2244 g_mutex_unlock (&priv->lock);
2249 static GstAppSinkCallbacks sink_cb = {
2250 NULL, /* not interested in EOS */
2251 NULL, /* not interested in preroll samples */
2256 get_rtp_encoder (GstRTSPStream * stream, guint session)
2258 GstRTSPStreamPrivate *priv = stream->priv;
2260 if (priv->srtpenc == NULL) {
2263 name = g_strdup_printf ("srtpenc_%u", session);
2264 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2267 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2269 return gst_object_ref (priv->srtpenc);
2273 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2275 GstRTSPStreamPrivate *priv = stream->priv;
2276 GstElement *oldenc, *enc;
2280 if (priv->idx != session)
2283 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2285 oldenc = priv->srtpenc;
2286 enc = get_rtp_encoder (stream, session);
2287 name = g_strdup_printf ("rtp_sink_%d", session);
2288 pad = gst_element_get_request_pad (enc, name);
2290 gst_object_unref (pad);
2293 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2300 request_rtcp_encoder (GstElement * rtpbin, guint session,
2301 GstRTSPStream * stream)
2303 GstRTSPStreamPrivate *priv = stream->priv;
2304 GstElement *oldenc, *enc;
2308 if (priv->idx != session)
2311 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2313 oldenc = priv->srtpenc;
2314 enc = get_rtp_encoder (stream, session);
2315 name = g_strdup_printf ("rtcp_sink_%d", session);
2316 pad = gst_element_get_request_pad (enc, name);
2318 gst_object_unref (pad);
2321 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2328 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2330 GstRTSPStreamPrivate *priv = stream->priv;
2333 GST_DEBUG ("request key %08x", ssrc);
2335 g_mutex_lock (&priv->lock);
2336 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2337 gst_caps_ref (caps);
2338 g_mutex_unlock (&priv->lock);
2344 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2345 GstRTSPStream * stream)
2347 GstRTSPStreamPrivate *priv = stream->priv;
2349 if (priv->idx != session)
2352 if (priv->srtpdec == NULL) {
2355 name = g_strdup_printf ("srtpdec_%u", session);
2356 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2359 g_signal_connect (priv->srtpdec, "request-key",
2360 (GCallback) request_key, stream);
2362 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_RTCP_DECODER],
2366 return gst_object_ref (priv->srtpdec);
2370 * gst_rtsp_stream_request_aux_sender:
2371 * @stream: a #GstRTSPStream
2372 * @sessid: the session id
2374 * Creating a rtxsend bin
2376 * Returns: (transfer full) (nullable): a #GstElement.
2381 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2385 GstStructure *pt_map;
2390 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2392 pt = gst_rtsp_stream_get_pt (stream);
2393 pt_s = g_strdup_printf ("%u", pt);
2394 rtx_pt = stream->priv->rtx_pt;
2396 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2398 bin = gst_bin_new (NULL);
2399 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2400 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2401 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2402 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2403 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2405 gst_structure_free (pt_map);
2406 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2408 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2409 name = g_strdup_printf ("src_%u", sessid);
2410 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2412 gst_object_unref (pad);
2414 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2415 name = g_strdup_printf ("sink_%u", sessid);
2416 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2418 gst_object_unref (pad);
2424 add_rtx_pt (gpointer key, GstCaps * caps, GstStructure * pt_map)
2426 guint pt = GPOINTER_TO_INT (key);
2427 const GstStructure *s = gst_caps_get_structure (caps, 0);
2430 if (!g_strcmp0 (gst_structure_get_string (s, "encoding-name"), "RTX") &&
2431 (apt = gst_structure_get_string (s, "apt"))) {
2432 gst_structure_set (pt_map, apt, G_TYPE_UINT, pt, NULL);
2436 /* Call with priv->lock taken */
2438 update_rtx_receive_pt_map (GstRTSPStream * stream)
2440 GstStructure *pt_map;
2442 if (!stream->priv->rtxreceive)
2445 pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
2446 g_hash_table_foreach (stream->priv->ptmap, (GHFunc) add_rtx_pt, pt_map);
2447 g_object_set (stream->priv->rtxreceive, "payload-type-map", pt_map, NULL);
2448 gst_structure_free (pt_map);
2455 retrieve_ulpfec_pt (gpointer key, GstCaps * caps, GstElement * ulpfec_decoder)
2457 guint pt = GPOINTER_TO_INT (key);
2458 const GstStructure *s = gst_caps_get_structure (caps, 0);
2460 if (!g_strcmp0 (gst_structure_get_string (s, "encoding-name"), "ULPFEC"))
2461 g_object_set (ulpfec_decoder, "pt", pt, NULL);
2465 update_ulpfec_decoder_pt (GstRTSPStream * stream)
2467 if (!stream->priv->ulpfec_decoder)
2470 g_hash_table_foreach (stream->priv->ptmap, (GHFunc) retrieve_ulpfec_pt,
2471 stream->priv->ulpfec_decoder);
2478 * gst_rtsp_stream_request_aux_receiver:
2479 * @stream: a #GstRTSPStream
2480 * @sessid: the session id
2482 * Creating a rtxreceive bin
2484 * Returns: (transfer full) (nullable): a #GstElement.
2489 gst_rtsp_stream_request_aux_receiver (GstRTSPStream * stream, guint sessid)
2495 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2497 bin = gst_bin_new (NULL);
2498 stream->priv->rtxreceive = gst_element_factory_make ("rtprtxreceive", NULL);
2499 update_rtx_receive_pt_map (stream);
2500 update_ulpfec_decoder_pt (stream);
2501 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxreceive));
2503 pad = gst_element_get_static_pad (stream->priv->rtxreceive, "src");
2504 name = g_strdup_printf ("src_%u", sessid);
2505 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2507 gst_object_unref (pad);
2509 pad = gst_element_get_static_pad (stream->priv->rtxreceive, "sink");
2510 name = g_strdup_printf ("sink_%u", sessid);
2511 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2513 gst_object_unref (pad);
2519 * gst_rtsp_stream_set_pt_map:
2520 * @stream: a #GstRTSPStream
2524 * Configure a pt map between @pt and @caps.
2527 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
2529 GstRTSPStreamPrivate *priv = stream->priv;
2531 if (!GST_IS_CAPS (caps))
2534 g_mutex_lock (&priv->lock);
2535 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
2536 update_rtx_receive_pt_map (stream);
2537 g_mutex_unlock (&priv->lock);
2541 * gst_rtsp_stream_set_publish_clock_mode:
2542 * @stream: a #GstRTSPStream
2543 * @mode: the clock publish mode
2545 * Sets if and how the stream clock should be published according to RFC7273.
2550 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
2551 GstRTSPPublishClockMode mode)
2553 GstRTSPStreamPrivate *priv;
2555 priv = stream->priv;
2556 g_mutex_lock (&priv->lock);
2557 priv->publish_clock_mode = mode;
2558 g_mutex_unlock (&priv->lock);
2562 * gst_rtsp_stream_get_publish_clock_mode:
2563 * @stream: a #GstRTSPStream
2565 * Gets if and how the stream clock should be published according to RFC7273.
2567 * Returns: The GstRTSPPublishClockMode
2571 GstRTSPPublishClockMode
2572 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
2574 GstRTSPStreamPrivate *priv;
2575 GstRTSPPublishClockMode ret;
2577 priv = stream->priv;
2578 g_mutex_lock (&priv->lock);
2579 ret = priv->publish_clock_mode;
2580 g_mutex_unlock (&priv->lock);
2586 request_pt_map (GstElement * rtpbin, guint session, guint pt,
2587 GstRTSPStream * stream)
2589 GstRTSPStreamPrivate *priv = stream->priv;
2590 GstCaps *caps = NULL;
2592 g_mutex_lock (&priv->lock);
2594 if (priv->idx == session) {
2595 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
2597 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
2598 gst_caps_ref (caps);
2600 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
2604 g_mutex_unlock (&priv->lock);
2610 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
2612 GstRTSPStreamPrivate *priv = stream->priv;
2614 GstPadLinkReturn ret;
2617 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
2618 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2620 name = gst_pad_get_name (pad);
2621 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
2627 if (priv->idx != sessid)
2630 if (gst_pad_is_linked (priv->sinkpad)) {
2631 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
2632 GST_DEBUG_PAD_NAME (priv->sinkpad));
2636 /* link the RTP pad to the session manager, it should not really fail unless
2637 * this is not really an RTP pad */
2638 ret = gst_pad_link (pad, priv->sinkpad);
2639 if (ret != GST_PAD_LINK_OK)
2641 priv->recv_rtp_src = gst_object_ref (pad);
2648 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
2649 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
2654 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
2655 GstRTSPStream * stream)
2657 /* TODO: What to do here other than this? */
2658 GST_DEBUG ("Stream %p: Got EOS", stream);
2659 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
2662 typedef struct _ProbeData ProbeData;
2666 GstRTSPStream *stream;
2667 /* existing sink, already linked to tee */
2669 /* new sink, about to be linked */
2671 /* new queue element, that will be linked to tee and sink1 */
2672 GstElement **queue1;
2673 /* new queue element, that will be linked to tee and sink2 */
2674 GstElement **queue2;
2681 free_cb_data (gpointer user_data)
2683 ProbeData *data = user_data;
2685 gst_object_unref (data->stream);
2686 gst_object_unref (data->sink1);
2687 gst_object_unref (data->sink2);
2688 gst_object_unref (data->sink_pad);
2689 gst_object_unref (data->tee_pad);
2695 create_and_plug_queue_to_unlinked_stream (GstRTSPStream * stream,
2696 GstElement * tee, GstElement * sink, GstElement ** queue)
2698 GstRTSPStreamPrivate *priv = stream->priv;
2703 /* create queue for the new stream */
2704 *queue = gst_element_factory_make ("queue", NULL);
2705 g_object_set (*queue, "max-size-buffers", 1, "max-size-bytes", 0,
2706 "max-size-time", G_GINT64_CONSTANT (0), NULL);
2707 gst_bin_add (priv->joined_bin, *queue);
2709 /* link tee to queue */
2710 tee_pad = gst_element_get_request_pad (tee, "src_%u");
2711 queue_pad = gst_element_get_static_pad (*queue, "sink");
2712 gst_pad_link (tee_pad, queue_pad);
2713 gst_object_unref (queue_pad);
2714 gst_object_unref (tee_pad);
2716 /* link queue to sink */
2717 queue_pad = gst_element_get_static_pad (*queue, "src");
2718 sink_pad = gst_element_get_static_pad (sink, "sink");
2719 gst_pad_link (queue_pad, sink_pad);
2720 gst_object_unref (queue_pad);
2721 gst_object_unref (sink_pad);
2723 gst_element_sync_state_with_parent (sink);
2724 gst_element_sync_state_with_parent (*queue);
2727 static GstPadProbeReturn
2728 create_and_plug_queue_to_linked_stream_probe_cb (GstPad * inpad,
2729 GstPadProbeInfo * info, gpointer user_data)
2731 GstRTSPStreamPrivate *priv;
2732 ProbeData *data = user_data;
2733 GstRTSPStream *stream;
2734 GstElement **queue1;
2735 GstElement **queue2;
2741 stream = data->stream;
2742 priv = stream->priv;
2743 queue1 = data->queue1;
2744 queue2 = data->queue2;
2745 sink_pad = data->sink_pad;
2746 tee_pad = data->tee_pad;
2747 index = data->index;
2749 /* unlink tee and the existing sink:
2750 * .-----. .---------.
2753 * '-----' '---------'
2755 g_assert (gst_pad_unlink (tee_pad, sink_pad));
2757 /* add queue to the already existing stream */
2758 *queue1 = gst_element_factory_make ("queue", NULL);
2759 g_object_set (*queue1, "max-size-buffers", 1, "max-size-bytes", 0,
2760 "max-size-time", G_GINT64_CONSTANT (0), NULL);
2761 gst_bin_add (priv->joined_bin, *queue1);
2763 /* link tee, queue and sink:
2764 * .-----. .---------. .---------.
2765 * | tee | | queue1 | | sink1 |
2766 * sink src->sink src->sink |
2767 * '-----' '---------' '---------'
2769 queue_pad = gst_element_get_static_pad (*queue1, "sink");
2770 gst_pad_link (tee_pad, queue_pad);
2771 gst_object_unref (queue_pad);
2772 queue_pad = gst_element_get_static_pad (*queue1, "src");
2773 gst_pad_link (queue_pad, sink_pad);
2774 gst_object_unref (queue_pad);
2776 gst_element_sync_state_with_parent (*queue1);
2778 /* create queue and link it to tee and the new sink */
2779 create_and_plug_queue_to_unlinked_stream (stream,
2780 priv->tee[index], data->sink2, queue2);
2782 /* the final stream:
2784 * .-----. .---------. .---------.
2785 * | tee | | queue1 | | sink1 |
2786 * sink src->sink src->sink |
2787 * | | '---------' '---------'
2788 * | | .---------. .---------.
2789 * | | | queue2 | | sink2 |
2790 * | src->sink src->sink |
2791 * '-----' '---------' '---------'
2794 return GST_PAD_PROBE_REMOVE;
2798 create_and_plug_queue_to_linked_stream (GstRTSPStream * stream,
2799 GstElement * sink1, GstElement * sink2, guint index, GstElement ** queue1,
2800 GstElement ** queue2)
2804 data = g_new0 (ProbeData, 1);
2805 data->stream = gst_object_ref (stream);
2806 data->sink1 = gst_object_ref (sink1);
2807 data->sink2 = gst_object_ref (sink2);
2808 data->queue1 = queue1;
2809 data->queue2 = queue2;
2810 data->index = index;
2812 data->sink_pad = gst_element_get_static_pad (sink1, "sink");
2813 g_assert (data->sink_pad);
2814 data->tee_pad = gst_pad_get_peer (data->sink_pad);
2815 g_assert (data->tee_pad);
2817 gst_pad_add_probe (data->tee_pad, GST_PAD_PROBE_TYPE_IDLE,
2818 create_and_plug_queue_to_linked_stream_probe_cb, data, free_cb_data);
2822 plug_udp_sink (GstRTSPStream * stream, GstElement * sink_to_plug,
2823 GstElement ** queue_to_plug, guint index, gboolean is_mcast)
2825 GstRTSPStreamPrivate *priv = stream->priv;
2826 GstElement *existing_sink;
2829 existing_sink = priv->udpsink[index];
2831 existing_sink = priv->mcast_udpsink[index];
2833 GST_DEBUG_OBJECT (stream, "plug %s sink", is_mcast ? "mcast" : "udp");
2835 /* add sink to the bin */
2836 gst_bin_add (priv->joined_bin, sink_to_plug);
2838 if (priv->appsink[index] && existing_sink) {
2840 /* queues are already added for the existing stream, add one for
2841 the newly added udp stream */
2842 create_and_plug_queue_to_unlinked_stream (stream, priv->tee[index],
2843 sink_to_plug, queue_to_plug);
2845 } else if (priv->appsink[index] || existing_sink) {
2847 GstElement *element;
2849 /* add queue to the already existing stream plus the newly created udp
2851 if (priv->appsink[index]) {
2852 element = priv->appsink[index];
2853 queue = &priv->appqueue[index];
2855 element = existing_sink;
2857 queue = &priv->udpqueue[index];
2859 queue = &priv->mcast_udpqueue[index];
2862 create_and_plug_queue_to_linked_stream (stream, element, sink_to_plug,
2863 index, queue, queue_to_plug);
2869 GST_DEBUG_OBJECT (stream, "creating first stream");
2871 /* no need to add queues */
2872 tee_pad = gst_element_get_request_pad (priv->tee[index], "src_%u");
2873 sink_pad = gst_element_get_static_pad (sink_to_plug, "sink");
2874 gst_pad_link (tee_pad, sink_pad);
2875 gst_object_unref (tee_pad);
2876 gst_object_unref (sink_pad);
2879 gst_element_sync_state_with_parent (sink_to_plug);
2883 plug_tcp_sink (GstRTSPStream * stream, guint index)
2885 GstRTSPStreamPrivate *priv = stream->priv;
2887 GST_DEBUG_OBJECT (stream, "plug tcp sink");
2889 /* add sink to the bin */
2890 gst_bin_add (priv->joined_bin, priv->appsink[index]);
2892 if (priv->mcast_udpsink[index] && priv->udpsink[index]) {
2894 /* queues are already added for the existing stream, add one for
2895 the newly added tcp stream */
2896 create_and_plug_queue_to_unlinked_stream (stream,
2897 priv->tee[index], priv->appsink[index], &priv->appqueue[index]);
2899 } else if (priv->mcast_udpsink[index] || priv->udpsink[index]) {
2901 GstElement *element;
2903 /* add queue to the already existing stream plus the newly created tcp
2905 if (priv->mcast_udpsink[index]) {
2906 element = priv->mcast_udpsink[index];
2907 queue = &priv->mcast_udpqueue[index];
2909 element = priv->udpsink[index];
2910 queue = &priv->udpqueue[index];
2913 create_and_plug_queue_to_linked_stream (stream, element,
2914 priv->appsink[index], index, queue, &priv->appqueue[index]);
2920 /* no need to add queues */
2921 tee_pad = gst_element_get_request_pad (priv->tee[index], "src_%u");
2922 sink_pad = gst_element_get_static_pad (priv->appsink[index], "sink");
2923 gst_pad_link (tee_pad, sink_pad);
2924 gst_object_unref (tee_pad);
2925 gst_object_unref (sink_pad);
2928 gst_element_sync_state_with_parent (priv->appsink[index]);
2932 plug_sink (GstRTSPStream * stream, const GstRTSPTransport * transport,
2935 GstRTSPStreamPrivate *priv;
2936 gboolean is_tcp, is_udp, is_mcast;
2937 priv = stream->priv;
2939 is_tcp = transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP;
2940 is_udp = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP;
2941 is_mcast = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST;
2944 plug_udp_sink (stream, priv->udpsink[index],
2945 &priv->udpqueue[index], index, FALSE);
2948 plug_udp_sink (stream, priv->mcast_udpsink[index],
2949 &priv->mcast_udpqueue[index], index, TRUE);
2952 plug_tcp_sink (stream, index);
2955 /* must be called with lock */
2957 create_sender_part (GstRTSPStream * stream, const GstRTSPTransport * transport)
2959 GstRTSPStreamPrivate *priv;
2962 gboolean is_tcp, is_udp, is_mcast;
2966 GST_DEBUG_OBJECT (stream, "create sender part");
2967 priv = stream->priv;
2968 bin = priv->joined_bin;
2970 is_tcp = transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP;
2971 is_udp = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP;
2972 is_mcast = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST;
2975 mcast_ttl = transport->ttl;
2977 GST_DEBUG_OBJECT (stream, "tcp: %d, udp: %d, mcast: %d (ttl: %d)", is_tcp,
2978 is_udp, is_mcast, mcast_ttl);
2980 if (is_udp && !priv->server_addr_v4 && !priv->server_addr_v6) {
2981 GST_WARNING_OBJECT (stream, "no sockets assigned for UDP");
2985 if (is_mcast && !priv->mcast_addr_v4 && !priv->mcast_addr_v6) {
2986 GST_WARNING_OBJECT (stream, "no sockets assigned for UDP multicast");
2990 for (i = 0; i < 2; i++) {
2991 gboolean link_tee = FALSE;
2992 /* For the sender we create this bit of pipeline for both
2994 * Initially there will be only one active transport for
2995 * the stream, so the pipeline will look like this:
2997 * .--------. .-----. .---------.
2998 * | rtpbin | | tee | | sink |
2999 * | send->sink src->sink |
3000 * '--------' '-----' '---------'
3002 * For each new transport, the already existing branch will
3003 * be reconfigured by adding a queue element:
3005 * .--------. .-----. .---------. .---------.
3006 * | rtpbin | | tee | | queue | | udpsink |
3007 * | send->sink src->sink src->sink |
3008 * '--------' | | '---------' '---------'
3009 * | | .---------. .---------.
3010 * | | | queue | | udpsink |
3011 * | src->sink src->sink |
3012 * | | '---------' '---------'
3013 * | | .---------. .---------.
3014 * | | | queue | | appsink |
3015 * | src->sink src->sink |
3016 * '-----' '---------' '---------'
3019 /* Only link the RTP send src if we're going to send RTP, link
3020 * the RTCP send src always */
3021 if (!priv->srcpad && i == 0)
3024 if (!priv->tee[i]) {
3025 /* make tee for RTP/RTCP */
3026 priv->tee[i] = gst_element_factory_make ("tee", NULL);
3027 gst_bin_add (bin, priv->tee[i]);
3031 if (is_udp && !priv->udpsink[i]) {
3032 /* we create only one pair of udpsinks for IPv4 and IPv6 */
3033 create_and_configure_udpsink (stream, &priv->udpsink[i],
3034 priv->socket_v4[i], priv->socket_v6[i], FALSE, (i == 0), mcast_ttl);
3035 plug_sink (stream, transport, i);
3036 } else if (is_mcast && !priv->mcast_udpsink[i]) {
3037 /* we create only one pair of mcast-udpsinks for IPv4 and IPv6 */
3038 create_and_configure_udpsink (stream, &priv->mcast_udpsink[i],
3039 priv->mcast_socket_v4[i], priv->mcast_socket_v6[i], TRUE, (i == 0),
3041 plug_sink (stream, transport, i);
3042 } else if (is_tcp && !priv->appsink[i]) {
3044 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
3045 g_object_set (priv->appsink[i], "emit-signals", FALSE, "max-buffers", 1,
3048 /* we need to set sync and preroll to FALSE for the sink to avoid
3049 * deadlock. This is only needed for sink sending RTCP data. */
3051 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
3053 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
3054 &sink_cb, stream, NULL);
3055 plug_sink (stream, transport, i);
3059 /* and link to rtpbin send pad */
3060 gst_element_sync_state_with_parent (priv->tee[i]);
3061 pad = gst_element_get_static_pad (priv->tee[i], "sink");
3062 gst_pad_link (priv->send_src[i], pad);
3063 gst_object_unref (pad);
3070 /* must be called with lock */
3072 plug_src (GstRTSPStream * stream, GstBin * bin, GstElement * src,
3073 GstElement * funnel)
3075 GstRTSPStreamPrivate *priv;
3076 GstPad *pad, *selpad;
3079 priv = stream->priv;
3081 pad = gst_element_get_static_pad (src, "src");
3083 /* block pad so src can't push data while it's not yet linked */
3084 id = gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BLOCK |
3085 GST_PAD_PROBE_TYPE_BUFFER, NULL, NULL, NULL);
3086 /* we set and keep these to playing so that they don't cause NO_PREROLL return
3087 * values. This is only relevant for PLAY pipelines */
3088 gst_element_set_state (src, GST_STATE_PLAYING);
3089 gst_element_set_locked_state (src, TRUE);
3093 gst_bin_add (bin, src);
3095 /* and link to the funnel */
3096 selpad = gst_element_get_request_pad (funnel, "sink_%u");
3097 gst_pad_link (pad, selpad);
3099 gst_pad_remove_probe (pad, id);
3100 gst_object_unref (pad);
3101 gst_object_unref (selpad);
3104 /* must be called with lock */
3106 create_receiver_part (GstRTSPStream * stream, const GstRTSPTransport *
3109 GstRTSPStreamPrivate *priv;
3117 GST_DEBUG_OBJECT (stream, "create receiver part");
3118 priv = stream->priv;
3119 bin = priv->joined_bin;
3121 tcp = transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP;
3122 udp = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP;
3123 mcast = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST;
3125 for (i = 0; i < 2; i++) {
3126 /* For the receiver we create this bit of pipeline for both
3127 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
3128 * and it is all funneled into the rtpbin receive pad.
3131 * .--------. .--------. .--------.
3132 * | udpsrc | | funnel | | rtpbin |
3133 * | RTP src->sink src->sink |
3134 * '--------' | | | |
3135 * .--------. | | | |
3136 * | appsrc | | | | |
3137 * | RTP src->sink | | |
3138 * '--------' '--------' | |
3140 * .--------. .--------. | |
3141 * | udpsrc | | funnel | | |
3142 * | RTCP src->sink src->sink |
3143 * '--------' | | '--------'
3146 * | RTCP src->sink |
3147 * '--------' '--------'
3150 if (!priv->sinkpad && i == 0) {
3151 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
3152 * RTCP sink always */
3156 /* make funnel for the RTP/RTCP receivers */
3157 if (!priv->funnel[i]) {
3158 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
3159 gst_bin_add (bin, priv->funnel[i]);
3161 pad = gst_element_get_static_pad (priv->funnel[i], "src");
3162 gst_pad_link (pad, priv->recv_sink[i]);
3163 gst_object_unref (pad);
3166 if (udp && !priv->udpsrc_v4[i] && priv->server_addr_v4) {
3167 GST_DEBUG_OBJECT (stream, "udp IPv4, create and configure udpsources");
3168 if (!create_and_configure_udpsource (&priv->udpsrc_v4[i],
3169 priv->socket_v4[i]))
3172 plug_src (stream, bin, priv->udpsrc_v4[i], priv->funnel[i]);
3175 if (udp && !priv->udpsrc_v6[i] && priv->server_addr_v6) {
3176 GST_DEBUG_OBJECT (stream, "udp IPv6, create and configure udpsources");
3177 if (!create_and_configure_udpsource (&priv->udpsrc_v6[i],
3178 priv->socket_v6[i]))
3181 plug_src (stream, bin, priv->udpsrc_v6[i], priv->funnel[i]);
3184 if (mcast && !priv->mcast_udpsrc_v4[i] && priv->mcast_addr_v4) {
3185 GST_DEBUG_OBJECT (stream, "mcast IPv4, create and configure udpsources");
3186 if (!create_and_configure_udpsource (&priv->mcast_udpsrc_v4[i],
3187 priv->mcast_socket_v4[i]))
3188 goto mcast_udpsrc_error;
3189 plug_src (stream, bin, priv->mcast_udpsrc_v4[i], priv->funnel[i]);
3192 if (mcast && !priv->mcast_udpsrc_v6[i] && priv->mcast_addr_v6) {
3193 GST_DEBUG_OBJECT (stream, "mcast IPv6, create and configure udpsources");
3194 if (!create_and_configure_udpsource (&priv->mcast_udpsrc_v6[i],
3195 priv->mcast_socket_v6[i]))
3196 goto mcast_udpsrc_error;
3197 plug_src (stream, bin, priv->mcast_udpsrc_v6[i], priv->funnel[i]);
3200 if (tcp && !priv->appsrc[i]) {
3201 /* make and add appsrc */
3202 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
3203 priv->appsrc_base_time[i] = -1;
3204 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
3206 plug_src (stream, bin, priv->appsrc[i], priv->funnel[i]);
3209 gst_element_sync_state_with_parent (priv->funnel[i]);
3220 check_mcast_part_for_transport (GstRTSPStream * stream,
3221 const GstRTSPTransport * tr)
3223 GstRTSPStreamPrivate *priv = stream->priv;
3224 GInetAddress *inetaddr;
3225 GSocketFamily family;
3226 GstRTSPAddress *mcast_addr;
3228 /* Check if it's a ipv4 or ipv6 transport */
3229 inetaddr = g_inet_address_new_from_string (tr->destination);
3230 family = g_inet_address_get_family (inetaddr);
3231 g_object_unref (inetaddr);
3233 /* Select fields corresponding to the family */
3234 if (family == G_SOCKET_FAMILY_IPV4) {
3235 mcast_addr = priv->mcast_addr_v4;
3237 mcast_addr = priv->mcast_addr_v6;
3240 /* We support only one mcast group per family, make sure this transport
3245 if (g_ascii_strcasecmp (tr->destination, mcast_addr->address) != 0 ||
3246 tr->port.min != mcast_addr->port ||
3247 tr->port.max != mcast_addr->port + mcast_addr->n_ports - 1 ||
3248 tr->ttl != mcast_addr->ttl)
3255 GST_WARNING_OBJECT (stream, "Adding mcast transport, but no mcast address "
3256 "has been reserved");
3261 GST_WARNING_OBJECT (stream, "Adding mcast transport, but it doesn't match "
3262 "the reserved address");
3268 * gst_rtsp_stream_join_bin:
3269 * @stream: a #GstRTSPStream
3270 * @bin: (transfer none): a #GstBin to join
3271 * @rtpbin: (transfer none): a rtpbin element in @bin
3272 * @state: the target state of the new elements
3274 * Join the #GstBin @bin that contains the element @rtpbin.
3276 * @stream will link to @rtpbin, which must be inside @bin. The elements
3277 * added to @bin will be set to the state given in @state.
3279 * Returns: %TRUE on success.
3282 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
3283 GstElement * rtpbin, GstState state)
3285 GstRTSPStreamPrivate *priv;
3288 GstPadLinkReturn ret;
3290 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3291 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
3292 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
3294 priv = stream->priv;
3296 g_mutex_lock (&priv->lock);
3297 if (priv->joined_bin != NULL)
3300 /* create a session with the same index as the stream */
3303 GST_INFO ("stream %p joining bin as session %u", stream, idx);
3305 if (priv->profiles & GST_RTSP_PROFILE_SAVP
3306 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
3308 g_signal_connect (rtpbin, "request-rtp-encoder",
3309 (GCallback) request_rtp_encoder, stream);
3310 g_signal_connect (rtpbin, "request-rtcp-encoder",
3311 (GCallback) request_rtcp_encoder, stream);
3312 g_signal_connect (rtpbin, "request-rtp-decoder",
3313 (GCallback) request_rtp_rtcp_decoder, stream);
3314 g_signal_connect (rtpbin, "request-rtcp-decoder",
3315 (GCallback) request_rtp_rtcp_decoder, stream);
3318 if (priv->sinkpad) {
3319 g_signal_connect (rtpbin, "request-pt-map",
3320 (GCallback) request_pt_map, stream);
3323 /* get pads from the RTP session element for sending and receiving
3326 /* get a pad for sending RTP */
3327 name = g_strdup_printf ("send_rtp_sink_%u", idx);
3328 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
3331 /* link the RTP pad to the session manager, it should not really fail unless
3332 * this is not really an RTP pad */
3333 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
3334 if (ret != GST_PAD_LINK_OK)
3337 name = g_strdup_printf ("send_rtp_src_%u", idx);
3338 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
3341 /* RECORD case: need to connect our sinkpad from here */
3342 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
3344 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
3346 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
3347 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
3351 name = g_strdup_printf ("send_rtcp_src_%u", idx);
3352 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
3354 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
3355 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
3358 /* get the session */
3359 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
3361 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
3363 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
3365 g_signal_connect (priv->session, "on-ssrc-active",
3366 (GCallback) on_ssrc_active, stream);
3367 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3369 g_signal_connect (priv->session, "on-bye-timeout",
3370 (GCallback) on_bye_timeout, stream);
3371 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
3374 /* signal for sender ssrc */
3375 g_signal_connect (priv->session, "on-new-sender-ssrc",
3376 (GCallback) on_new_sender_ssrc, stream);
3377 g_signal_connect (priv->session, "on-sender-ssrc-active",
3378 (GCallback) on_sender_ssrc_active, stream);
3381 /* be notified of caps changes */
3382 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
3383 (GCallback) caps_notify, stream);
3384 priv->caps = gst_pad_get_current_caps (priv->send_src[0]);
3387 priv->joined_bin = bin;
3388 GST_DEBUG_OBJECT (stream, "successfully joined bin");
3389 g_mutex_unlock (&priv->lock);
3396 g_mutex_unlock (&priv->lock);
3401 GST_WARNING ("failed to link stream %u", idx);
3402 gst_object_unref (priv->send_rtp_sink);
3403 priv->send_rtp_sink = NULL;
3404 g_mutex_unlock (&priv->lock);
3410 clear_element (GstBin * bin, GstElement ** elementptr)
3413 gst_element_set_locked_state (*elementptr, FALSE);
3414 gst_element_set_state (*elementptr, GST_STATE_NULL);
3415 if (GST_ELEMENT_PARENT (*elementptr))
3416 gst_bin_remove (bin, *elementptr);
3418 gst_object_unref (*elementptr);
3424 * gst_rtsp_stream_leave_bin:
3425 * @stream: a #GstRTSPStream
3426 * @bin: (transfer none): a #GstBin
3427 * @rtpbin: (transfer none): a rtpbin #GstElement
3429 * Remove the elements of @stream from @bin.
3431 * Return: %TRUE on success.
3434 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
3435 GstElement * rtpbin)
3437 GstRTSPStreamPrivate *priv;
3440 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3441 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
3442 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
3444 priv = stream->priv;
3446 g_mutex_lock (&priv->lock);
3447 if (priv->joined_bin == NULL)
3448 goto was_not_joined;
3449 if (priv->joined_bin != bin)
3452 priv->joined_bin = NULL;
3454 /* all transports must be removed by now */
3455 if (priv->transports != NULL)
3456 goto transports_not_removed;
3458 clear_tr_cache (priv, TRUE);
3459 clear_tr_cache (priv, FALSE);
3461 GST_INFO ("stream %p leaving bin", stream);
3464 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
3466 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
3467 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
3468 gst_object_unref (priv->send_rtp_sink);
3469 priv->send_rtp_sink = NULL;
3470 } else if (priv->recv_rtp_src) {
3471 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
3472 gst_object_unref (priv->recv_rtp_src);
3473 priv->recv_rtp_src = NULL;
3476 for (i = 0; i < 2; i++) {
3477 clear_element (bin, &priv->udpsrc_v4[i]);
3478 clear_element (bin, &priv->udpsrc_v6[i]);
3479 clear_element (bin, &priv->udpqueue[i]);
3480 clear_element (bin, &priv->udpsink[i]);
3482 clear_element (bin, &priv->mcast_udpsrc_v4[i]);
3483 clear_element (bin, &priv->mcast_udpsrc_v6[i]);
3484 clear_element (bin, &priv->mcast_udpqueue[i]);
3485 clear_element (bin, &priv->mcast_udpsink[i]);
3487 clear_element (bin, &priv->appsrc[i]);
3488 clear_element (bin, &priv->appqueue[i]);
3489 clear_element (bin, &priv->appsink[i]);
3491 clear_element (bin, &priv->tee[i]);
3492 clear_element (bin, &priv->funnel[i]);
3494 if (priv->sinkpad || i == 1) {
3495 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
3496 gst_object_unref (priv->recv_sink[i]);
3497 priv->recv_sink[i] = NULL;
3502 gst_object_unref (priv->send_src[0]);
3503 priv->send_src[0] = NULL;
3506 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
3507 gst_object_unref (priv->send_src[1]);
3508 priv->send_src[1] = NULL;
3510 g_object_unref (priv->session);
3511 priv->session = NULL;
3513 gst_caps_unref (priv->caps);
3517 gst_object_unref (priv->srtpenc);
3519 gst_object_unref (priv->srtpdec);
3521 if (priv->mcast_addr_v4)
3522 gst_rtsp_address_free (priv->mcast_addr_v4);
3523 priv->mcast_addr_v4 = NULL;
3524 if (priv->mcast_addr_v6)
3525 gst_rtsp_address_free (priv->mcast_addr_v6);
3526 priv->mcast_addr_v6 = NULL;
3527 if (priv->server_addr_v4)
3528 gst_rtsp_address_free (priv->server_addr_v4);
3529 priv->server_addr_v4 = NULL;
3530 if (priv->server_addr_v6)
3531 gst_rtsp_address_free (priv->server_addr_v6);
3532 priv->server_addr_v6 = NULL;
3534 g_mutex_unlock (&priv->lock);
3540 g_mutex_unlock (&priv->lock);
3543 transports_not_removed:
3545 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
3546 g_mutex_unlock (&priv->lock);
3551 GST_ERROR_OBJECT (stream, "leaving the wrong bin");
3552 g_mutex_unlock (&priv->lock);
3558 * gst_rtsp_stream_get_joined_bin:
3559 * @stream: a #GstRTSPStream
3561 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
3563 * Return: (transfer full) (nullable): the joined bin or NULL.
3566 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
3568 GstRTSPStreamPrivate *priv;
3571 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3573 priv = stream->priv;
3575 g_mutex_lock (&priv->lock);
3576 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
3577 g_mutex_unlock (&priv->lock);
3583 * gst_rtsp_stream_get_rtpinfo:
3584 * @stream: a #GstRTSPStream
3585 * @rtptime: (allow-none) (out caller-allocates): result RTP timestamp
3586 * @seq: (allow-none) (out caller-allocates): result RTP seqnum
3587 * @clock_rate: (allow-none) (out caller-allocates): the clock rate
3588 * @running_time: (out caller-allocates): result running-time
3590 * Retrieve the current rtptime, seq and running-time. This is used to
3591 * construct a RTPInfo reply header.
3593 * Returns: %TRUE when rtptime, seq and running-time could be determined.
3596 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
3597 guint * rtptime, guint * seq, guint * clock_rate,
3598 GstClockTime * running_time)
3600 GstRTSPStreamPrivate *priv;
3601 GstStructure *stats;
3602 GObjectClass *payobjclass;
3604 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3606 priv = stream->priv;
3608 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
3610 g_mutex_lock (&priv->lock);
3612 /* First try to extract the information from the last buffer on the sinks.
3613 * This will have a more accurate sequence number and timestamp, as between
3614 * the payloader and the sink there can be some queues
3616 if (priv->udpsink[0] || priv->appsink[0]) {
3617 GstSample *last_sample;
3619 if (priv->udpsink[0])
3620 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
3622 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
3627 GstSegment *segment;
3629 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
3631 caps = gst_sample_get_caps (last_sample);
3632 buffer = gst_sample_get_buffer (last_sample);
3633 segment = gst_sample_get_segment (last_sample);
3634 s = gst_caps_get_structure (caps, 0);
3636 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
3637 guint ssrc_buf = gst_rtp_buffer_get_ssrc (&rtp_buffer);
3638 guint ssrc_stream = 0;
3639 if (gst_structure_has_field_typed (s, "ssrc", G_TYPE_UINT) &&
3640 gst_structure_get_uint (s, "ssrc", &ssrc_stream) &&
3641 ssrc_buf != ssrc_stream) {
3642 /* Skip buffers from auxiliary streams. */
3643 GST_DEBUG_OBJECT (stream,
3644 "not a buffer from the payloader, SSRC: %08x", ssrc_buf);
3646 gst_rtp_buffer_unmap (&rtp_buffer);
3647 gst_sample_unref (last_sample);
3652 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
3656 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
3659 gst_rtp_buffer_unmap (&rtp_buffer);
3663 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
3664 GST_BUFFER_TIMESTAMP (buffer));
3668 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
3670 if (*clock_rate == 0 && running_time)
3671 *running_time = GST_CLOCK_TIME_NONE;
3673 gst_sample_unref (last_sample);
3677 gst_sample_unref (last_sample);
3683 if (g_object_class_find_property (payobjclass, "stats")) {
3684 g_object_get (priv->payloader, "stats", &stats, NULL);
3689 gst_structure_get_uint (stats, "seqnum", seq);
3692 gst_structure_get_uint (stats, "timestamp", rtptime);
3695 gst_structure_get_clock_time (stats, "running-time", running_time);
3698 gst_structure_get_uint (stats, "clock-rate", clock_rate);
3699 if (*clock_rate == 0 && running_time)
3700 *running_time = GST_CLOCK_TIME_NONE;
3702 gst_structure_free (stats);
3704 if (!g_object_class_find_property (payobjclass, "seqnum") ||
3705 !g_object_class_find_property (payobjclass, "timestamp"))
3709 g_object_get (priv->payloader, "seqnum", seq, NULL);
3712 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
3715 *running_time = GST_CLOCK_TIME_NONE;
3719 g_mutex_unlock (&priv->lock);
3726 GST_WARNING ("Could not get payloader stats");
3727 g_mutex_unlock (&priv->lock);
3733 * gst_rtsp_stream_get_caps:
3734 * @stream: a #GstRTSPStream
3736 * Retrieve the current caps of @stream.
3738 * Returns: (transfer full) (nullable): the #GstCaps of @stream.
3739 * use gst_caps_unref() after usage.
3742 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
3744 GstRTSPStreamPrivate *priv;
3747 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3749 priv = stream->priv;
3751 g_mutex_lock (&priv->lock);
3752 if ((result = priv->caps))
3753 gst_caps_ref (result);
3754 g_mutex_unlock (&priv->lock);
3760 * gst_rtsp_stream_recv_rtp:
3761 * @stream: a #GstRTSPStream
3762 * @buffer: (transfer full): a #GstBuffer
3764 * Handle an RTP buffer for the stream. This method is usually called when a
3765 * message has been received from a client using the TCP transport.
3767 * This function takes ownership of @buffer.
3769 * Returns: a GstFlowReturn.
3772 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
3774 GstRTSPStreamPrivate *priv;
3776 GstElement *element;
3778 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3779 priv = stream->priv;
3780 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3781 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
3783 g_mutex_lock (&priv->lock);
3784 if (priv->appsrc[0])
3785 element = gst_object_ref (priv->appsrc[0]);
3788 g_mutex_unlock (&priv->lock);
3791 if (priv->appsrc_base_time[0] == -1) {
3792 /* Take current running_time. This timestamp will be put on
3793 * the first buffer of each stream because we are a live source and so we
3794 * timestamp with the running_time. When we are dealing with TCP, we also
3795 * only timestamp the first buffer (using the DISCONT flag) because a server
3796 * typically bursts data, for which we don't want to compensate by speeding
3797 * up the media. The other timestamps will be interpollated from this one
3798 * using the RTP timestamps. */
3799 GST_OBJECT_LOCK (element);
3800 if (GST_ELEMENT_CLOCK (element)) {
3802 GstClockTime base_time;
3804 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3805 base_time = GST_ELEMENT_CAST (element)->base_time;
3807 priv->appsrc_base_time[0] = now - base_time;
3808 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
3809 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3810 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3811 GST_TIME_ARGS (base_time));
3813 GST_OBJECT_UNLOCK (element);
3816 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3817 gst_object_unref (element);
3825 * gst_rtsp_stream_recv_rtcp:
3826 * @stream: a #GstRTSPStream
3827 * @buffer: (transfer full): a #GstBuffer
3829 * Handle an RTCP buffer for the stream. This method is usually called when a
3830 * message has been received from a client using the TCP transport.
3832 * This function takes ownership of @buffer.
3834 * Returns: a GstFlowReturn.
3837 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
3839 GstRTSPStreamPrivate *priv;
3841 GstElement *element;
3843 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
3844 priv = stream->priv;
3845 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
3847 if (priv->joined_bin == NULL) {
3848 gst_buffer_unref (buffer);
3849 return GST_FLOW_NOT_LINKED;
3851 g_mutex_lock (&priv->lock);
3852 if (priv->appsrc[1])
3853 element = gst_object_ref (priv->appsrc[1]);
3856 g_mutex_unlock (&priv->lock);
3859 if (priv->appsrc_base_time[1] == -1) {
3860 /* Take current running_time. This timestamp will be put on
3861 * the first buffer of each stream because we are a live source and so we
3862 * timestamp with the running_time. When we are dealing with TCP, we also
3863 * only timestamp the first buffer (using the DISCONT flag) because a server
3864 * typically bursts data, for which we don't want to compensate by speeding
3865 * up the media. The other timestamps will be interpollated from this one
3866 * using the RTP timestamps. */
3867 GST_OBJECT_LOCK (element);
3868 if (GST_ELEMENT_CLOCK (element)) {
3870 GstClockTime base_time;
3872 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
3873 base_time = GST_ELEMENT_CAST (element)->base_time;
3875 priv->appsrc_base_time[1] = now - base_time;
3876 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
3877 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
3878 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
3879 GST_TIME_ARGS (base_time));
3881 GST_OBJECT_UNLOCK (element);
3884 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
3885 gst_object_unref (element);
3888 gst_buffer_unref (buffer);
3893 /* must be called with lock */
3895 add_client (GstElement * rtp_sink, GstElement * rtcp_sink, const gchar * host,
3896 gint rtp_port, gint rtcp_port)
3898 if (rtp_sink != NULL)
3899 g_signal_emit_by_name (rtp_sink, "add", host, rtp_port, NULL);
3900 if (rtcp_sink != NULL)
3901 g_signal_emit_by_name (rtcp_sink, "add", host, rtcp_port, NULL);
3904 /* must be called with lock */
3906 remove_client (GstElement * rtp_sink, GstElement * rtcp_sink,
3907 const gchar * host, gint rtp_port, gint rtcp_port)
3909 if (rtp_sink != NULL)
3910 g_signal_emit_by_name (rtp_sink, "remove", host, rtp_port, NULL);
3911 if (rtcp_sink != NULL)
3912 g_signal_emit_by_name (rtcp_sink, "remove", host, rtcp_port, NULL);
3915 /* must be called with lock */
3917 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
3920 GstRTSPStreamPrivate *priv = stream->priv;
3921 const GstRTSPTransport *tr;
3925 tr = gst_rtsp_stream_transport_get_transport (trans);
3926 dest = tr->destination;
3928 switch (tr->lower_transport) {
3929 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3935 GST_INFO ("adding %s:%d-%d", dest, min, max);
3936 if (!check_mcast_part_for_transport (stream, tr))
3939 /* FIXME: Is it ok to set ttl-mc if media is shared? */
3941 GST_INFO ("setting ttl-mc %d", tr->ttl);
3942 if (priv->mcast_udpsink[0])
3943 g_object_set (G_OBJECT (priv->mcast_udpsink[0]), "ttl-mc", tr->ttl,
3945 if (priv->mcast_udpsink[1])
3946 g_object_set (G_OBJECT (priv->mcast_udpsink[1]), "ttl-mc", tr->ttl,
3949 add_client (priv->mcast_udpsink[0], priv->mcast_udpsink[1], dest, min,
3951 priv->transports = g_list_prepend (priv->transports, trans);
3953 GST_INFO ("removing %s:%d-%d", dest, min, max);
3954 remove_client (priv->mcast_udpsink[0], priv->mcast_udpsink[1], dest,
3956 priv->transports = g_list_remove (priv->transports, trans);
3960 case GST_RTSP_LOWER_TRANS_UDP:
3962 if (priv->client_side) {
3963 /* In client side mode the 'destination' is the RTSP server, so send
3965 min = tr->server_port.min;
3966 max = tr->server_port.max;
3968 min = tr->client_port.min;
3969 max = tr->client_port.max;
3973 GST_INFO ("adding %s:%d-%d", dest, min, max);
3974 add_client (priv->udpsink[0], priv->udpsink[1], dest, min, max);
3975 priv->transports = g_list_prepend (priv->transports, trans);
3977 GST_INFO ("removing %s:%d-%d", dest, min, max);
3978 remove_client (priv->udpsink[0], priv->udpsink[1], dest, min, max);
3979 priv->transports = g_list_remove (priv->transports, trans);
3981 priv->transports_cookie++;
3984 case GST_RTSP_LOWER_TRANS_TCP:
3986 GST_INFO ("adding TCP %s", tr->destination);
3987 priv->transports = g_list_prepend (priv->transports, trans);
3988 priv->n_tcp_transports++;
3990 GST_INFO ("removing TCP %s", tr->destination);
3991 priv->transports = g_list_remove (priv->transports, trans);
3992 priv->n_tcp_transports--;
3994 priv->transports_cookie++;
3997 goto unknown_transport;
4004 GST_INFO ("Unknown transport %d", tr->lower_transport);
4014 on_message_sent (gpointer user_data)
4016 GstRTSPStream *stream = user_data;
4017 GstRTSPStreamPrivate *priv = stream->priv;
4020 GST_DEBUG_OBJECT (stream, "message send complete");
4022 g_mutex_lock (&priv->lock);
4024 g_assert (priv->n_outstanding >= 0);
4026 if (priv->n_outstanding == 0)
4027 goto no_outstanding;
4029 priv->n_outstanding--;
4030 if (priv->n_outstanding == 0) {
4033 /* iterate from 1 and down, so we prioritize RTCP over RTP */
4034 for (i = 1; i >= 0; i--) {
4035 if (priv->have_buffer[i]) {
4044 send_tcp_message (stream, idx);
4046 g_mutex_unlock (&priv->lock);
4053 GST_INFO ("no outstanding messages");
4054 g_mutex_unlock (&priv->lock);
4060 * gst_rtsp_stream_add_transport:
4061 * @stream: a #GstRTSPStream
4062 * @trans: (transfer none): a #GstRTSPStreamTransport
4064 * Add the transport in @trans to @stream. The media of @stream will
4065 * then also be send to the values configured in @trans.
4067 * @stream must be joined to a bin.
4069 * @trans must contain a valid #GstRTSPTransport.
4071 * Returns: %TRUE if @trans was added
4074 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
4075 GstRTSPStreamTransport * trans)
4077 GstRTSPStreamPrivate *priv;
4080 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4081 priv = stream->priv;
4082 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
4083 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
4085 g_mutex_lock (&priv->lock);
4086 res = update_transport (stream, trans, TRUE);
4088 gst_rtsp_stream_transport_set_message_sent (trans, on_message_sent, stream,
4090 g_mutex_unlock (&priv->lock);
4096 * gst_rtsp_stream_remove_transport:
4097 * @stream: a #GstRTSPStream
4098 * @trans: (transfer none): a #GstRTSPStreamTransport
4100 * Remove the transport in @trans from @stream. The media of @stream will
4101 * not be sent to the values configured in @trans.
4103 * @stream must be joined to a bin.
4105 * @trans must contain a valid #GstRTSPTransport.
4107 * Returns: %TRUE if @trans was removed
4110 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
4111 GstRTSPStreamTransport * trans)
4113 GstRTSPStreamPrivate *priv;
4116 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4117 priv = stream->priv;
4118 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
4119 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
4121 g_mutex_lock (&priv->lock);
4122 res = update_transport (stream, trans, FALSE);
4123 g_mutex_unlock (&priv->lock);
4129 * gst_rtsp_stream_update_crypto:
4130 * @stream: a #GstRTSPStream
4132 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
4134 * Update the new crypto information for @ssrc in @stream. If information
4135 * for @ssrc did not exist, it will be added. If information
4136 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
4137 * be removed from @stream.
4139 * Returns: %TRUE if @crypto could be updated
4142 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
4143 guint ssrc, GstCaps * crypto)
4145 GstRTSPStreamPrivate *priv;
4147 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4148 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
4150 priv = stream->priv;
4152 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
4154 g_mutex_lock (&priv->lock);
4156 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
4157 gst_caps_ref (crypto));
4159 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
4160 g_mutex_unlock (&priv->lock);
4166 * gst_rtsp_stream_get_rtp_socket:
4167 * @stream: a #GstRTSPStream
4168 * @family: the socket family
4170 * Get the RTP socket from @stream for a @family.
4172 * @stream must be joined to a bin.
4174 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
4175 * socket could be allocated for @family. Unref after usage
4178 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
4180 GstRTSPStreamPrivate *priv = stream->priv;
4183 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
4184 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
4185 family == G_SOCKET_FAMILY_IPV6, NULL);
4187 g_mutex_lock (&priv->lock);
4188 if (family == G_SOCKET_FAMILY_IPV6)
4189 socket = priv->socket_v6[0];
4191 socket = priv->socket_v4[0];
4194 socket = g_object_ref (socket);
4195 g_mutex_unlock (&priv->lock);
4201 * gst_rtsp_stream_get_rtcp_socket:
4202 * @stream: a #GstRTSPStream
4203 * @family: the socket family
4205 * Get the RTCP socket from @stream for a @family.
4207 * @stream must be joined to a bin.
4209 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
4210 * socket could be allocated for @family. Unref after usage
4213 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
4215 GstRTSPStreamPrivate *priv = stream->priv;
4218 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
4219 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
4220 family == G_SOCKET_FAMILY_IPV6, NULL);
4222 g_mutex_lock (&priv->lock);
4223 if (family == G_SOCKET_FAMILY_IPV6)
4224 socket = priv->socket_v6[1];
4226 socket = priv->socket_v4[1];
4229 socket = g_object_ref (socket);
4230 g_mutex_unlock (&priv->lock);
4236 * gst_rtsp_stream_get_rtp_multicast_socket:
4237 * @stream: a #GstRTSPStream
4238 * @family: the socket family
4240 * Get the multicast RTP socket from @stream for a @family.
4242 * Returns: (transfer full) (nullable): the multicast RTP socket or %NULL if no
4243 * socket could be allocated for @family. Unref after usage
4246 gst_rtsp_stream_get_rtp_multicast_socket (GstRTSPStream * stream,
4247 GSocketFamily family)
4249 GstRTSPStreamPrivate *priv = stream->priv;
4252 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
4253 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
4254 family == G_SOCKET_FAMILY_IPV6, NULL);
4256 g_mutex_lock (&priv->lock);
4257 if (family == G_SOCKET_FAMILY_IPV6)
4258 socket = priv->mcast_socket_v6[0];
4260 socket = priv->mcast_socket_v4[0];
4263 socket = g_object_ref (socket);
4264 g_mutex_unlock (&priv->lock);
4270 * gst_rtsp_stream_get_rtcp_multicast_socket:
4271 * @stream: a #GstRTSPStream
4272 * @family: the socket family
4274 * Get the multicast RTCP socket from @stream for a @family.
4276 * Returns: (transfer full) (nullable): the multicast RTCP socket or %NULL if no
4277 * socket could be allocated for @family. Unref after usage
4280 gst_rtsp_stream_get_rtcp_multicast_socket (GstRTSPStream * stream,
4281 GSocketFamily family)
4283 GstRTSPStreamPrivate *priv = stream->priv;
4286 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
4287 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
4288 family == G_SOCKET_FAMILY_IPV6, NULL);
4290 g_mutex_lock (&priv->lock);
4291 if (family == G_SOCKET_FAMILY_IPV6)
4292 socket = priv->mcast_socket_v6[1];
4294 socket = priv->mcast_socket_v4[1];
4297 socket = g_object_ref (socket);
4298 g_mutex_unlock (&priv->lock);
4304 * gst_rtsp_stream_set_seqnum:
4305 * @stream: a #GstRTSPStream
4306 * @seqnum: a new sequence number
4308 * Configure the sequence number in the payloader of @stream to @seqnum.
4311 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
4313 GstRTSPStreamPrivate *priv;
4315 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
4317 priv = stream->priv;
4319 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
4323 * gst_rtsp_stream_get_seqnum:
4324 * @stream: a #GstRTSPStream
4326 * Get the configured sequence number in the payloader of @stream.
4328 * Returns: the sequence number of the payloader.
4331 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
4333 GstRTSPStreamPrivate *priv;
4336 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
4338 priv = stream->priv;
4340 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
4346 * gst_rtsp_stream_transport_filter:
4347 * @stream: a #GstRTSPStream
4348 * @func: (scope call) (allow-none): a callback
4349 * @user_data: (closure): user data passed to @func
4351 * Call @func for each transport managed by @stream. The result value of @func
4352 * determines what happens to the transport. @func will be called with @stream
4353 * locked so no further actions on @stream can be performed from @func.
4355 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
4358 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
4360 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
4361 * will also be added with an additional ref to the result #GList of this
4364 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
4366 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
4367 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
4368 * element in the #GList should be unreffed before the list is freed.
4371 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
4372 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
4374 GstRTSPStreamPrivate *priv;
4375 GList *result, *walk, *next;
4376 GHashTable *visited = NULL;
4379 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
4381 priv = stream->priv;
4385 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
4387 g_mutex_lock (&priv->lock);
4389 cookie = priv->transports_cookie;
4390 for (walk = priv->transports; walk; walk = next) {
4391 GstRTSPStreamTransport *trans = walk->data;
4392 GstRTSPFilterResult res;
4395 next = g_list_next (walk);
4398 /* only visit each transport once */
4399 if (g_hash_table_contains (visited, trans))
4402 g_hash_table_add (visited, g_object_ref (trans));
4403 g_mutex_unlock (&priv->lock);
4405 res = func (stream, trans, user_data);
4407 g_mutex_lock (&priv->lock);
4409 res = GST_RTSP_FILTER_REF;
4411 changed = (cookie != priv->transports_cookie);
4414 case GST_RTSP_FILTER_REMOVE:
4415 update_transport (stream, trans, FALSE);
4417 case GST_RTSP_FILTER_REF:
4418 result = g_list_prepend (result, g_object_ref (trans));
4420 case GST_RTSP_FILTER_KEEP:
4427 g_mutex_unlock (&priv->lock);
4430 g_hash_table_unref (visited);
4435 static GstPadProbeReturn
4436 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
4438 GstRTSPStreamPrivate *priv;
4439 GstRTSPStream *stream;
4440 GstBuffer *buffer = NULL;
4443 priv = stream->priv;
4445 GST_DEBUG_OBJECT (pad, "now blocking");
4447 g_mutex_lock (&priv->lock);
4448 priv->blocking = TRUE;
4450 if ((info->type & GST_PAD_PROBE_TYPE_BUFFER)) {
4451 buffer = gst_pad_probe_info_get_buffer (info);
4452 } else if ((info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST)) {
4453 GstBufferList *list = gst_pad_probe_info_get_buffer_list (info);
4454 buffer = gst_buffer_list_get (list, 0);
4456 g_assert_not_reached ();
4460 priv->position = GST_BUFFER_TIMESTAMP (buffer);
4461 GST_DEBUG_OBJECT (stream, "buffer position: %" GST_TIME_FORMAT,
4462 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)));
4463 g_mutex_unlock (&priv->lock);
4465 gst_element_post_message (priv->payloader,
4466 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
4467 gst_structure_new_empty ("GstRTSPStreamBlocking")));
4469 return GST_PAD_PROBE_OK;
4473 set_blocked (GstRTSPStream * stream, gboolean blocked)
4475 GstRTSPStreamPrivate *priv;
4478 GST_DEBUG_OBJECT (stream, "blocked: %d", blocked);
4480 priv = stream->priv;
4483 for (i = 0; i < 2; i++) {
4484 if (priv->blocked_id[i] != 0)
4486 if (priv->send_src[i]) {
4487 priv->blocking = FALSE;
4488 priv->blocked_id[i] = gst_pad_add_probe (priv->send_src[i],
4489 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
4490 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
4491 g_object_ref (stream), g_object_unref);
4495 for (i = 0; i < 2; i++) {
4496 if (priv->blocked_id[i] != 0) {
4497 gst_pad_remove_probe (priv->send_src[i], priv->blocked_id[i]);
4498 priv->blocked_id[i] = 0;
4501 priv->blocking = FALSE;
4506 * gst_rtsp_stream_set_blocked:
4507 * @stream: a #GstRTSPStream
4508 * @blocked: boolean indicating we should block or unblock
4510 * Blocks or unblocks the dataflow on @stream.
4512 * Returns: %TRUE on success
4515 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
4517 GstRTSPStreamPrivate *priv;
4519 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4521 priv = stream->priv;
4522 g_mutex_lock (&priv->lock);
4523 set_blocked (stream, blocked);
4524 g_mutex_unlock (&priv->lock);
4530 * gst_rtsp_stream_ublock_linked:
4531 * @stream: a #GstRTSPStream
4533 * Unblocks the dataflow on @stream if it is linked.
4535 * Returns: %TRUE on success
4538 gst_rtsp_stream_unblock_linked (GstRTSPStream * stream)
4540 GstRTSPStreamPrivate *priv;
4542 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4544 priv = stream->priv;
4545 g_mutex_lock (&priv->lock);
4546 if (priv->send_src[0] && gst_pad_is_linked (priv->send_src[0]))
4547 set_blocked (stream, FALSE);
4548 g_mutex_unlock (&priv->lock);
4554 * gst_rtsp_stream_is_blocking:
4555 * @stream: a #GstRTSPStream
4557 * Check if @stream is blocking on a #GstBuffer.
4559 * Returns: %TRUE if @stream is blocking
4562 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
4564 GstRTSPStreamPrivate *priv;
4567 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4569 priv = stream->priv;
4571 g_mutex_lock (&priv->lock);
4572 result = priv->blocking;
4573 g_mutex_unlock (&priv->lock);
4579 * gst_rtsp_stream_query_position:
4580 * @stream: a #GstRTSPStream
4581 * @position: (out): current position of a #GstRTSPStream
4583 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
4584 * the RTP parts of the pipeline and not the RTCP parts.
4586 * Returns: %TRUE if the position could be queried
4589 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
4591 GstRTSPStreamPrivate *priv;
4595 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4597 /* query position: if no sinks have been added yet,
4598 * we obtain the position from the pad otherwise we query the sinks */
4600 priv = stream->priv;
4602 g_mutex_lock (&priv->lock);
4603 /* depending on the transport type, it should query corresponding sink */
4604 if (priv->configured_protocols & GST_RTSP_LOWER_TRANS_UDP)
4605 sink = priv->udpsink[0];
4606 else if (priv->configured_protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
4607 sink = priv->mcast_udpsink[0];
4609 sink = priv->appsink[0];
4612 gst_object_ref (sink);
4613 } else if (priv->send_src[0]) {
4614 pad = gst_object_ref (priv->send_src[0]);
4616 g_mutex_unlock (&priv->lock);
4617 GST_WARNING_OBJECT (stream, "Couldn't obtain postion: erroneous pipeline");
4620 g_mutex_unlock (&priv->lock);
4623 if (!gst_element_query_position (sink, GST_FORMAT_TIME, position)) {
4624 GST_WARNING_OBJECT (stream,
4625 "Couldn't obtain postion: position query failed");
4626 gst_object_unref (sink);
4629 gst_object_unref (sink);
4632 const GstSegment *segment;
4634 event = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
4636 GST_WARNING_OBJECT (stream, "Couldn't obtain postion: no segment event");
4637 gst_object_unref (pad);
4641 gst_event_parse_segment (event, &segment);
4642 if (segment->format != GST_FORMAT_TIME) {
4645 g_mutex_lock (&priv->lock);
4646 *position = priv->position;
4647 g_mutex_unlock (&priv->lock);
4649 gst_segment_to_stream_time (segment, GST_FORMAT_TIME, *position);
4651 gst_event_unref (event);
4652 gst_object_unref (pad);
4659 * gst_rtsp_stream_query_stop:
4660 * @stream: a #GstRTSPStream
4661 * @stop: (out): current stop of a #GstRTSPStream
4663 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
4664 * the RTP parts of the pipeline and not the RTCP parts.
4666 * Returns: %TRUE if the stop could be queried
4669 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
4671 GstRTSPStreamPrivate *priv;
4675 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4677 /* query stop position: if no sinks have been added yet,
4678 * we obtain the stop position from the pad otherwise we query the sinks */
4680 priv = stream->priv;
4682 g_mutex_lock (&priv->lock);
4683 /* depending on the transport type, it should query corresponding sink */
4684 if (priv->configured_protocols & GST_RTSP_LOWER_TRANS_UDP)
4685 sink = priv->udpsink[0];
4686 else if (priv->configured_protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
4687 sink = priv->mcast_udpsink[0];
4689 sink = priv->appsink[0];
4692 gst_object_ref (sink);
4693 } else if (priv->send_src[0]) {
4694 pad = gst_object_ref (priv->send_src[0]);
4696 g_mutex_unlock (&priv->lock);
4697 GST_WARNING_OBJECT (stream, "Couldn't obtain stop: erroneous pipeline");
4700 g_mutex_unlock (&priv->lock);
4706 query = gst_query_new_segment (GST_FORMAT_TIME);
4707 if (!gst_element_query (sink, query)) {
4708 GST_WARNING_OBJECT (stream, "Couldn't obtain stop: element query failed");
4709 gst_query_unref (query);
4710 gst_object_unref (sink);
4713 gst_query_parse_segment (query, NULL, &format, NULL, stop);
4714 if (format != GST_FORMAT_TIME)
4716 gst_query_unref (query);
4717 gst_object_unref (sink);
4720 const GstSegment *segment;
4722 event = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
4724 GST_WARNING_OBJECT (stream, "Couldn't obtain stop: no segment event");
4725 gst_object_unref (pad);
4728 gst_event_parse_segment (event, &segment);
4729 if (segment->format != GST_FORMAT_TIME) {
4732 *stop = segment->stop;
4734 *stop = segment->duration;
4736 *stop = gst_segment_to_stream_time (segment, GST_FORMAT_TIME, *stop);
4738 gst_event_unref (event);
4739 gst_object_unref (pad);
4746 * gst_rtsp_stream_seekable:
4747 * @stream: a #GstRTSPStream
4749 * Checks whether the individual @stream is seekable.
4751 * Returns: %TRUE if @stream is seekable, else %FALSE.
4754 gst_rtsp_stream_seekable (GstRTSPStream * stream)
4756 GstRTSPStreamPrivate *priv;
4758 GstQuery *query = NULL;
4759 gboolean seekable = FALSE;
4761 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4763 /* query stop position: if no sinks have been added yet,
4764 * we obtain the stop position from the pad otherwise we query the sinks */
4766 priv = stream->priv;
4768 g_mutex_lock (&priv->lock);
4769 /* depending on the transport type, it should query corresponding sink */
4771 pad = gst_object_ref (priv->srcpad);
4773 g_mutex_unlock (&priv->lock);
4774 GST_WARNING_OBJECT (stream, "Pad not available, can't query seekability");
4777 g_mutex_unlock (&priv->lock);
4779 query = gst_query_new_seeking (GST_FORMAT_TIME);
4780 if (!gst_pad_query (pad, query)) {
4781 GST_WARNING_OBJECT (stream, "seeking query failed");
4784 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
4788 gst_object_unref (pad);
4790 gst_query_unref (query);
4792 GST_DEBUG_OBJECT (stream, "Returning %d", seekable);
4798 * gst_rtsp_stream_complete_stream:
4799 * @stream: a #GstRTSPStream
4800 * @transport: a #GstRTSPTransport
4802 * Add a receiver and sender part to the pipeline based on the transport from
4805 * Returns: %TRUE if the stream has been sucessfully updated.
4808 gst_rtsp_stream_complete_stream (GstRTSPStream * stream,
4809 const GstRTSPTransport * transport)
4811 GstRTSPStreamPrivate *priv;
4813 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4815 priv = stream->priv;
4816 GST_DEBUG_OBJECT (stream, "complete stream");
4818 g_mutex_lock (&priv->lock);
4820 if (!(priv->allowed_protocols & transport->lower_transport))
4821 goto unallowed_transport;
4823 if (!create_receiver_part (stream, transport))
4824 goto create_receiver_error;
4826 /* in the RECORD case, we only add RTCP sender part */
4827 if (!create_sender_part (stream, transport))
4828 goto create_sender_error;
4830 priv->configured_protocols |= transport->lower_transport;
4832 priv->is_complete = TRUE;
4833 g_mutex_unlock (&priv->lock);
4835 GST_DEBUG_OBJECT (stream, "pipeline sucsessfully updated");
4838 create_receiver_error:
4839 create_sender_error:
4840 unallowed_transport:
4842 g_mutex_unlock (&priv->lock);
4848 * gst_rtsp_stream_is_complete:
4849 * @stream: a #GstRTSPStream
4851 * Checks whether the stream is complete, contains the receiver and the sender
4852 * parts. As the stream contains sink(s) element(s), it's possible to perform
4853 * seek operations on it.
4855 * Returns: %TRUE if the stream contains at least one sink element.
4858 gst_rtsp_stream_is_complete (GstRTSPStream * stream)
4860 GstRTSPStreamPrivate *priv;
4861 gboolean ret = FALSE;
4863 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4865 priv = stream->priv;
4866 g_mutex_lock (&priv->lock);
4867 ret = priv->is_complete;
4868 g_mutex_unlock (&priv->lock);
4874 * gst_rtsp_stream_is_sender:
4875 * @stream: a #GstRTSPStream
4877 * Checks whether the stream is a sender.
4879 * Returns: %TRUE if the stream is a sender and %FALSE otherwise.
4882 gst_rtsp_stream_is_sender (GstRTSPStream * stream)
4884 GstRTSPStreamPrivate *priv;
4885 gboolean ret = FALSE;
4887 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4889 priv = stream->priv;
4890 g_mutex_lock (&priv->lock);
4891 ret = (priv->srcpad != NULL);
4892 g_mutex_unlock (&priv->lock);
4898 * gst_rtsp_stream_is_receiver:
4899 * @stream: a #GstRTSPStream
4901 * Checks whether the stream is a receiver.
4903 * Returns: %TRUE if the stream is a receiver and %FALSE otherwise.
4906 gst_rtsp_stream_is_receiver (GstRTSPStream * stream)
4908 GstRTSPStreamPrivate *priv;
4909 gboolean ret = FALSE;
4911 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4913 priv = stream->priv;
4914 g_mutex_lock (&priv->lock);
4915 ret = (priv->sinkpad != NULL);
4916 g_mutex_unlock (&priv->lock);
4921 #define AES_128_KEY_LEN 16
4922 #define AES_256_KEY_LEN 32
4924 #define HMAC_32_KEY_LEN 4
4925 #define HMAC_80_KEY_LEN 10
4928 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
4930 const gchar *srtp_cipher;
4931 const gchar *srtp_auth;
4932 const GstMIKEYPayload *sp;
4935 /* loop over Security policy until we find one containing policy */
4937 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
4940 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
4944 /* the default ciphers */
4945 srtp_cipher = "aes-128-icm";
4946 srtp_auth = "hmac-sha1-80";
4948 /* now override the defaults with what is in the Security Policy */
4952 /* collect all the params and go over them */
4953 len = gst_mikey_payload_sp_get_n_params (sp);
4954 for (i = 0; i < len; i++) {
4955 const GstMIKEYPayloadSPParam *param =
4956 gst_mikey_payload_sp_get_param (sp, i);
4958 switch (param->type) {
4959 case GST_MIKEY_SP_SRTP_ENC_ALG:
4960 switch (param->val[0]) {
4962 srtp_cipher = "null";
4966 srtp_cipher = "aes-128-icm";
4972 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
4973 switch (param->val[0]) {
4974 case AES_128_KEY_LEN:
4975 srtp_cipher = "aes-128-icm";
4977 case AES_256_KEY_LEN:
4978 srtp_cipher = "aes-256-icm";
4984 case GST_MIKEY_SP_SRTP_AUTH_ALG:
4985 switch (param->val[0]) {
4991 srtp_auth = "hmac-sha1-80";
4997 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
4998 switch (param->val[0]) {
4999 case HMAC_32_KEY_LEN:
5000 srtp_auth = "hmac-sha1-32";
5002 case HMAC_80_KEY_LEN:
5003 srtp_auth = "hmac-sha1-80";
5009 case GST_MIKEY_SP_SRTP_SRTP_ENC:
5011 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
5018 /* now configure the SRTP parameters */
5019 gst_caps_set_simple (caps,
5020 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
5021 "srtp-auth", G_TYPE_STRING, srtp_auth,
5022 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
5023 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
5029 handle_mikey_data (GstRTSPStream * stream, guint8 * data, gsize size)
5031 GstMIKEYMessage *msg;
5033 GstCaps *caps = NULL;
5034 GstMIKEYPayloadKEMAC *kemac;
5035 const GstMIKEYPayloadKeyData *pkd;
5038 /* the MIKEY message contains a CSB or crypto session bundle. It is a
5039 * set of Crypto Sessions protected with the same master key.
5040 * In the context of SRTP, an RTP and its RTCP stream is part of a
5042 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
5045 /* we can only handle SRTP crypto sessions for now */
5046 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
5047 goto invalid_map_type;
5049 /* get the number of crypto sessions. This maps SSRC to its
5050 * security parameters */
5051 n_cs = gst_mikey_message_get_n_cs (msg);
5053 goto no_crypto_sessions;
5055 /* we also need keys */
5056 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
5057 (msg, GST_MIKEY_PT_KEMAC, 0)))
5060 /* we don't support encrypted keys */
5061 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
5062 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
5063 goto unsupported_encryption;
5065 /* get Key data sub-payload */
5066 pkd = (const GstMIKEYPayloadKeyData *)
5067 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
5070 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
5073 /* go over all crypto sessions and create the security policy for each
5075 for (i = 0; i < n_cs; i++) {
5076 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
5078 caps = gst_caps_new_simple ("application/x-srtp",
5079 "ssrc", G_TYPE_UINT, map->ssrc,
5080 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
5081 mikey_apply_policy (caps, msg, map->policy);
5083 gst_rtsp_stream_update_crypto (stream, map->ssrc, caps);
5084 gst_caps_unref (caps);
5086 gst_mikey_message_unref (msg);
5087 gst_buffer_unref (key);
5094 GST_DEBUG_OBJECT (stream, "failed to parse MIKEY message");
5099 GST_DEBUG_OBJECT (stream, "invalid map type %d", msg->map_type);
5100 goto cleanup_message;
5104 GST_DEBUG_OBJECT (stream, "no crypto sessions");
5105 goto cleanup_message;
5109 GST_DEBUG_OBJECT (stream, "no keys found");
5110 goto cleanup_message;
5112 unsupported_encryption:
5114 GST_DEBUG_OBJECT (stream, "unsupported key encryption");
5115 goto cleanup_message;
5119 gst_mikey_message_unref (msg);
5124 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
5127 strip_chars (gchar * str)
5134 if (!IS_STRIP_CHAR (str[len]))
5138 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
5139 memmove (str, s, len + 1);
5143 * gst_rtsp_stream_handle_keymgmt:
5144 * @stream: a #GstRTSPStream
5145 * @keymgmt: a keymgmt header
5147 * Parse and handle a KeyMgmt header.
5151 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
5152 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
5155 gst_rtsp_stream_handle_keymgmt (GstRTSPStream * stream, const gchar * keymgmt)
5160 specs = g_strsplit (keymgmt, ",", 0);
5161 for (i = 0; specs[i]; i++) {
5164 split = g_strsplit (specs[i], ";", 0);
5165 for (j = 0; split[j]; j++) {
5166 g_strstrip (split[j]);
5167 if (g_str_has_prefix (split[j], "prot=")) {
5168 g_strstrip (split[j] + 5);
5169 if (!g_str_equal (split[j] + 5, "mikey"))
5171 GST_DEBUG ("found mikey");
5172 } else if (g_str_has_prefix (split[j], "uri=")) {
5173 strip_chars (split[j] + 4);
5174 GST_DEBUG ("found uri '%s'", split[j] + 4);
5175 } else if (g_str_has_prefix (split[j], "data=")) {
5178 strip_chars (split[j] + 5);
5179 GST_DEBUG ("found data '%s'", split[j] + 5);
5180 data = g_base64_decode_inplace (split[j] + 5, &size);
5181 handle_mikey_data (stream, data, size);
5192 * gst_rtsp_stream_get_ulpfec_pt:
5194 * Returns: the payload type used for ULPFEC protection packets
5199 gst_rtsp_stream_get_ulpfec_pt (GstRTSPStream * stream)
5203 g_mutex_lock (&stream->priv->lock);
5204 res = stream->priv->ulpfec_pt;
5205 g_mutex_unlock (&stream->priv->lock);
5211 * gst_rtsp_stream_set_ulpfec_pt:
5213 * Set the payload type to be used for ULPFEC protection packets
5218 gst_rtsp_stream_set_ulpfec_pt (GstRTSPStream * stream, guint pt)
5220 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
5222 g_mutex_lock (&stream->priv->lock);
5223 stream->priv->ulpfec_pt = pt;
5224 if (stream->priv->ulpfec_encoder) {
5225 g_object_set (stream->priv->ulpfec_encoder, "pt", pt, NULL);
5227 g_mutex_unlock (&stream->priv->lock);
5231 * gst_rtsp_stream_request_ulpfec_decoder:
5233 * Creating a rtpulpfecdec element
5235 * Returns: (transfer full) (nullable): a #GstElement.
5240 gst_rtsp_stream_request_ulpfec_decoder (GstRTSPStream * stream,
5241 GstElement * rtpbin, guint sessid)
5243 GObject *internal_storage = NULL;
5245 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
5246 stream->priv->ulpfec_decoder =
5247 gst_object_ref (gst_element_factory_make ("rtpulpfecdec", NULL));
5249 g_signal_emit_by_name (G_OBJECT (rtpbin), "get-internal-storage", sessid,
5251 g_object_set (stream->priv->ulpfec_decoder, "storage", internal_storage,
5253 g_object_unref (internal_storage);
5254 update_ulpfec_decoder_pt (stream);
5256 return stream->priv->ulpfec_decoder;
5260 * gst_rtsp_stream_request_ulpfec_encoder:
5262 * Creating a rtpulpfecenc element
5264 * Returns: (transfer full) (nullable): a #GstElement.
5269 gst_rtsp_stream_request_ulpfec_encoder (GstRTSPStream * stream, guint sessid)
5271 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
5273 if (!stream->priv->ulpfec_percentage)
5276 stream->priv->ulpfec_encoder =
5277 gst_object_ref (gst_element_factory_make ("rtpulpfecenc", NULL));
5279 g_object_set (stream->priv->ulpfec_encoder, "pt", stream->priv->ulpfec_pt,
5280 "percentage", stream->priv->ulpfec_percentage, NULL);
5282 return stream->priv->ulpfec_encoder;
5286 * gst_rtsp_stream_set_ulpfec_percentage:
5288 * Sets the amount of redundancy to apply when creating ULPFEC
5289 * protection packets.
5294 gst_rtsp_stream_set_ulpfec_percentage (GstRTSPStream * stream, guint percentage)
5296 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
5298 g_mutex_lock (&stream->priv->lock);
5299 stream->priv->ulpfec_percentage = percentage;
5300 if (stream->priv->ulpfec_encoder) {
5301 g_object_set (stream->priv->ulpfec_encoder, "percentage", percentage, NULL);
5303 g_mutex_unlock (&stream->priv->lock);
5307 * gst_rtsp_stream_get_ulpfec_percentage:
5309 * Returns: the amount of redundancy applied when creating ULPFEC
5310 * protection packets.
5315 gst_rtsp_stream_get_ulpfec_percentage (GstRTSPStream * stream)
5319 g_mutex_lock (&stream->priv->lock);
5320 res = stream->priv->ulpfec_percentage;
5321 g_mutex_unlock (&stream->priv->lock);