client: Check client provided addresses against the address pool
[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
66   /* multicast addresses */
67   GstRTSPAddressPool *pool;
68   GstRTSPAddress *addr;
69
70   /* the caps of the stream */
71   gulong caps_sig;
72   GstCaps *caps;
73
74   /* transports we stream to */
75   guint n_active;
76   GList *transports;
77 };
78
79
80 enum
81 {
82   PROP_0,
83   PROP_LAST
84 };
85
86 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
87 #define GST_CAT_DEFAULT rtsp_stream_debug
88
89 static GQuark ssrc_stream_map_key;
90
91 static void gst_rtsp_stream_finalize (GObject * obj);
92
93 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
94
95 static void
96 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
97 {
98   GObjectClass *gobject_class;
99
100   g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
101
102   gobject_class = G_OBJECT_CLASS (klass);
103
104   gobject_class->finalize = gst_rtsp_stream_finalize;
105
106   GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
107
108   ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
109 }
110
111 static void
112 gst_rtsp_stream_init (GstRTSPStream * stream)
113 {
114   GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
115
116   GST_DEBUG ("new stream %p", stream);
117
118   stream->priv = priv;
119
120   g_mutex_init (&priv->lock);
121 }
122
123 static void
124 gst_rtsp_stream_finalize (GObject * obj)
125 {
126   GstRTSPStream *stream;
127   GstRTSPStreamPrivate *priv;
128
129   stream = GST_RTSP_STREAM (obj);
130   priv = stream->priv;
131
132   GST_DEBUG ("finalize stream %p", stream);
133
134   /* we really need to be unjoined now */
135   g_return_if_fail (!priv->is_joined);
136
137   if (priv->addr)
138     gst_rtsp_address_free (priv->addr);
139   if (priv->pool)
140     g_object_unref (priv->pool);
141   gst_object_unref (priv->payloader);
142   gst_object_unref (priv->srcpad);
143   g_mutex_clear (&priv->lock);
144
145   G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
146 }
147
148 /**
149  * gst_rtsp_stream_new:
150  * @idx: an index
151  * @srcpad: a #GstPad
152  * @payloader: a #GstElement
153  *
154  * Create a new media stream with index @idx that handles RTP data on
155  * @srcpad and has a payloader element @payloader.
156  *
157  * Returns: a new #GstRTSPStream
158  */
159 GstRTSPStream *
160 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
161 {
162   GstRTSPStreamPrivate *priv;
163   GstRTSPStream *stream;
164
165   g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
166   g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
167   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
168
169   stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
170   priv = stream->priv;
171   priv->idx = idx;
172   priv->payloader = gst_object_ref (payloader);
173   priv->srcpad = gst_object_ref (srcpad);
174
175   return stream;
176 }
177
178 /**
179  * gst_rtsp_stream_get_index:
180  * @stream: a #GstRTSPStream
181  *
182  * Get the stream index.
183  *
184  * Return: the stream index.
185  */
186 guint
187 gst_rtsp_stream_get_index (GstRTSPStream * stream)
188 {
189   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
190
191   return stream->priv->idx;
192 }
193
194 /**
195  * gst_rtsp_stream_set_mtu:
196  * @stream: a #GstRTSPStream
197  * @mtu: a new MTU
198  *
199  * Configure the mtu in the payloader of @stream to @mtu.
200  */
201 void
202 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
203 {
204   GstRTSPStreamPrivate *priv;
205
206   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
207
208   priv = stream->priv;
209
210   GST_LOG_OBJECT (stream, "set MTU %u", mtu);
211
212   g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
213 }
214
215 /**
216  * gst_rtsp_stream_get_mtu:
217  * @stream: a #GstRTSPStream
218  *
219  * Get the configured MTU in the payloader of @stream.
220  *
221  * Returns: the MTU of the payloader.
222  */
223 guint
224 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
225 {
226   GstRTSPStreamPrivate *priv;
227   guint mtu;
228
229   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
230
231   priv = stream->priv;
232
233   g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
234
235   return mtu;
236 }
237
238 /**
239  * gst_rtsp_stream_set_address_pool:
240  * @stream: a #GstRTSPStream
241  * @pool: a #GstRTSPAddressPool
242  *
243  * configure @pool to be used as the address pool of @stream.
244  */
245 void
246 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
247     GstRTSPAddressPool * pool)
248 {
249   GstRTSPStreamPrivate *priv;
250   GstRTSPAddressPool *old;
251
252   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
253
254   priv = stream->priv;
255
256   GST_LOG_OBJECT (stream, "set address pool %p", pool);
257
258   g_mutex_lock (&priv->lock);
259   if ((old = priv->pool) != pool)
260     priv->pool = pool ? g_object_ref (pool) : NULL;
261   else
262     old = NULL;
263   g_mutex_unlock (&priv->lock);
264
265   if (old)
266     g_object_unref (old);
267 }
268
269 /**
270  * gst_rtsp_stream_get_address_pool:
271  * @stream: a #GstRTSPStream
272  *
273  * Get the #GstRTSPAddressPool used as the address pool of @stream.
274  *
275  * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
276  * usage.
277  */
278 GstRTSPAddressPool *
279 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
280 {
281   GstRTSPStreamPrivate *priv;
282   GstRTSPAddressPool *result;
283
284   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
285
286   priv = stream->priv;
287
288   g_mutex_lock (&priv->lock);
289   if ((result = priv->pool))
290     g_object_ref (result);
291   g_mutex_unlock (&priv->lock);
292
293   return result;
294 }
295
296 /**
297  * gst_rtsp_stream_get_address:
298  * @stream: a #GstRTSPStream
299  *
300  * Get the multicast address of @stream.
301  *
302  * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
303  * allocated. gst_rtsp_address_free() after usage.
304  */
305 GstRTSPAddress *
306 gst_rtsp_stream_get_address (GstRTSPStream * stream)
307 {
308   GstRTSPStreamPrivate *priv;
309   GstRTSPAddress *result;
310
311   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
312
313   priv = stream->priv;
314
315   g_mutex_lock (&priv->lock);
316   if (priv->addr == NULL) {
317     if (priv->pool == NULL)
318       goto no_pool;
319
320     priv->addr = gst_rtsp_address_pool_acquire_address (priv->pool,
321         GST_RTSP_ADDRESS_FLAG_EVEN_PORT, 2);
322     if (priv->addr == NULL)
323       goto no_address;
324   }
325   result = gst_rtsp_address_copy (priv->addr);
326   g_mutex_unlock (&priv->lock);
327
328   return result;
329
330   /* ERRORS */
331 no_pool:
332   {
333     GST_ERROR_OBJECT (stream, "no address pool specified");
334     g_mutex_unlock (&priv->lock);
335     return NULL;
336   }
337 no_address:
338   {
339     GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
340     g_mutex_unlock (&priv->lock);
341     return NULL;
342   }
343 }
344
345 /**
346  * gst_rtsp_stream_reserve_address:
347  * @stream: a #GstRTSPStream
348  *
349  * Get a specific multicast address of @stream.
350  *
351  * Returns: the #GstRTSPAddress of @stream or %NULL when no address could be
352  * allocated. gst_rtsp_address_free() after usage.
353  */
354 GstRTSPAddress *
355 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
356     const gchar * address, guint port, guint n_ports, guint ttl)
357 {
358   GstRTSPStreamPrivate *priv;
359   GstRTSPAddress *result;
360
361   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
362   g_return_val_if_fail (address != NULL, NULL);
363   g_return_val_if_fail (port > 0, NULL);
364   g_return_val_if_fail (n_ports > 0, NULL);
365   g_return_val_if_fail (ttl > 0, NULL);
366
367   priv = stream->priv;
368
369   g_mutex_lock (&priv->lock);
370   if (priv->addr == NULL) {
371     if (priv->pool == NULL)
372       goto no_pool;
373
374     priv->addr = gst_rtsp_address_pool_reserve_address (priv->pool, address,
375         port, n_ports, ttl);
376     if (priv->addr == NULL)
377       goto no_address;
378   } else {
379     if (strcmp (priv->addr->address, address) ||
380         priv->addr->port != port || priv->addr->n_ports != n_ports ||
381         priv->addr->ttl != ttl)
382       goto different_address;
383   }
384   result = gst_rtsp_address_copy (priv->addr);
385   g_mutex_unlock (&priv->lock);
386
387   return result;
388
389   /* ERRORS */
390 no_pool:
391   {
392     GST_ERROR_OBJECT (stream, "no address pool specified");
393     g_mutex_unlock (&priv->lock);
394     return NULL;
395   }
396 no_address:
397   {
398     GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
399         address);
400     g_mutex_unlock (&priv->lock);
401     return NULL;
402   }
403 different_address:
404   {
405     GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
406         " reserved", address);
407     g_mutex_unlock (&priv->lock);
408     return NULL;
409   }
410 }
411
412 /* must be called with lock */
413 static gboolean
414 alloc_ports (GstRTSPStream * stream)
415 {
416   GstRTSPStreamPrivate *priv = stream->priv;
417   GstStateChangeReturn ret;
418   GstElement *udpsrc0, *udpsrc1;
419   GstElement *udpsink0, *udpsink1;
420   gint tmp_rtp, tmp_rtcp;
421   guint count;
422   gint rtpport, rtcpport;
423   GSocket *socket;
424   const gchar *host;
425
426   udpsrc0 = NULL;
427   udpsrc1 = NULL;
428   udpsink0 = NULL;
429   udpsink1 = NULL;
430   count = 0;
431
432   /* Start with random port */
433   tmp_rtp = 0;
434
435   if (priv->is_ipv6)
436     host = "udp://[::0]";
437   else
438     host = "udp://0.0.0.0";
439
440   /* try to allocate 2 UDP ports, the RTP port should be an even
441    * number and the RTCP port should be the next (uneven) port */
442 again:
443   udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
444   if (udpsrc0 == NULL)
445     goto no_udp_protocol;
446   g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, NULL);
447
448   ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
449   if (ret == GST_STATE_CHANGE_FAILURE) {
450     if (tmp_rtp != 0) {
451       tmp_rtp += 2;
452       if (++count > 20)
453         goto no_ports;
454
455       gst_element_set_state (udpsrc0, GST_STATE_NULL);
456       gst_object_unref (udpsrc0);
457
458       goto again;
459     }
460     goto no_udp_protocol;
461   }
462
463   g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
464
465   /* check if port is even */
466   if ((tmp_rtp & 1) != 0) {
467     /* port not even, close and allocate another */
468     if (++count > 20)
469       goto no_ports;
470
471     gst_element_set_state (udpsrc0, GST_STATE_NULL);
472     gst_object_unref (udpsrc0);
473
474     tmp_rtp++;
475     goto again;
476   }
477
478   /* allocate port+1 for RTCP now */
479   udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
480   if (udpsrc1 == NULL)
481     goto no_udp_rtcp_protocol;
482
483   /* set port */
484   tmp_rtcp = tmp_rtp + 1;
485   g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
486
487   ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
488   /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
489   if (ret == GST_STATE_CHANGE_FAILURE) {
490
491     if (++count > 20)
492       goto no_ports;
493
494     gst_element_set_state (udpsrc0, GST_STATE_NULL);
495     gst_object_unref (udpsrc0);
496
497     gst_element_set_state (udpsrc1, GST_STATE_NULL);
498     gst_object_unref (udpsrc1);
499
500     tmp_rtp += 2;
501     goto again;
502   }
503   /* all fine, do port check */
504   g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
505   g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
506
507   /* this should not happen... */
508   if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
509     goto port_error;
510
511   udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
512   if (!udpsink0)
513     goto no_udp_protocol;
514
515   g_object_get (G_OBJECT (udpsrc0), "used-socket", &socket, NULL);
516   g_object_set (G_OBJECT (udpsink0), "socket", socket, NULL);
517   g_object_unref (socket);
518   g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
519
520   udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
521   if (!udpsink1)
522     goto no_udp_protocol;
523
524   if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
525           "send-duplicates")) {
526     g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
527     g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
528   } else {
529     g_warning
530         ("old multiudpsink version found without send-duplicates property");
531   }
532
533   if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
534           "buffer-size")) {
535     g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
536   } else {
537     GST_WARNING ("multiudpsink version found without buffer-size property");
538   }
539
540   g_object_get (G_OBJECT (udpsrc1), "used-socket", &socket, NULL);
541   g_object_set (G_OBJECT (udpsink1), "socket", socket, NULL);
542   g_object_unref (socket);
543   g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
544   g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
545   g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
546   g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
547   g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
548   g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
549   g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
550
551   /* we keep these elements, we will further configure them when the
552    * client told us to really use the UDP ports. */
553   priv->udpsrc[0] = udpsrc0;
554   priv->udpsrc[1] = udpsrc1;
555   priv->udpsink[0] = udpsink0;
556   priv->udpsink[1] = udpsink1;
557   priv->server_port.min = rtpport;
558   priv->server_port.max = rtcpport;
559
560   return TRUE;
561
562   /* ERRORS */
563 no_udp_protocol:
564   {
565     goto cleanup;
566   }
567 no_ports:
568   {
569     goto cleanup;
570   }
571 no_udp_rtcp_protocol:
572   {
573     goto cleanup;
574   }
575 port_error:
576   {
577     goto cleanup;
578   }
579 cleanup:
580   {
581     if (udpsrc0) {
582       gst_element_set_state (udpsrc0, GST_STATE_NULL);
583       gst_object_unref (udpsrc0);
584     }
585     if (udpsrc1) {
586       gst_element_set_state (udpsrc1, GST_STATE_NULL);
587       gst_object_unref (udpsrc1);
588     }
589     if (udpsink0) {
590       gst_element_set_state (udpsink0, GST_STATE_NULL);
591       gst_object_unref (udpsink0);
592     }
593     if (udpsink1) {
594       gst_element_set_state (udpsink1, GST_STATE_NULL);
595       gst_object_unref (udpsink1);
596     }
597     return FALSE;
598   }
599 }
600
601 /**
602  * gst_rtsp_stream_get_server_port:
603  * @stream: a #GstRTSPStream
604  * @server_port: (out): result server port
605  *
606  * Fill @server_port with the port pair used by the server. This function can
607  * only be called when @stream has been joined.
608  */
609 void
610 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
611     GstRTSPRange * server_port)
612 {
613   GstRTSPStreamPrivate *priv;
614
615   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
616   priv = stream->priv;
617   g_return_if_fail (priv->is_joined);
618
619   g_mutex_lock (&priv->lock);
620   if (server_port)
621     *server_port = priv->server_port;
622   g_mutex_unlock (&priv->lock);
623 }
624
625 /**
626  * gst_rtsp_stream_get_ssrc:
627  * @stream: a #GstRTSPStream
628  * @ssrc: (out): result ssrc
629  *
630  * Get the SSRC used by the RTP session of this stream. This function can only
631  * be called when @stream has been joined.
632  */
633 void
634 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
635 {
636   GstRTSPStreamPrivate *priv;
637
638   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
639   priv = stream->priv;
640   g_return_if_fail (priv->is_joined);
641
642   g_mutex_lock (&priv->lock);
643   if (ssrc && priv->session)
644     g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
645   g_mutex_unlock (&priv->lock);
646 }
647
648 /* executed from streaming thread */
649 static void
650 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
651 {
652   GstRTSPStreamPrivate *priv = stream->priv;
653   GstCaps *newcaps, *oldcaps;
654
655   newcaps = gst_pad_get_current_caps (pad);
656
657   GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
658       newcaps);
659
660   g_mutex_lock (&priv->lock);
661   oldcaps = priv->caps;
662   priv->caps = newcaps;
663   g_mutex_unlock (&priv->lock);
664
665   if (oldcaps)
666     gst_caps_unref (oldcaps);
667 }
668
669 static void
670 dump_structure (const GstStructure * s)
671 {
672   gchar *sstr;
673
674   sstr = gst_structure_to_string (s);
675   GST_INFO ("structure: %s", sstr);
676   g_free (sstr);
677 }
678
679 static GstRTSPStreamTransport *
680 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
681 {
682   GstRTSPStreamPrivate *priv = stream->priv;
683   GList *walk;
684   GstRTSPStreamTransport *result = NULL;
685   const gchar *tmp;
686   gchar *dest;
687   guint port;
688
689   if (rtcp_from == NULL)
690     return NULL;
691
692   tmp = g_strrstr (rtcp_from, ":");
693   if (tmp == NULL)
694     return NULL;
695
696   port = atoi (tmp + 1);
697   dest = g_strndup (rtcp_from, tmp - rtcp_from);
698
699   g_mutex_lock (&priv->lock);
700   GST_INFO ("finding %s:%d in %d transports", dest, port,
701       g_list_length (priv->transports));
702
703   for (walk = priv->transports; walk; walk = g_list_next (walk)) {
704     GstRTSPStreamTransport *trans = walk->data;
705     const GstRTSPTransport *tr;
706     gint min, max;
707
708     tr = gst_rtsp_stream_transport_get_transport (trans);
709
710     min = tr->client_port.min;
711     max = tr->client_port.max;
712
713     if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
714       result = trans;
715       break;
716     }
717   }
718   g_mutex_unlock (&priv->lock);
719
720   g_free (dest);
721
722   return result;
723 }
724
725 static GstRTSPStreamTransport *
726 check_transport (GObject * source, GstRTSPStream * stream)
727 {
728   GstStructure *stats;
729   GstRTSPStreamTransport *trans;
730
731   /* see if we have a stream to match with the origin of the RTCP packet */
732   trans = g_object_get_qdata (source, ssrc_stream_map_key);
733   if (trans == NULL) {
734     g_object_get (source, "stats", &stats, NULL);
735     if (stats) {
736       const gchar *rtcp_from;
737
738       dump_structure (stats);
739
740       rtcp_from = gst_structure_get_string (stats, "rtcp-from");
741       if ((trans = find_transport (stream, rtcp_from))) {
742         GST_INFO ("%p: found transport %p for source  %p", stream, trans,
743             source);
744         g_object_set_qdata (source, ssrc_stream_map_key, trans);
745       }
746       gst_structure_free (stats);
747     }
748   }
749   return trans;
750 }
751
752
753 static void
754 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
755 {
756   GstRTSPStreamTransport *trans;
757
758   GST_INFO ("%p: new source %p", stream, source);
759
760   trans = check_transport (source, stream);
761
762   if (trans)
763     GST_INFO ("%p: source %p for transport %p", stream, source, trans);
764 }
765
766 static void
767 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
768 {
769   GST_INFO ("%p: new SDES %p", stream, source);
770 }
771
772 static void
773 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
774 {
775   GstRTSPStreamTransport *trans;
776
777   trans = check_transport (source, stream);
778
779   if (trans) {
780     GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
781     gst_rtsp_stream_transport_keep_alive (trans);
782   }
783 #ifdef DUMP_STATS
784   {
785     GstStructure *stats;
786     g_object_get (source, "stats", &stats, NULL);
787     if (stats) {
788       dump_structure (stats);
789       gst_structure_free (stats);
790     }
791   }
792 #endif
793 }
794
795 static void
796 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
797 {
798   GST_INFO ("%p: source %p bye", stream, source);
799 }
800
801 static void
802 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
803 {
804   GstRTSPStreamTransport *trans;
805
806   GST_INFO ("%p: source %p bye timeout", stream, source);
807
808   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
809     gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
810   }
811 }
812
813 static void
814 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
815 {
816   GstRTSPStreamTransport *trans;
817
818   GST_INFO ("%p: source %p timeout", stream, source);
819
820   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
821     gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
822   }
823 }
824
825 static GstFlowReturn
826 handle_new_sample (GstAppSink * sink, gpointer user_data)
827 {
828   GstRTSPStreamPrivate *priv;
829   GList *walk;
830   GstSample *sample;
831   GstBuffer *buffer;
832   GstRTSPStream *stream;
833
834   sample = gst_app_sink_pull_sample (sink);
835   if (!sample)
836     return GST_FLOW_OK;
837
838   stream = (GstRTSPStream *) user_data;
839   priv = stream->priv;
840   buffer = gst_sample_get_buffer (sample);
841
842   g_mutex_lock (&priv->lock);
843   for (walk = priv->transports; walk; walk = g_list_next (walk)) {
844     GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
845
846     if (GST_ELEMENT_CAST (sink) == priv->appsink[0]) {
847       gst_rtsp_stream_transport_send_rtp (tr, buffer);
848     } else {
849       gst_rtsp_stream_transport_send_rtcp (tr, buffer);
850     }
851   }
852   g_mutex_unlock (&priv->lock);
853
854   gst_sample_unref (sample);
855
856   return GST_FLOW_OK;
857 }
858
859 static GstAppSinkCallbacks sink_cb = {
860   NULL,                         /* not interested in EOS */
861   NULL,                         /* not interested in preroll samples */
862   handle_new_sample,
863 };
864
865 /**
866  * gst_rtsp_stream_join_bin:
867  * @stream: a #GstRTSPStream
868  * @bin: a #GstBin to join
869  * @rtpbin: a rtpbin element in @bin
870  * @state: the target state of the new elements
871  *
872  * Join the #Gstbin @bin that contains the element @rtpbin.
873  *
874  * @stream will link to @rtpbin, which must be inside @bin. The elements
875  * added to @bin will be set to the state given in @state.
876  *
877  * Returns: %TRUE on success.
878  */
879 gboolean
880 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
881     GstElement * rtpbin, GstState state)
882 {
883   GstRTSPStreamPrivate *priv;
884   gint i, idx;
885   gchar *name;
886   GstPad *pad, *teepad, *queuepad, *selpad;
887   GstPadLinkReturn ret;
888
889   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
890   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
891   g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
892
893   priv = stream->priv;
894
895   g_mutex_lock (&priv->lock);
896   if (priv->is_joined)
897     goto was_joined;
898
899   /* create a session with the same index as the stream */
900   idx = priv->idx;
901
902   GST_INFO ("stream %p joining bin as session %d", stream, idx);
903
904   if (!alloc_ports (stream))
905     goto no_ports;
906
907   /* get a pad for sending RTP */
908   name = g_strdup_printf ("send_rtp_sink_%u", idx);
909   priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
910   g_free (name);
911   /* link the RTP pad to the session manager, it should not really fail unless
912    * this is not really an RTP pad */
913   ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
914   if (ret != GST_PAD_LINK_OK)
915     goto link_failed;
916
917   /* get pads from the RTP session element for sending and receiving
918    * RTP/RTCP*/
919   name = g_strdup_printf ("send_rtp_src_%u", idx);
920   priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
921   g_free (name);
922   name = g_strdup_printf ("send_rtcp_src_%u", idx);
923   priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
924   g_free (name);
925   name = g_strdup_printf ("recv_rtp_sink_%u", idx);
926   priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
927   g_free (name);
928   name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
929   priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
930   g_free (name);
931
932   /* get the session */
933   g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
934
935   g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
936       stream);
937   g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
938       stream);
939   g_signal_connect (priv->session, "on-ssrc-active",
940       (GCallback) on_ssrc_active, stream);
941   g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
942       stream);
943   g_signal_connect (priv->session, "on-bye-timeout",
944       (GCallback) on_bye_timeout, stream);
945   g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
946       stream);
947
948   for (i = 0; i < 2; i++) {
949     /* For the sender we create this bit of pipeline for both
950      * RTP and RTCP. Sync and preroll are enabled on udpsink so
951      * we need to add a queue before appsink to make the pipeline
952      * not block. For the TCP case, we want to pump data to the
953      * client as fast as possible anyway.
954      *
955      * .--------.      .-----.    .---------.
956      * | rtpbin |      | tee |    | udpsink |
957      * |       send->sink   src->sink       |
958      * '--------'      |     |    '---------'
959      *                 |     |    .---------.    .---------.
960      *                 |     |    |  queue  |    | appsink |
961      *                 |    src->sink      src->sink       |
962      *                 '-----'    '---------'    '---------'
963      */
964     /* make tee for RTP/RTCP */
965     priv->tee[i] = gst_element_factory_make ("tee", NULL);
966     gst_bin_add (bin, priv->tee[i]);
967
968     /* and link to rtpbin send pad */
969     pad = gst_element_get_static_pad (priv->tee[i], "sink");
970     gst_pad_link (priv->send_src[i], pad);
971     gst_object_unref (pad);
972
973     /* add udpsink */
974     gst_bin_add (bin, priv->udpsink[i]);
975
976     /* link tee to udpsink */
977     teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
978     pad = gst_element_get_static_pad (priv->udpsink[i], "sink");
979     gst_pad_link (teepad, pad);
980     gst_object_unref (pad);
981     gst_object_unref (teepad);
982
983     /* make queue */
984     priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
985     gst_bin_add (bin, priv->appqueue[i]);
986     /* and link to tee */
987     teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
988     pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
989     gst_pad_link (teepad, pad);
990     gst_object_unref (pad);
991     gst_object_unref (teepad);
992
993     /* make appsink */
994     priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
995     g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
996     g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
997     gst_bin_add (bin, priv->appsink[i]);
998     gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
999         &sink_cb, stream, NULL);
1000     /* and link to queue */
1001     queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1002     pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1003     gst_pad_link (queuepad, pad);
1004     gst_object_unref (pad);
1005     gst_object_unref (queuepad);
1006
1007     /* For the receiver we create this bit of pipeline for both
1008      * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1009      * and it is all funneled into the rtpbin receive pad.
1010      *
1011      * .--------.     .--------.    .--------.
1012      * | udpsrc |     | funnel |    | rtpbin |
1013      * |       src->sink      src->sink      |
1014      * '--------'     |        |    '--------'
1015      * .--------.     |        |
1016      * | appsrc |     |        |
1017      * |       src->sink       |
1018      * '--------'     '--------'
1019      */
1020     /* make funnel for the RTP/RTCP receivers */
1021     priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1022     gst_bin_add (bin, priv->funnel[i]);
1023
1024     pad = gst_element_get_static_pad (priv->funnel[i], "src");
1025     gst_pad_link (pad, priv->recv_sink[i]);
1026     gst_object_unref (pad);
1027
1028     /* we set and keep these to playing so that they don't cause NO_PREROLL return
1029      * values */
1030     gst_element_set_state (priv->udpsrc[i], GST_STATE_PLAYING);
1031     gst_element_set_locked_state (priv->udpsrc[i], TRUE);
1032     /* add udpsrc */
1033     gst_bin_add (bin, priv->udpsrc[i]);
1034     /* and link to the funnel */
1035     selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1036     pad = gst_element_get_static_pad (priv->udpsrc[i], "src");
1037     gst_pad_link (pad, selpad);
1038     gst_object_unref (pad);
1039     gst_object_unref (selpad);
1040
1041     /* make and add appsrc */
1042     priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1043     gst_bin_add (bin, priv->appsrc[i]);
1044     /* and link to the funnel */
1045     selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1046     pad = gst_element_get_static_pad (priv->appsrc[i], "src");
1047     gst_pad_link (pad, selpad);
1048     gst_object_unref (pad);
1049     gst_object_unref (selpad);
1050
1051     /* check if we need to set to a special state */
1052     if (state != GST_STATE_NULL) {
1053       gst_element_set_state (priv->udpsink[i], state);
1054       gst_element_set_state (priv->appsink[i], state);
1055       gst_element_set_state (priv->appqueue[i], state);
1056       gst_element_set_state (priv->tee[i], state);
1057       gst_element_set_state (priv->funnel[i], state);
1058       gst_element_set_state (priv->appsrc[i], state);
1059     }
1060   }
1061
1062   /* be notified of caps changes */
1063   priv->caps_sig = g_signal_connect (priv->send_rtp_sink, "notify::caps",
1064       (GCallback) caps_notify, stream);
1065
1066   priv->is_joined = TRUE;
1067   g_mutex_unlock (&priv->lock);
1068
1069   return TRUE;
1070
1071   /* ERRORS */
1072 was_joined:
1073   {
1074     g_mutex_unlock (&priv->lock);
1075     return TRUE;
1076   }
1077 no_ports:
1078   {
1079     g_mutex_unlock (&priv->lock);
1080     GST_WARNING ("failed to allocate ports %d", idx);
1081     return FALSE;
1082   }
1083 link_failed:
1084   {
1085     GST_WARNING ("failed to link stream %d", idx);
1086     gst_object_unref (priv->send_rtp_sink);
1087     priv->send_rtp_sink = NULL;
1088     g_mutex_unlock (&priv->lock);
1089     return FALSE;
1090   }
1091 }
1092
1093 /**
1094  * gst_rtsp_stream_leave_bin:
1095  * @stream: a #GstRTSPStream
1096  * @bin: a #GstBin
1097  * @rtpbin: a rtpbin #GstElement
1098  *
1099  * Remove the elements of @stream from @bin. @bin must be set
1100  * to the NULL state before calling this.
1101  *
1102  * Return: %TRUE on success.
1103  */
1104 gboolean
1105 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1106     GstElement * rtpbin)
1107 {
1108   GstRTSPStreamPrivate *priv;
1109   gint i;
1110
1111   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1112   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1113   g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1114
1115   priv = stream->priv;
1116
1117   g_mutex_lock (&priv->lock);
1118   if (!priv->is_joined)
1119     goto was_not_joined;
1120
1121   /* all transports must be removed by now */
1122   g_return_val_if_fail (priv->transports == NULL, FALSE);
1123
1124   GST_INFO ("stream %p leaving bin", stream);
1125
1126   gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
1127   g_signal_handler_disconnect (priv->send_rtp_sink, priv->caps_sig);
1128   gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
1129   gst_object_unref (priv->send_rtp_sink);
1130   priv->send_rtp_sink = NULL;
1131
1132   for (i = 0; i < 2; i++) {
1133     /* and set udpsrc to NULL now before removing */
1134     gst_element_set_locked_state (priv->udpsrc[i], FALSE);
1135     gst_element_set_state (priv->udpsrc[i], GST_STATE_NULL);
1136
1137     /* removing them should also nicely release the request
1138      * pads when they finalize */
1139     gst_bin_remove (bin, priv->udpsrc[i]);
1140     gst_bin_remove (bin, priv->udpsink[i]);
1141     gst_bin_remove (bin, priv->appsrc[i]);
1142     gst_bin_remove (bin, priv->appsink[i]);
1143     gst_bin_remove (bin, priv->appqueue[i]);
1144     gst_bin_remove (bin, priv->tee[i]);
1145     gst_bin_remove (bin, priv->funnel[i]);
1146
1147     gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
1148     gst_object_unref (priv->recv_sink[i]);
1149     priv->recv_sink[i] = NULL;
1150
1151     priv->udpsrc[i] = NULL;
1152     priv->udpsink[i] = NULL;
1153     priv->appsrc[i] = NULL;
1154     priv->appsink[i] = NULL;
1155     priv->appqueue[i] = NULL;
1156     priv->tee[i] = NULL;
1157     priv->funnel[i] = NULL;
1158   }
1159   gst_object_unref (priv->send_src[0]);
1160   priv->send_src[0] = NULL;
1161
1162   gst_element_release_request_pad (rtpbin, priv->send_src[1]);
1163   gst_object_unref (priv->send_src[1]);
1164   priv->send_src[1] = NULL;
1165
1166   g_object_unref (priv->session);
1167   if (priv->caps)
1168     gst_caps_unref (priv->caps);
1169
1170   priv->is_joined = FALSE;
1171   g_mutex_unlock (&priv->lock);
1172
1173   return TRUE;
1174
1175 was_not_joined:
1176   {
1177     return TRUE;
1178   }
1179 }
1180
1181 /**
1182  * gst_rtsp_stream_get_rtpinfo:
1183  * @stream: a #GstRTSPStream
1184  * @rtptime: result RTP timestamp
1185  * @seq: result RTP seqnum
1186  *
1187  * Retrieve the current rtptime and seq. This is used to
1188  * construct a RTPInfo reply header.
1189  *
1190  * Returns: %TRUE when rtptime and seq could be determined.
1191  */
1192 gboolean
1193 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
1194     guint * rtptime, guint * seq)
1195 {
1196   GstRTSPStreamPrivate *priv;
1197   GObjectClass *payobjclass;
1198
1199   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1200   g_return_val_if_fail (rtptime != NULL, FALSE);
1201   g_return_val_if_fail (seq != NULL, FALSE);
1202
1203   priv = stream->priv;
1204
1205   payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
1206
1207   if (!g_object_class_find_property (payobjclass, "seqnum") ||
1208       !g_object_class_find_property (payobjclass, "timestamp"))
1209     return FALSE;
1210
1211   g_object_get (priv->payloader, "seqnum", seq, "timestamp", rtptime, NULL);
1212
1213   return TRUE;
1214 }
1215
1216 /**
1217  * gst_rtsp_stream_get_caps:
1218  * @stream: a #GstRTSPStream
1219  *
1220  * Retrieve the current caps of @stream.
1221  *
1222  * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
1223  *    after usage.
1224  */
1225 GstCaps *
1226 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
1227 {
1228   GstRTSPStreamPrivate *priv;
1229   GstCaps *result;
1230
1231   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1232
1233   priv = stream->priv;
1234
1235   g_mutex_lock (&priv->lock);
1236   if ((result = priv->caps))
1237     gst_caps_ref (result);
1238   g_mutex_unlock (&priv->lock);
1239
1240   return result;
1241 }
1242
1243 /**
1244  * gst_rtsp_stream_recv_rtp:
1245  * @stream: a #GstRTSPStream
1246  * @buffer: (transfer full): a #GstBuffer
1247  *
1248  * Handle an RTP buffer for the stream. This method is usually called when a
1249  * message has been received from a client using the TCP transport.
1250  *
1251  * This function takes ownership of @buffer.
1252  *
1253  * Returns: a GstFlowReturn.
1254  */
1255 GstFlowReturn
1256 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
1257 {
1258   GstRTSPStreamPrivate *priv;
1259   GstFlowReturn ret;
1260   GstElement *element;
1261
1262   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1263   priv = stream->priv;
1264   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1265   g_return_val_if_fail (priv->is_joined, FALSE);
1266
1267   g_mutex_lock (&priv->lock);
1268   element = gst_object_ref (priv->appsrc[0]);
1269   g_mutex_unlock (&priv->lock);
1270
1271   ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1272
1273   gst_object_unref (element);
1274
1275   return ret;
1276 }
1277
1278 /**
1279  * gst_rtsp_stream_recv_rtcp:
1280  * @stream: a #GstRTSPStream
1281  * @buffer: (transfer full): a #GstBuffer
1282  *
1283  * Handle an RTCP buffer for the stream. This method is usually called when a
1284  * message has been received from a client using the TCP transport.
1285  *
1286  * This function takes ownership of @buffer.
1287  *
1288  * Returns: a GstFlowReturn.
1289  */
1290 GstFlowReturn
1291 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
1292 {
1293   GstRTSPStreamPrivate *priv;
1294   GstFlowReturn ret;
1295   GstElement *element;
1296
1297   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1298   priv = stream->priv;
1299   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1300   g_return_val_if_fail (priv->is_joined, FALSE);
1301
1302   g_mutex_lock (&priv->lock);
1303   element = gst_object_ref (priv->appsrc[1]);
1304   g_mutex_unlock (&priv->lock);
1305
1306   ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1307
1308   gst_object_unref (element);
1309
1310   return ret;
1311 }
1312
1313 /* must be called with lock */
1314 static gboolean
1315 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
1316     gboolean add)
1317 {
1318   GstRTSPStreamPrivate *priv = stream->priv;
1319   const GstRTSPTransport *tr;
1320
1321   tr = gst_rtsp_stream_transport_get_transport (trans);
1322
1323   switch (tr->lower_transport) {
1324     case GST_RTSP_LOWER_TRANS_UDP:
1325     case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1326     {
1327       gchar *dest;
1328       gint min, max;
1329       guint ttl = 0;
1330
1331       dest = tr->destination;
1332       if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1333         min = tr->port.min;
1334         max = tr->port.max;
1335         ttl = tr->ttl;
1336       } else {
1337         min = tr->client_port.min;
1338         max = tr->client_port.max;
1339       }
1340
1341       if (add) {
1342         GST_INFO ("adding %s:%d-%d", dest, min, max);
1343         g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
1344         g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
1345         if (ttl > 0) {
1346           GST_INFO ("setting ttl-mc %d", ttl);
1347           g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
1348           g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
1349         }
1350         priv->transports = g_list_prepend (priv->transports, trans);
1351       } else {
1352         GST_INFO ("removing %s:%d-%d", dest, min, max);
1353         g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
1354         g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
1355         priv->transports = g_list_remove (priv->transports, trans);
1356       }
1357       break;
1358     }
1359     case GST_RTSP_LOWER_TRANS_TCP:
1360       if (add) {
1361         GST_INFO ("adding TCP %s", tr->destination);
1362         priv->transports = g_list_prepend (priv->transports, trans);
1363       } else {
1364         GST_INFO ("removing TCP %s", tr->destination);
1365         priv->transports = g_list_remove (priv->transports, trans);
1366       }
1367       break;
1368     default:
1369       goto unknown_transport;
1370   }
1371   return TRUE;
1372
1373   /* ERRORS */
1374 unknown_transport:
1375   {
1376     GST_INFO ("Unknown transport %d", tr->lower_transport);
1377     return FALSE;
1378   }
1379 }
1380
1381
1382 /**
1383  * gst_rtsp_stream_add_transport:
1384  * @stream: a #GstRTSPStream
1385  * @trans: a #GstRTSPStreamTransport
1386  *
1387  * Add the transport in @trans to @stream. The media of @stream will
1388  * then also be send to the values configured in @trans.
1389  *
1390  * @stream must be joined to a bin.
1391  *
1392  * @trans must contain a valid #GstRTSPTransport.
1393  *
1394  * Returns: %TRUE if @trans was added
1395  */
1396 gboolean
1397 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
1398     GstRTSPStreamTransport * trans)
1399 {
1400   GstRTSPStreamPrivate *priv;
1401   gboolean res;
1402
1403   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1404   priv = stream->priv;
1405   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1406   g_return_val_if_fail (priv->is_joined, FALSE);
1407
1408   g_mutex_lock (&priv->lock);
1409   res = update_transport (stream, trans, TRUE);
1410   g_mutex_unlock (&priv->lock);
1411
1412   return res;
1413 }
1414
1415 /**
1416  * gst_rtsp_stream_remove_transport:
1417  * @stream: a #GstRTSPStream
1418  * @trans: a #GstRTSPStreamTransport
1419  *
1420  * Remove the transport in @trans from @stream. The media of @stream will
1421  * not be sent to the values configured in @trans.
1422  *
1423  * @stream must be joined to a bin.
1424  *
1425  * @trans must contain a valid #GstRTSPTransport.
1426  *
1427  * Returns: %TRUE if @trans was removed
1428  */
1429 gboolean
1430 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
1431     GstRTSPStreamTransport * trans)
1432 {
1433   GstRTSPStreamPrivate *priv;
1434   gboolean res;
1435
1436   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1437   priv = stream->priv;
1438   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1439   g_return_val_if_fail (priv->is_joined, FALSE);
1440
1441   g_mutex_lock (&priv->lock);
1442   res = update_transport (stream, trans, FALSE);
1443   g_mutex_unlock (&priv->lock);
1444
1445   return res;
1446 }