2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 * @short_description: A media stream
22 * @see_also: #GstRTSPMedia
24 * The #GstRTSPStream object manages the data transport for one stream. It
25 * is created from a payloader element and a source pad that produce the RTP
26 * packets for the stream.
28 * With gst_rtsp_stream_join_bin() the streaming elements are added to the bin
29 * and rtpbin. gst_rtsp_stream_leave_bin() removes the elements again.
31 * The #GstRTSPStream will use the configured addresspool, as set with
32 * gst_rtsp_stream_set_address_pool(), to allocate multicast addresses for the
33 * stream. With gst_rtsp_stream_get_multicast_address() you can get the
36 * With gst_rtsp_stream_get_server_port () you can get the port that the server
37 * will use to receive RTCP. This is the part that the clients will use to send
40 * With gst_rtsp_stream_add_transport() destinations can be added where the
41 * stream should be sent to. Use gst_rtsp_stream_remove_transport() to remove
42 * the destination again.
44 * Last reviewed on 2013-07-16 (1.0.0)
53 #include <gst/app/gstappsrc.h>
54 #include <gst/app/gstappsink.h>
56 #include "rtsp-stream.h"
58 #define GST_RTSP_STREAM_GET_PRIVATE(obj) \
59 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
63 GstRTSPStreamTransport *transport;
65 /* RTP and RTCP source */
66 GstElement *udpsrc[2];
68 } GstRTSPMulticastTransportSource;
70 struct _GstRTSPStreamPrivate
75 GstElement *payloader;
80 GstRTSPProfile profiles;
81 GstRTSPLowerTrans protocols;
83 /* pads on the rtpbin */
84 GstPad *send_rtp_sink;
88 /* the RTPSession object */
91 /* SRTP encoder/decoder */
96 /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
98 GstElement *udpsrc_v4[2];
100 /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
102 GstElement *udpsrc_v6[2];
104 GstElement *udpsink[2];
106 /* for TCP transport */
107 GstElement *appsrc[2];
108 GstElement *appqueue[2];
109 GstElement *appsink[2];
112 GstElement *funnel[2];
117 GstClockTime rtx_time;
119 /* server ports for sending/receiving over ipv4 */
120 GstRTSPRange server_port_v4;
121 GstRTSPAddress *server_addr_v4;
124 /* server ports for sending/receiving over ipv6 */
125 GstRTSPRange server_port_v6;
126 GstRTSPAddress *server_addr_v6;
129 /* multicast addresses */
130 GstRTSPAddressPool *pool;
131 GstRTSPAddress *addr_v4;
132 GstRTSPAddress *addr_v6;
134 /* the caps of the stream */
138 /* transports we stream to */
141 guint transports_cookie;
143 GList *tr_cache_rtcp;
144 guint tr_cache_cookie;
146 /* UDP sources for UDP multicast transports */
147 GList *transport_sources;
151 /* stream blocking */
156 #define DEFAULT_CONTROL NULL
157 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
158 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
159 GST_RTSP_LOWER_TRANS_TCP
172 SIGNAL_NEW_RTP_ENCODER,
173 SIGNAL_NEW_RTCP_ENCODER,
177 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
178 #define GST_CAT_DEFAULT rtsp_stream_debug
180 static GQuark ssrc_stream_map_key;
182 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
183 GValue * value, GParamSpec * pspec);
184 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
185 const GValue * value, GParamSpec * pspec);
187 static void gst_rtsp_stream_finalize (GObject * obj);
189 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
191 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
194 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
196 GObjectClass *gobject_class;
198 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
200 gobject_class = G_OBJECT_CLASS (klass);
202 gobject_class->get_property = gst_rtsp_stream_get_property;
203 gobject_class->set_property = gst_rtsp_stream_set_property;
204 gobject_class->finalize = gst_rtsp_stream_finalize;
206 g_object_class_install_property (gobject_class, PROP_CONTROL,
207 g_param_spec_string ("control", "Control",
208 "The control string for this stream", DEFAULT_CONTROL,
209 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211 g_object_class_install_property (gobject_class, PROP_PROFILES,
212 g_param_spec_flags ("profiles", "Profiles",
213 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
214 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
217 g_param_spec_flags ("protocols", "Protocols",
218 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
219 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
222 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
224 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
226 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
227 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
228 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
229 G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
231 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
233 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
237 gst_rtsp_stream_init (GstRTSPStream * stream)
239 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
241 GST_DEBUG ("new stream %p", stream);
246 priv->control = g_strdup (DEFAULT_CONTROL);
247 priv->profiles = DEFAULT_PROFILES;
248 priv->protocols = DEFAULT_PROTOCOLS;
250 g_mutex_init (&priv->lock);
252 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
253 NULL, (GDestroyNotify) gst_caps_unref);
257 gst_rtsp_stream_finalize (GObject * obj)
259 GstRTSPStream *stream;
260 GstRTSPStreamPrivate *priv;
262 stream = GST_RTSP_STREAM (obj);
265 GST_DEBUG ("finalize stream %p", stream);
267 /* we really need to be unjoined now */
268 g_return_if_fail (!priv->is_joined);
271 gst_rtsp_address_free (priv->addr_v4);
273 gst_rtsp_address_free (priv->addr_v6);
274 if (priv->server_addr_v4)
275 gst_rtsp_address_free (priv->server_addr_v4);
276 if (priv->server_addr_v6)
277 gst_rtsp_address_free (priv->server_addr_v6);
279 g_object_unref (priv->pool);
281 g_object_unref (priv->rtxsend);
283 gst_object_unref (priv->payloader);
284 gst_object_unref (priv->srcpad);
285 g_free (priv->control);
286 g_mutex_clear (&priv->lock);
288 g_hash_table_unref (priv->keys);
290 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
294 gst_rtsp_stream_get_property (GObject * object, guint propid,
295 GValue * value, GParamSpec * pspec)
297 GstRTSPStream *stream = GST_RTSP_STREAM (object);
301 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
304 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
307 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
315 gst_rtsp_stream_set_property (GObject * object, guint propid,
316 const GValue * value, GParamSpec * pspec)
318 GstRTSPStream *stream = GST_RTSP_STREAM (object);
322 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
325 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
328 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
336 * gst_rtsp_stream_new:
339 * @payloader: a #GstElement
341 * Create a new media stream with index @idx that handles RTP data on
342 * @srcpad and has a payloader element @payloader.
344 * Returns: (transfer full): a new #GstRTSPStream
347 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
349 GstRTSPStreamPrivate *priv;
350 GstRTSPStream *stream;
352 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
353 g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
354 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
356 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
359 priv->payloader = gst_object_ref (payloader);
360 priv->srcpad = gst_object_ref (srcpad);
366 * gst_rtsp_stream_get_index:
367 * @stream: a #GstRTSPStream
369 * Get the stream index.
371 * Return: the stream index.
374 gst_rtsp_stream_get_index (GstRTSPStream * stream)
376 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
378 return stream->priv->idx;
382 * gst_rtsp_stream_get_pt:
383 * @stream: a #GstRTSPStream
385 * Get the stream payload type.
387 * Return: the stream payload type.
390 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
392 GstRTSPStreamPrivate *priv;
395 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
399 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
405 * gst_rtsp_stream_get_srcpad:
406 * @stream: a #GstRTSPStream
408 * Get the srcpad associated with @stream.
410 * Returns: (transfer full): the srcpad. Unref after usage.
413 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
415 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
417 return gst_object_ref (stream->priv->srcpad);
421 * gst_rtsp_stream_get_control:
422 * @stream: a #GstRTSPStream
424 * Get the control string to identify this stream.
426 * Returns: (transfer full): the control string. g_free() after usage.
429 gst_rtsp_stream_get_control (GstRTSPStream * stream)
431 GstRTSPStreamPrivate *priv;
434 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
438 g_mutex_lock (&priv->lock);
439 if ((result = g_strdup (priv->control)) == NULL)
440 result = g_strdup_printf ("stream=%u", priv->idx);
441 g_mutex_unlock (&priv->lock);
447 * gst_rtsp_stream_set_control:
448 * @stream: a #GstRTSPStream
449 * @control: a control string
451 * Set the control string in @stream.
454 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
456 GstRTSPStreamPrivate *priv;
458 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
462 g_mutex_lock (&priv->lock);
463 g_free (priv->control);
464 priv->control = g_strdup (control);
465 g_mutex_unlock (&priv->lock);
469 * gst_rtsp_stream_has_control:
470 * @stream: a #GstRTSPStream
471 * @control: a control string
473 * Check if @stream has the control string @control.
475 * Returns: %TRUE is @stream has @control as the control string
478 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
480 GstRTSPStreamPrivate *priv;
483 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
487 g_mutex_lock (&priv->lock);
489 res = (g_strcmp0 (priv->control, control) == 0);
493 if (sscanf (control, "stream=%u", &streamid) > 0)
494 res = (streamid == priv->idx);
498 g_mutex_unlock (&priv->lock);
504 * gst_rtsp_stream_set_mtu:
505 * @stream: a #GstRTSPStream
508 * Configure the mtu in the payloader of @stream to @mtu.
511 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
513 GstRTSPStreamPrivate *priv;
515 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
519 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
521 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
525 * gst_rtsp_stream_get_mtu:
526 * @stream: a #GstRTSPStream
528 * Get the configured MTU in the payloader of @stream.
530 * Returns: the MTU of the payloader.
533 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
535 GstRTSPStreamPrivate *priv;
538 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
542 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
547 /* Update the dscp qos property on the udp sinks */
549 update_dscp_qos (GstRTSPStream * stream)
551 GstRTSPStreamPrivate *priv;
553 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
557 if (priv->udpsink[0]) {
558 g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
562 if (priv->udpsink[1]) {
563 g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
569 * gst_rtsp_stream_set_dscp_qos:
570 * @stream: a #GstRTSPStream
571 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
573 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
576 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
578 GstRTSPStreamPrivate *priv;
580 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
584 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
586 if (dscp_qos < -1 || dscp_qos > 63) {
587 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
591 priv->dscp_qos = dscp_qos;
593 update_dscp_qos (stream);
597 * gst_rtsp_stream_get_dscp_qos:
598 * @stream: a #GstRTSPStream
600 * Get the configured DSCP QoS in of the outgoing sockets.
602 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
605 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
607 GstRTSPStreamPrivate *priv;
609 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
613 return priv->dscp_qos;
617 * gst_rtsp_stream_is_transport_supported:
618 * @stream: a #GstRTSPStream
619 * @transport: (transfer none): a #GstRTSPTransport
621 * Check if @transport can be handled by stream
623 * Returns: %TRUE if @transport can be handled by @stream.
626 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
627 GstRTSPTransport * transport)
629 GstRTSPStreamPrivate *priv;
631 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
635 g_mutex_lock (&priv->lock);
636 if (transport->trans != GST_RTSP_TRANS_RTP)
637 goto unsupported_transmode;
639 if (!(transport->profile & priv->profiles))
640 goto unsupported_profile;
642 if (!(transport->lower_transport & priv->protocols))
643 goto unsupported_ltrans;
645 g_mutex_unlock (&priv->lock);
650 unsupported_transmode:
652 GST_DEBUG ("unsupported transport mode %d", transport->trans);
653 g_mutex_unlock (&priv->lock);
658 GST_DEBUG ("unsupported profile %d", transport->profile);
659 g_mutex_unlock (&priv->lock);
664 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
665 g_mutex_unlock (&priv->lock);
671 * gst_rtsp_stream_set_profiles:
672 * @stream: a #GstRTSPStream
673 * @profiles: the new profiles
675 * Configure the allowed profiles for @stream.
678 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
680 GstRTSPStreamPrivate *priv;
682 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
686 g_mutex_lock (&priv->lock);
687 priv->profiles = profiles;
688 g_mutex_unlock (&priv->lock);
692 * gst_rtsp_stream_get_profiles:
693 * @stream: a #GstRTSPStream
695 * Get the allowed profiles of @stream.
697 * Returns: a #GstRTSPProfile
700 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
702 GstRTSPStreamPrivate *priv;
705 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
709 g_mutex_lock (&priv->lock);
710 res = priv->profiles;
711 g_mutex_unlock (&priv->lock);
717 * gst_rtsp_stream_set_protocols:
718 * @stream: a #GstRTSPStream
719 * @protocols: the new flags
721 * Configure the allowed lower transport for @stream.
724 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
725 GstRTSPLowerTrans protocols)
727 GstRTSPStreamPrivate *priv;
729 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
733 g_mutex_lock (&priv->lock);
734 priv->protocols = protocols;
735 g_mutex_unlock (&priv->lock);
739 * gst_rtsp_stream_get_protocols:
740 * @stream: a #GstRTSPStream
742 * Get the allowed protocols of @stream.
744 * Returns: a #GstRTSPLowerTrans
747 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
749 GstRTSPStreamPrivate *priv;
750 GstRTSPLowerTrans res;
752 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
753 GST_RTSP_LOWER_TRANS_UNKNOWN);
757 g_mutex_lock (&priv->lock);
758 res = priv->protocols;
759 g_mutex_unlock (&priv->lock);
765 * gst_rtsp_stream_set_address_pool:
766 * @stream: a #GstRTSPStream
767 * @pool: (transfer none): a #GstRTSPAddressPool
769 * configure @pool to be used as the address pool of @stream.
772 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
773 GstRTSPAddressPool * pool)
775 GstRTSPStreamPrivate *priv;
776 GstRTSPAddressPool *old;
778 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
782 GST_LOG_OBJECT (stream, "set address pool %p", pool);
784 g_mutex_lock (&priv->lock);
785 if ((old = priv->pool) != pool)
786 priv->pool = pool ? g_object_ref (pool) : NULL;
789 g_mutex_unlock (&priv->lock);
792 g_object_unref (old);
796 * gst_rtsp_stream_get_address_pool:
797 * @stream: a #GstRTSPStream
799 * Get the #GstRTSPAddressPool used as the address pool of @stream.
801 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
805 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
807 GstRTSPStreamPrivate *priv;
808 GstRTSPAddressPool *result;
810 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
814 g_mutex_lock (&priv->lock);
815 if ((result = priv->pool))
816 g_object_ref (result);
817 g_mutex_unlock (&priv->lock);
823 * gst_rtsp_stream_get_multicast_address:
824 * @stream: a #GstRTSPStream
825 * @family: the #GSocketFamily
827 * Get the multicast address of @stream for @family.
829 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
830 * or %NULL when no address could be allocated. gst_rtsp_address_free()
834 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
835 GSocketFamily family)
837 GstRTSPStreamPrivate *priv;
838 GstRTSPAddress *result;
839 GstRTSPAddress **addrp;
840 GstRTSPAddressFlags flags;
842 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
846 if (family == G_SOCKET_FAMILY_IPV6) {
847 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
848 addrp = &priv->addr_v6;
850 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
851 addrp = &priv->addr_v4;
854 g_mutex_lock (&priv->lock);
855 if (*addrp == NULL) {
856 if (priv->pool == NULL)
859 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
861 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
865 result = gst_rtsp_address_copy (*addrp);
866 g_mutex_unlock (&priv->lock);
873 GST_ERROR_OBJECT (stream, "no address pool specified");
874 g_mutex_unlock (&priv->lock);
879 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
880 g_mutex_unlock (&priv->lock);
886 * gst_rtsp_stream_reserve_address:
887 * @stream: a #GstRTSPStream
888 * @address: an address
893 * Reserve @address and @port as the address and port of @stream.
895 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
896 * the address could be reserved. gst_rtsp_address_free() after usage.
899 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
900 const gchar * address, guint port, guint n_ports, guint ttl)
902 GstRTSPStreamPrivate *priv;
903 GstRTSPAddress *result;
905 GSocketFamily family;
906 GstRTSPAddress **addrp;
908 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
909 g_return_val_if_fail (address != NULL, NULL);
910 g_return_val_if_fail (port > 0, NULL);
911 g_return_val_if_fail (n_ports > 0, NULL);
912 g_return_val_if_fail (ttl > 0, NULL);
916 addr = g_inet_address_new_from_string (address);
918 GST_ERROR ("failed to get inet addr from %s", address);
919 family = G_SOCKET_FAMILY_IPV4;
921 family = g_inet_address_get_family (addr);
922 g_object_unref (addr);
925 if (family == G_SOCKET_FAMILY_IPV6)
926 addrp = &priv->addr_v6;
928 addrp = &priv->addr_v4;
930 g_mutex_lock (&priv->lock);
931 if (*addrp == NULL) {
932 GstRTSPAddressPoolResult res;
934 if (priv->pool == NULL)
937 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
938 port, n_ports, ttl, addrp);
939 if (res != GST_RTSP_ADDRESS_POOL_OK)
942 if (strcmp ((*addrp)->address, address) ||
943 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
944 (*addrp)->ttl != ttl)
945 goto different_address;
947 result = gst_rtsp_address_copy (*addrp);
948 g_mutex_unlock (&priv->lock);
955 GST_ERROR_OBJECT (stream, "no address pool specified");
956 g_mutex_unlock (&priv->lock);
961 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
963 g_mutex_unlock (&priv->lock);
968 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
969 " reserved", address);
970 g_mutex_unlock (&priv->lock);
976 alloc_ports_one_family (GstRTSPAddressPool * pool, gint buffer_size,
977 GSocketFamily family, GstElement * udpsrc_out[2],
978 GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
979 GstRTSPAddress ** server_addr_out)
981 GstStateChangeReturn ret;
982 GstElement *udpsrc0, *udpsrc1;
983 GstElement *udpsink0, *udpsink1;
984 GSocket *rtp_socket = NULL;
985 GSocket *rtcp_socket;
986 gint tmp_rtp, tmp_rtcp;
988 gint rtpport, rtcpport;
989 GList *rejected_addresses = NULL;
990 GstRTSPAddress *addr = NULL;
991 GInetAddress *inetaddr = NULL;
992 GSocketAddress *rtp_sockaddr = NULL;
993 GSocketAddress *rtcp_sockaddr = NULL;
994 const gchar *multisink_socket;
996 if (family == G_SOCKET_FAMILY_IPV6)
997 multisink_socket = "socket-v6";
999 multisink_socket = "socket";
1007 /* Start with random port */
1010 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1011 G_SOCKET_PROTOCOL_UDP, NULL);
1013 goto no_udp_protocol;
1015 if (*server_addr_out)
1016 gst_rtsp_address_free (*server_addr_out);
1018 /* try to allocate 2 UDP ports, the RTP port should be an even
1019 * number and the RTCP port should be the next (uneven) port */
1022 if (rtp_socket == NULL) {
1023 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1024 G_SOCKET_PROTOCOL_UDP, NULL);
1026 goto no_udp_protocol;
1029 if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
1030 GstRTSPAddressFlags flags;
1033 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1035 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
1036 if (family == G_SOCKET_FAMILY_IPV6)
1037 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1039 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1041 addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1046 tmp_rtp = addr->port;
1048 g_clear_object (&inetaddr);
1049 inetaddr = g_inet_address_new_from_string (addr->address);
1057 if (inetaddr == NULL)
1058 inetaddr = g_inet_address_new_any (family);
1061 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1062 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1063 g_object_unref (rtp_sockaddr);
1066 g_object_unref (rtp_sockaddr);
1068 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1069 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1070 g_clear_object (&rtp_sockaddr);
1075 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1076 g_object_unref (rtp_sockaddr);
1078 /* check if port is even */
1079 if ((tmp_rtp & 1) != 0) {
1080 /* port not even, close and allocate another */
1082 g_clear_object (&rtp_socket);
1087 tmp_rtcp = tmp_rtp + 1;
1089 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1090 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1091 g_object_unref (rtcp_sockaddr);
1092 g_clear_object (&rtp_socket);
1095 g_object_unref (rtcp_sockaddr);
1097 g_clear_object (&inetaddr);
1099 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
1100 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
1102 if (udpsrc0 == NULL || udpsrc1 == NULL)
1103 goto no_udp_protocol;
1105 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
1106 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
1108 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1109 if (ret == GST_STATE_CHANGE_FAILURE)
1111 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1112 if (ret == GST_STATE_CHANGE_FAILURE)
1115 /* all fine, do port check */
1116 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
1117 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
1119 /* this should not happen... */
1120 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1124 udpsink0 = udpsink_out[0];
1126 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1129 goto no_udp_protocol;
1131 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1132 g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
1135 udpsink1 = udpsink_out[1];
1137 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1140 goto no_udp_protocol;
1142 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1143 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1144 g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
1146 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1147 g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
1148 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1149 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1150 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1151 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1152 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1153 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1155 /* we keep these elements, we will further configure them when the
1156 * client told us to really use the UDP ports. */
1157 udpsrc_out[0] = udpsrc0;
1158 udpsrc_out[1] = udpsrc1;
1159 udpsink_out[0] = udpsink0;
1160 udpsink_out[1] = udpsink1;
1162 server_port_out->min = rtpport;
1163 server_port_out->max = rtcpport;
1165 *server_addr_out = addr;
1166 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1168 g_object_unref (rtp_socket);
1169 g_object_unref (rtcp_socket);
1197 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1198 gst_object_unref (udpsrc0);
1201 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1202 gst_object_unref (udpsrc1);
1205 gst_element_set_state (udpsink0, GST_STATE_NULL);
1206 gst_object_unref (udpsink0);
1209 g_object_unref (inetaddr);
1210 g_list_free_full (rejected_addresses,
1211 (GDestroyNotify) gst_rtsp_address_free);
1213 gst_rtsp_address_free (addr);
1215 g_object_unref (rtp_socket);
1217 g_object_unref (rtcp_socket);
1222 /* must be called with lock */
1224 alloc_ports (GstRTSPStream * stream)
1226 GstRTSPStreamPrivate *priv = stream->priv;
1228 priv->have_ipv4 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1229 G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1230 &priv->server_port_v4, &priv->server_addr_v4);
1232 priv->have_ipv6 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1233 G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1234 &priv->server_port_v6, &priv->server_addr_v6);
1236 return priv->have_ipv4 || priv->have_ipv6;
1240 * gst_rtsp_stream_get_server_port:
1241 * @stream: a #GstRTSPStream
1242 * @server_port: (out): result server port
1243 * @family: the port family to get
1245 * Fill @server_port with the port pair used by the server. This function can
1246 * only be called when @stream has been joined.
1249 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1250 GstRTSPRange * server_port, GSocketFamily family)
1252 GstRTSPStreamPrivate *priv;
1254 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1255 priv = stream->priv;
1256 g_return_if_fail (priv->is_joined);
1258 g_mutex_lock (&priv->lock);
1259 if (family == G_SOCKET_FAMILY_IPV4) {
1261 *server_port = priv->server_port_v4;
1264 *server_port = priv->server_port_v6;
1266 g_mutex_unlock (&priv->lock);
1270 * gst_rtsp_stream_get_rtpsession:
1271 * @stream: a #GstRTSPStream
1273 * Get the RTP session of this stream.
1275 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1278 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1280 GstRTSPStreamPrivate *priv;
1283 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1285 priv = stream->priv;
1287 g_mutex_lock (&priv->lock);
1288 if ((session = priv->session))
1289 g_object_ref (session);
1290 g_mutex_unlock (&priv->lock);
1296 * gst_rtsp_stream_get_ssrc:
1297 * @stream: a #GstRTSPStream
1298 * @ssrc: (out): result ssrc
1300 * Get the SSRC used by the RTP session of this stream. This function can only
1301 * be called when @stream has been joined.
1304 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1306 GstRTSPStreamPrivate *priv;
1308 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1309 priv = stream->priv;
1310 g_return_if_fail (priv->is_joined);
1312 g_mutex_lock (&priv->lock);
1313 if (ssrc && priv->session)
1314 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1315 g_mutex_unlock (&priv->lock);
1319 * gst_rtsp_stream_set_retransmission_time:
1320 * @stream: a #GstRTSPStream
1321 * @time: a #GstClockTime
1323 * Set the amount of time to store retransmission packets.
1326 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1329 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1331 g_mutex_lock (&stream->priv->lock);
1332 stream->priv->rtx_time = time;
1333 if (stream->priv->rtxsend)
1334 g_object_set (stream->priv->rtxsend, "max-size-time",
1335 GST_TIME_AS_MSECONDS (time), NULL);
1336 g_mutex_unlock (&stream->priv->lock);
1340 * gst_rtsp_media_get_retransmission_time:
1341 * @media: a #GstRTSPMedia
1343 * Get the amount of time to store retransmission data.
1345 * Returns: the amount of time to store retransmission data.
1348 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1352 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1354 g_mutex_lock (&stream->priv->lock);
1355 ret = stream->priv->rtx_time;
1356 g_mutex_unlock (&stream->priv->lock);
1362 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1364 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1366 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1368 g_mutex_lock (&stream->priv->lock);
1369 stream->priv->rtx_pt = rtx_pt;
1370 if (stream->priv->rtxsend) {
1371 guint pt = gst_rtsp_stream_get_pt (stream);
1372 gchar *pt_s = g_strdup_printf ("%d", pt);
1373 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1374 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1375 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1377 gst_structure_free (rtx_pt_map);
1379 g_mutex_unlock (&stream->priv->lock);
1383 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1387 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1389 g_mutex_lock (&stream->priv->lock);
1390 rtx_pt = stream->priv->rtx_pt;
1391 g_mutex_unlock (&stream->priv->lock);
1396 /* executed from streaming thread */
1398 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1400 GstRTSPStreamPrivate *priv = stream->priv;
1401 GstCaps *newcaps, *oldcaps;
1403 newcaps = gst_pad_get_current_caps (pad);
1405 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1408 g_mutex_lock (&priv->lock);
1409 oldcaps = priv->caps;
1410 priv->caps = newcaps;
1411 g_mutex_unlock (&priv->lock);
1414 gst_caps_unref (oldcaps);
1418 dump_structure (const GstStructure * s)
1422 sstr = gst_structure_to_string (s);
1423 GST_INFO ("structure: %s", sstr);
1427 static GstRTSPStreamTransport *
1428 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1430 GstRTSPStreamPrivate *priv = stream->priv;
1432 GstRTSPStreamTransport *result = NULL;
1437 if (rtcp_from == NULL)
1440 tmp = g_strrstr (rtcp_from, ":");
1444 port = atoi (tmp + 1);
1445 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1447 g_mutex_lock (&priv->lock);
1448 GST_INFO ("finding %s:%d in %d transports", dest, port,
1449 g_list_length (priv->transports));
1451 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1452 GstRTSPStreamTransport *trans = walk->data;
1453 const GstRTSPTransport *tr;
1456 tr = gst_rtsp_stream_transport_get_transport (trans);
1458 min = tr->client_port.min;
1459 max = tr->client_port.max;
1461 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1467 g_object_ref (result);
1468 g_mutex_unlock (&priv->lock);
1475 static GstRTSPStreamTransport *
1476 check_transport (GObject * source, GstRTSPStream * stream)
1478 GstStructure *stats;
1479 GstRTSPStreamTransport *trans;
1481 /* see if we have a stream to match with the origin of the RTCP packet */
1482 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1483 if (trans == NULL) {
1484 g_object_get (source, "stats", &stats, NULL);
1486 const gchar *rtcp_from;
1488 dump_structure (stats);
1490 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1491 if ((trans = find_transport (stream, rtcp_from))) {
1492 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1494 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1497 gst_structure_free (stats);
1505 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1507 GstRTSPStreamTransport *trans;
1509 GST_INFO ("%p: new source %p", stream, source);
1511 trans = check_transport (source, stream);
1514 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1518 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1520 GST_INFO ("%p: new SDES %p", stream, source);
1524 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1526 GstRTSPStreamTransport *trans;
1528 trans = check_transport (source, stream);
1531 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1532 gst_rtsp_stream_transport_keep_alive (trans);
1536 GstStructure *stats;
1537 g_object_get (source, "stats", &stats, NULL);
1539 dump_structure (stats);
1540 gst_structure_free (stats);
1547 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1549 GST_INFO ("%p: source %p bye", stream, source);
1553 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1555 GstRTSPStreamTransport *trans;
1557 GST_INFO ("%p: source %p bye timeout", stream, source);
1559 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1560 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1561 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1566 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1568 GstRTSPStreamTransport *trans;
1570 GST_INFO ("%p: source %p timeout", stream, source);
1572 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1573 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1574 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1579 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1582 g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1583 g_list_free (priv->tr_cache_rtp);
1584 priv->tr_cache_rtp = NULL;
1586 g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1587 g_list_free (priv->tr_cache_rtcp);
1588 priv->tr_cache_rtcp = NULL;
1592 static GstFlowReturn
1593 handle_new_sample (GstAppSink * sink, gpointer user_data)
1595 GstRTSPStreamPrivate *priv;
1599 GstRTSPStream *stream;
1602 sample = gst_app_sink_pull_sample (sink);
1606 stream = (GstRTSPStream *) user_data;
1607 priv = stream->priv;
1608 buffer = gst_sample_get_buffer (sample);
1610 is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
1612 g_mutex_lock (&priv->lock);
1613 if (priv->tr_cache_cookie != priv->transports_cookie) {
1614 clear_tr_cache (priv, is_rtp);
1615 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1616 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1618 priv->tr_cache_rtp =
1619 g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
1621 priv->tr_cache_rtcp =
1622 g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
1625 priv->tr_cache_cookie = priv->transports_cookie;
1627 g_mutex_unlock (&priv->lock);
1630 for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
1631 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1632 gst_rtsp_stream_transport_send_rtp (tr, buffer);
1635 for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
1636 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1637 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1640 gst_sample_unref (sample);
1645 static GstAppSinkCallbacks sink_cb = {
1646 NULL, /* not interested in EOS */
1647 NULL, /* not interested in preroll samples */
1652 get_rtp_encoder (GstRTSPStream * stream, guint session)
1654 GstRTSPStreamPrivate *priv = stream->priv;
1656 if (priv->srtpenc == NULL) {
1659 name = g_strdup_printf ("srtpenc_%u", session);
1660 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
1663 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
1665 return gst_object_ref (priv->srtpenc);
1669 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
1671 GstRTSPStreamPrivate *priv = stream->priv;
1672 GstElement *oldenc, *enc;
1676 if (priv->idx != session)
1679 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
1681 oldenc = priv->srtpenc;
1682 enc = get_rtp_encoder (stream, session);
1683 name = g_strdup_printf ("rtp_sink_%d", session);
1684 pad = gst_element_get_request_pad (enc, name);
1686 gst_object_unref (pad);
1689 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
1696 request_rtcp_encoder (GstElement * rtpbin, guint session,
1697 GstRTSPStream * stream)
1699 GstRTSPStreamPrivate *priv = stream->priv;
1700 GstElement *oldenc, *enc;
1704 if (priv->idx != session)
1707 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
1709 oldenc = priv->srtpenc;
1710 enc = get_rtp_encoder (stream, session);
1711 name = g_strdup_printf ("rtcp_sink_%d", session);
1712 pad = gst_element_get_request_pad (enc, name);
1714 gst_object_unref (pad);
1717 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
1724 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
1726 GstRTSPStreamPrivate *priv = stream->priv;
1729 GST_DEBUG ("request key %08x", ssrc);
1731 g_mutex_lock (&priv->lock);
1732 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
1733 gst_caps_ref (caps);
1734 g_mutex_unlock (&priv->lock);
1740 request_rtcp_decoder (GstElement * rtpbin, guint session,
1741 GstRTSPStream * stream)
1743 GstRTSPStreamPrivate *priv = stream->priv;
1745 if (priv->idx != session)
1748 if (priv->srtpdec == NULL) {
1751 name = g_strdup_printf ("srtpdec_%u", session);
1752 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
1755 g_signal_connect (priv->srtpdec, "request-key",
1756 (GCallback) request_key, stream);
1758 return gst_object_ref (priv->srtpdec);
1762 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPStream * stream)
1766 GstStructure *pt_map;
1771 pt = gst_rtsp_stream_get_pt (stream);
1772 pt_s = g_strdup_printf ("%u", pt);
1773 rtx_pt = stream->priv->rtx_pt;
1775 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
1777 bin = gst_bin_new (NULL);
1778 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
1779 pt_map = gst_structure_new ("application/x-rtp-pt-map",
1780 pt_s, G_TYPE_UINT, rtx_pt, NULL);
1781 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
1782 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
1784 gst_structure_free (pt_map);
1785 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
1787 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
1788 name = g_strdup_printf ("src_%u", sessid);
1789 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1791 gst_object_unref (pad);
1793 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
1794 name = g_strdup_printf ("sink_%u", sessid);
1795 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1797 gst_object_unref (pad);
1803 * gst_rtsp_stream_join_bin:
1804 * @stream: a #GstRTSPStream
1805 * @bin: (transfer none): a #GstBin to join
1806 * @rtpbin: (transfer none): a rtpbin element in @bin
1807 * @state: the target state of the new elements
1809 * Join the #GstBin @bin that contains the element @rtpbin.
1811 * @stream will link to @rtpbin, which must be inside @bin. The elements
1812 * added to @bin will be set to the state given in @state.
1814 * Returns: %TRUE on success.
1817 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1818 GstElement * rtpbin, GstState state)
1820 GstRTSPStreamPrivate *priv;
1824 GstPad *pad, *sinkpad, *selpad;
1825 GstPadLinkReturn ret;
1827 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1828 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1829 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1831 priv = stream->priv;
1833 g_mutex_lock (&priv->lock);
1834 if (priv->is_joined)
1837 /* create a session with the same index as the stream */
1840 GST_INFO ("stream %p joining bin as session %u", stream, idx);
1842 if (!alloc_ports (stream))
1845 /* update the dscp qos field in the sinks */
1846 update_dscp_qos (stream);
1848 if (priv->profiles & GST_RTSP_PROFILE_SAVP
1849 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
1851 g_signal_connect (rtpbin, "request-rtp-encoder",
1852 (GCallback) request_rtp_encoder, stream);
1853 g_signal_connect (rtpbin, "request-rtcp-encoder",
1854 (GCallback) request_rtcp_encoder, stream);
1855 g_signal_connect (rtpbin, "request-rtcp-decoder",
1856 (GCallback) request_rtcp_decoder, stream);
1859 if (priv->rtx_time > 0) {
1860 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
1861 g_signal_connect (rtpbin, "request-aux-sender",
1862 (GCallback) request_aux_sender, stream);
1865 /* get a pad for sending RTP */
1866 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1867 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
1869 /* link the RTP pad to the session manager, it should not really fail unless
1870 * this is not really an RTP pad */
1871 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
1872 if (ret != GST_PAD_LINK_OK)
1875 /* get pads from the RTP session element for sending and receiving
1877 name = g_strdup_printf ("send_rtp_src_%u", idx);
1878 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
1880 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1881 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
1883 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1884 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
1886 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1887 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
1890 /* get the session */
1891 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
1893 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1895 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1897 g_signal_connect (priv->session, "on-ssrc-active",
1898 (GCallback) on_ssrc_active, stream);
1899 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1901 g_signal_connect (priv->session, "on-bye-timeout",
1902 (GCallback) on_bye_timeout, stream);
1903 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1906 for (i = 0; i < 2; i++) {
1907 GstPad *teepad, *queuepad;
1908 /* For the sender we create this bit of pipeline for both
1909 * RTP and RTCP. Sync and preroll are enabled on udpsink so
1910 * we need to add a queue before appsink to make the pipeline
1911 * not block. For the TCP case, we want to pump data to the
1912 * client as fast as possible anyway.
1914 * .--------. .-----. .---------.
1915 * | rtpbin | | tee | | udpsink |
1916 * | send->sink src->sink |
1917 * '--------' | | '---------'
1918 * | | .---------. .---------.
1919 * | | | queue | | appsink |
1920 * | src->sink src->sink |
1921 * '-----' '---------' '---------'
1923 * When only UDP is allowed, we skip the tee, queue and appsink and link the
1924 * udpsink directly to the session.
1927 gst_bin_add (bin, priv->udpsink[i]);
1928 sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1930 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1931 /* make tee for RTP/RTCP */
1932 priv->tee[i] = gst_element_factory_make ("tee", NULL);
1933 gst_bin_add (bin, priv->tee[i]);
1935 /* and link to rtpbin send pad */
1936 pad = gst_element_get_static_pad (priv->tee[i], "sink");
1937 gst_pad_link (priv->send_src[i], pad);
1938 gst_object_unref (pad);
1940 /* link tee to udpsink */
1941 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1942 gst_pad_link (teepad, sinkpad);
1943 gst_object_unref (teepad);
1946 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1947 gst_bin_add (bin, priv->appqueue[i]);
1948 /* and link to tee */
1949 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1950 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1951 gst_pad_link (teepad, pad);
1952 gst_object_unref (pad);
1953 gst_object_unref (teepad);
1956 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1957 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1958 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1959 gst_bin_add (bin, priv->appsink[i]);
1960 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1961 &sink_cb, stream, NULL);
1962 /* and link to queue */
1963 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1964 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1965 gst_pad_link (queuepad, pad);
1966 gst_object_unref (pad);
1967 gst_object_unref (queuepad);
1969 /* else only udpsink needed, link it to the session */
1970 gst_pad_link (priv->send_src[i], sinkpad);
1972 gst_object_unref (sinkpad);
1974 /* For the receiver we create this bit of pipeline for both
1975 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1976 * and it is all funneled into the rtpbin receive pad.
1978 * .--------. .--------. .--------.
1979 * | udpsrc | | funnel | | rtpbin |
1980 * | src->sink src->sink |
1981 * '--------' | | '--------'
1985 * '--------' '--------'
1987 /* make funnel for the RTP/RTCP receivers */
1988 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1989 gst_bin_add (bin, priv->funnel[i]);
1991 pad = gst_element_get_static_pad (priv->funnel[i], "src");
1992 gst_pad_link (pad, priv->recv_sink[i]);
1993 gst_object_unref (pad);
1995 if (priv->udpsrc_v4[i]) {
1996 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1998 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
1999 gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
2001 gst_bin_add (bin, priv->udpsrc_v4[i]);
2003 /* and link to the funnel v4 */
2004 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2005 pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
2006 gst_pad_link (pad, selpad);
2007 gst_object_unref (pad);
2008 gst_object_unref (selpad);
2011 if (priv->udpsrc_v6[i]) {
2012 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
2013 gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
2014 gst_bin_add (bin, priv->udpsrc_v6[i]);
2016 /* and link to the funnel v6 */
2017 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2018 pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2019 gst_pad_link (pad, selpad);
2020 gst_object_unref (pad);
2021 gst_object_unref (selpad);
2024 if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
2025 /* make and add appsrc */
2026 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2027 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, NULL);
2028 gst_bin_add (bin, priv->appsrc[i]);
2029 /* and link to the funnel */
2030 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2031 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2032 gst_pad_link (pad, selpad);
2033 gst_object_unref (pad);
2034 gst_object_unref (selpad);
2037 /* check if we need to set to a special state */
2038 if (state != GST_STATE_NULL) {
2039 if (priv->udpsink[i])
2040 gst_element_set_state (priv->udpsink[i], state);
2041 if (priv->appsink[i])
2042 gst_element_set_state (priv->appsink[i], state);
2043 if (priv->appqueue[i])
2044 gst_element_set_state (priv->appqueue[i], state);
2046 gst_element_set_state (priv->tee[i], state);
2047 if (priv->funnel[i])
2048 gst_element_set_state (priv->funnel[i], state);
2049 if (priv->appsrc[i])
2050 gst_element_set_state (priv->appsrc[i], state);
2054 /* be notified of caps changes */
2055 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2056 (GCallback) caps_notify, stream);
2058 priv->is_joined = TRUE;
2059 g_mutex_unlock (&priv->lock);
2066 g_mutex_unlock (&priv->lock);
2071 g_mutex_unlock (&priv->lock);
2072 GST_WARNING ("failed to allocate ports %u", idx);
2077 GST_WARNING ("failed to link stream %u", idx);
2078 gst_object_unref (priv->send_rtp_sink);
2079 priv->send_rtp_sink = NULL;
2080 g_mutex_unlock (&priv->lock);
2086 * gst_rtsp_stream_leave_bin:
2087 * @stream: a #GstRTSPStream
2088 * @bin: (transfer none): a #GstBin
2089 * @rtpbin: (transfer none): a rtpbin #GstElement
2091 * Remove the elements of @stream from @bin.
2093 * Return: %TRUE on success.
2096 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2097 GstElement * rtpbin)
2099 GstRTSPStreamPrivate *priv;
2103 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2104 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2105 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2107 priv = stream->priv;
2109 g_mutex_lock (&priv->lock);
2110 if (!priv->is_joined)
2111 goto was_not_joined;
2113 /* all transports must be removed by now */
2114 if (priv->transports != NULL)
2115 goto transports_not_removed;
2117 clear_tr_cache (priv, TRUE);
2118 clear_tr_cache (priv, FALSE);
2120 GST_INFO ("stream %p leaving bin", stream);
2122 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2123 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2124 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2125 gst_object_unref (priv->send_rtp_sink);
2126 priv->send_rtp_sink = NULL;
2128 for (i = 0; i < 2; i++) {
2129 if (priv->udpsink[i])
2130 gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
2131 if (priv->appsink[i])
2132 gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
2133 if (priv->appqueue[i])
2134 gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
2136 gst_element_set_state (priv->tee[i], GST_STATE_NULL);
2137 if (priv->funnel[i])
2138 gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
2139 if (priv->appsrc[i])
2140 gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2141 if (priv->udpsrc_v4[i]) {
2142 /* and set udpsrc to NULL now before removing */
2143 gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
2144 gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2145 /* removing them should also nicely release the request
2146 * pads when they finalize */
2147 gst_bin_remove (bin, priv->udpsrc_v4[i]);
2149 if (priv->udpsrc_v6[i]) {
2150 gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
2151 gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2152 gst_bin_remove (bin, priv->udpsrc_v6[i]);
2155 for (l = priv->transport_sources; l; l = l->next) {
2156 GstRTSPMulticastTransportSource *s = l->data;
2161 gst_element_set_locked_state (s->udpsrc[i], FALSE);
2162 gst_element_set_state (s->udpsrc[i], GST_STATE_NULL);
2163 gst_bin_remove (bin, s->udpsrc[i]);
2166 if (priv->udpsink[i])
2167 gst_bin_remove (bin, priv->udpsink[i]);
2168 if (priv->appsrc[i])
2169 gst_bin_remove (bin, priv->appsrc[i]);
2170 if (priv->appsink[i])
2171 gst_bin_remove (bin, priv->appsink[i]);
2172 if (priv->appqueue[i])
2173 gst_bin_remove (bin, priv->appqueue[i]);
2175 gst_bin_remove (bin, priv->tee[i]);
2176 if (priv->funnel[i])
2177 gst_bin_remove (bin, priv->funnel[i]);
2179 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2180 gst_object_unref (priv->recv_sink[i]);
2181 priv->recv_sink[i] = NULL;
2183 priv->udpsrc_v4[i] = NULL;
2184 priv->udpsrc_v6[i] = NULL;
2185 priv->udpsink[i] = NULL;
2186 priv->appsrc[i] = NULL;
2187 priv->appsink[i] = NULL;
2188 priv->appqueue[i] = NULL;
2189 priv->tee[i] = NULL;
2190 priv->funnel[i] = NULL;
2193 for (l = priv->transport_sources; l; l = l->next) {
2194 GstRTSPMulticastTransportSource *s = l->data;
2195 g_slice_free (GstRTSPMulticastTransportSource, s);
2197 g_list_free (priv->transport_sources);
2198 priv->transport_sources = NULL;
2200 gst_object_unref (priv->send_src[0]);
2201 priv->send_src[0] = NULL;
2203 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2204 gst_object_unref (priv->send_src[1]);
2205 priv->send_src[1] = NULL;
2207 g_object_unref (priv->session);
2208 priv->session = NULL;
2210 gst_caps_unref (priv->caps);
2214 gst_object_unref (priv->srtpenc);
2216 gst_object_unref (priv->srtpdec);
2218 priv->is_joined = FALSE;
2219 g_mutex_unlock (&priv->lock);
2225 g_mutex_unlock (&priv->lock);
2228 transports_not_removed:
2230 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2231 g_mutex_unlock (&priv->lock);
2237 * gst_rtsp_stream_get_rtpinfo:
2238 * @stream: a #GstRTSPStream
2239 * @rtptime: (allow-none): result RTP timestamp
2240 * @seq: (allow-none): result RTP seqnum
2241 * @clock_rate: (allow-none): the clock rate
2242 * @running_time: (allow-none): result running-time
2244 * Retrieve the current rtptime, seq and running-time. This is used to
2245 * construct a RTPInfo reply header.
2247 * Returns: %TRUE when rtptime, seq and running-time could be determined.
2250 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2251 guint * rtptime, guint * seq, guint * clock_rate,
2252 GstClockTime * running_time)
2254 GstRTSPStreamPrivate *priv;
2255 GstStructure *stats;
2256 GObjectClass *payobjclass;
2258 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2260 priv = stream->priv;
2262 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2264 g_mutex_lock (&priv->lock);
2266 if (g_object_class_find_property (payobjclass, "stats")) {
2267 g_object_get (priv->payloader, "stats", &stats, NULL);
2272 gst_structure_get_uint (stats, "seqnum", seq);
2275 gst_structure_get_uint (stats, "timestamp", rtptime);
2278 gst_structure_get_clock_time (stats, "running-time", running_time);
2281 gst_structure_get_uint (stats, "clock-rate", clock_rate);
2282 if (*clock_rate == 0 && running_time)
2283 *running_time = GST_CLOCK_TIME_NONE;
2285 gst_structure_free (stats);
2287 if (!g_object_class_find_property (payobjclass, "seqnum") ||
2288 !g_object_class_find_property (payobjclass, "timestamp"))
2292 g_object_get (priv->payloader, "seqnum", seq, NULL);
2295 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
2298 *running_time = GST_CLOCK_TIME_NONE;
2300 g_mutex_unlock (&priv->lock);
2307 GST_WARNING ("Could not get payloader stats");
2308 g_mutex_unlock (&priv->lock);
2314 * gst_rtsp_stream_get_caps:
2315 * @stream: a #GstRTSPStream
2317 * Retrieve the current caps of @stream.
2319 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
2323 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
2325 GstRTSPStreamPrivate *priv;
2328 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2330 priv = stream->priv;
2332 g_mutex_lock (&priv->lock);
2333 if ((result = priv->caps))
2334 gst_caps_ref (result);
2335 g_mutex_unlock (&priv->lock);
2341 * gst_rtsp_stream_recv_rtp:
2342 * @stream: a #GstRTSPStream
2343 * @buffer: (transfer full): a #GstBuffer
2345 * Handle an RTP buffer for the stream. This method is usually called when a
2346 * message has been received from a client using the TCP transport.
2348 * This function takes ownership of @buffer.
2350 * Returns: a GstFlowReturn.
2353 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
2355 GstRTSPStreamPrivate *priv;
2357 GstElement *element;
2359 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2360 priv = stream->priv;
2361 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2362 g_return_val_if_fail (priv->is_joined, FALSE);
2364 g_mutex_lock (&priv->lock);
2365 if (priv->appsrc[0])
2366 element = gst_object_ref (priv->appsrc[0]);
2369 g_mutex_unlock (&priv->lock);
2372 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2373 gst_object_unref (element);
2381 * gst_rtsp_stream_recv_rtcp:
2382 * @stream: a #GstRTSPStream
2383 * @buffer: (transfer full): a #GstBuffer
2385 * Handle an RTCP buffer for the stream. This method is usually called when a
2386 * message has been received from a client using the TCP transport.
2388 * This function takes ownership of @buffer.
2390 * Returns: a GstFlowReturn.
2393 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
2395 GstRTSPStreamPrivate *priv;
2397 GstElement *element;
2399 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2400 priv = stream->priv;
2401 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2403 if (!priv->is_joined) {
2404 gst_buffer_unref (buffer);
2405 return GST_FLOW_NOT_LINKED;
2407 g_mutex_lock (&priv->lock);
2408 if (priv->appsrc[1])
2409 element = gst_object_ref (priv->appsrc[1]);
2412 g_mutex_unlock (&priv->lock);
2415 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2416 gst_object_unref (element);
2419 gst_buffer_unref (buffer);
2424 /* must be called with lock */
2426 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2429 GstRTSPStreamPrivate *priv = stream->priv;
2430 const GstRTSPTransport *tr;
2432 tr = gst_rtsp_stream_transport_get_transport (trans);
2434 switch (tr->lower_transport) {
2435 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2437 GstRTSPMulticastTransportSource *source;
2440 bin = GST_BIN (gst_object_get_parent (GST_OBJECT (priv->funnel[0])));
2445 GstPad *selpad, *pad;
2447 source = g_slice_new0 (GstRTSPMulticastTransportSource);
2448 source->transport = trans;
2450 for (i = 0; i < 2; i++) {
2452 g_strdup_printf ("udp://%s:%d", tr->destination,
2453 (i == 0) ? tr->port.min : tr->port.max);
2455 gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2458 /* we set and keep these to playing so that they don't cause NO_PREROLL return
2460 gst_element_set_state (source->udpsrc[i], GST_STATE_PLAYING);
2461 gst_element_set_locked_state (source->udpsrc[i], TRUE);
2463 gst_bin_add (bin, source->udpsrc[i]);
2465 /* and link to the funnel v4 */
2466 source->selpad[i] = selpad =
2467 gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2468 pad = gst_element_get_static_pad (source->udpsrc[i], "src");
2469 gst_pad_link (pad, selpad);
2470 gst_object_unref (pad);
2471 gst_object_unref (selpad);
2473 gst_object_unref (bin);
2475 priv->transport_sources =
2476 g_list_prepend (priv->transport_sources, source);
2480 for (l = priv->transport_sources; l; l = l->next) {
2483 if (source->transport == trans) {
2484 priv->transport_sources =
2485 g_list_delete_link (priv->transport_sources, l);
2493 for (i = 0; i < 2; i++) {
2494 /* Will automatically unlink everything */
2495 gst_bin_remove (bin,
2496 GST_ELEMENT (gst_object_ref (source->udpsrc[i])));
2498 gst_element_set_state (source->udpsrc[i], GST_STATE_NULL);
2499 gst_object_unref (source->udpsrc[i]);
2501 gst_element_release_request_pad (priv->funnel[i],
2505 g_slice_free (GstRTSPMulticastTransportSource, source);
2509 /* fall through for the generic case */
2511 case GST_RTSP_LOWER_TRANS_UDP:
2517 dest = tr->destination;
2518 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2523 min = tr->client_port.min;
2524 max = tr->client_port.max;
2529 GST_INFO ("setting ttl-mc %d", ttl);
2530 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
2531 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
2533 GST_INFO ("adding %s:%d-%d", dest, min, max);
2534 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
2535 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
2536 priv->transports = g_list_prepend (priv->transports, trans);
2538 GST_INFO ("removing %s:%d-%d", dest, min, max);
2539 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
2540 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
2541 priv->transports = g_list_remove (priv->transports, trans);
2543 priv->transports_cookie++;
2546 case GST_RTSP_LOWER_TRANS_TCP:
2548 GST_INFO ("adding TCP %s", tr->destination);
2549 priv->transports = g_list_prepend (priv->transports, trans);
2551 GST_INFO ("removing TCP %s", tr->destination);
2552 priv->transports = g_list_remove (priv->transports, trans);
2554 priv->transports_cookie++;
2557 goto unknown_transport;
2564 GST_INFO ("Unknown transport %d", tr->lower_transport);
2571 * gst_rtsp_stream_add_transport:
2572 * @stream: a #GstRTSPStream
2573 * @trans: (transfer none): a #GstRTSPStreamTransport
2575 * Add the transport in @trans to @stream. The media of @stream will
2576 * then also be send to the values configured in @trans.
2578 * @stream must be joined to a bin.
2580 * @trans must contain a valid #GstRTSPTransport.
2582 * Returns: %TRUE if @trans was added
2585 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
2586 GstRTSPStreamTransport * trans)
2588 GstRTSPStreamPrivate *priv;
2591 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2592 priv = stream->priv;
2593 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2594 g_return_val_if_fail (priv->is_joined, FALSE);
2596 g_mutex_lock (&priv->lock);
2597 res = update_transport (stream, trans, TRUE);
2598 g_mutex_unlock (&priv->lock);
2604 * gst_rtsp_stream_remove_transport:
2605 * @stream: a #GstRTSPStream
2606 * @trans: (transfer none): a #GstRTSPStreamTransport
2608 * Remove the transport in @trans from @stream. The media of @stream will
2609 * not be sent to the values configured in @trans.
2611 * @stream must be joined to a bin.
2613 * @trans must contain a valid #GstRTSPTransport.
2615 * Returns: %TRUE if @trans was removed
2618 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2619 GstRTSPStreamTransport * trans)
2621 GstRTSPStreamPrivate *priv;
2624 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2625 priv = stream->priv;
2626 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2627 g_return_val_if_fail (priv->is_joined, FALSE);
2629 g_mutex_lock (&priv->lock);
2630 res = update_transport (stream, trans, FALSE);
2631 g_mutex_unlock (&priv->lock);
2637 * gst_rtsp_stream_update_crypto:
2638 * @stream: a #GstRTSPStream
2640 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
2642 * Update the new crypto information for @ssrc in @stream. If information
2643 * for @ssrc did not exist, it will be added. If information
2644 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
2645 * be removed from @stream.
2647 * Returns: %TRUE if @crypto could be updated
2650 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
2651 guint ssrc, GstCaps * crypto)
2653 GstRTSPStreamPrivate *priv;
2655 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2656 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
2658 priv = stream->priv;
2660 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
2662 g_mutex_lock (&priv->lock);
2664 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
2665 gst_caps_ref (crypto));
2667 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
2668 g_mutex_unlock (&priv->lock);
2674 * gst_rtsp_stream_get_rtp_socket:
2675 * @stream: a #GstRTSPStream
2676 * @family: the socket family
2678 * Get the RTP socket from @stream for a @family.
2680 * @stream must be joined to a bin.
2682 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
2683 * socket could be allocated for @family. Unref after usage
2686 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2688 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2692 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2693 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2694 family == G_SOCKET_FAMILY_IPV6, NULL);
2695 g_return_val_if_fail (priv->udpsink[0], NULL);
2697 if (family == G_SOCKET_FAMILY_IPV6)
2702 g_object_get (priv->udpsink[0], name, &socket, NULL);
2708 * gst_rtsp_stream_get_rtcp_socket:
2709 * @stream: a #GstRTSPStream
2710 * @family: the socket family
2712 * Get the RTCP socket from @stream for a @family.
2714 * @stream must be joined to a bin.
2716 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
2717 * socket could be allocated for @family. Unref after usage
2720 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
2722 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2726 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2727 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2728 family == G_SOCKET_FAMILY_IPV6, NULL);
2729 g_return_val_if_fail (priv->udpsink[1], NULL);
2731 if (family == G_SOCKET_FAMILY_IPV6)
2736 g_object_get (priv->udpsink[1], name, &socket, NULL);
2742 * gst_rtsp_stream_set_seqnum:
2743 * @stream: a #GstRTSPStream
2744 * @seqnum: a new sequence number
2746 * Configure the sequence number in the payloader of @stream to @seqnum.
2749 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
2751 GstRTSPStreamPrivate *priv;
2753 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
2755 priv = stream->priv;
2757 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
2761 * gst_rtsp_stream_get_seqnum:
2762 * @stream: a #GstRTSPStream
2764 * Get the configured sequence number in the payloader of @stream.
2766 * Returns: the sequence number of the payloader.
2769 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
2771 GstRTSPStreamPrivate *priv;
2774 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
2776 priv = stream->priv;
2778 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
2784 * gst_rtsp_stream_transport_filter:
2785 * @stream: a #GstRTSPStream
2786 * @func: (scope call) (allow-none): a callback
2787 * @user_data: (closure): user data passed to @func
2789 * Call @func for each transport managed by @stream. The result value of @func
2790 * determines what happens to the transport. @func will be called with @stream
2791 * locked so no further actions on @stream can be performed from @func.
2793 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
2796 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
2798 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
2799 * will also be added with an additional ref to the result #GList of this
2802 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
2804 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
2805 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2806 * element in the #GList should be unreffed before the list is freed.
2809 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
2810 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
2812 GstRTSPStreamPrivate *priv;
2813 GList *result, *walk, *next;
2814 GHashTable *visited = NULL;
2817 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2819 priv = stream->priv;
2823 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
2825 g_mutex_lock (&priv->lock);
2827 cookie = priv->transports_cookie;
2828 for (walk = priv->transports; walk; walk = next) {
2829 GstRTSPStreamTransport *trans = walk->data;
2830 GstRTSPFilterResult res;
2833 next = g_list_next (walk);
2836 /* only visit each transport once */
2837 if (g_hash_table_contains (visited, trans))
2840 g_hash_table_add (visited, g_object_ref (trans));
2841 g_mutex_unlock (&priv->lock);
2843 res = func (stream, trans, user_data);
2845 g_mutex_lock (&priv->lock);
2847 res = GST_RTSP_FILTER_REF;
2849 changed = (cookie != priv->transports_cookie);
2852 case GST_RTSP_FILTER_REMOVE:
2853 update_transport (stream, trans, FALSE);
2855 case GST_RTSP_FILTER_REF:
2856 result = g_list_prepend (result, g_object_ref (trans));
2858 case GST_RTSP_FILTER_KEEP:
2865 g_mutex_unlock (&priv->lock);
2868 g_hash_table_unref (visited);
2873 static GstPadProbeReturn
2874 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2876 GstRTSPStreamPrivate *priv;
2877 GstRTSPStream *stream;
2880 priv = stream->priv;
2882 GST_DEBUG_OBJECT (pad, "now blocking");
2884 g_mutex_lock (&priv->lock);
2885 priv->blocking = TRUE;
2886 g_mutex_unlock (&priv->lock);
2888 gst_element_post_message (priv->payloader,
2889 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
2890 gst_structure_new_empty ("GstRTSPStreamBlocking")));
2892 return GST_PAD_PROBE_OK;
2896 * gst_rtsp_stream_set_blocked:
2897 * @stream: a #GstRTSPStream
2898 * @blocked: boolean indicating we should block or unblock
2900 * Blocks or unblocks the dataflow on @stream.
2902 * Returns: %TRUE on success
2905 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
2907 GstRTSPStreamPrivate *priv;
2909 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2911 priv = stream->priv;
2913 g_mutex_lock (&priv->lock);
2915 priv->blocking = FALSE;
2916 if (priv->blocked_id == 0) {
2917 priv->blocked_id = gst_pad_add_probe (priv->srcpad,
2918 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
2919 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
2920 g_object_ref (stream), g_object_unref);
2923 if (priv->blocked_id != 0) {
2924 gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
2925 priv->blocked_id = 0;
2926 priv->blocking = FALSE;
2929 g_mutex_unlock (&priv->lock);
2935 * gst_rtsp_stream_is_blocking:
2936 * @stream: a #GstRTSPStream
2938 * Check if @stream is blocking on a #GstBuffer.
2940 * Returns: %TRUE if @stream is blocking
2943 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
2945 GstRTSPStreamPrivate *priv;
2948 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2950 priv = stream->priv;
2952 g_mutex_lock (&priv->lock);
2953 result = priv->blocking;
2954 g_mutex_unlock (&priv->lock);
2960 * gst_rtsp_stream_query_position:
2961 * @stream: a #GstRTSPStream
2963 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
2964 * the RTP parts of the pipeline and not the RTCP parts.
2966 * Returns: %TRUE if the position could be queried
2969 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
2971 GstRTSPStreamPrivate *priv;
2975 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2977 priv = stream->priv;
2979 g_mutex_lock (&priv->lock);
2980 if ((sink = priv->udpsink[0]))
2981 gst_object_ref (sink);
2982 g_mutex_unlock (&priv->lock);
2987 ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
2988 gst_object_unref (sink);
2994 * gst_rtsp_stream_query_stop:
2995 * @stream: a #GstRTSPStream
2997 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
2998 * the RTP parts of the pipeline and not the RTCP parts.
3000 * Returns: %TRUE if the stop could be queried
3003 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3005 GstRTSPStreamPrivate *priv;
3010 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3012 priv = stream->priv;
3014 g_mutex_lock (&priv->lock);
3015 if ((sink = priv->udpsink[0]))
3016 gst_object_ref (sink);
3017 g_mutex_unlock (&priv->lock);
3022 query = gst_query_new_segment (GST_FORMAT_TIME);
3023 if ((ret = gst_element_query (sink, query))) {
3026 gst_query_parse_segment (query, NULL, &format, NULL, stop);
3027 if (format != GST_FORMAT_TIME)
3030 gst_query_unref (query);
3031 gst_object_unref (sink);