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