stream: clear session and caps for reuse
[platform/upstream/gstreamer.git] / gst / rtsp-server / rtsp-stream.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include <gio/gio.h>
24
25 #include <gst/app/gstappsrc.h>
26 #include <gst/app/gstappsink.h>
27
28 #include "rtsp-stream.h"
29
30 #define GST_RTSP_STREAM_GET_PRIVATE(obj)  \
31      (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
32
33 struct _GstRTSPStreamPrivate
34 {
35   GMutex lock;
36   guint idx;
37   GstPad *srcpad;
38   GstElement *payloader;
39   gboolean is_ipv6;
40   guint buffer_size;
41   gboolean is_joined;
42
43   /* pads on the rtpbin */
44   GstPad *send_rtp_sink;
45   GstPad *recv_sink[2];
46   GstPad *send_src[2];
47
48   /* the RTPSession object */
49   GObject *session;
50
51   /* sinks used for sending and receiving RTP and RTCP, they share
52    * sockets */
53   GstElement *udpsrc[2];
54   GstElement *udpsink[2];
55   /* for TCP transport */
56   GstElement *appsrc[2];
57   GstElement *appqueue[2];
58   GstElement *appsink[2];
59
60   GstElement *tee[2];
61   GstElement *funnel[2];
62
63   /* server ports for sending/receiving */
64   GstRTSPRange server_port;
65   GstRTSPAddress *server_addr;
66
67   /* multicast addresses */
68   GstRTSPAddressPool *pool;
69   GstRTSPAddress *addr;
70
71   /* the caps of the stream */
72   gulong caps_sig;
73   GstCaps *caps;
74
75   /* transports we stream to */
76   guint n_active;
77   GList *transports;
78 };
79
80
81 enum
82 {
83   PROP_0,
84   PROP_LAST
85 };
86
87 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
88 #define GST_CAT_DEFAULT rtsp_stream_debug
89
90 static GQuark ssrc_stream_map_key;
91
92 static void gst_rtsp_stream_finalize (GObject * obj);
93
94 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
95
96 static void
97 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
98 {
99   GObjectClass *gobject_class;
100
101   g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
102
103   gobject_class = G_OBJECT_CLASS (klass);
104
105   gobject_class->finalize = gst_rtsp_stream_finalize;
106
107   GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
108
109   ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
110 }
111
112 static void
113 gst_rtsp_stream_init (GstRTSPStream * stream)
114 {
115   GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
116
117   GST_DEBUG ("new stream %p", stream);
118
119   stream->priv = priv;
120
121   g_mutex_init (&priv->lock);
122 }
123
124 static void
125 gst_rtsp_stream_finalize (GObject * obj)
126 {
127   GstRTSPStream *stream;
128   GstRTSPStreamPrivate *priv;
129
130   stream = GST_RTSP_STREAM (obj);
131   priv = stream->priv;
132
133   GST_DEBUG ("finalize stream %p", stream);
134
135   /* we really need to be unjoined now */
136   g_return_if_fail (!priv->is_joined);
137
138   if (priv->addr)
139     gst_rtsp_address_free (priv->addr);
140   if (priv->server_addr)
141     gst_rtsp_address_free (priv->server_addr);
142   if (priv->pool)
143     g_object_unref (priv->pool);
144   gst_object_unref (priv->payloader);
145   gst_object_unref (priv->srcpad);
146   g_mutex_clear (&priv->lock);
147
148   G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
149 }
150
151 /**
152  * gst_rtsp_stream_new:
153  * @idx: an index
154  * @srcpad: a #GstPad
155  * @payloader: a #GstElement
156  *
157  * Create a new media stream with index @idx that handles RTP data on
158  * @srcpad and has a payloader element @payloader.
159  *
160  * Returns: a new #GstRTSPStream
161  */
162 GstRTSPStream *
163 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
164 {
165   GstRTSPStreamPrivate *priv;
166   GstRTSPStream *stream;
167
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);
171
172   stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
173   priv = stream->priv;
174   priv->idx = idx;
175   priv->payloader = gst_object_ref (payloader);
176   priv->srcpad = gst_object_ref (srcpad);
177
178   return stream;
179 }
180
181 /**
182  * gst_rtsp_stream_get_index:
183  * @stream: a #GstRTSPStream
184  *
185  * Get the stream index.
186  *
187  * Return: the stream index.
188  */
189 guint
190 gst_rtsp_stream_get_index (GstRTSPStream * stream)
191 {
192   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
193
194   return stream->priv->idx;
195 }
196
197 /**
198  * gst_rtsp_stream_set_mtu:
199  * @stream: a #GstRTSPStream
200  * @mtu: a new MTU
201  *
202  * Configure the mtu in the payloader of @stream to @mtu.
203  */
204 void
205 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
206 {
207   GstRTSPStreamPrivate *priv;
208
209   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
210
211   priv = stream->priv;
212
213   GST_LOG_OBJECT (stream, "set MTU %u", mtu);
214
215   g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
216 }
217
218 /**
219  * gst_rtsp_stream_get_mtu:
220  * @stream: a #GstRTSPStream
221  *
222  * Get the configured MTU in the payloader of @stream.
223  *
224  * Returns: the MTU of the payloader.
225  */
226 guint
227 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
228 {
229   GstRTSPStreamPrivate *priv;
230   guint mtu;
231
232   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
233
234   priv = stream->priv;
235
236   g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
237
238   return mtu;
239 }
240
241 /**
242  * gst_rtsp_stream_set_address_pool:
243  * @stream: a #GstRTSPStream
244  * @pool: a #GstRTSPAddressPool
245  *
246  * configure @pool to be used as the address pool of @stream.
247  */
248 void
249 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
250     GstRTSPAddressPool * pool)
251 {
252   GstRTSPStreamPrivate *priv;
253   GstRTSPAddressPool *old;
254
255   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
256
257   priv = stream->priv;
258
259   GST_LOG_OBJECT (stream, "set address pool %p", pool);
260
261   g_mutex_lock (&priv->lock);
262   if ((old = priv->pool) != pool)
263     priv->pool = pool ? g_object_ref (pool) : NULL;
264   else
265     old = NULL;
266   g_mutex_unlock (&priv->lock);
267
268   if (old)
269     g_object_unref (old);
270 }
271
272 /**
273  * gst_rtsp_stream_get_address_pool:
274  * @stream: a #GstRTSPStream
275  *
276  * Get the #GstRTSPAddressPool used as the address pool of @stream.
277  *
278  * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
279  * usage.
280  */
281 GstRTSPAddressPool *
282 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
283 {
284   GstRTSPStreamPrivate *priv;
285   GstRTSPAddressPool *result;
286
287   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
288
289   priv = stream->priv;
290
291   g_mutex_lock (&priv->lock);
292   if ((result = priv->pool))
293     g_object_ref (result);
294   g_mutex_unlock (&priv->lock);
295
296   return result;
297 }
298
299 /**
300  * gst_rtsp_stream_get_address:
301  * @stream: a #GstRTSPStream
302  *
303  * Get the multicast address of @stream.
304  *
305  * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
306  * allocated. gst_rtsp_address_free() after usage.
307  */
308 GstRTSPAddress *
309 gst_rtsp_stream_get_address (GstRTSPStream * stream)
310 {
311   GstRTSPStreamPrivate *priv;
312   GstRTSPAddress *result;
313
314   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
315
316   priv = stream->priv;
317
318   g_mutex_lock (&priv->lock);
319   if (priv->addr == NULL) {
320     if (priv->pool == NULL)
321       goto no_pool;
322
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)
326       goto no_address;
327   }
328   result = gst_rtsp_address_copy (priv->addr);
329   g_mutex_unlock (&priv->lock);
330
331   return result;
332
333   /* ERRORS */
334 no_pool:
335   {
336     GST_ERROR_OBJECT (stream, "no address pool specified");
337     g_mutex_unlock (&priv->lock);
338     return NULL;
339   }
340 no_address:
341   {
342     GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
343     g_mutex_unlock (&priv->lock);
344     return NULL;
345   }
346 }
347
348 /**
349  * gst_rtsp_stream_reserve_address:
350  * @stream: a #GstRTSPStream
351  *
352  * Get a specific multicast address of @stream.
353  *
354  * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
355  * allocated. gst_rtsp_address_free() after usage.
356  */
357 GstRTSPAddress *
358 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
359     const gchar * address, guint port, guint n_ports, guint ttl)
360 {
361   GstRTSPStreamPrivate *priv;
362   GstRTSPAddress *result;
363
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);
369
370   priv = stream->priv;
371
372   g_mutex_lock (&priv->lock);
373   if (priv->addr == NULL) {
374     if (priv->pool == NULL)
375       goto no_pool;
376
377     priv->addr = gst_rtsp_address_pool_reserve_address (priv->pool, address,
378         port, n_ports, ttl);
379     if (priv->addr == NULL)
380       goto no_address;
381   } else {
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;
386   }
387   result = gst_rtsp_address_copy (priv->addr);
388   g_mutex_unlock (&priv->lock);
389
390   return result;
391
392   /* ERRORS */
393 no_pool:
394   {
395     GST_ERROR_OBJECT (stream, "no address pool specified");
396     g_mutex_unlock (&priv->lock);
397     return NULL;
398   }
399 no_address:
400   {
401     GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
402         address);
403     g_mutex_unlock (&priv->lock);
404     return NULL;
405   }
406 different_address:
407   {
408     GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
409         " reserved", address);
410     g_mutex_unlock (&priv->lock);
411     return NULL;
412   }
413 }
414
415 /* must be called with lock */
416 static gboolean
417 alloc_ports (GstRTSPStream * stream)
418 {
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;
426   guint count;
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;
434
435   udpsrc0 = NULL;
436   udpsrc1 = NULL;
437   udpsink0 = NULL;
438   udpsink1 = NULL;
439   count = 0;
440
441   /* Start with random port */
442   tmp_rtp = 0;
443
444   if (priv->is_ipv6) {
445     family = G_SOCKET_FAMILY_IPV6;
446   } else {
447     family = G_SOCKET_FAMILY_IPV4;
448   }
449
450   rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
451       G_SOCKET_PROTOCOL_UDP, NULL);
452   if (!rtcp_socket)
453     goto no_udp_protocol;
454
455   if (priv->server_addr)
456     gst_rtsp_address_free (priv->server_addr);
457
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 */
460 again:
461
462   if (rtp_socket == NULL) {
463     rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
464         G_SOCKET_PROTOCOL_UDP, NULL);
465     if (!rtp_socket)
466       goto no_udp_protocol;
467   }
468
469   if (priv->pool && gst_rtsp_address_pool_has_unicast_addresses (priv->pool)) {
470     GstRTSPAddressFlags flags;
471
472     if (addr)
473       rejected_addresses = g_list_prepend (rejected_addresses, addr);
474
475     flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
476     if (priv->is_ipv6)
477       flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
478     else
479       flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
480
481     addr = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
482
483     if (addr == NULL)
484       goto no_ports;
485
486     tmp_rtp = addr->port;
487
488     g_clear_object (&inetaddr);
489     inetaddr = g_inet_address_new_from_string (addr->address);
490   } else {
491     if (tmp_rtp != 0) {
492       tmp_rtp += 2;
493       if (++count > 20)
494         goto no_ports;
495     }
496
497     if (inetaddr == NULL)
498       inetaddr = g_inet_address_new_any (family);
499   }
500
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);
504     goto again;
505   }
506   g_object_unref (rtp_sockaddr);
507
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);
511     goto socket_error;
512   }
513
514   tmp_rtp =
515       g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
516   g_object_unref (rtp_sockaddr);
517
518   /* check if port is even */
519   if ((tmp_rtp & 1) != 0) {
520     /* port not even, close and allocate another */
521     tmp_rtp++;
522     g_clear_object (&rtp_socket);
523     goto again;
524   }
525
526   /* set port */
527   tmp_rtcp = tmp_rtp + 1;
528
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);
533     goto again;
534   }
535   g_object_unref (rtcp_sockaddr);
536
537   g_clear_object (&inetaddr);
538
539   udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
540   udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
541
542   if (udpsrc0 == NULL || udpsrc1 == NULL)
543     goto no_udp_protocol;
544
545   g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
546   g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
547
548   ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
549   if (ret == GST_STATE_CHANGE_FAILURE)
550     goto element_error;
551   ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
552   if (ret == GST_STATE_CHANGE_FAILURE)
553     goto element_error;
554
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);
558
559   /* this should not happen... */
560   if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
561     goto port_error;
562
563   udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
564   if (!udpsink0)
565     goto no_udp_protocol;
566
567   g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
568   g_object_set (G_OBJECT (udpsink0), "socket", rtp_socket, NULL);
569
570   udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
571   if (!udpsink1)
572     goto no_udp_protocol;
573
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);
577
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);
586
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;
595
596   priv->server_addr = addr;
597   g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
598
599   g_object_unref (rtp_socket);
600   g_object_unref (rtcp_socket);
601
602   return TRUE;
603
604   /* ERRORS */
605 no_udp_protocol:
606   {
607     goto cleanup;
608   }
609 no_ports:
610   {
611     goto cleanup;
612   }
613 port_error:
614   {
615     goto cleanup;
616   }
617 socket_error:
618   {
619     goto cleanup;
620   }
621 element_error:
622   {
623     goto cleanup;
624   }
625 cleanup:
626   {
627     if (udpsrc0) {
628       gst_element_set_state (udpsrc0, GST_STATE_NULL);
629       gst_object_unref (udpsrc0);
630     }
631     if (udpsrc1) {
632       gst_element_set_state (udpsrc1, GST_STATE_NULL);
633       gst_object_unref (udpsrc1);
634     }
635     if (udpsink0) {
636       gst_element_set_state (udpsink0, GST_STATE_NULL);
637       gst_object_unref (udpsink0);
638     }
639     if (udpsink1) {
640       gst_element_set_state (udpsink1, GST_STATE_NULL);
641       gst_object_unref (udpsink1);
642     }
643     if (inetaddr)
644       g_object_unref (inetaddr);
645     g_list_free_full (rejected_addresses,
646         (GDestroyNotify) gst_rtsp_address_free);
647     if (addr)
648       gst_rtsp_address_free (addr);
649     if (rtp_socket)
650       g_object_unref (rtp_socket);
651     if (rtcp_socket)
652       g_object_unref (rtcp_socket);
653     return FALSE;
654   }
655 }
656
657 /**
658  * gst_rtsp_stream_get_server_port:
659  * @stream: a #GstRTSPStream
660  * @server_port: (out): result server port
661  *
662  * Fill @server_port with the port pair used by the server. This function can
663  * only be called when @stream has been joined.
664  */
665 void
666 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
667     GstRTSPRange * server_port)
668 {
669   GstRTSPStreamPrivate *priv;
670
671   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
672   priv = stream->priv;
673   g_return_if_fail (priv->is_joined);
674
675   g_mutex_lock (&priv->lock);
676   if (server_port)
677     *server_port = priv->server_port;
678   g_mutex_unlock (&priv->lock);
679 }
680
681 /**
682  * gst_rtsp_stream_get_ssrc:
683  * @stream: a #GstRTSPStream
684  * @ssrc: (out): result ssrc
685  *
686  * Get the SSRC used by the RTP session of this stream. This function can only
687  * be called when @stream has been joined.
688  */
689 void
690 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
691 {
692   GstRTSPStreamPrivate *priv;
693
694   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
695   priv = stream->priv;
696   g_return_if_fail (priv->is_joined);
697
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);
702 }
703
704 /* executed from streaming thread */
705 static void
706 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
707 {
708   GstRTSPStreamPrivate *priv = stream->priv;
709   GstCaps *newcaps, *oldcaps;
710
711   newcaps = gst_pad_get_current_caps (pad);
712
713   GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
714       newcaps);
715
716   g_mutex_lock (&priv->lock);
717   oldcaps = priv->caps;
718   priv->caps = newcaps;
719   g_mutex_unlock (&priv->lock);
720
721   if (oldcaps)
722     gst_caps_unref (oldcaps);
723 }
724
725 static void
726 dump_structure (const GstStructure * s)
727 {
728   gchar *sstr;
729
730   sstr = gst_structure_to_string (s);
731   GST_INFO ("structure: %s", sstr);
732   g_free (sstr);
733 }
734
735 static GstRTSPStreamTransport *
736 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
737 {
738   GstRTSPStreamPrivate *priv = stream->priv;
739   GList *walk;
740   GstRTSPStreamTransport *result = NULL;
741   const gchar *tmp;
742   gchar *dest;
743   guint port;
744
745   if (rtcp_from == NULL)
746     return NULL;
747
748   tmp = g_strrstr (rtcp_from, ":");
749   if (tmp == NULL)
750     return NULL;
751
752   port = atoi (tmp + 1);
753   dest = g_strndup (rtcp_from, tmp - rtcp_from);
754
755   g_mutex_lock (&priv->lock);
756   GST_INFO ("finding %s:%d in %d transports", dest, port,
757       g_list_length (priv->transports));
758
759   for (walk = priv->transports; walk; walk = g_list_next (walk)) {
760     GstRTSPStreamTransport *trans = walk->data;
761     const GstRTSPTransport *tr;
762     gint min, max;
763
764     tr = gst_rtsp_stream_transport_get_transport (trans);
765
766     min = tr->client_port.min;
767     max = tr->client_port.max;
768
769     if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
770       result = trans;
771       break;
772     }
773   }
774   g_mutex_unlock (&priv->lock);
775
776   g_free (dest);
777
778   return result;
779 }
780
781 static GstRTSPStreamTransport *
782 check_transport (GObject * source, GstRTSPStream * stream)
783 {
784   GstStructure *stats;
785   GstRTSPStreamTransport *trans;
786
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);
789   if (trans == NULL) {
790     g_object_get (source, "stats", &stats, NULL);
791     if (stats) {
792       const gchar *rtcp_from;
793
794       dump_structure (stats);
795
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,
799             source);
800         g_object_set_qdata (source, ssrc_stream_map_key, trans);
801       }
802       gst_structure_free (stats);
803     }
804   }
805   return trans;
806 }
807
808
809 static void
810 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
811 {
812   GstRTSPStreamTransport *trans;
813
814   GST_INFO ("%p: new source %p", stream, source);
815
816   trans = check_transport (source, stream);
817
818   if (trans)
819     GST_INFO ("%p: source %p for transport %p", stream, source, trans);
820 }
821
822 static void
823 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
824 {
825   GST_INFO ("%p: new SDES %p", stream, source);
826 }
827
828 static void
829 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
830 {
831   GstRTSPStreamTransport *trans;
832
833   trans = check_transport (source, stream);
834
835   if (trans) {
836     GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
837     gst_rtsp_stream_transport_keep_alive (trans);
838   }
839 #ifdef DUMP_STATS
840   {
841     GstStructure *stats;
842     g_object_get (source, "stats", &stats, NULL);
843     if (stats) {
844       dump_structure (stats);
845       gst_structure_free (stats);
846     }
847   }
848 #endif
849 }
850
851 static void
852 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
853 {
854   GST_INFO ("%p: source %p bye", stream, source);
855 }
856
857 static void
858 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
859 {
860   GstRTSPStreamTransport *trans;
861
862   GST_INFO ("%p: source %p bye timeout", stream, source);
863
864   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
865     gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
866   }
867 }
868
869 static void
870 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
871 {
872   GstRTSPStreamTransport *trans;
873
874   GST_INFO ("%p: source %p timeout", stream, source);
875
876   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
877     gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
878   }
879 }
880
881 static GstFlowReturn
882 handle_new_sample (GstAppSink * sink, gpointer user_data)
883 {
884   GstRTSPStreamPrivate *priv;
885   GList *walk;
886   GstSample *sample;
887   GstBuffer *buffer;
888   GstRTSPStream *stream;
889
890   sample = gst_app_sink_pull_sample (sink);
891   if (!sample)
892     return GST_FLOW_OK;
893
894   stream = (GstRTSPStream *) user_data;
895   priv = stream->priv;
896   buffer = gst_sample_get_buffer (sample);
897
898   g_mutex_lock (&priv->lock);
899   for (walk = priv->transports; walk; walk = g_list_next (walk)) {
900     GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
901
902     if (GST_ELEMENT_CAST (sink) == priv->appsink[0]) {
903       gst_rtsp_stream_transport_send_rtp (tr, buffer);
904     } else {
905       gst_rtsp_stream_transport_send_rtcp (tr, buffer);
906     }
907   }
908   g_mutex_unlock (&priv->lock);
909
910   gst_sample_unref (sample);
911
912   return GST_FLOW_OK;
913 }
914
915 static GstAppSinkCallbacks sink_cb = {
916   NULL,                         /* not interested in EOS */
917   NULL,                         /* not interested in preroll samples */
918   handle_new_sample,
919 };
920
921 /**
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
927  *
928  * Join the #Gstbin @bin that contains the element @rtpbin.
929  *
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.
932  *
933  * Returns: %TRUE on success.
934  */
935 gboolean
936 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
937     GstElement * rtpbin, GstState state)
938 {
939   GstRTSPStreamPrivate *priv;
940   gint i, idx;
941   gchar *name;
942   GstPad *pad, *teepad, *queuepad, *selpad;
943   GstPadLinkReturn ret;
944
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);
948
949   priv = stream->priv;
950
951   g_mutex_lock (&priv->lock);
952   if (priv->is_joined)
953     goto was_joined;
954
955   /* create a session with the same index as the stream */
956   idx = priv->idx;
957
958   GST_INFO ("stream %p joining bin as session %d", stream, idx);
959
960   if (!alloc_ports (stream))
961     goto no_ports;
962
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);
966   g_free (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)
971     goto link_failed;
972
973   /* get pads from the RTP session element for sending and receiving
974    * RTP/RTCP*/
975   name = g_strdup_printf ("send_rtp_src_%u", idx);
976   priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
977   g_free (name);
978   name = g_strdup_printf ("send_rtcp_src_%u", idx);
979   priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
980   g_free (name);
981   name = g_strdup_printf ("recv_rtp_sink_%u", idx);
982   priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
983   g_free (name);
984   name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
985   priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
986   g_free (name);
987
988   /* get the session */
989   g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
990
991   g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
992       stream);
993   g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
994       stream);
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,
998       stream);
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,
1002       stream);
1003
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.
1010      *
1011      * .--------.      .-----.    .---------.
1012      * | rtpbin |      | tee |    | udpsink |
1013      * |       send->sink   src->sink       |
1014      * '--------'      |     |    '---------'
1015      *                 |     |    .---------.    .---------.
1016      *                 |     |    |  queue  |    | appsink |
1017      *                 |    src->sink      src->sink       |
1018      *                 '-----'    '---------'    '---------'
1019      */
1020     /* make tee for RTP/RTCP */
1021     priv->tee[i] = gst_element_factory_make ("tee", NULL);
1022     gst_bin_add (bin, priv->tee[i]);
1023
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);
1028
1029     /* add udpsink */
1030     gst_bin_add (bin, priv->udpsink[i]);
1031
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);
1038
1039     /* make queue */
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);
1048
1049     /* make appsink */
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);
1062
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.
1066      *
1067      * .--------.     .--------.    .--------.
1068      * | udpsrc |     | funnel |    | rtpbin |
1069      * |       src->sink      src->sink      |
1070      * '--------'     |        |    '--------'
1071      * .--------.     |        |
1072      * | appsrc |     |        |
1073      * |       src->sink       |
1074      * '--------'     '--------'
1075      */
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]);
1079
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);
1083
1084     /* we set and keep these to playing so that they don't cause NO_PREROLL return
1085      * values */
1086     gst_element_set_state (priv->udpsrc[i], GST_STATE_PLAYING);
1087     gst_element_set_locked_state (priv->udpsrc[i], TRUE);
1088     /* add udpsrc */
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);
1096
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);
1106
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);
1115     }
1116   }
1117
1118   /* be notified of caps changes */
1119   priv->caps_sig = g_signal_connect (priv->send_rtp_sink, "notify::caps",
1120       (GCallback) caps_notify, stream);
1121
1122   priv->is_joined = TRUE;
1123   g_mutex_unlock (&priv->lock);
1124
1125   return TRUE;
1126
1127   /* ERRORS */
1128 was_joined:
1129   {
1130     g_mutex_unlock (&priv->lock);
1131     return TRUE;
1132   }
1133 no_ports:
1134   {
1135     g_mutex_unlock (&priv->lock);
1136     GST_WARNING ("failed to allocate ports %d", idx);
1137     return FALSE;
1138   }
1139 link_failed:
1140   {
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);
1145     return FALSE;
1146   }
1147 }
1148
1149 /**
1150  * gst_rtsp_stream_leave_bin:
1151  * @stream: a #GstRTSPStream
1152  * @bin: a #GstBin
1153  * @rtpbin: a rtpbin #GstElement
1154  *
1155  * Remove the elements of @stream from @bin. @bin must be set
1156  * to the NULL state before calling this.
1157  *
1158  * Return: %TRUE on success.
1159  */
1160 gboolean
1161 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1162     GstElement * rtpbin)
1163 {
1164   GstRTSPStreamPrivate *priv;
1165   gint i;
1166
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);
1170
1171   priv = stream->priv;
1172
1173   g_mutex_lock (&priv->lock);
1174   if (!priv->is_joined)
1175     goto was_not_joined;
1176
1177   /* all transports must be removed by now */
1178   g_return_val_if_fail (priv->transports == NULL, FALSE);
1179
1180   GST_INFO ("stream %p leaving bin", stream);
1181
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;
1187
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);
1192
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]);
1202
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;
1206
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;
1214   }
1215   gst_object_unref (priv->send_src[0]);
1216   priv->send_src[0] = NULL;
1217
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;
1221
1222   g_object_unref (priv->session);
1223   priv->session = NULL;
1224   if (priv->caps)
1225     gst_caps_unref (priv->caps);
1226   priv->caps = NULL;
1227
1228   priv->is_joined = FALSE;
1229   g_mutex_unlock (&priv->lock);
1230
1231   return TRUE;
1232
1233 was_not_joined:
1234   {
1235     return TRUE;
1236   }
1237 }
1238
1239 /**
1240  * gst_rtsp_stream_get_rtpinfo:
1241  * @stream: a #GstRTSPStream
1242  * @rtptime: result RTP timestamp
1243  * @seq: result RTP seqnum
1244  *
1245  * Retrieve the current rtptime and seq. This is used to
1246  * construct a RTPInfo reply header.
1247  *
1248  * Returns: %TRUE when rtptime and seq could be determined.
1249  */
1250 gboolean
1251 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
1252     guint * rtptime, guint * seq)
1253 {
1254   GstRTSPStreamPrivate *priv;
1255   GObjectClass *payobjclass;
1256
1257   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1258   g_return_val_if_fail (rtptime != NULL, FALSE);
1259   g_return_val_if_fail (seq != NULL, FALSE);
1260
1261   priv = stream->priv;
1262
1263   payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
1264
1265   if (!g_object_class_find_property (payobjclass, "seqnum") ||
1266       !g_object_class_find_property (payobjclass, "timestamp"))
1267     return FALSE;
1268
1269   g_object_get (priv->payloader, "seqnum", seq, "timestamp", rtptime, NULL);
1270
1271   return TRUE;
1272 }
1273
1274 /**
1275  * gst_rtsp_stream_get_caps:
1276  * @stream: a #GstRTSPStream
1277  *
1278  * Retrieve the current caps of @stream.
1279  *
1280  * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
1281  *    after usage.
1282  */
1283 GstCaps *
1284 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
1285 {
1286   GstRTSPStreamPrivate *priv;
1287   GstCaps *result;
1288
1289   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1290
1291   priv = stream->priv;
1292
1293   g_mutex_lock (&priv->lock);
1294   if ((result = priv->caps))
1295     gst_caps_ref (result);
1296   g_mutex_unlock (&priv->lock);
1297
1298   return result;
1299 }
1300
1301 /**
1302  * gst_rtsp_stream_recv_rtp:
1303  * @stream: a #GstRTSPStream
1304  * @buffer: (transfer full): a #GstBuffer
1305  *
1306  * Handle an RTP buffer for the stream. This method is usually called when a
1307  * message has been received from a client using the TCP transport.
1308  *
1309  * This function takes ownership of @buffer.
1310  *
1311  * Returns: a GstFlowReturn.
1312  */
1313 GstFlowReturn
1314 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
1315 {
1316   GstRTSPStreamPrivate *priv;
1317   GstFlowReturn ret;
1318   GstElement *element;
1319
1320   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1321   priv = stream->priv;
1322   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1323   g_return_val_if_fail (priv->is_joined, FALSE);
1324
1325   g_mutex_lock (&priv->lock);
1326   element = gst_object_ref (priv->appsrc[0]);
1327   g_mutex_unlock (&priv->lock);
1328
1329   ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1330
1331   gst_object_unref (element);
1332
1333   return ret;
1334 }
1335
1336 /**
1337  * gst_rtsp_stream_recv_rtcp:
1338  * @stream: a #GstRTSPStream
1339  * @buffer: (transfer full): a #GstBuffer
1340  *
1341  * Handle an RTCP buffer for the stream. This method is usually called when a
1342  * message has been received from a client using the TCP transport.
1343  *
1344  * This function takes ownership of @buffer.
1345  *
1346  * Returns: a GstFlowReturn.
1347  */
1348 GstFlowReturn
1349 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
1350 {
1351   GstRTSPStreamPrivate *priv;
1352   GstFlowReturn ret;
1353   GstElement *element;
1354
1355   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1356   priv = stream->priv;
1357   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1358   g_return_val_if_fail (priv->is_joined, FALSE);
1359
1360   g_mutex_lock (&priv->lock);
1361   element = gst_object_ref (priv->appsrc[1]);
1362   g_mutex_unlock (&priv->lock);
1363
1364   ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1365
1366   gst_object_unref (element);
1367
1368   return ret;
1369 }
1370
1371 /* must be called with lock */
1372 static gboolean
1373 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
1374     gboolean add)
1375 {
1376   GstRTSPStreamPrivate *priv = stream->priv;
1377   const GstRTSPTransport *tr;
1378
1379   tr = gst_rtsp_stream_transport_get_transport (trans);
1380
1381   switch (tr->lower_transport) {
1382     case GST_RTSP_LOWER_TRANS_UDP:
1383     case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1384     {
1385       gchar *dest;
1386       gint min, max;
1387       guint ttl = 0;
1388
1389       dest = tr->destination;
1390       if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1391         min = tr->port.min;
1392         max = tr->port.max;
1393         ttl = tr->ttl;
1394       } else {
1395         min = tr->client_port.min;
1396         max = tr->client_port.max;
1397       }
1398
1399       if (add) {
1400         GST_INFO ("adding %s:%d-%d", dest, min, max);
1401         g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
1402         g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
1403         if (ttl > 0) {
1404           GST_INFO ("setting ttl-mc %d", ttl);
1405           g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
1406           g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
1407         }
1408         priv->transports = g_list_prepend (priv->transports, trans);
1409       } else {
1410         GST_INFO ("removing %s:%d-%d", dest, min, max);
1411         g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
1412         g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
1413         priv->transports = g_list_remove (priv->transports, trans);
1414       }
1415       break;
1416     }
1417     case GST_RTSP_LOWER_TRANS_TCP:
1418       if (add) {
1419         GST_INFO ("adding TCP %s", tr->destination);
1420         priv->transports = g_list_prepend (priv->transports, trans);
1421       } else {
1422         GST_INFO ("removing TCP %s", tr->destination);
1423         priv->transports = g_list_remove (priv->transports, trans);
1424       }
1425       break;
1426     default:
1427       goto unknown_transport;
1428   }
1429   return TRUE;
1430
1431   /* ERRORS */
1432 unknown_transport:
1433   {
1434     GST_INFO ("Unknown transport %d", tr->lower_transport);
1435     return FALSE;
1436   }
1437 }
1438
1439
1440 /**
1441  * gst_rtsp_stream_add_transport:
1442  * @stream: a #GstRTSPStream
1443  * @trans: a #GstRTSPStreamTransport
1444  *
1445  * Add the transport in @trans to @stream. The media of @stream will
1446  * then also be send to the values configured in @trans.
1447  *
1448  * @stream must be joined to a bin.
1449  *
1450  * @trans must contain a valid #GstRTSPTransport.
1451  *
1452  * Returns: %TRUE if @trans was added
1453  */
1454 gboolean
1455 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
1456     GstRTSPStreamTransport * trans)
1457 {
1458   GstRTSPStreamPrivate *priv;
1459   gboolean res;
1460
1461   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1462   priv = stream->priv;
1463   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1464   g_return_val_if_fail (priv->is_joined, FALSE);
1465
1466   g_mutex_lock (&priv->lock);
1467   res = update_transport (stream, trans, TRUE);
1468   g_mutex_unlock (&priv->lock);
1469
1470   return res;
1471 }
1472
1473 /**
1474  * gst_rtsp_stream_remove_transport:
1475  * @stream: a #GstRTSPStream
1476  * @trans: a #GstRTSPStreamTransport
1477  *
1478  * Remove the transport in @trans from @stream. The media of @stream will
1479  * not be sent to the values configured in @trans.
1480  *
1481  * @stream must be joined to a bin.
1482  *
1483  * @trans must contain a valid #GstRTSPTransport.
1484  *
1485  * Returns: %TRUE if @trans was removed
1486  */
1487 gboolean
1488 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
1489     GstRTSPStreamTransport * trans)
1490 {
1491   GstRTSPStreamPrivate *priv;
1492   gboolean res;
1493
1494   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1495   priv = stream->priv;
1496   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1497   g_return_val_if_fail (priv->is_joined, FALSE);
1498
1499   g_mutex_lock (&priv->lock);
1500   res = update_transport (stream, trans, FALSE);
1501   g_mutex_unlock (&priv->lock);
1502
1503   return res;
1504 }