stream: add locking
[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 enum
31 {
32   PROP_0,
33   PROP_LAST
34 };
35
36 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
37 #define GST_CAT_DEFAULT rtsp_stream_debug
38
39 static GQuark ssrc_stream_map_key;
40
41 static void gst_rtsp_stream_finalize (GObject * obj);
42
43 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
44
45 static void
46 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
47 {
48   GObjectClass *gobject_class;
49
50   gobject_class = G_OBJECT_CLASS (klass);
51
52   gobject_class->finalize = gst_rtsp_stream_finalize;
53
54   GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
55
56   ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
57 }
58
59 static void
60 gst_rtsp_stream_init (GstRTSPStream * stream)
61 {
62   g_mutex_init (&stream->lock);
63 }
64
65 static void
66 gst_rtsp_stream_finalize (GObject * obj)
67 {
68   GstRTSPStream *stream;
69
70   stream = GST_RTSP_STREAM (obj);
71
72   /* we really need to be unjoined now */
73   g_return_if_fail (!stream->is_joined);
74
75   gst_object_unref (stream->payloader);
76   gst_object_unref (stream->srcpad);
77   g_mutex_clear (&stream->lock);
78
79   G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
80 }
81
82 /**
83  * gst_rtsp_stream_new:
84  * @idx: an index
85  * @srcpad: a #GstPad
86  * @payloader: a #GstElement
87  *
88  * Create a new media stream with index @idx that handles RTP data on
89  * @srcpad and has a payloader element @payloader.
90  *
91  * Returns: a new #GstRTSPStream
92  */
93 GstRTSPStream *
94 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
95 {
96   GstRTSPStream *stream;
97
98   g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
99   g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
100   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
101
102   stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
103   stream->idx = idx;
104   stream->payloader = gst_object_ref (payloader);
105   stream->srcpad = gst_object_ref (srcpad);
106
107   return stream;
108 }
109
110 /**
111  * gst_rtsp_stream_set_mtu:
112  * @stream: a #GstRTSPStream
113  * @mtu: a new MTU
114  *
115  * Configure the mtu in the payloader of @stream to @mtu.
116  */
117 void
118 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
119 {
120   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
121
122   g_object_set (G_OBJECT (stream->payloader), "mtu", mtu, NULL);
123 }
124
125 /**
126  * gst_rtsp_stream_get_mtu:
127  * @stream: a #GstRTSPStream
128  *
129  * Get the configured MTU in the payloader of @stream.
130  *
131  * Returns: the MTU of the payloader.
132  */
133 guint
134 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
135 {
136   guint mtu;
137
138   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
139
140   g_object_get (G_OBJECT (stream->payloader), "mtu", &mtu, NULL);
141
142   return mtu;
143 }
144
145 /* must be called with lock */
146 static gboolean
147 alloc_ports (GstRTSPStream * stream)
148 {
149   GstStateChangeReturn ret;
150   GstElement *udpsrc0, *udpsrc1;
151   GstElement *udpsink0, *udpsink1;
152   gint tmp_rtp, tmp_rtcp;
153   guint count;
154   gint rtpport, rtcpport;
155   GSocket *socket;
156   const gchar *host;
157
158   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
159
160   udpsrc0 = NULL;
161   udpsrc1 = NULL;
162   udpsink0 = NULL;
163   udpsink1 = NULL;
164   count = 0;
165
166   /* Start with random port */
167   tmp_rtp = 0;
168
169   if (stream->is_ipv6)
170     host = "udp://[::0]";
171   else
172     host = "udp://0.0.0.0";
173
174   /* try to allocate 2 UDP ports, the RTP port should be an even
175    * number and the RTCP port should be the next (uneven) port */
176 again:
177   udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
178   if (udpsrc0 == NULL)
179     goto no_udp_protocol;
180   g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, NULL);
181
182   ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
183   if (ret == GST_STATE_CHANGE_FAILURE) {
184     if (tmp_rtp != 0) {
185       tmp_rtp += 2;
186       if (++count > 20)
187         goto no_ports;
188
189       gst_element_set_state (udpsrc0, GST_STATE_NULL);
190       gst_object_unref (udpsrc0);
191
192       goto again;
193     }
194     goto no_udp_protocol;
195   }
196
197   g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
198
199   /* check if port is even */
200   if ((tmp_rtp & 1) != 0) {
201     /* port not even, close and allocate another */
202     if (++count > 20)
203       goto no_ports;
204
205     gst_element_set_state (udpsrc0, GST_STATE_NULL);
206     gst_object_unref (udpsrc0);
207
208     tmp_rtp++;
209     goto again;
210   }
211
212   /* allocate port+1 for RTCP now */
213   udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
214   if (udpsrc1 == NULL)
215     goto no_udp_rtcp_protocol;
216
217   /* set port */
218   tmp_rtcp = tmp_rtp + 1;
219   g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
220
221   ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
222   /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
223   if (ret == GST_STATE_CHANGE_FAILURE) {
224
225     if (++count > 20)
226       goto no_ports;
227
228     gst_element_set_state (udpsrc0, GST_STATE_NULL);
229     gst_object_unref (udpsrc0);
230
231     gst_element_set_state (udpsrc1, GST_STATE_NULL);
232     gst_object_unref (udpsrc1);
233
234     tmp_rtp += 2;
235     goto again;
236   }
237   /* all fine, do port check */
238   g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
239   g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
240
241   /* this should not happen... */
242   if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
243     goto port_error;
244
245   udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
246   if (!udpsink0)
247     goto no_udp_protocol;
248
249   g_object_get (G_OBJECT (udpsrc0), "socket", &socket, NULL);
250   g_object_set (G_OBJECT (udpsink0), "socket", socket, NULL);
251   g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
252
253   udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
254   if (!udpsink1)
255     goto no_udp_protocol;
256
257   if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
258           "send-duplicates")) {
259     g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
260     g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
261   } else {
262     g_warning
263         ("old multiudpsink version found without send-duplicates property");
264   }
265
266   if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
267           "buffer-size")) {
268     g_object_set (G_OBJECT (udpsink0), "buffer-size", stream->buffer_size,
269         NULL);
270   } else {
271     GST_WARNING ("multiudpsink version found without buffer-size property");
272   }
273
274   g_object_get (G_OBJECT (udpsrc1), "socket", &socket, NULL);
275   g_object_set (G_OBJECT (udpsink1), "socket", socket, NULL);
276   g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
277   g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
278   g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
279   g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
280   g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
281   g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
282   g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
283
284   /* we keep these elements, we will further configure them when the
285    * client told us to really use the UDP ports. */
286   stream->udpsrc[0] = udpsrc0;
287   stream->udpsrc[1] = udpsrc1;
288   stream->udpsink[0] = udpsink0;
289   stream->udpsink[1] = udpsink1;
290   stream->server_port.min = rtpport;
291   stream->server_port.max = rtcpport;
292
293   return TRUE;
294
295   /* ERRORS */
296 no_udp_protocol:
297   {
298     goto cleanup;
299   }
300 no_ports:
301   {
302     goto cleanup;
303   }
304 no_udp_rtcp_protocol:
305   {
306     goto cleanup;
307   }
308 port_error:
309   {
310     goto cleanup;
311   }
312 cleanup:
313   {
314     if (udpsrc0) {
315       gst_element_set_state (udpsrc0, GST_STATE_NULL);
316       gst_object_unref (udpsrc0);
317     }
318     if (udpsrc1) {
319       gst_element_set_state (udpsrc1, GST_STATE_NULL);
320       gst_object_unref (udpsrc1);
321     }
322     if (udpsink0) {
323       gst_element_set_state (udpsink0, GST_STATE_NULL);
324       gst_object_unref (udpsink0);
325     }
326     if (udpsink1) {
327       gst_element_set_state (udpsink1, GST_STATE_NULL);
328       gst_object_unref (udpsink1);
329     }
330     return FALSE;
331   }
332 }
333
334 /* executed from streaming thread */
335 static void
336 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
337 {
338   GstCaps *newcaps, *oldcaps;
339
340   newcaps = gst_pad_get_current_caps (pad);
341
342   GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
343       newcaps);
344
345   g_mutex_lock (&stream->lock);
346   oldcaps = stream->caps;
347   stream->caps = newcaps;
348   g_mutex_unlock (&stream->lock);
349
350   if (oldcaps)
351     gst_caps_unref (oldcaps);
352 }
353
354 static void
355 dump_structure (const GstStructure * s)
356 {
357   gchar *sstr;
358
359   sstr = gst_structure_to_string (s);
360   GST_INFO ("structure: %s", sstr);
361   g_free (sstr);
362 }
363
364 static GstRTSPStreamTransport *
365 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
366 {
367   GList *walk;
368   GstRTSPStreamTransport *result = NULL;
369   const gchar *tmp;
370   gchar *dest;
371   guint port;
372
373   if (rtcp_from == NULL)
374     return NULL;
375
376   tmp = g_strrstr (rtcp_from, ":");
377   if (tmp == NULL)
378     return NULL;
379
380   port = atoi (tmp + 1);
381   dest = g_strndup (rtcp_from, tmp - rtcp_from);
382
383   g_mutex_lock (&stream->lock);
384   GST_INFO ("finding %s:%d in %d transports", dest, port,
385       g_list_length (stream->transports));
386
387   for (walk = stream->transports; walk; walk = g_list_next (walk)) {
388     GstRTSPStreamTransport *trans = walk->data;
389     gint min, max;
390
391     min = trans->transport->client_port.min;
392     max = trans->transport->client_port.max;
393
394     if ((strcmp (trans->transport->destination, dest) == 0) && (min == port
395             || max == port)) {
396       result = trans;
397       break;
398     }
399   }
400   g_mutex_unlock (&stream->lock);
401
402   g_free (dest);
403
404   return result;
405 }
406
407 static GstRTSPStreamTransport *
408 check_transport (GObject * source, GstRTSPStream * stream)
409 {
410   GstStructure *stats;
411   GstRTSPStreamTransport *trans;
412
413   /* see if we have a stream to match with the origin of the RTCP packet */
414   trans = g_object_get_qdata (source, ssrc_stream_map_key);
415   if (trans == NULL) {
416     g_object_get (source, "stats", &stats, NULL);
417     if (stats) {
418       const gchar *rtcp_from;
419
420       dump_structure (stats);
421
422       rtcp_from = gst_structure_get_string (stats, "rtcp-from");
423       if ((trans = find_transport (stream, rtcp_from))) {
424         GST_INFO ("%p: found transport %p for source  %p", stream, trans,
425             source);
426
427         /* keep ref to the source */
428         trans->rtpsource = source;
429
430         g_object_set_qdata (source, ssrc_stream_map_key, trans);
431       }
432       gst_structure_free (stats);
433     }
434   }
435
436   return trans;
437 }
438
439
440 static void
441 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
442 {
443   GstRTSPStreamTransport *trans;
444
445   GST_INFO ("%p: new source %p", stream, source);
446
447   trans = check_transport (source, stream);
448
449   if (trans)
450     GST_INFO ("%p: source %p for transport %p", stream, source, trans);
451 }
452
453 static void
454 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
455 {
456   GST_INFO ("%p: new SDES %p", stream, source);
457 }
458
459 static void
460 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
461 {
462   GstRTSPStreamTransport *trans;
463
464   trans = check_transport (source, stream);
465
466   if (trans) {
467     GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
468     gst_rtsp_stream_transport_keep_alive (trans);
469   }
470 #ifdef DUMP_STATS
471   {
472     GstStructure *stats;
473     g_object_get (source, "stats", &stats, NULL);
474     if (stats) {
475       dump_structure (stats);
476       gst_structure_free (stats);
477     }
478   }
479 #endif
480 }
481
482 static void
483 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
484 {
485   GST_INFO ("%p: source %p bye", stream, source);
486 }
487
488 static void
489 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
490 {
491   GstRTSPStreamTransport *trans;
492
493   GST_INFO ("%p: source %p bye timeout", stream, source);
494
495   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
496     trans->rtpsource = NULL;
497     trans->timeout = TRUE;
498   }
499 }
500
501 static void
502 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
503 {
504   GstRTSPStreamTransport *trans;
505
506   GST_INFO ("%p: source %p timeout", stream, source);
507
508   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
509     trans->rtpsource = NULL;
510     trans->timeout = TRUE;
511   }
512 }
513
514 static GstFlowReturn
515 handle_new_sample (GstAppSink * sink, gpointer user_data)
516 {
517   GList *walk;
518   GstSample *sample;
519   GstBuffer *buffer;
520   GstRTSPStream *stream;
521
522   sample = gst_app_sink_pull_sample (sink);
523   if (!sample)
524     return GST_FLOW_OK;
525
526   stream = (GstRTSPStream *) user_data;
527   buffer = gst_sample_get_buffer (sample);
528
529   g_mutex_lock (&stream->lock);
530   for (walk = stream->transports; walk; walk = g_list_next (walk)) {
531     GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
532
533     if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
534       gst_rtsp_stream_transport_send_rtp (tr, buffer);
535     } else {
536       gst_rtsp_stream_transport_send_rtcp (tr, buffer);
537     }
538   }
539   g_mutex_unlock (&stream->lock);
540
541   gst_sample_unref (sample);
542
543   return GST_FLOW_OK;
544 }
545
546 static GstAppSinkCallbacks sink_cb = {
547   NULL,                         /* not interested in EOS */
548   NULL,                         /* not interested in preroll samples */
549   handle_new_sample,
550 };
551
552 /**
553  * gst_rtsp_stream_join_bin:
554  * @stream: a #GstRTSPStream
555  * @bin: a #GstBin to join
556  * @rtpbin: a rtpbin element in @bin
557  * @state: the target state of the new elements
558  *
559  * Join the #Gstbin @bin that contains the element @rtpbin.
560  *
561  * @stream will link to @rtpbin, which must be inside @bin. The elements
562  * added to @bin will be set to the state given in @state.
563  *
564  * Returns: %TRUE on success.
565  */
566 gboolean
567 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
568     GstElement * rtpbin, GstState state)
569 {
570   gint i, idx;
571   gchar *name;
572   GstPad *pad, *teepad, *queuepad, *selpad;
573   GstPadLinkReturn ret;
574
575   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
576   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
577   g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
578
579   g_mutex_lock (&stream->lock);
580   if (stream->is_joined)
581     goto was_joined;
582
583   /* create a session with the same index as the stream */
584   idx = stream->idx;
585
586   GST_INFO ("stream %p joining bin as session %d", stream, idx);
587
588   if (!alloc_ports (stream))
589     goto no_ports;
590
591   /* get a pad for sending RTP */
592   name = g_strdup_printf ("send_rtp_sink_%u", idx);
593   stream->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
594   g_free (name);
595   /* link the RTP pad to the session manager, it should not really fail unless
596    * this is not really an RTP pad */
597   ret = gst_pad_link (stream->srcpad, stream->send_rtp_sink);
598   if (ret != GST_PAD_LINK_OK)
599     goto link_failed;
600
601   /* get pads from the RTP session element for sending and receiving
602    * RTP/RTCP*/
603   name = g_strdup_printf ("send_rtp_src_%u", idx);
604   stream->send_src[0] = gst_element_get_static_pad (rtpbin, name);
605   g_free (name);
606   name = g_strdup_printf ("send_rtcp_src_%u", idx);
607   stream->send_src[1] = gst_element_get_request_pad (rtpbin, name);
608   g_free (name);
609   name = g_strdup_printf ("recv_rtp_sink_%u", idx);
610   stream->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
611   g_free (name);
612   name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
613   stream->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
614   g_free (name);
615
616   /* get the session */
617   g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &stream->session);
618
619   g_signal_connect (stream->session, "on-new-ssrc", (GCallback) on_new_ssrc,
620       stream);
621   g_signal_connect (stream->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
622       stream);
623   g_signal_connect (stream->session, "on-ssrc-active",
624       (GCallback) on_ssrc_active, stream);
625   g_signal_connect (stream->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
626       stream);
627   g_signal_connect (stream->session, "on-bye-timeout",
628       (GCallback) on_bye_timeout, stream);
629   g_signal_connect (stream->session, "on-timeout", (GCallback) on_timeout,
630       stream);
631
632   for (i = 0; i < 2; i++) {
633     /* For the sender we create this bit of pipeline for both
634      * RTP and RTCP. Sync and preroll are enabled on udpsink so
635      * we need to add a queue before appsink to make the pipeline
636      * not block. For the TCP case, we want to pump data to the
637      * client as fast as possible anyway.
638      *
639      * .--------.      .-----.    .---------.
640      * | rtpbin |      | tee |    | udpsink |
641      * |       send->sink   src->sink       |
642      * '--------'      |     |    '---------'
643      *                 |     |    .---------.    .---------.
644      *                 |     |    |  queue  |    | appsink |
645      *                 |    src->sink      src->sink       |
646      *                 '-----'    '---------'    '---------'
647      */
648     /* make tee for RTP/RTCP */
649     stream->tee[i] = gst_element_factory_make ("tee", NULL);
650     gst_bin_add (bin, stream->tee[i]);
651
652     /* and link to rtpbin send pad */
653     pad = gst_element_get_static_pad (stream->tee[i], "sink");
654     gst_pad_link (stream->send_src[i], pad);
655     gst_object_unref (pad);
656
657     /* add udpsink */
658     gst_bin_add (bin, stream->udpsink[i]);
659
660     /* link tee to udpsink */
661     teepad = gst_element_get_request_pad (stream->tee[i], "src_%u");
662     pad = gst_element_get_static_pad (stream->udpsink[i], "sink");
663     gst_pad_link (teepad, pad);
664     gst_object_unref (pad);
665     gst_object_unref (teepad);
666
667     /* make queue */
668     stream->appqueue[i] = gst_element_factory_make ("queue", NULL);
669     gst_bin_add (bin, stream->appqueue[i]);
670     /* and link to tee */
671     teepad = gst_element_get_request_pad (stream->tee[i], "src_%u");
672     pad = gst_element_get_static_pad (stream->appqueue[i], "sink");
673     gst_pad_link (teepad, pad);
674     gst_object_unref (pad);
675     gst_object_unref (teepad);
676
677     /* make appsink */
678     stream->appsink[i] = gst_element_factory_make ("appsink", NULL);
679     g_object_set (stream->appsink[i], "async", FALSE, "sync", FALSE, NULL);
680     g_object_set (stream->appsink[i], "emit-signals", FALSE, NULL);
681     gst_bin_add (bin, stream->appsink[i]);
682     gst_app_sink_set_callbacks (GST_APP_SINK_CAST (stream->appsink[i]),
683         &sink_cb, stream, NULL);
684     /* and link to queue */
685     queuepad = gst_element_get_static_pad (stream->appqueue[i], "src");
686     pad = gst_element_get_static_pad (stream->appsink[i], "sink");
687     gst_pad_link (queuepad, pad);
688     gst_object_unref (pad);
689     gst_object_unref (queuepad);
690
691     /* For the receiver we create this bit of pipeline for both
692      * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
693      * and it is all funneled into the rtpbin receive pad.
694      *
695      * .--------.     .--------.    .--------.
696      * | udpsrc |     | funnel |    | rtpbin |
697      * |       src->sink      src->sink      |
698      * '--------'     |        |    '--------'
699      * .--------.     |        |
700      * | appsrc |     |        |
701      * |       src->sink       |
702      * '--------'     '--------'
703      */
704     /* make funnel for the RTP/RTCP receivers */
705     stream->funnel[i] = gst_element_factory_make ("funnel", NULL);
706     gst_bin_add (bin, stream->funnel[i]);
707
708     pad = gst_element_get_static_pad (stream->funnel[i], "src");
709     gst_pad_link (pad, stream->recv_sink[i]);
710     gst_object_unref (pad);
711
712     /* add udpsrc */
713     gst_bin_add (bin, stream->udpsrc[i]);
714     /* and link to the funnel */
715     selpad = gst_element_get_request_pad (stream->funnel[i], "sink_%u");
716     pad = gst_element_get_static_pad (stream->udpsrc[i], "src");
717     gst_pad_link (pad, selpad);
718     gst_object_unref (pad);
719     gst_object_unref (selpad);
720
721     /* make and add appsrc */
722     stream->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
723     gst_bin_add (bin, stream->appsrc[i]);
724     /* and link to the funnel */
725     selpad = gst_element_get_request_pad (stream->funnel[i], "sink_%u");
726     pad = gst_element_get_static_pad (stream->appsrc[i], "src");
727     gst_pad_link (pad, selpad);
728     gst_object_unref (pad);
729     gst_object_unref (selpad);
730
731     /* check if we need to set to a special state */
732     if (state != GST_STATE_NULL) {
733       gst_element_set_state (stream->udpsink[i], state);
734       gst_element_set_state (stream->appsink[i], state);
735       gst_element_set_state (stream->appqueue[i], state);
736       gst_element_set_state (stream->tee[i], state);
737       gst_element_set_state (stream->funnel[i], state);
738       gst_element_set_state (stream->appsrc[i], state);
739     }
740     /* we set and keep these to playing so that they don't cause NO_PREROLL return
741      * values */
742     gst_element_set_state (stream->udpsrc[i], GST_STATE_PLAYING);
743     gst_element_set_locked_state (stream->udpsrc[i], TRUE);
744   }
745
746   /* be notified of caps changes */
747   stream->caps_sig = g_signal_connect (stream->send_rtp_sink, "notify::caps",
748       (GCallback) caps_notify, stream);
749
750   stream->is_joined = TRUE;
751   g_mutex_unlock (&stream->lock);
752
753   return TRUE;
754
755   /* ERRORS */
756 was_joined:
757   {
758     g_mutex_unlock (&stream->lock);
759     return TRUE;
760   }
761 no_ports:
762   {
763     g_mutex_unlock (&stream->lock);
764     GST_WARNING ("failed to allocate ports %d", idx);
765     return FALSE;
766   }
767 link_failed:
768   {
769     GST_WARNING ("failed to link stream %d", idx);
770     gst_object_unref (stream->send_rtp_sink);
771     stream->send_rtp_sink = NULL;
772     g_mutex_unlock (&stream->lock);
773     return FALSE;
774   }
775 }
776
777 /**
778  * gst_rtsp_stream_leave_bin:
779  * @stream: a #GstRTSPStream
780  * @bin: a #GstBin
781  * @rtpbin: a rtpbin #GstElement
782  *
783  * Remove the elements of @stream from @bin. @bin must be set
784  * to the NULL state before calling this.
785  *
786  * Return: %TRUE on success.
787  */
788 gboolean
789 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
790     GstElement * rtpbin)
791 {
792   gint i;
793
794   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
795   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
796   g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
797
798   g_mutex_lock (&stream->lock);
799   if (!stream->is_joined)
800     goto was_not_joined;
801
802   /* all transports must be removed by now */
803   g_return_val_if_fail (stream->transports == NULL, FALSE);
804
805   GST_INFO ("stream %p leaving bin", stream);
806
807   gst_pad_unlink (stream->srcpad, stream->send_rtp_sink);
808   g_signal_handler_disconnect (stream->send_rtp_sink, stream->caps_sig);
809   gst_element_release_request_pad (rtpbin, stream->send_rtp_sink);
810   gst_object_unref (stream->send_rtp_sink);
811   stream->send_rtp_sink = NULL;
812
813   for (i = 0; i < 2; i++) {
814     /* and set udpsrc to NULL now before removing */
815     gst_element_set_locked_state (stream->udpsrc[i], FALSE);
816     gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
817
818     /* removing them should also nicely release the request
819      * pads when they finalize */
820     gst_bin_remove (bin, stream->udpsrc[i]);
821     gst_bin_remove (bin, stream->udpsink[i]);
822     gst_bin_remove (bin, stream->appsrc[i]);
823     gst_bin_remove (bin, stream->appsink[i]);
824     gst_bin_remove (bin, stream->appqueue[i]);
825     gst_bin_remove (bin, stream->tee[i]);
826     gst_bin_remove (bin, stream->funnel[i]);
827
828     gst_element_release_request_pad (rtpbin, stream->recv_sink[i]);
829     gst_object_unref (stream->recv_sink[i]);
830     stream->recv_sink[i] = NULL;
831
832     stream->udpsrc[i] = NULL;
833     stream->udpsink[i] = NULL;
834     stream->appsrc[i] = NULL;
835     stream->appsink[i] = NULL;
836     stream->appqueue[i] = NULL;
837     stream->tee[i] = NULL;
838     stream->funnel[i] = NULL;
839   }
840   gst_object_unref (stream->send_src[0]);
841   stream->send_src[0] = NULL;
842
843   gst_element_release_request_pad (rtpbin, stream->send_src[1]);
844   gst_object_unref (stream->send_src[1]);
845   stream->send_src[1] = NULL;
846
847   g_object_unref (stream->session);
848   if (stream->caps)
849     gst_caps_unref (stream->caps);
850
851   stream->is_joined = FALSE;
852   g_mutex_unlock (&stream->lock);
853
854   return TRUE;
855
856 was_not_joined:
857   {
858     return TRUE;
859   }
860 }
861
862 /**
863  * gst_rtsp_stream_get_rtpinfo:
864  * @stream: a #GstRTSPStream
865  * @rtptime: result RTP timestamp
866  * @seq: result RTP seqnum
867  *
868  * Retrieve the current rtptime and seq. This is used to
869  * construct a RTPInfo reply header.
870  *
871  * Returns: %TRUE when rtptime and seq could be determined.
872  */
873 gboolean
874 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
875     guint * rtptime, guint * seq)
876 {
877   GObjectClass *payobjclass;
878
879   payobjclass = G_OBJECT_GET_CLASS (stream->payloader);
880
881   if (!g_object_class_find_property (payobjclass, "seqnum") ||
882       !g_object_class_find_property (payobjclass, "timestamp"))
883     return FALSE;
884
885   g_object_get (stream->payloader, "seqnum", seq, "timestamp", rtptime, NULL);
886
887   return TRUE;
888 }
889
890 /**
891  * gst_rtsp_stream_recv_rtp:
892  * @stream: a #GstRTSPStream
893  * @buffer: (transfer full): a #GstBuffer
894  *
895  * Handle an RTP buffer for the stream. This method is usually called when a
896  * message has been received from a client using the TCP transport.
897  *
898  * This function takes ownership of @buffer.
899  *
900  * Returns: a GstFlowReturn.
901  */
902 GstFlowReturn
903 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
904 {
905   GstFlowReturn ret;
906   GstElement *element;
907
908   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
909   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
910   g_return_val_if_fail (stream->is_joined, FALSE);
911
912   g_mutex_lock (&stream->lock);
913   element = gst_object_ref (stream->appsrc[0]);
914   g_mutex_unlock (&stream->lock);
915
916   ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
917
918   gst_object_unref (element);
919
920   return ret;
921 }
922
923 /**
924  * gst_rtsp_stream_recv_rtcp:
925  * @stream: a #GstRTSPStream
926  * @buffer: (transfer full): a #GstBuffer
927  *
928  * Handle an RTCP buffer for the stream. This method is usually called when a
929  * message has been received from a client using the TCP transport.
930  *
931  * This function takes ownership of @buffer.
932  *
933  * Returns: a GstFlowReturn.
934  */
935 GstFlowReturn
936 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
937 {
938   GstFlowReturn ret;
939   GstElement *element;
940
941   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
942   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
943   g_return_val_if_fail (stream->is_joined, FALSE);
944
945   g_mutex_lock (&stream->lock);
946   element = gst_object_ref (stream->appsrc[1]);
947   g_mutex_unlock (&stream->lock);
948
949   ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
950
951   gst_object_unref (element);
952
953   return ret;
954 }
955
956 /* must be called with lock */
957 static gboolean
958 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
959     gboolean add)
960 {
961   GstRTSPTransport *tr;
962   gboolean updated;
963
964   updated = FALSE;
965
966   tr = trans->transport;
967
968   switch (tr->lower_transport) {
969     case GST_RTSP_LOWER_TRANS_UDP:
970     case GST_RTSP_LOWER_TRANS_UDP_MCAST:
971     {
972       gchar *dest;
973       gint min, max;
974       guint ttl = 0;
975
976       dest = tr->destination;
977       if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
978         min = tr->port.min;
979         max = tr->port.max;
980         ttl = tr->ttl;
981       } else {
982         min = tr->client_port.min;
983         max = tr->client_port.max;
984       }
985
986       if (add && !trans->active) {
987         GST_INFO ("adding %s:%d-%d", dest, min, max);
988         g_signal_emit_by_name (stream->udpsink[0], "add", dest, min, NULL);
989         g_signal_emit_by_name (stream->udpsink[1], "add", dest, max, NULL);
990         if (ttl > 0) {
991           GST_INFO ("setting ttl-mc %d", ttl);
992           g_object_set (G_OBJECT (stream->udpsink[0]), "ttl-mc", ttl, NULL);
993           g_object_set (G_OBJECT (stream->udpsink[1]), "ttl-mc", ttl, NULL);
994         }
995         stream->transports = g_list_prepend (stream->transports, trans);
996         trans->active = TRUE;
997         updated = TRUE;
998       } else if (trans->active) {
999         GST_INFO ("removing %s:%d-%d", dest, min, max);
1000         g_signal_emit_by_name (stream->udpsink[0], "remove", dest, min, NULL);
1001         g_signal_emit_by_name (stream->udpsink[1], "remove", dest, max, NULL);
1002         stream->transports = g_list_remove (stream->transports, trans);
1003         trans->active = FALSE;
1004         updated = TRUE;
1005       }
1006       break;
1007     }
1008     case GST_RTSP_LOWER_TRANS_TCP:
1009       if (add && !trans->active) {
1010         GST_INFO ("adding TCP %s", tr->destination);
1011         stream->transports = g_list_prepend (stream->transports, trans);
1012         trans->active = TRUE;
1013         updated = TRUE;
1014       } else if (trans->active) {
1015         GST_INFO ("removing TCP %s", tr->destination);
1016         stream->transports = g_list_remove (stream->transports, trans);
1017         trans->active = FALSE;
1018         updated = TRUE;
1019       }
1020       break;
1021     default:
1022       GST_INFO ("Unknown transport %d", tr->lower_transport);
1023       break;
1024   }
1025   return updated;
1026 }
1027
1028
1029 /**
1030  * gst_rtsp_stream_add_transport:
1031  * @stream: a #GstRTSPStream
1032  * @trans: a #GstRTSPStreamTransport
1033  *
1034  * Add the transport in @trans to @stream. The media of @stream will
1035  * then also be send to the values configured in @trans.
1036  *
1037  * @stream must be joined to a bin.
1038  *
1039  * @trans must contain a valid #GstRTSPTransport.
1040  *
1041  * Returns: %TRUE if @trans was added
1042  */
1043 gboolean
1044 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
1045     GstRTSPStreamTransport * trans)
1046 {
1047   gboolean res;
1048
1049   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1050   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1051   g_return_val_if_fail (stream->is_joined, FALSE);
1052   g_return_val_if_fail (trans->transport != NULL, FALSE);
1053
1054   g_mutex_lock (&stream->lock);
1055   res = update_transport (stream, trans, TRUE);
1056   g_mutex_unlock (&stream->lock);
1057
1058   return res;
1059 }
1060
1061 /**
1062  * gst_rtsp_stream_remove_transport:
1063  * @stream: a #GstRTSPStream
1064  * @trans: a #GstRTSPStreamTransport
1065  *
1066  * Remove the transport in @trans from @stream. The media of @stream will
1067  * not be sent to the values configured in @trans.
1068  *
1069  * @stream must be joined to a bin.
1070  *
1071  * @trans must contain a valid #GstRTSPTransport.
1072  *
1073  * Returns: %TRUE if @trans was removed
1074  */
1075 gboolean
1076 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
1077     GstRTSPStreamTransport * trans)
1078 {
1079   gboolean res;
1080
1081   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1082   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
1083   g_return_val_if_fail (stream->is_joined, FALSE);
1084   g_return_val_if_fail (trans->transport != NULL, FALSE);
1085
1086   g_mutex_lock (&stream->lock);
1087   res = update_transport (stream, trans, FALSE);
1088   g_mutex_unlock (&stream->lock);
1089
1090   return res;
1091 }