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 * Each #GstRTSPStreamTransport spawns one queue that will serve as a backlog of a
47 * controllable maximum size when the reflux from the TCP connection's backpressure
48 * starts spilling all over.
50 * Unlike the backlog in rtspconnection, which we have decided should only contain
51 * at most one RTP and one RTCP data message in order to allow control messages to
52 * go through unobstructed, this backlog only consists of data messages, allowing
53 * us to fill it up without concern.
55 * When multiple TCP transports exist, for example in the context of a shared media,
56 * we only pop samples from our appsinks when at least one of the transports doesn't
57 * experience back pressure: this allows us to pace our sample popping to the speed
58 * of the fastest client.
60 * When a sample is popped, it is either sent directly on transports that don't
61 * experience backpressure, or queued on the transport's backlog otherwise. Samples
62 * are then popped from that backlog when the transport reports it has sent the message.
64 * Once the backlog reaches an overly large duration, the transport is dropped as
65 * the client was deemed too slow.
77 #include <gst/app/gstappsrc.h>
78 #include <gst/app/gstappsink.h>
80 #include <gst/rtp/gstrtpbuffer.h>
82 #include "rtsp-stream.h"
83 #include "rtsp-server-internal.h"
85 struct _GstRTSPStreamPrivate
89 /* Only one pad is ever set */
90 GstPad *srcpad, *sinkpad;
91 GstElement *payloader;
95 /* TRUE if this stream is running on
96 * the client side of an RTSP link (for RECORD) */
100 /* TRUE if stream is complete. This means that the receiver and the sender
101 * parts are present in the stream. */
102 gboolean is_complete;
103 GstRTSPProfile profiles;
104 GstRTSPLowerTrans allowed_protocols;
105 GstRTSPLowerTrans configured_protocols;
107 /* pads on the rtpbin */
108 GstPad *send_rtp_sink;
109 GstPad *recv_rtp_src;
110 GstPad *recv_sink[2];
113 /* the RTPSession object */
116 /* SRTP encoder/decoder */
121 /* for UDP unicast */
122 GstElement *udpsrc_v4[2];
123 GstElement *udpsrc_v6[2];
124 GstElement *udpqueue[2];
125 GstElement *udpsink[2];
126 GSocket *socket_v4[2];
127 GSocket *socket_v6[2];
129 /* for UDP multicast */
130 GstElement *mcast_udpsrc_v4[2];
131 GstElement *mcast_udpsrc_v6[2];
132 GstElement *mcast_udpqueue[2];
133 GstElement *mcast_udpsink[2];
134 GSocket *mcast_socket_v4[2];
135 GSocket *mcast_socket_v6[2];
136 GList *mcast_clients;
138 /* for TCP transport */
139 GstElement *appsrc[2];
140 GstClockTime appsrc_base_time[2];
141 GstElement *appqueue[2];
142 GstElement *appsink[2];
145 GstElement *funnel[2];
149 GstElement *rtxreceive;
151 GstClockTime rtx_time;
154 gboolean do_rate_control;
156 /* Forward Error Correction with RFC 5109 */
157 GstElement *ulpfec_decoder;
158 GstElement *ulpfec_encoder;
160 gboolean ulpfec_enabled;
161 guint ulpfec_percentage;
163 /* pool used to manage unicast and multicast addresses */
164 GstRTSPAddressPool *pool;
166 /* unicast server addr/port */
167 GstRTSPAddress *server_addr_v4;
168 GstRTSPAddress *server_addr_v6;
170 /* multicast addresses */
171 GstRTSPAddress *mcast_addr_v4;
172 GstRTSPAddress *mcast_addr_v6;
174 gchar *multicast_iface;
176 gboolean bind_mcast_address;
178 /* the caps of the stream */
182 /* transports we stream to */
185 guint transports_cookie;
187 guint tr_cache_cookie;
188 guint n_tcp_transports;
189 gboolean have_buffer[2];
193 /* Sending logic for TCP */
194 GThread *send_thread;
197 /* @send_lock is released when pushing data out, we use
198 * a cookie to decide whether we should wait on @send_cond
199 * before checking the transports' backlogs again
202 /* Used to control shutdown of @send_thread */
203 gboolean continue_sending;
205 /* stream blocking */
206 gulong blocked_id[2];
209 /* current stream postion */
210 GstClockTime position;
212 /* pt->caps map for RECORD streams */
215 GstRTSPPublishClockMode publish_clock_mode;
216 GThreadPool *send_pool;
218 /* Used to provide accurate rtpinfo when the stream is blocking */
219 gboolean blocked_buffer;
220 guint32 blocked_seqnum;
221 guint32 blocked_rtptime;
222 GstClockTime blocked_running_time;
223 gint blocked_clock_rate;
225 /* Whether we should send and receive RTCP */
226 gboolean enable_rtcp;
228 /* blocking early rtcp packets */
229 GstPad *block_early_rtcp_pad;
230 gulong block_early_rtcp_probe;
231 GstPad *block_early_rtcp_pad_ipv6;
232 gulong block_early_rtcp_probe_ipv6;
235 #define DEFAULT_CONTROL NULL
236 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
237 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
238 GST_RTSP_LOWER_TRANS_TCP
239 #define DEFAULT_MAX_MCAST_TTL 255
240 #define DEFAULT_BIND_MCAST_ADDRESS FALSE
241 #define DEFAULT_DO_RATE_CONTROL TRUE
242 #define DEFAULT_ENABLE_RTCP TRUE
255 SIGNAL_NEW_RTP_ENCODER,
256 SIGNAL_NEW_RTCP_ENCODER,
257 SIGNAL_NEW_RTP_RTCP_DECODER,
261 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
262 #define GST_CAT_DEFAULT rtsp_stream_debug
264 static GQuark ssrc_stream_map_key;
266 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
267 GValue * value, GParamSpec * pspec);
268 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
269 const GValue * value, GParamSpec * pspec);
271 static void gst_rtsp_stream_finalize (GObject * obj);
274 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
277 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
279 G_DEFINE_TYPE_WITH_PRIVATE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
282 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
284 GObjectClass *gobject_class;
286 gobject_class = G_OBJECT_CLASS (klass);
288 gobject_class->get_property = gst_rtsp_stream_get_property;
289 gobject_class->set_property = gst_rtsp_stream_set_property;
290 gobject_class->finalize = gst_rtsp_stream_finalize;
292 g_object_class_install_property (gobject_class, PROP_CONTROL,
293 g_param_spec_string ("control", "Control",
294 "The control string for this stream", DEFAULT_CONTROL,
295 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
297 g_object_class_install_property (gobject_class, PROP_PROFILES,
298 g_param_spec_flags ("profiles", "Profiles",
299 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
300 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
302 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
303 g_param_spec_flags ("protocols", "Protocols",
304 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
305 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
308 g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
309 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
311 gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
312 g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
313 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
315 gst_rtsp_stream_signals[SIGNAL_NEW_RTP_RTCP_DECODER] =
316 g_signal_new ("new-rtp-rtcp-decoder", G_TYPE_FROM_CLASS (klass),
317 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
319 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
321 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
325 gst_rtsp_stream_init (GstRTSPStream * stream)
327 GstRTSPStreamPrivate *priv = gst_rtsp_stream_get_instance_private (stream);
329 GST_DEBUG ("new stream %p", stream);
334 priv->control = g_strdup (DEFAULT_CONTROL);
335 priv->profiles = DEFAULT_PROFILES;
336 priv->allowed_protocols = DEFAULT_PROTOCOLS;
337 priv->configured_protocols = 0;
338 priv->publish_clock_mode = GST_RTSP_PUBLISH_CLOCK_MODE_CLOCK;
339 priv->max_mcast_ttl = DEFAULT_MAX_MCAST_TTL;
340 priv->bind_mcast_address = DEFAULT_BIND_MCAST_ADDRESS;
341 priv->do_rate_control = DEFAULT_DO_RATE_CONTROL;
342 priv->enable_rtcp = DEFAULT_ENABLE_RTCP;
344 g_mutex_init (&priv->lock);
346 priv->continue_sending = TRUE;
347 priv->send_cookie = 0;
348 g_cond_init (&priv->send_cond);
349 g_mutex_init (&priv->send_lock);
351 priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
352 NULL, (GDestroyNotify) gst_caps_unref);
353 priv->ptmap = g_hash_table_new_full (NULL, NULL, NULL,
354 (GDestroyNotify) gst_caps_unref);
355 priv->send_pool = NULL;
356 priv->block_early_rtcp_pad = NULL;
357 priv->block_early_rtcp_probe = 0;
358 priv->block_early_rtcp_pad_ipv6 = NULL;
359 priv->block_early_rtcp_probe_ipv6 = 0;
362 typedef struct _UdpClientAddrInfo UdpClientAddrInfo;
364 struct _UdpClientAddrInfo
368 guint add_count; /* how often this address has been added */
372 free_mcast_client (gpointer data)
374 UdpClientAddrInfo *client = data;
376 g_free (client->address);
381 gst_rtsp_stream_finalize (GObject * obj)
383 GstRTSPStream *stream;
384 GstRTSPStreamPrivate *priv;
387 stream = GST_RTSP_STREAM (obj);
390 GST_DEBUG ("finalize stream %p", stream);
392 /* we really need to be unjoined now */
393 g_return_if_fail (priv->joined_bin == NULL);
396 g_thread_pool_free (priv->send_pool, TRUE, TRUE);
397 if (priv->mcast_addr_v4)
398 gst_rtsp_address_free (priv->mcast_addr_v4);
399 if (priv->mcast_addr_v6)
400 gst_rtsp_address_free (priv->mcast_addr_v6);
401 if (priv->server_addr_v4)
402 gst_rtsp_address_free (priv->server_addr_v4);
403 if (priv->server_addr_v6)
404 gst_rtsp_address_free (priv->server_addr_v6);
406 g_object_unref (priv->pool);
408 g_object_unref (priv->rtxsend);
409 if (priv->rtxreceive)
410 g_object_unref (priv->rtxreceive);
411 if (priv->ulpfec_encoder)
412 gst_object_unref (priv->ulpfec_encoder);
413 if (priv->ulpfec_decoder)
414 gst_object_unref (priv->ulpfec_decoder);
416 for (i = 0; i < 2; i++) {
417 if (priv->socket_v4[i])
418 g_object_unref (priv->socket_v4[i]);
419 if (priv->socket_v6[i])
420 g_object_unref (priv->socket_v6[i]);
421 if (priv->mcast_socket_v4[i])
422 g_object_unref (priv->mcast_socket_v4[i]);
423 if (priv->mcast_socket_v6[i])
424 g_object_unref (priv->mcast_socket_v6[i]);
427 g_free (priv->multicast_iface);
428 g_list_free_full (priv->mcast_clients, (GDestroyNotify) free_mcast_client);
430 gst_object_unref (priv->payloader);
432 gst_object_unref (priv->srcpad);
434 gst_object_unref (priv->sinkpad);
435 g_free (priv->control);
436 g_mutex_clear (&priv->lock);
438 g_hash_table_unref (priv->keys);
439 g_hash_table_destroy (priv->ptmap);
441 g_mutex_clear (&priv->send_lock);
442 g_cond_clear (&priv->send_cond);
444 if (priv->block_early_rtcp_probe != 0) {
446 (priv->block_early_rtcp_pad, priv->block_early_rtcp_probe);
447 gst_object_unref (priv->block_early_rtcp_pad);
450 if (priv->block_early_rtcp_probe_ipv6 != 0) {
452 (priv->block_early_rtcp_pad_ipv6, priv->block_early_rtcp_probe_ipv6);
453 gst_object_unref (priv->block_early_rtcp_pad_ipv6);
456 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
460 gst_rtsp_stream_get_property (GObject * object, guint propid,
461 GValue * value, GParamSpec * pspec)
463 GstRTSPStream *stream = GST_RTSP_STREAM (object);
467 g_value_take_string (value, gst_rtsp_stream_get_control (stream));
470 g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
473 g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
476 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
481 gst_rtsp_stream_set_property (GObject * object, guint propid,
482 const GValue * value, GParamSpec * pspec)
484 GstRTSPStream *stream = GST_RTSP_STREAM (object);
488 gst_rtsp_stream_set_control (stream, g_value_get_string (value));
491 gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
494 gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
497 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
502 * gst_rtsp_stream_new:
505 * @payloader: a #GstElement
507 * Create a new media stream with index @idx that handles RTP data on
508 * @pad and has a payloader element @payloader if @pad is a source pad
509 * or a depayloader element @payloader if @pad is a sink pad.
511 * Returns: (transfer full): a new #GstRTSPStream
514 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * pad)
516 GstRTSPStreamPrivate *priv;
517 GstRTSPStream *stream;
519 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
520 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
522 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
525 priv->payloader = gst_object_ref (payloader);
526 if (GST_PAD_IS_SRC (pad))
527 priv->srcpad = gst_object_ref (pad);
529 priv->sinkpad = gst_object_ref (pad);
535 * gst_rtsp_stream_get_index:
536 * @stream: a #GstRTSPStream
538 * Get the stream index.
540 * Return: the stream index.
543 gst_rtsp_stream_get_index (GstRTSPStream * stream)
545 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
547 return stream->priv->idx;
551 * gst_rtsp_stream_get_pt:
552 * @stream: a #GstRTSPStream
554 * Get the stream payload type.
556 * Return: the stream payload type.
559 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
561 GstRTSPStreamPrivate *priv;
564 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
568 g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
574 * gst_rtsp_stream_get_srcpad:
575 * @stream: a #GstRTSPStream
577 * Get the srcpad associated with @stream.
579 * Returns: (transfer full) (nullable): the srcpad. Unref after usage.
582 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
584 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
586 if (!stream->priv->srcpad)
589 return gst_object_ref (stream->priv->srcpad);
593 * gst_rtsp_stream_get_sinkpad:
594 * @stream: a #GstRTSPStream
596 * Get the sinkpad associated with @stream.
598 * Returns: (transfer full) (nullable): the sinkpad. Unref after usage.
601 gst_rtsp_stream_get_sinkpad (GstRTSPStream * stream)
603 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
605 if (!stream->priv->sinkpad)
608 return gst_object_ref (stream->priv->sinkpad);
612 * gst_rtsp_stream_get_control:
613 * @stream: a #GstRTSPStream
615 * Get the control string to identify this stream.
617 * Returns: (transfer full) (nullable): the control string. g_free() after usage.
620 gst_rtsp_stream_get_control (GstRTSPStream * stream)
622 GstRTSPStreamPrivate *priv;
625 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
629 g_mutex_lock (&priv->lock);
630 if ((result = g_strdup (priv->control)) == NULL)
631 result = g_strdup_printf ("stream=%u", priv->idx);
632 g_mutex_unlock (&priv->lock);
638 * gst_rtsp_stream_set_control:
639 * @stream: a #GstRTSPStream
640 * @control: (nullable): a control string
642 * Set the control string in @stream.
645 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
647 GstRTSPStreamPrivate *priv;
649 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
653 g_mutex_lock (&priv->lock);
654 g_free (priv->control);
655 priv->control = g_strdup (control);
656 g_mutex_unlock (&priv->lock);
660 * gst_rtsp_stream_has_control:
661 * @stream: a #GstRTSPStream
662 * @control: (nullable): a control string
664 * Check if @stream has the control string @control.
666 * Returns: %TRUE is @stream has @control as the control string
669 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
671 GstRTSPStreamPrivate *priv;
674 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
678 g_mutex_lock (&priv->lock);
680 res = (g_strcmp0 (priv->control, control) == 0);
684 if (sscanf (control, "stream=%u", &streamid) > 0)
685 res = (streamid == priv->idx);
689 g_mutex_unlock (&priv->lock);
695 * gst_rtsp_stream_set_mtu:
696 * @stream: a #GstRTSPStream
699 * Configure the mtu in the payloader of @stream to @mtu.
702 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
704 GstRTSPStreamPrivate *priv;
706 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
710 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
712 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
716 * gst_rtsp_stream_get_mtu:
717 * @stream: a #GstRTSPStream
719 * Get the configured MTU in the payloader of @stream.
721 * Returns: the MTU of the payloader.
724 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
726 GstRTSPStreamPrivate *priv;
729 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
733 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
738 /* Update the dscp qos property on the udp sinks */
740 update_dscp_qos (GstRTSPStream * stream, GstElement ** udpsink)
742 GstRTSPStreamPrivate *priv;
747 g_object_set (G_OBJECT (*udpsink), "qos-dscp", priv->dscp_qos, NULL);
752 * gst_rtsp_stream_set_dscp_qos:
753 * @stream: a #GstRTSPStream
754 * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
756 * Configure the dscp qos of the outgoing sockets to @dscp_qos.
759 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
761 GstRTSPStreamPrivate *priv;
763 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
767 GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
769 if (dscp_qos < -1 || dscp_qos > 63) {
770 GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
774 priv->dscp_qos = dscp_qos;
776 update_dscp_qos (stream, priv->udpsink);
780 * gst_rtsp_stream_get_dscp_qos:
781 * @stream: a #GstRTSPStream
783 * Get the configured DSCP QoS in of the outgoing sockets.
785 * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
788 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
790 GstRTSPStreamPrivate *priv;
792 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
796 return priv->dscp_qos;
800 * gst_rtsp_stream_is_transport_supported:
801 * @stream: a #GstRTSPStream
802 * @transport: (transfer none): a #GstRTSPTransport
804 * Check if @transport can be handled by stream
806 * Returns: %TRUE if @transport can be handled by @stream.
809 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
810 GstRTSPTransport * transport)
812 GstRTSPStreamPrivate *priv;
814 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
815 g_return_val_if_fail (transport != NULL, FALSE);
819 g_mutex_lock (&priv->lock);
820 if (transport->trans != GST_RTSP_TRANS_RTP)
821 goto unsupported_transmode;
823 if (!(transport->profile & priv->profiles))
824 goto unsupported_profile;
826 if (!(transport->lower_transport & priv->allowed_protocols))
827 goto unsupported_ltrans;
829 g_mutex_unlock (&priv->lock);
834 unsupported_transmode:
836 GST_DEBUG ("unsupported transport mode %d", transport->trans);
837 g_mutex_unlock (&priv->lock);
842 GST_DEBUG ("unsupported profile %d", transport->profile);
843 g_mutex_unlock (&priv->lock);
848 GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
849 g_mutex_unlock (&priv->lock);
855 * gst_rtsp_stream_set_profiles:
856 * @stream: a #GstRTSPStream
857 * @profiles: the new profiles
859 * Configure the allowed profiles for @stream.
862 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
864 GstRTSPStreamPrivate *priv;
866 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
870 g_mutex_lock (&priv->lock);
871 priv->profiles = profiles;
872 g_mutex_unlock (&priv->lock);
876 * gst_rtsp_stream_get_profiles:
877 * @stream: a #GstRTSPStream
879 * Get the allowed profiles of @stream.
881 * Returns: a #GstRTSPProfile
884 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
886 GstRTSPStreamPrivate *priv;
889 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
893 g_mutex_lock (&priv->lock);
894 res = priv->profiles;
895 g_mutex_unlock (&priv->lock);
901 * gst_rtsp_stream_set_protocols:
902 * @stream: a #GstRTSPStream
903 * @protocols: the new flags
905 * Configure the allowed lower transport for @stream.
908 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
909 GstRTSPLowerTrans protocols)
911 GstRTSPStreamPrivate *priv;
913 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
917 g_mutex_lock (&priv->lock);
918 priv->allowed_protocols = protocols;
919 g_mutex_unlock (&priv->lock);
923 * gst_rtsp_stream_get_protocols:
924 * @stream: a #GstRTSPStream
926 * Get the allowed protocols of @stream.
928 * Returns: a #GstRTSPLowerTrans
931 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
933 GstRTSPStreamPrivate *priv;
934 GstRTSPLowerTrans res;
936 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
937 GST_RTSP_LOWER_TRANS_UNKNOWN);
941 g_mutex_lock (&priv->lock);
942 res = priv->allowed_protocols;
943 g_mutex_unlock (&priv->lock);
949 * gst_rtsp_stream_set_address_pool:
950 * @stream: a #GstRTSPStream
951 * @pool: (transfer none) (nullable): a #GstRTSPAddressPool
953 * configure @pool to be used as the address pool of @stream.
956 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
957 GstRTSPAddressPool * pool)
959 GstRTSPStreamPrivate *priv;
960 GstRTSPAddressPool *old;
962 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
966 GST_LOG_OBJECT (stream, "set address pool %p", pool);
968 g_mutex_lock (&priv->lock);
969 if ((old = priv->pool) != pool)
970 priv->pool = pool ? g_object_ref (pool) : NULL;
973 g_mutex_unlock (&priv->lock);
976 g_object_unref (old);
980 * gst_rtsp_stream_get_address_pool:
981 * @stream: a #GstRTSPStream
983 * Get the #GstRTSPAddressPool used as the address pool of @stream.
985 * Returns: (transfer full) (nullable): the #GstRTSPAddressPool of @stream.
986 * g_object_unref() after usage.
989 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
991 GstRTSPStreamPrivate *priv;
992 GstRTSPAddressPool *result;
994 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
998 g_mutex_lock (&priv->lock);
999 if ((result = priv->pool))
1000 g_object_ref (result);
1001 g_mutex_unlock (&priv->lock);
1007 * gst_rtsp_stream_set_multicast_iface:
1008 * @stream: a #GstRTSPStream
1009 * @multicast_iface: (transfer none) (nullable): a multicast interface name
1011 * configure @multicast_iface to be used for @stream.
1014 gst_rtsp_stream_set_multicast_iface (GstRTSPStream * stream,
1015 const gchar * multicast_iface)
1017 GstRTSPStreamPrivate *priv;
1020 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1022 priv = stream->priv;
1024 GST_LOG_OBJECT (stream, "set multicast iface %s",
1025 GST_STR_NULL (multicast_iface));
1027 g_mutex_lock (&priv->lock);
1028 if ((old = priv->multicast_iface) != multicast_iface)
1029 priv->multicast_iface = multicast_iface ? g_strdup (multicast_iface) : NULL;
1032 g_mutex_unlock (&priv->lock);
1039 * gst_rtsp_stream_get_multicast_iface:
1040 * @stream: a #GstRTSPStream
1042 * Get the multicast interface used for @stream.
1044 * Returns: (transfer full) (nullable): the multicast interface for @stream.
1045 * g_free() after usage.
1048 gst_rtsp_stream_get_multicast_iface (GstRTSPStream * stream)
1050 GstRTSPStreamPrivate *priv;
1053 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1055 priv = stream->priv;
1057 g_mutex_lock (&priv->lock);
1058 if ((result = priv->multicast_iface))
1059 result = g_strdup (result);
1060 g_mutex_unlock (&priv->lock);
1066 * gst_rtsp_stream_get_multicast_address:
1067 * @stream: a #GstRTSPStream
1068 * @family: the #GSocketFamily
1070 * Get the multicast address of @stream for @family. The original
1071 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
1072 * won't release the address from the pool.
1074 * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
1075 * or %NULL when no address could be allocated. gst_rtsp_address_free()
1079 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
1080 GSocketFamily family)
1082 GstRTSPStreamPrivate *priv;
1083 GstRTSPAddress *result;
1084 GstRTSPAddress **addrp;
1085 GstRTSPAddressFlags flags;
1087 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1089 priv = stream->priv;
1091 g_mutex_lock (&stream->priv->lock);
1093 if (family == G_SOCKET_FAMILY_IPV6) {
1094 flags = GST_RTSP_ADDRESS_FLAG_IPV6;
1095 addrp = &priv->mcast_addr_v6;
1097 flags = GST_RTSP_ADDRESS_FLAG_IPV4;
1098 addrp = &priv->mcast_addr_v4;
1101 if (*addrp == NULL) {
1102 if (priv->pool == NULL)
1105 flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
1107 *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
1111 /* FIXME: Also reserve the same port with unicast ANY address, since that's
1112 * where we are going to bind our socket. Probably loop until we find a port
1113 * available in both mcast and unicast pools. Maybe GstRTSPAddressPool
1114 * should do it for us when both GST_RTSP_ADDRESS_FLAG_MULTICAST and
1115 * GST_RTSP_ADDRESS_FLAG_UNICAST are givent. */
1117 result = gst_rtsp_address_copy (*addrp);
1119 g_mutex_unlock (&stream->priv->lock);
1126 GST_ERROR_OBJECT (stream, "no address pool specified");
1127 g_mutex_unlock (&stream->priv->lock);
1132 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
1133 g_mutex_unlock (&stream->priv->lock);
1139 * gst_rtsp_stream_reserve_address:
1140 * @stream: a #GstRTSPStream
1141 * @address: an address
1146 * Reserve @address and @port as the address and port of @stream. The original
1147 * #GstRTSPAddress is cached and copy is returned, so freeing the return value
1148 * won't release the address from the pool.
1150 * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
1151 * the address could not be reserved. gst_rtsp_address_free() after
1155 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
1156 const gchar * address, guint port, guint n_ports, guint ttl)
1158 GstRTSPStreamPrivate *priv;
1159 GstRTSPAddress *result;
1161 GSocketFamily family;
1162 GstRTSPAddress **addrp;
1164 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1165 g_return_val_if_fail (address != NULL, NULL);
1166 g_return_val_if_fail (port > 0, NULL);
1167 g_return_val_if_fail (n_ports > 0, NULL);
1168 g_return_val_if_fail (ttl > 0, NULL);
1170 priv = stream->priv;
1172 addr = g_inet_address_new_from_string (address);
1174 GST_ERROR ("failed to get inet addr from %s", address);
1175 family = G_SOCKET_FAMILY_IPV4;
1177 family = g_inet_address_get_family (addr);
1178 g_object_unref (addr);
1181 if (family == G_SOCKET_FAMILY_IPV6)
1182 addrp = &priv->mcast_addr_v6;
1184 addrp = &priv->mcast_addr_v4;
1186 g_mutex_lock (&priv->lock);
1187 if (*addrp == NULL) {
1188 GstRTSPAddressPoolResult res;
1190 if (priv->pool == NULL)
1193 res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
1194 port, n_ports, ttl, addrp);
1195 if (res != GST_RTSP_ADDRESS_POOL_OK)
1198 /* FIXME: Also reserve the same port with unicast ANY address, since that's
1199 * where we are going to bind our socket. */
1201 if (g_ascii_strcasecmp ((*addrp)->address, address) ||
1202 (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
1203 (*addrp)->ttl != ttl)
1204 goto different_address;
1206 result = gst_rtsp_address_copy (*addrp);
1207 g_mutex_unlock (&priv->lock);
1214 GST_ERROR_OBJECT (stream, "no address pool specified");
1215 g_mutex_unlock (&priv->lock);
1220 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
1222 g_mutex_unlock (&priv->lock);
1227 GST_ERROR_OBJECT (stream,
1228 "address %s is not the same as %s that was already reserved",
1229 address, (*addrp)->address);
1230 g_mutex_unlock (&priv->lock);
1235 /* must be called with lock */
1237 set_socket_for_udpsink (GstElement * udpsink, GSocket * socket,
1238 GSocketFamily family)
1240 const gchar *multisink_socket;
1242 if (family == G_SOCKET_FAMILY_IPV6)
1243 multisink_socket = "socket-v6";
1245 multisink_socket = "socket";
1247 g_object_set (G_OBJECT (udpsink), multisink_socket, socket, NULL);
1250 /* must be called with lock */
1252 set_multicast_socket_for_udpsink (GstElement * udpsink, GSocket * socket,
1253 GSocketFamily family, const gchar * multicast_iface,
1254 const gchar * addr_str, gint port, gint mcast_ttl)
1256 set_socket_for_udpsink (udpsink, socket, family);
1258 if (multicast_iface) {
1259 GST_INFO ("setting multicast-iface %s", multicast_iface);
1260 g_object_set (G_OBJECT (udpsink), "multicast-iface", multicast_iface, NULL);
1263 if (mcast_ttl > 0) {
1264 GST_INFO ("setting ttl-mc %d", mcast_ttl);
1265 g_object_set (G_OBJECT (udpsink), "ttl-mc", mcast_ttl, NULL);
1270 /* must be called with lock */
1272 set_unicast_socket_for_udpsink (GstElement * udpsink, GSocket * socket,
1273 GSocketFamily family)
1275 set_socket_for_udpsink (udpsink, socket, family);
1279 get_port_from_socket (GSocket * socket)
1282 GSocketAddress *sockaddr;
1285 GST_DEBUG ("socket: %p", socket);
1286 sockaddr = g_socket_get_local_address (socket, &err);
1287 if (sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (sockaddr)) {
1288 g_clear_object (&sockaddr);
1289 GST_ERROR ("failed to get sockaddr: %s", err->message);
1294 port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (sockaddr));
1295 g_object_unref (sockaddr);
1302 create_and_configure_udpsink (GstRTSPStream * stream, GstElement ** udpsink,
1303 GSocket * socket_v4, GSocket * socket_v6, gboolean multicast,
1304 gboolean is_rtp, gint mcast_ttl)
1306 GstRTSPStreamPrivate *priv = stream->priv;
1308 *udpsink = gst_element_factory_make ("multiudpsink", NULL);
1311 goto no_udp_protocol;
1313 /* configure sinks */
1315 g_object_set (G_OBJECT (*udpsink), "close-socket", FALSE, NULL);
1317 g_object_set (G_OBJECT (*udpsink), "send-duplicates", FALSE, NULL);
1320 g_object_set (G_OBJECT (*udpsink), "buffer-size", priv->buffer_size, NULL);
1322 g_object_set (G_OBJECT (*udpsink), "sync", FALSE, NULL);
1324 /* Needs to be async for RECORD streams, otherwise we will never go to
1325 * PLAYING because the sinks will wait for data while the udpsrc can't
1326 * provide data with timestamps in PAUSED. */
1327 if (!is_rtp || priv->sinkpad)
1328 g_object_set (G_OBJECT (*udpsink), "async", FALSE, NULL);
1331 /* join multicast group when adding clients, so we'll start receiving from it.
1332 * We cannot rely on the udpsrc to join the group since its socket is always a
1333 * local unicast one. */
1334 g_object_set (G_OBJECT (*udpsink), "auto-multicast", TRUE, NULL);
1336 g_object_set (G_OBJECT (*udpsink), "loop", FALSE, NULL);
1339 /* update the dscp qos field in the sinks */
1340 update_dscp_qos (stream, udpsink);
1342 if (priv->server_addr_v4) {
1343 GST_DEBUG_OBJECT (stream, "udp IPv4, configure udpsinks");
1344 set_unicast_socket_for_udpsink (*udpsink, socket_v4, G_SOCKET_FAMILY_IPV4);
1347 if (priv->server_addr_v6) {
1348 GST_DEBUG_OBJECT (stream, "udp IPv6, configure udpsinks");
1349 set_unicast_socket_for_udpsink (*udpsink, socket_v6, G_SOCKET_FAMILY_IPV6);
1354 if (priv->mcast_addr_v4) {
1355 GST_DEBUG_OBJECT (stream, "mcast IPv4, configure udpsinks");
1356 port = get_port_from_socket (socket_v4);
1358 goto get_port_failed;
1359 set_multicast_socket_for_udpsink (*udpsink, socket_v4,
1360 G_SOCKET_FAMILY_IPV4, priv->multicast_iface,
1361 priv->mcast_addr_v4->address, port, mcast_ttl);
1364 if (priv->mcast_addr_v6) {
1365 GST_DEBUG_OBJECT (stream, "mcast IPv6, configure udpsinks");
1366 port = get_port_from_socket (socket_v6);
1368 goto get_port_failed;
1369 set_multicast_socket_for_udpsink (*udpsink, socket_v6,
1370 G_SOCKET_FAMILY_IPV6, priv->multicast_iface,
1371 priv->mcast_addr_v6->address, port, mcast_ttl);
1381 GST_ERROR_OBJECT (stream, "failed to create udpsink element");
1386 GST_ERROR_OBJECT (stream, "failed to get udp port");
1391 /* must be called with lock */
1393 create_and_configure_udpsource (GstElement ** udpsrc, GSocket * socket)
1395 GstStateChangeReturn ret;
1397 g_assert (socket != NULL);
1399 *udpsrc = gst_element_factory_make ("udpsrc", NULL);
1400 if (*udpsrc == NULL)
1403 g_object_set (G_OBJECT (*udpsrc), "socket", socket, NULL);
1405 /* The udpsrc cannot do the join because its socket is always a local unicast
1406 * one. The udpsink sharing the same socket will do it for us. */
1407 g_object_set (G_OBJECT (*udpsrc), "auto-multicast", FALSE, NULL);
1409 g_object_set (G_OBJECT (*udpsrc), "loop", FALSE, NULL);
1411 g_object_set (G_OBJECT (*udpsrc), "close-socket", FALSE, NULL);
1413 ret = gst_element_set_state (*udpsrc, GST_STATE_READY);
1414 if (ret == GST_STATE_CHANGE_FAILURE)
1423 gst_element_set_state (*udpsrc, GST_STATE_NULL);
1424 g_clear_object (udpsrc);
1431 alloc_ports_one_family (GstRTSPStream * stream, GSocketFamily family,
1432 GSocket * socket_out[2], GstRTSPAddress ** server_addr_out,
1433 gboolean multicast, GstRTSPTransport * ct, gboolean use_transport_settings)
1435 GstRTSPStreamPrivate *priv = stream->priv;
1436 GSocket *rtp_socket = NULL;
1437 GSocket *rtcp_socket = NULL;
1438 gint tmp_rtp, tmp_rtcp;
1440 GList *rejected_addresses = NULL;
1441 GstRTSPAddress *addr = NULL;
1442 GInetAddress *inetaddr = NULL;
1443 GSocketAddress *rtp_sockaddr = NULL;
1444 GSocketAddress *rtcp_sockaddr = NULL;
1445 GstRTSPAddressPool *pool;
1446 gboolean transport_settings_defined = FALSE;
1451 /* Start with random port */
1455 if (use_transport_settings) {
1462 /* multicast and transport specific case */
1463 if (ct->destination != NULL) {
1464 tmp_rtp = ct->port.min;
1465 tmp_rtcp = ct->port.max;
1467 /* check if the provided address is a multicast address */
1468 inetaddr = g_inet_address_new_from_string (ct->destination);
1469 if (inetaddr == NULL)
1470 goto destination_error;
1471 if (!g_inet_address_get_is_multicast (inetaddr))
1472 goto destination_no_mcast;
1475 if (!priv->bind_mcast_address) {
1476 g_clear_object (&inetaddr);
1477 inetaddr = g_inet_address_new_any (family);
1480 GST_DEBUG_OBJECT (stream, "use transport settings");
1481 transport_settings_defined = TRUE;
1485 if (priv->enable_rtcp) {
1486 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1487 G_SOCKET_PROTOCOL_UDP, NULL);
1489 goto no_udp_protocol;
1490 g_socket_set_multicast_loopback (rtcp_socket, FALSE);
1493 /* try to allocate UDP ports, the RTP port should be an even
1494 * number and the RTCP port (if enabled) should be the next (uneven) port */
1497 if (rtp_socket == NULL) {
1498 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1499 G_SOCKET_PROTOCOL_UDP, NULL);
1501 goto no_udp_protocol;
1502 g_socket_set_multicast_loopback (rtp_socket, FALSE);
1505 if (!transport_settings_defined) {
1506 if ((pool && gst_rtsp_address_pool_has_unicast_addresses (pool))
1508 GstRTSPAddressFlags flags;
1511 rejected_addresses = g_list_prepend (rejected_addresses, addr);
1516 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT;
1518 flags |= GST_RTSP_ADDRESS_FLAG_MULTICAST;
1520 flags |= GST_RTSP_ADDRESS_FLAG_UNICAST;
1522 if (family == G_SOCKET_FAMILY_IPV6)
1523 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1525 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1527 if (*server_addr_out)
1528 addr = *server_addr_out;
1530 addr = gst_rtsp_address_pool_acquire_address (pool, flags,
1531 priv->enable_rtcp ? 2 : 1);
1536 tmp_rtp = addr->port;
1538 g_clear_object (&inetaddr);
1539 /* FIXME: Does it really work with the IP_MULTICAST_ALL socket option and
1540 * socket control message set in udpsrc? */
1541 if (priv->bind_mcast_address || !multicast)
1542 inetaddr = g_inet_address_new_from_string (addr->address);
1544 inetaddr = g_inet_address_new_any (family);
1552 if (inetaddr == NULL)
1553 inetaddr = g_inet_address_new_any (family);
1557 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1558 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1559 GST_DEBUG_OBJECT (stream, "rtp bind() failed, will try again");
1560 g_object_unref (rtp_sockaddr);
1561 if (transport_settings_defined)
1562 goto transport_settings_error;
1565 g_object_unref (rtp_sockaddr);
1567 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1568 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1569 g_clear_object (&rtp_sockaddr);
1573 if (!transport_settings_defined) {
1575 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1577 /* check if port is even. RFC 3550 encorages the use of an even/odd port
1578 * pair, however it's not a strict requirement so this check is not done
1579 * for the client selected ports. */
1580 if ((tmp_rtp & 1) != 0) {
1581 /* port not even, close and allocate another */
1583 g_object_unref (rtp_sockaddr);
1584 g_clear_object (&rtp_socket);
1588 g_object_unref (rtp_sockaddr);
1591 if (priv->enable_rtcp) {
1592 tmp_rtcp = tmp_rtp + 1;
1594 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1595 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1596 GST_DEBUG_OBJECT (stream, "rctp bind() failed, will try again");
1597 g_object_unref (rtcp_sockaddr);
1598 g_clear_object (&rtp_socket);
1599 if (transport_settings_defined)
1600 goto transport_settings_error;
1603 g_object_unref (rtcp_sockaddr);
1607 addr = g_slice_new0 (GstRTSPAddress);
1608 addr->port = tmp_rtp;
1610 if (transport_settings_defined)
1611 addr->address = g_strdup (ct->destination);
1613 addr->address = g_inet_address_to_string (inetaddr);
1614 addr->ttl = ct->ttl;
1617 g_clear_object (&inetaddr);
1619 if (multicast && (ct->ttl > 0) && (ct->ttl <= priv->max_mcast_ttl)) {
1620 GST_DEBUG ("setting mcast ttl to %d", ct->ttl);
1621 g_socket_set_multicast_ttl (rtp_socket, ct->ttl);
1623 g_socket_set_multicast_ttl (rtcp_socket, ct->ttl);
1626 socket_out[0] = rtp_socket;
1627 socket_out[1] = rtcp_socket;
1628 *server_addr_out = addr;
1630 if (priv->enable_rtcp) {
1631 GST_DEBUG_OBJECT (stream, "allocated address: %s and ports: %d, %d",
1632 addr->address, tmp_rtp, tmp_rtcp);
1634 GST_DEBUG_OBJECT (stream, "allocated address: %s and port: %d",
1635 addr->address, tmp_rtp);
1638 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1645 GST_ERROR_OBJECT (stream, "failed to allocate UDP ports: wrong transport");
1650 GST_ERROR_OBJECT (stream, "failed to allocate UDP ports: no transport");
1655 GST_ERROR_OBJECT (stream,
1656 "failed to allocate UDP ports: destination error");
1659 destination_no_mcast:
1661 GST_ERROR_OBJECT (stream,
1662 "failed to allocate UDP ports: destination not multicast address");
1667 GST_WARNING_OBJECT (stream, "failed to allocate UDP ports: protocol error");
1672 GST_WARNING_OBJECT (stream,
1673 "failed to allocate UDP ports: no address pool specified");
1678 GST_WARNING_OBJECT (stream, "failed to acquire address from pool");
1683 GST_WARNING_OBJECT (stream, "failed to allocate UDP ports: no ports");
1686 transport_settings_error:
1688 GST_ERROR_OBJECT (stream,
1689 "failed to allocate UDP ports with requested transport settings");
1694 GST_WARNING_OBJECT (stream, "failed to allocate UDP ports: socket error");
1700 g_object_unref (inetaddr);
1701 g_list_free_full (rejected_addresses,
1702 (GDestroyNotify) gst_rtsp_address_free);
1704 gst_rtsp_address_free (addr);
1706 g_object_unref (rtp_socket);
1708 g_object_unref (rtcp_socket);
1713 /* must be called with lock */
1715 add_mcast_client_addr (GstRTSPStream * stream, const gchar * destination,
1716 guint rtp_port, guint rtcp_port)
1718 GstRTSPStreamPrivate *priv;
1720 UdpClientAddrInfo *client;
1723 priv = stream->priv;
1725 if (destination == NULL)
1728 inet = g_inet_address_new_from_string (destination);
1730 goto invalid_address;
1732 if (!g_inet_address_get_is_multicast (inet)) {
1733 g_object_unref (inet);
1734 goto invalid_address;
1736 g_object_unref (inet);
1738 for (walk = priv->mcast_clients; walk; walk = g_list_next (walk)) {
1739 UdpClientAddrInfo *cli = walk->data;
1741 if ((g_strcmp0 (cli->address, destination) == 0) &&
1742 (cli->rtp_port == rtp_port)) {
1743 GST_DEBUG ("requested destination already exists: %s:%u-%u",
1744 destination, rtp_port, rtcp_port);
1750 client = g_new0 (UdpClientAddrInfo, 1);
1751 client->address = g_strdup (destination);
1752 client->rtp_port = rtp_port;
1753 client->add_count = 1;
1754 priv->mcast_clients = g_list_prepend (priv->mcast_clients, client);
1756 GST_DEBUG ("added mcast client %s:%u-%u", destination, rtp_port, rtcp_port);
1762 GST_WARNING_OBJECT (stream, "Multicast address is invalid: %s",
1768 /* must be called with lock */
1770 remove_mcast_client_addr (GstRTSPStream * stream, const gchar * destination,
1771 guint rtp_port, guint rtcp_port)
1773 GstRTSPStreamPrivate *priv;
1776 priv = stream->priv;
1778 if (destination == NULL)
1779 goto no_destination;
1781 for (walk = priv->mcast_clients; walk; walk = g_list_next (walk)) {
1782 UdpClientAddrInfo *cli = walk->data;
1784 if ((g_strcmp0 (cli->address, destination) == 0) &&
1785 (cli->rtp_port == rtp_port)) {
1788 if (!cli->add_count) {
1789 priv->mcast_clients = g_list_remove (priv->mcast_clients, cli);
1790 free_mcast_client (cli);
1796 GST_WARNING_OBJECT (stream, "Address not found");
1801 GST_WARNING_OBJECT (stream, "No destination has been provided");
1808 * gst_rtsp_stream_allocate_udp_sockets:
1809 * @stream: a #GstRTSPStream
1810 * @family: protocol family
1811 * @transport: transport method
1812 * @use_client_settings: Whether to use client settings or not
1814 * Allocates RTP and RTCP ports.
1816 * Returns: %TRUE if the RTP and RTCP sockets have been succeccully allocated.
1819 gst_rtsp_stream_allocate_udp_sockets (GstRTSPStream * stream,
1820 GSocketFamily family, GstRTSPTransport * ct,
1821 gboolean use_transport_settings)
1823 GstRTSPStreamPrivate *priv;
1824 gboolean ret = FALSE;
1825 GstRTSPLowerTrans transport;
1826 gboolean allocated = FALSE;
1828 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1829 g_return_val_if_fail (ct != NULL, FALSE);
1830 priv = stream->priv;
1832 transport = ct->lower_transport;
1834 g_mutex_lock (&priv->lock);
1836 if (transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1837 if (family == G_SOCKET_FAMILY_IPV4 && priv->mcast_socket_v4[0])
1839 else if (family == G_SOCKET_FAMILY_IPV6 && priv->mcast_socket_v6[0])
1841 } else if (transport == GST_RTSP_LOWER_TRANS_UDP) {
1842 if (family == G_SOCKET_FAMILY_IPV4 && priv->socket_v4[0])
1844 else if (family == G_SOCKET_FAMILY_IPV6 && priv->socket_v6[0])
1849 GST_DEBUG_OBJECT (stream, "Allocated already");
1850 g_mutex_unlock (&priv->lock);
1854 if (family == G_SOCKET_FAMILY_IPV4) {
1856 if (transport == GST_RTSP_LOWER_TRANS_UDP) {
1858 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_UDP, ipv4");
1859 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1860 priv->socket_v4, &priv->server_addr_v4, FALSE, ct, FALSE);
1863 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_MCAST_UDP, ipv4");
1864 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV4,
1865 priv->mcast_socket_v4, &priv->mcast_addr_v4, TRUE, ct,
1866 use_transport_settings);
1870 if (transport == GST_RTSP_LOWER_TRANS_UDP) {
1872 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_UDP, ipv6");
1873 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1874 priv->socket_v6, &priv->server_addr_v6, FALSE, ct, FALSE);
1878 GST_DEBUG_OBJECT (stream, "GST_RTSP_LOWER_TRANS_MCAST_UDP, ipv6");
1879 ret = alloc_ports_one_family (stream, G_SOCKET_FAMILY_IPV6,
1880 priv->mcast_socket_v6, &priv->mcast_addr_v6, TRUE, ct,
1881 use_transport_settings);
1884 g_mutex_unlock (&priv->lock);
1890 * gst_rtsp_stream_set_client_side:
1891 * @stream: a #GstRTSPStream
1892 * @client_side: TRUE if this #GstRTSPStream is running on the 'client' side of
1893 * an RTSP connection.
1895 * Sets the #GstRTSPStream as a 'client side' stream - used for sending
1896 * streams to an RTSP server via RECORD. This has the practical effect
1897 * of changing which UDP port numbers are used when setting up the local
1898 * side of the stream sending to be either the 'server' or 'client' pair
1899 * of a configured UDP transport.
1902 gst_rtsp_stream_set_client_side (GstRTSPStream * stream, gboolean client_side)
1904 GstRTSPStreamPrivate *priv;
1906 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1907 priv = stream->priv;
1908 g_mutex_lock (&priv->lock);
1909 priv->client_side = client_side;
1910 g_mutex_unlock (&priv->lock);
1914 * gst_rtsp_stream_is_client_side:
1915 * @stream: a #GstRTSPStream
1917 * See gst_rtsp_stream_set_client_side()
1919 * Returns: TRUE if this #GstRTSPStream is client-side.
1922 gst_rtsp_stream_is_client_side (GstRTSPStream * stream)
1924 GstRTSPStreamPrivate *priv;
1927 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1929 priv = stream->priv;
1930 g_mutex_lock (&priv->lock);
1931 ret = priv->client_side;
1932 g_mutex_unlock (&priv->lock);
1938 * gst_rtsp_stream_get_server_port:
1939 * @stream: a #GstRTSPStream
1940 * @server_port: (out): result server port
1941 * @family: the port family to get
1943 * Fill @server_port with the port pair used by the server. This function can
1944 * only be called when @stream has been joined.
1947 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1948 GstRTSPRange * server_port, GSocketFamily family)
1950 GstRTSPStreamPrivate *priv;
1952 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1953 priv = stream->priv;
1954 g_return_if_fail (priv->joined_bin != NULL);
1957 server_port->min = 0;
1958 server_port->max = 0;
1961 g_mutex_lock (&priv->lock);
1962 if (family == G_SOCKET_FAMILY_IPV4) {
1963 if (server_port && priv->server_addr_v4) {
1964 server_port->min = priv->server_addr_v4->port;
1965 if (priv->enable_rtcp) {
1967 priv->server_addr_v4->port + priv->server_addr_v4->n_ports - 1;
1971 if (server_port && priv->server_addr_v6) {
1972 server_port->min = priv->server_addr_v6->port;
1973 if (priv->enable_rtcp) {
1975 priv->server_addr_v6->port + priv->server_addr_v6->n_ports - 1;
1979 g_mutex_unlock (&priv->lock);
1983 * gst_rtsp_stream_get_rtpsession:
1984 * @stream: a #GstRTSPStream
1986 * Get the RTP session of this stream.
1988 * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1991 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1993 GstRTSPStreamPrivate *priv;
1996 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1998 priv = stream->priv;
2000 g_mutex_lock (&priv->lock);
2001 if ((session = priv->session))
2002 g_object_ref (session);
2003 g_mutex_unlock (&priv->lock);
2009 * gst_rtsp_stream_get_srtp_encoder:
2010 * @stream: a #GstRTSPStream
2012 * Get the SRTP encoder for this stream.
2014 * Returns: (transfer full): The SRTP encoder for this stream. Unref after usage.
2017 gst_rtsp_stream_get_srtp_encoder (GstRTSPStream * stream)
2019 GstRTSPStreamPrivate *priv;
2020 GstElement *encoder;
2022 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2024 priv = stream->priv;
2026 g_mutex_lock (&priv->lock);
2027 if ((encoder = priv->srtpenc))
2028 g_object_ref (encoder);
2029 g_mutex_unlock (&priv->lock);
2035 * gst_rtsp_stream_get_ssrc:
2036 * @stream: a #GstRTSPStream
2037 * @ssrc: (out): result ssrc
2039 * Get the SSRC used by the RTP session of this stream. This function can only
2040 * be called when @stream has been joined.
2043 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
2045 GstRTSPStreamPrivate *priv;
2047 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
2048 priv = stream->priv;
2049 g_return_if_fail (priv->joined_bin != NULL);
2051 g_mutex_lock (&priv->lock);
2052 if (ssrc && priv->session)
2053 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
2054 g_mutex_unlock (&priv->lock);
2058 * gst_rtsp_stream_set_retransmission_time:
2059 * @stream: a #GstRTSPStream
2060 * @time: a #GstClockTime
2062 * Set the amount of time to store retransmission packets.
2065 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
2068 GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
2070 g_mutex_lock (&stream->priv->lock);
2071 stream->priv->rtx_time = time;
2072 if (stream->priv->rtxsend)
2073 g_object_set (stream->priv->rtxsend, "max-size-time",
2074 GST_TIME_AS_MSECONDS (time), NULL);
2075 g_mutex_unlock (&stream->priv->lock);
2079 * gst_rtsp_stream_get_retransmission_time:
2080 * @stream: a #GstRTSPStream
2082 * Get the amount of time to store retransmission data.
2084 * Returns: the amount of time to store retransmission data.
2087 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
2091 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
2093 g_mutex_lock (&stream->priv->lock);
2094 ret = stream->priv->rtx_time;
2095 g_mutex_unlock (&stream->priv->lock);
2101 * gst_rtsp_stream_set_retransmission_pt:
2102 * @stream: a #GstRTSPStream
2105 * Set the payload type (pt) for retransmission of this stream.
2108 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
2110 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
2112 GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
2114 g_mutex_lock (&stream->priv->lock);
2115 stream->priv->rtx_pt = rtx_pt;
2116 if (stream->priv->rtxsend) {
2117 guint pt = gst_rtsp_stream_get_pt (stream);
2118 gchar *pt_s = g_strdup_printf ("%d", pt);
2119 GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
2120 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2121 g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
2123 gst_structure_free (rtx_pt_map);
2125 g_mutex_unlock (&stream->priv->lock);
2129 * gst_rtsp_stream_get_retransmission_pt:
2130 * @stream: a #GstRTSPStream
2132 * Get the payload-type used for retransmission of this stream
2134 * Returns: The retransmission PT.
2137 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
2141 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
2143 g_mutex_lock (&stream->priv->lock);
2144 rtx_pt = stream->priv->rtx_pt;
2145 g_mutex_unlock (&stream->priv->lock);
2151 * gst_rtsp_stream_set_buffer_size:
2152 * @stream: a #GstRTSPStream
2153 * @size: the buffer size
2155 * Set the size of the UDP transmission buffer (in bytes)
2156 * Needs to be set before the stream is joined to a bin.
2161 gst_rtsp_stream_set_buffer_size (GstRTSPStream * stream, guint size)
2163 g_mutex_lock (&stream->priv->lock);
2164 stream->priv->buffer_size = size;
2165 g_mutex_unlock (&stream->priv->lock);
2169 * gst_rtsp_stream_get_buffer_size:
2170 * @stream: a #GstRTSPStream
2172 * Get the size of the UDP transmission buffer (in bytes)
2174 * Returns: the size of the UDP TX buffer
2179 gst_rtsp_stream_get_buffer_size (GstRTSPStream * stream)
2183 g_mutex_lock (&stream->priv->lock);
2184 buffer_size = stream->priv->buffer_size;
2185 g_mutex_unlock (&stream->priv->lock);
2191 * gst_rtsp_stream_set_max_mcast_ttl:
2192 * @stream: a #GstRTSPStream
2193 * @ttl: the new multicast ttl value
2195 * Set the maximum time-to-live value of outgoing multicast packets.
2197 * Returns: %TRUE if the requested ttl has been set successfully.
2202 gst_rtsp_stream_set_max_mcast_ttl (GstRTSPStream * stream, guint ttl)
2204 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2206 g_mutex_lock (&stream->priv->lock);
2207 if (ttl == 0 || ttl > DEFAULT_MAX_MCAST_TTL) {
2208 GST_WARNING_OBJECT (stream, "The reqested mcast TTL value is not valid.");
2209 g_mutex_unlock (&stream->priv->lock);
2212 stream->priv->max_mcast_ttl = ttl;
2213 g_mutex_unlock (&stream->priv->lock);
2219 * gst_rtsp_stream_get_max_mcast_ttl:
2220 * @stream: a #GstRTSPStream
2222 * Get the the maximum time-to-live value of outgoing multicast packets.
2224 * Returns: the maximum time-to-live value of outgoing multicast packets.
2229 gst_rtsp_stream_get_max_mcast_ttl (GstRTSPStream * stream)
2233 g_mutex_lock (&stream->priv->lock);
2234 ttl = stream->priv->max_mcast_ttl;
2235 g_mutex_unlock (&stream->priv->lock);
2241 * gst_rtsp_stream_verify_mcast_ttl:
2242 * @stream: a #GstRTSPStream
2243 * @ttl: a requested multicast ttl
2245 * Check if the requested multicast ttl value is allowed.
2247 * Returns: TRUE if the requested ttl value is allowed.
2252 gst_rtsp_stream_verify_mcast_ttl (GstRTSPStream * stream, guint ttl)
2254 gboolean res = FALSE;
2256 g_mutex_lock (&stream->priv->lock);
2257 if ((ttl > 0) && (ttl <= stream->priv->max_mcast_ttl))
2259 g_mutex_unlock (&stream->priv->lock);
2265 * gst_rtsp_stream_set_bind_mcast_address:
2266 * @stream: a #GstRTSPStream,
2267 * @bind_mcast_addr: the new value
2269 * Decide whether the multicast socket should be bound to a multicast address or
2275 gst_rtsp_stream_set_bind_mcast_address (GstRTSPStream * stream,
2276 gboolean bind_mcast_addr)
2278 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
2280 g_mutex_lock (&stream->priv->lock);
2281 stream->priv->bind_mcast_address = bind_mcast_addr;
2282 g_mutex_unlock (&stream->priv->lock);
2286 * gst_rtsp_stream_is_bind_mcast_address:
2287 * @stream: a #GstRTSPStream
2289 * Check if multicast sockets are configured to be bound to multicast addresses.
2291 * Returns: %TRUE if multicast sockets are configured to be bound to multicast addresses.
2296 gst_rtsp_stream_is_bind_mcast_address (GstRTSPStream * stream)
2300 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2302 g_mutex_lock (&stream->priv->lock);
2303 result = stream->priv->bind_mcast_address;
2304 g_mutex_unlock (&stream->priv->lock);
2310 gst_rtsp_stream_set_enable_rtcp (GstRTSPStream * stream, gboolean enable)
2312 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
2314 g_mutex_lock (&stream->priv->lock);
2315 stream->priv->enable_rtcp = enable;
2316 g_mutex_unlock (&stream->priv->lock);
2319 /* executed from streaming thread */
2321 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
2323 GstRTSPStreamPrivate *priv = stream->priv;
2324 GstCaps *newcaps, *oldcaps;
2326 newcaps = gst_pad_get_current_caps (pad);
2328 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
2331 g_mutex_lock (&priv->lock);
2332 oldcaps = priv->caps;
2333 priv->caps = newcaps;
2334 g_mutex_unlock (&priv->lock);
2337 gst_caps_unref (oldcaps);
2341 dump_structure (const GstStructure * s)
2345 sstr = gst_structure_to_string (s);
2346 GST_INFO ("structure: %s", sstr);
2350 static GstRTSPStreamTransport *
2351 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
2353 GstRTSPStreamPrivate *priv = stream->priv;
2355 GstRTSPStreamTransport *result = NULL;
2360 if (rtcp_from == NULL)
2363 tmp = g_strrstr (rtcp_from, ":");
2367 port = atoi (tmp + 1);
2368 dest = g_strndup (rtcp_from, tmp - rtcp_from);
2370 g_mutex_lock (&priv->lock);
2371 GST_INFO ("finding %s:%d in %d transports", dest, port,
2372 g_list_length (priv->transports));
2374 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2375 GstRTSPStreamTransport *trans = walk->data;
2376 const GstRTSPTransport *tr;
2379 tr = gst_rtsp_stream_transport_get_transport (trans);
2381 if (priv->client_side) {
2382 /* In client side mode the 'destination' is the RTSP server, so send
2384 min = tr->server_port.min;
2385 max = tr->server_port.max;
2387 min = tr->client_port.min;
2388 max = tr->client_port.max;
2391 if ((g_ascii_strcasecmp (tr->destination, dest) == 0) &&
2392 (min == port || max == port)) {
2398 g_object_ref (result);
2399 g_mutex_unlock (&priv->lock);
2406 static GstRTSPStreamTransport *
2407 check_transport (GObject * source, GstRTSPStream * stream)
2409 GstStructure *stats;
2410 GstRTSPStreamTransport *trans;
2412 /* see if we have a stream to match with the origin of the RTCP packet */
2413 trans = g_object_get_qdata (source, ssrc_stream_map_key);
2414 if (trans == NULL) {
2415 g_object_get (source, "stats", &stats, NULL);
2417 const gchar *rtcp_from;
2419 dump_structure (stats);
2421 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
2422 if ((trans = find_transport (stream, rtcp_from))) {
2423 GST_INFO ("%p: found transport %p for source %p", stream, trans,
2425 g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
2428 gst_structure_free (stats);
2436 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2438 GstRTSPStreamTransport *trans;
2440 GST_INFO ("%p: new source %p", stream, source);
2442 trans = check_transport (source, stream);
2445 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
2449 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
2451 GST_INFO ("%p: new SDES %p", stream, source);
2455 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2457 GstRTSPStreamTransport *trans;
2459 trans = check_transport (source, stream);
2462 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
2463 gst_rtsp_stream_transport_keep_alive (trans);
2467 GstStructure *stats;
2468 g_object_get (source, "stats", &stats, NULL);
2470 dump_structure (stats);
2471 gst_structure_free (stats);
2478 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2480 GST_INFO ("%p: source %p bye", stream, source);
2484 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2486 GstRTSPStreamTransport *trans;
2488 GST_INFO ("%p: source %p bye timeout", stream, source);
2490 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2491 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2492 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2497 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2499 GstRTSPStreamTransport *trans;
2501 GST_INFO ("%p: source %p timeout", stream, source);
2503 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
2504 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
2505 g_object_set_qdata (source, ssrc_stream_map_key, NULL);
2510 on_new_sender_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2512 GST_INFO ("%p: new sender source %p", stream, source);
2515 GstStructure *stats;
2516 g_object_get (source, "stats", &stats, NULL);
2518 dump_structure (stats);
2519 gst_structure_free (stats);
2526 on_sender_ssrc_active (GObject * session, GObject * source,
2527 GstRTSPStream * stream)
2531 GstStructure *stats;
2532 g_object_get (source, "stats", &stats, NULL);
2534 dump_structure (stats);
2535 gst_structure_free (stats);
2542 clear_tr_cache (GstRTSPStreamPrivate * priv)
2545 g_ptr_array_unref (priv->tr_cache);
2546 priv->tr_cache = NULL;
2549 /* With lock taken */
2551 any_transport_ready (GstRTSPStream * stream, gboolean is_rtp)
2553 gboolean ret = TRUE;
2554 GstRTSPStreamPrivate *priv = stream->priv;
2555 GPtrArray *transports;
2558 transports = priv->tr_cache;
2563 for (index = 0; index < transports->len; index++) {
2564 GstRTSPStreamTransport *tr = g_ptr_array_index (transports, index);
2565 if (!gst_rtsp_stream_transport_check_back_pressure (tr, is_rtp)) {
2577 /* Must be called *without* priv->lock */
2579 push_data (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2580 GstBuffer * buffer, GstBufferList * buffer_list, gboolean is_rtp)
2582 gboolean send_ret = TRUE;
2586 send_ret = gst_rtsp_stream_transport_send_rtp (trans, buffer);
2588 send_ret = gst_rtsp_stream_transport_send_rtp_list (trans, buffer_list);
2591 send_ret = gst_rtsp_stream_transport_send_rtcp (trans, buffer);
2593 send_ret = gst_rtsp_stream_transport_send_rtcp_list (trans, buffer_list);
2599 /* With priv->lock */
2601 ensure_cached_transports (GstRTSPStream * stream)
2603 GstRTSPStreamPrivate *priv = stream->priv;
2606 if (priv->tr_cache_cookie != priv->transports_cookie) {
2607 clear_tr_cache (priv);
2609 g_ptr_array_new_full (priv->n_tcp_transports, g_object_unref);
2611 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
2612 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
2613 const GstRTSPTransport *t = gst_rtsp_stream_transport_get_transport (tr);
2615 if (t->lower_transport != GST_RTSP_LOWER_TRANS_TCP)
2618 g_ptr_array_add (priv->tr_cache, g_object_ref (tr));
2620 priv->tr_cache_cookie = priv->transports_cookie;
2624 /* Must be called *without* priv->lock */
2626 check_transport_backlog (GstRTSPStream * stream, GstRTSPStreamTransport * trans)
2628 GstRTSPStreamPrivate *priv = stream->priv;
2629 gboolean send_ret = TRUE;
2631 gst_rtsp_stream_transport_lock_backlog (trans);
2633 if (!gst_rtsp_stream_transport_backlog_is_empty (trans)) {
2635 GstBufferList *buffer_list;
2640 gst_rtsp_stream_transport_backlog_pop (trans, &buffer, &buffer_list,
2643 g_assert (popped == TRUE);
2645 send_ret = push_data (stream, trans, buffer, buffer_list, is_rtp);
2647 gst_clear_buffer (&buffer);
2648 gst_clear_buffer_list (&buffer_list);
2651 gst_rtsp_stream_transport_unlock_backlog (trans);
2654 /* remove transport on send error */
2655 g_mutex_lock (&priv->lock);
2656 update_transport (stream, trans, FALSE);
2657 g_mutex_unlock (&priv->lock);
2661 /* Must be called with priv->lock */
2663 send_tcp_message (GstRTSPStream * stream, gint idx)
2665 GstRTSPStreamPrivate *priv = stream->priv;
2669 GstBufferList *buffer_list;
2670 guint n_messages = 0;
2672 GPtrArray *transports;
2674 if (!priv->have_buffer[idx])
2677 ensure_cached_transports (stream);
2679 is_rtp = (idx == 0);
2681 if (!any_transport_ready (stream, is_rtp))
2684 priv->have_buffer[idx] = FALSE;
2686 if (priv->appsink[idx] == NULL) {
2687 /* session expired */
2691 sink = GST_APP_SINK (priv->appsink[idx]);
2692 sample = gst_app_sink_pull_sample (sink);
2697 buffer = gst_sample_get_buffer (sample);
2698 buffer_list = gst_sample_get_buffer_list (sample);
2700 /* We will get one message-sent notification per buffer or
2701 * complete buffer-list. We handle each buffer-list as a unit */
2707 transports = priv->tr_cache;
2709 g_ptr_array_ref (transports);
2714 for (index = 0; index < transports->len; index++) {
2715 GstRTSPStreamTransport *tr = g_ptr_array_index (transports, index);
2716 GstBuffer *buf_ref = NULL;
2717 GstBufferList *buflist_ref = NULL;
2719 gst_rtsp_stream_transport_lock_backlog (tr);
2722 buf_ref = gst_buffer_ref (buffer);
2724 buflist_ref = gst_buffer_list_ref (buffer_list);
2726 if (!gst_rtsp_stream_transport_backlog_push (tr,
2727 buf_ref, buflist_ref, is_rtp)) {
2728 GST_ERROR_OBJECT (stream,
2729 "Dropping slow transport %" GST_PTR_FORMAT, tr);
2730 update_transport (stream, tr, FALSE);
2733 gst_rtsp_stream_transport_unlock_backlog (tr);
2736 gst_sample_unref (sample);
2738 g_mutex_unlock (&priv->lock);
2743 for (index = 0; index < transports->len; index++) {
2744 GstRTSPStreamTransport *tr = g_ptr_array_index (transports, index);
2746 check_transport_backlog (stream, tr);
2748 g_ptr_array_unref (transports);
2751 g_mutex_lock (&priv->lock);
2755 send_func (GstRTSPStream * stream)
2757 GstRTSPStreamPrivate *priv = stream->priv;
2759 g_mutex_lock (&priv->send_lock);
2761 while (priv->continue_sending) {
2766 cookie = priv->send_cookie;
2767 g_mutex_unlock (&priv->send_lock);
2769 g_mutex_lock (&priv->lock);
2771 /* iterate from 1 and down, so we prioritize RTCP over RTP */
2772 for (i = 1; i >= 0; i--) {
2773 if (priv->have_buffer[i]) {
2781 send_tcp_message (stream, idx);
2784 g_mutex_unlock (&priv->lock);
2786 g_mutex_lock (&priv->send_lock);
2787 while (cookie == priv->send_cookie && priv->continue_sending) {
2788 g_cond_wait (&priv->send_cond, &priv->send_lock);
2792 g_mutex_unlock (&priv->send_lock);
2797 static GstFlowReturn
2798 handle_new_sample (GstAppSink * sink, gpointer user_data)
2800 GstRTSPStream *stream = user_data;
2801 GstRTSPStreamPrivate *priv = stream->priv;
2804 g_mutex_lock (&priv->lock);
2806 for (i = 0; i < 2; i++) {
2807 if (GST_ELEMENT_CAST (sink) == priv->appsink[i]) {
2808 priv->have_buffer[i] = TRUE;
2813 if (priv->send_thread == NULL) {
2814 priv->send_thread = g_thread_new (NULL, (GThreadFunc) send_func, user_data);
2817 g_mutex_unlock (&priv->lock);
2819 g_mutex_lock (&priv->send_lock);
2820 priv->send_cookie++;
2821 g_cond_signal (&priv->send_cond);
2822 g_mutex_unlock (&priv->send_lock);
2827 static GstAppSinkCallbacks sink_cb = {
2828 NULL, /* not interested in EOS */
2829 NULL, /* not interested in preroll samples */
2834 get_rtp_encoder (GstRTSPStream * stream, guint session)
2836 GstRTSPStreamPrivate *priv = stream->priv;
2838 if (priv->srtpenc == NULL) {
2841 name = g_strdup_printf ("srtpenc_%u", session);
2842 priv->srtpenc = gst_element_factory_make ("srtpenc", name);
2845 g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
2847 return gst_object_ref (priv->srtpenc);
2851 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2853 GstRTSPStreamPrivate *priv = stream->priv;
2854 GstElement *oldenc, *enc;
2858 if (priv->idx != session)
2861 GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
2863 oldenc = priv->srtpenc;
2864 enc = get_rtp_encoder (stream, session);
2865 name = g_strdup_printf ("rtp_sink_%d", session);
2866 pad = gst_element_request_pad_simple (enc, name);
2868 gst_object_unref (pad);
2871 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
2878 request_rtcp_encoder (GstElement * rtpbin, guint session,
2879 GstRTSPStream * stream)
2881 GstRTSPStreamPrivate *priv = stream->priv;
2882 GstElement *oldenc, *enc;
2886 if (priv->idx != session)
2889 GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
2891 oldenc = priv->srtpenc;
2892 enc = get_rtp_encoder (stream, session);
2893 name = g_strdup_printf ("rtcp_sink_%d", session);
2894 pad = gst_element_request_pad_simple (enc, name);
2896 gst_object_unref (pad);
2899 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
2906 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2908 GstRTSPStreamPrivate *priv = stream->priv;
2911 GST_DEBUG ("request key %08x", ssrc);
2913 g_mutex_lock (&priv->lock);
2914 if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
2915 gst_caps_ref (caps);
2916 g_mutex_unlock (&priv->lock);
2922 request_rtp_rtcp_decoder (GstElement * rtpbin, guint session,
2923 GstRTSPStream * stream)
2925 GstRTSPStreamPrivate *priv = stream->priv;
2927 if (priv->idx != session)
2930 if (priv->srtpdec == NULL) {
2933 name = g_strdup_printf ("srtpdec_%u", session);
2934 priv->srtpdec = gst_element_factory_make ("srtpdec", name);
2937 g_signal_connect (priv->srtpdec, "request-key",
2938 (GCallback) request_key, stream);
2940 g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_RTCP_DECODER],
2944 return gst_object_ref (priv->srtpdec);
2948 * gst_rtsp_stream_request_aux_sender:
2949 * @stream: a #GstRTSPStream
2950 * @sessid: the session id
2952 * Creating a rtxsend bin
2954 * Returns: (transfer full) (nullable): a #GstElement.
2959 gst_rtsp_stream_request_aux_sender (GstRTSPStream * stream, guint sessid)
2963 GstStructure *pt_map;
2968 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2970 pt = gst_rtsp_stream_get_pt (stream);
2971 pt_s = g_strdup_printf ("%u", pt);
2972 rtx_pt = stream->priv->rtx_pt;
2974 GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
2976 bin = gst_bin_new (NULL);
2977 stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
2978 pt_map = gst_structure_new ("application/x-rtp-pt-map",
2979 pt_s, G_TYPE_UINT, rtx_pt, NULL);
2980 g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
2981 "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
2983 gst_structure_free (pt_map);
2984 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
2986 pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
2987 name = g_strdup_printf ("src_%u", sessid);
2988 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2990 gst_object_unref (pad);
2992 pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
2993 name = g_strdup_printf ("sink_%u", sessid);
2994 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2996 gst_object_unref (pad);
3002 add_rtx_pt (gpointer key, GstCaps * caps, GstStructure * pt_map)
3004 guint pt = GPOINTER_TO_INT (key);
3005 const GstStructure *s = gst_caps_get_structure (caps, 0);
3008 if (!g_strcmp0 (gst_structure_get_string (s, "encoding-name"), "RTX") &&
3009 (apt = gst_structure_get_string (s, "apt"))) {
3010 gst_structure_set (pt_map, apt, G_TYPE_UINT, pt, NULL);
3014 /* Call with priv->lock taken */
3016 update_rtx_receive_pt_map (GstRTSPStream * stream)
3018 GstStructure *pt_map;
3020 if (!stream->priv->rtxreceive)
3023 pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3024 g_hash_table_foreach (stream->priv->ptmap, (GHFunc) add_rtx_pt, pt_map);
3025 g_object_set (stream->priv->rtxreceive, "payload-type-map", pt_map, NULL);
3026 gst_structure_free (pt_map);
3033 retrieve_ulpfec_pt (gpointer key, GstCaps * caps, GstElement * ulpfec_decoder)
3035 guint pt = GPOINTER_TO_INT (key);
3036 const GstStructure *s = gst_caps_get_structure (caps, 0);
3038 if (!g_strcmp0 (gst_structure_get_string (s, "encoding-name"), "ULPFEC"))
3039 g_object_set (ulpfec_decoder, "pt", pt, NULL);
3043 update_ulpfec_decoder_pt (GstRTSPStream * stream)
3045 if (!stream->priv->ulpfec_decoder)
3048 g_hash_table_foreach (stream->priv->ptmap, (GHFunc) retrieve_ulpfec_pt,
3049 stream->priv->ulpfec_decoder);
3056 * gst_rtsp_stream_request_aux_receiver:
3057 * @stream: a #GstRTSPStream
3058 * @sessid: the session id
3060 * Creating a rtxreceive bin
3062 * Returns: (transfer full) (nullable): a #GstElement.
3067 gst_rtsp_stream_request_aux_receiver (GstRTSPStream * stream, guint sessid)
3073 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
3075 bin = gst_bin_new (NULL);
3076 stream->priv->rtxreceive = gst_element_factory_make ("rtprtxreceive", NULL);
3077 update_rtx_receive_pt_map (stream);
3078 update_ulpfec_decoder_pt (stream);
3079 gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxreceive));
3081 pad = gst_element_get_static_pad (stream->priv->rtxreceive, "src");
3082 name = g_strdup_printf ("src_%u", sessid);
3083 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3085 gst_object_unref (pad);
3087 pad = gst_element_get_static_pad (stream->priv->rtxreceive, "sink");
3088 name = g_strdup_printf ("sink_%u", sessid);
3089 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3091 gst_object_unref (pad);
3097 * gst_rtsp_stream_set_pt_map:
3098 * @stream: a #GstRTSPStream
3102 * Configure a pt map between @pt and @caps.
3105 gst_rtsp_stream_set_pt_map (GstRTSPStream * stream, guint pt, GstCaps * caps)
3107 GstRTSPStreamPrivate *priv = stream->priv;
3109 if (!GST_IS_CAPS (caps))
3112 g_mutex_lock (&priv->lock);
3113 g_hash_table_insert (priv->ptmap, GINT_TO_POINTER (pt), gst_caps_ref (caps));
3114 update_rtx_receive_pt_map (stream);
3115 g_mutex_unlock (&priv->lock);
3119 * gst_rtsp_stream_set_publish_clock_mode:
3120 * @stream: a #GstRTSPStream
3121 * @mode: the clock publish mode
3123 * Sets if and how the stream clock should be published according to RFC7273.
3128 gst_rtsp_stream_set_publish_clock_mode (GstRTSPStream * stream,
3129 GstRTSPPublishClockMode mode)
3131 GstRTSPStreamPrivate *priv;
3133 priv = stream->priv;
3134 g_mutex_lock (&priv->lock);
3135 priv->publish_clock_mode = mode;
3136 g_mutex_unlock (&priv->lock);
3140 * gst_rtsp_stream_get_publish_clock_mode:
3141 * @stream: a #GstRTSPStream
3143 * Gets if and how the stream clock should be published according to RFC7273.
3145 * Returns: The GstRTSPPublishClockMode
3149 GstRTSPPublishClockMode
3150 gst_rtsp_stream_get_publish_clock_mode (GstRTSPStream * stream)
3152 GstRTSPStreamPrivate *priv;
3153 GstRTSPPublishClockMode ret;
3155 priv = stream->priv;
3156 g_mutex_lock (&priv->lock);
3157 ret = priv->publish_clock_mode;
3158 g_mutex_unlock (&priv->lock);
3164 request_pt_map (GstElement * rtpbin, guint session, guint pt,
3165 GstRTSPStream * stream)
3167 GstRTSPStreamPrivate *priv = stream->priv;
3168 GstCaps *caps = NULL;
3170 g_mutex_lock (&priv->lock);
3172 if (priv->idx == session) {
3173 caps = g_hash_table_lookup (priv->ptmap, GINT_TO_POINTER (pt));
3175 GST_DEBUG ("Stream %p, pt %u: caps %" GST_PTR_FORMAT, stream, pt, caps);
3176 gst_caps_ref (caps);
3178 GST_DEBUG ("Stream %p, pt %u: no caps", stream, pt);
3182 g_mutex_unlock (&priv->lock);
3188 pad_added (GstElement * rtpbin, GstPad * pad, GstRTSPStream * stream)
3190 GstRTSPStreamPrivate *priv = stream->priv;
3192 GstPadLinkReturn ret;
3195 GST_DEBUG ("Stream %p added pad %s:%s for pad %s:%s", stream,
3196 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
3198 name = gst_pad_get_name (pad);
3199 if (sscanf (name, "recv_rtp_src_%u", &sessid) != 1) {
3205 if (priv->idx != sessid)
3208 if (gst_pad_is_linked (priv->sinkpad)) {
3209 GST_WARNING ("Stream %p: Pad %s:%s is linked already", stream,
3210 GST_DEBUG_PAD_NAME (priv->sinkpad));
3214 /* link the RTP pad to the session manager, it should not really fail unless
3215 * this is not really an RTP pad */
3216 ret = gst_pad_link (pad, priv->sinkpad);
3217 if (ret != GST_PAD_LINK_OK)
3219 priv->recv_rtp_src = gst_object_ref (pad);
3226 GST_ERROR ("Stream %p: Failed to link pads %s:%s and %s:%s", stream,
3227 GST_DEBUG_PAD_NAME (pad), GST_DEBUG_PAD_NAME (priv->sinkpad));
3232 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc,
3233 GstRTSPStream * stream)
3235 /* TODO: What to do here other than this? */
3236 GST_DEBUG ("Stream %p: Got EOS", stream);
3237 gst_pad_send_event (stream->priv->sinkpad, gst_event_new_eos ());
3240 typedef struct _ProbeData ProbeData;
3244 GstRTSPStream *stream;
3245 /* existing sink, already linked to tee */
3247 /* new sink, about to be linked */
3249 /* new queue element, that will be linked to tee and sink1 */
3250 GstElement **queue1;
3251 /* new queue element, that will be linked to tee and sink2 */
3252 GstElement **queue2;
3259 free_cb_data (gpointer user_data)
3261 ProbeData *data = user_data;
3263 gst_object_unref (data->stream);
3264 gst_object_unref (data->sink1);
3265 gst_object_unref (data->sink2);
3266 gst_object_unref (data->sink_pad);
3267 gst_object_unref (data->tee_pad);
3273 create_and_plug_queue_to_unlinked_stream (GstRTSPStream * stream,
3274 GstElement * tee, GstElement * sink, GstElement ** queue)
3276 GstRTSPStreamPrivate *priv = stream->priv;
3281 /* create queue for the new stream */
3282 *queue = gst_element_factory_make ("queue", NULL);
3283 g_object_set (*queue, "max-size-buffers", 1, "max-size-bytes", 0,
3284 "max-size-time", G_GINT64_CONSTANT (0), NULL);
3285 gst_bin_add (priv->joined_bin, *queue);
3287 /* link tee to queue */
3288 tee_pad = gst_element_request_pad_simple (tee, "src_%u");
3289 queue_pad = gst_element_get_static_pad (*queue, "sink");
3290 gst_pad_link (tee_pad, queue_pad);
3291 gst_object_unref (queue_pad);
3292 gst_object_unref (tee_pad);
3294 /* link queue to sink */
3295 queue_pad = gst_element_get_static_pad (*queue, "src");
3296 sink_pad = gst_element_get_static_pad (sink, "sink");
3297 gst_pad_link (queue_pad, sink_pad);
3298 gst_object_unref (queue_pad);
3299 gst_object_unref (sink_pad);
3301 gst_element_sync_state_with_parent (sink);
3302 gst_element_sync_state_with_parent (*queue);
3305 static GstPadProbeReturn
3306 create_and_plug_queue_to_linked_stream_probe_cb (GstPad * inpad,
3307 GstPadProbeInfo * info, gpointer user_data)
3309 GstRTSPStreamPrivate *priv;
3310 ProbeData *data = user_data;
3311 GstRTSPStream *stream;
3312 GstElement **queue1;
3313 GstElement **queue2;
3319 stream = data->stream;
3320 priv = stream->priv;
3321 queue1 = data->queue1;
3322 queue2 = data->queue2;
3323 sink_pad = data->sink_pad;
3324 tee_pad = data->tee_pad;
3325 index = data->index;
3327 /* unlink tee and the existing sink:
3328 * .-----. .---------.
3331 * '-----' '---------'
3333 g_assert (gst_pad_unlink (tee_pad, sink_pad));
3335 /* add queue to the already existing stream */
3336 *queue1 = gst_element_factory_make ("queue", NULL);
3337 g_object_set (*queue1, "max-size-buffers", 1, "max-size-bytes", 0,
3338 "max-size-time", G_GINT64_CONSTANT (0), NULL);
3339 gst_bin_add (priv->joined_bin, *queue1);
3341 /* link tee, queue and sink:
3342 * .-----. .---------. .---------.
3343 * | tee | | queue1 | | sink1 |
3344 * sink src->sink src->sink |
3345 * '-----' '---------' '---------'
3347 queue_pad = gst_element_get_static_pad (*queue1, "sink");
3348 gst_pad_link (tee_pad, queue_pad);
3349 gst_object_unref (queue_pad);
3350 queue_pad = gst_element_get_static_pad (*queue1, "src");
3351 gst_pad_link (queue_pad, sink_pad);
3352 gst_object_unref (queue_pad);
3354 gst_element_sync_state_with_parent (*queue1);
3356 /* create queue and link it to tee and the new sink */
3357 create_and_plug_queue_to_unlinked_stream (stream,
3358 priv->tee[index], data->sink2, queue2);
3360 /* the final stream:
3362 * .-----. .---------. .---------.
3363 * | tee | | queue1 | | sink1 |
3364 * sink src->sink src->sink |
3365 * | | '---------' '---------'
3366 * | | .---------. .---------.
3367 * | | | queue2 | | sink2 |
3368 * | src->sink src->sink |
3369 * '-----' '---------' '---------'
3372 return GST_PAD_PROBE_REMOVE;
3376 create_and_plug_queue_to_linked_stream (GstRTSPStream * stream,
3377 GstElement * sink1, GstElement * sink2, guint index, GstElement ** queue1,
3378 GstElement ** queue2)
3382 data = g_new0 (ProbeData, 1);
3383 data->stream = gst_object_ref (stream);
3384 data->sink1 = gst_object_ref (sink1);
3385 data->sink2 = gst_object_ref (sink2);
3386 data->queue1 = queue1;
3387 data->queue2 = queue2;
3388 data->index = index;
3390 data->sink_pad = gst_element_get_static_pad (sink1, "sink");
3391 g_assert (data->sink_pad);
3392 data->tee_pad = gst_pad_get_peer (data->sink_pad);
3393 g_assert (data->tee_pad);
3395 gst_pad_add_probe (data->tee_pad, GST_PAD_PROBE_TYPE_IDLE,
3396 create_and_plug_queue_to_linked_stream_probe_cb, data, free_cb_data);
3400 plug_udp_sink (GstRTSPStream * stream, GstElement * sink_to_plug,
3401 GstElement ** queue_to_plug, guint index, gboolean is_mcast)
3403 GstRTSPStreamPrivate *priv = stream->priv;
3404 GstElement *existing_sink;
3407 existing_sink = priv->udpsink[index];
3409 existing_sink = priv->mcast_udpsink[index];
3411 GST_DEBUG_OBJECT (stream, "plug %s sink", is_mcast ? "mcast" : "udp");
3413 /* add sink to the bin */
3414 gst_bin_add (priv->joined_bin, sink_to_plug);
3416 if (priv->appsink[index] && existing_sink) {
3418 /* queues are already added for the existing stream, add one for
3419 the newly added udp stream */
3420 create_and_plug_queue_to_unlinked_stream (stream, priv->tee[index],
3421 sink_to_plug, queue_to_plug);
3423 } else if (priv->appsink[index] || existing_sink) {
3425 GstElement *element;
3427 /* add queue to the already existing stream plus the newly created udp
3429 if (priv->appsink[index]) {
3430 element = priv->appsink[index];
3431 queue = &priv->appqueue[index];
3433 element = existing_sink;
3435 queue = &priv->udpqueue[index];
3437 queue = &priv->mcast_udpqueue[index];
3440 create_and_plug_queue_to_linked_stream (stream, element, sink_to_plug,
3441 index, queue, queue_to_plug);
3447 GST_DEBUG_OBJECT (stream, "creating first stream");
3449 /* no need to add queues */
3450 tee_pad = gst_element_request_pad_simple (priv->tee[index], "src_%u");
3451 sink_pad = gst_element_get_static_pad (sink_to_plug, "sink");
3452 gst_pad_link (tee_pad, sink_pad);
3453 gst_object_unref (tee_pad);
3454 gst_object_unref (sink_pad);
3457 gst_element_sync_state_with_parent (sink_to_plug);
3461 plug_tcp_sink (GstRTSPStream * stream, guint index)
3463 GstRTSPStreamPrivate *priv = stream->priv;
3465 GST_DEBUG_OBJECT (stream, "plug tcp sink");
3467 /* add sink to the bin */
3468 gst_bin_add (priv->joined_bin, priv->appsink[index]);
3470 if (priv->mcast_udpsink[index] && priv->udpsink[index]) {
3472 /* queues are already added for the existing stream, add one for
3473 the newly added tcp stream */
3474 create_and_plug_queue_to_unlinked_stream (stream,
3475 priv->tee[index], priv->appsink[index], &priv->appqueue[index]);
3477 } else if (priv->mcast_udpsink[index] || priv->udpsink[index]) {
3479 GstElement *element;
3481 /* add queue to the already existing stream plus the newly created tcp
3483 if (priv->mcast_udpsink[index]) {
3484 element = priv->mcast_udpsink[index];
3485 queue = &priv->mcast_udpqueue[index];
3487 element = priv->udpsink[index];
3488 queue = &priv->udpqueue[index];
3491 create_and_plug_queue_to_linked_stream (stream, element,
3492 priv->appsink[index], index, queue, &priv->appqueue[index]);
3498 /* no need to add queues */
3499 tee_pad = gst_element_request_pad_simple (priv->tee[index], "src_%u");
3500 sink_pad = gst_element_get_static_pad (priv->appsink[index], "sink");
3501 gst_pad_link (tee_pad, sink_pad);
3502 gst_object_unref (tee_pad);
3503 gst_object_unref (sink_pad);
3506 gst_element_sync_state_with_parent (priv->appsink[index]);
3510 plug_sink (GstRTSPStream * stream, const GstRTSPTransport * transport,
3513 GstRTSPStreamPrivate *priv;
3514 gboolean is_tcp, is_udp, is_mcast;
3515 priv = stream->priv;
3517 is_tcp = transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP;
3518 is_udp = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP;
3519 is_mcast = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST;
3522 plug_udp_sink (stream, priv->udpsink[index],
3523 &priv->udpqueue[index], index, FALSE);
3526 plug_udp_sink (stream, priv->mcast_udpsink[index],
3527 &priv->mcast_udpqueue[index], index, TRUE);
3530 plug_tcp_sink (stream, index);
3533 /* must be called with lock */
3535 create_sender_part (GstRTSPStream * stream, const GstRTSPTransport * transport)
3537 GstRTSPStreamPrivate *priv;
3540 gboolean is_tcp, is_udp, is_mcast;
3544 GST_DEBUG_OBJECT (stream, "create sender part");
3545 priv = stream->priv;
3546 bin = priv->joined_bin;
3548 is_tcp = transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP;
3549 is_udp = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP;
3550 is_mcast = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST;
3553 mcast_ttl = transport->ttl;
3555 GST_DEBUG_OBJECT (stream, "tcp: %d, udp: %d, mcast: %d (ttl: %d)", is_tcp,
3556 is_udp, is_mcast, mcast_ttl);
3558 if (is_udp && !priv->server_addr_v4 && !priv->server_addr_v6) {
3559 GST_WARNING_OBJECT (stream, "no sockets assigned for UDP");
3563 if (is_mcast && !priv->mcast_addr_v4 && !priv->mcast_addr_v6) {
3564 GST_WARNING_OBJECT (stream, "no sockets assigned for UDP multicast");
3568 if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv->payloader),
3569 "onvif-no-rate-control"))
3570 g_object_set (priv->payloader, "onvif-no-rate-control",
3571 !priv->do_rate_control, NULL);
3573 for (i = 0; i < (priv->enable_rtcp ? 2 : 1); i++) {
3574 gboolean link_tee = FALSE;
3575 /* For the sender we create this bit of pipeline for both
3576 * RTP and RTCP (when enabled).
3577 * Initially there will be only one active transport for
3578 * the stream, so the pipeline will look like this:
3580 * .--------. .-----. .---------.
3581 * | rtpbin | | tee | | sink |
3582 * | send->sink src->sink |
3583 * '--------' '-----' '---------'
3585 * For each new transport, the already existing branch will
3586 * be reconfigured by adding a queue element:
3588 * .--------. .-----. .---------. .---------.
3589 * | rtpbin | | tee | | queue | | udpsink |
3590 * | send->sink src->sink src->sink |
3591 * '--------' | | '---------' '---------'
3592 * | | .---------. .---------.
3593 * | | | queue | | udpsink |
3594 * | src->sink src->sink |
3595 * | | '---------' '---------'
3596 * | | .---------. .---------.
3597 * | | | queue | | appsink |
3598 * | src->sink src->sink |
3599 * '-----' '---------' '---------'
3602 /* Only link the RTP send src if we're going to send RTP, link
3603 * the RTCP send src always */
3604 if (!priv->srcpad && i == 0)
3607 if (!priv->tee[i]) {
3608 /* make tee for RTP/RTCP */
3609 priv->tee[i] = gst_element_factory_make ("tee", NULL);
3610 gst_bin_add (bin, priv->tee[i]);
3614 if (is_udp && !priv->udpsink[i]) {
3615 /* we create only one pair of udpsinks for IPv4 and IPv6 */
3616 create_and_configure_udpsink (stream, &priv->udpsink[i],
3617 priv->socket_v4[i], priv->socket_v6[i], FALSE, (i == 0), mcast_ttl);
3618 plug_sink (stream, transport, i);
3619 } else if (is_mcast && !priv->mcast_udpsink[i]) {
3620 /* we create only one pair of mcast-udpsinks for IPv4 and IPv6 */
3621 create_and_configure_udpsink (stream, &priv->mcast_udpsink[i],
3622 priv->mcast_socket_v4[i], priv->mcast_socket_v6[i], TRUE, (i == 0),
3624 plug_sink (stream, transport, i);
3625 } else if (is_tcp && !priv->appsink[i]) {
3627 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
3628 g_object_set (priv->appsink[i], "emit-signals", FALSE, "buffer-list",
3629 TRUE, "max-buffers", 1, NULL);
3632 g_object_set (priv->appsink[i], "sync", priv->do_rate_control, NULL);
3634 /* we need to set sync and preroll to FALSE for the sink to avoid
3635 * deadlock. This is only needed for sink sending RTCP data. */
3637 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
3639 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
3640 &sink_cb, stream, NULL);
3641 plug_sink (stream, transport, i);
3645 /* and link to rtpbin send pad */
3646 gst_element_sync_state_with_parent (priv->tee[i]);
3647 pad = gst_element_get_static_pad (priv->tee[i], "sink");
3648 gst_pad_link (priv->send_src[i], pad);
3649 gst_object_unref (pad);
3656 /* must be called with lock */
3658 plug_src (GstRTSPStream * stream, GstBin * bin, GstElement * src,
3659 GstElement * funnel)
3661 GstRTSPStreamPrivate *priv;
3662 GstPad *pad, *selpad;
3665 priv = stream->priv;
3668 gst_bin_add (bin, src);
3670 pad = gst_element_get_static_pad (src, "src");
3672 /* block pad so src can't push data while it's not yet linked */
3673 id = gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_BLOCK |
3674 GST_PAD_PROBE_TYPE_BUFFER, NULL, NULL, NULL);
3675 /* we set and keep these to playing so that they don't cause NO_PREROLL return
3676 * values. This is only relevant for PLAY pipelines */
3677 gst_element_set_state (src, GST_STATE_PLAYING);
3678 gst_element_set_locked_state (src, TRUE);
3681 /* and link to the funnel */
3682 selpad = gst_element_request_pad_simple (funnel, "sink_%u");
3683 gst_pad_link (pad, selpad);
3685 gst_pad_remove_probe (pad, id);
3686 gst_object_unref (pad);
3687 gst_object_unref (selpad);
3690 /* must be called with lock */
3692 create_receiver_part (GstRTSPStream * stream, const GstRTSPTransport *
3695 gboolean ret = FALSE;
3696 GstRTSPStreamPrivate *priv;
3707 GST_DEBUG_OBJECT (stream, "create receiver part");
3708 priv = stream->priv;
3709 bin = priv->joined_bin;
3711 tcp = transport->lower_transport == GST_RTSP_LOWER_TRANS_TCP;
3712 udp = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP;
3713 mcast = transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST;
3714 secure = (priv->profiles & GST_RTSP_PROFILE_SAVP)
3715 || (priv->profiles & GST_RTSP_PROFILE_SAVPF);
3718 rtp_caps = gst_caps_new_empty_simple ("application/x-srtp");
3719 rtcp_caps = gst_caps_new_empty_simple ("application/x-srtcp");
3721 rtp_caps = gst_caps_new_empty_simple ("application/x-rtp");
3722 rtcp_caps = gst_caps_new_empty_simple ("application/x-rtcp");
3725 GST_DEBUG_OBJECT (stream,
3726 "RTP caps: %" GST_PTR_FORMAT " RTCP caps: %" GST_PTR_FORMAT, rtp_caps,
3729 for (i = 0; i < (priv->enable_rtcp ? 2 : 1); i++) {
3730 /* For the receiver we create this bit of pipeline for both
3731 * RTP and RTCP (when enabled). We receive RTP/RTCP on appsrc and udpsrc
3732 * and it is all funneled into the rtpbin receive pad.
3735 * .--------. .--------. .--------.
3736 * | udpsrc | | funnel | | rtpbin |
3737 * | RTP src->sink src->sink |
3738 * '--------' | | | |
3739 * .--------. | | | |
3740 * | appsrc | | | | |
3741 * | RTP src->sink | | |
3742 * '--------' '--------' | |
3744 * .--------. .--------. | |
3745 * | udpsrc | | funnel | | |
3746 * | RTCP src->sink src->sink |
3747 * '--------' | | '--------'
3750 * | RTCP src->sink |
3751 * '--------' '--------'
3754 if (!priv->sinkpad && i == 0) {
3755 /* Only connect recv RTP sink if we expect to receive RTP. Connect recv
3756 * RTCP sink always */
3760 /* make funnel for the RTP/RTCP receivers */
3761 if (!priv->funnel[i]) {
3762 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
3763 gst_bin_add (bin, priv->funnel[i]);
3765 pad = gst_element_get_static_pad (priv->funnel[i], "src");
3766 gst_pad_link (pad, priv->recv_sink[i]);
3767 gst_object_unref (pad);
3770 if (udp && !priv->udpsrc_v4[i] && priv->server_addr_v4) {
3771 GST_DEBUG_OBJECT (stream, "udp IPv4, create and configure udpsources");
3772 if (!create_and_configure_udpsource (&priv->udpsrc_v4[i],
3773 priv->socket_v4[i]))
3777 g_object_set (priv->udpsrc_v4[i], "caps", rtp_caps, NULL);
3779 g_object_set (priv->udpsrc_v4[i], "caps", rtcp_caps, NULL);
3781 /* block early rtcp packets, pipeline not ready */
3782 g_assert (priv->block_early_rtcp_pad == NULL);
3783 priv->block_early_rtcp_pad = gst_element_get_static_pad
3784 (priv->udpsrc_v4[i], "src");
3785 priv->block_early_rtcp_probe = gst_pad_add_probe
3786 (priv->block_early_rtcp_pad,
3787 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER, NULL, NULL,
3791 plug_src (stream, bin, priv->udpsrc_v4[i], priv->funnel[i]);
3794 if (udp && !priv->udpsrc_v6[i] && priv->server_addr_v6) {
3795 GST_DEBUG_OBJECT (stream, "udp IPv6, create and configure udpsources");
3796 if (!create_and_configure_udpsource (&priv->udpsrc_v6[i],
3797 priv->socket_v6[i]))
3801 g_object_set (priv->udpsrc_v6[i], "caps", rtp_caps, NULL);
3803 g_object_set (priv->udpsrc_v6[i], "caps", rtcp_caps, NULL);
3805 /* block early rtcp packets, pipeline not ready */
3806 g_assert (priv->block_early_rtcp_pad_ipv6 == NULL);
3807 priv->block_early_rtcp_pad_ipv6 = gst_element_get_static_pad
3808 (priv->udpsrc_v6[i], "src");
3809 priv->block_early_rtcp_probe_ipv6 = gst_pad_add_probe
3810 (priv->block_early_rtcp_pad_ipv6,
3811 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER, NULL, NULL,
3815 plug_src (stream, bin, priv->udpsrc_v6[i], priv->funnel[i]);
3818 if (mcast && !priv->mcast_udpsrc_v4[i] && priv->mcast_addr_v4) {
3819 GST_DEBUG_OBJECT (stream, "mcast IPv4, create and configure udpsources");
3820 if (!create_and_configure_udpsource (&priv->mcast_udpsrc_v4[i],
3821 priv->mcast_socket_v4[i]))
3825 g_object_set (priv->mcast_udpsrc_v4[i], "caps", rtp_caps, NULL);
3827 g_object_set (priv->mcast_udpsrc_v4[i], "caps", rtcp_caps, NULL);
3830 plug_src (stream, bin, priv->mcast_udpsrc_v4[i], priv->funnel[i]);
3833 if (mcast && !priv->mcast_udpsrc_v6[i] && priv->mcast_addr_v6) {
3834 GST_DEBUG_OBJECT (stream, "mcast IPv6, create and configure udpsources");
3835 if (!create_and_configure_udpsource (&priv->mcast_udpsrc_v6[i],
3836 priv->mcast_socket_v6[i]))
3840 g_object_set (priv->mcast_udpsrc_v6[i], "caps", rtp_caps, NULL);
3842 g_object_set (priv->mcast_udpsrc_v6[i], "caps", rtcp_caps, NULL);
3845 plug_src (stream, bin, priv->mcast_udpsrc_v6[i], priv->funnel[i]);
3848 if (tcp && !priv->appsrc[i]) {
3849 /* make and add appsrc */
3850 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
3851 priv->appsrc_base_time[i] = -1;
3852 g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, "is-live",
3854 plug_src (stream, bin, priv->appsrc[i], priv->funnel[i]);
3857 gst_element_sync_state_with_parent (priv->funnel[i]);
3863 gst_caps_unref (rtp_caps);
3864 gst_caps_unref (rtcp_caps);
3869 gst_rtsp_stream_is_tcp_receiver (GstRTSPStream * stream)
3871 GstRTSPStreamPrivate *priv;
3872 gboolean ret = FALSE;
3874 priv = stream->priv;
3875 g_mutex_lock (&priv->lock);
3876 ret = (priv->sinkpad != NULL && priv->appsrc[0] != NULL);
3877 g_mutex_unlock (&priv->lock);
3883 check_mcast_client_addr (GstRTSPStream * stream, const GstRTSPTransport * tr)
3885 GstRTSPStreamPrivate *priv = stream->priv;
3888 if (priv->mcast_clients == NULL)
3894 if (tr->destination == NULL)
3895 goto no_destination;
3897 for (walk = priv->mcast_clients; walk; walk = g_list_next (walk)) {
3898 UdpClientAddrInfo *cli = walk->data;
3900 if ((g_strcmp0 (cli->address, tr->destination) == 0) &&
3901 (cli->rtp_port == tr->port.min))
3909 GST_WARNING_OBJECT (stream, "Adding mcast transport, but no mcast address "
3910 "has been reserved");
3915 GST_WARNING_OBJECT (stream, "Adding mcast transport, but no transport "
3916 "has been provided");
3921 GST_WARNING_OBJECT (stream, "Adding mcast transport, but it doesn't match "
3922 "the reserved address");
3928 * gst_rtsp_stream_join_bin:
3929 * @stream: a #GstRTSPStream
3930 * @bin: (transfer none): a #GstBin to join
3931 * @rtpbin: (transfer none): a rtpbin element in @bin
3932 * @state: the target state of the new elements
3934 * Join the #GstBin @bin that contains the element @rtpbin.
3936 * @stream will link to @rtpbin, which must be inside @bin. The elements
3937 * added to @bin will be set to the state given in @state.
3939 * Returns: %TRUE on success.
3942 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
3943 GstElement * rtpbin, GstState state)
3945 GstRTSPStreamPrivate *priv;
3948 GstPadLinkReturn ret;
3950 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3951 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
3952 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
3954 priv = stream->priv;
3956 g_mutex_lock (&priv->lock);
3957 if (priv->joined_bin != NULL)
3960 /* create a session with the same index as the stream */
3963 GST_INFO ("stream %p joining bin as session %u", stream, idx);
3965 if (priv->profiles & GST_RTSP_PROFILE_SAVP
3966 || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
3968 g_signal_connect (rtpbin, "request-rtp-encoder",
3969 (GCallback) request_rtp_encoder, stream);
3970 g_signal_connect (rtpbin, "request-rtcp-encoder",
3971 (GCallback) request_rtcp_encoder, stream);
3972 g_signal_connect (rtpbin, "request-rtp-decoder",
3973 (GCallback) request_rtp_rtcp_decoder, stream);
3974 g_signal_connect (rtpbin, "request-rtcp-decoder",
3975 (GCallback) request_rtp_rtcp_decoder, stream);
3978 if (priv->sinkpad) {
3979 g_signal_connect (rtpbin, "request-pt-map",
3980 (GCallback) request_pt_map, stream);
3983 /* get pads from the RTP session element for sending and receiving
3986 /* get a pad for sending RTP */
3987 name = g_strdup_printf ("send_rtp_sink_%u", idx);
3988 priv->send_rtp_sink = gst_element_request_pad_simple (rtpbin, name);
3991 /* link the RTP pad to the session manager, it should not really fail unless
3992 * this is not really an RTP pad */
3993 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
3994 if (ret != GST_PAD_LINK_OK)
3997 name = g_strdup_printf ("send_rtp_src_%u", idx);
3998 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
4001 /* RECORD case: need to connect our sinkpad from here */
4002 g_signal_connect (rtpbin, "pad-added", (GCallback) pad_added, stream);
4004 g_signal_connect (rtpbin, "on-npt-stop", (GCallback) on_npt_stop, stream);
4006 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
4007 priv->recv_sink[0] = gst_element_request_pad_simple (rtpbin, name);
4011 if (priv->enable_rtcp) {
4012 name = g_strdup_printf ("send_rtcp_src_%u", idx);
4013 priv->send_src[1] = gst_element_request_pad_simple (rtpbin, name);
4016 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
4017 priv->recv_sink[1] = gst_element_request_pad_simple (rtpbin, name);
4021 /* get the session */
4022 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
4024 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
4026 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
4028 g_signal_connect (priv->session, "on-ssrc-active",
4029 (GCallback) on_ssrc_active, stream);
4030 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
4032 g_signal_connect (priv->session, "on-bye-timeout",
4033 (GCallback) on_bye_timeout, stream);
4034 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
4037 /* signal for sender ssrc */
4038 g_signal_connect (priv->session, "on-new-sender-ssrc",
4039 (GCallback) on_new_sender_ssrc, stream);
4040 g_signal_connect (priv->session, "on-sender-ssrc-active",
4041 (GCallback) on_sender_ssrc_active, stream);
4043 g_object_set (priv->session, "disable-sr-timestamp", !priv->do_rate_control,
4047 /* be notified of caps changes */
4048 priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
4049 (GCallback) caps_notify, stream);
4050 priv->caps = gst_pad_get_current_caps (priv->send_src[0]);
4053 priv->joined_bin = bin;
4054 GST_DEBUG_OBJECT (stream, "successfully joined bin");
4055 g_mutex_unlock (&priv->lock);
4062 g_mutex_unlock (&priv->lock);
4067 GST_WARNING ("failed to link stream %u", idx);
4068 gst_object_unref (priv->send_rtp_sink);
4069 priv->send_rtp_sink = NULL;
4070 g_mutex_unlock (&priv->lock);
4076 clear_element (GstBin * bin, GstElement ** elementptr)
4079 gst_element_set_locked_state (*elementptr, FALSE);
4080 gst_element_set_state (*elementptr, GST_STATE_NULL);
4081 if (GST_ELEMENT_PARENT (*elementptr))
4082 gst_bin_remove (bin, *elementptr);
4084 gst_object_unref (*elementptr);
4090 * gst_rtsp_stream_leave_bin:
4091 * @stream: a #GstRTSPStream
4092 * @bin: (transfer none): a #GstBin
4093 * @rtpbin: (transfer none): a rtpbin #GstElement
4095 * Remove the elements of @stream from @bin.
4097 * Return: %TRUE on success.
4100 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
4101 GstElement * rtpbin)
4103 GstRTSPStreamPrivate *priv;
4106 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4107 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
4108 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
4110 priv = stream->priv;
4112 g_mutex_lock (&priv->send_lock);
4113 priv->continue_sending = FALSE;
4114 priv->send_cookie++;
4115 g_cond_signal (&priv->send_cond);
4116 g_mutex_unlock (&priv->send_lock);
4118 if (priv->send_thread) {
4119 g_thread_join (priv->send_thread);
4122 g_mutex_lock (&priv->lock);
4123 if (priv->joined_bin == NULL)
4124 goto was_not_joined;
4125 if (priv->joined_bin != bin)
4128 priv->joined_bin = NULL;
4130 /* all transports must be removed by now */
4131 if (priv->transports != NULL)
4132 goto transports_not_removed;
4134 if (priv->send_pool) {
4137 slask = priv->send_pool;
4138 priv->send_pool = NULL;
4139 g_mutex_unlock (&priv->lock);
4140 g_thread_pool_free (slask, TRUE, TRUE);
4141 g_mutex_lock (&priv->lock);
4144 clear_tr_cache (priv);
4146 GST_INFO ("stream %p leaving bin", stream);
4149 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
4151 g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
4152 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
4153 gst_object_unref (priv->send_rtp_sink);
4154 priv->send_rtp_sink = NULL;
4155 } else if (priv->recv_rtp_src) {
4156 gst_pad_unlink (priv->recv_rtp_src, priv->sinkpad);
4157 gst_object_unref (priv->recv_rtp_src);
4158 priv->recv_rtp_src = NULL;
4161 for (i = 0; i < (priv->enable_rtcp ? 2 : 1); i++) {
4162 clear_element (bin, &priv->udpsrc_v4[i]);
4163 clear_element (bin, &priv->udpsrc_v6[i]);
4164 clear_element (bin, &priv->udpqueue[i]);
4165 clear_element (bin, &priv->udpsink[i]);
4167 clear_element (bin, &priv->mcast_udpsrc_v4[i]);
4168 clear_element (bin, &priv->mcast_udpsrc_v6[i]);
4169 clear_element (bin, &priv->mcast_udpqueue[i]);
4170 clear_element (bin, &priv->mcast_udpsink[i]);
4172 clear_element (bin, &priv->appsrc[i]);
4173 clear_element (bin, &priv->appqueue[i]);
4174 clear_element (bin, &priv->appsink[i]);
4176 clear_element (bin, &priv->tee[i]);
4177 clear_element (bin, &priv->funnel[i]);
4179 if (priv->sinkpad || i == 1) {
4180 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
4181 gst_object_unref (priv->recv_sink[i]);
4182 priv->recv_sink[i] = NULL;
4187 gst_object_unref (priv->send_src[0]);
4188 priv->send_src[0] = NULL;
4191 if (priv->enable_rtcp) {
4192 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
4193 gst_object_unref (priv->send_src[1]);
4194 priv->send_src[1] = NULL;
4197 g_object_unref (priv->session);
4198 priv->session = NULL;
4200 gst_caps_unref (priv->caps);
4204 gst_object_unref (priv->srtpenc);
4206 gst_object_unref (priv->srtpdec);
4208 if (priv->mcast_addr_v4)
4209 gst_rtsp_address_free (priv->mcast_addr_v4);
4210 priv->mcast_addr_v4 = NULL;
4211 if (priv->mcast_addr_v6)
4212 gst_rtsp_address_free (priv->mcast_addr_v6);
4213 priv->mcast_addr_v6 = NULL;
4214 if (priv->server_addr_v4)
4215 gst_rtsp_address_free (priv->server_addr_v4);
4216 priv->server_addr_v4 = NULL;
4217 if (priv->server_addr_v6)
4218 gst_rtsp_address_free (priv->server_addr_v6);
4219 priv->server_addr_v6 = NULL;
4221 g_mutex_unlock (&priv->lock);
4227 g_mutex_unlock (&priv->lock);
4230 transports_not_removed:
4232 GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
4233 g_mutex_unlock (&priv->lock);
4238 GST_ERROR_OBJECT (stream, "leaving the wrong bin");
4239 g_mutex_unlock (&priv->lock);
4245 * gst_rtsp_stream_get_joined_bin:
4246 * @stream: a #GstRTSPStream
4248 * Get the previous joined bin with gst_rtsp_stream_join_bin() or NULL.
4250 * Return: (transfer full) (nullable): the joined bin or NULL.
4253 gst_rtsp_stream_get_joined_bin (GstRTSPStream * stream)
4255 GstRTSPStreamPrivate *priv;
4258 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4260 priv = stream->priv;
4262 g_mutex_lock (&priv->lock);
4263 bin = priv->joined_bin ? gst_object_ref (priv->joined_bin) : NULL;
4264 g_mutex_unlock (&priv->lock);
4270 * gst_rtsp_stream_get_rtpinfo:
4271 * @stream: a #GstRTSPStream
4272 * @rtptime: (allow-none) (out caller-allocates): result RTP timestamp
4273 * @seq: (allow-none) (out caller-allocates): result RTP seqnum
4274 * @clock_rate: (allow-none) (out caller-allocates): the clock rate
4275 * @running_time: (out caller-allocates): result running-time
4277 * Retrieve the current rtptime, seq and running-time. This is used to
4278 * construct a RTPInfo reply header.
4280 * Returns: %TRUE when rtptime, seq and running-time could be determined.
4283 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
4284 guint * rtptime, guint * seq, guint * clock_rate,
4285 GstClockTime * running_time)
4287 GstRTSPStreamPrivate *priv;
4288 GstStructure *stats;
4289 GObjectClass *payobjclass;
4291 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4293 priv = stream->priv;
4295 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
4297 g_mutex_lock (&priv->lock);
4299 /* First try to extract the information from the last buffer on the sinks.
4300 * This will have a more accurate sequence number and timestamp, as between
4301 * the payloader and the sink there can be some queues
4303 if (priv->udpsink[0] || priv->mcast_udpsink[0] || priv->appsink[0]) {
4304 GstSample *last_sample;
4306 if (priv->udpsink[0])
4307 g_object_get (priv->udpsink[0], "last-sample", &last_sample, NULL);
4308 else if (priv->mcast_udpsink[0])
4309 g_object_get (priv->mcast_udpsink[0], "last-sample", &last_sample, NULL);
4311 g_object_get (priv->appsink[0], "last-sample", &last_sample, NULL);
4316 GstSegment *segment;
4318 GstRTPBuffer rtp_buffer = GST_RTP_BUFFER_INIT;
4320 caps = gst_sample_get_caps (last_sample);
4321 buffer = gst_sample_get_buffer (last_sample);
4322 segment = gst_sample_get_segment (last_sample);
4323 s = gst_caps_get_structure (caps, 0);
4325 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp_buffer)) {
4326 guint ssrc_buf = gst_rtp_buffer_get_ssrc (&rtp_buffer);
4327 guint ssrc_stream = 0;
4328 if (gst_structure_has_field_typed (s, "ssrc", G_TYPE_UINT) &&
4329 gst_structure_get_uint (s, "ssrc", &ssrc_stream) &&
4330 ssrc_buf != ssrc_stream) {
4331 /* Skip buffers from auxiliary streams. */
4332 GST_DEBUG_OBJECT (stream,
4333 "not a buffer from the payloader, SSRC: %08x", ssrc_buf);
4335 gst_rtp_buffer_unmap (&rtp_buffer);
4336 gst_sample_unref (last_sample);
4341 *seq = gst_rtp_buffer_get_seq (&rtp_buffer);
4345 *rtptime = gst_rtp_buffer_get_timestamp (&rtp_buffer);
4348 gst_rtp_buffer_unmap (&rtp_buffer);
4352 gst_segment_to_running_time (segment, GST_FORMAT_TIME,
4353 GST_BUFFER_TIMESTAMP (buffer));
4357 gst_structure_get_int (s, "clock-rate", (gint *) clock_rate);
4359 if (*clock_rate == 0 && running_time)
4360 *running_time = GST_CLOCK_TIME_NONE;
4362 gst_sample_unref (last_sample);
4366 gst_sample_unref (last_sample);
4368 } else if (priv->blocking) {
4370 if (!priv->blocked_buffer)
4372 *seq = priv->blocked_seqnum;
4376 if (!priv->blocked_buffer)
4378 *rtptime = priv->blocked_rtptime;
4382 if (!GST_CLOCK_TIME_IS_VALID (priv->blocked_running_time))
4384 *running_time = priv->blocked_running_time;
4388 *clock_rate = priv->blocked_clock_rate;
4390 if (*clock_rate == 0 && running_time)
4391 *running_time = GST_CLOCK_TIME_NONE;
4399 if (g_object_class_find_property (payobjclass, "stats")) {
4400 g_object_get (priv->payloader, "stats", &stats, NULL);
4405 gst_structure_get_uint (stats, "seqnum-offset", seq);
4408 gst_structure_get_uint (stats, "timestamp", rtptime);
4411 gst_structure_get_clock_time (stats, "running-time", running_time);
4414 gst_structure_get_uint (stats, "clock-rate", clock_rate);
4415 if (*clock_rate == 0 && running_time)
4416 *running_time = GST_CLOCK_TIME_NONE;
4418 gst_structure_free (stats);
4420 if (!g_object_class_find_property (payobjclass, "seqnum") ||
4421 !g_object_class_find_property (payobjclass, "timestamp"))
4425 g_object_get (priv->payloader, "seqnum", seq, NULL);
4428 g_object_get (priv->payloader, "timestamp", rtptime, NULL);
4431 *running_time = GST_CLOCK_TIME_NONE;
4435 g_mutex_unlock (&priv->lock);
4442 GST_WARNING ("Could not get payloader stats");
4443 g_mutex_unlock (&priv->lock);
4449 * gst_rtsp_stream_get_rates:
4450 * @stream: a #GstRTSPStream
4451 * @rate: (optional) (out caller-allocates): the configured rate
4452 * @applied_rate: (optional) (out caller-allocates): the configured applied_rate
4454 * Retrieve the current rate and/or applied_rate.
4456 * Returns: %TRUE if rate and/or applied_rate could be determined.
4460 gst_rtsp_stream_get_rates (GstRTSPStream * stream, gdouble * rate,
4461 gdouble * applied_rate)
4463 GstRTSPStreamPrivate *priv;
4465 const GstSegment *segment;
4467 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4469 if (!rate && !applied_rate) {
4470 GST_WARNING_OBJECT (stream, "rate and applied_rate are both NULL");
4474 priv = stream->priv;
4476 g_mutex_lock (&priv->lock);
4478 if (!priv->send_rtp_sink)
4479 goto no_rtp_sink_pad;
4481 event = gst_pad_get_sticky_event (priv->send_rtp_sink, GST_EVENT_SEGMENT, 0);
4483 goto no_sticky_event;
4485 gst_event_parse_segment (event, &segment);
4487 *rate = segment->rate;
4489 *applied_rate = segment->applied_rate;
4491 gst_event_unref (event);
4492 g_mutex_unlock (&priv->lock);
4499 GST_WARNING_OBJECT (stream, "no send_rtp_sink pad yet");
4500 g_mutex_unlock (&priv->lock);
4505 GST_WARNING_OBJECT (stream, "no segment event on send_rtp_sink pad");
4506 g_mutex_unlock (&priv->lock);
4513 * gst_rtsp_stream_get_caps:
4514 * @stream: a #GstRTSPStream
4516 * Retrieve the current caps of @stream.
4518 * Returns: (transfer full) (nullable): the #GstCaps of @stream.
4519 * use gst_caps_unref() after usage.
4522 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
4524 GstRTSPStreamPrivate *priv;
4527 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
4529 priv = stream->priv;
4531 g_mutex_lock (&priv->lock);
4532 if ((result = priv->caps))
4533 gst_caps_ref (result);
4534 g_mutex_unlock (&priv->lock);
4540 * gst_rtsp_stream_recv_rtp:
4541 * @stream: a #GstRTSPStream
4542 * @buffer: (transfer full): a #GstBuffer
4544 * Handle an RTP buffer for the stream. This method is usually called when a
4545 * message has been received from a client using the TCP transport.
4547 * This function takes ownership of @buffer.
4549 * Returns: a GstFlowReturn.
4552 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
4554 GstRTSPStreamPrivate *priv;
4556 GstElement *element;
4558 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
4559 priv = stream->priv;
4560 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4561 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
4563 g_mutex_lock (&priv->lock);
4564 if (priv->appsrc[0])
4565 element = gst_object_ref (priv->appsrc[0]);
4568 g_mutex_unlock (&priv->lock);
4571 if (priv->appsrc_base_time[0] == -1) {
4572 /* Take current running_time. This timestamp will be put on
4573 * the first buffer of each stream because we are a live source and so we
4574 * timestamp with the running_time. When we are dealing with TCP, we also
4575 * only timestamp the first buffer (using the DISCONT flag) because a server
4576 * typically bursts data, for which we don't want to compensate by speeding
4577 * up the media. The other timestamps will be interpollated from this one
4578 * using the RTP timestamps. */
4579 GST_OBJECT_LOCK (element);
4580 if (GST_ELEMENT_CLOCK (element)) {
4582 GstClockTime base_time;
4584 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
4585 base_time = GST_ELEMENT_CAST (element)->base_time;
4587 priv->appsrc_base_time[0] = now - base_time;
4588 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[0];
4589 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
4590 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
4591 GST_TIME_ARGS (base_time));
4593 GST_OBJECT_UNLOCK (element);
4596 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
4597 gst_object_unref (element);
4605 * gst_rtsp_stream_recv_rtcp:
4606 * @stream: a #GstRTSPStream
4607 * @buffer: (transfer full): a #GstBuffer
4609 * Handle an RTCP buffer for the stream. This method is usually called when a
4610 * message has been received from a client using the TCP transport.
4612 * This function takes ownership of @buffer.
4614 * Returns: a GstFlowReturn.
4617 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
4619 GstRTSPStreamPrivate *priv;
4621 GstElement *element;
4623 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
4624 priv = stream->priv;
4625 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
4627 if (priv->joined_bin == NULL) {
4628 gst_buffer_unref (buffer);
4629 return GST_FLOW_NOT_LINKED;
4631 g_mutex_lock (&priv->lock);
4632 if (priv->appsrc[1])
4633 element = gst_object_ref (priv->appsrc[1]);
4636 g_mutex_unlock (&priv->lock);
4639 if (priv->appsrc_base_time[1] == -1) {
4640 /* Take current running_time. This timestamp will be put on
4641 * the first buffer of each stream because we are a live source and so we
4642 * timestamp with the running_time. When we are dealing with TCP, we also
4643 * only timestamp the first buffer (using the DISCONT flag) because a server
4644 * typically bursts data, for which we don't want to compensate by speeding
4645 * up the media. The other timestamps will be interpollated from this one
4646 * using the RTP timestamps. */
4647 GST_OBJECT_LOCK (element);
4648 if (GST_ELEMENT_CLOCK (element)) {
4650 GstClockTime base_time;
4652 now = gst_clock_get_time (GST_ELEMENT_CLOCK (element));
4653 base_time = GST_ELEMENT_CAST (element)->base_time;
4655 priv->appsrc_base_time[1] = now - base_time;
4656 GST_BUFFER_TIMESTAMP (buffer) = priv->appsrc_base_time[1];
4657 GST_DEBUG ("stream %p: first buffer at time %" GST_TIME_FORMAT
4658 ", base %" GST_TIME_FORMAT, stream, GST_TIME_ARGS (now),
4659 GST_TIME_ARGS (base_time));
4661 GST_OBJECT_UNLOCK (element);
4664 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
4665 gst_object_unref (element);
4668 gst_buffer_unref (buffer);
4673 /* must be called with lock */
4675 add_client (GstElement * rtp_sink, GstElement * rtcp_sink, const gchar * host,
4676 gint rtp_port, gint rtcp_port)
4678 if (rtp_sink != NULL)
4679 g_signal_emit_by_name (rtp_sink, "add", host, rtp_port, NULL);
4680 if (rtcp_sink != NULL)
4681 g_signal_emit_by_name (rtcp_sink, "add", host, rtcp_port, NULL);
4684 /* must be called with lock */
4686 remove_client (GstElement * rtp_sink, GstElement * rtcp_sink,
4687 const gchar * host, gint rtp_port, gint rtcp_port)
4689 if (rtp_sink != NULL)
4690 g_signal_emit_by_name (rtp_sink, "remove", host, rtp_port, NULL);
4691 if (rtcp_sink != NULL)
4692 g_signal_emit_by_name (rtcp_sink, "remove", host, rtcp_port, NULL);
4695 /* must be called with lock */
4697 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
4700 GstRTSPStreamPrivate *priv = stream->priv;
4701 const GstRTSPTransport *tr;
4706 tr = gst_rtsp_stream_transport_get_transport (trans);
4707 dest = tr->destination;
4709 tr_element = g_list_find (priv->transports, trans);
4711 if (add && tr_element)
4713 else if (!add && !tr_element)
4716 switch (tr->lower_transport) {
4717 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4723 GST_INFO ("adding %s:%d-%d", dest, min, max);
4724 if (!check_mcast_client_addr (stream, tr))
4726 add_client (priv->mcast_udpsink[0], priv->mcast_udpsink[1], dest, min,
4730 GST_INFO ("setting ttl-mc %d", tr->ttl);
4731 if (priv->mcast_udpsink[0])
4732 g_object_set (G_OBJECT (priv->mcast_udpsink[0]), "ttl-mc", tr->ttl,
4734 if (priv->mcast_udpsink[1])
4735 g_object_set (G_OBJECT (priv->mcast_udpsink[1]), "ttl-mc", tr->ttl,
4738 priv->transports = g_list_prepend (priv->transports, trans);
4740 GST_INFO ("removing %s:%d-%d", dest, min, max);
4741 if (!remove_mcast_client_addr (stream, dest, min, max))
4742 GST_WARNING_OBJECT (stream,
4743 "Failed to remove multicast address: %s:%d-%d", dest, min, max);
4744 priv->transports = g_list_delete_link (priv->transports, tr_element);
4745 remove_client (priv->mcast_udpsink[0], priv->mcast_udpsink[1], dest,
4750 case GST_RTSP_LOWER_TRANS_UDP:
4752 if (priv->client_side) {
4753 /* In client side mode the 'destination' is the RTSP server, so send
4755 min = tr->server_port.min;
4756 max = tr->server_port.max;
4758 min = tr->client_port.min;
4759 max = tr->client_port.max;
4763 GST_INFO ("adding %s:%d-%d", dest, min, max);
4764 add_client (priv->udpsink[0], priv->udpsink[1], dest, min, max);
4765 priv->transports = g_list_prepend (priv->transports, trans);
4767 GST_INFO ("removing %s:%d-%d", dest, min, max);
4768 priv->transports = g_list_delete_link (priv->transports, tr_element);
4769 remove_client (priv->udpsink[0], priv->udpsink[1], dest, min, max);
4771 priv->transports_cookie++;
4774 case GST_RTSP_LOWER_TRANS_TCP:
4776 GST_INFO ("adding TCP %s", tr->destination);
4777 priv->transports = g_list_prepend (priv->transports, trans);
4778 priv->n_tcp_transports++;
4780 GST_INFO ("removing TCP %s", tr->destination);
4781 priv->transports = g_list_delete_link (priv->transports, tr_element);
4783 gst_rtsp_stream_transport_lock_backlog (trans);
4784 gst_rtsp_stream_transport_clear_backlog (trans);
4785 gst_rtsp_stream_transport_unlock_backlog (trans);
4787 priv->n_tcp_transports--;
4789 priv->transports_cookie++;
4792 goto unknown_transport;
4799 GST_INFO ("Unknown transport %d", tr->lower_transport);
4809 on_message_sent (GstRTSPStreamTransport * trans, gpointer user_data)
4811 GstRTSPStream *stream = GST_RTSP_STREAM (user_data);
4812 GstRTSPStreamPrivate *priv = stream->priv;
4814 GST_DEBUG_OBJECT (stream, "message send complete");
4816 check_transport_backlog (stream, trans);
4818 g_mutex_lock (&priv->send_lock);
4819 priv->send_cookie++;
4820 g_cond_signal (&priv->send_cond);
4821 g_mutex_unlock (&priv->send_lock);
4825 * gst_rtsp_stream_add_transport:
4826 * @stream: a #GstRTSPStream
4827 * @trans: (transfer none): a #GstRTSPStreamTransport
4829 * Add the transport in @trans to @stream. The media of @stream will
4830 * then also be send to the values configured in @trans. Adding the
4831 * same transport twice will not add it a second time.
4833 * @stream must be joined to a bin.
4835 * @trans must contain a valid #GstRTSPTransport.
4837 * Returns: %TRUE if @trans was added
4840 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
4841 GstRTSPStreamTransport * trans)
4843 GstRTSPStreamPrivate *priv;
4846 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4847 priv = stream->priv;
4848 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
4849 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
4851 g_mutex_lock (&priv->lock);
4852 res = update_transport (stream, trans, TRUE);
4854 gst_rtsp_stream_transport_set_message_sent_full (trans, on_message_sent,
4856 g_mutex_unlock (&priv->lock);
4862 * gst_rtsp_stream_remove_transport:
4863 * @stream: a #GstRTSPStream
4864 * @trans: (transfer none): a #GstRTSPStreamTransport
4866 * Remove the transport in @trans from @stream. The media of @stream will
4867 * not be sent to the values configured in @trans.
4869 * @stream must be joined to a bin.
4871 * @trans must contain a valid #GstRTSPTransport.
4873 * Returns: %TRUE if @trans was removed
4876 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
4877 GstRTSPStreamTransport * trans)
4879 GstRTSPStreamPrivate *priv;
4882 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4883 priv = stream->priv;
4884 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
4885 g_return_val_if_fail (priv->joined_bin != NULL, FALSE);
4887 g_mutex_lock (&priv->lock);
4888 res = update_transport (stream, trans, FALSE);
4889 g_mutex_unlock (&priv->lock);
4895 * gst_rtsp_stream_update_crypto:
4896 * @stream: a #GstRTSPStream
4898 * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
4900 * Update the new crypto information for @ssrc in @stream. If information
4901 * for @ssrc did not exist, it will be added. If information
4902 * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
4903 * be removed from @stream.
4905 * Returns: %TRUE if @crypto could be updated
4908 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
4909 guint ssrc, GstCaps * crypto)
4911 GstRTSPStreamPrivate *priv;
4913 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
4914 g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
4916 priv = stream->priv;
4918 GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
4920 g_mutex_lock (&priv->lock);
4922 g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
4923 gst_caps_ref (crypto));
4925 g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
4926 g_mutex_unlock (&priv->lock);
4932 * gst_rtsp_stream_get_rtp_socket:
4933 * @stream: a #GstRTSPStream
4934 * @family: the socket family
4936 * Get the RTP socket from @stream for a @family.
4938 * @stream must be joined to a bin.
4940 * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
4941 * socket could be allocated for @family. Unref after usage
4944 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
4946 GstRTSPStreamPrivate *priv = stream->priv;
4949 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
4950 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
4951 family == G_SOCKET_FAMILY_IPV6, NULL);
4953 g_mutex_lock (&priv->lock);
4954 if (family == G_SOCKET_FAMILY_IPV6)
4955 socket = priv->socket_v6[0];
4957 socket = priv->socket_v4[0];
4960 socket = g_object_ref (socket);
4961 g_mutex_unlock (&priv->lock);
4967 * gst_rtsp_stream_get_rtcp_socket:
4968 * @stream: a #GstRTSPStream
4969 * @family: the socket family
4971 * Get the RTCP socket from @stream for a @family.
4973 * @stream must be joined to a bin.
4975 * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
4976 * socket could be allocated for @family. Unref after usage
4979 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
4981 GstRTSPStreamPrivate *priv = stream->priv;
4984 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
4985 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
4986 family == G_SOCKET_FAMILY_IPV6, NULL);
4988 g_mutex_lock (&priv->lock);
4989 if (family == G_SOCKET_FAMILY_IPV6)
4990 socket = priv->socket_v6[1];
4992 socket = priv->socket_v4[1];
4995 socket = g_object_ref (socket);
4996 g_mutex_unlock (&priv->lock);
5002 * gst_rtsp_stream_get_rtp_multicast_socket:
5003 * @stream: a #GstRTSPStream
5004 * @family: the socket family
5006 * Get the multicast RTP socket from @stream for a @family.
5008 * Returns: (transfer full) (nullable): the multicast RTP socket or %NULL if no
5010 * socket could be allocated for @family. Unref after usage
5013 gst_rtsp_stream_get_rtp_multicast_socket (GstRTSPStream * stream,
5014 GSocketFamily family)
5016 GstRTSPStreamPrivate *priv = stream->priv;
5019 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
5020 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
5021 family == G_SOCKET_FAMILY_IPV6, NULL);
5023 g_mutex_lock (&priv->lock);
5024 if (family == G_SOCKET_FAMILY_IPV6)
5025 socket = priv->mcast_socket_v6[0];
5027 socket = priv->mcast_socket_v4[0];
5030 socket = g_object_ref (socket);
5031 g_mutex_unlock (&priv->lock);
5037 * gst_rtsp_stream_get_rtcp_multicast_socket:
5038 * @stream: a #GstRTSPStream
5039 * @family: the socket family
5041 * Get the multicast RTCP socket from @stream for a @family.
5043 * Returns: (transfer full) (nullable): the multicast RTCP socket or %NULL if no
5044 * socket could be allocated for @family. Unref after usage
5049 gst_rtsp_stream_get_rtcp_multicast_socket (GstRTSPStream * stream,
5050 GSocketFamily family)
5052 GstRTSPStreamPrivate *priv = stream->priv;
5055 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
5056 g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
5057 family == G_SOCKET_FAMILY_IPV6, NULL);
5059 g_mutex_lock (&priv->lock);
5060 if (family == G_SOCKET_FAMILY_IPV6)
5061 socket = priv->mcast_socket_v6[1];
5063 socket = priv->mcast_socket_v4[1];
5066 socket = g_object_ref (socket);
5067 g_mutex_unlock (&priv->lock);
5073 * gst_rtsp_stream_add_multicast_client_address:
5074 * @stream: a #GstRTSPStream
5075 * @destination: (transfer none): a multicast address to add
5076 * @rtp_port: RTP port
5077 * @rtcp_port: RTCP port
5078 * @family: socket family
5080 * Add multicast client address to stream. At this point, the sockets that
5081 * will stream RTP and RTCP data to @destination are supposed to be
5084 * Returns: %TRUE if @destination can be addedd and handled by @stream.
5089 gst_rtsp_stream_add_multicast_client_address (GstRTSPStream * stream,
5090 const gchar * destination, guint rtp_port, guint rtcp_port,
5091 GSocketFamily family)
5093 GstRTSPStreamPrivate *priv;
5095 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5096 g_return_val_if_fail (destination != NULL, FALSE);
5098 priv = stream->priv;
5099 g_mutex_lock (&priv->lock);
5100 if ((family == G_SOCKET_FAMILY_IPV4) && (priv->mcast_socket_v4[0] == NULL))
5102 else if ((family == G_SOCKET_FAMILY_IPV6) &&
5103 (priv->mcast_socket_v6[0] == NULL))
5106 if (!add_mcast_client_addr (stream, destination, rtp_port, rtcp_port))
5107 goto add_addr_error;
5108 g_mutex_unlock (&priv->lock);
5114 GST_WARNING_OBJECT (stream,
5115 "Failed to add multicast address: no udp socket");
5116 g_mutex_unlock (&priv->lock);
5121 GST_WARNING_OBJECT (stream,
5122 "Failed to add multicast address: invalid address");
5123 g_mutex_unlock (&priv->lock);
5129 * gst_rtsp_stream_get_multicast_client_addresses
5130 * @stream: a #GstRTSPStream
5132 * Get all multicast client addresses that RTP data will be sent to
5134 * Returns: A comma separated list of host:port pairs with destinations
5139 gst_rtsp_stream_get_multicast_client_addresses (GstRTSPStream * stream)
5141 GstRTSPStreamPrivate *priv;
5145 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
5147 priv = stream->priv;
5148 str = g_string_new ("");
5150 g_mutex_lock (&priv->lock);
5151 clients = priv->mcast_clients;
5152 while (clients != NULL) {
5153 UdpClientAddrInfo *client;
5155 client = (UdpClientAddrInfo *) clients->data;
5156 clients = g_list_next (clients);
5157 g_string_append_printf (str, "%s:%d%s", client->address, client->rtp_port,
5158 (clients != NULL ? "," : ""));
5160 g_mutex_unlock (&priv->lock);
5162 return g_string_free (str, FALSE);
5166 * gst_rtsp_stream_set_seqnum:
5167 * @stream: a #GstRTSPStream
5168 * @seqnum: a new sequence number
5170 * Configure the sequence number in the payloader of @stream to @seqnum.
5173 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
5175 GstRTSPStreamPrivate *priv;
5177 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
5179 priv = stream->priv;
5181 g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
5185 * gst_rtsp_stream_get_seqnum:
5186 * @stream: a #GstRTSPStream
5188 * Get the configured sequence number in the payloader of @stream.
5190 * Returns: the sequence number of the payloader.
5193 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
5195 GstRTSPStreamPrivate *priv;
5198 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
5200 priv = stream->priv;
5202 g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
5208 * gst_rtsp_stream_transport_filter:
5209 * @stream: a #GstRTSPStream
5210 * @func: (scope call) (allow-none): a callback
5211 * @user_data: (closure): user data passed to @func
5213 * Call @func for each transport managed by @stream. The result value of @func
5214 * determines what happens to the transport. @func will be called with @stream
5215 * locked so no further actions on @stream can be performed from @func.
5217 * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
5220 * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
5222 * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
5223 * will also be added with an additional ref to the result #GList of this
5226 * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
5228 * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
5229 * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
5230 * element in the #GList should be unreffed before the list is freed.
5233 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
5234 GstRTSPStreamTransportFilterFunc func, gpointer user_data)
5236 GstRTSPStreamPrivate *priv;
5237 GList *result, *walk, *next;
5238 GHashTable *visited = NULL;
5241 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
5243 priv = stream->priv;
5247 visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
5249 g_mutex_lock (&priv->lock);
5251 cookie = priv->transports_cookie;
5252 for (walk = priv->transports; walk; walk = next) {
5253 GstRTSPStreamTransport *trans = walk->data;
5254 GstRTSPFilterResult res;
5257 next = g_list_next (walk);
5260 /* only visit each transport once */
5261 if (g_hash_table_contains (visited, trans))
5264 g_hash_table_add (visited, g_object_ref (trans));
5265 g_mutex_unlock (&priv->lock);
5267 res = func (stream, trans, user_data);
5269 g_mutex_lock (&priv->lock);
5271 res = GST_RTSP_FILTER_REF;
5273 changed = (cookie != priv->transports_cookie);
5276 case GST_RTSP_FILTER_REMOVE:
5277 update_transport (stream, trans, FALSE);
5279 case GST_RTSP_FILTER_REF:
5280 result = g_list_prepend (result, g_object_ref (trans));
5282 case GST_RTSP_FILTER_KEEP:
5289 g_mutex_unlock (&priv->lock);
5292 g_hash_table_unref (visited);
5297 static GstPadProbeReturn
5298 rtp_pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
5300 GstRTSPStreamPrivate *priv;
5301 GstRTSPStream *stream;
5302 GstBuffer *buffer = NULL;
5303 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
5307 priv = stream->priv;
5309 g_mutex_lock (&priv->lock);
5311 if ((info->type & GST_PAD_PROBE_TYPE_BUFFER)) {
5312 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
5314 buffer = gst_pad_probe_info_get_buffer (info);
5315 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp)) {
5316 priv->blocked_buffer = TRUE;
5317 priv->blocked_seqnum = gst_rtp_buffer_get_seq (&rtp);
5318 priv->blocked_rtptime = gst_rtp_buffer_get_timestamp (&rtp);
5319 gst_rtp_buffer_unmap (&rtp);
5321 priv->position = GST_BUFFER_TIMESTAMP (buffer);
5322 } else if ((info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST)) {
5323 GstRTPBuffer rtp = GST_RTP_BUFFER_INIT;
5325 GstBufferList *list = gst_pad_probe_info_get_buffer_list (info);
5326 buffer = gst_buffer_list_get (list, 0);
5327 if (gst_rtp_buffer_map (buffer, GST_MAP_READ, &rtp)) {
5328 priv->blocked_buffer = TRUE;
5329 priv->blocked_seqnum = gst_rtp_buffer_get_seq (&rtp);
5330 priv->blocked_rtptime = gst_rtp_buffer_get_timestamp (&rtp);
5331 gst_rtp_buffer_unmap (&rtp);
5333 priv->position = GST_BUFFER_TIMESTAMP (buffer);
5334 } else if ((info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM)) {
5335 if (GST_EVENT_TYPE (info->data) == GST_EVENT_GAP) {
5336 gst_event_parse_gap (info->data, &priv->position, NULL);
5338 ret = GST_PAD_PROBE_PASS;
5339 g_mutex_unlock (&priv->lock);
5343 g_assert_not_reached ();
5346 event = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
5348 const GstSegment *segment;
5350 gst_event_parse_segment (event, &segment);
5351 priv->blocked_running_time =
5352 gst_segment_to_stream_time (segment, GST_FORMAT_TIME, priv->position);
5353 gst_event_unref (event);
5356 event = gst_pad_get_sticky_event (pad, GST_EVENT_CAPS, 0);
5361 gst_event_parse_caps (event, &caps);
5362 s = gst_caps_get_structure (caps, 0);
5363 gst_structure_get_int (s, "clock-rate", &priv->blocked_clock_rate);
5364 gst_event_unref (event);
5367 priv->blocking = TRUE;
5369 GST_DEBUG_OBJECT (pad, "Now blocking");
5371 GST_DEBUG_OBJECT (stream, "position: %" GST_TIME_FORMAT,
5372 GST_TIME_ARGS (priv->position));
5374 g_mutex_unlock (&priv->lock);
5376 gst_element_post_message (priv->payloader,
5377 gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
5378 gst_structure_new ("GstRTSPStreamBlocking", "is_complete",
5379 G_TYPE_BOOLEAN, priv->is_complete, NULL)));
5385 static GstPadProbeReturn
5386 rtcp_pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
5388 GstRTSPStreamPrivate *priv;
5389 GstRTSPStream *stream;
5390 GstPadProbeReturn ret = GST_PAD_PROBE_OK;
5393 priv = stream->priv;
5395 g_mutex_lock (&priv->lock);
5397 if ((info->type & GST_PAD_PROBE_TYPE_BUFFER) ||
5398 (info->type & GST_PAD_PROBE_TYPE_BUFFER_LIST)) {
5399 GST_DEBUG_OBJECT (pad, "Now blocking on buffer");
5400 } else if ((info->type & GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM)) {
5401 if (GST_EVENT_TYPE (info->data) == GST_EVENT_GAP) {
5402 GST_DEBUG_OBJECT (pad, "Now blocking on gap event");
5403 ret = GST_PAD_PROBE_OK;
5405 ret = GST_PAD_PROBE_PASS;
5406 g_mutex_unlock (&priv->lock);
5410 g_assert_not_reached ();
5413 g_mutex_unlock (&priv->lock);
5421 set_blocked (GstRTSPStream * stream, gboolean blocked)
5423 GstRTSPStreamPrivate *priv;
5426 GST_DEBUG_OBJECT (stream, "blocked: %d", blocked);
5428 priv = stream->priv;
5432 if (priv->sinkpad) {
5433 priv->blocking = TRUE;
5436 for (i = 0; i < 2; i++) {
5437 if (priv->blocked_id[i] != 0)
5439 if (priv->send_src[i]) {
5440 priv->blocking = FALSE;
5441 priv->blocked_buffer = FALSE;
5442 priv->blocked_running_time = GST_CLOCK_TIME_NONE;
5443 priv->blocked_clock_rate = 0;
5446 priv->blocked_id[i] = gst_pad_add_probe (priv->send_src[i],
5447 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
5448 GST_PAD_PROBE_TYPE_BUFFER_LIST |
5449 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, rtp_pad_blocking,
5450 g_object_ref (stream), g_object_unref);
5452 priv->blocked_id[i] = gst_pad_add_probe (priv->send_src[i],
5453 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
5454 GST_PAD_PROBE_TYPE_BUFFER_LIST |
5455 GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM, rtcp_pad_blocking,
5456 g_object_ref (stream), g_object_unref);
5461 for (i = 0; i < 2; i++) {
5462 if (priv->blocked_id[i] != 0) {
5463 gst_pad_remove_probe (priv->send_src[i], priv->blocked_id[i]);
5464 priv->blocked_id[i] = 0;
5467 priv->blocking = FALSE;
5472 * gst_rtsp_stream_set_blocked:
5473 * @stream: a #GstRTSPStream
5474 * @blocked: boolean indicating we should block or unblock
5476 * Blocks or unblocks the dataflow on @stream.
5478 * Returns: %TRUE on success
5481 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
5483 GstRTSPStreamPrivate *priv;
5485 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5487 priv = stream->priv;
5488 g_mutex_lock (&priv->lock);
5489 set_blocked (stream, blocked);
5490 g_mutex_unlock (&priv->lock);
5496 * gst_rtsp_stream_ublock_linked:
5497 * @stream: a #GstRTSPStream
5499 * Unblocks the dataflow on @stream if it is linked.
5501 * Returns: %TRUE on success
5506 gst_rtsp_stream_unblock_linked (GstRTSPStream * stream)
5508 GstRTSPStreamPrivate *priv;
5510 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5512 priv = stream->priv;
5513 g_mutex_lock (&priv->lock);
5514 if (priv->send_src[0] && gst_pad_is_linked (priv->send_src[0]))
5515 set_blocked (stream, FALSE);
5516 g_mutex_unlock (&priv->lock);
5522 * gst_rtsp_stream_is_blocking:
5523 * @stream: a #GstRTSPStream
5525 * Check if @stream is blocking on a #GstBuffer.
5527 * Returns: %TRUE if @stream is blocking
5530 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
5532 GstRTSPStreamPrivate *priv;
5535 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5537 priv = stream->priv;
5539 g_mutex_lock (&priv->lock);
5540 result = priv->blocking;
5541 g_mutex_unlock (&priv->lock);
5547 * gst_rtsp_stream_query_position:
5548 * @stream: a #GstRTSPStream
5549 * @position: (out): current position of a #GstRTSPStream
5551 * Query the position of the stream in %GST_FORMAT_TIME. This only considers
5552 * the RTP parts of the pipeline and not the RTCP parts.
5554 * Returns: %TRUE if the position could be queried
5557 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
5559 GstRTSPStreamPrivate *priv;
5563 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5565 /* query position: if no sinks have been added yet,
5566 * we obtain the position from the pad otherwise we query the sinks */
5568 priv = stream->priv;
5570 g_mutex_lock (&priv->lock);
5572 if (priv->blocking && GST_CLOCK_TIME_IS_VALID (priv->blocked_running_time)) {
5573 *position = priv->blocked_running_time;
5574 g_mutex_unlock (&priv->lock);
5578 /* depending on the transport type, it should query corresponding sink */
5579 if (priv->configured_protocols & GST_RTSP_LOWER_TRANS_UDP)
5580 sink = priv->udpsink[0];
5581 else if (priv->configured_protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
5582 sink = priv->mcast_udpsink[0];
5584 sink = priv->appsink[0];
5587 gst_object_ref (sink);
5588 } else if (priv->send_src[0]) {
5589 pad = gst_object_ref (priv->send_src[0]);
5591 g_mutex_unlock (&priv->lock);
5592 GST_WARNING_OBJECT (stream, "Couldn't obtain postion: erroneous pipeline");
5595 g_mutex_unlock (&priv->lock);
5598 if (!gst_element_query_position (sink, GST_FORMAT_TIME, position)) {
5599 GST_WARNING_OBJECT (stream,
5600 "Couldn't obtain postion: position query failed");
5601 gst_object_unref (sink);
5604 gst_object_unref (sink);
5607 const GstSegment *segment;
5609 event = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
5611 GST_WARNING_OBJECT (stream, "Couldn't obtain postion: no segment event");
5612 gst_object_unref (pad);
5616 gst_event_parse_segment (event, &segment);
5617 if (segment->format != GST_FORMAT_TIME) {
5620 g_mutex_lock (&priv->lock);
5621 *position = priv->position;
5622 g_mutex_unlock (&priv->lock);
5624 gst_segment_to_stream_time (segment, GST_FORMAT_TIME, *position);
5626 gst_event_unref (event);
5627 gst_object_unref (pad);
5634 * gst_rtsp_stream_query_stop:
5635 * @stream: a #GstRTSPStream
5636 * @stop: (out): current stop of a #GstRTSPStream
5638 * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
5639 * the RTP parts of the pipeline and not the RTCP parts.
5641 * Returns: %TRUE if the stop could be queried
5644 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
5646 GstRTSPStreamPrivate *priv;
5650 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5652 /* query stop position: if no sinks have been added yet,
5653 * we obtain the stop position from the pad otherwise we query the sinks */
5655 priv = stream->priv;
5657 g_mutex_lock (&priv->lock);
5658 /* depending on the transport type, it should query corresponding sink */
5659 if (priv->configured_protocols & GST_RTSP_LOWER_TRANS_UDP)
5660 sink = priv->udpsink[0];
5661 else if (priv->configured_protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST)
5662 sink = priv->mcast_udpsink[0];
5664 sink = priv->appsink[0];
5667 gst_object_ref (sink);
5668 } else if (priv->send_src[0]) {
5669 pad = gst_object_ref (priv->send_src[0]);
5671 g_mutex_unlock (&priv->lock);
5672 GST_WARNING_OBJECT (stream, "Couldn't obtain stop: erroneous pipeline");
5675 g_mutex_unlock (&priv->lock);
5684 query = gst_query_new_segment (GST_FORMAT_TIME);
5685 if (!gst_element_query (sink, query)) {
5686 GST_WARNING_OBJECT (stream, "Couldn't obtain stop: element query failed");
5687 gst_query_unref (query);
5688 gst_object_unref (sink);
5691 gst_query_parse_segment (query, &rate, &format, &start_value, &stop_value);
5692 if (format != GST_FORMAT_TIME)
5695 *stop = rate > 0.0 ? stop_value : start_value;
5696 gst_query_unref (query);
5697 gst_object_unref (sink);
5700 const GstSegment *segment;
5702 event = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
5704 GST_WARNING_OBJECT (stream, "Couldn't obtain stop: no segment event");
5705 gst_object_unref (pad);
5708 gst_event_parse_segment (event, &segment);
5709 if (segment->format != GST_FORMAT_TIME) {
5712 *stop = segment->stop;
5714 *stop = segment->duration;
5716 *stop = gst_segment_to_stream_time (segment, GST_FORMAT_TIME, *stop);
5718 gst_event_unref (event);
5719 gst_object_unref (pad);
5726 * gst_rtsp_stream_seekable:
5727 * @stream: a #GstRTSPStream
5729 * Checks whether the individual @stream is seekable.
5731 * Returns: %TRUE if @stream is seekable, else %FALSE.
5736 gst_rtsp_stream_seekable (GstRTSPStream * stream)
5738 GstRTSPStreamPrivate *priv;
5740 GstQuery *query = NULL;
5741 gboolean seekable = FALSE;
5743 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5745 /* query stop position: if no sinks have been added yet,
5746 * we obtain the stop position from the pad otherwise we query the sinks */
5748 priv = stream->priv;
5750 g_mutex_lock (&priv->lock);
5751 /* depending on the transport type, it should query corresponding sink */
5753 pad = gst_object_ref (priv->srcpad);
5755 g_mutex_unlock (&priv->lock);
5756 GST_WARNING_OBJECT (stream, "Pad not available, can't query seekability");
5759 g_mutex_unlock (&priv->lock);
5761 query = gst_query_new_seeking (GST_FORMAT_TIME);
5762 if (!gst_pad_query (pad, query)) {
5763 GST_WARNING_OBJECT (stream, "seeking query failed");
5766 gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
5770 gst_object_unref (pad);
5772 gst_query_unref (query);
5774 GST_DEBUG_OBJECT (stream, "Returning %d", seekable);
5780 * gst_rtsp_stream_complete_stream:
5781 * @stream: a #GstRTSPStream
5782 * @transport: a #GstRTSPTransport
5784 * Add a receiver and sender part to the pipeline based on the transport from
5787 * Returns: %TRUE if the stream has been sucessfully updated.
5792 gst_rtsp_stream_complete_stream (GstRTSPStream * stream,
5793 const GstRTSPTransport * transport)
5795 GstRTSPStreamPrivate *priv;
5797 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5799 priv = stream->priv;
5800 GST_DEBUG_OBJECT (stream, "complete stream");
5802 g_mutex_lock (&priv->lock);
5804 if (!(priv->allowed_protocols & transport->lower_transport))
5805 goto unallowed_transport;
5807 if (!create_receiver_part (stream, transport))
5808 goto create_receiver_error;
5810 /* in the RECORD case, we only add RTCP sender part */
5811 if (!create_sender_part (stream, transport))
5812 goto create_sender_error;
5814 priv->configured_protocols |= transport->lower_transport;
5816 priv->is_complete = TRUE;
5817 g_mutex_unlock (&priv->lock);
5819 GST_DEBUG_OBJECT (stream, "pipeline sucsessfully updated");
5822 create_receiver_error:
5823 create_sender_error:
5824 unallowed_transport:
5826 g_mutex_unlock (&priv->lock);
5832 * gst_rtsp_stream_is_complete:
5833 * @stream: a #GstRTSPStream
5835 * Checks whether the stream is complete, contains the receiver and the sender
5836 * parts. As the stream contains sink(s) element(s), it's possible to perform
5837 * seek operations on it.
5839 * Returns: %TRUE if the stream contains at least one sink element.
5844 gst_rtsp_stream_is_complete (GstRTSPStream * stream)
5846 GstRTSPStreamPrivate *priv;
5847 gboolean ret = FALSE;
5849 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5851 priv = stream->priv;
5852 g_mutex_lock (&priv->lock);
5853 ret = priv->is_complete;
5854 g_mutex_unlock (&priv->lock);
5860 * gst_rtsp_stream_is_sender:
5861 * @stream: a #GstRTSPStream
5863 * Checks whether the stream is a sender.
5865 * Returns: %TRUE if the stream is a sender and %FALSE otherwise.
5870 gst_rtsp_stream_is_sender (GstRTSPStream * stream)
5872 GstRTSPStreamPrivate *priv;
5873 gboolean ret = FALSE;
5875 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5877 priv = stream->priv;
5878 g_mutex_lock (&priv->lock);
5879 ret = (priv->srcpad != NULL);
5880 g_mutex_unlock (&priv->lock);
5886 * gst_rtsp_stream_is_receiver:
5887 * @stream: a #GstRTSPStream
5889 * Checks whether the stream is a receiver.
5891 * Returns: %TRUE if the stream is a receiver and %FALSE otherwise.
5896 gst_rtsp_stream_is_receiver (GstRTSPStream * stream)
5898 GstRTSPStreamPrivate *priv;
5899 gboolean ret = FALSE;
5901 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
5903 priv = stream->priv;
5904 g_mutex_lock (&priv->lock);
5905 ret = (priv->sinkpad != NULL);
5906 g_mutex_unlock (&priv->lock);
5911 #define AES_128_KEY_LEN 16
5912 #define AES_256_KEY_LEN 32
5914 #define HMAC_32_KEY_LEN 4
5915 #define HMAC_80_KEY_LEN 10
5918 mikey_apply_policy (GstCaps * caps, GstMIKEYMessage * msg, guint8 policy)
5920 const gchar *srtp_cipher;
5921 const gchar *srtp_auth;
5922 const GstMIKEYPayload *sp;
5925 /* loop over Security policy until we find one containing policy */
5927 if ((sp = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, i)) == NULL)
5930 if (((GstMIKEYPayloadSP *) sp)->policy == policy)
5934 /* the default ciphers */
5935 srtp_cipher = "aes-128-icm";
5936 srtp_auth = "hmac-sha1-80";
5938 /* now override the defaults with what is in the Security Policy */
5941 guint enc_alg = GST_MIKEY_ENC_AES_CM_128;
5943 /* collect all the params and go over them */
5944 len = gst_mikey_payload_sp_get_n_params (sp);
5945 for (i = 0; i < len; i++) {
5946 const GstMIKEYPayloadSPParam *param =
5947 gst_mikey_payload_sp_get_param (sp, i);
5949 switch (param->type) {
5950 case GST_MIKEY_SP_SRTP_ENC_ALG:
5951 enc_alg = param->val[0];
5952 switch (param->val[0]) {
5953 case GST_MIKEY_ENC_NULL:
5954 srtp_cipher = "null";
5956 case GST_MIKEY_ENC_AES_CM_128:
5957 case GST_MIKEY_ENC_AES_KW_128:
5958 srtp_cipher = "aes-128-icm";
5960 case GST_MIKEY_ENC_AES_GCM_128:
5961 srtp_cipher = "aes-128-gcm";
5967 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
5968 switch (param->val[0]) {
5969 case AES_128_KEY_LEN:
5970 if (enc_alg == GST_MIKEY_ENC_AES_CM_128 ||
5971 enc_alg == GST_MIKEY_ENC_AES_KW_128) {
5972 srtp_cipher = "aes-128-icm";
5973 } else if (enc_alg == GST_MIKEY_ENC_AES_GCM_128) {
5974 srtp_cipher = "aes-128-gcm";
5977 case AES_256_KEY_LEN:
5978 if (enc_alg == GST_MIKEY_ENC_AES_CM_128 ||
5979 enc_alg == GST_MIKEY_ENC_AES_KW_128) {
5980 srtp_cipher = "aes-256-icm";
5981 } else if (enc_alg == GST_MIKEY_ENC_AES_GCM_128) {
5982 srtp_cipher = "aes-256-gcm";
5989 case GST_MIKEY_SP_SRTP_AUTH_ALG:
5990 switch (param->val[0]) {
5991 case GST_MIKEY_MAC_NULL:
5994 case GST_MIKEY_MAC_HMAC_SHA_1_160:
5995 srtp_auth = "hmac-sha1-80";
6001 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
6002 switch (param->val[0]) {
6003 case HMAC_32_KEY_LEN:
6004 srtp_auth = "hmac-sha1-32";
6006 case HMAC_80_KEY_LEN:
6007 srtp_auth = "hmac-sha1-80";
6013 case GST_MIKEY_SP_SRTP_SRTP_ENC:
6015 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
6022 /* now configure the SRTP parameters */
6023 gst_caps_set_simple (caps,
6024 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
6025 "srtp-auth", G_TYPE_STRING, srtp_auth,
6026 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
6027 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
6033 handle_mikey_data (GstRTSPStream * stream, guint8 * data, gsize size)
6035 GstMIKEYMessage *msg;
6037 GstCaps *caps = NULL;
6038 GstMIKEYPayloadKEMAC *kemac;
6039 const GstMIKEYPayloadKeyData *pkd;
6042 /* the MIKEY message contains a CSB or crypto session bundle. It is a
6043 * set of Crypto Sessions protected with the same master key.
6044 * In the context of SRTP, an RTP and its RTCP stream is part of a
6046 if ((msg = gst_mikey_message_new_from_data (data, size, NULL, NULL)) == NULL)
6049 /* we can only handle SRTP crypto sessions for now */
6050 if (msg->map_type != GST_MIKEY_MAP_TYPE_SRTP)
6051 goto invalid_map_type;
6053 /* get the number of crypto sessions. This maps SSRC to its
6054 * security parameters */
6055 n_cs = gst_mikey_message_get_n_cs (msg);
6057 goto no_crypto_sessions;
6059 /* we also need keys */
6060 if (!(kemac = (GstMIKEYPayloadKEMAC *) gst_mikey_message_find_payload
6061 (msg, GST_MIKEY_PT_KEMAC, 0)))
6064 /* we don't support encrypted keys */
6065 if (kemac->enc_alg != GST_MIKEY_ENC_NULL
6066 || kemac->mac_alg != GST_MIKEY_MAC_NULL)
6067 goto unsupported_encryption;
6069 /* get Key data sub-payload */
6070 pkd = (const GstMIKEYPayloadKeyData *)
6071 gst_mikey_payload_kemac_get_sub (&kemac->pt, 0);
6073 key = gst_buffer_new_memdup (pkd->key_data, pkd->key_len);
6075 /* go over all crypto sessions and create the security policy for each
6077 for (i = 0; i < n_cs; i++) {
6078 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
6080 caps = gst_caps_new_simple ("application/x-srtp",
6081 "ssrc", G_TYPE_UINT, map->ssrc,
6082 "roc", G_TYPE_UINT, map->roc, "srtp-key", GST_TYPE_BUFFER, key, NULL);
6083 mikey_apply_policy (caps, msg, map->policy);
6085 gst_rtsp_stream_update_crypto (stream, map->ssrc, caps);
6086 gst_caps_unref (caps);
6088 gst_mikey_message_unref (msg);
6089 gst_buffer_unref (key);
6096 GST_DEBUG_OBJECT (stream, "failed to parse MIKEY message");
6101 GST_DEBUG_OBJECT (stream, "invalid map type %d", msg->map_type);
6102 goto cleanup_message;
6106 GST_DEBUG_OBJECT (stream, "no crypto sessions");
6107 goto cleanup_message;
6111 GST_DEBUG_OBJECT (stream, "no keys found");
6112 goto cleanup_message;
6114 unsupported_encryption:
6116 GST_DEBUG_OBJECT (stream, "unsupported key encryption");
6117 goto cleanup_message;
6121 gst_mikey_message_unref (msg);
6126 #define IS_STRIP_CHAR(c) (g_ascii_isspace ((guchar)(c)) || ((c) == '\"'))
6129 strip_chars (gchar * str)
6136 if (!IS_STRIP_CHAR (str[len]))
6140 for (s = str; *s && IS_STRIP_CHAR (*s); s++);
6141 memmove (str, s, len + 1);
6145 * gst_rtsp_stream_handle_keymgmt:
6146 * @stream: a #GstRTSPStream
6147 * @keymgmt: a keymgmt header
6149 * Parse and handle a KeyMgmt header.
6153 /* KeyMgmt = "KeyMgmt" ":" key-mgmt-spec 0*("," key-mgmt-spec)
6154 * key-mgmt-spec = "prot" "=" KMPID ";" ["uri" "=" %x22 URI %x22 ";"]
6157 gst_rtsp_stream_handle_keymgmt (GstRTSPStream * stream, const gchar * keymgmt)
6162 specs = g_strsplit (keymgmt, ",", 0);
6163 for (i = 0; specs[i]; i++) {
6166 split = g_strsplit (specs[i], ";", 0);
6167 for (j = 0; split[j]; j++) {
6168 g_strstrip (split[j]);
6169 if (g_str_has_prefix (split[j], "prot=")) {
6170 g_strstrip (split[j] + 5);
6171 if (!g_str_equal (split[j] + 5, "mikey"))
6173 GST_DEBUG ("found mikey");
6174 } else if (g_str_has_prefix (split[j], "uri=")) {
6175 strip_chars (split[j] + 4);
6176 GST_DEBUG ("found uri '%s'", split[j] + 4);
6177 } else if (g_str_has_prefix (split[j], "data=")) {
6180 strip_chars (split[j] + 5);
6181 GST_DEBUG ("found data '%s'", split[j] + 5);
6182 data = g_base64_decode_inplace (split[j] + 5, &size);
6183 handle_mikey_data (stream, data, size);
6194 * gst_rtsp_stream_get_ulpfec_pt:
6196 * Returns: the payload type used for ULPFEC protection packets
6201 gst_rtsp_stream_get_ulpfec_pt (GstRTSPStream * stream)
6205 g_mutex_lock (&stream->priv->lock);
6206 res = stream->priv->ulpfec_pt;
6207 g_mutex_unlock (&stream->priv->lock);
6213 * gst_rtsp_stream_set_ulpfec_pt:
6215 * Set the payload type to be used for ULPFEC protection packets
6220 gst_rtsp_stream_set_ulpfec_pt (GstRTSPStream * stream, guint pt)
6222 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
6224 g_mutex_lock (&stream->priv->lock);
6225 stream->priv->ulpfec_pt = pt;
6226 if (stream->priv->ulpfec_encoder) {
6227 g_object_set (stream->priv->ulpfec_encoder, "pt", pt, NULL);
6229 g_mutex_unlock (&stream->priv->lock);
6233 * gst_rtsp_stream_request_ulpfec_decoder:
6235 * Creating a rtpulpfecdec element
6237 * Returns: (transfer full) (nullable): a #GstElement.
6242 gst_rtsp_stream_request_ulpfec_decoder (GstRTSPStream * stream,
6243 GstElement * rtpbin, guint sessid)
6245 GObject *internal_storage = NULL;
6247 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
6248 stream->priv->ulpfec_decoder =
6249 gst_object_ref (gst_element_factory_make ("rtpulpfecdec", NULL));
6251 g_signal_emit_by_name (G_OBJECT (rtpbin), "get-internal-storage", sessid,
6253 g_object_set (stream->priv->ulpfec_decoder, "storage", internal_storage,
6255 g_object_unref (internal_storage);
6256 update_ulpfec_decoder_pt (stream);
6258 return stream->priv->ulpfec_decoder;
6262 * gst_rtsp_stream_request_ulpfec_encoder:
6264 * Creating a rtpulpfecenc element
6266 * Returns: (transfer full) (nullable): a #GstElement.
6271 gst_rtsp_stream_request_ulpfec_encoder (GstRTSPStream * stream, guint sessid)
6273 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
6275 if (!stream->priv->ulpfec_percentage)
6278 stream->priv->ulpfec_encoder =
6279 gst_object_ref (gst_element_factory_make ("rtpulpfecenc", NULL));
6281 g_object_set (stream->priv->ulpfec_encoder, "pt", stream->priv->ulpfec_pt,
6282 "percentage", stream->priv->ulpfec_percentage, NULL);
6284 return stream->priv->ulpfec_encoder;
6288 * gst_rtsp_stream_set_ulpfec_percentage:
6290 * Sets the amount of redundancy to apply when creating ULPFEC
6291 * protection packets.
6296 gst_rtsp_stream_set_ulpfec_percentage (GstRTSPStream * stream, guint percentage)
6298 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
6300 g_mutex_lock (&stream->priv->lock);
6301 stream->priv->ulpfec_percentage = percentage;
6302 if (stream->priv->ulpfec_encoder) {
6303 g_object_set (stream->priv->ulpfec_encoder, "percentage", percentage, NULL);
6305 g_mutex_unlock (&stream->priv->lock);
6309 * gst_rtsp_stream_get_ulpfec_percentage:
6311 * Returns: the amount of redundancy applied when creating ULPFEC
6312 * protection packets.
6317 gst_rtsp_stream_get_ulpfec_percentage (GstRTSPStream * stream)
6321 g_mutex_lock (&stream->priv->lock);
6322 res = stream->priv->ulpfec_percentage;
6323 g_mutex_unlock (&stream->priv->lock);
6329 * gst_rtsp_stream_set_rate_control:
6331 * Define whether @stream will follow the Rate-Control=no behaviour as specified
6332 * in the ONVIF replay spec.
6337 gst_rtsp_stream_set_rate_control (GstRTSPStream * stream, gboolean enabled)
6339 GST_DEBUG_OBJECT (stream, "%s rate control",
6340 enabled ? "Enabling" : "Disabling");
6342 g_mutex_lock (&stream->priv->lock);
6343 stream->priv->do_rate_control = enabled;
6344 if (stream->priv->appsink[0])
6345 g_object_set (stream->priv->appsink[0], "sync", enabled, NULL);
6346 if (stream->priv->payloader
6347 && g_object_class_find_property (G_OBJECT_GET_CLASS (stream->
6348 priv->payloader), "onvif-no-rate-control"))
6349 g_object_set (stream->priv->payloader, "onvif-no-rate-control", !enabled,
6351 if (stream->priv->session) {
6352 g_object_set (stream->priv->session, "disable-sr-timestamp", !enabled,
6355 g_mutex_unlock (&stream->priv->lock);
6359 * gst_rtsp_stream_get_rate_control:
6361 * Returns: whether @stream will follow the Rate-Control=no behaviour as specified
6362 * in the ONVIF replay spec.
6367 gst_rtsp_stream_get_rate_control (GstRTSPStream * stream)
6371 g_mutex_lock (&stream->priv->lock);
6372 ret = stream->priv->do_rate_control;
6373 g_mutex_unlock (&stream->priv->lock);
6379 * gst_rtsp_stream_unblock_rtcp:
6381 * Remove blocking probe from the RTCP source. When creating an UDP source for
6382 * RTCP it is initially blocked until this function is called.
6383 * This functions should be called once the pipeline is ready for handling RTCP
6389 gst_rtsp_stream_unblock_rtcp (GstRTSPStream * stream)
6391 GstRTSPStreamPrivate *priv;
6393 priv = stream->priv;
6394 g_mutex_lock (&priv->lock);
6395 if (priv->block_early_rtcp_probe != 0) {
6396 gst_pad_remove_probe
6397 (priv->block_early_rtcp_pad, priv->block_early_rtcp_probe);
6398 priv->block_early_rtcp_probe = 0;
6399 gst_object_unref (priv->block_early_rtcp_pad);
6400 priv->block_early_rtcp_pad = NULL;
6402 if (priv->block_early_rtcp_probe_ipv6 != 0) {
6403 gst_pad_remove_probe
6404 (priv->block_early_rtcp_pad_ipv6, priv->block_early_rtcp_probe_ipv6);
6405 priv->block_early_rtcp_probe_ipv6 = 0;
6406 gst_object_unref (priv->block_early_rtcp_pad_ipv6);
6407 priv->block_early_rtcp_pad_ipv6 = NULL;
6409 g_mutex_unlock (&priv->lock);