stream: Properties are always there in Gst 1.0
[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 | GST_RTSP_ADDRESS_FLAG_MULTICAST, 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   g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
525   g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
526   g_object_set (G_OBJECT (udpsink0), "buffer-size", priv->buffer_size, NULL);
527
528   g_object_get (G_OBJECT (udpsrc1), "used-socket", &socket, NULL);
529   g_object_set (G_OBJECT (udpsink1), "socket", socket, NULL);
530   g_object_unref (socket);
531   g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
532   g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
533   g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
534   g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
535   g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
536   g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
537   g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
538
539   /* we keep these elements, we will further configure them when the
540    * client told us to really use the UDP ports. */
541   priv->udpsrc[0] = udpsrc0;
542   priv->udpsrc[1] = udpsrc1;
543   priv->udpsink[0] = udpsink0;
544   priv->udpsink[1] = udpsink1;
545   priv->server_port.min = rtpport;
546   priv->server_port.max = rtcpport;
547
548   return TRUE;
549
550   /* ERRORS */
551 no_udp_protocol:
552   {
553     goto cleanup;
554   }
555 no_ports:
556   {
557     goto cleanup;
558   }
559 no_udp_rtcp_protocol:
560   {
561     goto cleanup;
562   }
563 port_error:
564   {
565     goto cleanup;
566   }
567 cleanup:
568   {
569     if (udpsrc0) {
570       gst_element_set_state (udpsrc0, GST_STATE_NULL);
571       gst_object_unref (udpsrc0);
572     }
573     if (udpsrc1) {
574       gst_element_set_state (udpsrc1, GST_STATE_NULL);
575       gst_object_unref (udpsrc1);
576     }
577     if (udpsink0) {
578       gst_element_set_state (udpsink0, GST_STATE_NULL);
579       gst_object_unref (udpsink0);
580     }
581     if (udpsink1) {
582       gst_element_set_state (udpsink1, GST_STATE_NULL);
583       gst_object_unref (udpsink1);
584     }
585     return FALSE;
586   }
587 }
588
589 /**
590  * gst_rtsp_stream_get_server_port:
591  * @stream: a #GstRTSPStream
592  * @server_port: (out): result server port
593  *
594  * Fill @server_port with the port pair used by the server. This function can
595  * only be called when @stream has been joined.
596  */
597 void
598 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
599     GstRTSPRange * server_port)
600 {
601   GstRTSPStreamPrivate *priv;
602
603   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
604   priv = stream->priv;
605   g_return_if_fail (priv->is_joined);
606
607   g_mutex_lock (&priv->lock);
608   if (server_port)
609     *server_port = priv->server_port;
610   g_mutex_unlock (&priv->lock);
611 }
612
613 /**
614  * gst_rtsp_stream_get_ssrc:
615  * @stream: a #GstRTSPStream
616  * @ssrc: (out): result ssrc
617  *
618  * Get the SSRC used by the RTP session of this stream. This function can only
619  * be called when @stream has been joined.
620  */
621 void
622 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
623 {
624   GstRTSPStreamPrivate *priv;
625
626   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
627   priv = stream->priv;
628   g_return_if_fail (priv->is_joined);
629
630   g_mutex_lock (&priv->lock);
631   if (ssrc && priv->session)
632     g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
633   g_mutex_unlock (&priv->lock);
634 }
635
636 /* executed from streaming thread */
637 static void
638 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
639 {
640   GstRTSPStreamPrivate *priv = stream->priv;
641   GstCaps *newcaps, *oldcaps;
642
643   newcaps = gst_pad_get_current_caps (pad);
644
645   GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
646       newcaps);
647
648   g_mutex_lock (&priv->lock);
649   oldcaps = priv->caps;
650   priv->caps = newcaps;
651   g_mutex_unlock (&priv->lock);
652
653   if (oldcaps)
654     gst_caps_unref (oldcaps);
655 }
656
657 static void
658 dump_structure (const GstStructure * s)
659 {
660   gchar *sstr;
661
662   sstr = gst_structure_to_string (s);
663   GST_INFO ("structure: %s", sstr);
664   g_free (sstr);
665 }
666
667 static GstRTSPStreamTransport *
668 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
669 {
670   GstRTSPStreamPrivate *priv = stream->priv;
671   GList *walk;
672   GstRTSPStreamTransport *result = NULL;
673   const gchar *tmp;
674   gchar *dest;
675   guint port;
676
677   if (rtcp_from == NULL)
678     return NULL;
679
680   tmp = g_strrstr (rtcp_from, ":");
681   if (tmp == NULL)
682     return NULL;
683
684   port = atoi (tmp + 1);
685   dest = g_strndup (rtcp_from, tmp - rtcp_from);
686
687   g_mutex_lock (&priv->lock);
688   GST_INFO ("finding %s:%d in %d transports", dest, port,
689       g_list_length (priv->transports));
690
691   for (walk = priv->transports; walk; walk = g_list_next (walk)) {
692     GstRTSPStreamTransport *trans = walk->data;
693     const GstRTSPTransport *tr;
694     gint min, max;
695
696     tr = gst_rtsp_stream_transport_get_transport (trans);
697
698     min = tr->client_port.min;
699     max = tr->client_port.max;
700
701     if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
702       result = trans;
703       break;
704     }
705   }
706   g_mutex_unlock (&priv->lock);
707
708   g_free (dest);
709
710   return result;
711 }
712
713 static GstRTSPStreamTransport *
714 check_transport (GObject * source, GstRTSPStream * stream)
715 {
716   GstStructure *stats;
717   GstRTSPStreamTransport *trans;
718
719   /* see if we have a stream to match with the origin of the RTCP packet */
720   trans = g_object_get_qdata (source, ssrc_stream_map_key);
721   if (trans == NULL) {
722     g_object_get (source, "stats", &stats, NULL);
723     if (stats) {
724       const gchar *rtcp_from;
725
726       dump_structure (stats);
727
728       rtcp_from = gst_structure_get_string (stats, "rtcp-from");
729       if ((trans = find_transport (stream, rtcp_from))) {
730         GST_INFO ("%p: found transport %p for source  %p", stream, trans,
731             source);
732         g_object_set_qdata (source, ssrc_stream_map_key, trans);
733       }
734       gst_structure_free (stats);
735     }
736   }
737   return trans;
738 }
739
740
741 static void
742 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
743 {
744   GstRTSPStreamTransport *trans;
745
746   GST_INFO ("%p: new source %p", stream, source);
747
748   trans = check_transport (source, stream);
749
750   if (trans)
751     GST_INFO ("%p: source %p for transport %p", stream, source, trans);
752 }
753
754 static void
755 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
756 {
757   GST_INFO ("%p: new SDES %p", stream, source);
758 }
759
760 static void
761 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
762 {
763   GstRTSPStreamTransport *trans;
764
765   trans = check_transport (source, stream);
766
767   if (trans) {
768     GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
769     gst_rtsp_stream_transport_keep_alive (trans);
770   }
771 #ifdef DUMP_STATS
772   {
773     GstStructure *stats;
774     g_object_get (source, "stats", &stats, NULL);
775     if (stats) {
776       dump_structure (stats);
777       gst_structure_free (stats);
778     }
779   }
780 #endif
781 }
782
783 static void
784 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
785 {
786   GST_INFO ("%p: source %p bye", stream, source);
787 }
788
789 static void
790 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
791 {
792   GstRTSPStreamTransport *trans;
793
794   GST_INFO ("%p: source %p bye timeout", stream, source);
795
796   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
797     gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
798   }
799 }
800
801 static void
802 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
803 {
804   GstRTSPStreamTransport *trans;
805
806   GST_INFO ("%p: source %p 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 GstFlowReturn
814 handle_new_sample (GstAppSink * sink, gpointer user_data)
815 {
816   GstRTSPStreamPrivate *priv;
817   GList *walk;
818   GstSample *sample;
819   GstBuffer *buffer;
820   GstRTSPStream *stream;
821
822   sample = gst_app_sink_pull_sample (sink);
823   if (!sample)
824     return GST_FLOW_OK;
825
826   stream = (GstRTSPStream *) user_data;
827   priv = stream->priv;
828   buffer = gst_sample_get_buffer (sample);
829
830   g_mutex_lock (&priv->lock);
831   for (walk = priv->transports; walk; walk = g_list_next (walk)) {
832     GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
833
834     if (GST_ELEMENT_CAST (sink) == priv->appsink[0]) {
835       gst_rtsp_stream_transport_send_rtp (tr, buffer);
836     } else {
837       gst_rtsp_stream_transport_send_rtcp (tr, buffer);
838     }
839   }
840   g_mutex_unlock (&priv->lock);
841
842   gst_sample_unref (sample);
843
844   return GST_FLOW_OK;
845 }
846
847 static GstAppSinkCallbacks sink_cb = {
848   NULL,                         /* not interested in EOS */
849   NULL,                         /* not interested in preroll samples */
850   handle_new_sample,
851 };
852
853 /**
854  * gst_rtsp_stream_join_bin:
855  * @stream: a #GstRTSPStream
856  * @bin: a #GstBin to join
857  * @rtpbin: a rtpbin element in @bin
858  * @state: the target state of the new elements
859  *
860  * Join the #Gstbin @bin that contains the element @rtpbin.
861  *
862  * @stream will link to @rtpbin, which must be inside @bin. The elements
863  * added to @bin will be set to the state given in @state.
864  *
865  * Returns: %TRUE on success.
866  */
867 gboolean
868 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
869     GstElement * rtpbin, GstState state)
870 {
871   GstRTSPStreamPrivate *priv;
872   gint i, idx;
873   gchar *name;
874   GstPad *pad, *teepad, *queuepad, *selpad;
875   GstPadLinkReturn ret;
876
877   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
878   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
879   g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
880
881   priv = stream->priv;
882
883   g_mutex_lock (&priv->lock);
884   if (priv->is_joined)
885     goto was_joined;
886
887   /* create a session with the same index as the stream */
888   idx = priv->idx;
889
890   GST_INFO ("stream %p joining bin as session %d", stream, idx);
891
892   if (!alloc_ports (stream))
893     goto no_ports;
894
895   /* get a pad for sending RTP */
896   name = g_strdup_printf ("send_rtp_sink_%u", idx);
897   priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
898   g_free (name);
899   /* link the RTP pad to the session manager, it should not really fail unless
900    * this is not really an RTP pad */
901   ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
902   if (ret != GST_PAD_LINK_OK)
903     goto link_failed;
904
905   /* get pads from the RTP session element for sending and receiving
906    * RTP/RTCP*/
907   name = g_strdup_printf ("send_rtp_src_%u", idx);
908   priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
909   g_free (name);
910   name = g_strdup_printf ("send_rtcp_src_%u", idx);
911   priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
912   g_free (name);
913   name = g_strdup_printf ("recv_rtp_sink_%u", idx);
914   priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
915   g_free (name);
916   name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
917   priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
918   g_free (name);
919
920   /* get the session */
921   g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
922
923   g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
924       stream);
925   g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
926       stream);
927   g_signal_connect (priv->session, "on-ssrc-active",
928       (GCallback) on_ssrc_active, stream);
929   g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
930       stream);
931   g_signal_connect (priv->session, "on-bye-timeout",
932       (GCallback) on_bye_timeout, stream);
933   g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
934       stream);
935
936   for (i = 0; i < 2; i++) {
937     /* For the sender we create this bit of pipeline for both
938      * RTP and RTCP. Sync and preroll are enabled on udpsink so
939      * we need to add a queue before appsink to make the pipeline
940      * not block. For the TCP case, we want to pump data to the
941      * client as fast as possible anyway.
942      *
943      * .--------.      .-----.    .---------.
944      * | rtpbin |      | tee |    | udpsink |
945      * |       send->sink   src->sink       |
946      * '--------'      |     |    '---------'
947      *                 |     |    .---------.    .---------.
948      *                 |     |    |  queue  |    | appsink |
949      *                 |    src->sink      src->sink       |
950      *                 '-----'    '---------'    '---------'
951      */
952     /* make tee for RTP/RTCP */
953     priv->tee[i] = gst_element_factory_make ("tee", NULL);
954     gst_bin_add (bin, priv->tee[i]);
955
956     /* and link to rtpbin send pad */
957     pad = gst_element_get_static_pad (priv->tee[i], "sink");
958     gst_pad_link (priv->send_src[i], pad);
959     gst_object_unref (pad);
960
961     /* add udpsink */
962     gst_bin_add (bin, priv->udpsink[i]);
963
964     /* link tee to udpsink */
965     teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
966     pad = gst_element_get_static_pad (priv->udpsink[i], "sink");
967     gst_pad_link (teepad, pad);
968     gst_object_unref (pad);
969     gst_object_unref (teepad);
970
971     /* make queue */
972     priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
973     gst_bin_add (bin, priv->appqueue[i]);
974     /* and link to tee */
975     teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
976     pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
977     gst_pad_link (teepad, pad);
978     gst_object_unref (pad);
979     gst_object_unref (teepad);
980
981     /* make appsink */
982     priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
983     g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
984     g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
985     gst_bin_add (bin, priv->appsink[i]);
986     gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
987         &sink_cb, stream, NULL);
988     /* and link to queue */
989     queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
990     pad = gst_element_get_static_pad (priv->appsink[i], "sink");
991     gst_pad_link (queuepad, pad);
992     gst_object_unref (pad);
993     gst_object_unref (queuepad);
994
995     /* For the receiver we create this bit of pipeline for both
996      * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
997      * and it is all funneled into the rtpbin receive pad.
998      *
999      * .--------.     .--------.    .--------.
1000      * | udpsrc |     | funnel |    | rtpbin |
1001      * |       src->sink      src->sink      |
1002      * '--------'     |        |    '--------'
1003      * .--------.     |        |
1004      * | appsrc |     |        |
1005      * |       src->sink       |
1006      * '--------'     '--------'
1007      */
1008     /* make funnel for the RTP/RTCP receivers */
1009     priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1010     gst_bin_add (bin, priv->funnel[i]);
1011
1012     pad = gst_element_get_static_pad (priv->funnel[i], "src");
1013     gst_pad_link (pad, priv->recv_sink[i]);
1014     gst_object_unref (pad);
1015
1016     /* we set and keep these to playing so that they don't cause NO_PREROLL return
1017      * values */
1018     gst_element_set_state (priv->udpsrc[i], GST_STATE_PLAYING);
1019     gst_element_set_locked_state (priv->udpsrc[i], TRUE);
1020     /* add udpsrc */
1021     gst_bin_add (bin, priv->udpsrc[i]);
1022     /* and link to the funnel */
1023     selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1024     pad = gst_element_get_static_pad (priv->udpsrc[i], "src");
1025     gst_pad_link (pad, selpad);
1026     gst_object_unref (pad);
1027     gst_object_unref (selpad);
1028
1029     /* make and add appsrc */
1030     priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1031     gst_bin_add (bin, priv->appsrc[i]);
1032     /* and link to the funnel */
1033     selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
1034     pad = gst_element_get_static_pad (priv->appsrc[i], "src");
1035     gst_pad_link (pad, selpad);
1036     gst_object_unref (pad);
1037     gst_object_unref (selpad);
1038
1039     /* check if we need to set to a special state */
1040     if (state != GST_STATE_NULL) {
1041       gst_element_set_state (priv->udpsink[i], state);
1042       gst_element_set_state (priv->appsink[i], state);
1043       gst_element_set_state (priv->appqueue[i], state);
1044       gst_element_set_state (priv->tee[i], state);
1045       gst_element_set_state (priv->funnel[i], state);
1046       gst_element_set_state (priv->appsrc[i], state);
1047     }
1048   }
1049
1050   /* be notified of caps changes */
1051   priv->caps_sig = g_signal_connect (priv->send_rtp_sink, "notify::caps",
1052       (GCallback) caps_notify, stream);
1053
1054   priv->is_joined = TRUE;
1055   g_mutex_unlock (&priv->lock);
1056
1057   return TRUE;
1058
1059   /* ERRORS */
1060 was_joined:
1061   {
1062     g_mutex_unlock (&priv->lock);
1063     return TRUE;
1064   }
1065 no_ports:
1066   {
1067     g_mutex_unlock (&priv->lock);
1068     GST_WARNING ("failed to allocate ports %d", idx);
1069     return FALSE;
1070   }
1071 link_failed:
1072   {
1073     GST_WARNING ("failed to link stream %d", idx);
1074     gst_object_unref (priv->send_rtp_sink);
1075     priv->send_rtp_sink = NULL;
1076     g_mutex_unlock (&priv->lock);
1077     return FALSE;
1078   }
1079 }
1080
1081 /**
1082  * gst_rtsp_stream_leave_bin:
1083  * @stream: a #GstRTSPStream
1084  * @bin: a #GstBin
1085  * @rtpbin: a rtpbin #GstElement
1086  *
1087  * Remove the elements of @stream from @bin. @bin must be set
1088  * to the NULL state before calling this.
1089  *
1090  * Return: %TRUE on success.
1091  */
1092 gboolean
1093 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
1094     GstElement * rtpbin)
1095 {
1096   GstRTSPStreamPrivate *priv;
1097   gint i;
1098
1099   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1100   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1101   g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1102
1103   priv = stream->priv;
1104
1105   g_mutex_lock (&priv->lock);
1106   if (!priv->is_joined)
1107     goto was_not_joined;
1108
1109   /* all transports must be removed by now */
1110   g_return_val_if_fail (priv->transports == NULL, FALSE);
1111
1112   GST_INFO ("stream %p leaving bin", stream);
1113
1114   gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
1115   g_signal_handler_disconnect (priv->send_rtp_sink, priv->caps_sig);
1116   gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
1117   gst_object_unref (priv->send_rtp_sink);
1118   priv->send_rtp_sink = NULL;
1119
1120   for (i = 0; i < 2; i++) {
1121     /* and set udpsrc to NULL now before removing */
1122     gst_element_set_locked_state (priv->udpsrc[i], FALSE);
1123     gst_element_set_state (priv->udpsrc[i], GST_STATE_NULL);
1124
1125     /* removing them should also nicely release the request
1126      * pads when they finalize */
1127     gst_bin_remove (bin, priv->udpsrc[i]);
1128     gst_bin_remove (bin, priv->udpsink[i]);
1129     gst_bin_remove (bin, priv->appsrc[i]);
1130     gst_bin_remove (bin, priv->appsink[i]);
1131     gst_bin_remove (bin, priv->appqueue[i]);
1132     gst_bin_remove (bin, priv->tee[i]);
1133     gst_bin_remove (bin, priv->funnel[i]);
1134
1135     gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
1136     gst_object_unref (priv->recv_sink[i]);
1137     priv->recv_sink[i] = NULL;
1138
1139     priv->udpsrc[i] = NULL;
1140     priv->udpsink[i] = NULL;
1141     priv->appsrc[i] = NULL;
1142     priv->appsink[i] = NULL;
1143     priv->appqueue[i] = NULL;
1144     priv->tee[i] = NULL;
1145     priv->funnel[i] = NULL;
1146   }
1147   gst_object_unref (priv->send_src[0]);
1148   priv->send_src[0] = NULL;
1149
1150   gst_element_release_request_pad (rtpbin, priv->send_src[1]);
1151   gst_object_unref (priv->send_src[1]);
1152   priv->send_src[1] = NULL;
1153
1154   g_object_unref (priv->session);
1155   if (priv->caps)
1156     gst_caps_unref (priv->caps);
1157
1158   priv->is_joined = FALSE;
1159   g_mutex_unlock (&priv->lock);
1160
1161   return TRUE;
1162
1163 was_not_joined:
1164   {
1165     return TRUE;
1166   }
1167 }
1168
1169 /**
1170  * gst_rtsp_stream_get_rtpinfo:
1171  * @stream: a #GstRTSPStream
1172  * @rtptime: result RTP timestamp
1173  * @seq: result RTP seqnum
1174  *
1175  * Retrieve the current rtptime and seq. This is used to
1176  * construct a RTPInfo reply header.
1177  *
1178  * Returns: %TRUE when rtptime and seq could be determined.
1179  */
1180 gboolean
1181 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
1182     guint * rtptime, guint * seq)
1183 {
1184   GstRTSPStreamPrivate *priv;
1185   GObjectClass *payobjclass;
1186
1187   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1188   g_return_val_if_fail (rtptime != NULL, FALSE);
1189   g_return_val_if_fail (seq != NULL, FALSE);
1190
1191   priv = stream->priv;
1192
1193   payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
1194
1195   if (!g_object_class_find_property (payobjclass, "seqnum") ||
1196       !g_object_class_find_property (payobjclass, "timestamp"))
1197     return FALSE;
1198
1199   g_object_get (priv->payloader, "seqnum", seq, "timestamp", rtptime, NULL);
1200
1201   return TRUE;
1202 }
1203
1204 /**
1205  * gst_rtsp_stream_get_caps:
1206  * @stream: a #GstRTSPStream
1207  *
1208  * Retrieve the current caps of @stream.
1209  *
1210  * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
1211  *    after usage.
1212  */
1213 GstCaps *
1214 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
1215 {
1216   GstRTSPStreamPrivate *priv;
1217   GstCaps *result;
1218
1219   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1220
1221   priv = stream->priv;
1222
1223   g_mutex_lock (&priv->lock);
1224   if ((result = priv->caps))
1225     gst_caps_ref (result);
1226   g_mutex_unlock (&priv->lock);
1227
1228   return result;
1229 }
1230
1231 /**
1232  * gst_rtsp_stream_recv_rtp:
1233  * @stream: a #GstRTSPStream
1234  * @buffer: (transfer full): a #GstBuffer
1235  *
1236  * Handle an RTP buffer for the stream. This method is usually called when a
1237  * message has been received from a client using the TCP transport.
1238  *
1239  * This function takes ownership of @buffer.
1240  *
1241  * Returns: a GstFlowReturn.
1242  */
1243 GstFlowReturn
1244 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
1245 {
1246   GstRTSPStreamPrivate *priv;
1247   GstFlowReturn ret;
1248   GstElement *element;
1249
1250   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1251   priv = stream->priv;
1252   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1253   g_return_val_if_fail (priv->is_joined, FALSE);
1254
1255   g_mutex_lock (&priv->lock);
1256   element = gst_object_ref (priv->appsrc[0]);
1257   g_mutex_unlock (&priv->lock);
1258
1259   ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1260
1261   gst_object_unref (element);
1262
1263   return ret;
1264 }
1265
1266 /**
1267  * gst_rtsp_stream_recv_rtcp:
1268  * @stream: a #GstRTSPStream
1269  * @buffer: (transfer full): a #GstBuffer
1270  *
1271  * Handle an RTCP buffer for the stream. This method is usually called when a
1272  * message has been received from a client using the TCP transport.
1273  *
1274  * This function takes ownership of @buffer.
1275  *
1276  * Returns: a GstFlowReturn.
1277  */
1278 GstFlowReturn
1279 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
1280 {
1281   GstRTSPStreamPrivate *priv;
1282   GstFlowReturn ret;
1283   GstElement *element;
1284
1285   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
1286   priv = stream->priv;
1287   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
1288   g_return_val_if_fail (priv->is_joined, FALSE);
1289
1290   g_mutex_lock (&priv->lock);
1291   element = gst_object_ref (priv->appsrc[1]);
1292   g_mutex_unlock (&priv->lock);
1293
1294   ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
1295
1296   gst_object_unref (element);
1297
1298   return ret;
1299 }
1300
1301 /* must be called with lock */
1302 static gboolean
1303 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
1304     gboolean add)
1305 {
1306   GstRTSPStreamPrivate *priv = stream->priv;
1307   const GstRTSPTransport *tr;
1308
1309   tr = gst_rtsp_stream_transport_get_transport (trans);
1310
1311   switch (tr->lower_transport) {
1312     case GST_RTSP_LOWER_TRANS_UDP:
1313     case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1314     {
1315       gchar *dest;
1316       gint min, max;
1317       guint ttl = 0;
1318
1319       dest = tr->destination;
1320       if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1321         min = tr->port.min;
1322         max = tr->port.max;
1323         ttl = tr->ttl;
1324       } else {
1325         min = tr->client_port.min;
1326         max = tr->client_port.max;
1327       }
1328
1329       if (add) {
1330         GST_INFO ("adding %s:%d-%d", dest, min, max);
1331         g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
1332         g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
1333         if (ttl > 0) {
1334           GST_INFO ("setting ttl-mc %d", ttl);
1335           g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
1336           g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
1337         }
1338         priv->transports = g_list_prepend (priv->transports, trans);
1339       } else {
1340         GST_INFO ("removing %s:%d-%d", dest, min, max);
1341         g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
1342         g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
1343         priv->transports = g_list_remove (priv->transports, trans);
1344       }
1345       break;
1346     }
1347     case GST_RTSP_LOWER_TRANS_TCP:
1348       if (add) {
1349         GST_INFO ("adding TCP %s", tr->destination);
1350         priv->transports = g_list_prepend (priv->transports, trans);
1351       } else {
1352         GST_INFO ("removing TCP %s", tr->destination);
1353         priv->transports = g_list_remove (priv->transports, trans);
1354       }
1355       break;
1356     default:
1357       goto unknown_transport;
1358   }
1359   return TRUE;
1360
1361   /* ERRORS */
1362 unknown_transport:
1363   {
1364     GST_INFO ("Unknown transport %d", tr->lower_transport);
1365     return FALSE;
1366   }
1367 }
1368
1369
1370 /**
1371  * gst_rtsp_stream_add_transport:
1372  * @stream: a #GstRTSPStream
1373  * @trans: a #GstRTSPStreamTransport
1374  *
1375  * Add the transport in @trans to @stream. The media of @stream will
1376  * then also be send to the values configured in @trans.
1377  *
1378  * @stream must be joined to a bin.
1379  *
1380  * @trans must contain a valid #GstRTSPTransport.
1381  *
1382  * Returns: %TRUE if @trans was added
1383  */
1384 gboolean
1385 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
1386     GstRTSPStreamTransport * trans)
1387 {
1388   GstRTSPStreamPrivate *priv;
1389   gboolean res;
1390
1391   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1392   priv = stream->priv;
1393   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1394   g_return_val_if_fail (priv->is_joined, FALSE);
1395
1396   g_mutex_lock (&priv->lock);
1397   res = update_transport (stream, trans, TRUE);
1398   g_mutex_unlock (&priv->lock);
1399
1400   return res;
1401 }
1402
1403 /**
1404  * gst_rtsp_stream_remove_transport:
1405  * @stream: a #GstRTSPStream
1406  * @trans: a #GstRTSPStreamTransport
1407  *
1408  * Remove the transport in @trans from @stream. The media of @stream will
1409  * not be sent to the values configured in @trans.
1410  *
1411  * @stream must be joined to a bin.
1412  *
1413  * @trans must contain a valid #GstRTSPTransport.
1414  *
1415  * Returns: %TRUE if @trans was removed
1416  */
1417 gboolean
1418 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
1419     GstRTSPStreamTransport * trans)
1420 {
1421   GstRTSPStreamPrivate *priv;
1422   gboolean res;
1423
1424   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1425   priv = stream->priv;
1426   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1427   g_return_val_if_fail (priv->is_joined, FALSE);
1428
1429   g_mutex_lock (&priv->lock);
1430   res = update_transport (stream, trans, FALSE);
1431   g_mutex_unlock (&priv->lock);
1432
1433   return res;
1434 }