rtsp-stream: Have one copy of the transports cache for RTP and RTCP each
[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  * SECTION:rtsp-stream
21  * @short_description: A media stream
22  * @see_also: #GstRTSPMedia
23  *
24  * The #GstRTSPStream object manages the data transport for one stream. It
25  * is created from a payloader element and a source pad that produce the RTP
26  * packets for the stream.
27  *
28  * With gst_rtsp_stream_join_bin() the streaming elements are added to the bin
29  * and rtpbin. gst_rtsp_stream_leave_bin() removes the elements again.
30  *
31  * The #GstRTSPStream will use the configured addresspool, as set with
32  * gst_rtsp_stream_set_address_pool(), to allocate multicast addresses for the
33  * stream. With gst_rtsp_stream_get_multicast_address() you can get the
34  * configured address.
35  *
36  * With gst_rtsp_stream_get_server_port () you can get the port that the server
37  * will use to receive RTCP. This is the part that the clients will use to send
38  * RTCP to.
39  *
40  * With gst_rtsp_stream_add_transport() destinations can be added where the
41  * stream should be sent to. Use gst_rtsp_stream_remove_transport() to remove
42  * the destination again.
43  *
44  * Last reviewed on 2013-07-16 (1.0.0)
45  */
46
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50
51 #include <gio/gio.h>
52
53 #include <gst/app/gstappsrc.h>
54 #include <gst/app/gstappsink.h>
55
56 #include "rtsp-stream.h"
57
58 #define GST_RTSP_STREAM_GET_PRIVATE(obj)  \
59      (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_STREAM, GstRTSPStreamPrivate))
60
61 typedef struct
62 {
63   GstRTSPStreamTransport *transport;
64
65   /* RTP and RTCP source */
66   GstElement *udpsrc[2];
67   GstPad *selpad[2];
68 } GstRTSPMulticastTransportSource;
69
70 struct _GstRTSPStreamPrivate
71 {
72   GMutex lock;
73   guint idx;
74   GstPad *srcpad;
75   GstElement *payloader;
76   guint buffer_size;
77   gboolean is_joined;
78   gchar *control;
79
80   GstRTSPProfile profiles;
81   GstRTSPLowerTrans protocols;
82
83   /* pads on the rtpbin */
84   GstPad *send_rtp_sink;
85   GstPad *recv_sink[2];
86   GstPad *send_src[2];
87
88   /* the RTPSession object */
89   GObject *session;
90
91   /* SRTP encoder/decoder */
92   GstElement *srtpenc;
93   GstElement *srtpdec;
94   GHashTable *keys;
95
96   /* sinks used for sending and receiving RTP and RTCP over ipv4, they share
97    * sockets */
98   GstElement *udpsrc_v4[2];
99
100   /* sinks used for sending and receiving RTP and RTCP over ipv6, they share
101    * sockets */
102   GstElement *udpsrc_v6[2];
103
104   GstElement *udpsink[2];
105
106   /* for TCP transport */
107   GstElement *appsrc[2];
108   GstElement *appqueue[2];
109   GstElement *appsink[2];
110
111   GstElement *tee[2];
112   GstElement *funnel[2];
113
114   /* retransmission */
115   GstElement *rtxsend;
116   guint rtx_pt;
117   GstClockTime rtx_time;
118
119   /* server ports for sending/receiving over ipv4 */
120   GstRTSPRange server_port_v4;
121   GstRTSPAddress *server_addr_v4;
122   gboolean have_ipv4;
123
124   /* server ports for sending/receiving over ipv6 */
125   GstRTSPRange server_port_v6;
126   GstRTSPAddress *server_addr_v6;
127   gboolean have_ipv6;
128
129   /* multicast addresses */
130   GstRTSPAddressPool *pool;
131   GstRTSPAddress *addr_v4;
132   GstRTSPAddress *addr_v6;
133
134   /* the caps of the stream */
135   gulong caps_sig;
136   GstCaps *caps;
137
138   /* transports we stream to */
139   guint n_active;
140   GList *transports;
141   guint transports_cookie;
142   GList *tr_cache_rtp;
143   GList *tr_cache_rtcp;
144   guint tr_cache_cookie;
145
146   /* UDP sources for UDP multicast transports */
147   GList *transport_sources;
148
149   gint dscp_qos;
150
151   /* stream blocking */
152   gulong blocked_id;
153   gboolean blocking;
154 };
155
156 #define DEFAULT_CONTROL         NULL
157 #define DEFAULT_PROFILES        GST_RTSP_PROFILE_AVP
158 #define DEFAULT_PROTOCOLS       GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
159                                         GST_RTSP_LOWER_TRANS_TCP
160
161 enum
162 {
163   PROP_0,
164   PROP_CONTROL,
165   PROP_PROFILES,
166   PROP_PROTOCOLS,
167   PROP_LAST
168 };
169
170 enum
171 {
172   SIGNAL_NEW_RTP_ENCODER,
173   SIGNAL_NEW_RTCP_ENCODER,
174   SIGNAL_LAST
175 };
176
177 GST_DEBUG_CATEGORY_STATIC (rtsp_stream_debug);
178 #define GST_CAT_DEFAULT rtsp_stream_debug
179
180 static GQuark ssrc_stream_map_key;
181
182 static void gst_rtsp_stream_get_property (GObject * object, guint propid,
183     GValue * value, GParamSpec * pspec);
184 static void gst_rtsp_stream_set_property (GObject * object, guint propid,
185     const GValue * value, GParamSpec * pspec);
186
187 static void gst_rtsp_stream_finalize (GObject * obj);
188
189 static guint gst_rtsp_stream_signals[SIGNAL_LAST] = { 0 };
190
191 G_DEFINE_TYPE (GstRTSPStream, gst_rtsp_stream, G_TYPE_OBJECT);
192
193 static void
194 gst_rtsp_stream_class_init (GstRTSPStreamClass * klass)
195 {
196   GObjectClass *gobject_class;
197
198   g_type_class_add_private (klass, sizeof (GstRTSPStreamPrivate));
199
200   gobject_class = G_OBJECT_CLASS (klass);
201
202   gobject_class->get_property = gst_rtsp_stream_get_property;
203   gobject_class->set_property = gst_rtsp_stream_set_property;
204   gobject_class->finalize = gst_rtsp_stream_finalize;
205
206   g_object_class_install_property (gobject_class, PROP_CONTROL,
207       g_param_spec_string ("control", "Control",
208           "The control string for this stream", DEFAULT_CONTROL,
209           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210
211   g_object_class_install_property (gobject_class, PROP_PROFILES,
212       g_param_spec_flags ("profiles", "Profiles",
213           "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
214           DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
215
216   g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
217       g_param_spec_flags ("protocols", "Protocols",
218           "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
219           DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
220
221   gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER] =
222       g_signal_new ("new-rtp-encoder", G_TYPE_FROM_CLASS (klass),
223       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
224       G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
225
226   gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER] =
227       g_signal_new ("new-rtcp-encoder", G_TYPE_FROM_CLASS (klass),
228       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
229       G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
230
231   GST_DEBUG_CATEGORY_INIT (rtsp_stream_debug, "rtspstream", 0, "GstRTSPStream");
232
233   ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
234 }
235
236 static void
237 gst_rtsp_stream_init (GstRTSPStream * stream)
238 {
239   GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
240
241   GST_DEBUG ("new stream %p", stream);
242
243   stream->priv = priv;
244
245   priv->dscp_qos = -1;
246   priv->control = g_strdup (DEFAULT_CONTROL);
247   priv->profiles = DEFAULT_PROFILES;
248   priv->protocols = DEFAULT_PROTOCOLS;
249
250   g_mutex_init (&priv->lock);
251
252   priv->keys = g_hash_table_new_full (g_direct_hash, g_direct_equal,
253       NULL, (GDestroyNotify) gst_caps_unref);
254 }
255
256 static void
257 gst_rtsp_stream_finalize (GObject * obj)
258 {
259   GstRTSPStream *stream;
260   GstRTSPStreamPrivate *priv;
261
262   stream = GST_RTSP_STREAM (obj);
263   priv = stream->priv;
264
265   GST_DEBUG ("finalize stream %p", stream);
266
267   /* we really need to be unjoined now */
268   g_return_if_fail (!priv->is_joined);
269
270   if (priv->addr_v4)
271     gst_rtsp_address_free (priv->addr_v4);
272   if (priv->addr_v6)
273     gst_rtsp_address_free (priv->addr_v6);
274   if (priv->server_addr_v4)
275     gst_rtsp_address_free (priv->server_addr_v4);
276   if (priv->server_addr_v6)
277     gst_rtsp_address_free (priv->server_addr_v6);
278   if (priv->pool)
279     g_object_unref (priv->pool);
280   if (priv->rtxsend)
281     g_object_unref (priv->rtxsend);
282
283   gst_object_unref (priv->payloader);
284   gst_object_unref (priv->srcpad);
285   g_free (priv->control);
286   g_mutex_clear (&priv->lock);
287
288   g_hash_table_unref (priv->keys);
289
290   G_OBJECT_CLASS (gst_rtsp_stream_parent_class)->finalize (obj);
291 }
292
293 static void
294 gst_rtsp_stream_get_property (GObject * object, guint propid,
295     GValue * value, GParamSpec * pspec)
296 {
297   GstRTSPStream *stream = GST_RTSP_STREAM (object);
298
299   switch (propid) {
300     case PROP_CONTROL:
301       g_value_take_string (value, gst_rtsp_stream_get_control (stream));
302       break;
303     case PROP_PROFILES:
304       g_value_set_flags (value, gst_rtsp_stream_get_profiles (stream));
305       break;
306     case PROP_PROTOCOLS:
307       g_value_set_flags (value, gst_rtsp_stream_get_protocols (stream));
308       break;
309     default:
310       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
311   }
312 }
313
314 static void
315 gst_rtsp_stream_set_property (GObject * object, guint propid,
316     const GValue * value, GParamSpec * pspec)
317 {
318   GstRTSPStream *stream = GST_RTSP_STREAM (object);
319
320   switch (propid) {
321     case PROP_CONTROL:
322       gst_rtsp_stream_set_control (stream, g_value_get_string (value));
323       break;
324     case PROP_PROFILES:
325       gst_rtsp_stream_set_profiles (stream, g_value_get_flags (value));
326       break;
327     case PROP_PROTOCOLS:
328       gst_rtsp_stream_set_protocols (stream, g_value_get_flags (value));
329       break;
330     default:
331       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
332   }
333 }
334
335 /**
336  * gst_rtsp_stream_new:
337  * @idx: an index
338  * @srcpad: a #GstPad
339  * @payloader: a #GstElement
340  *
341  * Create a new media stream with index @idx that handles RTP data on
342  * @srcpad and has a payloader element @payloader.
343  *
344  * Returns: (transfer full): a new #GstRTSPStream
345  */
346 GstRTSPStream *
347 gst_rtsp_stream_new (guint idx, GstElement * payloader, GstPad * srcpad)
348 {
349   GstRTSPStreamPrivate *priv;
350   GstRTSPStream *stream;
351
352   g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
353   g_return_val_if_fail (GST_IS_PAD (srcpad), NULL);
354   g_return_val_if_fail (GST_PAD_IS_SRC (srcpad), NULL);
355
356   stream = g_object_new (GST_TYPE_RTSP_STREAM, NULL);
357   priv = stream->priv;
358   priv->idx = idx;
359   priv->payloader = gst_object_ref (payloader);
360   priv->srcpad = gst_object_ref (srcpad);
361
362   return stream;
363 }
364
365 /**
366  * gst_rtsp_stream_get_index:
367  * @stream: a #GstRTSPStream
368  *
369  * Get the stream index.
370  *
371  * Return: the stream index.
372  */
373 guint
374 gst_rtsp_stream_get_index (GstRTSPStream * stream)
375 {
376   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
377
378   return stream->priv->idx;
379 }
380
381 /**
382  * gst_rtsp_stream_get_pt:
383  * @stream: a #GstRTSPStream
384  *
385  * Get the stream payload type.
386  *
387  * Return: the stream payload type.
388  */
389 guint
390 gst_rtsp_stream_get_pt (GstRTSPStream * stream)
391 {
392   GstRTSPStreamPrivate *priv;
393   guint pt;
394
395   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
396
397   priv = stream->priv;
398
399   g_object_get (G_OBJECT (priv->payloader), "pt", &pt, NULL);
400
401   return pt;
402 }
403
404 /**
405  * gst_rtsp_stream_get_srcpad:
406  * @stream: a #GstRTSPStream
407  *
408  * Get the srcpad associated with @stream.
409  *
410  * Returns: (transfer full): the srcpad. Unref after usage.
411  */
412 GstPad *
413 gst_rtsp_stream_get_srcpad (GstRTSPStream * stream)
414 {
415   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
416
417   return gst_object_ref (stream->priv->srcpad);
418 }
419
420 /**
421  * gst_rtsp_stream_get_control:
422  * @stream: a #GstRTSPStream
423  *
424  * Get the control string to identify this stream.
425  *
426  * Returns: (transfer full): the control string. g_free() after usage.
427  */
428 gchar *
429 gst_rtsp_stream_get_control (GstRTSPStream * stream)
430 {
431   GstRTSPStreamPrivate *priv;
432   gchar *result;
433
434   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
435
436   priv = stream->priv;
437
438   g_mutex_lock (&priv->lock);
439   if ((result = g_strdup (priv->control)) == NULL)
440     result = g_strdup_printf ("stream=%u", priv->idx);
441   g_mutex_unlock (&priv->lock);
442
443   return result;
444 }
445
446 /**
447  * gst_rtsp_stream_set_control:
448  * @stream: a #GstRTSPStream
449  * @control: a control string
450  *
451  * Set the control string in @stream.
452  */
453 void
454 gst_rtsp_stream_set_control (GstRTSPStream * stream, const gchar * control)
455 {
456   GstRTSPStreamPrivate *priv;
457
458   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
459
460   priv = stream->priv;
461
462   g_mutex_lock (&priv->lock);
463   g_free (priv->control);
464   priv->control = g_strdup (control);
465   g_mutex_unlock (&priv->lock);
466 }
467
468 /**
469  * gst_rtsp_stream_has_control:
470  * @stream: a #GstRTSPStream
471  * @control: a control string
472  *
473  * Check if @stream has the control string @control.
474  *
475  * Returns: %TRUE is @stream has @control as the control string
476  */
477 gboolean
478 gst_rtsp_stream_has_control (GstRTSPStream * stream, const gchar * control)
479 {
480   GstRTSPStreamPrivate *priv;
481   gboolean res;
482
483   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
484
485   priv = stream->priv;
486
487   g_mutex_lock (&priv->lock);
488   if (priv->control)
489     res = (g_strcmp0 (priv->control, control) == 0);
490   else {
491     guint streamid;
492
493     if (sscanf (control, "stream=%u", &streamid) > 0)
494       res = (streamid == priv->idx);
495     else
496       res = FALSE;
497   }
498   g_mutex_unlock (&priv->lock);
499
500   return res;
501 }
502
503 /**
504  * gst_rtsp_stream_set_mtu:
505  * @stream: a #GstRTSPStream
506  * @mtu: a new MTU
507  *
508  * Configure the mtu in the payloader of @stream to @mtu.
509  */
510 void
511 gst_rtsp_stream_set_mtu (GstRTSPStream * stream, guint mtu)
512 {
513   GstRTSPStreamPrivate *priv;
514
515   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
516
517   priv = stream->priv;
518
519   GST_LOG_OBJECT (stream, "set MTU %u", mtu);
520
521   g_object_set (G_OBJECT (priv->payloader), "mtu", mtu, NULL);
522 }
523
524 /**
525  * gst_rtsp_stream_get_mtu:
526  * @stream: a #GstRTSPStream
527  *
528  * Get the configured MTU in the payloader of @stream.
529  *
530  * Returns: the MTU of the payloader.
531  */
532 guint
533 gst_rtsp_stream_get_mtu (GstRTSPStream * stream)
534 {
535   GstRTSPStreamPrivate *priv;
536   guint mtu;
537
538   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
539
540   priv = stream->priv;
541
542   g_object_get (G_OBJECT (priv->payloader), "mtu", &mtu, NULL);
543
544   return mtu;
545 }
546
547 /* Update the dscp qos property on the udp sinks */
548 static void
549 update_dscp_qos (GstRTSPStream * stream)
550 {
551   GstRTSPStreamPrivate *priv;
552
553   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
554
555   priv = stream->priv;
556
557   if (priv->udpsink[0]) {
558     g_object_set (G_OBJECT (priv->udpsink[0]), "qos-dscp", priv->dscp_qos,
559         NULL);
560   }
561
562   if (priv->udpsink[1]) {
563     g_object_set (G_OBJECT (priv->udpsink[1]), "qos-dscp", priv->dscp_qos,
564         NULL);
565   }
566 }
567
568 /**
569  * gst_rtsp_stream_set_dscp_qos:
570  * @stream: a #GstRTSPStream
571  * @dscp_qos: a new dscp qos value (0-63, or -1 to disable)
572  *
573  * Configure the dscp qos of the outgoing sockets to @dscp_qos.
574  */
575 void
576 gst_rtsp_stream_set_dscp_qos (GstRTSPStream * stream, gint dscp_qos)
577 {
578   GstRTSPStreamPrivate *priv;
579
580   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
581
582   priv = stream->priv;
583
584   GST_LOG_OBJECT (stream, "set DSCP QoS %d", dscp_qos);
585
586   if (dscp_qos < -1 || dscp_qos > 63) {
587     GST_WARNING_OBJECT (stream, "trying to set illegal dscp qos %d", dscp_qos);
588     return;
589   }
590
591   priv->dscp_qos = dscp_qos;
592
593   update_dscp_qos (stream);
594 }
595
596 /**
597  * gst_rtsp_stream_get_dscp_qos:
598  * @stream: a #GstRTSPStream
599  *
600  * Get the configured DSCP QoS in of the outgoing sockets.
601  *
602  * Returns: the DSCP QoS value of the outgoing sockets, or -1 if disbled.
603  */
604 gint
605 gst_rtsp_stream_get_dscp_qos (GstRTSPStream * stream)
606 {
607   GstRTSPStreamPrivate *priv;
608
609   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), -1);
610
611   priv = stream->priv;
612
613   return priv->dscp_qos;
614 }
615
616 /**
617  * gst_rtsp_stream_is_transport_supported:
618  * @stream: a #GstRTSPStream
619  * @transport: (transfer none): a #GstRTSPTransport
620  *
621  * Check if @transport can be handled by stream
622  *
623  * Returns: %TRUE if @transport can be handled by @stream.
624  */
625 gboolean
626 gst_rtsp_stream_is_transport_supported (GstRTSPStream * stream,
627     GstRTSPTransport * transport)
628 {
629   GstRTSPStreamPrivate *priv;
630
631   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
632
633   priv = stream->priv;
634
635   g_mutex_lock (&priv->lock);
636   if (transport->trans != GST_RTSP_TRANS_RTP)
637     goto unsupported_transmode;
638
639   if (!(transport->profile & priv->profiles))
640     goto unsupported_profile;
641
642   if (!(transport->lower_transport & priv->protocols))
643     goto unsupported_ltrans;
644
645   g_mutex_unlock (&priv->lock);
646
647   return TRUE;
648
649   /* ERRORS */
650 unsupported_transmode:
651   {
652     GST_DEBUG ("unsupported transport mode %d", transport->trans);
653     g_mutex_unlock (&priv->lock);
654     return FALSE;
655   }
656 unsupported_profile:
657   {
658     GST_DEBUG ("unsupported profile %d", transport->profile);
659     g_mutex_unlock (&priv->lock);
660     return FALSE;
661   }
662 unsupported_ltrans:
663   {
664     GST_DEBUG ("unsupported lower transport %d", transport->lower_transport);
665     g_mutex_unlock (&priv->lock);
666     return FALSE;
667   }
668 }
669
670 /**
671  * gst_rtsp_stream_set_profiles:
672  * @stream: a #GstRTSPStream
673  * @profiles: the new profiles
674  *
675  * Configure the allowed profiles for @stream.
676  */
677 void
678 gst_rtsp_stream_set_profiles (GstRTSPStream * stream, GstRTSPProfile profiles)
679 {
680   GstRTSPStreamPrivate *priv;
681
682   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
683
684   priv = stream->priv;
685
686   g_mutex_lock (&priv->lock);
687   priv->profiles = profiles;
688   g_mutex_unlock (&priv->lock);
689 }
690
691 /**
692  * gst_rtsp_stream_get_profiles:
693  * @stream: a #GstRTSPStream
694  *
695  * Get the allowed profiles of @stream.
696  *
697  * Returns: a #GstRTSPProfile
698  */
699 GstRTSPProfile
700 gst_rtsp_stream_get_profiles (GstRTSPStream * stream)
701 {
702   GstRTSPStreamPrivate *priv;
703   GstRTSPProfile res;
704
705   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_RTSP_PROFILE_UNKNOWN);
706
707   priv = stream->priv;
708
709   g_mutex_lock (&priv->lock);
710   res = priv->profiles;
711   g_mutex_unlock (&priv->lock);
712
713   return res;
714 }
715
716 /**
717  * gst_rtsp_stream_set_protocols:
718  * @stream: a #GstRTSPStream
719  * @protocols: the new flags
720  *
721  * Configure the allowed lower transport for @stream.
722  */
723 void
724 gst_rtsp_stream_set_protocols (GstRTSPStream * stream,
725     GstRTSPLowerTrans protocols)
726 {
727   GstRTSPStreamPrivate *priv;
728
729   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
730
731   priv = stream->priv;
732
733   g_mutex_lock (&priv->lock);
734   priv->protocols = protocols;
735   g_mutex_unlock (&priv->lock);
736 }
737
738 /**
739  * gst_rtsp_stream_get_protocols:
740  * @stream: a #GstRTSPStream
741  *
742  * Get the allowed protocols of @stream.
743  *
744  * Returns: a #GstRTSPLowerTrans
745  */
746 GstRTSPLowerTrans
747 gst_rtsp_stream_get_protocols (GstRTSPStream * stream)
748 {
749   GstRTSPStreamPrivate *priv;
750   GstRTSPLowerTrans res;
751
752   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream),
753       GST_RTSP_LOWER_TRANS_UNKNOWN);
754
755   priv = stream->priv;
756
757   g_mutex_lock (&priv->lock);
758   res = priv->protocols;
759   g_mutex_unlock (&priv->lock);
760
761   return res;
762 }
763
764 /**
765  * gst_rtsp_stream_set_address_pool:
766  * @stream: a #GstRTSPStream
767  * @pool: (transfer none): a #GstRTSPAddressPool
768  *
769  * configure @pool to be used as the address pool of @stream.
770  */
771 void
772 gst_rtsp_stream_set_address_pool (GstRTSPStream * stream,
773     GstRTSPAddressPool * pool)
774 {
775   GstRTSPStreamPrivate *priv;
776   GstRTSPAddressPool *old;
777
778   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
779
780   priv = stream->priv;
781
782   GST_LOG_OBJECT (stream, "set address pool %p", pool);
783
784   g_mutex_lock (&priv->lock);
785   if ((old = priv->pool) != pool)
786     priv->pool = pool ? g_object_ref (pool) : NULL;
787   else
788     old = NULL;
789   g_mutex_unlock (&priv->lock);
790
791   if (old)
792     g_object_unref (old);
793 }
794
795 /**
796  * gst_rtsp_stream_get_address_pool:
797  * @stream: a #GstRTSPStream
798  *
799  * Get the #GstRTSPAddressPool used as the address pool of @stream.
800  *
801  * Returns: (transfer full): the #GstRTSPAddressPool of @stream. g_object_unref() after
802  * usage.
803  */
804 GstRTSPAddressPool *
805 gst_rtsp_stream_get_address_pool (GstRTSPStream * stream)
806 {
807   GstRTSPStreamPrivate *priv;
808   GstRTSPAddressPool *result;
809
810   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
811
812   priv = stream->priv;
813
814   g_mutex_lock (&priv->lock);
815   if ((result = priv->pool))
816     g_object_ref (result);
817   g_mutex_unlock (&priv->lock);
818
819   return result;
820 }
821
822 /**
823  * gst_rtsp_stream_get_multicast_address:
824  * @stream: a #GstRTSPStream
825  * @family: the #GSocketFamily
826  *
827  * Get the multicast address of @stream for @family.
828  *
829  * Returns: (transfer full) (nullable): the #GstRTSPAddress of @stream
830  * or %NULL when no address could be allocated. gst_rtsp_address_free()
831  * after usage.
832  */
833 GstRTSPAddress *
834 gst_rtsp_stream_get_multicast_address (GstRTSPStream * stream,
835     GSocketFamily family)
836 {
837   GstRTSPStreamPrivate *priv;
838   GstRTSPAddress *result;
839   GstRTSPAddress **addrp;
840   GstRTSPAddressFlags flags;
841
842   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
843
844   priv = stream->priv;
845
846   if (family == G_SOCKET_FAMILY_IPV6) {
847     flags = GST_RTSP_ADDRESS_FLAG_IPV6;
848     addrp = &priv->addr_v6;
849   } else {
850     flags = GST_RTSP_ADDRESS_FLAG_IPV4;
851     addrp = &priv->addr_v4;
852   }
853
854   g_mutex_lock (&priv->lock);
855   if (*addrp == NULL) {
856     if (priv->pool == NULL)
857       goto no_pool;
858
859     flags |= GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_MULTICAST;
860
861     *addrp = gst_rtsp_address_pool_acquire_address (priv->pool, flags, 2);
862     if (*addrp == NULL)
863       goto no_address;
864   }
865   result = gst_rtsp_address_copy (*addrp);
866   g_mutex_unlock (&priv->lock);
867
868   return result;
869
870   /* ERRORS */
871 no_pool:
872   {
873     GST_ERROR_OBJECT (stream, "no address pool specified");
874     g_mutex_unlock (&priv->lock);
875     return NULL;
876   }
877 no_address:
878   {
879     GST_ERROR_OBJECT (stream, "failed to acquire address from pool");
880     g_mutex_unlock (&priv->lock);
881     return NULL;
882   }
883 }
884
885 /**
886  * gst_rtsp_stream_reserve_address:
887  * @stream: a #GstRTSPStream
888  * @address: an address
889  * @port: a port
890  * @n_ports: n_ports
891  * @ttl: a TTL
892  *
893  * Reserve @address and @port as the address and port of @stream.
894  *
895  * Returns: (nullable): the #GstRTSPAddress of @stream or %NULL when
896  * the address could be reserved. gst_rtsp_address_free() after usage.
897  */
898 GstRTSPAddress *
899 gst_rtsp_stream_reserve_address (GstRTSPStream * stream,
900     const gchar * address, guint port, guint n_ports, guint ttl)
901 {
902   GstRTSPStreamPrivate *priv;
903   GstRTSPAddress *result;
904   GInetAddress *addr;
905   GSocketFamily family;
906   GstRTSPAddress **addrp;
907
908   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
909   g_return_val_if_fail (address != NULL, NULL);
910   g_return_val_if_fail (port > 0, NULL);
911   g_return_val_if_fail (n_ports > 0, NULL);
912   g_return_val_if_fail (ttl > 0, NULL);
913
914   priv = stream->priv;
915
916   addr = g_inet_address_new_from_string (address);
917   if (!addr) {
918     GST_ERROR ("failed to get inet addr from %s", address);
919     family = G_SOCKET_FAMILY_IPV4;
920   } else {
921     family = g_inet_address_get_family (addr);
922     g_object_unref (addr);
923   }
924
925   if (family == G_SOCKET_FAMILY_IPV6)
926     addrp = &priv->addr_v6;
927   else
928     addrp = &priv->addr_v4;
929
930   g_mutex_lock (&priv->lock);
931   if (*addrp == NULL) {
932     GstRTSPAddressPoolResult res;
933
934     if (priv->pool == NULL)
935       goto no_pool;
936
937     res = gst_rtsp_address_pool_reserve_address (priv->pool, address,
938         port, n_ports, ttl, addrp);
939     if (res != GST_RTSP_ADDRESS_POOL_OK)
940       goto no_address;
941   } else {
942     if (strcmp ((*addrp)->address, address) ||
943         (*addrp)->port != port || (*addrp)->n_ports != n_ports ||
944         (*addrp)->ttl != ttl)
945       goto different_address;
946   }
947   result = gst_rtsp_address_copy (*addrp);
948   g_mutex_unlock (&priv->lock);
949
950   return result;
951
952   /* ERRORS */
953 no_pool:
954   {
955     GST_ERROR_OBJECT (stream, "no address pool specified");
956     g_mutex_unlock (&priv->lock);
957     return NULL;
958   }
959 no_address:
960   {
961     GST_ERROR_OBJECT (stream, "failed to acquire address %s from pool",
962         address);
963     g_mutex_unlock (&priv->lock);
964     return NULL;
965   }
966 different_address:
967   {
968     GST_ERROR_OBJECT (stream, "address %s is not the same that was already"
969         " reserved", address);
970     g_mutex_unlock (&priv->lock);
971     return NULL;
972   }
973 }
974
975 static gboolean
976 alloc_ports_one_family (GstRTSPAddressPool * pool, gint buffer_size,
977     GSocketFamily family, GstElement * udpsrc_out[2],
978     GstElement * udpsink_out[2], GstRTSPRange * server_port_out,
979     GstRTSPAddress ** server_addr_out)
980 {
981   GstStateChangeReturn ret;
982   GstElement *udpsrc0, *udpsrc1;
983   GstElement *udpsink0, *udpsink1;
984   GSocket *rtp_socket = NULL;
985   GSocket *rtcp_socket;
986   gint tmp_rtp, tmp_rtcp;
987   guint count;
988   gint rtpport, rtcpport;
989   GList *rejected_addresses = NULL;
990   GstRTSPAddress *addr = NULL;
991   GInetAddress *inetaddr = NULL;
992   GSocketAddress *rtp_sockaddr = NULL;
993   GSocketAddress *rtcp_sockaddr = NULL;
994   const gchar *multisink_socket;
995
996   if (family == G_SOCKET_FAMILY_IPV6)
997     multisink_socket = "socket-v6";
998   else
999     multisink_socket = "socket";
1000
1001   udpsrc0 = NULL;
1002   udpsrc1 = NULL;
1003   udpsink0 = NULL;
1004   udpsink1 = NULL;
1005   count = 0;
1006
1007   /* Start with random port */
1008   tmp_rtp = 0;
1009
1010   rtcp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1011       G_SOCKET_PROTOCOL_UDP, NULL);
1012   if (!rtcp_socket)
1013     goto no_udp_protocol;
1014
1015   if (*server_addr_out)
1016     gst_rtsp_address_free (*server_addr_out);
1017
1018   /* try to allocate 2 UDP ports, the RTP port should be an even
1019    * number and the RTCP port should be the next (uneven) port */
1020 again:
1021
1022   if (rtp_socket == NULL) {
1023     rtp_socket = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM,
1024         G_SOCKET_PROTOCOL_UDP, NULL);
1025     if (!rtp_socket)
1026       goto no_udp_protocol;
1027   }
1028
1029   if (pool && gst_rtsp_address_pool_has_unicast_addresses (pool)) {
1030     GstRTSPAddressFlags flags;
1031
1032     if (addr)
1033       rejected_addresses = g_list_prepend (rejected_addresses, addr);
1034
1035     flags = GST_RTSP_ADDRESS_FLAG_EVEN_PORT | GST_RTSP_ADDRESS_FLAG_UNICAST;
1036     if (family == G_SOCKET_FAMILY_IPV6)
1037       flags |= GST_RTSP_ADDRESS_FLAG_IPV6;
1038     else
1039       flags |= GST_RTSP_ADDRESS_FLAG_IPV4;
1040
1041     addr = gst_rtsp_address_pool_acquire_address (pool, flags, 2);
1042
1043     if (addr == NULL)
1044       goto no_ports;
1045
1046     tmp_rtp = addr->port;
1047
1048     g_clear_object (&inetaddr);
1049     inetaddr = g_inet_address_new_from_string (addr->address);
1050   } else {
1051     if (tmp_rtp != 0) {
1052       tmp_rtp += 2;
1053       if (++count > 20)
1054         goto no_ports;
1055     }
1056
1057     if (inetaddr == NULL)
1058       inetaddr = g_inet_address_new_any (family);
1059   }
1060
1061   rtp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtp);
1062   if (!g_socket_bind (rtp_socket, rtp_sockaddr, FALSE, NULL)) {
1063     g_object_unref (rtp_sockaddr);
1064     goto again;
1065   }
1066   g_object_unref (rtp_sockaddr);
1067
1068   rtp_sockaddr = g_socket_get_local_address (rtp_socket, NULL);
1069   if (rtp_sockaddr == NULL || !G_IS_INET_SOCKET_ADDRESS (rtp_sockaddr)) {
1070     g_clear_object (&rtp_sockaddr);
1071     goto socket_error;
1072   }
1073
1074   tmp_rtp =
1075       g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (rtp_sockaddr));
1076   g_object_unref (rtp_sockaddr);
1077
1078   /* check if port is even */
1079   if ((tmp_rtp & 1) != 0) {
1080     /* port not even, close and allocate another */
1081     tmp_rtp++;
1082     g_clear_object (&rtp_socket);
1083     goto again;
1084   }
1085
1086   /* set port */
1087   tmp_rtcp = tmp_rtp + 1;
1088
1089   rtcp_sockaddr = g_inet_socket_address_new (inetaddr, tmp_rtcp);
1090   if (!g_socket_bind (rtcp_socket, rtcp_sockaddr, FALSE, NULL)) {
1091     g_object_unref (rtcp_sockaddr);
1092     g_clear_object (&rtp_socket);
1093     goto again;
1094   }
1095   g_object_unref (rtcp_sockaddr);
1096
1097   g_clear_object (&inetaddr);
1098
1099   udpsrc0 = gst_element_factory_make ("udpsrc", NULL);
1100   udpsrc1 = gst_element_factory_make ("udpsrc", NULL);
1101
1102   if (udpsrc0 == NULL || udpsrc1 == NULL)
1103     goto no_udp_protocol;
1104
1105   g_object_set (G_OBJECT (udpsrc0), "socket", rtp_socket, NULL);
1106   g_object_set (G_OBJECT (udpsrc1), "socket", rtcp_socket, NULL);
1107
1108   ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1109   if (ret == GST_STATE_CHANGE_FAILURE)
1110     goto element_error;
1111   ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1112   if (ret == GST_STATE_CHANGE_FAILURE)
1113     goto element_error;
1114
1115   /* all fine, do port check */
1116   g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
1117   g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
1118
1119   /* this should not happen... */
1120   if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
1121     goto port_error;
1122
1123   if (udpsink_out[0])
1124     udpsink0 = udpsink_out[0];
1125   else
1126     udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
1127
1128   if (!udpsink0)
1129     goto no_udp_protocol;
1130
1131   g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
1132   g_object_set (G_OBJECT (udpsink0), multisink_socket, rtp_socket, NULL);
1133
1134   if (udpsink_out[1])
1135     udpsink1 = udpsink_out[1];
1136   else
1137     udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
1138
1139   if (!udpsink1)
1140     goto no_udp_protocol;
1141
1142   g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
1143   g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
1144   g_object_set (G_OBJECT (udpsink0), "buffer-size", buffer_size, NULL);
1145
1146   g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
1147   g_object_set (G_OBJECT (udpsink1), multisink_socket, rtcp_socket, NULL);
1148   g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
1149   g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
1150   g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
1151   g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
1152   g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
1153   g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
1154
1155   /* we keep these elements, we will further configure them when the
1156    * client told us to really use the UDP ports. */
1157   udpsrc_out[0] = udpsrc0;
1158   udpsrc_out[1] = udpsrc1;
1159   udpsink_out[0] = udpsink0;
1160   udpsink_out[1] = udpsink1;
1161
1162   server_port_out->min = rtpport;
1163   server_port_out->max = rtcpport;
1164
1165   *server_addr_out = addr;
1166   g_list_free_full (rejected_addresses, (GDestroyNotify) gst_rtsp_address_free);
1167
1168   g_object_unref (rtp_socket);
1169   g_object_unref (rtcp_socket);
1170
1171   return TRUE;
1172
1173   /* ERRORS */
1174 no_udp_protocol:
1175   {
1176     goto cleanup;
1177   }
1178 no_ports:
1179   {
1180     goto cleanup;
1181   }
1182 port_error:
1183   {
1184     goto cleanup;
1185   }
1186 socket_error:
1187   {
1188     goto cleanup;
1189   }
1190 element_error:
1191   {
1192     goto cleanup;
1193   }
1194 cleanup:
1195   {
1196     if (udpsrc0) {
1197       gst_element_set_state (udpsrc0, GST_STATE_NULL);
1198       gst_object_unref (udpsrc0);
1199     }
1200     if (udpsrc1) {
1201       gst_element_set_state (udpsrc1, GST_STATE_NULL);
1202       gst_object_unref (udpsrc1);
1203     }
1204     if (udpsink0) {
1205       gst_element_set_state (udpsink0, GST_STATE_NULL);
1206       gst_object_unref (udpsink0);
1207     }
1208     if (inetaddr)
1209       g_object_unref (inetaddr);
1210     g_list_free_full (rejected_addresses,
1211         (GDestroyNotify) gst_rtsp_address_free);
1212     if (addr)
1213       gst_rtsp_address_free (addr);
1214     if (rtp_socket)
1215       g_object_unref (rtp_socket);
1216     if (rtcp_socket)
1217       g_object_unref (rtcp_socket);
1218     return FALSE;
1219   }
1220 }
1221
1222 /* must be called with lock */
1223 static gboolean
1224 alloc_ports (GstRTSPStream * stream)
1225 {
1226   GstRTSPStreamPrivate *priv = stream->priv;
1227
1228   priv->have_ipv4 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1229       G_SOCKET_FAMILY_IPV4, priv->udpsrc_v4, priv->udpsink,
1230       &priv->server_port_v4, &priv->server_addr_v4);
1231
1232   priv->have_ipv6 = alloc_ports_one_family (priv->pool, priv->buffer_size,
1233       G_SOCKET_FAMILY_IPV6, priv->udpsrc_v6, priv->udpsink,
1234       &priv->server_port_v6, &priv->server_addr_v6);
1235
1236   return priv->have_ipv4 || priv->have_ipv6;
1237 }
1238
1239 /**
1240  * gst_rtsp_stream_get_server_port:
1241  * @stream: a #GstRTSPStream
1242  * @server_port: (out): result server port
1243  * @family: the port family to get
1244  *
1245  * Fill @server_port with the port pair used by the server. This function can
1246  * only be called when @stream has been joined.
1247  */
1248 void
1249 gst_rtsp_stream_get_server_port (GstRTSPStream * stream,
1250     GstRTSPRange * server_port, GSocketFamily family)
1251 {
1252   GstRTSPStreamPrivate *priv;
1253
1254   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1255   priv = stream->priv;
1256   g_return_if_fail (priv->is_joined);
1257
1258   g_mutex_lock (&priv->lock);
1259   if (family == G_SOCKET_FAMILY_IPV4) {
1260     if (server_port)
1261       *server_port = priv->server_port_v4;
1262   } else {
1263     if (server_port)
1264       *server_port = priv->server_port_v6;
1265   }
1266   g_mutex_unlock (&priv->lock);
1267 }
1268
1269 /**
1270  * gst_rtsp_stream_get_rtpsession:
1271  * @stream: a #GstRTSPStream
1272  *
1273  * Get the RTP session of this stream.
1274  *
1275  * Returns: (transfer full): The RTP session of this stream. Unref after usage.
1276  */
1277 GObject *
1278 gst_rtsp_stream_get_rtpsession (GstRTSPStream * stream)
1279 {
1280   GstRTSPStreamPrivate *priv;
1281   GObject *session;
1282
1283   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
1284
1285   priv = stream->priv;
1286
1287   g_mutex_lock (&priv->lock);
1288   if ((session = priv->session))
1289     g_object_ref (session);
1290   g_mutex_unlock (&priv->lock);
1291
1292   return session;
1293 }
1294
1295 /**
1296  * gst_rtsp_stream_get_ssrc:
1297  * @stream: a #GstRTSPStream
1298  * @ssrc: (out): result ssrc
1299  *
1300  * Get the SSRC used by the RTP session of this stream. This function can only
1301  * be called when @stream has been joined.
1302  */
1303 void
1304 gst_rtsp_stream_get_ssrc (GstRTSPStream * stream, guint * ssrc)
1305 {
1306   GstRTSPStreamPrivate *priv;
1307
1308   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1309   priv = stream->priv;
1310   g_return_if_fail (priv->is_joined);
1311
1312   g_mutex_lock (&priv->lock);
1313   if (ssrc && priv->session)
1314     g_object_get (priv->session, "internal-ssrc", ssrc, NULL);
1315   g_mutex_unlock (&priv->lock);
1316 }
1317
1318 /**
1319  * gst_rtsp_stream_set_retransmission_time:
1320  * @stream: a #GstRTSPStream
1321  * @time: a #GstClockTime
1322  *
1323  * Set the amount of time to store retransmission packets.
1324  */
1325 void
1326 gst_rtsp_stream_set_retransmission_time (GstRTSPStream * stream,
1327     GstClockTime time)
1328 {
1329   GST_DEBUG_OBJECT (stream, "set retransmission time %" G_GUINT64_FORMAT, time);
1330
1331   g_mutex_lock (&stream->priv->lock);
1332   stream->priv->rtx_time = time;
1333   if (stream->priv->rtxsend)
1334     g_object_set (stream->priv->rtxsend, "max-size-time",
1335         GST_TIME_AS_MSECONDS (time), NULL);
1336   g_mutex_unlock (&stream->priv->lock);
1337 }
1338
1339 /**
1340  * gst_rtsp_media_get_retransmission_time:
1341  * @media: a #GstRTSPMedia
1342  *
1343  * Get the amount of time to store retransmission data.
1344  *
1345  * Returns: the amount of time to store retransmission data.
1346  */
1347 GstClockTime
1348 gst_rtsp_stream_get_retransmission_time (GstRTSPStream * stream)
1349 {
1350   GstClockTime ret;
1351
1352   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1353
1354   g_mutex_lock (&stream->priv->lock);
1355   ret = stream->priv->rtx_time;
1356   g_mutex_unlock (&stream->priv->lock);
1357
1358   return ret;
1359 }
1360
1361 void
1362 gst_rtsp_stream_set_retransmission_pt (GstRTSPStream * stream, guint rtx_pt)
1363 {
1364   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
1365
1366   GST_DEBUG_OBJECT (stream, "set retransmission pt %u", rtx_pt);
1367
1368   g_mutex_lock (&stream->priv->lock);
1369   stream->priv->rtx_pt = rtx_pt;
1370   if (stream->priv->rtxsend) {
1371     guint pt = gst_rtsp_stream_get_pt (stream);
1372     gchar *pt_s = g_strdup_printf ("%d", pt);
1373     GstStructure *rtx_pt_map = gst_structure_new ("application/x-rtp-pt-map",
1374         pt_s, G_TYPE_UINT, rtx_pt, NULL);
1375     g_object_set (stream->priv->rtxsend, "payload-type-map", rtx_pt_map, NULL);
1376     g_free (pt_s);
1377     gst_structure_free (rtx_pt_map);
1378   }
1379   g_mutex_unlock (&stream->priv->lock);
1380 }
1381
1382 guint
1383 gst_rtsp_stream_get_retransmission_pt (GstRTSPStream * stream)
1384 {
1385   guint rtx_pt;
1386
1387   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
1388
1389   g_mutex_lock (&stream->priv->lock);
1390   rtx_pt = stream->priv->rtx_pt;
1391   g_mutex_unlock (&stream->priv->lock);
1392
1393   return rtx_pt;
1394 }
1395
1396 /* executed from streaming thread */
1397 static void
1398 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPStream * stream)
1399 {
1400   GstRTSPStreamPrivate *priv = stream->priv;
1401   GstCaps *newcaps, *oldcaps;
1402
1403   newcaps = gst_pad_get_current_caps (pad);
1404
1405   GST_INFO ("stream %p received caps %p, %" GST_PTR_FORMAT, stream, newcaps,
1406       newcaps);
1407
1408   g_mutex_lock (&priv->lock);
1409   oldcaps = priv->caps;
1410   priv->caps = newcaps;
1411   g_mutex_unlock (&priv->lock);
1412
1413   if (oldcaps)
1414     gst_caps_unref (oldcaps);
1415 }
1416
1417 static void
1418 dump_structure (const GstStructure * s)
1419 {
1420   gchar *sstr;
1421
1422   sstr = gst_structure_to_string (s);
1423   GST_INFO ("structure: %s", sstr);
1424   g_free (sstr);
1425 }
1426
1427 static GstRTSPStreamTransport *
1428 find_transport (GstRTSPStream * stream, const gchar * rtcp_from)
1429 {
1430   GstRTSPStreamPrivate *priv = stream->priv;
1431   GList *walk;
1432   GstRTSPStreamTransport *result = NULL;
1433   const gchar *tmp;
1434   gchar *dest;
1435   guint port;
1436
1437   if (rtcp_from == NULL)
1438     return NULL;
1439
1440   tmp = g_strrstr (rtcp_from, ":");
1441   if (tmp == NULL)
1442     return NULL;
1443
1444   port = atoi (tmp + 1);
1445   dest = g_strndup (rtcp_from, tmp - rtcp_from);
1446
1447   g_mutex_lock (&priv->lock);
1448   GST_INFO ("finding %s:%d in %d transports", dest, port,
1449       g_list_length (priv->transports));
1450
1451   for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1452     GstRTSPStreamTransport *trans = walk->data;
1453     const GstRTSPTransport *tr;
1454     gint min, max;
1455
1456     tr = gst_rtsp_stream_transport_get_transport (trans);
1457
1458     min = tr->client_port.min;
1459     max = tr->client_port.max;
1460
1461     if ((strcmp (tr->destination, dest) == 0) && (min == port || max == port)) {
1462       result = trans;
1463       break;
1464     }
1465   }
1466   if (result)
1467     g_object_ref (result);
1468   g_mutex_unlock (&priv->lock);
1469
1470   g_free (dest);
1471
1472   return result;
1473 }
1474
1475 static GstRTSPStreamTransport *
1476 check_transport (GObject * source, GstRTSPStream * stream)
1477 {
1478   GstStructure *stats;
1479   GstRTSPStreamTransport *trans;
1480
1481   /* see if we have a stream to match with the origin of the RTCP packet */
1482   trans = g_object_get_qdata (source, ssrc_stream_map_key);
1483   if (trans == NULL) {
1484     g_object_get (source, "stats", &stats, NULL);
1485     if (stats) {
1486       const gchar *rtcp_from;
1487
1488       dump_structure (stats);
1489
1490       rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1491       if ((trans = find_transport (stream, rtcp_from))) {
1492         GST_INFO ("%p: found transport %p for source  %p", stream, trans,
1493             source);
1494         g_object_set_qdata_full (source, ssrc_stream_map_key, trans,
1495             g_object_unref);
1496       }
1497       gst_structure_free (stats);
1498     }
1499   }
1500   return trans;
1501 }
1502
1503
1504 static void
1505 on_new_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1506 {
1507   GstRTSPStreamTransport *trans;
1508
1509   GST_INFO ("%p: new source %p", stream, source);
1510
1511   trans = check_transport (source, stream);
1512
1513   if (trans)
1514     GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1515 }
1516
1517 static void
1518 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPStream * stream)
1519 {
1520   GST_INFO ("%p: new SDES %p", stream, source);
1521 }
1522
1523 static void
1524 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
1525 {
1526   GstRTSPStreamTransport *trans;
1527
1528   trans = check_transport (source, stream);
1529
1530   if (trans) {
1531     GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1532     gst_rtsp_stream_transport_keep_alive (trans);
1533   }
1534 #ifdef DUMP_STATS
1535   {
1536     GstStructure *stats;
1537     g_object_get (source, "stats", &stats, NULL);
1538     if (stats) {
1539       dump_structure (stats);
1540       gst_structure_free (stats);
1541     }
1542   }
1543 #endif
1544 }
1545
1546 static void
1547 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
1548 {
1549   GST_INFO ("%p: source %p bye", stream, source);
1550 }
1551
1552 static void
1553 on_bye_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1554 {
1555   GstRTSPStreamTransport *trans;
1556
1557   GST_INFO ("%p: source %p bye timeout", stream, source);
1558
1559   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1560     gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1561     g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1562   }
1563 }
1564
1565 static void
1566 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
1567 {
1568   GstRTSPStreamTransport *trans;
1569
1570   GST_INFO ("%p: source %p timeout", stream, source);
1571
1572   if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1573     gst_rtsp_stream_transport_set_timed_out (trans, TRUE);
1574     g_object_set_qdata (source, ssrc_stream_map_key, NULL);
1575   }
1576 }
1577
1578 static void
1579 clear_tr_cache (GstRTSPStreamPrivate * priv, gboolean is_rtp)
1580 {
1581   if (is_rtp) {
1582     g_list_foreach (priv->tr_cache_rtp, (GFunc) g_object_unref, NULL);
1583     g_list_free (priv->tr_cache_rtp);
1584     priv->tr_cache_rtp = NULL;
1585   } else {
1586     g_list_foreach (priv->tr_cache_rtcp, (GFunc) g_object_unref, NULL);
1587     g_list_free (priv->tr_cache_rtcp);
1588     priv->tr_cache_rtcp = NULL;
1589   }
1590 }
1591
1592 static GstFlowReturn
1593 handle_new_sample (GstAppSink * sink, gpointer user_data)
1594 {
1595   GstRTSPStreamPrivate *priv;
1596   GList *walk;
1597   GstSample *sample;
1598   GstBuffer *buffer;
1599   GstRTSPStream *stream;
1600   gboolean is_rtp;
1601
1602   sample = gst_app_sink_pull_sample (sink);
1603   if (!sample)
1604     return GST_FLOW_OK;
1605
1606   stream = (GstRTSPStream *) user_data;
1607   priv = stream->priv;
1608   buffer = gst_sample_get_buffer (sample);
1609
1610   is_rtp = GST_ELEMENT_CAST (sink) == priv->appsink[0];
1611
1612   g_mutex_lock (&priv->lock);
1613   if (priv->tr_cache_cookie != priv->transports_cookie) {
1614     clear_tr_cache (priv, is_rtp);
1615     for (walk = priv->transports; walk; walk = g_list_next (walk)) {
1616       GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1617       if (is_rtp) {
1618         priv->tr_cache_rtp =
1619             g_list_prepend (priv->tr_cache_rtp, g_object_ref (tr));
1620       } else {
1621         priv->tr_cache_rtcp =
1622             g_list_prepend (priv->tr_cache_rtcp, g_object_ref (tr));
1623       }
1624     }
1625     priv->tr_cache_cookie = priv->transports_cookie;
1626   }
1627   g_mutex_unlock (&priv->lock);
1628
1629   if (is_rtp) {
1630     for (walk = priv->tr_cache_rtp; walk; walk = g_list_next (walk)) {
1631       GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1632       gst_rtsp_stream_transport_send_rtp (tr, buffer);
1633     }
1634   } else {
1635     for (walk = priv->tr_cache_rtcp; walk; walk = g_list_next (walk)) {
1636       GstRTSPStreamTransport *tr = (GstRTSPStreamTransport *) walk->data;
1637       gst_rtsp_stream_transport_send_rtcp (tr, buffer);
1638     }
1639   }
1640   gst_sample_unref (sample);
1641
1642   return GST_FLOW_OK;
1643 }
1644
1645 static GstAppSinkCallbacks sink_cb = {
1646   NULL,                         /* not interested in EOS */
1647   NULL,                         /* not interested in preroll samples */
1648   handle_new_sample,
1649 };
1650
1651 static GstElement *
1652 get_rtp_encoder (GstRTSPStream * stream, guint session)
1653 {
1654   GstRTSPStreamPrivate *priv = stream->priv;
1655
1656   if (priv->srtpenc == NULL) {
1657     gchar *name;
1658
1659     name = g_strdup_printf ("srtpenc_%u", session);
1660     priv->srtpenc = gst_element_factory_make ("srtpenc", name);
1661     g_free (name);
1662
1663     g_object_set (priv->srtpenc, "random-key", TRUE, NULL);
1664   }
1665   return gst_object_ref (priv->srtpenc);
1666 }
1667
1668 static GstElement *
1669 request_rtp_encoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
1670 {
1671   GstRTSPStreamPrivate *priv = stream->priv;
1672   GstElement *oldenc, *enc;
1673   GstPad *pad;
1674   gchar *name;
1675
1676   if (priv->idx != session)
1677     return NULL;
1678
1679   GST_DEBUG_OBJECT (stream, "make RTP encoder for session %u", session);
1680
1681   oldenc = priv->srtpenc;
1682   enc = get_rtp_encoder (stream, session);
1683   name = g_strdup_printf ("rtp_sink_%d", session);
1684   pad = gst_element_get_request_pad (enc, name);
1685   g_free (name);
1686   gst_object_unref (pad);
1687
1688   if (oldenc == NULL)
1689     g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTP_ENCODER], 0,
1690         enc);
1691
1692   return enc;
1693 }
1694
1695 static GstElement *
1696 request_rtcp_encoder (GstElement * rtpbin, guint session,
1697     GstRTSPStream * stream)
1698 {
1699   GstRTSPStreamPrivate *priv = stream->priv;
1700   GstElement *oldenc, *enc;
1701   GstPad *pad;
1702   gchar *name;
1703
1704   if (priv->idx != session)
1705     return NULL;
1706
1707   GST_DEBUG_OBJECT (stream, "make RTCP encoder for session %u", session);
1708
1709   oldenc = priv->srtpenc;
1710   enc = get_rtp_encoder (stream, session);
1711   name = g_strdup_printf ("rtcp_sink_%d", session);
1712   pad = gst_element_get_request_pad (enc, name);
1713   g_free (name);
1714   gst_object_unref (pad);
1715
1716   if (oldenc == NULL)
1717     g_signal_emit (stream, gst_rtsp_stream_signals[SIGNAL_NEW_RTCP_ENCODER], 0,
1718         enc);
1719
1720   return enc;
1721 }
1722
1723 static GstCaps *
1724 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
1725 {
1726   GstRTSPStreamPrivate *priv = stream->priv;
1727   GstCaps *caps;
1728
1729   GST_DEBUG ("request key %08x", ssrc);
1730
1731   g_mutex_lock (&priv->lock);
1732   if ((caps = g_hash_table_lookup (priv->keys, GINT_TO_POINTER (ssrc))))
1733     gst_caps_ref (caps);
1734   g_mutex_unlock (&priv->lock);
1735
1736   return caps;
1737 }
1738
1739 static GstElement *
1740 request_rtcp_decoder (GstElement * rtpbin, guint session,
1741     GstRTSPStream * stream)
1742 {
1743   GstRTSPStreamPrivate *priv = stream->priv;
1744
1745   if (priv->idx != session)
1746     return NULL;
1747
1748   if (priv->srtpdec == NULL) {
1749     gchar *name;
1750
1751     name = g_strdup_printf ("srtpdec_%u", session);
1752     priv->srtpdec = gst_element_factory_make ("srtpdec", name);
1753     g_free (name);
1754
1755     g_signal_connect (priv->srtpdec, "request-key",
1756         (GCallback) request_key, stream);
1757   }
1758   return gst_object_ref (priv->srtpdec);
1759 }
1760
1761 static GstElement *
1762 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPStream * stream)
1763 {
1764   GstElement *bin;
1765   GstPad *pad;
1766   GstStructure *pt_map;
1767   gchar *name;
1768   guint pt, rtx_pt;
1769   gchar *pt_s;
1770
1771   pt = gst_rtsp_stream_get_pt (stream);
1772   pt_s = g_strdup_printf ("%u", pt);
1773   rtx_pt = stream->priv->rtx_pt;
1774
1775   GST_INFO ("creating rtxsend with pt %u to %u", pt, rtx_pt);
1776
1777   bin = gst_bin_new (NULL);
1778   stream->priv->rtxsend = gst_element_factory_make ("rtprtxsend", NULL);
1779   pt_map = gst_structure_new ("application/x-rtp-pt-map",
1780       pt_s, G_TYPE_UINT, rtx_pt, NULL);
1781   g_object_set (stream->priv->rtxsend, "payload-type-map", pt_map,
1782       "max-size-time", GST_TIME_AS_MSECONDS (stream->priv->rtx_time), NULL);
1783   g_free (pt_s);
1784   gst_structure_free (pt_map);
1785   gst_bin_add (GST_BIN (bin), gst_object_ref (stream->priv->rtxsend));
1786
1787   pad = gst_element_get_static_pad (stream->priv->rtxsend, "src");
1788   name = g_strdup_printf ("src_%u", sessid);
1789   gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1790   g_free (name);
1791   gst_object_unref (pad);
1792
1793   pad = gst_element_get_static_pad (stream->priv->rtxsend, "sink");
1794   name = g_strdup_printf ("sink_%u", sessid);
1795   gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
1796   g_free (name);
1797   gst_object_unref (pad);
1798
1799   return bin;
1800 }
1801
1802 /**
1803  * gst_rtsp_stream_join_bin:
1804  * @stream: a #GstRTSPStream
1805  * @bin: (transfer none): a #GstBin to join
1806  * @rtpbin: (transfer none): a rtpbin element in @bin
1807  * @state: the target state of the new elements
1808  *
1809  * Join the #GstBin @bin that contains the element @rtpbin.
1810  *
1811  * @stream will link to @rtpbin, which must be inside @bin. The elements
1812  * added to @bin will be set to the state given in @state.
1813  *
1814  * Returns: %TRUE on success.
1815  */
1816 gboolean
1817 gst_rtsp_stream_join_bin (GstRTSPStream * stream, GstBin * bin,
1818     GstElement * rtpbin, GstState state)
1819 {
1820   GstRTSPStreamPrivate *priv;
1821   gint i;
1822   guint idx;
1823   gchar *name;
1824   GstPad *pad, *sinkpad, *selpad;
1825   GstPadLinkReturn ret;
1826
1827   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
1828   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
1829   g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
1830
1831   priv = stream->priv;
1832
1833   g_mutex_lock (&priv->lock);
1834   if (priv->is_joined)
1835     goto was_joined;
1836
1837   /* create a session with the same index as the stream */
1838   idx = priv->idx;
1839
1840   GST_INFO ("stream %p joining bin as session %u", stream, idx);
1841
1842   if (!alloc_ports (stream))
1843     goto no_ports;
1844
1845   /* update the dscp qos field in the sinks */
1846   update_dscp_qos (stream);
1847
1848   if (priv->profiles & GST_RTSP_PROFILE_SAVP
1849       || priv->profiles & GST_RTSP_PROFILE_SAVPF) {
1850     /* For SRTP */
1851     g_signal_connect (rtpbin, "request-rtp-encoder",
1852         (GCallback) request_rtp_encoder, stream);
1853     g_signal_connect (rtpbin, "request-rtcp-encoder",
1854         (GCallback) request_rtcp_encoder, stream);
1855     g_signal_connect (rtpbin, "request-rtcp-decoder",
1856         (GCallback) request_rtcp_decoder, stream);
1857   }
1858
1859   if (priv->rtx_time > 0) {
1860     /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
1861     g_signal_connect (rtpbin, "request-aux-sender",
1862         (GCallback) request_aux_sender, stream);
1863   }
1864
1865   /* get a pad for sending RTP */
1866   name = g_strdup_printf ("send_rtp_sink_%u", idx);
1867   priv->send_rtp_sink = gst_element_get_request_pad (rtpbin, name);
1868   g_free (name);
1869   /* link the RTP pad to the session manager, it should not really fail unless
1870    * this is not really an RTP pad */
1871   ret = gst_pad_link (priv->srcpad, priv->send_rtp_sink);
1872   if (ret != GST_PAD_LINK_OK)
1873     goto link_failed;
1874
1875   /* get pads from the RTP session element for sending and receiving
1876    * RTP/RTCP*/
1877   name = g_strdup_printf ("send_rtp_src_%u", idx);
1878   priv->send_src[0] = gst_element_get_static_pad (rtpbin, name);
1879   g_free (name);
1880   name = g_strdup_printf ("send_rtcp_src_%u", idx);
1881   priv->send_src[1] = gst_element_get_request_pad (rtpbin, name);
1882   g_free (name);
1883   name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1884   priv->recv_sink[0] = gst_element_get_request_pad (rtpbin, name);
1885   g_free (name);
1886   name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1887   priv->recv_sink[1] = gst_element_get_request_pad (rtpbin, name);
1888   g_free (name);
1889
1890   /* get the session */
1891   g_signal_emit_by_name (rtpbin, "get-internal-session", idx, &priv->session);
1892
1893   g_signal_connect (priv->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1894       stream);
1895   g_signal_connect (priv->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1896       stream);
1897   g_signal_connect (priv->session, "on-ssrc-active",
1898       (GCallback) on_ssrc_active, stream);
1899   g_signal_connect (priv->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1900       stream);
1901   g_signal_connect (priv->session, "on-bye-timeout",
1902       (GCallback) on_bye_timeout, stream);
1903   g_signal_connect (priv->session, "on-timeout", (GCallback) on_timeout,
1904       stream);
1905
1906   for (i = 0; i < 2; i++) {
1907     GstPad *teepad, *queuepad;
1908     /* For the sender we create this bit of pipeline for both
1909      * RTP and RTCP. Sync and preroll are enabled on udpsink so
1910      * we need to add a queue before appsink to make the pipeline
1911      * not block. For the TCP case, we want to pump data to the
1912      * client as fast as possible anyway.
1913      *
1914      * .--------.      .-----.    .---------.
1915      * | rtpbin |      | tee |    | udpsink |
1916      * |       send->sink   src->sink       |
1917      * '--------'      |     |    '---------'
1918      *                 |     |    .---------.    .---------.
1919      *                 |     |    |  queue  |    | appsink |
1920      *                 |    src->sink      src->sink       |
1921      *                 '-----'    '---------'    '---------'
1922      *
1923      * When only UDP is allowed, we skip the tee, queue and appsink and link the
1924      * udpsink directly to the session.
1925      */
1926     /* add udpsink */
1927     gst_bin_add (bin, priv->udpsink[i]);
1928     sinkpad = gst_element_get_static_pad (priv->udpsink[i], "sink");
1929
1930     if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
1931       /* make tee for RTP/RTCP */
1932       priv->tee[i] = gst_element_factory_make ("tee", NULL);
1933       gst_bin_add (bin, priv->tee[i]);
1934
1935       /* and link to rtpbin send pad */
1936       pad = gst_element_get_static_pad (priv->tee[i], "sink");
1937       gst_pad_link (priv->send_src[i], pad);
1938       gst_object_unref (pad);
1939
1940       /* link tee to udpsink */
1941       teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1942       gst_pad_link (teepad, sinkpad);
1943       gst_object_unref (teepad);
1944
1945       /* make queue */
1946       priv->appqueue[i] = gst_element_factory_make ("queue", NULL);
1947       gst_bin_add (bin, priv->appqueue[i]);
1948       /* and link to tee */
1949       teepad = gst_element_get_request_pad (priv->tee[i], "src_%u");
1950       pad = gst_element_get_static_pad (priv->appqueue[i], "sink");
1951       gst_pad_link (teepad, pad);
1952       gst_object_unref (pad);
1953       gst_object_unref (teepad);
1954
1955       /* make appsink */
1956       priv->appsink[i] = gst_element_factory_make ("appsink", NULL);
1957       g_object_set (priv->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1958       g_object_set (priv->appsink[i], "emit-signals", FALSE, NULL);
1959       gst_bin_add (bin, priv->appsink[i]);
1960       gst_app_sink_set_callbacks (GST_APP_SINK_CAST (priv->appsink[i]),
1961           &sink_cb, stream, NULL);
1962       /* and link to queue */
1963       queuepad = gst_element_get_static_pad (priv->appqueue[i], "src");
1964       pad = gst_element_get_static_pad (priv->appsink[i], "sink");
1965       gst_pad_link (queuepad, pad);
1966       gst_object_unref (pad);
1967       gst_object_unref (queuepad);
1968     } else {
1969       /* else only udpsink needed, link it to the session */
1970       gst_pad_link (priv->send_src[i], sinkpad);
1971     }
1972     gst_object_unref (sinkpad);
1973
1974     /* For the receiver we create this bit of pipeline for both
1975      * RTP and RTCP. We receive RTP/RTCP on appsrc and udpsrc
1976      * and it is all funneled into the rtpbin receive pad.
1977      *
1978      * .--------.     .--------.    .--------.
1979      * | udpsrc |     | funnel |    | rtpbin |
1980      * |       src->sink      src->sink      |
1981      * '--------'     |        |    '--------'
1982      * .--------.     |        |
1983      * | appsrc |     |        |
1984      * |       src->sink       |
1985      * '--------'     '--------'
1986      */
1987     /* make funnel for the RTP/RTCP receivers */
1988     priv->funnel[i] = gst_element_factory_make ("funnel", NULL);
1989     gst_bin_add (bin, priv->funnel[i]);
1990
1991     pad = gst_element_get_static_pad (priv->funnel[i], "src");
1992     gst_pad_link (pad, priv->recv_sink[i]);
1993     gst_object_unref (pad);
1994
1995     if (priv->udpsrc_v4[i]) {
1996       /* we set and keep these to playing so that they don't cause NO_PREROLL return
1997        * values */
1998       gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_PLAYING);
1999       gst_element_set_locked_state (priv->udpsrc_v4[i], TRUE);
2000       /* add udpsrc */
2001       gst_bin_add (bin, priv->udpsrc_v4[i]);
2002
2003       /* and link to the funnel v4 */
2004       selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2005       pad = gst_element_get_static_pad (priv->udpsrc_v4[i], "src");
2006       gst_pad_link (pad, selpad);
2007       gst_object_unref (pad);
2008       gst_object_unref (selpad);
2009     }
2010
2011     if (priv->udpsrc_v6[i]) {
2012       gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_PLAYING);
2013       gst_element_set_locked_state (priv->udpsrc_v6[i], TRUE);
2014       gst_bin_add (bin, priv->udpsrc_v6[i]);
2015
2016       /* and link to the funnel v6 */
2017       selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2018       pad = gst_element_get_static_pad (priv->udpsrc_v6[i], "src");
2019       gst_pad_link (pad, selpad);
2020       gst_object_unref (pad);
2021       gst_object_unref (selpad);
2022     }
2023
2024     if (priv->protocols & GST_RTSP_LOWER_TRANS_TCP) {
2025       /* make and add appsrc */
2026       priv->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
2027       g_object_set (priv->appsrc[i], "format", GST_FORMAT_TIME, NULL);
2028       gst_bin_add (bin, priv->appsrc[i]);
2029       /* and link to the funnel */
2030       selpad = gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2031       pad = gst_element_get_static_pad (priv->appsrc[i], "src");
2032       gst_pad_link (pad, selpad);
2033       gst_object_unref (pad);
2034       gst_object_unref (selpad);
2035     }
2036
2037     /* check if we need to set to a special state */
2038     if (state != GST_STATE_NULL) {
2039       if (priv->udpsink[i])
2040         gst_element_set_state (priv->udpsink[i], state);
2041       if (priv->appsink[i])
2042         gst_element_set_state (priv->appsink[i], state);
2043       if (priv->appqueue[i])
2044         gst_element_set_state (priv->appqueue[i], state);
2045       if (priv->tee[i])
2046         gst_element_set_state (priv->tee[i], state);
2047       if (priv->funnel[i])
2048         gst_element_set_state (priv->funnel[i], state);
2049       if (priv->appsrc[i])
2050         gst_element_set_state (priv->appsrc[i], state);
2051     }
2052   }
2053
2054   /* be notified of caps changes */
2055   priv->caps_sig = g_signal_connect (priv->send_src[0], "notify::caps",
2056       (GCallback) caps_notify, stream);
2057
2058   priv->is_joined = TRUE;
2059   g_mutex_unlock (&priv->lock);
2060
2061   return TRUE;
2062
2063   /* ERRORS */
2064 was_joined:
2065   {
2066     g_mutex_unlock (&priv->lock);
2067     return TRUE;
2068   }
2069 no_ports:
2070   {
2071     g_mutex_unlock (&priv->lock);
2072     GST_WARNING ("failed to allocate ports %u", idx);
2073     return FALSE;
2074   }
2075 link_failed:
2076   {
2077     GST_WARNING ("failed to link stream %u", idx);
2078     gst_object_unref (priv->send_rtp_sink);
2079     priv->send_rtp_sink = NULL;
2080     g_mutex_unlock (&priv->lock);
2081     return FALSE;
2082   }
2083 }
2084
2085 /**
2086  * gst_rtsp_stream_leave_bin:
2087  * @stream: a #GstRTSPStream
2088  * @bin: (transfer none): a #GstBin
2089  * @rtpbin: (transfer none): a rtpbin #GstElement
2090  *
2091  * Remove the elements of @stream from @bin.
2092  *
2093  * Return: %TRUE on success.
2094  */
2095 gboolean
2096 gst_rtsp_stream_leave_bin (GstRTSPStream * stream, GstBin * bin,
2097     GstElement * rtpbin)
2098 {
2099   GstRTSPStreamPrivate *priv;
2100   gint i;
2101   GList *l;
2102
2103   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2104   g_return_val_if_fail (GST_IS_BIN (bin), FALSE);
2105   g_return_val_if_fail (GST_IS_ELEMENT (rtpbin), FALSE);
2106
2107   priv = stream->priv;
2108
2109   g_mutex_lock (&priv->lock);
2110   if (!priv->is_joined)
2111     goto was_not_joined;
2112
2113   /* all transports must be removed by now */
2114   if (priv->transports != NULL)
2115     goto transports_not_removed;
2116
2117   clear_tr_cache (priv, TRUE);
2118   clear_tr_cache (priv, FALSE);
2119
2120   GST_INFO ("stream %p leaving bin", stream);
2121
2122   gst_pad_unlink (priv->srcpad, priv->send_rtp_sink);
2123   g_signal_handler_disconnect (priv->send_src[0], priv->caps_sig);
2124   gst_element_release_request_pad (rtpbin, priv->send_rtp_sink);
2125   gst_object_unref (priv->send_rtp_sink);
2126   priv->send_rtp_sink = NULL;
2127
2128   for (i = 0; i < 2; i++) {
2129     if (priv->udpsink[i])
2130       gst_element_set_state (priv->udpsink[i], GST_STATE_NULL);
2131     if (priv->appsink[i])
2132       gst_element_set_state (priv->appsink[i], GST_STATE_NULL);
2133     if (priv->appqueue[i])
2134       gst_element_set_state (priv->appqueue[i], GST_STATE_NULL);
2135     if (priv->tee[i])
2136       gst_element_set_state (priv->tee[i], GST_STATE_NULL);
2137     if (priv->funnel[i])
2138       gst_element_set_state (priv->funnel[i], GST_STATE_NULL);
2139     if (priv->appsrc[i])
2140       gst_element_set_state (priv->appsrc[i], GST_STATE_NULL);
2141     if (priv->udpsrc_v4[i]) {
2142       /* and set udpsrc to NULL now before removing */
2143       gst_element_set_locked_state (priv->udpsrc_v4[i], FALSE);
2144       gst_element_set_state (priv->udpsrc_v4[i], GST_STATE_NULL);
2145       /* removing them should also nicely release the request
2146        * pads when they finalize */
2147       gst_bin_remove (bin, priv->udpsrc_v4[i]);
2148     }
2149     if (priv->udpsrc_v6[i]) {
2150       gst_element_set_locked_state (priv->udpsrc_v6[i], FALSE);
2151       gst_element_set_state (priv->udpsrc_v6[i], GST_STATE_NULL);
2152       gst_bin_remove (bin, priv->udpsrc_v6[i]);
2153     }
2154
2155     for (l = priv->transport_sources; l; l = l->next) {
2156       GstRTSPMulticastTransportSource *s = l->data;
2157
2158       if (!s->udpsrc[i])
2159         continue;
2160
2161       gst_element_set_locked_state (s->udpsrc[i], FALSE);
2162       gst_element_set_state (s->udpsrc[i], GST_STATE_NULL);
2163       gst_bin_remove (bin, s->udpsrc[i]);
2164     }
2165
2166     if (priv->udpsink[i])
2167       gst_bin_remove (bin, priv->udpsink[i]);
2168     if (priv->appsrc[i])
2169       gst_bin_remove (bin, priv->appsrc[i]);
2170     if (priv->appsink[i])
2171       gst_bin_remove (bin, priv->appsink[i]);
2172     if (priv->appqueue[i])
2173       gst_bin_remove (bin, priv->appqueue[i]);
2174     if (priv->tee[i])
2175       gst_bin_remove (bin, priv->tee[i]);
2176     if (priv->funnel[i])
2177       gst_bin_remove (bin, priv->funnel[i]);
2178
2179     gst_element_release_request_pad (rtpbin, priv->recv_sink[i]);
2180     gst_object_unref (priv->recv_sink[i]);
2181     priv->recv_sink[i] = NULL;
2182
2183     priv->udpsrc_v4[i] = NULL;
2184     priv->udpsrc_v6[i] = NULL;
2185     priv->udpsink[i] = NULL;
2186     priv->appsrc[i] = NULL;
2187     priv->appsink[i] = NULL;
2188     priv->appqueue[i] = NULL;
2189     priv->tee[i] = NULL;
2190     priv->funnel[i] = NULL;
2191   }
2192
2193   for (l = priv->transport_sources; l; l = l->next) {
2194     GstRTSPMulticastTransportSource *s = l->data;
2195     g_slice_free (GstRTSPMulticastTransportSource, s);
2196   }
2197   g_list_free (priv->transport_sources);
2198   priv->transport_sources = NULL;
2199
2200   gst_object_unref (priv->send_src[0]);
2201   priv->send_src[0] = NULL;
2202
2203   gst_element_release_request_pad (rtpbin, priv->send_src[1]);
2204   gst_object_unref (priv->send_src[1]);
2205   priv->send_src[1] = NULL;
2206
2207   g_object_unref (priv->session);
2208   priv->session = NULL;
2209   if (priv->caps)
2210     gst_caps_unref (priv->caps);
2211   priv->caps = NULL;
2212
2213   if (priv->srtpenc)
2214     gst_object_unref (priv->srtpenc);
2215   if (priv->srtpdec)
2216     gst_object_unref (priv->srtpdec);
2217
2218   priv->is_joined = FALSE;
2219   g_mutex_unlock (&priv->lock);
2220
2221   return TRUE;
2222
2223 was_not_joined:
2224   {
2225     g_mutex_unlock (&priv->lock);
2226     return TRUE;
2227   }
2228 transports_not_removed:
2229   {
2230     GST_ERROR_OBJECT (stream, "can't leave bin (transports not removed)");
2231     g_mutex_unlock (&priv->lock);
2232     return FALSE;
2233   }
2234 }
2235
2236 /**
2237  * gst_rtsp_stream_get_rtpinfo:
2238  * @stream: a #GstRTSPStream
2239  * @rtptime: (allow-none): result RTP timestamp
2240  * @seq: (allow-none): result RTP seqnum
2241  * @clock_rate: (allow-none): the clock rate
2242  * @running_time: (allow-none): result running-time
2243  *
2244  * Retrieve the current rtptime, seq and running-time. This is used to
2245  * construct a RTPInfo reply header.
2246  *
2247  * Returns: %TRUE when rtptime, seq and running-time could be determined.
2248  */
2249 gboolean
2250 gst_rtsp_stream_get_rtpinfo (GstRTSPStream * stream,
2251     guint * rtptime, guint * seq, guint * clock_rate,
2252     GstClockTime * running_time)
2253 {
2254   GstRTSPStreamPrivate *priv;
2255   GstStructure *stats;
2256   GObjectClass *payobjclass;
2257
2258   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2259
2260   priv = stream->priv;
2261
2262   payobjclass = G_OBJECT_GET_CLASS (priv->payloader);
2263
2264   g_mutex_lock (&priv->lock);
2265
2266   if (g_object_class_find_property (payobjclass, "stats")) {
2267     g_object_get (priv->payloader, "stats", &stats, NULL);
2268     if (stats == NULL)
2269       goto no_stats;
2270
2271     if (seq)
2272       gst_structure_get_uint (stats, "seqnum", seq);
2273
2274     if (rtptime)
2275       gst_structure_get_uint (stats, "timestamp", rtptime);
2276
2277     if (running_time)
2278       gst_structure_get_clock_time (stats, "running-time", running_time);
2279
2280     if (clock_rate) {
2281       gst_structure_get_uint (stats, "clock-rate", clock_rate);
2282       if (*clock_rate == 0 && running_time)
2283         *running_time = GST_CLOCK_TIME_NONE;
2284     }
2285     gst_structure_free (stats);
2286   } else {
2287     if (!g_object_class_find_property (payobjclass, "seqnum") ||
2288         !g_object_class_find_property (payobjclass, "timestamp"))
2289       goto no_stats;
2290
2291     if (seq)
2292       g_object_get (priv->payloader, "seqnum", seq, NULL);
2293
2294     if (rtptime)
2295       g_object_get (priv->payloader, "timestamp", rtptime, NULL);
2296
2297     if (running_time)
2298       *running_time = GST_CLOCK_TIME_NONE;
2299   }
2300   g_mutex_unlock (&priv->lock);
2301
2302   return TRUE;
2303
2304   /* ERRORS */
2305 no_stats:
2306   {
2307     GST_WARNING ("Could not get payloader stats");
2308     g_mutex_unlock (&priv->lock);
2309     return FALSE;
2310   }
2311 }
2312
2313 /**
2314  * gst_rtsp_stream_get_caps:
2315  * @stream: a #GstRTSPStream
2316  *
2317  * Retrieve the current caps of @stream.
2318  *
2319  * Returns: (transfer full): the #GstCaps of @stream. use gst_caps_unref()
2320  * after usage.
2321  */
2322 GstCaps *
2323 gst_rtsp_stream_get_caps (GstRTSPStream * stream)
2324 {
2325   GstRTSPStreamPrivate *priv;
2326   GstCaps *result;
2327
2328   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2329
2330   priv = stream->priv;
2331
2332   g_mutex_lock (&priv->lock);
2333   if ((result = priv->caps))
2334     gst_caps_ref (result);
2335   g_mutex_unlock (&priv->lock);
2336
2337   return result;
2338 }
2339
2340 /**
2341  * gst_rtsp_stream_recv_rtp:
2342  * @stream: a #GstRTSPStream
2343  * @buffer: (transfer full): a #GstBuffer
2344  *
2345  * Handle an RTP buffer for the stream. This method is usually called when a
2346  * message has been received from a client using the TCP transport.
2347  *
2348  * This function takes ownership of @buffer.
2349  *
2350  * Returns: a GstFlowReturn.
2351  */
2352 GstFlowReturn
2353 gst_rtsp_stream_recv_rtp (GstRTSPStream * stream, GstBuffer * buffer)
2354 {
2355   GstRTSPStreamPrivate *priv;
2356   GstFlowReturn ret;
2357   GstElement *element;
2358
2359   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2360   priv = stream->priv;
2361   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2362   g_return_val_if_fail (priv->is_joined, FALSE);
2363
2364   g_mutex_lock (&priv->lock);
2365   if (priv->appsrc[0])
2366     element = gst_object_ref (priv->appsrc[0]);
2367   else
2368     element = NULL;
2369   g_mutex_unlock (&priv->lock);
2370
2371   if (element) {
2372     ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2373     gst_object_unref (element);
2374   } else {
2375     ret = GST_FLOW_OK;
2376   }
2377   return ret;
2378 }
2379
2380 /**
2381  * gst_rtsp_stream_recv_rtcp:
2382  * @stream: a #GstRTSPStream
2383  * @buffer: (transfer full): a #GstBuffer
2384  *
2385  * Handle an RTCP buffer for the stream. This method is usually called when a
2386  * message has been received from a client using the TCP transport.
2387  *
2388  * This function takes ownership of @buffer.
2389  *
2390  * Returns: a GstFlowReturn.
2391  */
2392 GstFlowReturn
2393 gst_rtsp_stream_recv_rtcp (GstRTSPStream * stream, GstBuffer * buffer)
2394 {
2395   GstRTSPStreamPrivate *priv;
2396   GstFlowReturn ret;
2397   GstElement *element;
2398
2399   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), GST_FLOW_ERROR);
2400   priv = stream->priv;
2401   g_return_val_if_fail (GST_IS_BUFFER (buffer), GST_FLOW_ERROR);
2402
2403   if (!priv->is_joined) {
2404     gst_buffer_unref (buffer);
2405     return GST_FLOW_NOT_LINKED;
2406   }
2407   g_mutex_lock (&priv->lock);
2408   if (priv->appsrc[1])
2409     element = gst_object_ref (priv->appsrc[1]);
2410   else
2411     element = NULL;
2412   g_mutex_unlock (&priv->lock);
2413
2414   if (element) {
2415     ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (element), buffer);
2416     gst_object_unref (element);
2417   } else {
2418     ret = GST_FLOW_OK;
2419     gst_buffer_unref (buffer);
2420   }
2421   return ret;
2422 }
2423
2424 /* must be called with lock */
2425 static gboolean
2426 update_transport (GstRTSPStream * stream, GstRTSPStreamTransport * trans,
2427     gboolean add)
2428 {
2429   GstRTSPStreamPrivate *priv = stream->priv;
2430   const GstRTSPTransport *tr;
2431
2432   tr = gst_rtsp_stream_transport_get_transport (trans);
2433
2434   switch (tr->lower_transport) {
2435     case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2436     {
2437       GstRTSPMulticastTransportSource *source;
2438       GstBin *bin;
2439
2440       bin = GST_BIN (gst_object_get_parent (GST_OBJECT (priv->funnel[0])));
2441
2442       if (add) {
2443         gchar *host;
2444         gint i;
2445         GstPad *selpad, *pad;
2446
2447         source = g_slice_new0 (GstRTSPMulticastTransportSource);
2448         source->transport = trans;
2449
2450         for (i = 0; i < 2; i++) {
2451           host =
2452               g_strdup_printf ("udp://%s:%d", tr->destination,
2453               (i == 0) ? tr->port.min : tr->port.max);
2454           source->udpsrc[i] =
2455               gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2456           g_free (host);
2457
2458           /* we set and keep these to playing so that they don't cause NO_PREROLL return
2459            * values */
2460           gst_element_set_state (source->udpsrc[i], GST_STATE_PLAYING);
2461           gst_element_set_locked_state (source->udpsrc[i], TRUE);
2462           /* add udpsrc */
2463           gst_bin_add (bin, source->udpsrc[i]);
2464
2465           /* and link to the funnel v4 */
2466           source->selpad[i] = selpad =
2467               gst_element_get_request_pad (priv->funnel[i], "sink_%u");
2468           pad = gst_element_get_static_pad (source->udpsrc[i], "src");
2469           gst_pad_link (pad, selpad);
2470           gst_object_unref (pad);
2471           gst_object_unref (selpad);
2472         }
2473         gst_object_unref (bin);
2474
2475         priv->transport_sources =
2476             g_list_prepend (priv->transport_sources, source);
2477       } else {
2478         GList *l;
2479
2480         for (l = priv->transport_sources; l; l = l->next) {
2481           source = l->data;
2482
2483           if (source->transport == trans) {
2484             priv->transport_sources =
2485                 g_list_delete_link (priv->transport_sources, l);
2486             break;
2487           }
2488         }
2489
2490         if (l != NULL) {
2491           gint i;
2492
2493           for (i = 0; i < 2; i++) {
2494             /* Will automatically unlink everything */
2495             gst_bin_remove (bin,
2496                 GST_ELEMENT (gst_object_ref (source->udpsrc[i])));
2497
2498             gst_element_set_state (source->udpsrc[i], GST_STATE_NULL);
2499             gst_object_unref (source->udpsrc[i]);
2500
2501             gst_element_release_request_pad (priv->funnel[i],
2502                 source->selpad[i]);
2503           }
2504
2505           g_slice_free (GstRTSPMulticastTransportSource, source);
2506         }
2507       }
2508
2509       /* fall through for the generic case */
2510     }
2511     case GST_RTSP_LOWER_TRANS_UDP:
2512     {
2513       gchar *dest;
2514       gint min, max;
2515       guint ttl = 0;
2516
2517       dest = tr->destination;
2518       if (tr->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2519         min = tr->port.min;
2520         max = tr->port.max;
2521         ttl = tr->ttl;
2522       } else {
2523         min = tr->client_port.min;
2524         max = tr->client_port.max;
2525       }
2526
2527       if (add) {
2528         if (ttl > 0) {
2529           GST_INFO ("setting ttl-mc %d", ttl);
2530           g_object_set (G_OBJECT (priv->udpsink[0]), "ttl-mc", ttl, NULL);
2531           g_object_set (G_OBJECT (priv->udpsink[1]), "ttl-mc", ttl, NULL);
2532         }
2533         GST_INFO ("adding %s:%d-%d", dest, min, max);
2534         g_signal_emit_by_name (priv->udpsink[0], "add", dest, min, NULL);
2535         g_signal_emit_by_name (priv->udpsink[1], "add", dest, max, NULL);
2536         priv->transports = g_list_prepend (priv->transports, trans);
2537       } else {
2538         GST_INFO ("removing %s:%d-%d", dest, min, max);
2539         g_signal_emit_by_name (priv->udpsink[0], "remove", dest, min, NULL);
2540         g_signal_emit_by_name (priv->udpsink[1], "remove", dest, max, NULL);
2541         priv->transports = g_list_remove (priv->transports, trans);
2542       }
2543       priv->transports_cookie++;
2544       break;
2545     }
2546     case GST_RTSP_LOWER_TRANS_TCP:
2547       if (add) {
2548         GST_INFO ("adding TCP %s", tr->destination);
2549         priv->transports = g_list_prepend (priv->transports, trans);
2550       } else {
2551         GST_INFO ("removing TCP %s", tr->destination);
2552         priv->transports = g_list_remove (priv->transports, trans);
2553       }
2554       priv->transports_cookie++;
2555       break;
2556     default:
2557       goto unknown_transport;
2558   }
2559   return TRUE;
2560
2561   /* ERRORS */
2562 unknown_transport:
2563   {
2564     GST_INFO ("Unknown transport %d", tr->lower_transport);
2565     return FALSE;
2566   }
2567 }
2568
2569
2570 /**
2571  * gst_rtsp_stream_add_transport:
2572  * @stream: a #GstRTSPStream
2573  * @trans: (transfer none): a #GstRTSPStreamTransport
2574  *
2575  * Add the transport in @trans to @stream. The media of @stream will
2576  * then also be send to the values configured in @trans.
2577  *
2578  * @stream must be joined to a bin.
2579  *
2580  * @trans must contain a valid #GstRTSPTransport.
2581  *
2582  * Returns: %TRUE if @trans was added
2583  */
2584 gboolean
2585 gst_rtsp_stream_add_transport (GstRTSPStream * stream,
2586     GstRTSPStreamTransport * trans)
2587 {
2588   GstRTSPStreamPrivate *priv;
2589   gboolean res;
2590
2591   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2592   priv = stream->priv;
2593   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2594   g_return_val_if_fail (priv->is_joined, FALSE);
2595
2596   g_mutex_lock (&priv->lock);
2597   res = update_transport (stream, trans, TRUE);
2598   g_mutex_unlock (&priv->lock);
2599
2600   return res;
2601 }
2602
2603 /**
2604  * gst_rtsp_stream_remove_transport:
2605  * @stream: a #GstRTSPStream
2606  * @trans: (transfer none): a #GstRTSPStreamTransport
2607  *
2608  * Remove the transport in @trans from @stream. The media of @stream will
2609  * not be sent to the values configured in @trans.
2610  *
2611  * @stream must be joined to a bin.
2612  *
2613  * @trans must contain a valid #GstRTSPTransport.
2614  *
2615  * Returns: %TRUE if @trans was removed
2616  */
2617 gboolean
2618 gst_rtsp_stream_remove_transport (GstRTSPStream * stream,
2619     GstRTSPStreamTransport * trans)
2620 {
2621   GstRTSPStreamPrivate *priv;
2622   gboolean res;
2623
2624   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2625   priv = stream->priv;
2626   g_return_val_if_fail (GST_IS_RTSP_STREAM_TRANSPORT (trans), FALSE);
2627   g_return_val_if_fail (priv->is_joined, FALSE);
2628
2629   g_mutex_lock (&priv->lock);
2630   res = update_transport (stream, trans, FALSE);
2631   g_mutex_unlock (&priv->lock);
2632
2633   return res;
2634 }
2635
2636 /**
2637  * gst_rtsp_stream_update_crypto:
2638  * @stream: a #GstRTSPStream
2639  * @ssrc: the SSRC
2640  * @crypto: (transfer none) (allow-none): a #GstCaps with crypto info
2641  *
2642  * Update the new crypto information for @ssrc in @stream. If information
2643  * for @ssrc did not exist, it will be added. If information
2644  * for @ssrc existed, it will be replaced. If @crypto is %NULL, it will
2645  * be removed from @stream.
2646  *
2647  * Returns: %TRUE if @crypto could be updated
2648  */
2649 gboolean
2650 gst_rtsp_stream_update_crypto (GstRTSPStream * stream,
2651     guint ssrc, GstCaps * crypto)
2652 {
2653   GstRTSPStreamPrivate *priv;
2654
2655   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2656   g_return_val_if_fail (crypto == NULL || GST_IS_CAPS (crypto), FALSE);
2657
2658   priv = stream->priv;
2659
2660   GST_DEBUG_OBJECT (stream, "update key for %08x", ssrc);
2661
2662   g_mutex_lock (&priv->lock);
2663   if (crypto)
2664     g_hash_table_insert (priv->keys, GINT_TO_POINTER (ssrc),
2665         gst_caps_ref (crypto));
2666   else
2667     g_hash_table_remove (priv->keys, GINT_TO_POINTER (ssrc));
2668   g_mutex_unlock (&priv->lock);
2669
2670   return TRUE;
2671 }
2672
2673 /**
2674  * gst_rtsp_stream_get_rtp_socket:
2675  * @stream: a #GstRTSPStream
2676  * @family: the socket family
2677  *
2678  * Get the RTP socket from @stream for a @family.
2679  *
2680  * @stream must be joined to a bin.
2681  *
2682  * Returns: (transfer full) (nullable): the RTP socket or %NULL if no
2683  * socket could be allocated for @family. Unref after usage
2684  */
2685 GSocket *
2686 gst_rtsp_stream_get_rtp_socket (GstRTSPStream * stream, GSocketFamily family)
2687 {
2688   GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2689   GSocket *socket;
2690   const gchar *name;
2691
2692   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2693   g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2694       family == G_SOCKET_FAMILY_IPV6, NULL);
2695   g_return_val_if_fail (priv->udpsink[0], NULL);
2696
2697   if (family == G_SOCKET_FAMILY_IPV6)
2698     name = "socket-v6";
2699   else
2700     name = "socket";
2701
2702   g_object_get (priv->udpsink[0], name, &socket, NULL);
2703
2704   return socket;
2705 }
2706
2707 /**
2708  * gst_rtsp_stream_get_rtcp_socket:
2709  * @stream: a #GstRTSPStream
2710  * @family: the socket family
2711  *
2712  * Get the RTCP socket from @stream for a @family.
2713  *
2714  * @stream must be joined to a bin.
2715  *
2716  * Returns: (transfer full) (nullable): the RTCP socket or %NULL if no
2717  * socket could be allocated for @family. Unref after usage
2718  */
2719 GSocket *
2720 gst_rtsp_stream_get_rtcp_socket (GstRTSPStream * stream, GSocketFamily family)
2721 {
2722   GstRTSPStreamPrivate *priv = GST_RTSP_STREAM_GET_PRIVATE (stream);
2723   GSocket *socket;
2724   const gchar *name;
2725
2726   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2727   g_return_val_if_fail (family == G_SOCKET_FAMILY_IPV4 ||
2728       family == G_SOCKET_FAMILY_IPV6, NULL);
2729   g_return_val_if_fail (priv->udpsink[1], NULL);
2730
2731   if (family == G_SOCKET_FAMILY_IPV6)
2732     name = "socket-v6";
2733   else
2734     name = "socket";
2735
2736   g_object_get (priv->udpsink[1], name, &socket, NULL);
2737
2738   return socket;
2739 }
2740
2741 /**
2742  * gst_rtsp_stream_set_seqnum:
2743  * @stream: a #GstRTSPStream
2744  * @seqnum: a new sequence number
2745  *
2746  * Configure the sequence number in the payloader of @stream to @seqnum.
2747  */
2748 void
2749 gst_rtsp_stream_set_seqnum_offset (GstRTSPStream * stream, guint16 seqnum)
2750 {
2751   GstRTSPStreamPrivate *priv;
2752
2753   g_return_if_fail (GST_IS_RTSP_STREAM (stream));
2754
2755   priv = stream->priv;
2756
2757   g_object_set (G_OBJECT (priv->payloader), "seqnum-offset", seqnum, NULL);
2758 }
2759
2760 /**
2761  * gst_rtsp_stream_get_seqnum:
2762  * @stream: a #GstRTSPStream
2763  *
2764  * Get the configured sequence number in the payloader of @stream.
2765  *
2766  * Returns: the sequence number of the payloader.
2767  */
2768 guint16
2769 gst_rtsp_stream_get_current_seqnum (GstRTSPStream * stream)
2770 {
2771   GstRTSPStreamPrivate *priv;
2772   guint seqnum;
2773
2774   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), 0);
2775
2776   priv = stream->priv;
2777
2778   g_object_get (G_OBJECT (priv->payloader), "seqnum", &seqnum, NULL);
2779
2780   return seqnum;
2781 }
2782
2783 /**
2784  * gst_rtsp_stream_transport_filter:
2785  * @stream: a #GstRTSPStream
2786  * @func: (scope call) (allow-none): a callback
2787  * @user_data: (closure): user data passed to @func
2788  *
2789  * Call @func for each transport managed by @stream. The result value of @func
2790  * determines what happens to the transport. @func will be called with @stream
2791  * locked so no further actions on @stream can be performed from @func.
2792  *
2793  * If @func returns #GST_RTSP_FILTER_REMOVE, the transport will be removed from
2794  * @stream.
2795  *
2796  * If @func returns #GST_RTSP_FILTER_KEEP, the transport will remain in @stream.
2797  *
2798  * If @func returns #GST_RTSP_FILTER_REF, the transport will remain in @stream but
2799  * will also be added with an additional ref to the result #GList of this
2800  * function..
2801  *
2802  * When @func is %NULL, #GST_RTSP_FILTER_REF will be assumed for each transport.
2803  *
2804  * Returns: (element-type GstRTSPStreamTransport) (transfer full): a #GList with all
2805  * transports for which @func returned #GST_RTSP_FILTER_REF. After usage, each
2806  * element in the #GList should be unreffed before the list is freed.
2807  */
2808 GList *
2809 gst_rtsp_stream_transport_filter (GstRTSPStream * stream,
2810     GstRTSPStreamTransportFilterFunc func, gpointer user_data)
2811 {
2812   GstRTSPStreamPrivate *priv;
2813   GList *result, *walk, *next;
2814   GHashTable *visited;
2815   guint cookie;
2816
2817   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), NULL);
2818
2819   priv = stream->priv;
2820
2821   result = NULL;
2822   if (func)
2823     visited = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
2824
2825   g_mutex_lock (&priv->lock);
2826 restart:
2827   cookie = priv->transports_cookie;
2828   for (walk = priv->transports; walk; walk = next) {
2829     GstRTSPStreamTransport *trans = walk->data;
2830     GstRTSPFilterResult res;
2831     gboolean changed;
2832
2833     next = g_list_next (walk);
2834
2835     if (func) {
2836       /* only visit each transport once */
2837       if (g_hash_table_contains (visited, trans))
2838         continue;
2839
2840       g_hash_table_add (visited, g_object_ref (trans));
2841       g_mutex_unlock (&priv->lock);
2842
2843       res = func (stream, trans, user_data);
2844
2845       g_mutex_lock (&priv->lock);
2846     } else
2847       res = GST_RTSP_FILTER_REF;
2848
2849     changed = (cookie != priv->transports_cookie);
2850
2851     switch (res) {
2852       case GST_RTSP_FILTER_REMOVE:
2853         update_transport (stream, trans, FALSE);
2854         break;
2855       case GST_RTSP_FILTER_REF:
2856         result = g_list_prepend (result, g_object_ref (trans));
2857         break;
2858       case GST_RTSP_FILTER_KEEP:
2859       default:
2860         break;
2861     }
2862     if (changed)
2863       goto restart;
2864   }
2865   g_mutex_unlock (&priv->lock);
2866
2867   if (func)
2868     g_hash_table_unref (visited);
2869
2870   return result;
2871 }
2872
2873 static GstPadProbeReturn
2874 pad_blocking (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2875 {
2876   GstRTSPStreamPrivate *priv;
2877   GstRTSPStream *stream;
2878
2879   stream = user_data;
2880   priv = stream->priv;
2881
2882   GST_DEBUG_OBJECT (pad, "now blocking");
2883
2884   g_mutex_lock (&priv->lock);
2885   priv->blocking = TRUE;
2886   g_mutex_unlock (&priv->lock);
2887
2888   gst_element_post_message (priv->payloader,
2889       gst_message_new_element (GST_OBJECT_CAST (priv->payloader),
2890           gst_structure_new_empty ("GstRTSPStreamBlocking")));
2891
2892   return GST_PAD_PROBE_OK;
2893 }
2894
2895 /**
2896  * gst_rtsp_stream_set_blocked:
2897  * @stream: a #GstRTSPStream
2898  * @blocked: boolean indicating we should block or unblock
2899  *
2900  * Blocks or unblocks the dataflow on @stream.
2901  *
2902  * Returns: %TRUE on success
2903  */
2904 gboolean
2905 gst_rtsp_stream_set_blocked (GstRTSPStream * stream, gboolean blocked)
2906 {
2907   GstRTSPStreamPrivate *priv;
2908
2909   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2910
2911   priv = stream->priv;
2912
2913   g_mutex_lock (&priv->lock);
2914   if (blocked) {
2915     priv->blocking = FALSE;
2916     if (priv->blocked_id == 0) {
2917       priv->blocked_id = gst_pad_add_probe (priv->srcpad,
2918           GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
2919           GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocking,
2920           g_object_ref (stream), g_object_unref);
2921     }
2922   } else {
2923     if (priv->blocked_id != 0) {
2924       gst_pad_remove_probe (priv->srcpad, priv->blocked_id);
2925       priv->blocked_id = 0;
2926       priv->blocking = FALSE;
2927     }
2928   }
2929   g_mutex_unlock (&priv->lock);
2930
2931   return TRUE;
2932 }
2933
2934 /**
2935  * gst_rtsp_stream_is_blocking:
2936  * @stream: a #GstRTSPStream
2937  *
2938  * Check if @stream is blocking on a #GstBuffer.
2939  *
2940  * Returns: %TRUE if @stream is blocking
2941  */
2942 gboolean
2943 gst_rtsp_stream_is_blocking (GstRTSPStream * stream)
2944 {
2945   GstRTSPStreamPrivate *priv;
2946   gboolean result;
2947
2948   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2949
2950   priv = stream->priv;
2951
2952   g_mutex_lock (&priv->lock);
2953   result = priv->blocking;
2954   g_mutex_unlock (&priv->lock);
2955
2956   return result;
2957 }
2958
2959 /**
2960  * gst_rtsp_stream_query_position:
2961  * @stream: a #GstRTSPStream
2962  *
2963  * Query the position of the stream in %GST_FORMAT_TIME. This only considers
2964  * the RTP parts of the pipeline and not the RTCP parts.
2965  *
2966  * Returns: %TRUE if the position could be queried
2967  */
2968 gboolean
2969 gst_rtsp_stream_query_position (GstRTSPStream * stream, gint64 * position)
2970 {
2971   GstRTSPStreamPrivate *priv;
2972   GstElement *sink;
2973   gboolean ret;
2974
2975   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
2976
2977   priv = stream->priv;
2978
2979   g_mutex_lock (&priv->lock);
2980   if ((sink = priv->udpsink[0]))
2981     gst_object_ref (sink);
2982   g_mutex_unlock (&priv->lock);
2983
2984   if (!sink)
2985     return FALSE;
2986
2987   ret = gst_element_query_position (sink, GST_FORMAT_TIME, position);
2988   gst_object_unref (sink);
2989
2990   return ret;
2991 }
2992
2993 /**
2994  * gst_rtsp_stream_query_stop:
2995  * @stream: a #GstRTSPStream
2996  *
2997  * Query the stop of the stream in %GST_FORMAT_TIME. This only considers
2998  * the RTP parts of the pipeline and not the RTCP parts.
2999  *
3000  * Returns: %TRUE if the stop could be queried
3001  */
3002 gboolean
3003 gst_rtsp_stream_query_stop (GstRTSPStream * stream, gint64 * stop)
3004 {
3005   GstRTSPStreamPrivate *priv;
3006   GstElement *sink;
3007   GstQuery *query;
3008   gboolean ret;
3009
3010   g_return_val_if_fail (GST_IS_RTSP_STREAM (stream), FALSE);
3011
3012   priv = stream->priv;
3013
3014   g_mutex_lock (&priv->lock);
3015   if ((sink = priv->udpsink[0]))
3016     gst_object_ref (sink);
3017   g_mutex_unlock (&priv->lock);
3018
3019   if (!sink)
3020     return FALSE;
3021
3022   query = gst_query_new_segment (GST_FORMAT_TIME);
3023   if ((ret = gst_element_query (sink, query))) {
3024     GstFormat format;
3025
3026     gst_query_parse_segment (query, NULL, &format, NULL, stop);
3027     if (format != GST_FORMAT_TIME)
3028       *stop = -1;
3029   }
3030   gst_query_unref (query);
3031   gst_object_unref (sink);
3032
3033   return ret;
3034
3035 }