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 "rtsp-stream.h"
60 #define GST_RTSP_STREAM_GET_PRIVATE(obj) \
61 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
65 GstRTSPStreamTransport *transport;
67 /* RTP and RTCP source */
68 GstElement *udpsrc[2];
70 } GstRTSPMulticastTransportSource;
72 struct _GstRTSPStreamPrivate
76 /* Only one pad is ever set */
77 GstPad *srcpad, *sinkpad;
78 GstElement *payloader;
83 GstRTSPProfile profiles;
84 GstRTSPLowerTrans protocols;
86 /* pads on the rtpbin */
87 GstPad *send_rtp_sink;
92 /* the RTPSession object */
95 /* SRTP encoder/decoder */
100 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
102 GstElement *udpsrc_v4[2];
104 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
106 GstElement *udpsrc_v6[2];
108 GstElement *udpsink[2];
110 /* for TCP transport */
111 GstElement *appsrc[2];
112 GstElement *appqueue[2];
113 GstElement *appsink[2];
116 GstElement *funnel[2];
121 GstClockTime rtx_time;
123 /* server ports for sending/receiving over ipv4 */
124 GstRTSPRange server_port_v4;
125 GstRTSPAddress *server_addr_v4;
128 /* server ports for sending/receiving over ipv6 */
129 GstRTSPRange server_port_v6;
130 GstRTSPAddress *server_addr_v6;
133 /* multicast addresses */
134 GstRTSPAddressPool *pool;
135 GstRTSPAddress *addr_v4;
136 GstRTSPAddress *addr_v6;
138 /* the caps of the stream */
142 /* transports we stream to */
145 guint transports_cookie;
147 GList *tr_cache_rtcp;
148 guint tr_cache_cookie_rtp;
149 guint tr_cache_cookie_rtcp;
152 /* UDP sources for UDP multicast transports */
153 GList *transport_sources;
157 /* stream blocking */
161 /* pt->caps map for RECORD streams */
165 #define DEFAULT_CONTROL NULL
166 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
167 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
168 GST_RTSP_LOWER_TRANS_TCP
181 SIGNAL_NEW_RTP_ENCODER,
182 SIGNAL_NEW_RTCP_ENCODER,
186 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
187 #define GST_CAT_DEFAULT rtsp_stream_debug
189 static GQuark ssrc_stream_map_key;
191 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
192 GValue * value, GParamSpec * pspec);
193 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
194 const GValue * value, GParamSpec * pspec);
196 static void gst_rtsp_stream_finalize (GObject * obj);
198 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
200 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
203 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
205 GObjectClass *gobject_class;
207 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
209 gobject_class = G_OBJECT_CLASS (klass);
211 gobject_class->get_property = gst_rtsp_stream_get_property;
212 gobject_class->set_property = gst_rtsp_stream_set_property;
213 gobject_class->finalize = gst_rtsp_stream_finalize;
215 g_object_class_install_property (gobject_class, PROP_CONTROL,
216 g_param_spec_string ("control", "Control",
217 "The control string for this stream", DEFAULT_CONTROL,
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220 g_object_class_install_property (gobject_class, PROP_PROFILES,
221 g_param_spec_flags ("profiles", "Profiles",
222 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
223 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
226 g_param_spec_flags ("protocols", "Protocols",
227 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
228 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
231 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
232 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
233 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
235 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
236 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
237 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
238 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
240 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
242 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
246 gst_rtsp_stream_init (GstRTSPStream * stream)
248 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
250 GST_DEBUG ("new stream %p", stream);
255 priv->control = g_strdup (DEFAULT_CONTROL);
256 priv->profiles = DEFAULT_PROFILES;
257 priv->protocols = DEFAULT_PROTOCOLS;
259 g_mutex_init (&priv->lock);
261 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
262 NULL, (GDestroyNotify) gst_caps_unref);
263 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
264 (GDestroyNotify) gst_caps_unref);
268 gst_rtsp_stream_finalize (GObject * obj)
270 GstRTSPStream *stream;
271 GstRTSPStreamPrivate *priv;
273 stream = GST_RTSP_STREAM (obj);
276 GST_DEBUG ("finalize stream %p", stream);
278 /* we really need to be unjoined now */
279 g_return_if_fail (!priv->is_joined);
282 gst_rtsp_address_free (priv->addr_v4);
284 gst_rtsp_address_free (priv->addr_v6);
285 if (priv->server_addr_v4)
286 gst_rtsp_address_free (priv->server_addr_v4);
287 if (priv->server_addr_v6)
288 gst_rtsp_address_free (priv->server_addr_v6);
290 g_object_unref (priv->pool);
292 g_object_unref (priv->rtxsend);
294 gst_object_unref (priv->payloader);
296 gst_object_unref (priv->srcpad);
298 gst_object_unref (priv->sinkpad);
299 g_free (priv->control);
300 g_mutex_clear (&priv->lock);
302 g_hash_table_unref (priv->keys);
303 g_hash_table_destroy (priv->ptmap);
305 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
309 gst_rtsp_stream_get_property (GObject * object, guint propid,
310 GValue * value, GParamSpec * pspec)
312 GstRTSPStream *stream = GST_RTSP_STREAM (object);
316 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
319 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
322 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
325 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
330 gst_rtsp_stream_set_property (GObject * object, guint propid,
331 const GValue * value, GParamSpec * pspec)
333 GstRTSPStream *stream = GST_RTSP_STREAM (object);
337 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
340 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
343 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
346 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
351 * gst_rtsp_stream_new:
354 * @payloader: a #GstElement
356 * Create a new media stream with index @idx that handles RTP data on
357 * @pad and has a payloader element @payloader if @pad is a source pad
358 * or a depayloader element @payloader if @pad is a sink pad.
360 * Returns: (transfer full): a new #GstRTSPStream
363 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
365 GstRTSPStreamPrivate *priv;
366 GstRTSPStream *stream;
368 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
369 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
371 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
374 priv->payloader = gst_object_ref (payloader);
375 if (GST_PAD_IS_SRC (pad))
376 priv->srcpad = gst_object_ref (pad);
378 priv->sinkpad = gst_object_ref (pad);
384 * gst_rtsp_stream_get_index:
385 * @stream: a #GstRTSPStream
387 * Get the stream index.
389 * Return: the stream index.
392 gst_rtsp_stream_get_index (GstRTSPStream * stream)
394 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
396 return stream->priv->idx;
400 * gst_rtsp_stream_get_pt:
401 * @stream: a #GstRTSPStream
403 * Get the stream payload type.
405 * Return: the stream payload type.
408 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
410 GstRTSPStreamPrivate *priv;
413 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
417 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
423 * gst_rtsp_stream_get_srcpad:
424 * @stream: a #GstRTSPStream
426 * Get the srcpad associated with @stream.
428 * Returns: (transfer full): the srcpad. Unref after usage.
431 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
433 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
435 if (!stream->priv->srcpad)
438 return gst_object_ref (stream->priv->srcpad);
442 * gst_rtsp_stream_get_sinkpad:
443 * @stream: a #GstRTSPStream
445 * Get the sinkpad associated with @stream.
447 * Returns: (transfer full): the sinkpad. Unref after usage.
450 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
452 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
454 if (!stream->priv->sinkpad)
457 return gst_object_ref (stream->priv->sinkpad);
461 * gst_rtsp_stream_get_control:
462 * @stream: a #GstRTSPStream
464 * Get the control string to identify this stream.
466 * Returns: (transfer full): the control string. g_free() after usage.
469 gst_rtsp_stream_get_control (GstRTSPStream * stream)
471 GstRTSPStreamPrivate *priv;
474 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
478 g_mutex_lock (&priv->lock);
479 if ((result = g_strdup (priv->control)) == NULL)
480 result = g_strdup_printf ("stream=%u", priv->idx);
481 g_mutex_unlock (&priv->lock);
487 * gst_rtsp_stream_set_control:
488 * @stream: a #GstRTSPStream
489 * @control: a control string
491 * Set the control string in @stream.
494 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
496 GstRTSPStreamPrivate *priv;
498 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
502 g_mutex_lock (&priv->lock);
503 g_free (priv->control);
504 priv->control = g_strdup (control);
505 g_mutex_unlock (&priv->lock);
509 * gst_rtsp_stream_has_control:
510 * @stream: a #GstRTSPStream
511 * @control: a control string
513 * Check if @stream has the control string @control.
515 * Returns: %TRUE is @stream has @control as the control string
518 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
520 GstRTSPStreamPrivate *priv;
523 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
527 g_mutex_lock (&priv->lock);
529 res = (g_strcmp0 (priv->control, control) == 0);
533 if (sscanf (control, "stream=%u", &streamid) > 0)
534 res = (streamid == priv->idx);
538 g_mutex_unlock (&priv->lock);
544 * gst_rtsp_stream_set_mtu:
545 * @stream: a #GstRTSPStream
548 * Configure the mtu in the payloader of @stream to @mtu.
551 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
553 GstRTSPStreamPrivate *priv;
555 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
559 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
561 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
565 * gst_rtsp_stream_get_mtu:
566 * @stream: a #GstRTSPStream
568 * Get the configured MTU in the payloader of @stream.
570 * Returns: the MTU of the payloader.
573 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
575 GstRTSPStreamPrivate *priv;
578 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
582 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
587 /* Update the dscp qos property on the udp sinks */
589 update_dscp_qos (GstRTSPStream * stream)
591 GstRTSPStreamPrivate *priv;
593 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
597 if (priv->udpsink[0]) {
598 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
602 if (priv->udpsink[1]) {
603 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
609 * gst_rtsp_stream_set_dscp_qos:
610 * @stream: a #GstRTSPStream
611 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
613 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
616 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
618 GstRTSPStreamPrivate *priv;
620 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
624 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
626 if (dscp_qos < -1 || dscp_qos > 63) {
627 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
631 priv->dscp_qos = dscp_qos;
633 update_dscp_qos (stream);
637 * gst_rtsp_stream_get_dscp_qos:
638 * @stream: a #GstRTSPStream
640 * Get the configured DSCP QoS in of the outgoing sockets.
642 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
645 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
647 GstRTSPStreamPrivate *priv;
649 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
653 return priv->dscp_qos;
657 * gst_rtsp_stream_is_transport_supported:
658 * @stream: a #GstRTSPStream
659 * @transport: (transfer none): a #GstRTSPTransport
661 * Check if @transport can be handled by stream
663 * Returns: %TRUE if @transport can be handled by @stream.
666 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
667 GstRTSPTransport * transport)
669 GstRTSPStreamPrivate *priv;
671 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
675 g_mutex_lock (&priv->lock);
676 if (transport->trans != GST_RTSP_TRANS_RTP)
677 goto unsupported_transmode;
679 if (!(transport->profile & priv->profiles))
680 goto unsupported_profile;
682 if (!(transport->lower_transport & priv->protocols))
683 goto unsupported_ltrans;
685 g_mutex_unlock (&priv->lock);
690 unsupported_transmode:
692 GST_DEBUG ("unsupported transport mode %d", transport->trans);
693 g_mutex_unlock (&priv->lock);
698 GST_DEBUG ("unsupported profile %d", transport->profile);
699 g_mutex_unlock (&priv->lock);
704 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
705 g_mutex_unlock (&priv->lock);
711 * gst_rtsp_stream_set_profiles:
712 * @stream: a #GstRTSPStream
713 * @profiles: the new profiles
715 * Configure the allowed profiles for @stream.
718 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
720 GstRTSPStreamPrivate *priv;
722 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
726 g_mutex_lock (&priv->lock);
727 priv->profiles = profiles;
728 g_mutex_unlock (&priv->lock);
732 * gst_rtsp_stream_get_profiles:
733 * @stream: a #GstRTSPStream
735 * Get the allowed profiles of @stream.
737 * Returns: a #GstRTSPProfile
740 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
742 GstRTSPStreamPrivate *priv;
745 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
749 g_mutex_lock (&priv->lock);
750 res = priv->profiles;
751 g_mutex_unlock (&priv->lock);
757 * gst_rtsp_stream_set_protocols:
758 * @stream: a #GstRTSPStream
759 * @protocols: the new flags
761 * Configure the allowed lower transport for @stream.
764 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
765 GstRTSPLowerTrans protocols)
767 GstRTSPStreamPrivate *priv;
769 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
773 g_mutex_lock (&priv->lock);
774 priv->protocols = protocols;
775 g_mutex_unlock (&priv->lock);
779 * gst_rtsp_stream_get_protocols:
780 * @stream: a #GstRTSPStream
782 * Get the allowed protocols of @stream.
784 * Returns: a #GstRTSPLowerTrans
787 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
789 GstRTSPStreamPrivate *priv;
790 GstRTSPLowerTrans res;
792 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
793 GST_RTSP_LOWER_TRANS_UNKNOWN);
797 g_mutex_lock (&priv->lock);
798 res = priv->protocols;
799 g_mutex_unlock (&priv->lock);
805 * gst_rtsp_stream_set_address_pool:
806 * @stream: a #GstRTSPStream
807 * @pool: (transfer none): a #GstRTSPAddressPool
809 * configure @pool to be used as the address pool of @stream.
812 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
813 GstRTSPAddressPool * pool)
815 GstRTSPStreamPrivate *priv;
816 GstRTSPAddressPool *old;
818 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
822 GST_LOG_OBJECT (stream, "set address pool %p", pool);
824 g_mutex_lock (&priv->lock);
825 if ((old = priv->pool) != pool)
826 priv->pool = pool ? g_object_ref (pool) : NULL;
829 g_mutex_unlock (&priv->lock);
832 g_object_unref (old);
836 * gst_rtsp_stream_get_address_pool:
837 * @stream: a #GstRTSPStream
839 * Get the #GstRTSPAddressPool used as the address pool of @stream.
841 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
845 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
847 GstRTSPStreamPrivate *priv;
848 GstRTSPAddressPool *result;
850 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
854 g_mutex_lock (&priv->lock);
855 if ((result = priv->pool))
856 g_object_ref (result);
857 g_mutex_unlock (&priv->lock);
863 * gst_rtsp_stream_get_multicast_address:
864 * @stream: a #GstRTSPStream
865 * @family: the #GSocketFamily
867 * Get the multicast address of @stream for @family.
869 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
870 * or %NULL when no address could be allocated. gst_rtsp_address_free()
874 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
875 GSocketFamily family)
877 GstRTSPStreamPrivate *priv;
878 GstRTSPAddress *result;
879 GstRTSPAddress **addrp;
880 GstRTSPAddressFlags flags;
882 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
886 if (family == G_SOCKET_FAMILY_IPV6) {
887 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
888 addrp = &priv->addr_v6;
890 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
891 addrp = &priv->addr_v4;
894 g_mutex_lock (&priv->lock);
895 if (*addrp == NULL) {
896 if (priv->pool == NULL)
899 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
901 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
905 result = gst_rtsp_address_copy (*addrp);
906 g_mutex_unlock (&priv->lock);
913 GST_ERROR_OBJECT (stream, "no address pool specified");
914 g_mutex_unlock (&priv->lock);
919 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
920 g_mutex_unlock (&priv->lock);
926 * gst_rtsp_stream_reserve_address:
927 * @stream: a #GstRTSPStream
928 * @address: an address
933 * Reserve @address and @port as the address and port of @stream.
935 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
936 * the address could be reserved. gst_rtsp_address_free() after usage.
939 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
940 const gchar * address, guint port, guint n_ports, guint ttl)
942 GstRTSPStreamPrivate *priv;
943 GstRTSPAddress *result;
945 GSocketFamily family;
946 GstRTSPAddress **addrp;
948 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
949 g_return_val_if_fail (address != NULL, NULL);
950 g_return_val_if_fail (port > 0, NULL);
951 g_return_val_if_fail (n_ports > 0, NULL);
952 g_return_val_if_fail (ttl > 0, NULL);
956 addr = g_inet_address_new_from_string (address);
958 GST_ERROR ("failed to get inet addr from %s", address);
959 family = G_SOCKET_FAMILY_IPV4;
961 family = g_inet_address_get_family (addr);
962 g_object_unref (addr);
965 if (family == G_SOCKET_FAMILY_IPV6)
966 addrp = &priv->addr_v6;
968 addrp = &priv->addr_v4;
970 g_mutex_lock (&priv->lock);
971 if (*addrp == NULL) {
972 GstRTSPAddressPoolResult res;
974 if (priv->pool == NULL)
977 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
978 port, n_ports, ttl, addrp);
979 if (res != GST_RTSP_ADDRESS_POOL_OK)
982 if (strcmp ((*addrp)->address, address) ||
983 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
984 (*addrp)->ttl != ttl)
985 goto different_address;
987 result = gst_rtsp_address_copy (*addrp);
988 g_mutex_unlock (&priv->lock);
995 GST_ERROR_OBJECT (stream, "no address pool specified");
996 g_mutex_unlock (&priv->lock);
1001 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1003 g_mutex_unlock (&priv->lock);
1008 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
1009 " reserved", address);
1010 g_mutex_unlock (&priv->lock);
1016 alloc_ports_one_family (GstRTSPStream * stream, GstRTSPAddressPool * pool,
1017 gint buffer_size, GSocketFamily family, GstElement * udpsrc_out[2],
1018 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
1019 GstRTSPAddress ** server_addr_out)
1021 GstRTSPStreamPrivate *priv = stream->priv;
1022 GstStateChangeReturn ret;
1023 GstElement *udpsrc0, *udpsrc1;
1024 GstElement *udpsink0, *udpsink1;
1025 GSocket *rtp_socket = NULL;
1026 GSocket *rtcp_socket;
1027 gint tmp_rtp, tmp_rtcp;
1029 gint rtpport, rtcpport;
1030 GList *rejected_addresses = NULL;
1031 GstRTSPAddress *addr = NULL;
1032 GInetAddress *inetaddr = NULL;
1033 GSocketAddress *rtp_sockaddr = NULL;
1034 GSocketAddress *rtcp_sockaddr = NULL;
1035 const gchar *multisink_socket;
1037 if (family == G_SOCKET_FAMILY_IPV6)
1038 multisink_socket = "socket-v6";
1040 multisink_socket = "socket";
1048 /* Start with random port */
1051 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1052 G_SOCKET_PROTOCOL_UDP, NULL);
1054 goto no_udp_protocol;
1056 if (*server_addr_out)
1057 gst_rtsp_address_free (*server_addr_out);
1059 /* try to allocate 2 UDP ports, the RTP port should be an even
1060 * number and the RTCP port should be the next (uneven) port */
1063 if (rtp_socket == NULL) {
1064 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1065 G_SOCKET_PROTOCOL_UDP, NULL);
1067 goto no_udp_protocol;
1070 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
1071 GstRTSPAddressFlags flags;
1074 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1076 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
1077 if (family == G_SOCKET_FAMILY_IPV6)
1078 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1080 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1082 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1087 tmp_rtp = addr->port;
1089 g_clear_object (&inetaddr);
1090 inetaddr = g_inet_address_new_from_string (addr->address);
1098 if (inetaddr == NULL)
1099 inetaddr = g_inet_address_new_any (family);
1102 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1103 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1104 g_object_unref (rtp_sockaddr);
1107 g_object_unref (rtp_sockaddr);
1109 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1110 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1111 g_clear_object (&rtp_sockaddr);
1116 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1117 g_object_unref (rtp_sockaddr);
1119 /* check if port is even */
1120 if ((tmp_rtp & 1) != 0) {
1121 /* port not even, close and allocate another */
1123 g_clear_object (&rtp_socket);
1128 tmp_rtcp = tmp_rtp + 1;
1130 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1131 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1132 g_object_unref (rtcp_sockaddr);
1133 g_clear_object (&rtp_socket);
1136 g_object_unref (rtcp_sockaddr);
1138 g_clear_object (&inetaddr);
1140 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
1141 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
1143 if (udpsrc0 == NULL || udpsrc1 == NULL)
1144 goto no_udp_protocol;
1146 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
1147 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
1149 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1150 if (ret == GST_STATE_CHANGE_FAILURE)
1152 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1153 if (ret == GST_STATE_CHANGE_FAILURE)
1156 /* all fine, do port check */
1157 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
1158 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
1160 /* this should not happen... */
1161 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1165 udpsink0 = udpsink_out[0];
1167 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1170 goto no_udp_protocol;
1172 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1173 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
1176 udpsink1 = udpsink_out[1];
1178 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1181 goto no_udp_protocol;
1183 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1184 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1185 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
1187 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1188 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
1189 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1190 /* Needs to be async for RECORD streams, otherwise we will never go to
1191 * PLAYING because the sinks will wait for data while the udpsrc can't
1192 * provide data with timestamps in PAUSED. */
1194 g_object_set (G_OBJECT (udpsink0), "async", FALSE, NULL);
1195 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1196 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1197 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1198 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1199 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1201 /* we keep these elements, we will further configure them when the
1202 * client told us to really use the UDP ports. */
1203 udpsrc_out[0] = udpsrc0;
1204 udpsrc_out[1] = udpsrc1;
1205 udpsink_out[0] = udpsink0;
1206 udpsink_out[1] = udpsink1;
1208 server_port_out->min = rtpport;
1209 server_port_out->max = rtcpport;
1211 *server_addr_out = addr;
1212 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1214 g_object_unref (rtp_socket);
1215 g_object_unref (rtcp_socket);
1243 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1244 gst_object_unref (udpsrc0);
1247 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1248 gst_object_unref (udpsrc1);
1251 gst_element_set_state (udpsink0, GST_STATE_NULL);
1252 gst_object_unref (udpsink0);
1255 g_object_unref (inetaddr);
1256 g_list_free_full (rejected_addresses,
1257 (GDestroyNotify) gst_rtsp_address_free);
1259 gst_rtsp_address_free (addr);
1261 g_object_unref (rtp_socket);
1263 g_object_unref (rtcp_socket);
1268 /* must be called with lock */
1270 alloc_ports (GstRTSPStream * stream)
1272 GstRTSPStreamPrivate *priv = stream->priv;
1275 alloc_ports_one_family (stream, priv->pool, priv->buffer_size,
1276 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1277 &priv->server_port_v4, &priv->server_addr_v4);
1280 alloc_ports_one_family (stream, priv->pool, priv->buffer_size,
1281 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1282 &priv->server_port_v6, &priv->server_addr_v6);
1284 return priv->have_ipv4 || priv->have_ipv6;
1288 * gst_rtsp_stream_get_server_port:
1289 * @stream: a #GstRTSPStream
1290 * @server_port: (out): result server port
1291 * @family: the port family to get
1293 * Fill @server_port with the port pair used by the server. This function can
1294 * only be called when @stream has been joined.
1297 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1298 GstRTSPRange * server_port, GSocketFamily family)
1300 GstRTSPStreamPrivate *priv;
1302 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1303 priv = stream->priv;
1304 g_return_if_fail (priv->is_joined);
1306 g_mutex_lock (&priv->lock);
1307 if (family == G_SOCKET_FAMILY_IPV4) {
1309 *server_port = priv->server_port_v4;
1312 *server_port = priv->server_port_v6;
1314 g_mutex_unlock (&priv->lock);
1318 * gst_rtsp_stream_get_rtpsession:
1319 * @stream: a #GstRTSPStream
1321 * Get the RTP session of this stream.
1323 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1326 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1328 GstRTSPStreamPrivate *priv;
1331 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1333 priv = stream->priv;
1335 g_mutex_lock (&priv->lock);
1336 if ((session = priv->session))
1337 g_object_ref (session);
1338 g_mutex_unlock (&priv->lock);
1344 * gst_rtsp_stream_get_ssrc:
1345 * @stream: a #GstRTSPStream
1346 * @ssrc: (out): result ssrc
1348 * Get the SSRC used by the RTP session of this stream. This function can only
1349 * be called when @stream has been joined.
1352 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1354 GstRTSPStreamPrivate *priv;
1356 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1357 priv = stream->priv;
1358 g_return_if_fail (priv->is_joined);
1360 g_mutex_lock (&priv->lock);
1361 if (ssrc && priv->session)
1362 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1363 g_mutex_unlock (&priv->lock);
1367 * gst_rtsp_stream_set_retransmission_time:
1368 * @stream: a #GstRTSPStream
1369 * @time: a #GstClockTime
1371 * Set the amount of time to store retransmission packets.
1374 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1377 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1379 g_mutex_lock (&stream->priv->lock);
1380 stream->priv->rtx_time = time;
1381 if (stream->priv->rtxsend)
1382 g_object_set (stream->priv->rtxsend, "max-size-time",
1383 GST_TIME_AS_MSECONDS (time), NULL);
1384 g_mutex_unlock (&stream->priv->lock);
1388 * gst_rtsp_media_get_retransmission_time:
1389 * @media: a #GstRTSPMedia
1391 * Get the amount of time to store retransmission data.
1393 * Returns: the amount of time to store retransmission data.
1396 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1400 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1402 g_mutex_lock (&stream->priv->lock);
1403 ret = stream->priv->rtx_time;
1404 g_mutex_unlock (&stream->priv->lock);
1410 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1412 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1414 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1416 g_mutex_lock (&stream->priv->lock);
1417 stream->priv->rtx_pt = rtx_pt;
1418 if (stream->priv->rtxsend) {
1419 guint pt = gst_rtsp_stream_get_pt (stream);
1420 gchar *pt_s = g_strdup_printf ("%d", pt);
1421 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1422 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1423 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1425 gst_structure_free (rtx_pt_map);
1427 g_mutex_unlock (&stream->priv->lock);
1431 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1435 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1437 g_mutex_lock (&stream->priv->lock);
1438 rtx_pt = stream->priv->rtx_pt;
1439 g_mutex_unlock (&stream->priv->lock);
1444 /* executed from streaming thread */
1446 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1448 GstRTSPStreamPrivate *priv = stream->priv;
1449 GstCaps *newcaps, *oldcaps;
1451 newcaps = gst_pad_get_current_caps (pad);
1453 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1456 g_mutex_lock (&priv->lock);
1457 oldcaps = priv->caps;
1458 priv->caps = newcaps;
1459 g_mutex_unlock (&priv->lock);
1462 gst_caps_unref (oldcaps);
1466 dump_structure (const GstStructure * s)
1470 sstr = gst_structure_to_string (s);
1471 GST_INFO ("structure: %s", sstr);
1475 static GstRTSPStreamTransport *
1476 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1478 GstRTSPStreamPrivate *priv = stream->priv;
1480 GstRTSPStreamTransport *result = NULL;
1485 if (rtcp_from == NULL)
1488 tmp = g_strrstr (rtcp_from, ":");
1492 port = atoi (tmp + 1);
1493 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1495 g_mutex_lock (&priv->lock);
1496 GST_INFO ("finding %s:%d in %d transports", dest, port,
1497 g_list_length (priv->transports));
1499 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1500 GstRTSPStreamTransport *trans = walk->data;
1501 const GstRTSPTransport *tr;
1504 tr = gst_rtsp_stream_transport_get_transport (trans);
1506 min = tr->client_port.min;
1507 max = tr->client_port.max;
1509 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1515 g_object_ref (result);
1516 g_mutex_unlock (&priv->lock);
1523 static GstRTSPStreamTransport *
1524 check_transport (GObject * source, GstRTSPStream * stream)
1526 GstStructure *stats;
1527 GstRTSPStreamTransport *trans;
1529 /* see if we have a stream to match with the origin of the RTCP packet */
1530 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1531 if (trans == NULL) {
1532 g_object_get (source, "stats", &stats, NULL);
1534 const gchar *rtcp_from;
1536 dump_structure (stats);
1538 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1539 if ((trans = find_transport (stream, rtcp_from))) {
1540 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1542 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1545 gst_structure_free (stats);
1553 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1555 GstRTSPStreamTransport *trans;
1557 GST_INFO ("%p: new source %p", stream, source);
1559 trans = check_transport (source, stream);
1562 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1566 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1568 GST_INFO ("%p: new SDES %p", stream, source);
1572 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1574 GstRTSPStreamTransport *trans;
1576 trans = check_transport (source, stream);
1579 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1580 gst_rtsp_stream_transport_keep_alive (trans);
1584 GstStructure *stats;
1585 g_object_get (source, "stats", &stats, NULL);
1587 dump_structure (stats);
1588 gst_structure_free (stats);
1595 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1597 GST_INFO ("%p: source %p bye", stream, source);
1601 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1603 GstRTSPStreamTransport *trans;
1605 GST_INFO ("%p: source %p bye timeout", stream, source);
1607 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1608 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1609 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1614 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1616 GstRTSPStreamTransport *trans;
1618 GST_INFO ("%p: source %p timeout", stream, source);
1620 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1621 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1622 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1627 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1630 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1631 g_list_free (priv->tr_cache_rtp);
1632 priv->tr_cache_rtp = NULL;
1634 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1635 g_list_free (priv->tr_cache_rtcp);
1636 priv->tr_cache_rtcp = NULL;
1640 static GstFlowReturn
1641 handle_new_sample (GstAppSink * sink, gpointer user_data)
1643 GstRTSPStreamPrivate *priv;
1647 GstRTSPStream *stream;
1650 sample = gst_app_sink_pull_sample (sink);
1654 stream = (GstRTSPStream *) user_data;
1655 priv = stream->priv;
1656 buffer = gst_sample_get_buffer (sample);
1658 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
1660 g_mutex_lock (&priv->lock);
1662 if (priv->tr_cache_cookie_rtp != priv->transports_cookie) {
1663 clear_tr_cache (priv, is_rtp);
1664 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1665 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1666 priv->tr_cache_rtp =
1667 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
1669 priv->tr_cache_cookie_rtp = priv->transports_cookie;
1672 if (priv->tr_cache_cookie_rtcp != priv->transports_cookie) {
1673 clear_tr_cache (priv, is_rtp);
1674 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1675 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1676 priv->tr_cache_rtcp =
1677 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
1679 priv->tr_cache_cookie_rtcp = priv->transports_cookie;
1682 g_mutex_unlock (&priv->lock);
1685 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
1686 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1687 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1690 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
1691 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1692 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1695 gst_sample_unref (sample);
1700 static GstAppSinkCallbacks sink_cb = {
1701 NULL, /* not interested in EOS */
1702 NULL, /* not interested in preroll samples */
1707 get_rtp_encoder (GstRTSPStream * stream, guint session)
1709 GstRTSPStreamPrivate *priv = stream->priv;
1711 if (priv->srtpenc == NULL) {
1714 name = g_strdup_printf ("srtpenc_%u", session);
1715 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
1718 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
1720 return gst_object_ref (priv->srtpenc);
1724 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
1726 GstRTSPStreamPrivate *priv = stream->priv;
1727 GstElement *oldenc, *enc;
1731 if (priv->idx != session)
1734 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
1736 oldenc = priv->srtpenc;
1737 enc = get_rtp_encoder (stream, session);
1738 name = g_strdup_printf ("rtp_sink_%d", session);
1739 pad = gst_element_get_request_pad (enc, name);
1741 gst_object_unref (pad);
1744 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
1751 request_rtcp_encoder (GstElement * rtpbin, guint session,
1752 GstRTSPStream * stream)
1754 GstRTSPStreamPrivate *priv = stream->priv;
1755 GstElement *oldenc, *enc;
1759 if (priv->idx != session)
1762 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
1764 oldenc = priv->srtpenc;
1765 enc = get_rtp_encoder (stream, session);
1766 name = g_strdup_printf ("rtcp_sink_%d", session);
1767 pad = gst_element_get_request_pad (enc, name);
1769 gst_object_unref (pad);
1772 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
1779 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
1781 GstRTSPStreamPrivate *priv = stream->priv;
1784 GST_DEBUG ("request key %08x", ssrc);
1786 g_mutex_lock (&priv->lock);
1787 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
1788 gst_caps_ref (caps);
1789 g_mutex_unlock (&priv->lock);
1795 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
1796 GstRTSPStream * stream)
1798 GstRTSPStreamPrivate *priv = stream->priv;
1800 if (priv->idx != session)
1803 if (priv->srtpdec == NULL) {
1806 name = g_strdup_printf ("srtpdec_%u", session);
1807 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
1810 g_signal_connect (priv->srtpdec, "request-key",
1811 (GCallback) request_key, stream);
1813 return gst_object_ref (priv->srtpdec);
1817 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPStream * stream)
1821 GstStructure *pt_map;
1826 pt = gst_rtsp_stream_get_pt (stream);
1827 pt_s = g_strdup_printf ("%u", pt);
1828 rtx_pt = stream->priv->rtx_pt;
1830 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
1832 bin = gst_bin_new (NULL);
1833 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
1834 pt_map = gst_structure_new ("application/x-rtp-pt-map",
1835 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1836 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
1837 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
1839 gst_structure_free (pt_map);
1840 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
1842 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
1843 name = g_strdup_printf ("src_%u", sessid);
1844 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1846 gst_object_unref (pad);
1848 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
1849 name = g_strdup_printf ("sink_%u", sessid);
1850 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1852 gst_object_unref (pad);
1858 * gst_rtsp_stream_set_pt_map:
1859 * @stream: a #GstRTSPStream
1863 * Configure a pt map between @pt and @caps.
1866 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
1868 GstRTSPStreamPrivate *priv = stream->priv;
1870 g_mutex_lock (&priv->lock);
1871 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
1872 g_mutex_unlock (&priv->lock);
1876 request_pt_map (GstElement * rtpbin, guint session, guint pt,
1877 GstRTSPStream * stream)
1879 GstRTSPStreamPrivate *priv = stream->priv;
1880 GstCaps *caps = NULL;
1882 g_mutex_lock (&priv->lock);
1884 if (priv->idx == session) {
1885 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
1887 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
1888 gst_caps_ref (caps);
1890 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
1894 g_mutex_unlock (&priv->lock);
1900 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
1902 GstRTSPStreamPrivate *priv = stream->priv;
1904 GstPadLinkReturn ret;
1907 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
1908 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
1910 name = gst_pad_get_name (pad);
1911 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
1917 if (priv->idx != sessid)
1920 if (gst_pad_is_linked (priv->sinkpad)) {
1921 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
1922 GST_DEBUG_PAD_NAME (priv->sinkpad));
1926 /* link the RTP pad to the session manager, it should not really fail unless
1927 * this is not really an RTP pad */
1928 ret = gst_pad_link (pad, priv->sinkpad);
1929 if (ret != GST_PAD_LINK_OK)
1931 priv->recv_rtp_src = gst_object_ref (pad);
1938 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
1939 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
1944 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
1945 GstRTSPStream * stream)
1947 /* TODO: What to do here other than this? */
1948 GST_DEBUG ("Stream %p: Got EOS", stream);
1949 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
1953 * gst_rtsp_stream_join_bin:
1954 * @stream: a #GstRTSPStream
1955 * @bin: (transfer none): a #GstBin to join
1956 * @rtpbin: (transfer none): a rtpbin element in @bin
1957 * @state: the target state of the new elements
1959 * Join the #GstBin @bin that contains the element @rtpbin.
1961 * @stream will link to @rtpbin, which must be inside @bin. The elements
1962 * added to @bin will be set to the state given in @state.
1964 * Returns: %TRUE on success.
1967 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1968 GstElement * rtpbin, GstState state)
1970 GstRTSPStreamPrivate *priv;
1974 GstPad *pad, *sinkpad, *selpad;
1975 GstPadLinkReturn ret;
1977 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1978 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1979 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1981 priv = stream->priv;
1983 g_mutex_lock (&priv->lock);
1984 if (priv->is_joined)
1987 /* create a session with the same index as the stream */
1990 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1992 if (!alloc_ports (stream))
1995 /* update the dscp qos field in the sinks */
1996 update_dscp_qos (stream);
1998 if (priv->profiles & GST_RTSP_PROFILE_SAVP
1999 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
2001 g_signal_connect (rtpbin, "request-rtp-encoder",
2002 (GCallback) request_rtp_encoder, stream);
2003 g_signal_connect (rtpbin, "request-rtcp-encoder",
2004 (GCallback) request_rtcp_encoder, stream);
2005 g_signal_connect (rtpbin, "request-rtp-decoder",
2006 (GCallback) request_rtp_rtcp_decoder, stream);
2007 g_signal_connect (rtpbin, "request-rtcp-decoder",
2008 (GCallback) request_rtp_rtcp_decoder, stream);
2011 if (priv->rtx_time > 0 && priv->srcpad) {
2012 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
2013 g_signal_connect (rtpbin, "request-aux-sender",
2014 (GCallback) request_aux_sender, stream);
2016 if (priv->sinkpad) {
2017 g_signal_connect (rtpbin, "request-pt-map",
2018 (GCallback) request_pt_map, stream);
2021 /* get a pad for sending RTP */
2022 name = g_strdup_printf ("send_rtp_sink_%u", idx);
2023 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
2027 /* link the RTP pad to the session manager, it should not really fail unless
2028 * this is not really an RTP pad */
2029 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
2030 if (ret != GST_PAD_LINK_OK)
2033 /* Need to connect our sinkpad from here */
2034 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
2036 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
2039 /* get pads from the RTP session element for sending and receiving
2041 name = g_strdup_printf ("send_rtp_src_%u", idx);
2042 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
2044 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
2045 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
2048 name = g_strdup_printf ("send_rtcp_src_%u", idx);
2049 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
2051 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
2052 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
2055 /* get the session */
2056 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
2058 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
2060 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
2062 g_signal_connect (priv->session, "on-ssrc-active",
2063 (GCallback) on_ssrc_active, stream);
2064 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2066 g_signal_connect (priv->session, "on-bye-timeout",
2067 (GCallback) on_bye_timeout, stream);
2068 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
2071 for (i = 0; i < 2; i++) {
2072 GstPad *teepad, *queuepad;
2073 /* For the sender we create this bit of pipeline for both
2074 * RTP and RTCP. Sync and preroll are enabled on udpsink so
2075 * we need to add a queue before appsink to make the pipeline
2076 * not block. For the TCP case, we want to pump data to the
2077 * client as fast as possible anyway.
2079 * .--------. .-----. .---------.
2080 * | rtpbin | | tee | | udpsink |
2081 * | send->sink src->sink |
2082 * '--------' | | '---------'
2083 * | | .---------. .---------.
2084 * | | | queue | | appsink |
2085 * | src->sink src->sink |
2086 * '-----' '---------' '---------'
2088 * When only UDP is allowed, we skip the tee, queue and appsink and link the
2089 * udpsink directly to the session.
2092 gst_bin_add (bin, priv->udpsink[i]);
2093 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
2095 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
2096 /* make tee for RTP/RTCP */
2097 priv->tee[i] = gst_element_factory_make ("tee", NULL);
2098 gst_bin_add (bin, priv->tee[i]);
2100 /* and link to rtpbin send pad */
2101 pad = gst_element_get_static_pad (priv->tee[i], "sink");
2102 gst_pad_link (priv->send_src[i], pad);
2103 gst_object_unref (pad);
2105 /* link tee to udpsink */
2106 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2107 gst_pad_link (teepad, sinkpad);
2108 gst_object_unref (teepad);
2111 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
2112 gst_bin_add (bin, priv->appqueue[i]);
2113 /* and link to tee */
2114 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
2115 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
2116 gst_pad_link (teepad, pad);
2117 gst_object_unref (pad);
2118 gst_object_unref (teepad);
2121 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
2122 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
2123 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
2124 gst_bin_add (bin, priv->appsink[i]);
2125 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
2126 &sink_cb, stream, NULL);
2127 /* and link to queue */
2128 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
2129 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
2130 gst_pad_link (queuepad, pad);
2131 gst_object_unref (pad);
2132 gst_object_unref (queuepad);
2134 /* else only udpsink needed, link it to the session */
2135 gst_pad_link (priv->send_src[i], sinkpad);
2137 gst_object_unref (sinkpad);
2139 /* For the receiver we create this bit of pipeline for both
2140 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
2141 * and it is all funneled into the rtpbin receive pad.
2143 * .--------. .--------. .--------.
2144 * | udpsrc | | funnel | | rtpbin |
2145 * | src->sink src->sink |
2146 * '--------' | | '--------'
2150 * '--------' '--------'
2152 /* make funnel for the RTP/RTCP receivers */
2153 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
2154 gst_bin_add (bin, priv->funnel[i]);
2156 pad = gst_element_get_static_pad (priv->funnel[i], "src");
2157 gst_pad_link (pad, priv->recv_sink[i]);
2158 gst_object_unref (pad);
2160 if (priv->udpsrc_v4[i]) {
2162 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2163 * values. This is only relevant for PLAY pipelines */
2164 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
2165 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
2168 gst_bin_add (bin, priv->udpsrc_v4[i]);
2170 /* and link to the funnel v4 */
2171 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2172 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
2173 gst_pad_link (pad, selpad);
2174 gst_object_unref (pad);
2175 gst_object_unref (selpad);
2178 if (priv->udpsrc_v6[i]) {
2180 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
2181 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
2183 gst_bin_add (bin, priv->udpsrc_v6[i]);
2185 /* and link to the funnel v6 */
2186 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2187 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2188 gst_pad_link (pad, selpad);
2189 gst_object_unref (pad);
2190 gst_object_unref (selpad);
2193 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
2194 /* make and add appsrc */
2195 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2196 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, NULL);
2197 gst_bin_add (bin, priv->appsrc[i]);
2198 /* and link to the funnel */
2199 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2200 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2201 gst_pad_link (pad, selpad);
2202 gst_object_unref (pad);
2203 gst_object_unref (selpad);
2206 /* check if we need to set to a special state */
2207 if (state != GST_STATE_NULL) {
2208 if (priv->udpsink[i])
2209 gst_element_set_state (priv->udpsink[i], state);
2210 if (priv->appsink[i])
2211 gst_element_set_state (priv->appsink[i], state);
2212 if (priv->appqueue[i])
2213 gst_element_set_state (priv->appqueue[i], state);
2215 gst_element_set_state (priv->tee[i], state);
2216 if (priv->funnel[i])
2217 gst_element_set_state (priv->funnel[i], state);
2218 if (priv->appsrc[i])
2219 gst_element_set_state (priv->appsrc[i], state);
2223 /* be notified of caps changes */
2224 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2225 (GCallback) caps_notify, stream);
2227 priv->is_joined = TRUE;
2228 g_mutex_unlock (&priv->lock);
2235 g_mutex_unlock (&priv->lock);
2240 g_mutex_unlock (&priv->lock);
2241 GST_WARNING ("failed to allocate ports %u", idx);
2246 GST_WARNING ("failed to link stream %u", idx);
2247 gst_object_unref (priv->send_rtp_sink);
2248 priv->send_rtp_sink = NULL;
2249 g_mutex_unlock (&priv->lock);
2255 * gst_rtsp_stream_leave_bin:
2256 * @stream: a #GstRTSPStream
2257 * @bin: (transfer none): a #GstBin
2258 * @rtpbin: (transfer none): a rtpbin #GstElement
2260 * Remove the elements of @stream from @bin.
2262 * Return: %TRUE on success.
2265 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2266 GstElement * rtpbin)
2268 GstRTSPStreamPrivate *priv;
2272 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2273 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2274 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2276 priv = stream->priv;
2278 g_mutex_lock (&priv->lock);
2279 if (!priv->is_joined)
2280 goto was_not_joined;
2282 /* all transports must be removed by now */
2283 if (priv->transports != NULL)
2284 goto transports_not_removed;
2286 clear_tr_cache (priv, TRUE);
2287 clear_tr_cache (priv, FALSE);
2289 GST_INFO ("stream %p leaving bin", stream);
2292 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2293 } else if (priv->recv_rtp_src) {
2294 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
2295 gst_object_unref (priv->recv_rtp_src);
2296 priv->recv_rtp_src = NULL;
2298 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2299 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2300 gst_object_unref (priv->send_rtp_sink);
2301 priv->send_rtp_sink = NULL;
2303 for (i = 0; i < 2; i++) {
2304 if (priv->udpsink[i])
2305 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
2306 if (priv->appsink[i])
2307 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
2308 if (priv->appqueue[i])
2309 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
2311 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
2312 if (priv->funnel[i])
2313 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
2314 if (priv->appsrc[i])
2315 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2316 if (priv->udpsrc_v4[i]) {
2317 /* and set udpsrc to NULL now before removing */
2318 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
2319 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2320 /* removing them should also nicely release the request
2321 * pads when they finalize */
2322 gst_bin_remove (bin, priv->udpsrc_v4[i]);
2324 if (priv->udpsrc_v6[i]) {
2325 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
2326 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2327 gst_bin_remove (bin, priv->udpsrc_v6[i]);
2330 for (l = priv->transport_sources; l; l = l->next) {
2331 GstRTSPMulticastTransportSource *s = l->data;
2336 gst_element_set_locked_state (s->udpsrc[i], FALSE);
2337 gst_element_set_state (s->udpsrc[i], GST_STATE_NULL);
2338 gst_bin_remove (bin, s->udpsrc[i]);
2341 if (priv->udpsink[i])
2342 gst_bin_remove (bin, priv->udpsink[i]);
2343 if (priv->appsrc[i])
2344 gst_bin_remove (bin, priv->appsrc[i]);
2345 if (priv->appsink[i])
2346 gst_bin_remove (bin, priv->appsink[i]);
2347 if (priv->appqueue[i])
2348 gst_bin_remove (bin, priv->appqueue[i]);
2350 gst_bin_remove (bin, priv->tee[i]);
2351 if (priv->funnel[i])
2352 gst_bin_remove (bin, priv->funnel[i]);
2354 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2355 gst_object_unref (priv->recv_sink[i]);
2356 priv->recv_sink[i] = NULL;
2358 priv->udpsrc_v4[i] = NULL;
2359 priv->udpsrc_v6[i] = NULL;
2360 priv->udpsink[i] = NULL;
2361 priv->appsrc[i] = NULL;
2362 priv->appsink[i] = NULL;
2363 priv->appqueue[i] = NULL;
2364 priv->tee[i] = NULL;
2365 priv->funnel[i] = NULL;
2368 for (l = priv->transport_sources; l; l = l->next) {
2369 GstRTSPMulticastTransportSource *s = l->data;
2370 g_slice_free (GstRTSPMulticastTransportSource, s);
2372 g_list_free (priv->transport_sources);
2373 priv->transport_sources = NULL;
2375 gst_object_unref (priv->send_src[0]);
2376 priv->send_src[0] = NULL;
2378 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2379 gst_object_unref (priv->send_src[1]);
2380 priv->send_src[1] = NULL;
2382 g_object_unref (priv->session);
2383 priv->session = NULL;
2385 gst_caps_unref (priv->caps);
2389 gst_object_unref (priv->srtpenc);
2391 gst_object_unref (priv->srtpdec);
2393 priv->is_joined = FALSE;
2394 g_mutex_unlock (&priv->lock);
2400 g_mutex_unlock (&priv->lock);
2403 transports_not_removed:
2405 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2406 g_mutex_unlock (&priv->lock);
2412 * gst_rtsp_stream_get_rtpinfo:
2413 * @stream: a #GstRTSPStream
2414 * @rtptime: (allow-none): result RTP timestamp
2415 * @seq: (allow-none): result RTP seqnum
2416 * @clock_rate: (allow-none): the clock rate
2417 * @running_time: (allow-none): result running-time
2419 * Retrieve the current rtptime, seq and running-time. This is used to
2420 * construct a RTPInfo reply header.
2422 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2425 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2426 guint * rtptime, guint * seq, guint * clock_rate,
2427 GstClockTime * running_time)
2429 GstRTSPStreamPrivate *priv;
2430 GstStructure *stats;
2431 GObjectClass *payobjclass;
2433 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2435 priv = stream->priv;
2437 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2439 g_mutex_lock (&priv->lock);
2441 if (g_object_class_find_property (payobjclass, "stats")) {
2442 g_object_get (priv->payloader, "stats", &stats, NULL);
2447 gst_structure_get_uint (stats, "seqnum", seq);
2450 gst_structure_get_uint (stats, "timestamp", rtptime);
2453 gst_structure_get_clock_time (stats, "running-time", running_time);
2456 gst_structure_get_uint (stats, "clock-rate", clock_rate);
2457 if (*clock_rate == 0 && running_time)
2458 *running_time = GST_CLOCK_TIME_NONE;
2460 gst_structure_free (stats);
2462 if (!g_object_class_find_property (payobjclass, "seqnum") ||
2463 !g_object_class_find_property (payobjclass, "timestamp"))
2467 g_object_get (priv->payloader, "seqnum", seq, NULL);
2470 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
2473 *running_time = GST_CLOCK_TIME_NONE;
2475 g_mutex_unlock (&priv->lock);
2482 GST_WARNING ("Could not get payloader stats");
2483 g_mutex_unlock (&priv->lock);
2489 * gst_rtsp_stream_get_caps:
2490 * @stream: a #GstRTSPStream
2492 * Retrieve the current caps of @stream.
2494 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
2498 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
2500 GstRTSPStreamPrivate *priv;
2503 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2505 priv = stream->priv;
2507 g_mutex_lock (&priv->lock);
2508 if ((result = priv->caps))
2509 gst_caps_ref (result);
2510 g_mutex_unlock (&priv->lock);
2516 * gst_rtsp_stream_recv_rtp:
2517 * @stream: a #GstRTSPStream
2518 * @buffer: (transfer full): a #GstBuffer
2520 * Handle an RTP buffer for the stream. This method is usually called when a
2521 * message has been received from a client using the TCP transport.
2523 * This function takes ownership of @buffer.
2525 * Returns: a GstFlowReturn.
2528 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
2530 GstRTSPStreamPrivate *priv;
2532 GstElement *element;
2534 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2535 priv = stream->priv;
2536 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2537 g_return_val_if_fail (priv->is_joined, FALSE);
2539 g_mutex_lock (&priv->lock);
2540 if (priv->appsrc[0])
2541 element = gst_object_ref (priv->appsrc[0]);
2544 g_mutex_unlock (&priv->lock);
2547 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2548 gst_object_unref (element);
2556 * gst_rtsp_stream_recv_rtcp:
2557 * @stream: a #GstRTSPStream
2558 * @buffer: (transfer full): a #GstBuffer
2560 * Handle an RTCP buffer for the stream. This method is usually called when a
2561 * message has been received from a client using the TCP transport.
2563 * This function takes ownership of @buffer.
2565 * Returns: a GstFlowReturn.
2568 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
2570 GstRTSPStreamPrivate *priv;
2572 GstElement *element;
2574 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2575 priv = stream->priv;
2576 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2578 if (!priv->is_joined) {
2579 gst_buffer_unref (buffer);
2580 return GST_FLOW_NOT_LINKED;
2582 g_mutex_lock (&priv->lock);
2583 if (priv->appsrc[1])
2584 element = gst_object_ref (priv->appsrc[1]);
2587 g_mutex_unlock (&priv->lock);
2590 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2591 gst_object_unref (element);
2594 gst_buffer_unref (buffer);
2599 /* must be called with lock */
2601 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2604 GstRTSPStreamPrivate *priv = stream->priv;
2605 const GstRTSPTransport *tr;
2607 tr = gst_rtsp_stream_transport_get_transport (trans);
2609 switch (tr->lower_transport) {
2610 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2612 GstRTSPMulticastTransportSource *source;
2615 bin = GST_BIN (gst_object_get_parent (GST_OBJECT (priv->funnel[0])));
2620 GstPad *selpad, *pad;
2622 source = g_slice_new0 (GstRTSPMulticastTransportSource);
2623 source->transport = trans;
2625 for (i = 0; i < 2; i++) {
2627 g_strdup_printf ("udp://%s:%d", tr->destination,
2628 (i == 0) ? tr->port.min : tr->port.max);
2630 gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2634 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2635 * values. This is only relevant for PLAY pipelines */
2636 gst_element_set_state (source->udpsrc[i], GST_STATE_PLAYING);
2637 gst_element_set_locked_state (source->udpsrc[i], TRUE);
2640 gst_bin_add (bin, source->udpsrc[i]);
2642 /* and link to the funnel v4 */
2643 source->selpad[i] = selpad =
2644 gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2645 pad = gst_element_get_static_pad (source->udpsrc[i], "src");
2646 gst_pad_link (pad, selpad);
2647 gst_object_unref (pad);
2648 gst_object_unref (selpad);
2650 gst_object_unref (bin);
2652 priv->transport_sources =
2653 g_list_prepend (priv->transport_sources, source);
2657 for (l = priv->transport_sources; l; l = l->next) {
2660 if (source->transport == trans) {
2661 priv->transport_sources =
2662 g_list_delete_link (priv->transport_sources, l);
2670 for (i = 0; i < 2; i++) {
2671 /* Will automatically unlink everything */
2672 gst_bin_remove (bin,
2673 GST_ELEMENT (gst_object_ref (source->udpsrc[i])));
2675 gst_element_set_state (source->udpsrc[i], GST_STATE_NULL);
2676 gst_object_unref (source->udpsrc[i]);
2678 gst_element_release_request_pad (priv->funnel[i],
2682 g_slice_free (GstRTSPMulticastTransportSource, source);
2686 /* fall through for the generic case */
2688 case GST_RTSP_LOWER_TRANS_UDP:
2694 dest = tr->destination;
2695 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2700 min = tr->client_port.min;
2701 max = tr->client_port.max;
2706 GST_INFO ("setting ttl-mc %d", ttl);
2707 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
2708 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
2710 GST_INFO ("adding %s:%d-%d", dest, min, max);
2711 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
2712 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
2713 priv->transports = g_list_prepend (priv->transports, trans);
2715 GST_INFO ("removing %s:%d-%d", dest, min, max);
2716 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
2717 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
2718 priv->transports = g_list_remove (priv->transports, trans);
2720 priv->transports_cookie++;
2723 case GST_RTSP_LOWER_TRANS_TCP:
2725 GST_INFO ("adding TCP %s", tr->destination);
2726 priv->transports = g_list_prepend (priv->transports, trans);
2728 GST_INFO ("removing TCP %s", tr->destination);
2729 priv->transports = g_list_remove (priv->transports, trans);
2731 priv->transports_cookie++;
2734 goto unknown_transport;
2741 GST_INFO ("Unknown transport %d", tr->lower_transport);
2748 * gst_rtsp_stream_add_transport:
2749 * @stream: a #GstRTSPStream
2750 * @trans: (transfer none): a #GstRTSPStreamTransport
2752 * Add the transport in @trans to @stream. The media of @stream will
2753 * then also be send to the values configured in @trans.
2755 * @stream must be joined to a bin.
2757 * @trans must contain a valid #GstRTSPTransport.
2759 * Returns: %TRUE if @trans was added
2762 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
2763 GstRTSPStreamTransport * trans)
2765 GstRTSPStreamPrivate *priv;
2768 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2769 priv = stream->priv;
2770 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2771 g_return_val_if_fail (priv->is_joined, FALSE);
2773 g_mutex_lock (&priv->lock);
2774 res = update_transport (stream, trans, TRUE);
2775 g_mutex_unlock (&priv->lock);
2781 * gst_rtsp_stream_remove_transport:
2782 * @stream: a #GstRTSPStream
2783 * @trans: (transfer none): a #GstRTSPStreamTransport
2785 * Remove the transport in @trans from @stream. The media of @stream will
2786 * not be sent to the values configured in @trans.
2788 * @stream must be joined to a bin.
2790 * @trans must contain a valid #GstRTSPTransport.
2792 * Returns: %TRUE if @trans was removed
2795 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2796 GstRTSPStreamTransport * trans)
2798 GstRTSPStreamPrivate *priv;
2801 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2802 priv = stream->priv;
2803 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2804 g_return_val_if_fail (priv->is_joined, FALSE);
2806 g_mutex_lock (&priv->lock);
2807 res = update_transport (stream, trans, FALSE);
2808 g_mutex_unlock (&priv->lock);
2814 * gst_rtsp_stream_update_crypto:
2815 * @stream: a #GstRTSPStream
2817 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
2819 * Update the new crypto information for @ssrc in @stream. If information
2820 * for @ssrc did not exist, it will be added. If information
2821 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
2822 * be removed from @stream.
2824 * Returns: %TRUE if @crypto could be updated
2827 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
2828 guint ssrc, GstCaps * crypto)
2830 GstRTSPStreamPrivate *priv;
2832 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2833 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
2835 priv = stream->priv;
2837 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
2839 g_mutex_lock (&priv->lock);
2841 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
2842 gst_caps_ref (crypto));
2844 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
2845 g_mutex_unlock (&priv->lock);
2851 * gst_rtsp_stream_get_rtp_socket:
2852 * @stream: a #GstRTSPStream
2853 * @family: the socket family
2855 * Get the RTP socket from @stream for a @family.
2857 * @stream must be joined to a bin.
2859 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
2860 * socket could be allocated for @family. Unref after usage
2863 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2865 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2869 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2870 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2871 family == G_SOCKET_FAMILY_IPV6, NULL);
2872 g_return_val_if_fail (priv->udpsink[0], NULL);
2874 if (family == G_SOCKET_FAMILY_IPV6)
2879 g_object_get (priv->udpsink[0], name, &socket, NULL);
2885 * gst_rtsp_stream_get_rtcp_socket:
2886 * @stream: a #GstRTSPStream
2887 * @family: the socket family
2889 * Get the RTCP socket from @stream for a @family.
2891 * @stream must be joined to a bin.
2893 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
2894 * socket could be allocated for @family. Unref after usage
2897 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
2899 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2903 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2904 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2905 family == G_SOCKET_FAMILY_IPV6, NULL);
2906 g_return_val_if_fail (priv->udpsink[1], NULL);
2908 if (family == G_SOCKET_FAMILY_IPV6)
2913 g_object_get (priv->udpsink[1], name, &socket, NULL);
2919 * gst_rtsp_stream_set_seqnum:
2920 * @stream: a #GstRTSPStream
2921 * @seqnum: a new sequence number
2923 * Configure the sequence number in the payloader of @stream to @seqnum.
2926 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
2928 GstRTSPStreamPrivate *priv;
2930 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
2932 priv = stream->priv;
2934 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
2938 * gst_rtsp_stream_get_seqnum:
2939 * @stream: a #GstRTSPStream
2941 * Get the configured sequence number in the payloader of @stream.
2943 * Returns: the sequence number of the payloader.
2946 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
2948 GstRTSPStreamPrivate *priv;
2951 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
2953 priv = stream->priv;
2955 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
2961 * gst_rtsp_stream_transport_filter:
2962 * @stream: a #GstRTSPStream
2963 * @func: (scope call) (allow-none): a callback
2964 * @user_data: (closure): user data passed to @func
2966 * Call @func for each transport managed by @stream. The result value of @func
2967 * determines what happens to the transport. @func will be called with @stream
2968 * locked so no further actions on @stream can be performed from @func.
2970 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
2973 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
2975 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
2976 * will also be added with an additional ref to the result #GList of this
2979 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
2981 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
2982 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2983 * element in the #GList should be unreffed before the list is freed.
2986 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
2987 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
2989 GstRTSPStreamPrivate *priv;
2990 GList *result, *walk, *next;
2991 GHashTable *visited = NULL;
2994 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2996 priv = stream->priv;
3000 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
3002 g_mutex_lock (&priv->lock);
3004 cookie = priv->transports_cookie;
3005 for (walk = priv->transports; walk; walk = next) {
3006 GstRTSPStreamTransport *trans = walk->data;
3007 GstRTSPFilterResult res;
3010 next = g_list_next (walk);
3013 /* only visit each transport once */
3014 if (g_hash_table_contains (visited, trans))
3017 g_hash_table_add (visited, g_object_ref (trans));
3018 g_mutex_unlock (&priv->lock);
3020 res = func (stream, trans, user_data);
3022 g_mutex_lock (&priv->lock);
3024 res = GST_RTSP_FILTER_REF;
3026 changed = (cookie != priv->transports_cookie);
3029 case GST_RTSP_FILTER_REMOVE:
3030 update_transport (stream, trans, FALSE);
3032 case GST_RTSP_FILTER_REF:
3033 result = g_list_prepend (result, g_object_ref (trans));
3035 case GST_RTSP_FILTER_KEEP:
3042 g_mutex_unlock (&priv->lock);
3045 g_hash_table_unref (visited);
3050 static GstPadProbeReturn
3051 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
3053 GstRTSPStreamPrivate *priv;
3054 GstRTSPStream *stream;
3057 priv = stream->priv;
3059 GST_DEBUG_OBJECT (pad, "now blocking");
3061 g_mutex_lock (&priv->lock);
3062 priv->blocking = TRUE;
3063 g_mutex_unlock (&priv->lock);
3065 gst_element_post_message (priv->payloader,
3066 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
3067 gst_structure_new_empty ("GstRTSPStreamBlocking")));
3069 return GST_PAD_PROBE_OK;
3073 * gst_rtsp_stream_set_blocked:
3074 * @stream: a #GstRTSPStream
3075 * @blocked: boolean indicating we should block or unblock
3077 * Blocks or unblocks the dataflow on @stream.
3079 * Returns: %TRUE on success
3082 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
3084 GstRTSPStreamPrivate *priv;
3086 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3088 priv = stream->priv;
3090 g_mutex_lock (&priv->lock);
3092 priv->blocking = FALSE;
3093 if (priv->blocked_id == 0) {
3094 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
3095 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3096 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
3097 g_object_ref (stream), g_object_unref);
3100 if (priv->blocked_id != 0) {
3101 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
3102 priv->blocked_id = 0;
3103 priv->blocking = FALSE;
3106 g_mutex_unlock (&priv->lock);
3112 * gst_rtsp_stream_is_blocking:
3113 * @stream: a #GstRTSPStream
3115 * Check if @stream is blocking on a #GstBuffer.
3117 * Returns: %TRUE if @stream is blocking
3120 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
3122 GstRTSPStreamPrivate *priv;
3125 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3127 priv = stream->priv;
3129 g_mutex_lock (&priv->lock);
3130 result = priv->blocking;
3131 g_mutex_unlock (&priv->lock);
3137 * gst_rtsp_stream_query_position:
3138 * @stream: a #GstRTSPStream
3140 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
3141 * the RTP parts of the pipeline and not the RTCP parts.
3143 * Returns: %TRUE if the position could be queried
3146 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
3148 GstRTSPStreamPrivate *priv;
3152 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3154 priv = stream->priv;
3156 g_mutex_lock (&priv->lock);
3157 if ((sink = priv->udpsink[0]))
3158 gst_object_ref (sink);
3159 g_mutex_unlock (&priv->lock);
3164 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
3165 gst_object_unref (sink);
3171 * gst_rtsp_stream_query_stop:
3172 * @stream: a #GstRTSPStream
3174 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
3175 * the RTP parts of the pipeline and not the RTCP parts.
3177 * Returns: %TRUE if the stop could be queried
3180 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3182 GstRTSPStreamPrivate *priv;
3187 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3189 priv = stream->priv;
3191 g_mutex_lock (&priv->lock);
3192 if ((sink = priv->udpsink[0]))
3193 gst_object_ref (sink);
3194 g_mutex_unlock (&priv->lock);
3199 query = gst_query_new_segment (GST_FORMAT_TIME);
3200 if ((ret = gst_element_query (sink, query))) {
3203 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3204 if (format != GST_FORMAT_TIME)
3207 gst_query_unref (query);
3208 gst_object_unref (sink);