2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
25 #include <gst/app/gstappsrc.h>
26 #include <gst/app/gstappsink.h>
28 #include "rtsp-stream.h"
30 #define GST_RTSP_STREAM_GET_PRIVATE(obj) \
31 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
33 struct _GstRTSPStreamPrivate
38 GstElement *payloader;
43 /* pads on the rtpbin */
44 GstPad *send_rtp_sink;
48 /* the RTPSession object */
51 /* sinks used for sending and receiving RTP and RTCP, they share
53 GstElement *udpsrc[2];
54 GstElement *udpsink[2];
55 /* for TCP transport */
56 GstElement *appsrc[2];
57 GstElement *appqueue[2];
58 GstElement *appsink[2];
61 GstElement *funnel[2];
63 /* server ports for sending/receiving */
64 GstRTSPRange server_port;
65 GstRTSPAddress *server_addr;
67 /* multicast addresses */
68 GstRTSPAddressPool *pool;
71 /* the caps of the stream */
75 /* transports we stream to */
87 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
88 #define GST_CAT_DEFAULT rtsp_stream_debug
90 static GQuark ssrc_stream_map_key;
92 static void gst_rtsp_stream_finalize (GObject * obj);
94 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
97 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
99 GObjectClass *gobject_class;
101 g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
103 gobject_class = G_OBJECT_CLASS (klass);
105 gobject_class->finalize = gst_rtsp_stream_finalize;
107 GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
109 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
113 gst_rtsp_stream_init (GstRTSPStream * stream)
115 GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
117 GST_DEBUG ("new stream %p", stream);
121 g_mutex_init (&priv->lock);
125 gst_rtsp_stream_finalize (GObject * obj)
127 GstRTSPStream *stream;
128 GstRTSPStreamPrivate *priv;
130 stream = GST_RTSP_STREAM (obj);
133 GST_DEBUG ("finalize stream %p", stream);
135 /* we really need to be unjoined now */
136 g_return_if_fail (!priv->is_joined);
139 gst_rtsp_address_free (priv->addr);
140 if (priv->server_addr)
141 gst_rtsp_address_free (priv->server_addr);
143 g_object_unref (priv->pool);
144 gst_object_unref (priv->payloader);
145 gst_object_unref (priv->srcpad);
146 g_mutex_clear (&priv->lock);
148 G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
152 * gst_rtsp_stream_new:
155 * @payloader: a #GstElement
157 * Create a new media stream with index @idx that handles RTP data on
158 * @srcpad and has a payloader element @payloader.
160 * Returns: a new #GstRTSPStream
163 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
165 GstRTSPStreamPrivate *priv;
166 GstRTSPStream *stream;
168 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
169 g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
170 g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
172 stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
175 priv->payloader = gst_object_ref (payloader);
176 priv->srcpad = gst_object_ref (srcpad);
182 * gst_rtsp_stream_get_index:
183 * @stream: a #GstRTSPStream
185 * Get the stream index.
187 * Return: the stream index.
190 gst_rtsp_stream_get_index (GstRTSPStream * stream)
192 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
194 return stream->priv->idx;
198 * gst_rtsp_stream_set_mtu:
199 * @stream: a #GstRTSPStream
202 * Configure the mtu in the payloader of @stream to @mtu.
205 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
207 GstRTSPStreamPrivate *priv;
209 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
213 GST_LOG_OBJECT (stream, "set MTU %u", mtu);
215 g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
219 * gst_rtsp_stream_get_mtu:
220 * @stream: a #GstRTSPStream
222 * Get the configured MTU in the payloader of @stream.
224 * Returns: the MTU of the payloader.
227 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
229 GstRTSPStreamPrivate *priv;
232 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
236 g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
242 * gst_rtsp_stream_set_address_pool:
243 * @stream: a #GstRTSPStream
244 * @pool: a #GstRTSPAddressPool
246 * configure @pool to be used as the address pool of @stream.
249 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
250 GstRTSPAddressPool * pool)
252 GstRTSPStreamPrivate *priv;
253 GstRTSPAddressPool *old;
255 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
259 GST_LOG_OBJECT (stream, "set address pool %p", pool);
261 g_mutex_lock (&priv->lock);
262 if ((old = priv->pool) != pool)
263 priv->pool = pool ? g_object_ref (pool) : NULL;
266 g_mutex_unlock (&priv->lock);
269 g_object_unref (old);
273 * gst_rtsp_stream_get_address_pool:
274 * @stream: a #GstRTSPStream
276 * Get the #GstRTSPAddressPool used as the address pool of @stream.
278 * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
282 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
284 GstRTSPStreamPrivate *priv;
285 GstRTSPAddressPool *result;
287 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
291 g_mutex_lock (&priv->lock);
292 if ((result = priv->pool))
293 g_object_ref (result);
294 g_mutex_unlock (&priv->lock);
300 * gst_rtsp_stream_get_address:
301 * @stream: a #GstRTSPStream
303 * Get the multicast address of @stream.
305 * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
306 * allocated. gst_rtsp_address_free() after usage.
309 gst_rtsp_stream_get_address (GstRTSPStream * stream)
311 GstRTSPStreamPrivate *priv;
312 GstRTSPAddress *result;
314 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
318 g_mutex_lock (&priv->lock);
319 if (priv->addr == NULL) {
320 if (priv->pool == NULL)
323 priv->addr = gst_rtsp_address_pool_acquire_address (priv->pool,
324 GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST, 2);
325 if (priv->addr == NULL)
328 result = gst_rtsp_address_copy (priv->addr);
329 g_mutex_unlock (&priv->lock);
336 GST_ERROR_OBJECT (stream, "no address pool specified");
337 g_mutex_unlock (&priv->lock);
342 GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
343 g_mutex_unlock (&priv->lock);
349 * gst_rtsp_stream_reserve_address:
350 * @stream: a #GstRTSPStream
352 * Get a specific multicast address of @stream.
354 * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
355 * allocated. gst_rtsp_address_free() after usage.
358 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
359 const gchar * address, guint port, guint n_ports, guint ttl)
361 GstRTSPStreamPrivate *priv;
362 GstRTSPAddress *result;
364 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
365 g_return_val_if_fail (address != NULL, NULL);
366 g_return_val_if_fail (port > 0, NULL);
367 g_return_val_if_fail (n_ports > 0, NULL);
368 g_return_val_if_fail (ttl > 0, NULL);
372 g_mutex_lock (&priv->lock);
373 if (priv->addr == NULL) {
374 if (priv->pool == NULL)
377 priv->addr = gst_rtsp_address_pool_reserve_address (priv->pool, address,
379 if (priv->addr == NULL)
382 if (strcmp (priv->addr->address, address) ||
383 priv->addr->port != port || priv->addr->n_ports != n_ports ||
384 priv->addr->ttl != ttl)
385 goto different_address;
387 result = gst_rtsp_address_copy (priv->addr);
388 g_mutex_unlock (&priv->lock);
395 GST_ERROR_OBJECT (stream, "no address pool specified");
396 g_mutex_unlock (&priv->lock);
401 GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
403 g_mutex_unlock (&priv->lock);
408 GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
409 " reserved", address);
410 g_mutex_unlock (&priv->lock);
415 /* must be called with lock */
417 alloc_ports (GstRTSPStream * stream)
419 GstRTSPStreamPrivate *priv = stream->priv;
420 GstStateChangeReturn ret;
421 GstElement *udpsrc0, *udpsrc1;
422 GstElement *udpsink0, *udpsink1;
423 GSocket *rtp_socket = NULL;
424 GSocket *rtcp_socket;
425 gint tmp_rtp, tmp_rtcp;
427 gint rtpport, rtcpport;
428 GList *rejected_addresses = NULL;
429 GstRTSPAddress *addr = NULL;
430 GSocketFamily family;
431 GInetAddress *inetaddr = NULL;
432 GSocketAddress *rtp_sockaddr = NULL;
433 GSocketAddress *rtcp_sockaddr = NULL;
441 /* Start with random port */
445 family = G_SOCKET_FAMILY_IPV6;
447 family = G_SOCKET_FAMILY_IPV4;
450 rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
451 G_SOCKET_PROTOCOL_UDP, NULL);
453 goto no_udp_protocol;
455 if (priv->server_addr)
456 gst_rtsp_address_free (priv->server_addr);
458 /* try to allocate 2 UDP ports, the RTP port should be an even
459 * number and the RTCP port should be the next (uneven) port */
462 if (rtp_socket == NULL) {
463 rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
464 G_SOCKET_PROTOCOL_UDP, NULL);
466 goto no_udp_protocol;
469 if (priv->pool && gst_rtsp_address_pool_has_unicast_addresses (priv->pool)) {
470 GstRTSPAddressFlags flags;
473 rejected_addresses = g_list_prepend (rejected_addresses, addr);
475 flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
477 flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
479 flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
481 addr = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
486 tmp_rtp = addr->port;
488 g_clear_object (&inetaddr);
489 inetaddr = g_inet_address_new_from_string (addr->address);
497 if (inetaddr == NULL)
498 inetaddr = g_inet_address_new_any (family);
501 rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
502 if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
503 g_object_unref (rtp_sockaddr);
506 g_object_unref (rtp_sockaddr);
508 rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
509 if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
510 g_clear_object (&rtp_sockaddr);
515 g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
516 g_object_unref (rtp_sockaddr);
518 /* check if port is even */
519 if ((tmp_rtp & 1) != 0) {
520 /* port not even, close and allocate another */
522 g_clear_object (&rtp_socket);
527 tmp_rtcp = tmp_rtp + 1;
529 rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
530 if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
531 g_object_unref (rtcp_sockaddr);
532 g_clear_object (&rtp_socket);
535 g_object_unref (rtcp_sockaddr);
537 g_clear_object (&inetaddr);
539 udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
540 udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
542 if (udpsrc0 == NULL || udpsrc1 == NULL)
543 goto no_udp_protocol;
545 g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
546 g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
548 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
549 if (ret == GST_STATE_CHANGE_FAILURE)
551 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
552 if (ret == GST_STATE_CHANGE_FAILURE)
555 /* all fine, do port check */
556 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
557 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
559 /* this should not happen... */
560 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
563 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
565 goto no_udp_protocol;
567 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
568 g_object_set (G_OBJECT (udpsink0), "socket", rtp_socket, NULL);
570 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
572 goto no_udp_protocol;
574 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
575 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
576 g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
578 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
579 g_object_set (G_OBJECT (udpsink1), "socket", rtcp_socket, NULL);
580 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
581 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
582 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
583 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
584 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
585 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
587 /* we keep these elements, we will further configure them when the
588 * client told us to really use the UDP ports. */
589 priv->udpsrc[0] = udpsrc0;
590 priv->udpsrc[1] = udpsrc1;
591 priv->udpsink[0] = udpsink0;
592 priv->udpsink[1] = udpsink1;
593 priv->server_port.min = rtpport;
594 priv->server_port.max = rtcpport;
596 priv->server_addr = addr;
597 g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
599 g_object_unref (rtp_socket);
600 g_object_unref (rtcp_socket);
628 gst_element_set_state (udpsrc0, GST_STATE_NULL);
629 gst_object_unref (udpsrc0);
632 gst_element_set_state (udpsrc1, GST_STATE_NULL);
633 gst_object_unref (udpsrc1);
636 gst_element_set_state (udpsink0, GST_STATE_NULL);
637 gst_object_unref (udpsink0);
640 gst_element_set_state (udpsink1, GST_STATE_NULL);
641 gst_object_unref (udpsink1);
644 g_object_unref (inetaddr);
645 g_list_free_full (rejected_addresses,
646 (GDestroyNotify) gst_rtsp_address_free);
648 gst_rtsp_address_free (addr);
650 g_object_unref (rtp_socket);
652 g_object_unref (rtcp_socket);
658 * gst_rtsp_stream_get_server_port:
659 * @stream: a #GstRTSPStream
660 * @server_port: (out): result server port
662 * Fill @server_port with the port pair used by the server. This function can
663 * only be called when @stream has been joined.
666 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
667 GstRTSPRange * server_port)
669 GstRTSPStreamPrivate *priv;
671 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
673 g_return_if_fail (priv->is_joined);
675 g_mutex_lock (&priv->lock);
677 *server_port = priv->server_port;
678 g_mutex_unlock (&priv->lock);
682 * gst_rtsp_stream_get_ssrc:
683 * @stream: a #GstRTSPStream
684 * @ssrc: (out): result ssrc
686 * Get the SSRC used by the RTP session of this stream. This function can only
687 * be called when @stream has been joined.
690 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
692 GstRTSPStreamPrivate *priv;
694 g_return_if_fail (GST_IS_RTSP_STREAM (stream));
696 g_return_if_fail (priv->is_joined);
698 g_mutex_lock (&priv->lock);
699 if (ssrc && priv->session)
700 g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
701 g_mutex_unlock (&priv->lock);
704 /* executed from streaming thread */
706 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
708 GstRTSPStreamPrivate *priv = stream->priv;
709 GstCaps *newcaps, *oldcaps;
711 newcaps = gst_pad_get_current_caps (pad);
713 GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
716 g_mutex_lock (&priv->lock);
717 oldcaps = priv->caps;
718 priv->caps = newcaps;
719 g_mutex_unlock (&priv->lock);
722 gst_caps_unref (oldcaps);
726 dump_structure (const GstStructure * s)
730 sstr = gst_structure_to_string (s);
731 GST_INFO ("structure: %s", sstr);
735 static GstRTSPStreamTransport *
736 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
738 GstRTSPStreamPrivate *priv = stream->priv;
740 GstRTSPStreamTransport *result = NULL;
745 if (rtcp_from == NULL)
748 tmp = g_strrstr (rtcp_from, ":");
752 port = atoi (tmp + 1);
753 dest = g_strndup (rtcp_from, tmp - rtcp_from);
755 g_mutex_lock (&priv->lock);
756 GST_INFO ("finding %s:%d in %d transports", dest, port,
757 g_list_length (priv->transports));
759 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
760 GstRTSPStreamTransport *trans = walk->data;
761 const GstRTSPTransport *tr;
764 tr = gst_rtsp_stream_transport_get_transport (trans);
766 min = tr->client_port.min;
767 max = tr->client_port.max;
769 if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
774 g_mutex_unlock (&priv->lock);
781 static GstRTSPStreamTransport *
782 check_transport (GObject * source, GstRTSPStream * stream)
785 GstRTSPStreamTransport *trans;
787 /* see if we have a stream to match with the origin of the RTCP packet */
788 trans = g_object_get_qdata (source, ssrc_stream_map_key);
790 g_object_get (source, "stats", &stats, NULL);
792 const gchar *rtcp_from;
794 dump_structure (stats);
796 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
797 if ((trans = find_transport (stream, rtcp_from))) {
798 GST_INFO ("%p: found transport %p for source %p", stream, trans,
800 g_object_set_qdata (source, ssrc_stream_map_key, trans);
802 gst_structure_free (stats);
810 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
812 GstRTSPStreamTransport *trans;
814 GST_INFO ("%p: new source %p", stream, source);
816 trans = check_transport (source, stream);
819 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
823 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
825 GST_INFO ("%p: new SDES %p", stream, source);
829 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
831 GstRTSPStreamTransport *trans;
833 trans = check_transport (source, stream);
836 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
837 gst_rtsp_stream_transport_keep_alive (trans);
842 g_object_get (source, "stats", &stats, NULL);
844 dump_structure (stats);
845 gst_structure_free (stats);
852 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
854 GST_INFO ("%p: source %p bye", stream, source);
858 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
860 GstRTSPStreamTransport *trans;
862 GST_INFO ("%p: source %p bye timeout", stream, source);
864 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
865 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
870 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
872 GstRTSPStreamTransport *trans;
874 GST_INFO ("%p: source %p timeout", stream, source);
876 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
877 gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
882 handle_new_sample (GstAppSink * sink, gpointer user_data)
884 GstRTSPStreamPrivate *priv;
888 GstRTSPStream *stream;
890 sample = gst_app_sink_pull_sample (sink);
894 stream = (GstRTSPStream *) user_data;
896 buffer = gst_sample_get_buffer (sample);
898 g_mutex_lock (&priv->lock);
899 for (walk = priv->transports; walk; walk = g_list_next (walk)) {
900 GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
902 if (GST_ELEMENT_CAST (sink) == priv->appsink[0]) {
903 gst_rtsp_stream_transport_send_rtp (tr, buffer);
905 gst_rtsp_stream_transport_send_rtcp (tr, buffer);
908 g_mutex_unlock (&priv->lock);
910 gst_sample_unref (sample);
915 static GstAppSinkCallbacks sink_cb = {
916 NULL, /* not interested in EOS */
917 NULL, /* not interested in preroll samples */
922 * gst_rtsp_stream_join_bin:
923 * @stream: a #GstRTSPStream
924 * @bin: a #GstBin to join
925 * @rtpbin: a rtpbin element in @bin
926 * @state: the target state of the new elements
928 * Join the #Gstbin @bin that contains the element @rtpbin.
930 * @stream will link to @rtpbin, which must be inside @bin. The elements
931 * added to @bin will be set to the state given in @state.
933 * Returns: %TRUE on success.
936 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
937 GstElement * rtpbin, GstState state)
939 GstRTSPStreamPrivate *priv;
942 GstPad *pad, *teepad, *queuepad, *selpad;
943 GstPadLinkReturn ret;
945 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
946 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
947 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
951 g_mutex_lock (&priv->lock);
955 /* create a session with the same index as the stream */
958 GST_INFO ("stream %p joining bin as session %d", stream, idx);
960 if (!alloc_ports (stream))
963 /* get a pad for sending RTP */
964 name = g_strdup_printf ("send_rtp_sink_%u", idx);
965 priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
967 /* link the RTP pad to the session manager, it should not really fail unless
968 * this is not really an RTP pad */
969 ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
970 if (ret != GST_PAD_LINK_OK)
973 /* get pads from the RTP session element for sending and receiving
975 name = g_strdup_printf ("send_rtp_src_%u", idx);
976 priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
978 name = g_strdup_printf ("send_rtcp_src_%u", idx);
979 priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
981 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
982 priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
984 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
985 priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
988 /* get the session */
989 g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
991 g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
993 g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
995 g_signal_connect (priv->session, "on-ssrc-active",
996 (GCallback) on_ssrc_active, stream);
997 g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
999 g_signal_connect (priv->session, "on-bye-timeout",
1000 (GCallback) on_bye_timeout, stream);
1001 g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1004 for (i = 0; i < 2; i++) {
1005 /* For the sender we create this bit of pipeline for both
1006 * RTP and RTCP. Sync and preroll are enabled on udpsink so
1007 * we need to add a queue before appsink to make the pipeline
1008 * not block. For the TCP case, we want to pump data to the
1009 * client as fast as possible anyway.
1011 * .--------. .-----. .---------.
1012 * | rtpbin | | tee | | udpsink |
1013 * | send->sink src->sink |
1014 * '--------' | | '---------'
1015 * | | .---------. .---------.
1016 * | | | queue | | appsink |
1017 * | src->sink src->sink |
1018 * '-----' '---------' '---------'
1020 /* make tee for RTP/RTCP */
1021 priv->tee[i] = gst_element_factory_make ("tee", NULL);
1022 gst_bin_add (bin, priv->tee[i]);
1024 /* and link to rtpbin send pad */
1025 pad = gst_element_get_static_pad (priv->tee[i], "sink");
1026 gst_pad_link (priv->send_src[i], pad);
1027 gst_object_unref (pad);
1030 gst_bin_add (bin, priv->udpsink[i]);
1032 /* link tee to udpsink */
1033 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1034 pad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1035 gst_pad_link (teepad, pad);
1036 gst_object_unref (pad);
1037 gst_object_unref (teepad);
1040 priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1041 gst_bin_add (bin, priv->appqueue[i]);
1042 /* and link to tee */
1043 teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1044 pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1045 gst_pad_link (teepad, pad);
1046 gst_object_unref (pad);
1047 gst_object_unref (teepad);
1050 priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1051 g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1052 g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1053 gst_bin_add (bin, priv->appsink[i]);
1054 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1055 &sink_cb, stream, NULL);
1056 /* and link to queue */
1057 queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1058 pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1059 gst_pad_link (queuepad, pad);
1060 gst_object_unref (pad);
1061 gst_object_unref (queuepad);
1063 /* For the receiver we create this bit of pipeline for both
1064 * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1065 * and it is all funneled into the rtpbin receive pad.
1067 * .--------. .--------. .--------.
1068 * | udpsrc | | funnel | | rtpbin |
1069 * | src->sink src->sink |
1070 * '--------' | | '--------'
1074 * '--------' '--------'
1076 /* make funnel for the RTP/RTCP receivers */
1077 priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1078 gst_bin_add (bin, priv->funnel[i]);
1080 pad = gst_element_get_static_pad (priv->funnel[i], "src");
1081 gst_pad_link (pad, priv->recv_sink[i]);
1082 gst_object_unref (pad);
1084 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1086 gst_element_set_state (priv->udpsrc[i], GST_STATE_PLAYING);
1087 gst_element_set_locked_state (priv->udpsrc[i], TRUE);
1089 gst_bin_add (bin, priv->udpsrc[i]);
1090 /* and link to the funnel */
1091 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1092 pad = gst_element_get_static_pad (priv->udpsrc[i], "src");
1093 gst_pad_link (pad, selpad);
1094 gst_object_unref (pad);
1095 gst_object_unref (selpad);
1097 /* make and add appsrc */
1098 priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1099 gst_bin_add (bin, priv->appsrc[i]);
1100 /* and link to the funnel */
1101 selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1102 pad = gst_element_get_static_pad (priv->appsrc[i], "src");
1103 gst_pad_link (pad, selpad);
1104 gst_object_unref (pad);
1105 gst_object_unref (selpad);
1107 /* check if we need to set to a special state */
1108 if (state != GST_STATE_NULL) {
1109 gst_element_set_state (priv->udpsink[i], state);
1110 gst_element_set_state (priv->appsink[i], state);
1111 gst_element_set_state (priv->appqueue[i], state);
1112 gst_element_set_state (priv->tee[i], state);
1113 gst_element_set_state (priv->funnel[i], state);
1114 gst_element_set_state (priv->appsrc[i], state);
1118 /* be notified of caps changes */
1119 priv->caps_sig = g_signal_connect (priv->send_rtp_sink, "notify::caps",
1120 (GCallback) caps_notify, stream);
1122 priv->is_joined = TRUE;
1123 g_mutex_unlock (&priv->lock);
1130 g_mutex_unlock (&priv->lock);
1135 g_mutex_unlock (&priv->lock);
1136 GST_WARNING ("failed to allocate ports %d", idx);
1141 GST_WARNING ("failed to link stream %d", idx);
1142 gst_object_unref (priv->send_rtp_sink);
1143 priv->send_rtp_sink = NULL;
1144 g_mutex_unlock (&priv->lock);
1150 * gst_rtsp_stream_leave_bin:
1151 * @stream: a #GstRTSPStream
1153 * @rtpbin: a rtpbin #GstElement
1155 * Remove the elements of @stream from @bin. @bin must be set
1156 * to the NULL state before calling this.
1158 * Return: %TRUE on success.
1161 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1162 GstElement * rtpbin)
1164 GstRTSPStreamPrivate *priv;
1167 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1168 g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1169 g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1171 priv = stream->priv;
1173 g_mutex_lock (&priv->lock);
1174 if (!priv->is_joined)
1175 goto was_not_joined;
1177 /* all transports must be removed by now */
1178 g_return_val_if_fail (priv->transports == NULL, FALSE);
1180 GST_INFO ("stream %p leaving bin", stream);
1182 gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
1183 g_signal_handler_disconnect (priv->send_rtp_sink, priv->caps_sig);
1184 gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
1185 gst_object_unref (priv->send_rtp_sink);
1186 priv->send_rtp_sink = NULL;
1188 for (i = 0; i < 2; i++) {
1189 /* and set udpsrc to NULL now before removing */
1190 gst_element_set_locked_state (priv->udpsrc[i], FALSE);
1191 gst_element_set_state (priv->udpsrc[i], GST_STATE_NULL);
1193 /* removing them should also nicely release the request
1194 * pads when they finalize */
1195 gst_bin_remove (bin, priv->udpsrc[i]);
1196 gst_bin_remove (bin, priv->udpsink[i]);
1197 gst_bin_remove (bin, priv->appsrc[i]);
1198 gst_bin_remove (bin, priv->appsink[i]);
1199 gst_bin_remove (bin, priv->appqueue[i]);
1200 gst_bin_remove (bin, priv->tee[i]);
1201 gst_bin_remove (bin, priv->funnel[i]);
1203 gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
1204 gst_object_unref (priv->recv_sink[i]);
1205 priv->recv_sink[i] = NULL;
1207 priv->udpsrc[i] = NULL;
1208 priv->udpsink[i] = NULL;
1209 priv->appsrc[i] = NULL;
1210 priv->appsink[i] = NULL;
1211 priv->appqueue[i] = NULL;
1212 priv->tee[i] = NULL;
1213 priv->funnel[i] = NULL;
1215 gst_object_unref (priv->send_src[0]);
1216 priv->send_src[0] = NULL;
1218 gst_element_release_request_pad (rtpbin, priv->send_src[1]);
1219 gst_object_unref (priv->send_src[1]);
1220 priv->send_src[1] = NULL;
1222 g_object_unref (priv->session);
1224 gst_caps_unref (priv->caps);
1226 priv->is_joined = FALSE;
1227 g_mutex_unlock (&priv->lock);
1238 * gst_rtsp_stream_get_rtpinfo:
1239 * @stream: a #GstRTSPStream
1240 * @rtptime: result RTP timestamp
1241 * @seq: result RTP seqnum
1243 * Retrieve the current rtptime and seq. This is used to
1244 * construct a RTPInfo reply header.
1246 * Returns: %TRUE when rtptime and seq could be determined.
1249 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
1250 guint * rtptime, guint * seq)
1252 GstRTSPStreamPrivate *priv;
1253 GObjectClass *payobjclass;
1255 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1256 g_return_val_if_fail (rtptime != NULL, FALSE);
1257 g_return_val_if_fail (seq != NULL, FALSE);
1259 priv = stream->priv;
1261 payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
1263 if (!g_object_class_find_property (payobjclass, "seqnum") ||
1264 !g_object_class_find_property (payobjclass, "timestamp"))
1267 g_object_get (priv->payloader, "seqnum", seq, "timestamp", rtptime, NULL);
1273 * gst_rtsp_stream_get_caps:
1274 * @stream: a #GstRTSPStream
1276 * Retrieve the current caps of @stream.
1278 * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
1282 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
1284 GstRTSPStreamPrivate *priv;
1287 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1289 priv = stream->priv;
1291 g_mutex_lock (&priv->lock);
1292 if ((result = priv->caps))
1293 gst_caps_ref (result);
1294 g_mutex_unlock (&priv->lock);
1300 * gst_rtsp_stream_recv_rtp:
1301 * @stream: a #GstRTSPStream
1302 * @buffer: (transfer full): a #GstBuffer
1304 * Handle an RTP buffer for the stream. This method is usually called when a
1305 * message has been received from a client using the TCP transport.
1307 * This function takes ownership of @buffer.
1309 * Returns: a GstFlowReturn.
1312 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
1314 GstRTSPStreamPrivate *priv;
1316 GstElement *element;
1318 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1319 priv = stream->priv;
1320 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1321 g_return_val_if_fail (priv->is_joined, FALSE);
1323 g_mutex_lock (&priv->lock);
1324 element = gst_object_ref (priv->appsrc[0]);
1325 g_mutex_unlock (&priv->lock);
1327 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1329 gst_object_unref (element);
1335 * gst_rtsp_stream_recv_rtcp:
1336 * @stream: a #GstRTSPStream
1337 * @buffer: (transfer full): a #GstBuffer
1339 * Handle an RTCP buffer for the stream. This method is usually called when a
1340 * message has been received from a client using the TCP transport.
1342 * This function takes ownership of @buffer.
1344 * Returns: a GstFlowReturn.
1347 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
1349 GstRTSPStreamPrivate *priv;
1351 GstElement *element;
1353 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1354 priv = stream->priv;
1355 g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1356 g_return_val_if_fail (priv->is_joined, FALSE);
1358 g_mutex_lock (&priv->lock);
1359 element = gst_object_ref (priv->appsrc[1]);
1360 g_mutex_unlock (&priv->lock);
1362 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1364 gst_object_unref (element);
1369 /* must be called with lock */
1371 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
1374 GstRTSPStreamPrivate *priv = stream->priv;
1375 const GstRTSPTransport *tr;
1377 tr = gst_rtsp_stream_transport_get_transport (trans);
1379 switch (tr->lower_transport) {
1380 case GST_RTSP_LOWER_TRANS_UDP:
1381 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1387 dest = tr->destination;
1388 if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1393 min = tr->client_port.min;
1394 max = tr->client_port.max;
1398 GST_INFO ("adding %s:%d-%d", dest, min, max);
1399 g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
1400 g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
1402 GST_INFO ("setting ttl-mc %d", ttl);
1403 g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
1404 g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
1406 priv->transports = g_list_prepend (priv->transports, trans);
1408 GST_INFO ("removing %s:%d-%d", dest, min, max);
1409 g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
1410 g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
1411 priv->transports = g_list_remove (priv->transports, trans);
1415 case GST_RTSP_LOWER_TRANS_TCP:
1417 GST_INFO ("adding TCP %s", tr->destination);
1418 priv->transports = g_list_prepend (priv->transports, trans);
1420 GST_INFO ("removing TCP %s", tr->destination);
1421 priv->transports = g_list_remove (priv->transports, trans);
1425 goto unknown_transport;
1432 GST_INFO ("Unknown transport %d", tr->lower_transport);
1439 * gst_rtsp_stream_add_transport:
1440 * @stream: a #GstRTSPStream
1441 * @trans: a #GstRTSPStreamTransport
1443 * Add the transport in @trans to @stream. The media of @stream will
1444 * then also be send to the values configured in @trans.
1446 * @stream must be joined to a bin.
1448 * @trans must contain a valid #GstRTSPTransport.
1450 * Returns: %TRUE if @trans was added
1453 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
1454 GstRTSPStreamTransport * trans)
1456 GstRTSPStreamPrivate *priv;
1459 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1460 priv = stream->priv;
1461 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1462 g_return_val_if_fail (priv->is_joined, FALSE);
1464 g_mutex_lock (&priv->lock);
1465 res = update_transport (stream, trans, TRUE);
1466 g_mutex_unlock (&priv->lock);
1472 * gst_rtsp_stream_remove_transport:
1473 * @stream: a #GstRTSPStream
1474 * @trans: a #GstRTSPStreamTransport
1476 * Remove the transport in @trans from @stream. The media of @stream will
1477 * not be sent to the values configured in @trans.
1479 * @stream must be joined to a bin.
1481 * @trans must contain a valid #GstRTSPTransport.
1483 * Returns: %TRUE if @trans was removed
1486 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
1487 GstRTSPStreamTransport * trans)
1489 GstRTSPStreamPrivate *priv;
1492 g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1493 priv = stream->priv;
1494 g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1495 g_return_val_if_fail (priv->is_joined, FALSE);
1497 g_mutex_lock (&priv->lock);
1498 res = update_transport (stream, trans, FALSE);
1499 g_mutex_unlock (&priv->lock);